Files
Maven/internal/store/migrations.go
T
kami 59a4e06615 maven: scheduled routines + persistent long-term memory
Two additive proactive/recall features.

Routines (internal/routine): a third proactive class beside reminders
(user-stated) and care rules (world-state) — operator-declared clockwork.
config.routines[] (cron + literal RU body + severity) fire through the
normal dispatcher on schedule. Bodies are literal, not LLM-phrased (can't
hallucinate); rule name routine:<name> keeps them out of the care
autotuner; a cold-start guard seeds on first sight so a restart never
replays a missed schedule. Pure routine.Due + config validation, unit-
tested; the tick driver holds the last-fired map and calls fireRoutines.

Persistent memory (internal/store/memory.go): store.MemoryStore backs the
memory.Store interface with the SAME encrypted sqlite db — survives
restarts and recall text inherits at-rest encryption (no plaintext
sidecar). float32-blob vectors, brute-force cosine (ANN is a later swap
behind the interface), upsert-by-id. The daemon wires st.VectorMemory()
into wireVoice; the in-memory impl stays the test/no-store floor. Closes
the "in-memory only, lost on restart" gap (PROGRESS #8).

Gate green: gofmt/vet clean, -race across routine/config/store/mavend.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U2PNdwDj2Gt8YW294J7oSc
2026-07-06 12:37:28 +04:00

60 lines
2.1 KiB
Go

package store
import (
"context"
"database/sql"
"fmt"
)
// migrations are ordered, forward-only schema steps applied after schema.sql.
// Index i (1-based) is the user_version the step at migrations[i-1] brings the
// DB TO; there is no step 0 — schema.sql is the idempotent baseline (version 0).
// An empty slice is a clean no-op that leaves user_version at 0.
//
// To add migration #1 (e.g. the sqlcipher rekey), append its SQL:
//
// var migrations = []string{
// `ALTER TABLE ...;`, // #1
// }
var migrations = []string{
`ALTER TABLE tools ADD COLUMN scope TEXT NOT NULL DEFAULT 'homelab';`, // #1
`ALTER TABLE reminders ADD COLUMN cron TEXT;
ALTER TABLE reminders ADD COLUMN next_fire_ts INTEGER;`, // #2
`CREATE TABLE memory_vectors (
id TEXT PRIMARY KEY,
vec BLOB NOT NULL,
meta TEXT NOT NULL DEFAULT '{}',
created_ts INTEGER NOT NULL
);`, // #3 — long-term vector memory (persistent backend for internal/memory)
}
// migrate applies every migration with a number greater than the DB's current
// user_version, each in its own transaction that also bumps user_version. Fails
// closed: the first erroring step aborts and leaves prior steps committed.
func migrate(ctx context.Context, db *sql.DB) error {
var current int
if err := db.QueryRowContext(ctx, "PRAGMA user_version").Scan(&current); err != nil {
return fmt.Errorf("read user_version: %w", err)
}
for i := current; i < len(migrations); i++ {
version := i + 1 // 1-based: migrations[i] brings DB to `version`
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("migration %d begin: %w", version, err)
}
if _, err := tx.ExecContext(ctx, migrations[i]); err != nil {
_ = tx.Rollback()
return fmt.Errorf("migration %d: %w", version, err)
}
// PRAGMA user_version can't be parameterized; version is our own int.
if _, err := tx.ExecContext(ctx, fmt.Sprintf("PRAGMA user_version = %d", version)); err != nil {
_ = tx.Rollback()
return fmt.Errorf("migration %d bump: %w", version, err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("migration %d commit: %w", version, err)
}
}
return nil
}