From 45fff13b1d609b582e2fcbd831dd2b243b9e5336 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 21 Feb 2026 01:13:02 -0500 Subject: [PATCH] TUI: strip only leading inbound metadata (#22461) --- src/auto-reply/reply/strip-inbound-meta.ts | 46 ++++++++++++++++++++++ src/tui/tui-formatters.ts | 4 +- src/tui/tui-session-actions.ts | 3 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/auto-reply/reply/strip-inbound-meta.ts b/src/auto-reply/reply/strip-inbound-meta.ts index 775de7564..29cf42c48 100644 --- a/src/auto-reply/reply/strip-inbound-meta.ts +++ b/src/auto-reply/reply/strip-inbound-meta.ts @@ -87,3 +87,49 @@ export function stripInboundMetadata(text: string): string { return result.join("\n").replace(/^\n+/, ""); } + +export function stripLeadingInboundMetadata(text: string): string { + if (!text || !SENTINEL_FAST_RE.test(text)) { + return text; + } + + const lines = text.split("\n"); + let index = 0; + + while (index < lines.length && lines[index] === "") { + index++; + } + if (index >= lines.length) { + return ""; + } + + if (!INBOUND_META_SENTINELS.some((s) => lines[index].startsWith(s))) { + return text; + } + + while (index < lines.length) { + const line = lines[index]; + if (!INBOUND_META_SENTINELS.some((s) => line.startsWith(s))) { + break; + } + + index++; + if (index < lines.length && lines[index].trim() === "```json") { + index++; + while (index < lines.length && lines[index].trim() !== "```") { + index++; + } + if (index < lines.length && lines[index].trim() === "```") { + index++; + } + } else { + return text; + } + + while (index < lines.length && lines[index].trim() === "") { + index++; + } + } + + return lines.slice(index).join("\n"); +} diff --git a/src/tui/tui-formatters.ts b/src/tui/tui-formatters.ts index 9d2ea8284..ae52e3b37 100644 --- a/src/tui/tui-formatters.ts +++ b/src/tui/tui-formatters.ts @@ -1,5 +1,5 @@ import { formatRawAssistantErrorForUi } from "../agents/pi-embedded-helpers.js"; -import { stripInboundMetadata } from "../auto-reply/reply/strip-inbound-meta.js"; +import { stripLeadingInboundMetadata } from "../auto-reply/reply/strip-inbound-meta.js"; import { stripAnsi } from "../terminal/ansi.js"; import { formatTokenCount } from "../utils/usage-format.js"; @@ -275,7 +275,7 @@ export function extractTextFromMessage( const text = extractTextBlocks(record.content, opts); if (text) { if (record.role === "user") { - return stripInboundMetadata(text); + return stripLeadingInboundMetadata(text); } return text; } diff --git a/src/tui/tui-session-actions.ts b/src/tui/tui-session-actions.ts index 894b59d6f..82c6a795d 100644 --- a/src/tui/tui-session-actions.ts +++ b/src/tui/tui-session-actions.ts @@ -1,5 +1,4 @@ import type { TUI } from "@mariozechner/pi-tui"; -import { stripInboundMetadata } from "../auto-reply/reply/strip-inbound-meta.js"; import type { SessionsPatchResult } from "../gateway/protocol/index.js"; import { normalizeAgentId, @@ -327,7 +326,7 @@ export function createSessionActions(context: SessionActionContext) { if (message.role === "user") { const text = extractTextFromMessage(message); if (text) { - chatLog.addUser(stripInboundMetadata(text)); + chatLog.addUser(text); } continue; }