Skip to content

perf(approvals): index and reuse pending replay by session - #184453

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-approval-replay-session-index-20261026
Oct 26, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-approval-replay-session-index-20261026

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

A reconnect burst with approval replay repeatedly scans and decodes every pending approval. The authenticated subscribe scheduler admits four preparations at a time, so sharing only unsettled promises still repeats the scan across successive groups of a 40-socket burst.

User Impact

Reconnects with approval replay complete sooner while delivering the same pending approvals in the same order. No configuration or migration is required.

Why This Change Was Made

The approval store now owns a pending-approval view indexed by persisted session audience. It hydrates once in bounded read-worker pages, updates from committed postimages/tombstones, and retires with the physical database owner. Warm selection walks the selected session's pending approvals. The runtime retains at most 64 idle session/reviewer replays while the store confirms their currency, sharing completed preparation across scheduling groups.

Queued approval writes prevent reuse from FIFO admission through settlement. Expiry, visible receipt changes, unknown outcomes and owner retirement invalidate snapshots. Existing expiry reconciliation, corrupt-row repair, reviewer filtering, delivery order and per-client authority checks remain intact. No schema, retention or protocol changes.

Evidence

Reconnect measurement

Real built Gateway, synthetic 10,000 sessions and 10,000 live pending approvals across 1,000 audiences, 40 authenticated sockets reconnecting simultaneously. The committed harness verifies exact ordered IDs and sanitized payload equality on every reply. Five alternating AB/BA rounds, 1,000 warm subscriptions per arm/case/round; initial bursts are separate. The first distinct-session burst has cold replay scopes and an already hydrated pending index.

All 24,320 ordered-payload checks passed across the timing and separate allocation runs. Base: 798570bc460f96d8321744be7bb99637d44a5b7d; measured candidate: b5966f91df4578cb503136f25ca0bdc02bb4383e. Final head only fixes the benchmark probe's promise-returning event listener; production sources and measurement behavior are unchanged.

Warm metric Same session: base → candidate Distinct sessions: base → candidate
Client p50 / p99, ms (5,000/arm/case) 435.34 / 1219.22 → 3.58 / 99.71 1599.69 / 3641.89 → 3.63 / 16.17
Server handler p50 / p99, ms (5,000/arm/case) 73.845 / 162.707 → 0.039 / 0.281 292.975 / 803.591 → 0.046 / 0.264
FIFO queue p50 / p99, ms (5,000/arm/case) 359.13 / 1135.86 → 2.77 / 85.58 1291.85 / 3291.95 → 2.82 / 14.65
Main CPU, µs/subscribe 787.45 → 174.64 (−77.8%) 2355.18 → 159.72 (−93.2%)
Main sampled allocation, B/subscribe (1,000/arm/case) 183,600 → 86,457 (−52.9%) 471,093 → 85,790 (−81.8%)
Allocation samples, base / candidate 10,554 / 5,203 26,761 / 5,108
Observed mean RSS, MiB 1798.9 → 1717.5 1780.2 → 1727.2
Observed mean main heap used, MiB 652.9 → 680.7 658.8 → 715.0
Host load mean (range), base 19.55 (14.71–25.39) 18.42 (13.39–26.64)
Host load mean (range), candidate 17.42 (14.80–20.07) 17.65 (14.80–20.07)

Every timing pair improves. Candidate same-session p99 ranges from 6.03 to 174.94 ms; the outlier is retained. The initial same-session burst, including index hydration, improves client p50/p99 451.02/904.51 → 186.64/189.26 ms (40 samples; context only). First distinct-session burst: 3544.00/5667.11 → 8.48/13.08 ms, with the index already hydrated.

Tradeoff: first-burst main CPU rises 63.95→76.36 ms, and sampled allocation 17.52→40.44 MiB, to populate the pending index. Warm observed heap is higher; RSS/heap were sampled after bursts without forced collection and do not establish retained-memory deltas. This focused FAST PATH proof does not claim whole-Gateway GC, event-loop, sessions.list, or worker-CPU improvements.

Run instructions and fixture details are in scripts/perf/approval-replay-burst-measure.ts. Allocation sampling runs separately from untraced timing; CPU/allocation claims cover the Gateway main thread, not worker CPU or whole-Gateway load.

Correctness and checks

  • Regression before runtime caching: 120 preparations for 40 interleaved session/reviewer triplets, versus 3 expected; fixed version passes, including warm expiry.
  • Receipt-maintained page test: inserts/terminal decisions update ordered session pages without another worker read; close/reopen hydrates fresh state. Equal-timestamp Unicode IDs preserve SQLite binary ordering.
  • 38 focused store/runtime/subscription tests and 20 approval-worker/subscription sibling tests passed. 26 import/PR-wrapper closure checks passed; export-name collision guard passed.
  • Targeted core typecheck, changed-file lint and formatting passed. No full local suite.
  • Independent subagent review (autoreview route unavailable): clean P1/P2 review. The helper refused a provider route mismatch; operator configuration was unchanged.

The necessary production growth implements the requested session index and its receipt/expiry/physical-owner lifecycle. The author simplification pass removed redundant seed-promise sharing because the existing FIFO already serializes seeding. The retained SQL reader is the existing corruption/expiry/maintenance repair path, not a second warm replay policy.

Source production delta: +482 LOC (507 added, 25 deleted). This growth is required by the explicitly requested session index plus its existing FIFO, corruption, expiry and physical-owner contracts; bounded replay retention uses those owner facts. The committed real-socket benchmark is separate tooling.

CI follow-up: the first run caught the probe listener lint issue, now fixed and independently reviewed; both IPC modes pass a focused smoke and the exact scripts typed-lint configuration passes. On final head 0bcfdac168ec72e2dbdecc518de847c223410028, all 40 other CI jobs passed. The remaining failure in run 40302829656 is ui/src/e2e/stale-build-recovery.production.e2e.test.ts: command timed out after 120000ms: node scripts/run-node.mjs --help, during cold build preparation before Gateway startup.

The same single test file reproduces the identical timeout on the CI merge's unchanged main parent 1b6eb7bf2e7f7cb31ce9ee71c6f1d778f7ee6ce0, using provider blacksmith-testbox, lease tbx_01m5vtf6p6m6wfkxy28b3yjdxr (Linux x64, Node 24.22.1, four CPUs, 15.4 GiB RAM). Command: taskset -c 0,1,2,3 node scripts/run-vitest.mjs run --config test/vitest/vitest.ui-e2e.config.ts --configLoader runner ui/src/e2e/stale-build-recovery.production.e2e.test.ts --maxWorkers=1, with isolated synthetic state and a cold dist tree. Result: exit 1, exact 120000ms CLI-build timeout; total remote command 211.5 s. The test, suite helper, CLI bootstrap and build orchestrator are unchanged by this PR. The lease is stopped. An earlier Mac baseline passed, so the failure is host-sensitive; the matching four-CPU Linux baseline establishes it as inherited. No CI rerun was requested. Landing uses the authorized inherited-failure exception, retaining the red result.

All timing pairs, per-arm memory and host load

Per-round latency and CPU

Case Round Arm n Client p50/p99 ms Handler p50/p99 ms Admission p50/p99 ms FIFO queue p50/p99 ms Main CPU µs/op
same cold base 40 451.016/904.512 86.287/101.313 360.685/800.218 360.545/800.181 1598.85
same cold candidate 40 186.642/189.260 0.077/174.876 176.864/179.236 176.778/179.199 1909.10
same 0 base 1000 463.462/1219.799 77.569/186.454 383.751/1135.934 383.694/1135.862 874.44
same 0 candidate 1000 4.132/9.745 0.050/0.205 3.255/8.416 3.204/8.375 187.16
same 1 base 1000 440.265/1276.577 72.799/470.262 364.790/1202.611 364.672/1202.528 851.40
same 1 candidate 1000 3.115/6.885 0.031/0.159 2.401/6.048 2.358/5.998 128.17
same 2 base 1000 430.623/1839.081 72.493/374.931 356.521/1566.580 356.432/1566.531 703.57
same 2 candidate 1000 4.724/174.936 0.056/0.433 3.708/142.598 3.649/142.508 264.89
same 3 base 1000 432.688/870.419 75.574/128.686 352.070/791.159 352.007/791.119 773.97
same 3 candidate 1000 3.185/6.027 0.033/0.152 2.513/4.998 2.482/4.970 126.39
same 4 base 1000 393.601/767.626 71.915/87.643 311.007/687.103 310.914/687.066 733.86
same 4 candidate 1000 3.505/54.283 0.036/0.317 2.737/53.142 2.707/53.026 166.60
same all-warm base 5000 435.338/1219.220 73.845/162.707 359.173/1135.934 359.130/1135.862 787.45
same all-warm candidate 5000 3.581/99.708 0.039/0.281 2.807/86.104 2.768/85.579 174.64
distinct cold base 40 3544.005/5667.107 331.799/2122.673 1889.667/5345.988 1889.507/5345.871 3794.32
distinct cold candidate 40 8.482/13.080 0.173/0.448 6.903/11.541 6.844/11.492 326.07
distinct 0 base 1000 1490.857/3153.312 286.210/743.128 1187.129/2783.030 1187.000/2782.919 2236.52
distinct 0 candidate 1000 4.421/8.961 0.053/0.193 3.542/7.595 3.493/7.564 173.28
distinct 1 base 1000 1684.932/3658.695 309.444/834.332 1372.336/3318.212 1372.219/3318.090 2677.66
distinct 1 candidate 1000 3.299/6.365 0.041/0.151 2.596/5.498 2.560/5.450 130.57
distinct 2 base 1000 1552.906/3341.662 284.022/696.928 1246.963/2996.992 1246.725/2996.872 2229.27
distinct 2 candidate 1000 5.123/15.680 0.067/0.365 4.188/13.614 4.113/13.551 229.93
distinct 3 base 1000 1629.583/3858.737 295.241/755.110 1303.354/3492.353 1303.234/3492.230 2255.54
distinct 3 candidate 1000 3.260/8.326 0.044/0.149 2.620/7.272 2.582/7.250 134.16
distinct 4 base 1000 1669.252/3933.460 297.977/847.552 1349.739/3478.461 1349.624/3478.365 2376.92
distinct 4 candidate 1000 3.105/65.292 0.039/0.194 2.337/63.434 2.307/63.307 130.66
distinct all-warm base 5000 1599.694/3641.891 292.975/803.591 1291.986/3292.130 1291.848/3291.946 2355.18
distinct all-warm candidate 5000 3.630/16.169 0.046/0.264 2.867/14.748 2.822/14.648 159.72

Memory and host context

Case Round Arm RSS mean/max MiB Heap used mean/max MiB Host load mean (min–max)
same cold base 1845.1/1845.1 619.7/619.7 24.40 (24.40–24.40)
same cold candidate 1940.4/1940.4 628.6/628.6 24.36 (24.36–24.36)
same 0 base 1933.0/1981.4 637.1/657.6 22.96 (21.86–24.36)
same 0 candidate 1826.6/1871.5 658.0/695.4 16.86 (16.86–16.86)
same 1 base 1863.6/1888.1 661.9/692.2 17.29 (16.50–19.32)
same 1 candidate 1845.3/1874.4 743.6/767.3 16.86 (16.86–16.86)
same 2 base 1804.9/1810.8 659.1/679.7 23.85 (22.45–25.39)
same 2 candidate 1649.1/1667.3 657.8/677.7 14.80 (14.80–14.80)
same 3 base 1692.7/1714.7 651.4/691.4 17.62 (16.61–18.50)
same 3 candidate 1655.3/1656.0 687.4/714.7 18.50 (18.50–18.50)
same 4 base 1700.1/1700.6 655.3/684.5 16.00 (14.71–19.47)
same 4 candidate 1611.1/1611.2 656.8/685.0 20.07 (20.07–20.07)
same all-warm base 1798.9/1981.4 652.9/692.2 19.55 (14.71–25.39)
same all-warm candidate 1717.5/1874.4 680.7/767.3 17.42 (14.80–20.07)
distinct cold base 1834.2/1834.2 620.9/620.9 24.40 (24.40–24.40)
distinct cold candidate 1941.1/1941.1 635.8/635.8 24.36 (24.36–24.36)
distinct 0 base 1912.9/1951.6 658.7/689.8 18.56 (15.76–21.86)
distinct 0 candidate 1873.1/1874.3 705.8/728.2 16.86 (16.86–16.86)
distinct 1 base 1870.0/1882.5 658.9/693.5 21.22 (18.39–26.64)
distinct 1 candidate 1838.7/1839.3 782.5/806.5 16.70 (16.63–16.86)
distinct 2 base 1727.2/1813.4 659.2/695.0 18.46 (15.04–22.45)
distinct 2 candidate 1639.0/1654.0 678.4/716.4 16.13 (14.80–18.50)
distinct 3 base 1704.4/1714.8 657.5/696.4 15.81 (13.39–17.82)
distinct 3 candidate 1656.0/1656.0 726.5/752.9 18.50 (18.50–18.50)
distinct 4 base 1686.5/1692.3 659.6/695.5 18.04 (16.32–20.71)
distinct 4 candidate 1629.3/1658.4 681.7/730.7 20.07 (20.07–20.07)
distinct all-warm base 1780.2/1951.6 658.8/696.4 18.42 (13.39–26.64)
distinct all-warm candidate 1727.2/1874.3 715.0/806.5 17.65 (14.80–20.07)

