From f9c8a5107ce6830b16481234971aa93e1e806153 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Sun, 26 Apr 2026 09:14:53 +0530 Subject: [PATCH] fix(cli): cap fresh session reseed size --- src/agents/cli-runner/session-history.test.ts | 12 ++++++++++++ src/agents/cli-runner/session-history.ts | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/agents/cli-runner/session-history.test.ts b/src/agents/cli-runner/session-history.test.ts index effe9756b3a..e166946f447 100644 --- a/src/agents/cli-runner/session-history.test.ts +++ b/src/agents/cli-runner/session-history.test.ts @@ -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("\ncurrent ask must survive\n"); + expect(prompt).not.toContain("x".repeat(80)); + }); }); diff --git a/src/agents/cli-runner/session-history.ts b/src/agents/cli-runner/session-history.ts index 0fbd5b102e1..1f5d2a51568 100644 --- a/src/agents/cli-runner/session-history.ts +++ b/src/agents/cli-runner/session-history.ts @@ -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;