From d5fa7b083826298c6a627c6244064a2742004b63 Mon Sep 17 00:00:00 2001 From: Chris Zhang Date: Tue, 14 Apr 2026 21:07:16 +0800 Subject: [PATCH] memory: apply inbound envelope strip to parallel packages/memory-host-sdk copy Greptile flagged the `packages/memory-host-sdk/src/host/session-files.ts` copy as a P2 gap: the parallel standalone implementation was not updated with the same fix, so if it ever gets wired up the corpus-truncation bug returns silently. The file isn't currently on any production code path, but keeping the two copies consistent prevents a future regression. --- .../memory-host-sdk/src/host/session-files.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/memory-host-sdk/src/host/session-files.ts b/packages/memory-host-sdk/src/host/session-files.ts index fc25a35a4d7..8d19279180f 100644 --- a/packages/memory-host-sdk/src/host/session-files.ts +++ b/packages/memory-host-sdk/src/host/session-files.ts @@ -1,5 +1,6 @@ import fs from "node:fs/promises"; import path from "node:path"; +import { stripInboundMetadata } from "../../../../src/auto-reply/reply/strip-inbound-meta.js"; import { isUsageCountedSessionTranscriptFileName } from "../../../../src/config/sessions/artifacts.js"; import { resolveSessionTranscriptsDirForAgent } from "../../../../src/config/sessions/paths.js"; import { redactSensitiveText } from "../../../../src/logging/redact.js"; @@ -68,9 +69,26 @@ function normalizeSessionText(value: string): string { .trim(); } -export function extractSessionText(content: unknown): string | null { +/** + * Strip OpenClaw-injected inbound metadata envelopes from a raw text block + * on user-role messages before normalization. See the authoritative + * implementation in `src/memory-host-sdk/host/session-files.ts` for the + * full rationale; duplicated here to keep this parallel copy bug-free. + */ +function stripInboundMetadataForUserRole(text: string, role: "user" | "assistant"): string { + if (role !== "user") { + return text; + } + return stripInboundMetadata(text); +} + +export function extractSessionText( + content: unknown, + role: "user" | "assistant" = "assistant", +): string | null { if (typeof content === "string") { - const normalized = normalizeSessionText(content); + const stripped = stripInboundMetadataForUserRole(content, role); + const normalized = normalizeSessionText(stripped); return normalized ? normalized : null; } if (!Array.isArray(content)) { @@ -85,7 +103,8 @@ export function extractSessionText(content: unknown): string | null { if (record.type !== "text" || typeof record.text !== "string") { continue; } - const normalized = normalizeSessionText(record.text); + const stripped = stripInboundMetadataForUserRole(record.text, role); + const normalized = normalizeSessionText(stripped); if (normalized) { parts.push(normalized); } @@ -134,7 +153,7 @@ export async function buildSessionEntry(absPath: string): Promise