Skip to content

fix(queue): avoid repeated scans when cancelling backlogs - #148754

Merged
vincentkoc merged 1 commit into
mainfrom
fix-command-queue-cancel-cpu-20260915
Sep 15, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
fix-command-queue-cancel-cpu-20260915

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes quadratic CPU work when a gateway restart or caller cancels a large backlog of queued commands.

User Impact

Cancelling pending work no longer rescans the remaining backlog for each entry. Priority, FIFO ordering, capacity limits, and already-active task behavior remain unchanged.

Why This Change Was Made

Each priority queue now links its pending entries directly, so cancellation unlinks only the affected entry. A fired once-abort listener also avoids a redundant search through the remaining shared-signal listeners. Removal clears queue membership and references before cleanup can reenter the queue.

The change removes 25 production lines and adds cancellation, ordering, refill, cleanup, and deterministic work-growth coverage. The obsolete ring-allocation lint suppression is removed from its exact allowlist.

Evidence

  • Independent canonical review found no actionable P0–P2 findings. Targeted oxfmt and git diff --check passed on the reviewed bytes.
  • Exact-head native Testbox proof passed all 83 tests in six files: cancellation (6), queue (37), capacity groups (21), scoped lanes (11), background work (4), and the lint-suppression guard (4).
  • Four fresh Node 24.19.0 Linux x64 processes per variant, interleaved ABBABAAB, compared base a4790958b284dfa50fed9c4ae4a5e6685e798435 with head 604a7e85592e0667a4c5d83d42addcc9eb7ee673. Both queue modules used the same native TypeScript source-load transform, detached before warmup; all 195 install inputs and candidate source bytes stayed unchanged.
  • At 4,000 pending commands, median synchronous individual cancellation fell from 430.00 ms to 5.73 ms; shared-signal cancellation fell from 441.82 ms to 1.39 ms. Cumulative action-to-settlement fell from 430.27 to 5.98 ms and 442.09 to 1.65 ms, respectively. Cancelled tasks never ran; reason identity, priority/FIFO, active-task behavior, empty state, and refill checks passed.
  • The deterministic 128/256-entry storage-work guard measured 206,014/821,630 accesses before versus 2,431/4,863 after. Redundant once-listener removals fell from 8 to 0. Both original-behavior guards failed on the base and passed on the candidate in every process.
  • Initial ordinary enqueue-plus-settlement medians changed by −1.1% at N=1, +22.3% at N=16, +67.6% at N=1,000, +11.3% at N=2,000, and −13.0% at N=4,000. Small queues used 16 warmups/64 measurements; larger queues used one warmup/one measurement per process. These slower initial results are retained; no JIT explanation is claimed.
  • A separate ordinary-only Testbox follow-up kept the same inputs, source-loading contract, workload, and eight-process ABBABAAB order, with 16 warmups/64 measurements at every size. All 2,560 observations were retained. Median full-cycle wall times fell 4.33%, 6.09%, 13.15%, 1.30%, and 5.24% at N=1/16/1,000/2,000/4,000. At N=1,000, fixed-work wall ranges were 239.41–261.56 ms before and 211.81–227.45 ms after; CPU ranges were 348.62–371.95 versus 313.70–331.67 ms. The other four wall ranges overlap; N=2,000 CPU was effectively unchanged (+0.09%).
  • Maintainer decision: accept the bounded enqueue and RSS costs for linear cancellation. Steady enqueue medians increased 4.66%, 8.51%, and 4.38% at N=1,000/2,000/4,000, while full-cycle medians were lower. Whole-process peak RSS median increased 413.92→425.70 MiB (+2.85%); the highest candidate sample was 444.72 MiB versus the baseline maximum 414.66 MiB (+30.06 MiB). This resolves the two ClawSweeper evidence items and matching rank-up request; it does not claim universal enqueue improvement, cold-start gains, or reduced retained memory. Follow-up result SHA-256: 4f665096f379c0ebee0db0fd1f580cea1572ef0556fab90724474799e9058822; native command/artifact checks passed and its owned lease was stopped.
  • Initial whole-process peak RSS ranges were 297.86–299.39 MiB before and 297.84–299.00 MiB after. These are process snapshots/peaks, not retained-memory or leak measurements.
  • Native proof run: command and artifact verification passed; the owned lease was stopped. Result SHA-256: 4a8d6309c8f0c5bf1af962de45fbbbada6675c5a2ec56f50b7bcc53e29dcd295. An earlier attempt stopped before tests because a stale local base ref produced the wrong carrier parent; it is retained as a launcher failure, not test evidence.

