Repository navigation
perf(diagnostics): attribute Worker heap usage by script - #154295
Conversation
|
🦞👀 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 September 20, 2026, 10:31 PM ET / September 21, 2026, 02:31 UTC. ClawSweeper reviewWhat this changesAdds Worker script names to memory-pressure warnings and a per-script Prometheus heap gauge, using the existing sampling registry. Merge readiness✅ Ready for maintainer review Keep open: this is a useful, bounded follow-up to aggregate Worker memory accounting, and current main does not provide the script attribution. No actionable correctness or security defect was found. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway samples background Worker memory through its diagnostics heartbeat. Those samples feed operator warnings and the Prometheus plugin’s authenticated metrics endpoint. flowchart TD
A[Background Workers] --> B[Worker resource registry]
B --> C[Fresh heap samples with bounded script names]
D[Diagnostics heartbeat] --> C
C --> E[Memory diagnostic event]
E --> F[Top five heaps in pressure warnings]
E --> G[Per-script Prometheus gauge]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +81, Tests +94, Docs +8. Total +183 across 11 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep script attribution with the existing Worker resource owner and derive bounded warnings and metrics from its fresh samples. Do we have a high-confidence way to reproduce the issue? Not applicable: this adds diagnostic attribution rather than repairing a demonstrated heap leak; current-main source confirms the attribution gap. Is this the best way to solve the issue? Yes: extending the existing sampling owner and exporter avoids a competing registry or timer while preserving existing aggregate metrics. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 20db76a79212. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Merged via squash.
|
Related: #153842
What Problem This Solves
Aggregate Worker heap totals cannot identify which Worker script is growing when Gateway memory pressure rises.
User Impact
Operators get the five largest fresh Worker heaps in the memory-pressure warning and a bounded-label
openclaw_worker_heap_used_bytes{script}gauge. This is attribution only; it does not claim to fix heap growth.Example warning fragment (synthetic byte counts):
Example metric (sum of two fresh Workers running the same script):
Why This Change Was Made
The existing Worker resource registry prepares a fixed allowlisted basename once at construction. Unknown, eval, and directly constructed Workers use
other; installation paths and eval sources are never retained. Archive and lease-heartbeat constructors now use that same owner. The existing 30-second heartbeat, single outstanding native request, 60-second sample expiry, and native-exit cleanup remain in charge. Metrics aggregate fresh samples by script and remove series absent from the next sample. No new timer or cache lifecycle.The existing metric store moves into a sibling module to satisfy the service file's line-cap ratchet; the new clearing operation remains part of that store. Net production growth: 81 lines. Probe scripts remain local and are excluded from this PR.
Evidence
Tiny single-process benchmark of the original and candidate sampler/exporter function bodies, using 50 cached Worker samples and 2,000 unrelated gauges. Five alternating rounds, median time per call:
This measures attribution overhead, excludes unchanged native heap interrupts and event dispatch, and ran in under one second on a loaded host. No end-to-end speedup is claimed.
Validation uses provider
blacksmith-testbox, profileopenclaw-check, leasetbx_01m30rwcft36t12jtfbb7wc97x, Testbox run. The first focused run stopped before executing tests because the synced checkout lacked dependencies;pnpm install --frozen-lockfileon that lease repaired hydration. The first unpinned changed-file check caught the service line-cap growth and also included changes from newer main; the extraction resolves the former, and subsequent checks pin the lane's original base647e62369ffb4b684be2b8adadce825b21b042c2.The pinned broad check passed core, all 24 core-test, extension, and extension-test typechecks plus boundary/dead-export guards, then found one test-only property-shorthand lint error. After fixing that line, focused
check-changedruns for the changed Worker test and the exporter/docs completed successfully on replacement leasetbx_01m30v43kcda3kyp8jaxp8ke7s(run). Prior successful typecheck evidence was reused for unchanged inputs. Both leases are stopped.blacksmith-testbox/ first leasenode scripts/run-vitest.mjs src/infra/worker-cpu.test.ts src/logging/diagnostic-memory.test.ts extensions/diagnostics-prometheus/src/service.event-loop.test.ts --maxWorkers=1blacksmith-testbox/ first leasenode scripts/check-changed.mjs --base 647e62369ffb4b684be2b8adadce825b21b042c2blacksmith-testbox/ replacement leasenode scripts/check-changed.mjs --base 647e62369ffb4b684be2b8adadce825b21b042c2 -- src/infra/worker-cpu.test.tsblacksmith-testbox/ replacement leasecheck-changedcommand with the three exporter files anddocs/gateway/prometheus.mdblacksmith-testbox/ replacement leaserun-vitestfiltered to the new output assertionsworkerHeaps/openclaw_worker_heap_used_bytesoutput. Candidate files restored immediately afterward.blacksmith-testbox/ replacement leasenode scripts/run-vitest.mjscommand belowChanged test-file wall times on the final run: Worker lifecycle 2.71 s, diagnostic memory 4.59 s, Prometheus metrics 2.59 s (including startup/transformation; each is under 0.5 s of test execution). Tests cover native-exit cleanup, stale samples, one outstanding heap request during a stall, source/bundled/unknown labels, top-five log records including total heap, same-script summation, trust filtering, and absent-series removal. PR CI is separate and pending at publication.
Recovery notes: two post-fix native sync attempts timed out during the provider's full-checksum fallback, before tests ran; the old lease was retired and targeted sync on the replacement succeeded. A compound login-shell command initially ran outside the synced checkout and failed to resolve the pinned base; a non-login shell verified the exact candidate base and ran all subsequent compound proof commands. No failed test was rerun without identifying its cause.
Independent Codex autoreview: no actionable P0–P2 findings.
git diff --checkpasses. No production deployment was performed. The original sustained heap growth remains unproven by synthetic workloads; nested Workers and direct plugin Workers cannot be individually attributed beyond the documented registry coverage.