diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d0a7022c28..eac5c3d2463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Agents/context pruning: guard assistant thinking/text char estimation against malformed blocks (missing `thinking`/`text` strings or null entries) so pruning no longer crashes with malformed provider content. (openclaw#35146) thanks @Sid-Qin. - Agents/schema cleaning: detect Venice + Grok model IDs as xAI-proxied targets so unsupported JSON Schema keywords are stripped before requests, preventing Venice/Grok `Invalid arguments` failures. (openclaw#35355) thanks @Sid-Qin. - Skills/native command deduplication: centralize skill command dedupe by canonical `skillName` in `listSkillCommandsForAgents` so duplicate suffixed variants (for example `_2`) are no longer surfaced across interfaces outside Discord. (#27521) thanks @shivama205. - Agents/xAI tool-call argument decoding: decode HTML-entity encoded xAI/Grok tool-call argument values (`&`, `"`, `<`, `>`, numeric entities) before tool execution so commands with shell operators and quotes no longer fail with parse errors. (#35276) Thanks @Sid-Qin. diff --git a/src/agents/pi-extensions/context-pruning/pruner.test.ts b/src/agents/pi-extensions/context-pruning/pruner.test.ts new file mode 100644 index 00000000000..3985bb2feb1 --- /dev/null +++ b/src/agents/pi-extensions/context-pruning/pruner.test.ts @@ -0,0 +1,112 @@ +import type { AgentMessage } from "@mariozechner/pi-agent-core"; +import type { ExtensionContext } from "@mariozechner/pi-coding-agent"; +import { describe, expect, it } from "vitest"; +import { pruneContextMessages } from "./pruner.js"; +import { DEFAULT_CONTEXT_PRUNING_SETTINGS } from "./settings.js"; + +type AssistantMessage = Extract; +type AssistantContentBlock = AssistantMessage["content"][number]; + +const CONTEXT_WINDOW_1M = { + model: { contextWindow: 1_000_000 }, +} as unknown as ExtensionContext; + +function makeUser(text: string): AgentMessage { + return { + role: "user", + content: text, + timestamp: Date.now(), + }; +} + +function makeAssistant(content: AssistantMessage["content"]): AgentMessage { + return { + role: "assistant", + content, + api: "openai-responses", + provider: "openai", + model: "test-model", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + total: 0, + }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; +} + +describe("pruneContextMessages", () => { + it("does not crash on assistant message with malformed thinking block (missing thinking string)", () => { + const messages: AgentMessage[] = [ + makeUser("hello"), + makeAssistant([ + { type: "thinking" } as unknown as AssistantContentBlock, + { type: "text", text: "ok" }, + ]), + ]; + expect(() => + pruneContextMessages({ + messages, + settings: DEFAULT_CONTEXT_PRUNING_SETTINGS, + ctx: CONTEXT_WINDOW_1M, + }), + ).not.toThrow(); + }); + + it("does not crash on assistant message with null content entries", () => { + const messages: AgentMessage[] = [ + makeUser("hello"), + makeAssistant([null as unknown as AssistantContentBlock, { type: "text", text: "world" }]), + ]; + expect(() => + pruneContextMessages({ + messages, + settings: DEFAULT_CONTEXT_PRUNING_SETTINGS, + ctx: CONTEXT_WINDOW_1M, + }), + ).not.toThrow(); + }); + + it("does not crash on assistant message with malformed text block (missing text string)", () => { + const messages: AgentMessage[] = [ + makeUser("hello"), + makeAssistant([ + { type: "text" } as unknown as AssistantContentBlock, + { type: "thinking", thinking: "still fine" }, + ]), + ]; + expect(() => + pruneContextMessages({ + messages, + settings: DEFAULT_CONTEXT_PRUNING_SETTINGS, + ctx: CONTEXT_WINDOW_1M, + }), + ).not.toThrow(); + }); + + it("handles well-formed thinking blocks correctly", () => { + const messages: AgentMessage[] = [ + makeUser("hello"), + makeAssistant([ + { type: "thinking", thinking: "let me think" }, + { type: "text", text: "here is the answer" }, + ]), + ]; + const result = pruneContextMessages({ + messages, + settings: DEFAULT_CONTEXT_PRUNING_SETTINGS, + ctx: CONTEXT_WINDOW_1M, + }); + expect(result).toHaveLength(2); + }); +}); diff --git a/src/agents/pi-extensions/context-pruning/pruner.ts b/src/agents/pi-extensions/context-pruning/pruner.ts index f9e3791b135..c195fa79e09 100644 --- a/src/agents/pi-extensions/context-pruning/pruner.ts +++ b/src/agents/pi-extensions/context-pruning/pruner.ts @@ -121,10 +121,13 @@ function estimateMessageChars(message: AgentMessage): number { if (message.role === "assistant") { let chars = 0; for (const b of message.content) { - if (b.type === "text") { + if (!b || typeof b !== "object") { + continue; + } + if (b.type === "text" && typeof b.text === "string") { chars += b.text.length; } - if (b.type === "thinking") { + if (b.type === "thinking" && typeof b.thinking === "string") { chars += b.thinking.length; } if (b.type === "toolCall") {