-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Extract agent context updates into bundled agent-context extension #2546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
20
commits into
main
Choose a base branch
from
copilot/add-agent-context-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1d6c160
Initial plan
Copilot cca1825
Extract agent context updates into bundled agent-context extension
Copilot d79fd51
Potential fix for pull request finding 'Unused import'
mnriem 8e0d40e
Potential fix for pull request finding 'Unused import'
mnriem 8512915
fix: address review comments on agent-context extension
Copilot 7bc560e
fix: gate context_markers in _update_init_options_for_integration on …
Copilot 1759d8d
fix: move context_file/context_markers from init-options.json to agen…
Copilot c7c9812
Potential fix for pull request finding 'Unused global variable'
mnriem 2e822be
fix: clarify local import comment in agents.py
Copilot 84df982
Fix remaining agent-context review findings
Copilot 22d6ef5
Fix follow-up agent-context review issues
Copilot f0b5b0b
Address review feedback: narrow except, improve PyYAML messaging, sur…
Copilot 019c263
Fix double-space in PyYAML install hint message
Copilot 404ba4d
Potential fix for pull request finding 'Empty except'
mnriem d4a1754
Potential fix for pull request finding 'Empty except'
mnriem 8073e08
Address latest agent-context review feedback
Copilot 765afb2
Harden bash config parse output handling
Copilot b848b62
Clarify ImportError-only fallback comment
Copilot 2849e6d
Apply review feedback: drop dead try/except, guard ext-config creatio…
Copilot 7402f3d
Remove redundant $Options = $null in PS1 catch block
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Coding Agent Context Extension | ||
|
|
||
| This bundled extension manages the **coding agent context/instruction file** (e.g. `CLAUDE.md`, `.github/copilot-instructions.md`, `AGENTS.md`, `GEMINI.md`, …) for the active integration. | ||
|
|
||
| It owns the lifecycle of the managed section delimited by the configurable start/end markers (defaults: `<!-- SPECKIT START -->` / `<!-- SPECKIT END -->`). | ||
|
|
||
| ## Why an extension? | ||
|
|
||
| Not every Spec Kit user wants Spec Kit to write into the coding agent's context file. Extracting this behavior into a dedicated extension lets users: | ||
|
|
||
| - **Opt out** entirely with `specify extension disable agent-context` — Spec Kit will then never create or modify the agent context file. | ||
| - **Customize the markers** by editing `.specify/init-options.json` — both the Python layer and the bundled scripts honor the same `context_markers` value. | ||
| - **Refresh on demand** with `/speckit.agent-context.update`, or automatically through the hooks declared in `extension.yml` (`after_specify`, `after_plan`). | ||
|
|
||
| ## Commands | ||
|
|
||
| | Command | Description | | ||
| |---------|-------------| | ||
| | `speckit.agent-context.update` | Refresh the managed section in the agent context file with the current plan path. | | ||
|
|
||
| ## Configuration | ||
|
|
||
| All configuration flows through `.specify/init-options.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "context_file": "CLAUDE.md", | ||
| "context_markers": { | ||
| "start": "<!-- SPECKIT START -->", | ||
| "end": "<!-- SPECKIT END -->" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - `context_file` — the project-relative path to the coding agent context file, written by `specify init` and `specify integration install`. | ||
| - `context_markers.start` / `.end` — the delimiters around the managed section. Edit these to use custom markers. | ||
|
|
||
| ## Disable | ||
|
|
||
| ```bash | ||
| specify extension disable agent-context | ||
| ``` | ||
|
|
||
| When disabled, `IntegrationBase.setup()` and `IntegrationBase.teardown()` skip context file creation, updates, and removal. | ||
|
mnriem marked this conversation as resolved.
Outdated
|
||
25 changes: 25 additions & 0 deletions
25
extensions/agent-context/commands/speckit.agent-context.update.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| description: "Refresh the managed Spec Kit section in the coding agent context file" | ||
| --- | ||
|
|
||
| # Update Coding Agent Context | ||
|
|
||
| Refresh the managed Spec Kit section inside the active coding agent's context/instruction file (e.g. `CLAUDE.md`, `.github/copilot-instructions.md`, `AGENTS.md`). | ||
|
|
||
| ## Behavior | ||
|
|
||
| The script reads the project's `.specify/init-options.json` to discover: | ||
|
|
||
| - `context_file` — the path of the coding agent context file to manage. | ||
| - `context_markers.start` / `.end` — the delimiters surrounding the managed section. Defaults to `<!-- SPECKIT START -->` and `<!-- SPECKIT END -->` when the field is missing. | ||
|
|
||
| It then creates, replaces, or appends the managed block so that the section points at the most recent plan path when one can be discovered (`specs/<feature>/plan.md`). | ||
|
|
||
| If `context_file` is empty or the file cannot be located, the command reports nothing to do and exits successfully. | ||
|
|
||
| ## Execution | ||
|
|
||
| - **Bash**: `.specify/extensions/agent-context/scripts/bash/update-agent-context.sh [plan_path]` | ||
| - **PowerShell**: `.specify/extensions/agent-context/scripts/powershell/update-agent-context.ps1 [plan_path]` | ||
|
|
||
| When `plan_path` is omitted, the script auto-detects the most recently modified `specs/*/plan.md`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| schema_version: "1.0" | ||
|
|
||
| extension: | ||
| id: agent-context | ||
| name: "Coding Agent Context" | ||
| version: "1.0.0" | ||
| 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 | ||
| license: MIT | ||
|
|
||
| requires: | ||
| speckit_version: ">=0.2.0" | ||
|
|
||
| provides: | ||
| commands: | ||
| - name: speckit.agent-context.update | ||
| file: commands/speckit.agent-context.update.md | ||
| description: "Refresh the managed Spec Kit section in the coding agent context file" | ||
|
|
||
| hooks: | ||
| after_specify: | ||
| command: speckit.agent-context.update | ||
| optional: true | ||
| description: "Refresh agent context after specification" | ||
| after_plan: | ||
| command: speckit.agent-context.update | ||
| optional: true | ||
| description: "Refresh agent context after planning" | ||
|
|
||
| tags: | ||
| - "agent" | ||
| - "context" | ||
| - "core" |
126 changes: 126 additions & 0 deletions
126
extensions/agent-context/scripts/bash/update-agent-context.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #!/usr/bin/env bash | ||
| # update-agent-context.sh | ||
| # | ||
| # Refresh the managed Spec Kit section in the coding agent's context file | ||
| # (e.g. CLAUDE.md, .github/copilot-instructions.md, AGENTS.md). | ||
| # | ||
| # Reads `context_file` and `context_markers.{start,end}` from | ||
| # `.specify/init-options.json`. Falls back to the default markers when | ||
| # `context_markers` is absent. | ||
| # | ||
| # Usage: update-agent-context.sh [plan_path] | ||
| # | ||
| # When `plan_path` is omitted, the script picks the most recently modified | ||
| # `specs/*/plan.md` if any exist, otherwise emits the section without a | ||
| # concrete plan path. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| PROJECT_ROOT="$(pwd)" | ||
| INIT_OPTIONS="$PROJECT_ROOT/.specify/init-options.json" | ||
| DEFAULT_START="<!-- SPECKIT START -->" | ||
| DEFAULT_END="<!-- SPECKIT END -->" | ||
|
|
||
| if [[ ! -f "$INIT_OPTIONS" ]]; then | ||
| echo "agent-context: $INIT_OPTIONS not found; nothing to do." >&2 | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Use python for JSON parsing (always available in spec-kit projects). | ||
| read_json_field() { | ||
| # $1 = jq-style dotted path, e.g. "context_markers.start" | ||
| python3 - "$INIT_OPTIONS" "$1" <<'PY' | ||
| import json, sys | ||
| path = sys.argv[1] | ||
| key = sys.argv[2] | ||
| try: | ||
| with open(path, "r", encoding="utf-8") as fh: | ||
| data = json.load(fh) | ||
| except Exception: | ||
| sys.exit(0) | ||
| node = data | ||
| for part in key.split("."): | ||
| if isinstance(node, dict) and part in node: | ||
| node = node[part] | ||
| else: | ||
| sys.exit(0) | ||
| if isinstance(node, str): | ||
| sys.stdout.write(node) | ||
| PY | ||
| } | ||
|
mnriem marked this conversation as resolved.
Outdated
|
||
|
|
||
| CONTEXT_FILE="$(read_json_field 'context_file' || true)" | ||
| if [[ -z "$CONTEXT_FILE" ]]; then | ||
| echo "agent-context: context_file not set in init-options.json; nothing to do." >&2 | ||
| exit 0 | ||
|
Comment on lines
+25
to
+99
|
||
| fi | ||
|
|
||
| MARKER_START="$(read_json_field 'context_markers.start' || true)" | ||
| MARKER_END="$(read_json_field 'context_markers.end' || true)" | ||
|
mnriem marked this conversation as resolved.
Outdated
|
||
| [[ -z "$MARKER_START" ]] && MARKER_START="$DEFAULT_START" | ||
| [[ -z "$MARKER_END" ]] && MARKER_END="$DEFAULT_END" | ||
|
|
||
| PLAN_PATH="${1:-}" | ||
| if [[ -z "$PLAN_PATH" ]]; then | ||
| if compgen -G "$PROJECT_ROOT/specs/*/plan.md" > /dev/null; then | ||
| # Pick the most recently modified plan.md | ||
| PLAN_PATH="$(ls -1t "$PROJECT_ROOT"/specs/*/plan.md 2>/dev/null | head -1 | sed "s|$PROJECT_ROOT/||")" | ||
|
mnriem marked this conversation as resolved.
Outdated
mnriem marked this conversation as resolved.
Outdated
|
||
| fi | ||
| fi | ||
|
|
||
| CTX_PATH="$PROJECT_ROOT/$CONTEXT_FILE" | ||
| mkdir -p "$(dirname "$CTX_PATH")" | ||
|
|
||
| # Build the managed section | ||
| TMP_SECTION="$(mktemp)" | ||
| trap 'rm -f "$TMP_SECTION"' EXIT | ||
| { | ||
| echo "$MARKER_START" | ||
| echo "For additional context about technologies to be used, project structure," | ||
| echo "shell commands, and other important information, read the current plan" | ||
| if [[ -n "$PLAN_PATH" ]]; then | ||
| echo "at $PLAN_PATH" | ||
| fi | ||
| echo "$MARKER_END" | ||
| } > "$TMP_SECTION" | ||
|
|
||
| python3 - "$CTX_PATH" "$MARKER_START" "$MARKER_END" "$TMP_SECTION" <<'PY' | ||
| import sys, os | ||
| ctx_path, start, end, section_path = sys.argv[1:5] | ||
| with open(section_path, "r", encoding="utf-8") as fh: | ||
| section = fh.read().rstrip("\n") + "\n" | ||
|
|
||
| if os.path.exists(ctx_path): | ||
| with open(ctx_path, "r", encoding="utf-8-sig") as fh: | ||
| content = fh.read() | ||
| s = content.find(start) | ||
| e = content.find(end, s if s != -1 else 0) | ||
| if s != -1 and e != -1 and e > s: | ||
| end_of_marker = e + len(end) | ||
| if end_of_marker < len(content) and content[end_of_marker] == "\r": | ||
| end_of_marker += 1 | ||
| if end_of_marker < len(content) and content[end_of_marker] == "\n": | ||
| end_of_marker += 1 | ||
| new_content = content[:s] + section + content[end_of_marker:] | ||
| elif s != -1: | ||
| new_content = content[:s] + section | ||
| elif e != -1: | ||
| end_of_marker = e + len(end) | ||
| if end_of_marker < len(content) and content[end_of_marker] == "\r": | ||
| end_of_marker += 1 | ||
| if end_of_marker < len(content) and content[end_of_marker] == "\n": | ||
| end_of_marker += 1 | ||
| new_content = section + content[end_of_marker:] | ||
| else: | ||
| if content and not content.endswith("\n"): | ||
| content += "\n" | ||
| new_content = (content + "\n" + section) if content else section | ||
| else: | ||
| new_content = section | ||
|
|
||
| new_content = new_content.replace("\r\n", "\n").replace("\r", "\n") | ||
| with open(ctx_path, "wb") as fh: | ||
| fh.write(new_content.encode("utf-8")) | ||
| PY | ||
|
|
||
| echo "agent-context: updated $CONTEXT_FILE" | ||
113 changes: 113 additions & 0 deletions
113
extensions/agent-context/scripts/powershell/update-agent-context.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #!/usr/bin/env pwsh | ||
| # update-agent-context.ps1 | ||
| # | ||
| # Refresh the managed Spec Kit section in the coding agent's context file | ||
| # (e.g. CLAUDE.md, .github/copilot-instructions.md, AGENTS.md). | ||
| # | ||
| # Reads `context_file` and `context_markers.{start,end}` from | ||
| # `.specify/init-options.json`. Falls back to the default markers when | ||
| # `context_markers` is absent. | ||
| # | ||
| # Usage: update-agent-context.ps1 [plan_path] | ||
|
|
||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position = 0)] | ||
| [string]$PlanPath | ||
| ) | ||
|
|
||
| $ErrorActionPreference = 'Stop' | ||
| $DefaultStart = '<!-- SPECKIT START -->' | ||
| $DefaultEnd = '<!-- SPECKIT END -->' | ||
| $ProjectRoot = (Get-Location).Path | ||
| $InitOptions = Join-Path $ProjectRoot '.specify/init-options.json' | ||
|
|
||
| if (-not (Test-Path -LiteralPath $InitOptions)) { | ||
| Write-Host "agent-context: $InitOptions not found; nothing to do." | ||
| exit 0 | ||
| } | ||
|
|
||
| try { | ||
| $Options = Get-Content -LiteralPath $InitOptions -Raw | ConvertFrom-Json | ||
| } catch { | ||
| Write-Host "agent-context: failed to parse init-options.json; nothing to do." | ||
| exit 0 | ||
| } | ||
|
|
||
| $ContextFile = $Options.context_file | ||
| if (-not $ContextFile) { | ||
| Write-Host 'agent-context: context_file not set in init-options.json; nothing to do.' | ||
| exit 0 | ||
| } | ||
|
|
||
| $MarkerStart = $DefaultStart | ||
| $MarkerEnd = $DefaultEnd | ||
| if ($Options.context_markers) { | ||
| if ($Options.context_markers.start -is [string] -and $Options.context_markers.start) { | ||
| $MarkerStart = $Options.context_markers.start | ||
| } | ||
| if ($Options.context_markers.end -is [string] -and $Options.context_markers.end) { | ||
| $MarkerEnd = $Options.context_markers.end | ||
| } | ||
| } | ||
|
|
||
| if (-not $PlanPath) { | ||
| $candidate = Get-ChildItem -Path (Join-Path $ProjectRoot 'specs') -Filter 'plan.md' -Recurse -ErrorAction SilentlyContinue | | ||
| Sort-Object LastWriteTime -Descending | | ||
| Select-Object -First 1 | ||
| if ($candidate) { | ||
| $PlanPath = $candidate.FullName.Substring($ProjectRoot.Length).TrimStart('/', '\').Replace('\','/') | ||
|
mnriem marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
mnriem marked this conversation as resolved.
mnriem marked this conversation as resolved.
|
||
|
|
||
| $CtxPath = Join-Path $ProjectRoot $ContextFile | ||
| $CtxDir = Split-Path -Parent $CtxPath | ||
| if ($CtxDir -and -not (Test-Path -LiteralPath $CtxDir)) { | ||
| New-Item -ItemType Directory -Path $CtxDir -Force | Out-Null | ||
| } | ||
|
|
||
| $lines = @($MarkerStart, | ||
| 'For additional context about technologies to be used, project structure,', | ||
| 'shell commands, and other important information, read the current plan') | ||
| if ($PlanPath) { | ||
| $lines += "at $PlanPath" | ||
| } | ||
| $lines += $MarkerEnd | ||
| $Section = ($lines -join "`n") + "`n" | ||
|
|
||
| if (Test-Path -LiteralPath $CtxPath) { | ||
| $rawBytes = [System.IO.File]::ReadAllBytes($CtxPath) | ||
| # Strip UTF-8 BOM if present | ||
| if ($rawBytes.Length -ge 3 -and $rawBytes[0] -eq 0xEF -and $rawBytes[1] -eq 0xBB -and $rawBytes[2] -eq 0xBF) { | ||
| $content = [System.Text.Encoding]::UTF8.GetString($rawBytes, 3, $rawBytes.Length - 3) | ||
| } else { | ||
| $content = [System.Text.Encoding]::UTF8.GetString($rawBytes) | ||
| } | ||
|
|
||
| $s = $content.IndexOf($MarkerStart) | ||
| $e = if ($s -ge 0) { $content.IndexOf($MarkerEnd, $s) } else { $content.IndexOf($MarkerEnd) } | ||
|
|
||
| if ($s -ge 0 -and $e -ge 0 -and $e -gt $s) { | ||
| $endOfMarker = $e + $MarkerEnd.Length | ||
| if ($endOfMarker -lt $content.Length -and $content[$endOfMarker] -eq "`r") { $endOfMarker++ } | ||
| if ($endOfMarker -lt $content.Length -and $content[$endOfMarker] -eq "`n") { $endOfMarker++ } | ||
| $newContent = $content.Substring(0, $s) + $Section + $content.Substring($endOfMarker) | ||
| } elseif ($s -ge 0) { | ||
| $newContent = $content.Substring(0, $s) + $Section | ||
| } elseif ($e -ge 0) { | ||
| $endOfMarker = $e + $MarkerEnd.Length | ||
| if ($endOfMarker -lt $content.Length -and $content[$endOfMarker] -eq "`r") { $endOfMarker++ } | ||
| if ($endOfMarker -lt $content.Length -and $content[$endOfMarker] -eq "`n") { $endOfMarker++ } | ||
| $newContent = $Section + $content.Substring($endOfMarker) | ||
| } else { | ||
| if ($content -and -not $content.EndsWith("`n")) { $content += "`n" } | ||
| if ($content) { $newContent = $content + "`n" + $Section } else { $newContent = $Section } | ||
| } | ||
| } else { | ||
| $newContent = $Section | ||
| } | ||
|
|
||
| $newContent = $newContent.Replace("`r`n", "`n").Replace("`r", "`n") | ||
| [System.IO.File]::WriteAllText($CtxPath, $newContent, (New-Object System.Text.UTF8Encoding($false))) | ||
|
|
||
| Write-Host "agent-context: updated $ContextFile" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.