Skip to content

improve: reduce memory when the catalog first captures plugin source - #206276

Merged
vincentkoc merged 1 commit into
mainfrom
perf/bundle-fs-safe-broker-attachments-20261127
Nov 27, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
perf/bundle-fs-safe-broker-attachments-20261127

Conversation

@vincentkoc

Copy link
Copy Markdown
Member

What Problem This Solves

When the catalog first asks the broker to own a plugin source, the broker evaluates 216 additional files from the installed @openclaw/fs-safe package. The files total 1,454,020 bytes. This happens while attaching the owner, before the first request to create a source directory.

User Impact

The catalog can keep its source owner alive with a smaller broker process. In a packaged Linux x64 fixture, median broker PSS after the first source request fell from 67,662 to 59,960 KiB. Operator configuration and filesystem permissions stay the same.

Why This Change Was Made

The two broker attachment entry points already share private chunks. Bundle the filesystem code they use in that group. Native libraries and platform helper assets still resolve from the installed dependency. The logger and semver keep their runtime-selected exports. The normal compressed package grows by 65,127 bytes (124,125,394 → 124,190,521), with two additional private chunks. The global runtime, SDK and sealed workers keep their existing packaging.

Related: #192547

Evidence

Compared normal package builds from 3c6820c69e2a and 35f1d307320a. The standalone broker ran on Node 24.22.1, Linux x64, one CPU, 2 GiB RAM, no swap and no network. Counts come from a separate module-evaluation probe. Timing and PSS come from six fresh, uninstrumented processes in stock/changed/changed/stock/stock/changed order. All six created and released real source directories and joined on close. Testbox allocation

Broker stage Stock PSS (KiB) Changed PSS (KiB)
Ready 49,680 49,618
Source owner attached 65,876 57,962
First source created 67,662 59,960
First source released 67,972 60,050
After warm cycles 70,940 62,766
  • Ready already evaluated 13 external fs-safe files in both builds. Attaching the stock owner evaluated 216 more, including the advanced entry. The changed owner added zero. Both evaluated the same 33 external logger files. The SQLite entry was absent from this workload.
  • Median cold attachment took 130.873 → 47.636 ms. First source creation took 7.076 → 6.675 ms. Each child then warmed up for 10 cycles and timed 160 source-create/release cycles. Median per-run p50/p95/p99 were 0.805/1.155/1.850 ms stock and 0.826/1.122/2.049 ms changed. The p99 rose 0.199 ms. Across runs the p99 ranged from 1.517 to 28.603 ms stock and 1.798 to 27.287 ms changed. Median broker CPU was 15 ticks per 160 cycles in both arms (100 Hz). These data support the cold attachment and memory result. Warm throughput and whole-Gateway memory remain unproven.
  • Both normal packages passed plugin-first, SQLite-first, and SQLite-only lifecycles. The sentinel snapshot remained readable after renaming the installed dist tree. Existing resource cleanup, sealing and process exit succeeded.
  • Both passed the real catalog owner/source/release sequence on Bun 1.4.4. A separate asset probe exercised the installed optional Linux N-API binary from a relocated package, including a native canonical-path operation. It verified the Windows helper asset path and bytes on Linux. The changed probe exposed existing private bindings in disposable copies of the emitted chunks. Windows execution was outside this run. Node and Bun selected their respective external logger exports.
  • The package, shared group, resource retirement and relocated-runtime tests passed, along with targeted formatting and lint. The new group contract assertions failed against stock.

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

clawsweeper Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@vincentkoc
vincentkoc marked this pull request as ready for review November 27, 2026 01:19
@clawsweeper clawsweeper Bot added the P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. label Nov 27, 2026
@vincentkoc

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature 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:feature Preliminary submission classification; full source review pending. labels Nov 27, 2026
@clawsweeper

clawsweeper Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

This PR bundles filesystem helpers into the broker attachments’ private build while preserving installed native assets and runtime-selected companion dependencies.

Example: A packaged Linux x64 broker handles its first plugin source request.

  • Before: Median broker PSS was 67,662 KiB, and attaching the source owner evaluated 216 additional external filesystem-package files.
  • After: Median broker PSS was 59,960 KiB, and attaching the source owner evaluated no additional external filesystem-package files.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, worthwhile packaging improvement with real production-boundary measurements and no identified defect; the overall rating reflects partial coverage of the linked constrained-host problem.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The body records before/after normal-package broker runs exercising real source creation, release, and shutdown on Node/Linux, plus Bun catalog operations, both attachment orderings, retained SQLite reads after relocation, and installed native-asset checks; these support the scoped cold-memory claim without claiming full ARM64 Gateway success.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Partial
