Repository navigation
fix(desktop): stop every chat row re-rendering on each streamed token #9495
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
Open
harshithmullapudi
wants to merge
3
commits into
main
Choose a base branch
from
superset/super-2637-chat-lag-investigation-0cc85269
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−20
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5798c3b
fix(desktop): stop every chat row re-rendering on each streamed token
harshithmullapudi db34379
test(desktop): cover tool-run, harness and approval-target cases in s…
harshithmullapudi 42375fa
refactor(desktop): read the snapshot for a chat row through one view
harshithmullapudi 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
1 change: 1 addition & 0 deletions
1
...Session/components/Transcript/components/TranscriptRowItem/utils/sameRowSnapshot/index.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 @@ | ||
| export { sameRowSnapshot } from "./sameRowSnapshot"; |
115 changes: 115 additions & 0 deletions
115
...nts/Transcript/components/TranscriptRowItem/utils/sameRowSnapshot/sameRowSnapshot.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,115 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { reduceMany, type SessionSnapshot } from "@superset/chat/core"; | ||
| import type { Envelope, Item, ToolCall } from "@superset/chat/protocol"; | ||
| import type { TranscriptRow } from "../../../../utils/transcriptRows"; | ||
| import { sameRowSnapshot } from "./sameRowSnapshot"; | ||
|
|
||
| const reply: Item = { | ||
| id: "a-1", | ||
| kind: "agent_message", | ||
| startedAtMs: 1, | ||
| completedAtMs: 2, | ||
| text: "Done.", | ||
| }; | ||
| const streaming: Item = { | ||
| id: "a-2", | ||
| kind: "agent_message", | ||
| startedAtMs: 3, | ||
| text: "", | ||
| }; | ||
| const command: ToolCall = { | ||
| id: "tool-1", | ||
| kind: "tool_call", | ||
| startedAtMs: 4, | ||
| title: "bun test", | ||
| toolKind: "execute", | ||
| toolName: "bash", | ||
| status: "running", | ||
| content: [], | ||
| }; | ||
| const approval = { | ||
| id: "approval-1", | ||
| kind: "approval_request", | ||
| startedAtMs: 5, | ||
| title: "bun test", | ||
| targetItemId: command.id, | ||
| status: "pending", | ||
| options: [], | ||
| } as unknown as Item; | ||
| const thinking: Item = { | ||
| id: "r-1", | ||
| kind: "reasoning", | ||
| startedAtMs: 6, | ||
| text: "", | ||
| }; | ||
|
|
||
| function snapshotOf(items: readonly Item[], harness = "acp"): SessionSnapshot { | ||
| return { | ||
| session: { status: "running", harness, backgroundTasks: [] }, | ||
| turns: new Map(), | ||
| items: new Map(items.map((item) => [item.id, { item, turnId: "t1" }])), | ||
| liveStreams: new Map(), | ||
| cursor: { epoch: "e", seq: 1 }, | ||
| pendingReset: null, | ||
| } as unknown as SessionSnapshot; | ||
| } | ||
|
|
||
| function streamInto(snapshot: SessionSnapshot, itemId: string, append: string) { | ||
| return reduceMany(snapshot, [ | ||
| { | ||
| cursor: { epoch: "e", seq: 2 }, | ||
| delta: { type: "text", itemId, append }, | ||
| } as unknown as Envelope, | ||
| ]); | ||
| } | ||
|
|
||
| function itemRow(item: Item): TranscriptRow { | ||
| return { kind: "item", key: item.id, groupStart: false, item }; | ||
| } | ||
|
|
||
| const toolRun: TranscriptRow = { | ||
| kind: "tool_run", | ||
| key: "run-1", | ||
| groupStart: false, | ||
| items: [command], | ||
| steps: [command, thinking], | ||
| defaultCollapsed: false, | ||
| }; | ||
|
|
||
| describe("sameRowSnapshot", () => { | ||
| const items = [reply, streaming, command, approval, thinking]; | ||
| const base = snapshotOf(items); | ||
|
|
||
| test("another message streaming leaves a settled row alone", () => { | ||
| expect( | ||
| sameRowSnapshot(itemRow(reply), base, streamInto(base, "a-2", "Hi")), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test("the row's own text streaming changes it", () => { | ||
| expect( | ||
| sameRowSnapshot(itemRow(streaming), base, streamInto(base, "a-2", "Hi")), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("a tool run changes when its reasoning step streams", () => { | ||
| const next = streamInto(base, thinking.id, "Checking"); | ||
| expect(sameRowSnapshot(toolRun, base, next)).toBe(false); | ||
| expect(sameRowSnapshot(toolRun, base, streamInto(base, "a-2", "Hi"))).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test("an approval row follows changes to its target", () => { | ||
| const finished: ToolCall = { ...command, status: "completed" }; | ||
| const next = snapshotOf([reply, streaming, finished, approval, thinking]); | ||
| expect(sameRowSnapshot(itemRow(approval), base, next)).toBe(false); | ||
| expect(sameRowSnapshot(itemRow(reply), base, next)).toBe(true); | ||
| }); | ||
|
|
||
| test("every row changes when the harness does", () => { | ||
| expect( | ||
| sameRowSnapshot(itemRow(reply), base, snapshotOf(items, "codex")), | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
20 changes: 20 additions & 0 deletions
20
...mponents/Transcript/components/TranscriptRowItem/utils/sameRowSnapshot/sameRowSnapshot.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,20 @@ | ||
| import type { SessionSnapshot } from "@superset/chat/core"; | ||
| import { rowSnapshotView } from "../../../../utils/rowSnapshotView"; | ||
| import type { TranscriptRow } from "../../../../utils/transcriptRows"; | ||
|
|
||
| export function sameRowSnapshot( | ||
| row: TranscriptRow, | ||
| previous: SessionSnapshot, | ||
| next: SessionSnapshot, | ||
| ): boolean { | ||
| if (previous === next) return true; | ||
| const before = rowSnapshotView(row, previous); | ||
| const after = rowSnapshotView(row, next); | ||
| return ( | ||
| before.harness === after.harness && | ||
| before.asker === after.asker && | ||
| before.approvalTarget === after.approvalTarget && | ||
| before.texts.length === after.texts.length && | ||
| before.texts.every((text, index) => text === after.texts[index]) | ||
| ); | ||
| } |
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
1 change: 1 addition & 0 deletions
1
...ePaneRegistry/components/ChatSession/components/Transcript/utils/rowSnapshotView/index.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 @@ | ||
| export { type RowSnapshotView, rowSnapshotView } from "./rowSnapshotView"; |
63 changes: 63 additions & 0 deletions
63
...try/components/ChatSession/components/Transcript/utils/rowSnapshotView/rowSnapshotView.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,63 @@ | ||
| import { | ||
| approvalAsker, | ||
| displayText, | ||
| type SessionSnapshot, | ||
| } from "@superset/chat/core"; | ||
| import type { ApprovalRequest, Item, ToolCall } from "@superset/chat/protocol"; | ||
| import type { TranscriptRow } from "../transcriptRows"; | ||
|
|
||
| export type RowSnapshotView = { | ||
| harness: string | undefined; | ||
| /** Per item for item and footer rows, per step for tool runs. */ | ||
| texts: readonly string[]; | ||
| asker: ToolCall | undefined; | ||
| approvalTarget: Item | undefined; | ||
| }; | ||
|
|
||
| function approvalTarget( | ||
| snapshot: SessionSnapshot, | ||
| item: Item, | ||
| ): Item | undefined { | ||
| if (item.kind !== "approval_request") return undefined; | ||
| const targetId = (item as ApprovalRequest).targetItemId; | ||
| return targetId ? snapshot.items.get(targetId)?.item : undefined; | ||
| } | ||
|
|
||
| export function rowSnapshotView( | ||
| row: TranscriptRow, | ||
| snapshot: SessionSnapshot, | ||
| ): RowSnapshotView { | ||
| const harness = snapshot.session?.harness; | ||
| switch (row.kind) { | ||
| case "item": | ||
| return { | ||
| harness, | ||
| texts: [displayText(snapshot, row.item.id)], | ||
| asker: approvalAsker(snapshot.items, row.item), | ||
| approvalTarget: approvalTarget(snapshot, row.item), | ||
| }; | ||
| case "reply_footer": | ||
| return { | ||
| harness, | ||
| texts: [displayText(snapshot, row.item.id)], | ||
| asker: undefined, | ||
| approvalTarget: undefined, | ||
| }; | ||
| case "tool_run": | ||
| return { | ||
| harness, | ||
| texts: row.steps.map((step) => | ||
| step.kind === "reasoning" ? displayText(snapshot, step.id) : "", | ||
| ), | ||
| asker: undefined, | ||
| approvalTarget: undefined, | ||
| }; | ||
| default: | ||
| return { | ||
| harness, | ||
| texts: [], | ||
| asker: undefined, | ||
| approvalTarget: undefined, | ||
| }; | ||
| } | ||
| } |
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.