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.
This commit is contained in:
Chris Zhang
2026-04-14 21:07:16 +08:00
committed by Josh Lehman
parent 8f84a54511
commit d5fa7b0838

View File

@@ -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<SessionFileEnt
if (message.role !== "user" && message.role !== "assistant") {
continue;
}
const text = extractSessionText(message.content);
const text = extractSessionText(message.content, message.role);
if (!text) {
continue;
}