sqlite

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sqlite provides the SQLite persistence layer for the Provenance task dependency tracker. It implements all CRUD operations for tasks, edges, agents, labels, comments, and activities.

This package imports pkg/ptypes for all type definitions and uses zombiezen.com/go/sqlite for pure-Go SQLite access (no CGo required at runtime, though CGo tests use the C library for the race detector).

The DB struct holds a single SQLite connection guarded by a sync.Mutex. All exported methods acquire the mutex before accessing the connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanActivity

func ScanActivity(stmt *zs.Stmt) (ptypes.Activity, error)

ScanActivity converts a SQL result row into a ptypes.Activity. The stmt must select:

id, agent_id, phase_id, stage_id, started_at, ended_at, notes

(7 columns, indexed 0–6).

func ScanComment

func ScanComment(stmt *zs.Stmt) (ptypes.Comment, error)

ScanComment converts a SQL result row into a ptypes.Comment. The stmt must select:

id, task_id, author_id, body, created_at

(5 columns, indexed 0–4).

func ScanTask

func ScanTask(stmt *zs.Stmt) (ptypes.Task, error)

ScanTask converts a SQL result row into a ptypes.Task. The stmt must select:

id, namespace, title, description, status_id, priority_id, type_id,
phase_id, owner_id, notes, created_at, updated_at, closed_at, close_reason

(14 columns, indexed 0–13).

func TimeToNullInt

func TimeToNullInt(t *time.Time) any

TimeToNullInt converts *time.Time to a nullable int64 value for SQLite. Returns nil if t is nil, otherwise returns t.UnixNano().

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB wraps a single SQLite connection with a mutex for safe concurrent access. Use Open to create a new DB instance.

func Open

func Open(dbPath string, models []ptypes.ModelEntry) (*DB, error)

Open opens (or creates) a SQLite database at dbPath and returns an initialised DB. Pass ":memory:" for an in-memory database.

The schema is applied idempotently on every open (CREATE TABLE IF NOT EXISTS). Reference data (enums) is inserted via INSERT OR IGNORE. The models parameter provides the ML model entries to seed into ml_models.

func (*DB) AddComment

func (db *DB) AddComment(id ptypes.TaskID, authorID ptypes.AgentID, body string) (ptypes.Comment, error)

AddComment adds a comment to a task authored by authorID. A UUIDv7 CommentID is assigned automatically. Acquires the DB mutex.

func (*DB) AddLabel

func (db *DB) AddLabel(id ptypes.TaskID, label string) error

AddLabel attaches a label to a task. Idempotent (INSERT OR IGNORE). Acquires the DB mutex.

func (*DB) BlockedTasks

func (db *DB) BlockedTasks() ([]ptypes.Task, error)

BlockedTasks returns tasks that are not closed and have at least one open blocker. Acquires the DB mutex.

func (*DB) Close

func (db *DB) Close() error

Close releases the SQLite connection. Safe to call multiple times.

func (*DB) CloseTask

func (db *DB) CloseTask(id ptypes.TaskID, reason string, now time.Time) (ptypes.Task, error)

CloseTask marks a task as closed with the given reason. Returns the updated task. Returns ptypes.ErrNotFound if the task does not exist after the update. Acquires the DB mutex.

func (*DB) Conn

func (db *DB) Conn() *zs.Conn

Conn returns the underlying SQLite connection. This is exposed so that the root package's graphStore can access the connection for vertex/edge operations without duplicating SQL. The caller MUST hold the DB mutex (via Lock/Unlock) when using this connection.

func (*DB) DeleteEdge

func (db *DB) DeleteEdge(sourceID ptypes.TaskID, targetID string, kind ptypes.EdgeKind) error

DeleteEdge deletes an edge. Acquires the DB mutex.

func (*DB) EndActivity

func (db *DB) EndActivity(id ptypes.ActivityID) (ptypes.Activity, error)

EndActivity records the end time of an activity. Returns the updated activity. Returns ptypes.ErrNotFound if the activity does not exist. Acquires the DB mutex.

func (*DB) GetActivities

func (db *DB) GetActivities(agentID *ptypes.AgentID) ([]ptypes.Activity, error)

GetActivities returns all activities, optionally filtered by agent. Pass nil to return activities for all agents. Acquires the DB mutex.

func (*DB) GetAgent

func (db *DB) GetAgent(id ptypes.AgentID) (ptypes.Agent, error)

GetAgent returns the base agent (kind only) by ID. Returns ptypes.ErrNotFound if the agent does not exist. Acquires the DB mutex.

func (*DB) GetBlockedByEdges

func (db *DB) GetBlockedByEdges() ([]ptypes.Edge, error)

GetBlockedByEdges returns all EdgeBlockedBy edges in the database. Acquires the DB mutex.

func (*DB) GetComments

func (db *DB) GetComments(id ptypes.TaskID) ([]ptypes.Comment, error)

GetComments returns all comments on a task in chronological order. Acquires the DB mutex.

func (*DB) GetDepTree

func (db *DB) GetDepTree(rootID ptypes.TaskID) ([]ptypes.Edge, error)

GetDepTree returns all blocked-by edges reachable from rootID via DFS. The result is in DFS traversal order. Acquires the DB mutex.

func (*DB) GetEdges

func (db *DB) GetEdges(sourceID ptypes.TaskID, kind *ptypes.EdgeKind) ([]ptypes.Edge, error)

