Skip to content

Commit 9bace93

Browse files
committed
fix(auto-reply): keep memory flush error truncation surrogate-safe
formatMemoryFlushVisibleError and truncateMemoryFlushErrorMessage use .slice(0, N) to truncate error text. When the truncation boundary falls inside a UTF-16 surrogate pair, the resulting text contains a dangling surrogate that can corrupt session state diagnostics. Replace .slice(0, N) with truncateUtf16Safe(text, N).
1 parent 99d82a2 commit 9bace93

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/auto-reply/reply/agent-runner-memory.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/** Preflight compaction and memory flush helpers for agent runner sessions. */
21
import crypto from "node:crypto";
32
import fs from "node:fs";
43
import path from "node:path";
54
import {
65
normalizeLowercaseStringOrEmpty,
76
normalizeOptionalString,
87
} from "@openclaw/normalization-core/string-coerce";
8+
/** Preflight compaction and memory flush helpers for agent runner sessions. */
9+
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
910
import { resolveBootstrapWarningSignaturesSeen } from "../../agents/bootstrap-budget.js";
1011
import { estimateMessagesTokens } from "../../agents/compaction.js";
1112
import { classifyCompactionReason } from "../../agents/embedded-agent-runner/compact-reasons.js";
@@ -364,7 +365,7 @@ function buildMemoryFlushErrorPayload(err: unknown): ReplyPayload | undefined {
364365
return {
365366
text:
366367
visibleText.length > MAX_VISIBLE_MEMORY_FLUSH_ERROR_CHARS
367-
? `${visibleText.slice(0, MAX_VISIBLE_MEMORY_FLUSH_ERROR_CHARS - 1)}…`
368+
? `${truncateUtf16Safe(visibleText, MAX_VISIBLE_MEMORY_FLUSH_ERROR_CHARS - 1)}…`
368369
: visibleText,
369370
isError: true,
370371
};
@@ -373,7 +374,7 @@ function buildMemoryFlushErrorPayload(err: unknown): ReplyPayload | undefined {
373374
function truncateMemoryFlushErrorMessage(err: unknown): string {
374375
const message = normalizeOptionalString(formatErrorMessage(err)) || String(err);
375376
return message.length > MAX_FLUSH_ERROR_LENGTH
376-
? `${message.slice(0, MAX_FLUSH_ERROR_LENGTH - 1)}…`
377+
? `${truncateUtf16Safe(message, MAX_FLUSH_ERROR_LENGTH - 1)}…`
377378
: message;
378379
}
379380

0 commit comments

Comments
 (0)