diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 35aace644..2078a5f44 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -267,9 +267,6 @@ the setting (`claudeTUIErr`, `claudeNotTUIWhy`). That is when - it is Claude Code in WSL whose home the probe didn't find, or, in the distro user's own folder, one Windows can't read (`wslread`); - the request has a JSON schema; -- it is a branch of a `-p` run's session (`branch`: see - [Tool results another request answers first](#tool-results-another-request-answers-first)); - a branch of an interactive run's session is interactive too. A first message of any length is pasted: a conversation told whole to a new Claude Code (one older than its session, or a client's own) can @@ -976,8 +973,12 @@ session's lines up to the reply's uuid into a session file of its own next to it, its `sessionId` set to the copy's id (`cutSession`, which waits up to `cutWait` for that line to be whole), and starts with `--resume ` in the mode its source run had (`savedSession.tui`): -interactive for an interactive run, `-p` for a `-p` one (`branch` in -`claudePrint` is only for the latter). Claude Code's own +interactive for an interactive run, `-p` for a `-p` one while the +interactive mode is off. With it on, a branch of a `-p` run's session (one +made before it was turned on) is told the whole conversation in a new +interactive Claude Code instead (`startRun`): its prompt isn't the +interactive mode's, so the cache has nothing of it to read. Until 11-01 +it was refused with a 502 (`branch` in `claudePrint`). Claude Code's own `--fork-session --resume-session-at` cut is not used, as the interactive mode ignores `--resume-session-at` and resumes the whole session, and a `-p` branch of an interactive run's session read only tools and system from diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index afbd2677d..ded95f88c 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -881,6 +881,14 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, from = nil } } + if from != nil && from == req.branch && !from.tui && claudeTUIOn() { + // a branch of a -p run's session, made before the interactive + // mode was turned on: -p's prompt isn't the interactive mode's, + // so the cache has nothing of it; a new interactive Claude Code + // is told the whole conversation, never a -p run + log.Printf("claude: a branch of a claude -p run's session goes to a new interactive Claude Code, told the whole conversation") + from, req.branch = nil, nil + } if from != nil && from.cut != "" { // a branch is a copy of the run's session as far as the reply // that made the calls, a session of its own: Claude Code's @@ -1169,9 +1177,6 @@ func claudePrint(req *Request, binary claudeCLI) (code, why string) { }{ {len(req.Format.schema()) > 0, "schema", "a JSON schema"}, {urlImages(req.Messages) > 0, "image", "an image known by its URL"}, - // a branch goes on in its session's mode: -p's prompt isn't the - // interactive mode's, and the cache has the one its run asked - {req.branch != nil && !req.branch.tui, "branch", "a branch of a claude -p run's session"}, } { if c.is { return c.code, c.why diff --git a/internal/gateway/claude_tui_branch_test.go b/internal/gateway/claude_tui_branch_test.go new file mode 100644 index 000000000..e6d4bea62 --- /dev/null +++ b/internal/gateway/claude_tui_branch_test.go @@ -0,0 +1,74 @@ +package gateway + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// A branch of a claude -p run's session, made before the interactive mode +// was turned on, goes to a new interactive Claude Code told the whole +// conversation: -p's prompt isn't the interactive mode's, so the cache has +// nothing of it. It was refused with the interactive mode on. +func TestClaudeTUIBranchOfPrintRunIsToldWhole(t *testing.T) { + h := tuiScriptHarness(t, "") + h.ask("rules", "a0") + // the run waits for the conversation's next turn: its session's + // folders are where a -p run's would be + var sessions []string + h.s.subscription.mu.Lock() + for _, run := range h.s.subscription.idle { + run.mu.Lock() + sessions = run.sessions + run.mu.Unlock() + } + h.s.subscription.mu.Unlock() + if len(sessions) == 0 { + t.Fatal("no run waits with its session's folders") + } + if err := os.MkdirAll(sessions[0], 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(sessions[0], "s-print.jsonl"), []byte(`{"type":"assistant","uuid":"u-print","sessionId":"s-print"}`+"\n"), 0o600); err != nil { + t.Fatal(err) + } + before := h.runs() + msgs := []Message{ + {Role: "user", Parts: []Part{{Kind: Text, Text: "first words"}}}, + {Role: "assistant", Parts: []Part{{Kind: Text, Text: "ok"}}}, + {Role: "user", Parts: []Part{{Kind: Text, Text: "later words"}}}, + } + from := &savedSession{session: "s-print", sessions: sessions, at: time.Now(), since: msgs[2:], cut: "u-print", gone: true} + req := &Request{Messages: msgs, branch: from} + run, _, err := h.s.subscription.start(context.Background(), req, "claude-sonnet-5", h.config, "", from) + if err != nil { + t.Fatal(err) + } + if !run.tui { + t.Fatal("a branch of a -p run's session ran as -p") + } + var log string + eventually(t, "the branch's run told its turn", func() bool { + log = newRun(h, before) + return strings.Contains(log, "later words") + }) + if strings.Contains(log, "--resume") || !strings.Contains(log, "first words") { + t.Fatalf("the branch's run wasn't a new one told the whole conversation: %s", log) + } + if req.branch != nil { + t.Fatal("the request still names the -p run's branch") + } +} + +// newRun is the log of the run h started since before. +func newRun(h *resumeHarness, before map[string]string) string { + for name, log := range h.runs() { + if _, ok := before[name]; !ok { + return log + } + } + return "" +}