Skip to content

fix: session deletion delays unrelated agent writes - #189287

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-session-lifecycle-delete-sql-20260919
Oct 31, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-session-lifecycle-delete-sql-20260919

Conversation

@steipete

@steipete steipete commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Session deletion unnecessarily scans unrelated completion history and delivery rows while holding an agent database's only SQLite writer, delaying other session writes.

User Impact

Deletion releases the writer sooner. Slow session.lifecycle.delete warnings now distinguish statement execution from remaining callback time, without logging SQL text or bindings. This removes proven scan work; the reported multi-second production tail is not yet attributed or claimed fixed.

Why This Change Was Made

Add nonunique indexes for the completion foreign-key cascade and delivery cleanup predicates. Existing admission and first-use schema owners install them once using published schema facts. This is a lead-approved same-version SQLite addition: schema 39, row contracts, atomic deletion, and retention remain unchanged. Older-schema readers and writers accept the extra indexes. PostgreSQL v39's exact sealed catalog stays byte-identical; its adoption waits for the next natural migration. All 23 historical SQLite catalogs also remain identical to base. Version-crossing migration witnesses permit only the two validated canonical additions; same-version witnesses and local indexes remain strict.

Reuse native statement observation for this operation only. The existing 1,000 ms slow-hold threshold controls output; at most 32 statement groups plus overflow carry counts, total/max elapsed time, and normalized-SQL fingerprints. callbackOtherMs also includes preparation, final guards, rollback, GC and scheduling; neither field is CPU time.

Related: #184370. Its projection drain requires a nonempty transcript and does not run for the empty-transcript path labeled session.lifecycle.delete; it also leaves canonical deletion unbounded. No code is adopted. A 480 MiB board-artifact probe held the writer for 60–115 ms, so this change preserves atomic deletion instead of introducing a speculative authoritative-data drain.

Evidence

Synthetic fixture: 1,000,000 transcript rows, 10,001 sessions, 34.94 GiB, 12,497 synthetic completion outcomes and 14 deliveries. Five counterbalanced pairs, 20 deletions per arm per pair, separate allocation sampling, with A/A controls. The real native deletion owner runs inside the agent writer transaction; rollback restores identical input. Both index arms use the same diagnostic instrumentation. Samples include common assertions and rollback, and exclude worker transport, pre-BEGIN admission, and commit durability costs; the real worker flow is tested separately.

Metric Without indexes With indexes
Median writer hold 14.900 ms 6.428 ms
p95 writer hold 43.740 ms 18.287 ms
Maximum writer hold 222.392 ms 428.296 ms
Thread CPU per deletion 13.509 ms 6.523 ms
Sampled JS allocation per deletion 543,385 B 537,972 B

A/A writer medians were 11.437/13.793 ms and CPU 12.243/13.869 ms; this heavily loaded local host cannot establish production tail latency. The A/A maximum was already 427.591 ms, matching the indexed outlier; maximum latency is therefore explicitly unqualified, not claimed improved. Every paired median and CPU result improved. Exact row counts and retained-content digests passed. Completion index construction took 4.6–13.3 ms; delivery index construction took 0.3–0.9 ms on the seeded disk database. These are isolated DDL timings on an admitted connection; full cold/dirty admission and integrity checks are excluded, so they do not establish a sub-second deployment or database-open bound.

Diagnostic cost, separately measured over five pairs of 100,000 tiny 16-statement transactions: +1.75 µs CPU and +1.38 KiB sampled JS allocation per transaction. This explicitly requested attribution has a measurable collection cost; normalization/hashing happens only for slow warnings.

Reproduce with the committed scripts/perf/session-lifecycle-delete-sql-measure.ts and scripts/perf/session-delete-diagnostics-measure.ts commands documented in their headers. The fixture uses incremental auto-vacuum on local APFS with an empty freelist, not a fragmented btrfs store; no production data was copied.

Initial CI run 41192504394 found five attributable migration-test failures across four suites: historical-schema drift and rejected migration witnesses. These were reproduced locally and repaired at those owners, without changing frozen expectations; all 47 tests in the four affected suites now pass. Added assertions retain rejection of lost local indexes and incorrect definitions under canonical names.

Focused proof: 39 index/compatibility tests, 20 transaction/observation tests, and six real worker deletion tests pass (112 distinct tests including the 47 migration tests). The two affected generation checks were rerun after the repair, along with scoped lint, state type checking, formatting, and the test-line ratchet. Both new diagnostic regressions fail on the base. All three actual DELETE plans scan on the base and use indexes after the fix. The lazy-first-use test caught an extraction-boundary omission; its corrected path passes. The older-schema open/write test checks retained rows, foreign keys, and unchanged version markers.

Scoped lint, formatting, unused-file scans (zero entries), and the 243-line regression allocation pass. pnpm check:changed identified a test tuple typing error, now corrected with the affected state type check passing. Its export scan also reports four inherited exports: resolveFollowupReplyAnchor, captureRepositoryClientActor, the Gateway RepositoryOperation re-export, and RepositoryAuthorization. Their owners/consumers are unchanged from base; #189237 independently repairs the latter three and documents the first as inherited. No aggregate green claim is made for that baseline scan.

Fresh independent Codex autoreview covered the complete staged candidate against 33facea809027bccac675f53701eea6344230da9 through P2 in two passes. Its sole finding was rejected after source inspection: the older-schema test imports sqlite-schema-contract.test-support, which explicitly builds a catalog from frozen/synthetic DDL before invoking the runtime validator. The finding assumed the runtime string-only path. No accepted or actionable findings remain; no production code changed after review.

Handwritten production delta is +115 lines (+124 including generated TypeScript); the measurement scripts total 577 lines. Necessary production growth covers two derived indexes, existing observation/admission owners, and historical schema/witness compatibility; no config, cache, queue or deletion protocol is added. Regression growth covers scan plans, older-schema writes, lazy admission, PostgreSQL catalog compatibility, and bounded private diagnostics.

@clawsweeper

clawsweeper Bot commented Oct 31, 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 docs Improvements or additions to documentation scripts Repository scripts size: XL labels Oct 31, 2026
@steipete
steipete marked this pull request as ready for review October 31, 2026 10:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 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-31T10:59:18.228366Z 96db960 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.

@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. labels Oct 31, 2026
@clawsweeper clawsweeper Bot added 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-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 31, 2026
@steipete
steipete enabled auto-merge (squash) October 31, 2026 11:18
@steipete
steipete merged commit ce90837 into main Oct 31, 2026
142 of 144 checks passed
@steipete
steipete deleted the steipete/perf-micro-session-lifecycle-delete-sql-20260919 branch October 31, 2026 11:21
@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 Nov 1, 2026
* fix: session deletion delays unrelated agent writes

* fix: preserve historical schemas during deletion index admission
RomneyDa added a commit that referenced this pull request Nov 17, 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: 🚨 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. scripts Repository scripts size: XL 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