refactor(session-persistence-sqlite): drop the materialized column; use row existence as the signal (review #35)

The `materialized` INTEGER column was redundant: create()/update() already
keep a lazy session in memory and write no row, so a `sessions` row is
written only by the first append. Its EXISTENCE is the materialization
signal — has()/list() now report exactly the sessions that have a row,
matching the JSONL backend's "file exists ⇔ materialized".

The column only existed to force has()/list() to FALSE for an all-tail
crash (a partial first turn, zero committed events). That actually
DIVERGED from the JSONL backend, whose file (and thus has()=true) survives
a first append that never reached turn/end. Removing the column drops that
special case: an all-tail session keeps its row and stays present, the
same as JSONL. The orphaned tail rows are still removed by the deferred
truncation-repair on the next append, and load() stays non-mutating.

Also: add a TODO to route through a cordis db service if one is adopted,
and correct the README's Node-version framing to the repo's engines (>=24).
This commit is contained in:
Tianyi Cui
2026-06-16 20:35:01 +08:00
parent c27b1bba6e
commit 3bef6b38c7
4 files changed
+41 -44

No files matched your search

@@ -18,10 +18,11 @@ import type { SessionEvent, SessionId, SessionMeta } from '@deepseek-ai/dsh-sess
export const SCHEMA_VERSION = 1
/**
* A row of the `sessions` table — the out-of-log metadata (`SessionMeta`) plus
* the `materialized` flag that implements lazy materialization (a created-but-
* never-appended session has `materialized = 0` and is excluded from
* `has`/`list`, mirroring the JSONL backend's "no file until first append").
* A row of the `sessions` table — the out-of-log metadata (`SessionMeta`). The
* row's EXISTENCE is the materialization signal: it is written only by the
* first `append` (lazy materialization), so a created-but-never-appended
* session has no row and is absent from `has`/`list`, mirroring the JSONL
* backend's "no file until first append".
*/
export interface SessionRow {
id: string
@@ -32,7 +33,6 @@ export interface SessionRow {
updated_at: number
title: string | null
first_prompt: string | null
materialized: number
}
/** An `events` table row: one `SessionEvent` mapped 1:1 (`data` is JSON text). */
@@ -81,8 +81,7 @@ export function openDatabase(path: string): DatabaseSync {
parent_session TEXT,
updated_at INTEGER NOT NULL,
title TEXT,
first_prompt TEXT,
materialized INTEGER NOT NULL DEFAULT 0
first_prompt TEXT
) STRICT
`)
db.exec(`