[PR #1937] [Engine] serve: parked conversations on the disk, optionally across restarts (--conversation-disk-dir, --conversation-disk-persist) #3765

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1937
Author: @xrip
Created: 10/11/2026
Status: 🔄 Open

Base: main ← Head: pr/conversation-disk-tier


📝 Commits (2)

  • 65a79b5 serve: parked conversations on the disk, optionally across restarts (--conversation-disk-dir, --conversation-disk-persist)
  • c6b9997 conversation disk: read a file back only when that is faster than reading the tokens it adds

📊 Changes

10 files changed (+1304 additions, -31 deletions)

View changed files

📝 CMakeLists.txt (+9 -1)
📝 docs/DETAILS.md (+53 -0)
📝 include/strata/core/conversation_cache.hpp (+7 -0)
➕ include/strata/core/conversation_disk.hpp (+153 -0)
📝 metal/CMakeLists.txt (+1 -0)
📝 serve/server.py (+4 -1)
➕ src/core/conversation_disk.cpp (+575 -0)
➕ src/core/conversation_disk_test.cpp (+335 -0)
📝 src/program/generate.cpp (+166 -28)
📝 sycl/CMakeLists.txt (+1 -1)

📄 Description

Problem and result

A conversation the RAM conversation cache evicts (its slots or its byte budget full) is dropped, and a client that comes back to it reads the whole prompt again: an agent that switches between its main conversation and sub-agents too large for the budget together reads ~150K tokens again on every switch. Every restart of the engine (an update, a settings change, a reboot, a poweroff) also reads every conversation from token 0 again.

  • Before: an evicted conversation, and every conversation at a restart, costs a full re-read (73.3 s for 165,558 tokens on the box below).
  • After, with --conversation-disk-dir DIR: the evicted conversation goes to DIR and comes back from there when a request continues it. With --conversation-disk-persist on|exit-only the conversations outlive the engine: the same 165,558-token conversation came back after a restart in 8.4 s, with the same answer.

The maintainer said in #1090 that one disk-persistence design will be chosen; this is one of them. It was closed in the 0.2.0-beta reset and is rebased here onto 0.2.1. #3404 (separate, in the conversation cache) drops stale parked copies of a conversation, which the disk tier would otherwise write out.

Approach

Off by default (opt-in options, help text and docs/DETAILS.md):

  • conversation_disk.hpp/.cpp: the tier. Each image (one per stage with --layer-split) is one session file, written and read with the session files' own code (size, header and hash checks, the free-space preflight). A background writer (nice 10 on Linux) means a switch never waits for the disk, and queued images can be read back from RAM. Every checkpoint is kept, as in RAM. --conversation-disk-mib N (default 32768): oldest out first.
  • conversation_cache.hpp: set_on_evict, so an evicted conversation goes to the tier instead of being dropped. A park that does not fit the cache's byte budget is also sent to the disk.
  • generate.cpp: options, wiring, the read back when the disk holds a longer prefix than the session / a batch slot / the RAM cache (it needs RAM for the whole image above --conversation-cache-min-free-mib, else the prompt is read again), and the request line says when it was reused from the disk.
  • --conversation-disk-persist on|exit-only: the files are bound to the session files' identity (model, engine version, settings), with an index per conversation, so the next start takes them in. Files of another identity are removed at start. On QUIT the engine saves the current conversation and the parked ones; while persist is on it ignores SIGINT/SIGTERM/SIGHUP and ends through its stdin, so a poweroff's SIGTERM does not cut the save short. The server waits up to 300 s for QUIT (serve/server.py, 4 lines). exit-only writes nothing while running and saves only at QUIT.
  • A file is read back only when that is faster than reading the tokens it adds, by this run's measured disk and prompt speeds (the match reports its files' bytes; the prompt path's parts of 2,048 tokens or more are timed). A new conversation that shares only the system prompt with a saved one no longer reads that one's whole file. Until both speeds are measured the file is read; a skip is logged as not read. Second commit.
  • conversation_disk.cpp is added to the CUDA/HIP, SYCL and Metal engine lists (all three compile generate.cpp).

Compatibility: with no --conversation-disk-dir nothing is written or read. One change stays in the parking path: before the RAM admission evicts a conversation, it calls glibc's malloc_trim(0), so freed images count in MemAvailable. Output is unaffected.

Validation

Passed:

  • Rebased onto 0.2.1 (90767252) as two commits: 65a79b51 (the tier) and c6b99976 (the read check). CUDA build in Docker (nvidia/cuda:13.3.0-devel-ubuntu24.04, GCC 14, -DCMAKE_CUDA_ARCHITECTURES=75 -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=ON -DSTRATA_BUILD_CONVERSATION_TESTS=ON): strata and the tests build; conversation_disk_test 69 checks, conversation_cache_test 4,772, conversation_file_test 201, all pass.
  • python -m pytest -q serve/test_server.py serve/test_parallel.py serve/test_parallel_yield.py serve/test_request_accounting.py: 392 passed, 3 skipped, 444 subtests passed (Windows 11, Python 3.14).
  • Hardware end to end on my fork's main 342185bd: that is 0.2.0-beta plus this disk tier, plus one diagnostic log line and fork-only serve/tool patches (the disk-tier code is otherwise this commit). Box: 2x CMP 50HX 20 GB (sm_75, layer split), 2x Xeon E5-2670 v2, 64 GB, NVMe ext4, Ubuntu 24.04.4, driver 610.43.03, Flash-Next IQ3_XXS; exit-only, --adapt-swaps 0, greedy, 48 tokens.
    • Three conversations that share no prefix (41,839 / 41,846 / 165,558 tokens, cold 22.5 / 23.3 / 73.3 s), then SIGTERM to the server: saved for the next start in 17.0 s (5,786 MiB).
    • The next start took in all three. Each came back from the disk with the same answer as before the restart, in 2.6 / 2.9 / 8.4 s.
    • The disk tier has been in daily use on that box since 2026-10-11. Since 2026-10-20 on a 12 TB 7200 rpm HDD with on (my fork on 0.2.1 with both commits and #3404): a 304,364-token conversation came back from the disk in 47.6 s (5,465 MiB read in 44.1 s, then the restore) and a 286,199-token one in 41.7 s, where reading them takes ~140 s.
    • The read check, live on the same box: after a 124,097-token conversation came back from the HDD in 19.6 s (2,856 MiB at 169 MB/s), a new sub-agent that shared only the 16,384-token system prompt with a saved 2,018 MiB conversation skipped that file: not read: its 16384 more tokens read in ~9.4 s, its file (2018 MiB) in ~12.5 s. On the NVMe days the same case read whole files: four reads of 4-6 GB for 13,764-16,384 tokens (6,033 MiB in 11.4 s for 16,384 tokens).

Failed: the same end-to-end check with adaptive expert swaps on (the live config) gave one restored conversation a different first token. Swaps make decode non-repeatable on this box even without a restore; with --adapt-swaps 0 the check passed (above).

Not run:

  • HIP, SYCL, Metal and Windows builds and runs: I have no environment for them.
  • The restart check on these exact commits: it ran on my fork (above), whose disk-tier code is the same, and that code has served the box live since.

Limits

  • Linux, one box.
  • With RAM this short, a park waits for an evicted conversation's write before it has room: 28.6-31 s for 5.5-6 GB on the HDD, 7-8 s on the NVMe.
  • The streamed park/restore of the earlier version of this PR (used when RAM is short) is left out to keep this one smaller; it can follow as its own PR.

🔄 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/1937 **Author:** [@xrip](https://github.com/xrip) **Created:** 10/11/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/conversation-disk-tier` --- ### 📝 Commits (2) - [`65a79b5`](https://github.com/Niko1221/Strata/commit/65a79b51dfd67a43a47a3c241614d2857fb37eae) serve: parked conversations on the disk, optionally across restarts (--conversation-disk-dir, --conversation-disk-persist) - [`c6b9997`](https://github.com/Niko1221/Strata/commit/c6b99976ca087e243f57655853ab51caa7c1a1d9) conversation disk: read a file back only when that is faster than reading the tokens it adds ### 📊 Changes **10 files changed** (+1304 additions, -31 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+9 -1) 📝 `docs/DETAILS.md` (+53 -0) 📝 `include/strata/core/conversation_cache.hpp` (+7 -0) ➕ `include/strata/core/conversation_disk.hpp` (+153 -0) 📝 `metal/CMakeLists.txt` (+1 -0) 📝 `serve/server.py` (+4 -1) ➕ `src/core/conversation_disk.cpp` (+575 -0) ➕ `src/core/conversation_disk_test.cpp` (+335 -0) 📝 `src/program/generate.cpp` (+166 -28) 📝 `sycl/CMakeLists.txt` (+1 -1) </details> ### 📄 Description ## Problem and result A conversation the RAM conversation cache evicts (its slots or its byte budget full) is dropped, and a client that comes back to it reads the whole prompt again: an agent that switches between its main conversation and sub-agents too large for the budget together reads ~150K tokens again on every switch. Every restart of the engine (an update, a settings change, a reboot, a poweroff) also reads every conversation from token 0 again. - **Before:** an evicted conversation, and every conversation at a restart, costs a full re-read (73.3 s for 165,558 tokens on the box below). - **After, with `--conversation-disk-dir DIR`:** the evicted conversation goes to `DIR` and comes back from there when a request continues it. With `--conversation-disk-persist on|exit-only` the conversations outlive the engine: the same 165,558-token conversation came back after a restart in 8.4 s, with the same answer. ## Related discussion The maintainer said in #1090 that one disk-persistence design will be chosen; this is one of them. It was closed in the 0.2.0-beta reset and is rebased here onto 0.2.1. #3404 (separate, in the conversation cache) drops stale parked copies of a conversation, which the disk tier would otherwise write out. ## Approach Off by default (opt-in options, help text and `docs/DETAILS.md`): - `conversation_disk.hpp/.cpp`: the tier. Each image (one per stage with `--layer-split`) is one session file, written and read with the session files' own code (size, header and hash checks, the free-space preflight). A background writer (nice 10 on Linux) means a switch never waits for the disk, and queued images can be read back from RAM. Every checkpoint is kept, as in RAM. `--conversation-disk-mib N` (default 32768): oldest out first. - `conversation_cache.hpp`: `set_on_evict`, so an evicted conversation goes to the tier instead of being dropped. A park that does not fit the cache's byte budget is also sent to the disk. - `generate.cpp`: options, wiring, the read back when the disk holds a longer prefix than the session / a batch slot / the RAM cache (it needs RAM for the whole image above `--conversation-cache-min-free-mib`, else the prompt is read again), and the request line says when it was reused from the disk. - `--conversation-disk-persist on|exit-only`: the files are bound to the session files' identity (model, engine version, settings), with an index per conversation, so the next start takes them in. Files of another identity are removed at start. On QUIT the engine saves the current conversation and the parked ones; while persist is on it ignores SIGINT/SIGTERM/SIGHUP and ends through its stdin, so a poweroff's SIGTERM does not cut the save short. The server waits up to 300 s for QUIT (`serve/server.py`, 4 lines). `exit-only` writes nothing while running and saves only at QUIT. - A file is read back only when that is faster than reading the tokens it adds, by this run's measured disk and prompt speeds (the match reports its files' bytes; the prompt path's parts of 2,048 tokens or more are timed). A new conversation that shares only the system prompt with a saved one no longer reads that one's whole file. Until both speeds are measured the file is read; a skip is logged as `not read`. Second commit. - `conversation_disk.cpp` is added to the CUDA/HIP, SYCL and Metal engine lists (all three compile `generate.cpp`). Compatibility: with no `--conversation-disk-dir` nothing is written or read. One change stays in the parking path: before the RAM admission evicts a conversation, it calls glibc's `malloc_trim(0)`, so freed images count in MemAvailable. Output is unaffected. ## Validation **Passed:** - Rebased onto 0.2.1 (`90767252`) as two commits: `65a79b51` (the tier) and `c6b99976` (the read check). CUDA build in Docker (`nvidia/cuda:13.3.0-devel-ubuntu24.04`, GCC 14, `-DCMAKE_CUDA_ARCHITECTURES=75 -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=ON -DSTRATA_BUILD_CONVERSATION_TESTS=ON`): `strata` and the tests build; `conversation_disk_test` 69 checks, `conversation_cache_test` 4,772, `conversation_file_test` 201, all pass. - `python -m pytest -q serve/test_server.py serve/test_parallel.py serve/test_parallel_yield.py serve/test_request_accounting.py`: 392 passed, 3 skipped, 444 subtests passed (Windows 11, Python 3.14). - Hardware end to end on my fork's main `342185bd`: that is 0.2.0-beta plus this disk tier, plus one diagnostic log line and fork-only serve/tool patches (the disk-tier code is otherwise this commit). Box: 2x CMP 50HX 20 GB (sm_75, layer split), 2x Xeon E5-2670 v2, 64 GB, NVMe ext4, Ubuntu 24.04.4, driver 610.43.03, Flash-Next IQ3_XXS; `exit-only`, `--adapt-swaps 0`, greedy, 48 tokens. - Three conversations that share no prefix (41,839 / 41,846 / 165,558 tokens, cold 22.5 / 23.3 / 73.3 s), then SIGTERM to the server: saved for the next start in 17.0 s (5,786 MiB). - The next start took in all three. Each came back from the disk with the same answer as before the restart, in 2.6 / 2.9 / 8.4 s. - The disk tier has been in daily use on that box since 2026-10-11. Since 2026-10-20 on a 12 TB 7200 rpm HDD with `on` (my fork on 0.2.1 with both commits and #3404): a 304,364-token conversation came back from the disk in 47.6 s (5,465 MiB read in 44.1 s, then the restore) and a 286,199-token one in 41.7 s, where reading them takes ~140 s. - The read check, live on the same box: after a 124,097-token conversation came back from the HDD in 19.6 s (2,856 MiB at 169 MB/s), a new sub-agent that shared only the 16,384-token system prompt with a saved 2,018 MiB conversation skipped that file: `not read: its 16384 more tokens read in ~9.4 s, its file (2018 MiB) in ~12.5 s`. On the NVMe days the same case read whole files: four reads of 4-6 GB for 13,764-16,384 tokens (6,033 MiB in 11.4 s for 16,384 tokens). **Failed:** the same end-to-end check with adaptive expert swaps on (the live config) gave one restored conversation a different first token. Swaps make decode non-repeatable on this box even without a restore; with `--adapt-swaps 0` the check passed (above). **Not run:** - HIP, SYCL, Metal and Windows builds and runs: I have no environment for them. - The restart check on these exact commits: it ran on my fork (above), whose disk-tier code is the same, and that code has served the box live since. ## Limits - Linux, one box. - With RAM this short, a park waits for an evicted conversation's write before it has room: 28.6-31 s for 5.5-6 GB on the HDD, 7-8 s on the NVMe. - The streamed park/restore of the earlier version of this PR (used when RAM is short) is left out to keep this one smaller; it can follow as its own PR. --- <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#3765
No description provided.