Skip to content

fix(doctor): bound recovered session migrations - #126265

Merged
jalehman merged 4 commits into
mainfrom
agent/bound-doctor-session-scan
Aug 21, 2026
Merged

jalehman merged 4 commits into
mainfrom
agent/bound-doctor-session-scan

Conversation

@hannesrudolph

@hannesrudolph hannesrudolph commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

A package-to-dev update can activate the new core and then abort during post-core Doctor when a large recovered SQLite session corpus exhausts Node's heap. PR #126353 bounded session-entry and canonical-repair paths, but the authentic #126115 corpus still failed earlier in migrateLegacyMediaPersistence, which eagerly materialized all transcript and trajectory JSON. The Gateway remained stopped until manual recovery.

Why This Change Was Made

Doctor now scans transcript and trajectory rows in ordered 64-row keyset batches. Current-schema detection runs in a deferred read transaction; required rewrites rescan inside the existing immediate write transaction. PRAGMA data_version, constant-memory fingerprints, exact-row transcript rewrites, event identity, index maintenance, and all-or-nothing schema advancement remain intact.

The rebase dropped the former tracked-path buffer commit because current main replaced that child-process scan with listRepoFilesSync; resurrecting the old path would be obsolete.

No heap override, schema-version bump, config, dependency, protocol, or public CLI contract change.

User Impact

Operators with large retained or recovered agent databases can complete Doctor and update finalization without an 8 GiB heap override. The authentic 1.13 GiB recovered corpus completed Doctor in 25 seconds at 911.6 MiB peak RSS after the original path failed at 2,147.6 MiB.

Evidence

  • Rebases cleanly onto origin/main 81ace374f99.
  • Production runtime: +200/-200 (net zero).
  • Regression: +146, including second-page rewrites for transcript and trajectory rows at sequence 64 and preservation through sequence 127.
  • node scripts/run-vitest.mjs across all eight state-migrations.media-persistence*.test.ts files: 33 tests passed.
  • Low-heap regression: 2,640 sessions / 5,120 transcript events / 128 trajectory events under a 256 MiB old-space cap.
  • node scripts/check-changed.mjs -- src/infra/state-migrations.media-persistence.ts src/infra/state-migrations.media-persistence.large-corpus.test.ts: passed.
  • pnpm build: passed.
  • Fresh branch autoreview against origin/main: clean, no accepted/actionable findings.
  • Existing authentic recovered-state proof remains unchanged: Doctor, config validation, plugins, Discord, recovered/fresh turns, Web UI, and ten-minute soak passed. Private state, transcripts, credentials, and raw config remain unattached.

Follow-up for #126115 and #126353.

@clawsweeper

clawsweeper Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: L maintainer Maintainer-authored PR labels Aug 19, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. 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 Aug 19, 2026
@clawsweeper

clawsweeper Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 21, 2026, 2:25 PM ET / 18:25 UTC.

ClawSweeper review

What this changes

This PR changes Doctor’s legacy-media migration to scan transcript and trajectory records in 64-row keyset batches and adds a low-heap pagination regression.

Regression provenance

Possible regression — probable (reproduction; failure trace). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open for maintainer review. Current main still eagerly materializes the relevant transcript and trajectory rows; this PR replaces that path with bounded scans and preserves the existing transaction and drift checks.

Priority: P1
Reviewed head: 2960f57504c4f05279d9981f8337b65fd838054d

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch is focused, preserves the migration’s existing safeguards, and adds a credible page-boundary regression.
Proof confidence 🌊 off-meta tidepool Not applicable: The contributor-proof gate does not apply to this MEMBER-authored PR; its body nevertheless supplies a concrete after-fix low-heap Doctor result.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The contributor-proof gate does not apply to this MEMBER-authored PR; its body nevertheless supplies a concrete after-fix low-heap Doctor result.
Evidence reviewed 6 items Current-main failure surface: Current main still selects the full transcript row set before grouping and planning rewrites, which is the allocation pattern this PR removes.
Bounded replacement: The PR iterates each session by ordered sequence cursor with a 64-row limit, then rewrites only changed rows.
Migration boundary: Doctor invokes this migration during doctor-only state migrations, so the repair remains at the persistent-state owner boundary.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.large-corpus.test.ts

