Skip to content

Commit 80600a2

Browse files
committed
fix: address Copilot review feedback on update-agent-context
- bash: add explicit encoding="utf-8" to feature.json open() call - powershell: replace GetRelativePath (.NET 5+ only) with manual prefix-strip in mtime fallback for PS 5.1 compatibility - tests: add coverage for absolute feature_directory values (under and outside PROJECT_ROOT)
1 parent 0b68e9b commit 80600a2

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎extensions/agent-context/scripts/bash/update-agent-context.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ if [[ -z "$PLAN_PATH" ]]; then
128128
_feature_dir="$("$_python" - "$_feature_json" <<'PY'
129129
import sys, json
130130
try:
131-
d = json.load(open(sys.argv[1]))
131+
d = json.load(open(sys.argv[1], encoding="utf-8"))
132132
print(d.get("feature_directory", ""))
133133
except Exception:
134134
print("")

‎extensions/agent-context/scripts/powershell/update-agent-context.ps1‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,14 @@ if (-not $PlanPath) {
211211
Sort-Object LastWriteTime -Descending |
212212
Select-Object -First 1
213213
if ($candidate) {
214-
$PlanPath = [System.IO.Path]::GetRelativePath($ProjectRoot, $candidate.FullName).Replace('\','/')
214+
# GetRelativePath is .NET 5+ only; strip prefix manually for PS 5.1 compat.
215+
$fullPath = $candidate.FullName.Replace('\', '/')
216+
$normRoot = $ProjectRoot.Replace('\', '/').TrimEnd('/') + '/'
217+
if ($fullPath.StartsWith($normRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
218+
$PlanPath = $fullPath.Substring($normRoot.Length)
219+
} else {
220+
$PlanPath = $fullPath
221+
}
215222
}
216223
} catch {
217224
# Non-fatal: continue without a plan path.

‎tests/extensions/test_update_agent_context_feature_json.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,52 @@ def test_bash_falls_back_to_mtime_when_plan_not_yet_created(tmp_path: Path) -> N
138138
assert "specs/000-old/plan.md" in ctx
139139

140140

141+
@requires_bash
142+
def test_bash_absolute_feature_dir_under_project_root(tmp_path: Path) -> None:
143+
"""Absolute feature_directory under PROJECT_ROOT → project-relative path in context."""
144+
_setup_project(tmp_path)
145+
_make_plan(tmp_path, "specs/001-active")
146+
# Write absolute path to feature.json
147+
_write_feature_json(tmp_path, str(tmp_path / "specs" / "001-active"))
148+
149+
result = subprocess.run(
150+
["bash", str(UPDATE_AGENT_CTX_SH)],
151+
cwd=tmp_path,
152+
capture_output=True,
153+
text=True,
154+
check=False,
155+
)
156+
assert result.returncode == 0, result.stderr + result.stdout
157+
ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8")
158+
# Must be project-relative, not machine-specific absolute
159+
assert "specs/001-active/plan.md" in ctx
160+
assert str(tmp_path) not in ctx
161+
162+
163+
@requires_bash
164+
def test_bash_absolute_feature_dir_outside_project_root(tmp_path: Path) -> None:
165+
"""Absolute feature_directory outside PROJECT_ROOT → absolute path preserved in context."""
166+
project = tmp_path / "project"
167+
external = tmp_path / "external" / "001-feature"
168+
project.mkdir()
169+
external.mkdir(parents=True)
170+
(external / "plan.md").write_text("# plan\n", encoding="utf-8")
171+
172+
_setup_project(project)
173+
_write_feature_json(project, str(external))
174+
175+
result = subprocess.run(
176+
["bash", str(UPDATE_AGENT_CTX_SH)],
177+
cwd=project,
178+
capture_output=True,
179+
text=True,
180+
check=False,
181+
)
182+
assert result.returncode == 0, result.stderr + result.stdout
183+
ctx = (project / "CLAUDE.md").read_text(encoding="utf-8")
184+
assert str(external) + "/plan.md" in ctx
185+
186+
141187
@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available")
142188
def test_ps_uses_feature_json_when_plan_exists(tmp_path: Path) -> None:
143189
"""PowerShell: feature.json points to the active feature; that plan is injected."""

0 commit comments

Comments
 (0)