GetEdges returns edges originating from sourceID, optionally filtered by kind. Pass nil for kind to get all edge kinds. Acquires the DB mutex.

func (*DB) GetHumanAgent

func (db *DB) GetHumanAgent(id ptypes.AgentID) (ptypes.HumanAgent, error)

GetHumanAgent returns the human agent by ID. Returns ptypes.ErrNotFound if not found or if the agent is a different kind. Acquires the DB mutex.

func (*DB) GetLabels

func (db *DB) GetLabels(id ptypes.TaskID) ([]string, error)

GetLabels returns all labels attached to a task, sorted alphabetically. Acquires the DB mutex.

func (*DB) GetMLAgent

func (db *DB) GetMLAgent(id ptypes.AgentID) (ptypes.MLAgent, error)

GetMLAgent returns the ML agent by ID. Returns ptypes.ErrNotFound if not found or if the agent is a different kind. Acquires the DB mutex.

func (*DB) GetSoftwareAgent

func (db *DB) GetSoftwareAgent(id ptypes.AgentID) (ptypes.SoftwareAgent, error)

GetSoftwareAgent returns the software agent by ID. Returns ptypes.ErrNotFound if not found or if the agent is a different kind. Acquires the DB mutex.

func (*DB) GetTask

func (db *DB) GetTask(id ptypes.TaskID) (ptypes.Task, bool, error)

GetTask retrieves a task by ID. Returns (task, true, nil) if found, (zero, false, nil) if not found, or (zero, false, err) on error. Acquires the DB mutex.

func (*DB) InsertEdge

func (db *DB) InsertEdge(sourceID ptypes.TaskID, targetID string, kind ptypes.EdgeKind, now time.Time) error

InsertEdge inserts a typed edge. Acquires the DB mutex.

func (*DB) InsertTask

func (db *DB) InsertTask(task ptypes.Task) error

InsertTask inserts a task row into the tasks table. Acquires the DB mutex.

func (*DB) ListTasks

func (db *DB) ListTasks(filter ptypes.ListFilter) ([]ptypes.Task, error)

ListTasks returns tasks matching the given filter. An empty filter returns all tasks ordered by creation time (ascending). Acquires the DB mutex.

func (*DB) Lock

func (db *DB) Lock()

Lock acquires the DB mutex. Use this when you need direct access to Conn().

func (*DB) ReadyTasks

func (db *DB) ReadyTasks() ([]ptypes.Task, error)

ReadyTasks returns tasks that are not closed and have no open blockers. Acquires the DB mutex.

func (*DB) RegisterHumanAgent

func (db *DB) RegisterHumanAgent(namespace, name, contact string) (ptypes.HumanAgent, error)

RegisterHumanAgent registers a new human agent with a UUIDv7 ID. Acquires the DB mutex.

func (*DB) RegisterMLAgent

func (db *DB) RegisterMLAgent(namespace string, role ptypes.Role, provider ptypes.Provider, modelName ptypes.ModelID) (ptypes.MLAgent, error)

RegisterMLAgent registers a new ML agent. The (provider, modelName) pair must exist in the ml_models seed table; returns ptypes.ErrNotFound if unknown. Acquires the DB mutex.

func (*DB) RegisterSoftwareAgent

func (db *DB) RegisterSoftwareAgent(namespace, name, version, source string) (ptypes.SoftwareAgent, error)

RegisterSoftwareAgent registers a new software agent with a UUIDv7 ID. Acquires the DB mutex.

func (*DB) RemoveLabel

func (db *DB) RemoveLabel(id ptypes.TaskID, label string) error

RemoveLabel detaches a label from a task. Idempotent (no error if not present). Acquires the DB mutex.

func (*DB) StartActivity

func (db *DB) StartActivity(agentID ptypes.AgentID, phase ptypes.Phase, stage ptypes.Stage, notes string) (ptypes.Activity, error)

StartActivity records the start of an activity for the given agent. A UUIDv7 ActivityID is assigned automatically. Acquires the DB mutex.

func (*DB) StartActivityWithID added in v0.0.2

func (db *DB) StartActivityWithID(id ptypes.ActivityID, agentID ptypes.AgentID, phase ptypes.Phase, stage ptypes.Stage, notes string) (ptypes.Activity, error)

StartActivityWithID records the start of an activity using a CALLER-SUPPLIED ActivityID, idempotently. Unlike StartActivity (which mints a random UUIDv7), the caller owns the id; a second call with the same id is a no-op (INSERT ... ON CONFLICT(id) DO NOTHING) and the existing row is returned. This makes activity emission safe to replay — e.g. when a durable-workflow step re-executes after a crash, a deterministic id (such as a name-based UUIDv5 over the workflow's logical identity) collapses the duplicate to one row. Acquires the DB mutex.

func (*DB) TaskCount

func (db *DB) TaskCount() (int, error)

TaskCount returns the total number of tasks via COUNT(*). This is O(1) in SQLite (index scan). Acquires the DB mutex.

func (*DB) Unlock

func (db *DB) Unlock()

Unlock releases the DB mutex.

func (*DB) UpdateTask

func (db *DB) UpdateTask(id ptypes.TaskID, fields ptypes.UpdateFields, now time.Time) (ptypes.Task, error)

UpdateTask applies partial updates to a task. Returns the updated task. Returns ptypes.ErrNotFound if the task does not exist. Acquires the DB mutex.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL