Skip to content

fix: recover large session histories without worker memory failure - #182073

Closed
diregoblin wants to merge 1 commit into
openclaw:mainfrom
diregoblin:fix/restart-recovery-stream-20261024
Closed

diregoblin wants to merge 1 commit into
openclaw:mainfrom
diregoblin:fix/restart-recovery-stream-20261024

Conversation

@diregoblin

Copy link
Copy Markdown
Contributor

Closes #182060

What Problem This Solves

Fixes restart recovery failing with ERR_WORKER_OUT_OF_MEMORY when a session has a large SQLite-backed history.

User Impact

Users can recover the complete history without increasing the worker heap or truncating the conversation. Existing atomicity, lifecycle checks, and successor linking are preserved; no configuration or schema changes are required.

Why This Change Was Made

Recovery currently materializes every decoded event, then filters and spreads additional whole-history arrays. The canonical reader now supports scoped synchronous iteration; recovery passes a lazy replacement-header/body stream to the existing bounded append owner inside the same transaction. Existing array consumers retain their contract and share query, decoding, hot-snapshot admission, and cursor cleanup.

This removes recovery-wide decoded/filter/spread arrays, not the existing array API. Production delta is +68/-47 lines (net +21), with +94 test lines for the memory boundary and partial-write rollback. PostgreSQL buffering and the cost of an individual event are unchanged.

Evidence

  • Red on the exact base: the new compressed-history regression reaches the production recovery command after successful setup, then fails with ERR_WORKER_OUT_OF_MEMORY under the default 512 MiB worker limit (60.162 s wall).
  • Green with the fix: the real-worker recovery suite passes 12/12, including >512 MiB decoded compressed history with 601 successor events and an intact last payload/tombstone link. A separate malformed late row rolls back the partial clone and source metadata, and a repaired retry succeeds.
  • Recovery-suite command: OPENCLAW_VITEST_RUNTIME=node node scripts/run-vitest.mjs src/config/sessions/session-accessor.recovery.test.ts --maxWorkers=1 (equivalent repository test wrapper; 56.996 s wall, about 23 s test time; large-history case 13.647 s). The actual worker heap boundary cannot be established by a parser mock. CI seconds are not yet available.
  • Six directly affected reader/append/fork/rewind/lifecycle/mirroring suites: 77/77 pass (263.981 s wall, including 103.318 s initial worker compilation).
  • Focused TypeScript check, targeted type-aware lint, formatting, and diff checks pass. Simplification pass complete; fresh independent P0/P1 review is scoped-clean.

The fixture is entirely synthetic. This is production-worker proof, not yet proof of recovery through a serving Gateway. Broader checks are left to PR CI.

@clawsweeper

clawsweeper Bot commented Oct 24, 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.

@diregoblin
diregoblin marked this pull request as ready for review October 24, 2026 01:16
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. 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. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 24, 2026
@clawsweeper

clawsweeper Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

Streams transcript events through the existing atomic recovery transaction instead of retaining whole-history arrays.

Example: Recover a tombstoned SQLite session containing 600 compressed body events that decode to more than 512 MiB.

  • Before: Recovery retains the decoded history and can exhaust the worker heap before publishing a successor.
  • After: Recovery incrementally copies a replacement header and all 600 body events using bounded append batches.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused repair and worker regressions are useful, but shipped-entry-point proof is absent and the added assertion breaks a required ratchet.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A user cannot recover a long conversation because decoding its complete history exhausts the SQLite worker heap.
Reason: Complete, atomic recovery is an existing contract; streaming restores it without changing configuration, retention, or recovery policy.

Merge readiness

⛔ Blocked before merge - 3 items remain

This PR addresses a current, source-backed recovery defect with a focused streaming fix; the new assertion also introduces a narrow, repairable CI failure.

Priority: P2
Reviewed head: cefe8f62704c72cb76f34c7cf92c078ef9d13624

Before merge

  • Add real behavior proof - The supplied evidence exercises the real recovery worker through regression tests, including the heap boundary and rollback, but contains no terminal capture, logs, or live output from sessions.recover through a serving Gateway; provide that scenario with IPs, API keys, phone numbers, and non-public endpoints redacted, and include latest-stable upgrade evidence for existing persisted state. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Restore the reader's assertion-safety budget (P2) - The new as TranscriptEvent adds a third undocumented production assertion to a file whose allowance is two, causing the exact-head assertion-safety ratchet to fail. Preserve the original boundary typing with const event: TranscriptEvent = JSON.parse(row.event_json); yield event; rather than increasing the baseline.
  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

  • [P2] Restore the reader's assertion-safety budget — src/config/sessions/session-accessor.sqlite-read.ts:548

