Skip to content

perf(gateway): trim redundant startup session reads - #169138

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-startup-prep-ack-concurrency-20260919
Oct 11, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-startup-prep-ack-concurrency-20260919

Conversation

@steipete

@steipete steipete commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Concurrent first messages repeat database-ordered session reads that do not establish an additional guarantee, adding work to an already contended startup path.

User Impact

Reduces unnecessary startup work and permits non-steering queued input to proceed without an unrelated recovery-metadata read. Session FIFO, cancellation, durable-input custody, and current caller/membership checks remain at their existing effect boundaries.

Why This Change Was Made

The pending chat reservation checks caller authority. Final writer-ordered admission then reads current session membership, checks authority and exclusivity, and registers the run synchronously. The intermediate lifecycle check now checks only the retained session target.

The reply runner reads recovery metadata only for channel-source deduplication or active steering. Ordinary source-less, non-steering turns do not consume that row. Their later durable-input admission still performs its required current read.

Production LOC delta: −4. Including the test-routing metadata, non-test code is −3 lines. No new cache, configuration option, schema, or compatibility path.

Evidence

Real isolated Gateway, real sessions.create followed by first chat.send, synthetic loopback Responses API, Node 26.8.1 on an 8-core Linux rig. The fixture uses the existing maxConcurrent: 20 setting. All state is isolated; no live deployment or real account data was used.

Paired baseline d1618ccd0b276f985711ff297b1b87a77bdad35d and candidate, warmed series, p50 / p95 milliseconds:

Stage Concurrency / samples Before After
Preparation 1 / 6 253.3 / 272.9 236.8 / 246.5
Preparation 20 / 60 5058.9 / 6582.4 4721.9 / 5497.4
Client-observed ACK 1 / 6 109.4 / 374.3 112.4 / 412.8
Client-observed ACK 20 / 60 1552.3 / 1671.9 1509.7 / 1644.4

A preceding 60-start burst series measured preparation 5088.1 / 5991.5 → 5117.6 / 5758.8 ms and ACK 1458.5 / 1667.9 → 1623.7 / 1799.6 ms. Preparation p95 improved in both series (4% and 16%); ACK variation is mixed, so this PR claims no reliable ACK latency improvement and no 1.5× overall speedup. Required database-ordered preparation remains the dominant cost.

The Gateway benchmark uses source imports and the normal compiled-worker preparation. The measurement replay reuses previously built E2E package artifacts through the repository's supported OPENCLAW_E2E_SKIP_BUILD=1 path.

Validation provider: blacksmith-testbox, lease tbx_01m4nc5tbes89fesg9y9a0g195:

  • node scripts/run-vitest.mjs src/auto-reply/reply/agent-runner-recovery-read.test.ts — 3 passed. The queued-input regression fails on unchanged baseline with the intended recovery-read error. File wrapper time 38.68 s; first cold test 11.67 s, other cases 4/3 ms. This exercises the actual reply runner rather than adding a production-only test seam.
  • node scripts/run-vitest.mjs src/auto-reply/reply/agent-runner.runreplyagent.e2e.test.ts src/auto-reply/reply/agent-runner-core.test.ts src/auto-reply/reply/restart-recovery-claim.lifecycle.test.ts src/gateway/server-methods/chat-send-admission-authority.test.ts src/gateway/server-methods/chat-send-admission.reads.test.ts src/gateway/server-methods/chat-send-ordering.test.ts — 282 passed; 387.17 s wrapper time.
  • node --import ./scripts/tsx.mjs scripts/check-export-name-collisions.mts — passed.
  • node scripts/run-vitest.mjs test/scripts/eager-import-closure.test.ts test/scripts/pr-wrapper-source-closure.test.ts test/scripts/ci-node-test-plan.test.ts test/scripts/ci-node-test-plan.commands.test.ts — 143 passed.
  • node scripts/check-changed.mjs --staged — all preceding guards, formatting, core types and all 28 core-test type graphs passed; stopped at the unrelated unused-export scan.

Rig openclaw-node-01:

  • node scripts/run-vitest.mjs src/gateway/gateway.test.ts with the isolated benchmark fixture — both variants passed; JSON measurements collected.
  • Native remaining core/scripts lint completed; two unused imports left by moving the recovery test were removed. node scripts/run-oxlint.mjs --tsconfig tsconfig.core.json src/gateway/server-methods/chat-send-admission.ts src/auto-reply/reply/agent-runner-run.ts src/auto-reply/reply/agent-runner-recovery-read.test.ts src/auto-reply/reply/agent-runner.runreplyagent.e2e.test.ts — 0 warnings, 0 errors. Scripts lint also passed.
  • node --import ./scripts/tsx.mjs scripts/check-deadcode-exports.mts on the unchanged baseline reproduces the unused UI exports and loadSubagentMaintenanceCandidatesInDatabase finding. None of those files or exports is changed here.