@clawsweeper

clawsweeper Bot commented Oct 26, 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 gateway Gateway runtime scripts Repository scripts size: XL labels Oct 26, 2026
@steipete
steipete marked this pull request as ready for review October 26, 2026 09:28
@chatgpt-codex-connector

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 🔄 Running since 2026-10-26T09:28:48.841523Z b5966f9 Draft marked ready
ℹ️ 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:feature Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR indexes pending approvals by session and reuses completed replay preparation while the approval store confirms its currency.

Example: 40 authenticated sockets reconnect with 10,000 pending approvals across 1,000 audiences.

  • Before: Successive subscription scheduling groups repeat pending-approval scanning and decoding.
  • After: The store selects the requested session’s approvals from its index and matching subscribers reuse prepared replay.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful performance work with no definite code defect; retained-disclosure authority evidence remains incomplete.
Proof confidence 🦐 gold shrimp (3/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) Security review found an item that needs attention.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Opening approval-enabled chats during reconnect bursts repeatedly scans unrelated pending approvals and delays subscription replies.
Reason: The maintainer-authored direction has measured user-visible value, preserves existing contracts, and adds no configuration or public API.

Merge readiness

⛔ Blocked before merge - 3 items remain

Keep open: this maintainer-authored PR addresses evidenced reconnect overhead that remains on current main; no definite introduced code defect was found.

Priority: P2
Reviewed head: b5966f91df4578cb503136f25ca0bdc02bb4383e

Before merge

  • Add real behavior proof - Authority-chain proof required: the supplied built-Gateway benchmark exercises 40 admin sockets and exact replay payloads, but does not show another reviewer or revoked authority being refused at the final response when completed replay is reused; existing lower-boundary tests support the implementation without closing that gap, and the paired warm timing and allocation results remain in progress. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Resolve security review attention item - No concrete bypass was found, but retained replay disclosure needs final-response authority proof.
  • Resolve merge risk - The PR author must demonstrate that completed replay reuse cannot disclose approval payloads to another reviewer or to a client whose disclosure authority is revoked before the response.

Findings

None.

Tests

  • Missing end-to-end proof: Show final subscription responses for a warmed replay with an authorized reviewer, a different reviewer, and authority revoked before response delivery.
Agent review details

How this fits together

Gateway message subscriptions request pending approvals from the approval store, which reads persisted rows through workers and returns sanitized replay after reviewer and session authorization.

flowchart TD
  A[Authenticated subscribers] --> B[Subscription handler]
  B --> C[Session and reviewer replay scopes]
  C --> D[Pending approval index]
  E[Database read worker] --> D
  F[Committed writer receipts] --> D
  C --> G[Final disclosure checks]
  G --> H[Subscription response]
Loading

Technical review

Best possible solution:

Keep the index under the existing approval store and complete final-response authority validation before landing.

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

Current-main source repeats pending selection after in-flight sharing ends, and the supplied real-Gateway measurements establish the reported reconnect overhead.

Is this the best way to solve the issue?

The index uses existing writer receipts and physical-owner lifecycle rather than adding another persistence owner, and the retained SQL path remains necessary for repair and maintenance.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 1a7c8156dc64.

