Skip to content

Commit 3dd57c8

Browse files
committed
fix(install): strip legacy tokenwise blocks from CLAUDE.md and codex AGENTS.md
The pre-rename `tokenwise` installer wrote routing tables under `<!-- tokenwise-begin -->` / `<!-- tokenwise-codex-begin -->` fences. After the rename to token-goat, the installer started emitting a new fence (`<!-- token-goat-begin -->`) but the old block was never removed, leaving users with a duplicate stale block in `~/.claude/CLAUDE.md` and, in some cases, a Codex `AGENTS.md` that still pointed at the old binary. Add a `_strip_legacy_block` helper invoked from both `patch_claude_md` and `patch_codex_agents_md` so a single install migrates the file. Also tighten `_patch_md_block` to not leave leading blank lines when the file is whitespace-only right after a legacy strip. Regression tests cover the strip path and idempotence on both fences.
1 parent 1ee0f36 commit 3dd57c8

3 files changed

Lines changed: 164 additions & 2 deletions

File tree

‎src/token_goat/install.py‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class _HookMatcherEntry(TypedDict):
5353
CLAUDE_MD_BEGIN = "<!-- token-goat-begin -->"
5454
CLAUDE_MD_END = "<!-- token-goat-end -->"
5555

56+
# Legacy markers from the pre-rename "tokenwise" era. These blocks describe the
57+
# old binary name and produce incorrect routing instructions; the patch path
58+
# strips them on install so a single install run leaves only the modern block.
59+
LEGACY_CLAUDE_MD_BEGIN = "<!-- tokenwise-begin -->"
60+
LEGACY_CLAUDE_MD_END = "<!-- tokenwise-end -->"
61+
LEGACY_CODEX_AGENTS_BEGIN = "<!-- tokenwise-codex-begin -->"
62+
LEGACY_CODEX_AGENTS_END = "<!-- tokenwise-codex-end -->"
63+
5664
# Scheduled task names (Windows)
5765
TASK_WORKER = "token-goat-worker"
5866
TASK_UPDATE = "token-goat-update"
@@ -1042,10 +1050,14 @@ def _patch_md_block(md_path: Path, begin_marker: str, end_marker: str, content:
10421050
existing,
10431051
flags=re.DOTALL,
10441052
)
1045-
else:
1053+
elif existing.strip():
10461054
if not existing.endswith("\n"):
10471055
existing += "\n"
10481056
updated = existing + "\n" + block + "\n"
1057+
else:
1058+
# File exists but is whitespace-only (common right after a legacy
1059+
# strip wiped its sole block). Don't preserve the leading blanks.
1060+
updated = block + "\n"
10491061
else:
10501062
updated = block + "\n"
10511063

