Merge branch 'run/pg-w2-2' into worktree-giggly-marinating-lamport
This commit is contained in:
commit
dfed51547c
17 files changed
+2424
-29
No files matched your search
@@ -370,4 +370,42 @@ var (
|
||||
ErrAlreadyDispatched = errors.New("store: binding already dispatched for capture")
|
||||
// ErrMappingNotFound is returned when a mapping ID does not exist.
|
||||
ErrMappingNotFound = errors.New("store: mapping not found")
|
||||
|
||||
// ErrResourceNotFound is returned when a control-plane resource kind has
|
||||
// no stored document. It is in-process only (not on the gRPC wire): the
|
||||
// control plane runs in the daemon against the local store.
|
||||
ErrResourceNotFound = errors.New("resource not found")
|
||||
// ErrResourceVersionConflict is returned when a resource write carries an
|
||||
// expected version that does not match the stored revision.
|
||||
ErrResourceVersionConflict = errors.New("resource version conflict")
|
||||
)
|
||||
|
||||
// Resource is one control-plane resource document: the stored value for a
|
||||
// kind, its revision, and the audit record written with the last write.
|
||||
// Resource and ResourceWrite live here (rather than internal/store) so the
|
||||
// PostgreSQL implementation and the UI-side consumers can reference them
|
||||
// without linking the SQLite implementation.
|
||||
type Resource struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
Actor string
|
||||
Source string
|
||||
RequestID string
|
||||
ExpectedVersion int64
|
||||
CurrentVersion int64
|
||||
At time.Time
|
||||
}
|
||||
|
||||
// ResourceWrite is a control-plane resource write request. ExpectedVersion is
|
||||
// the optimistic-concurrency guard: a write whose expectation does not match
|
||||
// the stored revision is refused with ErrResourceVersionConflict.
|
||||
type ResourceWrite struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
Actor string
|
||||
Source string
|
||||
RequestID string
|
||||
ExpectedVersion int64
|
||||
At time.Time
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// clip limits s to n bytes without splitting a UTF-8 rune. It is the exact
|
||||
// write-side normalisation the SQLite store applied before inserting event
|
||||
// detail, transition detail, park reasons and review payloads: a generated
|
||||
// query that drops it lets an oversized value through where the old store
|
||||
// truncated it.
|
||||
func clip(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
pos := 0
|
||||
for pos < len(s) {
|
||||
_, size := utf8.DecodeRuneInString(s[pos:])
|
||||
if pos+size > n {
|
||||
break
|
||||
}
|
||||
pos += size
|
||||
}
|
||||
return s[:pos]
|
||||
}
|
||||
|
||||
// isUniqueViolation reports whether err is a PostgreSQL unique-constraint
|
||||
// violation (SQLSTATE 23505), matching the SQLite store's "UNIQUE constraint
|
||||
// failed" string test rather than grepping driver error text.
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == "23505"
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// insertEventQ writes one event through q (a pool- or tx-scoped Queries value)
|
||||
// and returns its row id. It reproduces the SQLite insertEvent's write-side
|
||||
// semantics: a zero At becomes now-UTC, an unmarshallable Data becomes the
|
||||
// marshal_error envelope, and Detail is clipped to 4000 bytes.
|
||||
func insertEventQ(ctx context.Context, q *postgresdb.Queries, e events.Event) (int64, error) {
|
||||
if e.At.IsZero() {
|
||||
e.At = time.Now().UTC()
|
||||
}
|
||||
data, err := json.Marshal(e.Data)
|
||||
if err != nil {
|
||||
data = fmt.Appendf(nil, `{"marshal_error":%q}`, err.Error())
|
||||
}
|
||||
return q.InsertEvent(ctx, postgresdb.InsertEventParams{
|
||||
At: e.At.UTC(), Kind: e.Kind, TaskID: e.TaskID, Repo: e.Repo,
|
||||
Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage,
|
||||
Attempt: int64(e.Attempt), ActorID: e.ActorID, ActorKind: e.ActorKind,
|
||||
PrincipalID: e.PrincipalID, Detail: clip(e.Detail, 4000), Data: string(data),
|
||||
})
|
||||
}
|
||||
|
||||
// InsertEvent appends an event to the log and returns its row id.
|
||||
func (s *Store) InsertEvent(ctx context.Context, e events.Event) (int64, error) {
|
||||
return insertEventQ(ctx, s.queries(), e)
|
||||
}
|
||||
|
||||
// eventFromRow maps the generated event row to the domain event, decoding the
|
||||
// stored JSON data document back into a map.
|
||||
func eventFromRow(e postgresdb.Event) events.Event {
|
||||
var data map[string]any
|
||||
_ = json.Unmarshal([]byte(e.Data), &data)
|
||||
return events.Event{
|
||||
ID: e.ID, At: e.At.UTC(), Kind: e.Kind, TaskID: e.TaskID,
|
||||
Repo: e.Repo, Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage,
|
||||
Attempt: int(e.Attempt), ActorID: e.ActorID, ActorKind: e.ActorKind,
|
||||
PrincipalID: e.PrincipalID, Detail: e.Detail, Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// EventsSince returns up to limit events after the opaque cursor, oldest
|
||||
// first. The cursor carries the total order (at, id): an empty or malformed
|
||||
// cursor means "from the beginning".
|
||||
func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) {
|
||||
at, id, ok := storecontract.ParseEventCursor(cursor)
|
||||
if !ok {
|
||||
return s.listEventsFromBeginning(ctx, limit)
|
||||
}
|
||||
atTime, err := time.Parse(storecontract.EventCursorLayout, at)
|
||||
if err != nil {
|
||||
// A malformed sort key means "from the beginning".
|
||||
return s.listEventsFromBeginning(ctx, limit)
|
||||
}
|
||||
rows, err := s.queries().ListEventsAfter(ctx, postgresdb.ListEventsAfterParams{
|
||||
At: atTime, ID: id, Limit: int32(limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eventsFromRows(rows), nil
|
||||
}
|
||||
|
||||
func (s *Store) listEventsFromBeginning(ctx context.Context, limit int) ([]events.Event, error) {
|
||||
rows, err := s.queries().ListEventsFromBeginning(ctx, int32(limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eventsFromRows(rows), nil
|
||||
}
|
||||
|
||||
func eventsFromRows(rows []postgresdb.Event) []events.Event {
|
||||
out := make([]events.Event, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, eventFromRow(r))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TaskEvents returns a task's full timeline, oldest first.
|
||||
func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
||||
rows, err := s.queries().TaskEventsByID(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eventsFromRows(rows), nil
|
||||
}
|
||||
|
||||
// WorkflowStats aggregates outcomes and spend per workflow.
|
||||
func (s *Store) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
||||
rows, err := s.queries().WorkflowStats(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats := make([]storecontract.WorkflowStat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
stats = append(stats, storecontract.WorkflowStat{
|
||||
Workflow: r.Workflow,
|
||||
Runs: int(r.Runs),
|
||||
Merged: int(r.Merged),
|
||||
PROpen: int(r.PrOpen),
|
||||
Parked: int(r.Parked),
|
||||
AvgTokens: int(r.AvgTokens),
|
||||
AvgSteps: r.AvgSteps,
|
||||
TotalToken: int(r.TotalTokens),
|
||||
})
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// StageStats aggregates stage_finish events -- where time goes, and which
|
||||
// stages fail.
|
||||
func (s *Store) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
||||
rows, err := s.queries().StageStats(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats := make([]storecontract.StageStat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
stats = append(stats, storecontract.StageStat{
|
||||
Workflow: r.Workflow, Stage: r.Stage, Runs: int(r.Runs),
|
||||
AvgMs: int(r.AvgMs), Errors: int(r.Errors),
|
||||
})
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// TokensByDay sums task token spend per UTC day, using agent_finish events as
|
||||
// the per-run accounting source and tasks.tokens_used as the durable fallback.
|
||||
func (s *Store) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
||||
rows, err := s.queries().TokensByDay(ctx, int32(days))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens := make([]storecontract.DayTokens, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
tokens = append(tokens, storecontract.DayTokens{Day: r.Day, Tokens: int(r.Tokens)})
|
||||
}
|
||||
return tokens, nil
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// identityFromRow maps the generated identity row to the domain identity.
|
||||
func identityFromRow(i postgresdb.Identity) identity.Identity {
|
||||
return identity.Identity{
|
||||
ID: identity.IdentityID(i.ID), Kind: identity.Kind(i.Kind),
|
||||
DisplayName: i.DisplayName, Lifecycle: identity.Lifecycle(i.Lifecycle),
|
||||
Version: i.Version, CreatedAt: i.CreatedAt, UpdatedAt: i.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// insertIdentityEvent writes one identity audit event through q, refusing an
|
||||
// audit that cannot answer who acted and whose authority they used.
|
||||
func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, id identity.IdentityID, ev identity.Event, audit identity.Audit, at time.Time) error {
|
||||
if audit.ActorID == "" || audit.Source == "" || audit.RequestID == "" {
|
||||
return fmt.Errorf("%w: audit actor, source, and request ID are required", identity.ErrInvalid)
|
||||
}
|
||||
return q.InsertIdentityEvent(ctx, postgresdb.InsertIdentityEventParams{
|
||||
IdentityID: string(id), EventType: string(ev.Type),
|
||||
FromLifecycle: string(ev.From), ToLifecycle: string(ev.To),
|
||||
DisplayName: ev.DisplayName, ActorID: string(audit.ActorID),
|
||||
Source: audit.Source, RequestID: audit.RequestID, At: at,
|
||||
})
|
||||
}
|
||||
|
||||
// List returns every identity, oldest first.
|
||||
func (s *Store) List(ctx context.Context) ([]identity.Identity, error) {
|
||||
rows, err := s.queries().ListIdentities(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]identity.Identity, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, identityFromRow(r))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Get returns one identity by id, or ErrNotFound.
|
||||
func (s *Store) Get(ctx context.Context, id identity.IdentityID) (identity.Identity, error) {
|
||||
i, err := s.queries().GetIdentity(ctx, string(id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return identityFromRow(i), nil
|
||||
}
|
||||
|
||||
// ResolveLegacyName resolves a legacy display name through the aliases table.
|
||||
// Aliases match case-insensitively, mirroring the SQLite schema's COLLATE
|
||||
// NOCASE.
|
||||
func (s *Store) ResolveLegacyName(ctx context.Context, name string) (identity.Identity, error) {
|
||||
i, err := s.queries().ResolveIdentityAlias(ctx, strings.TrimSpace(name))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return identityFromRow(i), nil
|
||||
}
|
||||
|
||||
// Create inserts a new identity, its display-name alias and its create audit
|
||||
// event in one transaction.
|
||||
func (s *Store) Create(ctx context.Context, value identity.Identity, audit identity.Audit) (identity.Identity, error) {
|
||||
if err := value.Validate(); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
now := audit.At.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
value.Version, value.CreatedAt, value.UpdatedAt = 1, now, now
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
if err := q.InsertIdentity(ctx, postgresdb.InsertIdentityParams{
|
||||
ID: string(value.ID), Kind: string(value.Kind), DisplayName: value.DisplayName,
|
||||
Lifecycle: string(value.Lifecycle), Version: value.Version, CreatedAt: now, UpdatedAt: now,
|
||||
}); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if err := q.InsertIdentityAlias(ctx, postgresdb.InsertIdentityAliasParams{
|
||||
Alias: value.DisplayName, IdentityID: string(value.ID),
|
||||
}); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if err := insertIdentityEvent(ctx, q, value.ID, identity.Event{
|
||||
IdentityID: value.ID, Type: "create", To: value.Lifecycle, DisplayName: value.DisplayName,
|
||||
}, audit, now); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// Apply mutates an identity under an optimistic version guard and records the
|
||||
// lifecycle event.
|
||||
func (s *Store) Apply(ctx context.Context, id identity.IdentityID, expectedVersion int64, command identity.Command, audit identity.Audit) (identity.Identity, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
current, err := q.GetIdentity(ctx, string(id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
cur := identityFromRow(current)
|
||||
if cur.Version != expectedVersion {
|
||||
return identity.Identity{}, identity.ErrConflict
|
||||
}
|
||||
next, ev, err := identity.Apply(cur, command)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
now := audit.At.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
next.UpdatedAt = now
|
||||
|
||||
n, err := q.UpdateIdentity(ctx, postgresdb.UpdateIdentityParams{
|
||||
ID: string(id), DisplayName: next.DisplayName, Lifecycle: string(next.Lifecycle),
|
||||
Version: next.Version, UpdatedAt: now, Version_2: expectedVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if n != 1 {
|
||||
return identity.Identity{}, identity.ErrConflict
|
||||
}
|
||||
if command.Type == identity.CommandRename {
|
||||
if err := q.InsertIdentityAliasIgnoreConflict(ctx, postgresdb.InsertIdentityAliasIgnoreConflictParams{
|
||||
Alias: next.DisplayName, IdentityID: string(id),
|
||||
}); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
}
|
||||
if err := insertIdentityEvent(ctx, q, id, ev, audit, now); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// ResolveSubject returns the identity bound to a provider subject.
|
||||
func (s *Store) ResolveSubject(ctx context.Context, subject identity.Subject) (identity.Identity, error) {
|
||||
i, err := s.queries().ResolveIdentitySubject(ctx, postgresdb.ResolveIdentitySubjectParams{
|
||||
Issuer: subject.Issuer, Subject: subject.Subject,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return identityFromRow(i), nil
|
||||
}
|
||||
|
||||
// BindSubject binds an identity to the provider subject that asserts it,
|
||||
// moving any prior binding for that subject.
|
||||
func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
|
||||
if err := subject.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if audit.ActorID == "" || audit.Source == "" || audit.RequestID == "" {
|
||||
return fmt.Errorf("%w: audit actor, source, and request ID are required", identity.ErrInvalid)
|
||||
}
|
||||
now := audit.At.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
current, err := q.GetIdentity(ctx, string(id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cur := identityFromRow(current)
|
||||
|
||||
if err := q.UpsertIdentitySubject(ctx, postgresdb.UpsertIdentitySubjectParams{
|
||||
Issuer: subject.Issuer, Subject: subject.Subject, IdentityID: string(id), BoundAt: now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertIdentityEvent(ctx, q, id, identity.Event{
|
||||
IdentityID: id, Type: "bind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
|
||||
}, audit, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// BootstrapIdentities seeds the system identity and the configured legacy
|
||||
// names idempotently, migrating any forge tasks that still carry a legacy
|
||||
// name to the stable id.
|
||||
func (s *Store) BootstrapIdentities(ctx context.Context, legacyNames []string) error {
|
||||
values := append([]struct {
|
||||
id identity.IdentityID
|
||||
kind identity.Kind
|
||||
name string
|
||||
}{{identity.SystemID, identity.KindSystem, "System"}}, make([]struct {
|
||||
id identity.IdentityID
|
||||
kind identity.Kind
|
||||
name string
|
||||
}, len(legacyNames))...)
|
||||
for i, name := range legacyNames {
|
||||
name = strings.TrimSpace(name)
|
||||
values[i+1] = struct {
|
||||
id identity.IdentityID
|
||||
kind identity.Kind
|
||||
name string
|
||||
}{identity.StableID(name), identity.KindBot, name}
|
||||
}
|
||||
for _, seed := range values {
|
||||
if seed.name == "" {
|
||||
continue
|
||||
}
|
||||
value, err := identity.New(seed.id, seed.kind, seed.name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.Create(ctx, value, identity.Audit{
|
||||
ActorID: identity.SystemID, Source: "legacy-config", RequestID: "identity-import:" + string(seed.id),
|
||||
})
|
||||
if err != nil && !isUniqueViolation(err) {
|
||||
return fmt.Errorf("bootstrap identity %q: %w", seed.name, err)
|
||||
}
|
||||
if err := s.queries().InsertIdentityAliasIgnoreConflict(ctx, postgresdb.InsertIdentityAliasIgnoreConflictParams{
|
||||
Alias: seed.name, IdentityID: string(seed.id),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if seed.kind != identity.KindSystem {
|
||||
if err := s.queries().UpdateTaskIdentity(ctx, postgresdb.UpdateTaskIdentityParams{
|
||||
Identity: string(seed.id), Identity_2: seed.name,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
-- review_cursor is the poll backstop's high-water mark over forge review IDs,
|
||||
-- kept separate from watch_comment_id (the review-comment cursor) because the
|
||||
-- two forge ID sequences are independent. 0001 omitted the column, so a dev
|
||||
-- database that already applied 0001 needs this follow-up rather than a
|
||||
-- rewrite of the original migration: goose tracks version numbers, not
|
||||
-- checksums.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE tasks ADD COLUMN review_cursor bigint NOT NULL DEFAULT 0;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE tasks DROP COLUMN review_cursor;
|
||||
@@ -0,0 +1,339 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: events.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const insertEvent = `-- name: InsertEvent :one
|
||||
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type InsertEventParams struct {
|
||||
At time.Time
|
||||
Kind string
|
||||
TaskID int64
|
||||
Repo string
|
||||
Issue int64
|
||||
Workflow string
|
||||
Stage string
|
||||
Attempt int64
|
||||
ActorID string
|
||||
ActorKind string
|
||||
PrincipalID string
|
||||
Detail string
|
||||
Data string
|
||||
}
|
||||
|
||||
// Event-log queries. The (at, id) cursor is a wire contract: at is the
|
||||
// fixed-width sort key and id only breaks ties, so a reader pages by
|
||||
// "after (at, id)" and never skips or repeats a row. See
|
||||
// internal/domain/storecontract/cursor.go.
|
||||
func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, insertEvent,
|
||||
arg.At,
|
||||
arg.Kind,
|
||||
arg.TaskID,
|
||||
arg.Repo,
|
||||
arg.Issue,
|
||||
arg.Workflow,
|
||||
arg.Stage,
|
||||
arg.Attempt,
|
||||
arg.ActorID,
|
||||
arg.ActorKind,
|
||||
arg.PrincipalID,
|
||||
arg.Detail,
|
||||
arg.Data,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const listEventsAfter = `-- name: ListEventsAfter :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
WHERE at > $1 OR (at = $1 AND id > $2)
|
||||
ORDER BY at, id
|
||||
LIMIT $3
|
||||
`
|
||||
|
||||
type ListEventsAfterParams struct {
|
||||
At time.Time
|
||||
ID int64
|
||||
Limit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsAfter, arg.At, arg.ID, arg.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Event
|
||||
for rows.Next() {
|
||||
var i Event
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.At,
|
||||
&i.Kind,
|
||||
&i.TaskID,
|
||||
&i.Repo,
|
||||
&i.Issue,
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Attempt,
|
||||
&i.ActorID,
|
||||
&i.ActorKind,
|
||||
&i.PrincipalID,
|
||||
&i.Detail,
|
||||
&i.Data,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listEventsFromBeginning = `-- name: ListEventsFromBeginning :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
ORDER BY at, id
|
||||
LIMIT $1
|
||||
`
|
||||
|
||||
func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsFromBeginning, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Event
|
||||
for rows.Next() {
|
||||
var i Event
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.At,
|
||||
&i.Kind,
|
||||
&i.TaskID,
|
||||
&i.Repo,
|
||||
&i.Issue,
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Attempt,
|
||||
&i.ActorID,
|
||||
&i.ActorKind,
|
||||
&i.PrincipalID,
|
||||
&i.Detail,
|
||||
&i.Data,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const stageStats = `-- name: StageStats :many
|
||||
SELECT workflow, stage,
|
||||
COUNT(*)::int AS runs,
|
||||
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
|
||||
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
|
||||
FROM events
|
||||
WHERE kind = 'stage_finish'
|
||||
GROUP BY workflow, stage
|
||||
ORDER BY workflow, stage
|
||||
`
|
||||
|
||||
type StageStatsRow struct {
|
||||
Workflow string
|
||||
Stage string
|
||||
Runs int32
|
||||
AvgMs int64
|
||||
Errors int32
|
||||
}
|
||||
|
||||
func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, stageStats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []StageStatsRow
|
||||
for rows.Next() {
|
||||
var i StageStatsRow
|
||||
if err := rows.Scan(
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Runs,
|
||||
&i.AvgMs,
|
||||
&i.Errors,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const taskEventsByID = `-- name: TaskEventsByID :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
WHERE task_id = $1
|
||||
ORDER BY id
|
||||
`
|
||||
|
||||
func (q *Queries) TaskEventsByID(ctx context.Context, taskID int64) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, taskEventsByID, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Event
|
||||
for rows.Next() {
|
||||
var i Event
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.At,
|
||||
&i.Kind,
|
||||
&i.TaskID,
|
||||
&i.Repo,
|
||||
&i.Issue,
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Attempt,
|
||||
&i.ActorID,
|
||||
&i.ActorKind,
|
||||
&i.PrincipalID,
|
||||
&i.Detail,
|
||||
&i.Data,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const tokensByDay = `-- name: TokensByDay :many
|
||||
WITH event_totals AS (
|
||||
SELECT task_id, to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
|
||||
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
|
||||
FROM events WHERE kind = 'agent_finish'
|
||||
GROUP BY task_id, day
|
||||
), usage AS (
|
||||
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
||||
UNION ALL
|
||||
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
|
||||
FROM tasks t
|
||||
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
||||
SELECT 1 FROM event_totals e
|
||||
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
||||
)
|
||||
)
|
||||
SELECT day, SUM(tokens)::bigint AS tokens
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1
|
||||
`
|
||||
|
||||
type TokensByDayRow struct {
|
||||
Day string
|
||||
Tokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) TokensByDay(ctx context.Context, limit int32) ([]TokensByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, tokensByDay, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []TokensByDayRow
|
||||
for rows.Next() {
|
||||
var i TokensByDayRow
|
||||
if err := rows.Scan(&i.Day, &i.Tokens); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const workflowStats = `-- name: WorkflowStats :many
|
||||
SELECT workflow,
|
||||
COUNT(*)::int AS runs,
|
||||
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
|
||||
COUNT(*) FILTER (WHERE status = 'pr_open')::int AS pr_open,
|
||||
COUNT(*) FILTER (WHERE status = 'parked')::int AS parked,
|
||||
CAST(AVG(tokens_used) AS bigint) AS avg_tokens,
|
||||
AVG(iterations)::float8 AS avg_steps,
|
||||
SUM(tokens_used)::bigint AS total_tokens
|
||||
FROM tasks
|
||||
WHERE workflow <> ''
|
||||
GROUP BY workflow
|
||||
ORDER BY COUNT(*) DESC
|
||||
`
|
||||
|
||||
type WorkflowStatsRow struct {
|
||||
Workflow string
|
||||
Runs int32
|
||||
Merged int32
|
||||
PrOpen int32
|
||||
Parked int32
|
||||
AvgTokens int64
|
||||
AvgSteps float64
|
||||
TotalTokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) WorkflowStats(ctx context.Context) ([]WorkflowStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, workflowStats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []WorkflowStatsRow
|
||||
for rows.Next() {
|
||||
var i WorkflowStatsRow
|
||||
if err := rows.Scan(
|
||||
&i.Workflow,
|
||||
&i.Runs,
|
||||
&i.Merged,
|
||||
&i.PrOpen,
|
||||
&i.Parked,
|
||||
&i.AvgTokens,
|
||||
&i.AvgSteps,
|
||||
&i.TotalTokens,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: identities.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const getIdentity = `-- name: GetIdentity :one
|
||||
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
||||
FROM identities WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetIdentity(ctx context.Context, id string) (Identity, error) {
|
||||
row := q.db.QueryRow(ctx, getIdentity, id)
|
||||
var i Identity
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.DisplayName,
|
||||
&i.Lifecycle,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertIdentity = `-- name: InsertIdentity :exec
|
||||
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
`
|
||||
|
||||
type InsertIdentityParams struct {
|
||||
ID string
|
||||
Kind string
|
||||
DisplayName string
|
||||
Lifecycle string
|
||||
Version int64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) InsertIdentity(ctx context.Context, arg InsertIdentityParams) error {
|
||||
_, err := q.db.Exec(ctx, insertIdentity,
|
||||
arg.ID,
|
||||
arg.Kind,
|
||||
arg.DisplayName,
|
||||
arg.Lifecycle,
|
||||
arg.Version,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertIdentityAlias = `-- name: InsertIdentityAlias :exec
|
||||
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type InsertIdentityAliasParams struct {
|
||||
Alias string
|
||||
IdentityID string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertIdentityAlias(ctx context.Context, arg InsertIdentityAliasParams) error {
|
||||
_, err := q.db.Exec(ctx, insertIdentityAlias, arg.Alias, arg.IdentityID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertIdentityAliasIgnoreConflict = `-- name: InsertIdentityAliasIgnoreConflict :exec
|
||||
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
type InsertIdentityAliasIgnoreConflictParams struct {
|
||||
Alias string
|
||||
IdentityID string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertIdentityAliasIgnoreConflict(ctx context.Context, arg InsertIdentityAliasIgnoreConflictParams) error {
|
||||
_, err := q.db.Exec(ctx, insertIdentityAliasIgnoreConflict, arg.Alias, arg.IdentityID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertIdentityEvent = `-- name: InsertIdentityEvent :exec
|
||||
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
`
|
||||
|
||||
type InsertIdentityEventParams struct {
|
||||
IdentityID string
|
||||
EventType string
|
||||
FromLifecycle string
|
||||
ToLifecycle string
|
||||
DisplayName string
|
||||
ActorID string
|
||||
Source string
|
||||
RequestID string
|
||||
At time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEventParams) error {
|
||||
_, err := q.db.Exec(ctx, insertIdentityEvent,
|
||||
arg.IdentityID,
|
||||
arg.EventType,
|
||||
arg.FromLifecycle,
|
||||
arg.ToLifecycle,
|
||||
arg.DisplayName,
|
||||
arg.ActorID,
|
||||
arg.Source,
|
||||
arg.RequestID,
|
||||
arg.At,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listIdentities = `-- name: ListIdentities :many
|
||||
|
||||
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
||||
FROM identities ORDER BY created_at, id
|
||||
`
|
||||
|
||||
// Identity persistence. The domain owns lifecycle semantics (internal/domain/
|
||||
// identity); these queries are the durable half of that contract. Aliases are
|
||||
// unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
|
||||
// the SQLite schema's COLLATE NOCASE.
|
||||
func (q *Queries) ListIdentities(ctx context.Context) ([]Identity, error) {
|
||||
rows, err := q.db.Query(ctx, listIdentities)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Identity
|
||||
for rows.Next() {
|
||||
var i Identity
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.DisplayName,
|
||||
&i.Lifecycle,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const resolveIdentityAlias = `-- name: ResolveIdentityAlias :one
|
||||
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
||||
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
|
||||
WHERE lower(a.alias) = lower($1)
|
||||
`
|
||||
|
||||
func (q *Queries) ResolveIdentityAlias(ctx context.Context, lower string) (Identity, error) {
|
||||
row := q.db.QueryRow(ctx, resolveIdentityAlias, lower)
|
||||
var i Identity
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.DisplayName,
|
||||
&i.Lifecycle,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const resolveIdentitySubject = `-- name: ResolveIdentitySubject :one
|
||||
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
||||
FROM identities i JOIN identity_subjects b ON b.identity_id = i.id
|
||||
WHERE b.issuer = $1 AND b.subject = $2
|
||||
`
|
||||
|
||||
type ResolveIdentitySubjectParams struct {
|
||||
Issuer string
|
||||
Subject string
|
||||
}
|
||||
|
||||
func (q *Queries) ResolveIdentitySubject(ctx context.Context, arg ResolveIdentitySubjectParams) (Identity, error) {
|
||||
row := q.db.QueryRow(ctx, resolveIdentitySubject, arg.Issuer, arg.Subject)
|
||||
var i Identity
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.DisplayName,
|
||||
&i.Lifecycle,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateIdentity = `-- name: UpdateIdentity :execrows
|
||||
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
|
||||
WHERE id = $1 AND version = $6
|
||||
`
|
||||
|
||||
type UpdateIdentityParams struct {
|
||||
ID string
|
||||
DisplayName string
|
||||
Lifecycle string
|
||||
Version int64
|
||||
UpdatedAt time.Time
|
||||
Version_2 int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateIdentity(ctx context.Context, arg UpdateIdentityParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateIdentity,
|
||||
arg.ID,
|
||||
arg.DisplayName,
|
||||
arg.Lifecycle,
|
||||
arg.Version,
|
||||
arg.UpdatedAt,
|
||||
arg.Version_2,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateTaskIdentity = `-- name: UpdateTaskIdentity :exec
|
||||
UPDATE tasks SET identity = $1 WHERE identity = $2
|
||||
`
|
||||
|
||||
type UpdateTaskIdentityParams struct {
|
||||
Identity string
|
||||
Identity_2 string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateTaskIdentity(ctx context.Context, arg UpdateTaskIdentityParams) error {
|
||||
_, err := q.db.Exec(ctx, updateTaskIdentity, arg.Identity, arg.Identity_2)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertIdentitySubject = `-- name: UpsertIdentitySubject :exec
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at
|
||||
`
|
||||
|
||||
type UpsertIdentitySubjectParams struct {
|
||||
Issuer string
|
||||
Subject string
|
||||
IdentityID string
|
||||
BoundAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertIdentitySubject(ctx context.Context, arg UpsertIdentitySubjectParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertIdentitySubject,
|
||||
arg.Issuer,
|
||||
arg.Subject,
|
||||
arg.IdentityID,
|
||||
arg.BoundAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -138,6 +138,7 @@ type Task struct {
|
||||
WorkflowDefinitionYaml string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ReviewCursor int64
|
||||
}
|
||||
|
||||
type Transition struct {
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: status.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const configSnapshotByID = `-- name: ConfigSnapshotByID :one
|
||||
SELECT schema, document, published_at FROM config_snapshot WHERE id = 1
|
||||
`
|
||||
|
||||
type ConfigSnapshotByIDRow struct {
|
||||
Schema string
|
||||
Document string
|
||||
PublishedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ConfigSnapshotByID(ctx context.Context) (ConfigSnapshotByIDRow, error) {
|
||||
row := q.db.QueryRow(ctx, configSnapshotByID)
|
||||
var i ConfigSnapshotByIDRow
|
||||
err := row.Scan(&i.Schema, &i.Document, &i.PublishedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteChannelStatusNotIn = `-- name: DeleteChannelStatusNotIn :exec
|
||||
DELETE FROM channel_status WHERE NOT (id = ANY($1::text[]))
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteChannelStatusNotIn(ctx context.Context, dollar_1 []string) error {
|
||||
_, err := q.db.Exec(ctx, deleteChannelStatusNotIn, dollar_1)
|
||||
return err
|
||||
}
|
||||
|
||||
const listApplyStatus = `-- name: ListApplyStatus :many
|
||||
SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind
|
||||
`
|
||||
|
||||
func (q *Queries) ListApplyStatus(ctx context.Context) ([]ApplyStatus, error) {
|
||||
rows, err := q.db.Query(ctx, listApplyStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ApplyStatus
|
||||
for rows.Next() {
|
||||
var i ApplyStatus
|
||||
if err := rows.Scan(
|
||||
&i.Process,
|
||||
&i.Kind,
|
||||
&i.AppliedVersion,
|
||||
&i.Error,
|
||||
&i.ReportedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listChannelStatus = `-- name: ListChannelStatus :many
|
||||
SELECT id, name, state, detail, configured, reload_supported, observed_at
|
||||
FROM channel_status ORDER BY id
|
||||
`
|
||||
|
||||
func (q *Queries) ListChannelStatus(ctx context.Context) ([]ChannelStatus, error) {
|
||||
rows, err := q.db.Query(ctx, listChannelStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ChannelStatus
|
||||
for rows.Next() {
|
||||
var i ChannelStatus
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.State,
|
||||
&i.Detail,
|
||||
&i.Configured,
|
||||
&i.ReloadSupported,
|
||||
&i.ObservedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const upsertApplyStatus = `-- name: UpsertApplyStatus :exec
|
||||
INSERT INTO apply_status (process, kind, applied_version, error, reported_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (process, kind) DO UPDATE SET
|
||||
applied_version = excluded.applied_version,
|
||||
error = excluded.error,
|
||||
reported_at = excluded.reported_at
|
||||
`
|
||||
|
||||
type UpsertApplyStatusParams struct {
|
||||
Process string
|
||||
Kind string
|
||||
AppliedVersion int64
|
||||
Error string
|
||||
ReportedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertApplyStatus(ctx context.Context, arg UpsertApplyStatusParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertApplyStatus,
|
||||
arg.Process,
|
||||
arg.Kind,
|
||||
arg.AppliedVersion,
|
||||
arg.Error,
|
||||
arg.ReportedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertChannelStatus = `-- name: UpsertChannelStatus :exec
|
||||
INSERT INTO channel_status (id, name, state, detail, configured, reload_supported, observed_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
state = excluded.state,
|
||||
detail = excluded.detail,
|
||||
configured = excluded.configured,
|
||||
reload_supported = excluded.reload_supported,
|
||||
observed_at = excluded.observed_at
|
||||
`
|
||||
|
||||
type UpsertChannelStatusParams struct {
|
||||
ID string
|
||||
Name string
|
||||
State string
|
||||
Detail string
|
||||
Configured bool
|
||||
ReloadSupported bool
|
||||
ObservedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertChannelStatus(ctx context.Context, arg UpsertChannelStatusParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertChannelStatus,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.State,
|
||||
arg.Detail,
|
||||
arg.Configured,
|
||||
arg.ReloadSupported,
|
||||
arg.ObservedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertConfigSnapshot = `-- name: UpsertConfigSnapshot :exec
|
||||
|
||||
INSERT INTO config_snapshot (id, schema, document, published_at)
|
||||
VALUES (1, $1, $2, $3)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
schema = excluded.schema,
|
||||
document = excluded.document,
|
||||
published_at = excluded.published_at
|
||||
`
|
||||
|
||||
type UpsertConfigSnapshotParams struct {
|
||||
Schema string
|
||||
Document string
|
||||
PublishedAt time.Time
|
||||
}
|
||||
|
||||
// Status projection queries: the published configuration snapshot and the
|
||||
// channel / apply-status runtime reports the UI reads across the State Store
|
||||
// boundary.
|
||||
func (q *Queries) UpsertConfigSnapshot(ctx context.Context, arg UpsertConfigSnapshotParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertConfigSnapshot, arg.Schema, arg.Document, arg.PublishedAt)
|
||||
return err
|
||||
}
|
||||
@@ -10,6 +10,94 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const archiveTaskDelete = `-- name: ArchiveTaskDelete :execrows
|
||||
DELETE FROM tasks WHERE id = $1 AND status = $2
|
||||
`
|
||||
|
||||
type ArchiveTaskDeleteParams struct {
|
||||
ID int64
|
||||
Status string
|
||||
}
|
||||
|
||||
func (q *Queries) ArchiveTaskDelete(ctx context.Context, arg ArchiveTaskDeleteParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, archiveTaskDelete, arg.ID, arg.Status)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const beginRemediationTask = `-- name: BeginRemediationTask :execrows
|
||||
UPDATE tasks SET status = 'queued', workflow = 'remediate', stage = '', park_reason = '', park_class = 'needs_human', review_payload = $2, updated_at = now()
|
||||
WHERE id = $1 AND status = 'pr_open'
|
||||
`
|
||||
|
||||
type BeginRemediationTaskParams struct {
|
||||
ID int64
|
||||
ReviewPayload string
|
||||
}
|
||||
|
||||
func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediationTaskParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, beginRemediationTask, arg.ID, arg.ReviewPayload)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const claimByIssue = `-- name: ClaimByIssue :one
|
||||
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
|
||||
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor
|
||||
`
|
||||
|
||||
type ClaimByIssueParams struct {
|
||||
Owner string
|
||||
Repo string
|
||||
IssueNumber int64
|
||||
}
|
||||
|
||||
func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, claimByIssue, arg.Owner, arg.Repo, arg.IssueNumber)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.IssueNumber,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Labels,
|
||||
&i.Status,
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Branch,
|
||||
&i.Plan,
|
||||
&i.Notes,
|
||||
&i.PrNumber,
|
||||
&i.TokensUsed,
|
||||
&i.Iterations,
|
||||
&i.Attempt,
|
||||
&i.ParkReason,
|
||||
&i.WatchCommentID,
|
||||
&i.ParkClass,
|
||||
&i.RemediationRounds,
|
||||
&i.RetryCount,
|
||||
&i.Source,
|
||||
&i.Identity,
|
||||
&i.BindingID,
|
||||
&i.BindingVersion,
|
||||
&i.ReviewPayload,
|
||||
&i.WorkflowDefinitionVersion,
|
||||
&i.WorkflowDefinitionDigest,
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const claimNextTask = `-- name: ClaimNextTask :one
|
||||
UPDATE tasks
|
||||
SET status = 'running', attempt = attempt + 1, updated_at = now()
|
||||
@@ -20,7 +108,7 @@ WHERE id = (
|
||||
FOR UPDATE SKIP LOCKED
|
||||
LIMIT 1
|
||||
)
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor
|
||||
`
|
||||
|
||||
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
|
||||
@@ -62,6 +150,7 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -108,6 +197,38 @@ func (q *Queries) CountTasksByStatus(ctx context.Context) ([]CountTasksByStatusR
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const enqueueIssue = `-- name: EnqueueIssue :execrows
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (owner, repo, issue_number) DO NOTHING
|
||||
`
|
||||
|
||||
type EnqueueIssueParams struct {
|
||||
Owner string
|
||||
Repo string
|
||||
IssueNumber int64
|
||||
Title string
|
||||
Body string
|
||||
Labels string
|
||||
Identity string
|
||||
}
|
||||
|
||||
func (q *Queries) EnqueueIssue(ctx context.Context, arg EnqueueIssueParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, enqueueIssue,
|
||||
arg.Owner,
|
||||
arg.Repo,
|
||||
arg.IssueNumber,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.Labels,
|
||||
arg.Identity,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const insertChatTask = `-- name: InsertChatTask :one
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
|
||||
VALUES (
|
||||
@@ -120,7 +241,7 @@ VALUES (
|
||||
), $3) + 1,
|
||||
$4, $5, 'chat', $6, 'chat', $7
|
||||
)
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor
|
||||
`
|
||||
|
||||
type InsertChatTaskParams struct {
|
||||
@@ -181,6 +302,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -207,6 +329,57 @@ func (q *Queries) InsertTransition(ctx context.Context, arg InsertTransitionPara
|
||||
return err
|
||||
}
|
||||
|
||||
const listOpenPRs = `-- name: ListOpenPRs :many
|
||||
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt, review_cursor, watch_comment_id
|
||||
FROM tasks WHERE status = 'pr_open'
|
||||
`
|
||||
|
||||
type ListOpenPRsRow struct {
|
||||
ID int64
|
||||
Owner string
|
||||
Repo string
|
||||
IssueNumber int64
|
||||
PrNumber int64
|
||||
Status string
|
||||
Source string
|
||||
Identity string
|
||||
Attempt int64
|
||||
ReviewCursor int64
|
||||
WatchCommentID int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListOpenPRs(ctx context.Context) ([]ListOpenPRsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listOpenPRs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListOpenPRsRow
|
||||
for rows.Next() {
|
||||
var i ListOpenPRsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.IssueNumber,
|
||||
&i.PrNumber,
|
||||
&i.Status,
|
||||
&i.Source,
|
||||
&i.Identity,
|
||||
&i.Attempt,
|
||||
&i.ReviewCursor,
|
||||
&i.WatchCommentID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listTaskSummaries = `-- name: ListTaskSummaries :many
|
||||
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
|
||||
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
||||
@@ -321,6 +494,67 @@ func (q *Queries) RecoverStaleTasks(ctx context.Context) (int64, error) {
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const requeueTask = `-- name: RequeueTask :execrows
|
||||
UPDATE tasks SET status = 'queued',
|
||||
workflow = CASE WHEN $1::text = '' THEN workflow ELSE $1::text END,
|
||||
stage = '', park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = $2 AND status = $3
|
||||
`
|
||||
|
||||
type RequeueTaskParams struct {
|
||||
Workflow string
|
||||
ID int64
|
||||
FromStatus string
|
||||
}
|
||||
|
||||
func (q *Queries) RequeueTask(ctx context.Context, arg RequeueTaskParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, requeueTask, arg.Workflow, arg.ID, arg.FromStatus)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const retryTask = `-- name: RetryTask :execrows
|
||||
UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
|
||||
workflow = CASE WHEN $1::text = '' THEN workflow ELSE $1::text END,
|
||||
stage = '', park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = $2 AND status = $3
|
||||
`
|
||||
|
||||
type RetryTaskParams struct {
|
||||
Workflow string
|
||||
ID int64
|
||||
FromStatus string
|
||||
}
|
||||
|
||||
func (q *Queries) RetryTask(ctx context.Context, arg RetryTaskParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, retryTask, arg.Workflow, arg.ID, arg.FromStatus)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setReviewCursors = `-- name: SetReviewCursors :execrows
|
||||
UPDATE tasks SET review_cursor = $2, watch_comment_id = $3, updated_at = now()
|
||||
WHERE id = $1 AND status = 'pr_open'
|
||||
`
|
||||
|
||||
type SetReviewCursorsParams struct {
|
||||
ID int64
|
||||
ReviewCursor int64
|
||||
WatchCommentID int64
|
||||
}
|
||||
|
||||
func (q *Queries) SetReviewCursors(ctx context.Context, arg SetReviewCursorsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, setReviewCursors, arg.ID, arg.ReviewCursor, arg.WatchCommentID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const stampTaskBinding = `-- name: StampTaskBinding :exec
|
||||
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1
|
||||
`
|
||||
@@ -337,7 +571,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
|
||||
}
|
||||
|
||||
const taskByID = `-- name: TaskByID :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at FROM tasks WHERE id = $1
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor FROM tasks WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
@@ -376,12 +610,13 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const taskByIssue = `-- name: TaskByIssue :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||
`
|
||||
|
||||
type TaskByIssueParams struct {
|
||||
@@ -426,22 +661,32 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const taskByPR = `-- name: TaskByPR :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
|
||||
`
|
||||
|
||||
type TaskByPRParams struct {
|
||||
Owner string
|
||||
Repo string
|
||||
PrNumber int64
|
||||
Status string
|
||||
}
|
||||
|
||||
// OpenTaskByPR is the live-task lookup: it only resolves a PR the task is
|
||||
// currently tracking as open, so a merged or rejected task does not read as
|
||||
// the owner of a PR number it no longer holds.
|
||||
func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, taskByPR, arg.Owner, arg.Repo, arg.PrNumber)
|
||||
row := q.db.QueryRow(ctx, taskByPR,
|
||||
arg.Owner,
|
||||
arg.Repo,
|
||||
arg.PrNumber,
|
||||
arg.Status,
|
||||
)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
@@ -476,6 +721,7 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -513,6 +759,24 @@ func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams)
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateReviewPayloadTask = `-- name: UpdateReviewPayloadTask :execrows
|
||||
UPDATE tasks SET review_payload = $2, updated_at = now()
|
||||
WHERE id = $1 AND status = 'queued' AND workflow = 'remediate'
|
||||
`
|
||||
|
||||
type UpdateReviewPayloadTaskParams struct {
|
||||
ID int64
|
||||
ReviewPayload string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateReviewPayloadTask(ctx context.Context, arg UpdateReviewPayloadTaskParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateReviewPayloadTask, arg.ID, arg.ReviewPayload)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateTask = `-- name: UpdateTask :exec
|
||||
UPDATE tasks SET workflow = $2, stage = $3, branch = $4, plan = $5, notes = $6,
|
||||
pr_number = $7, tokens_used = $8, iterations = $9, park_reason = $10,
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
-- Event-log queries. The (at, id) cursor is a wire contract: at is the
|
||||
-- fixed-width sort key and id only breaks ties, so a reader pages by
|
||||
-- "after (at, id)" and never skips or repeats a row. See
|
||||
-- internal/domain/storecontract/cursor.go.
|
||||
|
||||
-- name: InsertEvent :one
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
||||
RETURNING id;
|
||||
|
||||
-- name: ListEventsFromBeginning :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
ORDER BY at, id
|
||||
LIMIT $1;
|
||||
|
||||
-- name: ListEventsAfter :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
WHERE at > $1 OR (at = $1 AND id > $2)
|
||||
ORDER BY at, id
|
||||
LIMIT $3;
|
||||
|
||||
-- name: TaskEventsByID :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
|
||||
FROM events
|
||||
WHERE task_id = $1
|
||||
ORDER BY id;
|
||||
|
||||
-- name: WorkflowStats :many
|
||||
SELECT workflow,
|
||||
COUNT(*)::int AS runs,
|
||||
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
|
||||
COUNT(*) FILTER (WHERE status = 'pr_open')::int AS pr_open,
|
||||
COUNT(*) FILTER (WHERE status = 'parked')::int AS parked,
|
||||
CAST(AVG(tokens_used) AS bigint) AS avg_tokens,
|
||||
AVG(iterations)::float8 AS avg_steps,
|
||||
SUM(tokens_used)::bigint AS total_tokens
|
||||
FROM tasks
|
||||
WHERE workflow <> ''
|
||||
GROUP BY workflow
|
||||
ORDER BY COUNT(*) DESC;
|
||||
|
||||
-- name: StageStats :many
|
||||
SELECT workflow, stage,
|
||||
COUNT(*)::int AS runs,
|
||||
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
|
||||
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
|
||||
FROM events
|
||||
WHERE kind = 'stage_finish'
|
||||
GROUP BY workflow, stage
|
||||
ORDER BY workflow, stage;
|
||||
|
||||
-- name: TokensByDay :many
|
||||
WITH event_totals AS (
|
||||
SELECT task_id, to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
|
||||
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
|
||||
FROM events WHERE kind = 'agent_finish'
|
||||
GROUP BY task_id, day
|
||||
), usage AS (
|
||||
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
||||
UNION ALL
|
||||
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
|
||||
FROM tasks t
|
||||
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
||||
SELECT 1 FROM event_totals e
|
||||
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
||||
)
|
||||
)
|
||||
SELECT day, SUM(tokens)::bigint AS tokens
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1;
|
||||
@@ -0,0 +1,48 @@
|
||||
-- Identity persistence. The domain owns lifecycle semantics (internal/domain/
|
||||
-- identity); these queries are the durable half of that contract. Aliases are
|
||||
-- unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
|
||||
-- the SQLite schema's COLLATE NOCASE.
|
||||
|
||||
-- name: ListIdentities :many
|
||||
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
||||
FROM identities ORDER BY created_at, id;
|
||||
|
||||
-- name: GetIdentity :one
|
||||
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
||||
FROM identities WHERE id = $1;
|
||||
|
||||
-- name: ResolveIdentityAlias :one
|
||||
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
||||
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
|
||||
WHERE lower(a.alias) = lower($1);
|
||||
|
||||
-- name: InsertIdentity :exec
|
||||
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
||||
|
||||
-- name: InsertIdentityAlias :exec
|
||||
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2);
|
||||
|
||||
-- name: InsertIdentityAliasIgnoreConflict :exec
|
||||
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: InsertIdentityEvent :exec
|
||||
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
||||
|
||||
-- name: UpdateIdentity :execrows
|
||||
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
|
||||
WHERE id = $1 AND version = $6;
|
||||
|
||||
-- name: ResolveIdentitySubject :one
|
||||
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
||||
FROM identities i JOIN identity_subjects b ON b.identity_id = i.id
|
||||
WHERE b.issuer = $1 AND b.subject = $2;
|
||||
|
||||
-- name: UpsertIdentitySubject :exec
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at;
|
||||
|
||||
-- name: UpdateTaskIdentity :exec
|
||||
UPDATE tasks SET identity = $1 WHERE identity = $2;
|
||||
@@ -0,0 +1,44 @@
|
||||
-- Status projection queries: the published configuration snapshot and the
|
||||
-- channel / apply-status runtime reports the UI reads across the State Store
|
||||
-- boundary.
|
||||
|
||||
-- name: UpsertConfigSnapshot :exec
|
||||
INSERT INTO config_snapshot (id, schema, document, published_at)
|
||||
VALUES (1, $1, $2, $3)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
schema = excluded.schema,
|
||||
document = excluded.document,
|
||||
published_at = excluded.published_at;
|
||||
|
||||
-- name: ConfigSnapshotByID :one
|
||||
SELECT schema, document, published_at FROM config_snapshot WHERE id = 1;
|
||||
|
||||
-- name: UpsertChannelStatus :exec
|
||||
INSERT INTO channel_status (id, name, state, detail, configured, reload_supported, observed_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
state = excluded.state,
|
||||
detail = excluded.detail,
|
||||
configured = excluded.configured,
|
||||
reload_supported = excluded.reload_supported,
|
||||
observed_at = excluded.observed_at;
|
||||
|
||||
-- name: DeleteChannelStatusNotIn :exec
|
||||
DELETE FROM channel_status WHERE NOT (id = ANY($1::text[]));
|
||||
|
||||
-- name: ListChannelStatus :many
|
||||
SELECT id, name, state, detail, configured, reload_supported, observed_at
|
||||
FROM channel_status ORDER BY id;
|
||||
|
||||
-- name: UpsertApplyStatus :exec
|
||||
INSERT INTO apply_status (process, kind, applied_version, error, reported_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (process, kind) DO UPDATE SET
|
||||
applied_version = excluded.applied_version,
|
||||
error = excluded.error,
|
||||
reported_at = excluded.reported_at;
|
||||
|
||||
-- name: ListApplyStatus :many
|
||||
SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind;
|
||||
@@ -20,7 +20,10 @@ SELECT * FROM tasks WHERE id = $1;
|
||||
SELECT * FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3;
|
||||
|
||||
-- name: TaskByPR :one
|
||||
SELECT * FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3;
|
||||
-- OpenTaskByPR is the live-task lookup: it only resolves a PR the task is
|
||||
-- currently tracking as open, so a merged or rejected task does not read as
|
||||
-- the owner of a PR number it no longer holds.
|
||||
SELECT * FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4;
|
||||
|
||||
-- name: ListTaskSummaries :many
|
||||
-- The dashboard's list. This projection is deliberately narrow: Plan gates
|
||||
@@ -89,3 +92,44 @@ WHERE status IN ('merged', 'rejected', 'dead', 'closed_wont_do');
|
||||
|
||||
-- name: RecoverStaleTasks :execrows
|
||||
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running';
|
||||
|
||||
-- name: EnqueueIssue :execrows
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (owner, repo, issue_number) DO NOTHING;
|
||||
|
||||
-- name: ClaimByIssue :one
|
||||
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
|
||||
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
|
||||
RETURNING *;
|
||||
|
||||
-- name: BeginRemediationTask :execrows
|
||||
UPDATE tasks SET status = 'queued', workflow = 'remediate', stage = '', park_reason = '', park_class = 'needs_human', review_payload = $2, updated_at = now()
|
||||
WHERE id = $1 AND status = 'pr_open';
|
||||
|
||||
-- name: UpdateReviewPayloadTask :execrows
|
||||
UPDATE tasks SET review_payload = $2, updated_at = now()
|
||||
WHERE id = $1 AND status = 'queued' AND workflow = 'remediate';
|
||||
|
||||
-- name: SetReviewCursors :execrows
|
||||
UPDATE tasks SET review_cursor = $2, watch_comment_id = $3, updated_at = now()
|
||||
WHERE id = $1 AND status = 'pr_open';
|
||||
|
||||
-- name: RequeueTask :execrows
|
||||
UPDATE tasks SET status = 'queued',
|
||||
workflow = CASE WHEN @workflow::text = '' THEN workflow ELSE @workflow::text END,
|
||||
stage = '', park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = @id AND status = @from_status;
|
||||
|
||||
-- name: RetryTask :execrows
|
||||
UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
|
||||
workflow = CASE WHEN @workflow::text = '' THEN workflow ELSE @workflow::text END,
|
||||
stage = '', park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = @id AND status = @from_status;
|
||||
|
||||
-- name: ArchiveTaskDelete :execrows
|
||||
DELETE FROM tasks WHERE id = $1 AND status = $2;
|
||||
|
||||
-- name: ListOpenPRs :many
|
||||
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt, review_cursor, watch_comment_id
|
||||
FROM tasks WHERE status = 'pr_open';
|
||||
@@ -0,0 +1,125 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// PutConfigSnapshot replaces the published snapshot (one row, id = 1).
|
||||
func (s *Store) PutConfigSnapshot(ctx context.Context, snapshot storecontract.ConfigSnapshot) error {
|
||||
published := snapshot.PublishedAt
|
||||
if published.IsZero() {
|
||||
published = time.Now()
|
||||
}
|
||||
if err := s.queries().UpsertConfigSnapshot(ctx, postgresdb.UpsertConfigSnapshotParams{
|
||||
Schema: snapshot.Schema, Document: string(snapshot.Document), PublishedAt: published.UTC(),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("store: put config snapshot: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigSnapshot returns the published snapshot. found is false, with a nil
|
||||
// error, before anything has published one.
|
||||
func (s *Store) ConfigSnapshot(ctx context.Context) (storecontract.ConfigSnapshot, bool, error) {
|
||||
row, err := s.queries().ConfigSnapshotByID(ctx)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return storecontract.ConfigSnapshot{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return storecontract.ConfigSnapshot{}, false, fmt.Errorf("store: read config snapshot: %w", err)
|
||||
}
|
||||
return storecontract.ConfigSnapshot{
|
||||
Schema: row.Schema, Document: []byte(row.Document), PublishedAt: row.PublishedAt,
|
||||
}, true, nil
|
||||
}
|
||||
|
||||
// PutChannelStatus records the reporting process's whole set in one
|
||||
// transaction, removing rows it no longer reports.
|
||||
func (s *Store) PutChannelStatus(ctx context.Context, channels []storecontract.ChannelStatus) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("store: put channel status: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
reported := make([]string, 0, len(channels))
|
||||
for _, channel := range channels {
|
||||
if channel.ID == "" {
|
||||
return fmt.Errorf("store: put channel status: a channel needs an id")
|
||||
}
|
||||
observed := channel.ObservedAt
|
||||
if observed.IsZero() {
|
||||
observed = time.Now()
|
||||
}
|
||||
if err := q.UpsertChannelStatus(ctx, postgresdb.UpsertChannelStatusParams{
|
||||
ID: channel.ID, Name: channel.Name, State: channel.State, Detail: channel.Detail,
|
||||
Configured: channel.Configured, ReloadSupported: channel.ReloadSupported,
|
||||
ObservedAt: observed.UTC(),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("store: put channel status %s: %w", channel.ID, err)
|
||||
}
|
||||
reported = append(reported, channel.ID)
|
||||
}
|
||||
|
||||
if err := q.DeleteChannelStatusNotIn(ctx, reported); err != nil {
|
||||
return fmt.Errorf("store: put channel status: prune: %w", err)
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// ChannelStatus returns every reported channel, ordered by id.
|
||||
func (s *Store) ChannelStatus(ctx context.Context) ([]storecontract.ChannelStatus, error) {
|
||||
rows, err := s.queries().ListChannelStatus(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list channel status: %w", err)
|
||||
}
|
||||
out := make([]storecontract.ChannelStatus, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, storecontract.ChannelStatus{
|
||||
ID: r.ID, Name: r.Name, State: r.State, Detail: r.Detail,
|
||||
Configured: r.Configured, ReloadSupported: r.ReloadSupported, ObservedAt: r.ObservedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PutApplyStatus records what one process applied for one resource kind,
|
||||
// replacing its own (process, kind) row.
|
||||
func (s *Store) PutApplyStatus(ctx context.Context, status storecontract.ApplyStatus) error {
|
||||
reported := status.ReportedAt
|
||||
if reported.IsZero() {
|
||||
reported = time.Now()
|
||||
}
|
||||
if err := s.queries().UpsertApplyStatus(ctx, postgresdb.UpsertApplyStatusParams{
|
||||
Process: status.Process, Kind: status.Kind, AppliedVersion: status.AppliedVersion,
|
||||
Error: status.Error, ReportedAt: reported.UTC(),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("store: put apply status: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListApplyStatus returns every process's report, ordered by (process, kind).
|
||||
func (s *Store) ListApplyStatus(ctx context.Context) ([]storecontract.ApplyStatus, error) {
|
||||
rows, err := s.queries().ListApplyStatus(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list apply status: %w", err)
|
||||
}
|
||||
out := make([]storecontract.ApplyStatus, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, storecontract.ApplyStatus{
|
||||
Process: r.Process, Kind: r.Kind, AppliedVersion: r.AppliedVersion,
|
||||
Error: r.Error, ReportedAt: r.ReportedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,491 @@
|
||||
// Package postgres implements the State Store contract on PostgreSQL.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// Store is the PostgreSQL implementation of the State Store. It carries the
|
||||
// full task/event/identity/status surface behind the storecontract interfaces,
|
||||
// with the control-plane resource surface embedded from Resources. The pool is
|
||||
// process-scoped and owned by the composition (internal/app/archied), not by
|
||||
// the Store, so Close is a no-op.
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool
|
||||
*Resources
|
||||
}
|
||||
|
||||
// New returns a Store over pool.
|
||||
func New(pool *pgxpool.Pool) *Store {
|
||||
return &Store{pool: pool, Resources: NewResources(pool)}
|
||||
}
|
||||
|
||||
func (s *Store) queries() *postgresdb.Queries {
|
||||
return postgresdb.New(s.pool)
|
||||
}
|
||||
|
||||
// Close is a no-op: unlike the SQLite store, this store owns no file handle
|
||||
// or connection -- the pool belongs to the composition that opened it.
|
||||
func (s *Store) Close() error { return nil }
|
||||
|
||||
// syntheticIssueNumberBase is the seed for a repo's first chat task. The
|
||||
// COALESCE allocator adds one to the passed fallback, so the fallback here is
|
||||
// base-1 and the first chat task lands on exactly base, matching the SQLite
|
||||
// store's reserved JSON-safe floor (see internal/store.EnqueueChatTask).
|
||||
const syntheticIssueNumberBase = 1_000_000_000_000_000
|
||||
|
||||
// taskFromRow maps the generated task row to the workflow.Task the daemon and
|
||||
// webui consume.
|
||||
func taskFromRow(t postgresdb.Task) *workflow.Task {
|
||||
return &workflow.Task{
|
||||
ID: t.ID,
|
||||
Owner: t.Owner,
|
||||
Repo: t.Repo,
|
||||
IssueNumber: int(t.IssueNumber),
|
||||
Title: t.Title,
|
||||
Body: t.Body,
|
||||
Labels: t.Labels,
|
||||
Status: t.Status,
|
||||
Workflow: t.Workflow,
|
||||
WorkflowDefinitionVersion: t.WorkflowDefinitionVersion,
|
||||
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
|
||||
WorkflowDefinitionYAML: t.WorkflowDefinitionYaml,
|
||||
Stage: t.Stage,
|
||||
Branch: t.Branch,
|
||||
Plan: t.Plan,
|
||||
Notes: t.Notes,
|
||||
PRNumber: int(t.PrNumber),
|
||||
TokensUsed: int(t.TokensUsed),
|
||||
Iterations: int(t.Iterations),
|
||||
Attempt: int(t.Attempt),
|
||||
ParkReason: t.ParkReason,
|
||||
RetryCount: int(t.RetryCount),
|
||||
WatchCommentID: t.WatchCommentID,
|
||||
ReviewCursor: t.ReviewCursor,
|
||||
Source: t.Source,
|
||||
Identity: t.Identity,
|
||||
BindingID: t.BindingID,
|
||||
BindingVersion: int(t.BindingVersion),
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ParkClass: t.ParkClass,
|
||||
RemediationRounds: int(t.RemediationRounds),
|
||||
CreatedAt: t.CreatedAt,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// Compile-time checks: *Store satisfies every surface the SQLite store did.
|
||||
var (
|
||||
_ storecontract.TaskStore = (*Store)(nil)
|
||||
_ storecontract.BindingTaskCreator = (*Store)(nil)
|
||||
_ storecontract.ConfigSnapshotStore = (*Store)(nil)
|
||||
_ storecontract.ChannelStatusStore = (*Store)(nil)
|
||||
_ storecontract.ApplyStatusStore = (*Store)(nil)
|
||||
_ workflow.Store = (*Store)(nil)
|
||||
)
|
||||
|
||||
// EnqueueIssue inserts a new queued task for the issue; returns false if the
|
||||
// issue is already tracked (the idempotency key is owner/repo/number).
|
||||
func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
||||
n, err := s.queries().EnqueueIssue(ctx, postgresdb.EnqueueIssueParams{
|
||||
Owner: owner, Repo: repo, IssueNumber: int64(number),
|
||||
Title: title, Body: body, Labels: labels, Identity: identity,
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// EnqueueChatTask inserts a queued chat-sourced task and returns the full row.
|
||||
// The store allocates the synthetic issue number durably so processes sharing
|
||||
// the database cannot generate the same value.
|
||||
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity string) (*workflow.Task, error) {
|
||||
t, err := s.queries().InsertChatTask(ctx, postgresdb.InsertChatTaskParams{
|
||||
Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity,
|
||||
FallbackIssueNumber: syntheticIssueNumberBase - 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// EnqueueBindingTask enqueues a binding-triggered task and stamps its binding
|
||||
// provenance in a second statement (best-effort, as in the SQLite store).
|
||||
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int) (*workflow.Task, error) {
|
||||
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, wf, identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.queries().StampTaskBinding(ctx, postgresdb.StampTaskBindingParams{
|
||||
ID: t.ID, BindingID: bindingID, BindingVersion: int64(bindingVersion),
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("store: stamp binding provenance: %w", err)
|
||||
}
|
||||
t.BindingID = bindingID
|
||||
t.BindingVersion = bindingVersion
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// ClaimNext atomically moves the oldest queued task to running and returns it;
|
||||
// nil when the queue is empty.
|
||||
func (s *Store) ClaimNext(ctx context.Context) (*workflow.Task, error) {
|
||||
t, err := s.queries().ClaimNextTask(ctx)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// ClaimByIssue atomically claims a queued task by owner/repo/issue_number.
|
||||
func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
||||
t, err := s.queries().ClaimByIssue(ctx, postgresdb.ClaimByIssueParams{
|
||||
Owner: owner, Repo: repo, IssueNumber: int64(number),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// Transition moves a task to a new status and records the audit detail. The
|
||||
// from status guards the update; a mismatch returns ErrStaleTransition without
|
||||
// writing an audit row. Transitioning to parked also stores detail as
|
||||
// ParkReason in the same transaction.
|
||||
func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.TransitionTask(ctx, postgresdb.TransitionTaskParams{
|
||||
ID: taskID, Status: to, ParkReason: clip(detail, 4000),
|
||||
ParkClass: taskstate.ParkNeedsHuman, Status_2: from,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: from, ToStatus: to, Detail: clip(detail, 4000),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// ParkTask is the classified park write: the same guarded running->parked
|
||||
// transition Transition performs, carrying the park class the site chose.
|
||||
func (s *Store) ParkTask(ctx context.Context, taskID int64, from, detail, class string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.ParkTask(ctx, postgresdb.ParkTaskParams{
|
||||
ID: taskID, ParkReason: clip(detail, 4000),
|
||||
ParkClass: taskstate.NormalizeParkClass(class), Status: from,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: from, ToStatus: workflow.StatusParked, Detail: clip(detail, 4000),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Update persists mutable task fields written by workflows.
|
||||
func (s *Store) Update(ctx context.Context, t *workflow.Task) error {
|
||||
return s.queries().UpdateTask(ctx, postgresdb.UpdateTaskParams{
|
||||
ID: t.ID,
|
||||
Workflow: t.Workflow,
|
||||
Stage: t.Stage,
|
||||
Branch: t.Branch,
|
||||
Plan: t.Plan,
|
||||
Notes: t.Notes,
|
||||
PrNumber: int64(t.PRNumber),
|
||||
TokensUsed: int64(t.TokensUsed),
|
||||
Iterations: int64(t.Iterations),
|
||||
ParkReason: clip(t.ParkReason, 4000),
|
||||
WatchCommentID: t.WatchCommentID,
|
||||
RetryCount: int64(t.RetryCount),
|
||||
RemediationRounds: int64(t.RemediationRounds),
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
WorkflowDefinitionVersion: t.WorkflowDefinitionVersion,
|
||||
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
|
||||
WorkflowDefinitionYaml: t.WorkflowDefinitionYAML,
|
||||
})
|
||||
}
|
||||
|
||||
// BeginRemediation queues a remediate run carrying the review unit in the same
|
||||
// guarded write, so a claimed remediation always has its input.
|
||||
func (s *Store) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.BeginRemediationTask(ctx, postgresdb.BeginRemediationTaskParams{
|
||||
ID: taskID, ReviewPayload: clip(payload, 4000),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: workflow.StatusPROpen, ToStatus: workflow.StatusQueued,
|
||||
Detail: "review reaction queued remediation",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// UpdateReviewPayload replaces a queued remediation's review unit.
|
||||
func (s *Store) UpdateReviewPayload(ctx context.Context, taskID int64, payload string) error {
|
||||
n, err := s.queries().UpdateReviewPayloadTask(ctx, postgresdb.UpdateReviewPayloadTaskParams{
|
||||
ID: taskID, ReviewPayload: clip(payload, 4000),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetReviewCursors persists the poll backstop's per-task review and comment
|
||||
// high-water marks, guarded on pr_open.
|
||||
func (s *Store) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
|
||||
n, err := s.queries().SetReviewCursors(ctx, postgresdb.SetReviewCursorsParams{
|
||||
ID: taskID, ReviewCursor: reviewCursor, WatchCommentID: commentCursor,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Requeue puts a task back on the queue; an empty workflow keeps the task's
|
||||
// current workflow.
|
||||
func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.RequeueTask(ctx, postgresdb.RequeueTaskParams{
|
||||
ID: taskID, Workflow: wf, FromStatus: fromStatus,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: fromStatus, ToStatus: workflow.StatusQueued, Detail: "requeued " + wf,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// RetryTask requeues a task and increments retry_count in the same guarded
|
||||
// transaction.
|
||||
func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.RetryTask(ctx, postgresdb.RetryTaskParams{
|
||||
ID: taskID, Workflow: wf, FromStatus: fromStatus,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: fromStatus, ToStatus: workflow.StatusQueued, Detail: "retried " + wf,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// ArchiveTask removes one terminal task from the active task board and returns
|
||||
// the id of the audit event written alongside the delete.
|
||||
func (s *Store) ArchiveTask(ctx context.Context, taskID int64, fromStatus string, audit events.Event) (int64, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
eventID, err := insertEventQ(ctx, q, audit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err := q.ArchiveTaskDelete(ctx, postgresdb.ArchiveTaskDeleteParams{
|
||||
ID: taskID, Status: fromStatus,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, storecontract.ErrStaleTransition
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return eventID, nil
|
||||
}
|
||||
|
||||
// RecoverStale re-queues tasks left running by a crashed daemon.
|
||||
func (s *Store) RecoverStale(ctx context.Context) (int64, error) {
|
||||
return s.queries().RecoverStaleTasks(ctx)
|
||||
}
|
||||
|
||||
// OpenPRs returns tasks whose PR state should be reconciled with the forge,
|
||||
// carrying the attempt and the review/comment cursors the reconcile loop and
|
||||
// poll backstop read.
|
||||
func (s *Store) OpenPRs(ctx context.Context) ([]workflow.Task, error) {
|
||||
rows, err := s.queries().ListOpenPRs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks := make([]workflow.Task, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
tasks = append(tasks, workflow.Task{
|
||||
ID: r.ID, Owner: r.Owner, Repo: r.Repo, IssueNumber: int(r.IssueNumber),
|
||||
PRNumber: int(r.PrNumber), Status: r.Status, Source: r.Source,
|
||||
Identity: r.Identity, Attempt: int(r.Attempt),
|
||||
ReviewCursor: r.ReviewCursor, WatchCommentID: r.WatchCommentID,
|
||||
})
|
||||
}
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
// ClearTerminalTasks deletes tasks whose status is terminal.
|
||||
func (s *Store) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
return s.queries().ClearTerminalTasks(ctx)
|
||||
}
|
||||
|
||||
// Tasks returns all tasks, newest first (dashboard listing).
|
||||
func (s *Store) Tasks(ctx context.Context, limit int) ([]workflow.Task, error) {
|
||||
rows, err := s.queries().ListTaskSummaries(ctx, int32(limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks := make([]workflow.Task, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
tasks = append(tasks, workflow.Task{
|
||||
ID: r.ID, Owner: r.Owner, Repo: r.Repo, IssueNumber: int(r.IssueNumber),
|
||||
Title: r.Title, Status: r.Status, Workflow: r.Workflow, Stage: r.Stage,
|
||||
PRNumber: int(r.PrNumber), TokensUsed: int(r.TokensUsed), Iterations: int(r.Iterations),
|
||||
Attempt: int(r.Attempt), ParkReason: r.ParkReason, RetryCount: int(r.RetryCount),
|
||||
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt, Plan: r.Plan, Source: r.Source,
|
||||
Identity: r.Identity, BindingID: r.BindingID, BindingVersion: int(r.BindingVersion),
|
||||
})
|
||||
}
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
// StatusCounts returns task counts by status.
|
||||
func (s *Store) StatusCounts(ctx context.Context) (map[string]int, error) {
|
||||
rows, err := s.queries().CountTasksByStatus(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts := make(map[string]int, len(rows))
|
||||
for _, r := range rows {
|
||||
counts[r.Status] = int(r.Count)
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
// TaskByIssue returns the task tracking an issue, or nil.
|
||||
func (s *Store) TaskByIssue(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
||||
t, err := s.queries().TaskByIssue(ctx, postgresdb.TaskByIssueParams{
|
||||
Owner: owner, Repo: repo, IssueNumber: int64(number),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// OpenTaskByPR returns the live task that owns the given pull request, or nil.
|
||||
func (s *Store) OpenTaskByPR(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
||||
t, err := s.queries().TaskByPR(ctx, postgresdb.TaskByPRParams{
|
||||
Owner: owner, Repo: repo, PrNumber: int64(number), Status: workflow.StatusPROpen,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// TaskByID returns the task with the given database ID, or nil.
|
||||
func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, error) {
|
||||
t, err := s.queries().TaskByID(ctx, taskID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
+10
-22
@@ -6,34 +6,22 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// Resource types and their sentinels moved to storecontract (the producer-owned
|
||||
// contract) so the PostgreSQL implementation can reference them without linking
|
||||
// this SQLite package. These aliases keep daemon-side callers (the control
|
||||
// plane) and the in-package tests unchanged.
|
||||
var (
|
||||
ErrResourceNotFound = errors.New("resource not found")
|
||||
ErrResourceVersionConflict = errors.New("resource version conflict")
|
||||
ErrResourceNotFound = storecontract.ErrResourceNotFound
|
||||
ErrResourceVersionConflict = storecontract.ErrResourceVersionConflict
|
||||
)
|
||||
|
||||
type Resource struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
Actor string
|
||||
Source string
|
||||
RequestID string
|
||||
ExpectedVersion int64
|
||||
CurrentVersion int64
|
||||
At time.Time
|
||||
}
|
||||
type Resource = storecontract.Resource
|
||||
|
||||
type ResourceWrite struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
Actor string
|
||||
Source string
|
||||
RequestID string
|
||||
ExpectedVersion int64
|
||||
At time.Time
|
||||
}
|
||||
type ResourceWrite = storecontract.ResourceWrite
|
||||
|
||||
const resourcesSchema = `
|
||||
CREATE TABLE IF NOT EXISTS resources (
|
||||
|
||||
Reference in new issue
Block a user