Skip to content

improve(gateway): reduce idle worker startup memory - #133176

Merged
vincentkoc merged 1 commit into
mainfrom
perf/lazy-worker-session-tools
Aug 30, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
perf/lazy-worker-session-tools

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Aug 30, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes an issue where every Gateway process paid the memory cost of worker session-tool execution even when no worker invoked sessions_spawn, sessions_send, or related tools. On memory-constrained devices, that idle import graph consumed roughly 20-27 MiB of avoidable resident memory.

Why This Change Was Made

The worker session-tool executor is now loaded on first use through the existing lazy-runtime boundary. Executor construction is memoized per Gateway, so concurrent first calls share one initialization while the existing live dispatch, publication, portal, placement, and authority bindings remain unchanged.

User Impact

Idle Gateway startup uses less memory. In an 11-pair real Gateway A/B run, ready-state RSS fell by 27.0 MiB and maximum RSS fell by 19.6 MiB. Operators who use worker session tools keep the same behavior; the first tool call pays the one-time module load.

Evidence

  • Real Gateway A/B, 11 paired cold runs:
    • ready RSS median: 587.4 MiB -> 560.4 MiB (-27.0 MiB)
    • maximum RSS median: 596.5 MiB -> 576.9 MiB (-19.6 MiB)
    • startup completion median: 5898.9 ms -> 5552 ms
    • all 22 health, readiness, and shutdown checks passed
  • Exact-head focused tests: 63/63 passed across six Gateway files.
  • Exact-head full build passed in a normal full checkout, including CLI import guards, runtime postbuild, plugin SDK exports, and Control UI.
  • Exact-head QA Lab subagent-lineage scenario passed through real worker-hosted nested session spawn and communication.
  • The lazy-init regression test proves concurrent first calls construct one executor.
  • The static import-boundary test prevents worker session tools from returning to idle startup.
  • Exact-head ARM64 1 GB/no-swap package proof passed on t4g.micro:
    • cgroup peak: 583,389,184 bytes
    • Gateway maximum RSS: 401,156 KiB
    • gateway status maximum RSS: 178,712 KiB
    • peak swap: 0 bytes
    • OOM / OOM kill / group OOM kill: 0 / 0 / 0
    • all install, CLI, Gateway, status RPC, and planned-shutdown phases passed
  • Production delta: +29/-17 (net +12). Tests: +117.

@clawsweeper

clawsweeper Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S maintainer Maintainer-authored PR labels Aug 30, 2026
@vincentkoc vincentkoc self-assigned this Aug 30, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Aug 30, 2026
@clawsweeper

clawsweeper Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 30, 2026, 4:11 AM ET / 08:11 UTC.

ClawSweeper review

What this changes

The branch defers Gateway worker session-tool executor loading and construction until a worker first requests a session, publication, or portal tool, with import-boundary and concurrent-first-use coverage.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

No introduced correctness defect was found. Current main still eagerly imports the worker session-tool executor, so this optimization remains necessary; as a maintainer-authored, maintainer-labeled PR, it should remain in ordinary maintainer review until exact-head checks finish.

Priority: P3
Reviewed head: 8d2b3e995dcf7dfb3f709726dc989ebc7f6febf7
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, conventional lazy-loading change with targeted concurrency coverage and no identified introduced defect.
Proof confidence 🌊 off-meta tidepool Not applicable: The external-contributor proof gate does not apply to this MEMBER-authored PR. The captured body nevertheless reports real Gateway, worker-session, and ARM64 package results, although raw command output or artifacts were not included in the supplied snapshot.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The external-contributor proof gate does not apply to this MEMBER-authored PR. The captured body nevertheless reports real Gateway, worker-session, and ARM64 package results, although raw command output or artifacts were not included in the supplied snapshot.
Evidence reviewed 7 items Introduced lazy owner boundary: The PR adds a cached lazy module loader and a per-Gateway promise so concurrent first requests share one executor, then dispatches through that executor.
Caller retains request-time validation: The worker RPC validates tool authority before invoking the service callback and validates again after awaited execution, so deferring executor construction does not replace the existing request-time gate.
Callee retains authoritative checks: The unchanged executor resolves the exact worker source before processing requests and revalidates publication authority after awaited work.
Findings None None.
Security None None.

How this fits together

The Gateway initializes worker-environment services for hosted worker sessions. Worker tool requests pass through the session-tool executor to session dispatch, GitHub publication, or portal services after the Gateway has started.

flowchart LR
  A[Gateway startup] --> B[Worker environment runtime]
  B --> C[Idle worker service]
  D[Worker tool request] --> E[Lazy executor initialization]
  E --> F[Session-tool executor]
  F --> G[Session, publication, or portal action]
Loading

Decision needed

Question Recommendation
Should this maintainer-authored Gateway memory optimization be merged once the exact-head required checks are green? Merge after exact-head CI: Accept the existing lazy-loading pattern once the current head is green and no maintainer raises a concern.

Why: The patch is focused and no correctness blocker was found, but the maintainer label requires ordinary human acceptance rather than automated cleanup.

Before merge

  • Complete next step (P2) - This maintainer-authored, maintainer-labeled PR needs ordinary maintainer acceptance after exact-head checks complete; no repair lane is needed.
Agent review details

Security

None.

PR surface

Source +12, Tests +117. Total +129 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 29 17 +12
Tests 2 117 0 +117
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 146 17 +129

Review metrics

Metric Value Why it matters
Production versus test delta production +29/-17 (net +12); tests +117/-0 The small production increase implements the per-Gateway lazy wrapper; test growth covers idle construction and concurrent first use.

Technical review

Best possible solution:

Keep one memoized lazy executor per Gateway while preserving the current request-time authority checks and live-binding closures for dispatch, publication, and portals.

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

Not applicable as a performance optimization rather than a discrete bug report; current main source confirms the eager startup import that the branch targets.

Is this the best way to solve the issue?

Yes. It uses the repository's existing lazy-runtime abstraction at the Gateway owner boundary while leaving worker RPC validation and executor-side authority checks intact.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against eb3e09b47bce.

Labels

Label justifications:

  • P3: This is an internal idle-memory optimization, not a reported availability, delivery, auth, or security regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The external-contributor proof gate does not apply to this MEMBER-authored PR. The captured body nevertheless reports real Gateway, worker-session, and ARM64 package results, although raw command output or artifacts were not included in the supplied snapshot.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: 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-08-30T07:52:10.138Z sha 8d2b3e9 :: needs maintainer review before merge. :: none

@vincentkoc
vincentkoc marked this pull request as ready for review August 30, 2026 07:59
@vincentkoc

Copy link
Copy Markdown
Member Author

ClawSweeper's remaining procedural item is addressed: the PR stayed draft while the ARM64 proof was running. The body now includes the exact-head 1 GB/no-swap result, the PR is in ordinary maintainer review, and no repair lane is requested.

@vincentkoc
vincentkoc merged commit 46ef974 into main Aug 30, 2026
318 checks passed
@vincentkoc
vincentkoc deleted the perf/lazy-worker-session-tools branch August 30, 2026 08:11
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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