Skip to content

fix(acp): keep Unicode replay within its byte budget - #124476

Merged
steipete merged 3 commits into
openclaw:mainfrom
coaiMax:pr/3-event-ledger-byte-length
Sep 6, 2026
Merged

steipete merged 3 commits into
openclaw:mainfrom
coaiMax:pr/3-event-ledger-byte-length

Conversation

@coaiMax

@coaiMax coaiMax commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where ACP replay history containing Unicode could exceed its byte budget and repeated metadata writes could distort stored totals. Older derived estimates also remained wrong after upgrading, and a partial UTF-8 conversion could still mix byte additions with SQL character subtraction.

Why This Change Was Made

Use one per-field UTF-8 accounting owner for runtime writes, Doctor imports, and existing-row repair. Metadata updates apply the difference between old and new byte counts; appends add only the new event. Repair streams canonical event text and atomically rebuilds every derived total without reserializing or deleting history.

Maintainer decision, September 5: accepted the accounting and retention correction, including repair of nonzero estimates and earlier application of existing limits to undercounted Unicode history. Schema, version, configuration, caps, eviction order, and canonical replay bytes remain unchanged. The existing app-version/Doctor repair lifecycle owns backfill; replacing a same-version build does not automatically trigger it. Reverting needs no migration but restores inconsistent accounting and cannot recover already evicted history.

The remaining ClawSweeper retention-risk item is resolved by honoring that accepted correction: later eviction can remove historic tool-call/system-notice events that transcript fallback cannot reconstruct. We retain the existing cap and eviction order rather than silently preserving an undercount.

This expands @coaiMax's original UTF-8 fix across the affected owners while preserving the original commit ancestry and credit.

User Impact

ACP history consistently uses the existing 16 MiB UTF-8 retained-content budget, including row overhead. Corrected Unicode history may truncate sooner on its next ordinary write and use transcript fallback when loaded. Repair itself preserves identifiers, timestamps, sequences, event JSON formatting, and all replay rows.

Evidence

  • New/strengthened runtime, importer and repair regressions fail on the original code. All 39 focused/sibling tests pass, covering Unicode, embedded NULs, lone surrogates, UTF-16 SQLite encoding, repeated metadata, eviction, exact JSON retry identity, repair rollback and current-version no-write reopen.
  • Full changed-file gate and independent P0–P2 review pass on the integrated candidate, based on main 70940793461136fa91464bbc7155dd3bf9f8e683. The final main refresh preserves all eight executable/test blobs and includes the canonical upstream CI fixture corrections; exact-head CI is green.
  • Real ACP stdio and GatewayClient, against a synthetic local WebSocket Gateway, passed two process restarts: exact Unicode replay below cap, transcript fallback over cap, no model requests and unchanged canonical rows. This is bridge boundary proof, not a production Gateway/provider run.
  • Streamed repair preserved 30,000 events containing 212,250,000 JSON bytes in 4.246 s under a 96 MiB V8 old-space limit. Native memory and young-generation space are outside that limit; repair did not prune.
  • Local five-trial medians for a 1,000-event Unicode fixture: append 395.2 → 375.6 ms; 30 replays 184.5 → 179.9 ms; upgrade repair 78.0 → 89.0 ms; no-change Doctor repair 53.4 → 56.7 ms. These shared-host timings show the one-time repair cost, not a general performance guarantee.

@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Aug 16, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 16, 2026
@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 6, 2026, 5:00 AM ET / 09:00 UTC.

ClawSweeper review

What this changes

Correct ACP session-history byte accounting across runtime writes, legacy imports, and upgrade repair, with regression tests and retention documentation.

Merge readiness

⛔ Blocked before merge - 1 item remains

This PR remains necessary: current main and the latest release retain inconsistent character-based accounting. Earlier findings are addressed, supplied boundary proof is sufficient, and no new blocking defect was found. The documented retention tradeoff has already been accepted.

