feat: crash-safe state persistence via periodic flush#303
Merged
Conversation
4d811d2 to
9937c02
Compare
Persist remaining volatile session state across process restarts without relying on shutdown handlers (which SIGKILL/crashes bypass). Three-tier persistence strategy: - Immediate: session identity (fingerprint, header promotion) — rare - 30s periodic (dirty-flag gated): gradient calibration, cache warming state, and cost tracker snapshot via idle tick DB migration v24 adds 12 columns to session_state for identity (3), cache warming (2), and gradient calibration (7) fields. Review fixes: - Atomic gradient restore (lastTurnAt > 0 proxy, not per-field sentinels) - Dirty flag on SessionState — only flush changed sessions on 30s tick - Moved cost persistence from per-turn to 30s tick (reduces write amplification) - Validate TTL from DB, log.warn on corrupt warmup JSON
9937c02 to
b77e89a
Compare
BYK
added a commit
that referenced
this pull request
May 14, 2026
## Summary Follow-up to #303. Fixes a pre-existing bug where `headerSessionIndex` was empty after process restart, causing Tier 1 session identification to fail and orphan persisted session state. - Load header→session mappings from `session_state` table in `initIfNeeded()` (before the first `identifySession()` call) - Clear `headerSessionIndex` in `resetPipelineState()` for test isolation - New `loadHeaderSessionIndex()` DB function + tests
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #299
Persist remaining volatile session state across process restarts without relying on shutdown handlers (which
SIGKILL/crashes bypass).Changes
session_state— session identity (3), cache warming (2), gradient calibration (7)gradient.ts: NewsaveGradientState()+ atomic restore of gradient fields from DB on firstgetSessionState()access (useslastTurnAt > 0as "ever flushed" proxy)pipeline.ts: Restore session identity + cache warming from DB on session creation; persist fingerprint/headers immediately on mutation; dirty-flag marking on each turnidle.ts: 30s tick flushes gradient + cache warming + cost state for dirty sessions only (skips unchanged sessions)types.ts:_dirtyflag onSessionStatefor periodic flush gating