Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(openclaw): preserve tool-call thought signatures (#4038)

Mansurshakh Japarov37f3f9efe16f
diff --git a/plugins/openclaw/README.md b/plugins/openclaw/README.md
index 4bbd333d6..0994c72f6 100644
--- a/plugins/openclaw/README.md
+++ b/plugins/openclaw/README.md
@@ -2,6 +2,8 @@
Context compression plugin for [OpenClaw](https://github.com/openclaw/openclaw). Compresses tool outputs, code, logs, and structured data — 70-90% token savings with zero LLM calls.
+Tool-call normalization preserves provider metadata, including thought signatures, when restoring assistant turns after compression.
+
## Install
Recommended one-command setup:
diff --git a/plugins/openclaw/src/convert.ts b/plugins/openclaw/src/convert.ts
index 6717b8a33..4f04aaa2a 100644
--- a/plugins/openclaw/src/convert.ts
+++ b/plugins/openclaw/src/convert.ts
@@ -486,8 +486,11 @@ function normalizeAssistantContent(content: unknown): any[] {
(block.type === "toolCall" || block.type === "tool_use") &&
typeof block.name === "string"
) {
+ // Legacy input becomes arguments; do not retain an alias that can go stale after compression.
+ const { input: _legacyInput, ...metadata } = block;
return [
{
+ ...(block.type === "tool_use" ? metadata : block),
type: "toolCall",
id: typeof block.id === "string" ? block.id : "unknown",
name: block.name,
diff --git a/plugins/openclaw/test/convert.test.ts b/plugins/openclaw/test/convert.test.ts
index 4eb76dc7a..544d5cd9e 100644
--- a/plugins/openclaw/test/convert.test.ts
+++ b/plugins/openclaw/test/convert.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { agentToOpenAI, normalizeAgentMessages, openAIToAgent, type OpenAIMessage } from "../src/convert";
+import { agentToOpenAI, agentToOpenAIIndexed, normalizeAgentMessages, openAIToAgent, restoreAgentMessages, type OpenAIMessage } from "../src/convert";
describe("openAIToAgent", () => {
it("emits toolResult content as blocks so transports can safely filter", () => {
@@ -63,6 +63,108 @@ describe("normalizeAgentMessages", () => {
isError: false,
});
});
+
+ it("preserves provider thought signatures on canonical tool calls", () => {
+ const result = normalizeAgentMessages([
+ {
+ role: "assistant",
+ content: [
+ {
+ type: "toolCall",
+ id: "call_signed",
+ name: "read",
+ arguments: { path: "file.ts" },
+ thoughtSignature: "provider-tool-call-signature",
+ },
+ ],
+ },
+ ]);
+
+ expect(result[0].content[0].thoughtSignature).toBe("provider-tool-call-signature");
+ expect(result[0].content[0].arguments).toEqual({ path: "file.ts" });
+ });
+
+ it("normalizes legacy tool inputs without retaining a duplicate alias", () => {
+ const block = {
+ type: "tool_use",
+ name: "read",
+ input: { path: "file.ts" },
+ thoughtSignature: "legacy-signature",
+ providerMetadata: { opaque: true },
+ };
+
+ const result = normalizeAgentMessages([{ role: "assistant", content: [block] }]);
+
+ expect(result[0].content[0]).toEqual({
+ type: "toolCall",
+ id: "unknown",
+ name: "read",
+ arguments: { path: "file.ts" },
+ thoughtSignature: "legacy-signature",
+ providerMetadata: { opaque: true },
+ });
+ expect(block.input).toEqual({ path: "file.ts" });
+ expect(block.type).toBe("tool_use");
+ });
+
+ it("keeps existing arguments precedence while removing the legacy alias", () => {
+ const result = normalizeAgentMessages([
+ {
+ role: "assistant",
+ content: [{ type: "tool_use", name: "read", arguments: null, input: { path: "old.ts" } }],
+ },
+ ]);
+
+ expect(result[0].content[0].arguments).toBeNull();
+ expect(result[0].content[0]).not.toHaveProperty("input");
+ });
+
+ it("retains opaque extension fields on already canonical tool calls", () => {
+ const block = {
+ type: "toolCall",
+ id: "call_canonical",
+ name: "read",
+ arguments: { path: "file.ts" },
+ input: { extension: "opaque canonical metadata" },
+ thoughtSignature: "canonical-signature",
+ providerMetadata: { opaque: true },
+ };
+
+ const result = normalizeAgentMessages([{ role: "assistant", content: [block] }]);
+
+ expect(result[0].content[0]).toEqual(block);
+ });
+});
+
+describe("restoreAgentMessages", () => {
+ it("restores compressed legacy arguments without stale input and preserves metadata", () => {
+ const block = {
+ type: "tool_use",
+ id: "call_legacy",
+ name: "read",
+ input: { path: "file.ts", lines: ["long", "original", "payload"] },
+ thoughtSignature: "legacy-signature",
+ providerMetadata: { opaque: true },
+ };
+ const original = [{ role: "assistant", content: [block] }];
+ const sent = agentToOpenAIIndexed(original);
+ const returned = structuredClone(sent);
+ returned[0].tool_calls![0].function.arguments = '{"path":"file.ts","lines":["short"]}';
+
+ const result = restoreAgentMessages(original, sent, returned);
+
+ expect(result[0].content[0]).toEqual({
+ type: "toolCall",
+ id: "call_legacy",
+ name: "read",
+ arguments: { path: "file.ts", lines: ["short"] },
+ thoughtSignature: "legacy-signature",
+ providerMetadata: { opaque: true },
+ });
+ expect(block.input.lines).toEqual(["long", "original", "payload"]);
+ expect(agentToOpenAI(result)[0].tool_calls![0].function.arguments)
+ .toBe('{"path":"file.ts","lines":["short"]}');
+ });
});
describe("agentToOpenAI", () => {