Merge-risk options

Maintainer options:

  1. Prove cached replay disclosure at the response boundary (recommended)
    The PR author should exercise the real subscription handler and replay store with a warmed cache, checking an authorized reviewer, another reviewer, and revocation before the response; capture the final payloads or refusals.

Provenance checked

Testing

Proof path: shipped entry point.

Security

Needs attention: No concrete bypass was found, but retained replay disclosure needs final-response authority proof.

Evidence

What I checked:

PR surface

Source +482, Tests +70, Docs +2, Other +595. Total +1149 across 13 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 6 507 25 +482
Tests 3 100 30 +70
Docs 2 5 3 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 2 595 0 +595
Total 13 1207 58 +1149

Review metrics

Metric Value Why it matters
Production versus test growth Production +507/-25 LOC; tests +100/-30 LOC Growth implements the requested session index and its receipt, expiry, and owner lifecycle; added tests cover distinct reuse, ordering, isolation, and retention failures.

Labels

Label changes:

  • add P2: The measured overhead affects approval-enabled reconnects under a large backlog, without evidence of a core outage or lost data.
  • add merge-risk: 🚨 security-boundary: Completed reviewer-filtered approval payloads now survive across requests, requiring final-response proof that isolation and live revocation still hold.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: The measured overhead affects approval-enabled reconnects under a large backlog, without evidence of a core outage or lost data.
  • merge-risk: 🚨 security-boundary: Completed reviewer-filtered approval payloads now survive across requests, requiring final-response proof that isolation and live revocation still hold.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

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

  • Prove another reviewer receives no protected payload and revocation invalidates a warmed replay before the final subscription response.
  • Publish the completed paired warm timing and allocation results.

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.

Reviewed October 26, 2026, 5:46 AM ET / 09:46 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

Landing with the authorized inherited-failure exception for head 0bcfdac168ec72e2dbdecc518de847c223410028.

The sole non-aggregate failure in CI run 40302829656 is ui/src/e2e/stale-build-recovery.production.e2e.test.ts: command timed out after 120000ms: node scripts/run-node.mjs --help. It occurs during cold-build preparation, before Gateway startup or the browser assertion. The aggregate CI gate fails because of that job; all other selected jobs passed, including security-fast and the repaired probe lint.

I reproduced the same cold-build deadline failure on the CI merge's unchanged main parent, 1b6eb7bf2e7f7cb31ce9ee71c6f1d778f7ee6ce0, using blacksmith-testbox lease tbx_01m5vtf6p6m6wfkxy28b3yjdxr: Linux x64, Node 24.22.1, four CPUs, 15.4 GiB RAM, isolated synthetic state, cold dist. Exact command: taskset -c 0,1,2,3 node scripts/run-vitest.mjs run --config test/vitest/vitest.ui-e2e.config.ts --configLoader runner ui/src/e2e/stale-build-recovery.production.e2e.test.ts --maxWorkers=1. Result: exit 1 with the same 120000ms deadline; 211.5 seconds total remote command. The lease is stopped. Test, suite/Gateway helpers, CLI launcher and Vitest configuration are unchanged; an independent reviewer confirmed the attribution. An earlier Mac baseline passed, so this is host-sensitive. The failed UI assertion is not claimed as passing proof.

Independent review is clean. The approval change passes 58 focused/sibling tests, 26 closure tests, targeted core typecheck, lint and format, plus 24,320 exact ordered replay checks on isolated built Gateways. No CI rerun was requested. The PR body contains the full reconnect latency, CPU, allocation, memory and host-load evidence.

Native workspace preflight resolves to a separate canonical checkout outside this lane; the lane explicitly forbids creating or using that checkout. Landing therefore uses the task-authorized exact-head squash API fallback. No required product review or security gate is waived.

@steipete
steipete merged commit 0bde641 into main Oct 26, 2026
135 of 138 checks passed
@steipete
steipete deleted the steipete/perf-approval-replay-session-index-20261026 branch October 26, 2026 10:10
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
…184453)

* perf(approvals): index and reuse pending replay by session

* fix(perf): keep diagnostic IPC listener synchronous
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 merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: XL status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant