Skip to content

perf(agents): reduce approval tracking across concurrent runs - #136205

Merged
steipete merged 1 commit into
mainfrom
codex/perf-approval-wait-run-routing
Sep 2, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-approval-wait-run-routing

Conversation

@steipete

@steipete steipete commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Approval tracking adds work to every concurrent run's streamed events, even when the event belongs to another run. This is the remaining approval-wait listener left on the global path after #129174.

Why This Change Was Made

Subscribe through the existing run-indexed event API. The approval owner keeps its run/session guards, overlapping approval tracking, timeout-pause arithmetic and disposal. The dispatcher already preserves registration order, nested delivery and listener mutations; no new dispatcher, cache, configuration or authority path is added.

User Impact

Concurrent agent runs spend less time dispatching events to unrelated approval observers. This is a dispatch improvement, not a measured reduction in provider or whole-turn latency. Production LOC: +2/-2 (net 0); tests unchanged. This completes a part of Tran Quang's original optimization proposal in #129174.

Evidence

All 100 existing tests pass on both versions across the complete agent-events, run-listeners, Code Mode wait and embedded timeout preparation files. Changed-source checks pass. Independent native proof checked 16 control cases on each version, including overlapping approvals, cancellation, timeout/grace resumption, nested events, disposal, owner claims and lifecycle generation changes. No new authority semantics are claimed.

The fixed before/candidate/candidate/before benchmark retained all 912 calls (768 measured samples, plus every first/warm call), with real event emission and approval observers. All prospective median gates passed in both orders:

Workload Forward median Reverse median
8 concurrent runs 21.55% faster 19.83% faster
16 concurrent runs 41.27% faster 42.03% faster
Register + dispatch + dispose, 8 runs 20.67% faster 21.27% faster
Approval lifecycle traffic, 8 runs 19.58% faster 18.29% faster

Registration/disposal alone was slower by 0.75–0.85 microseconds per eight observers (9.26–10.76%); the complete lifecycle saved 34.4–34.6 microseconds. That setup row was designated descriptive before data collection. Small-workload adverse medians remained below 3%; first-call and tail results were mixed, including a 173% forward p95 increase in the paired-observer row that did not repeat in reverse. No tail-latency improvement is claimed. No samples, failures or warmups were discarded and there were no timing retries.

The internal helper has no runtime retargeting contract: artificially replacing params.runId after subscription differs from the former global callback. Actual callers supply per-run inputs and never retarget them. Generation fencing remains with the existing emitter and lifecycle owner. Canonical tests, the exact native output comparison and independent source review cover those boundaries.

Local proof used node scripts/run-vitest.mjs run with the canonical infra, agents-core and agents-embedded-agent-run configurations, followed by node scripts/check-changed.mjs. The native benchmark exercised the actual owner without a live provider. The campaign separately ran real OpenAI dev-Gateway turns and web requests; those unpaired diagnostics are not attributed to this change. Raw benchmark and closure artifacts are retained locally. Fresh isolated Codex autoreview found no blocking findings; exact PR-head CI is required before landing.

Maintainer disposition of review evidence risk

The ClawSweeper review correctly notes that its reviewer could not access the local raw benchmark artifacts. I reviewed the complete fixed cohort and an independent agent recomputed the measurements from all retained raw calls, checked source/build identities, and verified cleanup. The aggregate result is identified by SHA-256 5a3f106e58ffaa7d96b8eb3673365004ba692322363a31651499495e19ad70b5; the independent audit is identified by 70eefd9016eed75bdb4bbbdd8bb55f45ccdbde72c630d040f9e626e2395512b8.

I accept the evidence-access limitation for this two-line internal consumer conversion: raw artifacts remain local, so these are maintainer-verified measurements, not publicly reproducible or ClawSweeper-verified results. The change's correctness is separately covered by the existing lifecycle suites and source review. No wider latency claim or benchmark retry is used to justify landing; normal exact-head CI still must pass. The unrelated scan-history authentication failure was repaired and merged in #136232. This branch was mechanically rebased onto that repair (main 9fda3b1); the approval owner, dispatcher, callers and canonical tests are unchanged from the measured sources. All 100 canonical tests were rerun successfully on refreshed head aae2c15, and a fresh isolated Codex review found no blocking findings. The historical measurements above remain bound to their original experiment; the rebase is not a new benchmark.

@clawsweeper

clawsweeper Bot commented Sep 2, 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 September 2, 2026 09:24
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS maintainer Maintainer-authored PR labels Sep 2, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Sep 2, 2026
@clawsweeper

clawsweeper Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 2, 2026, 7:08 AM ET / 11:08 UTC.

ClawSweeper review

What this changes

The PR changes approval-wait tracking to subscribe only to lifecycle events for its own agent run, avoiding unrelated concurrent-run callbacks.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

The conversion uses the dispatcher API intended for listeners that discard other runs and keeps the approval observer’s lifecycle and session guards intact; no actionable patch defect was found. This PR remains necessary because current main still uses the global listener, and it should stay open pending a completed green exact-head CI rollup.

Priority: P3
Reviewed head: aae2c15f9f87879995d5e41d21726e2654d0fe57

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A small, coherent reuse of the existing per-run dispatcher contract with relevant existing behavioral coverage.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored internal dispatcher optimization is exempt from the external-contributor proof gate; the supplied benchmark artifacts remain locally retained and were not independently assessed here.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored internal dispatcher optimization is exempt from the external-contributor proof gate; the supplied benchmark artifacts remain locally retained and were not independently assessed here.
Evidence reviewed 5 items Current-main behavior: Current main still registers the approval observer through the global listener, so the central change is not already implemented or superseded.
Existing dispatcher contract: The run-indexed listener is the existing dispatcher surface explicitly intended for listeners that discard events from other runs; the PR does not modify its ordering, mutation, or unsubscribe implementation.
Behavioral coverage: Existing tests cover run-only delivery, mixed listener order, listener mutations, nested emission, and approval timeout pause/resume while ignoring foreign-run and foreign-session lifecycle events.
Findings None None.
Security None None.

How this fits together

Agent runs emit lifecycle events through the shared event dispatcher. Approval-wait tracking consumes those events to pause and resume timeout scheduling for the associated run.

flowchart LR
  A[Agent run] --> B[Lifecycle event]
  B --> C[Shared event dispatcher]
  C --> D[Run-indexed listener]
  D --> E[Approval wait observer]
  E --> F[Timeout scheduling]
Loading

Before merge

  • Resolve merge risk (P1) - The supplied exact-head CI rollup is still incomplete, leaving final integration verification unresolved before merge.
  • Complete next step (P2) - Wait for the exact current head to complete with a green CI rollup before merge.
Agent review details

Security

None.

PR surface

Source 0. Total 0 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 2 2 0
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 2 2 0

Review metrics

Metric Value Why it matters
Production vs test LOC production +2/-2 (net 0); tests unchanged The change replaces one internal subscription call without expanding production surface or adding a new configuration or compatibility path.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the existing run-indexed subscription conversion and merge only after the exact current head has a completed green CI rollup.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the existing run-indexed subscription conversion and merge only after the exact current head has a completed green CI rollup.

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

Not applicable: this is an internal performance optimization rather than a report of broken existing user behavior. The supplied timing artifacts are not independently available, while source inspection establishes the dispatch-path change.

Is this the best way to solve the issue?

Yes: the PR reuses the dispatcher’s canonical per-run listener rather than adding filtering, caching, or a parallel event path.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P3: This is a bounded internal dispatch-performance improvement with no reported user-facing regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This maintainer-authored internal dispatcher optimization is exempt from the external-contributor proof gate; the supplied benchmark artifacts remain locally retained and were not independently assessed here.

Evidence

What I checked:

  • Current-main behavior: Current main still registers the approval observer through the global listener, so the central change is not already implemented or superseded. (src/agents/agent-run-approval-wait.ts:29, dae372c49c12)
  • Existing dispatcher contract: The run-indexed listener is the existing dispatcher surface explicitly intended for listeners that discard events from other runs; the PR does not modify its ordering, mutation, or unsubscribe implementation. (src/infra/agent-events.ts:447, aae2c15f9f87)
  • Behavioral coverage: Existing tests cover run-only delivery, mixed listener order, listener mutations, nested emission, and approval timeout pause/resume while ignoring foreign-run and foreign-session lifecycle events. (src/agents/embedded-agent-runner/run/attempt-timeout-prepare.test.ts:164, aae2c15f9f87)
  • Approval-wait provenance: The approval-wait owner was introduced as a new file by the earlier Code Mode approval repair; the current PR changes only its subscription call. (src/agents/agent-run-approval-wait.ts:10, d1177a78825d)
  • Exact-head verification status: The supplied exact-head check rollup contains completed successes but remains incomplete with queued and in-progress jobs, so it does not yet establish the repository-required full CI gate. (aae2c15f9f87)

Likely related people:

  • Peter Steinberger: Raw commit d1177a7 adds src/agents/agent-run-approval-wait.ts:10 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: d1177a78825d; files: src/agents/agent-run-approval-wait.ts)
  • Tran Quang: 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-02T09:32:56.326Z sha 36fead4af74efd50fb12c57ccbd1ea539b9b79c8 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-02T09:59:55.053Z sha 36fead4af74efd50fb12c57ccbd1ea539b9b79c8 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-02T11:02:36.959Z sha aae2c15 :: needs maintainer review before merge. :: none

Reuse the canonical run-indexed event listener while preserving approval state, timeout pauses and disposal. Native event-dispatch measurements improve both eight-run and sixteen-run workloads without adding production lines.

Related: #129174

Co-authored-by: Tran Quang <[email protected]>
@steipete
steipete force-pushed the codex/perf-approval-wait-run-routing branch from 36fead4 to aae2c15 Compare September 2, 2026 10:55
@steipete steipete self-assigned this Sep 2, 2026
@steipete steipete closed this Sep 2, 2026
@steipete steipete reopened this Sep 2, 2026
@steipete
steipete merged commit 188fd77 into main Sep 2, 2026
504 of 506 checks passed
@steipete
steipete deleted the codex/perf-approval-wait-run-routing branch September 2, 2026 11:25
@steipete

steipete commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Reuse the canonical run-indexed event listener while preserving approval state, timeout pauses and disposal. Native event-dispatch measurements improve both eight-run and sixteen-run workloads without adding production lines.

Related: openclaw#129174

Co-authored-by: Tran Quang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS 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