Skip to content

perf(sessions): skip cold envelopes for transcripts over the archive limit - #183378

Merged
steipete merged 2 commits into
mainfrom
steipete/cold-storage-oversized-precheck
Oct 25, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/cold-storage-oversized-precheck

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Cold storage only learned that a transcript is over the 64 MiB uncompressed archive limit after building its whole envelope inside a read snapshot. The rejection then ended the batch, so later eligible windows waited for the next pass. The oversized verdict lives in a process-local 24-hour cooldown that resets on every restart.

Read-only measurements on Team (2026-10-24/25, 45 GB roboclaw agent database):

  • 50 windows are over the limit, holding 6.15 GiB of stored payload.
  • cold transcript snapshot holds for those reads: median 3.2 s, max 106 s.
  • The warning fired about 7 times an hour without naming the session.

User Impact

User impact: on large installs, cold-storage maintenance no longer spends seconds to minutes re-reading transcripts it cannot archive, and other eligible sessions keep moving to cold storage in the same pass. Operators can see which session is too large in the warning.

Why This Change Was Made

  • Worker selection sums each candidate's original event bytes with the existing coalesce(event_utf8_bytes, octet_length(event_json)) helper. That is an indexed range on sqlite_autoindex_transcript_events_1, and no index is needed.
  • A window whose sum exceeds MAX_COLD_ARCHIVE_BYTES is reported as oversized before any envelope work.
  • The envelope contains every original event body and is limit-checked uncompressed (session-cold-storage-worker.ts:144-148, :176-188), so the sum is a valid lower bound.
  • Later candidates in the same pass still prepare and archive.
  • The warning now includes sessionId and lowerBoundBytes.
  • The cooldown stays. It still saves the byte sum and the warning, and it still covers envelopes that cross the limit only through restore metadata.

Production diff: 4 files, about +57/−8.

Evidence

Owner-boundary assertion (real temp agent DB, limit lowered to 4 KiB in the test) origin/main this PR
Envelope snapshots prepared for the oversized window 1 0
Following eligible window archived in the same pass 0 1
UTF-16 store: reported lower bound ≤ real UTF-8 bytes (red on afe2074: 168,838 > 156,419) n/a yes
  • pnpm test src/config/sessions/session-cold-storage-worker.test.ts --maxWorkers=1: both cases red on the base, green here.
  • pnpm test src/config/sessions/session-cold-storage*.test.ts --maxWorkers=1: 12 files, 131 tests passed.
  • pnpm tsgo:core, the changed-file test typecheck shard, changed-file oxlint, oxfmt --check, and git diff --check: all passed.
  • Autoreview (Codex, P1): scoped-clean on both commits. ClawSweeper's UTF-16 finding is fixed in dff3331.

Gap: no full Gateway end-to-end run. The 64 MiB limit is a constant, so reproducing it end to end needs a >64 MiB transcript. The integration tests exercise the real worker selection, preparation and mutation against a real agent database. Expect Team's next deploy to show oversized warnings that carry a sessionId, and no long cold transcript snapshot holds for those windows.

Not changed

Schema, indexes, config, archive format, the 64 MiB limit, eligibility and protection rules, ordering, and retention.

Release note context: cold storage no longer re-reads transcripts that are too large to archive, and keeps archiving other eligible sessions in the same pass.

…limit

Cold storage learned that a transcript exceeds the 64 MiB uncompressed
archive limit only after building its whole envelope inside a read
snapshot, then ended the batch. On Team, 50 windows (6.15 GiB) are over
the limit; their snapshot reads took a median 3.2 s and up to 106 s, and
the in-memory cooldown resets on every deploy, so each restart re-read
them.

Selection now sums the original event bytes through the session index
and classifies those windows as oversized before any envelope work; later
candidates in the same pass still archive. The warning names the session
and the lower-bound bytes.
@clawsweeper

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T11:07:13.111533Z dff3331 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: S labels Oct 25, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug 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. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR skips archive-envelope preparation when transcript event bytes already exceed 64 MiB and reports the affected session.

