Skip to content

fix(uninstall): remove claude() shell wrapper + unwire output_cap hook (#117) - #439

Closed
anshss wants to merge 1 commit into
mainfrom
fix-117-uninstall-wrapper
Closed

anshss wants to merge 1 commit into
mainfrom
fix-117-uninstall-wrapper

Conversation

@anshss

@anshss anshss commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

tj onboard --claude-code writes TWO things into ~/.zshrc: (1) a # tj harness observability OTEL export block, and (2) a # tj per-terminal naming … # end tj per-terminal naming block defining a claude() shell function that calls tj otel-resource-attrs and tj session-end. tj uninstall removed block (1) but had NO code path removing block (2) — so after tj uninstall + pip uninstall tokenjam, the leftover claude() function errors (tj: command not found) on every claude launch.

  • Added _unwire_claude_wrapper() in cmd_onboard.py — the counterpart to _install_claude_wrapper(). Removes the # tj per-terminal naming … # end tj per-terminal naming block from ~/.zshrc / ~/.bashrc via exact marker-delimited removal (not a fragile regex on the block body), idempotent, preserves everything else.
  • Wired _unwire_claude_wrapper() into cmd_uninstall.py.
  • Also wired in the existing-but-never-called _unwire_claude_output_cap_hook() so an opted-in PostToolUse cap hook is cleaned up on uninstall too.
  • Left the OTEL-export block's marker-matching logic untouched (owned by README: add Lens and Reuse to the roadmap #118).

Test plan

  • New unit tests in tests/unit/test_uninstall_wrapper.py: _unwire_claude_wrapper() removes the block written by _install_claude_wrapper(), cleans .bashrc when present, is idempotent/no-op when absent, preserves surrounding rc content, and an end-to-end cmd_uninstall CLI test (fixture ~/.zshrc with both onboard-written blocks + a wired cap-output hook) asserting no tj references or claude() function remain after uninstall.
  • python3 -m pytest tests/unit/ -k "uninstall or onboard or wrapper" -q → 144 passed, 1498 deselected
  • python3 -m pytest tests/unit/ -q → 1641 passed, 1 skipped
  • python3 -m pytest tests/integration/test_cli_uninstall.py tests/integration/test_cli.py -q → 85 passed
  • ruff check clean on changed files

Merge order: Part of the CC install/remove cluster — merge #117 → #118 → #121; this is #117 (foundation, merge first).

🤖 Generated with Claude Code

#117)

`tj onboard --claude-code` writes a `claude()` shell function into
~/.zshrc (and ~/.bashrc) via `_install_claude_wrapper()`, but
`tj uninstall` had no counterpart removal path — leaving a `claude()`
function that calls `tj` on every launch, which errors once the
package is uninstalled. Add `_unwire_claude_wrapper()` (marker-delimited
removal, mirroring the install path) and call it from `cmd_uninstall`.
Also wire in the existing but never-called `_unwire_claude_output_cap_hook()`
so an opted-in PostToolUse cap hook is cleaned up too.

Co-Authored-By: Claude <[email protected]>
@anshss
anshss requested a review from anilmurty as a code owner July 9, 2026 18:48
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a long-standing gap in tj uninstall: the claude() shell wrapper installed by tj onboard --claude-code was never removed, causing tj: command not found errors on every claude launch after the package was uninstalled. The fix adds _unwire_claude_wrapper() in cmd_onboard.py and wires it (along with the previously-dormant _unwire_claude_output_cap_hook()) into cmd_uninstall.py.

  • _unwire_claude_wrapper() mirrors _install_claude_wrapper() exactly \u2014 same marker-delimited regex, idempotent, skips rc files that don't exist, preserves all surrounding content.
  • _unwire_claude_output_cap_hook() is now wired into the settings-cleanup block alongside the existing resume-brief hook removal; the conditional write guards against unnecessary file churn.
  • New unit tests cover remove-after-install, bashrc cleanup, idempotency/no-op, surrounding-content preservation, and a full CLI end-to-end scenario.

Confidence Score: 4/5

Safe to merge — the change only adds cleanup logic, all paths are guarded with try/except, and the test suite validates the key scenarios end-to-end.

The new _unwire_claude_wrapper() function correctly removes the block in all realistic scenarios. Two minor edge cases exist: the regex requires a trailing newline after _WRAPPER_END_MARKER, so a manually-edited rc file missing that newline would silently skip removal; and the preceding blank line added by the installer is not consumed on removal, leaving a cosmetic gap. Neither affects normal usage.

The regex in _unwire_claude_wrapper() in cmd_onboard.py is worth a second glance for the (?: |$) edge case; everything else is straightforward.

Important Files Changed

Filename Overview
tokenjam/cli/cmd_onboard.py Adds _unwire_claude_wrapper() — counterpart to _install_claude_wrapper() — using the same marker-delimited regex strategy; one minor edge case around trailing \n and a cosmetic blank-line residue after removal.
tokenjam/cli/cmd_uninstall.py Wires _unwire_claude_wrapper() and the previously-uncalled _unwire_claude_output_cap_hook() into the uninstall flow; both are guarded with try/except and conditional writes, correct and safe.
tests/unit/test_uninstall_wrapper.py New test file with good coverage: install-then-remove, bashrc cleanup, idempotency, surrounding-content preservation, and an end-to-end CLI test. All assertions are targeted and meaningful.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant U as User shell
    participant UNC as cmd_uninstall
    participant OB as cmd_onboard
    participant FS as ~/.zshrc / ~/.bashrc
    participant CS as ~/.claude/settings.json

    U->>UNC: tj uninstall --yes
    UNC->>CS: read global settings.json
    UNC->>OB: _unwire_claude_resume_brief_hook(gs)
    OB-->>UNC: hook_removed
    UNC->>OB: _unwire_claude_output_cap_hook(gs) [NEW]
    OB-->>UNC: cap_removed
    UNC->>CS: write updated settings.json (if changed)
    UNC->>FS: re.sub(OTEL block) on ~/.zshrc
    FS-->>UNC: cleaned
    UNC->>OB: _unwire_claude_wrapper() [NEW]
    OB->>FS: read ~/.zshrc
    OB->>FS: re.sub(_WRAPPER_MARKER.._WRAPPER_END_MARKER) on ~/.zshrc
    OB->>FS: re.sub(_WRAPPER_MARKER.._WRAPPER_END_MARKER) on ~/.bashrc (if exists)
    OB-->>UNC: list of modified rc files
    UNC->>U: print removed paths + success message
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"}}}%%
sequenceDiagram
    participant U as User shell
    participant UNC as cmd_uninstall
    participant OB as cmd_onboard
    participant FS as ~/.zshrc / ~/.bashrc
    participant CS as ~/.claude/settings.json

    U->>UNC: tj uninstall --yes
    UNC->>CS: read global settings.json
    UNC->>OB: _unwire_claude_resume_brief_hook(gs)
    OB-->>UNC: hook_removed
    UNC->>OB: _unwire_claude_output_cap_hook(gs) [NEW]
    OB-->>UNC: cap_removed
    UNC->>CS: write updated settings.json (if changed)
    UNC->>FS: re.sub(OTEL block) on ~/.zshrc
    FS-->>UNC: cleaned
    UNC->>OB: _unwire_claude_wrapper() [NEW]
    OB->>FS: read ~/.zshrc
    OB->>FS: re.sub(_WRAPPER_MARKER.._WRAPPER_END_MARKER) on ~/.zshrc
    OB->>FS: re.sub(_WRAPPER_MARKER.._WRAPPER_END_MARKER) on ~/.bashrc (if exists)
    OB-->>UNC: list of modified rc files
    UNC->>U: print removed paths + success message
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:2318-2323
The regex requires `
` immediately after `_WRAPPER_END_MARKER`, which is correct for every normal install path. However, if the user's rc file ends on that line without a trailing newline (e.g., a manual edit or a tool that strips final newlines), the pattern silently produces no match — `updated == text`, the file is not written, the function returns `[]`, and no error is raised. Adding `$` as a fallback via `(?:
|$)` handles this edge case without changing normal-path behaviour.

```suggestion
        updated = _re.sub(
            _re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"(?:\n|$)",
            "",
            text,
            flags=_re.DOTALL,
        )
