Skip to content

[BACKPORT] fix(sessions): avoid OOM while archiving thousands of session generations - #128980

Merged
Kimiyu-186 merged 4 commits into
sjf_openclaw_2026-07-31from
codex/backport-bounded-session-archive-plans
Aug 25, 2026
Merged

Kimiyu-186 merged 4 commits into
sjf_openclaw_2026-07-31from
codex/backport-bounded-session-archive-plans

Conversation

@Kimiyu-186

@Kimiyu-186 Kimiyu-186 commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

On the pinned release branch, one logical session can accumulate thousands of historical SQLite transcript generations. When normal session maintenance exceeds maxEntries, it eagerly reads every generation into archive deletion plans containing the complete serialized transcript and retains the entire batch on the gateway heap. The resulting repeated transcript reads and retained strings can exhaust the Node.js V8 heap even when no individual session is unusually large.

Why This Change Was Made

Backport the existing upstream fix from #112424 (9bfe292c8686c9f55282fb5773b8729f96b68051) and its direct writer-lane follow-up #119460 (141a16f7b39b0ee1ddda13bc898de02214cb7cf1) to sjf_openclaw_2026-07-31 instead of creating a divergent release-only implementation. Archive plans now retain lightweight SQLite state snapshots; an existing upstream worker materializes one transcript archive at a time, and snapshot validation protects concurrent writes before state is reclaimed. The writer lane is released during expensive archive work.

The release branch also lacks the narrowly related background-title admission fix from #123608 (530b33e4e37264c89ecd5abdd06279dd23d5c867). Backport only its chat-send-background.ts lifecycle fence: dashboard title generation now holds the same session work admission as deletion, preventing a concurrent title metadata update from invalidating the archive snapshot. The unrelated 37 files from #123608 are intentionally excluded.

The broader CI matrix also exposed a pre-existing release-branch-only responsive-layout test race: checks-ui-e2e (1/4) reported the identical CTA x=31 versus stale nudge x=302 already fixed upstream in #116613. Include its exact one-file, test-only cherry-pick (871abad910e76e76cf9f5922ce091ede076bbd3b), preserving the original author and all layout assertions; it makes no production changes.

User Impact

Gateways can maintain sessions with many historical transcript generations without retaining all transcript contents on the main V8 heap. Existing transcripts remain complete and durable, concurrent session changes remain protected, and unrelated gateway requests stay responsive while archives are built.

Evidence

  • Added a regression that creates 24 historical generations for one logical cron session and asserts that every deletion plan omits transcript content and remains below 1 KiB.
  • Added a disk-budget eviction regression proving a concurrent same-store session write completes while transcript materialization is blocked.
  • The complete built-gateway SQLite sessions/transcripts flip-proof E2E, including concurrent chat history, sending, reset, and deletion, passed twice consecutively after the background-title lifecycle fix.
  • 117 focused tests passed across dashboard-title generation, session deletion, cleanup races, and memory flush. The memory-flush stat fixture is isolated from legitimate asynchronous disk-budget scans without weakening its assertion.
  • The exact-head CI SQLite flip proof and previously failing memory-flush Node shard both passed before the test-only fix(ci): settle custodian mobile layout assertions #116613 cherry-pick; UI test typechecking, focused lint, and formatting passed for that cherry-pick.
  • 145 focused tests passed across archive-worker behavior, cleanup races, incognito transcripts, CI change detection, package scripts, release checks, and production bundling configuration.
  • node scripts/run-tsgo.mjs -p tsconfig.core.json --incremental --tsBuildInfoFile .artifacts/tsgo-cache/core.tsbuildinfo
  • Focused repository-native oxlint and oxfmt --check passed.
  • Production unified build passed and emitted dist/config/sessions/session-accessor.sqlite-archive.worker.js.
  • Built-gateway end-to-end test passed: a running gateway stays RPC-responsive while archiving a large SQLite transcript.

Upstream: #112424, #119460, the targeted lifecycle fix from #123608, and the test-only CI fix from #116613