Tests

  • Missing end-to-end proof: Supply sanitized serving-Gateway sessions.recover output for oversized persisted history, including complete successor history and retry behavior, and an upgrade run from the latest stable release with existing settings and data intact.
Agent review details

How this fits together

Restart recovery sends a guarded command to the agent database worker, which clones the transcript and atomically records the successor and source link before the host publishes identity changes.

flowchart LR
  A[Recovery request] --> B[Host authority and admission]
  B --> C[Agent database worker]
  C --> D[Scoped transcript iterator]
  D --> E[Bounded transcript append]
  E --> F[Atomic successor and source commit]
  F --> G[Host identity publication]
Loading

Technical review

Best possible solution:

Keep the scoped streaming reader and existing atomic append owner, restoring the original typed JSON parse to satisfy the assertion ratchet.

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

Current main visibly retains the complete decoded transcript, and the contributor reports an exact-base production-worker OOM regression; this read-only review did not execute it.

Is this the best way to solve the issue?

The smallest complete repair removes whole-history retention while preserving query admission, cursor cleanup, bounded appends, and atomic publication through existing owners.

Full review comments:

  • [P2] Restore the reader's assertion-safety budget — src/config/sessions/session-accessor.sqlite-read.ts:548
    The new as TranscriptEvent adds a third undocumented production assertion to a file whose allowance is two, causing the exact-head assertion-safety ratchet to fail. Preserve the original boundary typing with const event: TranscriptEvent = JSON.parse(row.event_json); yield event; rather than increasing the baseline.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.95

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Recovery transaction and worker ownership keeps the original intent (d35e7d4: Move recovery off the Gateway thread while preserving atomic cloning, live admission, settlement, and identity publication.)
  • Transcript reader admission and projection keeps the original intent (d4580e1: Reject budgeted oversized exports before materialization while retaining existing no-budget and bounded-reader behavior.)
  • Complete transcript recovery keeps the original intent (bb5ef98: Create one retry-safe successor, preserve the source transcript, and atomically link the recovered conversation.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm check:assertion-safety
  • git diff --check

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit d35e7d4 adds src/config/sessions/session-accessor.sqlite-recovery.worker.ts:25 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: d35e7d4a2a27; files: src/config/sessions/session-accessor.sqlite-recovery.worker.ts)
  • Josh Lehman: Raw commit bb5ef98 adds src/config/sessions/session-accessor.recovery.test.ts:17 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: bb5ef9872a70; files: src/config/sessions/session-accessor.recovery.test.ts)

PR surface

Source +21, Tests +94. Total +115 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 68 47 +21
Tests 1 94 0 +94
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 162 47 +115

Review metrics

Metric Value Why it matters
Production versus test change Production +68/-47; tests +94/-0 The production growth extracts one shared scoped reader; the tests cover distinct heap-exhaustion and partial-clone rollback failures.

Labels

Label changes:

  • add P2: Large-history recovery is blocked by a demonstrated worker-memory boundary, with limited established blast radius and no demonstrated transcript loss.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: Large-history recovery is blocked by a demonstrated worker-memory boundary, with limited established blast radius and no demonstrated transcript loss.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 23, 2026, 9:35 PM ET / October 24, 2026, 01:35 UTC.

steipete added a commit that referenced this pull request Oct 24, 2026
…story (#182091)

Restart recovery now streams transcript events through the existing bounded append owner instead of holding the fully decoded history plus a filtered copy; the successor and its tombstone link still publish in one synchronous transaction, with no schema change. A 544 MiB transcript that previously hit ERR_WORKER_OUT_OF_MEMORY now recovers; ~100 MiB recovered in 8.5 s at 244 MiB peak worker heap. Fixes #182060. Supersedes #182073. Thanks @diregoblin.

Co-authored-by: Pavel Zun <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

Thank you @diregoblin for the fix and the measured report. We re-implemented this in #182091, now on main, with you credited as co-author. Restart recovery streams transcript events through the bounded append owner instead of holding the whole decoded history. Closing as superseded.

@steipete steipete closed this Oct 24, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 24, 2026
…story (openclaw#182091)

Restart recovery now streams transcript events through the existing bounded append owner instead of holding the fully decoded history plus a filtered copy; the successor and its tombstone link still publish in one synchronous transaction, with no schema change. A 544 MiB transcript that previously hit ERR_WORKER_OUT_OF_MEMORY now recovers; ~100 MiB recovered in 8.5 s at 244 MiB peak worker heap. Fixes openclaw#182060. Supersedes openclaw#182073. Thanks @diregoblin.

Co-authored-by: Pavel Zun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M 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.

Restart recovery exhausts SQLite worker heap on large compressed transcripts

2 participants