Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extensions/agent-context/extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ schema_version: "1.0"
extension:
id: agent-context
name: "Coding Agent Context"
version: "1.0.3"
version: "1.0.4"
description: "Manages coding agent context/instruction files (e.g., CLAUDE.md, copilot-instructions.md) with project-specific plan references and configurable markers"
author: spec-kit-core
repository: https://github.com/github/spec-kit
Expand Down
38 changes: 29 additions & 9 deletions extensions/agent-context/scripts/bash/update-agent-context.sh
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,38 @@ def ensure_mdc_frontmatter(content):
opening, fm_text, closing, sep, rest = match.groups()
newline = "\r\n" if "\r\n" in opening else "\n"

if re.search(r"(?m)^[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?$", fm_text):
return content

if re.search(r"(?m)^[ \t]*alwaysApply[ \t]*:", fm_text):
fm_text = re.sub(
r"(?m)^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?)$",
lines = fm_text.split("\n")
visible = []
block = None
for n, line in enumerate(lines):
depth = len(line) - len(line.lstrip(" \t"))
if block is not None and (not line.strip(" \t\r") or depth > block):
continue
code = re.sub(r"(?:^|[ \t])#.*", "", line.rstrip("\r"))
indicator = r"(?:(?:^|[ \t])(?:[-?]|[&!][^ \t]*)|:)[ \t]+[|>][-+0-9]*[ \t]*$"
block = depth if re.search(indicator, code) else None
visible.append(n)

keys = [n for n in visible if re.match(r"[ \t]*alwaysApply[ \t]*:", lines[n])]
for n in keys:
if re.match(r"[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?\r?$", lines[n]):
return content

if keys:
lines[keys[0]] = re.sub(
r"^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?\r?)$",
r"\1alwaysApply: true\2",
fm_text,
count=1,
lines[keys[0]],
)
fm_text = "\n".join(lines)
elif fm_text.strip():
fm_text = fm_text + newline + "alwaysApply: true"
depths = [
len(lines[n]) - len(lines[n].lstrip(" "))
for n in visible
if lines[n].strip(" \t\r") and not lines[n].lstrip(" \t").startswith("#")
]
indent = " " * min(depths, default=0)
fm_text = fm_text + newline + indent + "alwaysApply: true"
else:
fm_text = "alwaysApply: true"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,44 @@ function Add-MdcFrontmatter {
$rest = $fm.Groups[5].Value
$newline = if ($opening.Contains("`r`n")) { "`r`n" } else { "`n" }

if ([regex]::IsMatch($fmText, '(?m)^[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?$')) {
return $Content
$lines = $fmText -split "`n"
$visible = New-Object System.Collections.Generic.List[int]
$block = -1
for ($n = 0; $n -lt $lines.Length; $n++) {
$line = $lines[$n]
$depth = $line.Length - $line.TrimStart([char[]]" `t").Length
if ($block -ge 0 -and ($line.Trim([char[]]" `t`r").Length -eq 0 -or $depth -gt $block)) {
continue
}
$code = [regex]::Replace($line.TrimEnd([char[]]"`r"), '(?:^|[ \t])#.*', '')
$block = if ([regex]::IsMatch($code, '(?:(?:^|[ \t])(?:[-?]|[&!][^ \t]*)|:)[ \t]+[|>][-+0-9]*[ \t]*$')) { $depth } else { -1 }
$visible.Add($n)
}

$keys = @($visible | Where-Object { [regex]::IsMatch($lines[$_], '^[ \t]*alwaysApply[ \t]*:') })
foreach ($n in $keys) {
if ([regex]::IsMatch($lines[$n], '^[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?\r?$')) {
return $Content
}
}

if ([regex]::IsMatch($fmText, '(?m)^[ \t]*alwaysApply[ \t]*:')) {
$alwaysApplyRegex = [regex]'(?m)^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?)$'
$fmText = $alwaysApplyRegex.Replace($fmText, '${1}alwaysApply: true${2}', 1)
if ($keys.Count -gt 0) {
$lines[$keys[0]] = [regex]::Replace($lines[$keys[0]], '^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?\r?)$', '${1}alwaysApply: true${2}')
$fmText = [string]::Join("`n", $lines)
} elseif ($fmText.Trim()) {
$fmText = $fmText + $newline + 'alwaysApply: true'
$minDepth = -1
foreach ($n in $visible) {
$line = $lines[$n]
if ($line.Trim([char[]]" `t`r").Length -eq 0 -or $line.TrimStart([char[]]" `t").StartsWith('#')) {
continue
}
$depth = $line.Length - $line.TrimStart([char[]]" ").Length
if ($minDepth -lt 0 -or $depth -lt $minDepth) {
$minDepth = $depth
}
}
$indent = ' ' * [Math]::Max($minDepth, 0)
$fmText = $fmText + $newline + $indent + 'alwaysApply: true'
} else {
$fmText = 'alwaysApply: true'
}
Expand Down
38 changes: 29 additions & 9 deletions extensions/agent-context/scripts/python/update_agent_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,38 @@ def ensure_mdc_frontmatter(content: str) -> str:
opening, fm_text, closing, sep, rest = match.groups()
newline = "\r\n" if "\r\n" in opening else "\n"

if re.search(r"(?m)^[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?$", fm_text):
return content

if re.search(r"(?m)^[ \t]*alwaysApply[ \t]*:", fm_text):
fm_text = re.sub(
r"(?m)^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?)$",
lines = fm_text.split("\n")
visible = []
block = None
for n, line in enumerate(lines):
depth = len(line) - len(line.lstrip(" \t"))
if block is not None and (not line.strip(" \t\r") or depth > block):
continue
code = re.sub(r"(?:^|[ \t])#.*", "", line.rstrip("\r"))
indicator = r"(?:(?:^|[ \t])(?:[-?]|[&!][^ \t]*)|:)[ \t]+[|>][-+0-9]*[ \t]*$"
block = depth if re.search(indicator, code) else None
Comment on lines +257 to +258
Comment on lines +256 to +258
visible.append(n)

keys = [n for n in visible if re.match(r"[ \t]*alwaysApply[ \t]*:", lines[n])]
for n in keys:
if re.match(r"[ \t]*alwaysApply[ \t]*:[ \t]*true[ \t]*(?:#.*)?\r?$", lines[n]):
return content

if keys:
lines[keys[0]] = re.sub(
r"^([ \t]*)alwaysApply[ \t]*:.*?([ \t]*(?:#.*)?\r?)$",
r"\1alwaysApply: true\2",
fm_text,
count=1,
lines[keys[0]],
)
fm_text = "\n".join(lines)
elif fm_text.strip():
fm_text = fm_text + newline + "alwaysApply: true"
depths = [
len(lines[n]) - len(lines[n].lstrip(" "))
for n in visible
if lines[n].strip(" \t\r") and not lines[n].lstrip(" \t").startswith("#")
]
indent = " " * min(depths, default=0)
fm_text = fm_text + newline + indent + "alwaysApply: true"
else:
fm_text = "alwaysApply: true"

Expand Down
2 changes: 1 addition & 1 deletion extensions/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"agent-context": {
"name": "Coding Agent Context",
"id": "agent-context",
"version": "1.0.3",
"version": "1.0.4",
"description": "Manages coding agent context/instruction files (e.g., CLAUDE.md, copilot-instructions.md) with project-specific plan references and configurable markers",
"author": "spec-kit-core",
"repository": "https://github.com/github/spec-kit",
Expand Down
127 changes: 127 additions & 0 deletions tests/extensions/test_extension_agent_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,91 @@ def test_bash_script_nothing_to_do_without_integration(self, tmp_path):

_MDC_CONTEXT_FILE = ".cursor/rules/specify-rules.mdc"

_MDC_INDENTED_TRUE_ONLY = (
"---\ndescription: |\n Cursor reads this key:\n alwaysApply: true\n---\n\nUser notes\n"
)
_MDC_INDENTED_TRUE_ONLY_REPAIRED = (
"---\ndescription: |\n Cursor reads this key:\n alwaysApply: true\n"
"alwaysApply: true\n---\n"
)
_MDC_INDENTED_AND_TOP_LEVEL_FALSE = (
"---\ndescription: |\n Disabled example:\n alwaysApply: false\n"
"alwaysApply: false\n---\n\nUser notes\n"
)
_MDC_INDENTED_AND_TOP_LEVEL_FALSE_REPAIRED = (
"---\ndescription: |\n Disabled example:\n alwaysApply: false\n"
"alwaysApply: true\n---\n"
)
_MDC_ROOT_KEY_CASES = [
pytest.param(
_MDC_INDENTED_TRUE_ONLY,
_MDC_INDENTED_TRUE_ONLY_REPAIRED,
id="indented-true-only",
),
pytest.param(
_MDC_INDENTED_AND_TOP_LEVEL_FALSE,
_MDC_INDENTED_AND_TOP_LEVEL_FALSE_REPAIRED,
id="indented-and-top-level-false",
),
pytest.param(
"---\n description: Rules\n alwaysApply: true\n---\n\nUser notes\n",
"---\n description: Rules\n alwaysApply: true\n---\n",
id="indented-root-true",
),
pytest.param(
"---\n description: Rules\n alwaysApply: false\n---\n\nUser notes\n",
"---\n description: Rules\n alwaysApply: true\n---\n",
id="indented-root-false",
),
pytest.param(
"---\n description: Rules\n---\n\nUser notes\n",
"---\n description: Rules\n alwaysApply: true\n---\n",
id="indented-root-missing",
),
pytest.param(
"---\n description: |\n Cursor reads this key:\n alwaysApply: true\n"
"---\n\nUser notes\n",
"---\n description: |\n Cursor reads this key:\n alwaysApply: true\n"
" alwaysApply: true\n---\n",
id="indented-root-and-block-scalar",
),
pytest.param(
"---\n&rules\n description: Rules\n alwaysApply: false\n---\n\nUser notes\n",
"---\n&rules\n description: Rules\n alwaysApply: true\n---\n",
id="anchored-root-false",
),
pytest.param(
"---\n!!map\n description: Rules\n alwaysApply: true\n---\n\nUser notes\n",
"---\n!!map\n description: Rules\n alwaysApply: true\n---\n",
id="tagged-root-true",
),
pytest.param(
"---\n&description description: |\n Rules\nalwaysApply: false\n---\n\nUser notes\n",
"---\n&description description: |\n Rules\nalwaysApply: true\n---\n",
id="anchored-first-key-false",
),
pytest.param(
"---\n!!str description: |\n Rules\nalwaysApply: false\n---\n\nUser notes\n",
"---\n!!str description: |\n Rules\nalwaysApply: true\n---\n",
id="tagged-first-key-false",
),
pytest.param(
"---\n# example |\n description: Rules\n alwaysApply: false\n---\n\nUser notes\n",
"---\n# example |\n description: Rules\n alwaysApply: true\n---\n",
id="comment-with-indicator-false",
),
pytest.param(
'---\n? "custom\n key: value"\n: Rules\n---\n\nUser notes\n',
'---\n? "custom\n key: value"\n: Rules\nalwaysApply: true\n---\n',
id="multiline-quoted-key-missing",
),
pytest.param(
'---\n{\n description: "foo | #baz",\n alwaysApply: false\n}\n---\n\nUser notes\n',
'---\n{\n description: "foo | #baz",\n alwaysApply: true\n}\n---\n',
id="flow-mapping-quoted-indicator-false",
),
]


class TestPlanDiscovery:
"""Mtime fallback must find plans in nested spec layouts (#3024).
Expand Down Expand Up @@ -855,6 +940,27 @@ def test_bash_script_repairs_existing_mdc_frontmatter(self, tmp_path):
assert "description: My rules" in text
assert "User notes" in text

@requires_bash
@pytest.mark.parametrize(
("existing", "expected_frontmatter"), _MDC_ROOT_KEY_CASES
)
def test_bash_script_repairs_only_root_always_apply(
self, tmp_path, existing, expected_frontmatter
):
project = tmp_path / "project"
project.mkdir()
_install_agent_context_config(project, context_file=_MDC_CONTEXT_FILE)
target = project / _MDC_CONTEXT_FILE
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(existing, encoding="utf-8")

result = _run_bash_agent_context_script(project)

assert result.returncode == 0, result.stderr + result.stdout
text = target.read_text(encoding="utf-8")
assert text.startswith(expected_frontmatter)
assert "User notes" in text

@requires_bash
def test_bash_script_skips_frontmatter_for_non_mdc(self, tmp_path):
project = tmp_path / "project"
Expand Down Expand Up @@ -902,6 +1008,27 @@ def test_powershell_script_repairs_existing_mdc_frontmatter(self, tmp_path):
assert "description: My rules" in text
assert "User notes" in text

@pytest.mark.skipif(POWERSHELL is None, reason="PowerShell not available")
@pytest.mark.parametrize(
("existing", "expected_frontmatter"), _MDC_ROOT_KEY_CASES
)
def test_powershell_script_repairs_only_root_always_apply(
self, tmp_path, existing, expected_frontmatter
):
project = tmp_path / "project"
project.mkdir()
_install_agent_context_config(project, context_file=_MDC_CONTEXT_FILE)
target = project / _MDC_CONTEXT_FILE
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(existing, encoding="utf-8")

result = _run_powershell_agent_context_script(project)

assert result.returncode == 0, result.stderr + result.stdout
text = target.read_text(encoding="utf-8")
assert text.startswith(expected_frontmatter)
assert "User notes" in text

@pytest.mark.skipif(POWERSHELL is None, reason="PowerShell not available")
def test_powershell_script_skips_frontmatter_for_non_mdc(self, tmp_path):
project = tmp_path / "project"
Expand Down
Loading