Skip to content

perf(gateway): remove redundant chat admission metadata reads - #158586

Merged
steipete merged 5 commits into
mainfrom
perf/l40-admission-prepared-read
Sep 26, 2026
Merged

steipete merged 5 commits into
mainfrom
perf/l40-admission-prepared-read

Conversation

@steipete

@steipete steipete commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

chat.send reloads and decodes full session metadata in provisional lifecycle validation before reading it again under the writer barrier.

User Impact

Normal pre-admission consumes the prepared session entry. The lifecycle precheck retains live caller, physical-target, and in-memory exclusive-work checks. A single fresh metadata read remains under the authoritative writer barrier, alongside the existing settings, expected-leaf, session identity, cancellation, retry, and registration checks.

Why This Change Was Made

The provisional metadata reload duplicates the authoritative read. Removing it also removes the conditional provisional/commit branches. The small warm reads remain synchronous under the campaign's measured-CPU rule; no new worker traffic, cache, configuration, protocol, schema, or durability behavior is introduced.

Related: #158026 adds admitted-entry plumbing in the same helper for SSH attachments. This change keeps metadata capture inside the final commit callback; that PR's admittedSessionEntry assignment belongs there unconditionally once the changes are combined. It does not duplicate the attachment fix.

Evidence

  • The real-storage regression fails on the original code's two metadata reads and passes with at most one. It changes permissions after initial preparation and confirms admission sees the fresh value. Negative control: 35.41 s wall.
  • Fresh metadata, physical identity, pending Stop, native profile, and orphaned recovery group: 43 passed, 88.10 s wall on the final head. Settings/delete/reset and concurrent/durable-retry races: 5 passed, 21.41 s wall on the final head.
  • New file alone: node scripts/run-vitest.mjs src/gateway/server-methods/chat-send-admission.reads.test.ts --maxWorkers=1: 1 passed, 10.94 s wall.
  • The original cheap early busy refusal remains before waiting for the writer. Independent Codex autoreview (P0–P2) is scoped-clean on e05999278be68d38527769413394a8afafb599d4, with no accepted/actionable findings.
  • Final-head changed-lane gate passed in 1630.33 s (27 min 10 s), including production types, all 25 core test graphs, dead-export scans, lint (zero warnings/errors), and repository guards. The complete remote proof batch took 1815.644 s (30 min 16 s).
  • Production: +35/-55 (−20); tests: +81/-0 (+81). Diff-scoped deslop completed.

Interleaved A/B, seven warm waves per size, 50 synthetic sessions, actual Gateway exact readers: three baseline loads versus initial preparation plus one final read. Canonical per-agent store, Node 26.9.0 / Apple M3 Ultra, shared host load around 39. CPU uses process.threadCpuUsage().

Saved skills prompt Main-thread CPU median before → after Wall median before → after
0 KiB 13.240 → 9.246 ms 13.332 → 9.291 ms
32 KiB 18.235 → 11.841 ms 18.321 → 11.868 ms
128 KiB 31.576 → 20.543 ms 31.741 → 20.723 ms

That is 30–35% less main-thread CPU and wall time. Foreground statements fall 550 → 350 in every wave. These are component measurements, not ACK/first-token latency or a claim that all baseline reads occur in one event-loop turn.

Final-head proof uses Crabbox / Blacksmith Testbox tbx_01m3e1dx8ysh9n4zegtyh4fjm3, environment run, with focused node scripts/run-vitest.mjs … --maxWorkers=1 calls and node scripts/check-changed.mjs --base b4d60fa5edfc2713a5cb3643fa1c936fec3c3cd0. The previous lease (tbx_01m3dtdst4g4wh2e9gyzgzjcje) finished during its gate after 14 typecheck groups; that interrupted gate is not counted as passed. It was closed before acquiring the replacement. Only one box was active. The final command exited 0; optional external-portal reporting emitted a nonfatal timeout after completion. No full build or swarm run was needed for the read-count change.

@clawsweeper

clawsweeper Bot commented Sep 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 gateway Gateway runtime size: L labels Sep 26, 2026
@steipete
steipete marked this pull request as ready for review September 26, 2026 02:36
@steipete
steipete marked this pull request as draft September 26, 2026 03:34
@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 26, 2026, 1:41 AM ET / 05:41 UTC (Revision 4).

ClawSweeper review

What this changes

Gateway chat admission replaces a full early session metadata check with a smaller check, retains one fresh read under the session writer barrier, and adds a SQLite read-count regression test.

Merge readiness

✅ Ready for maintainer review

Keep open. Current main still performs the redundant admission read, and the pinned PR has credible proof for the focused optimization with no identified correctness or security blocker.

Priority: P2
Reviewed head: e05999278be68d38527769413394a8afafb599d4

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused reduction has credible real-storage and component proof, with no concrete introduced defect found.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The changed Gateway admission owner was exercised from session preparation through final admission against real SQLite on a Testbox: the reported baseline control saw two metadata reads, while the final head admitted a freshly changed permission with at most one read. Separate measurements support a component performance gain; no stored-data format changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The changed Gateway admission owner was exercised from session preparation through final admission against real SQLite on a Testbox: the reported baseline control saw two metadata reads, while the final head admitted a freshly changed permission with at most one read. Separate measurements support a component performance gain; no stored-data format changes.
Evidence reviewed 9 items Introduced admission change: The introduced diff replaces the provisional full validation with caller, physical-target, and exclusive-work checks; final revalidation remains under the writer barrier.
Final admission authority: The final callback reloads the session, checks request conflicts and expected settings, rejects changed session identity, and then registers the abortable run.
Writer-barrier ordering: The lifecycle owner calls the inexpensive initial check before its session-store writer barrier and calls revalidation inside that barrier.
Findings None None.
Security None None.

How this fits together

The Gateway receives a chat send request with a prepared session and decides whether to admit an agent run or return a rejection or replay. Its session writer barrier orders the final check against current session metadata before run registration.

flowchart LR
 A[Chat client] --> B[Gateway send request]
 B --> C[Prepared session]
 C --> D[Early admission check]
 D --> E[Session writer barrier]
 E --> F[Fresh session check]
 F --> G[Run or response]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -20, Tests +81. Total +61 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 35 55 -20
Tests 1 81 0 +81
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 116 55 +61

Review metrics

Metric Value Why it matters
Admission metadata reads 2 → at most 1 after preparation This measures the foreground metadata work removed while the final fresh read remains.

Technical review

Best possible solution:

Retain one fresh, writer-ordered session validation while preserving early busy refusal, live caller checks, cancellation, and run registration.

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

Yes. Current-main source performs both metadata checks, and the PR reports a real-SQLite negative control; this read-only review did not rerun it.

Is this the best way to solve the issue?

Yes. The existing writer barrier remains the authority point, and the early check can stay inexpensive without introducing another cache or contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 20f3170d3672.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This is a bounded Gateway performance improvement without evidence of an urgent user-facing failure.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The changed Gateway admission owner was exercised from session preparation through final admission against real SQLite on a Testbox: the reported baseline control saw two metadata reads, while the final head admitted a freshly changed permission with at most one read. Separate measurements support a component performance gain; no stored-data format changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The changed Gateway admission owner was exercised from session preparation through final admission against real SQLite on a Testbox: the reported baseline control saw two metadata reads, while the final head admitted a freshly changed permission with at most one read. Separate measurements support a component performance gain; no stored-data format changes.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Shakker: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (3 earlier review cycles)
  • reviewed 2026-09-26T03:42:53.540Z sha 259e885 :: blocked before merge. :: [P2] Skip stale admissions before the worker read
  • reviewed 2026-09-26T04:56:00.473Z sha e059992 :: blocked before merge. :: [P2] Measure the complete chat admission path
  • reviewed 2026-09-26T05:02:06.322Z sha e059992 :: needs changes before merge. :: none

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 26, 2026
@steipete steipete changed the title perf(gateway): move chat admission reads off the main thread perf(gateway): remove redundant chat admission metadata reads Sep 26, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 26, 2026
@steipete
steipete marked this pull request as ready for review September 26, 2026 05:35
@steipete
steipete merged commit 7dc4905 into main Sep 26, 2026
290 of 291 checks passed
@steipete
steipete deleted the perf/l40-admission-prepared-read branch September 26, 2026 07:49
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 27, 2026
…aw#158586)

* perf(gateway): prepare chat admission reads off thread

* refactor(gateway): validate chat admission once under its barrier

* test(gateway): await native chat session preparation

* perf(gateway): remove redundant provisional session reads

* fix(gateway): preserve early exclusive admission refusal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant