diff --git a/extensions/agent-context/extension.yml b/extensions/agent-context/extension.yml index 8c5c189757..055f0c5754 100644 --- a/extensions/agent-context/extension.yml +++ b/extensions/agent-context/extension.yml @@ -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 diff --git a/extensions/agent-context/scripts/bash/update-agent-context.sh b/extensions/agent-context/scripts/bash/update-agent-context.sh index 0c0b67e93b..819a525232 100755 --- a/extensions/agent-context/scripts/bash/update-agent-context.sh +++ b/extensions/agent-context/scripts/bash/update-agent-context.sh @@ -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" diff --git a/extensions/agent-context/scripts/powershell/update-agent-context.ps1 b/extensions/agent-context/scripts/powershell/update-agent-context.ps1 index 91d067cc41..a2719c7741 100644 --- a/extensions/agent-context/scripts/powershell/update-agent-context.ps1 +++ b/extensions/agent-context/scripts/powershell/update-agent-context.ps1 @@ -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' } diff --git a/extensions/agent-context/scripts/python/update_agent_context.py b/extensions/agent-context/scripts/python/update_agent_context.py index 090d743bbc..a00c843f9f 100644 --- a/extensions/agent-context/scripts/python/update_agent_context.py +++ b/extensions/agent-context/scripts/python/update_agent_context.py @@ -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 + 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" diff --git a/extensions/catalog.json b/extensions/catalog.json index 4f15711235..d011fe6588 100644 --- a/extensions/catalog.json +++ b/extensions/catalog.json @@ -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", diff --git a/tests/extensions/test_extension_agent_context.py b/tests/extensions/test_extension_agent_context.py index c881cab31c..be52c5d87d 100644 --- a/tests/extensions/test_extension_agent_context.py +++ b/tests/extensions/test_extension_agent_context.py @@ -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). @@ -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" @@ -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" diff --git a/tests/extensions/test_update_agent_context_python_parity.py b/tests/extensions/test_update_agent_context_python_parity.py index fe21dc4c11..2e7ff1bd7a 100644 --- a/tests/extensions/test_update_agent_context_python_parity.py +++ b/tests/extensions/test_update_agent_context_python_parity.py @@ -288,6 +288,104 @@ def test_python_mdc_frontmatter_repair_matching_bash(tmp_path: Path) -> None: assert "alwaysApply: true" in content, name +@requires_posix_bash +@pytest.mark.parametrize( + ("existing", "expected_frontmatter"), + [ + ( + "---\ndescription: |\n Cursor reads this key:\n alwaysApply: true\n---\n\n# Rules\n", + "---\ndescription: |\n Cursor reads this key:\n alwaysApply: true\n" + "alwaysApply: true\n---\n", + ), + ( + "---\ndescription: |\n Disabled example:\n alwaysApply: false\n" + "alwaysApply: false\n---\n\n# Rules\n", + "---\ndescription: |\n Disabled example:\n alwaysApply: false\n" + "alwaysApply: true\n---\n", + ), + ( + "---\n description: Rules\n alwaysApply: true\n---\n\n# Rules\n", + "---\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + "---\n description: Rules\n alwaysApply: false\n---\n\n# Rules\n", + "---\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + "---\n description: Rules\n---\n\n# Rules\n", + "---\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + "---\n description: |\n Cursor reads this key:\n alwaysApply: true\n" + "---\n\n# Rules\n", + "---\n description: |\n Cursor reads this key:\n alwaysApply: true\n" + " alwaysApply: true\n---\n", + ), + ( + "---\n&rules\n description: Rules\n alwaysApply: false\n---\n\n# Rules\n", + "---\n&rules\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + "---\n!!map\n description: Rules\n alwaysApply: true\n---\n\n# Rules\n", + "---\n!!map\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + "---\n&description description: |\n Rules\nalwaysApply: false\n---\n\n# Rules\n", + "---\n&description description: |\n Rules\nalwaysApply: true\n---\n", + ), + ( + "---\n!!str description: |\n Rules\nalwaysApply: false\n---\n\n# Rules\n", + "---\n!!str description: |\n Rules\nalwaysApply: true\n---\n", + ), + ( + "---\n# example |\n description: Rules\n alwaysApply: false\n---\n\n# Rules\n", + "---\n# example |\n description: Rules\n alwaysApply: true\n---\n", + ), + ( + '---\n? "custom\n key: value"\n: Rules\n---\n\n# Rules\n', + '---\n? "custom\n key: value"\n: Rules\nalwaysApply: true\n---\n', + ), + ( + '---\n{\n description: "foo | #baz",\n alwaysApply: false\n}\n---\n\n# Rules\n', + '---\n{\n description: "foo | #baz",\n alwaysApply: true\n}\n---\n', + ), + ], + ids=[ + "indented-true-only", + "indented-and-top-level-false", + "indented-root-true", + "indented-root-false", + "indented-root-missing", + "indented-root-and-block-scalar", + "anchored-root-false", + "tagged-root-true", + "anchored-first-key-false", + "tagged-first-key-false", + "comment-with-indicator-false", + "multiline-quoted-key-missing", + "flow-mapping-quoted-indicator-false", + ], +) +def test_python_mdc_frontmatter_repairs_only_root_key_matching_bash( + tmp_path: Path, existing: str, expected_frontmatter: str +) -> None: + mdc = ".cursor/rules/specify-rules.mdc" + repo_a, repo_b = twin_projects(tmp_path, context_file=mdc) + for repo in (repo_a, repo_b): + add_plan(repo) + target = repo / mdc + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(existing, encoding="utf-8") + + bash = run_bash(repo_a) + py = run_python(repo_b) + + assert_parity(bash, py, repo_a, repo_b) + content = (repo_b / mdc).read_text(encoding="utf-8") + assert content == (repo_a / mdc).read_text(encoding="utf-8") + assert content.startswith(expected_frontmatter) + + # ── Plan-path resolution ─────────────────────────────────────────────────────