Documentation
¶
Overview ¶
Package store manages the ctx SQLite database: opening it with the connection pragmas every ctx component depends on, and applying the embedded schema migrations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadQuery ¶
LoadQuery extracts the SQL body of a named query from a sqlc-format file (queries/retrieval.sql), so that file stays the literal, single source of truth actually executed at runtime rather than a hand-copied duplicate baked into Go code. This is deliberately not sqlc codegen — that's worth adopting once the query surface (MCP tools, milestone 4) is big enough for the generated boilerplate to pay for itself; for now, one query (SearchKeyword) doesn't need it.
func MergeProjects ¶
MergeProjects folds project `from` into project `into` and deletes `from`.
Why this exists: projects are identified by normalised git remote, but a session whose cwd has since vanished (a deleted or moved checkout) can no longer have its remote read, so it lands in a remote-less project of its own and project-scoped search never finds it. Think of two client files for the same client, opened before anyone knew they were the same person: merging moves every page into one folder and shreds the empty one.
Rules:
- from and into must differ and both must exist.
- If both carry a git_remote they are different repos by definition, so the merge is refused. If only `from` has one, `into` inherits it.
- Vectors of moved chunks are dropped and those chunks are marked pending again: chunk_vectors.project_id is vec0's PARTITION KEY and can't be updated in place (see internal/embed/worker.go), so the embed worker simply re-embeds them under the new project.
Everything happens in one transaction; a failure leaves both projects untouched.
func Open ¶
Open opens (creating if necessary) the SQLite database at path with the four connection pragmas required by every ctx component: journal_mode=WAL, foreign_keys=ON, busy_timeout=5000, synchronous=NORMAL.
The pragmas ride on the DSN rather than a post-Open Exec. database/sql pools connections lazily and opens more of them under concurrent load, and PRAGMA journal_mode/foreign_keys/synchronous are per-connection state in SQLite — an Exec run once right after Open only ever reaches whichever single connection the pool happened to create first. Putting the pragmas in the DSN's query string makes mattn/go-sqlite3 apply them to every connection it opens, present and future.
func ResolveProject ¶
ResolveProject finds or creates the project that owns cwd. Matching is by the longest known project_paths prefix, so an agent invoked in a subdirectory of a known project root still resolves to that project — the same rule the daemon will use later when mapping a live agent's cwd to a project.
A brand-new cwd creates a new project. If cwd is inside a git repo with an `origin` remote, that remote (normalized) becomes the project's identity: if a project with the same normalized remote already exists — the same repo cloned at a different path, WSL vs. a Codespace being the motivating case — cwd is just added as another path to that existing project instead of creating a duplicate. No remote (not a git repo, or a repo with none configured) means the project is identified purely by path.
Types ¶
type MigrateResult ¶
type MigrateResult struct {
VectorsLoaded bool
VectorsErr error // non-nil iff !VectorsLoaded; the reason, for logging
}
MigrateResult reports what Migrate actually applied, so callers (ctx init, tests) can tell whether semantic search is available without parsing error strings.
func Migrate ¶
func Migrate(db *sql.DB) (MigrateResult, error)
Migrate applies the embedded schema migrations. 001_core.sql is non-optional: sessions, messages, keyword search, notes all depend on it, so a failure there is fatal. 002_vectors.sql needs the sqlite-vec extension's vec0 virtual table module; if that module isn't available the CREATE VIRTUAL TABLE statement fails with "no such module: vec0", and Migrate reports that in the result instead of returning an error — the catalogue stays open even when the "find similar cases" desk is closed.
Both migration files are idempotent at the SQL level (CREATE ... IF NOT EXISTS, INSERT OR IGNORE), so calling Migrate more than once on the same database is a no-op, not an error.