Skip to content

refactor(state): reduce agent database admission queries - #172373

Merged
steipete merged 3 commits into
mainfrom
dbasync/admitcut
Oct 14, 2026
Merged

steipete merged 3 commits into
mainfrom
dbasync/admitcut

Conversation

@steipete

@steipete steipete commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Agent database admission repeats version, ownership-table, and empty-catalog probes while opening a fresh store, then runs legacy migration discovery before recognizing that the store is empty. Lease claim also rereads peer paths already loaded in its transaction, and completion rereads the claim before updating it.

Why This Change Was Made

Carry the existing admission owner's catalog facts through the agent opener and schema preparation. Skip legacy discovery for an empty store, derive surviving peer ownership from the claim transaction's existing rows, and record completion with one conditional update. The existing DDL/rollback invalidation owner remains authoritative; this adds no cache or handshake and removes the superseded reads.

User Impact

Agent database opening does less repeated bookkeeping with no intended behavior change. There is no schema/version, stored-format, retention, permission, or SDK contract change. Existing integrity, cross-agent ownership, migration custody, and destructive cleanup authority checks remain. State boot/worker admission is outside this change.

Removed checks and accepted edge cases

  • Repeated version and empty-catalog probes: admission facts remain authoritative until tracked DDL invalidates them; unsupported external writers during Gateway custody are outside the ownership contract.
  • Repeated ownership/version reads immediately before schema creation: synchronous preparation remains under Gateway or exclusive maintenance custody; unsupported writes between preparation and the transaction are not polled for.
  • Legacy-table discovery on a proven empty store: an empty catalog has no legacy tables to migrate.
  • Second peer-lease query: the claim transaction's existing rows, excluding stale rows it removed, determine other owners.
  • Completion reread and process-start recapture: the original claim identifies completion; the conditional update still refuses a missing or mismatched lease without recreating it.

Measurement

Three alternating pairs, M1/C1/M2/C2/M3/C3, compare base e589d87d7b7e02bccf241fad47b62f0fdab04a2a with reviewed candidate 02d5010ba90781a50165ccd6321c85b7ce3c569c. All six workloads completed 13 successful turns, explicit child completion, cron and committed compaction, then exited successfully. Each warmed value is the median [minimum–maximum] of three ten-turn means; other values are medians/ranges of per-run totals. SQL counts include all observed threads. Causal counts follow turn attribution; raw counts include background work inside the phase window.

Metric Base Candidate
Warmed causal SQL / turn 117.4 [117.3–120.2] 115.3 [114.5–115.6]
Warmed causal requests / turn 31.9 [31.9–32.6] 31.3 [31.2–31.7]
Warmed causal MAIN / turn 0 [0–0] 0 [0–0]
Warmed raw SQL / turn 148.8 [145.4–154.4] 143.9 [143.5–146.6]
Warmed raw requests / turn 44.6 [42.6–47] 43.2 [42.4–43.5]
Warmed raw MAIN / turn 1.3 [1.3–1.3] 1.3 [0.4–1.3]
Connect + first turn SQL 1447 [1440–1495] 1418 [1407–1444]
Connect + first turn requests 183 [179–188] 180 [175–186]
Connect + first turn raw MAIN 0 [0–4] 0 [0–9]
Boot SQL 688 [682–690] 688 [685–691]
Boot requests 92 [89–92] 91 [90–94]
Boot MAIN 454 [450–454] 454 [454–454]
Whole workload SQL 4936 [4884–5224] 4917 [4879–4950]
Whole workload requests 1079 [1042–1135] 1082 [1064–1085]
Whole workload MAIN 476 [476–476] 476 [476–476]
T1 call sites in touched admission/lease owners 0 0

The primary admission reduction is cold work. Summing the eight admission operations below within each run before computing medians gives connect+first 228 [208–229] → 208 [207–208] statements, with 3 [2–3] → 3 [3–3] distinct linked requests and zero MAIN. Across the whole workload those operations total 281 [261–282] → 257 [256–257] statements. These scopes include creation DDL and necessary admission, not just removable probes.

Connect + first operation SQL base → candidate Linked requests base → candidate
Schema maintenance 144 [143–144] → 132 [132–132] 3 [2–3] → 3 [3–3]
Integrity preparation 43 [24–43] → 37 [37–37] 3 [2–3] → 3 [3–3]
Canonical persistence precheck 3 [3–3] → 0 [0–0] 1 [1–1] → 0 [0–0]
Lease claim 5 [5–5] → 4 [4–4] 1 [1–1] → 1 [1–1]
Admission completion 12 [12–12] → 11 [11–11] 1 [1–1] → 1 [1–1]
Native opener 14 [14–14] → 16 [16–16] 1 [1–1] → 1 [1–1]
Schema metadata 1 [1–2] → 2 [1–2] 1 [1–2] → 2 [1–2]
Registration 6 [6–6] → 6 [6–6] 1 [1–1] → 1 [1–1]

