Repository navigation
perf(workers): reduce task scheduling overhead - #165230
Conversation
Capture per-task caller state with a separately scoped AsyncResource runner instead of a variadic snapshot binding. Preserve automatic resource destruction and caller context through retained cleanup. Keep the resource closure independent of other task callbacks so a retained completed task handle does not retain its released caller. Extend the existing native retention proof and include the helper in the PR wrapper's runtime import closure.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 5, 2026, 12:48 AM ET / 04:48 UTC (Revision 5). ClawSweeper reviewWhat this changesThe PR reduces worker scheduling overhead by capturing task context more cheaply, servicing shared native workers once per pass, and sending reference-control messages only when reference state changes. Merge readiness✅ Ready for maintainer review This PR remains useful: current main still performs the scheduling work it removes. No actionable introduced defect or unresolved prior finding was found. Priority: P3 Review scores
Verification
How this fits togetherOpenClaw's worker runtime accepts queued computation and retained tasks, then manages execution, replies, cancellation, and cleanup. Its host adapter connects that scheduler to native worker lifetimes and resource owners. flowchart TD
A[Task submissions] --> B[Capture caller context]
B --> C[Bounded worker scheduler]
C --> D[Native worker host]
D --> E[Shared source servicing]
E --> F[Replies and cleanup receipts]
F --> C
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +37, Tests +95, Docs +7, Other +2. Total +141 across 12 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep one scheduler and native lifetime owner while reducing allocation and redundant servicing without changing task custody or public SDK behavior. Do we have a high-confidence way to reproduce the issue? Not applicable as a bug reproduction: current-main source shows the avoidable scheduling operations, and the PR records comparative allocation and actual-worker observations. Is this the best way to solve the issue? Yes. The optimization stays within the existing scheduler and host owners, preserves public entrypoints, and retains availability and cleanup checks. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against fa468f6a64c4. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
Require task-server readiness on internal retained transports. Freeze new worker construction after a genuine bootstrap failure, preserve healthy sibling capacity and checkpoint fairness, and retry only after successful rotation or a new pool generation. Keep arbitrary SDK workers, ordinary task failures, input preparation, and native cleanup ownership on their existing paths. Proved the 12-task failure cohort drops from 12 starts to one, including actual retained-source worker execution and explicit recovery.
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
* perf(worker-runtime): reduce task context capture allocations Capture per-task caller state with a separately scoped AsyncResource runner instead of a variadic snapshot binding. Preserve automatic resource destruction and caller context through retained cleanup. Keep the resource closure independent of other task callbacks so a retained completed task handle does not retain its released caller. Extend the existing native retention proof and include the helper in the PR wrapper's runtime import closure. * fix(workers): stop repeated starts after initialization failure Require task-server readiness on internal retained transports. Freeze new worker construction after a genuine bootstrap failure, preserve healthy sibling capacity and checkpoint fairness, and retry only after successful rotation or a new pool generation. Keep arbitrary SDK workers, ordinary task failures, input preparation, and native cleanup ownership on their existing paths. Proved the 12-task failure cohort drops from 12 starts to one, including actual retained-source worker execution and explicit recovery.
What Problem This Solves
Worker task scheduling performs avoidable async-context allocation, repeats shared native-source servicing, and sends redundant reference-control messages.
User Impact
There is no user-visible API or lifecycle change. Tasks retain their submitting context through execution and owned cleanup; released tasks can let that context be collected while their handles remain usable. Native failure checks, synchronous progress, resource custody, and shutdown settlement remain intact.
Why This Change Was Made
Apply the focused task-context approach used by Piscina and Tinypool to our internal worker runtime. A separately scoped
AsyncResourcerunner reduces capture allocations without retaining callers through unrelated enqueue callbacks. Each synchronous pool or rotation pass captures its workers before callbacks and lets the host service their shared native source once; nested calls create a fresh pass. Nativeref()andunref()send messages only on reference transitions while retaining availability checks and liveness refreshes.The existing context capture already allowed released callers to be collected. This change reduces scheduling work while preserving that behavior. The internal host owns source grouping, and individual stop/resource operations retain their own synchronous servicing.
Evidence
pnpm test … --maxWorkers=1runs: the combined candidate passed 87 tests across six files in 332.10 seconds wrapper wall time. After extracting the shared service-pass helper, all 38 affected tests across four files passed again in 190.04 seconds. The context-retention GC fixture took 395 ms; the new reference-transition assertion took 1 ms and failed the original implementation as intended. Per-file CI seconds were unavailable in the inspected run artifacts.Context optimization measurements and earlier controls
Actual-core retention controls passed on Node 24 and Bun 1.4.2. Earlier actual-source transport checks passed on Node 26.10.0. The unsafe alternative that captured its resource inside enqueue failed the intended GC assertion; the isolated helper and original baseline both collected released callers correctly.
Isolated Node 26 measurements reduced context capture plus four invocations from about 770 ns to 127 ns, and retained memory per capture from about 513 to 152 bytes. Longer native-worker runs used less main-thread CPU in 13 of 14 paired windows, but shared-host wall-time variation was substantial. These are allocation/CPU and synthetic-worker results, not a stable Gateway-throughput percentage.
Historical CI qualification for the original context-only head
CI run 37249796216, attempt 1 completed with one independently attributed inherited failure: checks-ui-e2e (4/7), plus the downstream CI gate. The offline mocked-Gateway rail test expected transcript scroll position 0 after a wheel gesture and observed 8925. Its UI, test, and workflow sources exactly match the tested main parent; the in-browser mock was deliberately disconnected before the interaction, so this path never reaches the changed Node worker pool. The exact UI failure mechanism remains unresolved, and no rerun or speculative fix was made. All other selected owners, including security-fast and real-Gateway UI, succeeded.
This is historical evidence for the original context-only head, not a current-head CI exception. The combined head uses the normal fresh-CI landing path.