Skip to content

Commit 75b4954

Browse files
fix(agent-context): recurse for nested plans in Python mtime fallback
The Python port's mtime fallback discovered plans with a one-level specs/*/plan.md glob, so a scoped layout created via SPECIFY_FEATURE_DIRECTORY (specs/<scope>/<feature>/plan.md) was missed when feature.json is absent — the fallback returned no plan and the managed context section omitted the 'at <plan>' line. The bash and PowerShell twins were already fixed to recurse (github#3024); the Python twin was left behind. Switch to specs.rglob('plan.md') with the same symlink-safe containment check the bash twin uses (resolve each candidate and confirm it stays within the project root before ranking by mtime), so a plan reached through a specs/ symlink pointing outside the project is not selected. Adds parity regression tests (vs bash and vs PowerShell) covering a nested specs/<scope>/<feature>/plan.md; both fail on the pre-fix one-level glob. Fixes github#3733
1 parent afbb2c7 commit 75b4954

2 files changed

Lines changed: 61 additions & 16 deletions

File tree

‎extensions/agent-context/scripts/python/update_agent_context.py‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,31 @@ def _resolve_plan_path(project_root: str) -> str:
173173

174174
if not plan_path:
175175
root = Path(project_root).resolve()
176-
plans = sorted(
177-
(root / "specs").rglob("plan.md"),
178-
key=lambda p: p.stat().st_mtime,
179-
reverse=True,
180-
)
181-
if plans:
176+
specs = root / "specs"
177+
178+
def _resolved_rel(p: Path) -> Path | None:
179+
# Resolve symlinks before checking containment: relative_to() is
180+
# lexical and would otherwise accept a plan reached through a specs/
181+
# symlink that points outside the project, emitting an
182+
# in-project-looking path for an out-of-project file (or picking it
183+
# as "most recent").
182184
try:
183-
plan_path = plans[0].relative_to(root).as_posix()
184-
except ValueError:
185-
plan_path = ""
185+
return p.resolve().relative_to(root)
186+
except (OSError, ValueError):
187+
return None
188+
189+
# Recurse (rather than the old one-level specs/*/plan.md glob) so scoped
190+
# layouts created via SPECIFY_FEATURE_DIRECTORY, e.g.
191+
# specs/<scope>/<feature>/plan.md, are still discovered when
192+
# feature.json is absent (#3024). Mirrors the bash and PowerShell twins.
193+
candidates = []
194+
for p in specs.rglob("plan.md"):
195+
rel = _resolved_rel(p)
196+
if rel is not None:
197+
candidates.append((p, rel))
198+
candidates.sort(key=lambda pr: pr[0].stat().st_mtime, reverse=True)
199+
if candidates:
200+
plan_path = candidates[0][1].as_posix()
186201
return plan_path
187202

188203

‎tests/extensions/test_update_agent_context_python_parity.py‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,19 @@ def test_python_mtime_fallback_matching_bash(tmp_path: Path) -> None:
344344

345345

346346
@requires_posix_bash
347-
def test_python_mtime_fallback_finds_nested_plan_matching_bash(tmp_path: Path) -> None:
348-
# Regression: the mtime fallback must discover plan.md in nested scoped
349-
# layouts (specs/<scope>/<feature>/plan.md), matching the Bash/PowerShell
350-
# ports and the documented recursive-discovery contract (see #3024). A
351-
# one-level scan (specs/*/plan.md) would miss this and omit the plan link.
347+
def test_python_mtime_fallback_finds_nested_plan_matching_bash(
348+
tmp_path: Path,
349+
) -> None:
350+
"""The mtime fallback must recurse into scoped layouts.
351+
352+
A plan created under specs/<scope>/<feature>/plan.md (as produced via
353+
SPECIFY_FEATURE_DIRECTORY) is more than one level below specs/. The old
354+
Python port used a one-level specs/*/plan.md glob and missed it, while the
355+
bash/PowerShell twins recurse (#3024). This locks in the parity.
356+
"""
352357
repo_a, repo_b = twin_projects(tmp_path, context_file="AGENTS.md")
353358
for repo in (repo_a, repo_b):
354-
plan = repo / "specs" / "scope-a" / "002-nested" / "plan.md"
359+
plan = repo / "specs" / "backend" / "001-nested" / "plan.md"
355360
plan.parent.mkdir(parents=True, exist_ok=True)
356361
plan.write_text("# plan\n", encoding="utf-8")
357362

@@ -361,7 +366,7 @@ def test_python_mtime_fallback_finds_nested_plan_matching_bash(tmp_path: Path) -
361366
assert_parity(bash, py, repo_a, repo_b)
362367
content = (repo_b / "AGENTS.md").read_bytes()
363368
assert content == (repo_a / "AGENTS.md").read_bytes()
364-
assert b"at specs/scope-a/002-nested/plan.md" in content
369+
assert b"at specs/backend/001-nested/plan.md" in content
365370

366371

367372
@requires_posix_bash
@@ -508,6 +513,31 @@ def test_python_fresh_context_file_matches_powershell(tmp_path: Path) -> None:
508513
assert (repo_a / "AGENTS.md").read_bytes() == (repo_b / "AGENTS.md").read_bytes()
509514

510515

516+
@pytest.mark.skipif(not POWERSHELL, reason="no PowerShell available")
517+
def test_python_mtime_fallback_finds_nested_plan_matches_powershell(
518+
tmp_path: Path,
519+
) -> None:
520+
"""Python's mtime fallback must recurse like the PowerShell twin.
521+
522+
With no feature.json, discovery falls back to scanning under specs/. A plan
523+
at specs/<scope>/<feature>/plan.md sits more than one level deep; the old
524+
Python one-level glob missed it while PowerShell already recurses (#3024).
525+
"""
526+
repo_a = make_project(tmp_path / "proj-ps", context_file="AGENTS.md")
527+
repo_b = make_project(tmp_path / "proj-py", context_file="AGENTS.md")
528+
for repo in (repo_a, repo_b):
529+
plan = repo / "specs" / "backend" / "001-nested" / "plan.md"
530+
plan.parent.mkdir(parents=True, exist_ok=True)
531+
plan.write_text("# plan\n", encoding="utf-8")
532+
533+
ps = run_powershell(repo_a)
534+
py = run_python(repo_b)
535+
536+
assert ps.returncode == py.returncode == 0, ps.stderr + py.stderr
537+
assert (repo_a / "AGENTS.md").read_bytes() == (repo_b / "AGENTS.md").read_bytes()
538+
assert b"at specs/backend/001-nested/plan.md" in (repo_b / "AGENTS.md").read_bytes()
539+
540+
511541
@pytest.mark.skipif(not POWERSHELL, reason="no PowerShell available")
512542
def test_python_upsert_matches_powershell(tmp_path: Path) -> None:
513543
repo_a = make_project(tmp_path / "proj-ps", context_file="AGENTS.md")

0 commit comments

Comments
 (0)