Skip to content

Commit 70ce23d

Browse files
committed
fix(ui): keep the projected first prompt above the replies it started
A pane that admits the accepted prompt before its transcript bootstrap resolves leaves that row in chatMessages while the history request is in flight. The merge then re-placed it as a late optimistic tail, and since #113266 stable rows keep insertion order, the first turn rendered below the tool activity it kicked off. The handoff reconciler owns that head row, so exclude it from the late tail.
1 parent 21b5925 commit 70ce23d

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎ui/src/e2e/new-session-page.e2e.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describeControlUiE2e("Control UI new-session page mocked Gateway E2E", () => {
268268
const sessionKey = "agent:main:visible-initial-prompt";
269269
const message = "keep this prompt visible while the agent works";
270270
const activeOutputTimestamp = Date.now() + 60_000;
271-
const gateway = await installMockGateway(page, {
271+
await installMockGateway(page, {
272272
methodResponses: {
273273
"sessions.create": { key: sessionKey, runStarted: true },
274274
"chat.history": {

‎ui/src/pages/chat/chat-history.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ import {
5656
preserveOptimisticTailMessages,
5757
readTranscriptSequence,
5858
} from "./history-merge.ts";
59-
import { reconcileInitialUserMessageHandoff } from "./initial-turn-handoff.ts";
59+
import {
60+
isPendingInitialUserMessage,
61+
reconcileInitialUserMessageHandoff,
62+
} from "./initial-turn-handoff.ts";
6063
import {
6164
controlUiNowMs,
6265
recordControlUiPerformanceEvent,
@@ -1187,6 +1190,9 @@ async function loadChatHistoryUncached(
11871190
reconciledTerminal.previousMessages,
11881191
reconciledTerminal.currentMessages,
11891192
authoritativeMessages,
1193+
).filter(
1194+
(message) =>
1195+
!isPendingInitialUserMessage(state.initialUserMessage, state, sessionKey, message),
11901196
);
11911197
state.chatMessages = preserveOptimisticTailMessages(
11921198
authoritativeMessages,

‎ui/src/pages/chat/initial-turn-handoff.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
22
import { createInitialUserMessageHandoff } from "../../app/initial-user-message-handoff.ts";
33
import {
44
admitInitialUserMessageHandoff,
5+
isPendingInitialUserMessage,
56
prepareInitialUserMessageHandoff,
67
reconcileInitialUserMessageHandoff,
78
} from "./initial-turn-handoff.ts";
@@ -82,6 +83,33 @@ describe("initial user message handoff", () => {
8283
);
8384
});
8485

86+
it("identifies an already-projected prompt so history cannot re-place it", () => {
87+
const sessionKey = "agent:main:new-session";
88+
const client = {};
89+
const handoff = createInitialUserMessageHandoff();
90+
prepareInitialUserMessageHandoff(
91+
handoff,
92+
sessionKey,
93+
{ text: "keep this above the replies it started", createdAt: 10 },
94+
client,
95+
);
96+
97+
const host = { chatMessages: [] as unknown[], client };
98+
admitInitialUserMessageHandoff(handoff, host, sessionKey);
99+
const projected = host.chatMessages[0];
100+
101+
expect(isPendingInitialUserMessage(handoff, host, sessionKey, projected)).toBe(true);
102+
expect(
103+
isPendingInitialUserMessage(handoff, host, sessionKey, {
104+
role: "assistant",
105+
content: [{ type: "text", text: "reply" }],
106+
timestamp: 11,
107+
}),
108+
).toBe(false);
109+
expect(isPendingInitialUserMessage(handoff, host, "agent:main:other", projected)).toBe(false);
110+
expect(isPendingInitialUserMessage(undefined, host, sessionKey, projected)).toBe(false);
111+
});
112+
85113
it("does not duplicate a first prompt that history already loaded", () => {
86114
const sessionKey = "agent:main:main";
87115
const routeSessionKey = "main";

‎ui/src/pages/chat/initial-turn-handoff.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ export function admitInitialUserMessageHandoff(
203203
return true;
204204
}
205205

206+
/**
207+
* The projected prompt is a head row owned by reconcileInitialUserMessageHandoff.
208+
* A history merge that re-places it as a late optimistic tail would render the
209+
* first turn below the replies it started.
210+
*/
211+
export function isPendingInitialUserMessage(
212+
handoff: ApplicationInitialUserMessageHandoff | undefined,
213+
host: { client?: object | null },
214+
sessionKey: string,
215+
candidate: unknown,
216+
): boolean {
217+
const message = handoff?.read(sessionKey, host.client ?? null);
218+
return Boolean(message && isSameInitialUserMessage(candidate, message));
219+
}
220+
206221
/** Keeps the accepted prompt projected until authoritative history owns it. */
207222
export function reconcileInitialUserMessageHandoff(
208223
handoff: ApplicationInitialUserMessageHandoff,

0 commit comments

Comments
 (0)