Skip to content

Commit cd3543f

Browse files
authored
perf: load web chat history in larger, faster batches (#133565)
* perf: load web chat history in larger, faster batches * fix: type-check history token capture groups * test: derive localized release routes from English navigation
1 parent 47f2529 commit cd3543f

28 files changed

Lines changed: 178 additions & 120 deletions

‎docs/web/control-ui.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ select it to open the owning Approvals page.
337337
<AccordionGroup>
338338
<Accordion title="Chat and Talk">
339339
- Chat with the model via Gateway WS (`chat.history`, `chat.send`, `chat.abort`, `chat.inject`). Archived sessions keep the composer disabled and show a banner with an **Unarchive** action before the conversation can continue.
340-
- Chat history refreshes request a bounded recent window with per-message text caps, so large sessions do not force the browser to render a full transcript payload before chat becomes usable.
340+
- Chat history opens with up to 400 recent messages. Session prefetches and history refreshes use the same window. Scrolling back requests up to 1,000 older messages per page and prefetches the next page to reduce loading pauses. Per-message text caps and the Gateway response-size limit still apply, so pages can contain fewer messages.
341341
- A previous run's error banner clears when Chat adopts a new run or history confirms a newer successful run. Retiring the banner does not erase recorded diagnostics. A late error from the same run can remain visible beside its delivered answer; reconnecting or refreshing metadata alone does not establish recovery.
342342
- A saved assistant answer replaces its live stream without waiting for the run to finish. Remote workspace reconciliation can keep the working indicator and Stop control active after the answer appears; a later reconciliation failure remains visible beside the answer.
343343
- Scroll up to read earlier messages without following incoming output. Use the down-arrow button to return to the latest message. Scrolling manually interrupts that movement or a restored scroll position; messages continue to reserve their space as full text, images, and tool output load.

‎src/agents/internal-runtime-context.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ describe("internal runtime context codec", () => {
130130
expect(hasInternalRuntimeContext(preface)).toBe(true);
131131
expect(stripInternalRuntimeContext(preface)).toBe("");
132132
expect(stripInternalRuntimeContext(input)).toBe("Visible reply");
133+
expect(
134+
stripInternalRuntimeContext(
135+
` \t${header}\r\n ${OPENCLAW_RUNTIME_CONTEXT_NOTICE} \r\n\r\nVisible reply`,
136+
),
137+
).toBe("Visible reply");
138+
});
139+
140+
it.each([
141+
[`Visible reply\n${INTERNAL_RUNTIME_CONTEXT_END}`, "Visible reply"],
142+
[`Visible reply\n${INTERNAL_RUNTIME_CONTEXT_BEGIN}\nprivate`, "Visible reply"],
143+
[" \tVisible reply\r\n\r\n", " \tVisible reply\r\n\r\n"],
144+
])("preserves delimiter cleanup and ordinary whitespace in %j", (text, expected) => {
145+
expect(stripInternalRuntimeContext(text)).toBe(expected);
133146
});
134147

135148
it("preserves text when the runtime-context header or notice does not match", () => {

‎src/agents/internal-runtime-context.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ export function stripInternalRuntimeContext(
253253
text: string,
254254
options: { preserveSurroundingWhitespace?: boolean; separator?: string } = {},
255255
): string {
256-
if (!text) {
256+
// All removable formats contain a delimiter or the exact runtime notice.
257+
// Skip regex construction and line parsing for ordinary display text.
258+
if (
259+
!text.includes(INTERNAL_RUNTIME_CONTEXT_BEGIN) &&
260+
!text.includes(INTERNAL_RUNTIME_CONTEXT_END) &&
261+
!text.includes(OPENCLAW_RUNTIME_CONTEXT_NOTICE)
262+
) {
257263
return text;
258264
}
259265
const withoutDelimitedBlocks = stripStandaloneDelimitedTokenLines(

‎src/auto-reply/reply/strip-inbound-meta.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export function stripLeadingInboundMetadata(text: string): string {
359359

360360
/** Extracts the sender label from injected inbound metadata when present. */
361361
export function extractInboundSenderLabel(text: string): string | null {
362-
if (!text || !SENTINEL_FAST_RE.test(text)) {
362+
if (!text.includes(INBOUND_CONTEXT_MARKER)) {
363363
return null;
364364
}
365365

‎src/auto-reply/tokens.test.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,29 @@ describe("isSilentReplyText", () => {
123123
" .NO_REPLY ",
124124
" NO_REPLY. ",
125125
" *NO_REPLY* ",
126+
"«NO_REPLY»",
127+
"\u{10100}NO_REPLY\u{10101}",
128+
"\u{10100}No_RePlY NO_REPLY\u{10101}",
126129
])("returns true for punctuation-wrapped token-only text: %j (#98166)", (text) => {
127130
expect(isSilentReplyText(text)).toBe(true);
128131
});
129132

130-
it.each(["the sentinel is NO_REPLY, fyi", "💬NO_REPLY", "NO_REPLY👍"])(
131-
"keeps substantive punctuation or symbols: %j",
132-
(text) => {
133-
expect(isSilentReplyText(text)).toBe(false);
134-
},
135-
);
133+
it.each([
134+
"the sentinel is NO_REPLY, fyi",
135+
"💬NO_REPLY",
136+
"NO_REPLY👍",
137+
"NO_REPLY\ud800",
138+
"NO_REPLY\udc00",
139+
])("keeps substantive punctuation or symbols: %j", (text) => {
140+
expect(isSilentReplyText(text)).toBe(false);
141+
});
136142

137143
it("preserves exact custom-token matches with punctuation-edged tokens", () => {
138144
// Custom tokens whose first/last character is punctuation must still match
139145
expect(isSilentReplyText("*SILENT*", "*SILENT*")).toBe(true);
140146
expect(isSilentReplyText("#QUIET#", "#QUIET#")).toBe(true);
141147
expect(isSilentReplyText("^^MUTE^^", "^^MUTE^^")).toBe(true);
148+
expect(isSilentReplyText("**SILENT**", "*SILENT*")).toBe(false);
142149
});
143150
});
144151

@@ -237,8 +244,13 @@ describe("stripSilentToken", () => {
237244
expect(stripSilentToken("NO_REPLY ok NO_REPLY")).toBe("NO_REPLY ok");
238245
});
239246

240-
it("strips every adjacent trailing silent token", () => {
241-
expect(stripSilentToken("Done. NO_REPLY NO_REPLY")).toBe("Done.");
247+
it.each([
248+
["Done. NO_REPLY NO_REPLY", "Done."],
249+
["Done.\r\nno_reply\tNO_REPLY\u00a0", "Done."],
250+
["**NO_REPLY NO_REPLY", ""],
251+
["Done.NO_REPLY NO_REPLY", "Done.NO_REPLY"],
252+
])("strips adjacent trailing silent tokens in %j", (text, expected) => {
253+
expect(stripSilentToken(text)).toBe(expected);
242254
});
243255

244256
it("returns empty string when only token remains", () => {
@@ -275,6 +287,16 @@ describe("custom silent tokens", () => {
275287
check: () => stripSilentToken("done HEARTBEAT_OK", "HEARTBEAT_OK"),
276288
expected: "done",
277289
},
290+
{
291+
name: "trailing token with regex punctuation",
292+
check: () => stripSilentToken("done [quiet] [QUIET]\n", "[quiet]"),
293+
expected: "done",
294+
},
295+
{
296+
name: "trailing token containing whitespace",
297+
check: () => stripSilentToken("done KEEP QUIET KEEP QUIET", "KEEP QUIET"),
298+
expected: "done",
299+
},
278300
])("handles custom token for $name", ({ check, expected }) => {
279301
expect(check()).toBe(expected);
280302
});

‎src/auto-reply/tokens.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ function getSilentTrailingRegex(token: string): RegExp {
3939
const escaped = escapeRegExp(token);
4040
// Keep main's whitespace/Markdown boundaries: punctuation-attached tokens
4141
// can be visible text. Consume repeated tokens only after a real delimiter.
42-
const regex = new RegExp(`(?:^|\\s+|\\*+)${escaped}(?:\\s+${escaped})*\\s*$`, "i");
42+
// Start at the end so ordinary replies never scan for an absent suffix.
43+
const regex = new RegExp(`$(?<=((?:^|\\s+|\\*+)${escaped}(?:\\s+${escaped})*\\s*))`, "i");
4344
silentTrailingRegexByToken.set(token, regex);
4445
return regex;
4546
}
4647

4748
function stripEdgePunctuation(text: string): string {
48-
return text.replace(/^\p{P}+|\p{P}+$/gu, "");
49+
const start = text.match(/^\p{P}+/u)?.[0].length ?? 0;
50+
// Anchor at the end before matching backwards, so ordinary replies do not
51+
// get scanned in full while searching for a punctuation suffix.
52+
const tail = text.match(/$(?<=(\p{P}+))/u)?.[1]?.length ?? 0;
53+
return text.slice(start, text.length - tail);
4954
}
5055

5156
/** Returns true only for token-only silent replies. */
@@ -234,7 +239,8 @@ export function isSilentReplyPayloadText(
234239
* If the result is empty, the entire message should be treated as silent.
235240
*/
236241
export function stripSilentToken(text: string, token: string = SILENT_REPLY_TOKEN): string {
237-
return text.replace(getSilentTrailingRegex(token), "").trim();
242+
const tail = getSilentTrailingRegex(token).exec(text)?.[1]?.length ?? 0;
243+
return text.slice(0, text.length - tail).trim();
238244
}
239245

240246
const silentLeadingRegexByToken = new Map<string, RegExp>();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function stripAssistantMediaDirectivesForDisplay(
3838
text: string,
3939
managedMediaUrls: readonly string[],
4040
): string {
41-
if (!/(?:^|\n)\s*MEDIA:/iu.test(text) || managedMediaUrls.length === 0) {
41+
if (managedMediaUrls.length === 0 || !/(?:^|\n)\s*MEDIA:/iu.test(text)) {
4242
return text;
4343
}
4444
const managed = new Set(managedMediaUrls.map((url) => url.trim()).filter(Boolean));
@@ -165,6 +165,9 @@ export function shouldPreserveAssistantControlReplyText(message: Record<string,
165165
if (isProjectedSessionsSendForwardedMessage(message)) {
166166
return true;
167167
}
168+
if (!hasAssistantDisplayableNonTextContent(message)) {
169+
return false;
170+
}
168171
const content = message.text ?? message.content;
169172
const texts =
170173
typeof content === "string"
@@ -180,11 +183,7 @@ export function shouldPreserveAssistantControlReplyText(message: Record<string,
180183
: [];
181184
})
182185
: [];
183-
return (
184-
texts.length > 0 &&
185-
texts.every((text) => isSuppressedControlReplyText(text)) &&
186-
hasAssistantDisplayableNonTextContent(message)
187-
);
186+
return texts.length > 0 && texts.every((text) => isSuppressedControlReplyText(text));
188187
}
189188

190189
export function asRoleContentMessage(message: Record<string, unknown>): RoleContentMessage | null {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,11 @@ export function mirrorMessageToolVisibleReplies(messages: unknown[]): unknown[]
503503
succeeded: false,
504504
});
505505
}
506-
} else if (deliveryMirrorText === undefined && isRenderableAssistantDisplayMessage(record)) {
506+
} else if (
507+
pending.length > 0 &&
508+
deliveryMirrorText === undefined &&
509+
isRenderableAssistantDisplayMessage(record)
510+
) {
507511
clearPending();
508512
}
509513

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ export function sanitizeChatHistoryMessages(
727727
}
728728
const res = sanitizeChatHistoryMessage(message, maxChars);
729729
changed ||= res.changed;
730-
if (shouldDropAssistantHistoryMessage(res.message)) {
730+
if (res.changed && shouldDropAssistantHistoryMessage(res.message)) {
731731
changed = true;
732732
continue;
733733
}

‎test/scripts/docs-sync-publish.test.ts‎

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -173,34 +173,16 @@ describe("docs-sync-publish", () => {
173173
"Help",
174174
]);
175175

176-
const releaseRoutes = [
177-
"releases/index",
178-
"releases/2026.8.1",
179-
"releases/2026.7.1",
180-
"releases/2026.6.11",
181-
"maturity/scorecard",
182-
"maturity/taxonomy",
183-
"reference/RELEASING",
184-
"reference/full-release-validation",
185-
"reference/release-performance-sweep",
186-
"reference/test",
187-
"ci",
188-
"help/scripts",
189-
];
190176
const releaseTab = english!.tabs.find((tab) => tab.tab === "Release & CI");
177+
const releaseRoutes = collectPages(releaseTab);
178+
const releaseNotes = collectPages(releaseTab?.groups?.[0]);
191179
expect(releaseTab?.groups?.map((group) => group.group)).toEqual([
192180
"Release notes",
193181
"Maturity",
194182
"Release process",
195183
"Testing and CI",
196184
]);
197-
expect(releaseTab?.groups?.[0]?.pages).toEqual([
198-
"releases/index",
199-
"releases/2026.8.1",
200-
"releases/2026.7.1",
201-
"releases/2026.6.11",
202-
]);
203-
expect(collectPages(releaseTab)).toEqual(releaseRoutes);
185+
expect(releaseNotes[0]).toBe("releases/index");
204186
expect(new Set(collectPages(releaseTab))).toHaveLength(releaseRoutes.length);
205187

206188
const englishWithoutClawHub = {
@@ -222,12 +204,9 @@ describe("docs-sync-publish", () => {
222204
"发布流程",
223205
"测试与 CI",
224206
]);
225-
expect(simplifiedChineseReleaseTab?.groups?.[0]?.pages).toEqual([
226-
"zh-CN/releases/index",
227-
"zh-CN/releases/2026.8.1",
228-
"zh-CN/releases/2026.7.1",
229-
"zh-CN/releases/2026.6.11",
230-
]);
207+
expect(collectPages(simplifiedChineseReleaseTab?.groups?.[0])).toEqual(
208+
releaseNotes.map((page) => `zh-CN/${page}`),
209+
);
231210
expect(collectPages(simplifiedChineseReleaseTab)).toEqual(
232211
releaseRoutes.map((page) => `zh-CN/${page}`),
233212
);

0 commit comments

Comments
 (0)