@@ -1075,6 +1087,29 @@ def _unpatch_md_block(md_path: Path, begin_marker: str, end_marker: str, not_fou
10751087
return str(md_path)
10761088

10771089

1090+
def _strip_legacy_block(md_path: Path, begin_marker: str, end_marker: str) -> bool:
1091+
"""Remove a legacy ``tokenwise``-era delimited block from *md_path* if present.
1092+
1093+
Returns ``True`` if a block was stripped, ``False`` otherwise. The modern
1094+
patch path calls this before writing its block so a single install run
1095+
leaves only the up-to-date content — even on machines that were installed
1096+
under the old binary name and never had their routing tables migrated.
1097+
"""
1098+
if not md_path.exists():
1099+
return False
1100+
content = md_path.read_text(encoding="utf-8")
1101+
if begin_marker not in content or end_marker not in content:
1102+
return False
1103+
new = re.sub(
1104+
r"\n*" + re.escape(begin_marker) + r".*?" + re.escape(end_marker) + r"\n*",
1105+
"\n",
1106+
content,
1107+
flags=re.DOTALL,
1108+
).strip()
1109+
paths.atomic_write_text(md_path, new + "\n" if new else "")
1110+
return True
1111+
1112+
10781113
# ---------------------------------------------------------------------------
10791114
# CLAUDE.md patching
10801115
# ---------------------------------------------------------------------------
@@ -1113,6 +1148,8 @@ def patch_claude_md() -> str:
11131148
"""Add or update the token-goat block in ~/.claude/CLAUDE.md, idempotently."""
11141149
md_path = claude_md_path()
11151150
existed = md_path.exists()
1151+
if _strip_legacy_block(md_path, LEGACY_CLAUDE_MD_BEGIN, LEGACY_CLAUDE_MD_END):
1152+
_LOG.info("patch_claude_md: stripped legacy tokenwise block from %s", md_path)
11161153
result = _patch_md_block(md_path, CLAUDE_MD_BEGIN, CLAUDE_MD_END, CLAUDE_MD_CONTENT)
11171154
action = "updated" if existed else "created"
11181155
_LOG.info("patch_claude_md: %s %s", action, md_path)
@@ -1383,8 +1420,11 @@ def unpatch_codex_config() -> str:
13831420

13841421
def patch_codex_agents_md() -> str:
13851422
"""Append/replace the delimited token-goat block in ~/.codex/AGENTS.md."""
1423+
md_path = codex_agents_path()
1424+
if _strip_legacy_block(md_path, LEGACY_CODEX_AGENTS_BEGIN, LEGACY_CODEX_AGENTS_END):
1425+
_LOG.info("patch_codex_agents_md: stripped legacy tokenwise-codex block from %s", md_path)
13861426
return _patch_md_block(
1387-
codex_agents_path(), CODEX_AGENTS_BEGIN, CODEX_AGENTS_END, CODEX_AGENTS_MD_CONTENT
1427+
md_path, CODEX_AGENTS_BEGIN, CODEX_AGENTS_END, CODEX_AGENTS_MD_CONTENT
13881428
)
13891429

13901430

‎tests/test_install.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,71 @@ def test_unpatch_claude_md_removes_block(tmp_path, monkeypatch):
204204
assert install.CLAUDE_MD_END not in content
205205

206206

207+
# ---------------------------------------------------------------------------
208+
# 8b. patch_claude_md — strips legacy tokenwise block left over from pre-rename
209+
# ---------------------------------------------------------------------------
210+
211+
212+
def test_patch_claude_md_strips_legacy_tokenwise_block(tmp_path, monkeypatch):
213+
"""A CLAUDE.md installed under the old ``tokenwise`` binary name still
214+
contains a ``<!-- tokenwise-begin -->...-end -->`` block describing the
215+
old routing table. Running the modern installer must strip it so the file
216+
is left with only the up-to-date ``token-goat`` block, not both.
217+
"""
218+
home = _fake_home(tmp_path)
219+
_patch_home(monkeypatch, home)
220+
221+
claude_dir = home / ".claude"
222+
claude_dir.mkdir(parents=True, exist_ok=True)
223+
legacy_block = (
224+
f"{install.LEGACY_CLAUDE_MD_BEGIN}\n"
225+
"## tokenwise - route code reads through tokenwise first\n\n"
226+
"| Goal | Do this | Not this |\n"
227+
"|------|---------|----------|\n"
228+
"| Find a function | `tokenwise symbol X` | `Grep X` |\n"
229+
f"{install.LEGACY_CLAUDE_MD_END}\n"
230+
)
231+
seed = "# My existing CLAUDE.md\n\nSome prior content.\n\n" + legacy_block
232+
md_path = claude_dir / "CLAUDE.md"
233+
md_path.write_text(seed, encoding="utf-8")
234+
235+
install.patch_claude_md()
236+
content = md_path.read_text()
237+
238+
# User content survives.
239+
assert "My existing CLAUDE.md" in content
240+
# Modern block landed.
241+
assert install.CLAUDE_MD_BEGIN in content
242+
assert install.CLAUDE_MD_END in content
243+
# Legacy fence is gone, and so is the misleading body.
244+
assert install.LEGACY_CLAUDE_MD_BEGIN not in content
245+
assert install.LEGACY_CLAUDE_MD_END not in content
246+
assert "tokenwise symbol X" not in content
247+
248+
249+
def test_patch_claude_md_legacy_strip_is_idempotent(tmp_path, monkeypatch):
250+
"""Two consecutive installs leave exactly one modern block, no legacy."""
251+
home = _fake_home(tmp_path)
252+
_patch_home(monkeypatch, home)
253+
254+
claude_dir = home / ".claude"
255+
claude_dir.mkdir(parents=True, exist_ok=True)
256+
seed = (
257+
f"{install.LEGACY_CLAUDE_MD_BEGIN}\nlegacy body\n{install.LEGACY_CLAUDE_MD_END}\n"
258+
)
259+
md_path = claude_dir / "CLAUDE.md"
260+
md_path.write_text(seed, encoding="utf-8")
261+
262+
install.patch_claude_md()
263+
install.patch_claude_md()
264+
content = md_path.read_text()
265+
266+
assert content.count(install.CLAUDE_MD_BEGIN) == 1
267+
assert content.count(install.CLAUDE_MD_END) == 1
268+
assert install.LEGACY_CLAUDE_MD_BEGIN not in content
269+
assert install.LEGACY_CLAUDE_MD_END not in content
270+
271+
207272
# ---------------------------------------------------------------------------
208273
# 9. write_skill — creates SKILL.md under ~/.claude/skills/token-goat/
209274
# ---------------------------------------------------------------------------

‎tests/test_install_codex.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,63 @@ def test_patch_codex_agents_md_idempotent(tmp_path, monkeypatch):
193193
assert content.count(install.CODEX_AGENTS_END) == 1
194194

195195

196+
def test_patch_codex_agents_md_strips_legacy_tokenwise_block(tmp_path, monkeypatch):
197+
"""An AGENTS.md written under the old ``tokenwise`` binary name still
198+
contains a ``<!-- tokenwise-codex-begin -->...-end -->`` block instructing
199+
Codex to use the wrong binary. The modern installer must strip it so the
200+
file ends up with only the up-to-date ``token-goat`` routing table.
201+
"""
202+
home = _fake_home(tmp_path)
203+
_patch_home(monkeypatch, home)
204+
205+
codex_dir = home / ".codex"
206+
codex_dir.mkdir(parents=True, exist_ok=True)
207+
legacy_block = (
208+
f"{install.LEGACY_CODEX_AGENTS_BEGIN}\n"
209+
"## tokenwise - route code reads through tokenwise first (Codex)\n\n"
210+
"| Goal | Do this | Not this |\n"
211+
"|------|---------|----------|\n"
212+
"| Find a function | `tokenwise symbol X` | `rg X` |\n"
213+
f"{install.LEGACY_CODEX_AGENTS_END}\n"
214+
)
215+
md_path = codex_dir / "AGENTS.md"
216+
md_path.write_text(legacy_block, encoding="utf-8")
217+
218+
install.patch_codex_agents_md()
219+
content = md_path.read_text(encoding="utf-8")
220+
221+
assert install.CODEX_AGENTS_BEGIN in content
222+
assert install.CODEX_AGENTS_END in content
223+
assert install.LEGACY_CODEX_AGENTS_BEGIN not in content
224+
assert install.LEGACY_CODEX_AGENTS_END not in content
225+
assert "tokenwise symbol X" not in content
226+
227+
228+
def test_patch_codex_agents_md_legacy_strip_is_idempotent(tmp_path, monkeypatch):
229+
"""Two installs in a row leave one modern block and no legacy artifacts."""
230+
home = _fake_home(tmp_path)
231+
_patch_home(monkeypatch, home)
232+
233+
codex_dir = home / ".codex"
234+
codex_dir.mkdir(parents=True, exist_ok=True)
235+
seed = (
236+
f"{install.LEGACY_CODEX_AGENTS_BEGIN}\n"
237+
f"legacy body\n"
238+
f"{install.LEGACY_CODEX_AGENTS_END}\n"
239+
)
240+
md_path = codex_dir / "AGENTS.md"
241+
md_path.write_text(seed, encoding="utf-8")
242+
243+
install.patch_codex_agents_md()
244+
install.patch_codex_agents_md()
245+
content = md_path.read_text(encoding="utf-8")
246+
247+
assert content.count(install.CODEX_AGENTS_BEGIN) == 1
248+
assert content.count(install.CODEX_AGENTS_END) == 1
249+
assert install.LEGACY_CODEX_AGENTS_BEGIN not in content
250+
assert install.LEGACY_CODEX_AGENTS_END not in content
251+
252+
196253
# ---------------------------------------------------------------------------
197254
# 8. patch_codex_agents_md appends to existing file without our block
198255
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)