Skip to content

Commit c960a56

Browse files
roboclaw-botVACInc
andcommitted
fix: keep retained inputs in transcript order
Worked on by: - @VACInc Co-authored-by: Vito Cappello <[email protected]>
1 parent 00d542d commit c960a56

4 files changed

Lines changed: 90 additions & 12 deletions

File tree

‎src/gateway/server-methods/chat-history-handler.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,59 @@ import { chatHistoryHandlers } from "./chat-history-handler.js";
1515
import type { GatewayRequestContext, RespondFn } from "./types.js";
1616

1717
describe("chat history consumption receipts", () => {
18+
it("projects pending input at its acceptance time", async () => {
19+
await withOpenClawTestState({ scenario: "minimal" }, async () => {
20+
const now = vi.spyOn(Date, "now").mockReturnValue(2_000);
21+
const scope = {
22+
agentId: "main",
23+
sessionKey: "agent:main:pending-display-time",
24+
sessionId: "pending-display-time",
25+
};
26+
await upsertSessionEntryCore(scope, { sessionId: scope.sessionId, updatedAt: 1 });
27+
const receipt = expectDefined(
28+
await stageSessionPendingInput(scope, {
29+
runId: "pending-display-run",
30+
assertCurrent: () => {},
31+
message: {
32+
role: "user",
33+
content: "Display me where I was accepted",
34+
timestamp: 1_000,
35+
idempotencyKey: "pending-display-run:user",
36+
},
37+
}),
38+
"pending input receipt",
39+
);
40+
try {
41+
let result: unknown;
42+
await expectDefined(
43+
chatHistoryHandlers["chat.history"],
44+
"history handler",
45+
)({
46+
params: { sessionKey: scope.sessionKey },
47+
context: createDirectChatContext(),
48+
req: { type: "req", id: "history", method: "chat.history" },
49+
client: null,
50+
isWebchatConnect: () => false,
51+
respond: (ok, payload, error) => {
52+
expect(error).toBeUndefined();
53+
expect(ok).toBe(true);
54+
result = payload;
55+
},
56+
});
57+
const page = expectDefined(asOptionalRecord(result), "history response");
58+
const pendingInputs = expectDefined(asOptionalRecord(page.pendingInputs), "pending inputs");
59+
const [pending] = pendingInputs.items as Array<Record<string, unknown>>;
60+
expect(pending).toMatchObject({
61+
acceptedAt: 2_000,
62+
message: { content: "Display me where I was accepted", timestamp: 2_000 },
63+
});
64+
} finally {
65+
receipt.finish("interrupted");
66+
now.mockRestore();
67+
}
68+
});
69+
});
70+
1871
it.each(["chat.history", "chat.startup"] as const)(
1972
"%s returns only requested current-session receipts in pages and empty deltas",
2073
async (method) => {

‎src/gateway/server-methods/chat-pending-inputs.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function projectPendingInputMessage(input: SessionPendingInput, maxChars:
2727
delete metadata.runId;
2828
return {
2929
...message,
30+
timestamp: input.acceptedAt,
3031
idempotencyKey: undefined,
3132
__openclaw: { ...metadata, id: `${CHAT_PENDING_INPUT_MESSAGE_PREFIX}${input.id}` },
3233
};

‎ui/src/pages/chat/chat-pending-inputs.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const input: ChatPendingInputsPage["items"][number] = {
5151
message: {
5252
role: "user",
5353
content: "Keep my accepted input",
54+
timestamp: 100,
5455
__openclaw: { id: "pending:input-1" },
5556
},
5657
};
@@ -787,6 +788,34 @@ describe("server-owned pending input display", () => {
787788
});
788789
});
789790

791+
it("places accepted input at its acceptance time instead of after newer history", () => {
792+
const earlier = { role: "assistant", content: "Earlier reply", timestamp: 50 };
793+
const later = { role: "assistant", content: "Later reply", timestamp: 150 };
794+
const items = buildChatItems({
795+
paneId: "chronological-pending-pane",
796+
sessionKey,
797+
messages: [earlier, later],
798+
pendingInputs: page.items,
799+
queue: [],
800+
toolMessages: [],
801+
streamSegments: [],
802+
stream: null,
803+
streamStartedAt: null,
804+
showToolCalls: true,
805+
});
806+
807+
expect(items).toMatchObject([
808+
{ kind: "group", role: "assistant", messages: [{ message: earlier }] },
809+
{
810+
kind: "group",
811+
role: "user",
812+
messages: [{ message: { content: "Keep my accepted input" } }],
813+
},
814+
{ kind: "notice", timestamp: input.acceptedAt },
815+
{ kind: "group", role: "assistant", messages: [{ message: later }] },
816+
]);
817+
});
818+
790819
it.each(["user", "assistant"])(
791820
"preserves a canonical %s sharing the pending run correlation",
792821
(role) => {

‎ui/src/pages/chat/chat-thread-build.ts‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@ export function buildChatItems(props: BuildChatItemsProps): Array<ChatItem | Mes
238238
earliest == null ? queued.createdAt : Math.min(earliest, queued.createdAt),
239239
null,
240240
);
241-
// Live tool cards and stream segments are collected separately and merged into
242-
// the stable history + queued-send rows by timestamp below. We never reorder
243-
// the stable rows themselves, so optimistic user bubbles stay after the
244-
// preceding assistant reply even when client and Gateway clocks disagree.
245-
const timestampedProjectionItems: ChatItem[] = [];
241+
// Transient projections merge into stable history + queued-send rows by timestamp.
242+
// Stable rows keep their relative order despite client and Gateway clock skew.
243+
const timestampedProjectionItems: ChatItem[] = buildPendingInputItems(
244+
pendingInputs,
245+
history,
246+
props.searchOpen ? props.searchQuery : undefined,
247+
);
246248
const appendQueuedSend = (queued: ChatQueueItem, beforeMessage?: unknown) => {
247249
if (!shouldRenderQueuedSendInThread(queued)) {
248250
return;
@@ -679,12 +681,5 @@ export function buildChatItems(props: BuildChatItemsProps): Array<ChatItem | Mes
679681
appendQueuedSend(queued);
680682
}
681683

682-
items.push(
683-
...buildPendingInputItems(
684-
pendingInputs,
685-
history,
686-
props.searchOpen ? props.searchQuery : undefined,
687-
),
688-
);
689684
return groupMessages(coalesceToolActivityMessages(items));
690685
}

0 commit comments

Comments
 (0)