Files
magpie/internal/claudetui/command.go
T
yetone d39358f1bb claude: /usage and "hi" reads run in magpie's work folder and say why they ended (#4830)
mydosu's Claude Code quota reads on Windows failed for both accounts
with "exit status 1" alone. Two things were behind it:

- Each one-off interactive run (claudeUsageTUI, askClaudeTUI) started in
  a new magpie-claude-<n> temp folder, which the helper trusted, so each
  read wrote one more trusted projects entry into the config's state file.
  The reporter had 67 in ~/.claude.json.
- The caller's budget (subscriptionTimeout, 20s) is shorter than the
  helper's own waits (45s for the prompt, 20s for a panel). The context's
  kill came first, and that is TerminateProcess on Windows, so Wait said
  "exit status 1" with an empty stderr. On Unix the same kill says
  "signal: killed".

What changed:

- One-off TUI runs work in claudeWorkDir (oneOffDir). That is the
  owner-checked folder the bridge's own runs already use, so each config
  directory holds one trusted entry instead of one per read. A temp
  folder is made only when that folder can't be.
- The run's session file there is removed by the session_id the helper's
  init line names (dropOneOff). It is not removed under WSL.
- The helper is told its caller's deadline (claudetui.DeadlineEnv, the
  context's deadline less min(3s, a quarter of what is left)). At that
  time it ends on its own, saying what Claude Code hadn't done and its
  last screen, e.g. "Claude Code hadn't drawn /usage's panel in the
  2.9s its caller waits for it: ...".
- A run that still says nothing reads "Claude Code's interactive mode
  said nothing before magpie stopped waiting for it (...)" (oneOffErr),
  not an exit code.
- quotaError says these reasons on the card as "Allowance unavailable:
  {why}" (an existing key in every language). The full text stays in
  the tooltip. Nearby comments were cut so the core script budget holds
  without a raise.
- No -p fallback was added. The -p asks keep their own temp folder,
  since -p asks nothing about trust.
- The entries older magpies left in ~/.claude.json are left alone:
  magpie never writes that file.

Run at this head (after the rebase on c875e66c2), on mote:

- go test -count=1 ./internal/claudetui: ok.
- go test -count=1 -run 'Claude|TUI|Usage|Warm|Ask' ./internal/gateway: ok.
- go test ./internal/gui -run 'Budget|I18n|Script': ok.
- node --test claude-quota-onboarding (chromium and webkit, en, zh,
  zh-TW, ja and de, 440px) and i18n-keys: 14 of 14.
- GOOS=linux and GOOS=windows go vet of claudetui and gateway: clean.
- gofmt -l: clean.

On xiaomi-gpu (Windows 11, win-run):

- TestShimEndsAtItsCallersDeadline passes through ConPTY.
- TestShimFirstMessageWaitsForTools and TestShimReadsUsage are red the
  same way on origin/main c875e66c2. They are filed as #4810 and #4820.
- The real Claude Code 2.1.316 ran under a sandbox HOME and
  CLAUDE_CONFIG_DIR, with fake credentials and a rebuilt magpie.exe.
  Before the change, each run left a new trusted temp entry. With
  MAGPIE_CLAUDE_TUI_BY 3s out, the helper now exits saying "Claude Code
  hadn't drawn /usage's panel in the 2.9s its caller waits for it: <its
  screen>". Untrusted, it says the trust reason.

Broken by hand (each went red):

- The helper ignores DeadlineEnv: claudetui's deadline test.
- oneOffDir never uses claudeWorkDir: two different pwds and sessions
  left.
- removeSession dropped: "session was left".
- oneOffErr's context branch removed: got "signal: killed".
- helperDeadline not passed: "helper was told by:" empty.
- quotaError's new lines removed: the onboarding GUI test shows
  "Allowance unavailable" alone.

Not tried:

- A real signed-in Claude account on Windows. So which step is slow on
  the reporter's machine (start-up, the prompt or the panel) is not
  known. The card now names it.
- Unrelated, seen while breaking it: with no deadline, a /usage whose
  panel is never drawn ends after 20s as a success with an empty result.

Reported by mydosu (myduso_27930 on Discord).
2026-11-02 10:14:49 +08:00

52 lines
2.4 KiB
Go

package claudetui
// Command is the hidden subcommand that runs it.
const Command = "claude-tui-helper"
// TrustEnv, "1", has the shim tell Claude Code yes when it asks whether
// to trust the folder it runs in: the bridge sets it for magpie's own
// folder, and for a caller's in an account folder of magpie's, whose
// Claude Code no one else runs. Otherwise the question is the user's to
// answer in their own Claude Code, and the run ends, saying so.
const TrustEnv = "MAGPIE_CLAUDE_TUI_TRUST"
// DeadlineEnv holds, in Unix milliseconds, when a one-off run's caller
// (a /usage read, a test's "hi") stops waiting for it. The helper ends
// then on its own, saying on its stderr what Claude Code hadn't done and
// what it last drew. Killed at the caller's deadline instead, it said
// nothing, and the reading's error was its exit code alone, "exit status
// 1" on Windows (#4830).
const DeadlineEnv = "MAGPIE_CLAUDE_TUI_BY"
// Untrusted begins what the helper says, on its stderr, as it ends over a
// folder it wasn't told to trust: the bridge then runs the turn in a
// folder of magpie's.
const Untrusted = "Claude Code asks whether to trust the folder"
// WSLEnv holds, as JSON (WSL), how to run a Claude Code that is in a WSL
// distro: the helper, on Windows, runs wsl.exe in its pseudo console.
const WSLEnv = "MAGPIE_CLAUDE_TUI_WSL"
// WSL is a Claude Code in a WSL distro, as the helper runs it.
type WSL struct {
// Args are wsl.exe's arguments up to Claude Code's own, its path last
Args []string `json:"args"`
// Mount is where the distro mounts Windows' drives ("/mnt/"): an
// image's path is pasted as Claude Code there reads it
Mount string `json:"mount"`
// Projects is Claude Code's projects folder, as Windows reads it; ""
// with Distro set when Windows can't, or its home is unknown: the
// transcript is then read inside the distro (wslread.go)
Projects string `json:"projects,omitempty"`
// Distro is the distro, as wsl.exe -d names it
Distro string `json:"distro,omitempty"`
}
// MCPCommand is the hidden subcommand of magpie's tool server, the MCP
// server a run's --mcp-config names "magpie" (claudebridge.RunMCP). Its
// arguments are the callback's file, the tools' file and, for the
// interactive mode, a file it writes once Claude Code has listed its
// tools: the first message waits for it (MinhuWang on X: a first message
// sent before then had none of the agent's tools).
const MCPCommand = "claude-mcp-helper"