fix(cli): cap fresh session reseed size

This commit is contained in:
Ayaan Zaidi
2026-04-26 09:14:53 +05:30
parent 8559a84e4e
commit f9c8a5107c
2 changed files with 20 additions and 1 deletions

View File

@@ -329,4 +329,16 @@ describe("buildCliSessionHistoryPrompt", () => {
}),
).toBeUndefined();
});
it("caps rendered reseed history before adding the next user message", () => {
const prompt = buildCliSessionHistoryPrompt({
messages: [{ role: "compactionSummary", summary: "x".repeat(100) }],
prompt: "current ask must survive",
maxHistoryChars: 20,
});
expect(prompt).toContain("[OpenClaw reseed history truncated]");
expect(prompt).toContain("<next_user_message>\ncurrent ask must survive\n</next_user_message>");
expect(prompt).not.toContain("x".repeat(80));
});
});

View File

@@ -14,6 +14,7 @@ import {
export const MAX_CLI_SESSION_HISTORY_FILE_BYTES = 5 * 1024 * 1024;
export const MAX_CLI_SESSION_HISTORY_MESSAGES = MAX_AGENT_HOOK_HISTORY_MESSAGES;
export const MAX_CLI_SESSION_RESEED_HISTORY_CHARS = 12 * 1024;
type HistoryMessage = {
role?: unknown;
@@ -48,8 +49,10 @@ function coerceHistoryText(content: unknown): string {
export function buildCliSessionHistoryPrompt(params: {
messages: unknown[];
prompt: string;
maxHistoryChars?: number;
}): string | undefined {
const renderedHistory = params.messages
const maxHistoryChars = params.maxHistoryChars ?? MAX_CLI_SESSION_RESEED_HISTORY_CHARS;
const renderedHistoryRaw = params.messages
.flatMap((message) => {
if (!message || typeof message !== "object") {
return [];
@@ -74,6 +77,10 @@ export function buildCliSessionHistoryPrompt(params: {
})
.join("\n\n")
.trim();
const renderedHistory =
renderedHistoryRaw.length > maxHistoryChars
? `${renderedHistoryRaw.slice(0, maxHistoryChars).trimEnd()}\n[OpenClaw reseed history truncated]`
: renderedHistoryRaw;
if (!renderedHistory) {
return undefined;