Independent Codex reviews, including the test-only CI follow-up: scoped-clean, no actionable P0–P2 findings.

CI follow-up

The first PR run exposed two timing-dependent assertions in the existing admission-authority tests. On the unchanged CI base 89564fa3a101b437a485fbe501dd99281c9402ba, both fail identically (2 failed / 4 passed; 145.92 s wrapper time). Database closure can be observed as a handled {ok:false} denial, or reach a reader after its synchronous consumption has finished. The tests now assert denied admission and preserve reservation cleanup, same-ID retry, callback custody, all lease cleanup, and no dispatch. A rejected reader must still propagate its exact error. No production behavior was changed for these test repairs.

Rig openclaw-node-01, Node 24.19.0, isolated checkout at that CI base plus this PR:

  • pnpm install --frozen-lockfile --prefer-offline — passed, 5.4 s.
  • node scripts/run-vitest.mjs src/gateway/server-methods/chat-send-admission-authority.test.ts on the unchanged base — reproduced both CI failures.
  • node scripts/run-vitest.mjs src/gateway/server-methods/chat-send-admission-authority.test.ts src/gateway/server-methods/chat-send-admission.reads.test.ts src/gateway/server-methods/chat-send-ordering.test.ts src/gateway/server-methods/chat-send-pre-admission.test.ts src/gateway/server-methods/chat-send-work-admission.test.ts — 60 passed across five files, 205.74 s wrapper time.

The first CI run also failed on unrelated Solid panel attribute/type declarations in unchanged UI files. Those are separate from the admission test repair.

The updated exact-head CI run also exposed an unrelated chat-panel-layout.e2e.test.ts terminal-canvas visibility timeout. On unchanged CI base ebdc7d54c0842d96c4bb2fb2bb4be4758f07f255, the same file reproduces the same failure at line 379 (1 failed / 1 passed). It uses a mocked Gateway and does not execute the changed admission code. The rig changed-code gate passed its preceding guards and core production types, then stopped at the unchanged UI tab type errors from the earlier base; the focused gateway-method type graph reports those same UI errors.

The final CI run also fails test/scripts/managed-child-process.windows.test.ts:83 because the unchanged helper exports stopWindowsGatewayProcess while the test calls stopGatewayProcess. Running node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.test.root.scripts.json --noEmit on unchanged ebdc7d54c0842d96c4bb2fb2bb4be4758f07f255 reproduces the identical TS2339 error. Both remaining failures are independently baseline-proven; all changed core test shards, core lint, production types, security, and architecture/source guards passed on exact head 340cace36eb93ac3519b8f2b2a7f3ba7cee85cc0. No CI reruns were requested.

A supplementary rig lint invocation stopped while preparing plugin SDK declarations (300-second artifact-preparation timeout); exact-head CI core lint passed and supplies that check. The rig was not retried.

Keep the final writer-ordered authority and exclusivity check while removing the intermediate chat admission read. Read recovery metadata only when channel-source deduplication or active steering needs it.

Preserve queue admission and recovery failure coverage. Synthetic concurrent-start measurements show a modest preparation tail improvement; ACK variation remains mixed.
@clawsweeper

clawsweeper Bot commented Oct 11, 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: S labels Oct 11, 2026
@steipete
steipete marked this pull request as ready for review October 11, 2026 13:57
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Oct 11, 2026
@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR removes a redundant chat-admission read and limits reply recovery reads to source deduplication and active steering.

Example: A source-less webchat message queues behind an active run without requesting steering.

  • Before: An unnecessary recovery-metadata read can reject the message before queueing.
  • After: The message can enter the follow-up queue while execution retains its required durable-input checks.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct simplification with supplied real-Gateway measurements and meaningful regression coverage; no new execution or second review was performed.
Proof confidence 🐚 platinum hermit (4/6) This maintainer-authored PR is outside the ordinary contributor proof gate; supplied evidence exercises real isolated Gateway sessions.create/chat.send with a loopback provider, and the follow-up changes only closure tests while retaining production bytes.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Concurrent first messages perform unnecessary database work, and ordinary queued input can fail on recovery metadata it does not consume.
Reason: The feature owner's direction has scoped measurements, preserves existing product contracts, and adds no public option, schema, or compatibility path.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep this PR open: it provides a useful, feature-owner-authored simplification absent from current main, with no actionable introduced defect found.

Priority: P2
Reviewed head: 340cace36eb93ac3519b8f2b2a7f3ba7cee85cc0

Before merge

  • Complete next step - Attribute the current Control UI terminal-canvas timeout using its diagnostics and matching baseline evidence before landing; do not assume the historical UI type failures explain it.

Findings

None.

Agent review details

How this fits together

Gateway admission converts authorized chat requests into registered runs; the reply runner then selects steering, queueing, or execution against canonical session storage.

flowchart TD
A[Chat request] --> B[Pending reservation]
B --> C[Session lifecycle admission]
C --> D[Writer ordered authority check]
D --> E[Registered run]
E --> F[Reply runner]
F --> G[Steering or followup queue]
F --> H[Durable input admission]
Loading

Technical review

Best possible solution:

Retain this focused read reduction while preserving authoritative admission and fresh recovery reads at their consuming boundaries.

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

Current-main source establishes both unnecessary reads, and supplied baseline validation reports the intended queued-input failure; this read-only review did not execute a reproduction.

Is this the best way to solve the issue?

The narrow removal is appropriate because required consumers retain fresh reads and final admission preserves current authority, exclusivity, and session identity.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Reply recovery reads changes intended behavior with a stated reason (a682168: Move recovery reads off the Gateway thread while preserving duplicate handling, physical ownership, and failure cleanup.)
  • Intermediate chat admission check changes intended behavior with a stated reason (00daeeb: Keep asynchronous chat preparation bound to current caller, membership, and physical-session authority.)
  • Database closure test assertions changes intended behavior with a stated reason (17e945f: Restore runtime and test checks; the revised tests preserve denial and cleanup while accepting both legitimate closure outcomes.)
  • Database-worker test routing keeps the original intent (a682168: Route worker-backed recovery tests through their database-worker owner.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit a682168 adds src/auto-reply/reply/agent-runner-run.ts:175 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: a68216889f19; files: src/auto-reply/reply/agent-runner-run.ts)

PR surface

Source -4, Tests +68. Total +64 across 6 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 2 6 -4
Tests 4 111 43 +68
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 113 49 +64

Review metrics

Metric Value Why it matters
Production versus tests Production −4 LOC; tests +67 LOC; test routing +1 LOC Production shrinks while focused regression coverage and closure-denial assertions preserve existing contracts without adding a production test seam.

Labels

Label changes:

  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.
  • remove status: 👀 ready for maintainer look: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P2: A bounded startup-performance improvement also avoids rejecting ordinary queued input on an unused recovery read.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

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 need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-11T14:03:36.265Z sha d107fcd :: needs maintainer review before merge. :: none

Reviewed October 11, 2026, 10:48 AM ET / 14:48 UTC (Revision 2).

@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 11, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Landing under the authorized inherited-failure exception for exact head 340cace36eb93ac3519b8f2b2a7f3ba7cee85cc0. CI run 38148151663 has two unrelated failures, both reproduced on its unchanged main baseline ebdc7d54c0842d96c4bb2fb2bb4be4758f07f255 on the isolated Linux rig:

  • chat-panel-layout.e2e.test.ts:379: terminal canvas stays hidden, same failing case and selector (1 failed / 1 passed on baseline). This mocked-Gateway layout test does not execute the changed admission path.
  • managed-child-process.windows.test.ts:83: TS2339, calls testing.stopGatewayProcess while the baseline helper exports stopWindowsGatewayProcess. The root-scripts typecheck reproduces it.

Changed core test shards, core lint, production types, security, architecture and source-contract guards passed on this exact head. The focused admission/ordering/custody run passed 60 tests; earlier Testbox runner/recovery coverage passed 282 tests and closure/CI-plan guards passed 143. Independent review is clean. No rerun, update-branch, or CI dispatch was used to chase green.

@steipete
steipete merged commit 5f93ba3 into main Oct 11, 2026
138 of 142 checks passed
@steipete
steipete deleted the steipete/perf-micro-startup-prep-ack-concurrency-20260919 branch October 11, 2026 15:02
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 12, 2026
* perf(gateway): trim redundant startup session reads

Keep the final writer-ordered authority and exclusivity check while removing the intermediate chat admission read. Read recovery metadata only when channel-source deduplication or active steering needs it.

Preserve queue admission and recovery failure coverage. Synthetic concurrent-start measurements show a modest preparation tail improvement; ACK variation remains mixed.

* test(gateway): assert admission denial across reader closure timing
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. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant