mirror of
https://github.com/franalgaba/grimoire.git
synced 2026-09-14 20:26:31 +08:00
f1ee667c8a
* Add preview/commit execution model, remove stale docs and defihack * implement preview/commit flow end-to-end and remove spec-coupled naming * implement value-flow mandate and hook stability * fixed hook * fixed tests * fixed tests * fixed tests * fixed tests * value-flow mandate end-to-end * session lifecycle normalization * added new docs and skills * skill instructions
2.1 KiB
2.1 KiB
State Persistence Reference
State persistence is implemented via StateStore and used by CLI wrappers in packages/cli/src/commands/state-helpers.ts.
Lifecycle
Default CLI flow (simulate, cast):
- Load prior persistent state by spell ID.
- Execute spell with
persistentState. - Save new persistent state.
- Append run record.
- Save ledger entries for run.
--no-state bypasses this flow.
Database
Default SQLite path:
.grimoire/grimoire.db
Configurable via:
--state-dir <dir>(CLI)SqliteStateStore({ dbPath })(API)
Schema tables:
spell_state: latest persistent state per spellruns: execution historyledger: ledger event arrays per run
Pruning:
maxRunsdefaults to100inSqliteStateStore- older runs are pruned on insert
API
StateStore interface:
interface StateStore {
load(spellId: string): Promise<Record<string, unknown> | null>;
save(spellId: string, state: Record<string, unknown>): Promise<void>;
addRun(spellId: string, run: RunRecord): Promise<void>;
getRuns(spellId: string, limit?: number): Promise<RunRecord[]>;
saveLedger(spellId: string, runId: string, entries: LedgerEntry[]): Promise<void>;
loadLedger(spellId: string, runId: string): Promise<LedgerEntry[] | null>;
listSpells(): Promise<string[]>;
}
Run record helper:
createRunRecord(executionResult, provenance?)
Session Views
Derived rollups in runtime:
getSessionLedgerView(store, spellId, limit?)getSessionPnlView(store, spellId, limit?)
history <spell> uses these to print:
- run counts and trigger distribution
- receipt status counts
- value-delta-derived P&L/accounting summary
Node/Bun Compatibility
SqliteStateStore behavior:
- Bun: uses
bun:sqlite - Node: uses
better-sqlite3via adapter
If running in Node without better-sqlite3, store initialization throws an install hint.
Operational Notes
- Ledger entries are stored as JSON (with bigint values stringified by replacer when needed).
runIdand timestamps are generated by runtime context.- Persistent and ephemeral runtime state are distinct; only persistent state is saved across runs.