@vincentkoc vincentkoc self-assigned this Sep 15, 2026
@clawsweeper

clawsweeper Bot commented Sep 15, 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 size: M maintainer Maintainer-authored PR labels Sep 15, 2026
@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 Sep 15, 2026
@clawsweeper

clawsweeper Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 15, 2026, 12:37 AM ET / 04:37 UTC (Revision 2).

ClawSweeper review

What this changes

Replaces command-queue ring storage with linked entries so backlog cancellation avoids repeated scans, and adds cancellation and cleanup regression coverage.

Merge readiness

✅ Ready for maintainer review

The fix remains useful: current main and the latest release still rescan pending entries during cancellation. No actionable patch defects were found. The updated measurements and explicit acceptance of the performance tradeoff resolve the previous review’s evidence request.

Priority: P2
Reviewed head: 604a7e85592e0667a4c5d83d42addcc9eb7ee673

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused owner-level fix with meaningful regression coverage, matched production-path measurements, and transparent acceptance of measured costs.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The captured native Node/Linux evidence exercises the changed command-queue owner with individual and shared-signal cancellation, records substantially lower cancellation time, and checks settlement, ordering, active work, and refill. Matched ordinary-work and RSS measurements address the prior review’s remaining questions.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured native Node/Linux evidence exercises the changed command-queue owner with individual and shared-signal cancellation, records substantially lower cancellation time, and checks settlement, ordering, active work, and refill. Matched ordinary-work and RSS measurements address the prior review’s remaining questions.
Evidence reviewed 8 items Current main still has the cancellation scan: The current implementation loops over the entire priority ring for each removed entry, producing quadratic storage work when cancelling a backlog.
Latest-release comparison: The supplied latest release, v2026.9.4, also contains the ring-scanning cancellation implementation; this optimization is not already present there.
Removal and admission invariants: Removal updates neighboring links and queue counts, then clears membership, neighboring references, and the cleanup callback before invoking cleanup. Enqueue, drain, and clear use the same storage owner; active-task accounting remains unchanged.
Findings None None.
Security None None.

How this fits together

OpenClaw’s command queue schedules pending agent and background tasks under priority and concurrency limits. Caller or restart cancellation removes waiting tasks, while admitted tasks retain their existing cancellation ownership.

flowchart TD
  A[Agent and background tasks] --> B[Priority queues]
  B --> C{Capacity available}
  C -->|Yes| D[Admitted task execution]
  C -->|No| B
  E[Caller or restart cancellation] --> F[Unlink pending entry]
  B --> F
  F --> G[Reject pending promise]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -25, Tests +242. Total +217 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 52 77 -25
Tests 2 244 2 +242
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 296 79 +217

Review metrics

Metric Value Why it matters
Measured backlog cancellation 4,000 entries: individual 430.00→5.73 ms; shared signal 441.82→1.39 ms Matched native measurements directly exercise the synchronous backlog-cancellation cost.
Accepted performance tradeoff Large-queue enqueue medians +4.38–8.51%; peak RSS median +2.85% The PR explicitly accepts these measured costs, while distinguishing whole-process RSS from retained-memory measurements.

Technical review

Best possible solution:

Keep cancellation within the existing queue owner, with constant-time unlinking and unchanged priority, concurrency, and active-task semantics.

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

Yes: pause a lane, enqueue a large backlog, and abort its pending entries; current main visibly rescans the shrinking priority ring for every cancellation. This review established the mechanism from source and inspected supplied native measurements without running code.

Is this the best way to solve the issue?

Yes. Direct unlinking repairs the existing storage owner, removes both identified repeated scans, and preserves the documented queue contract without another implementation or configuration option.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This addresses excessive CPU work during large-backlog cancellation without changing user configuration or scheduling policy.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured native Node/Linux evidence exercises the changed command-queue owner with individual and shared-signal cancellation, records substantially lower cancellation time, and checks settlement, ordering, active work, and refill. Matched ordinary-work and RSS measurements address the prior review’s remaining questions.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured native Node/Linux evidence exercises the changed command-queue owner with individual and shared-signal cancellation, records substantially lower cancellation time, and checks settlement, ordering, active work, and refill. Matched ordinary-work and RSS measurements address the prior review’s remaining questions.

