Skip to content

fix(claude): isolate the account-usage query process with --safe-mode - #29

Merged
xxchan merged 1 commit into
mainfrom
kai/claude-usage-safe-mode
Sep 29, 2026
Merged

xxchan merged 1 commit into
mainfrom
kai/claude-usage-safe-mode

Conversation

@TennyZhuang

Copy link
Copy Markdown
Contributor

Fixes #28. Reviewed on the Raft side by @XX, who asked for both points below.

Problem: claudeAccountUsage spawns claude -p … with the user's full config. On CLI 2.1.283, every usage read ran the user's SessionStart hook and started their MCP servers, at both user level (CLAUDE_CONFIG_DIR) and project level. The HTTP reader in 0.0.7 had no such side effect.

Change

  • Add --safe-mode to the query process. It disables hooks, MCP servers, plugins and CLAUDE.md while keeping auth, and initialize/get_usage answer as before.
  • Fallback: if claude --help doesn't list --safe-mode, return { kind: "unsupported", reason: "unsupported_installation" }. The reader never launches the CLI without isolation. The help probe takes about 60 ms. Only a successful probe is cached (per command and version), so a transient probe failure is retried on the next read.
  • Tests pin --safe-mode in the spawn args, check that an old CLI is never spawned, and check that a failed probe isn't cached. Removing the flag fails 1 test; skipping the support check fails 2.

Verification

  • typecheck, lint, the full vitest suite (362 passed, 27 skipped) and the build all pass.
  • Live runs with the built package (CLI 2.1.283):
Setup Hook / MCP markers Result
0.8.0, user-level config SessionStart + MCP both fired reads usage
This PR, user-level config none reads usage
This PR, project-level config none available, 3 windows

The user-level runs return quota_unavailable on both 0.8.0 and this branch; that comes from the empty temp config dir, not from this change.

Should ship in the same release as #27 (Kimi nickname), so that Raft can upgrade once, from 0.0.7 straight to that version.

🤖 Generated with Claude Code

The native get_usage reader spawned `claude -p` with the user's full
configuration, so every usage read ran their SessionStart hooks and
started their MCP servers (verified live on CLI 2.1.283, user- and
project-level). --safe-mode disables those while keeping auth, and
get_usage still answers.

A CLI whose --help lacks --safe-mode is reported as
unsupported_installation instead of being launched unisolated. Only a
successful help probe is cached per command+version.

Fixes #28

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xxchan
xxchan merged commit b151959 into main Sep 29, 2026
12 checks passed
xxchan added a commit that referenced this pull request Sep 29, 2026
Upgrade Pi to 0.87.1 and pin undici to 8.10.2, matching the SDK
dependency and removing the affected 8.9.0 version. Update the Pi
system-prompt snapshot for its new tagged format and bump both packages
to 0.9.0 for the release containing #27 and #29.

Fixes #30.

Validation: typecheck and lint pass; Pi behavior suite and strict
CI-mode vendor snapshots pass. Full local suite encountered a process
timing failure under heavy machine load; CI will validate on clean
runners before release.
@xxchan
xxchan deleted the kai/claude-usage-safe-mode branch October 3, 2026 07:50
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.

claude accountUsage spawns a full-config CLI: user hooks fire and MCP servers start on every usage read

2 participants