Skip to content

fix(agents): use UTF-16 safe truncation for compaction reason details - #104987

Closed
whiteyzy wants to merge 1 commit into
openclaw:mainfrom
whiteyzy:fix/compact-reasons-utf16
Closed

whiteyzy wants to merge 1 commit into
openclaw:mainfrom
whiteyzy:fix/compact-reasons-utf16

Conversation

@whiteyzy

Copy link
Copy Markdown
Contributor

What Problem This Solves

formatUnknownCompactionReasonDetail in src/agents/embedded-agent-runner/compact-reasons.ts truncates sanitized compaction reasons with String.prototype.slice(0, MAX_COMPACTION_REASON_DETAIL_CHARS), which can split surrogate pairs in reason strings containing emoji or other multi-byte characters.

Why This Change Was Made

Replaces sanitized.slice(0, MAX_COMPACTION_REASON_DETAIL_CHARS) with truncateUtf16Safe() from @openclaw/normalization-core/utf16-slice, matching the pattern established by 10+ merged UTF-16 safety PRs across the codebase.

User Impact

  • Compaction failure reason details containing emoji/Unicode are now truncated without corrupting surrogate pairs
  • No change to truncation behavior for ASCII strings
  • Same MAX_COMPACTION_REASON_DETAIL_CHARS = 100 limit preserved

Evidence

$ npx vitest run src/agents/embedded-agent-runner/compact-reasons.test.ts --reporter=verbose

 ✓  unit-fast  compact-reasons.test.ts > resolveCompactionFailureReason > replaces generic compaction cancellation
 ✓  unit-fast  compact-reasons.test.ts > resolveCompactionFailureReason > preserves non-generic compaction failures
 ✓  unit-fast  compact-reasons.test.ts > classifyCompactionReason > classifies "nothing to compact"
 ✓  unit-fast  compact-reasons.test.ts > classifyCompactionReason > classifies "already under target"
 ✓  unit-fast  compact-reasons.test.ts > classifyCompactionReason > classifies deferred background maintenance
 ✓  unit-fast  compact-reasons.test.ts > classifyCompactionReason > classifies safeguard messages as guard-blocked
 ✓  unit-fast  compact-reasons.test.ts > classifyCompactionReason > keeps unclassified provider errors in unknown bucket
 ✓  unit-fast  compact-reasons.test.ts > formatUnknownCompactionReasonDetail > formats unknown reasons as single-token
 ✓  unit-fast  compact-reasons.test.ts > formatUnknownCompactionReasonDetail > strips terminal escapes and log separators
 ✓  unit-fast  compact-reasons.test.ts > formatUnknownCompactionReasonDetail > omits empty unknown reason detail
 ✓  unit-fast  compact-reasons.test.ts > formatUnknownCompactionReasonDetail > limits unknown reason detail length

 Test Files  1 passed (1)
      Tests  11 passed (11)

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jul 12, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. labels Jul 12, 2026
@clawsweeper

clawsweeper Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the report. I gave this a fresh shell check against current main, and I could not reproduce it anymore.

Current main and v2026.6.11 sanitize unknown compaction details into an ASCII-only token before truncation, so the reported surrogate-pair corruption is unreachable and this PR has no observable effect.

Review details

Best possible solution:

Keep the current ASCII-only diagnostic formatter unchanged; if Unicode retention is desired later, change sanitization and truncation together with boundary regression coverage and real runtime proof.

Do we have a high-confidence way to reproduce the issue?

No. Current main and v2026.6.11 replace all non-ASCII input before truncation, and the prior direct boundary probes found no dangling surrogate.

Is this the best way to solve the issue?

No. UTF-16-safe truncation is useful for Unicode-preserving strings, but this formatter deliberately emits an ASCII-only metric token, making the proposed substitution redundant.

Security review:

Security review cleared: The diff only imports and calls an existing pure internal helper and introduces no concrete security or supply-chain concern.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • steipete: Live path history connects the module to the agent-runtime internalization plus terminal-core and normalization-core ownership changes. (role: introduced current module and carried adjacent refactors; confidence: high; commits: bb46b79d3c14, de1dfab03ef0, 00d8d7ead059; files: src/agents/embedded-agent-runner/compact-reasons.ts, src/agents/embedded-agent-runner/compact-reasons.test.ts, packages/normalization-core/src/utf16-slice.ts)
  • ArthurNie: Commit 9d54285 changed preflight compaction and reason classification used by the same failure-diagnostic path. (role: introduced adjacent compaction behavior; confidence: medium; commits: 9d54285b0d4a; files: src/agents/embedded-agent-runner/compact-reasons.ts, src/agents/embedded-agent-runner/compact.ts)
  • joelnishanth: Commit 38a1194 is a recent substantive history touch involving compact-command behavior in the same module path. (role: recent adjacent contributor; confidence: medium; commits: 38a11944f4d4; files: src/agents/embedded-agent-runner/compact-reasons.ts)

Codex review notes: model internal, reasoning high; reviewed against 6f9a607b9039.

@whiteyzy
whiteyzy force-pushed the fix/compact-reasons-utf16 branch from 3884526 to 1cf2767 Compare July 12, 2026 12:42
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 12, 2026
@whiteyzy
whiteyzy force-pushed the fix/compact-reasons-utf16 branch from 1cf2767 to 0b415a3 Compare July 12, 2026 13:09
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 12, 2026
@whiteyzy
whiteyzy force-pushed the fix/compact-reasons-utf16 branch from f0dc847 to 7e7e597 Compare July 12, 2026 13:35
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jul 28, 2026
@steipete

Copy link
Copy Markdown
Contributor

Closing per ClawSweeper review verdict cannot_reproduce (decision packet 2026-07-12T13:45:24.391Z). This item was part of the audited close backlog (openclaw/clawsweeper#920) stranded by an apply-lane defect, drained manually after re-verification. If this is wrong, reopen and ClawSweeper will re-review.

@steipete steipete closed this Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: XS stale Marked as stale due to inactivity status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants