[PR #5500] [Engine] Prototype incremental and prefix-readable session persistence #5472

Open
opened 2026-11-07 22:42:35 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5500
Author: @giphoo
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: l2-reference


📝 Commits (1)

  • 4277f9d Add L2 session persistence reference implementation

📊 Changes

26 files changed (+6325 additions, -267 deletions)

View changed files

📝 CMakeLists.txt (+15 -1)
➕ docs/l2-design.md (+206 -0)
➕ docs/l2-guide.md (+212 -0)
📝 include/strata/core/conversation_cache.hpp (+32 -3)
➕ include/strata/core/conversation_cache_l2.inc (+117 -0)
📝 include/strata/core/conversation_file.hpp (+49 -0)
📝 include/strata/core/conversation_snapshot.hpp (+5 -1)
➕ include/strata/core/ple_metrics.hpp (+91 -0)
📝 include/strata/kernels/ngram.hpp (+3 -0)
➕ l2/bench_rotation.py (+206 -0)
➕ l2/cache_control.py (+187 -0)
➕ l2/reference_controller.py (+357 -0)
➕ l2/test-deviation-contracts.py (+744 -0)
📝 serve/server.py (+283 -22)
➕ src/core/conversation_cache_l2_test.cpp (+56 -0)
📝 src/core/conversation_cache_test.cpp (+42 -0)
📝 src/core/conversation_file.cpp (+101 -5)
➕ src/core/conversation_file_l2.inc (+461 -0)
📝 src/core/conversation_file_test.cpp (+595 -0)
➕ src/core/conversation_file_v2.inc (+1683 -0)

...and 6 more files

📄 Description

Problem and result

Long agent conversations that alternate between sessions pay repeated prefill costs when the conversation is no longer in the RAM cache. Existing session files write and read whole snapshots, which amplifies I/O when only a small part changes or only a prefix is needed.

This draft adds:

  • a block-based v2 session format with LZ4 compression;
  • incremental saves that reuse unchanged checkpoint and K/V segments;
  • checkpoint-prefix restore into the existing PARKED RAM cache;
  • an asynchronous writer with per-file generation commits;
  • a read-only probe for cache availability;
  • a minimal reference controller and rotation benchmark.

All features are opt-in. Default behavior is unchanged.

  • #57 (multi-conversation caching, solved by #189 for RAM)
  • #143 (OpenClaw agent full-prompt rereads)
  • #190 (disk tier PR, closed pending rebase)
  • #3027 (disk durability for parked conversations)
  • #3485 (incremental disk-tier measurements)
  • #4021 (tail-only parking, complementary RAM optimization)

Approach

The engine owns all KV state and transfers. The controller only decides when to save and restore. The key primitives are:

  1. SAVEINC: plan against the old file, capture only fresh state, append in place, fall back to full save on any mismatch.
  2. RESTOREP: stream a v2 file directly into a PARKED entry (no GPU transfer, no active-session replacement).
  3. RESTOREPP / preload: read only the checkpoint prefix that matches the incoming prompt, as a PARKED entry.
  4. PROBE: read-only query of how much the cache would return.
  5. WriterPool: bounded async append queue with per-generation commit tracking, synchronous fallback on budget exceeded.

A minimal reference controller (~300 lines, l2/reference_controller.py) demonstrates the full save ? preload ? generate ? saveinc loop. A rotation benchmark (l2/bench_rotation.py) measures reuse rates across a three-conversation workload with engine restart.

Validation

Passed (reference implementation, Linux/HIP, AMD W7900 48 GB, Qwen3.8-Flash-Next UD-IQ4_XS, v0.2.12/engine 0.1.58):

  • 36/36 L2 contract tests
  • 5/5 core C++ tests (conversation_cache/memory/file/reclaim/cache_l2)
  • 99/99 upstream server tests (tool calls, request accounting, Monitor)
  • 51-request three-conversation rotation: 0 LOW-REUSE, 0 restore 400/404, 0 OOM/segfault
  • 9 restores, P50=3.41s, P90=7.48s
  • Writer final pending=0, 5 save commits committed

Failed:

  • HIP flag-order test on gfx1201 (device enumeration order); passed with HIP_VISIBLE_DEVICES=1 on W7900

Not run:

  • CUDA build or execution
  • SYCL build or execution
  • Metal build or execution
  • Windows build or execution
  • Power-loss / SIGKILL recovery for SAVEINC
  • Multi-GPU / layer-split / peer-device / batch configurations

Limits

  • In-place SAVEINC is not power-loss atomic. A crash mid-append can leave the file unreadable. Recovery needs a journal, dual footer, or COW design.
  • File I/O uses POSIX APIs directly (open/pread/pwrite/fsync). Windows needs a separate implementation.
  • The default checkpoint tolerance is currently enabled globally in conversation_snapshot_restore(); it should be scoped to the L2 opt-in path only.
  • Writer memory accounting approximates metadata as 64 MiB; fresh checkpoints may exceed this.
  • The reference controller is a demonstration, not a production gateway (no auth, no CORS, no scheduler).
  • No message-content retention is included.

This is a draft for design discussion. I can split it into smaller PRs based on feedback.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5500 **Author:** [@giphoo](https://github.com/giphoo) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `l2-reference` --- ### 📝 Commits (1) - [`4277f9d`](https://github.com/Niko1221/Strata/commit/4277f9d21122f6ea5e46216b8d3afb5fc916e340) Add L2 session persistence reference implementation ### 📊 Changes **26 files changed** (+6325 additions, -267 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+15 -1) ➕ `docs/l2-design.md` (+206 -0) ➕ `docs/l2-guide.md` (+212 -0) 📝 `include/strata/core/conversation_cache.hpp` (+32 -3) ➕ `include/strata/core/conversation_cache_l2.inc` (+117 -0) 📝 `include/strata/core/conversation_file.hpp` (+49 -0) 📝 `include/strata/core/conversation_snapshot.hpp` (+5 -1) ➕ `include/strata/core/ple_metrics.hpp` (+91 -0) 📝 `include/strata/kernels/ngram.hpp` (+3 -0) ➕ `l2/bench_rotation.py` (+206 -0) ➕ `l2/cache_control.py` (+187 -0) ➕ `l2/reference_controller.py` (+357 -0) ➕ `l2/test-deviation-contracts.py` (+744 -0) 📝 `serve/server.py` (+283 -22) ➕ `src/core/conversation_cache_l2_test.cpp` (+56 -0) 📝 `src/core/conversation_cache_test.cpp` (+42 -0) 📝 `src/core/conversation_file.cpp` (+101 -5) ➕ `src/core/conversation_file_l2.inc` (+461 -0) 📝 `src/core/conversation_file_test.cpp` (+595 -0) ➕ `src/core/conversation_file_v2.inc` (+1683 -0) _...and 6 more files_ </details> ### 📄 Description ## Problem and result Long agent conversations that alternate between sessions pay repeated prefill costs when the conversation is no longer in the RAM cache. Existing session files write and read whole snapshots, which amplifies I/O when only a small part changes or only a prefix is needed. This draft adds: - a block-based v2 session format with LZ4 compression; - incremental saves that reuse unchanged checkpoint and K/V segments; - checkpoint-prefix restore into the existing PARKED RAM cache; - an asynchronous writer with per-file generation commits; - a read-only probe for cache availability; - a minimal reference controller and rotation benchmark. All features are opt-in. Default behavior is unchanged. ## Related discussion - #57 (multi-conversation caching, solved by #189 for RAM) - #143 (OpenClaw agent full-prompt rereads) - #190 (disk tier PR, closed pending rebase) - #3027 (disk durability for parked conversations) - #3485 (incremental disk-tier measurements) - #4021 (tail-only parking, complementary RAM optimization) ## Approach The engine owns all KV state and transfers. The controller only decides when to save and restore. The key primitives are: 1. **SAVEINC**: plan against the old file, capture only fresh state, append in place, fall back to full save on any mismatch. 2. **RESTOREP**: stream a v2 file directly into a PARKED entry (no GPU transfer, no active-session replacement). 3. **RESTOREPP / preload**: read only the checkpoint prefix that matches the incoming prompt, as a PARKED entry. 4. **PROBE**: read-only query of how much the cache would return. 5. **WriterPool**: bounded async append queue with per-generation commit tracking, synchronous fallback on budget exceeded. A minimal reference controller (~300 lines, `l2/reference_controller.py`) demonstrates the full save ? preload ? generate ? saveinc loop. A rotation benchmark (`l2/bench_rotation.py`) measures reuse rates across a three-conversation workload with engine restart. ## Validation **Passed (reference implementation, Linux/HIP, AMD W7900 48 GB, Qwen3.8-Flash-Next UD-IQ4_XS, v0.2.12/engine 0.1.58):** - 36/36 L2 contract tests - 5/5 core C++ tests (conversation_cache/memory/file/reclaim/cache_l2) - 99/99 upstream server tests (tool calls, request accounting, Monitor) - 51-request three-conversation rotation: 0 LOW-REUSE, 0 restore 400/404, 0 OOM/segfault - 9 restores, P50=3.41s, P90=7.48s - Writer final pending=0, 5 save commits committed **Failed:** - HIP flag-order test on gfx1201 (device enumeration order); passed with `HIP_VISIBLE_DEVICES=1` on W7900 **Not run:** - CUDA build or execution - SYCL build or execution - Metal build or execution - Windows build or execution - Power-loss / SIGKILL recovery for SAVEINC - Multi-GPU / layer-split / peer-device / batch configurations ## Limits - In-place SAVEINC is not power-loss atomic. A crash mid-append can leave the file unreadable. Recovery needs a journal, dual footer, or COW design. - File I/O uses POSIX APIs directly (`open/pread/pwrite/fsync`). Windows needs a separate implementation. - The default checkpoint tolerance is currently enabled globally in `conversation_snapshot_restore()`; it should be scoped to the L2 opt-in path only. - Writer memory accounting approximates metadata as 64 MiB; fresh checkpoints may exceed this. - The reference controller is a demonstration, not a production gateway (no auth, no CORS, no scheduler). - No message-content retention is included. This is a draft for design discussion. I can split it into smaller PRs based on feedback. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5472
No description provided.