store

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package store is the Postgres metadata store: principals, repos, branches, efforts, workspaces, changesets and the operation log. File contents and trees live in the blob store; this package holds everything else.

Index

Constants

View Source
const TokenPrefix = "vxt_"

TokenPrefix starts every vx token, so leaked tokens are easy to spot.

Variables

View Source
var ErrExists = errors.New("already exists")

ErrExists is returned when a unique row already exists.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a row does not exist.

Functions

func NewID

func NewID() uuid.UUID

NewID returns a new random UUID.

func Slugify

func Slugify(name string) string

Slugify turns an effort name into a short URL-safe slug.

func ValidateHandle

func ValidateHandle(kind PrincipalKind, handle string) error

ValidateHandle checks a handle: lowercase name, and for agents "owner/name".

func ValidateRepoName

func ValidateRepoName(name string) error

ValidateRepoName checks names like "shop-api" or "acme/shop-api".

Types

type Branch

type Branch struct {
	RepoID   uuid.UUID
	Name     string
	Head     hash.Hash // changeset
	GitSHA   string
	SyncedAt time.Time
}

Branch is an upstream branch that vx follows.

type ChangesetRow

type ChangesetRow struct {
	Hash     hash.Hash
	RepoID   uuid.UUID
	EffortID *uuid.UUID
	*object.Changeset
}

ChangesetRow is a changeset with the metadata vx keeps beside it.

type Effort

type Effort struct {
	ID         uuid.UUID
	RepoID     uuid.UUID
	Slug       string
	Name       string
	Goal       string
	BaseBranch string
	Base       hash.Hash // upstream changeset the effort sits on
	Head       hash.Hash
	Version    int64
	State      EffortState
	OwnerID    uuid.UUID
	CreatedAt  time.Time
	UpdatedAt  time.Time
}

Effort is a shared line of work that agents land changes into.

type EffortState

type EffortState string

EffortState is the lifecycle state of an effort.

const (
	EffortOpen      EffortState = "open"
	EffortShipping  EffortState = "shipping"
	EffortMerged    EffortState = "merged"
	EffortAbandoned EffortState = "abandoned"
)

type FileChange

type FileChange struct {
	object.Change
	Added   int
	Deleted int
}

FileChange is a changed path recorded with a changeset, with line counts.

type GitObject

type GitObject struct {
	GitHash string
	VXHash  hash.Hash
	Kind    string // blob or tree
}

GitObject maps a git object to the vx object with the same content.

type Op

type Op struct {
	ID      int64
	RepoID  *uuid.UUID
	ActorID *uuid.UUID
	Op      string
	Target  string
	Before  json.RawMessage
	After   json.RawMessage
	At      time.Time
}

Op is one entry in the operation log.

type Principal

type Principal struct {
	ID          uuid.UUID
	Kind        PrincipalKind
	Handle      string // "alice" or "alice/claude-backend"
	ParentID    *uuid.UUID
	DisplayName string
	Email       string
	CreatedAt   time.Time
}

Principal is a human, an agent acting for a human, or a service account.

func (Principal) Ref

func (p Principal) Ref() string

Ref is the principal's reference string, as recorded on changesets: "user:alice", "agent:alice/claude-backend" or "service:ci".

type PrincipalKind

type PrincipalKind string

PrincipalKind is the type of an identity.

const (
	KindHuman   PrincipalKind = "human"
	KindAgent   PrincipalKind = "agent"
	KindService PrincipalKind = "service"
)

type Provider

type Provider string

Provider is where a repo's upstream lives.

const (
	ProviderGitHub Provider = "github" // remote is "owner/name"
	ProviderGit    Provider = "git"    // remote is a git URL or path
	ProviderNone   Provider = "none"   // vx only, nothing to sync or ship to
)

type Q

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

Q holds the query methods. Store and Tx both embed it, so every query can run on its own or inside a transaction.

func (Q) AdvanceEffort

func (q Q) AdvanceEffort(ctx context.Context, id uuid.UUID, seenVersion int64, newHead hash.Hash) (bool, error)

AdvanceEffort moves the effort head with a compare-and-swap on version. It reports false when another writer moved the head first.

func (Q) Authenticate

func (q Q) Authenticate(ctx context.Context, secret string) (Principal, error)

Authenticate resolves a token to its principal.

func (Q) Chain

func (q Q) Chain(ctx context.Context, repoID uuid.UUID, head, stop hash.Hash, limit int) ([]ChangesetRow, error)

Chain returns changesets from head back along first parents, stopping before stop (exclusive) or after limit rows. Newest first.

func (Q) ChangesetByGitSHA

func (q Q) ChangesetByGitSHA(ctx context.Context, repoID uuid.UUID, sha string) (ChangesetRow, error)

ChangesetByGitSHA finds the changeset imported from a git commit.

func (Q) CreateEffort

func (q Q) CreateEffort(ctx context.Context, e Effort) (Effort, error)

CreateEffort inserts an effort with a unique slug derived from its name.

func (Q) CreatePrincipal

func (q Q) CreatePrincipal(ctx context.Context, p Principal) (Principal, error)

CreatePrincipal inserts a principal.

func (Q) CreateRepo

func (q Q) CreateRepo(ctx context.Context, r Repo) (Repo, error)

CreateRepo inserts a repo and its upstream.

func (Q) CreateToken

func (q Q) CreateToken(ctx context.Context, principalID uuid.UUID, name string) (string, error)

CreateToken issues a new token for a principal and returns the secret. Only its hash is stored, so the secret cannot be shown again.

func (Q) CreateWorkspace

func (q Q) CreateWorkspace(ctx context.Context, w Workspace) (Workspace, error)

CreateWorkspace inserts an open workspace.

func (Q) EffortByID

func (q Q) EffortByID(ctx context.Context, id uuid.UUID) (Effort, error)

EffortByID finds an effort by id.

func (Q) FileChanges

func (q Q) FileChanges(ctx context.Context, repoID uuid.UUID, h hash.Hash) ([]FileChange, error)

FileChanges returns the recorded file list of a changeset.

func (Q) FinishWorkspace

func (q Q) FinishWorkspace(ctx context.Context, id uuid.UUID, state WorkspaceState, landedAs hash.Hash) error

FinishWorkspace marks a workspace landed (with its changeset) or abandoned.

func (Q) GetBranch

func (q Q) GetBranch(ctx context.Context, repoID uuid.UUID, name string) (Branch, error)

GetBranch returns a tracked branch.

func (Q) GetChangeset

func (q Q) GetChangeset(ctx context.Context, repoID uuid.UUID, h hash.Hash) (ChangesetRow, error)

GetChangeset loads a changeset by hash.

func (Q) GetEffort

func (q Q) GetEffort(ctx context.Context, repoID uuid.UUID, slug string) (Effort, error)

GetEffort finds an effort by repo and slug.

func (Q) GetWorkspace

func (q Q) GetWorkspace(ctx context.Context, id uuid.UUID) (Workspace, error)

GetWorkspace loads a workspace.

func (Q) GetWorkspaceForUpdate

func (q Q) GetWorkspaceForUpdate(ctx context.Context, id uuid.UUID) (Workspace, error)

GetWorkspaceForUpdate loads a workspace and locks its row until the transaction ends, so two lands of the same workspace cannot race.

func (Q) GitHashesFor

func (q Q) GitHashesFor(ctx context.Context, repoID uuid.UUID, vxHashes []hash.Hash) (map[hash.Hash]string, error)

GitHashesFor returns git hashes for vx objects (the reverse mapping).

func (Q) InsertChangeset

func (q Q) InsertChangeset(ctx context.Context, repoID uuid.UUID, effortID *uuid.UUID, cs *object.Changeset, files []FileChange) (hash.Hash, error)

InsertChangeset stores a changeset and its file list. Inserting the same changeset twice is a no-op, which makes imports safe to repeat.

func (Q) ListAgents

func (q Q) ListAgents(ctx context.Context, owner uuid.UUID) ([]Principal, error)

ListAgents returns the agents acting for owner.

func (Q) ListBranches

func (q Q) ListBranches(ctx context.Context, repoID uuid.UUID) ([]Branch, error)

ListBranches returns the tracked branches of a repo.

func (Q) ListEfforts

func (q Q) ListEfforts(ctx context.Context, repoID uuid.UUID, state EffortState) ([]Effort, error)

ListEfforts lists a repo's efforts, newest first. An empty state lists all.

func (Q) ListOps

func (q Q) ListOps(ctx context.Context, repoID uuid.UUID, limit int) ([]Op, error)

ListOps returns a repo's newest operations first.

func (Q) ListRepos

func (q Q) ListRepos(ctx context.Context) ([]Repo, error)

ListRepos returns all repos by name.

func (Q) ListShipments

func (q Q) ListShipments(ctx context.Context, effortID uuid.UUID) ([]Shipment, error)

ListShipments returns an effort's shipments with their changes.

func (Q) ListWorkspaces

func (q Q) ListWorkspaces(ctx context.Context, effortID uuid.UUID, state WorkspaceState) ([]Workspace, error)

ListWorkspaces lists an effort's workspaces. An empty state lists all.

func (Q) LogOp

func (q Q) LogOp(ctx context.Context, repoID, actorID *uuid.UUID, op, target string, before, after any) error

LogOp appends to the operation log. before and after are marshaled to JSON.

func (Q) LookupGitObjects

func (q Q) LookupGitObjects(ctx context.Context, repoID uuid.UUID, gitHashes []string) (map[string]hash.Hash, error)

LookupGitObjects returns the vx hashes known for the given git hashes.

