Skip to content

improve: copy less session data when restoring warm prompt caches - #206384

Merged
vincentkoc merged 3 commits into
mainfrom
perf/prompt-cache-recovery-keys-20261127
Nov 27, 2026
Merged

vincentkoc merged 3 commits into
mainfrom
perf/prompt-cache-recovery-keys-20261127

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

When the Gateway first checks which prompt caches to keep warm, it requests every saved session’s summary. A store with hundreds of inactive sessions sends their metadata back to the Gateway even if only a few sessions qualify.

User Impact

The Gateway can resume an existing keep-warm series or find a recently active chat by receiving only the matching session keys. This reduces the amount of data copied into the Gateway after startup. Existing installations pick up the change automatically.

Why This Change Was Made

The session worker already owns database selection and canonical validation. It now checks the original activity and last-ping timestamps before returning keys. A single read snapshot and ordered 64-row pages bound the candidate scan, including on PostgreSQL, which buffers each query. The worker still checks cold rows for invalid delivery aliases. Memory-only sessions use their existing actor and permissions.

Evidence

  • On Linux x64 with Node 24.22.1, both readers selected the same two keys from 133 synthetic sessions. The original worker returned 133 summaries. The new response contained those two keys and zero summaries. Serializing the complete returned objects with Node V8 measured 189,142 bytes versus 330 bytes. This measures response size. Peak process RSS was outside this test.

  • The original full list, the key-only read, a repeat key-only read and a repeat full list took 40.90, 3.46, 1.62 and 1.36 ms in one retained worker. These ordered timings include a first read and cached repeats.

  • The focused Node suites passed all 11 cases, including the exact cutoff, a recent preserved ping with old activity, stale side-column values, a dirty alias elsewhere in the store, memory-only actor ownership, and deferral under host memory pressure.

  • PostgreSQL 17 read 130 persisted sessions in pages of 64, 64 and 2, returned the three expected keys in order, and opened one repeatable-read, read-only transaction. The unmodified canonical admission may still scan an unreconciled store before this selection.

  • All nine TypeScript files passed lint. Core typechecking and the affected session tests’ typecheck passed.

  • Local autoreview covered the full branch at signed head 2bb29a1de72589a07360a07de68ef8f5d62ef032 against 535f39d01f699955e245f7ef6a5dfcd665b63604, priorities P0–P2. It reported no actionable findings.

  • The new conformance test passed on the first hosted PostgreSQL run. Its addition placed two existing tests on the same database shard, exposing an unscoped schema comparison. The fixture repair is included in the new base. The complete feature source and test patch is byte-identical to the original native proof.

The broader NAS memory report remains open. #192547

The hosted review suggested an isolated Gateway restart as an additional demonstration. The scoped proof uses the real retained session worker, both database engines and the scheduler tests. A populated Gateway restart and process memory measurement remain open follow-ups for the NAS report.

Select eligible keys inside the retained worker's read snapshot. Preserve recent prewarms and canonical session validation while avoiding the transfer of every saved summary to the Gateway.
@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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: M labels Nov 27, 2026
@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 27, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review November 27, 2026 04:38
@vincentkoc

Copy link
Copy Markdown
Member Author

@clawsweeper 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. 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
@vincentkoc
vincentkoc enabled auto-merge (squash) November 27, 2026 04:44
@clawsweeper

clawsweeper Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

Select warm-cache recovery candidates inside the session worker and return their keys instead of every session summary.

Example: A store contains 133 sessions, with two eligible for warm-cache recovery.

  • Before: The worker returns 133 summaries, measured in the supplied probe at 189,142 serialized bytes.
  • After: The worker returns two matching keys and no summaries, measured at 330 serialized bytes.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The patch is focused and source-consistent, but required shipped-entry-point proof is absent.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Restoring warm-cache candidates copies inactive sessions' metadata into the Gateway, adding avoidable startup work.
Reason: The maintainer-authored direction removes measured response payload without adding configuration, changing storage, or altering eligibility.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR removes a concrete data-transfer cost, preserves candidate selection, and has no identified code defects.

Reviewed head: 2bb29a1de72589a07360a07de68ef8f5d62ef032

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide redacted terminal output or logs from a populated isolated Gateway restart showing the key-only recovery response and a resumed prewarm series; the supplied evidence also does not show a regression test failing on the base.
Agent review details

How this fits together

Cron maintenance discovers session stores after startup; the retained session reader selects eligible keys, which the existing scheduler uses to resume prompt-cache prewarming.

flowchart LR
  A[Cron maintenance] --> B[Recovery reader]
  B --> C[Retained session worker]
  C --> D[SQLite or PostgreSQL]
  D --> C
  C --> E[Eligible session keys]
  E --> F[Existing prewarm scheduler]
Loading

Technical review

Best possible solution:

Keep candidate selection in the existing session worker while preserving the current scheduler, validation, and persisted format.

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

Current-main source clearly transfers every session summary before filtering; the supplied native-worker comparison reports matching selected keys and substantially smaller serialized replies, but this review executed no target code.

Is this the best way to solve the issue?

Filtering at the existing worker boundary is a focused solution: both storage paths share eligibility, and the obsolete Gateway-side filter is removed.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 9e787b7b0c0a.

Provenance checked

  • Warm-cache recovery eligibility keeps the original intent (b36ba3f: Resume eligible prompt-cache prewarm series after Gateway restarts, including sessions with old activity and a recent preserved ping.)
  • Lazy worker implementation loading keeps the original intent (3de0a78: Avoid loading unrelated listing and command implementations during cold session recovery.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Ayaan Zaidi: Raw commit b36ba3f adds src/agents/prompt-cache-keep-warm.ts:143 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: b36ba3fed244; files: src/agents/prompt-cache-keep-warm.ts)
  • Peter Steinberger: Raw commit 3de0a78 adds src/config/sessions/session-entry-list.worker.ts:26 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 3de0a781925c; files: src/config/sessions/session-entry-list.worker.ts)
  • Vincent Koc: Raw commit 9f50802 adds src/agents/prompt-cache-keep-warm.ts:460 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9f50802605b4; files: src/agents/prompt-cache-keep-warm.ts)

PR surface

Source +97, Tests +213, Docs +1. Total +311 across 10 files. Added test files: 2.

View PR surface stats
Area Files Added Removed Net
Source 7 132 35 +97
Tests 2 213 0 +213
Docs 1 4 3 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 349 38 +311

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.

Review metrics

Metric Value Why it matters
Production versus test changes Production +132/-35 lines; tests +213/-0 lines Growth provides the worker selection path and distinct SQLite, PostgreSQL paging, alias-validation, and memory-actor coverage.

Labels

Label changes:

  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🧂 unranked krab, so this older rating label is no longer current.

Label justifications:

  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Supply the Gateway restart capture described in the proof assessment, redacting IPs, API keys, phone numbers, and non-public endpoints.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-11-27T04:44:02.458Z sha d6788d1 :: passed. :: none

Reviewed November 27, 2026, 12:21 AM ET / 05:21 UTC (Revision 2).

@vincentkoc
vincentkoc disabled auto-merge November 27, 2026 05:10
@vincentkoc

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

The feature branch now includes the PostgreSQL fixture isolation from #206391 at 2bb29a1de72589a07360a07de68ef8f5d62ef032. The production and test patch is byte-identical to the previously reviewed range.

@clawsweeper

clawsweeper Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Nov 27, 2026
@vincentkoc
vincentkoc merged commit b30a9cc into main Nov 27, 2026
134 checks passed
@vincentkoc
vincentkoc deleted the perf/prompt-cache-recovery-keys-20261127 branch November 27, 2026 05:39
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 28, 2026
…enclaw#206384)

* improve: copy less session data when restoring warm prompt caches

Select eligible keys inside the retained worker's read snapshot. Preserve recent prewarms and canonical session validation while avoiding the transfer of every saved summary to the Gateway.

* test: clarify the PostgreSQL recovery observation
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
* fix(runtime): defer optional work under host memory pressure

* style(infra): format memory pressure admission

* style(gateway): format the deferred prewarm check

(cherry picked from commit 9f50802)

Companion audit: #207993 selected later for publication fixture isolation. #206779 required if the new Cron host-memory fixture spoofs platform: preserve native SQLite namespace. #206041/#206230/#206308/#206746 are separate cgroup detection and optimization changes; #206322 is service-aware cron run budget, #206483 forks chat and #206384 changes a main-only warmer.

Release adaptation: Extend the stable optional-memory owner and its existing callers only. Preserve cgroup/Bun/macOS behavior and upstream threshold cases; omit main-only prompt warmer, cgroup diagnosis, and Cron platform-spoof changes.

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

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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.

1 participant