[PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1… #4854

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3957
Author: @ruibeikaa
Created: 10/24/2026
Status: 🔄 Open

Base: main ← Head: park-restart-ram


📝 Commits (3)

  • 0f1fd4f serve: parked conversations kept over a restart (opt-in STRATA_PARK_DIR; engine PARKSAVE / PARKLOAD, layer split included)
  • 5d50a16 serve: STRATA_PARK_RAMLIKE=1 - the parked conversations kept in RAM over a restart, nothing written to a disk (opt-in, Windows)
  • 4b2ade7 serve: the hand-over to the park keeper is bounded: ready before the page walk, one wait for all of it

📊 Changes

10 files changed (+1599 additions, -11 deletions)

View changed files

📝 docs/DETAILS.md (+60 -1)
📝 include/strata/core/conversation_cache.hpp (+11 -0)
📝 include/strata/core/conversation_file.hpp (+9 -0)
➕ serve/park_keeper.py (+342 -0)
📝 serve/server.py (+144 -5)
➕ serve/test_park_files.py (+153 -0)
➕ serve/test_park_ramlike.py (+197 -0)
📝 src/core/conversation_file.cpp (+193 -3)
📝 src/core/conversation_file_test.cpp (+54 -0)
📝 src/program/generate.cpp (+436 -2)

📄 Description

What this improves

  • The next turn of a long conversation after a restart takes seconds, not minutes. Today every stop of the engine forgets the conversation cache, so the first turn after a restart reads the whole conversation again from token 0. With STRATA_PARK_DIR=<folder> the parked conversations are saved at a graceful stop and put back in the cache at the next start, before the first request. Measured on four GP100 dies (Swift 1.5 GGUF, --layer-split 12,24,36): a 260,834-token conversation that takes 296 s to read from zero was saved in 3.6 s and loaded in 3.2 s, and its next turn after the restart took 7.2 s (260,834 tokens reused, 893 read). The saving grows with the conversation: a re-read costs roughly its length divided by the prompt speed, a load costs its size divided by the disk (or memory) speed.
  • A knob for no disk writes at all (Windows): STRATA_PARK_RAMLIKE=1. Saving to files writes several GB per long conversation at every graceful stop, which spends an SSD's write endurance. With the knob, the same session bytes go to named memory sections that a small keeper process holds in RAM from the stop to the next start. For a 103,749-token conversation the stop wrote 3.4 MiB to the system disk instead of 1.94 GB, no page went to the pagefile in the ten minutes after it, and the next turn after the restart reused 103,721 tokens and took 975 ms instead of 94-95 s from zero, with the same reply.
  • It is what real agent sessions lose today. In six days of one coding-agent client's logs on that rig, five restarts of the engine came with a long conversation open, and each first turn read it again from zero: 33K-382K tokens, 40-378 s each, about 1,180 s in all (~200 s a day), and the user waits that long on the first message after each. With the engine file and its switches unchanged, this brings such a restart back in seconds; one of the five crossed an engine update, which it deliberately does not load (below).
  • Layer splits too. Session files (SAVE / RESTORE) refuse --layer-split (#3539, #3744); here each stage of a conversation is its own session image, so multi-GPU splits keep their conversations. A single GPU takes the same path.
  • Who benefits: anyone who stops and starts the engine while long conversations are open - for the night, to free the GPUs for another program and back, the idle unload, POST /unload, or a switch to another model and back within STRATA_PARK_MAX_AGE_H - and most of all long agent sessions (100K-400K tokens) on slower prompt reads. The RAM knob is for Windows users who have the RAM to spare between the stop and the next start and do not want the disk writes.

What it costs (all opt-in; unset, nothing is written, kept or read):

  • Files (default mode): disk writes at every graceful stop, for the conversations that changed since the last save (5.33 GB for the 260K one). To keep them small a save skips conversations already in the folder and not run since (a stop with no request since the last load writes 0 bytes), conversations under STRATA_PARK_MIN_TOKENS (default 16384) and ones parked more than STRATA_PARK_MAX_AGE_H ago (default 48 h), and writes only two checkpoints of each. Disk space up to the conversation cache's budget, and a longer stop by the save's time (3.6 s for 5.33 GB on an NVMe SSD).
  • RAM knob: RAM and commit equal to the kept conversations' size (1.94 GB for 103K tokens; at most the conversation cache's budget) from the stop until the next start loads them, or until STRATA_PARK_MAX_AGE_H (the keeper then ends by itself). A save is refused when less than STRATA_PARK_RAMLIKE_FLOOR_MIB (8192) of commit would be left. Every stop copies every kept conversation again (RAM to RAM, ~1.4 s for 1.94 GB). Lost at a reboot or sign-out, or when the keeper cannot outlive the server (a job that ends its processes and allows no breakaway); then the next start reads the conversations again, as without the change, and says why. Windows only.
  • (My own setting is the RAM knob with STRATA_PARK_MIN_TOKENS=50000 and STRATA_PARK_MAX_AGE_H=1: long sessions only, restarts within the hour.)
  • Both: graceful stops only (a kill, a crash or a power cut keeps nothing new; with files the previous save stays). The same engine and settings only: the saved images are bound to the model, the engine file (size and modification time) and every STRATA_* variable except STRATA_PARK_* (plus the session files' own config fingerprint); after an update or a changed switch nothing is loaded and the next stop replaces them.

Problem and result

A restart of the engine drops every parked conversation, and the next turn of a long one reads its whole prompt again. The session files keep one conversation, need a client to save and restore it, and refuse a layer split.

before STRATA_PARK_DIR (files) + STRATA_PARK_RAMLIKE=1 (RAM, Windows)
first turn after a graceful restart read from zero (260,834 tokens: 296 s) loaded at start (3.2 s), turn 7.2 s, 260,834 reused 103,749 tokens: loaded in 3.0 s, turn 975 ms, 103,721 reused (from zero: 94 s)
written to the disk at the stop - the changed conversations (5.33 GB for 260K; 0 bytes after a stop with no request) 3.4 MiB (two small files and logs); 0 pagefile writes in the next 10 min
reply after the restart - bit for bit the one without a restart (30K conversation, two restarts) bit for bit the one without a restart
--layer-split SAVE / RESTORE refused one session image per stage one section per stage
unset (default) - nothing written or read; the INFO line has one more key -

Why named sections and not a file marked temporary: on Windows 11 a file created with FILE_ATTRIBUTE_TEMPORARY (also with FILE_FLAG_DELETE_ON_CLOSE) and never flushed was written to the disk as it was written - 4 GiB of 4 GiB, with the handle still open (1 GiB of 1 GiB with DELETE_ON_CLOSE). A named, pagefile-backed section (CreateFileMappingW(INVALID_HANDLE_VALUE, ..., SEC_COMMIT)) is not file cache: its pages stay in the working set of a process that maps it and touches them, and they go to the pagefile only if Windows trims that working set. The sections live as long as one process holds them, so a keeper outside the server's process tree holds them while no engine runs.

  • #1090: the maintainers said one disk-persistence design will be picked among the open ones. This is a narrower one than the disk tiers: nothing changes while the engine runs (no eviction to disk, no read path in requests); it only saves the parked list at a graceful stop and loads it at the next start, through the existing session-file format and identity checks - to files, or with the knob to memory sections that never touch the disk.
  • #1937 (open) also persists conversations across restarts (--conversation-disk-persist on|exit-only, one session file per stage) as part of a disk tier for evictions. The difference here: no change to the cache's eviction or request path, a save writes only what changed since the last one (and nothing when nothing ran), short and stale conversations are not written, two checkpoints per conversation instead of every one, the RAM knob with no disk writes, and it was run on Windows with a four-stage split. If the maintainers prefer #1937's design, the write-reduction rules and the RAM knob can go on top of its exit-only path instead.
  • #3341 and #3565 park through session files during a run (a disk budget; switches between conversations); #3539 and #3744 ask for session files under --layer-split, which this covers for the restart case only.
  • No scope issue was opened: it is opt-in and changes no default.

Approach

Three commits: the save / load at stop and start (files), the RAM knob on top, then a bound on the hand-over to the keeper.

  • src/program/generate.cpp: two commands between requests, PARKSAVE <folder> and PARKLOAD <folder>, answered like SAVE / RESTORE (SAVED / RESTORED <conversations> <bytes> <ms>, SESSION / SWAIT on the way, SERR for a refusal that leaves the engine in step).
    • PARKSAVE parks the live conversation, waits for the background copies (STRATA_CKPT_ASYNC, STRATA_PARK_ASYNC), as SAVE does, then writes every parked conversation that is kept. A parked image is one plain single-carve SavedConversation per stage (the park already moved the checkpoints' stage parts apart), so each stage goes through session_file_write as its own v1 image under the same model/config identity as SAVE. The image lends only the two checkpoints it writes (the deepest, where the next turn resumes, and the shallowest, the system prompt's root) for the length of one write and gets the whole chain back, also on a failure.
    • Files are named after a hash of the conversation's token ids and its length; one already in the folder whose conversation has not run since is not written again (a conversation that runs again is taken out and parked as a new image). A manifest (park.manifest, format 2: the tag, the stage count, one line per conversation with its parked time) is written to a temporary name and renamed over the old one last; only then are the files it no longer lists removed. A save that fails keeps the old manifest and every file it lists.
    • The tag: a hash of STRATA_VERSION, the engine file's size and modification time, the sorted STRATA_* environment (not STRATA_PARK_*) and the stage count. Another tag loads nothing.
    • PARKLOAD reads each listed conversation's stage images (the same RAM admission, read limits and conversation_snapshot_validate as RESTORE, before anything is put back) and puts the image in the parked list; the next matching request restores it the usual way. A conversation that does not validate or fit is skipped with a log line; the others still load.
    • Refused with --peer-device, --batch and without the conversation cache.
    • SavedConversation gets two run-time fields (parked_unix, disk_name; not in the file) and ConversationCache read access to its entries; the park stamps parked_unix.
    • The INFO line says park_files=1, or park_files=2 on Windows builds (files and sections), so the server knows what the engine takes.
  • The RAM knob, STRATA_PARK_RAMLIKE=1 (Windows only; elsewhere a "mem:" path and the variable are refused with a message):
    • src/core/conversation_file.{hpp,cpp}: session_file_write / session_file_read take "mem:<name>" for a named section holding the session file's exact bytes behind a 64-byte header ([u64 length]). A write has no temporary file, no rename and no flush; its preflight is the free commit (a SEC_COMMIT section charges its whole size when it is created) instead of the free disk space; an existing name is refused, never written into. A written section stays held (handle and view) until the process exits, so a keeper can open it by name meanwhile. session_section_exists for the name check.
    • generate.cpp: each kept conversation's stage images go to mem:Local\strata-park-<name>-s<stage> (the name also carries the engine's process id and the second, so a name is never reused), the manifest is park-mem.manifest, every kept conversation is written anew at each save, and no .ses files are removed.
    • serve/park_keeper.py: after PARKSAVE the server starts the keeper through a middle process that exits at once (so the keeper is nobody's descendant and a tree kill of the server passes it by; out of the server's job when the job allows a breakaway). The keeper opens every section the manifest lists, maps it, touches its pages for at most 8 s (the rest of the walk goes on afterwards, and every page again every 30 s), sets a soft working-set floor and writes keeper.json; the server waits for that before it closes the engine, at most STRATA_PARK_KEEPER_WAIT_S (default 30 s) in all. A hand-over still running 15 s after that leaves its stack in the log and the stop goes on; those conversations are then read again, as without the change. The next start's PARKLOAD reads the sections, then the server removes keeper.json and the keeper ends within ~2 s - the sections go with the last handle. A keeper also ends after STRATA_PARK_MAX_AGE_H or when a newer save replaced its manifest. A start that finds a manifest but no keeper (a reboot) says so, removes the manifest and loads nothing.
  • serve/server.py: STRATA_PARK_DIR and STRATA_PARK_RAMLIKE (read at start). StrataEngine.unload() sends PARKSAVE before it closes the engine (POST /unload, the idle unload); at a clean stop of the server (Ctrl+C, SIGTERM, the console's close) Service.park_at_exit sends it when no request holds the engine (it waits 1 s for the FIFO, else skips). After READY - first start, restart, lazy load - PARKLOAD when the folder has a manifest. Only to an engine whose INFO says park_files=1 (files) or park_files=2 (the RAM knob, so an older engine never writes files instead); never with batch slots or replicas. Any failure is logged and the stop or start goes on.
  • docs/DETAILS.md: what it does, what it writes and costs, why sections and not temporary files, the measurements.

Compatibility: unset, no command is sent and the engine's only change is the INFO key. No file format changes (the stage images are v1 session files).

Validation

Environment note: measured only on PH402 SKU 200 boards - four GP100 dies of 48 SMs and 32 GB HBM2 each (128 GB in all), application clocks 715 / 1050 MHz - with every expert resident in VRAM (no CPU share, no PCIe share, no adaptive tier). Nothing in the change depends on the card.

Rig: Windows 11 (build 26300), driver 581.80 (TCC), CUDA 12.9, VS 2022; build -DSTRATA_EXPERIMENTAL_SM60=ON -DCMAKE_CUDA_ARCHITECTURES=60 -DSTRATA_BUILD_CONVERSATION_TESTS=ON, ggml at the pinned 3cf03257f; NVMe SSD for the folder.

Passed:

  • Build of this branch (main 870aedd9 + these three commits): sm_60 with STRATA_EXPERIMENTAL_SM60, strata and the conversation tests, clean.

  • This branch, CPU, CUDA_VISIBLE_DEVICES=-1: conversation_file_test 195 checks (12 new: a section round trip gives the file's exact bytes, no flush phase, the section held after the write, an existing name refused, a short commit refused with no section left behind, an absent name not read), conversation_cache_test 4,842, conversation_split_failure_test (8 / 12 / 16 cases at 1 / 2 / 3 stages), conversation_memory_test 33, conversation_validation_test 972 host-only checks - all passed.

  • python -m unittest discover -s serve -p "test_*.py" (Windows 11, Python 3.13): 1,274 tests OK (25 skipped). One timing test, test_benchmark_prefill_cancel_observes_progress_and_companion_completion, failed once in a run that shared the machine with a model load; it passed 3 of 3 alone, here and on main, and in this full run on the idle machine. New: serve/test_park_files.py (7: PARKSAVE before the close at an unload; nothing sent when unset, to an engine without park_files, or with batch slots; PARKLOAD only with a manifest; a refusal leaves the engine in step; the save at exit skipped while a request holds the engine) and serve/test_park_ramlike.py (11: the keeper holds four 3 MiB sections after the process that made them exited and gives the same bytes back, ends when released and when a newer manifest replaces its own; a malformed manifest or one naming a path lists nothing; the server asks only an engine with park_files=2, drops a manifest no keeper holds and loads nothing, loads then releases a live keeper, hands the sections to a keeper before it closes the engine, a spawn that gives up within its whole wait and says what the folder shows, and a page walk that stops at its deadline and goes on later).

  • CPU probes on the same machine (the reason for the sections): a 4 GiB file with FILE_ATTRIBUTE_TEMPORARY, written in 64 MiB blocks and never flushed: 4,204 MiB written to the disk during the write (physical disk write counters, 1 s samples); 1 GiB with the handle held 30 s: 1,023 MiB; 1 GiB with FILE_FLAG_DELETE_ON_CLOSE too: 1,024 MiB. A keeper holding two sections of 8 GiB in all for 10 minutes: working set 8.03 GiB throughout, Pages Output 0, the bytes read back equal.

  • End to end, this branch's own revision, four GP100 dies, Swift 1.5 IQ3_S GGUF, --layer-split 12,24,36, --pipeline-windows 2, MTP, with STRATA_PARK_ASYNC=1, STRATA_CKPT_ASYNC=1 and STRATA_PARK_VRAM_MIB=1024 on (the trace shows the background copies and restores from the VRAM slots). The reference is three turns of one conversation on one engine with no restart: 12,166 tokens read from zero in 40 s, then two short turns. The same turns across stops: 4 of 4 replies byte for byte the reference, each next turn reused all but the last 27 tokens and read them in 0.63-0.66 s.

    mode stop stop took back reply
    RAM knob Ctrl+Break to the server's process group (the exit hook, real engine) 6.6 s new server, load 0.23 s equal
    RAM knob POST /unload 5.7 s next request reloads the engine, load 0.29 s equal
    files POST /unload 7.0 s next request reloads the engine, load 0.17 s equal
    files Ctrl+Break to the server's process group 8.4 s new server, load 0.20 s equal

    Conversations of 425-545 MB across four stage images; on a save with files, 2.3-2.9 s of that stop.

    A long one on the same revision and switches, RAM knob: a 195,912-token prompt read from zero in 369 s; Ctrl+Break to the server's process group, stop 11.0 s (3.36 GB into four sections in 3.1 s, the keeper ready in 1.5 s); new server, load 2.0 s; the turn repeated with 195,929 tokens reused and 5 read took 2.5 s, its reply byte for byte the one before the stop; POST /unload then took 7.0 s and a keeper held it again.

  • End to end, four GP100 dies, earlier stacks, not this branch's revision (the save / load code is the same apart from comments, the RAM probe - main's sample_host_memory() here, as RESTORE uses - and the RAM knob's STRATA_PARK_MIN_TOKENS default; checked with a comment-stripped diff; my serve fork's side also exported a prompt-echo table main does not have):

    • Files, engine 0.1.41 with my opt-in GP100 commits, Swift 1.5 IQ3_XXS GGUF, --layer-split 12,24,36, conversation cache on, folder on an NVMe SSD: a 260,834-token conversation (296 s from zero) saved 5.33 GB in 3.6 s at the stop, loaded in 3.2 s at the start, next turn 7.2 s with 260,834 reused and 893 read; a 30K conversation and two 1K ones at a stop: only the 30K one written (0.89 GB, 0.74 s), the 1K ones not kept; a stop with no request since the load: 0 bytes written; after the 30K conversation ran again: its new image written and the old files removed; its manifest entry back-dated 3 days: not loaded, its files removed at the next stop; its turn after two restarts: 34,838 tokens reused, the reply bit for bit the one without a restart.
    • RAM knob, engine 0.1.47 (main 011c9e60) with about 35 opt-in GP100 commits of mine (#3945 included), Swift 1.5 IQ3_S GGUF, --layer-split 12,24,36, MTP, STRATA_PARK_RAMLIKE=1, STRATA_PARK_MIN_TOKENS=50000. Run 1: a 103,490-token prompt read from zero in 94.1 s; graceful stop 8.2 s (1 conversation, 1,944,355,440 bytes in 1.43 s; a 66-token one not kept; the keeper holding the four stage sections); system disk 3.4 MiB during the stop against a 0.19 MiB/s background, 0.21 MiB/s median in the next 596 s, Pages Output 0, keeper working set 1.84 GiB throughout; next start park load: 1 of 1 ... 2965 ms; the next turn (103,749 tokens) reused 103,721 and read 28 in 975 ms, reply hash equal to the same turn without the restart. Run 2: the stack with the change against the stack without it, one fresh engine per arm: 13 standard requests plus the 103K read and its next turn, 15/15 reply hashes equal, decode -0.04%, prompt reads +0.04% (noise); stop: 3 conversations, 6.75 GB in 3.6 s into 12 stage sections, the whole stop 11.1 s; next start park load: 3 of 3 ... 3514 ms; the 103K turn reused 103,721 / read 28 in 973 ms; released at the end: the keeper exited and no section could be opened any more.

Failed: none observed.

Not run:

  • HIP, SYCL and Metal builds (no such machine here; generate.cpp is compiled by all of them, the added code is host C++ with a _WIN32 / POSIX split; the section code is inside _WIN32 parts) and any Linux build or run: the POSIX side (extern char** environ, the folder's file operations through std::filesystem) is not compiled here.
  • A Ctrl+C typed in a real console window: the exit hook was run with a Ctrl+Break sent to the server's process group.
  • A keeper that is really late: the bound on the hand-over is covered by unit tests only.
  • A stop through the Terminal (strata launch --stop), which waits --shutdown-timeout + 8 s (38 s by default) before it ends the server: a save longer than that is cut off (with files the previous save stays); the keeper under the Terminal's or the desktop app's process management (job objects).
  • Memory pressure on the keeper's pages (the pagefile path), sign-out and reboot with the RAM knob (a reboot is handled by the "no keeper" path, which the unit test covers).
  • Any card other than GP100, a single-GPU run, and a slow disk (HDD).

Limits

  • On when asked only; graceful stops only; the same engine file and STRATA_* switches only. The RAM knob is Windows only.
  • One folder per server: replicas ("replicas") and batch slots (--batch / "parallel") are not parked; two servers must not share a folder (each start releases the other's keeper).
  • Files: every save writes the changed conversations; the rules above keep it small but not zero. The folder is never cleaned when the variable is unset again: delete it to free the space.
  • RAM knob: the kept conversations hold their size in RAM and commit until the next start or STRATA_PARK_MAX_AGE_H; deleting keeper.json in the folder ends the keeper at once. A reboot, a sign-out or a keeper that dies loses them; nothing is written as a fallback. The sections are in the session's Local\ namespace with the default security of the user's processes.
  • The Windows console's close event gives a process a few seconds: a save longer than that is cut off there too.
  • On Windows a Ctrl+Break sent to every process on the console also reaches the engine, which ends at once, so nothing is saved then (seen here: the stop took 1.4 s and wrote nothing). A Break sent to the server's process group saves as usual.
  • Only two checkpoints per conversation are kept: an edit further back than the last turn reads more again, as after a RESTORE.

CI note: if the metal-link check is red, it is not caused by this PR: on #3945 the job failed while compiling upstream's metal/src/mtl_rt.mm (lines 69 and 92: MTLGPUFamilyMetal4, MTLCompileOptions.mathMode, MTLMathModeFast / MTLMathModeSafe), which the macos-14 runner's SDK does not provide, and those lines are unchanged on main. The workflow runs on any pull request that touches src/ or include/; this PR changes no file under metal/.


🔄 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/3957 **Author:** [@ruibeikaa](https://github.com/ruibeikaa) **Created:** 10/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `park-restart-ram` --- ### 📝 Commits (3) - [`0f1fd4f`](https://github.com/Niko1221/Strata/commit/0f1fd4f9c7ec624c6b5dcc5da6f3b18cb352fe21) serve: parked conversations kept over a restart (opt-in STRATA_PARK_DIR; engine PARKSAVE / PARKLOAD, layer split included) - [`5d50a16`](https://github.com/Niko1221/Strata/commit/5d50a1681ad9a2efc81e24775f1616438cd6dc05) serve: STRATA_PARK_RAMLIKE=1 - the parked conversations kept in RAM over a restart, nothing written to a disk (opt-in, Windows) - [`4b2ade7`](https://github.com/Niko1221/Strata/commit/4b2ade71e16f2a102ef53681c6550df53f7a68c6) serve: the hand-over to the park keeper is bounded: ready before the page walk, one wait for all of it ### 📊 Changes **10 files changed** (+1599 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+60 -1) 📝 `include/strata/core/conversation_cache.hpp` (+11 -0) 📝 `include/strata/core/conversation_file.hpp` (+9 -0) ➕ `serve/park_keeper.py` (+342 -0) 📝 `serve/server.py` (+144 -5) ➕ `serve/test_park_files.py` (+153 -0) ➕ `serve/test_park_ramlike.py` (+197 -0) 📝 `src/core/conversation_file.cpp` (+193 -3) 📝 `src/core/conversation_file_test.cpp` (+54 -0) 📝 `src/program/generate.cpp` (+436 -2) </details> ### 📄 Description ## What this improves - **The next turn of a long conversation after a restart takes seconds, not minutes.** Today every stop of the engine forgets the conversation cache, so the first turn after a restart reads the whole conversation again from token 0. With `STRATA_PARK_DIR=<folder>` the parked conversations are saved at a graceful stop and put back in the cache at the next start, before the first request. Measured on four GP100 dies (Swift 1.5 GGUF, `--layer-split 12,24,36`): a 260,834-token conversation that takes **296 s** to read from zero was saved in 3.6 s and loaded in 3.2 s, and its next turn after the restart took **7.2 s** (260,834 tokens reused, 893 read). The saving grows with the conversation: a re-read costs roughly its length divided by the prompt speed, a load costs its size divided by the disk (or memory) speed. - **A knob for no disk writes at all (Windows): `STRATA_PARK_RAMLIKE=1`.** Saving to files writes several GB per long conversation at every graceful stop, which spends an SSD's write endurance. With the knob, the same session bytes go to named memory sections that a small keeper process holds in RAM from the stop to the next start. For a 103,749-token conversation the stop wrote **3.4 MiB** to the system disk **instead of 1.94 GB**, no page went to the pagefile in the ten minutes after it, and the next turn after the restart reused 103,721 tokens and took **975 ms** instead of 94-95 s from zero, with the same reply. - **It is what real agent sessions lose today.** In six days of one coding-agent client's logs on that rig, five restarts of the engine came with a long conversation open, and each first turn read it again from zero: 33K-382K tokens, **40-378 s each, about 1,180 s in all (~200 s a day)**, and the user waits that long on the first message after each. With the engine file and its switches unchanged, this brings such a restart back in seconds; one of the five crossed an engine update, which it deliberately does not load (below). - **Layer splits too.** Session files (`SAVE` / `RESTORE`) refuse `--layer-split` (#3539, #3744); here each stage of a conversation is its own session image, so multi-GPU splits keep their conversations. A single GPU takes the same path. - **Who benefits:** anyone who stops and starts the engine while long conversations are open - for the night, to free the GPUs for another program and back, the idle unload, `POST /unload`, or a switch to another model and back within `STRATA_PARK_MAX_AGE_H` - and most of all long agent sessions (100K-400K tokens) on slower prompt reads. The RAM knob is for Windows users who have the RAM to spare between the stop and the next start and do not want the disk writes. What it costs (all opt-in; unset, nothing is written, kept or read): - **Files (default mode):** disk writes at every graceful stop, for the conversations that changed since the last save (5.33 GB for the 260K one). To keep them small a save skips conversations already in the folder and not run since (a stop with no request since the last load writes 0 bytes), conversations under `STRATA_PARK_MIN_TOKENS` (default 16384) and ones parked more than `STRATA_PARK_MAX_AGE_H` ago (default 48 h), and writes only two checkpoints of each. Disk space up to the conversation cache's budget, and a longer stop by the save's time (3.6 s for 5.33 GB on an NVMe SSD). - **RAM knob:** RAM and commit equal to the kept conversations' size (1.94 GB for 103K tokens; at most the conversation cache's budget) from the stop until the next start loads them, or until `STRATA_PARK_MAX_AGE_H` (the keeper then ends by itself). A save is refused when less than `STRATA_PARK_RAMLIKE_FLOOR_MIB` (8192) of commit would be left. Every stop copies every kept conversation again (RAM to RAM, ~1.4 s for 1.94 GB). Lost at a reboot or sign-out, or when the keeper cannot outlive the server (a job that ends its processes and allows no breakaway); then the next start reads the conversations again, as without the change, and says why. Windows only. - (My own setting is the RAM knob with `STRATA_PARK_MIN_TOKENS=50000` and `STRATA_PARK_MAX_AGE_H=1`: long sessions only, restarts within the hour.) - **Both:** graceful stops only (a kill, a crash or a power cut keeps nothing new; with files the previous save stays). The same engine and settings only: the saved images are bound to the model, the engine file (size and modification time) and every `STRATA_*` variable except `STRATA_PARK_*` (plus the session files' own config fingerprint); after an update or a changed switch nothing is loaded and the next stop replaces them. ## Problem and result A restart of the engine drops every parked conversation, and the next turn of a long one reads its whole prompt again. The session files keep one conversation, need a client to save and restore it, and refuse a layer split. | | before | `STRATA_PARK_DIR` (files) | `+ STRATA_PARK_RAMLIKE=1` (RAM, Windows) | |---|---|---|---| | first turn after a graceful restart | read from zero (260,834 tokens: 296 s) | loaded at start (3.2 s), turn 7.2 s, 260,834 reused | 103,749 tokens: loaded in 3.0 s, turn 975 ms, 103,721 reused (from zero: 94 s) | | written to the disk at the stop | - | the changed conversations (5.33 GB for 260K; 0 bytes after a stop with no request) | 3.4 MiB (two small files and logs); 0 pagefile writes in the next 10 min | | reply after the restart | - | bit for bit the one without a restart (30K conversation, two restarts) | bit for bit the one without a restart | | `--layer-split` | SAVE / RESTORE refused | one session image per stage | one section per stage | | unset (default) | - | nothing written or read; the INFO line has one more key | - | Why named sections and not a file marked temporary: on Windows 11 a file created with `FILE_ATTRIBUTE_TEMPORARY` (also with `FILE_FLAG_DELETE_ON_CLOSE`) and never flushed was written to the disk as it was written - 4 GiB of 4 GiB, with the handle still open (1 GiB of 1 GiB with `DELETE_ON_CLOSE`). A named, pagefile-backed section (`CreateFileMappingW(INVALID_HANDLE_VALUE, ..., SEC_COMMIT)`) is not file cache: its pages stay in the working set of a process that maps it and touches them, and they go to the pagefile only if Windows trims that working set. The sections live as long as one process holds them, so a keeper outside the server's process tree holds them while no engine runs. ## Related discussion - #1090: the maintainers said one disk-persistence design will be picked among the open ones. This is a narrower one than the disk tiers: nothing changes while the engine runs (no eviction to disk, no read path in requests); it only saves the parked list at a graceful stop and loads it at the next start, through the existing session-file format and identity checks - to files, or with the knob to memory sections that never touch the disk. - #1937 (open) also persists conversations across restarts (`--conversation-disk-persist on|exit-only`, one session file per stage) as part of a disk tier for evictions. The difference here: no change to the cache's eviction or request path, a save writes only what changed since the last one (and nothing when nothing ran), short and stale conversations are not written, two checkpoints per conversation instead of every one, the RAM knob with no disk writes, and it was run on Windows with a four-stage split. If the maintainers prefer #1937's design, the write-reduction rules and the RAM knob can go on top of its `exit-only` path instead. - #3341 and #3565 park through session files during a run (a disk budget; switches between conversations); #3539 and #3744 ask for session files under `--layer-split`, which this covers for the restart case only. - No scope issue was opened: it is opt-in and changes no default. ## Approach Three commits: the save / load at stop and start (files), the RAM knob on top, then a bound on the hand-over to the keeper. - `src/program/generate.cpp`: two commands between requests, `PARKSAVE <folder>` and `PARKLOAD <folder>`, answered like `SAVE` / `RESTORE` (`SAVED` / `RESTORED <conversations> <bytes> <ms>`, `SESSION` / `SWAIT` on the way, `SERR` for a refusal that leaves the engine in step). - PARKSAVE parks the live conversation, waits for the background copies (`STRATA_CKPT_ASYNC`, `STRATA_PARK_ASYNC`), as `SAVE` does, then writes every parked conversation that is kept. A parked image is one plain single-carve `SavedConversation` per stage (the park already moved the checkpoints' stage parts apart), so each stage goes through `session_file_write` as its own v1 image under the same model/config identity as `SAVE`. The image lends only the two checkpoints it writes (the deepest, where the next turn resumes, and the shallowest, the system prompt's root) for the length of one write and gets the whole chain back, also on a failure. - Files are named after a hash of the conversation's token ids and its length; one already in the folder whose conversation has not run since is not written again (a conversation that runs again is taken out and parked as a new image). A manifest (`park.manifest`, format 2: the tag, the stage count, one line per conversation with its parked time) is written to a temporary name and renamed over the old one last; only then are the files it no longer lists removed. A save that fails keeps the old manifest and every file it lists. - The tag: a hash of `STRATA_VERSION`, the engine file's size and modification time, the sorted `STRATA_*` environment (not `STRATA_PARK_*`) and the stage count. Another tag loads nothing. - PARKLOAD reads each listed conversation's stage images (the same RAM admission, read limits and `conversation_snapshot_validate` as `RESTORE`, before anything is put back) and `put`s the image in the parked list; the next matching request restores it the usual way. A conversation that does not validate or fit is skipped with a log line; the others still load. - Refused with `--peer-device`, `--batch` and without the conversation cache. - `SavedConversation` gets two run-time fields (`parked_unix`, `disk_name`; not in the file) and `ConversationCache` read access to its entries; the park stamps `parked_unix`. - The INFO line says `park_files=1`, or `park_files=2` on Windows builds (files and sections), so the server knows what the engine takes. - The RAM knob, `STRATA_PARK_RAMLIKE=1` (Windows only; elsewhere a `"mem:"` path and the variable are refused with a message): - `src/core/conversation_file.{hpp,cpp}`: `session_file_write` / `session_file_read` take `"mem:<name>"` for a named section holding the session file's exact bytes behind a 64-byte header (`[u64 length]`). A write has no temporary file, no rename and no flush; its preflight is the free commit (a `SEC_COMMIT` section charges its whole size when it is created) instead of the free disk space; an existing name is refused, never written into. A written section stays held (handle and view) until the process exits, so a keeper can open it by name meanwhile. `session_section_exists` for the name check. - `generate.cpp`: each kept conversation's stage images go to `mem:Local\strata-park-<name>-s<stage>` (the name also carries the engine's process id and the second, so a name is never reused), the manifest is `park-mem.manifest`, every kept conversation is written anew at each save, and no `.ses` files are removed. - `serve/park_keeper.py`: after PARKSAVE the server starts the keeper through a middle process that exits at once (so the keeper is nobody's descendant and a tree kill of the server passes it by; out of the server's job when the job allows a breakaway). The keeper opens every section the manifest lists, maps it, touches its pages for at most 8 s (the rest of the walk goes on afterwards, and every page again every 30 s), sets a soft working-set floor and writes `keeper.json`; the server waits for that before it closes the engine, at most `STRATA_PARK_KEEPER_WAIT_S` (default 30 s) in all. A hand-over still running 15 s after that leaves its stack in the log and the stop goes on; those conversations are then read again, as without the change. The next start's PARKLOAD reads the sections, then the server removes `keeper.json` and the keeper ends within ~2 s - the sections go with the last handle. A keeper also ends after `STRATA_PARK_MAX_AGE_H` or when a newer save replaced its manifest. A start that finds a manifest but no keeper (a reboot) says so, removes the manifest and loads nothing. - `serve/server.py`: `STRATA_PARK_DIR` and `STRATA_PARK_RAMLIKE` (read at start). `StrataEngine.unload()` sends PARKSAVE before it closes the engine (POST `/unload`, the idle unload); at a clean stop of the server (Ctrl+C, SIGTERM, the console's close) `Service.park_at_exit` sends it when no request holds the engine (it waits 1 s for the FIFO, else skips). After READY - first start, restart, lazy load - PARKLOAD when the folder has a manifest. Only to an engine whose INFO says `park_files=1` (files) or `park_files=2` (the RAM knob, so an older engine never writes files instead); never with batch slots or replicas. Any failure is logged and the stop or start goes on. - `docs/DETAILS.md`: what it does, what it writes and costs, why sections and not temporary files, the measurements. Compatibility: unset, no command is sent and the engine's only change is the INFO key. No file format changes (the stage images are v1 session files). ## Validation **Environment note: measured only on PH402 SKU 200 boards - four GP100 dies of 48 SMs and 32 GB HBM2 each (128 GB in all), application clocks 715 / 1050 MHz - with every expert resident in VRAM (no CPU share, no PCIe share, no adaptive tier).** Nothing in the change depends on the card. Rig: Windows 11 (build 26300), driver 581.80 (TCC), CUDA 12.9, VS 2022; build `-DSTRATA_EXPERIMENTAL_SM60=ON -DCMAKE_CUDA_ARCHITECTURES=60 -DSTRATA_BUILD_CONVERSATION_TESTS=ON`, ggml at the pinned 3cf03257f; NVMe SSD for the folder. **Passed:** - Build of this branch (main 870aedd9 + these three commits): sm_60 with `STRATA_EXPERIMENTAL_SM60`, `strata` and the conversation tests, clean. - This branch, CPU, `CUDA_VISIBLE_DEVICES=-1`: `conversation_file_test` 195 checks (12 new: a section round trip gives the file's exact bytes, no flush phase, the section held after the write, an existing name refused, a short commit refused with no section left behind, an absent name not read), `conversation_cache_test` 4,842, `conversation_split_failure_test` (8 / 12 / 16 cases at 1 / 2 / 3 stages), `conversation_memory_test` 33, `conversation_validation_test` 972 host-only checks - all passed. - `python -m unittest discover -s serve -p "test_*.py"` (Windows 11, Python 3.13): 1,274 tests OK (25 skipped). One timing test, `test_benchmark_prefill_cancel_observes_progress_and_companion_completion`, failed once in a run that shared the machine with a model load; it passed 3 of 3 alone, here and on main, and in this full run on the idle machine. New: `serve/test_park_files.py` (7: PARKSAVE before the close at an unload; nothing sent when unset, to an engine without `park_files`, or with batch slots; PARKLOAD only with a manifest; a refusal leaves the engine in step; the save at exit skipped while a request holds the engine) and `serve/test_park_ramlike.py` (11: the keeper holds four 3 MiB sections after the process that made them exited and gives the same bytes back, ends when released and when a newer manifest replaces its own; a malformed manifest or one naming a path lists nothing; the server asks only an engine with `park_files=2`, drops a manifest no keeper holds and loads nothing, loads then releases a live keeper, hands the sections to a keeper before it closes the engine, a spawn that gives up within its whole wait and says what the folder shows, and a page walk that stops at its deadline and goes on later). - CPU probes on the same machine (the reason for the sections): a 4 GiB file with `FILE_ATTRIBUTE_TEMPORARY`, written in 64 MiB blocks and never flushed: 4,204 MiB written to the disk during the write (physical disk write counters, 1 s samples); 1 GiB with the handle held 30 s: 1,023 MiB; 1 GiB with `FILE_FLAG_DELETE_ON_CLOSE` too: 1,024 MiB. A keeper holding two sections of 8 GiB in all for 10 minutes: working set 8.03 GiB throughout, Pages Output 0, the bytes read back equal. - End to end, **this branch's own revision**, four GP100 dies, Swift 1.5 IQ3_S GGUF, `--layer-split 12,24,36`, `--pipeline-windows 2`, MTP, with `STRATA_PARK_ASYNC=1`, `STRATA_CKPT_ASYNC=1` and `STRATA_PARK_VRAM_MIB=1024` on (the trace shows the background copies and restores from the VRAM slots). The reference is three turns of one conversation on one engine with no restart: 12,166 tokens read from zero in 40 s, then two short turns. The same turns across stops: **4 of 4 replies byte for byte the reference**, each next turn reused all but the last 27 tokens and read them in 0.63-0.66 s. | mode | stop | stop took | back | reply | |---|---|---|---|---| | RAM knob | Ctrl+Break to the server's process group (the exit hook, real engine) | 6.6 s | new server, load 0.23 s | equal | | RAM knob | `POST /unload` | 5.7 s | next request reloads the engine, load 0.29 s | equal | | files | `POST /unload` | 7.0 s | next request reloads the engine, load 0.17 s | equal | | files | Ctrl+Break to the server's process group | 8.4 s | new server, load 0.20 s | equal | Conversations of 425-545 MB across four stage images; on a save with files, 2.3-2.9 s of that stop. A long one on the same revision and switches, RAM knob: a 195,912-token prompt read from zero in 369 s; Ctrl+Break to the server's process group, stop 11.0 s (3.36 GB into four sections in 3.1 s, the keeper ready in 1.5 s); new server, load 2.0 s; the turn repeated with 195,929 tokens reused and 5 read took 2.5 s, its reply byte for byte the one before the stop; `POST /unload` then took 7.0 s and a keeper held it again. - End to end, four GP100 dies, earlier stacks, **not this branch's revision** (the save / load code is the same apart from comments, the RAM probe - main's `sample_host_memory()` here, as `RESTORE` uses - and the RAM knob's `STRATA_PARK_MIN_TOKENS` default; checked with a comment-stripped diff; my serve fork's side also exported a prompt-echo table main does not have): - Files, engine 0.1.41 with my opt-in GP100 commits, Swift 1.5 IQ3_XXS GGUF, `--layer-split 12,24,36`, conversation cache on, folder on an NVMe SSD: a 260,834-token conversation (296 s from zero) saved 5.33 GB in 3.6 s at the stop, loaded in 3.2 s at the start, next turn 7.2 s with 260,834 reused and 893 read; a 30K conversation and two 1K ones at a stop: only the 30K one written (0.89 GB, 0.74 s), the 1K ones not kept; a stop with no request since the load: 0 bytes written; after the 30K conversation ran again: its new image written and the old files removed; its manifest entry back-dated 3 days: not loaded, its files removed at the next stop; its turn after two restarts: 34,838 tokens reused, the reply bit for bit the one without a restart. - RAM knob, engine 0.1.47 (main 011c9e60) with about 35 opt-in GP100 commits of mine (#3945 included), Swift 1.5 IQ3_S GGUF, `--layer-split 12,24,36`, MTP, `STRATA_PARK_RAMLIKE=1`, `STRATA_PARK_MIN_TOKENS=50000`. Run 1: a 103,490-token prompt read from zero in 94.1 s; graceful stop 8.2 s (1 conversation, 1,944,355,440 bytes in 1.43 s; a 66-token one not kept; the keeper holding the four stage sections); system disk 3.4 MiB during the stop against a 0.19 MiB/s background, 0.21 MiB/s median in the next 596 s, Pages Output 0, keeper working set 1.84 GiB throughout; next start `park load: 1 of 1 ... 2965 ms`; the next turn (103,749 tokens) reused 103,721 and read 28 in 975 ms, reply hash equal to the same turn without the restart. Run 2: the stack with the change against the stack without it, one fresh engine per arm: 13 standard requests plus the 103K read and its next turn, **15/15 reply hashes equal**, decode -0.04%, prompt reads +0.04% (noise); stop: 3 conversations, 6.75 GB in 3.6 s into 12 stage sections, the whole stop 11.1 s; next start `park load: 3 of 3 ... 3514 ms`; the 103K turn reused 103,721 / read 28 in 973 ms; released at the end: the keeper exited and no section could be opened any more. **Failed:** none observed. **Not run:** - HIP, SYCL and Metal builds (no such machine here; `generate.cpp` is compiled by all of them, the added code is host C++ with a `_WIN32` / POSIX split; the section code is inside `_WIN32` parts) and any Linux build or run: the POSIX side (`extern char** environ`, the folder's file operations through `std::filesystem`) is not compiled here. - A Ctrl+C typed in a real console window: the exit hook was run with a Ctrl+Break sent to the server's process group. - A keeper that is really late: the bound on the hand-over is covered by unit tests only. - A stop through the Terminal (`strata launch --stop`), which waits `--shutdown-timeout` + 8 s (38 s by default) before it ends the server: a save longer than that is cut off (with files the previous save stays); the keeper under the Terminal's or the desktop app's process management (job objects). - Memory pressure on the keeper's pages (the pagefile path), sign-out and reboot with the RAM knob (a reboot is handled by the "no keeper" path, which the unit test covers). - Any card other than GP100, a single-GPU run, and a slow disk (HDD). ## Limits - On when asked only; graceful stops only; the same engine file and `STRATA_*` switches only. The RAM knob is Windows only. - One folder per server: replicas (`"replicas"`) and batch slots (`--batch` / `"parallel"`) are not parked; two servers must not share a folder (each start releases the other's keeper). - Files: every save writes the changed conversations; the rules above keep it small but not zero. The folder is never cleaned when the variable is unset again: delete it to free the space. - RAM knob: the kept conversations hold their size in RAM and commit until the next start or `STRATA_PARK_MAX_AGE_H`; deleting `keeper.json` in the folder ends the keeper at once. A reboot, a sign-out or a keeper that dies loses them; nothing is written as a fallback. The sections are in the session's `Local\` namespace with the default security of the user's processes. - The Windows console's close event gives a process a few seconds: a save longer than that is cut off there too. - On Windows a Ctrl+Break sent to every process on the console also reaches the engine, which ends at once, so nothing is saved then (seen here: the stop took 1.4 s and wrote nothing). A Break sent to the server's process group saves as usual. - Only two checkpoints per conversation are kept: an edit further back than the last turn reads more again, as after a `RESTORE`. **CI note:** if the `metal-link` check is red, it is not caused by this PR: on #3945 the job failed while compiling upstream's `metal/src/mtl_rt.mm` (lines 69 and 92: `MTLGPUFamilyMetal4`, `MTLCompileOptions.mathMode`, `MTLMathModeFast` / `MTLMathModeSafe`), which the `macos-14` runner's SDK does not provide, and those lines are unchanged on main. The workflow runs on any pull request that touches `src/` or `include/`; this PR changes no file under `metal/`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1… to [PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1… 2026-11-07 17:32:51 +07:00
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#4854
No description provided.