Files
magpie/docs/subsystems/claude-subscription-bridge.md
T
yetone 055f17883f gateway: a Claude subscription conversation the client sends back rewritten goes on in the run that had its last turn, and Claude Code's temp images go with their session (#912, margbug01) — Alma takes the [Context: …] block out of its last user message once the turn is over, the <alma_notification> block out of the end of the reply, and picks the tools for each turn, so turnKey, which hashes all of it, never matched and every turn started a new claude -p told the whole conversation, written to the prompt cache again with its images. ended keeps a looseTurn beside the key: the settings without the tools, the conversation before the last turn hashed, and the last turn's messages one by one. When the exact key misses, rewritten takes the one idle run or saved session whose looseTurn has the same owner, settings and earlier turns, the same roles, tool calls and results in the last turn, where a user message only had one part taken out (cutFrom) and the reply is a non-empty start of what the run said. Two that could be it start a new run, as does an idle run not told a tool offered now (offers) or a saved session one of whose tools is gone (toolsCover). Claude Code's <CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>/<work project>/<session> folder, where it keeps the images it was given, is removed with the session (removeSession), and the sweep removes those a stopped gateway left once their session file is gone and nothing in them changed for a day; CLAUDE_CODE_TMPDIR joins agentenv.Vars. Old images are still sent to a new run: replacing them would change what the model sees. docs/subsystems/claude-subscription-bridge.md is new. claude_rewritten_test.go has a case for each relaxation (failing before the change) and one for each rule that keeps another conversation out (each failing with that rule taken out). Tested for real with the owner's Claude Max account in a sandbox HOME: turn one asked with a [Context] block and two tools, turn two sent it back with the block taken out, the reply cut short and one tool, and went on in the same claude process ("goes on in its run", cache_read 1932). go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 22:56:30 +08:00

5.1 KiB

Claude subscription bridge: turns across Claude Code runs

A Claude subscription's requests are answered by the local claude binary (internal/gateway/claude_subscription.go, serveClaudeSubscription). This page covers how a conversation's turns find the Claude Code process, or the saved session, that had its last turn, and what a run leaves on disk. Tool calls within a turn (match, continueWith) are only touched on where they meet it.

Why turns go on in one run

A run started anew is told the whole conversation in one user message (renderClaudePrompt), a prefix the prompt cache has never seen, so the conversation is written to the cache again and its images are sent again. A turn that goes on in the run that had the last one is told only the messages since (renderClaudeTurn), and Anthropic reads the rest from the cache.

Runtime path for a new turn

serveClaudeSubscription's start tries, in order:

  1. subscriptionBridge.resume: an idle run waiting at the conversation the request goes on from (b.idle). It is written the turn's messages on its stdin, as a user message with a uuid it is rewound to if the client gives up on the turn (letGo).
  2. subscriptionBridge.unshelve: the saved session of a run let go past idleMost (b.shelf, at most shelfMost), which a new Claude Code starts from with --resume, told the turn's messages alone.
  3. retire, then a new run told the whole conversation.

Finding the run: keys

  • nextTurn splits a request at its last reply that calls no tool: the conversation up to it, and the messages since (with any calls and results the client made itself after it).
  • ended keeps a run whose reply was whole under turnKey(owner, req, messages + reply): the account, model, whether an effort was asked, thinking off, tool choice, system prompt, the tools, web search, schema, and the messages as hashMessages reads them. The exact match is that key, from nextTurn's split of the next request.
  • It also keeps a looseTurn: the same settings without the tools, the message count, the conversation before the last turn (hashed), and the last turn's messages one by one (messageWords), the reply last. Shelving copies it, with the tool names the run's agent had, to the savedSession.

A conversation the client rewrote (#912)

Some clients send a turn's conversation back changed. Alma takes the [Context: …] block it gave the latest user message out once the turn is over, and its <alma_notification> block out of the end of the reply, and picks the tools for each turn. When the exact key misses, rewritten compares the request's looseTurn with every idle run's and saved session's. The request matches one when all of these hold:

  • the same owner, settings and conversation before the last turn (looseTurn.key);
  • the last turn has the same messages, roles, tool calls and results, and only these changed (looseTurn.rewrote):
    • a user message had one part taken out (cutFrom), not all of it;
    • the reply is a non-empty start of what the run said.

It is taken only when exactly one run or saved session matches. Two that could be it are two conversations alike as far as their words go, and a run handed the other one would answer from what that one was told.

An idle run is taken only when its agent was told every tool offered now (offers). Claude Code is not told of new tools at a turn's start, only with tool results. A saved session is taken only when the tools offered now include every one its agent had (toolsCover). It is resumed with the new list, and its earlier calls name no tool that is gone.

The run goes on with the conversation as it told it. Its Claude Code still holds the context block and the notification. The client's next turn brings its own context.

Files a run leaves

  • Its session: <config>/projects/<work project>/<session>.jsonl, for --resume (claudeSessions). It is removed when the run ends unless shelved, and when a saved session is discarded (removeSession). sweepSessions removes those a stopped gateway left, once older than idleLongest.
  • Claude Code's temp files for the session, the images it was given: <CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>/<work project>/<session> (claudeTempDir, not on Windows). They are removed with the session (removeSession). The sweep removes those whose session file is gone and in which nothing changed for tempLongest (a day). The folder is the work folder's for every account, and another account's session files are in its own config folder.

A run started anew still sends every image in the conversation. Replacing earlier images with a placeholder would change what the model sees, and magpie doesn't do it.

Verification

go test -tags nogui ./internal/gateway/ -run 'ClaudeRewritten|ClaudeSessionTempFiles|ClaudeLetGo|ClaudeSessionFiles|ClaudeOldSessions' -count=1

claude_rewritten_test.go has a case for each relaxation and one for each rule that keeps another conversation out. claude_resume_test.go's harness stands in for Claude Code with a script that keeps sessions as it does.