Priority: P2
Reviewed head: f0c08a0e491e652d7dc8a5a5a3516afdb1717ecd

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A coherent owner-level repair with strong persistence coverage and sufficient reported bridge proof; the retention consequence is explicitly accepted.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The captured body reports real ACP stdio and GatewayClient exercising the SQLite replay owner across restarts against a synthetic WebSocket Gateway, with exact below-cap Unicode replay and above-cap transcript fallback; streamed repair preserves canonical rows. This satisfies the bridge-boundary proof requirement.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured body reports real ACP stdio and GatewayClient exercising the SQLite replay owner across restarts against a synthetic WebSocket Gateway, with exact below-cap Unicode replay and above-cap transcript fallback; streamed repair preserves canonical rows. This satisfies the bridge-boundary proof requirement.
Evidence reviewed 9 items Current main still needs the repair: Fetched main still uses JavaScript string lengths for new estimates and SQLite length() when subtracting metadata, preserving both Unicode undercounting and inconsistent subtraction.
Latest release also retains the defect: The v2026.9.2 ledger contains the same character-based estimates and subtraction; no shipped fix was established.
Runtime accounting resolves earlier findings: The inspected ledger uses one per-field UTF-8 estimator, applies the old-to-new metadata delta once, and adds only event bytes after upsert. Existing transaction and eviction owners remain in place.
Findings None None.
Security None None.

How this fits together

The ACP bridge connects editor clients to OpenClaw and stores replayable session updates in shared SQLite state. Retention accounting determines whether a client receives complete recorded history or falls back to the Gateway transcript.

flowchart TD
  A[Editor session updates] --> B[ACP bridge]
  B --> C[UTF-8 byte accounting]
  D[Legacy import and upgrade repair] --> C
  C --> E[SQLite replay history]
  E --> F{Complete retained history?}
  F -->|Yes| G[Exact event replay]
  F -->|No| H[Transcript fallback]
Loading

Before merge

  • Resolve merge risk (P2) - Corrected totals can evict previously undercounted Unicode replay events on the next ordinary write; transcript fallback cannot reconstruct historic tool calls or system notices. This consequence is documented and accepted in the PR body.
Agent review details

Security

None.

PR surface

Source +36, Tests +196, Docs +20. Total +252 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 4 126 90 +36
Tests 4 262 66 +196
Docs 2 21 1 +20
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 409 157 +252

Review metrics

Metric Value Why it matters
Accounting repair scope Production +36 net lines; tests/support +196 net lines Production growth supplies shared accounting and streamed repair across existing owners; most growth validates persistence and upgrade invariants.

Merge-risk options

Maintainer options:

  1. Honor the accepted retention correction (recommended)
    Proceed with the documented earlier-eviction consequence, supported by canonical-row preservation, rollback coverage, and below/above-cap bridge proof.

Technical review

Best possible solution:

Retain the unified accounting and atomic repair under the accepted retention policy, preserving canonical replay bytes and the existing upgrade lifecycle.

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

Yes, from source: current main counts Unicode with string lengths and subtracts SQLite character counts; repeated metadata writes and a Unicode payload crossing the byte cap expose the inconsistency. No local runtime reproduction was executed.

Is this the best way to solve the issue?

Yes. A shared per-field byte estimator and transaction-owned rebuild repair all affected producers without changing schema, configuration, canonical JSON, or eviction order.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against f5d4d6bf53a8.

Labels

Label justifications:

  • P2: This is a bounded correction to ACP replay retention accounting, with no demonstrated urgent core-runtime outage.
  • merge-risk: 🚨 session-state: Applying accurate byte totals can shorten retained replay history for existing Unicode-heavy sessions, an explicitly accepted consequence.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured body reports real ACP stdio and GatewayClient exercising the SQLite replay owner across restarts against a synthetic WebSocket Gateway, with exact below-cap Unicode replay and above-cap transcript fallback; streamed repair preserves canonical rows. This satisfies the bridge-boundary proof requirement.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured body reports real ACP stdio and GatewayClient exercising the SQLite replay owner across restarts against a synthetic WebSocket Gateway, with exact below-cap Unicode replay and above-cap transcript fallback; streamed repair preserves canonical rows. This satisfies the bridge-boundary proof requirement.

Evidence