Result: PASS (completed)

runner@runnervm76f27:/tmp/clawsweeper-live-proof-126265-9ckbcc/target$ node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.large-corpus.tes
t.ts
[test] starting test/vitest/vitest.infra.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-126265-9ckbcc/target

node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.large-corpus.test.ts
 ✓  infra  src/infra/state-migrations.media-persistence.large-corpus.test.ts (1 test) 5856ms
     ✓ rewrites bounded batches under a 256 MiB old-space cap  5853ms

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  18:26:03
   Duration  8.08s (transform 1.76s, setup 1.16s, import 931ms, tests 5.86s, environment 0ms)

[test] passed 1 Vitest shard in 14.13s
runner@runnervm76f27:/tmp/clawsweeper-live-proof-126265-9ckbcc/target$ node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.large-corpus.tes
t.ts

































Assertions:

  • PASS expect_output: 1 passed

How this fits together

Doctor migrates persisted agent SQLite state during update finalization, before the Gateway resumes. This migration reads recovered transcript and trajectory data, rewrites legacy media fields when needed, and records the repaired state.

flowchart LR
  A[Update finalization] --> B[Doctor]
  B --> C[Recovered agent SQLite state]
  C --> D[Ordered 64-row batches]
  D --> E[Media rewrite decision]
  E --> F[Immediate SQLite transaction]
  F --> G[Gateway resumes]
Loading

Before merge

  • Resolve merge risk (P1) - This upgrade-time migration rewrites persisted transcript and trajectory state; merge should remain gated on exact-head validation of drift rejection and all-or-nothing schema advancement.
  • Complete next step (P2) - This protected MEMBER-authored PR needs ordinary maintainer review and exact-head validation, not an automated repair lane.
Agent review details

Security

None.

PR surface

Source 0, Tests +146. Total +146 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 200 200 0
Tests 1 146 0 +146
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 346 200 +146

Review metrics

Metric Value Why it matters
Production versus regression coverage production +200/-200 (net 0); tests +146 The migration is structurally refactored without net production growth, while the dedicated low-heap pagination regression adds coverage.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/infra/state-migrations.media-persistence.large-corpus.test.ts, serialized state: src/infra/state-migrations.media-persistence.large-corpus.test.ts, serialized state: src/infra/state-migrations.media-persistence.ts, unknown-data-model-change: src/infra/state-migrations.media-persistence.large-corpus.test.ts, unknown-data-model-change: src/infra/state-migrations.media-persistence.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #126115
Summary: This PR is a candidate follow-up repair for the large recovered-session Doctor OOM reported in the canonical incident.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Validate the final migration head (recommended)
    Run the focused migration suite and low-heap Doctor regression on the final merge head before landing this persisted-state rewrite.

Technical review

Best possible solution:

Retain one canonical bounded scan-and-rewrite flow, with the existing source-drift and atomic schema-advance guarantees validated on the final merge head.

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

Yes, from source with high confidence: current main materializes the complete stored event corpus before migration; this review did not run the private recovered database.

Is this the best way to solve the issue?

Yes: an ordered, owner-level batch scan directly removes the whole-corpus allocation while retaining the existing migration boundary and exact rewrite helper.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • remove proof: sufficient: Current real behavior proof status is not_applicable, not sufficient.

Label justifications:

  • P1: The reported update-time Doctor failure can leave an operator’s Gateway stopped until manual recovery.
  • merge-risk: 🚨 compatibility: The changed code migrates existing agent SQLite state during upgrades.
  • merge-risk: 🚨 session-state: The migration can rewrite persisted transcript and trajectory events.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The contributor-proof gate does not apply to this MEMBER-authored PR; its body nevertheless supplies a concrete after-fix low-heap Doctor result.

