[PR #3565] [Engine] serve: auto sessions, an opt-in disk parking around a conversation switch #4691

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3565
Author: @drumblund
Created: 10/21/2026
Status: 🔄 Open

Base: main ← Head: serve-auto-sessions


📝 Commits (1)

  • 69e254e Serve: auto sessions, an opt-in disk parking around a conversation switch

📊 Changes

1 file changed (+169 additions, -0 deletions)

View changed files

📝 serve/server.py (+169 -0)

📄 Description

Branch serve-auto-sessions, 1 commit on origin/main @ 9de04cd4. serve/server.py only — no engine change.
Status: exercised end to end on macOS/Metal with a 512K IQ3_S engine (see Validation).

Problem and result

The engine has held a saved conversation across restarts for a while: --slot-save-path plus
POST /slots/0?action=save|restore write and read a session file, and SAVE <path> / RESTORE <path> on
the engine's control stream does the work. What was missing is anything that calls it.

So a single-slot service had exactly one way to survive a conversation switch: the in-RAM cache, bounded by
--conversation-cache-mib. That bound is easy to hit — at 512K a 27k-token conversation parks 651 MB, so a
24 GiB ceiling holds roughly thirty of them and then starts evicting. And a restart, or a conversation that
was evicted, pays a full re-read of a prompt that can be minutes of prefill.

After, with "auto_sessions": true:

before after
switch back to a conversation the RAM cache had dropped full re-read restored from its file
27k-token conversation, 512K config — saved 27,174 tokens (0.65 GB) in 223 ms, restored in 146 ms
normal service stop held conversation lost saved before the stop, restored by the next start

The restored answer matches the one given before the switch character for character.

none — no issue was opened for this. Off by default (auto_sessions is false unless the config says
otherwise), and every failure is logged and leaves the request alone.

Approach

The conversation is identified by its first user message. conversation_key() takes the text of the
first role: user message and returns sha1(text)[:16], or None when a request has no user text (a
tokenize-only call, say) so it stays out of the way. That is the same boundary the engine's prompt cache
sees, so the two agree on what "a different conversation" means.

_auto_session_turn() runs between requests, holding the service FIFO. When the incoming request's key
differs from the held one: save the held conversation to auto-<key>.bin (only if it reached
auto_sessions_min_tokens), then restore the incoming one if auto-<key>.bin exists. Same key → nothing
but a token count update. A save that fails does not skip the restore only in one case — a held conversation
with nothing durable, i.e. a cancelled request's provisional state, which the engine refuses as invalid
and which has no file worth protecting.

A failed step never fails the request. The whole body is wrapped: print(f"[strata] auto sessions: {e}")
and the request runs as it always did. In particular, if a save failed, the held conversation is still in
the engine and still parks in RAM — restoring over it would replace a full state with a weaker one.

A normal stop saves what the engine holds (auto_session_shutdown_save()), but only when the service is
idle: a running request's session is live and saving it would capture a partial state.

Pruning touches only its own files. _auto_session_prune() keeps the newest auto_sessions_keep files
and at most auto_sessions_max_gb of them, and deletes only names matching auto-*.bin — a file saved by
hand through /slots/0 is never removed.

Defaults, all overridable from the config: auto_sessions_min_tokens 16000, auto_sessions_keep 12,
auto_sessions_max_gb 100.

Validation

Passed:​

Environment: macOS 26.5.2, M5 Max 128 GB, engine 0.1.44.1-metal, IQ3_S 79 GB, 512K context,
--conversation-cache-mib 24576 --conversation-cache-slots 3, slot_save_path set,
auto_sessions_min_tokens 20000.

  • A 26,958-token conversation, then a request from a different conversation and back:
    auto sessions: saved 27,174 tokens (0.65 GB) to auto-<key>.bin in 223 ms,
    auto sessions: restored 27,174 tokens from auto-<key>.bin in 146 ms, answer identical.
  • Service stop with a conversation held: saved on shutdown, restored on the next start
    (cached=26,987 of 26,992 prompt tokens).
  • python3 -m py_compile serve/server.py clean.
  • Restore does not fire for conversations below auto_sessions_min_tokens: logged as skipped, request
    unaffected.

Failed:​ none observed.

Not run:​

  • A CUDA/HIP/SYCL engine. The change is serve/ only and does not depend on the backend, but this machine
    has no such hardware.
  • parallel / --batch setups: /slots/0 and this path are refused there by the pre-existing check, since
    a session file could interleave with a request being admitted.

Limits

  • It is a heuristic. A client that rewrites the first user message (a system-prompt edit prepended to
    the same question, a re-rendered template) starts what looks like a new conversation and gets a new file;
    one that keeps the text but changes everything after it reuses the old one. That matches what the prompt
    cache does, but it is a guess.
  • Disk, not RAM. The engine's --conversation-cache-mib pool is still the fast path; this is what covers
    a restart and a conversation the pool dropped. Both are worth having.
  • Depends on the engine's session file. On a Metal engine without #3564, SAVE fails with
    invalid checkpoint running-state payload — the server logs it and the request proceeds. On CUDA the path
    already works. This PR is independent of that one at the source level: serve/server.py only.

🔄 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/3565 **Author:** [@drumblund](https://github.com/drumblund) **Created:** 10/21/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `serve-auto-sessions` --- ### 📝 Commits (1) - [`69e254e`](https://github.com/Niko1221/Strata/commit/69e254ec4e59cb7b1e5233c63f53d35f8ebe5c00) Serve: auto sessions, an opt-in disk parking around a conversation switch ### 📊 Changes **1 file changed** (+169 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+169 -0) </details> ### 📄 Description > Branch `serve-auto-sessions`, 1 commit on `origin/main` @ `9de04cd4`. `serve/server.py` only — no engine change. > Status: exercised end to end on macOS/Metal with a 512K IQ3_S engine (see Validation). ## Problem and result The engine has held a saved conversation across restarts for a while: `--slot-save-path` plus `POST /slots/0?action=save|restore` write and read a session file, and `SAVE <path>` / `RESTORE <path>` on the engine's control stream does the work. What was missing is anything that *calls* it. So a single-slot service had exactly one way to survive a conversation switch: the in-RAM cache, bounded by `--conversation-cache-mib`. That bound is easy to hit — at 512K a 27k-token conversation parks 651 MB, so a 24 GiB ceiling holds roughly thirty of them and then starts evicting. And a restart, or a conversation that was evicted, pays a full re-read of a prompt that can be minutes of prefill. After, with `"auto_sessions": true`: | | before | after | |---|---|---| | switch back to a conversation the RAM cache had dropped | full re-read | restored from its file | | 27k-token conversation, 512K config | — | `saved 27,174 tokens (0.65 GB) in 223 ms`, `restored in 146 ms` | | normal service stop | held conversation lost | saved before the stop, restored by the next start | The restored answer matches the one given before the switch character for character. ## Related discussion none — no issue was opened for this. Off by default (`auto_sessions` is false unless the config says otherwise), and every failure is logged and leaves the request alone. ## Approach **The conversation is identified by its first user message.** `conversation_key()` takes the text of the first `role: user` message and returns `sha1(text)[:16]`, or `None` when a request has no user text (a tokenize-only call, say) so it stays out of the way. That is the same boundary the engine's prompt cache sees, so the two agree on what "a different conversation" means. **`_auto_session_turn()` runs between requests, holding the service FIFO.** When the incoming request's key differs from the held one: save the held conversation to `auto-<key>.bin` (only if it reached `auto_sessions_min_tokens`), then restore the incoming one if `auto-<key>.bin` exists. Same key → nothing but a token count update. A save that fails does not skip the restore only in one case — a held conversation with nothing durable, i.e. a cancelled request's provisional state, which the engine refuses as `invalid` and which has no file worth protecting. **A failed step never fails the request.** The whole body is wrapped: `print(f"[strata] auto sessions: {e}")` and the request runs as it always did. In particular, if a save failed, the held conversation is still in the engine and still parks in RAM — restoring over it would replace a full state with a weaker one. **A normal stop saves what the engine holds** (`auto_session_shutdown_save()`), but only when the service is idle: a running request's session is live and saving it would capture a partial state. **Pruning touches only its own files.** `_auto_session_prune()` keeps the newest `auto_sessions_keep` files and at most `auto_sessions_max_gb` of them, and deletes only names matching `auto-*.bin` — a file saved by hand through `/slots/0` is never removed. Defaults, all overridable from the config: `auto_sessions_min_tokens` 16000, `auto_sessions_keep` 12, `auto_sessions_max_gb` 100. ## Validation **Passed:**&#8203; *Environment:* macOS 26.5.2, M5 Max 128 GB, engine `0.1.44.1-metal`, IQ3_S 79 GB, 512K context, `--conversation-cache-mib 24576 --conversation-cache-slots 3`, `slot_save_path` set, `auto_sessions_min_tokens` 20000. - A 26,958-token conversation, then a request from a different conversation and back: `auto sessions: saved 27,174 tokens (0.65 GB) to auto-<key>.bin in 223 ms`, `auto sessions: restored 27,174 tokens from auto-<key>.bin in 146 ms`, answer identical. - Service stop with a conversation held: saved on shutdown, restored on the next start (`cached=26,987` of 26,992 prompt tokens). - `python3 -m py_compile serve/server.py` clean. - Restore does not fire for conversations below `auto_sessions_min_tokens`: logged as skipped, request unaffected. **Failed:**&#8203; none observed. **Not run:**&#8203; - A CUDA/HIP/SYCL engine. The change is `serve/` only and does not depend on the backend, but this machine has no such hardware. - `parallel` / `--batch` setups: `/slots/0` and this path are refused there by the pre-existing check, since a session file could interleave with a request being admitted. ## Limits - **It is a heuristic.** A client that rewrites the first user message (a system-prompt edit prepended to the same question, a re-rendered template) starts what looks like a new conversation and gets a new file; one that keeps the text but changes everything after it reuses the old one. That matches what the prompt cache does, but it is a guess. - **Disk, not RAM.** The engine's `--conversation-cache-mib` pool is still the fast path; this is what covers a restart and a conversation the pool dropped. Both are worth having. - **Depends on the engine's session file.** On a Metal engine without #3564, `SAVE` fails with `invalid checkpoint running-state payload` — the server logs it and the request proceeds. On CUDA the path already works. This PR is independent of that one at the source level: `serve/server.py` only. --- <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#4691
No description provided.