Repository navigation
fix(sessions): share the change-feed mirror between hosts (#2205) #2404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
threepointone
merged 1 commit into
fix/2217-tool-result-cleanup
from
fix/2205-session-mirror
Sep 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "agents": minor | ||
| "@cloudflare/ai-chat": patch | ||
| "@cloudflare/think": patch | ||
| --- | ||
|
|
||
| Add `session.mirror()`, which keeps an in-memory transcript in step with one session's change feed. `AIChatAgent` and `Think` now share it instead of each carrying its own copy of the reduction; `Think` keeps its branch, compaction, and prompt-refresh handling through the `intercept` and `onApplied` hooks. |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { SessionChangeEvent, SessionMessage } from "./types"; | ||
|
|
||
| /** | ||
| * How a host keeps an in-memory transcript in step with one session's change | ||
| * feed. The mirror applies the reduction every such cache needs; the hooks | ||
| * carry the decisions only the host can make. | ||
| */ | ||
| export interface SessionMirrorOptions<M extends { id: string }> { | ||
| /** | ||
| * The cached transcript. Read on every event, so a host that reassigns its | ||
| * array (a public `messages` field, say) is followed rather than | ||
| * shadowed. | ||
| */ | ||
| get(): M[]; | ||
| /** Install a new array: after a delete, or emptied by a clear. */ | ||
| set(messages: M[]): void; | ||
| /** The cached form of a stored message. Defaults to the stored message. */ | ||
| transform?(message: SessionMessage): M; | ||
| /** | ||
| * Handle an event instead of the default reduction, returning `true` when | ||
| * it did. A host whose cache cannot express a change in place — a branch | ||
| * append, a compaction — re-reads storage here. | ||
| */ | ||
| intercept?(event: SessionChangeEvent): boolean | Promise<boolean>; | ||
| /** | ||
| * After the default reduction wrote `message` for an append or update. | ||
| * `previous` is the cached entry it replaced, if there was one. | ||
| */ | ||
| onApplied?( | ||
| event: Extract<SessionChangeEvent, { type: "append" | "update" }>, | ||
| message: M, | ||
| previous: M | undefined | ||
| ): void; | ||
| } | ||
|
|
||
| /** | ||
| * The default reduction, per event: | ||
| * | ||
| * - `append` of a new row: replace the cached entry with its id, or push it. | ||
| * A duplicate append (`inserted: false`) changed nothing. | ||
| * - `update`: replace the cached entry with its id; a row the cache does not | ||
| * hold stays out. | ||
| * - `delete`: drop the removed ids. `clear`: empty the cache. | ||
| * - `import`, `compact`, `compaction`: nothing, since no in-place patch | ||
| * expresses them. A host that must react intercepts them. | ||
| */ | ||
| export function mirrorSessionChanges<M extends { id: string }>( | ||
| subscribe: ( | ||
| listener: (event: SessionChangeEvent) => Promise<void> | ||
| ) => () => void, | ||
| sessionId: string, | ||
| options: SessionMirrorOptions<M> | ||
| ): () => void { | ||
| const transform = | ||
| options.transform ?? ((message: SessionMessage) => message as unknown as M); | ||
| return subscribe(async (event) => { | ||
| if (event.sessionId !== sessionId) return; | ||
| if (await options.intercept?.(event)) return; | ||
| switch (event.type) { | ||
| case "append": | ||
| case "update": { | ||
| if (event.type === "append" && !event.inserted) return; | ||
| const cache = options.get(); | ||
| const index = cache.findIndex((m) => m.id === event.message.id); | ||
| if (index === -1 && event.type === "update") return; | ||
| const message = transform(event.message); | ||
| const previous = index === -1 ? undefined : cache[index]; | ||
| if (index === -1) cache.push(message); | ||
| else cache[index] = message; | ||
| options.onApplied?.(event, message, previous); | ||
| return; | ||
| } | ||
| case "delete": { | ||
| const removed = new Set(event.messageIds); | ||
| options.set(options.get().filter((m) => !removed.has(m.id))); | ||
| return; | ||
| } | ||
| case "clear": | ||
| options.set([]); | ||
| return; | ||
| default: | ||
| return; | ||
| } | ||
| }); | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.