…s are built (#112424)

* fix(sessions): keep gateway responsive during transcript archiving

* test(sessions): strengthen archive worker coverage

* fix(sessions): bound archive worker concurrency

* test(sessions): normalize rollback paths on Windows

* fix(sessions): load source archive worker on node 22

* fix(sessions): align archive fencing with session windows

* test(sessions): adapt cleanup race to archive worker

* fix(sessions): harden archive worker lifecycle

* style(sessions): format archive lifecycle tests

* fix(sessions): release writer lane during archive work

---------

Co-authored-by: Josh Lehman <[email protected]>
(cherry picked from commit 9bfe292)
@clawsweeper

clawsweeper Bot commented Aug 25, 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 scripts Repository scripts size: XL maintainer Maintainer-authored PR labels Aug 25, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 25, 2026
@clawsweeper

clawsweeper Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 25, 2026, 2:32 AM ET / 06:32 UTC.

ClawSweeper review

What this changes

This PR backports worker-based SQLite transcript archiving and writer-lane release behavior to the pinned release branch, with a related session-title fence and a UI-test stability cherry-pick.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

No blocking patch defect found. This is a release-branch backport of merged session-archive fixes; its persisted-state and Gateway-availability impact require maintainer release inclusion review after exact-head archive and packaging checks finish.

Priority: P1
Reviewed head: 7176fe4e75d084054e9bffb0130306a4864317d0
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch is a focused upstream backport with strong targeted regression coverage and no identified correctness finding; release-gate completion remains necessary before merge.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable: this member-authored PR is exempt from the external-contributor proof gate; its body nevertheless describes built-Gateway after-fix coverage.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable: this member-authored PR is exempt from the external-contributor proof gate; its body nevertheless describes built-Gateway after-fix coverage.
Evidence reviewed 7 items Archive worker boundary: Plans retain identifiers and a state snapshot; transcript content is read only by the queued Worker, keeping whole transcript buffers out of the Gateway heap.
Snapshot fence: The Worker starts one SQLite read snapshot, rejects changed state before archive materialization, and the deletion owner rereads the same state before removal.
Final deletion validation: Reclamation skips referenced sessions and rejects any plan whose current snapshot differs from the planned snapshot.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-archive.worker.test.ts

Result: FAIL (partial) — step 2 expect_output SQLite transcript archive worker: expected terminal output was not visible within 30 seconds: "SQLite transcript archive worker"

node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-archive.worker.test.ts
runner@runnervm76f27:/tmp/clawsweeper-live-proof-128980-acElev/target$ node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-archive.worker.te
st.ts
node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-archive.worker.test.ts
[test] starting test/vitest/vitest.runtime-config.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-128980-acElev/target

 ✓  runtime-config  src/config/sessions/session-accessor.sqlite-archive.worker.test.ts (18 tests) 11216ms
     ✓ keeps archive plans small across many generations of one logical session  328ms
     ✓ keeps the event loop responsive while a transcript archive is built  2846ms
     ✓ publishes a durable archive before lifecycle deletion  687ms
     ✓ archives a logical agent transcript through the exact database's physical owner  580ms
     ✓ rejects transcript changes between deletion planning and the worker snapshot  572ms
     ✓ rejects the first append after planning an empty transcript  549ms
     ✓ recovers the lifecycle archive queue after a worker file failure  1076ms
     ✓ preserves all lifecycle state when the archive worker rejects publication  561ms
     ✓ keeps rows when a transcript changes after its archive snapshot  577ms
     ✓ keeps rows when the rewrite generation changes after archive materialization  556ms
     ✓ keeps rows when the transcript mutation watermark changes after archive materialization  551ms
     ✓ keeps rows when the window metadata changes after archive materialization  551ms
     ✓ keeps rows when trajectory state changes after archive materialization  555ms
     ✓ keeps rows when ACP parent-stream state changes after archive materialization  559ms
     ✓ reuses a matching archive before deleting entry rows  551ms

 Test Files  1 passed (1)
      Tests  18 passed (18)
   Start at  06:33:06
   Duration  16.76s (transform 4.15s, setup 77ms, import 5.27s, tests 11.22s, environment 0ms)

[test] passed 1 Vitest shard in 21.32s
runner@runnervm76f27:/tmp/clawsweeper-live-proof-128980-acElev/target$ node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-archive.worker.te
st.ts
[test] starting test/vitest/vitest.runtime-config.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-128980-acElev/target















Assertions:

  • FAIL expect_output: SQLite transcript archive worker

How this fits together

SQLite session maintenance plans archive-and-delete work for old transcript generations, then revalidates the database before reclaiming rows. The Gateway invokes these lifecycle paths while serving RPCs, and the new worker moves expensive archive creation off its event loop.

flowchart LR
A[Session maintenance] --> B[Deletion plan and state snapshot]
B --> C[Archive worker]
C --> D[Final snapshot validation]
D --> E[SQLite state reclamation]
C --> F[Durable transcript archive]
G[Gateway requests] --> A
Loading

Decision needed

Question Recommendation
Should this scoped session-archive and title-admission backport be included in the pinned release once its exact-head safety gates pass? Include after exact-head proof: Merge after the archive flip-proof and package checks pass on the reviewed head.

Why: The patch changes persisted transcript reclamation and Gateway lifecycle ordering on a release branch, so release scope acceptance is a maintainer decision.

Before merge

  • Resolve merge risk (P1) - This changes transcript archive/delete ordering and a dynamically loaded production Worker; merge should wait for the exact-head flip proof, package-artifact check, and remaining release-branch checks.
  • Complete next step (P2) - Maintainer release-scope approval and exact-head persisted-state proof are required before this protected release-branch PR can merge.
Agent review details

Security

None.

PR surface

Source +489, Tests +1574, Config 0, Other +5. Total +2068 across 29 files.

View PR surface stats
Area Files Added Removed Net
Source 12 757 268 +489
Tests 12 1608 34 +1574
Docs 0 0 0 0
Config 1 1 1 0
Generated 0 0 0 0
Other 4 7 2 +5
Total 29 2373 305 +2068

Review metrics

Metric Value Why it matters
Dynamic worker delivery 1 Worker entry registered in 4 delivery checks The runtime URL-loaded worker must be built, retained in package validation, excluded from dead-code detection, and exercised on Windows.

Stored data model

Persistent data-model change detected: persistent cache schema: scripts/ci-changed-scope.mjs, serialized state: src/config/sessions/incognito-session-transcript.test.ts, serialized state: src/config/sessions/session-accessor.sqlite-archive.ts, serialized state: src/config/sessions/session-accessor.sqlite-archive.worker.test.ts, serialized state: src/config/sessions/session-accessor.sqlite-archive.worker.ts, serialized state: src/config/sessions/session-accessor.sqlite-cleanup-race.test.ts, and 20 more. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: partial_overlap
Canonical: #112424
Summary: This is a release-branch backport of the upstream archive-worker repair, supplemented by its writer-lane and title-admission follow-ups.

Members:

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

Merge-risk options

Maintainer options:

  1. Finish release safety proof (recommended)
    Require successful exact-head archive flip, package-artifact, and relevant cross-platform checks before merging this persisted-state backport.
  2. Defer from this release
    Do not merge if release scope cannot absorb the archive/delete ordering change with its required validation window.

Technical review

Best possible solution:

Land the bounded worker backport only once exact-head validation proves archive-before-delete, same-store writer progress, and packaged Worker availability on the release branch.

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

Yes, from source: the release base retains full transcript content in deletion plans, while this branch replaces that path with snapshot-only plans and a Worker. I did not execute the heavy archive scenario in this read-only review.

Is this the best way to solve the issue?

Yes: the branch ports the already-merged upstream worker, writer-lane, and title-admission design rather than creating a release-only alternative; the remaining question is release inclusion and exact-head validation.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: The change addresses Gateway responsiveness and potential heap exhaustion during historical transcript archival.
  • merge-risk: 🚨 session-state: The backport changes when persisted transcript state is archived, revalidated, and deleted.
  • merge-risk: 🚨 availability: It changes Gateway event-loop and per-store writer-lane behavior during large archive work.
  • 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: Not applicable: this member-authored PR is exempt from the external-contributor proof gate; its body nevertheless describes built-Gateway after-fix coverage.

Evidence

What I checked:

Likely related people:

  • HermanZeng: Authored the merged upstream archive-worker implementation that this PR cherry-picks. (role: upstream implementation author; confidence: high; commits: 9bfe292c8686; files: src/config/sessions/session-accessor.sqlite-archive.ts, src/config/sessions/session-accessor.sqlite-archive.worker.ts)
  • fuller-stack-dev: Authored the merged upstream follow-up that releases the per-store writer lane during archive work. (role: writer-lane follow-up author; confidence: high; commits: 141a16f7b39b; files: src/config/sessions/session-accessor.sqlite-maintenance.ts, src/config/sessions/session-history-eviction.ts)
  • steipete: Authored the merged Gateway lifecycle change from which this PR ports the dashboard-title admission fence. (role: adjacent Gateway lifecycle contributor; confidence: high; commits: 530b33e4e372; files: src/gateway/server-methods/chat-send-background.ts)
  • vincentkoc: Authored the merged UI E2E assertion stabilization cherry-picked for the release branch. (role: UI test fix author; confidence: high; commits: 871abad910e7; files: ui/src/e2e/custodian-channel-onboarding.e2e.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Complete the exact-head archive flip-proof and package-artifact checks on the release branch.

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-25T03:07:53.608Z sha a83478d :: needs maintainer review before merge. :: none
  • reviewed 2026-08-25T03:51:02.007Z sha a83478d :: found issues before merge. :: [P1] Release the SQLite writer lane before archive materialization
  • reviewed 2026-08-25T04:44:42.205Z sha 8425baa :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed 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 25, 2026
@Kimiyu-186
Kimiyu-186 marked this pull request as ready for review August 25, 2026 04:32
@Kimiyu-186
Kimiyu-186 requested a review from a team as a code owner August 25, 2026 04:32
@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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 25, 2026
Backport the session-title admission fix from upstream commit 530b33e (#123608) without its unrelated gateway changes. Keep asynchronous disk-budget scans out of the transcript stat regression fixture.
@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Aug 25, 2026
@openclaw-barnacle openclaw-barnacle Bot added the app: web-ui App: web-ui label Aug 25, 2026
@Kimiyu-186

Copy link
Copy Markdown
Contributor Author

/merge

@Kimiyu-186
Kimiyu-186 merged commit 3a8258c into sjf_openclaw_2026-07-31 Aug 25, 2026
202 of 207 checks passed
@Kimiyu-186
Kimiyu-186 deleted the codex/backport-bounded-session-archive-plans branch August 25, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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. scripts Repository scripts size: XL 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.

4 participants