Evidence

What I checked:

  • Current main still has the cancellation scan: The current implementation loops over the entire priority ring for each removed entry, producing quadratic storage work when cancelling a backlog. (src/process/command-queue.state.ts:170, 3f10235f1a28)
  • Latest-release comparison: The supplied latest release, v2026.9.4, also contains the ring-scanning cancellation implementation; this optimization is not already present there. (src/process/command-queue.state.ts:170, 3a9d69db306c)
  • Removal and admission invariants: Removal updates neighboring links and queue counts, then clears membership, neighboring references, and the cleanup callback before invoking cleanup. Enqueue, drain, and clear use the same storage owner; active-task accounting remains unchanged. (src/process/command-queue.state.ts:126, 604a7e85592e)
  • Regression coverage: The added tests cover cancellation across priorities and queue positions, shared AbortSignal cleanup, admission versus active work, reentrant cleanup, refill, and deterministic cancellation-work growth. Existing queue and capacity-group tests protect FIFO and arbitration behavior. Tests were inspected, not executed during this read-only review. (src/process/command-queue.cancellation.test.ts:91, 604a7e85592e)
  • Production-path measurements and prior-review resolution: The complete captured PR body, context revision 2f65d665bf234e8e85107c4181e85ca8afe5d576bd281481b72c13ebd5317722, reports matched native Node 24.19.0 Linux processes exercising the queue before and after this exact head. At 4,000 entries, individual cancellation fell from 430.00 to 5.73 ms and shared-signal cancellation from 441.82 to 1.39 ms, with rejection identity, ordering, active work, empty state, and refill checks passing. The follow-up retained 2,560 ordinary-work observations and explicitly accepts measured enqueue and whole-process RSS costs. Result identities are 4a8d6309c8f0c5bf1af962de45fbbbada6675c5a2ec56f50b7bcc53e29dcd295 and 4f665096f379c0ebee0db0fd1f580cea1572ef0556fab90724474799e9058822. (604a7e85592e)
  • Linked proof-run status distinguished from behavioral evidence: The linked Testbox carrier run https://github.com/openclaw/openclaw/actions/runs/34928478233 is marked cancelled, with cleanup completed; its API status is not treated as a test pass. Behavioral results above come from the captured PR evidence, which states that the owned lease was stopped after verification.

Likely related people:

  • steipete: 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 (1 earlier review cycle)
  • reviewed 2026-09-15T04:07:09.967Z sha 604a7e8 :: blocked before merge. :: none

@vincentkoc
vincentkoc marked this pull request as ready for review September 15, 2026 04:26
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Sep 15, 2026
@vincentkoc
vincentkoc merged commit d24dbdb into main Sep 15, 2026
275 of 276 checks passed
@vincentkoc
vincentkoc deleted the fix-command-queue-cancel-cpu-20260915 branch September 15, 2026 07:11
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

vincentkoc added a commit that referenced this pull request Sep 15, 2026
* commit '0945dabbe487213e95e84a72ad1aae3659e0e000': (115 commits)
  fix: omit automation service aliases from squash coauthors (#148883)
  test: restore services typecheck shard headroom (#148885)
  perf(trajectory): avoid native probes for truncation records (#148870)
  improve(gateway): load cron only when reload requests it (#143500)
  fix: resolve issue #146088 — [Bug]: The most recent Linux AppImage does not ship with the correct versions of libs needed to run (#148174)
  refactor(theme): share nonvirtual ANSI color lookup (#148717)
  perf(gateway): scope exact session row topology reads (#148836)
  improve(memory): reduce vector replacement overhead (#148201)
  fix(i18n): keep HTTP headers out of native translations (#147119)
  refactor(plugins): share source-root display formatting (#148646)
  fix(cron): reject blank script paths before mutations (#148787)
  refactor(config): share audit positional scanning (#148643)
  fix(test): restore infrastructure typecheck shard headroom (#148861)
  fix(release): harden package validation fallbacks (#148729)
  refactor(config): share leaf and descendant unset pruning (#148216)
  test: reuse prepared runtime for pristine preflight (#148858)
  fix(ui): settle global model drafts across agent switches (#148843)
  fix(queue): avoid repeated scans when cancelling backlogs (#148754)
  fix(logging): orphaned \\\\?\\ path markers corrupt redacted Windows diagnostics (#147542)
  refactor(hooks): share optional reply metadata projection (#148641)
  ...
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: M 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