Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(context-engine): drop tool-only non-assistant messages instead of emitting empty content (#1512)

yeyiteche5ceb130b090
diff --git a/examples/openclaw-plugin/context-engine.ts b/examples/openclaw-plugin/context-engine.ts
index 8fbfff3a5..f5668a63d 100644
--- a/examples/openclaw-plugin/context-engine.ts
+++ b/examples/openclaw-plugin/context-engine.ts
@@ -339,8 +339,10 @@ export function convertToAgentMessages(msg: { role: string; parts: unknown[] }):
const texts = contentBlocks
.filter((b) => b.type === "text")
.map((b) => b.text as string);
- if (texts.length > 0 || toolUseBlocks.length === 0) {
- result.push({ role: msg.role, content: texts.join("\n") || "" });
+ if (texts.length > 0) {
+ result.push({ role: msg.role, content: texts.join("\n") });
+ } else if (toolUseBlocks.length === 0) {
+ result.push({ role: msg.role, content: "" });
}
if (toolUseBlocks.length > 0) {
result.push({ role: "assistant", content: toolUseBlocks });
diff --git a/examples/openclaw-plugin/tests/ut/context-engine-assemble.test.ts b/examples/openclaw-plugin/tests/ut/context-engine-assemble.test.ts
index d24cbc46d..beee51315 100644
--- a/examples/openclaw-plugin/tests/ut/context-engine-assemble.test.ts
+++ b/examples/openclaw-plugin/tests/ut/context-engine-assemble.test.ts
@@ -509,4 +509,37 @@ describe("context-engine assemble()", () => {
expect(result.estimatedTokens).toBe(roughEstimate(liveMessages));
expect(result.systemPromptAddition).toBeUndefined();
});
+
+ it("drops tool-only user messages instead of emitting empty content (issue #1485)", async () => {
+ const { engine } = makeEngine({
+ latest_archive_overview: "",
+ pre_archive_abstracts: [],
+ messages: [
+ {
+ id: "msg_tool_only_user",
+ role: "user",
+ created_at: "2026-04-17T00:00:00Z",
+ parts: [
+ {
+ type: "tool",
+ tool_id: "tool_abc",
+ tool_name: "bash",
+ tool_input: { command: "ls" },
+ tool_output: "file.txt",
+ tool_status: "completed",
+ },
+ ],
+ },
+ ],
+ estimatedTokens: 50,
+ stats: { ...makeStats(), activeTokens: 50 },
+ });
+
+ const result = await engine.assemble({ sessionId: "session-tool-only", messages: [] });
+
+ const emptyContentMsg = result.messages.find(
+ (m) => typeof m.content === "string" && m.content === "",
+ );
+ expect(emptyContentMsg).toBeUndefined();
+ });
});