```

### Issue 2 of 2
tokenjam/cli/cmd_onboard.py:2318-2326
**Blank-line residue after unwire**

`_install_claude_wrapper()` prepends `"\n"` (or `"\n\n"`) before the block for readability. The removal regex starts at `_WRAPPER_MARKER` and doesn't reach that preceding blank line, so after uninstall the rc file is left with an extra blank line where the block used to be. Functionally harmless, but the symmetric fix would be to also match an optional preceding newline, e.g. `r"\n?" + _re.escape(_WRAPPER_MARKER) + …`.

Reviews (1): Last reviewed commit: "fix(uninstall): remove claude() shell wr..." | Re-trigger Greptile

Comment on lines +2318 to +2323
updated = _re.sub(
_re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"\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 regex requires immediately after _WRAPPER_END_MARKER, which is correct for every normal install path. However, if the user's rc file ends on that line without a trailing newline (e.g., a manual edit or a tool that strips final newlines), the pattern silently produces no match — updated == text, the file is not written, the function returns [], and no error is raised. Adding $ as a fallback via (?: |$) handles this edge case without changing normal-path behaviour.

Suggested change
updated = _re.sub(
_re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"\n",
"",
text,
flags=_re.DOTALL,
)
updated = _re.sub(
_re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"(?:\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: 2318-2323

Comment:
The regex requires `
` immediately after `_WRAPPER_END_MARKER`, which is correct for every normal install path. However, if the user's rc file ends on that line without a trailing newline (e.g., a manual edit or a tool that strips final newlines), the pattern silently produces no match — `updated == text`, the file is not written, the function returns `[]`, and no error is raised. Adding `$` as a fallback via `(?:
|$)` handles this edge case without changing normal-path behaviour.

```suggestion
        updated = _re.sub(
            _re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"(?:\n|$)",
            "",
            text,
            flags=_re.DOTALL,
        )
```

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

Comment on lines +2318 to +2326
updated = _re.sub(
_re.escape(_WRAPPER_MARKER) + r".*?" + _re.escape(_WRAPPER_END_MARKER) + r"\n",
"",
text,
flags=_re.DOTALL,
)
if updated != text:
rc.write_text(updated)
removed.append(str(rc))

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 Blank-line residue after unwire

_install_claude_wrapper() prepends "\n" (or "\n\n") before the block for readability. The removal regex starts at _WRAPPER_MARKER and doesn't reach that preceding blank line, so after uninstall the rc file is left with an extra blank line where the block used to be. Functionally harmless, but the symmetric fix would be to also match an optional preceding newline, e.g. r"\n?" + _re.escape(_WRAPPER_MARKER) + ….

Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_onboard.py
Line: 2318-2326

Comment:
**Blank-line residue after unwire**

`_install_claude_wrapper()` prepends `"\n"` (or `"\n\n"`) before the block for readability. The removal regex starts at `_WRAPPER_MARKER` and doesn't reach that preceding blank line, so after uninstall the rc file is left with an extra blank line where the block used to be. Functionally harmless, but the symmetric fix would be to also match an optional preceding newline, e.g. `r"\n?" + _re.escape(_WRAPPER_MARKER) + …`.

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

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@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