func (Q) OnChain

func (q Q) OnChain(ctx context.Context, repoID uuid.UUID, head, target hash.Hash, limit int) (bool, error)

OnChain reports whether target is head or one of its first-parent ancestors, looking back at most limit steps.

func (Q) PrincipalByHandle

func (q Q) PrincipalByHandle(ctx context.Context, handle string) (Principal, error)

PrincipalByHandle looks a principal up by handle.

func (Q) PrincipalByID

func (q Q) PrincipalByID(ctx context.Context, id uuid.UUID) (Principal, error)

PrincipalByID looks a principal up by id.

func (Q) RebaseEffort

func (q Q) RebaseEffort(ctx context.Context, id uuid.UUID, seenVersion int64, newBase, newHead hash.Hash) (bool, error)

RebaseEffort moves the effort onto a new base and head, with the same compare-and-swap as AdvanceEffort.

func (Q) RepoByID

func (q Q) RepoByID(ctx context.Context, id uuid.UUID) (Repo, error)

RepoByID looks a repo up by id.

func (Q) RepoByName

func (q Q) RepoByName(ctx context.Context, name string) (Repo, error)

RepoByName looks a repo up by name.

func (Q) ResolveChangeset

func (q Q) ResolveChangeset(ctx context.Context, repoID uuid.UUID, ref string) (ChangesetRow, error)

ResolveChangeset finds a changeset by full hash, hex prefix (at least 6 characters) or change ID.

func (Q) SaveGitObjects

func (q Q) SaveGitObjects(ctx context.Context, repoID uuid.UUID, objs []GitObject) error

SaveGitObjects records mappings; existing ones are kept.

func (Q) SaveShipment

func (q Q) SaveShipment(ctx context.Context, s Shipment) (Shipment, error)

SaveShipment inserts a shipment or updates the one for the same effort and branch, replacing its change list.

func (Q) SetEffortState

func (q Q) SetEffortState(ctx context.Context, id uuid.UUID, state EffortState) error

SetEffortState changes the lifecycle state.

func (Q) SetWorkspaceBase

func (q Q) SetWorkspaceBase(ctx context.Context, id uuid.UUID, base hash.Hash) error

SetWorkspaceBase records the snapshot the workspace's files are based on.

func (Q) UpsertBranch

func (q Q) UpsertBranch(ctx context.Context, b Branch) error

UpsertBranch records the current head of an upstream branch.

type Repo

type Repo struct {
	ID            uuid.UUID
	Name          string
	Provider      Provider
	Remote        string
	DefaultBranch string
	CreatedAt     time.Time
}

Repo is a repository known to vx and its upstream.

type Shipment

type Shipment struct {
	ID          uuid.UUID
	EffortID    uuid.UUID
	Title       string
	Branch      string
	Target      string
	BaseGitSHA  string
	HeadGitSHA  string
	ProviderRef string
	URL         string
	State       string
	CreatedBy   uuid.UUID
	CreatedAt   time.Time
	UpdatedAt   time.Time
	Changes     []ShipmentChange
}

Shipment is an effort's changes pushed as a branch, usually with a PR.

type ShipmentChange

type ShipmentChange struct {
	Position  int
	Changeset hash.Hash
	ChangeID  string
	GitSHA    string
}

ShipmentChange is one changeset in a shipment and the commit made for it.

type Store

type Store struct {
	Q
	// contains filtered or unexported fields
}

Store is the connection pool plus queries.

func Open

func Open(ctx context.Context, url string) (*Store, error)

Open connects to Postgres.

func (*Store) Close

func (s *Store) Close()

Close closes the pool.

func (*Store) InTx

func (s *Store) InTx(ctx context.Context, fn func(tx *Tx) error) error

InTx runs fn in a transaction. The transaction commits if fn returns nil.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context) error

Migrate applies pending migrations in order, each in its own transaction.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping checks the database connection.

type Tx

type Tx struct {
	Q
	// contains filtered or unexported fields
}

Tx is a transaction plus queries.

type Workspace

type Workspace struct {
	ID        uuid.UUID
	EffortID  uuid.UUID
	OwnerID   uuid.UUID
	Intent    string
	ChangeID  string
	Base      hash.Hash
	State     WorkspaceState
	LandedAs  hash.Hash
	CreatedAt time.Time
	UpdatedAt time.Time
}

Workspace is one task's draft inside an effort.

type WorkspaceState

type WorkspaceState string

WorkspaceState is the lifecycle state of a workspace.

const (
	WorkspaceOpen      WorkspaceState = "open"
	WorkspaceLanded    WorkspaceState = "landed"
	WorkspaceAbandoned WorkspaceState = "abandoned"
)

Directories

Path Synopsis
Package storetest gives tests an isolated, migrated Postgres database.
Package storetest gives tests an isolated, migrated Postgres database.

Jump to

Keyboard shortcuts

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