Documentation
¶
Overview ¶
Package store implements SCHEMA.md v1.0 on SQLite: append-only truth (events, experiences), rebuildable projections (connections, surprise_ledger), and persistent working state (curiosity_queue).
Index ¶
- func NewID(tsMs int64) string
- type Event
- type Store
- func (s *Store) AllConnections() ([]*core.Connection, error)
- func (s *Store) AmendExperience(id string, tsMs int64, apply func(target *core.Experience) error) (newVer int, err error)
- func (s *Store) AppendEvent(sessionID, typ string, tsMs int64, payload map[string]any) error
- func (s *Store) CarryVerdictForward(sessionID, verdict string, tsMs int64) (newVer int, carried bool, err error)
- func (s *Store) ChildSessions(parentSID string) ([]string, error)
- func (s *Store) ClearProjections() error
- func (s *Store) Close() error
- func (s *Store) ConnectionsFor(kind, target string) ([]*core.Connection, error)
- func (s *Store) CurrentExperiences() ([]*core.Experience, error)
- func (s *Store) CurrentExperiencesBySessionKind(sessionID, kind string) ([]*core.Experience, error)
- func (s *Store) DeleteConnection(kind, scopeKey, target string) error
- func (s *Store) DeleteLedgerFor(kind, scopeKey, target string) error
- func (s *Store) EventsBySession(sessionID string) ([]*Event, error)
- func (s *Store) EventsSince(after int64) ([]*Event, error)
- func (s *Store) ForgetExperiences(tsMs int64, ids []string) (named, superseded int, err error)
- func (s *Store) ForgetSession(sid string) (events, exps int, err error)
- func (s *Store) GetConnection(kind, scopeKey, target string) (*core.Connection, error)
- func (s *Store) InsertExperience(e *core.Experience) error
- func (s *Store) InsertExperiences(exps []*core.Experience) error
- func (s *Store) InsertLedger(e *core.LedgerEntry) error
- func (s *Store) KnownValues(key string) ([]string, error)
- func (s *Store) LastEventTS(eventType string) (tsMs int64, found bool, err error)
- func (s *Store) LatestEventTS() (int64, error)
- func (s *Store) LedgerFor(kind, scopeKey, target string) ([]*core.LedgerEntry, error)
- func (s *Store) MaxEventID() (int64, error)
- func (s *Store) MaxExcess(expIDs []string) (float64, error)
- func (s *Store) PendingSessions(ver int) ([]string, error)
- func (s *Store) RecentProviderCosts(limit int) ([]float64, error)
- func (s *Store) UpsertConnection(c *core.Connection) error
- func (s *Store) Vacuum() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Store ¶
func (*Store) AllConnections ¶
func (s *Store) AllConnections() ([]*core.Connection, error)
func (*Store) AmendExperience ¶
func (s *Store) AmendExperience(id string, tsMs int64, apply func(target *core.Experience) error) (newVer int, err error)
AmendExperience is the corrective verb's whole read-modify-write, made atomic (修正3): the target's (session, kind) lookup, the current-generation read, the superseded check, the newVer computation, the new-generation write, and the user.amended marker all happen inside one transaction. Split across separate autocommit reads with only the final write in a transaction — the pre-fix shape — two concurrent amends of the same (session, kind) could both read the same max(extractor_ver), both compute the same newVer, and both commit a "current" generation at it; rebuild would then double-count whichever generation's evidence survived the write race, since experiences_current's max-ver selection cannot tell the two apart.
apply receives the target row — already loaded with its current-generation content — to edit in place; AmendExperience marks it extractor_model "human" afterwards regardless of which fields apply touched, since being amended at all is what makes a row a human re-perception, not which fields moved. An apply error aborts the transaction with nothing written.
Untouched siblings ride the new generation forward under their own extractor_model — the origin does not lie (ADR-0033 Decision 3) — because experiences_current selects the max extractor_ver per (session, kind): bumping only the target would drop the siblings from the view entirely. Returns the new generation's extractor_ver.
func (*Store) AppendEvent ¶
AppendEvent assigns the next seq within the session atomically.
func (*Store) CarryVerdictForward ¶ added in v0.2.0
func (s *Store) CarryVerdictForward(sessionID, verdict string, tsMs int64) (newVer int, carried bool, err error)
CarryVerdictForward makes an already-perceived session's execution experience show the human's layer-2 judgment now, by copying the current row forward at extractor_ver+1 with only Outcome.Verdict replaced (ADR-0055 Decision 2).
**The event is the truth; this copy only makes it effective today.** The verdict lives in the ledger as user.verdict, and every later re-perception reads it back through parseDeterministic — so the judgment is imperishable and what this carries is immediacy, not permanence. Without it a 👍 would change nothing until the extractor happened to be revised, which is how test.result stayed at zero for six years.
Exactly one row moves. Amend copies the whole current generation forward because experiences_current picks the max ver per (session, kind) and lifting one sibling would drop the others out of the view — but a session holds exactly one execution row (perceiveSession writes one), and preference siblings sit under a different kind, outside this view's grouping. So the full copy-forward amend needs is not needed here.
Nothing else about the row changes. extractor_model keeps its original value rather than becoming "human" (as amend's does): in an experience row the model's own claim is the *context*, while the outcome has always been parseDeterministic's — deterministic code reading events. Rewriting the provenance of a row whose provenance did not change would be the lie (ADR-0033: 出自は嘘をつかない). No user.amended marker is written either, so the session stays perceivable — freezing it would tax the human for using their veto (Decision 3).
carried reports whether a row was found to move. false is ordinary, not an error: a session whose boundary perception has not run yet has nothing to carry, and the pending queue will read the event when it gets there.
func (*Store) ChildSessions ¶
ChildSessions returns the session ids of tasks opened as subtasks of parentSID — their task.started names it as parent (ADR-0023 Decision 2) — in creation order (the events autoincrement id, which is the flat proposal order subtasks open in). The chat's split fold-back reads them to gather each subtask's output for the parent thread (ADR-0028 Decision 5); the flat order lets it tell a subtask that ran from one left 未着手 by a fail-stop.
func (*Store) ClearProjections ¶
ClearProjections wipes everything rebuildable (SCHEMA.md D10).
func (*Store) ConnectionsFor ¶
func (s *Store) ConnectionsFor(kind, target string) ([]*core.Connection, error)
func (*Store) CurrentExperiences ¶
func (s *Store) CurrentExperiences() ([]*core.Experience, error)
func (*Store) CurrentExperiencesBySessionKind ¶
func (s *Store) CurrentExperiencesBySessionKind(sessionID, kind string) ([]*core.Experience, error)
CurrentExperiencesBySessionKind returns the current-generation rows of one (session, kind) — the sibling set amend copies forward together, since experiences_current picks the max extractor_ver per (session, kind) and bumping only the target would drop the siblings from the view (ADR-0033 Decision 3).
func (*Store) DeleteConnection ¶
func (*Store) DeleteLedgerFor ¶
func (*Store) EventsSince ¶
EventsSince returns events with id > after, oldest first — the face window's polling read (ADR-0020 Decision 2: events末尾の定期ポーリング).
func (*Store) ForgetExperiences ¶
ForgetExperiences physically deletes the named experiences and, per ADR-0034 Decision 1, every row in the same (session_id, kind) whose extractor_ver is lower than the named row's. Deleting only the named row would let experiences_current's max(extractor_ver) selection for that group fall through to an older generation — resurrecting a perception the owner had already superseded (by amend or by a re-perception) the moment they forgot its replacement, a world where forgetting could bring a machine perception back over a human one. Sweeping every lower generation closes that off structurally rather than by chasing individual re-surfacing cases.
Only current-generation ids are accepted (ADR-0034 Decision 2 — the same discipline ADR-0033 Decision 3 already puts on amend): naming a superseded id would "forget" a row whose content still lives on in the current generation's copy-forward, which would make the deletion a lie.
It runs in one transaction: every id must exist and be current-generation first — a typo or a superseded id that silently no-ops would forge a "忘れたつもり", the worst failure mode (ADR-0033 Decision 5) — then the append-only guard is dropped, the rows deleted, and the guard recreated, so a crash rolls the trigger back with the rows. The user.forgot marker records only the ids given, never the swept-along superseded ones (ADR-0034 Consequences: the marker is what the owner said to forget, not a deletion log). Returns the count of named rows deleted and the count of superseded rows swept along with them.
func (*Store) ForgetSession ¶
ForgetSession physically deletes a whole session — events and experiences both — the only way to erase raw-log content (ADR-0033 Decision 2). No marker is written: the session vanishes from the events the queue derives from, so there is nothing left to exclude (Decision 4). Both append-only guards drop and come back inside the one transaction. Returns the counts deleted.
func (*Store) GetConnection ¶
func (s *Store) GetConnection(kind, scopeKey, target string) (*core.Connection, error)
func (*Store) InsertExperience ¶
func (s *Store) InsertExperience(e *core.Experience) error
func (*Store) InsertExperiences ¶
func (s *Store) InsertExperiences(exps []*core.Experience) error
InsertExperiences inserts a session's experiences atomically: any single failure rolls the whole batch back. A partial commit would drop the session out of PendingSessions (which is satisfied by even one experience at the version), stranding the uninserted preference experiences forever — truth is append-only, so no rebuild could recover them.
func (*Store) InsertLedger ¶
func (s *Store) InsertLedger(e *core.LedgerEntry) error
func (*Store) KnownValues ¶
KnownValues returns the distinct values already recorded for a context key — the vocabulary handed to the extraction prompt (SCHEMA.md D5). Reads experiences_current, not experiences: vocabulary from superseded extractor versions would otherwise be re-suggested to the model forever, resurrecting exactly the misextractions a version bump was meant to fix.
func (*Store) LastEventTS ¶
LastEventTS returns the timestamp of the most recent event of the given type. Derived from the append-only log, not a counter: the question budget is a View over events (ADR-0007 Decision 3).
func (*Store) LatestEventTS ¶
LatestEventTS returns the newest event timestamp of any type (0 when empty) — the absence detector's anchor (ADR-0019 Decision 2: 不在はeventsの空白から知覚できる).
func (*Store) LedgerFor ¶
func (s *Store) LedgerFor(kind, scopeKey, target string) ([]*core.LedgerEntry, error)
func (*Store) MaxEventID ¶
MaxEventID returns the id of the newest event, 0 when empty — where the face window starts its tail poll so a fresh window never replays history (ADR-0020 Decision 2).
func (*Store) MaxExcess ¶
MaxExcess returns the largest excess surprisal recorded for any of the given experience ids — the batch's sharpest miss, which grades Tomo's reaction line (ADR-0019 Decision 1: 驚き=ADR-0002の導出値の翻訳).
func (*Store) PendingSessions ¶
PendingSessions returns finished sessions that have no experience at extractor_ver >= ver — the Deferred Perception queue, derived by query (SCHEMA.md: no table needed). Sessions the owner has forgotten or amended are excluded permanently (ADR-0033 Decision 4): 人間の知覚は最終知覚であり、 extractor 改版で機械が忘れた記憶を復活させ人の訂正を上書きしてはならない.
func (*Store) RecentProviderCosts ¶
RecentProviderCosts returns the cost_usd values of the most recent provider.finished events that carry one, newest first, up to limit. Only claude-code reports cost_usd (codex does not — implementation confirmed), so this is the sample the parallel gate's cost estimate draws its median from (ADR-0028 Decision 3). An empty result means no real cost has ever been measured: the gate says so honestly rather than inventing a number.
func (*Store) UpsertConnection ¶
func (s *Store) UpsertConnection(c *core.Connection) error
func (*Store) Vacuum ¶
Vacuum reclaims the space freed by forget so a "消した" is not the sovereign lie of leaving the content in WAL frames or free pages (ADR-0033 Decision 5).
VACUUM runs first, checkpoint second — the reverse of the two commands' natural reading order, and worth stating explicitly: in WAL mode, VACUUM writes its rewritten (compacted, freed-page-free) copy of the database into new WAL frames, not into the main db file. Checkpointing first would only flush whatever the WAL held *before* the compaction — the forgotten row's now-unused but not-yet-overwritten page bytes stay in the main file regardless, and VACUUM's own output, still sitting in the WAL when Vacuum returns, is what a later, unrelated checkpoint would silently clean up (or not, if none ever runs). Running VACUUM first means the WAL holds the only up-to-date, post-delete pages; TRUNCATE-checkpointing them into the main file second is the step that actually overwrites the old bytes on disk. Both run outside any transaction — VACUUM cannot run inside one.
The checkpoint runs once, not in a retry loop. Measured (modernc/sqlite, WAL, busy_timeout=5000): an idle read-only connection, and one that has finished a SELECT, both leave TRUNCATE free to run — it returns busy=0 in under a millisecond. Only a connection holding an *open* read transaction pins the snapshot, and against that one the PRAGMA already waits out the full busy_timeout before reporting busy=1. busy_timeout is the wait; a loop around it only multiplies the same 5 seconds by the attempt count (measured: 25s for five tries) and buys nothing, because each attempt has already waited as long as the first. The face window's poller (ADR-0020, mode=ro) queries and returns, so it is the first case, not the third.
A busy report is non-error output from the PRAGMA, so it has to be turned into an error here or it becomes silence. It must not: the logical delete and rebuild are already committed by the time Vacuum runs (cmdForget calls it last), and there is no path back — a second forget of the same id fails as "unknown experience". An honest error is the only chance to say that physical erasure did not finish (Decision 5's 逆向きの嘘もつかない).