fix(ui): strip injected inbound metadata from user messages in history

Fixes #21106
Fixes #21109
Fixes #22116

OpenClaw prepends structured metadata blocks ("Conversation info",
"Sender:", reply-context) to user messages before sending them to the
LLM. These blocks are intentionally AI-context-only and must never reach
the chat history that users see.

Root cause:
`buildInboundUserContextPrefix` in `inbound-meta.ts` prepends the
blocks directly to the stored user message content string, so they are
persisted verbatim and later shown in webchat, TUI, and every other
rendering surface.

Fix:
• `src/auto-reply/reply/strip-inbound-meta.ts` — new utility with a
  6-sentinel fast-path strip (zero-alloc on miss) + 9-test suite.
• `src/tui/tui-session-actions.ts` — wraps `chatLog.addUser(...)` with
  `stripInboundMetadata()` so the TUI never stores the prefix.
• `ui/src/ui/chat/message-normalizer.ts` — strips user-role text content
  items during normalisation so webchat renders clean messages.
This commit is contained in:
Mars
2026-02-20 14:36:19 -05:00
committed by Vincent Koc
parent 2dba150c16
commit cb37d9ee72
4 changed files with 182 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
*/
import type { NormalizedMessage, MessageContentItem } from "../types/chat-types.ts";
import { stripInboundMetadata } from "../../../../src/auto-reply/reply/strip-inbound-meta.js";
/**
* Normalize a raw message object into a consistent structure.
@@ -50,6 +51,16 @@ export function normalizeMessage(message: unknown): NormalizedMessage {
const timestamp = typeof m.timestamp === "number" ? m.timestamp : Date.now();
const id = typeof m.id === "string" ? m.id : undefined;
// Strip AI-injected metadata prefix blocks from user messages before display.
if (role === "user" || role === "User") {
content = content.map((item) => {
if (item.type === "text" && typeof item.text === "string") {
return { ...item, text: stripInboundMetadata(item.text) };
}
return item;
});
}
return { role, content, timestamp, id };
}