Linked request counts overlap across operations; the aggregate above unions request IDs. Removing a precheck's linked request does not imply eliminating a worker dispatch. Earlier catalog admission moves encoding/schema-version/catalog reads into the native opener, accounting for its +2 statement delta; the aggregate includes that cost. Broad read-only callbacks and generic admission-wrapper descendants contain ordinary runtime work and are excluded from the admission saving. The statement ledger records observed database locations, opener frames when present, owner, thread, purpose, SQL shape and source frames; missing opener/location evidence remains explicit. Every selected row has source frames. Historical family matches are advisory, not proof of identical stores.

Whole-workload requests rise 3 (+0.28%), so this is not a claim of universally fewer requests. Whole causal requests fall 556→553 while background requests rise 526→529; medians are independently computed and are not additive. Wire-operation medians are execute 695→698, task 378→377, open 3→3 and close 3→3. The touched admission aggregate adds no median linked requests and the patch adds no worker dispatch. The observed increase is outside the scoped admission saving. Raw cron requests rise 105→117 and second-session requests 209→214; both warmed raw and causal medians decrease. No extra pair was run to chase a better number.

Deferred startup crosses the connect/first-turn boundary: connect SQL is 49→334 while first-turn SQL is 1391→1075. Use their combined 1447→1418 total; boot+connect+first is 2129→2106 SQL and 272→270 requests. Boot MAIN is unchanged. Warmed raw MAIN is unchanged plugin lifecycle lease checking, outside this diff; causal warmed MAIN stays zero.

Shared-reader leaf SELECTs also show no median increase (warmed causal statements/turn): exact-entry facts 0.2→0, actor hydration 0.2→0.1, entry-cache snapshot scans 0→0, row facts 0→0, dedicated watermark reads 0.5→0. These source files are unchanged. Their variation is not credited as a direct admission saving.

Counts use a task-local copy of the shared synthetic Gateway harness. Two adaptations relocate its temporary fixture prefix onto external storage and raise the spawn-receipt scanner depth from 8 to 16: current tool output nests a successful receipt at depth 11. The initial run was rejected because that parser missed the receipt and skipped the explicit child wait. All six accepted runs use identical corrected harness hashes; workload, SQL observer and causal counting are unchanged. Source identities and complete turn/request coverage were verified. No latency claim is made on the busy host.

Validation and limitations

New regressions cover stale lease removal with an unknown live peer, admission completion after release, and schema creation/rollback publication. Existing integrity, lease lifetime, ownership, worker admission and destructive cleanup tests cover retained boundaries. Focused owner tests, formatting, changed-file type-aware lint and diff checks pass. CI exposed a read-only refusal-order regression during development; the final change preserves the earlier newer-version refusal without adding SQL.

Two Codex review rounds completed clean. Candidate CI passed type-aware lint, production/test types, source contracts, PostgreSQL conformance, Madge (0 cycles), T1 (223→223; compatibility 12→12), and dialect ratchets (mechanical 0→0; design 1→1; engine maintenance 11→9). The run finished with 69 successful jobs, 25 skipped jobs, four failed behavioral jobs and the failed aggregate gate. The candidate Knip scan and exact-base Knip scan have exactly the same eight production and eight full-tree findings, none in this diff. The base job was canceled during a later unnecessary runtime rebuild, after Knip completed.

The Doctor noncanonical-session assertion also fails on main. Both chat rewrite/replacement assertions and all three completion-persistence assertions reproduce on the exact base; those files are untouched. The auth job reported a missing prepared reply-dispatch owner. A focused comparison passed on both exact revisions using CI's supported prebuilt runtime. This does not prove that failure fixed or inherited: it remains unreproduced and assigned for follow-up. No candidate-only product regression was reproduced; landing followed the authorized relaxed policy, retaining these failures explicitly.

The published-driver × candidate update cell passed on prior head e92de7a07fdabaac91b3e55a55ecf848ada1533e: package acceptance, including npm install acceptance and the targeted Docker update scenario. This is reused prior-head evidence for the identical admission patch, not an exact-final-head claim. SDK API diff is not applicable.

LOC: 8 files, +189/−84; production +83/−82, tests +98/−2, documentation +8. No generated schema or inventory baseline changes.

@clawsweeper

clawsweeper Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 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-14T20:20:26.863704Z 02d5010 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: M labels Oct 14, 2026
@steipete
steipete merged commit d6bed86 into main Oct 14, 2026
168 of 174 checks passed
@steipete
steipete deleted the dbasync/admitcut branch October 14, 2026 20:53
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 15, 2026
…2373)

Reuse admitted schema facts during agent database opening and fresh schema creation. Derive surviving peer ownership from the existing claim read and record admission with one conditional update. Preserve integrity, agent ownership, migration custody, and destructive cleanup authority.

No schema, stored-format, retention, permission, or SDK changes. Two scoped Codex reviews are clean. Current-head static gates pass or match exact inherited Knip findings; focused owner regressions pass. Doctor and session CI failures reproduce on main; the auth recovery case passes on both exact revisions but its original CI failure remains unreproduced. Landed under the authorized relaxed CI policy. Three alternating SQL measurement pairs follow in the PR evidence.
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 size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant