diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 2bc7376d7..bd03b731c 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -746,9 +746,26 @@ after the last reply, with nothing in the log). A turn that finds no run under its own key takes one whose `behindKey` is its key, rewinds it to `behindTurn` (as `letGo` does) and tells it only the turn's messages ("taken back from the side question it answered -since"). The run is marked `rewound`, so it isn't shelved. A run that -can't be rewound is let go and the turn told whole to a new run, as -before. Tried with the real 2.1.290 client in `-p --prompt-suggestions` +since"). The run is marked `rewound`, so it isn't shelved until it has +answered a whole turn since (`replied`, set as a turn ends with `stop`, +cleared at each take-back): the session's lines after the rewind chain +from the line before the side question, which is what `--resume` goes +on from. A run that can't be rewound is let go and the turn told whole +to a new run, as before. + +A run let go past `idleMost` while it waits at a side question's reply +is shelved with its `behindKey` too (#4914, ggbdpq: with suggestions on, +the next turn after a let-go read 19,844 from the cache and wrote 29,345; +with them off, 49,560 read and 50 written). A turn that finds no session +under its own key takes the newest one whose `behindKey` is its key, and +the run that takes it resumes a copy cut before the side question +(`cutBehind`): before the `-p` run's message `behindTurn` +(`cutSessionBefore`), or after an interactive run's `behindLine` +(`cutSession`). The shelved file is removed. Resumed whole, Claude Code +would go on with the suggestion's exchange as the conversation's, which +the client never had. A session that can't be cut is let go and the turn +told whole, as before. `TestClaudeLetGoConversationAfterSuggestionResumesItsSession` +holds it, with a stand-in for Claude Code; not tried with the real one. Tried with the real 2.1.290 client in `-p --prompt-suggestions` and the real 2.1.290 as the run, against a local mock of Anthropic: one run for the whole session, and the turn after each suggestion went on from the reply before it. Other side requests that go on from the diff --git a/internal/gateway/claude_resume_test.go b/internal/gateway/claude_resume_test.go index 0a0b8bb30..eca54f557 100644 --- a/internal/gateway/claude_resume_test.go +++ b/internal/gateway/claude_resume_test.go @@ -77,13 +77,18 @@ echo '{"type":"system","subtype":"init","session_id":"'$sid'"}' while read -r line; do case $line in *'"type":"control_request"'*) echo "control $line" >> $log - # acknowledged as Claude Code does: initialize, a settings change id=$(printf '%s' "$line" | sed 's/.*"request_id":"\([^"]*\)".*/\1/') - echo '{"type":"control_response","response":{"subtype":"success","request_id":"'$id'"}}' + # acknowledged as Claude Code does: initialize, a settings change, a + # rewind of the conversation it was asked to take back + case "$line" in *rewind_conversation*) + echo '{"type":"result","subtype":"error_during_execution","is_error":true,"result":""}' + echo '{"type":"control_response","response":{"subtype":"success","request_id":"'$id'","response":{"rewound":true}}}';; + *) echo '{"type":"control_response","response":{"subtype":"success","request_id":"'$id'"}}';; + esac continue ;; esac echo "told $line" >> $log - if [ -n "$file" ]; then echo "$line" >> "$file"; fi + if [ -n "$file" ]; then printf '%s\n' "$line" >> "$file"; fi echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}' echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":` + strconv.Quote(reply) + `}}}' echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}' @@ -205,6 +210,84 @@ func TestClaudeLetGoConversationResumesItsSession(t *testing.T) { } } +// A run that answered the conversation's prompt suggestion since its last +// turn, and was let go past idleMost, leaves its session where the +// conversation's next turn comes by: that turn resumes a copy of it cut +// before the suggestion, which the client's conversation never had, and +// Claude Code goes on from it told the new message alone. +// Before, the conversation's own turn had taken the run back from the +// suggestion (#1638), its session marked taken back, and the shelf took +// none of it: the next turn started a new Claude Code told the whole +// conversation in one message, written to the prompt cache again (#4914). +func TestClaudeLetGoConversationAfterSuggestionResumesItsSession(t *testing.T) { + h := newResumeHarness(t) + h.ask("rules A", "a1") + runs := h.runs() + if len(runs) != 1 { + t.Fatalf("runs: %v", runs) + } + var sidA string + for pid := range runs { + sidA = "s" + pid + } + // the prompt suggestion after each turn: the conversation and one + // message more + h.ask("rules A", "a1", suggestionMode) + h.ask("rules A", "a1", "a2") + h.ask("rules A", "a1", "a2", suggestionMode) + if n := len(h.runs()); n != 1 { + t.Fatalf("%d runs, the conversation's own all along", n) + } + // let go past idleMost, its session stays for its conversation's next + // turn, whatever suggestions it answered since + for i := range idleMost { + h.ask(fmt.Sprintf("rules %d", i), "x") + } + h.s.subscription.mu.Lock() + shelf := len(h.s.subscription.shelf) + h.s.subscription.mu.Unlock() + if shelf != 1 { + t.Fatalf("shelf: %d, the session was not kept for the conversation's next turn", shelf) + } + before := h.runs() + h.ask("rules A", "a1", "a2", "a3") + var resumed string + for pid, log := range h.runs() { + if _, ok := before[pid]; !ok { + resumed = log + } + } + _, after, ok := strings.Cut(resumed, "--resume ") + cut := "" + if f := strings.Fields(after); len(f) > 0 { + cut = f[0] + } + if !ok || cut == "" || cut == sidA { + t.Fatalf("the conversation's next turn did not resume a copy of its session cut before the suggestion:\n%s", resumed) + } + file := "" + dirs, _ := filepath.Glob(filepath.Join(h.config, "projects", "*")) + for _, dir := range dirs { + if b, err := os.ReadFile(filepath.Join(dir, cut+".jsonl")); err == nil { + file = string(b) + } + if _, err := os.Stat(filepath.Join(dir, sidA+".jsonl")); err == nil { + t.Errorf("the shelved session's own file is left beside its copy") + } + } + // the copy is the session as it stood before the second suggestion, + // a3 told after it; the first suggestion's exchange, taken back, + // sits above a2, off the line Claude Code resumes + lines := strings.Split(strings.TrimSpace(file), "\n") + if n := len(lines); n < 2 || !strings.Contains(lines[n-2], `"a2"`) || !strings.Contains(lines[n-1], `"a3"`) || strings.Count(file, "SUGGESTION") != 1 { + t.Fatalf("the resumed session wasn't cut before the second suggestion:\n%s", file) + } + told := resumed[strings.Index(resumed, "\ntold "):] + if !strings.Contains(told, `"a3"`) || strings.Contains(told, `"a1"`) || strings.Contains(told, `"a2"`) || strings.Contains(told, "SUGGESTION") { + t.Fatalf("the resumed run was told more than the turn:\n%s", told) + } +} + // Sessions are files of their conversations: one is removed with its run // when nothing will resume it (a one-off ask, a run that ended), and a // saved one once its conversation's next turn takes it, or it waited as diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 7345442f6..a3ca31cb5 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -230,6 +230,11 @@ type subscriptionRun struct { idleKey string idleAt time.Time convKey string + // convPrev is the boundary before convKey's (conversationKeys): the + // conversation as the client has it, when the run went on with a side + // question after its last turn — a prompt suggestion — whose exchange + // the client's own next turn leaves out + convPrev string // handed is set (b.mu held) when its account was handed over to // Claude Code itself during a turn (handedOver): it isn't kept for // the next one @@ -275,6 +280,11 @@ type subscriptionRun struct { sessions []string shelved bool rewound bool + // replied says it answered a whole turn since its conversation was + // taken back (rewound): the file's lines since are that turn and what + // came after, and Claude Code's resume goes on from them, whatever the + // taken-back turn's own still sit above (shelve) + replied bool // effort is the level its Claude Code thinks at, as it started (its // --effort) or was told since (setEffort); "" is Claude Code's own @@ -1663,9 +1673,10 @@ func (b *subscriptionBridge) resume(req *Request, owner string) (*subscriptionRu run.abortFor("it could not be taken back from a side question: " + err.Error()) return nil, nil } - // its session file may not say so: it isn't shelved (shelve) + // its session file may not say so: it isn't shelved until the run + // answers a whole turn (shelve) run.mu.Lock() - run.rewound = true + run.rewound, run.replied = true, false run.mu.Unlock() log.Printf("claude: the conversation's next turn goes on in its run, taken back from the side question it answered since") } @@ -2010,6 +2021,9 @@ func (r *subscriptionRun) ended(req *Request, said, stop string, ok bool) { if !ok || stop != "tool" { r.turnUUID, r.backKey, r.backLine = "", "", "" } + if ok && stop == "stop" { + r.replied = true + } r.mu.Unlock() switch { case !ok: @@ -2040,7 +2054,8 @@ func (r *subscriptionRun) ended(req *Request, said, stop string, ok bool) { msgs := append(req.Messages[:len(req.Messages):len(req.Messages)], reply) key := turnKey(r.owned(), req, msgs) conv := "" - if keys := conversationKeys(r.owned(), msgs); len(keys) > 0 { + keys := conversationKeys(r.owned(), msgs) + if len(keys) > 0 { conv = keys[len(keys)-1] } loose := newLooseTurn(r.owned(), req, msgs, true) @@ -2050,6 +2065,12 @@ func (r *subscriptionRun) ended(req *Request, said, stop string, ok bool) { // it: a side question Claude Code asked of it is taken back from // there when the conversation's own next turn comes (resume) r.behindKey, r.behindTurn, r.behindLine = back, turn, backLine + // the boundary before conv's: the conversation as the client has it, + // when this turn was a side question of it, whose exchange the + // client's own next turn leaves out + if n := len(keys); n > 1 { + r.convPrev = keys[n-2] + } r.bridge.mu.Unlock() if why, over := r.oversized(2); over { r.bridge.letGoOversized(r, key, conv, why) @@ -2147,11 +2168,21 @@ type savedSession struct { owner string conv string // its conversation whatever the model (convKey) key string // where it is on the shelf (turnKey) - loose *looseTurn - tools map[string]bool // the tools its agent was told of - at time.Time // since when it waits - timer *time.Timer - since []Message // the turn's messages, once unshelved + // behind is where the conversation's next turn comes by when the run + // went on with a side question after its last turn (behindKey): the + // session carries that exchange, and the next turn leaves it out, so + // the turn that takes it resumes a copy cut before it (unshelve): + // before the side question's own message, behindTurn, that a -p run + // was told under magpie's uuid, or, for the interactive mode, which + // names its messages itself, after behindLine, the reply before it + behind string + behindTurn string + behindLine string + loose *looseTurn + tools map[string]bool // the tools its agent was told of + at time.Time // since when it waits + timer *time.Timer + since []Message // the turn's messages, once unshelved // cut, for a branch of a live run's session (branch), is the line it // is cut after: the run that takes it resumes a copy of the session // as far as that line (cutSession), and the file stays the run's @@ -2171,13 +2202,18 @@ type savedSession struct { // go, leaves it. func (b *subscriptionBridge) shelve(run *subscriptionRun) { run.mu.Lock() - sid, sessions, rewound, tools := run.sessionID, run.sessions, run.rewound, maps.Clone(run.tools) + sid, sessions, rewound, replied, tools := run.sessionID, run.sessions, run.rewound, run.replied, maps.Clone(run.tools) run.mu.Unlock() - if sid == "" || len(sessions) == 0 || rewound || run.idleKey == "" || run.appended { + if sid == "" || len(sessions) == 0 || rewound && !replied || run.idleKey == "" || run.appended { return } key := run.idleKey - s := &savedSession{session: sid, sessions: sessions, owner: run.owned(), conv: run.convKey, key: key, loose: run.loose, tools: tools, at: run.idleAt, cachePrefix: run.cachePrefix} + s := &savedSession{session: sid, sessions: sessions, owner: run.owned(), conv: run.convKey, key: key, behind: run.behindKey, loose: run.loose, tools: tools, at: run.idleAt, cachePrefix: run.cachePrefix} + if run.tui { + s.behindLine = run.behindLine + } else { + s.behindTurn = run.behindTurn + } if old := b.shelf[key]; old != nil { go old.discard() } @@ -2215,7 +2251,7 @@ func (b *subscriptionBridge) unshelve(req *Request, owner string) *savedSession return nil } b.mu.Lock() - s := b.shelf[key] + s, behind := b.shelf[key], false if s == nil { // rewritten (resume); its Claude Code is told the tools the // client offers now, and the session keeps the calls made of @@ -2225,6 +2261,21 @@ func (b *subscriptionBridge) unshelve(req *Request, owner string) *savedSession log.Printf("claude: a conversation whose last turn the client rewrote goes on from its saved session") } } + if s == nil { + // the run went on with a side question after the conversation's + // last turn — a prompt suggestion — and its session is shelved + // where that turn put it, not where the conversation's own next + // turn comes by (#4914) + for _, e := range b.shelf { + if e.behind == key && (e.behindTurn != "" || e.behindLine != "") && (s == nil || e.at.After(s.at)) { + s = e + } + } + if s != nil { + key, behind = s.key, true + log.Printf("claude: a conversation whose run answered a side question since goes on from its saved session, cut before the side question") + } + } delete(b.shelf, key) b.mu.Unlock() if s == nil { @@ -2235,10 +2286,38 @@ func (b *subscriptionBridge) unshelve(req *Request, owner string) *savedSession s.discard() return nil } + if behind && !s.cutBehind() { + s.discard() + return nil + } s.since = since return s } +// cutBehind makes the shelved session s a copy of itself cut before the +// side question its run answered after the conversation's last turn +// (behind), and removes the file it was: resumed whole, Claude Code would +// go on with the side question's exchange as the conversation's, which +// the client never had (#4914). false when the session can't be cut. +func (s *savedSession) cutBehind() bool { + var cut string + var err error + if s.behindLine != "" { + cut, err = cutSession(s.sessions, s.session, s.behindLine) + } else { + cut, err = cutSessionBefore(s.sessions, s.session, s.behindTurn) + } + if err != nil { + log.Printf("claude: a saved session couldn't be cut before the side question its run answered (%v); a new Claude Code is told the whole conversation", err) + return false + } + removeSession(s.sessions, s.session) + s.mu.Lock() + s.session = cut + s.mu.Unlock() + return true +} + // noteLines keeps where in the run's session a line calling the client's // tools ids was written: uuid, as Claude Code's output tells it. Claude // Code's own lines () are no reply's. @@ -2383,6 +2462,16 @@ var cutWait = 2 * time.Second // as written, its sessionId the copy's. An error when the line isn't // there within cutWait. func cutSession(dirs []string, sid, uuid string) (string, error) { + return cutSessionAt(dirs, sid, uuid, cutAfter) +} + +// cutSessionBefore is cutSession as far as the line before uuid's: the +// session as it stood before that message was told. +func cutSessionBefore(dirs []string, sid, uuid string) (string, error) { + return cutSessionAt(dirs, sid, uuid, cutBefore) +} + +func cutSessionAt(dirs []string, sid, uuid string, at func(b, mark []byte, uuid string) int) (string, error) { if sid == "" || uuid == "" || strings.ContainsAny(sid, `/\`) || strings.Contains(sid, "..") { return "", errors.New("no session to cut") } @@ -2393,8 +2482,8 @@ func cutSession(dirs []string, sid, uuid string) (string, error) { if err != nil { continue } - end := cutAfter(b, mark, uuid) - if end < 0 { + end := at(b, mark, uuid) + if end <= 0 { continue } id := newUUID() @@ -2437,6 +2526,16 @@ func cutAfter(b, mark []byte, uuid string) int { return -1 } +// cutBefore is where in a session's lines b the whole line whose uuid is +// uuid begins, -1 when none is there yet. +func cutBefore(b, mark []byte, uuid string) int { + end := cutAfter(b, mark, uuid) + if end < 0 { + return -1 + } + return bytes.LastIndexByte(b[:end-1], '\n') + 1 +} + // saved says its session's file is still there. func (s *savedSession) saved() bool { for _, dir := range s.sessions { @@ -2617,7 +2716,7 @@ func (r *subscriptionRun) letGo() { if can { r.rewinding = done r.turnUUID, r.backKey = "", "" // once a turn - r.rewound = true + r.rewound, r.replied = true, false } r.mu.Unlock() if !can { @@ -5485,6 +5584,9 @@ func (b *subscriptionBridge) removeRun(run *subscriptionRun) { if run.convKey != "" { keepLost(b.lost, run.convKey, lost, lostMost) } + if run.convPrev != "" && run.convPrev != run.convKey { + keepLost(b.lost, run.convPrev, lost, lostMost) + } for _, id := range results { keepLost(b.lostCalls, id, lost, 4*lostMost) }