Since the interactive mode replaced -p for Claude subscription turns
(43966a53, e5f66f8a, 10-31), a turn that succeeded told no
rate_limit_event, so NoteClaudeLimits never ran and the Usage page's
5h/7d figures moved only on a manual refresh (/usage). Only a turn that
ended in a <synthetic> API error told one, from its transcript row.
What changed:
- tap.js, when a main-class reply opens, also copies its
anthropic-ratelimit-unified-* response headers into the {s, open}
line as "h". It only reads them; the request to Anthropic is sent as
Claude Code made it.
- tap.go (rateLimitInfo) turns them into the rate_limit_info -p tells
(Claude Code 2.1.291): status plus unifiedWindows five_hour, seven_day,
seven_day_overage_included (5h, 7d, 7d_oi), each only when both its
utilization and reset are numbers, the reset rounded to a second.
- A streamed reply tells it just before its message_start, as -p does,
so the bridge keeps it before the reply begins. A reply whose message
was told whole from the transcript, or that never began one, keeps it
for the turn's end (tap.settle). Only the latest is told: a kept
reading from an older reply is never told after a later reply's
(toldOf), whether the older one ended before or after the later one
began, since NoteClaudeLimits keeps the last it hears.
- transcript.go: a <synthetic> API error settles the tap first, then
tells its own rate_limit_info, so the error's reading is the newest.
- docs/subsystems/claude-subscription-bridge.md says all of this.
The gateway is unchanged: it already turns a rate_limit_event into
NoteClaudeLimits and limitHeaders.
Run at this head (base origin/main 6c201095):
- gofmt -l on the changed Go files: nothing
- go vet ./internal/claudetui/, GOOS=linux go vet -tags nogui, GOOS=windows
go vet: clean
- go test -count=1 ./internal/claudetui/: ok (62s)
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway/ -run
'Claude|Tap|TUI': ok (60s)
Broken by hand:
- tap.go, tap.js and transcript.go at 6c201095 with the new tests: all of
TestTapTellsReplyLimits, TestTapLimitsOfMessageToldWhole,
TestTapErrorLimitsLast, TestShimTellsTapLimits and
TestTapKeptLimitsNotAfterNewer (both subtests) red.
- keepLimits without `n > t.toldOf`: TestTapKeptLimitsNotAfterNewer/
ended_after red (told 0.42 then 0.3). message_start without dropping an
older kept reading: /ended_before red the same way.
Not tried: a turn on a real claude.ai account, so the headers were not
checked against a captured reply; Windows and WSL; the full go test
./... suite.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review: chosen over #5012, which reads the same headers but emits a
rate_limit_event the moment its line arrives. That event can land while an
unstreamed message is pending, and the bridge settles that message early.
This one tells the limits just before message_start, keeps them for a cut
stream's end, and never tells an older reply's limits after a newer one's.
ddnio's real setup-token run on #5012 shows Bun's response headers are
readable there, the same thing this reads.
Run at the pushed head on mote, go1.26.8: go test -tags nogui -count=1
./internal/claudetui/ ok (83s).
Broken by hand: with the limits before message_start turned off,
TestTapTellsReplyLimits, TestShimTellsTapLimits and
TestTapKeptLimitsNotAfterNewer are red.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
Auto mode's safeguards change with every tool batch: prior_turn_context
grows by the turn before. An interactive Claude Code run read them once,
from CLAUDE_CODE_EXTRA_BODY in its environment as it started. So a tool
loop either started the run again (the whole conversation written to the
cache again), or kept it and sent the classifier the context it started
with, which is not what real Claude Code sends.
Each turn's safeguards now reach the running run:
- The bridge writes the extra body to <run tmp>/guard.json and hands its
path to the helper as MAGPIE_CLAUDE_GUARD (mapped for WSL in newTap).
- tap.js, on each /v1/messages request whose string body has a
safeguards key, reads the file and puts its safeguards in the body. It
also sets CLAUDE_CODE_EXTRA_BODY to the file's text, so Claude Code's
own later reads agree. The first time, it writes guard.json.live.
- setSafeguards rewrites the file (temp and rename, retried for Windows)
instead of restarting, once .live exists. Without .live (a Claude Code
that doesn't load the tap), it falls back to the old rule: keep the run
when only the varying parts changed, otherwise restart. A beta change,
or safeguards appearing or going, still restarts.
The tap changes only the safeguards value; any other byte of the body is
Claude Code's own. Bodies without safeguards, count_tokens and non-string
bodies are passed through untouched.
The tool-results path logged "process expired" alone while the run's end
read "magpie ended it". It now aborts the run with the reason and logs
"tool results process expired: <reason>". continueWith's "not waiting for
tool results" does the same.
Tests:
- TestClaudeTUIToolLoopCarriesEachBatchsSafeguards: three tool batches
with growing prior_turn_context, then the answer. Checks one run and
that each request carried its own batch's safeguards.
- TestClaudeTUIToolResultsLogWhyTheRunEnded: no tap and the rules change.
Checks that the log has the reason.
- TestTapPutsTheGuardFilesSafeguards (bun and node): the rewrite, the
bodies left alone, the env, and .live.
- TestTapEnv: MAGPIE_CLAUDE_GUARD is passed through, WSL-mapped.
Run (worktree at c360d9d30 + this change):
- mote: go test -run 'TestClaudeTUI|TestClaudeBridgeSafeguard|
TestClaudeToolResults|TestClaudeNoRunSaysWhy' ./internal/gateway ok
(73 PASS); go test -run TestTap ./internal/claudetui ok (bun and
node); go vet ./internal/claudetui ./internal/gateway on darwin, linux
and windows ok; gofmt -l internal clean.
- Full suite, go test -timeout 45m ./..., ran on the Linux box (wsl-run,
magpie-desktop), not mote. mote had two other sessions' GUI suites
sitting at 0% CPU for 3h42m at load 300, and one suite per machine.
Result: 80 packages ok, internal/claudetui ok. Red:
- TestGrokInstalled (#3548)
- TestCodexBodyLimitFromMemory (#5118)
- TestMoveCutShort (#5119)
- TestCCListingKeepsItsNames (#4925)
All four are red the same way run alone at origin/main d8b3749ef on
that box. Root, internal/cli and internal/gui don't build there
without GTK4 (#5120).
- Real use, macOS:
- Sandbox HOME and CLAUDE_CONFIG_DIR, the owner's Max account copied
to the sandbox dir's keychain item and deleted after. The refresh
token didn't rotate.
- A gateway on 127.0.0.1:3496; magpie built -tags dev at c360d9d30
(before) and with this change (after).
- Client: real interactive Claude Code 2.1.316 in tmux,
--permission-mode auto, sonnet 5.5 through the gateway.
- One turn, three sequential Bash calls (echo one, ls -la, echo three),
four upstream requests.
- Before: one run (pid 49488), cache_write 23005/86/162/86 (the first
request cold). Its CLAUDE_CODE_EXTRA_BODY stayed the first request's
safeguards, with no prior_turn_context, so requests 2 to 4 sent the
classifier stale context.
- After: one run (pid 64000), cache_read 23005/23091/23091/23253,
cache_write 0/0/162/86. guard.json.live was written; the tap logged
3 "guard":"set" (requests 2 to 4), and guard.json ended with 3
prior_turn_context entries.
Broken by hand:
- The old claude_subscription.go makes both gateway tests red ("request
0 carried ..."; the log said "tool results process expired" alone).
- tap.js without the rewrite makes TestTapPutsTheGuardFilesSafeguards
red in bun and node.
Not tried:
- A tool loop whose rules change mid-loop with a real client (restart
path; only the fake covers it).
- Linux or WSL with a real Claude Code (the TUI isn't used on Windows).
- Comparing the before/after upstream bodies byte for byte (the tap
logs that it set the value, not the bodies).
- The gateway package as its own -timeout 45m run on macOS: only the
targeted tests ran on mote, and the full package ran on Linux.
The owner: make a Claude subscription stream as it goes while Claude
Code runs in its interactive mode (TUI), on macOS, Windows and WSL.
Until now the helper read Claude Code's transcript, which has a message
only once it ends, so a reply came to the client whole.
Claude Code is a Bun program, and Bun loads a script named in
BUN_OPTIONS (--preload) before it. The helper writes tap.js to a folder
of its own and starts Claude Code with BUN_OPTIONS="--preload tap.js"
(after the user's own BUN_OPTIONS, but not a Windows one's for a Claude
Code in WSL, where both go through WSLENV). tap.js makes Bun.FetchSession
a subclass whose fetch is the real one's, so the request, its headers,
its connection and TLS are Claude Code's own. For a reply to a
request marked x-claude-code-request-class: main to /v1/messages that
is text/event-stream, it tees the body: Claude Code reads its copy as
before, and tap.js writes each event, as it comes, as a JSON line to
the file MAGPIE_CLAUDE_TAP names. It takes itself out of BUN_OPTIONS,
execArgv and the environment first, so nothing Claude Code runs
inherits it.
The helper reads that file and tells the events as -p's stream_event
lines, so the gateway reads them as it reads -p's. A transcript message
waits up to 1s for its stream to begin (then it's told whole, its late
stream left out); a turn's end waits for its streams to end; a stream
cut short ends with a message_stop and no message_delta, as -p ends
one. The bridge now keeps the streamed messages by id, since a whole
message can come during the next message's stream, and a client
tool_use shown from the stream isn't told again from the whole message.
On Windows, BUN_OPTIONS has no quoting, so a temp path with a space
takes its short name; one that still has whitespace gets no tap, and
replies come whole, as they do from a Claude Code that isn't Bun or
doesn't send through FetchSession (2.1.281 on Windows doesn't; 2.1.289
and 2.1.316 do).
Run:
- gofmt -l: clean. go vet on mote for darwin, GOOS=linux and
GOOS=windows: 0.
- go test ./internal/claudetui ./internal/gateway on mote
(-timeout 45m): ok, ok (gateway 654s).
- go test ./internal/claudetui with win-run (Windows 11) and wsl-run
(magpie-desktop): ok, ok.
- macOS, real account (Claude Code 2.1.316, sandbox HOME, keychain
token): a reply came as message_start, text deltas,
content_block_stop, message_delta, message_stop, then the whole
message, then the result; stderr clean, the tap's folder removed.
- Windows native, Claude Code 2.1.316 against a fake that sends an SSE
event every 250ms (a user preload marking the request main, which
also checks the user's BUN_OPTIONS is kept): the deltas came 250ms
apart, before the whole message. The same with TEMP holding a space
(short name).
- WSL magpie-desktop, Claude Code 2.1.289 run through wsl.exe from the
Windows helper, the fake in WSL: the same.
- The scrub, on macOS and Windows: Claude Code's BUN_OPTIONS and
execArgv have no tap, MAGPIE_CLAUDE_TAP is gone, --smol is kept.
- Rebased on origin/main 02ff0e908 (no file in common): on mote, vet
for darwin, windows and linux nogui 0; go test ./internal/claudetui
ok; -run 'CoreScriptBudget|I18n|Claude|Budget' ./internal/gui ok;
-run Claude ./internal/gateway ok. In WSL claude-mode, account-weight,
old-webkit and i18n-keys: 59 of 60, the one red i18n-keys' "Unmarked"
(#4524).
Broken by hand: the told mark removed, TestClaudeTUIStreamedCallToldOnce
went red; streamed kept as the last id only, TestClaudeTUILateRowOfStreamed
went red; before() made a no-op, and settle's end loop removed, the tap
tests went red. Each was put back.
TestTapMessageWaitsForItsStream failed now and then on mote: the whole
message can be told once its stream has begun, before the rest of the
stream is read. It now checks what is promised: the stream begins
before the message, every event is told, and the result comes last
(-count=30 ok).
Not tried: a real account on Windows or in WSL (the keychain token
went missing mid-session; those runs used the fake, whose request
carries no x-claude-code-request-class since a custom base URL drops
it); a real turn of several messages with the client's tools; Claude
Code 2.1.281 on Windows, which doesn't stream and is told whole as
before.