What I checked:

  • Current main still needs the repair: Fetched main still uses JavaScript string lengths for new estimates and SQLite length() when subtracting metadata, preserving both Unicode undercounting and inconsistent subtraction. (src/acp/event-ledger.ts:107, f5d4d6bf53a8)
  • Latest release also retains the defect: The v2026.9.2 ledger contains the same character-based estimates and subtraction; no shipped fix was established. (src/acp/event-ledger.ts:107, 3928bad9badf)
  • Runtime accounting resolves earlier findings: The inspected ledger uses one per-field UTF-8 estimator, applies the old-to-new metadata delta once, and adds only event bytes after upsert. Existing transaction and eviction owners remain in place. (src/acp/event-ledger.ts:103, f0c08a0e491e)
  • Repair preserves canonical state: The introduced backfill streams stored text, repairs incorrect nonzero estimates as well as zero estimates, and updates only derived byte totals. Its callers run inside existing schema-repair transactions; app-version metadata is committed afterward. (src/state/openclaw-state-db-legacy-backfills.ts:360, f0c08a0e491e)
  • Regression and upgrade coverage: Read the runtime, importer, and repair tests. They cover UTF-8 and UTF-16 databases, repeated metadata writes, Unicode eviction, canonical JSON preservation, nonzero estimates, rollback, and current-version no-write reopening. Tests were not executed in this read-only review. (src/state/openclaw-state-db-acp-replay.test.ts:71, f0c08a0e491e)
  • Captured real behavior proof and accepted tradeoff: The supplied PR body at sourceRevision 8153fbe2cc3e17f7630b01eba8fadd1242fdfde4c16bfcb6456b55634b625f09 records real ACP stdio and GatewayClient against a synthetic WebSocket Gateway across two restarts: exact Unicode replay below cap, transcript fallback above cap, no model requests, and unchanged canonical rows. It also records a 30,000-event streamed repair and the September 5 acceptance of earlier eviction after corrected accounting. This is bridge-boundary proof, not a production provider run.

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (9 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-23T08:15:01.450Z sha 4409f057ba45aed0b4dd741194ce242cc71e9b01 :: needs real behavior proof before merge. :: [P1] Use UTF-8 for the removed session footprint | [P1] Migrate every producer and stored ledger estimate to UTF-8 | [P2] Exercise the SQLite byte-budget path
  • reviewed 2026-08-30T19:58:21.693Z sha 4409f057ba45aed0b4dd741194ce242cc71e9b01 :: needs real behavior proof before merge. :: [P1] Subtract UTF-8 bytes when replacing session fields | [P1] Subtract UTF-8 bytes after appending an event | [P1] Rebuild existing ledger estimates in UTF-8 units | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-02T14:47:43.758Z sha 4409f057ba45aed0b4dd741194ce242cc71e9b01 :: needs real behavior proof before merge. :: [P1] Subtract prior fields in UTF-8 bytes | [P1] Do not reapply the session key with a SQL character count | [P1] Rebuild persisted estimates in the new unit | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-02T15:36:48.027Z sha 210b6a6259576e10af12772e7dad16cd7d912200 :: needs real behavior proof before merge. :: [P1] Subtract old fields in UTF-8 bytes | [P1] Subtract the prior session key in UTF-8 bytes | [P1] Rebuild persisted estimates before changing their unit | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-02T16:14:52.831Z sha 997e913a2f9f517b877d25263f6afe65535d637c :: needs real behavior proof before merge. :: [P1] Subtract replaced session fields in UTF-8 bytes | [P1] Subtract the previous session key in UTF-8 bytes | [P1] Rebuild persisted estimates before changing their unit | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-02T16:33:30.014Z sha 4a068bd71a7a787b1cc3f4fed3cec420f28b4e3e :: needs real behavior proof before merge. :: [P1] Subtract prior session fields in UTF-8 bytes | [P1] Subtract the previous session key in UTF-8 bytes | [P1] Migrate every persisted estimate to the new unit | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-03T14:12:48.640Z sha 079c331 :: needs real behavior proof before merge. :: [P1] Subtract replaced fields in UTF-8 bytes | [P1] Stop reapplying a character-count session key | [P1] Migrate existing persisted estimates to the new unit | [P2] Exercise the SQLite byte-budget boundary
  • reviewed 2026-09-06T04:47:27.928Z sha 0ab96c9 :: blocked before merge. :: none

@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 23, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Sep 2, 2026
@coaiMax
coaiMax force-pushed the pr/3-event-ledger-byte-length branch 3 times, most recently from 997e913 to 4a068bd Compare September 2, 2026 16:25
Apply consistent UTF-8 accounting to runtime deltas, importer retries and versioned repair while preserving canonical event text. Retain existing caps and eviction order; repaired histories may use transcript fallback sooner.

Co-authored-by: lijing <[email protected]>
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: L and removed size: XS labels Sep 6, 2026
@steipete steipete changed the title fix(acp): use TextEncoder for byte-length estimates in event ledger fix(acp): keep Unicode replay within its byte budget Sep 6, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. and removed P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 6, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 6, 2026
@steipete
steipete merged commit c4822b8 into openclaw:main Sep 6, 2026
212 checks passed
@steipete

steipete commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants