[PR #5211] [Other] Serve: session_autosave, the conversation cache's L2 on disk #5346

Open
opened 2026-11-07 16:34:31 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5211
Author: @Flo5k5
Created: 11/4/2026
Status: 🔄 Open

Base: main ← Head: session-autosave


📝 Commits (1)

  • 3a64710 Serve: session_autosave, the conversation cache's L2 on disk

📊 Changes

3 files changed (+318 additions, -0 deletions)

View changed files

📝 serve/server.py (+83 -0)
➕ serve/session_persist.py (+85 -0)
➕ serve/test_session_persist.py (+150 -0)

📄 Description

Problem and result

A resumed conversation re-reads its whole prompt. On our M5 Pro 64 GB with Gyro-M (TQ2_0, expert-cache 24576), taking a 34K-token conversation back costs ~41 s of prompt reading before the first token. The conversation cache (#596) parks finished conversations in RAM and reuses them when the next prompt shares their tokens, but RAM evictions drop exactly the conversations a long session still needs, and nothing survives a server restart. /slots/0?action=save|restore already moves one whole session to or from disk by hand; the engine parks conversations by itself.

With this change (opt-in), each finished turn's conversation is saved to slot_save_path automatically, and a later prompt that begins with a saved conversation restores it before the request: the engine reads only the tokens past it - the resumed 34K conversation reads its new turn, not its history.

Context on our setup and the 64 GB fit: #5176 (the issue where the host-side improvements this belongs to were outlined). Maintainer's agreement on scope: not yet - this is a draft to settle the open questions below before anything is finalized.

Approach

The engine's two existing halves are joined with no engine change:

  • At the end of a finished turn, the service saves the engine's live conversation under auto-<digest>.bin in slot_save_path and records (len, digest, file, bytes) in index.json (LRU, capped by "session_persist_max_gb", default 8).
  • When a later prompt begins with a saved conversation's tokens, the service RESTOREs that snapshot before the request. A RESTORE makes the saved context the engine's live one, so the GEN that follows diffs against it.

The key is the digest of the PROMPT's tokens, not prompt + reply: a chat template renders message by message, so turn n's whole prompt is a prefix of turn n+1's, whatever the reply was. Matching takes the incoming prompt's digest at each recorded length (one sha256 per candidate); the engine's own prefix match absorbs a smaller divergence at the tail (verified on the stock chat template: turn 1's 249 prompt tokens are an exact prefix of turn 2's 299).

Skips: a restore of a conversation the engine already holds (its own prefix match reads nothing then); a save when the live conversation's size is not this turn's prompt plus its reply (another request came in between the turn and the save). A refused or failed save/restore never breaks the request - the prompt is then read in full, as without the option. Off unless --session-autosave / "session_autosave": true (needs slot_save_path and an engine with session files).

Validation

Passed:

  • python -m unittest serve.test_session_persist -v - revision 3a647107, Python 3.15 on macOS, mock engine (no GPU): 5 tests OK. The turn is saved under its prompt's digest; a prompt continuing the live conversation restores nothing; a returning prompt restores its saved conversation (and only it) after another conversation became the live one; an unrelated prompt restores nothing; the index LRU-trims to its cap and reloads without the dropped file.
  • python -m unittest serve.test_slots - same revision: 30 tests OK (the manual API unchanged).
  • python -m unittest serve.test_server - same revision: 359 tests OK (the option is inert unless enabled).

Failed:

  • none observed.

Not run:

  • Real-hardware measurement (Gyro-M: restore vs full re-read, the 41 s -> ? number) - follows once the placement question below is settled.
  • Windows and Linux host runs: host-only change, but only macOS was available here.

Limits

Draft on four open questions:

  1. Where the save runs. It takes the FIFO after the reply finished (the SAVE line is between requests). Between the turn's end and the save, another request may take the FIFO and leave a different conversation live - the size guard skips the save then, but the cleaner place would be inside the last turn's FIFO section. Where would you want it?
  2. The reply's latency. A non-streamed reply waits for the save (the file is written before the response). Acceptable for a first version, or should the save wait for the next idle gap?
  3. Batch/parallel (#465). slot_action refuses save/restore under --batch; this keeps the same rule implicitly (one slot). Right call for autosave too?
  4. Snapshot size. SAVED reports ~1.2 GB per 63K-token conversation on our model - the LRU cap keeps that sane. Is a delta format (snapshots of the same conversation chain) worth it upstream, or out of scope?

The prefix matching assumes the chat template renders message by message (true of the stock template; a template that rewrites earlier turns would not match, and the request then reads its prompt in full - no failure, just no saving).


🔄 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/5211 **Author:** [@Flo5k5](https://github.com/Flo5k5) **Created:** 11/4/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `session-autosave` --- ### 📝 Commits (1) - [`3a64710`](https://github.com/Niko1221/Strata/commit/3a64710760ca3b96806035cc64a4f8a01b222ab1) Serve: session_autosave, the conversation cache's L2 on disk ### 📊 Changes **3 files changed** (+318 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+83 -0) ➕ `serve/session_persist.py` (+85 -0) ➕ `serve/test_session_persist.py` (+150 -0) </details> ### 📄 Description ## Problem and result A resumed conversation re-reads its whole prompt. On our M5 Pro 64 GB with Gyro-M (TQ2_0, expert-cache 24576), taking a 34K-token conversation back costs ~41 s of prompt reading before the first token. The conversation cache (#596) parks finished conversations in RAM and reuses them when the next prompt shares their tokens, but RAM evictions drop exactly the conversations a long session still needs, and nothing survives a server restart. `/slots/0?action=save|restore` already moves one whole session to or from disk by hand; the engine parks conversations by itself. With this change (opt-in), each finished turn's conversation is saved to `slot_save_path` automatically, and a later prompt that begins with a saved conversation restores it before the request: the engine reads only the tokens past it - the resumed 34K conversation reads its new turn, not its history. ## Related discussion Context on our setup and the 64 GB fit: #5176 (the issue where the host-side improvements this belongs to were outlined). Maintainer's agreement on scope: not yet - this is a draft to settle the open questions below before anything is finalized. ## Approach The engine's two existing halves are joined with no engine change: - At the end of a finished turn, the service saves the engine's live conversation under `auto-<digest>.bin` in `slot_save_path` and records `(len, digest, file, bytes)` in `index.json` (LRU, capped by `"session_persist_max_gb"`, default 8). - When a later prompt begins with a saved conversation's tokens, the service RESTOREs that snapshot before the request. A RESTORE makes the saved context the engine's live one, so the GEN that follows diffs against it. The key is the digest of the PROMPT's tokens, not prompt + reply: a chat template renders message by message, so turn n's whole prompt is a prefix of turn n+1's, whatever the reply was. Matching takes the incoming prompt's digest at each recorded length (one sha256 per candidate); the engine's own prefix match absorbs a smaller divergence at the tail (verified on the stock chat template: turn 1's 249 prompt tokens are an exact prefix of turn 2's 299). Skips: a restore of a conversation the engine already holds (its own prefix match reads nothing then); a save when the live conversation's size is not this turn's prompt plus its reply (another request came in between the turn and the save). A refused or failed save/restore never breaks the request - the prompt is then read in full, as without the option. Off unless `--session-autosave` / `"session_autosave": true` (needs `slot_save_path` and an engine with session files). ## Validation **Passed:** - `python -m unittest serve.test_session_persist -v` - revision 3a647107, Python 3.15 on macOS, mock engine (no GPU): 5 tests OK. The turn is saved under its prompt's digest; a prompt continuing the live conversation restores nothing; a returning prompt restores its saved conversation (and only it) after another conversation became the live one; an unrelated prompt restores nothing; the index LRU-trims to its cap and reloads without the dropped file. - `python -m unittest serve.test_slots` - same revision: 30 tests OK (the manual API unchanged). - `python -m unittest serve.test_server` - same revision: 359 tests OK (the option is inert unless enabled). **Failed:** - none observed. **Not run:** - Real-hardware measurement (Gyro-M: restore vs full re-read, the 41 s -> ? number) - follows once the placement question below is settled. - Windows and Linux host runs: host-only change, but only macOS was available here. ## Limits Draft on four open questions: 1. **Where the save runs.** It takes the FIFO after the reply finished (the `SAVE` line is between requests). Between the turn's end and the save, another request may take the FIFO and leave a different conversation live - the size guard skips the save then, but the cleaner place would be inside the last turn's FIFO section. Where would you want it? 2. **The reply's latency.** A non-streamed reply waits for the save (the file is written before the response). Acceptable for a first version, or should the save wait for the next idle gap? 3. **Batch/parallel (#465).** `slot_action` refuses save/restore under `--batch`; this keeps the same rule implicitly (one slot). Right call for autosave too? 4. **Snapshot size.** `SAVED` reports ~1.2 GB per 63K-token conversation on our model - the LRU cap keeps that sane. Is a delta format (snapshots of the same conversation chain) worth it upstream, or out of scope? The prefix matching assumes the chat template renders message by message (true of the stock template; a template that rewrites earlier turns would not match, and the request then reads its prompt in full - no failure, just no saving). --- <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#5346
No description provided.