Skip to content

Commit 0e054de

Browse files
authored
fix(gateway): preserve replies after hidden user turns (#129426)
* fix(gateway): preserve replies after hidden user turns * test(gateway): satisfy hidden-turn metadata lint * test(agents): await native proxy socket closure
1 parent cf457d0 commit 0e054de

3 files changed

Lines changed: 64 additions & 16 deletions

File tree

‎src/agents/runtime/proxy.test.ts‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -889,25 +889,17 @@ describe("streamProxy loopback /api/stream", () => {
889889
throw new Error("expected loopback server address");
890890
}
891891

892-
const result = await resultWithinMs(
893-
streamProxy(model, context, {
894-
authToken: "token",
895-
proxyUrl: `http://127.0.0.1:${address.port}`,
896-
timeoutMs: 3_000,
897-
}),
898-
1_000,
899-
);
892+
const result = await streamProxy(model, context, {
893+
authToken: "token",
894+
proxyUrl: `http://127.0.0.1:${address.port}`,
895+
timeoutMs: 3_000,
896+
}).result();
897+
// Native cancellation closes the remote socket asynchronously; the test owns the deadline.
898+
await closed;
900899
expect(result).toMatchObject({
901900
...entry.expected,
902901
content: [{ type: "text", text: "visible" }],
903902
});
904-
const socketClosed = await Promise.race([
905-
closed.then(() => true),
906-
new Promise<boolean>((resolve) => {
907-
setTimeout(() => resolve(false), 300);
908-
}),
909-
]);
910-
expect(socketClosed).toBe(true);
911903
},
912904
);
913905

‎src/gateway/chat-display-projection.history.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export function filterVisibleProjectedHistoryMessages(
405405
continue;
406406
}
407407
if (
408-
isDuplicateAcpGatewayInjectedMessage(current, visible.at(-1)) ||
408+
isDuplicateAcpGatewayInjectedMessage(current, messages[i - 1]) ||
409409
isDuplicateChannelFinalDeliveryMirror(current, messages[i - 1])
410410
) {
411411
changed = true;

‎src/gateway/sessions-history-http.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,62 @@ describe("session history HTTP endpoints", () => {
12421242
});
12431243
});
12441244

1245+
test("keeps repeated assistant replies from separate hidden user turns in REST and SSE history", async () => {
1246+
const storePath = await createSessionStoreFile();
1247+
const sessionId = "sess-hidden-turn-replies";
1248+
const sessionKey = "agent:main:main";
1249+
await writeSessionStore({
1250+
entries: { main: { sessionId, updatedAt: Date.now() } },
1251+
storePath,
1252+
});
1253+
const assistantMessage = (text: string, model: string) =>
1254+
makeTranscriptAssistantMessage({ text, provider: "openclaw", model });
1255+
await replaceTranscriptEvents({ agentId: AGENT_ID, sessionId, sessionKey, storePath }, [
1256+
{ type: "session", version: 1, id: sessionId },
1257+
{ message: assistantMessage("First reply.", "acp-runtime") },
1258+
{ message: { role: "user", content: "" } },
1259+
{ message: assistantMessage("First reply.", "gateway-injected") },
1260+
{ message: assistantMessage("Second reply.", "acp-runtime") },
1261+
{ message: { role: "user", content: HEARTBEAT_PROMPT } },
1262+
{ message: assistantMessage("Second reply.", "gateway-injected") },
1263+
{ message: assistantMessage("Third reply.", "acp-runtime") },
1264+
{ message: { role: "user", content: HEARTBEAT_PROMPT } },
1265+
{ message: { role: "assistant", content: "HEARTBEAT_OK" } },
1266+
{ message: assistantMessage("Third reply.", "gateway-injected") },
1267+
]);
1268+
1269+
const expectedRows = [
1270+
"1:assistant:First reply.",
1271+
"3:assistant:First reply.",
1272+
"4:assistant:Second reply.",
1273+
"6:assistant:Second reply.",
1274+
"7:assistant:Third reply.",
1275+
"10:assistant:Third reply.",
1276+
];
1277+
await withGatewayHarness(async (harness) => {
1278+
const history = await readSessionHistoryBody(harness.port, sessionKey);
1279+
expect(history.messages?.map(sessionHistoryRowIdentity)).toEqual(expectedRows);
1280+
expect(history.messages?.map((message) => message["__openclaw"]?.turnBoundary)).toEqual([
1281+
undefined,
1282+
undefined,
1283+
undefined,
1284+
true,
1285+
undefined,
1286+
true,
1287+
]);
1288+
1289+
const stream = await openSessionHistorySse(harness.port, sessionKey);
1290+
try {
1291+
const event = await readSseEvent(stream.reader, stream.streamState);
1292+
expect(event.event).toBe("history");
1293+
const streamedHistory = event.data as SessionHistoryBody;
1294+
expect(streamedHistory.messages?.map(sessionHistoryRowIdentity)).toEqual(expectedRows);
1295+
} finally {
1296+
await stream.reader.cancel();
1297+
}
1298+
});
1299+
});
1300+
12451301
test.each([
12461302
{ name: "all-silent tail", heartbeatBoundary: false, silentMessages: 40 },
12471303
{ name: "heartbeat context boundary", heartbeatBoundary: true, silentMessages: 39 },

0 commit comments

Comments
 (0)