Example: An eligible 80 MiB transcript precedes a small eligible transcript.

  • Before: Preparing the oversized envelope exhausts the batch, delaying the small transcript.
  • After: The worker skips the oversized transcript before envelope preparation and can archive the small transcript in the same pass.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) Useful, focused optimization with meaningful SQLite regressions, but a verified PostgreSQL eligibility regression blocks merge.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; its supplied evidence exercises real SQLite owners in-process, while the inspected historical UI images do not demonstrate the new pre-check.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Large installations repeatedly prepare transcripts that cannot be archived, delaying other eligible histories.
Reason: Measured production waste supports the maintainer-authored direction, and the change reuses existing selection, cooldown, and mutation owners without adding configuration.

Merge readiness

⛔ Needs changes before merge - 1 item remains

This PR addresses measured cold-storage waste, but its new byte-count addition incorrectly rejects archivable PostgreSQL transcripts; the previous UTF-16 finding is fixed.

Priority: P2
Reviewed head: dff3331b0c87012905375c30826d13a9b14715aa

Before merge

  • Convert PostgreSQL aggregate values before adding byte totals (P2) - For PostgreSQL, event_utf8_bytes is bigint, so SUM returns numeric text through the current bridge; the Kysely number annotation performs no runtime conversion. With 8,000,000 known bytes and no unknown rows, this expression evaluates "8000000" + 0 to "80000000", exceeds 67,108,864, and incorrectly excludes an archivable transcript for the 24-hour cooldown. Convert the aggregate values to numbers before addition, or use a bridge-supported numeric result type, and add a PostgreSQL regression.

Findings

  • [P2] Convert PostgreSQL aggregate values before adding byte totals — src/config/sessions/session-cold-storage-selection.ts:130-132

Tests

  • Missing end-to-end proof: The supplied evidence does not exercise the new pre-check through Gateway maintenance or Run now, and it contains no PostgreSQL regression.
Agent review details

How this fits together

Gateway maintenance and sessions.storage.run dispatch candidate selection and archive preparation to existing workers, then commit verified archives through the session mutation owner.

flowchart TD
  A[Gateway maintenance or Run now] --> B[Archive worker selection]
  B --> C[Stored event byte totals]
  C --> D[Oversized warning and cooldown]
  C --> E[Prepare eligible envelopes]
  E --> F[Guarded archive mutation]
Loading

Technical review

Best possible solution:

Keep the existing worker-owned pre-check, convert aggregate values before arithmetic, and protect PostgreSQL archival with a focused regression.

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

Source inspection establishes both the original envelope-limit batch exit and the introduced PostgreSQL string-addition path; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The pre-check removes unnecessary envelope work at the existing selection owner, and the three added tests cover distinct failures; numeric conversion is needed to preserve PostgreSQL behavior.

Full review comments:

  • [P2] Convert PostgreSQL aggregate values before adding byte totals — src/config/sessions/session-cold-storage-selection.ts:130-132
    For PostgreSQL, event_utf8_bytes is bigint, so SUM returns numeric text through the current bridge; the Kysely number annotation performs no runtime conversion. With 8,000,000 known bytes and no unknown rows, this expression evaluates "8000000" + 0 to "80000000", exceeds 67,108,864, and incorrectly excludes an archivable transcript for the 24-hour cooldown. Convert the aggregate values to numbers before addition, or use a bridge-supported numeric result type, and add a PostgreSQL regression.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Archive envelope limit and oversized cooldown keeps the original intent (aa0ad4c: Bound restoration memory and worker time while retaining oversized transcripts in the database.)
  • Worker-owned candidate selection keeps the original intent (418ec9b: Move maintenance SQL off the Gateway thread while retaining ordering, budgets, and current commit authority.)
  • Host callback cancellation keeps the original intent (7dd2721: Carry task cancellation through queued callbacks while preserving accepted-write settlement.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/config/sessions/session-cold-storage-worker.test.ts src/config/sessions/session-cold-storage.selection.test.ts src/config/sessions/transcript-payload.test.ts --maxWorkers=1
  • pnpm test src/config/sessions/session-cold-storage-worker.postgres.test.ts --maxWorkers=1
  • pnpm tsgo:core
  • git diff --check

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 418ec9b adds src/config/sessions/session-cold-storage-selection.ts:28 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 418ec9b6bec7; files: src/config/sessions/session-cold-storage-selection.ts)

PR surface

Source +49, Tests +138, Docs +8. Total +195 across 6 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 4 57 8 +49
Tests 1 138 0 +138
Docs 1 8 0 +8
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 203 8 +195

