Skip to content

fix(onboard): stable sentinel + replace-all for zshrc OTEL block (#118) - #441

Closed
anshss wants to merge 2 commits into
mainfrom
fix-118-marker-drift
Closed

anshss wants to merge 2 commits into
mainfrom
fix-118-marker-drift

Conversation

@anshss

@anshss anshss commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #118 — the ~/.zshrc OTEL export block written by tj onboard --claude-code was keyed on a single-line comment marker that had itself already drifted once in this repo's history. That made re-onboard's "replace in place" and tj uninstall's removal blind to a block written under an older marker.

Git-history finding (the drift is real, not hypothetical)

The zshrc OTEL block's marker literally changed once already, in this repo's own tracked history:

  • Introduced in 300861c ("Add agents API endpoint, alert acknowledge REST+MCP, and full uninstall cleanup") as # ocw harness observability.
  • Renamed in 281275f ("Rename project: openclawwatch → Token Juice") to # tj harness observability — the ocw→tj rebrand.

The rename commit updated the marker string used by future onboards but shipped no migration for users who had already onboarded under the old marker — exactly the shape reported in #118 (a real ~/.zshrc with both an # ocw harness observability block and a # tj harness observability block, different bearer tokens). Because both onboard's replace and uninstall's removal only ever matched the current marker string, the old-marker block was invisible to both: re-onboard appended a second block instead of replacing the first (stale secrets accumulate), and uninstall left the old block behind.

(Aside: the Codex [otel] config has an analogous, already-fixed legacy-ocw purge — _codex_purge_legacy_ocw — showing this class of drift bit that surface too, pre-#118.)

Fix

  • Replaced the single-line marker with a stable, content-based sentinel pair: # >>> tokenjam OTEL (managed) >>> / # <<< tokenjam OTEL <<<. This pair is not tied to the product name and won't drift on a future rename.
  • _strip_zshrc_otel_blocks() (new, shared by both commands in cmd_onboard.py) removes the current sentinel block and every known legacy marker block (# tj harness observability, # ocw harness observability) in one pass.
  • onboard --claude-code now strips-then-appends: strip all managed blocks, then write exactly one fresh block. Running it twice (or against a zshrc with legacy blocks) leaves exactly one block, with the current secret.
  • tj uninstall now calls the same _strip_zshrc_otel_blocks() for removal, so it cleans up legacy-marker blocks too, not just the current one.

Scope boundary

Did not touch the claude() per-terminal wrapper block (# tj per-terminal naming) — that's owned by #117 (merges first).

Merge order: CC install/remove cluster — merge #117 → #118 → #121; this is #118 (merge after #117).

Test plan

  • New unit tests: tests/unit/test_onboard_zshrc_otel.py — pure-function coverage of _strip_zshrc_otel_blocks / _zshrc_otel_block, including the exact dual-legacy-marker shape from README: add Lens and Reuse to the roadmap #118.
  • New integration tests: tests/integration/test_cli.py — test_onboard_claude_code_replaces_legacy_zshrc_otel_markers and test_uninstall_removes_legacy_and_current_zshrc_otel_blocks, seeding a ~/.zshrc with both legacy blocks, running the real onboard --claude-code / uninstall CLI commands, and asserting exactly one block after onboard / zero after uninstall.
  • python -m pytest tests/unit/ -k "onboard or uninstall or otel or zshrc" -q → 146 passed
  • python -m pytest tests/integration/test_cli.py -q → 80 passed
  • python -m pytest tests/unit/ -q (full suite) → 1644 passed, 1 skipped (pre-existing, unrelated)
  • ruff check tokenjam/cli/cmd_onboard.py tokenjam/cli/cmd_uninstall.py → clean

🤖 Generated with Claude Code

…ip legacy markers (#118)

The ~/.zshrc OTEL export block was keyed on a single-line comment marker
that had itself already drifted once in this repo's history: the ocw->tj
rebrand (commit 281275f) renamed "# ocw harness observability" to
"# tj harness observability" but never migrated already-onboarded users'
shell rc files. A block written under the older marker was invisible to
both re-onboard's "replace in place" (which only matched the current
marker) and `tj uninstall`'s removal — so re-onboarding APPENDED a second
block with a fresh bearer token instead of replacing the stale one, and
uninstall left old-marker blocks behind.

Replace the marker with a stable, content-based sentinel pair
(">>> tokenjam OTEL (managed) >>>" / "<<< tokenjam OTEL <<<") that will
never be renamed. onboard now strips ALL managed blocks — the sentinel
pair plus every known legacy marker — before appending exactly one fresh
block; uninstall strips the same set. The removal routine
(_strip_zshrc_otel_blocks) is shared between both commands.

Co-Authored-By: Claude <[email protected]>
@anshss
anshss requested a review from anilmurty as a code owner July 9, 2026 18:52
…ent marker (#118)

Promotes the #118 root cause into Critical Rules so future onboard/uninstall
dotfile-writing features (zshrc blocks, shell wrappers, etc.) use a stable
sentinel + strip-all-legacy-markers pattern from the start, instead of
re-discovering the same marker-drift bug.

Co-Authored-By: Claude <[email protected]>
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes stale OTEL export blocks accumulating in ~/.zshrc after product renames (#118) by replacing the previous single-line comment marker approach with a stable sentinel pair (# >>> tokenjam OTEL (managed) >>> / # <<< tokenjam OTEL <<<) and a shared _strip_zshrc_otel_blocks() function that removes current and all known legacy marker blocks before writing exactly one fresh block.

  • cmd_onboard.py: Adds _ZSHRC_OTEL_START/_ZSHRC_OTEL_END constants, _zshrc_otel_block(), and _strip_zshrc_otel_blocks(); the onboard zshrc write becomes a strip-then-append with no conditional branch.
  • cmd_uninstall.py: Drops its inline re.sub in favour of the shared strip function, gaining legacy-marker cleanup for free.
  • Tests: New unit test file covers all strip scenarios; two new integration tests seed a zshrc with both legacy marker blocks and assert exactly one (onboard) or zero (uninstall) blocks remain after the CLI runs.

Confidence Score: 4/5

The core fix is correct and well-covered by tests; the two regex patterns have a shared edge case with files that lack a trailing newline on the last managed line, but the block builder always writes the trailing newline so real-world impact is limited.

The strip-then-write approach cleanly eliminates the marker-drift problem, and the tests directly reproduce the bug shape from #118. The only unguarded path is a ~/.zshrc whose final line of a managed block has no trailing newline — both the sentinel regex and the legacy-marker regex require
at the end of the matched range, so a manually-truncated file would leave the block (and the bearer token) in place after an uninstall or re-onboard. Since every in-codebase writer appends
, this is unlikely to bite real users, but making the final newline optional in both patterns would close the gap.

tokenjam/cli/cmd_onboard.py — specifically the two re.sub patterns in _strip_zshrc_otel_blocks; both require a trailing
on the last matched line.

Important Files Changed

Filename Overview
tokenjam/cli/cmd_onboard.py Adds stable sentinel constants, _zshrc_otel_block, and _strip_zshrc_otel_blocks; replaces the old check-then-append-or-replace logic with a clean strip-then-write pattern. One minor edge case: the sentinel regex requires a trailing \n after _ZSHRC_OTEL_END, so a manually-truncated block at end-of-file would survive stripping.
tokenjam/cli/cmd_uninstall.py Replaces inline regex with a shared _strip_zshrc_otel_blocks call; removes now-unused top-level import re. Clean refactor with no logic changes to the surrounding uninstall steps.
tests/unit/test_onboard_zshrc_otel.py New unit test file covering the strip function against all legacy marker combinations, idempotency, and the full onboard/uninstall round-trip at the function level. Good coverage of the bug's exact shape.
tests/integration/test_cli.py Adds two integration tests exercising the real CLI paths against a seeded zshrc with both legacy marker blocks; uses monkeypatch / patch isolation correctly. Minor: the onboard integration test relies on Path.home classmethod patching which works but couples to the module's import path.
CLAUDE.md Adds rule 21 documenting the stable-sentinel requirement for future dotfile-managed blocks, grounding it in the concrete history of the ocw->tj marker drift.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["tj onboard --claude-code"] --> B["Read ~/.zshrc"]
    B --> C["_strip_zshrc_otel_blocks(text)"]
    C --> D["Remove current sentinel block"]
    D --> E["Remove legacy 'tj harness observability' block"]
    E --> F["Remove legacy 'ocw harness observability' block"]
    F --> G{"stripped.strip() non-empty?"}
    G -- Yes --> H["stripped.rstrip() + newline + new_block"]
    G -- No --> I["new_block only"]
    H --> J["zshrc.write_text(updated)"]
    I --> J
    K["tj uninstall"] --> L["Read ~/.zshrc"]
    L --> M["_strip_zshrc_otel_blocks(text)"]
    M --> N{"cleaned != text?"}
    N -- Yes --> O["zshrc.write_text(cleaned)"]
    N -- No --> P["No-op"]
    style C fill:#d4edda
    style M fill:#d4edda
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["tj onboard --claude-code"] --> B["Read ~/.zshrc"]
    B --> C["_strip_zshrc_otel_blocks(text)"]
    C --> D["Remove current sentinel block"]
    D --> E["Remove legacy 'tj harness observability' block"]
    E --> F["Remove legacy 'ocw harness observability' block"]
    F --> G{"stripped.strip() non-empty?"}
    G -- Yes --> H["stripped.rstrip() + newline + new_block"]
    G -- No --> I["new_block only"]
    H --> J["zshrc.write_text(updated)"]
    I --> J
    K["tj uninstall"] --> L["Read ~/.zshrc"]
    L --> M["_strip_zshrc_otel_blocks(text)"]
    M --> N{"cleaned != text?"}
    N -- Yes --> O["zshrc.write_text(cleaned)"]
    N -- No --> P["No-op"]
    style C fill:#d4edda
    style M fill:#d4edda
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
tokenjam/cli/cmd_onboard.py:960-965
The sentinel-block regex requires `_ZSHRC_OTEL_END` to be followed by a literal `
`. If the block ever appears at the very end of a file without a trailing newline (e.g. a user's text editor stripped it), the pattern will fail to match and the block will survive `uninstall` or re-onboard. Adding `
?` makes the trailing newline optional without affecting any other case.

```suggestion
    cleaned = _re.sub(
        rf"{_re.escape(_ZSHRC_OTEL_START)}\n.*?{_re.escape(_ZSHRC_OTEL_END)}\n?",
        "",
        text,
        flags=_re.DOTALL,
    )
```

### Issue 2 of 2
tokenjam/cli/cmd_onboard.py:966-971
The legacy-marker regex uses `(?:export [^
]+
)*`, which requires each matched export line to end with `
`. If the legacy block's last line is also the last line of the file and lacks a trailing newline, the regex matches only the marker line itself and leaves the final export line (including the bearer token) in the file. Adding an optional trailing-newline alternative closes the gap.

```suggestion
    for marker in _ZSHRC_OTEL_LEGACY_MARKERS:
        cleaned = _re.sub(
            rf"{_re.escape(marker)}\n(?:export [^\n]+\n)*(?:export [^\n]+)?",
            "",
            cleaned,
        )
```

Reviews (1): Last reviewed commit: "docs: durable lesson — dotfile-managed b..." | Re-trigger Greptile

Comment on lines +960 to +965
cleaned = _re.sub(
rf"{_re.escape(_ZSHRC_OTEL_START)}\n.*?{_re.escape(_ZSHRC_OTEL_END)}\n",
"",
text,
flags=_re.DOTALL,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The sentinel-block regex requires _ZSHRC_OTEL_END to be followed by a literal . If the block ever appears at the very end of a file without a trailing newline (e.g. a user's text editor stripped it), the pattern will fail to match and the block will survive uninstall or re-onboard. Adding ? makes the trailing newline optional without affecting any other case.

Suggested change
cleaned = _re.sub(
rf"{_re.escape(_ZSHRC_OTEL_START)}\n.*?{_re.escape(_ZSHRC_OTEL_END)}\n",
"",
text,
flags=_re.DOTALL,
)
cleaned = _re.sub(
rf"{_re.escape(_ZSHRC_OTEL_START)}\n.*?{_re.escape(_ZSHRC_OTEL_END)}\n?",
"",
text,
flags=_re.DOTALL,
)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_onboard.py
Line: 960-965

Comment:
The sentinel-block regex requires `_ZSHRC_OTEL_END` to be followed by a literal `
`. If the block ever appears at the very end of a file without a trailing newline (e.g. a user's text editor stripped it), the pattern will fail to match and the block will survive `uninstall` or re-onboard. Adding `
?` makes the trailing newline optional without affecting any other case.

```suggestion
    cleaned = _re.sub(
        rf"{_re.escape(_ZSHRC_OTEL_START)}\n.*?{_re.escape(_ZSHRC_OTEL_END)}\n?",
        "",
        text,
        flags=_re.DOTALL,
    )
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +966 to +971
for marker in _ZSHRC_OTEL_LEGACY_MARKERS:
cleaned = _re.sub(
rf"{_re.escape(marker)}\n(?:export [^\n]+\n)*",
"",
cleaned,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The legacy-marker regex uses (?:export [^ ]+ )*, which requires each matched export line to end with . If the legacy block's last line is also the last line of the file and lacks a trailing newline, the regex matches only the marker line itself and leaves the final export line (including the bearer token) in the file. Adding an optional trailing-newline alternative closes the gap.

Suggested change
for marker in _ZSHRC_OTEL_LEGACY_MARKERS:
cleaned = _re.sub(
rf"{_re.escape(marker)}\n(?:export [^\n]+\n)*",
"",
cleaned,
)
for marker in _ZSHRC_OTEL_LEGACY_MARKERS:
cleaned = _re.sub(
rf"{_re.escape(marker)}\n(?:export [^\n]+\n)*(?:export [^\n]+)?",
"",
cleaned,
)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_onboard.py
Line: 966-971

Comment:
The legacy-marker regex uses `(?:export [^
]+
)*`, which requires each matched export line to end with `
`. If the legacy block's last line is also the last line of the file and lacks a trailing newline, the regex matches only the marker line itself and leaves the final export line (including the bearer token) in the file. Adding an optional trailing-newline alternative closes the gap.

```suggestion
    for marker in _ZSHRC_OTEL_LEGACY_MARKERS:
        cleaned = _re.sub(
            rf"{_re.escape(marker)}\n(?:export [^\n]+\n)*(?:export [^\n]+)?",
            "",
            cleaned,
        )
```

How can I resolve this? If you propose a fix, please make it concise.

@anshss

anshss commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #442, which folds this branch together with the other two per-ticket PRs (#439 #441 #440) into a single PR to main, plus the Greptile review fixes. Closing in favor of #442. Branch retained for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant