Skip to content

fix(gateway): reduce memory growth during session lookups - #140499

Merged
obviyus merged 9 commits into
openclaw:mainfrom
DakshK-02:fix/gateway-session-store-list-projection
Sep 16, 2026
Merged

obviyus merged 9 commits into
openclaw:mainfrom
DakshK-02:fix/gateway-session-store-list-projection

Conversation

@DakshK-02

@DakshK-02 DakshK-02 commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Run lookups decoded saved prompts from every stored session, causing unnecessary memory growth.

Why This Change Was Made

The combined Gateway reader now defaults to its existing metadata projection. Usage reports and the published plugin transcript helper explicitly retain full entries. The combined reader owns the projection default; existing storage writers and cache ownership are unchanged.

User Impact

Large-store lookups use less memory without changing session selection or saved prompts.

Evidence

A real isolated Gateway held 200 synthetic sessions across two agents. Three matched 48-request samples reduced prompt-bearing parses from 4,814 to 14 in the first sample and from 4,804 to four in each later sample. A shared background health refresh adds ten parses when it overlaps a sample. The largest resident-memory increase sampled after completed requests fell from 357–457 MB to 4.6–21.6 MB. These samples include the Gateway and client in one process and do not measure continuous memory peaks or exact heap allocation. Metadata scans and intentional full-entry reads remain.

node scripts/run-vitest.mjs run src/gateway/sessions-resolve-projection.test.ts passes after merging current main. Its registered artifacts.list regression fails on main because unrelated prompts are decoded.

Compatibility

No schema, configuration, protocol, or public signature changes. Both original contributor commits remain. Separate full-store and startup/selected-entry reads are unchanged.

Consumers

Usage context reports and plugin transcript entries retain both saved prompt fields.

Invalidation

Existing cache invalidation, ownership, and incognito rules remain intact.

Tests

Known, missing, freshest, ambiguous, agent-scoped and incognito controls; usage and plugin preservation; 98 focused owner and sibling tests passed.

Production growth is eight lines: combined reader +2, usage +1, plugin wrapper +5. Each retains the projection contract above. Growth accepted under the owner's standing acceptance (2026-09-15).

@clawsweeper

clawsweeper Bot commented Sep 6, 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 size: S labels Sep 6, 2026
@DakshK-02
DakshK-02 marked this pull request as ready for review September 6, 2026 22:42
@DakshK-02 DakshK-02 changed the title fix(gateway): default combined session-store reads to the list projection fix(gateway): whole-store session lookups OOM the gateway at scale Sep 6, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 6, 2026
@clawsweeper

clawsweeper Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 16, 2026, 2:38 AM ET / 06:38 UTC (Revision 13).

ClawSweeper review

What this changes

Gateway session lookups use lightweight session metadata by default, while usage reports and the published plugin transcript helper retain complete saved prompt data.

Merge readiness

✅ Ready for maintainer review

This remains a useful, focused performance fix: current main and v2026.9.4 retain the expensive default, and the related PRs address complementary paths. No introduced correctness defect was found, and the recorded Gateway measurements provide sufficient behavior proof.

Priority: P2
Reviewed head: 3990f090eef6c9a4031c71d7caac9778d8a1c172

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with credible Gateway measurements, preserved consumer contracts, and no actionable correctness findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The captured body records matched before/after requests through a real isolated Gateway with 200 stored sessions, demonstrating sharply fewer prompt-bearing parses and lower sampled memory growth on the changed lookup path; regression tests supplement these observations.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured body records matched before/after requests through a real isolated Gateway with 200 stored sessions, demonstrating sharply fewer prompt-bearing parses and lower sampled memory growth on the changed lookup path; regression tests supplement these observations.
Evidence reviewed 9 items Current main still needs the change: The fetched main revision still defaults the combined Gateway reader to "full" at line 623; the reviewed branch changes that default to "list".
Latest release retains the expensive default: The v2026.9.4 source also defaults this reader to "full", at release-source line 551.
Existing projection and cache owner: The existing SQLite metadata query removes skillsSnapshot and systemPromptReport before JavaScript decoding. The existing entry-cache owner retains metadata, bypasses caching for full reads, and validates database generations. This patch reuses those contracts without changing storage or authority.
Findings None None.
Security None None.

How this fits together

The Gateway combines session records from agent databases to resolve run identifiers and serve session information. Its projection choice controls whether those reads also decode saved prompts.

flowchart TD
  A[Agent session databases] --> B[Combined Gateway reader]
  C[Lookup or report request] --> B
  B --> D{Prompt data needed?}
  D -->|No| E[Session metadata]
  D -->|Yes| F[Complete session entries]
  E --> G[Run and worker lookup]
  F --> H[Usage reports and plugin results]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +8, Tests +270. Total +278 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 3 14 6 +8
Tests 4 274 4 +270
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 288 10 +278

Review metrics

Metric Value Why it matters
Production versus regression coverage production +8 net lines; tests +270 net lines Production growth is justified by documenting the default and preserving the two full-entry consumer contracts.

Technical review

Best possible solution:

Keep projection selection with the combined reader and preserve explicit full reads wherever saved prompt data is part of the consumer contract.

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

Yes: an uncached run lookup reaches the full combined-store reader on current main and decodes unrelated saved prompts. Source inspection and the recorded Gateway comparison establish the path; this review did not execute it.

Is this the best way to solve the issue?

Yes: the patch selects an existing lightweight projection at its owner and preserves full-entry consumers without adding a competing cache, API, or migration.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 6e2b9ea34516.

Labels

Label justifications:

  • P2: This is a bounded Gateway memory improvement for large session stores, supported by measured reductions in unnecessary prompt decoding.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured body records matched before/after requests through a real isolated Gateway with 200 stored sessions, demonstrating sharply fewer prompt-bearing parses and lower sampled memory growth on the changed lookup path; regression tests supplement these observations.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured body records matched before/after requests through a real isolated Gateway with 200 stored sessions, demonstrating sharply fewer prompt-bearing parses and lower sampled memory growth on the changed lookup path; regression tests supplement these observations.

Evidence

What I checked:

  • Current main still needs the change: The fetched main revision still defaults the combined Gateway reader to "full" at line 623; the reviewed branch changes that default to "list". (src/config/sessions/combined-store-gateway.ts:623, 6e2b9ea34516)
  • Latest release retains the expensive default: The v2026.9.4 source also defaults this reader to "full", at release-source line 551. (src/config/sessions/combined-store-gateway.ts:551, 3a9d69db306c)
  • Existing projection and cache owner: The existing SQLite metadata query removes skillsSnapshot and systemPromptReport before JavaScript decoding. The existing entry-cache owner retains metadata, bypasses caching for full reads, and validates database generations. This patch reuses those contracts without changing storage or authority. (src/config/sessions/session-accessor.sqlite-status.ts:33, 3990f090eef6)
  • Consumers preserve complete-entry contracts: Usage explicitly requests full entries because its response reads systemPromptReport for both contextWeight and hasContextWeight. The plugin transcript helper likewise pins full entries and retains incognito exclusion. Worker resolution performs a separate full read after selecting its target. (src/gateway/server-methods/usage-session-selection.ts:207, 3990f090eef6)
  • Recorded production-path measurements: The captured PR body, sourceRevision a13a4cbdfb16a2bc7f0552b40d17cec70215268db325c16c6dbabb67b3d8d217, reports an isolated real Gateway with 200 synthetic sessions across two agents and three matched 48-request samples. Prompt-bearing parses fell from 4,814 to 14 and subsequently from 4,804 to four; sampled post-request resident-memory growth fell from 357–457 MB to 4.6–21.6 MB. The body explicitly distinguishes these observations from continuous peak or exact allocation measurements. The contributor confirms obviyus supplied the Gateway measurements in fix(gateway): reduce memory growth during session lookups #140499 (comment).
  • Regression coverage and review continuity: Read the introduced tests covering the registered artifacts.list handler, missing runs, freshest matching session, worker payload preservation, and complete non-incognito plugin entries. The three production files and Gateway projection test are unchanged since the previous reviewed head. The previous review retained no findings; no new blocker was identified. Tests were inspected, not executed during this read-only review. (src/gateway/sessions-resolve-projection.test.ts:293, 3990f090eef6)

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)
  • obviyus: 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 (12 earlier review cycles; latest 8 shown)
  • reviewed 2026-09-09T16:56:14.668Z sha 71cac6d :: needs maintainer review before merge. :: none
  • reviewed 2026-09-09T17:03:45.742Z sha 71cac6d :: needs maintainer review before merge. :: none
  • reviewed 2026-09-12T06:19:15.342Z sha 71cac6d :: needs maintainer review before merge. :: none
  • reviewed 2026-09-14T19:38:15.214Z sha 2caeeb5 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-15T12:34:15.822Z sha 11b05b4 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T03:05:00.741Z sha 595dc47 :: needs changes before merge. :: none
  • reviewed 2026-09-16T03:16:21.259Z sha 595dc47 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T05:15:33.438Z sha b0466cc :: needs maintainer review before merge. :: none

@DakshK-02
DakshK-02 force-pushed the fix/gateway-session-store-list-projection branch from 7309ac3 to 0569aa4 Compare September 7, 2026 21:11
@DakshK-02 DakshK-02 changed the title fix(gateway): whole-store session lookups OOM the gateway at scale fix(gateway): whole-store session lookups exhaust gateway heap at scale Sep 7, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 7, 2026
@obviyus obviyus self-assigned this Sep 9, 2026
@obviyus
obviyus requested review from a team as code owners September 9, 2026 16:47
@obviyus obviyus changed the title fix(gateway): whole-store session lookups exhaust gateway heap at scale fix(gateway): reduce memory growth during session lookups Sep 9, 2026
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Sep 9, 2026
@DakshK-02

Copy link
Copy Markdown
Contributor Author

Thanks for taking this over, and for the test covering the plugin-SDK view — that was the gap I'd left.

Red CI looks unrelated to the change: two 120s timeouts in extension tests this branch doesn't touch (extensions/discord/src/monitor/native-command.reset.test.ts and extensions/codex/src/app-server/run-attempt.configured-mcp.test.ts, 1 failed / 3702 passed in that shard), and ci-gate is red only via checks-node-core-test-nondist-shard. The PR context and evidence failure looks stale too — it ran while the body was mid-edit and passes against the body as it stands now.

I don't have re-run rights here. Happy to close/reopen to re-trigger, or push an empty commit — whichever you prefer. Anything you need from me?

@DakshK-02 DakshK-02 closed this Sep 12, 2026
@DakshK-02 DakshK-02 reopened this Sep 12, 2026
@DakshK-02

Copy link
Copy Markdown
Contributor Author

Status update, plus a request for guidance on next steps.

@obviyus picked this up on 2026-09-09 — merged main in, rewrote the body with his own Gateway measurements, and added session-transcript-hit.projection.test.ts covering the pinned plugin-SDK view. Thanks again for that. I haven't heard back since, and I don't have re-run rights on this repo, so I'm unsure how to move it forward.

Where it stands:

  • ClawSweeper: 🐚 platinum hermit (4/6), proof 🦞 diamond lobster, Before merge: None
  • Branch is current with main (head 11b05b40f3)
  • The red CI is main-side, not this change. The latest run (34967899642, created 12:16:26Z) failed on two files this PR does not touch, both fixed on main shortly afterwards:

Separately, #149118 looks complementary to this rather than overlapping — no shared files. Its readCachedExactSessionEntries only reuses a complete entry cache, and on main readSessionEntryCache skips caching entirely when projection === "full" (session-accessor.sqlite-entry-cache.ts:242). This PR flips the Gateway store-wide default to "list", which is what populates that complete cache — so on Gateway lookup paths the two appear to compose rather than conflict. Happy to be corrected if I've misread it.

Could someone advise what's needed from me to get this merged, or re-run CI against current main? I'm glad to rebase, narrow the scope, or split it if that helps.

@obviyus
obviyus marked this pull request as draft September 16, 2026 02:58
@obviyus

obviyus commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

landpr-approve-admin-merge
pr: 140499
head: 595dc47
scope: owner-review

@obviyus
obviyus marked this pull request as ready for review September 16, 2026 03:09
@obviyus

obviyus commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

landpr-approve-admin-merge
pr: 140499
head: b0466cc
scope: owner-review

@obviyus

obviyus commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

landpr-approve-admin-merge
pr: 140499
head: 3990f09
scope: owner-review

@obviyus
obviyus merged commit 1e5ebed into openclaw:main Sep 16, 2026
378 of 381 checks passed
@obviyus

obviyus commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Thanks @DakshK-02!

Proof: Real Gateway requests against 200 stored sessions preserved lookup results, usage reports, and plugin prompt fields. Repeated 48-request samples reduced prompt-bearing parses from 4,804 to four; a shared health refresh contributed ten additional parses when it overlapped a sample.

  • Bug: Run lookups decoded saved prompts from unrelated sessions and increased memory use.
  • Fix: Session selection now uses metadata while consumers that need saved prompts retain full entries.
Before After
357–457 MB sampled resident-memory growth 4.6–21.6 MB sampled resident-memory growth

@DakshK-02

Copy link
Copy Markdown
Contributor Author

Thanks @obviyus !!!!
Pleasure is all mine

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 17, 2026
…aw#140499)

## What Problem This Solves

Run lookups decoded saved prompts from every stored session, causing unnecessary memory growth.

## Why This Change Was Made

The combined Gateway reader now defaults to its existing metadata projection. Usage reports and the published plugin transcript helper explicitly retain full entries. The combined reader owns the projection default; existing storage writers and cache ownership are unchanged.

## User Impact

Large-store lookups use less memory without changing session selection or saved prompts.

## Evidence

A real isolated Gateway held 200 synthetic sessions across two agents. Three matched 48-request samples reduced prompt-bearing parses from 4,814 to 14 in the first sample and from 4,804 to four in each later sample. A shared background health refresh adds ten parses when it overlaps a sample. The largest resident-memory increase sampled after completed requests fell from 357–457 MB to 4.6–21.6 MB. These samples include the Gateway and client in one process and do not measure continuous memory peaks or exact heap allocation. Metadata scans and intentional full-entry reads remain.

`node scripts/run-vitest.mjs run src/gateway/sessions-resolve-projection.test.ts` passes after merging current main. Its registered `artifacts.list` regression fails on main because unrelated prompts are decoded.

## Compatibility

No schema, configuration, protocol, or public signature changes. Both original contributor commits remain. Separate full-store and startup/selected-entry reads are unchanged.

## Consumers

Usage context reports and plugin transcript entries retain both saved prompt fields.

## Invalidation

Existing cache invalidation, ownership, and incognito rules remain intact.

## Tests

Known, missing, freshest, ambiguous, agent-scoped and incognito controls; usage and plugin preservation; 98 focused owner and sibling tests passed.

Production growth is eight lines: combined reader +2, usage +1, plugin wrapper +5. Each retains the projection contract above. Growth accepted under the owner's standing acceptance (2026-09-15).

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary 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.

2 participants