spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

fix(agent-context): support multiple context files safely

Open AustinZ21 opened this issue 5 months ago • 3 comments

Summary

This PR lets the bundled agent-context extension manage more than one coding-agent context file from the same Spec Kit run.

The motivating workflow is a repo that uses Claude Code and Codex interchangeably and wants Spec Kit's generated context block synced to both CLAUDE.md and AGENTS.md. The implementation keeps the existing singular context_file behavior, while adding an optional context_files list for projects that intentionally keep multiple agent anchors in sync.

What Changed

  • Added optional context_files support to the agent-context config template.
  • Updated the bash and PowerShell update scripts to refresh every configured context file.
  • Updated Python integration setup/removal paths to upsert and remove the managed block across configured files.
  • Rendered command templates with a readable multi-file context target when context_files is configured.
  • Preserved existing non-empty context_files during integration switches.
  • Added project-root containment validation for context file paths:
    • rejects absolute paths
    • rejects Windows drive paths
    • rejects backslash separators
    • rejects .. path segments
    • confirms the resolved target stays inside the project root
  • Kept specify extension disable agent-context as a full opt-out: disabled projects skip upsert/removal and ignore stale context_files during command rendering.
  • Updated agent-context docs and command help to describe multi-file configuration and safety behavior.

Why

Today Spec Kit stores one managed context block target in context_file. That works well for a single coding agent, but mixed-agent projects can need more than one context anchor. For example, Codex reads AGENTS.md, while Claude-oriented setups often use CLAUDE.md. Without first-class multi-file support, teams have to choose one anchor or maintain duplicate context manually.

This change makes the multi-agent case explicit and configurable without hard-coding any specific agent names or file paths.

Safety Notes

The script and Python integration paths now enforce matching path constraints so a configured context file cannot escape the project root. The disabled-extension path also avoids validating stale config before the opt-out gate, so disabling agent-context remains a complete opt-out even if old config contains invalid paths.

Validation

  • bash -n extensions/agent-context/scripts/bash/update-agent-context.sh
  • python -m compileall -q src\specify_cli
  • pytest tests/extensions/test_extension_agent_context.py -q
    • 50 passed
  • pytest tests/integrations/test_integration_codex.py::TestCodexInitFlow::test_plan_skill_references_configured_context_files tests/integrations/test_integration_codex.py::TestCodexInitFlow::test_plan_skill_ignores_context_files_when_agent_context_disabled -q
    • 2 passed
  • pytest tests/test_agent_config_consistency.py -q
    • 28 passed
  • specify --help
  • Manual PowerShell smoke test:
    • configured context_files: [AGENTS.md, CLAUDE.md]
    • ran update-agent-context.ps1 specs/123-test/plan.md
    • verified both files were updated
  • Manual bash smoke test:
    • configured context_files: [AGENTS.md, CLAUDE.md]
    • ran update-agent-context.sh specs/123-test/plan.md
    • verified both files were updated

Known Existing Test Failure

The broader Codex integration slice still has two inventory failures:

  • TestCodexIntegration.test_complete_file_inventory_sh
  • TestCodexIntegration.test_complete_file_inventory_ps

Both failures reproduce on a clean upstream/main worktree at 1b0556c, before this PR's changes. They expect bundled agent-context extension artifacts in the generated project, but the generated project does not include them. The focused tests added by this PR pass.

AI Assistance Disclosure

I used Codex/ChatGPT to inspect the integration paths, draft the implementation, and generate test coverage. I reviewed the code paths and validated the behavior locally with automated and manual checks.

AustinZ21 avatar Jun 13 '26 18:06 AustinZ21

Hi maintainers, I've marked this PR as ready for review. It looks like the workflows are awaiting approval before the checks can run. Thanks for taking a look.

AustinZ21 avatar Jun 14 '26 21:06 AustinZ21

Addressed the Copilot review feedback in commit 10cbff3.

Summary:

  • Validated context_files / context_file before rendering skill placeholders.
  • Restored a string fallback for disabled agent-context template rendering.
  • Added bash and PowerShell resolved-path containment checks before context-file writes.
  • Added PowerShell backslash rejection for configured context files.
  • Added targeted tests for invalid config paths, backslashes, symlink/junction escape paths, and disabled-extension behavior.