Evidence

What I checked:

Likely related people:

  • jalehman: Authored the current-head pagination-boundary commit and documented the exact-head validation refresh. (role: recent migration contributor; confidence: high; commits: 2960f57504c4; files: src/infra/state-migrations.media-persistence.ts, src/infra/state-migrations.media-persistence.large-corpus.test.ts)
  • steipete: Authored the merged related repair that bounded neighboring large-session Doctor paths, leaving this legacy-media path as the remaining overlap. (role: adjacent repair author; confidence: medium; commits: 61221c4c20ec; files: src/infra/state-migrations.media-persistence.ts)
  • Yiğit ERDOĞAN: Current-main blame reaches this shallow-history boundary for the pre-existing eager migration implementation; deeper local provenance is unavailable in the partial checkout. (role: current-main history trace; confidence: low; commits: ff83e3efe811; files: src/infra/state-migrations.media-persistence.ts)

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 (3 earlier review cycles)
  • reviewed 2026-08-19T08:44:18.188Z sha df6c460 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-19T18:16:10.598Z sha df41e20 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-19T22:44:53.592Z sha 79fc111 :: needs maintainer review before merge. :: none

@hannesrudolph
hannesrudolph marked this pull request as ready for review August 19, 2026 14:59
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Aug 19, 2026
@hannesrudolph
hannesrudolph force-pushed the agent/bound-doctor-session-scan branch from 9552a64 to df41e20 Compare August 19, 2026 17:30
@hannesrudolph

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. proof: sufficient ClawSweeper judged the real behavior proof convincing. labels Aug 19, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the commands Command implementations label Aug 19, 2026
@jalehman jalehman self-assigned this Aug 21, 2026
@jalehman
jalehman force-pushed the agent/bound-doctor-session-scan branch from 79fc111 to 2960f57 Compare August 21, 2026 18:19
@openclaw-barnacle openclaw-barnacle Bot removed the scripts Repository scripts label Aug 21, 2026
@jalehman

Copy link
Copy Markdown
Contributor

Rebased onto current main and addressed the pagination-boundary proof gap: the 256 MiB regression now requires second-page rewrites for both transcript and trajectory rows at sequence 64, while preserving representative rows through sequence 127.

Local proof on head 2960f57504c:

  • all eight state-migrations.media-persistence*.test.ts files: 33 tests passed
  • scoped check-changed: passed
  • pnpm build: passed
  • fresh branch autoreview: clean, no accepted/actionable findings
  • production migration runtime remains net zero (+200/-200)

The obsolete tracked-path buffer commit was dropped during conflict resolution because current main replaced that child process with listRepoFilesSync.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Aug 21, 2026
@jalehman

Copy link
Copy Markdown
Contributor

Land-ready at exact head 2960f57504c4f05279d9981f8337b65fd838054d.

  • Exact-head CI run 32512739932 is complete and green; the full exact-head rollup has 186 successful checks, 48 intentional skips, one neutral result, zero failures, and zero pending checks.
  • Fresh ClawSweeper exact-head review 32512786765 completed successfully with no actionable findings.
  • ClawSweeper independently ran node scripts/run-vitest.mjs src/infra/state-migrations.media-persistence.large-corpus.test.ts: 1 file and 1 test passed, including transcript and trajectory second-page rewrites under the 256 MiB old-space cap.
  • The existing drift-rejection coverage remains green in exact-head CI; no proof gap or Rank-up move remains beyond ordinary maintainer landing.

Proceeding through the repository-native review-artifact, prepare, and merge gates.

@jalehman
jalehman merged commit d06c0b8 into main Aug 21, 2026
235 checks passed
@jalehman
jalehman deleted the agent/bound-doctor-session-scan branch August 21, 2026 18:47
@jalehman

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 22, 2026
* fix(doctor): bound recovered session migrations

* refactor(doctor): trim bounded migration flow

* test(doctor): strengthen migration heap regression

* test(doctor): cover media migration page boundary

---------

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

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. 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: 🐚 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