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
- Variables
- func LoadQuery(fsys fs.FS, file, name string) (string, error)
- func LookupProject(db *sql.DB, cwd string) (int64, bool, error)
- func MergeProjects(db *sql.DB, from, into int64) error
- func Open(path string) (*sql.DB, error)
- func ResolveProject(db *sql.DB, cwd string) (int64, error)
- type GitResolver
- type MigrateResult
Constants ¶
const ( // SourceBranch: resolved on the branch the session logged. Reliable, up to // the branch having been rebased since. SourceBranch = "branch" // SourceHead: the branch was unknown (Codex logs none) or has since been // deleted, so the commit was read from whatever HEAD's history says now. // Usually right, unverifiable. SourceHead = "head" // SourceUnverified: a commit stored by an earlier build (some stamped HEAD // at ingest time) that `ctx repair git` could not re-derive because the // repository is gone. Kept, but do not lean on it. SourceUnverified = "unverified" // SourceLogged: the agent's own log carried the commit. SourceLogged = "logged" )
Where a commit came from, recorded in sessions.commit_source so `explain` can say how much to trust it.
Variables ¶
var SourceRank = map[string]int{"": 0, SourceLogged: 1, SourceBranch: 2, SourceHead: 3, SourceUnverified: 4}
SourceRank orders sources from most to least trustworthy; a session's span is only as good as its weakest end, so the stored source only ever worsens.
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 LookupProject ¶ added in v0.2.0
LookupProject finds the project that owns cwd without registering a new project or path. Read-only commands use it to avoid changing the catalogue merely because they were invoked from a new checkout or unrelated folder.
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 GitResolver ¶ added in v0.2.0
type GitResolver struct {
// contains filtered or unexported fields
}
GitResolver answers "which commit was checked out around time T?" for session logs, without one subprocess per log line: it reads each directory's first-parent history once (one `git log`), then answers every lookup from memory. Non-git directories are cached as such, so they cost a single failed spawn.
The answer is the newest commit on the branch (or HEAD when the branch no longer exists) whose committer time is at or before the timestamp. That is the state the session started from, which is what matters for retroactively ingested logs — HEAD as of ingest time would describe a repository the session never saw.
A GitResolver is meant to live for one ingest run: commits made after it loaded a directory are invisible to it, and the next run reloads.
func NewGitResolver ¶ added in v0.2.0
func NewGitResolver() *GitResolver
func (*GitResolver) CommitAt ¶ added in v0.2.0
func (g *GitResolver) CommitAt(dir, branch, ts string) string
CommitAt returns the commit that was current in dir at timestamp ts (RFC 3339), preferring branch's history when branch is non-empty. It returns "" when dir is not a git checkout, ts is unparsable, or every known commit is newer than ts.
func (*GitResolver) ProjectPath ¶ added in v0.2.0
func (g *GitResolver) ProjectPath(cwd, path string) string
ProjectPath turns a path a tool reported into the form file_touches stores: relative to the git repository root, slash-separated. path may be absolute or relative to the session's cwd.
Anchoring at the repo root rather than at cwd matters because agents are often launched from a subdirectory: with cwd-relative paths the same file is "store.go" in one session and "internal/store/store.go" in another, and `ctx explain` from the repo root can only ever find the second.
A path outside the repository is returned exactly as given. When cwd is not a git checkout at all, the root falls back to cwd, which is the behaviour before file-aware memory.
func (*GitResolver) Resolve ¶ added in v0.2.0
func (g *GitResolver) Resolve(dir, branch, ts string) (hash, source string)
Resolve is CommitAt plus how the answer was obtained (SourceBranch or SourceHead). The source is "" whenever the hash is.
func (*GitResolver) Root ¶ added in v0.2.0
func (g *GitResolver) Root(dir string) string
Root returns the git toplevel containing dir, or "" if dir is not inside a checkout (including when the directory no longer exists).
func (*GitResolver) Spawns ¶ added in v0.2.0
func (g *GitResolver) Spawns() int
Spawns reports how many git subprocesses the resolver has started.
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.