Labels

Label changes:

No label changes.

Label justifications:

  • P2: The optimization improves optional background archival on large installations; the introduced defect affects archival eligibility without deleting history.
  • merge-risk: 🚨 compatibility: Existing PostgreSQL installations can stop archiving transcripts that previously fit beneath the archive limit.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-25T07:23:21.949Z sha afe2074 :: needs changes before merge. :: [P2] Use a UTF-8-safe lower bound for UTF-16 stores

Reviewed October 25, 2026, 7:10 AM ET / 11:10 UTC (Revision 2).

UTF-16 SQLite stores leave event_utf8_bytes null, and octet_length counts
stored UTF-16 bytes, so the pre-check could mark an archivable transcript
oversized and park it on cooldown. Sum exact UTF-8 bytes where known and
halve unknown stored bytes on UTF-16 stores, as sqlite-jsonl-budget does.
@steipete

Copy link
Copy Markdown
Contributor Author

Fixed the UTF-16 finding in dff3331.

The pre-check now sums exact event_utf8_bytes where known plus stored octet_length(event_json) where unknown. It halves the unknown part on UTF-16 SQLite stores, the same lower bound assertSqliteJsonlReadBudget uses (UTF-8 ≥ UTF-16 bytes / 2). PostgreSQL text keeps full stored-byte accounting.

New regression bounds UTF-16 stores by UTF-8 bytes instead of stored UTF-16 bytes builds the fixture in a UTF-16le store through the product writers. It asserts that the reported lower bound never exceeds the transcript's real UTF-8 bytes. It is red on afe2074 (168,838 > 156,419) and green here.

pnpm test src/config/sessions/session-cold-storage-worker.test.ts src/config/sessions/session-cold-storage.selection.test.ts src/config/sessions/transcript-payload.test.ts --maxWorkers=1: 25 passed. pnpm tsgo:core, the changed test typecheck shard, oxlint, oxfmt, and git diff --check all pass. Codex autoreview (P1) on the follow-up: scoped-clean.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dff3331b0c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +130 to +132
const stored = sizes?.stored ?? 0;
const lowerBoundBytes =
(sizes?.known ?? 0) + (utf16Storage ? Math.ceil(stored / 2) : stored);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Convert PostgreSQL aggregate results before adding byte counts

For PostgreSQL stores, event_utf8_bytes is a bigint, so SUM(event_utf8_bytes) returns numeric. The PostgreSQL bridge leaves that result as a string (decodePostgresRows only converts OID 20), and Kysely's <number> annotation does not convert it at runtime. Consequently, an ordinary transcript with 8,000,000 known bytes and no legacy rows computes "8000000" + 0 as "80000000", incorrectly exceeds the 64 MiB limit, and is excluded from archival for the 24-hour cooldown. Convert the aggregate results to numbers or cast the SQL aggregate to a bridge-supported integer type before performing this addition.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026
@steipete
steipete enabled auto-merge (squash) October 25, 2026 11:25
@steipete
steipete merged commit 4ca976f into main Oct 25, 2026
151 of 153 checks passed
@steipete
steipete deleted the steipete/cold-storage-oversized-precheck branch October 25, 2026 11:30
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash auto-merge.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
…limit (openclaw#183378)

* perf(sessions): skip cold envelopes for transcripts over the archive limit

Cold storage learned that a transcript exceeds the 64 MiB uncompressed
archive limit only after building its whole envelope inside a read
snapshot, then ended the batch. On Team, 50 windows (6.15 GiB) are over
the limit; their snapshot reads took a median 3.2 s and up to 106 s, and
the in-memory cooldown resets on every deploy, so each restart re-read
them.

Selection now sums the original event bytes through the session index
and classifies those windows as oversized before any envelope work; later
candidates in the same pass still archive. The warning names the session
and the lower-bound bytes.

* fix(sessions): bound UTF-16 stores by UTF-8 bytes in the cold pre-check

UTF-16 SQLite stores leave event_utf8_bytes null, and octet_length counts
stored UTF-16 bytes, so the pre-check could mark an archivable transcript
oversized and park it on cooldown. Sum exact UTF-8 bytes where known and
halve unknown stored bytes on UTF-16 stores, as sqlite-jsonl-budget does.
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: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant