Skip to content

Commit 33b732c

Browse files
committed
perf(gateway): bound older chat history pages
1 parent b840a3c commit 33b732c

10 files changed

Lines changed: 296 additions & 303 deletions

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,6 @@ export function projectChatDisplayMessages(
345345
return projectChatDisplayMessagesWithState(messages, options).messages;
346346
}
347347

348-
function limitChatDisplayMessages<T>(messages: T[], maxMessages?: number): T[] {
349-
if (
350-
typeof maxMessages !== "number" ||
351-
!Number.isFinite(maxMessages) ||
352-
maxMessages <= 0 ||
353-
messages.length <= maxMessages
354-
) {
355-
return messages;
356-
}
357-
return messages.slice(-Math.floor(maxMessages));
358-
}
359-
360-
export function projectRecentChatDisplayMessages(
361-
messages: unknown[],
362-
options?: ChatDisplayProjectionOptions & { maxMessages?: number },
363-
): Array<Record<string, unknown>> {
364-
return limitChatDisplayMessages(
365-
projectChatDisplayMessages(messages, options),
366-
options?.maxMessages,
367-
);
368-
}
369-
370348
export function projectChatDisplayMessage(
371349
message: unknown,
372350
options?: ChatDisplayProjectionOptions,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export {
44
projectChatDisplayMessage,
55
projectChatDisplayMessages,
66
projectChatDisplayMessagesWithState,
7-
projectRecentChatDisplayMessages,
87
} from "./chat-display-projection.core.js";
98
export {
109
DEFAULT_CHAT_HISTORY_TEXT_MAX_CHARS,

‎src/gateway/server-methods/server-methods.test.ts‎

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
DEFAULT_CHAT_HISTORY_TEXT_MAX_CHARS,
3636
augmentChatHistoryWithCanvasBlocks,
3737
dropPreSessionStartAnnouncePairs,
38-
projectRecentChatDisplayMessages,
38+
projectChatDisplayMessages,
3939
resolveEffectiveChatHistoryMaxChars,
4040
sanitizeChatHistoryMessages,
4141
} from "../chat-display-projection.js";
@@ -1146,7 +1146,7 @@ describe("sanitizeChatHistoryMessages", () => {
11461146
});
11471147
});
11481148

1149-
describe("projectRecentChatDisplayMessages", () => {
1149+
describe("projectChatDisplayMessages", () => {
11501150
const safeFailureContent = [
11511151
{ type: "text", text: "The agent run failed before producing a reply." },
11521152
];
@@ -1317,7 +1317,7 @@ describe("projectRecentChatDisplayMessages", () => {
13171317
];
13181318

13191319
it.each(displayErrorCases)("$name", ({ message, content, visibleText }) => {
1320-
const result = projectRecentChatDisplayMessages([
1320+
const result = projectChatDisplayMessages([
13211321
{ role: "assistant", stopReason: "error", timestamp: 1, ...message },
13221322
]);
13231323
expect(result).toEqual([
@@ -1371,7 +1371,7 @@ describe("projectRecentChatDisplayMessages", () => {
13711371
])(
13721372
"projects empty context-overflow assistant errors with recovery guidance: $name",
13731373
({ fields }) => {
1374-
const result = projectRecentChatDisplayMessages([
1374+
const result = projectChatDisplayMessages([
13751375
{
13761376
role: "assistant",
13771377
content: [],
@@ -1405,7 +1405,7 @@ describe("projectRecentChatDisplayMessages", () => {
14051405
["input_text", ""],
14061406
["input_text", "NO_REPLY"],
14071407
])("projects hidden %s assistant errors %j as a generic safe failure", (type, text) => {
1408-
const result = projectRecentChatDisplayMessages([
1408+
const result = projectChatDisplayMessages([
14091409
{
14101410
role: "assistant",
14111411
content: [{ type, text }],
@@ -1423,7 +1423,7 @@ describe("projectRecentChatDisplayMessages", () => {
14231423
it.each(["NO_REPLY", STREAM_ERROR_FALLBACK_TEXT])(
14241424
"projects display-hidden assistant error text %j as a generic safe failure",
14251425
(text) => {
1426-
const result = projectRecentChatDisplayMessages([
1426+
const result = projectChatDisplayMessages([
14271427
{
14281428
role: "assistant",
14291429
content: [{ type: "text", text }],
@@ -1446,7 +1446,7 @@ describe("projectRecentChatDisplayMessages", () => {
14461446
it.each([undefined, ""])(
14471447
"projects repaired stream errors with errorMessage %j as a generic safe failure",
14481448
(errorMessage) => {
1449-
const result = projectRecentChatDisplayMessages([
1449+
const result = projectChatDisplayMessages([
14501450
assistantHistoryMessage(STREAM_ERROR_FALLBACK_TEXT, {
14511451
stopReason: "error",
14521452
...(errorMessage === undefined ? {} : { errorMessage }),
@@ -1492,7 +1492,7 @@ describe("projectRecentChatDisplayMessages", () => {
14921492
expected: [{ type: "text", text: "I'm running on ollama-cloud now." }],
14931493
},
14941494
])("removes an internal stream-error prefix from same-message $name", ({ content, expected }) => {
1495-
const result = projectRecentChatDisplayMessages([
1495+
const result = projectChatDisplayMessages([
14961496
{
14971497
role: "assistant",
14981498
content,
@@ -1516,7 +1516,7 @@ describe("projectRecentChatDisplayMessages", () => {
15161516

15171517
it("keeps intentional mentions of the internal fallback inside a real assistant reply", () => {
15181518
const text = `Diagnostic note: ${STREAM_ERROR_FALLBACK_TEXT}`;
1519-
const result = projectRecentChatDisplayMessages([
1519+
const result = projectChatDisplayMessages([
15201520
assistantHistoryMessage(text, { stopReason: "error" }),
15211521
]);
15221522

@@ -1527,7 +1527,7 @@ describe("projectRecentChatDisplayMessages", () => {
15271527
"keeps literal fallback-prefixed assistant text without error provenance %j",
15281528
(stopReason) => {
15291529
const text = `${STREAM_ERROR_FALLBACK_TEXT} actual quoted text`;
1530-
const result = projectRecentChatDisplayMessages([
1530+
const result = projectChatDisplayMessages([
15311531
assistantHistoryMessage(text, stopReason ? { stopReason } : {}),
15321532
]);
15331533

@@ -1536,7 +1536,7 @@ describe("projectRecentChatDisplayMessages", () => {
15361536
);
15371537

15381538
it("removes a synthetic error prefix while preserving displayable image content", () => {
1539-
const result = projectRecentChatDisplayMessages([
1539+
const result = projectChatDisplayMessages([
15401540
{
15411541
role: "assistant",
15421542
content: [
@@ -1553,7 +1553,7 @@ describe("projectRecentChatDisplayMessages", () => {
15531553
});
15541554

15551555
it("drops a repaired stream-error placeholder before same-turn assistant content", () => {
1556-
const result = projectRecentChatDisplayMessages([
1556+
const result = projectChatDisplayMessages([
15571557
userHistoryMessage("hello", { timestamp: 1 }),
15581558
assistantHistoryMessage(STREAM_ERROR_FALLBACK_TEXT, {
15591559
stopReason: "error",
@@ -1570,7 +1570,7 @@ describe("projectRecentChatDisplayMessages", () => {
15701570
});
15711571

15721572
it("keeps a genuine failed turn before a new forwarded inter-session turn", () => {
1573-
const result = projectRecentChatDisplayMessages([
1573+
const result = projectChatDisplayMessages([
15741574
assistantHistoryMessage(STREAM_ERROR_FALLBACK_TEXT, {
15751575
stopReason: "error",
15761576
timestamp: 1,
@@ -1588,7 +1588,7 @@ describe("projectRecentChatDisplayMessages", () => {
15881588
});
15891589

15901590
it("keeps genuine stream-error failures when a hidden assistant row has text", () => {
1591-
const result = projectRecentChatDisplayMessages([
1591+
const result = projectChatDisplayMessages([
15921592
assistantHistoryMessage(STREAM_ERROR_FALLBACK_TEXT, { stopReason: "error" }),
15931593
assistantHistoryMessage("internal-only assistant content", { display: false }),
15941594
]);
@@ -1601,7 +1601,7 @@ describe("projectRecentChatDisplayMessages", () => {
16011601
});
16021602

16031603
it("keeps a stream-error placeholder when the next user turn starts first", () => {
1604-
const result = projectRecentChatDisplayMessages([
1604+
const result = projectChatDisplayMessages([
16051605
assistantHistoryMessage(STREAM_ERROR_FALLBACK_TEXT, { stopReason: "error", timestamp: 1 }),
16061606
userHistoryMessage("retry", { timestamp: 2 }),
16071607
assistantHistoryMessage("fresh answer", { timestamp: 3 }),
@@ -1618,7 +1618,7 @@ describe("projectRecentChatDisplayMessages", () => {
16181618
});
16191619

16201620
it("projects sessions_send inter-session turns as forwarded assistant-side display messages", () => {
1621-
const result = projectRecentChatDisplayMessages([
1621+
const result = projectChatDisplayMessages([
16221622
{
16231623
role: "user",
16241624
content: [
@@ -1648,13 +1648,13 @@ describe("projectRecentChatDisplayMessages", () => {
16481648
});
16491649

16501650
it("projects empty sessions_send inter-session turns before empty user filtering", () => {
1651-
const result = projectRecentChatDisplayMessages([sessionsSendHistoryMessage("", 1)]);
1651+
const result = projectChatDisplayMessages([sessionsSendHistoryMessage("", 1)]);
16521652

16531653
expect(result).toEqual([projectedSessionsSendHistoryMessage("", 1)]);
16541654
});
16551655

16561656
it("does not let sessions_send inter-session turns clear pending message-tool mirrors", () => {
1657-
const result = projectRecentChatDisplayMessages([
1657+
const result = projectChatDisplayMessages([
16581658
{
16591659
role: "assistant",
16601660
content: [
@@ -1719,7 +1719,7 @@ describe("projectRecentChatDisplayMessages", () => {
17191719
});
17201720

17211721
it("keeps forwarded sessions_send control-token text visible after stripping provenance", () => {
1722-
const result = projectRecentChatDisplayMessages([
1722+
const result = projectChatDisplayMessages([
17231723
{
17241724
role: "user",
17251725
content: [
@@ -1741,15 +1741,13 @@ describe("projectRecentChatDisplayMessages", () => {
17411741
});
17421742

17431743
it("keeps forwarded sessions_send heartbeat-looking text visible", () => {
1744-
const result = projectRecentChatDisplayMessages([
1745-
sessionsSendHistoryMessage("HEARTBEAT_OK", 1),
1746-
]);
1744+
const result = projectChatDisplayMessages([sessionsSendHistoryMessage("HEARTBEAT_OK", 1)]);
17471745

17481746
expect(result).toEqual([projectedSessionsSendHistoryMessage("HEARTBEAT_OK", 1)]);
17491747
});
17501748

17511749
it("keeps forwarded sessions_send heartbeat-looking text visible after a heartbeat prompt", () => {
1752-
const result = projectRecentChatDisplayMessages([
1750+
const result = projectChatDisplayMessages([
17531751
userHistoryMessage(HEARTBEAT_PROMPT, { timestamp: 1 }),
17541752
sessionsSendHistoryMessage("HEARTBEAT_OK", 2),
17551753
]);
@@ -1762,7 +1760,7 @@ describe("projectRecentChatDisplayMessages", () => {
17621760
});
17631761

17641762
it("marks only the first visible message after each hidden heartbeat input", () => {
1765-
const result = projectRecentChatDisplayMessages([
1763+
const result = projectChatDisplayMessages([
17661764
userHistoryMessage(HEARTBEAT_PROMPT, { __openclaw: { seq: 1 } }),
17671765
assistantHistoryMessage("First run started.", { __openclaw: { seq: 2 } }),
17681766
assistantHistoryMessage("First run finished.", { __openclaw: { seq: 3 } }),
@@ -1799,7 +1797,7 @@ describe("projectRecentChatDisplayMessages", () => {
17991797
});
18001798

18011799
it("does not project user-authored sessions_send envelope text without provenance", () => {
1802-
const result = projectRecentChatDisplayMessages([
1800+
const result = projectChatDisplayMessages([
18031801
{
18041802
role: "user",
18051803
content: [
@@ -1836,7 +1834,7 @@ describe("projectRecentChatDisplayMessages", () => {
18361834
const visibleText = "forwarded report";
18371835
const textSha256 = createHash("sha256").update(visibleText).digest("hex");
18381836

1839-
const result = projectRecentChatDisplayMessages([
1837+
const result = projectChatDisplayMessages([
18401838
sessionsSendHistoryMessage(visibleText, 1),
18411839
ttsSupplementHistoryMessage({ textSha256 }, 2),
18421840
]);
@@ -1848,7 +1846,7 @@ describe("projectRecentChatDisplayMessages", () => {
18481846
});
18491847

18501848
it("preserves structured trace alongside visible assistant progress text", () => {
1851-
const result = projectRecentChatDisplayMessages(
1849+
const result = projectChatDisplayMessages(
18521850
[
18531851
userHistoryMessage("fix it", { timestamp: 1 }),
18541852
{
@@ -1915,7 +1913,7 @@ describe("projectRecentChatDisplayMessages", () => {
19151913
});
19161914

19171915
it("projects pure keyed commentary as a durable preamble", () => {
1918-
const result = projectRecentChatDisplayMessages(
1916+
const result = projectChatDisplayMessages(
19191917
[
19201918
userHistoryMessage("status", { timestamp: 1 }),
19211919
{
@@ -1953,7 +1951,7 @@ describe("projectRecentChatDisplayMessages", () => {
19531951
});
19541952

19551953
it("drops duplicate ACP gateway-injected assistant replies from chat history", () => {
1956-
const result = projectRecentChatDisplayMessages([
1954+
const result = projectChatDisplayMessages([
19571955
userHistoryMessage("good morning", { timestamp: 1 }),
19581956
assistantHistoryMessage("Good morning.", {
19591957
provider: "openclaw",
@@ -1979,7 +1977,7 @@ describe("projectRecentChatDisplayMessages", () => {
19791977
});
19801978

19811979
it("drops channel-final delivery mirrors that duplicate the preceding assistant reply", () => {
1982-
const result = projectRecentChatDisplayMessages([
1980+
const result = projectChatDisplayMessages([
19831981
{
19841982
role: "user",
19851983
content: "yo big boy",
@@ -2010,7 +2008,7 @@ describe("projectRecentChatDisplayMessages", () => {
20102008
});
20112009

20122010
it("keeps a channel-final delivery mirror after a filtered user turn", () => {
2013-
const result = projectRecentChatDisplayMessages([
2011+
const result = projectChatDisplayMessages([
20142012
assistantHistoryMessage("Repeated reply", {
20152013
provider: "openai",
20162014
model: "gpt-5.5",
@@ -2035,7 +2033,7 @@ describe("projectRecentChatDisplayMessages", () => {
20352033
});
20362034

20372035
it("keeps adjacent channel-final delivery mirrors from distinct sends", () => {
2038-
const result = projectRecentChatDisplayMessages([
2036+
const result = projectChatDisplayMessages([
20392037
deliveryMirrorHistoryMessage("Repeated reply", "message-1", 1),
20402038
deliveryMirrorHistoryMessage("Repeated reply", "message-2", 2),
20412039
]);
@@ -2044,7 +2042,7 @@ describe("projectRecentChatDisplayMessages", () => {
20442042
});
20452043

20462044
it("keeps channel-final mirrors after unmarked assistant replies", () => {
2047-
const result = projectRecentChatDisplayMessages([
2045+
const result = projectChatDisplayMessages([
20482046
assistantHistoryMessage("Repeated reply", {
20492047
provider: "openai",
20502048
model: "gpt-5.5",
@@ -2057,7 +2055,7 @@ describe("projectRecentChatDisplayMessages", () => {
20572055
});
20582056

20592057
it("keeps channel-final mirrors after forwarded sessions_send messages", () => {
2060-
const result = projectRecentChatDisplayMessages([
2058+
const result = projectChatDisplayMessages([
20612059
sessionsSendHistoryMessage("Forwarded status", 1),
20622060
deliveryMirrorHistoryMessage("Forwarded status", "message-forwarded", 2),
20632061
]);
@@ -2078,7 +2076,7 @@ describe("projectRecentChatDisplayMessages", () => {
20782076
});
20792077

20802078
it("keeps gateway-injected assistant replies when they are not duplicate ACP text", () => {
2081-
const result = projectRecentChatDisplayMessages([
2079+
const result = projectChatDisplayMessages([
20822080
assistantHistoryMessage("First answer.", {
20832081
provider: "openclaw",
20842082
model: "acp-runtime",
@@ -2105,27 +2103,6 @@ describe("projectRecentChatDisplayMessages", () => {
21052103
]);
21062104
});
21072105

2108-
it("applies history limits after dropping display-hidden messages", () => {
2109-
const result = projectRecentChatDisplayMessages(
2110-
[
2111-
{ role: "user", content: "older visible", timestamp: 1 },
2112-
{ role: "assistant", content: "older answer", timestamp: 2 },
2113-
{ role: "assistant", content: "NO_REPLY", timestamp: 3 },
2114-
{ role: "assistant", content: "ANNOUNCE_SKIP", timestamp: 4 },
2115-
{
2116-
role: "custom",
2117-
customType: "openclaw.runtime-context",
2118-
content: "hidden runtime context",
2119-
display: false,
2120-
timestamp: 5,
2121-
},
2122-
],
2123-
{ maxMessages: 1 },
2124-
);
2125-
2126-
expect(result).toEqual([{ role: "assistant", content: "older answer", timestamp: 2 }]);
2127-
});
2128-
21292106
it.each([
21302107
{
21312108
name: "facts-only",
@@ -2159,7 +2136,7 @@ describe("projectRecentChatDisplayMessages", () => {
21592136
expectedPath: undefined,
21602137
},
21612138
])("keeps $name media-only users through canonical display projection", (testCase) => {
2162-
const result = projectRecentChatDisplayMessages([
2139+
const result = projectChatDisplayMessages([
21632140
{ role: "user", content: "", timestamp: 1, ...testCase.message },
21642141
{ role: "user", content: "", timestamp: 2 },
21652142
]);
@@ -2176,7 +2153,7 @@ describe("projectRecentChatDisplayMessages", () => {
21762153
const spokenText = "Here is the answer.";
21772154
const textSha256 = createHash("sha256").update(visibleText).digest("hex");
21782155

2179-
const result = projectRecentChatDisplayMessages([
2156+
const result = projectChatDisplayMessages([
21802157
userHistoryMessage("first", { timestamp: 1 }),
21812158
assistantHistoryMessage(visibleText, { timestamp: 2 }),
21822159
userHistoryMessage("second", { timestamp: 3 }),
@@ -2194,7 +2171,7 @@ describe("projectRecentChatDisplayMessages", () => {
21942171
const projectedVisibleText = "Visible answer ".repeat(8).trim();
21952172
const textSha256 = createHash("sha256").update(projectedVisibleText).digest("hex");
21962173

2197-
const result = projectRecentChatDisplayMessages(
2174+
const result = projectChatDisplayMessages(
21982175
[
21992176
assistantHistoryMessage(projectedVisibleText, { timestamp: 1 }),
22002177
ttsSupplementHistoryMessage({ textSha256 }, 2),
@@ -2216,7 +2193,7 @@ describe("projectRecentChatDisplayMessages", () => {
22162193
const textSha256 = createHash("sha256").update(visibleText).digest("hex");
22172194
const ttsSupplement = { textSha256 };
22182195

2219-
const result = projectRecentChatDisplayMessages([
2196+
const result = projectChatDisplayMessages([
22202197
assistantHistoryMessage(visibleText, { timestamp: 1 }),
22212198
userHistoryMessage("again", { timestamp: 2 }),
22222199
ttsSupplementHistoryMessage(ttsSupplement, 3, visibleText),

0 commit comments

Comments
 (0)