Skip to content

feat(uninstall): consented --purge package removal + runner-aware hint (#121) - #440

Closed
anshss wants to merge 1 commit into
mainfrom
fix-121-npx-remove
Closed

anshss wants to merge 1 commit into
mainfrom
fix-121-npx-remove

Conversation

@anshss

@anshss anshss commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Extends tj uninstall's existing opt-in package removal (--remove-package, Fix: clarify uninstall/reinstall two-step + optional package removal #430) with a --purge alias and full runner detection, making npx tokenjam uninstall [--purge] the complete, symmetric counterpart to npx tokenjam onboard.
  • Adds uv tool install detection alongside the existing pipx detection — --purge now auto-runs uv tool uninstall tokenjam for uv-tool installs, and the default (non-purge) closing hint prints the exact matching command (pipx uninstall / uv tool uninstall / pip uninstall) instead of only ever guessing pipx-or-pip.
  • Adds _is_ephemeral_runner() detection (verified empirically against real uv/pipx installs — see code comments for the exact paths) so an ephemeral invocation (uvx/pipx run, including via the npx tokenjam wrapper's runner fallback) makes --purge a safe no-op with an explanation, rather than guessing at a removal command for a package that was never persistently installed.
  • Documents install/remove as a symmetric pair in docs/claude-code-integration.md.

Resolves #121.

Test plan

  • python -m pytest tests/unit/ -k "uninstall or purge or runner" -q → 20 passed
  • python -m pytest tests/unit/ tests/integration/ -q (full suite) → 2048 passed, 1 skipped (pre-existing)
  • ruff check clean on all touched/new files
  • Manually exercised the CLI output for the ephemeral + --purge no-op path

Merge order: CC install/remove cluster — merge #117 → #118 → #121; this is #121, MERGE LAST (depends on #117 + #118 for a fully-clean uninstall).

🤖 Generated with Claude Code

…t; document symmetric npx remove (#121)

Extends `tj uninstall`'s existing --remove-package opt-in (#430) to make
`npx tokenjam uninstall` a complete, symmetric counterpart to `npx tokenjam
onboard`: detect uv-tool installs (not just pipx/pip) and auto-run the
matching removal command, and detect ephemeral runners (uvx/pipx run) to
make --purge a safe no-op with an explanation instead of guessing at a
removal command that doesn't apply. --purge is added as an alias for
--remove-package so existing scripts/tests keep working.

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

greptile-apps Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extends tj uninstall with a --purge alias for --remove-package, adds uv tool install detection alongside the existing pipx detection, and introduces _is_ephemeral_runner() to make --purge a safe no-op when invoked via uvx/pipx run. Documentation in docs/claude-code-integration.md is updated to present install and uninstall as a symmetric pair.

  • _installed_via_uv_tool() and _is_ephemeral_runner() are new pure-function detectors; _remove_package is refactored into a shared _run_auto_uninstall helper that handles both the pipx and uv-tool auto-removal branches identically.
  • The ephemeral-runner guard (_is_ephemeral_runner) uses \"/uv/\" in sys.executable which is broader than intended — it also matches uv-managed Python runtime paths, causing --purge to silently no-op for pip installs into uv-managed Python.
  • Four new integration tests and a new unit test file cover the happy paths well; one existing integration test is missing mocks for the new detectors, leaving it sensitive to the test runner's Python path.

Confidence Score: 3/5

The core uninstall flow is safe, but the ephemeral-runner guard can misfire on uv-managed Python installs, making --purge silently do nothing for a real persistent package.

The _is_ephemeral_runner check uses "/uv/" in sys.executable, which is satisfied by uv's own Python runtime installations, not just ephemeral uvx venvs. A user who installed tokenjam via pip into a uv-managed Python and runs tj uninstall --purge would see "nothing persistent to remove" and their package would silently not be removed. A one-character fix resolves the false-positive. Everything else is correct and well-tested.

tokenjam/cli/cmd_uninstall.py line 48 — the /uv/ path match in _is_ephemeral_runner. The integration test at tests/integration/test_cli_uninstall.py:89 is also worth a second look for missing mocks.

Important Files Changed

Filename Overview
tokenjam/cli/cmd_uninstall.py Adds _installed_via_uv_tool, _is_ephemeral_runner, --purge alias, and shared _run_auto_uninstall helper. Logic is sound except _is_ephemeral_runner uses a broad /uv/ substring that matches uv-managed Python runtimes as well as ephemeral uvx venvs, causing --purge to be a false no-op for pip installs into uv Python.
tests/integration/test_cli_uninstall.py Four new integration tests covering uv-tool removal, uv-tool reinstall hint, ephemeral no-op, and ephemeral prompt suppression. Tests are well-structured but test_remove_package_flag_non_pipx_prints_command lacks explicit mocks for _installed_via_uv_tool and _is_ephemeral_runner, coupling it to the test runner's Python path.
tests/unit/test_uninstall_runner_detect.py New unit tests covering all three detection functions against empirically verified path strings for pipx venv, uv tool venv, plain pip, uvx cache, and pipx run cache. Coverage is thorough and tests are self-contained.
docs/claude-code-integration.md Documents install/remove as a symmetric pair with --purge behavior for each runner type; content is accurate and consistent with the implementation.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["tj uninstall [--purge] [--yes]"] --> B[Clean data/config/daemon/MCP]
    B --> C{_is_ephemeral_runner?}
    C -- Yes --> D["Print: nothing persistent to remove"]
    D --> E{--purge passed?}
    E -- Yes --> F["Print: --purge is a no-op here"]
    E -- No --> G[return]
    F --> G
    C -- No --> H["Print: package still installed"]
    H --> I{--purge passed?}
    I -- Yes --> J[do_remove = True]
    I -- No --> K{--yes passed?}
    K -- Yes --> L[do_remove = False]
    K -- No --> M["Prompt user"]
    M --> N{User confirms?}
    N -- Yes --> J
    N -- No --> L
    J --> O[_remove_package]
    O --> P{_installed_via_pipx AND pipx on PATH?}
    P -- Yes --> Q["_run_auto_uninstall pipx"]
    P -- No --> R{_installed_via_uv_tool AND uv on PATH?}
    R -- Yes --> S["_run_auto_uninstall uv tool"]
    R -- No --> T["Print manual command"]
    L --> U["Print two-step reinstall hint"]
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 uninstall [--purge] [--yes]"] --> B[Clean data/config/daemon/MCP]
    B --> C{_is_ephemeral_runner?}
    C -- Yes --> D["Print: nothing persistent to remove"]
    D --> E{--purge passed?}
    E -- Yes --> F["Print: --purge is a no-op here"]
    E -- No --> G[return]
    F --> G
    C -- No --> H["Print: package still installed"]
    H --> I{--purge passed?}
    I -- Yes --> J[do_remove = True]
    I -- No --> K{--yes passed?}
    K -- Yes --> L[do_remove = False]
    K -- No --> M["Prompt user"]
    M --> N{User confirms?}
    N -- Yes --> J
    N -- No --> L
    J --> O[_remove_package]
    O --> P{_installed_via_pipx AND pipx on PATH?}
    P -- Yes --> Q["_run_auto_uninstall pipx"]
    P -- No --> R{_installed_via_uv_tool AND uv on PATH?}
    R -- Yes --> S["_run_auto_uninstall uv tool"]
    R -- No --> T["Print manual command"]
    L --> U["Print two-step reinstall hint"]
Loading

Comments Outside Diff (1)

  1. tests/integration/test_cli_uninstall.py, line 89-99 (link)

    P2 Incomplete mocking leaves test coupled to test-runner's Python path

    test_remove_package_flag_non_pipx_prints_command patches _installed_via_pipx but not _installed_via_uv_tool or _is_ephemeral_runner. The _isolate fixture's shutil.which → None mask prevents an auto-run, but if sys.executable during the test happens to contain /uv/ (e.g. a CI environment running under uvx or a uv-Python path), _is_ephemeral_runner() would return True and the command would hit the early-return branch instead of reaching _remove_package — causing the expected "not a pipx- or uv-tool-managed venv" assertion to fail. Adding patch.object(uninstall_mod, "_installed_via_uv_tool", return_value=False) and patch.object(uninstall_mod, "_is_ephemeral_runner", return_value=False) would make the test fully hermetic.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: tests/integration/test_cli_uninstall.py
    Line: 89-99
    
    Comment:
    **Incomplete mocking leaves test coupled to test-runner's Python path**
    
    `test_remove_package_flag_non_pipx_prints_command` patches `_installed_via_pipx` but not `_installed_via_uv_tool` or `_is_ephemeral_runner`. The `_isolate` fixture's `shutil.which → None` mask prevents an auto-run, but if `sys.executable` during the test happens to contain `/uv/` (e.g. a CI environment running under uvx or a uv-Python path), `_is_ephemeral_runner()` would return `True` and the command would hit the early-return branch instead of reaching `_remove_package` — causing the expected `"not a pipx- or uv-tool-managed venv"` assertion to fail. Adding `patch.object(uninstall_mod, "_installed_via_uv_tool", return_value=False)` and `patch.object(uninstall_mod, "_is_ephemeral_runner", return_value=False)` would make the test fully hermetic.
    
    How can I resolve this? If you propose a fix, please make it concise.
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_uninstall.py:47-48
**`/uv/` path match is too broad — falsely ephemeral on uv-managed Python**

`"/uv/" in exe` will match any path component literally named `uv`, including uv's own Python runtime installations (`~/.local/share/uv/python/cpython-3.12.0-linux-x86_64/bin/python3`). A user who installed tokenjam with `pip install tokenjam` using a uv-managed Python (not a uv tool or uvx venv) would hit this branch and see "nothing persistent to remove; --purge is a no-op here" — silently skipping a real package removal.

Per the docstring, the empirically verified path for `uvx`/`uv tool run` ephemeral venvs always contains `archive-v0`. Tightening the check to `"/uv/archive-v0/" in exe` avoids the false positive for uv-managed Python while still catching all current ephemeral uvx venvs.

```suggestion
    exe = sys.executable.replace("\\", "/")
    return "/uv/archive-v0/" in exe or "/pipx/.cache/" in exe
```

### Issue 2 of 2
tests/integration/test_cli_uninstall.py:89-99
**Incomplete mocking leaves test coupled to test-runner's Python path**

`test_remove_package_flag_non_pipx_prints_command` patches `_installed_via_pipx` but not `_installed_via_uv_tool` or `_is_ephemeral_runner`. The `_isolate` fixture's `shutil.which → None` mask prevents an auto-run, but if `sys.executable` during the test happens to contain `/uv/` (e.g. a CI environment running under uvx or a uv-Python path), `_is_ephemeral_runner()` would return `True` and the command would hit the early-return branch instead of reaching `_remove_package` — causing the expected `"not a pipx- or uv-tool-managed venv"` assertion to fail. Adding `patch.object(uninstall_mod, "_installed_via_uv_tool", return_value=False)` and `patch.object(uninstall_mod, "_is_ephemeral_runner", return_value=False)` would make the test fully hermetic.

Reviews (1): Last reviewed commit: "feat(uninstall): consented --purge packa..." | Re-trigger Greptile

Comment on lines +47 to +48
exe = sys.executable.replace("\\", "/")
return "/uv/" in exe or "/pipx/.cache/" in exe

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.

P1 /uv/ path match is too broad — falsely ephemeral on uv-managed Python

"/uv/" in exe will match any path component literally named uv, including uv's own Python runtime installations (~/.local/share/uv/python/cpython-3.12.0-linux-x86_64/bin/python3). A user who installed tokenjam with pip install tokenjam using a uv-managed Python (not a uv tool or uvx venv) would hit this branch and see "nothing persistent to remove; --purge is a no-op here" — silently skipping a real package removal.

Per the docstring, the empirically verified path for uvx/uv tool run ephemeral venvs always contains archive-v0. Tightening the check to "/uv/archive-v0/" in exe avoids the false positive for uv-managed Python while still catching all current ephemeral uvx venvs.

Suggested change
exe = sys.executable.replace("\\", "/")
return "/uv/" in exe or "/pipx/.cache/" in exe
exe = sys.executable.replace("\\", "/")
return "/uv/archive-v0/" in exe or "/pipx/.cache/" in exe
Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_uninstall.py
Line: 47-48

Comment:
**`/uv/` path match is too broad — falsely ephemeral on uv-managed Python**

`"/uv/" in exe` will match any path component literally named `uv`, including uv's own Python runtime installations (`~/.local/share/uv/python/cpython-3.12.0-linux-x86_64/bin/python3`). A user who installed tokenjam with `pip install tokenjam` using a uv-managed Python (not a uv tool or uvx venv) would hit this branch and see "nothing persistent to remove; --purge is a no-op here" — silently skipping a real package removal.

Per the docstring, the empirically verified path for `uvx`/`uv tool run` ephemeral venvs always contains `archive-v0`. Tightening the check to `"/uv/archive-v0/" in exe` avoids the false positive for uv-managed Python while still catching all current ephemeral uvx venvs.

```suggestion
    exe = sys.executable.replace("\\", "/")
    return "/uv/archive-v0/" in exe or "/pipx/.cache/" in exe
```

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.

@anshss anshss closed this Jul 9, 2026
@anilmurty
anilmurty deleted the fix-121-npx-remove branch July 29, 2026 23:07
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