iwinux on X: a Pi request through a group a Claude subscription serves
was listed twice in the request log, once as "Pi · Claude Code" and once
as "Claude Code · Local session", with the same tokens and cost.
Cause: a bridge run is the real Claude Code. It saves its session in the
account's projects folder, in the caller's folder's project: the user's
own ~/.claude or a saved account's claude-accounts folder, both of which
appdir.ClaudeHomes lists. listCCFiles read it as a session of the user's,
and the ledger's pairing (request id, then session and tokens) didn't
match it with the gateway's record, whose session is the caller's. So
every call was counted twice: request log, usage ledger, cost totals,
the Sessions page. -p runs and interactive (TUI) runs alike, plus a
branch's cut copy and a replayed transcript.
Fix: every session a bridge run saves is named by the new
internal/bridgesid. New is a version 4 UUID whose last six bytes are a
keyed SHA-256 of its first ten, the key a random file of this computer's
(<config>/claude-bridge-session-key, made once by temp file + link). To
Anthropic, which Claude Code sends the session id to, it is as random as
any, so it marks nothing upstream. startRun passes --session-id for a new
run (-p and interactive; the TUI helper keeps a --session-id it is given
and makes a marked one otherwise), replaySession and cutSessionWhere name
their files with it. listCCFiles leaves out a .jsonl, and a session's
sub-agent folder, whose id bridgesid.Is knows, for agent "claude" only.
That one listing feeds calls, the ledger, the request log, Sessions,
traces and WSL distros, so all of them drop the double count. Other
bridged runs (warm-up, askClaude, /usage, WSL -p) use
--no-session-persistence and write no session.
Rows already counted twice: a bridge session already removed (a run's
session is removed when it ends) is gone from the ledger already, as it
is read from the files there are. One still on disk under an old random
id (a live or shelved run resumed under it, or a stopped gateway's
leftover in a caller's folder) can't be told from the user's and stays
counted until it is removed; old ids carry no mark to fix it on read.
Tests: TestNewIsKnown, TestKeyKept, TestKeyMadeOnce (bridgesid);
TestBridgeSessionsAreNotLocal (sessions: a marked session in the user's
folder, its sub-agent file, and one in a saved account's folder give no
calls and no rows; the user's session beside them counts);
TestClaudeRunSessionsAreTheBridges (-p and interactive, a new run and a
replayed transcript) and TestClaudeCutSessionIsTheBridges (gateway).
Broken by hand, on mica: bridged() returning false made
TestBridgeSessionsAreNotLocal red (calls req_m1..m4, want req_m1 alone);
newUUID back in startRun, replaySession and cutSessionWhere made both
gateway tests red (print, interactive and the cut copy).
Run on mica at this head (rebased on 7ea1e6a4b): gofmt -l clean, go vet
./... ok, go build ok; go test -tags nogui ./internal/bridgesid
./internal/sessions ./internal/usage ok; ./internal/gateway -run
'Claude|Subscription|Bridge|Fork|Shelf' ok. At the first base (8a59e7616):
the same vet plus GOOS=linux -tags nogui and GOOS=windows vet ok, the three
builds ok, ./internal/claudetui ./internal/appdir ./internal/masking ok.
The full ./... suite was not run: mica's suite lock had five full suites
queued ahead of it, and this ships now.
Not tried: a real Claude Code run with a real account (mote is down;
the -p --session-id flag is Claude Code's documented one, and the
interactive helper already passed it); Windows and WSL runs.