Skip to content

Commit 18eed42

Browse files
wings1029claude
andcommitted
fix(ui): harden inFlightRun adoption — normalize whitespace in reconciliation, clear stale errors, add edge-case tests
Co-Authored-By: Claude <[email protected]>
1 parent 8e70a36 commit 18eed42

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎ui/src/ui/controllers/chat.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,4 +3748,87 @@ describe("loadChatHistory inFlightRun adoption (#90755)", () => {
37483748
expect(state.chatRunId).toBe("run-fresh");
37493749
expect(state.chatStream).toBe("fresh live text");
37503750
});
3751+
3752+
it("reconciles overlap when the buffer has leading whitespace not present in the committed message", async () => {
3753+
// Normalize whitespace before comparison: if the buffer starts with \n but
3754+
// the committed assistant message does not, the dedup should still work.
3755+
const messages = [
3756+
{ role: "user", content: [{ type: "text", text: "go ahead" }] },
3757+
{ role: "assistant", content: [{ type: "text", text: "Starting analysis..." }] },
3758+
];
3759+
const request = vi.fn().mockResolvedValue({
3760+
messages,
3761+
inFlightRun: { runId: "run-ws", text: "\nStarting analysis...\nPhase 1 complete.\nPhase 2 in progress..." },
3762+
});
3763+
const state = createState({
3764+
client: { request } as unknown as ChatState["client"],
3765+
connected: true,
3766+
chatRunId: null,
3767+
chatStream: null,
3768+
chatStreamStartedAt: null,
3769+
chatError: "previous stale error",
3770+
lastError: "another stale error",
3771+
});
3772+
3773+
await loadChatHistory(state);
3774+
3775+
expect(state.chatRunId).toBe("run-ws");
3776+
// The leading \n from the buffer is preserved; only the overlapping text is deduped
3777+
expect(state.chatStream).toContain("Phase 1 complete");
3778+
expect(state.chatStream).toContain("Phase 2 in progress");
3779+
expect(state.chatStream).not.toContain("Starting analysis...");
3780+
expect(state.chatStreamStartedAt).toEqual(expect.any(Number));
3781+
// Stale errors cleared on adoption
3782+
expect(state.chatError).toBeNull();
3783+
expect(state.lastError).toBeNull();
3784+
});
3785+
3786+
it("preserves full buffer when last visible message is not an assistant reply", async () => {
3787+
// Only assistant messages participate in reconciliation; user messages
3788+
// should not cause the buffer to be trimmed.
3789+
const messages = [
3790+
{ role: "user", content: [{ type: "text", text: "run lint" }] },
3791+
];
3792+
const request = vi.fn().mockResolvedValue({
3793+
messages,
3794+
inFlightRun: { runId: "run-nonassist", text: "Lint output:\n35 errors found" },
3795+
});
3796+
const state = createState({
3797+
client: { request } as unknown as ChatState["client"],
3798+
connected: true,
3799+
chatRunId: null,
3800+
chatStream: null,
3801+
chatStreamStartedAt: null,
3802+
});
3803+
3804+
await loadChatHistory(state);
3805+
3806+
expect(state.chatRunId).toBe("run-nonassist");
3807+
// Full buffer adopted since last message is user, not assistant
3808+
expect(state.chatStream).toBe("Lint output:\n35 errors found");
3809+
});
3810+
3811+
it("clears stale errors when adopting an in-flight run", async () => {
3812+
const messages = [{ role: "user", content: [{ type: "text", text: "Hello" }] }];
3813+
const request = vi.fn().mockResolvedValue({
3814+
messages,
3815+
inFlightRun: { runId: "run-err", text: "fresh stream" },
3816+
});
3817+
const state = createState({
3818+
client: { request } as unknown as ChatState["client"],
3819+
connected: true,
3820+
chatRunId: null,
3821+
chatStream: null,
3822+
chatStreamStartedAt: null,
3823+
chatError: "gateway timeout",
3824+
lastError: "request failed",
3825+
});
3826+
3827+
await loadChatHistory(state);
3828+
3829+
expect(state.chatRunId).toBe("run-err");
3830+
expect(state.chatStream).toBe("fresh stream");
3831+
expect(state.chatError).toBeNull();
3832+
expect(state.lastError).toBeNull();
3833+
});
37513834
});

‎ui/src/ui/controllers/chat.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ function adoptInFlightRunFromChatHistory(
699699
// Reconcile overlap with the last committed assistant message: if the
700700
// cumulative in-flight buffer starts with text already persisted as an
701701
// assistant reply, only adopt the new suffix.
702+
// Normalize whitespace on both sides before comparing so that small framing
703+
// differences (e.g. leading newlines the gateway buffer includes but the
704+
// committed message omits) do not silently defeat the dedup and cause
705+
// duplicated text to render.
702706
if (text && visibleMessages.length > 0) {
703707
const lastVisible = visibleMessages[visibleMessages.length - 1];
704708
const lastRole =
@@ -707,17 +711,32 @@ function adoptInFlightRunFromChatHistory(
707711
: undefined;
708712
if (lastRole === "assistant") {
709713
const lastText = extractText(lastVisible);
710-
if (lastText && text.startsWith(lastText)) {
711-
text = text.slice(lastText.length);
714+
if (lastText) {
715+
const committedTrimmed = lastText.replace(/^[\s]+|[\s]+$/g, "");
716+
const bufferTrimmed = text.replace(/^[\s]+|[\s]+$/g, "");
717+
if (committedTrimmed && bufferTrimmed.startsWith(committedTrimmed)) {
718+
// Extract only the suffix, preserving the original buffer framing.
719+
const overlapIdx = text.indexOf(committedTrimmed);
720+
if (overlapIdx >= 0) {
721+
text = text.slice(overlapIdx + committedTrimmed.length);
722+
} else {
723+
// Normalized text matches but framing whitespace differs (e.g.
724+
// leading \n in the buffer not present in the committed message).
725+
text = text.slice(text.length - (bufferTrimmed.length - committedTrimmed.length));
726+
}
727+
}
712728
}
713729
}
714730
}
715731

716732
// Adopt the run: track the run id so live deltas/finals continue, set the
717733
// stream text so partial content renders (empty string shows a reading
718-
// indicator instead of an idle thread), and mark the stream start time.
734+
// indicator instead of an idle thread), clear stale errors from the previous
735+
// session view, and mark the stream start time.
719736
state.chatRunId = runId;
720737
state.chatStream = text;
738+
state.chatError = null;
739+
state.lastError = null;
721740
state.chatStreamStartedAt = Date.now();
722741
}
723742

0 commit comments

Comments
 (0)