Skip to content

improve: reduce Gateway broker memory at startup - #206104

Merged
vincentkoc merged 2 commits into
mainfrom
perf/broker-cold-command-chunks-20261127
Nov 26, 2026
Merged

vincentkoc merged 2 commits into
mainfrom
perf/broker-cold-command-chunks-20261127

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

On a memory-constrained Gateway, the helper that holds native resources loaded readers for file operations that its mode rejects. In the original ARM investigation this helper accounted for 66,174 KiB of proportional set size shortly before the service reached its limit. Related Gateway report

User Impact

The Gateway can keep its native resources open with a smaller resident helper. Ordinary command helpers also start with less memory. Existing installations pick up the change on restart.

Why This Change Was Made

Build the broker with the private runtime chunks already used by other process entry points. Before announcing readiness, each broker captures the implementations its mode permits. Both modes capture command execution, and ordinary brokers also capture full and paged file reads. A running broker can still serve its first permitted command after an update removes its original package directory. A missing required implementation returns the existing startup failure to its owner.

Evidence

Three fresh packaged Linux x64 processes per arm and mode, comparing the same launcher, Node 24.22.1 and inputs against c30fc8135800. The table gives medians measured from the kernel after readiness. PSS apportions shared pages between processes.

Broker Stock PSS Updated PSS Stock RSS Updated RSS Median readiness
Native resources 83,021 KiB 38,322 KiB 120,256 KiB 75,344 KiB 185 → 72 ms
Ordinary commands 82,770 KiB 57,681 KiB 120,388 KiB 95,196 KiB 187 → 143 ms

The native-resource broker saved 43.65 MiB PSS at readiness. After its first synthetic native claim, PSS was 83,082 → 38,412 KiB. The claim, seal and close completed on both builds. In the separately matched 768 MiB ARM Gateway run, the original chat still failed before any acknowledgement or provider request. The native broker measured 66,174 KiB PSS on stock and 72,524 KiB on the updated package at each run’s last sample before its first kill. Those samples include the Gateway’s actual native claims, whose identities were unobserved. The isolated x64 saving did not carry through to that operating point. Isolated runner allocation

After removing the original installation, the first command execution succeeded in both modes. Ordinary brokers still served full and paged file reads and honored cancellation. Resource brokers still refused raw reads. With the original installation present, both builds identified a large PDF. Once its optional MIME detector became unavailable, both returned the same PDF prefix bytes and the same text metadata.

The new startup regression failed on stock. On the updated source, 43 broker and file-reader tests and the targeted Gateway import-boundary test passed. Independent source review also checked startup failure, disconnect, shutdown, cancellation and update behavior.

@vincentkoc vincentkoc self-assigned this Nov 26, 2026
@clawsweeper

clawsweeper Bot commented Nov 26, 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 scripts Repository scripts size: S labels Nov 26, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review November 26, 2026 18:50
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. labels Nov 26, 2026
@vincentkoc
vincentkoc enabled auto-merge (squash) November 26, 2026 18:56
@vincentkoc
vincentkoc disabled auto-merge November 26, 2026 18:58
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 26, 2026
@clawsweeper

clawsweeper Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

This PR gives spawn brokers private runtime chunks and captures only their permitted command and file-reader implementations before readiness.

Example: A packaged Linux x64 native-resource broker reaches readiness on Node 24.22.1.

  • Before: Reported median PSS was 83,021 KiB.
  • After: Reported median PSS was 38,322 KiB, with native claim, seal, and close still completing.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused implementation with supplied packaged behavior evidence and no actionable findings; overall C reflects partial coverage of the linked ARM memory problem.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The body supplies packaged Linux x64 broker measurements and successful native claims, commands after installation removal, file reads, and cancellation; its separate ARM Gateway failure limits the result to the scoped broker improvement.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Partial
User problem: Gateway helper processes consume memory loading file-reading code that native-resource mode cannot use.
Reason: The maintainer-authored direction has a concrete packaged x64 saving with limited implementation cost and explicitly leaves #192547 open.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR provides a useful, focused broker startup improvement; no actionable correctness or security defect was found.

Reviewed head: 0122e91752ad6e498fbeb065abc9c3e28855a7b6

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Gateway delegates command execution and native-resource custody to broker children; private IPC carries requests, process handles, results, and cleanup receipts.

flowchart TD
  A[Gateway host] --> B[Broker bootstrap]
  B --> C[Capture permitted handlers]
  C --> D[Command execution]
  C --> E[Ordinary broker file reads]
  B --> F[Native resource server]
  D --> G[Results and cleanup receipts]
  E --> G
  F --> G
Loading

Technical review

Best possible solution:

Retain this focused broker optimization while the linked issue continues to own constrained ARM64 response-and-soak acceptance.

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

Current main statically imports the full-file reader even for resource brokers that reject reads; the PR supplies packaged before/after memory measurements, which this read-only review did not rerun.

Is this the best way to solve the issue?

The change uses the existing build inventory and broker lifecycle, preserves SQLite read isolation, and captures permitted handlers before package replacement can remove their chunks.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Broker file-reader isolation keeps the original intent (perf(agents): reuse the command broker for isolated file reads #172476: Reuse the command broker for isolated reads while prohibiting raw reads in native-resource brokers to preserve SQLite POSIX locks.)
  • Private worker chunks keeps the original intent (98a9fde: Keep small worker processes from evaluating unrelated runtime modules while retaining lazy optional paths.)
  • Startup refusal and cleanup keeps the original intent (3bfccdb: Release undispatched native-resource claims only after confirmed startup refusal and physical broker closure.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduced change: Reviewed the supplied merge-base-to-head diff; the broker joins the existing private-chunk inventory without changing its entrypoint or introducing another process owner. (scripts/lib/runtime-process-core-build-entries.mts:62, 0122e91752ad)
  • Readiness and package replacement: Both modes capture execa before readiness; only ordinary brokers capture full and paged readers. Initialization failure reports through the existing startup-failed protocol, and shutdown still joins initialization. (src/process/spawn-broker/worker.ts:711, 0122e91752ad)
  • Packaged behavior evidence and explicit limitation: The live PR body reports three fresh packaged Linux x64 processes per arm and mode, kernel memory measurements, successful commands after installation removal, file reads, cancellation, and native-resource lifecycle operations. It explicitly records that the separate constrained ARM Gateway run still failed and showed no corresponding operating-point saving. The linked workflow identifies the allocation; its cancelled conclusion is not treated as performance proof. (0122e91752ad)
  • Distinct regression coverage: The new subprocess regression distinguishes resource-broker readiness without a reader from ordinary-broker startup rejection. The static import assertions protect bootstrap isolation, while the admission fixture now waits for actual readiness before checking capacity settlement. (src/process/spawn-broker/startup.test.ts:15, 0122e91752ad)
  • Original reader isolation contract: Merged perf(agents): reuse the command broker for isolated file reads #172476 explains that raw reads must remain outside processes holding SQLite locks; this PR preserves the resource-broker refusal and reuses the same readers. (src/process/spawn-broker/worker.ts:203, 0122e91752ad)
  • Current main and feature history: Fetched main retains the eager reader import and previous build registration. Broker history spans its initial Linux spawn optimization, isolated file-read reuse, paged reads, lifecycle repairs, and Vincent Koc's cold-start work; this change remains distinct from those merged improvements. (src/process/spawn-broker/worker.ts:4, 8e2c8f5b57ac)

Likely related people:

  • Peter Steinberger: Raw commit 67cedc3 adds src/process/spawn-broker/worker.ts:2 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 67cedc32e996; files: src/process/spawn-broker/worker.ts)
  • Vincent Koc: Raw commit 625945b adds src/process/spawn-broker/worker.ts:11 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 625945b7ae01; files: src/process/spawn-broker/worker.ts)
  • ToToKr: Raw commit 29a02f2 adds src/process/spawn-broker/worker.ts:6 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 29a02f29ea2f; files: src/process/spawn-broker/worker.ts)

PR surface

Source +21, Tests +35, Other +1. Total +57 across 7 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 45 24 +21
Tests 3 42 7 +35
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 2 1 +1
Total 7 89 32 +57

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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 execution lanes are retired.

Reviewed November 26, 2026, 2:12 PM ET / 19:12 UTC.

@vincentkoc
vincentkoc merged commit 0971864 into main Nov 26, 2026
203 of 205 checks passed
@vincentkoc
vincentkoc deleted the perf/broker-cold-command-chunks-20261127 branch November 26, 2026 19:54
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: S 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