Skip to content

perf(audit): reduce shared writer waits during retention pruning - #176797

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-channel-ingress-writer-wait-20260919
Oct 19, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-channel-ingress-writer-wait-20260919

Conversation

@steipete

@steipete steipete commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Reduces inbound-message and interactive-write delays when enabled execution-identity audit storage reaches its retention caps and competes for the shared SQLite writer.

User Impact

Audit-heavy installations spend less time holding the writer, so channel ingress and interactive state writes can proceed sooner. Retention membership, oldest-first deletion, expiry budgets, transaction boundaries, FIFO ordering, and ingress dedupe/durability remain unchanged. No schema, configuration, or migration change is required.

Why This Change Was Made

Each identity/decision insertion previously materialized up to 100,000/250,000 retained IDs for an overflow NOT IN query. The replacement finds the first overflow (timestamp, id) through the existing ordered index, then deletes the oldest eligible rows within the remaining expiry-cleanup budget. It still derives the boundary from database state inside the same transaction.

The shared writer remains the sole owner. Existing ingress batching is retained; another worker would still contend for the same SQLite database. This removes retained-set construction instead of introducing another queue, cache, or priority policy. Runtime production delta is +4 lines (14 added, 10 removed): the typed tuple cutoff needs an offset, a one-row limit, and tuple typing to preserve exact bounded retention. The rest is two compatibility cases (+59 test lines), documentation, and a rerunnable synthetic benchmark. The requested diff filter excluding tests and docs/* totals +522 lines: +4 runtime, +495 benchmark, and +23 benchmark README. The benchmark growth supplies the requested reproducible concurrent-writer workload and persistence/dedupe checks; it adds no product runtime state.

Evidence

Acceptance was recorded before measurement: preserve every accepted audit/ingress/interactive operation and bounded retention; reduce audit service time and ingress queue wait without an interactive p99 regression. The benchmark drives the real audit FIFO, channel-ingress queue, and machine-state writer with 100,000 retained contexts and 250,000 retained decisions. Each 60-second run offers 180 audit records (90 identities/90 decisions), 60 ingress messages, 12 duplicate attempts, 60 idle-drain polls, and 60 interactive writes.

Four concurrent A/B pairs completed on an isolated Linux sandbox, swapping 16-core CPU halves for each successive pair under one exclusive measurement lock. A is main 8a23c56a8338; B differs only in the two retention queries. Fixtures were seeded sequentially with the same timestamp and sizes; separate databases were reused across pairs, retaining the same caps. The branch was subsequently rebased onto latest main without conflicts; the audit/ingress/state paths were unchanged by that refresh.

Results below are medians across the four runs. Decisions use each pair's B minus A difference, not the difference between unpaired medians.

Metric A median B median Median paired B−A Paired B−A range
Ingress queue wait, s/min 15.316 0.847 -14.145 -25.489 to -11.698
Audit queue wait, s/min 0.103 0.105 +0.007 -0.002 to +0.025
Interactive execute queue wait, s/min 2.232 0.175 -1.753 -6.294 to -0.587
Audit service, s/min 19.690 0.744 -18.942 -20.033 to -18.153
Interactive write p50, ms 2.112 2.048 -0.020 -66.839 to +3.439
Interactive write p95, ms 161.209 5.454 -155.751 -174.712 to -54.980
Interactive write p99, ms 171.514 7.442 -149.357 -180.671 to -4.201
Ingress p99, ms 253.001 16.335 -237.298 -266.028 to -186.040
Pair Ingress queue Δ, s/min Audit queue Δ, s/min Audit service Δ, s/min Interactive p99 A → B, ms
1 -25.489 +0.001 -19.354 185.478 → 4.807
2 -14.566 -0.002 -18.531 157.549 → 9.688
3 -13.724 +0.025 -20.033 594.553 → 590.351
4 -11.698 +0.013 -18.153 156.050 → 5.196

The improvement is reduced audit service time and contention on other request classes; audit's own queue wait is neutral (+7 ms/min median paired difference). Pair 3 retained a shared ~590 ms interactive tail stall on both sides rather than discarding it. Interactive p50 is roughly neutral overall; pair 2 moved from 2.0 to 5.4 ms while its p95/p99 improved, reflecting the phase-sensitive contention at these low rates.

mpstat -P 96-127 recorded mean CPU occupancy for the two halves (including the experiment): 10.5%/9.0%, 7.2%/7.6%, 13.5%/12.0%, and 4.6%/5.6% across pairs 1–4. This is context, not an invalidation threshold. Both sides ran concurrently at the same offered start time.

All eight processes exited successfully, all submitted audit records persisted, duplicate attempts retained one ingress tombstone, no ingress/writer work remained unsettled, and no diagnostic samples were dropped. Row counts stayed at the existing caps.

Reproduction (run each side from its source tree with separate synthetic state):

node --import ./scripts/tsx.mjs scripts/bench-channel-ingress-writer.ts \
  --state-dir "$STATE" --seed-only --fixture-time-ms "$FIXTURE_TIME"
node --import ./scripts/tsx.mjs scripts/bench-channel-ingress-writer.ts \
  --state-dir "$STATE" --seconds 60 --start-at-ms "$SHARED_START" --output "$REPORT"

Prepare outside measurement, then run both sides concurrently with systemd-run --scope -p AllowedCPUs=96-111 / 112-127, swapping halves for the next pair. See scripts/perf/README.md for workload details.

Validation: 31 focused tests passed across execution-identity-context.test.ts, execution-decision-facts.test.ts, and audit-event-writer.worker.test.ts; changed-file oxfmt/lint and the single pnpm tsgo:core lane passed. The export-collision guard and 22 tests across eager-import-closure.test.ts / pr-wrapper-source-closure.test.ts also passed. Independent isolated Codex review was scoped-clean with no actionable P0–P2 findings. PR CI supplies broad validation; no Testbox/Crabbox lease was used.

Limits: this is storage-owner evidence, not Gateway transport, external-channel, model, or deployment end-to-end proof. It intentionally stresses opt-in identity/decision storage at retention caps; it does not establish current production table sizes, ordinary run/tool-only audit mix, or the fraction of reported production queue delay this change removes. The added tests protect retention equivalence; performance evidence comes from the benchmark, not a timing assertion in unit tests.

CI follow-through: initial run 39188739353 found one benchmark-only TS2345: assert.deepEqual(errors, []) narrowed the error accumulator to never[]. The follow-up uses length assertions with error diagnostics, preserving the measured workload, and received a fresh clean independent review. All selected audit/changed-code test shards, lint, docs, and architecture guards passed on that run.

The remaining initial-run failures share an inherited Control UI import mismatch introduced by #176453. Unchanged main d79b0a4df0b24fd1e630e22da22cc2bb40af45ce reproduces the identical SidebarProjectionMemo missing export in UI build job 117857506284, and TS2724/TS7006 in production-types job 117857507285. session-attention.ts and sidebar-projection-memo.ts are byte-identical on that main baseline and this PR; no UI files are changed here. These errors also propagate to the core test-type stripes and build-artifact workflow. No CI jobs were rerun to chase green.

The repaired head 296419aeacee94456faa2528cc4c80716d589a8d passes check-test-types in CI run 39189406757, including the benchmark script typecheck.

@clawsweeper

clawsweeper Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@steipete
steipete marked this pull request as ready for review October 19, 2026 02:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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-19T02:22:05.851523Z 296419a 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 gateway Gateway runtime scripts Repository scripts size: L labels Oct 19, 2026
Replace retained-ID materialization with indexed tuple cutoffs while preserving expiry budgets and oldest-first retention. Add focused compatibility cases and a synthetic shared-writer benchmark.
@steipete
steipete force-pushed the steipete/perf-micro-channel-ingress-writer-wait-20260919 branch from ebd3f25 to 296419a Compare October 19, 2026 02:19
@steipete
steipete merged commit 4061902 into main Oct 19, 2026
131 of 132 checks passed
@steipete
steipete deleted the steipete/perf-micro-channel-ingress-writer-wait-20260919 branch October 19, 2026 02:27
steipete added a commit that referenced this pull request Oct 19, 2026
Register the documented maintainer CLI from #176797 using the existing executable entry pattern. Remove the unused fixture left by #176648 after its diagnostic suite reductions were dropped; keep the original diagnostic tests intact.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 19, 2026
…nclaw#176797)

* perf(audit): reduce shared writer waits during retention pruning

Replace retained-ID materialization with indexed tuple cutoffs while preserving expiry budgets and oldest-first retention. Add focused compatibility cases and a synthetic shared-writer benchmark.

* fix(bench): preserve the writer error array type
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 19, 2026
Register the documented maintainer CLI from openclaw#176797 using the existing executable entry pattern. Remove the unused fixture left by openclaw#176648 after its diagnostic suite reductions were dropped; keep the original diagnostic tests intact.
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 gateway Gateway runtime scripts Repository scripts size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant