claude: a branch of a -p run's session goes to the interactive mode, told whole

With the interactive mode on, tool results another request answered
first for a run made as claude -p (before the mode was turned on) were
refused with a 502 (claudePrint's "branch"): the branch would have gone
on as -p. It now starts a new interactive Claude Code told the whole
conversation (startRun): -p's prompt isn't the interactive mode's, so
the cache has nothing of the -p session to read either way, and nothing
runs as -p while the mode is on. This is the second refusal behind
@crosscn's 502 on an old conversation (X, 11-01); the first, words over
400KB, went in v0.1.2614.

- startRun drops such a branch (from and req.branch nil) and logs it;
- the "branch" entry in claudePrint is gone;
- docs/subsystems/claude-subscription-bridge.md says so.

Test: TestClaudeTUIBranchOfPrintRunIsToldWhole (a -p run's saved
session given as the branch: the run is interactive, has no --resume,
and is told the first words too; req.branch is cleared).

Run on mote (go1.26.8), at origin/main da275848b plus this change:
gofmt, vet (darwin, windows, linux -tags nogui) ./internal/gateway ok;
go test -run Claude ./internal/gateway ok (61s); the fork, branch and
cut tests (-run 'Fork|Branch|Cut') ok before the rebase.

Broken by hand: the new block turned off: the test red, the run resumed
the -p session with -p's arguments.

Not tried: a real -p branch with real Claude Code (it needs a -p run's
lead parked on calls when the mode is switched); whether @crosscn's
conversation reached this path rather than the 400KB one; the full go
test ./... (the gateway's Claude tests ran).
This commit is contained in:
yetone
2026-11-01 21:36:20 +08:00
parent 900852e1c9
commit 049848c5ed
3 changed files with 88 additions and 8 deletions
@@ -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 <copy>` 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
+8 -3
View File
@@ -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
@@ -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 ""
}