fix(tui): preserve user message during slow model responses (#53115)

When a local run ends with an empty final event while another run is active,
skip history reload to prevent clearing the user's pending message from the
chat log. This fixes the 'message disappears' issue with slow models like Ollama.
This commit is contained in:
joelnishanth
2026-03-23 13:28:03 -07:00
committed by Peter Steinberger
parent 5e9ea804d4
commit cc8ed8d25b
2 changed files with 26 additions and 0 deletions

View File

@@ -590,4 +590,21 @@ describe("tui-event-handlers: handleAgentEvent", () => {
expect(loadHistory).toHaveBeenCalledTimes(1);
});
it("does not reload history for local run with empty final when another run is active (#53115)", () => {
const { state, loadHistory, noteLocalRunId, handleChatEvent } = createHandlersHarness({
state: { activeChatRunId: "run-main" },
});
noteLocalRunId("run-local-empty");
handleChatEvent({
runId: "run-local-empty",
sessionKey: state.currentSessionKey,
state: "final",
});
expect(state.activeChatRunId).toBe("run-main");
expect(loadHistory).not.toHaveBeenCalled();
});
});

View File

@@ -158,9 +158,18 @@ export function createEventHandlers(context: EventHandlerContext) {
const isLocalRun = isLocalRunId?.(runId) ?? false;
if (isLocalRun) {
forgetLocalRunId?.(runId);
// Never reload history for local runs that ended without displayable output.
// This prevents the user's message from disappearing when the backend is slow
// (e.g., Ollama) and sends an empty final event before the response is ready.
if (!opts?.allowLocalWithoutDisplayableFinal) {
return;
}
// Skip history reload if a DIFFERENT run is still active.
// This prevents clearing the user's pending message when a stale/concurrent
// empty final event arrives while a new message is being processed.
if (state.activeChatRunId && state.activeChatRunId !== runId) {
return;
}
}
if (hasConcurrentActiveRun(runId)) {
return;