Skip to content

Commit 2d2ae7c

Browse files
fix(tui): avoid pruning on late finalization of evicted runs
Use untracked temporary state when finalize() handles a run id that is no longer tracked, so capacity pruning only happens during delta ingest. Co-authored-by: Cursor <[email protected]>
1 parent 7b7433f commit 2d2ae7c

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎src/tui/tui-stream-assembler.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ describe("TuiStreamAssembler", () => {
125125
expect(second).toBeNull();
126126
});
127127

128+
it("does not evict another run when finalizing an already-evicted run id", () => {
129+
const assembler = new TuiStreamAssembler();
130+
for (let index = 0; index < 200; index += 1) {
131+
assembler.ingestDelta(`run-${index}`, messageWithContent([text(`Draft ${index}`)]), false);
132+
}
133+
134+
assembler.ingestDelta("run-200", messageWithContent([text("Newest")]), false);
135+
136+
expect(assembler.finalize("run-0", messageWithContent([text("Late final")]), false)).toBe(
137+
"Late final",
138+
);
139+
expect(assembler.finalize("run-1", { role: "assistant", content: [] }, false)).toBe("Draft 1");
140+
});
141+
128142
it("bounds orphaned run state while retaining recently active runs", () => {
129143
const assembler = new TuiStreamAssembler();
130144
for (let index = 0; index < 200; index += 1) {

‎src/tui/tui-stream-assembler.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ function shouldPreserveBoundaryDroppedText(params: {
112112
export class TuiStreamAssembler {
113113
private runs = new Map<string, RunStreamState>();
114114

115-
private getOrCreateRun(runId: string): RunStreamState {
115+
private createEmptyRunState(): RunStreamState {
116+
return {
117+
thinkingText: "",
118+
contentText: "",
119+
contentBlocks: [],
120+
sawNonTextContentBlocks: false,
121+
displayText: "",
122+
};
123+
}
124+
125+
private getTrackedRun(runId: string): RunStreamState {
116126
let state = this.runs.get(runId);
117127
if (state) {
118128
// Refresh insertion order so an active older run survives eviction when newer runs arrive.
@@ -121,18 +131,16 @@ export class TuiStreamAssembler {
121131
return state;
122132
}
123133

124-
state = {
125-
thinkingText: "",
126-
contentText: "",
127-
contentBlocks: [],
128-
sawNonTextContentBlocks: false,
129-
displayText: "",
130-
};
134+
state = this.createEmptyRunState();
131135
this.runs.set(runId, state);
132136
pruneMapToMaxSize(this.runs, MAX_TRACKED_STREAM_RUNS);
133137
return state;
134138
}
135139

140+
private getRunForFinalize(runId: string): RunStreamState {
141+
return this.runs.get(runId) ?? this.createEmptyRunState();
142+
}
143+
136144
private updateRunState(
137145
state: RunStreamState,
138146
message: unknown,
@@ -177,7 +185,7 @@ export class TuiStreamAssembler {
177185

178186
/** Ingests a streaming delta and returns updated display text only when it changed. */
179187
ingestDelta(runId: string, message: unknown, showThinking: boolean): string | null {
180-
const state = this.getOrCreateRun(runId);
188+
const state = this.getTrackedRun(runId);
181189
const previousDisplayText = state.displayText;
182190
this.updateRunState(state, message, showThinking, {
183191
boundaryDropMode: "streamed-or-incoming",
@@ -192,7 +200,7 @@ export class TuiStreamAssembler {
192200

193201
/** Finalizes a run, combines any error text, and drops stored stream state. */
194202
finalize(runId: string, message: unknown, showThinking: boolean, errorMessage?: string): string {
195-
const state = this.getOrCreateRun(runId);
203+
const state = this.getRunForFinalize(runId);
196204
const streamedDisplayText = state.displayText;
197205
const streamedTextBlocks = [...state.contentBlocks];
198206
const streamedSawNonTextContentBlocks = state.sawNonTextContentBlocks;

0 commit comments

Comments
 (0)