mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #668] [CLOSED] Session files: save and restore a conversation to disk (POST /slots/0?action=save|restore) #2756
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#2756
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/668
Author: @maverde73
Created: 10/3/2026
Status: ❌ Closed
Base:
main← Head:session-files📝 Commits (10+)
3a71f48Session files: save and restore a conversation to disk (SAVE/RESTORE, POST /slots/0?action=save|restore)b60bb3adocs: session files (the slot save/restore API, its answers and errors, format v1, the checks, measured at 63K)0e24adeSession files: review fixes (exclusive temp files, no-follow restore, full identity, bounds, durability, server hardening)b575517docs: session files after the review fixes (identity, durability, errors, measured)6f6a6f8conversation_file_test: format v1 golden (header offsets, size and hash of the fixed sample)b2f49bcsession files: resolved expert inputs, runtime read bounds, published-save status, typed errors, strict protocol, UNC free space (review R1-R6)480b5adsession files r3: include ; Out initialised by members403037asession files r3: RAM preflight for save; keep.bin.tmp planted-name test1cd654fsession files r4: SAVE RAM preflight before any checkpoint copy; progress after every block and bounded SWAIT allowances for blocking steps (review A/B)93e49dcdocs: session files r4 (SWAIT/progress protocol, preflight wording, version identity, POSIX/Windows flush, continuation limit, r4 measurements)📊 Changes
16 files changed (+3588 additions, -18 deletions)
View changed files
📝
CMakeLists.txt(+5 -1)📝
docs/DETAILS.md(+117 -3)➕
include/strata/core/conversation_file.hpp(+243 -0)📝
include/strata/core/conversation_snapshot.hpp(+20 -0)📝
include/strata/core/expert_source.hpp(+9 -0)📝
include/strata/core/progress.hpp(+9 -0)📝
serve/server.py(+254 -0)📝
serve/test_security.py(+19 -0)➕
serve/test_slots.py(+449 -0)➕
src/core/conversation_file.cpp(+1251 -0)➕
src/core/conversation_file_test.cpp(+742 -0)📝
src/core/conversation_snapshot.cpp(+29 -0)📝
src/core/conversation_snapshot_test.cpp(+54 -0)📝
src/core/conversation_state.cpp(+52 -0)📝
src/core/expert_source.cpp(+17 -0)📝
src/program/generate.cpp(+318 -14)📄 Description
Saves the conversation the engine holds to one file and restores it later, also after a restart of the same
engine version with the same model and settings. At 63K tokens on an RTX 4070 Ti the restore took 1.05 s once the
model was loaded (engine startup itself takes about 70-95 s) instead of re-reading the prompt for 25.5 s, and the next
turn gave the same 32 token IDs as without a restart. Files are bound to the engine version, model inputs and settings, must be trusted (the hashes
detect corruption, they do not authenticate), and there is one slot, slot 0.
Proposed in #661. Base: v0.1.39 (
6f32ec0); rebased from v0.1.38 (see "Rebased on v0.1.39" below). Opt-in: without--slot-save-pathnothing changes - in three alternating pairs at 3.4K, 29.6K and 63K there was no measurableprefill, decode or peak-VRAM difference and the output token IDs were identical in all nine pairs (measured on the
v0.1.38 base; the session code adds nothing to the inference path).
What it does
strata --servetakesSAVE <path>andRESTORE <path>between requests and answersSAVED <tokens> <bytes> <ms>,RESTORED <tokens> <bytes> <ms>,SERR <kind> <published 0|1> <reason>(failed;the engine and its session as they were; kind =
invalid,storage,memoryorio) orFATAL <reason>(arestore transfer failed after the device writes began; the engine exits).
SESSION <done> <total>follows everyblock of the file that moved (at most 16 MiB; the last partial block and a small file included).
SWAIT <phase> <seconds>comes before a step that blocks in one call (fingerprint, state capture, file flush, rename + folderflush, validation, device transfer): the engine's watchdog and the server allow that step those seconds (60 + 1 per
4 MiB, at most 3600), then it is stuck; the next line clears the allowance. The server checks every line and ends an
engine that answers out of protocol (malformed, negative or non-finite numbers, progress going back, an allowance
outside 1..3600). Server and engine change together: an older server does not know
SERR/SWAIT.--slot-save-path DIR(or"slot_save_path"in the config), resolved once to an absolute path and created withmode 0700. NAME must be one plain file name, portable to Windows (no drive, stream, device name, trailing dot or
space).
501disabled,400refused name/slot/action or an invalid file (corrupt, another model orconfiguration, over this session's limits),
404missing file,503model not loaded, RAM preflight refused orallocation failed,
507no disk space (preflight, or the OS reports no space/quota),500other I/O failures andan engine that ended, was silent or answered out of protocol (it is restarted on the next request). The status
comes from the engine's category (
error.kind), never from the message text. Slot actionsqueue behind running requests, show in
/statusand count as activity for the idle unload.Origin, theserver's own or an explicitly trusted one; this Origin rule also applies when an API key is configured.
POSIX), flushed (
fsync/FlushFileBuffers), renamed (rename/MoveFileExW), folder flushed on POSIX. A savethat fails before the rename keeps the old file and removes only its own temporary file. If the folder flush fails
after the rename, the save fails with
error.published: true: the new file has replaced the old one, but its namemay not survive a power loss. A folder that cannot be flushed (
EINVAL) is logged, not an error. Before writing,the save needs room for the whole new file plus
--session-min-free-mib(engine argument, default 4096, 0 = off;5.12 GiB free for a 63K file), also when replacing a file; and its RAM preflight - the checkpoint copies, the copied live state, the source
directory and the write buffer, above the parking floor - runs before anything is copied. Both are preflights, not
reservations; RAM is the host's available memory, not a cgroup limit.
O_NOFOLLOW,FILE_FLAG_OPEN_REPARSE_POINT), only a regular file with one link. Fileand runtime validation (size against the largest this session can restore, header, model and config
fingerprints before the payload is parsed, a RAM preflight for the parse's peak, geometry and layer range, every count against the bytes left
and this session's exact limits - tokens, checkpoints, layers, each state array and K/V part - before allocating
it, payload hash, snapshot validation) complete
before any device write; a rejection keeps the active session. A device-transfer failure is fatal.
PLE, the pack's own files, the MTP files) and every file the expert source actually read its experts from, per
layer and role as
native_experts.txtresolved it; each (role, file) pair counts, each file is read once; the config fingerprint holds typed, length-prefixed fields: engineversion, backend, KV settings, context, the resolved RoPE configuration, a digest of the uploaded control-vector
tables (content, exact scale, mode, layers, direction) and the arithmetic switches. The version is the release
string: another version is refused, two builds of one version are not told apart.
Refused:
--layer-split,--peer-device,--batch/"parallel"(#465; the server answers501),--prompt-cache 0.Files
include/strata/core/conversation_file.hpp,src/core/conversation_file.cppsrc/core/conversation_file_test.cppinclude/strata/core/conversation_snapshot.hpp,src/core/conversation_snapshot.cpp,src/core/conversation_state.cppsrc/core/conversation_snapshot_test.cppsrc/program/generate.cpp,include/strata/core/progress.hppSAVE/RESTORE, fingerprints,--session-min-free-mib, the watchdog's bounded allowance for blocking stepsinclude/strata/core/expert_source.hpp,src/core/expert_source.cppserve/server.py,serve/test_slots.py,serve/test_security.pySWAIT, status; 30 + security testsCMakeLists.txt,docs/DETAILS.mdHow to test
A request whose messages continue the restored conversation reuses the restored state or its checkpoint; a short
tail may be read again. Clients still send their messages and images.
Rebased on v0.1.39
Two textual conflicts, both resolved keeping upstream's code: the
/v1/responsesdispatch inserve/server.py(theslot route is now one more
elifafter it) and the--conversation-cache-min-free-mibhelp line ingenerate.cpp(next to
--vram-elastic/--batch);ArenaExpertSource::close()merged automatically except for one hunk (the newSTRATA_ARENA_MMAPunmap stays, the PR'sinputs_.clear()added after it). One commit on top for two thingsv0.1.39 changed underneath the PR:
request being admitted. With
--batchthe engine refusesSAVE/RESTORE(SERR invalid) and the server answers501before sending anything (new test inserve/test_slots.py; docs updated).STRATA_ARENA_MMAP=1: the new mapped-arena path returned before recording the experts file it read, so the modelfingerprint would have missed
experts.bin; it now records it like the pinned arena.#583 (the byte-budget ring, on by default) only sizes the prompt path's streamed ring; nothing it changes is in the
saved state, so the file format (v1) and its golden bytes are unchanged.
Checked on v0.1.39 (
6f32ec0+ this branch), the same machines as below; unmodified v0.1.39 built and run the same wayfor comparison:
argmaxremoved).conversation_file_test185,conversation_cache_test4191,conversation_memory_test23,conversation_validation_test972 (host-only),file_expert_source_test,expert_profile_save_test,pinned_shared_testpass. The GPU there was in use, soconversation_snapshot_testran on AMD (below).serve/test_*.py): 299 tests run on the branch, 268 on unmodified v0.1.39 (+30test_slots, +1test_security); in both the same one failure, not this PR's:test_responses.OverHttp.test_json_schema_text_format.conversation_snapshot_test3,901 checks on the GPU,6 runs of 6 (unmodified v0.1.39: 2,169);
conversation_file_test185 (also buffered);ctest -R conversation_5/5.Tested on (v0.1.38 base)
Linux + NVIDIA: RTX 4070 Ti 12 GB, Ryzen 9 5900X, 64 GB RAM, NVMe ext4, CUDA 13.1, gcc-15, sm_89, IQ3_XXS pack, the
setup's config (spec 4 + MTP,
--kv int8), binary sha2563bbe4fc38ab3...(this HEAD's code; the last commitchanges docs only):
verify.cpp, unused variable) is in a file this change does not touch;conversation_file_test185/185 (default andSTRATA_SESSION_BUFFERED=1),conversation_cache/memory/validation_testpass;
conversation_snapshot_test3,621 checks pass on the GPU. On a second Linux PC (gcc 15) the file test alsobuilds with
-Wall -Wextra -Werrorand passes under ASan+UBSan;same way with and without the branch in this Linux environment (it needs llama.cpp's gguf-py), so it is not
claimed as passing;
--short-read 13000)are byte-identical to v0.1.38's; this does not establish parity of every batched-prefill or restore path.
End to end at 63K
62,993-token prompt + 32 tokens,
--max-context 65536. Single runs:SESSIONlines, at most 16 MiB apart;SWAITallowances: capture 88 s, flush 346 s, publish 346 sSWAITallowances: validate 345 s, transfer 345 s--rope-freq-base(fresh engine, which then answered)--conversation-cache-min-free-mib 100000000:SERR memory 0in 2 ms, before any copy; the file it would replace unchanged, no temporary file, the engine answered afterwardsContinuation limit. A session file saves conversation state, not all of the process's execution history. Exact
future token replay across restarts is not guaranteed: expert residency and CPU/GPU rounding can change later output.
In the 63K tests the first 32-token continuation after a restore matched the process that kept running; on the next
continuation the restored processes agreed with one another but differed from that process from the 28th token on
(zero-based index 27). A refused, corrupted restore in between did not change the restored processes' continuation.
The cause has not been isolated.
Windows: an earlier revision's CPU file-I/O test passed as a 32-bit Windows executable (zig 0.16 clang,
x86-windows-gnu) under Wine 10.0; the current code has not been built for Windows. Not a Windows engine/GPUvalidation; not built with MSVC, not run on real Windows; UNC/Unicode paths and large files not tested there. Help with Windows would be welcome.
AMD (HIP): built and run on an RX 6650 XT (Navi 23, gfx1032, 8 GB), Ryzen 7 5700G, Ubuntu with kernel 7.0,
ROCm 7.14.0a20260612 from AMD's TheRock wheels (
gfx103X-allindex, in a user venv; the pinned7.10.0a20251120 is no longer on that index).
hip_backend.cmakerefuses gfx1032, so the build is-DCMAKE_HIP_ARCHITECTURES=gfx1030 -DSTRATA_BUILD_CONVERSATION_TESTS=ON(the setup.py HIP flags), run withROCR_VISIBLE_DEVICES=0 HSA_OVERRIDE_GFX_VERSION_1=10.3.0: without the first, the HSA runtime segfaults inGpuAgent::InitDmaon the 5700G's integrated gfx90c before any Strata code runs, the same on unmodifiedv0.1.38; a global
HSA_OVERRIDE_GFX_VERSIONfails with HSA_STATUS_ERROR_OUT_OF_RESOURCES.strata-deviceseesthe card as gfx1030 wave32 and its
--selftestpasses. On that card:conversation_snapshot_test3621 checkspassed (6 runs of 6, 0.9 s each; unmodified v0.1.38: 1889 checks),
conversation_file_test185 checks, alsowith
STRATA_SESSION_BUFFERED=1,conversation_cache_test4175,conversation_memory_test23,conversation_validation_test972 (host-only);ctest -R conversation_5 of 5. The HIP build has no newwarnings in the files this PR touches (67 against 68 on v0.1.38: one unused
argmaxremoved). Not run on AMD:the model itself (no model on this card), so session save/restore with real weights is measured on NVIDIA only.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.