Skip to content

perf(gateway): share concurrent pending approval replay - #156358

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-messages-subscribe-approval-replay-20260919
Sep 23, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-messages-subscribe-approval-replay-20260919

Conversation

@steipete

@steipete steipete commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Related: #154619.

What Problem This Solves

Concurrent chat opens repeat the same pending-approval expiry and scan work, while two nested revision loops can keep subscription acknowledgments waiting during approval changes.

User Impact

Subscribers with the same session and reviewer share in-flight replay preparation. Replay contents and per-client authorization stay unchanged. A subscription retries a changed snapshot once; if that attempt also becomes stale, it returns UNAVAILABLE and rolls back its provisional subscription so the client can retry.

Why This Change Was Made

The existing approval session event runtime owns the shared preparation and clears it on lifecycle publication or settlement. The handler owns the single retry. This adds 26 net production lines and no configuration, schema, dependency, or retained cache.

Only in-flight work is shared: the current publication revision is observational, not a durable store revision, so retaining completed snapshots could miss durable changes. Different sessions and reviewer bindings remain separate, and each caller rechecks its own authority after the shared await. Expiry still commits and publishes its tombstones before replay.

Evidence

Real sessions.messages.subscribe handler, real approval worker/store, 200 pending approvals, three concurrent admin subscribers, 30 measured batches (90 subscriptions), one warm-up batch. Latency is captured in the response callback; golden result assertions run outside the timed batch.

Metric Baseline Candidate
Subscribe p50 16.41 ms 5.49 ms
Subscribe p90 25.69 ms 7.32 ms
Pending store scans per subscribe 1 0.333
Explicit expiry calls per subscribe 1 0.333

Instrumented worker-await time per subscriber fell from 4.46 to 0.415 ms for expiry and 12.36 to 1.40 ms for pending selection. Shared waits are attributed once per preparation; these are elapsed waits, not CPU time.

The scan and expiry numbers count store API operations, not individual SQL statements. Both variants use the same synthetic data and exact replay expectations. The bounded-retry regression fails on the original handler and passes on the candidate.

Provider: blacksmith-testbox, profile openclaw-check. Initial lease tbx_01m36hh79jd3c6m8jsv3tcb5ey; initial Testbox run.

The original lease was stopped after two checksum-sync timeouts prevented the controlled comparison from starting. Replacement lease tbx_01m36kg5thcn6s1gjnpmsjrjmk installed dependencies successfully and ran both measured variants in one synced invocation.

  • Initial test invocation stopped before tests because remote dependencies were absent; pnpm install --frozen-lockfile repaired the lease.
  • Baseline rig reproduced one scan and one expiry per subscriber; sharing regression failed as expected.
  • node scripts/run-vitest.mjs src/gateway/server-methods/sessions.messages-subscribe-perf.test.ts src/gateway/server-methods/sessions.messages-subscribe-approvals.test.ts src/gateway/operator-approval-session-events.test.ts src/gateway/operator-approval-store.worker.test.ts: 36 tests passed. Covers replay equality, per-client revocation, session/reviewer isolation, expiry reconciliation/publication, stale snapshots, rollback, and off-thread storage.
  • node scripts/check-changed.mjs: initial gate found a missing explicit type on the new fixture array; fixed. A subsequent gate exposed the Testbox’s divergent origin/main reference (unrelated line-cap failures). Read-only inspection confirmed its materialized HEAD is the exact local baseline 5e5f9c27559, with only these six files changed. node scripts/check-changed.mjs --base HEAD -- <production/docs paths> and the corresponding test-path invocation: production/docs gate passed, including all 25 test typecheck graphs. The test-only gate passed its typechecks and guards, then found a missing if brace; fixed. The same targeted lint command (node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.core.json <three test files>) passed on the final source; unchanged successful checks were reused.
  • Same-invocation baseline/candidate comparison with pnpm test src/gateway/server-methods/sessions.messages-subscribe-perf.test.ts src/gateway/server-methods/sessions.messages-subscribe-approvals.test.ts --maxWorkers=1 -t 'shares prepared approval replay|rolls back after one retry': baseline failed the retry regression as expected (3 reads versus the bound of 2), while its golden replay passed; candidate passed both tests. Total command walls 28.45 s / 20.50 s, including worker compilation.
  • Final source: targeted lint and pnpm test <three changed test files> --maxWorkers=1 passed all 28 tests (52.62 s wrapper wall).
  • Per-file costs, each measured with pnpm test <file> --maxWorkers=1: operator-approval-session-events.test.ts 24.70 s (13 tests); sessions.messages-subscribe-approvals.test.ts 12.23 s (14 tests); sessions.messages-subscribe-perf.test.ts 13.00 s (1 test). All passed; command walls include transform/import startup.
  • Hosted CI run 35837828628 succeeded on 07487b7bfeba69b38035797b123a09c322a6322d: 1,890 s elapsed including queue time (08:33:49–09:05:19 UTC). All compact Node test jobs and the native openclaw/ci-gate check passed. Per-file CI seconds are not available in a timing/plan artifact; the measured per-file walls above are from Testbox.
  • Independent Codex autoreview: no actionable P0–P2 findings. Changed-file formatting and git diff --check passed.

A later full-suite candidate run measured 10.28/15.41 ms p50/p90 with the same 1/3 scan and expiry counts; the standalone cost run measured 9.11/15.99 ms. Latency is load-dependent. The table above uses the controlled paired comparison.

Gaps: this isolated rig does not reproduce the reported production median, and no live Gateway was changed or benchmarked. Sequential subscribers still acquire a fresh worker snapshot. chat.startup already uses prepared session facts and does not duplicate approval replay; restricted-role subscribers can still perform separate synchronous exact session-access lookups outside this change.

Share in-flight approval snapshots by session and reviewer, retain per-client authority checks, and bound subscription replay to one reread. Preserve expiry publication and roll back provisional subscriptions when replay remains stale.
@clawsweeper

clawsweeper Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime size: M labels Sep 23, 2026
@steipete
steipete merged commit 78cd580 into main Sep 23, 2026
204 of 205 checks passed
@steipete
steipete deleted the steipete/perf-micro-messages-subscribe-approval-replay-20260919 branch September 23, 2026 09:14
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 24, 2026
)

Share in-flight approval snapshots by session and reviewer, retain per-client authority checks, and bound subscription replay to one reread. Preserve expiry publication and roll back provisional subscriptions when replay remains stale.
@roboclaw-bot roboclaw-bot mentioned this pull request Sep 24, 2026
2 of 8 tasks
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 size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant