Documentation
¶
Overview ¶
Package postgres is kōjō's production store: one Postgres database holds the queue (FOR UPDATE SKIP LOCKED), sessions and checkpoints (JSONB), team memory (pgvector, with a full-text fallback), the append-only audit log, config revisions (hot reload through LISTEN/NOTIFY) and everything else.
Index ¶
- type Options
- type Store
- func (s *Store) ActivateRevision(ctx context.Context, id string) (*store.Revision, error)
- func (s *Store) ActiveRevision(ctx context.Context, scope, object string) (*store.Revision, error)
- func (s *Store) ActiveRevisions(ctx context.Context, scope string) ([]store.Revision, error)
- func (s *Store) AddMemory(ctx context.Context, m *store.Memory) error
- func (s *Store) AddSessionMessage(ctx context.Context, m *store.SessionMessage, max int) error
- func (s *Store) AppendAudit(ctx context.Context, e store.AuditEntry) error
- func (s *Store) AppendStep(ctx context.Context, st *store.Step) error
- func (s *Store) Claim(ctx context.Context, worker string, lease time.Duration) (*store.Session, error)
- func (s *Store) ClaimWithin(ctx context.Context, worker string, lease time.Duration, caps store.Caps) (*store.Session, error)
- func (s *Store) Close()
- func (s *Store) CreateCampaign(ctx context.Context, c *store.Campaign) error
- func (s *Store) DecideApproval(ctx context.Context, id, status, by string) (*store.Approval, error)
- func (s *Store) DeleteDraft(ctx context.Context, id string) error
- func (s *Store) DeleteMemory(ctx context.Context, id string) error
- func (s *Store) Enqueue(ctx context.Context, req store.Request) (*store.Session, error)
- func (s *Store) GetApproval(ctx context.Context, id string) (*store.Approval, error)
- func (s *Store) GetCampaign(ctx context.Context, id string) (*store.Campaign, error)
- func (s *Store) GetDraft(ctx context.Context, id string) (*store.SkillDraft, error)
- func (s *Store) GetEvalRun(ctx context.Context, id string) (*store.EvalRun, error)
- func (s *Store) GetMemory(ctx context.Context, id string) (*store.Memory, error)
- func (s *Store) GetProposal(ctx context.Context, id string) (*store.Proposal, error)
- func (s *Store) GetPullRequest(ctx context.Context, repo string, number int) (*store.PullRequest, error)
- func (s *Store) GetRevision(ctx context.Context, id string) (*store.Revision, error)
- func (s *Store) GetScheduleState(ctx context.Context, key string) (*store.ScheduleState, error)
- func (s *Store) GetSession(ctx context.Context, id string) (*store.Session, error)
- func (s *Store) HasVector() bool
- func (s *Store) Heartbeat(ctx context.Context, sessionID, worker string, lease time.Duration) error
- func (s *Store) ListAudit(ctx context.Context, f store.AuditFilter) ([]store.AuditEntry, error)
- func (s *Store) ListCampaigns(ctx context.Context, f store.CampaignFilter) ([]store.Campaign, error)
- func (s *Store) ListDrafts(ctx context.Context, scope string) ([]store.SkillDraft, error)
- func (s *Store) ListEvalRuns(ctx context.Context, skill string, limit int) ([]store.EvalRun, error)
- func (s *Store) ListMemory(ctx context.Context, f store.MemoryFilter) ([]store.Memory, error)
- func (s *Store) ListProposals(ctx context.Context, f store.ProposalFilter) ([]store.Proposal, error)
- func (s *Store) ListPullRequests(ctx context.Context, f store.PullRequestFilter) ([]store.PullRequest, error)
- func (s *Store) ListRevisions(ctx context.Context, scope, object string, limit int) ([]store.Revision, error)
- func (s *Store) ListSessionMessages(ctx context.Context, sessionID string) ([]store.SessionMessage, error)
- func (s *Store) ListSessions(ctx context.Context, f store.SessionFilter) ([]store.Session, error)
- func (s *Store) ListSkillIndex(ctx context.Context, repo string) ([]store.SkillIndexEntry, error)
- func (s *Store) ListSteps(ctx context.Context, sessionID string) ([]store.Step, error)
- func (s *Store) ListUsers(ctx context.Context) ([]store.User, error)
- func (s *Store) MarkSessionMessages(ctx context.Context, sessionID string, ids []string, status, to string) ([]store.SessionMessage, error)
- func (s *Store) Migrate(ctx context.Context) (int, error)
- func (s *Store) PendingMigrations(ctx context.Context) (int, error)
- func (s *Store) Ping(ctx context.Context) error
- func (s *Store) PutApproval(ctx context.Context, a *store.Approval) error
- func (s *Store) PutDraft(ctx context.Context, d *store.SkillDraft) error
- func (s *Store) PutEvalRun(ctx context.Context, r *store.EvalRun) error
- func (s *Store) PutProposal(ctx context.Context, p *store.Proposal) error
- func (s *Store) PutRevision(ctx context.Context, r store.Revision, activate bool) (*store.Revision, error)
- func (s *Store) PutScheduleState(ctx context.Context, st store.ScheduleState) error
- func (s *Store) RecordPullRequest(ctx context.Context, pr store.PullRequest) error
- func (s *Store) Release(ctx context.Context, sessionID, worker string, status store.SessionStatus, ...) error
- func (s *Store) ReplaceSkillIndex(ctx context.Context, repo string, entries []store.SkillIndexEntry) error
- func (s *Store) RequestCancel(ctx context.Context, id string) error
- func (s *Store) SearchMemory(ctx context.Context, q store.MemoryQuery) ([]store.Memory, error)
- func (s *Store) SpendByPerson(ctx context.Context, from, to time.Time) (map[string]float64, error)
- func (s *Store) SpendByRepo(ctx context.Context, from, to time.Time) (map[string]float64, error)
- func (s *Store) SpendBySkill(ctx context.Context, from, to time.Time) (map[string]float64, error)
- func (s *Store) SpendSince(ctx context.Context, t time.Time) (float64, error)
- func (s *Store) TryLead(ctx context.Context, name string) (func(), bool, error)
- func (s *Store) TryUpdateCampaign(ctx context.Context, id string, fn func(*store.Campaign) error) (*store.Campaign, error)
- func (s *Store) UndeliveredMessages(ctx context.Context, sessionID string) ([]store.SessionMessage, error)
- func (s *Store) UpdateCampaign(ctx context.Context, id string, fn func(*store.Campaign) error) (*store.Campaign, error)
- func (s *Store) UpdatePullRequest(ctx context.Context, pr *store.PullRequest) error
- func (s *Store) UpdateSession(ctx context.Context, ses *store.Session) error
- func (s *Store) UpdateStep(ctx context.Context, st *store.Step) error
- func (s *Store) UpsertUser(ctx context.Context, u store.User) error
- func (s *Store) WatchConfig(ctx context.Context) (<-chan store.ConfigChange, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// URL is a Postgres connection string.
URL string
// MaxConns is the pool's size: pgx's default (or the URL's
// pool_max_conns) when 0. It must be at least 2.
MaxConns int
Logger *slog.Logger
}
Options configure Open.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a store.Store on Postgres.
func (*Store) ActivateRevision ¶
ActivateRevision implements store.Store.
func (*Store) ActiveRevision ¶
ActiveRevision implements store.Store.
func (*Store) ActiveRevisions ¶
ActiveRevisions implements store.Store.
func (*Store) AddSessionMessage ¶
AddSessionMessage implements store.Store. The session's row is locked while its state is checked and the message written, so the message cannot land after the worker wrote the session's last status, and two messages cannot take the same Seq.
func (*Store) AppendAudit ¶
AppendAudit implements store.Store.
func (*Store) AppendStep ¶
AppendStep implements store.Store.
func (*Store) Claim ¶
func (s *Store) Claim(ctx context.Context, worker string, lease time.Duration) (*store.Session, error)
Claim implements store.Store.
func (*Store) ClaimWithin ¶ added in v0.3.0
func (s *Store) ClaimWithin(ctx context.Context, worker string, lease time.Duration, caps store.Caps) (*store.Session, error)
ClaimWithin implements store.Store.
Without caps it is Claim. With caps, claims take turns: each one holds a transaction-scoped advisory lock while it counts the running sessions and marks the one it picks running, and commits before the next one counts. Under READ COMMITTED two claims that did not take turns would each count without seeing the other's uncommitted session, and both start one for a repository with one slot left. Running sessions are counted by repository, trigger and person, so a person's cap holds across every repository. The lock is per schema, so it covers every worker of every replica of one install. A claim holds it for one count and one update; FOR UPDATE SKIP LOCKED still keeps a claim from waiting on a session another statement holds.
func (*Store) CreateCampaign ¶ added in v0.4.0
CreateCampaign implements store.Store.
func (*Store) DecideApproval ¶
DecideApproval implements store.Store.
func (*Store) DeleteDraft ¶
DeleteDraft implements store.Store.
func (*Store) DeleteMemory ¶
DeleteMemory implements store.Store.
func (*Store) GetApproval ¶
GetApproval implements store.Store.
func (*Store) GetCampaign ¶ added in v0.4.0
GetCampaign implements store.Store.
func (*Store) GetEvalRun ¶
GetEvalRun implements store.Store.
func (*Store) GetProposal ¶
GetProposal implements store.Store.
func (*Store) GetPullRequest ¶
func (s *Store) GetPullRequest(ctx context.Context, repo string, number int) (*store.PullRequest, error)
GetPullRequest implements store.Store.
func (*Store) GetRevision ¶
GetRevision implements store.Store.
func (*Store) GetScheduleState ¶
GetScheduleState implements store.Store.
func (*Store) GetSession ¶
GetSession implements store.Store.
func (*Store) ListAudit ¶
func (s *Store) ListAudit(ctx context.Context, f store.AuditFilter) ([]store.AuditEntry, error)
ListAudit implements store.Store.
func (*Store) ListCampaigns ¶ added in v0.4.0
func (s *Store) ListCampaigns(ctx context.Context, f store.CampaignFilter) ([]store.Campaign, error)
ListCampaigns implements store.Store.
func (*Store) ListDrafts ¶
ListDrafts implements store.Store.
func (*Store) ListEvalRuns ¶
ListEvalRuns implements store.Store.
func (*Store) ListMemory ¶
ListMemory implements store.Store.
func (*Store) ListProposals ¶
func (s *Store) ListProposals(ctx context.Context, f store.ProposalFilter) ([]store.Proposal, error)
ListProposals implements store.Store.
func (*Store) ListPullRequests ¶
func (s *Store) ListPullRequests(ctx context.Context, f store.PullRequestFilter) ([]store.PullRequest, error)
ListPullRequests implements store.Store.
func (*Store) ListRevisions ¶
func (s *Store) ListRevisions(ctx context.Context, scope, object string, limit int) ([]store.Revision, error)
ListRevisions implements store.Store.
func (*Store) ListSessionMessages ¶
func (s *Store) ListSessionMessages(ctx context.Context, sessionID string) ([]store.SessionMessage, error)
ListSessionMessages implements store.Store.
func (*Store) ListSessions ¶
ListSessions implements store.Store.
func (*Store) ListSkillIndex ¶
ListSkillIndex implements store.Store.
func (*Store) MarkSessionMessages ¶
func (s *Store) MarkSessionMessages(ctx context.Context, sessionID string, ids []string, status, to string) ([]store.SessionMessage, error)
MarkSessionMessages implements store.Store. The update only matches pending rows, so of two concurrent calls each message goes to one.
func (*Store) Migrate ¶
Migrate applies every migration that has not run yet and returns how many it applied. Replicas starting together serialize on an advisory lock.
func (*Store) PendingMigrations ¶
PendingMigrations returns how many migrations have not been applied.
func (*Store) PutApproval ¶
PutApproval implements store.Store.
func (*Store) PutEvalRun ¶
PutEvalRun implements store.Store.
func (*Store) PutProposal ¶
PutProposal implements store.Store. The partial unique index skill_proposals_open is what keeps one open proposal per skill and scope.
func (*Store) PutRevision ¶
func (s *Store) PutRevision(ctx context.Context, r store.Revision, activate bool) (*store.Revision, error)
PutRevision implements store.Store.
func (*Store) PutScheduleState ¶
PutScheduleState implements store.Store.
func (*Store) RecordPullRequest ¶
RecordPullRequest implements store.Store.
func (*Store) Release ¶
func (s *Store) Release(ctx context.Context, sessionID, worker string, status store.SessionStatus, runAfter time.Time) error
Release implements store.Store.
func (*Store) ReplaceSkillIndex ¶
func (s *Store) ReplaceSkillIndex(ctx context.Context, repo string, entries []store.SkillIndexEntry) error
ReplaceSkillIndex implements store.Store.
func (*Store) RequestCancel ¶
RequestCancel implements store.Store.
func (*Store) SearchMemory ¶
SearchMemory implements store.Store.
func (*Store) SpendByPerson ¶ added in v0.5.0
SpendByPerson implements store.Store.
func (*Store) SpendByRepo ¶ added in v0.3.0
SpendByRepo implements store.Store.
func (*Store) SpendBySkill ¶ added in v0.5.0
SpendBySkill implements store.Store. A step's name is its call's purpose; split_part takes the skill out of subagent:, judge: and plan:<skill>.
func (*Store) SpendSince ¶
SpendSince implements store.Store.
func (*Store) TryLead ¶
TryLead implements store.Store with a session-level advisory lock held on a dedicated connection. If that connection dies Postgres frees the lock, so two replicas never lead at once for longer than a failed tick.
func (*Store) TryUpdateCampaign ¶ added in v0.4.0
func (s *Store) TryUpdateCampaign(ctx context.Context, id string, fn func(*store.Campaign) error) (*store.Campaign, error)
TryUpdateCampaign implements store.Store: UpdateCampaign with SKIP LOCKED on the row.
func (*Store) UndeliveredMessages ¶
func (s *Store) UndeliveredMessages(ctx context.Context, sessionID string) ([]store.SessionMessage, error)
UndeliveredMessages implements store.Store.
func (*Store) UpdateCampaign ¶ added in v0.4.0
func (s *Store) UpdateCampaign(ctx context.Context, id string, fn func(*store.Campaign) error) (*store.Campaign, error)
UpdateCampaign implements store.Store. The campaign's row stays locked (SELECT … FOR UPDATE) from the read to the commit, so an update on another replica waits for this one and reads what it wrote.
fn's own store calls take other connections of the pool, while the update holds one for its transaction. Two things keep the pool from running dry: an update of this process waits for another of the same campaign before it takes a connection (campaignLocks), so at most one per campaign holds a connection while waiting for the row lock; and campaign updates hold at most one connection fewer than the pool's size at once (campaignSlots), so fn always gets one in the end. That is why fn must not update a campaign: it would take a second slot, and with a pool of 2 there is only one, so it would wait until ctx ends.
func (*Store) UpdatePullRequest ¶
UpdatePullRequest implements store.Store.
func (*Store) UpdateSession ¶
UpdateSession implements store.Store.
func (*Store) UpdateStep ¶
UpdateStep implements store.Store.
func (*Store) UpsertUser ¶
UpsertUser implements store.Store.
func (*Store) WatchConfig ¶
WatchConfig implements store.Store. It holds one connection on LISTEN. When that connection drops it reconnects and sends a change with Scope "*", which tells the receiver to reload everything it may have missed.