User problem: Catalog source capture keeps a larger broker process resident, reducing memory headroom on constrained hosts.
Reason: The maintainer-authored change demonstrates a focused memory reduction with a small packaging tradeoff; it correctly uses Related because the linked ARM64 first-response problem remains open.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it makes a focused, measured memory improvement using the existing build owner, and no actionable correctness or security defect was found.

Reviewed head: 35f1d307320a0426852b0054b4580b9862ab4af2

Before merge

None.

Findings

None.

Agent review details

How this fits together

The model catalog acquires plugin source custody through the native-resource broker; the build config packages its plugin and SQLite attachments, which create, retain, and release resources.

flowchart TD
  A[Model catalog] --> B[Native resource broker]
  B --> C[Plugin source attachment]
  B --> D[SQLite attachment]
  C --> E[Private filesystem helper chunks]
  D --> E
  E --> F[Installed native libraries and assets]
Loading

Technical review

Best possible solution:

Use the existing selective bundling and package-URL transform for the broker attachments while retaining the broader ARM64 memory investigation separately.

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

Current main excludes the broker attachment group from selective filesystem bundling, and the PR records matched packaged runs exercising the resulting external module evaluation; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The change removes unnecessary module evaluation through the existing build owner, preserves companion export selection and installed asset lookup, and introduces no competing runtime or persistence mechanism.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2ab7b31a13d9.

Provenance checked

  • Selective filesystem bundling and native asset ownership keeps the original intent (0873425: Reduce cold reader initialization while keeping native bindings and platform assets anchored to the installed dependency.)
  • Shared standalone build groups keeps the original intent (d38fed5: Emit common modules once for entries hosted in one isolate, reducing installed package duplication.)
  • Broker attachment graph and code generation keeps the original intent (ee4061c: Avoid loading shared Gateway chunks while preserving attachment module identity, public entry URLs, and ordinary runtime code generation.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned change and current-main comparison: The introduced diff adds broker attachments to the existing filesystem bundling mechanism and keeps companion bundling restricted to the previous worker set; fetched main still uses the earlier selection. (tsdown.config.ts:1049, 35f1d307320a)
  • Packaged production-boundary evidence: The PR body records six fresh packaged broker processes on Node 24.22.1/Linux x64, real source creation and release, lower cold-attachment memory, both attachment orderings, retained SQLite readability after a dist rename, Bun catalog lifecycle checks, and relocated native-asset checks; Windows execution and whole-Gateway savings are explicitly unproven. (35f1d307320a)
  • Existing ownership and asset-preservation mechanism: The existing transform anchors dependency import.meta.url references and package-manifest resolution to the installed package; the PR enables this transform for the broker group rather than adding another asset owner. (scripts/lib/package-runtime-url-build-plugin.mts:6, 2ab7b31a13d9)
  • Intentional predecessor behavior: perf(config): avoid synchronous plugin-state snapshots #170685 introduced selective JavaScript bundling with installed native-asset URLs; improve: reduce broker memory when loading plugin and SQLite resources #206142 grouped the broker attachments to preserve shared module identity and ordinary runtime code generation. (tsdown.config.ts:1067, ee4061ce5e81)
  • Accepted measured tradeoff: The maintainer-authored body explicitly records a 65,127-byte compressed-package increase and a 0.199 ms increase in median per-run p99, limits its performance claim to cold attachment and memory, and leaves warm-throughput claims unproven. (35f1d307320a)
  • Broader memory problem remains separate: Reduce Gateway startup memory on constrained ARM64 hosts #192547 still tracks constrained ARM64 first-response failures; its discussion attributes a surviving broker to catalog source capture, supporting this narrower optimization without establishing full acceptance.

Likely related people:

  • Vincent Koc: Raw commit ee4061c adds tsdown.config.ts:1048 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ee4061ce5e81; files: tsdown.config.ts)
  • Ayaan Zaidi: Raw commit d38fed5 adds tsdown.config.ts:1045 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: d38fed580398; files: tsdown.config.ts)

PR surface

Tests +6, Other +3. Total +9 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 1 6 0 +6
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 10 7 +3
Total 2 16 7 +9

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, 8:24 PM ET / November 27, 2026, 01:24 UTC.

@vincentkoc
vincentkoc enabled auto-merge (squash) November 27, 2026 01:29
@vincentkoc
vincentkoc merged commit 56ac869 into main Nov 27, 2026
157 checks passed
@vincentkoc
vincentkoc deleted the perf/bundle-fs-safe-broker-attachments-20261127 branch November 27, 2026 01:31
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash auto-merge.

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

Labels

P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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