Skip to content

Commit e263e41

Browse files
committed
fix(gateway): close interleaved history groups
1 parent 21462dd commit e263e41

3 files changed

Lines changed: 73 additions & 28 deletions

File tree

‎src/gateway/session-history-state.test.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,25 +327,36 @@ describe("SessionHistorySseState", () => {
327327

328328
test("keeps same-sequence projected rows reachable across cursor pages", () => {
329329
const rawMessages = [
330-
userTextMessage("reply here", 1),
330+
userTextMessage("send both here", 1),
331331
{
332-
role: "assistant",
333-
content: [messageToolCall("call-message-cursor", "Cursor-visible reply.")],
332+
role: "assistant" as const,
333+
content: [
334+
messageToolCall("call-message-first", "First visible reply."),
335+
messageToolCall("call-message-second", "Second visible reply."),
336+
],
334337
__openclaw: { seq: 2 },
335338
},
336-
messageToolResult("call-message-cursor", "cursor", 3),
337-
assistantTextMessage("NO_REPLY", 4),
339+
messageToolResult("call-message-first", "first", 3),
340+
messageToolResult("call-message-second", "second", 4),
341+
assistantTextMessage("NO_REPLY", 5),
338342
];
339343

340344
const newest = buildSessionHistorySnapshot({ rawMessages, limit: 1 }).history;
341345
expect(newest.messages).toMatchObject([
342-
{ role: "toolResult", toolCallId: "call-message-cursor", __openclaw: { seq: 3 } },
346+
{ role: "toolResult", toolCallId: "call-message-first", __openclaw: { seq: 3 } },
347+
{ role: "toolResult", toolCallId: "call-message-second", __openclaw: { seq: 4 } },
343348
{
344349
role: "assistant",
345-
content: [{ text: "Cursor-visible reply." }],
346-
openclawMessageToolMirror: { toolCallId: "call-message-cursor" },
350+
content: [{ text: "First visible reply." }],
351+
openclawMessageToolMirror: { toolCallId: "call-message-first" },
347352
__openclaw: { seq: 3 },
348353
},
354+
{
355+
role: "assistant",
356+
content: [{ text: "Second visible reply." }],
357+
openclawMessageToolMirror: { toolCallId: "call-message-second" },
358+
__openclaw: { seq: 4 },
359+
},
349360
]);
350361
expect(newest.nextCursor).toBe("3");
351362

@@ -355,7 +366,11 @@ describe("SessionHistorySseState", () => {
355366
cursor: newest.nextCursor,
356367
}).history;
357368
expect(middle.messages).toMatchObject([
358-
{ role: "assistant", content: [{ id: "call-message-cursor" }], __openclaw: { seq: 2 } },
369+
{
370+
role: "assistant",
371+
content: [{ id: "call-message-first" }, { id: "call-message-second" }],
372+
__openclaw: { seq: 2 },
373+
},
359374
]);
360375
expect(middle.nextCursor).toBe("2");
361376

@@ -364,7 +379,7 @@ describe("SessionHistorySseState", () => {
364379
limit: 1,
365380
cursor: middle.nextCursor,
366381
}).history;
367-
expect(oldest.messages).toEqual([userTextMessage("reply here", 1)]);
382+
expect(oldest.messages).toEqual([userTextMessage("send both here", 1)]);
368383
expect(oldest.hasMore).toBe(false);
369384
expect(oldest.nextCursor).toBeUndefined();
370385
});

‎src/gateway/session-history-state.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,24 @@ function paginateSessionMessages(
146146
}
147147
}
148148
let start = typeof limit === "number" && limit > 0 ? Math.max(0, endExclusive - limit) : 0;
149-
const boundarySeq = resolveMessageSeq(messages[start]);
150-
// Projection can expand one transcript record into several display rows.
151-
// Keep equal-seq runs atomic because the public cursor cannot address a row within the run.
152-
while (start > 0) {
153-
if (boundarySeq === undefined || resolveMessageSeq(messages[start - 1]) !== boundarySeq) {
154-
break;
149+
// Projection can interleave several rows from the same transcript records.
150+
// Close the page over their seq groups because the public cursor cannot split one.
151+
const pageSeqs = new Set(
152+
messages.slice(start, endExclusive).map(resolveMessageSeq).filter(Boolean),
153+
);
154+
const gapSeqs = new Set<number>();
155+
for (let index = start - 1; index >= 0; index--) {
156+
const seq = resolveMessageSeq(messages[index]);
157+
if (seq === undefined) {
158+
continue;
159+
}
160+
gapSeqs.add(seq);
161+
if (!pageSeqs.has(seq)) {
162+
continue;
155163
}
156-
start -= 1;
164+
start = index;
165+
gapSeqs.forEach((gapSeq) => pageSeqs.add(gapSeq));
166+
gapSeqs.clear();
157167
}
158168
const paginatedMessages = messages.slice(start, endExclusive);
159169
const firstSeq = resolveMessageSeq(paginatedMessages[0]);

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,21 +1108,37 @@ describe("session history HTTP endpoints", () => {
11081108
content: [
11091109
{
11101110
type: "toolCall",
1111-
id: "call-message-cursor",
1111+
id: "call-message-first",
11121112
name: "message",
1113-
arguments: { action: "send", message: "Cursor-visible reply." },
1113+
arguments: { action: "send", message: "First visible reply." },
1114+
},
1115+
{
1116+
type: "toolCall",
1117+
id: "call-message-second",
1118+
name: "message",
1119+
arguments: { action: "send", message: "Second visible reply." },
11141120
},
11151121
],
11161122
timestamp: sharedTimestamp,
11171123
},
11181124
},
11191125
{
1120-
id: "history-tool-result",
1126+
id: "history-tool-result-first",
1127+
message: {
1128+
role: "toolResult",
1129+
toolName: "message",
1130+
toolCallId: "call-message-first",
1131+
content: { ok: true, messageId: "same-sequence-first" },
1132+
timestamp: sharedTimestamp,
1133+
},
1134+
},
1135+
{
1136+
id: "history-tool-result-second",
11211137
message: {
11221138
role: "toolResult",
11231139
toolName: "message",
1124-
toolCallId: "call-message-cursor",
1125-
content: { ok: true, messageId: "same-sequence-result" },
1140+
toolCallId: "call-message-second",
1141+
content: { ok: true, messageId: "same-sequence-second" },
11261142
timestamp: sharedTimestamp,
11271143
},
11281144
},
@@ -1140,8 +1156,10 @@ describe("session history HTTP endpoints", () => {
11401156
query: "?limit=1",
11411157
});
11421158
expect(firstPage.messages?.map(sessionHistoryRowIdentity)).toEqual([
1143-
"3:toolResult:call-message-cursor",
1144-
"3:mirror:Cursor-visible reply.",
1159+
"3:toolResult:call-message-first",
1160+
"4:toolResult:call-message-second",
1161+
"3:mirror:First visible reply.",
1162+
"4:mirror:Second visible reply.",
11451163
]);
11461164
expect(firstPage.hasMore).toBe(true);
11471165
expect(firstPage.nextCursor).toBe("3");
@@ -1177,13 +1195,15 @@ describe("session history HTTP endpoints", () => {
11771195
const chronologicalRows = pages.toReversed().flatMap((page) => page.messages ?? []);
11781196
expect(chronologicalRows.map(sessionHistoryRowIdentity)).toEqual([
11791197
"1:user:reply here",
1180-
"2:assistant:call-message-cursor",
1181-
"3:toolResult:call-message-cursor",
1182-
"3:mirror:Cursor-visible reply.",
1198+
"2:assistant:call-message-first",
1199+
"3:toolResult:call-message-first",
1200+
"4:toolResult:call-message-second",
1201+
"3:mirror:First visible reply.",
1202+
"4:mirror:Second visible reply.",
11831203
]);
11841204
expect(
11851205
chronologicalRows.map((message) => requireRecord(message, "history timestamp").timestamp),
1186-
).toEqual(Array.from({ length: 4 }, () => sharedTimestamp));
1206+
).toEqual(Array.from({ length: 6 }, () => sharedTimestamp));
11871207
expect(
11881208
pages
11891209
.flatMap((page) => page.messages ?? [])

0 commit comments

Comments
 (0)