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 on7ea1e6a4b): 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.
54 lines
2.2 KiB
Go
54 lines
2.2 KiB
Go
package sessions
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/bridgesid"
|
|
)
|
|
|
|
// A session the Claude subscription bridge's Claude Code saved, in the
|
|
// user's own Claude folder or a saved account's, is not one of the user's:
|
|
// the gateway counts its calls already, under the agent that asked. Read
|
|
// as a local session too, each call was listed and counted twice (iwinux
|
|
// on X: a Pi request through a Claude subscription group, again as
|
|
// "Claude Code · Local session", the same tokens and cost). Its
|
|
// sub-agents' files go with it; the user's sessions, beside it in the
|
|
// same project, are counted as before.
|
|
func TestBridgeSessionsAreNotLocal(t *testing.T) {
|
|
d := setupCalls(t)
|
|
config := filepath.Join(t.TempDir(), "config")
|
|
t.Setenv("XDG_CONFIG_HOME", config)
|
|
own, other := bridgesid.New(), bridgesid.New()
|
|
saved := filepath.Join(config, "magpie", "claude-accounts", "a1")
|
|
user := func(id string) string {
|
|
return `{"type":"user","timestamp":"` + stamp(0) + `","message":{"role":"user","content":"hello"},"cwd":"/work/app","sessionId":"` + id + `"}`
|
|
}
|
|
// the user's own session, and a bridge run's in the same project
|
|
writeLines(t, filepath.Join(d.claude, "projects", "-work-app", "main1.jsonl"),
|
|
user("main1"), swap(claudeMsg("m1", "claude-opus-5-5", 1, 2, 0, 0, 1), `"sess1"`, `"main1"`))
|
|
writeLines(t, filepath.Join(d.claude, "projects", "-work-app", own+".jsonl"),
|
|
user(own), swap(claudeMsg("m2", "claude-opus-5-5", 2, 490, 0, 0, 2), `"sess1"`, `"`+own+`"`))
|
|
writeLines(t, filepath.Join(d.claude, "projects", "-work-app", own, "subagents", "agent-a.jsonl"),
|
|
swap(claudeMsg("m3", "claude-opus-5-5", 3, 4, 0, 0, 3), `"sess1"`, `"`+own+`"`))
|
|
// a saved account's run
|
|
writeLines(t, filepath.Join(saved, "projects", "-work-app", other+".jsonl"),
|
|
user(other), swap(claudeMsg("m4", "claude-opus-5-5", 5, 6, 0, 0, 4), `"sess1"`, `"`+other+`"`))
|
|
|
|
got := map[string]int{}
|
|
for _, c := range Calls(time.Time{}) {
|
|
got[c.RequestID]++
|
|
}
|
|
if len(got) != 1 || got["req_m1"] != 1 {
|
|
t.Fatalf("calls %v, want the user's req_m1 alone", got)
|
|
}
|
|
rows := map[string]int{}
|
|
for _, s := range List(0) {
|
|
rows[s.ID]++
|
|
}
|
|
if len(rows) != 1 || rows["main1"] != 1 {
|
|
t.Fatalf("sessions %v, want the user's main1 alone", rows)
|
|
}
|
|
}
|