Skip to content

perf(agents): stop retired model generations retaining runs - #168584

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-heap-snapshot-retainers-20260919
Oct 10, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-heap-snapshot-retainers-20260919

Conversation

@steipete

@steipete steipete commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Long-lived model-runtime snapshots can keep completed runs and their session histories in memory after the runtime generation is retired.

User Impact

Reduces Gateway memory growth after model-runtime generation changes without changing cancellation, runtime admission, or diagnostics.

Why This Change Was Made

A retained snapshot keeps its generation's abort signal. Node's default abort reason has a lazy stack that holds the retiring caller's function objects, including closures over completed run state. Materialize that stack at retirement so callers retain the native AbortError and its diagnostic text without retaining those closures.

The production snapshot had 195 SessionManager instances versus 18 in the earlier snapshot; their overlapping retained subtrees grew from 102 to 1,696 MiB. Strong paths passed through catalog snapshots → generation abort signals → unformatted error stacks → retiring run closures → session state. Simulating removal of these stack edges reduced reachable bytes by 1,811 MiB and released 69 managers. This is graph evidence, not a measured post-deployment reduction.

Production LOC: +4. This preserves the exact native abort reason, synchronous notification, and stack diagnostics; the existing retirement owner handles the fix without a new cache, owner, or compatibility path.

Evidence

  • Linux rig, Node 26.8.1, real generation capture/retirement APIs: the new 128-generation regression fails on unchanged code with 8,388,608 retained bytes, then passes with 0.
  • Matched 320-generation synthetic-history workload: old post-GC heap 75.9 → 336.5 MiB, fixed 10.9 → 11.2 MiB. Retained history slope 1 MiB/generation → 0; the fixed residual is deliberately retained signal/stack metadata. This accelerated owner-level workload does not establish a production MiB/hour rate.
  • Blacksmith Testbox tbx_01m4kamf1myv3ppyrhta3vzjwn: node scripts/run-vitest.mjs src/agents/prepared-model-runtime.lifecycle.test.ts --maxWorkers=1 passed both tests. New retention test: 92 ms; initial cold worker preparation made the file's first run 21.51 s. Initial attempt lacked dependencies; installed once with pnpm install --frozen-lockfile and reran successfully.
  • AWS Crabbox cbx_b42a658dbd1b: changed-file gate passed core production types, all 27 core test type graphs, formatting, and preceding guards; its only failure was the repository-wide unused-export scan.
  • Hetzner Crabbox cbx_68606ff1a95b: all nine remaining gate commands passed, including targeted lint and runtime import/boundary guards. Export-name collision guard passed. node scripts/run-vitest.mjs src/agents/prepared-model-runtime test/scripts/eager-import-closure.test.ts test/scripts/pr-wrapper-source-closure.test.ts test/scripts/ci-node-test-plan.test.ts passed all five shards in 250.81 s; the new retention case took 115 ms.
  • The unused-export scan was reproduced on unchanged main 06d231d65279a579b6cacbfb6a281e6fca1c5dbb: tools/solid-lint/index.mjs (default) and src/agents/tools/sessions-channel-fixture.test-support.ts (resolveSessionConversationStub, resolveSessionTargetStub). The earlier updater-export report is already fixed by main. No suppression or unrelated source change is included.
  • Independent review: clean at P2, with no accepted/actionable findings.
  • Exact-head CI passed: 40 successful jobs and no failures. The retention regression took 0.132 s in CI; its file took 3.53 s (4.88 s including the test wrapper). Merged as 3cfb066c0224328abbe696900ebf69dca0a31add.

Materializing the native stack adds about 8 μs per retirement on the rig (median 5.3 → 13.3 μs over seven 1,000-operation samples); it is outside token processing.

The production snapshots were analyzed privately on an isolated rig. No snapshot contents are included. Other retained roots remain; the full reported hourly slope needs measurement after deployment.

@clawsweeper

clawsweeper Bot commented Oct 10, 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 agents Agent runtime and tooling size: S labels Oct 10, 2026
@steipete
steipete marked this pull request as ready for review October 10, 2026 18:06
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Oct 10, 2026
@clawsweeper

clawsweeper Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR materializes retired model generations’ native abort stacks so retained signals can release completed run state.

Example: A Gateway retires a model-runtime generation while an older catalog snapshot remains reachable.

  • Before: The abort reason’s lazy stack can retain completed runs and their session histories.
  • After: The formatted stack preserves diagnostics while releasing those closure references.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, apparently correct owner-level repair with a meaningful native GC regression; evidence remains bounded to synthetic owner workloads rather than the shipped Gateway.
Proof confidence 🦐 gold shrimp (3/6) The ordinary contributor proof gate does not apply under the supplied maintainer-authored item classification; the available evidence exercises real generation capture and retirement with synthetic histories on Linux Node 26.8.1, reporting zero retained history bytes after the repair.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Long-running Gateways can retain completed runs and session histories through retired model-runtime signals.
Reason: The established feature owner proposes a minimal repair backed by reported heap-graph evidence and a failing retention regression; broader Gateway memory growth remains outside this specific fix.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it provides a focused repair for completed-run memory retention that current main still permits, with no actionable correctness or security findings.

Priority: P2
Reviewed head: c9b1e77b42378f3b4a60d38f44339afc58228393

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied owner-level GC workload does not exercise generation changes through a shipped Gateway or establish a deployed memory-growth reduction.
Agent review details

How this fits together

The prepared-model runtime owns generation retirement between model publication, configuration refresh, authentication changes, and catalog workers; it produces abort notifications consumed by retained snapshots and worker cleanup.

flowchart TD
  A[Runtime publication or refresh] --> B[Generation retirement owner]
  B --> C[Native abort notification]
  C --> D[Catalog worker cleanup]
  C --> E[Retained snapshot signal]
  B --> F[Materialize native error stack]
  F --> G[Completed run state can collect]
Loading

Technical review

Best possible solution:

Land the focused retirement-owner repair while preserving native abort diagnostics and existing generation fencing.

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

The source exposes the retained-signal/native-error path, and the supplied Node 26.8.1 GC experiment reports failure before the repair and collection afterward; this read-only review did not independently execute it.

Is this the best way to solve the issue?

Materializing the stack at the existing retirement owner directly addresses the reported retention edge with minimal code and preserves cancellation and diagnostic contracts.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 171df4e1c5a0.

Provenance checked

  • Generation retirement notifications keeps the original intent (9343ea4: Replace idle generation polling with exact-owner retirement notifications while retaining admission/currentness checks and physical worker cleanup.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 9343ea4 adds src/agents/prepared-model-runtime.lifecycle.ts:21 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9343ea4099ba; files: src/agents/prepared-model-runtime.lifecycle.ts)

PR surface

Source +4, Tests +79. Total +83 across 4 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 4 0 +4
Tests 3 79 0 +79
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 83 0 +83

Review metrics

Metric Value Why it matters
Production versus regression support Production +4 lines; tests and test support +79 lines The additional support is justified by a native GC retention regression that checks collection and existing abort semantics without adding a production test seam.

Labels

Label changes:

  • add P2: This is a focused memory-retention repair for long-running Gateways; the evidence does not establish an urgent outage caused by this particular root.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp 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:

  • P2: This is a focused memory-retention repair for long-running Gateways; the evidence does not establish an urgent outage caused by this particular root.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

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 need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

Reviewed October 10, 2026, 2:11 PM ET / 18:11 UTC.

@steipete
steipete merged commit 3cfb066 into main Oct 10, 2026
193 of 194 checks passed
@steipete
steipete deleted the steipete/perf-micro-heap-snapshot-retainers-20260919 branch October 10, 2026 18:24
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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