Repository navigation
AI-3198: copy a step's approval with its reply - #1975
Merged
Merged
Conversation
…wn approval DecideApproval answers a step's approval in place on the reply being written, while flush may be encoding the queued copy of that reply and the socket may be encoding a published update of it. Both copies shared the approval's pointer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 12, 2026
…-approval-race Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Loschcode
pushed a commit
that referenced
this pull request
Nov 13, 2026
…ai-3209-session-rewind-is-refused-for-every Brings in origin/accelerate through #1975. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # sdks/swift/core/generated.json
Loschcode
marked this pull request as ready for review
November 13, 2026 18:28
Author
|
/jeffery review |
|
Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it. How it works
Commands (each as a whole comment)
|
There was a problem hiding this comment.
Round 1 on 4a07fe39c63a · 🆕 1 new
Still open: 1 must-fix. Full list on the checks tab.
…vals The live tick copied the reply's parts with a shallow copy, so the update it encodes after unlocking shared each step's approval with the live reply: answering the approval meanwhile changed it mid-encode, the race this PR fixes for queued writes and published updates. The tick now sends liveCopy, whose parts are cloned like theirs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
/jeffery review |
Loschcode
added a commit
that referenced
this pull request
Nov 13, 2026
Ticket: [AI-3198](https://linear.app/stream/issue/AI-3198/regenerate-and-edit-in-place-with-branches-12-in-kept-conversations). Stacked on #1975 (approval race fix). PR 3/3 (JS SDK) and GetStream/stream-chat-react#3434 (UI) build on this. ## Merge order This PR is one of a stack. Merge in this order, each after the one before: 1. #1975, base `accelerate` 2. #1983 (this PR), base #1975's branch 3. #1984, base #1983's branch 4. GetStream/stream-chat-react#3434, base `master` 5. #2012, base #1983's branch. Deploy GetStream/chat#20865 before the router that carries it. ## Why In a conversation kept in Stream Chat, which is every non-incognito session, `responses.rewind` is refused, so StreamGPT regenerates by forking. Each regenerate becomes a new chat in the history list, an edit isn't possible, and there's no ‹ 1/2 ›. This PR lets a person regenerate any reply or edit any question in place, and keeps the earlier versions as branches they can switch between. ## How it works The router keeps every version as a tree of responses. The Chat channel only ever shows the branch the person is on: the others are hidden with Chat's agent-channel rewind (`POST /channels/{type}/{id}/rewind`), and the router keeps a copy of each hidden message. ``` regenerate reply k / edit question k ├─ refuse: shared, thread, dispatched, live call, speech-to-speech, │ not kept in Chat, a turn not asked through createResponse │ (such as one spoken on a call: "unmarked") ├─ pause: interrupt the reply, flush the Chat write queue ├─ copy every visible message from the point on -> agent_branch_messages ├─ journal the switch -> agent_branch_switches ├─ Chat rewind (expected_last_message_id = last id read; retry once on conflict) ├─ drop queued writes of hidden ids, remember them as hidden ├─ move the active path (rewound_at = off the path) across shards └─ ask again: new response, sibling of the replaced one browse ‹ i/n › -> GET .../siblings, GET .../branch (no Chat write) send on another branch -> POST .../activate: rewind at the fork point, re-send the branch's copies (<= 100), then send ``` ```mermaid sequenceDiagram participant Device participant Router participant Postgres participant Chat Device->>Router: createResponse replaces=r2 (regenerate) Router->>Chat: read messages from the question on Router->>Postgres: save copies, journal switch Router->>Chat: rewind to the question (exclusive) Router->>Postgres: move active path to r2's parent Router->>Chat: new reply, support_branch reply 2 of 2 Router-->>Device: AgentResponse branch, rewound_to Chat-->>Device: channel.rewound (other devices) ``` Each message carries `custom.support_branch = {response_id, parent_response_id, question_response_id, of, index, count}`, a new key rather than a field of `support_runtime`, whose strict decode would break the previous release on rollback. | Limit | What happens | Error | |---|---|---| | 100 hidden stretches per channel (Chat's cap): about 100 distinct regenerated or edited turns | the regenerate or edit is refused; asking and reading still work | 409 `branch_limit_reached` | | A branch brought back is over 100 messages | the activation is refused | 409 `branch_too_long` | | A branch's copies were deleted (memory truncate) | it can't be read or brought back | 410 `branch_unavailable` | | Merged hidden ranges per conversation | histogram `router_branch_merged_ranges`, warning log past 80 | none | ## Changes | Change | Where | Proved by | |---|---|---| | Migration `20261227100000_response_branches`: `parent_response_id` and `question_response_id`, copies table, switch journal | `migrations/`, `internal/store/branches.go` | `StoreSuite`, `ShardedStoreSuite` (path move across 2 shards, cascade, cross-customer isolation) | | Chat rewind client and paged tail read, through getstream-go v5's `MakeRequest` | `internal/conversation/rewind.go` | `rewind_test.go`; the chattest fake models Chat's hidden ranges and errors | | `flush` holds a send mutex from reading the queue head to popping it | `internal/conversation/conversation.go` | `TestFlushSuite`; fails without the mutex | | Pause, copy, rewind, purge, re-send and resume of an unfinished switch on reopen | `internal/conversation/branches.go` | `TestBranchesSuite` (27 tests: streaming reply, unwritable queue, every journal state) | | Regenerate, edit and activate, plus leaf tracking, history and titler reset, and documents following the active path | `internal/session/branches.go` | session `BranchesSuite` (model history equals what Chat shows; marks; reopen) | | `replaces` on createResponse; siblings, branch and activate endpoints | `internal/api/branches.go`, `responses.go` | `TestResponseBranchesSuite` (posture, owner-only, paging, 410, cap) | | `forget-user` and memory truncate delete the copies | `cmd/router/usage.go`, `session/manager.go` | `TestForgetUserSuite`, session tests | | Go SDK: `Regenerate`, `Edit`, `Siblings`, `Branch`, `Activate`; every client regenerated | `sdks/` | each SDK's `--check` and unit tests | ## Notes for review - **`rewound_at` now means "not on the active path".** Sharding rules out one recursive query, so the existing filters (`store/sessions.go`) stay unchanged. The non-persistent rewind keeps working on the same column. - **Re-sent messages are new Chat messages:** new ids, `skip_push`, and `custom.resent_from = <original id>`. Customer webhooks see `message.new` again, which is documented. - **What isn't cleaned up:** - Memories from abandoned branches are kept (AI-3203). - A server-side delete of a hidden Chat message doesn't reach the router (AI-3226). - **Prerequisite:** on a chat release before GetStream/chat#20865, the Chat app needs the `chat.agent_rewind` grant, and without it the request gets `not_configured`. From #20865 on, every agent channel can be rewound, and only the `chat.agent_rewind.kill` switch stops it. - **A switch whose rewind answer was lost is kept, not forgotten.** A rewind that timed out can still be committed by Chat after the router reads the channel. While the channel's newest message is still the switch's newest copy (the `expected_last_message_id` it sent), the journal row and its copies are kept and nothing is counted as hidden. Once anything newer is shown, Chat would refuse that rewind, so the switch is forgotten as before. A kept switch never refuses the next turn. - **Kept conversations can now be rewound** by `rewindSession` too, in #2012 (AI-3209), stacked on this PR. ## Fixes from the pre-ship review Each has a test that fails without it. | Fix | Commit | Test | |---|---|---| | A branch brought back is sent in the tree's order, not by its copies' times. A question above it that an earlier activation re-sent sorted after the branch: `[Q2, A2, Q1, A1]` | 348a49f | `TestABranchBroughtBackUnderAnAncestorSentAgainKeepsTheTreesOrder` (Chat, model history, reopen) | | A switch whose rewind may still land is marked unsettled, so the next turn checks again | 1f88f61 | `TestARewindThatLandsLateIsFoundByTheNextQuestion` | | An edit's text and id are checked before Chat hides anything | b8a87c8 | `TestAnEditThatCannotBeAskedIsRefusedBeforeAnythingIsHidden` | | A hidden request retried after a router restart is a duplicate, with no orphan reply | 87adc70 | `TestARequestAnEditHidRetriedAfterARestartIsADuplicate` | | Regenerate and edit flush the session's records, and drain the conversation's queued Chat writes, before reading. A turn sent through `/respond` is marked; it only looked unmarked while still queued (06a761e) | 9d04436, a19feb2 | `TestARegenerateRightAfterATurnNotYetRecordedRegeneratesIt`, `TestARegenerateWaitsForTheTurnsMessagesToReachChat` | | The PHP client is regenerated for `parent_response_id` | dddc9f2 | `composer generate-check` | `origin/accelerate` is merged in through #1975. ## Validation - `go test -race` passes on `internal/{store,conversation,session,api}`. The integration suites for store, conversation, session and api pass with `-tags integration`. - `TestKnowledgeFilesSuite/TestAChartOnAPageIsSummarisedAfterThePagesText` also fails on untouched `accelerate` (same assertion, `knowledgefiles_test.go:246`). It's unrelated. - The mutation checks below each fail as expected: removing the send mutex, the hidden-id skip, or the document fixes. - Every generated client's `--check` passes, as do Rust fmt and clippy, PHPStan, and each SDK's unit tests. `uv run dev.py check` passes. - Unverified against real Chat: - the rewind integration test skips without credentials; - the 404 wording Chat gives when updating a rewound message is assumed in the fake. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Laurent Schaffner <laurentschaffner@Laurents-MacBook-Pro-Stream.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Loschcode
added a commit
that referenced
this pull request
Nov 13, 2026
Ticket: [AI-3198](https://linear.app/stream/issue/AI-3198/regenerate-and-edit-in-place-with-branches-12-in-kept-conversations). Stacked on #1983 (router), which is stacked on #1975. The UI side is GetStream/stream-chat-react#3434. ## Merge order This PR is one of a stack. Merge in this order, each after the one before: 1. #1975, base `accelerate` 2. #1983, base #1975's branch 3. #1984 (this PR), base #1983's branch 4. GetStream/stream-chat-react#3434, base `master` 5. #2012, base #1983's branch. Deploy GetStream/chat#20865 before the router that carries it. ## Why #1983 adds regenerate, edit and branches to the router. This brings them to `@stream-io/stream-agents`: methods for any app, and `agentChatTransport` support. With that, an `AIConversationController` gets ‹ i/n ›, regenerate on any reply and edit on any question without app code. It also keeps the device's view true when Chat hides messages: stream-chat neither handles `channel.rewound` nor drops messages on a re-query. ## How it works ``` agentChatTransport send(regenerate, message) -> responses.regenerate(response of message) send(edit, message, text) -> responses.edit(response of the shown reply, text) switchBranch(message, to) -> siblings(of) -> sibling at index +/- 1 ├─ the active one -> back to the channel's messages └─ another -> branch(sibling): channel up to the fork, then its copies (display only, nothing written to Chat) send(message) while another version shows -> activate(newest shown) -> send session channel rewound_to in an answer -> drop the hidden messages the device held, at once channel.rewound / notification.channel_rewound -> drop them, reload, drop again (Chat's reads can lag) ``` | Change | Result | Test | |---|---|---| | `responses.regenerate`, `edit`, `siblings`/`unwindSiblings`, `branch`/`unwindBranch`, `activate`; `branchErrorOf`, `supportBranchOf` | typed calls for the new endpoints and error codes | `tests/session.test.ts` "Regenerate, edit and branches" | | Hidden messages dropped from channel state (`src/branches.ts`) | the acting device never shows old and new answers together; other devices update without a reload | prune tests fail with removal disabled | | `agentChatTransport` branches, `switchBranch`, `AgentBranchError` | the controller path works end to end | `tests/agent-chat-transport.test.ts` | | stream-chat 10: nanosecond `created_at`, `reload()` not `query()`, flushing the 500 ms message-list publish | the prune works on 10 too | the suite on 10.0.2; each fix fails its test when reverted | | A missing `of`/`index`/`count` reads as 1 of 1 | first answers can be branched | `tests/stream.test.ts` | ## Notes for review - **stream-chat 9 merges a re-query into state rather than replacing it** (`channel.ts` `_initializeState`). That's why the prune removes messages explicitly instead of relying on a reload. - **A question keeps the mark of the response that first answered it**, which a regenerate hides. So an edit goes through the reply shown after the question. - **The test harness moved onto a shared real-client helper** (`tests/stream-chat.ts`). CI's `js-stream-chat-10` job no longer compiles against 9-only APIs (`baseURL`, `addMessageSorted`). ## Fixes from the pre-ship review - `agentChatTransport` no longer treats a `switch_unfinished` 409 as the remembered response being gone (76c1aca). Tested by "keeps the response remembered for a target while the router finishes an unsettled switch". - The fake router's test token is built at run time (eff2da4). ## Validation - `npm run types -- --check` is in step with the spec, and `tsc --noEmit` is clean. - `npm test` passes 613 of 613 on stream-chat 9.54.0 and 613 of 613 on 10.0.2. - GitGuardian flagged the unsigned test token in `tests/stream-chat.ts` (and in `tests/session.test.ts` before the helper moved). It is now built at run time (eff2da4). The incident stays on the earlier commits of this PR until it is resolved as a false positive in the GitGuardian dashboard. - Unverified: that real Chat sends nanosecond numbers on the wire with stream-chat 10. That's inferred from its types. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Laurent Schaffner <laurentschaffner@Laurents-MacBook-Pro-Stream.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Loschcode
added a commit
that referenced
this pull request
Nov 13, 2026
Ticket: [AI-3209](https://linear.app/stream/issue/AI-3209/session-rewind-is-refused-for-every-session-on-the-hosted-agents-api). Stacked on #1983 (AI-3198 branches router), which is stacked on #1975. Chat side: GetStream/chat#20865. ## Merge order This PR is one of a stack. Merge in this order, each after the one before: 1. #1975, base `accelerate` 2. #1983, base #1975's branch 3. #1984, base #1983's branch 4. GetStream/stream-chat-react#3434, base `master` 5. #2012 (this PR), base #1983's branch. Deploy GetStream/chat#20865 before the router that carries it. ## Deploy order 1. **Chat first:** GetStream/chat#20865 makes the `agent` channel type the gate for rewind, instead of the per-app `chat.agent_rewind` grant. Without it, an app without the grant gets 403 from Chat, and this answers it as `not_configured`. 2. **Then the router**, with #1975 and #1983 under it. ## Why `POST /v1/agents/sessions/{id}/rewind` (`responses.rewind`) answered 400 for every session a caller can open. The sessions API keeps every non-incognito conversation in Stream Chat, and `Session.Rewind` refused any such session, because reopening it would read the rewound turns back from the channel. ## How it works A rewind to response R in a kept conversation is #1983's edit of the turn after R, without the new question: ``` rewind to R ├─ refuse as a branch switch is: thread, dispatched, shared, call, speech-to-speech, │ not kept, a turn with no branch mark such as a spoken one ("unmarked"), R off the conversation (409), │ an earlier switch unfinished (409 switch_unfinished) ├─ C = R's child on the active path (store.ActiveChild); none -> 204, nothing hidden ├─ find C's question in Chat by its support_branch mark ├─ #1983's switch, journaled as an edit of C: pause, copy C.. to agent_branch_messages, │ Chat rewind inclusive to C's question, purge queued writes of hidden ids ├─ leaf = R; model history and documents rebuilt from what Chat now shows └─ move the active path to R (rewound_at on C and after) ``` So the rewound turns are a branch like any other. The next question is an alternative to C (‹ 1/2 › on the question), and `getResponseBranch` and `activateResponse` read and bring C back rather than answering 410. A router that stops partway is finished by the next open, exactly as an edit is. It counts toward the 100 edit and regenerate points (`branch_limit_reached`), and is labelled `rewind` on `router_branch_merged_ranges`. ## Changes | Change | Where | Proved by | |---|---|---| | `Session.Rewind` rewinds a kept conversation instead of refusing it | `internal/session/session.go`, `rewindKept` in `internal/session/branches.go` | session `BranchesSuite`: `TestRewindingAConversationKeptInChatHidesTheLaterTurnsAndCarriesOnFromTheKeptOne` (Chat, model history and reopen agree), `TestTheTurnsARewindHidAreABranchThatCanBeBroughtBack`, `TestRewindingAKeptConversationToItsNewestResponseHidesNothing`, `TestARewindToAResponseOffTheConversationIsRefused`, `TestARewindToAResponseTheSessionDoesNotHaveIsNotFound`, `TestARewindChatHasTurnedOffIsRefusedAndTheConversationIsLeftAsItWas` | | `store.ActiveChild`: the response after another on the active path | `internal/store/branches.go` | the session tests above | | `rewindSession` answers branch refusals as the branch endpoints do (404 unknown response, 409, `not_configured`) | `internal/api/sessions.go` | `TestResponseBranchesSuite/TestADeviceRewindsAConversationKeptInChat`, `TestAServerRewindsAConversationKeptInChat`; `TestSessionVerbsSuite/TestRewindingToATurnNobodyTookIsRefused` now 404 | | Spec and clients regenerated; SDK doc comments and READMEs no longer say to fork instead | `api/openapi.yaml`, `sdks/*`, `plugins/stream` | each generator's `--check` | | CHANGELOG | `CHANGELOG.md` | | ## Notes for review - **Reconciled with #1983.** This branch first added its own `conversation/rewind.go`, chattest rewind handler and an `agent_responses.chat_message_id` column with migration `20261228100000_response_chat_message.sql`. #1983 already has a Chat rewind client, a fake that models Chat's hidden ranges, and `support_branch` marks that find a response's messages. The merge resolves every file this branch touched to #1983's version. **The migration is dropped**, so this PR adds no migration of its own. - **AI-3198's spec** now says plain `responses.rewind` on a kept session rewinds it, as this PR does. - **Only marked turns can be hidden by a rewind.** Turns asked through `createResponse` or `POST /respond` carry the marks. A turn spoken on a call doesn't, and a rewind that would hide it is refused as unmarked. - **A request retried after a rewind hid its reply** is answered as a duplicate of the hidden turn, the same as after #1983's edit. The first version of this branch forgot those commands instead. - `responses.rewind` on a kept session does not return `rewound_to`, so the acting device relies on `channel.rewound` (#1984's prune) to drop the hidden messages. ## Fixes from the pre-ship review Each has a test that fails without it. | Fix | Commit | Test | |---|---|---| | Records are flushed before the tree is read | 2f591e5 | `TestARewindRightAfterATurnNotYetRecordedHidesIt` | | The question asked after a rewind is marked 2 of 2, from the tree, so it survives a reopen | 2f591e5, eb78f8c | `TestTheTurnsARewindHidAreABranchThatCanBeBroughtBack`, `TestTheQuestionAskedAfterARewindAndAReopenIsShownBesideTheRewoundOne` | | A rewind asked again finishes its unfinished switch; any other rewind gets a fresh id, so repeating a rewind after bringing the turn back can't reuse a finished id | 2f591e5, 712e35b | `TestARewindAskedAgainAfterItsAnswerWasLostFinishesIt`, `TestARewindMadeAgainAfterItsTurnWasBroughtBackIsSettledWhenItsAnswerIsLost` | | Queued Chat writes are drained before the next turn's messages are read | 712e35b | `TestARewindWaitsForTheNextTurnsMessagesToReachChat` | | `rewindSession` carries on an ended kept conversation | 8a57e80 | `TestAnEndedConversationKeptInChatIsRewound` | | A `/respond` turn can be rewound to; the unmarked refusal is pinned on a turn with no marks | 8197579 | `TestATurnSentThroughRespondCanBeRewoundTo`, `TestARewindThatWouldHideAnUnmarkedResponseIsRefused` | | Rewinds and activations around a regenerate keep the tree's order | 2f591e5 | `TestRewindsAndActivationsAroundARegenerateKeepTheTreesOrder` | ## Validation With `-race -tags integration` against local Postgres and Redis: - `./internal/conversation/`, `./internal/store/`: pass. `./internal/session/`: pass, except six timing tests (voice notes, a retiring conversation, a spoken tool call) that failed under CPU contention and pass when run alone. - `./internal/api/`: pass, except `TestChatEpisodesSuite/TestAnIdleChatEpisodeIsSummarizedFromTheMessageThatOpenedIt`, which also fails locally on #1975 (`accelerate` plus one commit). Five more (StreamApps, Telnyx and the two chart tests) failed under load and pass when run alone. - Clients: `npm run types -- --check` (js), `uv run sdks/swift/generate.py --check`; Go, Python and Kotlin regenerated with their generators. .NET, Rust, PHP, Ruby and Dart carry no rewind description; their hand-written doc comments were edited. - Unverified against real Chat: the router's rewind is exercised only against the chattest fake. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Laurent Schaffner <laurentschaffner@Laurents-MacBook-Pro-Stream.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merge order
This PR is one of a stack. Merge in this order, each after the one before:
acceleratemasterWhy
Answering a held tool call's approval (
DecideApproval) changes the approval in place on the reply being written. At the same moment the router can be encoding a copy of that reply in two other goroutines: the queued Chat write thatflushsends, and theUpdatedit published to the conversation's socket. Both copies share the step's*Approvalpointer, becauseenqueueandpublishcopy the parts slice but not the pointers inside it. That's a data race, and a write can carry a half-changed approval.go test -race -tags integration ./internal/api/caught it while building AI-3198, whose flush changes made the timing likely. The bug itself is onacceleratetoday.How it works
clonedParts(internal/conversation/parts.go) copies each part's approvalTestDisplaySuite/TestQueuedAndPublishedCopiesKeepTheirOwnApprovalenqueueandpublishuse it (internal/conversation/conversation.go)DecideApprovalchanges only the live replyShould be empty, but was allowedfor both copiesAuthorization,StartedAtandFinishedAtare always replaced with a new pointer rather than changed in place, so they need no copy.Validation
go test -race ./internal/conversation/passes.clonedPartsreduced to a shallow copy, the new test fails on both the queued write and the published update.Part of AI-3198 (regenerate and edit with branches), split out because it stands alone. The branches PR stacks on this one.
🤖 Generated with Claude Code