Skip to content

Commit aef3b39

Browse files
committed
fix(ce-setup): require ordered BEGIN/END tool-map sentinels
Independent grep treated END-then-BEGIN as a removable map even though removeCodexAgentsToolMapBlock would leave the file unchanged. Warn only when the first BEGIN precedes its END.
1 parent 46d051f commit aef3b39

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

skills/ce-setup/references/legacy-codex-tool-map.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ the main thread. Native plugin install does **not** add this block.
1818
`~/.codex/AGENTS.md`. Also check `~/.codex/profiles/*/AGENTS.md`.
1919
2. Look for the exact sentinels `<!-- BEGIN COMPOUND CODEX TOOL MAP -->`
2020
and `<!-- END COMPOUND CODEX TOOL MAP -->`.
21-
3. If both are present, delete only the span from the BEGIN line through
22-
the END line (inclusive). Leave any other user content untouched. Do
23-
not edit project/repo `AGENTS.md` unless those exact sentinels are
24-
present there.
21+
3. Only if a BEGIN is followed later by its END, delete the span from
22+
that BEGIN through that END (inclusive). If END appears first, leave
23+
the file alone — there is no ordered block to remove. Leave any other
24+
user content untouched. Do not edit project/repo `AGENTS.md` unless
25+
those exact sentinels form an ordered pair there.
2526
4. If the file is empty after the removal, delete the file.
2627
5. Show a short before/after of what changed (or say the block was
2728
already absent). Do not add a replacement tool map.

skills/ce-setup/scripts/check-health

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,27 @@ CODEX_TOOL_MAP_END="<!-- END COMPOUND CODEX TOOL MAP -->"
295295
codex_home="${CODEX_HOME:-$HOME/.codex}"
296296
default_codex_home="$HOME/.codex"
297297
legacy_codex_map_files=()
298+
# Ordered pair only: first BEGIN must precede first END (same contract as
299+
# removeCodexAgentsToolMapBlock). Independent grep would treat END-then-BEGIN
300+
# as removable even though no safe span exists.
301+
has_ordered_codex_tool_map() {
302+
awk -v s="$CODEX_TOOL_MAP_START" -v e="$CODEX_TOOL_MAP_END" '
303+
{ buf = buf $0 "\n" }
304+
END {
305+
si = index(buf, s)
306+
ei = index(buf, e)
307+
exit (si > 0 && ei > 0 && ei > si) ? 0 : 1
308+
}
309+
' "$1"
310+
}
298311
scan_codex_agents_file() {
299312
local f="$1"
300313
local existing
301314
[ -f "$f" ] || return 0
302315
for existing in "${legacy_codex_map_files[@]}"; do
303316
[ "$existing" = "$f" ] && return 0
304317
done
305-
if grep -F -q -- "$CODEX_TOOL_MAP_START" "$f" && grep -F -q -- "$CODEX_TOOL_MAP_END" "$f"; then
318+
if has_ordered_codex_tool_map "$f"; then
306319
legacy_codex_map_files+=("$f")
307320
fi
308321
}

tests/skills/ce-setup-check-health.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ describe("ce-setup check-health", () => {
102102
}
103103
})
104104

105+
test("does not warn when END sentinel precedes BEGIN (no ordered span)", async () => {
106+
const root = await mkdtemp(path.join(os.tmpdir(), "ce-setup-health-"))
107+
try {
108+
await initGitRepo(root)
109+
await mkdir(path.join(root, ".codex"), { recursive: true })
110+
await writeFile(
111+
path.join(root, ".codex", "AGENTS.md"),
112+
[
113+
"keep this",
114+
"<!-- END COMPOUND CODEX TOOL MAP -->",
115+
"user-owned notes",
116+
"<!-- BEGIN COMPOUND CODEX TOOL MAP -->",
117+
"",
118+
].join("\n"),
119+
)
120+
const result = await runCheckHealth(root, "/usr/bin:/bin")
121+
expect(result.exitCode).toBe(0)
122+
expect(result.stdout).not.toContain("Legacy Compound Codex tool map still present")
123+
} finally {
124+
await rm(root, { recursive: true, force: true })
125+
}
126+
})
127+
105128
test("still finds named-profile copies under ~/.codex when CODEX_HOME is elsewhere", async () => {
106129
const root = await mkdtemp(path.join(os.tmpdir(), "ce-setup-health-"))
107130
try {

0 commit comments

Comments
 (0)