Validation:

  • pytest tests/extensions/test_extension_agent_context.py tests/integrations/test_integration_codex.py::TestCodexInitFlow -q
  • git diff --check
  • bash -n extensions/agent-context/scripts/bash/update-agent-context.sh
  • PowerShell parser syntax check

Posted by @AustinZ21 with fixes prepared using Codex.

AustinZ21 avatar Jun 16 '26 16:06 AustinZ21

Please address Copilot feedback

mnriem avatar Jun 16 '26 22:06 mnriem

Addressed the latest Copilot feedback and the Windows CI failures in commit 9458a6b.

Summary:

  • Preserved __CONTEXT_FILE__ rendering when agent-context is disabled by falling back to the extension config’s singular context_file.
  • Kept disabled-mode behavior from using stale context_files.
  • Updated the PowerShell updater tests so the subprocess uses the same Python environment as pytest, ensuring PyYAML is available in CI.

Validation:

  • pytest tests/extensions/test_extension_agent_context.py tests/integrations/test_integration_codex.py::TestCodexInitFlow -q
  • git diff --check
  • python -m compileall -q src/specify_cli/agents.py tests/extensions/test_extension_agent_context.py

The previous Windows failures were from commit 10cbff3; the fix is now pushed in 9458a6b.

AustinZ21 avatar Jun 16 '26 22:06 AustinZ21

Addressed the latest Copilot review follow-ups in commit e3a1b55.

Summary:

  • Replaced WSL-only /mnt/<drive> path handling in bash-related tests with a helper that prefers cygpath -u for Git-for-Windows/MSYS bash, with a WSL fallback for local runs.
  • Updated Windows symlink setup in the bash updater test to use the same path conversion helper.
  • Corrected the bash updater parser comment to clarify that only context_files is emitted as JSON and markers are plain strings.
  • Cached context_file_display before template loops so agent-context config parsing/validation is not repeated per template.

AustinZ21 avatar Jun 17 '26 16:06 AustinZ21

Please address Copilot feedback

mnriem avatar Jun 19 '26 18:06 mnriem

Addressed the latest Copilot feedback in commit e6dc328.

Summary:

  • Made Python context upsert/remove fall back to the agent-context config's singular context_file before the integration class default.
  • Validated the legacy init-options context_file fallback when agent-context is enabled.
  • Aligned the Bash updater's context_file fallback with the trimmed behavior used for context_files.
  • Updated the agent-context update command docs to describe file(s) and the full path constraints.

Validation:

  • PYTHONPATH=src python -m pytest tests/extensions/test_extension_agent_context.py tests/integrations/test_integration_codex.py -q -> 115 passed, 3 skipped
  • python -m compileall -q src tests/extensions/test_extension_agent_context.py
  • git diff --check

GitHub Actions are still awaiting maintainer approval for forked workflow runs.

Posted on behalf of @AustinZ21 by Codex (GPT-5).

AustinZ21 avatar Jun 19 '26 19:06 AustinZ21

Please address Copilot feedback

mnriem avatar Jun 22 '26 15:06 mnriem

Posted on behalf of @AustinZ21 by Codex (GPT-5).

Pushed follow-up commit 1343799 to address the latest review round. It centralizes context file resolution, de-duplication, validation, and display formatting in IntegrationBase and uses that from CommandRegistrar; adds the PowerShell drive-qualified path guard for C:tmp/out.md; and replaces brittle script source-substring assertions with behavior-level updater coverage while expanding invalid-path coverage.

Local validation:

  • PYTHONPATH=src python -m pytest tests/extensions/test_extension_agent_context.py tests/integrations/test_integration_codex.py tests/integrations/test_integration_claude.py -q (152 passed, 4 skipped)
  • PYTHONPATH=src python -m pytest tests/integrations/test_integration_base_markdown.py tests/integrations/test_integration_base_skills.py tests/integrations/test_integration_base_toml.py tests/integrations/test_integration_base_yaml.py tests/integrations/test_integration_generic.py tests/integrations/test_registry.py -q (1027 passed)
  • PYTHONPATH=src python -m compileall -q src tests/extensions/test_extension_agent_context.py
  • git diff --check

I also ran tests/test_registrar_path_traversal.py with the local Windows symlink-privilege case deselected; the remaining 21 tests passed. The unfiltered symlink case needs a symlink-capable Windows environment.

AustinZ21 avatar Jun 22 '26 16:06 AustinZ21

Thank you!

mnriem avatar Jun 22 '26 17:06 mnriem