-
Notifications
You must be signed in to change notification settings - Fork 14
perf(app): stabilize long session timelines #402
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad543cb
fix(app): bound long session rendered turns
Astro-Han a9e571c
perf(app): coalesce streaming deltas safely
Astro-Han 27f5981
fix(app): restore bottom history mode after manual return
Astro-Han 0a9ceff
perf(app): index queued delta pruning
Astro-Han 3989a65
fix(app): keep hash history mode sticky
Astro-Han 0159ca6
fix(app): gate latest window restore on bottom
Astro-Han ba6d688
fix(app): avoid queued event key collisions
Astro-Han f983ca3
fix(app): restore latest window after clearing hash
Astro-Han 89b2755
fix(app): preserve reading mode after clearing hash
Astro-Han 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
122 changes: 122 additions & 0 deletions
122
packages/app/src/context/global-sdk-event-queue.test.ts
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,122 @@ | ||
| import type { Event, SessionStatus } from "@opencode-ai/sdk/v2/client" | ||
| import { describe, expect, test } from "bun:test" | ||
| import { coalesceQueuedEvents, type QueuedGlobalEvent } from "./global-sdk-event-queue" | ||
|
|
||
| const directory = "/repo" | ||
|
|
||
| const delta = (partID: string, value: string, messageID = "msg_1", field = "text"): Event => ({ | ||
| type: "message.part.delta", | ||
| properties: { sessionID: "ses_1", messageID, partID, field, delta: value }, | ||
| }) | ||
|
|
||
| const updated = (partID: string, messageID = "msg_1"): Event => | ||
| ({ | ||
| type: "message.part.updated", | ||
| properties: { | ||
| sessionID: "ses_1", | ||
| time: 1, | ||
| part: { id: partID, sessionID: "ses_1", messageID, type: "text", text: "full" }, | ||
| }, | ||
| }) as Event | ||
|
|
||
| const status = (sessionID = "ses_1", type: "busy" | "idle" | "retry" = "busy"): Event => ({ | ||
| type: "session.status", | ||
| properties: { | ||
| sessionID, | ||
| status: type === "retry" ? ({ type, attempt: 1, message: "retry", next: 1 } satisfies SessionStatus) : { type }, | ||
| }, | ||
| }) | ||
|
|
||
| const queued = (...events: Event[]): QueuedGlobalEvent[] => events.map((payload) => ({ directory, payload })) | ||
| const queuedIn = (directory: string, ...events: Event[]): QueuedGlobalEvent[] => | ||
| events.map((payload) => ({ directory, payload })) | ||
| const eventTypes = (events: QueuedGlobalEvent[]) => events.map((event) => event.payload.type) | ||
| const deltas = (events: QueuedGlobalEvent[]) => | ||
| events | ||
| .filter((event) => event.payload.type === "message.part.delta") | ||
| .map((event) => (event.payload as Extract<Event, { type: "message.part.delta" }>).properties.delta) | ||
|
|
||
| describe("global SDK event queue coalescing", () => { | ||
| test("combines only contiguous deltas for the same part", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "a"), delta("prt_1", "b"))) | ||
|
|
||
| expect(events).toHaveLength(1) | ||
| expect(deltas(events)).toEqual(["ab"]) | ||
| }) | ||
|
|
||
| test("does not merge same-part deltas across another part delta", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "a"), delta("prt_2", "x"), delta("prt_1", "b"))) | ||
|
|
||
| expect(deltas(events)).toEqual(["a", "x", "b"]) | ||
| }) | ||
|
|
||
| test("does not merge deltas across non-delta barriers", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "a"), status(), delta("prt_1", "b"))) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["message.part.delta", "session.status", "message.part.delta"]) | ||
| expect(deltas(events)).toEqual(["a", "b"]) | ||
| }) | ||
|
|
||
| test("does not merge same-part deltas for different fields", () => { | ||
| const events = coalesceQueuedEvents( | ||
| queued(delta("prt_1", "a", "msg_1", "text"), delta("prt_1", "b", "msg_1", "metadata")), | ||
| ) | ||
|
|
||
| expect(deltas(events)).toEqual(["a", "b"]) | ||
| }) | ||
|
|
||
| test("drops stale deltas before a full part update but keeps later deltas", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "stale"), updated("prt_1"), delta("prt_1", "fresh"))) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["message.part.updated", "message.part.delta"]) | ||
| expect(deltas(events)).toEqual(["fresh"]) | ||
| }) | ||
|
|
||
| test("keeps only the full update and later delta for delta-update-delta ordering", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "before"), updated("prt_1"), delta("prt_1", "after"))) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["message.part.updated", "message.part.delta"]) | ||
| expect(deltas(events)).toEqual(["after"]) | ||
| }) | ||
|
|
||
| test("handles multiple full updates for the same part without resurrecting stale deltas", () => { | ||
| const events = coalesceQueuedEvents(queued(delta("prt_1", "stale"), updated("prt_1"), updated("prt_1"))) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["message.part.updated", "message.part.updated"]) | ||
| expect(deltas(events)).toEqual([]) | ||
| }) | ||
|
|
||
| test("keeps replaceable event indexes correct after stale delta removal", () => { | ||
| const events = coalesceQueuedEvents( | ||
| queued(delta("prt_1", "stale"), status("ses_1", "busy"), updated("prt_1"), status("ses_1", "idle")), | ||
| ) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["session.status", "message.part.updated"]) | ||
| expect(events[0].payload).toEqual(status("ses_1", "idle")) | ||
| }) | ||
|
|
||
| test("keeps delta merging and stale pruning isolated by directory", () => { | ||
| const events = coalesceQueuedEvents([ | ||
| ...queuedIn("/repo-a", delta("prt_1", "a")), | ||
| ...queuedIn("/repo-b", delta("prt_1", "b")), | ||
| ...queuedIn("/repo-a", delta("prt_1", "c")), | ||
| ...queuedIn("/repo-a", updated("prt_1")), | ||
| ...queuedIn("/repo-b", delta("prt_1", "d")), | ||
| ...queuedIn("/repo-b", updated("prt_1")), | ||
| ]) | ||
|
|
||
| expect(eventTypes(events)).toEqual(["message.part.updated", "message.part.updated"]) | ||
| expect(deltas(events)).toEqual([]) | ||
| expect(events.map((event) => event.directory)).toEqual(["/repo-a", "/repo-b"]) | ||
| }) | ||
|
|
||
| test("does not collide composite keys when directories contain separators", () => { | ||
| const events = coalesceQueuedEvents([ | ||
| ...queuedIn("/repo:msg_1", delta("prt_1", "a", "prt_2")), | ||
| ...queuedIn("/repo", delta("msg_1:prt_1", "b", "prt_2")), | ||
| ]) | ||
|
|
||
| expect(deltas(events)).toEqual(["a", "b"]) | ||
| expect(events.map((event) => event.directory)).toEqual(["/repo:msg_1", "/repo"]) | ||
| }) | ||
| }) |
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,96 @@ | ||
| import type { Event } from "@opencode-ai/sdk/v2/client" | ||
|
|
||
| export type QueuedGlobalEvent = { directory: string; payload: Event } | ||
|
|
||
| const deltaKey = (event: QueuedGlobalEvent) => { | ||
| if (event.payload.type !== "message.part.delta") return | ||
| const props = event.payload.properties | ||
| return JSON.stringify([event.directory, props.messageID, props.partID, props.field]) | ||
| } | ||
|
|
||
| const partKey = (event: QueuedGlobalEvent) => { | ||
| if (event.payload.type === "message.part.delta") { | ||
| const props = event.payload.properties | ||
| return JSON.stringify([event.directory, props.messageID, props.partID]) | ||
| } | ||
| if (event.payload.type === "message.part.updated") { | ||
| const part = event.payload.properties.part | ||
| return JSON.stringify([event.directory, part.messageID, part.id]) | ||
| } | ||
| } | ||
|
|
||
| const replaceableKey = (event: QueuedGlobalEvent) => { | ||
| if (event.payload.type === "session.status") | ||
| return JSON.stringify(["session.status", event.directory, event.payload.properties.sessionID]) | ||
| if (event.payload.type === "lsp.updated") return JSON.stringify(["lsp.updated", event.directory]) | ||
| } | ||
|
|
||
| const appendDelta = (event: QueuedGlobalEvent, delta: string): QueuedGlobalEvent => { | ||
| if (event.payload.type !== "message.part.delta") return event | ||
| return { | ||
| ...event, | ||
| payload: { | ||
| ...event.payload, | ||
| properties: { | ||
| ...event.payload.properties, | ||
| delta: event.payload.properties.delta + delta, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export function coalesceQueuedEvents(events: QueuedGlobalEvent[]): QueuedGlobalEvent[] { | ||
| const result: (QueuedGlobalEvent | undefined)[] = [] | ||
| const replaceable = new Map<string, number>() | ||
| const deltaIndexesByPart = new Map<string, Set<number>>() | ||
| const pushEvent = (event: QueuedGlobalEvent) => { | ||
| const index = result.length | ||
| result.push(event) | ||
|
|
||
| const key = partKey(event) | ||
| if (event.payload.type === "message.part.delta" && key) { | ||
| const indexes = deltaIndexesByPart.get(key) ?? new Set<number>() | ||
| indexes.add(index) | ||
| deltaIndexesByPart.set(key, indexes) | ||
| } | ||
|
|
||
| return index | ||
| } | ||
|
|
||
| for (const event of events) { | ||
| const replaceKey = replaceableKey(event) | ||
| if (replaceKey) { | ||
| const index = replaceable.get(replaceKey) | ||
| if (index !== undefined) { | ||
| result[index] = event | ||
| continue | ||
| } | ||
| replaceable.set(replaceKey, pushEvent(event)) | ||
| continue | ||
| } | ||
|
|
||
| if (event.payload.type === "message.part.updated") { | ||
| const updatedKey = partKey(event) | ||
| if (updatedKey) { | ||
| for (const index of deltaIndexesByPart.get(updatedKey) ?? []) { | ||
| result[index] = undefined | ||
| } | ||
| deltaIndexesByPart.delete(updatedKey) | ||
| } | ||
| pushEvent(event) | ||
| continue | ||
|
Astro-Han marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (event.payload.type === "message.part.delta") { | ||
| const last = result[result.length - 1] | ||
| if (last?.payload.type === "message.part.delta" && deltaKey(last) === deltaKey(event)) { | ||
| result[result.length - 1] = appendDelta(last, event.payload.properties.delta) | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| pushEvent(event) | ||
| } | ||
|
|
||
| return result.filter((event): event is QueuedGlobalEvent => event !== undefined) | ||
| } | ||
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
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.