refactor(scripts): drop the SQLite task-database backup from the installer and watchdog
The pre-update PostgreSQL snapshot is the only backup; the watchdog restores binaries and never the database.
This commit is contained in:
42 files changed
+14
-6999
No files matched your search
@@ -1,40 +0,0 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProductionTaskStoreUsesSQLite(t *testing.T) {
|
||||
configuredPath := filepath.Join(t.TempDir(), "state.db")
|
||||
path := taskDBPath(configuredPath)
|
||||
if path == configuredPath {
|
||||
t.Fatal("task database must use an independent fresh path")
|
||||
}
|
||||
st, err := openProductionTaskStore(t.Context(), path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(configuredPath); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("configured anchor path was opened directly: %v", err)
|
||||
}
|
||||
inserted, err := st.EnqueueIssue(t.Context(), "acme", "widget", 7, "persisted", "", "", "")
|
||||
if err != nil || !inserted {
|
||||
t.Fatalf("EnqueueIssue = (%v, %v), want (true, nil)", inserted, err)
|
||||
}
|
||||
if err := st.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
st, err = openProductionTaskStore(t.Context(), path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
task, err := st.TaskByIssue(t.Context(), "acme", "widget", 7)
|
||||
if err != nil || task == nil || task.Title != "persisted" {
|
||||
t.Fatalf("TaskByIssue after reopen = (%+v, %v)", task, err)
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
@@ -1,190 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// capturedAtLayout is fixed-width UTC with nanosecond precision, so string
|
||||
// comparison is time comparison. time.RFC3339Nano trims trailing zeros, which
|
||||
// would break that.
|
||||
const capturedAtLayout = "2006-01-02T15:04:05.000000000Z"
|
||||
|
||||
// This store is the implementation of every event-capture contract. The task
|
||||
// lifecycle contracts stay on the SQLite store; the two meet only at
|
||||
// binding_dispatches.task_id.
|
||||
var (
|
||||
_ storecontract.CaptureStore = (*Store)(nil)
|
||||
_ storecontract.MappingStore = (*Store)(nil)
|
||||
_ storecontract.BindingStore = (*Store)(nil)
|
||||
_ storecontract.BindingDispatcher = (*Store)(nil)
|
||||
_ storecontract.PlaybookDispatcher = (*Store)(nil)
|
||||
)
|
||||
|
||||
func (s *Store) record(collection, id string) (*core.Record, error) {
|
||||
r, err := s.app.FindRecordById(collection, id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
// InsertCapture stores one inbound event verbatim. Redaction happens upstream
|
||||
// in webhookguard; this persists exactly what it is given, because field
|
||||
// mapping is designed against the payload as received.
|
||||
//
|
||||
// retention and maxEvents are accepted for contract compatibility and applied
|
||||
// as a prune after the insert.
|
||||
func (s *Store) InsertCapture(ctx context.Context, c storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollCaptures)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
r.Set("source", c.Source)
|
||||
r.Set("remote_addr", c.RemoteAddr)
|
||||
r.Set("content_type", c.ContentType)
|
||||
r.Set("headers", c.Headers)
|
||||
r.Set("body", c.Body)
|
||||
r.Set("authenticated", c.Authenticated)
|
||||
received := c.ReceivedAt
|
||||
if received.IsZero() {
|
||||
received = time.Now()
|
||||
}
|
||||
r.Set("received_at", received.UTC().Format(capturedAtLayout))
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return "", fmt.Errorf("edastore: insert capture: %w", err)
|
||||
}
|
||||
if err := s.pruneCaptures(ctx, retention, maxEvents); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
// pruneCaptures enforces the retention window and count cap. A capture exists
|
||||
// to be mapped against while it is recent; keeping every event forever would
|
||||
// make an unauthenticated intake surface a disk-growth vector.
|
||||
func (s *Store) pruneCaptures(_ context.Context, retention time.Duration, maxEvents int) error {
|
||||
if retention > 0 {
|
||||
cutoff := time.Now().Add(-retention).UTC().Format(time.RFC3339)
|
||||
if _, err := s.app.DB().NewQuery(
|
||||
"DELETE FROM " + CollCaptures + " WHERE received_at < {:cutoff}").
|
||||
Bind(map[string]any{"cutoff": cutoff}).Execute(); err != nil {
|
||||
return fmt.Errorf("edastore: prune captures by age: %w", err)
|
||||
}
|
||||
}
|
||||
if maxEvents > 0 {
|
||||
if _, err := s.app.DB().NewQuery(
|
||||
"DELETE FROM " + CollCaptures + " WHERE id NOT IN (SELECT id FROM " + CollCaptures +
|
||||
" ORDER BY received_at DESC LIMIT {:keep})").
|
||||
Bind(map[string]any{"keep": maxEvents}).Execute(); err != nil {
|
||||
return fmt.Errorf("edastore: prune captures by count: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseCapturedAt reads the stored timestamp, tolerating the RFC3339 form a
|
||||
// caller-supplied value may carry.
|
||||
func parseCapturedAt(raw string) time.Time {
|
||||
for _, layout := range []string{capturedAtLayout, time.RFC3339Nano, time.RFC3339} {
|
||||
if t, err := time.Parse(layout, raw); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func captureValue(r *core.Record) storecontract.CapturedEvent {
|
||||
return storecontract.CapturedEvent{
|
||||
ID: r.Id,
|
||||
ReceivedAt: parseCapturedAt(r.GetString("received_at")),
|
||||
Source: r.GetString("source"),
|
||||
RemoteAddr: r.GetString("remote_addr"),
|
||||
ContentType: r.GetString("content_type"),
|
||||
Headers: r.GetString("headers"),
|
||||
Body: r.GetString("body"),
|
||||
Authenticated: r.GetBool("authenticated"),
|
||||
}
|
||||
}
|
||||
|
||||
// ListCaptures returns the newest captures first.
|
||||
func (s *Store) ListCaptures(_ context.Context, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
records, err := s.app.FindRecordsByFilter(CollCaptures, "", "-received_at", limit, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list captures: %w", err)
|
||||
}
|
||||
out := make([]storecontract.CapturedEvent, 0, len(records))
|
||||
for _, r := range records {
|
||||
out = append(out, captureValue(r))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListUndispatchedCaptures returns recent captures for the given sources that
|
||||
// no binding has dispatched yet.
|
||||
//
|
||||
// This is raw SQL rather than a record filter: the anti-join against the
|
||||
// dispatch ledger is a subquery, and PocketBase's filter DSL is not SQL (it
|
||||
// has no IN (SELECT ...) form). A read fires no hooks either way, so nothing
|
||||
// is lost by dropping to the query builder here.
|
||||
func (s *Store) ListUndispatchedCaptures(_ context.Context, sources []string, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
if len(sources) == 0 || limit <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
placeholders := make([]string, 0, len(sources))
|
||||
params := dbx.Params{"limit": limit}
|
||||
for i, src := range sources {
|
||||
key := fmt.Sprintf("s%d", i)
|
||||
placeholders = append(placeholders, "{:"+key+"}")
|
||||
params[key] = src
|
||||
}
|
||||
query := "SELECT id, received_at, source, remote_addr, content_type, headers, body, authenticated FROM " +
|
||||
CollCaptures + " WHERE source IN (" + strings.Join(placeholders, ",") +
|
||||
") AND id NOT IN (SELECT capture FROM " + CollBindingDispatches + ") ORDER BY received_at DESC LIMIT {:limit}"
|
||||
|
||||
var rows []captureRow
|
||||
if err := s.app.DB().NewQuery(query).Bind(params).All(&rows); err != nil {
|
||||
return nil, fmt.Errorf("edastore: list undispatched captures: %w", err)
|
||||
}
|
||||
out := make([]storecontract.CapturedEvent, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, r.value())
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// captureRow is the raw-SQL shape of a capture, used only where a record
|
||||
// filter cannot express the query.
|
||||
type captureRow struct {
|
||||
ID string `db:"id"`
|
||||
ReceivedAt string `db:"received_at"`
|
||||
Source string `db:"source"`
|
||||
RemoteAddr string `db:"remote_addr"`
|
||||
ContentType string `db:"content_type"`
|
||||
Headers string `db:"headers"`
|
||||
Body string `db:"body"`
|
||||
Authenticated bool `db:"authenticated"`
|
||||
}
|
||||
|
||||
func (r captureRow) value() storecontract.CapturedEvent {
|
||||
return storecontract.CapturedEvent{
|
||||
ID: r.ID,
|
||||
ReceivedAt: parseCapturedAt(r.ReceivedAt),
|
||||
Source: r.Source,
|
||||
RemoteAddr: r.RemoteAddr,
|
||||
ContentType: r.ContentType,
|
||||
Headers: r.Headers,
|
||||
Body: r.Body,
|
||||
Authenticated: r.Authenticated,
|
||||
}
|
||||
}
|
||||
@@ -1,393 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// --- mappings ---
|
||||
|
||||
func mappingValue(r *core.Record) (mapping.Mapping, error) {
|
||||
var fields []mapping.Field
|
||||
if raw := r.GetString("fields"); raw != "" {
|
||||
if err := json.Unmarshal([]byte(raw), &fields); err != nil {
|
||||
return mapping.Mapping{}, fmt.Errorf("edastore: decode mapping fields: %w", err)
|
||||
}
|
||||
}
|
||||
return mapping.Mapping{
|
||||
ID: r.Id,
|
||||
Name: r.GetString("name"),
|
||||
SourceHint: r.GetString("source_hint"),
|
||||
Fields: fields,
|
||||
CreatedAt: r.GetDateTime("created_at").Time(),
|
||||
UpdatedAt: r.GetDateTime("updated_at").Time(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Store) applyMapping(r *core.Record, m mapping.Mapping) error {
|
||||
encoded, err := json.Marshal(m.Fields)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: encode mapping fields: %w", err)
|
||||
}
|
||||
r.Set("name", m.Name)
|
||||
r.Set("source_hint", m.SourceHint)
|
||||
r.Set("fields", string(encoded))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) InsertMapping(_ context.Context, m mapping.Mapping) (string, error) {
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollMappings)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
if err := s.applyMapping(r, m); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return "", fmt.Errorf("edastore: insert mapping: %w", err)
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "create", r.Id)
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetMapping(_ context.Context, id string) (*mapping.Mapping, error) {
|
||||
r, err := s.record(CollMappings, id)
|
||||
if err != nil || r == nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := mappingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListMappings(_ context.Context) ([]mapping.Mapping, error) {
|
||||
records, err := s.app.FindRecordsByFilter(CollMappings, "", "-created_at", 0, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list mappings: %w", err)
|
||||
}
|
||||
out := make([]mapping.Mapping, 0, len(records))
|
||||
for _, r := range records {
|
||||
m, err := mappingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateMapping(_ context.Context, m mapping.Mapping) error {
|
||||
r, err := s.record(CollMappings, m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r == nil {
|
||||
return ErrMappingNotFound
|
||||
}
|
||||
if err := s.applyMapping(r, m); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "update", m.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteMapping(_ context.Context, id string) error {
|
||||
r, err := s.record(CollMappings, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r == nil {
|
||||
return ErrMappingNotFound
|
||||
}
|
||||
if err := s.app.Delete(r); err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "delete", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- bindings ---
|
||||
|
||||
func (s *Store) bindingValue(r *core.Record) (binding.Binding, error) {
|
||||
b := binding.Binding{
|
||||
ID: r.Id,
|
||||
Name: r.GetString("name"),
|
||||
Matcher: binding.Matcher{Source: r.GetString("source")},
|
||||
MappingID: r.GetString("mapping"),
|
||||
Workflow: r.GetString("workflow"),
|
||||
Owner: r.GetString("owner"),
|
||||
Repo: r.GetString("repo"),
|
||||
Version: r.GetInt("version"),
|
||||
Status: binding.Status(r.GetString("status")),
|
||||
Secret: r.GetString("secret"),
|
||||
CreatedAt: r.GetDateTime("created_at").Time(),
|
||||
UpdatedAt: r.GetDateTime("updated_at").Time(),
|
||||
}
|
||||
if s.cipher != nil && b.Secret != "" {
|
||||
plain, err := s.cipher.Decrypt(b.Secret)
|
||||
if err != nil {
|
||||
return binding.Binding{}, fmt.Errorf("edastore: decrypt binding secret: %w", err)
|
||||
}
|
||||
b.Secret = plain
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (s *Store) applyBinding(r *core.Record, b binding.Binding) error {
|
||||
r.Set("name", b.Name)
|
||||
r.Set("source", b.Matcher.Source)
|
||||
r.Set("mapping", b.MappingID)
|
||||
r.Set("workflow", b.Workflow)
|
||||
r.Set("owner", b.Owner)
|
||||
r.Set("repo", b.Repo)
|
||||
// An empty secret means "keep the stored one". An edit form that does not
|
||||
// echo the secret back must not silently disarm the binding by erasing it.
|
||||
if b.Secret == "" {
|
||||
return nil
|
||||
}
|
||||
secret := b.Secret
|
||||
if s.cipher != nil {
|
||||
encrypted, err := s.cipher.Encrypt(secret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: encrypt binding secret: %w", err)
|
||||
}
|
||||
secret = encrypted
|
||||
}
|
||||
r.Set("secret", secret)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertBinding stores a new binding as a draft. The caller's status is
|
||||
// ignored on purpose: a binding cannot go live without explicit approval, so
|
||||
// ApproveBinding is the only path to armed.
|
||||
//
|
||||
// A source may carry at most one binding, so an insert against a source that
|
||||
// already has one is refused rather than creating an ambiguous second matcher.
|
||||
func (s *Store) InsertBinding(ctx context.Context, b binding.Binding) (string, error) {
|
||||
existing, err := s.bindingsByFilter("source = {:source}", map[string]any{"source": b.Matcher.Source})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(existing) > 0 {
|
||||
return "", ErrBindingOverlap
|
||||
}
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollBindings)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
if err := s.applyBinding(r, b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
r.Set("version", 1)
|
||||
r.Set("status", string(binding.StatusPendingApproval))
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return "", fmt.Errorf("edastore: insert binding: %w", err)
|
||||
}
|
||||
_ = ctx
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "create", r.Id)
|
||||
return r.Id, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetBinding(_ context.Context, id string) (*binding.Binding, error) {
|
||||
r, err := s.record(CollBindings, id)
|
||||
if err != nil || r == nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := s.bindingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &b, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListBindings(_ context.Context) ([]binding.Binding, error) {
|
||||
return s.bindingsByFilter("", nil)
|
||||
}
|
||||
|
||||
// ArmedBindingsForSource returns the armed bindings a source can dispatch. A
|
||||
// draft or pending_approval binding is never returned: that is the gate.
|
||||
func (s *Store) ArmedBindingsForSource(_ context.Context, source string) ([]binding.Binding, error) {
|
||||
return s.bindingsByFilter("source = {:source} && status = {:status}",
|
||||
map[string]any{"source": source, "status": string(binding.StatusArmed)})
|
||||
}
|
||||
|
||||
func (s *Store) bindingsByFilter(filter string, params map[string]any) ([]binding.Binding, error) {
|
||||
var records []*core.Record
|
||||
var err error
|
||||
if params == nil {
|
||||
records, err = s.app.FindRecordsByFilter(CollBindings, filter, "-created_at", 0, 0)
|
||||
} else {
|
||||
records, err = s.app.FindRecordsByFilter(CollBindings, filter, "-created_at", 0, 0, params)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list bindings: %w", err)
|
||||
}
|
||||
out := make([]binding.Binding, 0, len(records))
|
||||
for _, r := range records {
|
||||
b, err := s.bindingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UpdateBinding rewrites a binding's editable fields, bumps its version, and
|
||||
// sends it back to pending_approval. An edit always requires re-approval: an
|
||||
// armed binding whose matcher or workflow changed is a different binding, and
|
||||
// re-arming it silently would defeat the gate.
|
||||
func (s *Store) UpdateBinding(_ context.Context, b binding.Binding) error {
|
||||
r, err := s.record(CollBindings, b.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r == nil {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
others, err := s.bindingsByFilter("source = {:source} && id != {:id}",
|
||||
map[string]any{"source": b.Matcher.Source, "id": b.ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(others) > 0 {
|
||||
return ErrBindingOverlap
|
||||
}
|
||||
if err := s.applyBinding(r, b); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Set("status", string(binding.StatusPendingApproval))
|
||||
r.Set("version", r.GetInt("version")+1)
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "update", b.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteBinding(_ context.Context, id string) error {
|
||||
r, err := s.record(CollBindings, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r == nil {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
if err := s.app.Delete(r); err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "delete", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApproveBinding is the only transition that arms a binding. It moves
|
||||
// pending_approval to armed; any other current state is refused, so a draft
|
||||
// cannot skip review and an armed binding cannot be re-approved.
|
||||
//
|
||||
// A source may have only one armed binding, so approval is refused when
|
||||
// another binding on the same source is already armed. Without that guard,
|
||||
// one capture could match two armed matchers and dispatch twice.
|
||||
func (s *Store) ApproveBinding(_ context.Context, id string) error {
|
||||
r, err := s.record(CollBindings, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r == nil {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
armed, err := s.bindingsByFilter("source = {:source} && status = {:status} && id != {:id}",
|
||||
map[string]any{"source": r.GetString("source"), "status": string(binding.StatusArmed), "id": id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(armed) > 0 {
|
||||
return ErrBindingOverlap
|
||||
}
|
||||
if r.GetString("status") != string(binding.StatusPendingApproval) {
|
||||
return ErrBindingTransition
|
||||
}
|
||||
r.Set("status", string(binding.StatusArmed))
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "approve", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- dispatch ledgers ---
|
||||
|
||||
// RecordDispatch writes one at-most-once binding dispatch. Uniqueness is the
|
||||
// collection's index, so a replay is refused by the schema; the caller sees
|
||||
// ErrAlreadyDispatched rather than a silent second run.
|
||||
func (s *Store) RecordDispatch(_ context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64) error {
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollBindingDispatches)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
r.Set("binding", bindingID)
|
||||
r.Set("binding_version", bindingVersion)
|
||||
r.Set("capture", captureID)
|
||||
r.Set("task_id", taskID)
|
||||
return ledgerWrite(s.app.Save(r), "edastore: record dispatch")
|
||||
}
|
||||
|
||||
func (s *Store) RecordPlaybookDispatch(_ context.Context, playbookID, playbookVersion, eventID, actionID string) error {
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollPlaybookDispatches)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
r.Set("playbook_id", playbookID)
|
||||
r.Set("playbook_version", playbookVersion)
|
||||
r.Set("event_id", eventID)
|
||||
r.Set("action_id", actionID)
|
||||
return ledgerWrite(s.app.Save(r), "edastore: record playbook dispatch")
|
||||
}
|
||||
|
||||
func (s *Store) DeletePlaybookDispatches(_ context.Context, playbookID string) error {
|
||||
_, err := s.app.DB().NewQuery(
|
||||
"DELETE FROM " + CollPlaybookDispatches + " WHERE playbook_id = {:id}").
|
||||
Bind(map[string]any{"id": playbookID}).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: delete playbook dispatches: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ledgerWrite turns the unique-index refusal into ErrAlreadyDispatched and
|
||||
// leaves every other failure alone. Treating all errors as "already recorded"
|
||||
// would turn an at-most-once ledger into a silent at-least-once dispatcher.
|
||||
func ledgerWrite(err error, what string) error {
|
||||
switch {
|
||||
case err == nil:
|
||||
return nil
|
||||
case isUniqueViolation(err):
|
||||
return ErrAlreadyDispatched
|
||||
default:
|
||||
return fmt.Errorf("%s: %w", what, err)
|
||||
}
|
||||
}
|
||||
|
||||
// isUniqueViolation reports whether err is the unique-index refusal.
|
||||
// PocketBase surfaces it as a validation failure rather than a typed error,
|
||||
// so the message is what there is to match on.
|
||||
func isUniqueViolation(err error) bool {
|
||||
text := strings.ToLower(err.Error())
|
||||
return strings.Contains(text, "unique") || strings.Contains(text, "already exists")
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
// Package edastore persists the event-capture and playbook-binding tables on
|
||||
// PocketBase-owned collections.
|
||||
//
|
||||
// These are the tables behind the event-capture epic: an inbound webhook is
|
||||
// captured whether or not anything understands it yet, an operator inspects
|
||||
// the real payload, maps fields against it, and binds a matcher and mapping
|
||||
// to a workflow. That loop wants a runtime-editable store with a live view of
|
||||
// arriving payloads, which is what PocketBase collections give directly.
|
||||
//
|
||||
// These collections are authoritative and writable: the admin UI editing a binding
|
||||
// IS the feature, not a hazard. The task lifecycle tables stay on SQLite --
|
||||
// they carry ordering, cursor and task-grant semantics that gain nothing from
|
||||
// being hand-editable. binding_dispatches.task reaches them by plain id, which
|
||||
// is the single seam between the two stores.
|
||||
//
|
||||
// Idempotency here is schema-enforced. The SQLite implementation relied on
|
||||
// INSERT OR IGNORE against a composite primary key; a unique index does the
|
||||
// same job without every caller having to remember the pattern.
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// Collection names. They are unprefixed because this store owns its database;
|
||||
// nothing else writes these tables.
|
||||
const (
|
||||
CollCaptures = "captures"
|
||||
CollMappings = "mappings"
|
||||
CollBindings = "bindings"
|
||||
CollBindingDispatches = "binding_dispatches"
|
||||
CollPlaybookDispatches = "playbook_dispatches"
|
||||
CollToolCalls = "tool_calls"
|
||||
)
|
||||
|
||||
// Sentinels are re-exported from the contract rather than redefined. Their
|
||||
// exact message strings are the wire contract: staterpc matches on (code,
|
||||
// canonical message) to rehydrate them on the client, so a second definition
|
||||
// with different text would break errors.Is across the gRPC boundary without
|
||||
// changing anything visible.
|
||||
var (
|
||||
ErrBindingNotFound = storecontract.ErrBindingNotFound
|
||||
ErrBindingOverlap = storecontract.ErrBindingOverlap
|
||||
ErrBindingTransition = storecontract.ErrBindingTransition
|
||||
ErrAlreadyDispatched = storecontract.ErrAlreadyDispatched
|
||||
ErrMappingNotFound = storecontract.ErrMappingNotFound
|
||||
)
|
||||
|
||||
// ErrCaptureNotFound reports a capture that is absent or aged out.
|
||||
var ErrCaptureNotFound = errors.New("edastore: capture not found")
|
||||
|
||||
// Collections returns every collection this store owns.
|
||||
func Collections() []string {
|
||||
return []string{
|
||||
CollCaptures, CollMappings, CollBindings,
|
||||
CollBindingDispatches, CollPlaybookDispatches, CollToolCalls,
|
||||
}
|
||||
}
|
||||
|
||||
// Config describes one store.
|
||||
type Config struct {
|
||||
// Cipher encrypts binding secrets at rest. Nil keeps them plaintext,
|
||||
// which is the behaviour that predates the option.
|
||||
Cipher BindingCipher
|
||||
// Notify receives one event per successful write on the bindings and
|
||||
// mappings tables, so an operator edit surfaces on the dashboard's
|
||||
// activity stream exactly like capture arrival does. Nil keeps writes
|
||||
// silent, which is the behaviour that predates the option and what
|
||||
// every existing constructor gets.
|
||||
Notify func(events.Event)
|
||||
// DBPath is the SQLite file PocketBase owns for these collections.
|
||||
DBPath string
|
||||
// DataDir holds PocketBase's auxiliary database and settings.
|
||||
DataDir string
|
||||
}
|
||||
|
||||
// Store is the PocketBase-backed EDA persistence.
|
||||
type Store struct {
|
||||
app *pocketbase.PocketBase
|
||||
cipher BindingCipher
|
||||
notify func(events.Event)
|
||||
}
|
||||
|
||||
// Open bootstraps the store and ensures its collections exist.
|
||||
func Open(cfg Config) (*Store, error) {
|
||||
if cfg.DBPath == "" || cfg.DataDir == "" {
|
||||
return nil, errors.New("edastore: DBPath and DataDir are required")
|
||||
}
|
||||
app := pocketbase.NewWithConfig(pocketbase.Config{
|
||||
DefaultDataDir: cfg.DataDir,
|
||||
HideStartBanner: true,
|
||||
DBConnect: func(requested string) (*dbx.DB, error) {
|
||||
if isDataDB(requested) {
|
||||
requested = cfg.DBPath
|
||||
}
|
||||
return dbx.Open("sqlite", requested+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
|
||||
},
|
||||
})
|
||||
if err := app.Bootstrap(); err != nil {
|
||||
return nil, fmt.Errorf("edastore: bootstrap: %w", err)
|
||||
}
|
||||
if err := app.RunAllMigrations(); err != nil {
|
||||
return nil, fmt.Errorf("edastore: migrations: %w", err)
|
||||
}
|
||||
if err := removeDefaultAuthCollection(app); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ensureCollections(app); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{app: app, cipher: cfg.Cipher, notify: cfg.Notify}, nil
|
||||
}
|
||||
|
||||
// App exposes the PocketBase application so one process can serve this store
|
||||
// and its admin UI from a single bootstrap.
|
||||
func (s *Store) App() core.App { return s.app }
|
||||
|
||||
// Close releases the store's database connections.
|
||||
func (s *Store) Close() error { return s.app.ClearBootstrap() }
|
||||
@@ -1,26 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/events"
|
||||
|
||||
// notifyWrite announces one successful write on the bindings or mappings
|
||||
// tables. It rides the same activity-event path capture arrival uses: the
|
||||
// State Store's event insert feeds the webui pump, which broadcasts to every
|
||||
// connected browser, so an operator edit is visible without a manual
|
||||
// refresh. The data carries the record id and action; the row itself stays
|
||||
// the only source of the full record, and consumers refetch through the
|
||||
// authenticated API -- the event is an invalidation signal, not a payload.
|
||||
//
|
||||
// A nil notifier is a silent no-op: stores composed without one (tests,
|
||||
// tooling) keep working unchanged, mirroring the nil Publish on
|
||||
// captureintake.Receiver.
|
||||
func (s *Store) notifyWrite(kind, subject, action, id string) {
|
||||
if s.notify == nil {
|
||||
return
|
||||
}
|
||||
past := map[string]string{"create": "created", "update": "updated", "approve": "approved", "delete": "deleted"}[action]
|
||||
s.notify(events.Event{
|
||||
Kind: kind,
|
||||
Detail: subject + " " + past,
|
||||
Data: map[string]any{"id": id, "action": action},
|
||||
})
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// recordingNotifier collects every event the store emits so a test can
|
||||
// assert on the exact sequence, mirroring captureintake's recordingPublisher.
|
||||
type recordingNotifier struct {
|
||||
got []events.Event
|
||||
}
|
||||
|
||||
func (r *recordingNotifier) notify(e events.Event) { r.got = append(r.got, e) }
|
||||
|
||||
func (r *recordingNotifier) reset() { r.got = nil }
|
||||
|
||||
// notifiedStore is an OpenTest store with the notifier wired, the way
|
||||
// composition does it through Config.Notify.
|
||||
func notifiedStore(t *testing.T) (*Store, *recordingNotifier) {
|
||||
t.Helper()
|
||||
s := OpenTest(t)
|
||||
n := &recordingNotifier{}
|
||||
s.notify = n.notify
|
||||
return s, n
|
||||
}
|
||||
|
||||
func TestWriteMethodsNotifyOnSuccess(t *testing.T) {
|
||||
b := func(source string) binding.Binding {
|
||||
return binding.Binding{
|
||||
Name: "webhook to workflow",
|
||||
Matcher: binding.Matcher{Source: source},
|
||||
Workflow: "triage",
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("InsertBinding", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id, err := s.InsertBinding(t.Context(), b("src-insert"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("InsertBinding notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindBindingChanged, "create", id)
|
||||
})
|
||||
|
||||
t.Run("UpdateBinding", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id := insertBinding(t, s, bindingInput{Name: "before", Source: "src-update", Workflow: "triage"})
|
||||
n.reset()
|
||||
bb := b("src-update")
|
||||
bb.ID = id
|
||||
bb.Workflow = "escalate"
|
||||
if err := s.UpdateBinding(t.Context(), bb); err != nil {
|
||||
t.Fatalf("UpdateBinding() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("UpdateBinding notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindBindingChanged, "update", id)
|
||||
})
|
||||
|
||||
t.Run("ApproveBinding", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id := insertBinding(t, s, bindingInput{Name: "draft", Source: "src-approve", Workflow: "triage"})
|
||||
bb := b("src-approve")
|
||||
bb.ID = id
|
||||
// Approval requires pending_approval; the edit transition also fires.
|
||||
if err := s.UpdateBinding(t.Context(), bb); err != nil {
|
||||
t.Fatalf("UpdateBinding() error = %v", err)
|
||||
}
|
||||
n.reset()
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("ApproveBinding notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindBindingChanged, "approve", id)
|
||||
})
|
||||
|
||||
t.Run("DeleteBinding", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id := insertBinding(t, s, bindingInput{Name: "doomed", Source: "src-delete", Workflow: "triage"})
|
||||
n.reset()
|
||||
if err := s.DeleteBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("DeleteBinding() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("DeleteBinding notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindBindingChanged, "delete", id)
|
||||
})
|
||||
|
||||
t.Run("InsertMapping", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "payload-map"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("InsertMapping notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindMappingChanged, "create", id)
|
||||
})
|
||||
|
||||
t.Run("UpdateMapping", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "payload-map"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
n.reset()
|
||||
m, err := s.GetMapping(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMapping() error = %v", err)
|
||||
}
|
||||
m.Name = "renamed"
|
||||
if err := s.UpdateMapping(t.Context(), *m); err != nil {
|
||||
t.Fatalf("UpdateMapping() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("UpdateMapping notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindMappingChanged, "update", id)
|
||||
})
|
||||
|
||||
t.Run("DeleteMapping", func(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "doomed"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
n.reset()
|
||||
if err := s.DeleteMapping(t.Context(), id); err != nil {
|
||||
t.Fatalf("DeleteMapping() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("DeleteMapping notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
assertEditEvent(t, n.got[0], events.KindMappingChanged, "delete", id)
|
||||
})
|
||||
}
|
||||
|
||||
func assertEditEvent(t *testing.T, e events.Event, kind, action, id string) {
|
||||
t.Helper()
|
||||
if e.Kind != kind {
|
||||
t.Errorf("event kind = %q, want %q", e.Kind, kind)
|
||||
}
|
||||
if e.Detail == "" {
|
||||
t.Errorf("event detail is empty")
|
||||
}
|
||||
if got := e.Data["id"]; got != id {
|
||||
t.Errorf("event data id = %v, want %q", got, id)
|
||||
}
|
||||
if got := e.Data["action"]; got != action {
|
||||
t.Errorf("event data action = %v, want %q", got, action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedWritesNotifyNothing(t *testing.T) {
|
||||
s, n := notifiedStore(t)
|
||||
|
||||
// Seed one binding and one mapping, then clear the seed-time events.
|
||||
seedID := insertBinding(t, s, bindingInput{Name: "seed", Source: "src-seed", Workflow: "triage"})
|
||||
if err := s.ApproveBinding(t.Context(), seedID); err != nil {
|
||||
t.Fatalf("ApproveBinding(seed) error = %v", err)
|
||||
}
|
||||
mid, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "seed-map"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
n.reset()
|
||||
|
||||
t.Run("UpdateBinding nonexistent", func(t *testing.T) {
|
||||
bb := binding.Binding{ID: "missing", Matcher: binding.Matcher{Source: "src-other"}}
|
||||
if err := s.UpdateBinding(t.Context(), bb); !errors.Is(err, ErrBindingNotFound) {
|
||||
t.Fatalf("UpdateBinding() error = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
})
|
||||
t.Run("ApproveBinding refuses an armed binding", func(t *testing.T) {
|
||||
if err := s.ApproveBinding(t.Context(), seedID); !errors.Is(err, ErrBindingTransition) {
|
||||
t.Fatalf("ApproveBinding() error = %v, want ErrBindingTransition", err)
|
||||
}
|
||||
})
|
||||
t.Run("InsertBinding overlaps source", func(t *testing.T) {
|
||||
if _, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Matcher: binding.Matcher{Source: "src-seed"},
|
||||
}); !errors.Is(err, ErrBindingOverlap) {
|
||||
t.Fatalf("InsertBinding() error = %v, want ErrBindingOverlap", err)
|
||||
}
|
||||
})
|
||||
t.Run("DeleteBinding nonexistent", func(t *testing.T) {
|
||||
if err := s.DeleteBinding(t.Context(), "missing"); !errors.Is(err, ErrBindingNotFound) {
|
||||
t.Fatalf("DeleteBinding() error = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
})
|
||||
t.Run("UpdateMapping nonexistent", func(t *testing.T) {
|
||||
if err := s.UpdateMapping(t.Context(), mapping.Mapping{ID: "missing"}); !errors.Is(err, ErrMappingNotFound) {
|
||||
t.Fatalf("UpdateMapping() error = %v, want ErrMappingNotFound", err)
|
||||
}
|
||||
})
|
||||
t.Run("DeleteMapping nonexistent", func(t *testing.T) {
|
||||
if err := s.DeleteMapping(t.Context(), "missing"); !errors.Is(err, ErrMappingNotFound) {
|
||||
t.Fatalf("DeleteMapping() error = %v, want ErrMappingNotFound", err)
|
||||
}
|
||||
})
|
||||
_ = mid
|
||||
|
||||
if len(n.got) != 0 {
|
||||
t.Fatalf("failed writes notified %d times: %+v", len(n.got), n.got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNilNotifierIsNoOp pins that a store composed without a notifier keeps
|
||||
// every write path working: the existing constructors and tests never set
|
||||
// one, so a nil-dereference there would break them all.
|
||||
func TestNilNotifierIsNoOp(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "nil-safe",
|
||||
Matcher: binding.Matcher{Source: "src-nil"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding with nil notifier: error = %v", err)
|
||||
}
|
||||
bb, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding() error = %v", err)
|
||||
}
|
||||
bb.Workflow = "escalate"
|
||||
if err := s.UpdateBinding(t.Context(), *bb); err != nil {
|
||||
t.Fatalf("UpdateBinding() error = %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding() error = %v", err)
|
||||
}
|
||||
if err := s.DeleteBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("DeleteBinding() error = %v", err)
|
||||
}
|
||||
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "m"}); err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenHonoursConfigNotify pins the composition surface: the wire-in sets
|
||||
// Config.Notify, not the unexported field.
|
||||
func TestOpenHonoursConfigNotify(t *testing.T) {
|
||||
n := &recordingNotifier{}
|
||||
dir := t.TempDir()
|
||||
s, err := Open(Config{
|
||||
DBPath: t.TempDir() + "/eda.sqlite",
|
||||
DataDir: dir,
|
||||
Notify: n.notify,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
|
||||
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "wired"}); err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
if len(n.got) != 1 {
|
||||
t.Fatalf("store built from Config.Notify notified %d times, want 1: %+v", len(n.got), n.got)
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// OpenTest builds a throwaway store for tests, closed when the test ends.
|
||||
// It mirrors store.OpenTest so a test that needs both stores wires them the
|
||||
// same way.
|
||||
func OpenTest(t *testing.T) *Store {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
s, err := Open(Config{
|
||||
DBPath: filepath.Join(dir, "eda.sqlite"),
|
||||
DataDir: filepath.Join(dir, "pb_data"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("edastore.OpenTest: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return s
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
)
|
||||
|
||||
// isDataDB reports whether PocketBase is asking for its main database, as
|
||||
// opposed to the auxiliary one it keeps for logs and settings.
|
||||
func isDataDB(path string) bool { return filepath.Base(path) == "data.db" }
|
||||
|
||||
// removeDefaultAuthCollection deletes the open-signup "users" collection
|
||||
// PocketBase creates on first bootstrap. This store has no end users, and its
|
||||
// createRule is open, so leaving it would accept unauthenticated writes.
|
||||
func removeDefaultAuthCollection(app core.App) error {
|
||||
c, err := app.FindCollectionByNameOrId("users")
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil
|
||||
case err != nil:
|
||||
return fmt.Errorf("edastore: look up default users collection: %w", err)
|
||||
}
|
||||
if err := app.Delete(c); err != nil {
|
||||
return fmt.Errorf("edastore: remove default users collection: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureCollections creates each collection once and is safe to re-run: the
|
||||
// store bootstraps on every process start.
|
||||
func ensureCollections(app core.App) error {
|
||||
for _, build := range []func(core.App) error{
|
||||
ensureCaptures, ensureMappings, ensureBindings,
|
||||
ensureBindingDispatches, ensurePlaybookDispatches, ensureToolCalls,
|
||||
} {
|
||||
if err := build(app); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// collection returns the named collection, or a new base collection when it
|
||||
// does not exist yet. A lookup that failed for any other reason is an error:
|
||||
// creating a second collection over a live one on a transient failure would be
|
||||
// a schema change made for the wrong reason.
|
||||
func collection(app core.App, name string) (*core.Collection, bool, error) {
|
||||
c, err := app.FindCollectionByNameOrId(name)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return core.NewBaseCollection(name), true, nil
|
||||
case err != nil:
|
||||
return nil, false, fmt.Errorf("edastore: look up collection %q: %w", name, err)
|
||||
}
|
||||
return c, false, nil
|
||||
}
|
||||
|
||||
func ensureCaptures(app core.App) error {
|
||||
c, fresh, err := collection(app, CollCaptures)
|
||||
if err != nil || !fresh {
|
||||
return err
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.TextField{Name: "source", Required: true},
|
||||
&core.TextField{Name: "remote_addr"},
|
||||
&core.TextField{Name: "content_type"},
|
||||
// Headers and body are stored verbatim. Field mapping is designed
|
||||
// against the real payload, so nothing here may be normalised away.
|
||||
&core.JSONField{Name: "headers", MaxSize: 1 << 20},
|
||||
&core.TextField{Name: "body", Max: 1 << 20},
|
||||
&core.BoolField{Name: "authenticated"},
|
||||
// received_at is written by the store, not an autodate field, because
|
||||
// it is also the sort key. PocketBase autodates are millisecond
|
||||
// precision and record ids are random, so two captures arriving in
|
||||
// the same millisecond would order arbitrarily -- the old store got
|
||||
// insertion order free from an autoincrement id. capturedAtLayout is
|
||||
// fixed-width so lexicographic order is chronological order.
|
||||
&core.TextField{Name: "received_at"},
|
||||
)
|
||||
c.AddIndex("idx_captures_source", false, "source, received_at", "")
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
func ensureMappings(app core.App) error {
|
||||
c, fresh, err := collection(app, CollMappings)
|
||||
if err != nil || !fresh {
|
||||
return err
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.TextField{Name: "name", Required: true},
|
||||
&core.TextField{Name: "source_hint"},
|
||||
&core.JSONField{Name: "fields", MaxSize: 1 << 18},
|
||||
&core.AutodateField{Name: "created_at", OnCreate: true},
|
||||
&core.AutodateField{Name: "updated_at", OnCreate: true, OnUpdate: true},
|
||||
)
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
func ensureBindings(app core.App) error {
|
||||
c, fresh, err := collection(app, CollBindings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fresh {
|
||||
return promoteDraftBindings(app)
|
||||
}
|
||||
mappings, err := app.FindCollectionByNameOrId(CollMappings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: bindings need mappings: %w", err)
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.TextField{Name: "name", Required: true},
|
||||
&core.TextField{Name: "source", Required: true},
|
||||
&core.RelationField{Name: "mapping", CollectionId: mappings.Id, MaxSelect: 1},
|
||||
&core.TextField{Name: "workflow"},
|
||||
&core.TextField{Name: "owner"},
|
||||
&core.TextField{Name: "repo"},
|
||||
&core.NumberField{Name: "version"},
|
||||
// status is the domain's lifecycle, not a local vocabulary:
|
||||
// pending_approval -> armed. Only ApproveBinding reaches armed,
|
||||
// which is the epic's approval gate.
|
||||
&core.SelectField{Name: "status", MaxSelect: 1, Values: []string{
|
||||
string(binding.StatusPendingApproval),
|
||||
string(binding.StatusArmed),
|
||||
}},
|
||||
&core.TextField{Name: "secret"},
|
||||
&core.AutodateField{Name: "created_at", OnCreate: true},
|
||||
&core.AutodateField{Name: "updated_at", OnCreate: true, OnUpdate: true},
|
||||
)
|
||||
c.AddIndex("idx_bindings_source_status", false, "source, status", "")
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
// promoteDraftBindings moves bindings an earlier version stored as "draft",
|
||||
// a state nothing could leave without an unrelated edit, to
|
||||
// pending_approval so they can be approved. It is a no-op once none remain.
|
||||
func promoteDraftBindings(app core.App) error {
|
||||
_, err := app.DB().NewQuery("UPDATE " + CollBindings + " SET status = {:pending} WHERE status = 'draft'").
|
||||
Bind(map[string]any{"pending": string(binding.StatusPendingApproval)}).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: promote draft bindings: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureBindingDispatches(app core.App) error {
|
||||
c, fresh, err := collection(app, CollBindingDispatches)
|
||||
if err != nil || !fresh {
|
||||
return err
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.TextField{Name: "binding", Required: true},
|
||||
&core.NumberField{Name: "binding_version"},
|
||||
&core.TextField{Name: "capture", Required: true},
|
||||
// task_id is the seam to the SQLite-owned task tables. It stays a
|
||||
// plain integer id rather than a relation precisely because the task
|
||||
// lifecycle is not migrating.
|
||||
&core.NumberField{Name: "task_id"},
|
||||
&core.AutodateField{Name: "dispatched_at", OnCreate: true},
|
||||
)
|
||||
// At-most-once, enforced by the schema rather than by every caller
|
||||
// remembering INSERT OR IGNORE.
|
||||
c.AddIndex("idx_binding_dispatch_once", true, "binding, capture", "")
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
func ensurePlaybookDispatches(app core.App) error {
|
||||
c, fresh, err := collection(app, CollPlaybookDispatches)
|
||||
if err != nil || !fresh {
|
||||
return err
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.TextField{Name: "playbook_id", Required: true},
|
||||
&core.TextField{Name: "playbook_version", Required: true},
|
||||
&core.TextField{Name: "event_id", Required: true},
|
||||
&core.TextField{Name: "action_id", Required: true},
|
||||
&core.AutodateField{Name: "dispatched_at", OnCreate: true},
|
||||
)
|
||||
c.AddIndex("idx_playbook_dispatch_once", true,
|
||||
"playbook_id, playbook_version, event_id, action_id", "")
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
// ensureToolCalls adds persistence that did not exist before. Tool calls were
|
||||
// only ever written to the on-disk task log (internal/logging), so they could
|
||||
// be read back as text but never queried, filtered or watched. As a collection
|
||||
// they become all three.
|
||||
func ensureToolCalls(app core.App) error {
|
||||
c, fresh, err := collection(app, CollToolCalls)
|
||||
if err != nil || !fresh {
|
||||
return err
|
||||
}
|
||||
c.Fields.Add(
|
||||
&core.NumberField{Name: "task_id"},
|
||||
&core.NumberField{Name: "attempt"},
|
||||
&core.TextField{Name: "tool", Required: true},
|
||||
&core.JSONField{Name: "args", MaxSize: 1 << 18},
|
||||
&core.JSONField{Name: "result", MaxSize: 1 << 18},
|
||||
&core.TextField{Name: "error"},
|
||||
&core.NumberField{Name: "duration_ms"},
|
||||
&core.AutodateField{Name: "called_at", OnCreate: true},
|
||||
)
|
||||
c.AddIndex("idx_tool_calls_task", false, "task_id, attempt", "")
|
||||
return save(app, c)
|
||||
}
|
||||
|
||||
func save(app core.App, c *core.Collection) error {
|
||||
if err := app.Save(c); err != nil {
|
||||
return fmt.Errorf("edastore: create collection %q: %w", c.Name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
package edastore_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
||||
)
|
||||
|
||||
func newStore(t *testing.T) *edastore.Store {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
st, err := edastore.Open(edastore.Config{
|
||||
DBPath: filepath.Join(dir, "eda.sqlite"),
|
||||
DataDir: filepath.Join(dir, "pb_data"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("edastore.Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
return st
|
||||
}
|
||||
|
||||
// TestCollectionsAreCreated pins that every EDA collection exists and is a
|
||||
// base collection, not a view: unlike the read-only operator surface, these
|
||||
// are the authoritative tables and the admin UI is expected to edit them.
|
||||
func TestCollectionsAreCreated(t *testing.T) {
|
||||
st := newStore(t)
|
||||
for _, name := range edastore.Collections() {
|
||||
c, err := st.App().FindCollectionByNameOrId(name)
|
||||
if err != nil {
|
||||
t.Fatalf("collection %q missing: %v", name, err)
|
||||
}
|
||||
if c.IsView() {
|
||||
t.Errorf("collection %q is a view; EDA tables are authoritative and must be writable", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBindingDispatchIsIdempotent pins the at-most-once ledger. The SQLite
|
||||
// implementation got this from INSERT OR IGNORE on a composite primary key
|
||||
// (bindings.go:365); here it must come from a unique index, so a duplicate is
|
||||
// refused by the schema rather than by the caller remembering to ignore it.
|
||||
func TestBindingDispatchIsIdempotent(t *testing.T) {
|
||||
st := newStore(t)
|
||||
if err := st.RecordDispatch(t.Context(), "b1", 1, "c1", 42); err != nil {
|
||||
t.Fatalf("first RecordDispatch() error = %v", err)
|
||||
}
|
||||
// A replay of the same (binding, capture) must be refused, not run again.
|
||||
err := st.RecordDispatch(t.Context(), "b1", 1, "c1", 99)
|
||||
if !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Errorf("second RecordDispatch() error = %v, want ErrAlreadyDispatched: (binding, capture) is an at-most-once ledger", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaybookDispatchIsIdempotent pins the same at-most-once rule on the
|
||||
// four-part playbook key (playbook, version, event, action).
|
||||
func TestPlaybookDispatchIsIdempotent(t *testing.T) {
|
||||
st := newStore(t)
|
||||
if err := st.RecordPlaybookDispatch(t.Context(), "p1", "v1", "e1", "a1"); err != nil {
|
||||
t.Fatalf("first RecordPlaybookDispatch() error = %v", err)
|
||||
}
|
||||
err := st.RecordPlaybookDispatch(t.Context(), "p1", "v1", "e1", "a1")
|
||||
if !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Errorf("second RecordPlaybookDispatch() error = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBindingStartsPendingApproval pins the approval gate from the epic's
|
||||
// acceptance criteria: "A binding cannot go live without explicit human
|
||||
// approval." A new binding awaits that approval and is not live.
|
||||
func TestBindingStartsPendingApproval(t *testing.T) {
|
||||
st := newStore(t)
|
||||
id, err := st.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "sentry"}, Workflow: "implement",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
b, err := st.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding() error = %v", err)
|
||||
}
|
||||
if b.Status != binding.StatusPendingApproval {
|
||||
t.Errorf("new binding status = %q, want %q: a binding must not be live before approval", b.Status, binding.StatusPendingApproval)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApproveRejectsUnknownBinding pins the sentinel the gRPC layer maps.
|
||||
func TestApproveRejectsUnknownBinding(t *testing.T) {
|
||||
st := newStore(t)
|
||||
if err := st.ApproveBinding(t.Context(), "nosuchbinding"); !errors.Is(err, storecontract.ErrBindingNotFound) {
|
||||
t.Errorf("ApproveBinding(unknown) error = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCaptureRoundTripsPayload pins the epic's core requirement: an event
|
||||
// arrives with a payload nobody has modelled yet and must be readable back
|
||||
// verbatim so an operator can map fields against it.
|
||||
func TestCaptureRoundTripsPayload(t *testing.T) {
|
||||
st := newStore(t)
|
||||
body := `{"action":"created","issue":{"number":7,"title":"it broke"}}`
|
||||
if _, err := st.InsertCapture(t.Context(), storecontract.CapturedEvent{
|
||||
Source: "sentry", Body: body, Headers: `{"X-Hook":"1"}`, ContentType: "application/json",
|
||||
}, 0, 0); err != nil {
|
||||
t.Fatalf("InsertCapture() error = %v", err)
|
||||
}
|
||||
list, err := st.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures() error = %v", err)
|
||||
}
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("ListCaptures() returned %d captures, want 1", len(list))
|
||||
}
|
||||
got := list[0]
|
||||
if got.Body != body {
|
||||
t.Errorf("Body round-trip = %q, want %q: the raw payload is what field mapping is designed against", got.Body, body)
|
||||
}
|
||||
}
|
||||
|
||||
var _ = core.Collection{}
|
||||
|
||||
// TestBindingLifecycleIsThreeStates pins draft -> pending_approval -> armed.
|
||||
// The states are the domain's, not this package's: collapsing them (as an
|
||||
// earlier version of this store did) removes the review step while still
|
||||
// looking like an approval gate, which is the worst of both.
|
||||
func TestBindingLifecycle(t *testing.T) {
|
||||
st := newStore(t)
|
||||
id, err := st.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "sentry"}, Workflow: "implement",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
|
||||
// It is created awaiting review.
|
||||
b, err := st.GetBinding(t.Context(), id)
|
||||
if err != nil || b.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("status after insert = %q (err %v), want %q", b.Status, err, binding.StatusPendingApproval)
|
||||
}
|
||||
|
||||
// Approval arms it, and only from pending_approval.
|
||||
if err := st.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding(pending_approval) error = %v", err)
|
||||
}
|
||||
if b, err = st.GetBinding(t.Context(), id); err != nil || b.Status != binding.StatusArmed {
|
||||
t.Fatalf("status after approve = %q (err %v), want %q", b.Status, err, binding.StatusArmed)
|
||||
}
|
||||
if err := st.ApproveBinding(t.Context(), id); !errors.Is(err, storecontract.ErrBindingTransition) {
|
||||
t.Errorf("re-approving an armed binding = %v, want ErrBindingTransition", err)
|
||||
}
|
||||
|
||||
// Only armed bindings dispatch.
|
||||
armed, err := st.ArmedBindingsForSource(t.Context(), "sentry")
|
||||
if err != nil || len(armed) != 1 {
|
||||
t.Fatalf("ArmedBindingsForSource() = %d bindings (err %v), want 1", len(armed), err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOneBindingPerSource pins the overlap guard: a second binding on a
|
||||
// source that already has one is refused, so a capture can never match two.
|
||||
func TestOneBindingPerSource(t *testing.T) {
|
||||
st := newStore(t)
|
||||
mk := func() error {
|
||||
_, err := st.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "sentry"}, Workflow: "implement",
|
||||
})
|
||||
return err
|
||||
}
|
||||
if err := mk(); err != nil {
|
||||
t.Fatalf("first InsertBinding() error = %v", err)
|
||||
}
|
||||
if err := mk(); !errors.Is(err, storecontract.ErrBindingOverlap) {
|
||||
t.Errorf("second InsertBinding() error = %v, want ErrBindingOverlap", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A binding stored as "draft" by an earlier version could never be approved.
|
||||
// Opening the store moves it to pending_approval, so it can be.
|
||||
func TestOpenMovesLegacyDraftBindingsToPendingApproval(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := edastore.Config{DBPath: filepath.Join(dir, "eda.sqlite"), DataDir: filepath.Join(dir, "pb_data")}
|
||||
st, err := edastore.Open(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, err := st.InsertBinding(t.Context(), binding.Binding{Name: "n", Matcher: binding.Matcher{Source: "sentry"}, Workflow: "implement"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.App().DB().NewQuery("UPDATE bindings SET status = 'draft' WHERE id = {:id}").Bind(map[string]any{"id": id}).Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = st.Close()
|
||||
|
||||
st, err = edastore.Open(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
if err := st.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding(legacy draft) = %v, want it approvable after reopen", err)
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
)
|
||||
|
||||
// bindingInput is the small shape the cipher tests need, kept separate from
|
||||
// binding.Binding so a domain field addition does not churn every test.
|
||||
type bindingInput struct {
|
||||
Name string
|
||||
Source string
|
||||
Workflow string
|
||||
Secret string
|
||||
}
|
||||
|
||||
func insertBinding(t *testing.T, s *Store, in bindingInput) string {
|
||||
t.Helper()
|
||||
id, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: in.Name,
|
||||
Matcher: binding.Matcher{Source: in.Source},
|
||||
Workflow: in.Workflow,
|
||||
Secret: in.Secret,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
// ToolCall is one completed tool invocation during an agent stage run, as the
|
||||
// tool_call event projection stores it. The projection adapts the event to the
|
||||
// existing collection (schema.go): a tool's output summary goes in result, a
|
||||
// failure's detail in error. The args and duration_ms fields the schema also
|
||||
// carries are left unwritten -- the tool_call event reports neither -- and a
|
||||
// future producer that has them can start filling them without a schema
|
||||
// change.
|
||||
type ToolCall struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int
|
||||
Tool string
|
||||
Result string
|
||||
Error string
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
// InsertToolCall appends one row to the tool_calls collection. There is no
|
||||
// idempotency here, deliberately: the collection is an append-only transcript
|
||||
// and the schema carries no unique index (the SQLite predecessor had none --
|
||||
// tool calls lived only in the on-disk task log), so one event is one row.
|
||||
//
|
||||
// The notify hook stays silent, matching the task-dispatch tables rather than
|
||||
// the operator-editable ones: tool calls are high-frequency telemetry, not
|
||||
// dashboard activity.
|
||||
func (s *Store) InsertToolCall(_ context.Context, tc ToolCall) error {
|
||||
collection, err := s.app.FindCollectionByNameOrId(CollToolCalls)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := core.NewRecord(collection)
|
||||
r.Set("task_id", tc.TaskID)
|
||||
r.Set("attempt", tc.Attempt)
|
||||
r.Set("tool", tc.Tool)
|
||||
if tc.Result != "" {
|
||||
r.Set("result", tc.Result)
|
||||
}
|
||||
if tc.Error != "" {
|
||||
r.Set("error", tc.Error)
|
||||
}
|
||||
if !tc.CalledAt.IsZero() {
|
||||
// called_at is an autodate field, but an explicitly set date is kept:
|
||||
// the row carries the time the tool call happened, not the time it
|
||||
// was projected.
|
||||
r.Set("called_at", tc.CalledAt)
|
||||
}
|
||||
if err := s.app.Save(r); err != nil {
|
||||
return fmt.Errorf("edastore: insert tool call: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TaskToolCalls returns one task's tool calls in call order.
|
||||
func (s *Store) TaskToolCalls(_ context.Context, taskID int64) ([]ToolCall, error) {
|
||||
records, err := s.app.FindRecordsByFilter(CollToolCalls, "task_id = {:task}", "+called_at", 0, 0,
|
||||
map[string]any{"task": taskID})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list tool calls: %w", err)
|
||||
}
|
||||
out := make([]ToolCall, 0, len(records))
|
||||
for _, r := range records {
|
||||
out = append(out, ToolCall{
|
||||
ID: r.Id,
|
||||
TaskID: r.GetInt64("task_id"),
|
||||
Attempt: r.GetInt("attempt"),
|
||||
Tool: r.GetString("tool"),
|
||||
Result: jsonTextToString(r.GetString("result")),
|
||||
Error: r.GetString("error"),
|
||||
CalledAt: r.GetDateTime("called_at").Time(),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// jsonTextToString unwraps the JSON string a JSONField stores a plain text
|
||||
// summary under, and falls back to the raw text for anything that is not a
|
||||
// JSON string (e.g. an object a future producer stored directly).
|
||||
func jsonTextToString(raw string) string {
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
var decoded string
|
||||
if err := json.Unmarshal([]byte(raw), &decoded); err != nil {
|
||||
return raw
|
||||
}
|
||||
return decoded
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package edastore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
||||
)
|
||||
|
||||
func insertToolCall(t *testing.T, s *edastore.Store, tc edastore.ToolCall) {
|
||||
t.Helper()
|
||||
if err := s.InsertToolCall(t.Context(), tc); err != nil {
|
||||
t.Fatalf("InsertToolCall() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertToolCallRoundTrip pins the write/read pair on the tool_calls
|
||||
// collection: a row written with the fields the tool_call projection provides
|
||||
// reads back unchanged, and the read filters on task_id so one task's calls
|
||||
// never surface in another's.
|
||||
func TestInsertToolCallRoundTrip(t *testing.T) {
|
||||
s := newStore(t)
|
||||
at := time.Date(2026, 9, 23, 1, 2, 3, 0, time.UTC)
|
||||
|
||||
insertToolCall(t, s, edastore.ToolCall{
|
||||
TaskID: 42,
|
||||
Attempt: 2,
|
||||
Tool: "shell",
|
||||
Result: "all checks passed",
|
||||
CalledAt: at,
|
||||
})
|
||||
insertToolCall(t, s, edastore.ToolCall{
|
||||
TaskID: 42,
|
||||
Attempt: 2,
|
||||
Tool: "read_file",
|
||||
Error: "no such file",
|
||||
CalledAt: at.Add(time.Second),
|
||||
})
|
||||
// A different task's call must not leak into the read below.
|
||||
insertToolCall(t, s, edastore.ToolCall{
|
||||
TaskID: 7,
|
||||
Attempt: 1,
|
||||
Tool: "shell",
|
||||
Result: "other task's call",
|
||||
CalledAt: at.Add(2 * time.Second),
|
||||
})
|
||||
|
||||
got, err := s.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows for task 42, want 2: %+v", len(got), got)
|
||||
}
|
||||
|
||||
first, second := got[0], got[1]
|
||||
// Chronological order: the earlier call first.
|
||||
if first.Tool != "shell" || second.Tool != "read_file" {
|
||||
t.Errorf("order = [%q, %q], want [shell, read_file] by called_at", first.Tool, second.Tool)
|
||||
}
|
||||
if first.TaskID != 42 || first.Attempt != 2 {
|
||||
t.Errorf("first row task/attempt = %d/%d, want 42/2", first.TaskID, first.Attempt)
|
||||
}
|
||||
if first.Result != "all checks passed" {
|
||||
t.Errorf("first row result = %q, want the stored summary without JSON quoting", first.Result)
|
||||
}
|
||||
if first.Error != "" {
|
||||
t.Errorf("first row error = %q, want empty: a succeeded call is not an error", first.Error)
|
||||
}
|
||||
if second.Error != "no such file" {
|
||||
t.Errorf("second row error = %q, want the stored failure detail", second.Error)
|
||||
}
|
||||
if second.Result != "" {
|
||||
t.Errorf("second row result = %q, want empty: a failed call has no result", second.Result)
|
||||
}
|
||||
if first.CalledAt.IsZero() {
|
||||
t.Error("first row called_at is zero; the call time must round-trip")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertToolCallIsAnAppendOnlyTranscript pins that the collection is one
|
||||
// row per tool call, not an upsert: repeated calls with the same (task,
|
||||
// attempt, tool) are distinct transcript entries. There is no unique index and
|
||||
// there must not be one -- the SQLite predecessor had none (tool calls lived
|
||||
// only in the on-disk task log), so the schema's non-unique (task_id, attempt)
|
||||
// index stays the only one.
|
||||
func TestInsertToolCallIsAnAppendOnlyTranscript(t *testing.T) {
|
||||
s := newStore(t)
|
||||
at := time.Date(2026, 9, 23, 1, 2, 3, 0, time.UTC)
|
||||
row := edastore.ToolCall{TaskID: 42, Attempt: 3, Tool: "shell", Result: "ok", CalledAt: at}
|
||||
|
||||
insertToolCall(t, s, row)
|
||||
insertToolCall(t, s, row)
|
||||
|
||||
got, err := s.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows, want 2: the transcript is append-only, not a dedup ledger", len(got))
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package releaseupdate
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -13,8 +12,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestUpdateInstallGatewayOnlySkipsRuntimeWork(t *testing.T) {
|
||||
@@ -236,91 +233,6 @@ func TestUpdateWatchdogDefaultHealthURLTargetsTheDaemon(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWatchdogRestoresTaskDatabaseOnDaemonRollback(t *testing.T) {
|
||||
root, err := filepath.Abs(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
work := t.TempDir()
|
||||
binDir, fakeDir := filepath.Join(work, "bin"), filepath.Join(work, "fake-bin")
|
||||
reportPath := filepath.Join(work, "report.json")
|
||||
dbPath, backupPath := filepath.Join(work, "tasks.sqlite"), filepath.Join(work, "tasks.prev.sqlite")
|
||||
if err := os.MkdirAll(binDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, content := range map[string]string{"archied": "new", "archied.prev": "old"} {
|
||||
if err := os.WriteFile(filepath.Join(binDir, name), []byte(content), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for path, version := range map[string]int{dbPath: 2, backupPath: 1} {
|
||||
createSQLiteDatabase(t, path, version)
|
||||
}
|
||||
callsPath := filepath.Join(work, "calls")
|
||||
writeFakeCommand(t, fakeDir, "systemctl", `printf '%s\n' "systemctl $*" >> "$ARCHIE_TEST_CALLS"`)
|
||||
writeFakeCommand(t, fakeDir, "cp", `printf '%s\n' "cp $*" >> "$ARCHIE_TEST_CALLS"; /usr/bin/cp "$@"`)
|
||||
writeFakeCommand(t, fakeDir, "curl", `exit 1`)
|
||||
cmd := exec.CommandContext(t.Context(), filepath.Join(root, "scripts", "archie-update-watchdog"))
|
||||
cmd.Env = append(os.Environ(), "PATH="+fakeDir+":"+os.Getenv("PATH"), "ARCHIE_BIN_DIR="+binDir,
|
||||
"ARCHIE_TEST_CALLS="+callsPath,
|
||||
"ARCHIE_UPDATE_REPORT_PATH="+reportPath, "ARCHIE_UPDATE_HEALTH_TIMEOUT=0", "ARCHIE_UPDATE_COMPONENTS=daemon",
|
||||
"ARCHIE_UPDATE_PREVIOUS_GATEWAY=1.19.10", "ARCHIE_UPDATE_INSTALLED_GATEWAY=1.20.0",
|
||||
"ARCHIE_TASK_DB_PATH="+dbPath, "ARCHIE_TASK_DB_BACKUP="+backupPath)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("watchdog failed: %v\n%s", err, output)
|
||||
}
|
||||
db, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
var version int
|
||||
if err := db.QueryRowContext(t.Context(), "PRAGMA user_version").Scan(&version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if version != 1 {
|
||||
t.Fatalf("restored schema version = %d, want 1", version)
|
||||
}
|
||||
calls, err := os.ReadFile(callsPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
callLines := strings.Split(strings.TrimSpace(string(calls)), "\n")
|
||||
stopIndex, copyIndex, startIndex := -1, -1, -1
|
||||
for i, line := range callLines {
|
||||
switch line {
|
||||
case "systemctl --user stop archied.service":
|
||||
stopIndex = i
|
||||
case "systemctl --user restart archied.service":
|
||||
startIndex = i
|
||||
}
|
||||
if strings.HasPrefix(line, "cp ") && strings.Contains(line, "tasks.prev.sqlite") {
|
||||
copyIndex = i
|
||||
}
|
||||
}
|
||||
if stopIndex < 0 || copyIndex < 0 || stopIndex > copyIndex {
|
||||
t.Fatalf("rollback must stop service before restoring database; calls = %q", calls)
|
||||
}
|
||||
if startIndex < 0 || startIndex <= copyIndex {
|
||||
t.Fatalf("rollback must restart service after restoring database; calls = %q", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func createSQLiteDatabase(t *testing.T, path string, version int) {
|
||||
t.Helper()
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
t.Fatalf("open database %s: %v", path, err)
|
||||
}
|
||||
defer db.Close()
|
||||
if _, err := db.ExecContext(t.Context(), "PRAGMA user_version = "+strconv.Itoa(version)); err != nil {
|
||||
t.Fatalf("set database version for %s: %v", path, err)
|
||||
}
|
||||
if _, err := db.ExecContext(t.Context(), "CREATE TABLE tasks (id INTEGER)"); err != nil {
|
||||
t.Fatalf("create tasks table in %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func runUpdateInstallScript(t *testing.T, environment map[string]string) (Result, []string) {
|
||||
t.Helper()
|
||||
result, calls, _ := runUpdateInstall(t, environment, false)
|
||||
@@ -406,13 +318,11 @@ esac
|
||||
`)
|
||||
writeFakeCommand(t, fakeDir, "go", `
|
||||
printf '%s\n' "go $*" >> "$ARCHIE_TEST_CALLS"
|
||||
# The pre-flight task-database backup runs through the release's own
|
||||
# The pre-flight database snapshot runs through the release's own
|
||||
# archie-state-store subcommand, so a release whose recovery path cannot run
|
||||
# must refuse the update.
|
||||
if [ "$1" = "run" ]; then
|
||||
[ "${ARCHIE_TEST_GO_FAILS_RUN:-}" = "1" ] && exit 1
|
||||
# The PostgreSQL snapshot is the backup run that names no -db file.
|
||||
[ "${ARCHIE_TEST_GO_FAILS_SNAPSHOT:-}" = "1" ] && [[ " $* " != *" -db "* ]] && exit 1
|
||||
[ "${ARCHIE_TEST_GO_FAILS_SNAPSHOT:-}" = "1" ] && exit 1
|
||||
exit 0
|
||||
fi
|
||||
out=""
|
||||
@@ -471,22 +381,6 @@ printf '%s\n' "install $*" >> "$ARCHIE_TEST_CALLS"
|
||||
printf '%s\n' "cp $*" >> "$ARCHIE_TEST_CALLS"
|
||||
/usr/bin/cp "$@"
|
||||
`)
|
||||
writeFakeCommand(t, fakeDir, "sqlite3", `
|
||||
printf '%s\n' "sqlite3 $*" >> "$ARCHIE_TEST_CALLS"
|
||||
[ "${ARCHIE_TEST_SQLITE3_FAILS:-}" = "1" ] && exit 1
|
||||
# Reproduce ".backup '<path>'" by writing a plausible backup beside the
|
||||
# source, so the watchdog's existence check behaves as it would in production.
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
.backup*) target="$(printf '%s' "$arg" | sed "s/^\.backup[[:space:]]*//; s/^'//; s/'$//")" ;;
|
||||
esac
|
||||
done
|
||||
if [ -n "${target:-}" ]; then
|
||||
mkdir -p "$(dirname "$target")"
|
||||
printf 'backup\n' > "$target"
|
||||
fi
|
||||
`)
|
||||
|
||||
// The production adapter prepends /usr/local/go/bin. Run an otherwise
|
||||
// byte-for-byte copy with the harness PATH left first so fake go records
|
||||
// build intent without compiling or touching Docker/systemd.
|
||||
@@ -877,105 +771,6 @@ func TestUpdateWatchdogCyclesEveryUnitInDependencyOrder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The updater must back up the database the daemon actually uses, which is
|
||||
// `<db_path>-tasks.sqlite` (taskDBPath in internal/app/archied/main.go), not
|
||||
// the configured path itself. The configured path may be a zero-byte
|
||||
// placeholder while the real store sits beside it -- so backing the literal
|
||||
// value up produces an empty backup, and a rollback then restores that empty
|
||||
// file over the database and deletes its -wal/-shm. A rollback that destroys
|
||||
// the task store is worse than no rollback.
|
||||
//
|
||||
// The snapshot is taken by the release's own recovery subcommand rather than
|
||||
// by hand-rolled shell, so the installer and an operator run the same code and
|
||||
// the two cannot drift apart.
|
||||
func TestUpdateInstallBacksUpTheSiblingTaskDatabase(t *testing.T) {
|
||||
work := t.TempDir()
|
||||
configured := filepath.Join(work, "archie.db")
|
||||
realStore := configured + "-tasks.sqlite"
|
||||
if err := os.WriteFile(configured, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(realStore, []byte("real task data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backup := filepath.Join(work, "backup.sqlite")
|
||||
|
||||
_, calls, _ := runUpdateInstallWithConfig(t, map[string]string{
|
||||
"ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.22.0",
|
||||
"ARCHIE_UPDATE_DAEMON_VERSION": "1.23.0",
|
||||
"ARCHIE_UPDATE_AGENT_PREVIOUS": "1.21.0",
|
||||
"ARCHIE_TASK_DB_BACKUP": backup,
|
||||
}, "db_path = \""+configured+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", false)
|
||||
|
||||
assertCallContains(t, calls, "go run ./cmd/archie-state-store backup", "-db "+realStore, "-out "+backup)
|
||||
assertCallAbsent(t, calls, "sqlite3")
|
||||
assertCallContains(t, calls, "systemd-run", "--setenv=ARCHIE_TASK_DB_PATH="+realStore)
|
||||
// The zero-byte placeholder is not the store, and must never be the thing
|
||||
// handed to the watchdog as ARCHIE_TASK_DB_PATH.
|
||||
assertCallAbsent(t, calls, "--setenv=ARCHIE_TASK_DB_PATH="+configured+" ")
|
||||
}
|
||||
|
||||
// db_path can also appear under a TOML table -- the retired [indexing] section
|
||||
// once owned its own database there, and a future section could do the same.
|
||||
// Reading the first match in the file can select an unrelated database, so the
|
||||
// value must come from the daemon's own top-level key.
|
||||
func TestUpdateInstallIgnoresDbPathFromOtherSections(t *testing.T) {
|
||||
work := t.TempDir()
|
||||
configured := filepath.Join(work, "archie.db")
|
||||
realStore := configured + "-tasks.sqlite"
|
||||
indexStore := filepath.Join(work, "workspace-indexes.db")
|
||||
for path, content := range map[string]string{realStore: "real task data", indexStore: "index data"} {
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
backup := filepath.Join(work, "backup.sqlite")
|
||||
|
||||
_, calls, _ := runUpdateInstallWithConfig(t, map[string]string{
|
||||
"ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.22.0",
|
||||
"ARCHIE_UPDATE_DAEMON_VERSION": "1.23.0",
|
||||
"ARCHIE_UPDATE_AGENT_PREVIOUS": "1.21.0",
|
||||
"ARCHIE_TASK_DB_BACKUP": backup,
|
||||
}, "db_path = \""+configured+"\"\n\n[indexing]\ndb_path = \""+indexStore+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", false)
|
||||
|
||||
assertCallContains(t, calls, "go run ./cmd/archie-state-store backup", "-db "+realStore)
|
||||
for _, call := range calls {
|
||||
if strings.Contains(call, "go run ./cmd/archie-state-store backup") && strings.Contains(call, indexStore) {
|
||||
t.Errorf("backed up the other section's database %s: %q", indexStore, call)
|
||||
}
|
||||
}
|
||||
assertCallAbsent(t, calls, "--setenv=ARCHIE_TASK_DB_PATH="+indexStore)
|
||||
}
|
||||
|
||||
// A store that cannot be backed up must fail the update before anything is
|
||||
// replaced. Installing with no usable rollback is how a failed update becomes
|
||||
// a destroyed task store.
|
||||
func TestUpdateInstallRefusesWhenTaskDatabaseCannotBeBackedUp(t *testing.T) {
|
||||
work := t.TempDir()
|
||||
configured := filepath.Join(work, "archie.db")
|
||||
if err := os.WriteFile(configured+"-tasks.sqlite", []byte("data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, calls, output := runUpdateInstallWithConfig(t, map[string]string{
|
||||
"ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.22.0",
|
||||
"ARCHIE_UPDATE_DAEMON_VERSION": "1.23.0",
|
||||
"ARCHIE_UPDATE_AGENT_PREVIOUS": "1.21.0",
|
||||
"ARCHIE_TEST_GO_FAILS_RUN": "1",
|
||||
}, "db_path = \""+configured+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", true)
|
||||
|
||||
if !strings.Contains(output, "task database") {
|
||||
t.Errorf("refusal must name the task database; output = %q", output)
|
||||
}
|
||||
// Nothing of the release may be placed on the host, and no backup of the
|
||||
// failure path may be attempted either.
|
||||
for _, call := range calls {
|
||||
if strings.Contains(call, "install -m755") && !strings.Contains(call, "/scripts/") {
|
||||
t.Errorf("refused update still installed %q", call)
|
||||
}
|
||||
}
|
||||
assertCallAbsent(t, calls, "arhied.prev")
|
||||
}
|
||||
|
||||
// The sidecar is the only record of the managed agent version, and
|
||||
// archie-update-check refuses anything but a bare release version, so a stray
|
||||
// escape in the write leaves the installed agent reported as unknown forever.
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const applyStatusSchema = `
|
||||
CREATE TABLE IF NOT EXISTS apply_status (
|
||||
process TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
applied_version INTEGER NOT NULL DEFAULT 0,
|
||||
error TEXT NOT NULL DEFAULT '',
|
||||
reported_at TEXT NOT NULL,
|
||||
PRIMARY KEY (process, kind)
|
||||
);
|
||||
`
|
||||
|
||||
// applyStatusTimeLayout matches the rest of the store's timestamp columns.
|
||||
const applyStatusTimeLayout = time.RFC3339
|
||||
|
||||
// PutApplyStatus records what one process applied for one resource kind. The
|
||||
// key is (process, kind) and a report replaces its own row: a process
|
||||
// re-stamps on an interval, and a history of re-stamps is not something any
|
||||
// reader wants.
|
||||
func (s *Store) PutApplyStatus(ctx context.Context, status ApplyStatus) error {
|
||||
reported := status.ReportedAt
|
||||
if reported.IsZero() {
|
||||
reported = time.Now()
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO apply_status (process, kind, applied_version, error, reported_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(process, kind) DO UPDATE SET
|
||||
applied_version=excluded.applied_version,
|
||||
error=excluded.error,
|
||||
reported_at=excluded.reported_at`,
|
||||
status.Process, status.Kind, status.AppliedVersion, status.Error,
|
||||
reported.UTC().Format(applyStatusTimeLayout))
|
||||
if err != nil {
|
||||
return fmt.Errorf("store: put apply status: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListApplyStatus returns every process's report. An empty slice is the
|
||||
// normal state of a store whose processes have not booted yet, not an error.
|
||||
func (s *Store) ListApplyStatus(ctx context.Context) ([]ApplyStatus, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list apply status: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
statuses := []ApplyStatus{}
|
||||
for rows.Next() {
|
||||
var (
|
||||
status ApplyStatus
|
||||
reported string
|
||||
)
|
||||
if err := rows.Scan(&status.Process, &status.Kind, &status.AppliedVersion, &status.Error, &reported); err != nil {
|
||||
return nil, fmt.Errorf("store: scan apply status: %w", err)
|
||||
}
|
||||
status.ReportedAt, err = time.Parse(applyStatusTimeLayout, reported)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: parse apply status time: %w", err)
|
||||
}
|
||||
statuses = append(statuses, status)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("store: list apply status: %w", err)
|
||||
}
|
||||
return statuses, nil
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestApplyStatusReplacesPerProcessAndKind pins the key: a process re-stamping
|
||||
// its report must overwrite its own row, not append one, or the settings page
|
||||
// accumulates a row per report and cannot say which is current.
|
||||
func TestApplyStatusReplacesPerProcessAndKind(t *testing.T) {
|
||||
s := openTest(t)
|
||||
first := time.Date(2026, 9, 21, 10, 0, 0, 0, time.UTC)
|
||||
|
||||
for _, status := range []ApplyStatus{
|
||||
{Process: "archied", Kind: "model-role-assignments", AppliedVersion: 3, ReportedAt: first},
|
||||
{Process: "archied", Kind: "repository-policies", AppliedVersion: 5, ReportedAt: first},
|
||||
{Process: "archie-gateway", Kind: "model-role-assignments", AppliedVersion: 2, ReportedAt: first},
|
||||
} {
|
||||
if err := s.PutApplyStatus(t.Context(), status); err != nil {
|
||||
t.Fatalf("PutApplyStatus(%s/%s): %v", status.Process, status.Kind, err)
|
||||
}
|
||||
}
|
||||
|
||||
restamp := first.Add(30 * time.Second)
|
||||
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
||||
Process: "archied", Kind: "model-role-assignments", AppliedVersion: 4, ReportedAt: restamp,
|
||||
}); err != nil {
|
||||
t.Fatalf("re-stamp: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.ListApplyStatus(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("ListApplyStatus: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("ListApplyStatus returned %d records, want 3: %+v", len(got), got)
|
||||
}
|
||||
for _, record := range got {
|
||||
if record.Process != "archied" || record.Kind != "model-role-assignments" {
|
||||
continue
|
||||
}
|
||||
if record.AppliedVersion != 4 {
|
||||
t.Errorf("AppliedVersion = %d, want the re-stamped 4", record.AppliedVersion)
|
||||
}
|
||||
if !record.ReportedAt.Equal(restamp) {
|
||||
t.Errorf("ReportedAt = %v, want the re-stamped %v", record.ReportedAt, restamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyStatusKeepsAppliedVersionWhenReportingAnError pins that a rejected
|
||||
// edit does not erase the fact that an older version is still live.
|
||||
func TestApplyStatusKeepsAppliedVersionWhenReportingAnError(t *testing.T) {
|
||||
s := openTest(t)
|
||||
at := time.Date(2026, 9, 21, 10, 0, 0, 0, time.UTC)
|
||||
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
||||
Process: "archied", Kind: "tool-settings", AppliedVersion: 7, ReportedAt: at,
|
||||
}); err != nil {
|
||||
t.Fatalf("PutApplyStatus: %v", err)
|
||||
}
|
||||
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
||||
Process: "archied", Kind: "tool-settings", AppliedVersion: 7,
|
||||
Error: "validate database settings: bad policy", ReportedAt: at.Add(time.Minute),
|
||||
}); err != nil {
|
||||
t.Fatalf("PutApplyStatus with error: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.ListApplyStatus(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("ListApplyStatus: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("ListApplyStatus returned %d records, want 1", len(got))
|
||||
}
|
||||
if got[0].AppliedVersion != 7 || got[0].Error == "" {
|
||||
t.Fatalf("record = %+v, want version 7 still live alongside the error", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
// TestListApplyStatusEmptyIsNotAnError pins the list convention: nothing has
|
||||
// reported yet is the normal state of a store whose processes have not booted,
|
||||
// and it is an empty slice rather than an error or a found=false.
|
||||
func TestListApplyStatusEmptyIsNotAnError(t *testing.T) {
|
||||
got, err := openTest(t).ListApplyStatus(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("ListApplyStatus on an empty store: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("ListApplyStatus returned %d records, want none", len(got))
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const channelStatusSchema = `
|
||||
CREATE TABLE IF NOT EXISTS channel_status (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
state TEXT NOT NULL DEFAULT '',
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
configured INTEGER NOT NULL DEFAULT 0,
|
||||
reload_supported INTEGER NOT NULL DEFAULT 0,
|
||||
observed_at TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
// PutChannelStatus records the reporting process's whole set, in one
|
||||
// transaction: rows it no longer reports are deleted, so a channel that stopped
|
||||
// or was removed does not linger as a stale "running" the dashboard would show
|
||||
// indefinitely.
|
||||
func (s *Store) PutChannelStatus(ctx context.Context, channels []ChannelStatus) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("store: put channel status: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
reported := make([]any, 0, len(channels)+1)
|
||||
placeholders := make([]byte, 0, len(channels)*2)
|
||||
for index, channel := range channels {
|
||||
if channel.ID == "" {
|
||||
return fmt.Errorf("store: put channel status: a channel needs an id")
|
||||
}
|
||||
if index > 0 {
|
||||
placeholders = append(placeholders, ',')
|
||||
}
|
||||
placeholders = append(placeholders, '?')
|
||||
reported = append(reported, channel.ID)
|
||||
|
||||
observed := channel.ObservedAt
|
||||
if observed.IsZero() {
|
||||
observed = time.Now()
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO channel_status (id, name, state, detail, configured, reload_supported, observed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
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`,
|
||||
channel.ID, channel.Name, channel.State, channel.Detail,
|
||||
channel.Configured, channel.ReloadSupported, observed.UTC().Format(configSnapshotTimeLayout)); err != nil {
|
||||
return fmt.Errorf("store: put channel status %s: %w", channel.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty report is a real report -- the process hosts no channels -- and it
|
||||
// clears the table rather than leaving the previous set standing.
|
||||
query := `DELETE FROM channel_status`
|
||||
if len(reported) > 0 {
|
||||
query += ` WHERE id NOT IN (` + string(placeholders) + `)`
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, query, reported...); err != nil {
|
||||
return fmt.Errorf("store: put channel status: prune: %w", err)
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ChannelStatus returns every reported channel, ordered by id so a reader and a
|
||||
// test see the same order twice.
|
||||
func (s *Store) ChannelStatus(ctx context.Context) ([]ChannelStatus, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, name, state, detail, configured, reload_supported, observed_at
|
||||
FROM channel_status ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list channel status: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var channels []ChannelStatus
|
||||
for rows.Next() {
|
||||
var (
|
||||
channel ChannelStatus
|
||||
observed string
|
||||
)
|
||||
if err := rows.Scan(&channel.ID, &channel.Name, &channel.State, &channel.Detail,
|
||||
&channel.Configured, &channel.ReloadSupported, &observed); err != nil {
|
||||
return nil, fmt.Errorf("store: scan channel status: %w", err)
|
||||
}
|
||||
if at, err := time.Parse(configSnapshotTimeLayout, observed); err == nil {
|
||||
channel.ObservedAt = at
|
||||
}
|
||||
channels = append(channels, channel)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("store: list channel status: %w", err)
|
||||
}
|
||||
return channels, nil
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestChannelStatusReplacesTheReportedSet: the process hosting the channels owns
|
||||
// the set it reports, so a channel it stops reporting must disappear. Leaving it
|
||||
// behind would show a stale "running" for a channel that is gone, which is worse
|
||||
// than showing nothing.
|
||||
func TestChannelStatusReplacesTheReportedSet(t *testing.T) {
|
||||
st := openTest(t)
|
||||
ctx := t.Context()
|
||||
|
||||
if rows, err := st.ChannelStatus(ctx); err != nil || len(rows) != 0 {
|
||||
t.Fatalf("ChannelStatus on an empty store = (%+v, %v), want (none, nil)", rows, err)
|
||||
}
|
||||
|
||||
observed := time.Date(2026, 9, 22, 8, 0, 0, 0, time.UTC)
|
||||
first := []ChannelStatus{
|
||||
{ID: "telegram", Name: "telegram", State: "running", Configured: true, ReloadSupported: true, ObservedAt: observed},
|
||||
{ID: "webhook", Name: "webhook", State: "failed", Detail: "listen: address in use", Configured: true, ObservedAt: observed},
|
||||
}
|
||||
if err := st.PutChannelStatus(ctx, first); err != nil {
|
||||
t.Fatalf("PutChannelStatus: %v", err)
|
||||
}
|
||||
rows, err := st.ChannelStatus(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ChannelStatus: %v", err)
|
||||
}
|
||||
if len(rows) != 2 {
|
||||
t.Fatalf("rows = %+v, want both channels", rows)
|
||||
}
|
||||
// Ordered by id, so a reader and this test see the same list twice.
|
||||
if rows[0].ID != "telegram" || rows[1].ID != "webhook" {
|
||||
t.Errorf("rows = %+v, want telegram then webhook", rows)
|
||||
}
|
||||
if rows[1].Detail != "listen: address in use" || rows[1].State != "failed" {
|
||||
t.Errorf("webhook row = %+v, want the failure it reported", rows[1])
|
||||
}
|
||||
if !rows[0].ReloadSupported || !rows[0].Configured {
|
||||
t.Errorf("telegram row = %+v, want its declared capabilities", rows[0])
|
||||
}
|
||||
if !rows[0].ObservedAt.Equal(observed) {
|
||||
t.Errorf("ObservedAt = %v, want %v", rows[0].ObservedAt, observed)
|
||||
}
|
||||
|
||||
// The reporter no longer mentions webhook: its row goes.
|
||||
if err := st.PutChannelStatus(ctx, first[:1]); err != nil {
|
||||
t.Fatalf("PutChannelStatus: %v", err)
|
||||
}
|
||||
rows, err = st.ChannelStatus(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ChannelStatus: %v", err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].ID != "telegram" {
|
||||
t.Fatalf("rows = %+v, want only telegram after the shorter report", rows)
|
||||
}
|
||||
|
||||
// An empty report is a report: a process that hosts no channels clears the set
|
||||
// rather than leaving the previous one standing.
|
||||
if err := st.PutChannelStatus(ctx, nil); err != nil {
|
||||
t.Fatalf("PutChannelStatus(empty): %v", err)
|
||||
}
|
||||
if rows, err := st.ChannelStatus(ctx); err != nil || len(rows) != 0 {
|
||||
t.Fatalf("rows after an empty report = (%+v, %v), want none", rows, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelStatusRequiresAnID: a row without an id cannot be replaced or
|
||||
// removed by a later report, so it is refused at the producer rather than
|
||||
// inserted as an orphan the reporter can never clear.
|
||||
func TestChannelStatusRequiresAnID(t *testing.T) {
|
||||
st := openTest(t)
|
||||
if err := st.PutChannelStatus(t.Context(), []ChannelStatus{{State: "running"}}); err == nil {
|
||||
t.Fatal("PutChannelStatus with no id = nil, want a refusal")
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const configSnapshotSchema = `
|
||||
CREATE TABLE IF NOT EXISTS config_snapshot (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
schema TEXT NOT NULL DEFAULT '',
|
||||
document TEXT NOT NULL DEFAULT '',
|
||||
published_at TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
// configSnapshotTimeLayout follows the rest of the store's timestamp columns:
|
||||
// fixed-width, so string and chronological order agree.
|
||||
const configSnapshotTimeLayout = time.RFC3339
|
||||
|
||||
// PutConfigSnapshot replaces the published snapshot. One running
|
||||
// configuration means one row, so a republish overwrites rather than
|
||||
// appending a history nothing reads.
|
||||
func (s *Store) PutConfigSnapshot(ctx context.Context, snapshot ConfigSnapshot) error {
|
||||
published := snapshot.PublishedAt
|
||||
if published.IsZero() {
|
||||
published = time.Now()
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO config_snapshot (id, schema, document, published_at)
|
||||
VALUES (1, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
schema=excluded.schema,
|
||||
document=excluded.document,
|
||||
published_at=excluded.published_at`,
|
||||
snapshot.Schema, string(snapshot.Document), published.UTC().Format(configSnapshotTimeLayout))
|
||||
if 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 -- the normal state of a store
|
||||
// whose daemon has not booted yet.
|
||||
func (s *Store) ConfigSnapshot(ctx context.Context) (ConfigSnapshot, bool, error) {
|
||||
var (
|
||||
snapshot ConfigSnapshot
|
||||
document string
|
||||
published string
|
||||
)
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT schema, document, published_at FROM config_snapshot WHERE id = 1`).
|
||||
Scan(&snapshot.Schema, &document, &published)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ConfigSnapshot{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return ConfigSnapshot{}, false, fmt.Errorf("store: read config snapshot: %w", err)
|
||||
}
|
||||
snapshot.Document = []byte(document)
|
||||
if at, err := time.Parse(configSnapshotTimeLayout, published); err == nil {
|
||||
snapshot.PublishedAt = at
|
||||
}
|
||||
return snapshot, true, nil
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestConfigSnapshotRoundTrip: the daemon publishes the dashboard's
|
||||
// configuration projection here and the UI process reads it, so the document
|
||||
// has to come back byte-identical and the newest publish has to win. There is
|
||||
// one running configuration, so there is one row.
|
||||
func TestConfigSnapshotRoundTrip(t *testing.T) {
|
||||
st := openTest(t)
|
||||
ctx := t.Context()
|
||||
|
||||
if _, found, err := st.ConfigSnapshot(ctx); err != nil || found {
|
||||
t.Fatalf("ConfigSnapshot on an empty store = (found %v, %v), want (false, nil)", found, err)
|
||||
}
|
||||
|
||||
first := ConfigSnapshot{
|
||||
Schema: "webui.ConfigView/1",
|
||||
Document: []byte(`{"identity":{"bot_user":"archie"}}`),
|
||||
PublishedAt: time.Date(2026, 9, 9, 10, 0, 0, 0, time.UTC),
|
||||
}
|
||||
if err := st.PutConfigSnapshot(ctx, first); err != nil {
|
||||
t.Fatalf("PutConfigSnapshot: %v", err)
|
||||
}
|
||||
|
||||
got, found, err := st.ConfigSnapshot(ctx)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("ConfigSnapshot = (found %v, %v), want it stored", found, err)
|
||||
}
|
||||
if got.Schema != first.Schema || string(got.Document) != string(first.Document) {
|
||||
t.Fatalf("snapshot = %+v, want %+v", got, first)
|
||||
}
|
||||
if !got.PublishedAt.Equal(first.PublishedAt) {
|
||||
t.Fatalf("published at = %v, want %v", got.PublishedAt, first.PublishedAt)
|
||||
}
|
||||
|
||||
second := ConfigSnapshot{
|
||||
Schema: "webui.ConfigView/1",
|
||||
Document: []byte(`{"identity":{"bot_user":"archie-2"}}`),
|
||||
PublishedAt: first.PublishedAt.Add(time.Hour),
|
||||
}
|
||||
if err := st.PutConfigSnapshot(ctx, second); err != nil {
|
||||
t.Fatalf("republish: %v", err)
|
||||
}
|
||||
got, found, err = st.ConfigSnapshot(ctx)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("ConfigSnapshot after republish = (found %v, %v)", found, err)
|
||||
}
|
||||
if string(got.Document) != string(second.Document) {
|
||||
t.Fatalf("document = %s, want the republished %s", got.Document, second.Document)
|
||||
}
|
||||
|
||||
var rows int
|
||||
if err := st.db.QueryRowContext(ctx, `SELECT count(*) FROM config_snapshot`).Scan(&rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rows != 1 {
|
||||
t.Fatalf("config_snapshot holds %d rows, want exactly 1: republishing replaces, it does not accumulate", rows)
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
const eventsSchema = `
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
at TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
task_id INTEGER NOT NULL DEFAULT 0,
|
||||
repo TEXT NOT NULL DEFAULT '',
|
||||
issue INTEGER NOT NULL DEFAULT 0,
|
||||
workflow TEXT NOT NULL DEFAULT '',
|
||||
stage TEXT NOT NULL DEFAULT '',
|
||||
attempt INTEGER NOT NULL DEFAULT 0,
|
||||
actor_id TEXT NOT NULL DEFAULT '',
|
||||
actor_kind TEXT NOT NULL DEFAULT '',
|
||||
principal_id TEXT NOT NULL DEFAULT '',
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
data TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_events_task ON events(task_id, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_events_kind ON events(kind, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_events_at ON events(at, id);
|
||||
`
|
||||
|
||||
// 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 insertEvent(ctx, s.db, e)
|
||||
}
|
||||
|
||||
type eventExecer interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
}
|
||||
|
||||
func insertEvent(ctx context.Context, execer eventExecer, 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())
|
||||
}
|
||||
res, err := execer.ExecContext(ctx, `
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
e.At.UTC().Format(storecontract.EventCursorLayout), e.Kind, e.TaskID, e.Repo, e.Issue,
|
||||
e.Workflow, e.Stage, e.Attempt, e.ActorID, e.ActorKind, e.PrincipalID,
|
||||
clip(e.Detail, 4000), string(data))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) {
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var e events.Event
|
||||
var at, data string
|
||||
if err := rows.Scan(&e.ID, &at, &e.Kind, &e.TaskID, &e.Repo, &e.Issue,
|
||||
&e.Workflow, &e.Stage, &e.Attempt, &e.ActorID, &e.ActorKind,
|
||||
&e.PrincipalID, &e.Detail, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.At = parseEventAt(at)
|
||||
_ = json.Unmarshal([]byte(data), &e.Data)
|
||||
out = append(out, e)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
const eventCols = "id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data"
|
||||
|
||||
// EventsSince returns up to limit events after the opaque cursor, oldest
|
||||
// first -- SSE catch-up and the live feed. The cursor carries the total
|
||||
// order (at, id): at is the fixed-width sort key and id only breaks ties, so
|
||||
// a non-monotonic id scheme can neither skip nor repeat an event. 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 {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT `+eventCols+` FROM events ORDER BY at, id LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanEvents(rows)
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT `+eventCols+` FROM events WHERE at > ? OR (at = ? AND id > ?) ORDER BY at, id LIMIT ?`,
|
||||
at, at, id, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanEvents(rows)
|
||||
}
|
||||
|
||||
// parseEventAt reads the stored timestamp, tolerating both the fixed-width
|
||||
// layout new rows are written in and the RFC3339Nano form older rows carried
|
||||
// before the migration normalised them.
|
||||
func parseEventAt(raw string) time.Time {
|
||||
for _, layout := range []string{storecontract.EventCursorLayout, time.RFC3339Nano, time.RFC3339} {
|
||||
if t, err := time.Parse(layout, raw); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// TaskEvents returns a task's full timeline, oldest first.
|
||||
func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT `+eventCols+` FROM events WHERE task_id = ? ORDER BY id`, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanEvents(rows)
|
||||
}
|
||||
|
||||
// Tasks returns all tasks, newest first (dashboard listing).
|
||||
func (s *Store) Tasks(ctx context.Context, limit int) (tasks []workflow.Task, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
|
||||
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version
|
||||
FROM tasks ORDER BY updated_at DESC LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var t workflow.Task
|
||||
// Plan and Source are read by the dashboard: Plan gates the
|
||||
// "Decision required" panel on a waiting_human task, and Source
|
||||
// decides whether an issue link is rendered at all -- a chat task's
|
||||
// issue number is synthetic and links nowhere.
|
||||
if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title,
|
||||
&t.Status, &t.Workflow, &t.Stage, &t.PRNumber, &t.TokensUsed,
|
||||
&t.Iterations, &t.Attempt, &t.ParkReason, &t.RetryCount,
|
||||
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt},
|
||||
&t.Plan, &t.Source, &t.Identity, &t.BindingID, &t.BindingVersion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks = append(tasks, t)
|
||||
}
|
||||
return tasks, rows.Err()
|
||||
}
|
||||
|
||||
// StatusCounts returns task counts by status.
|
||||
func (s *Store) StatusCounts(ctx context.Context) (counts map[string]int, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT status, COUNT(*) FROM tasks GROUP BY status`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
counts = map[string]int{}
|
||||
for rows.Next() {
|
||||
var st string
|
||||
var n int
|
||||
if err := rows.Scan(&st, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts[st] = n
|
||||
}
|
||||
return counts, rows.Err()
|
||||
}
|
||||
|
||||
// WorkflowStats aggregates outcomes and spend per workflow -- the
|
||||
// core "is archie getting better per dollar" table.
|
||||
func (s *Store) WorkflowStats(ctx context.Context) (stats []WorkflowStat, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT workflow, COUNT(*),
|
||||
SUM(status='merged'), SUM(status='pr_open'), SUM(status='parked'),
|
||||
CAST(AVG(tokens_used) AS INTEGER), AVG(iterations), SUM(tokens_used)
|
||||
FROM tasks WHERE workflow != '' GROUP BY workflow ORDER BY COUNT(*) DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var w WorkflowStat
|
||||
if err := rows.Scan(&w.Workflow, &w.Runs, &w.Merged, &w.PROpen, &w.Parked,
|
||||
&w.AvgTokens, &w.AvgSteps, &w.TotalToken); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats = append(stats, w)
|
||||
}
|
||||
return stats, rows.Err()
|
||||
}
|
||||
|
||||
// StageStats aggregates stage_finish events -- where does time go, and
|
||||
// which stages fail.
|
||||
func (s *Store) StageStats(ctx context.Context) (stats []StageStat, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT workflow, stage, COUNT(*),
|
||||
CAST(AVG(json_extract(data,'$.duration_ms')) AS INTEGER),
|
||||
SUM(CASE WHEN json_extract(data,'$.error') IS NOT NULL THEN 1 ELSE 0 END)
|
||||
FROM events WHERE kind = 'stage_finish'
|
||||
GROUP BY workflow, stage ORDER BY workflow, stage`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var st StageStat
|
||||
var avg sql.NullInt64
|
||||
if err := rows.Scan(&st.Workflow, &st.Stage, &st.Runs, &avg, &st.Errors); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st.AvgMs = int(avg.Int64)
|
||||
stats = append(stats, st)
|
||||
}
|
||||
return stats, rows.Err()
|
||||
}
|
||||
|
||||
// TokensByDay sums task token spend per UTC day. Agent-finish events are
|
||||
// the per-run accounting source; tasks.tokens_used is the durable fallback for
|
||||
// older or remote runs whose events were not persisted.
|
||||
func (s *Store) TokensByDay(ctx context.Context, days int) (tokens []DayTokens, retErr error) {
|
||||
tokens = make([]DayTokens, 0)
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
WITH event_totals AS (
|
||||
SELECT task_id, substr(at,1,10) AS day,
|
||||
SUM(CASE WHEN json_type(data,'$.tokens') IN ('integer','real')
|
||||
THEN CAST(json_extract(data,'$.tokens') AS INTEGER) ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN json_type(data,'$.tokens') IN ('integer','real') 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 substr(t.updated_at,1,10), 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, CAST(SUM(tokens) AS INTEGER)
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT ?`, days)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var d DayTokens
|
||||
if err := rows.Scan(&d.Day, &d.Tokens); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens = append(tokens, d)
|
||||
}
|
||||
return tokens, rows.Err()
|
||||
}
|
||||
@@ -1,252 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
const identitiesSchema = `
|
||||
CREATE TABLE IF NOT EXISTS identities (
|
||||
id TEXT PRIMARY KEY NOT NULL, kind TEXT NOT NULL, display_name TEXT NOT NULL,
|
||||
lifecycle TEXT NOT NULL, version INTEGER NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS identity_aliases (
|
||||
alias TEXT PRIMARY KEY COLLATE NOCASE NOT NULL, identity_id TEXT NOT NULL REFERENCES identities(id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS identity_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, identity_id TEXT NOT NULL, event_type TEXT NOT NULL,
|
||||
from_lifecycle TEXT NOT NULL, to_lifecycle TEXT NOT NULL, display_name TEXT NOT NULL,
|
||||
actor_id TEXT NOT NULL, source TEXT NOT NULL, request_id TEXT NOT NULL UNIQUE, at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS identity_subjects (
|
||||
issuer TEXT NOT NULL, subject TEXT NOT NULL, identity_id TEXT NOT NULL REFERENCES identities(id),
|
||||
bound_at TEXT NOT NULL, PRIMARY KEY (issuer, subject)
|
||||
);`
|
||||
|
||||
const identityTimeLayout = time.RFC3339Nano
|
||||
|
||||
func (s *Store) List(ctx context.Context) (_ []identity.Identity, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT id, kind, display_name, lifecycle, version, created_at, updated_at FROM identities ORDER BY created_at, id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { retErr = errors.Join(retErr, rows.Close()) }()
|
||||
var out []identity.Identity
|
||||
for rows.Next() {
|
||||
value, err := scanIdentity(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, value)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, id identity.IdentityID) (identity.Identity, error) {
|
||||
value, err := scanIdentity(s.db.QueryRowContext(ctx, `SELECT id, kind, display_name, lifecycle, version, created_at, updated_at FROM identities WHERE id=?`, id))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
|
||||
func (s *Store) ResolveLegacyName(ctx context.Context, name string) (identity.Identity, error) {
|
||||
value, err := scanIdentity(s.db.QueryRowContext(ctx, `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 a.alias=?`, strings.TrimSpace(name)))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
|
||||
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.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO identities(id,kind,display_name,lifecycle,version,created_at,updated_at) VALUES(?,?,?,?,?,?,?)`, value.ID, value.Kind, value.DisplayName, value.Lifecycle, value.Version, now.Format(identityTimeLayout), now.Format(identityTimeLayout)); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO identity_aliases(alias,identity_id) VALUES(?,?)`, value.DisplayName, value.ID); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if err = insertIdentityEvent(ctx, tx, value, identity.Event{IdentityID: value.ID, Type: "create", To: value.Lifecycle, DisplayName: value.DisplayName}, audit, now); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return value, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) Apply(ctx context.Context, id identity.IdentityID, expectedVersion int64, command identity.Command, audit identity.Audit) (identity.Identity, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
current, err := scanIdentity(tx.QueryRowContext(ctx, `SELECT id, kind, display_name, lifecycle, version, created_at, updated_at FROM identities WHERE id=?`, id))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
if current.Version != expectedVersion {
|
||||
return identity.Identity{}, identity.ErrConflict
|
||||
}
|
||||
next, event, err := identity.Apply(current, command)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
now := audit.At.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
next.UpdatedAt = now
|
||||
result, err := tx.ExecContext(ctx, `UPDATE identities SET display_name=?, lifecycle=?, version=?, updated_at=? WHERE id=? AND version=?`, next.DisplayName, next.Lifecycle, next.Version, now.Format(identityTimeLayout), id, expectedVersion)
|
||||
if err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
return identity.Identity{}, identity.ErrConflict
|
||||
}
|
||||
if command.Type == identity.CommandRename {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO identity_aliases(alias,identity_id) VALUES(?,?)`, next.DisplayName, id); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
}
|
||||
if err = insertIdentityEvent(ctx, tx, next, event, audit, now); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
return next, tx.Commit()
|
||||
}
|
||||
|
||||
// ResolveSubject returns the identity bound to a provider subject, so a
|
||||
// credential that verified against the provider's keys selects an identity
|
||||
// archie already knows rather than creating one.
|
||||
func (s *Store) ResolveSubject(ctx context.Context, subject identity.Subject) (identity.Identity, error) {
|
||||
value, err := scanIdentity(s.db.QueryRowContext(ctx, `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=? AND b.subject=?`, subject.Issuer, subject.Subject))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
|
||||
// BindSubject binds an identity to the provider subject that asserts it. The
|
||||
// binding is audited like any other identity mutation: who authorised a caller
|
||||
// to act as this identity is the question the record has to answer later.
|
||||
// Rebinding an already-bound subject moves it, because a subject that changed
|
||||
// hands must resolve to whoever it belongs to now.
|
||||
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.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
current, err := scanIdentity(tx.QueryRowContext(ctx, `SELECT id, kind, display_name, lifecycle, version, created_at, updated_at FROM identities WHERE id=?`, id))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO identity_subjects(issuer,subject,identity_id,bound_at) VALUES(?,?,?,?) ON CONFLICT(issuer,subject) DO UPDATE SET identity_id=excluded.identity_id, bound_at=excluded.bound_at`, subject.Issuer, subject.Subject, id, now.Format(identityTimeLayout)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = insertIdentityEvent(ctx, tx, current, identity.Event{IdentityID: id, Type: "bind_subject", From: current.Lifecycle, To: current.Lifecycle, DisplayName: subject.Subject}, audit, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
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 && !isSQLiteUnique(err) {
|
||||
return fmt.Errorf("bootstrap identity %q: %w", seed.name, err)
|
||||
}
|
||||
if _, aliasErr := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO identity_aliases(alias,identity_id) VALUES(?,?)`, seed.name, seed.id); aliasErr != nil {
|
||||
return aliasErr
|
||||
}
|
||||
if seed.kind != identity.KindSystem {
|
||||
if _, migrateErr := s.db.ExecContext(ctx, `UPDATE tasks SET identity=? WHERE identity=?`, seed.id, seed.name); migrateErr != nil {
|
||||
return migrateErr
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type identityScanner interface{ Scan(...any) error }
|
||||
|
||||
func scanIdentity(row identityScanner) (identity.Identity, error) {
|
||||
var v identity.Identity
|
||||
var created, updated string
|
||||
err := row.Scan(&v.ID, &v.Kind, &v.DisplayName, &v.Lifecycle, &v.Version, &created, &updated)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
v.CreatedAt, err = time.Parse(identityTimeLayout, created)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
v.UpdatedAt, err = time.Parse(identityTimeLayout, updated)
|
||||
return v, err
|
||||
}
|
||||
|
||||
func insertIdentityEvent(ctx context.Context, tx *sql.Tx, value identity.Identity, event 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)
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO identity_events(identity_id,event_type,from_lifecycle,to_lifecycle,display_name,actor_id,source,request_id,at) VALUES(?,?,?,?,?,?,?,?,?)`, value.ID, event.Type, event.From, event.To, event.DisplayName, audit.ActorID, audit.Source, audit.RequestID, at.Format(identityTimeLayout))
|
||||
return err
|
||||
}
|
||||
|
||||
func isSQLiteUnique(err error) bool { return strings.Contains(err.Error(), "UNIQUE constraint failed") }
|
||||
@@ -1,84 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
func TestIdentityMutationPersistsAuditAndHistoricalName(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
created, err := identity.New(identity.StableID("reviewer"), identity.KindBot, "Reviewer")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
created, err = s.Create(t.Context(), created, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "create"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renamed, err := s.Apply(t.Context(), created.ID, created.Version, identity.Command{Type: identity.CommandRename, DisplayName: "Review Bot"}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "rename"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolved, err := s.ResolveLegacyName(t.Context(), "reviewer")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resolved.ID != renamed.ID || resolved.DisplayName != "Review Bot" {
|
||||
t.Fatalf("resolved = %+v, want renamed identity", resolved)
|
||||
}
|
||||
var count int
|
||||
if err := s.DB().QueryRowContext(t.Context(), `SELECT COUNT(*) FROM identity_events WHERE identity_id = ?`, renamed.ID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("audit count = %d, want 2", count)
|
||||
}
|
||||
if _, err := s.Apply(t.Context(), renamed.ID, created.Version, identity.Command{Type: identity.CommandSuspend}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "stale"}); !errors.Is(err, identity.ErrConflict) {
|
||||
t.Fatalf("stale apply error = %v, want conflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapIdentitiesIsStableAndNonDestructive(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
if inserted, err := s.EnqueueIssue(t.Context(), "acme", "widget", 7, "Legacy", "", "", "reviewer"); err != nil || !inserted {
|
||||
t.Fatalf("seed legacy task = %v, %v", inserted, err)
|
||||
}
|
||||
if err := s.BootstrapIdentities(t.Context(), []string{"archie", "reviewer"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reviewer, err := s.ResolveLegacyName(t.Context(), "reviewer")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Apply(t.Context(), reviewer.ID, reviewer.Version, identity.Command{Type: identity.CommandRename, DisplayName: "Custom"}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "rename"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.BootstrapIdentities(t.Context(), []string{"archie", "reviewer"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.Get(t.Context(), reviewer.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.DisplayName != "Custom" {
|
||||
t.Fatalf("bootstrap overwrote name with %q", got.DisplayName)
|
||||
}
|
||||
all, err := s.List(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(all) != 3 {
|
||||
t.Fatalf("identity count = %d, want system plus two configured", len(all))
|
||||
}
|
||||
task, err := s.TaskByIssue(t.Context(), "acme", "widget", 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.Identity != string(reviewer.ID) {
|
||||
t.Fatalf("legacy task identity = %q, want %q", task.Identity, reviewer.ID)
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
func bindAudit(requestID string) identity.Audit {
|
||||
return identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: requestID, At: time.Now().UTC()}
|
||||
}
|
||||
|
||||
func newTestIdentity(t *testing.T, s *Store, id identity.IdentityID, kind identity.Kind, name string) identity.Identity {
|
||||
t.Helper()
|
||||
value, err := identity.New(id, kind, name)
|
||||
if err != nil {
|
||||
t.Fatalf("identity.New() error = %v", err)
|
||||
}
|
||||
created, err := s.Create(context.Background(), value, bindAudit("create:"+string(id)))
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
return created
|
||||
}
|
||||
|
||||
// TestResolveSubjectReturnsTheBoundIdentity: a verified provider subject selects
|
||||
// the identity it is bound to, which is what lets a credential resolve to a
|
||||
// record archie already knows rather than creating one.
|
||||
func TestResolveSubjectReturnsTheBoundIdentity(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
agent := newTestIdentity(t, s, identity.IdentityID("30000000-0000-5000-8000-000000000001"), identity.KindBot, "archie-agent")
|
||||
subject := identity.Subject{Issuer: "https://auth.catlow.cloud", Subject: "archie"}
|
||||
|
||||
if err := s.BindSubject(ctx, agent.ID, subject, bindAudit("bind:1")); err != nil {
|
||||
t.Fatalf("BindSubject() error = %v", err)
|
||||
}
|
||||
|
||||
got, err := s.ResolveSubject(ctx, subject)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveSubject() error = %v", err)
|
||||
}
|
||||
if got.ID != agent.ID || got.Kind != identity.KindBot {
|
||||
t.Fatalf("ResolveSubject() = %+v, want %+v", got, agent)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveSubjectIsKeyedOnIssuerAndSubject: the same subject string from a
|
||||
// different issuer is a different caller, so it must not resolve.
|
||||
func TestResolveSubjectIsKeyedOnIssuerAndSubject(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
agent := newTestIdentity(t, s, identity.IdentityID("30000000-0000-5000-8000-000000000002"), identity.KindBot, "archie-agent")
|
||||
bound := identity.Subject{Issuer: "https://auth.catlow.cloud", Subject: "archie"}
|
||||
if err := s.BindSubject(ctx, agent.ID, bound, bindAudit("bind:2")); err != nil {
|
||||
t.Fatalf("BindSubject() error = %v", err)
|
||||
}
|
||||
|
||||
others := []struct {
|
||||
name string
|
||||
subject identity.Subject
|
||||
}{
|
||||
{"another issuer, same subject", identity.Subject{Issuer: "https://elsewhere.example", Subject: "archie"}},
|
||||
{"same issuer, another subject", identity.Subject{Issuer: "https://auth.catlow.cloud", Subject: "someone-else"}},
|
||||
}
|
||||
for _, tc := range others {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if _, err := s.ResolveSubject(ctx, tc.subject); !errors.Is(err, identity.ErrNotFound) {
|
||||
t.Fatalf("ResolveSubject() error = %v, want %v", err, identity.ErrNotFound)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBindSubjectMovesAnAlreadyBoundSubject: when a subject changes hands it
|
||||
// resolves to whoever holds it now, so a reassignment cannot leave two identities
|
||||
// claiming one provider subject.
|
||||
func TestBindSubjectMovesAnAlreadyBoundSubject(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
first := newTestIdentity(t, s, identity.IdentityID("30000000-0000-5000-8000-000000000003"), identity.KindBot, "first-agent")
|
||||
second := newTestIdentity(t, s, identity.IdentityID("30000000-0000-5000-8000-000000000004"), identity.KindBot, "second-agent")
|
||||
subject := identity.Subject{Issuer: "https://auth.catlow.cloud", Subject: "archie"}
|
||||
|
||||
if err := s.BindSubject(ctx, first.ID, subject, bindAudit("bind:3")); err != nil {
|
||||
t.Fatalf("BindSubject() error = %v", err)
|
||||
}
|
||||
if err := s.BindSubject(ctx, second.ID, subject, bindAudit("bind:4")); err != nil {
|
||||
t.Fatalf("rebind error = %v", err)
|
||||
}
|
||||
|
||||
got, err := s.ResolveSubject(ctx, subject)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveSubject() error = %v", err)
|
||||
}
|
||||
if got.ID != second.ID {
|
||||
t.Fatalf("ResolveSubject() = %s, want %s", got.ID, second.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindSubjectRequiresAuditAndAKnownIdentity(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
subject := identity.Subject{Issuer: "https://auth.catlow.cloud", Subject: "archie"}
|
||||
|
||||
if err := s.BindSubject(ctx, identity.SystemID, subject, identity.Audit{}); err == nil {
|
||||
t.Fatal("BindSubject() accepted a binding with no audit")
|
||||
}
|
||||
if err := s.BindSubject(ctx, identity.IdentityID("does-not-exist"), subject, bindAudit("bind:5")); !errors.Is(err, identity.ErrNotFound) {
|
||||
t.Fatalf("BindSubject() error = %v, want %v", err, identity.ErrNotFound)
|
||||
}
|
||||
if err := s.BindSubject(ctx, identity.SystemID, identity.Subject{}, bindAudit("bind:6")); err == nil {
|
||||
t.Fatal("BindSubject() accepted an incomplete subject")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventAttributionSurvivesTheRoundTrip: the attributions are only useful if
|
||||
// they come back out of the store, so a reader can answer who acted and whose
|
||||
// authority they used.
|
||||
func TestEventAttributionSurvivesTheRoundTrip(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
inserted, err := s.InsertEvent(ctx, events.Event{
|
||||
Kind: events.KindAgentApproved,
|
||||
TaskID: 11,
|
||||
ActorID: "30000000-0000-5000-8000-000000000001",
|
||||
ActorKind: string(identity.KindBot),
|
||||
PrincipalID: "30000000-0000-5000-8000-000000000009",
|
||||
Detail: "approved by an agent",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertEvent() error = %v", err)
|
||||
}
|
||||
|
||||
got, err := s.TaskEvents(ctx, 11)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskEvents() error = %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("TaskEvents() returned %d events, want 1", len(got))
|
||||
}
|
||||
e := got[0]
|
||||
if e.ID != inserted {
|
||||
t.Fatalf("event id = %d, want %d", e.ID, inserted)
|
||||
}
|
||||
if e.Kind != events.KindAgentApproved {
|
||||
t.Fatalf("kind = %q, want %q", e.Kind, events.KindAgentApproved)
|
||||
}
|
||||
if e.ActorID == "" || e.ActorKind != string(identity.KindBot) || e.PrincipalID == "" {
|
||||
t.Fatalf("attribution did not survive the round trip: %+v", e)
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
// Package store defines the task and event persistence for archied's data.
|
||||
// TaskStore is the full surface the daemon needs and *Store is its SQLite
|
||||
// implementation.
|
||||
//
|
||||
// REVISED (archie-core-8cda.5.6): the producer-owned contract surfaces this
|
||||
// package used to define live in internal/contracts/store/v1 (package
|
||||
// storev1) -- the approved contract location -- so the archie-ui process can
|
||||
// reference the contract without linking this SQLite implementation. The
|
||||
// definitions below are type aliases for compatibility with the daemon-side
|
||||
// callers; new UI-process-facing code imports storev1 directly.
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// Aliases: the daemon/webui/intake store surfaces moved to storecontract. The
|
||||
// message strings of the sentinel errors are the wire contract (see
|
||||
// storev1's var block); the aliases change nothing observable.
|
||||
type (
|
||||
TaskStore = storecontract.TaskStore
|
||||
TaskLifecycle = storecontract.TaskLifecycle
|
||||
TaskArchiver = storecontract.TaskArchiver
|
||||
TaskRetryer = storecontract.TaskRetryer
|
||||
TaskQueries = storecontract.TaskQueries
|
||||
TaskEvents = storecontract.TaskEvents
|
||||
TaskLogStore = storecontract.TaskLogStore
|
||||
CaptureStore = storecontract.CaptureStore
|
||||
ConfigSnapshotStore = storecontract.ConfigSnapshotStore
|
||||
ChannelStatusStore = storecontract.ChannelStatusStore
|
||||
ApplyStatusStore = storecontract.ApplyStatusStore
|
||||
MappingStore = storecontract.MappingStore
|
||||
BindingStore = storecontract.BindingStore
|
||||
BindingDispatcher = storecontract.BindingDispatcher
|
||||
BindingTaskCreator = storecontract.BindingTaskCreator
|
||||
PlaybookDispatcher = storecontract.PlaybookDispatcher
|
||||
CapturedEvent = storecontract.CapturedEvent
|
||||
ConfigSnapshot = storecontract.ConfigSnapshot
|
||||
ChannelStatus = storecontract.ChannelStatus
|
||||
ApplyStatus = storecontract.ApplyStatus
|
||||
WorkflowStat = storecontract.WorkflowStat
|
||||
StageStat = storecontract.StageStat
|
||||
DayTokens = storecontract.DayTokens
|
||||
)
|
||||
|
||||
var (
|
||||
ErrStaleTransition = storecontract.ErrStaleTransition
|
||||
ErrBindingNotFound = storecontract.ErrBindingNotFound
|
||||
ErrBindingOverlap = storecontract.ErrBindingOverlap
|
||||
ErrBindingTransition = storecontract.ErrBindingTransition
|
||||
ErrAlreadyDispatched = storecontract.ErrAlreadyDispatched
|
||||
ErrMappingNotFound = storecontract.ErrMappingNotFound
|
||||
)
|
||||
|
||||
// Compile-time check: *Store satisfies TaskStore.
|
||||
var _ TaskStore = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies ChannelStatusStore.
|
||||
var _ ChannelStatusStore = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies workflow.Store.
|
||||
var _ workflow.Store = (*Store)(nil)
|
||||
@@ -1,265 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This file is the offline half of the store's contract with its owner: the
|
||||
// operations the State Store process cannot perform on itself, because they
|
||||
// read or replace the very file it has open. They are what
|
||||
// docs/prds/runtime-control-plane.md ("Bootstrap, migration, and recovery")
|
||||
// asks for -- back up, restore and validate the database without archied or
|
||||
// the Web UI -- and every one of them works on the file directly.
|
||||
//
|
||||
// The read-only operations never take the ownership lock, because they are
|
||||
// what an operator runs against a serving store. The one that replaces the
|
||||
// file does, because a rewrite under a running server is the only way an
|
||||
// offline command can destroy a task store.
|
||||
|
||||
// OpenReadOnly opens an existing store for reading only: no schema is created
|
||||
// and no migration runs. That matters for recovery -- the snapshot an update
|
||||
// takes has to stay exactly what the release that wrote it expects, and
|
||||
// migrating a copy during verification would hand a rollback a schema its
|
||||
// binary refuses.
|
||||
func OpenReadOnly(ctx context.Context, path string) (*Store, error) {
|
||||
if err := RequireDatabase(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := sql.Open("sqlite", readonlyDSN(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
// sql.Open is lazy: the first statement is where a file that is not a
|
||||
// database, a missing WAL sidecar or an unreadable path surfaces.
|
||||
var tables int
|
||||
if err := db.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master`).Scan(&tables); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("read task database %s: %w", path, err), db.Close())
|
||||
}
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
// readonlyDSN reads the store without the write-ahead log pragma the serving
|
||||
// process sets: a reader has no business changing the database's journal mode,
|
||||
// and the read-only flag makes an accidental write an error rather than a
|
||||
// silent modification of a file someone else owns.
|
||||
func readonlyDSN(path string) string {
|
||||
separator := "?"
|
||||
if strings.HasSuffix(path, "?") || strings.HasSuffix(path, "&") {
|
||||
separator = ""
|
||||
} else if strings.Contains(path, "?") {
|
||||
separator = "&"
|
||||
}
|
||||
return path + separator + "mode=ro&_pragma=busy_timeout(5000)"
|
||||
}
|
||||
|
||||
// StoresResources reports whether the store's file carries the control-plane
|
||||
// resources table. A store written before the control plane existed has none
|
||||
// and holds no stored settings to validate; the serving process creates the
|
||||
// table on its next start, so an offline check must tell that apart from
|
||||
// resources it cannot read.
|
||||
func (s *Store) StoresResources(ctx context.Context) (bool, error) {
|
||||
var tables int
|
||||
if err := s.db.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type='table' AND name='resources'`).Scan(&tables); err != nil {
|
||||
return false, fmt.Errorf("inspect store schema: %w", err)
|
||||
}
|
||||
return tables > 0, nil
|
||||
}
|
||||
|
||||
// RequireDatabase reports whether path is an existing store file. A writer
|
||||
// that must not create the file it fails to find asks this first: a recovery
|
||||
// command pointed at a mistyped path has to refuse, not leave a fresh empty
|
||||
// database where an operator expected theirs.
|
||||
func RequireDatabase(path string) error {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return fmt.Errorf("task database %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateFile checks an existing store the way the serving process meets it
|
||||
// on startup and reports the schema version it carries: the file is a SQLite
|
||||
// database this binary can read, it is not corrupt, and its schema is not
|
||||
// newer than this binary supports.
|
||||
func ValidateFile(ctx context.Context, path string) (int, error) {
|
||||
st, err := OpenReadOnly(ctx, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = st.Close() }()
|
||||
return st.check(ctx)
|
||||
}
|
||||
|
||||
// check runs the file-level checks. integrity_check returns one row per
|
||||
// problem, or the single row "ok"; reading the first row is therefore the
|
||||
// verdict rather than a sample.
|
||||
func (s *Store) check(ctx context.Context) (int, error) {
|
||||
var result string
|
||||
if err := s.db.QueryRowContext(ctx, `PRAGMA integrity_check`).Scan(&result); err != nil {
|
||||
return 0, fmt.Errorf("integrity check: %w", err)
|
||||
}
|
||||
if result != "ok" {
|
||||
return 0, fmt.Errorf("integrity check: %s", result)
|
||||
}
|
||||
var version int
|
||||
if err := s.db.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&version); err != nil {
|
||||
return 0, fmt.Errorf("read schema version: %w", err)
|
||||
}
|
||||
if version > taskSchemaVersion {
|
||||
return 0, fmt.Errorf("database schema version %d is newer than supported version %d", version, taskSchemaVersion)
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// Backup writes a transactionally consistent snapshot of database to snapshot,
|
||||
// using SQLite's own copy, so a store that is being written to cannot produce
|
||||
// a torn file.
|
||||
//
|
||||
// It deliberately does not take the ownership lock. The update installer runs
|
||||
// inside the process it is updating and cannot stop the State Store, so the
|
||||
// snapshot that makes a failed update reversible is taken against a serving
|
||||
// store; a backup that insisted on an exclusively owned file would break the
|
||||
// one caller that exists. The snapshot is written beside the destination and
|
||||
// renamed into place, so an interrupted backup never replaces a snapshot that
|
||||
// was good with one that is not.
|
||||
func Backup(ctx context.Context, database, snapshot string) error {
|
||||
if err := RequireDatabase(database); err != nil {
|
||||
return err
|
||||
}
|
||||
// The exemption from the ownership lock below is only sound while backup
|
||||
// never replaces the database. With -out naming the database it would
|
||||
// rename the snapshot over a file a serving State Store may still have
|
||||
// open, orphaning the WAL of the file it just unlinked -- so it is refused
|
||||
// exactly as Restore refuses it.
|
||||
if same, err := sameFile(database, snapshot); err != nil {
|
||||
return err
|
||||
} else if same {
|
||||
return fmt.Errorf("snapshot %s is the database itself", snapshot)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(snapshot), 0o755); err != nil {
|
||||
return fmt.Errorf("create snapshot directory: %w", err)
|
||||
}
|
||||
db, err := sql.Open("sqlite", sqliteDSN(database))
|
||||
if err != nil {
|
||||
return fmt.Errorf("open task database %s: %w", database, err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
staging := snapshot + ".tmp"
|
||||
if err := os.Remove(staging); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("clear staging snapshot %s: %w", staging, err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `VACUUM INTO ?`, staging); err != nil {
|
||||
return errors.Join(fmt.Errorf("snapshot %s: %w", database, err), os.Remove(staging))
|
||||
}
|
||||
// A snapshot that is not itself a usable store is worse than no snapshot:
|
||||
// the update path treats its existence as proof that a rollback is
|
||||
// possible.
|
||||
if _, err := ValidateFile(ctx, staging); err != nil {
|
||||
return errors.Join(fmt.Errorf("snapshot is not a usable store: %w", err), os.Remove(staging))
|
||||
}
|
||||
if err := os.Rename(staging, snapshot); err != nil {
|
||||
return errors.Join(fmt.Errorf("publish snapshot %s: %w", snapshot, err), os.Remove(staging))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restore replaces database with the snapshot at snapshot. The snapshot is
|
||||
// verified before anything is destroyed, so a file that is not a usable store
|
||||
// cannot take the database down with it, and the database file itself is
|
||||
// replaced by a rename, so a failure leaves either the old file or the new one
|
||||
// rather than a mixture. The replaced file's -wal/-shm are removed first, which
|
||||
// the comment at that step explains: between the two calls the database is
|
||||
// merely incomplete, and re-running the command completes it.
|
||||
//
|
||||
// It holds the ownership lock for its duration: rewriting a database a running
|
||||
// State Store still has open would leave that process writing to an unlinked
|
||||
// inode while the store served a file nobody owns.
|
||||
func Restore(ctx context.Context, database, snapshot string) error {
|
||||
if _, err := os.Stat(filepath.Dir(database)); err != nil {
|
||||
return fmt.Errorf("restore destination %s: %w", database, err)
|
||||
}
|
||||
if same, err := sameFile(database, snapshot); err != nil {
|
||||
return err
|
||||
} else if same {
|
||||
return fmt.Errorf("restore snapshot %s is the database itself", snapshot)
|
||||
}
|
||||
ownership, err := AcquireOwnership(database)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = ownership.Release() }()
|
||||
|
||||
if _, err := ValidateFile(ctx, snapshot); err != nil {
|
||||
return fmt.Errorf("snapshot is not a usable store: %w", err)
|
||||
}
|
||||
staging := database + ".restore.tmp"
|
||||
if err := copyFile(snapshot, staging); err != nil {
|
||||
return errors.Join(err, os.Remove(staging))
|
||||
} // The replaced database's write-ahead log belongs to the file that is
|
||||
// going away. It goes first: between the two calls the database is merely
|
||||
// incomplete, whereas a WAL left beside the file it does not describe is
|
||||
// the state SQLite cannot recover from.
|
||||
for _, sidecar := range []string{"-wal", "-shm"} {
|
||||
if err := os.Remove(database + sidecar); err != nil && !os.IsNotExist(err) {
|
||||
return errors.Join(fmt.Errorf("remove %s%s: %w", database, sidecar, err), os.Remove(staging))
|
||||
}
|
||||
}
|
||||
if err := os.Rename(staging, database); err != nil {
|
||||
return errors.Join(fmt.Errorf("publish restored database %s: %w", database, err), os.Remove(staging))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sameFile(a, b string) (bool, error) {
|
||||
first, err := os.Stat(a)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
second, err := os.Stat(b)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return os.SameFile(first, second), nil
|
||||
}
|
||||
|
||||
// copyFile writes source over a new file at path, flushed to disk before it is
|
||||
// returned: the rename that publishes it must not reach the directory entries
|
||||
// ahead of the bytes they point at. The snapshot's own permissions are kept, so
|
||||
// a restore does not silently change who may read the operator's database.
|
||||
func copyFile(source, path string) (retErr error) {
|
||||
info, err := os.Stat(source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat snapshot %s: %w", source, err)
|
||||
}
|
||||
in, err := os.Open(source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open snapshot %s: %w", source, err)
|
||||
}
|
||||
defer func() { retErr = errors.Join(retErr, in.Close()) }()
|
||||
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode().Perm())
|
||||
if err != nil {
|
||||
return fmt.Errorf("create %s: %w", path, err)
|
||||
}
|
||||
defer func() { retErr = errors.Join(retErr, out.Close()) }()
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
return fmt.Errorf("write %s: %w", path, err)
|
||||
}
|
||||
if err := out.Sync(); err != nil {
|
||||
return fmt.Errorf("sync %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Ownership is what makes "the State Store is stopped" a fact an offline
|
||||
// command can check, so the primitive itself is asserted here rather than only
|
||||
// through the commands that use it: exclusivity, release, and per-database
|
||||
// independence.
|
||||
func TestOwnershipIsExclusivePerDatabase(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "archie.db-tasks.sqlite")
|
||||
second := filepath.Join(dir, "other.db-tasks.sqlite")
|
||||
|
||||
held, err := AcquireOwnership(first)
|
||||
if err != nil {
|
||||
t.Fatalf("first AcquireOwnership: %v", err)
|
||||
}
|
||||
if _, err := AcquireOwnership(first); !errors.Is(err, ErrStoreOwned) {
|
||||
t.Fatalf("second AcquireOwnership on the same database = %v, want ErrStoreOwned", err)
|
||||
}
|
||||
// A different database is a different claim: one process owning its store
|
||||
// must not stop another from owning its own.
|
||||
elsewhere, err := AcquireOwnership(second)
|
||||
if err != nil {
|
||||
t.Fatalf("AcquireOwnership on an unrelated database: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(first + ".lock"); err != nil {
|
||||
t.Fatalf("ownership left no sidecar to lock: %v", err)
|
||||
}
|
||||
|
||||
if err := held.Release(); err != nil {
|
||||
t.Fatalf("Release: %v", err)
|
||||
}
|
||||
reacquired, err := AcquireOwnership(first)
|
||||
if err != nil {
|
||||
t.Fatalf("AcquireOwnership after Release: %v", err)
|
||||
}
|
||||
if err := errors.Join(reacquired.Release(), elsewhere.Release()); err != nil {
|
||||
t.Fatalf("release on cleanup: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The snapshot an update takes is the state a rollback restores, and it is
|
||||
// restored by the release that wrote it. Verifying the copy must therefore not
|
||||
// migrate it: a snapshot silently upgraded to this binary's schema would be
|
||||
// refused by the older binary it exists to serve.
|
||||
func TestBackupSnapshotKeepsTheSchemaVersionItCopied(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database := filepath.Join(dir, "archie.db-tasks.sqlite")
|
||||
snapshot := filepath.Join(dir, "snapshot.sqlite")
|
||||
st, err := Open(t.Context(), database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// An older release's file: the schema version it wrote, which this binary
|
||||
// is expected to migrate when the store is next opened for serving.
|
||||
if err := setUserVersion(t.Context(), database, taskSchemaVersion-1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := Backup(t.Context(), database, snapshot); err != nil {
|
||||
t.Fatalf("Backup: %v", err)
|
||||
}
|
||||
version, err := ValidateFile(t.Context(), snapshot)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateFile on the snapshot: %v", err)
|
||||
}
|
||||
if version != taskSchemaVersion-1 {
|
||||
t.Fatalf("snapshot schema version = %d, want the %d it copied", version, taskSchemaVersion-1)
|
||||
}
|
||||
}
|
||||
|
||||
func setUserVersion(ctx context.Context, path string, version int) error {
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
_, err = db.ExecContext(ctx, fmt.Sprintf(`PRAGMA user_version = %d`, version))
|
||||
return err
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ErrStoreOwned is returned when a task database is already owned by another
|
||||
// process. Ownership is what makes "the State Store is stopped" a fact an
|
||||
// offline command can check rather than a rule the operator is trusted to
|
||||
// follow: overwriting a database a running server still holds leaves it
|
||||
// writing to an unlinked inode while the store serves a file nobody owns.
|
||||
var ErrStoreOwned = errors.New("task database is owned by another process")
|
||||
|
||||
// Ownership is one process's exclusive claim over one task database, held for
|
||||
// as long as that process owns the file. The serving State Store takes it for
|
||||
// its whole life; the offline recovery commands take it for the duration of an
|
||||
// operation that rewrites the file.
|
||||
//
|
||||
// The claim lives in a sidecar file beside the database rather than on the
|
||||
// database inode itself, so it survives the rename a restore performs: after
|
||||
// the swap the sidecar is still the one path every process locks.
|
||||
type Ownership struct {
|
||||
f *os.File
|
||||
}
|
||||
|
||||
// AcquireOwnership takes the store's ownership lock without blocking. A
|
||||
// database another process owns comes back as ErrStoreOwned, never as a wait:
|
||||
// a process that must not proceed has to hear so, and a blocking lock would
|
||||
// turn a mistyped command into a hung terminal.
|
||||
func AcquireOwnership(path string) (*Ownership, error) {
|
||||
lockPath := ownershipLockPath(path)
|
||||
if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {
|
||||
return nil, fmt.Errorf("create store directory for %s: %w", lockPath, err)
|
||||
}
|
||||
f, err := os.OpenFile(lockPath, os.O_RDWR|os.O_CREATE, 0o644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open store lock %s: %w", lockPath, err)
|
||||
}
|
||||
taken, err := lockFile(f)
|
||||
if err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("lock %s: %w", lockPath, err), f.Close())
|
||||
}
|
||||
if !taken {
|
||||
return nil, errors.Join(fmt.Errorf("%s: %w", path, ErrStoreOwned), f.Close())
|
||||
}
|
||||
return &Ownership{f: f}, nil
|
||||
}
|
||||
|
||||
// Release drops the claim and closes the sidecar. Closing the descriptor alone
|
||||
// would release it too; the explicit unlock keeps the intent readable and
|
||||
// makes Release idempotent.
|
||||
func (o *Ownership) Release() error {
|
||||
if o == nil || o.f == nil {
|
||||
return nil
|
||||
}
|
||||
err := unlockFile(o.f)
|
||||
f := o.f
|
||||
o.f = nil
|
||||
return errors.Join(err, f.Close())
|
||||
}
|
||||
|
||||
// ownershipLockPath is the sidecar every owner of path locks. It is derived
|
||||
// from the database path, so the State Store process and an offline command
|
||||
// that were given the same database agree on it without configuration.
|
||||
func ownershipLockPath(path string) string { return path + ".lock" }
|
||||
@@ -1,30 +0,0 @@
|
||||
//go:build unix
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// lockFile takes a non-blocking exclusive flock on f. The boolean reports
|
||||
// whether it was taken: a lock another process holds (EWOULDBLOCK) is a normal
|
||||
// answer to "is the State Store running?", not an error.
|
||||
func lockFile(f *os.File) (bool, error) {
|
||||
err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
||||
if errors.Is(err, syscall.EWOULDBLOCK) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func unlockFile(f *os.File) error {
|
||||
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_UN); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
// flock is a POSIX primitive and Windows is not a supported deployment surface
|
||||
// (CLAUDE.md's deployment model is Linux and macOS), so this file fails closed:
|
||||
// every lock attempt reports an error rather than silently degrading to "no
|
||||
// lock". An offline command that cannot prove it owns the database must refuse
|
||||
// to rewrite it, which is the only safe reading of an unavailable primitive.
|
||||
//
|
||||
// If a Windows deployment ever becomes supported, swap this for a real
|
||||
// LockFileEx/UnlockFileEx pair; nothing else in the package needs to change.
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
func lockFile(*os.File) (bool, error) {
|
||||
return false, errors.New("store ownership locking requires a POSIX flock, which this platform does not provide")
|
||||
}
|
||||
|
||||
func unlockFile(*os.File) error { return nil }
|
||||
@@ -1,395 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
func TestTaskByIssue(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if got, err := s.TaskByIssue(ctx, "acme", "widget", 1); err != nil || got != nil {
|
||||
t.Fatalf("TaskByIssue on empty store = (%+v, %v)", got, err)
|
||||
}
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "title", "body", "bug", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.TaskByIssue(ctx, "acme", "widget", 1)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByIssue = (%+v, %v)", got, err)
|
||||
}
|
||||
if got.Title != "title" || got.IssueNumber != 1 {
|
||||
t.Fatalf("TaskByIssue = %+v", got)
|
||||
}
|
||||
|
||||
if got, err := s.TaskByIssue(ctx, "acme", "widget", 2); err != nil || got != nil {
|
||||
t.Fatalf("TaskByIssue for missing issue = (%+v, %v)", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenTaskByPR(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
|
||||
for i, status := range []string{workflow.StatusPROpen, workflow.StatusMerged, workflow.StatusRejected} {
|
||||
issueNumber := i + 1
|
||||
prNumber := 101 + i
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", issueNumber, "title", "body", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.TaskByIssue(ctx, "acme", "widget", issueNumber)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue(%d) = (%+v, %v)", issueNumber, task, err)
|
||||
}
|
||||
task.PRNumber = prNumber
|
||||
if err := s.Update(ctx, task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Transition(ctx, task.ID, workflow.StatusQueued, status, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := s.OpenTaskByPR(ctx, "acme", "widget", 101)
|
||||
if err != nil || got == nil || got.IssueNumber != 1 {
|
||||
t.Fatalf("OpenTaskByPR(open) = (%+v, %v)", got, err)
|
||||
}
|
||||
for _, prNumber := range []int{102, 103, 999} {
|
||||
got, err := s.OpenTaskByPR(ctx, "acme", "widget", prNumber)
|
||||
if err != nil || got != nil {
|
||||
t.Errorf("OpenTaskByPR(%d) = (%+v, %v), want (nil, nil)", prNumber, got, err)
|
||||
}
|
||||
}
|
||||
if got, err := s.OpenTaskByPR(ctx, "other", "widget", 101); err != nil || got != nil {
|
||||
t.Errorf("OpenTaskByPR(other owner) = (%+v, %v), want (nil, nil)", got, err)
|
||||
}
|
||||
|
||||
var indexedColumns string
|
||||
if err := s.db.QueryRowContext(ctx, `
|
||||
SELECT group_concat(name, ',')
|
||||
FROM pragma_index_info('idx_tasks_pr')`).Scan(&indexedColumns); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if indexedColumns != "owner,repo,pr_number" {
|
||||
t.Fatalf("idx_tasks_pr columns = %q, want owner,repo,pr_number", indexedColumns)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequeueFromWaitingHuman(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
if err := s.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusWaitingHuman, "needs input"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waiting, err := s.TaskByID(ctx, task.ID)
|
||||
if err != nil || waiting == nil || waiting.Status != workflow.StatusWaitingHuman {
|
||||
t.Fatalf("after transition = (%+v, %v), want waiting_human", waiting, err)
|
||||
}
|
||||
|
||||
// Requeue with a workflow forces it (waiting_human -> approved -> implement).
|
||||
if err := s.Requeue(ctx, task.ID, workflow.StatusWaitingHuman, "implement"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
requeued, err := s.TaskByIssue(ctx, "acme", "widget", 1)
|
||||
if err != nil || requeued == nil || requeued.Status != workflow.StatusQueued || requeued.Workflow != "implement" {
|
||||
t.Fatalf("after forced requeue = %+v, %v", requeued, err)
|
||||
}
|
||||
|
||||
// Requeue with an empty workflow keeps the task's current workflow
|
||||
// (retrying a parked task).
|
||||
task2, err := s.ClaimNext(ctx)
|
||||
if err != nil || task2 == nil {
|
||||
t.Fatalf("second claim = (%v, %v)", task2, err)
|
||||
}
|
||||
task2.Workflow = "feasibility"
|
||||
if err := s.Update(ctx, task2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Transition(ctx, task2.ID, workflow.StatusRunning, workflow.StatusParked, "parked"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Requeue(ctx, task2.ID, workflow.StatusParked, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := s.TaskByIssue(ctx, "acme", "widget", 1)
|
||||
if err != nil || retried == nil || retried.Status != workflow.StatusQueued || retried.Workflow != "feasibility" {
|
||||
t.Fatalf("after empty-workflow requeue = %+v, %v", retried, err)
|
||||
}
|
||||
if retried.ParkReason != "" {
|
||||
t.Fatalf("Requeue must clear park_reason, got %q", retried.ParkReason)
|
||||
}
|
||||
|
||||
// Verify retry_count is NOT reset by Requeue (it persists). Bumped
|
||||
// directly rather than through RetryTask, which also transitions
|
||||
// status -- this fixture only needs the counter moved.
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE tasks SET retry_count = retry_count + 1 WHERE id = ?`, task2.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE tasks SET retry_count = retry_count + 1 WHERE id = ?`, task2.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Park again and requeue.
|
||||
if err := s.Transition(ctx, task2.ID, workflow.StatusQueued, workflow.StatusParked, "parked again"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Requeue(ctx, task2.ID, workflow.StatusParked, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
afterRequeue, err := s.TaskByIssue(ctx, "acme", "widget", 1)
|
||||
if err != nil || afterRequeue == nil {
|
||||
t.Fatalf("TaskByIssue after second requeue = (%+v, %v)", afterRequeue, err)
|
||||
}
|
||||
if afterRequeue.RetryCount != 2 {
|
||||
t.Fatalf("Requeue must not reset retry_count, got %d, want 2", afterRequeue.RetryCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTasksListingAndStatusCounts(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
for i, status := range []string{workflow.StatusQueued, workflow.StatusRunning, workflow.StatusPROpen} {
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", i+1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.TaskByIssue(ctx, "acme", "widget", i+1)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue(%d) = (%+v, %v)", i+1, task, err)
|
||||
}
|
||||
if status != workflow.StatusQueued {
|
||||
if err := s.Transition(ctx, task.ID, workflow.StatusQueued, status, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks, err := s.Tasks(ctx, 10)
|
||||
if err != nil || len(tasks) != 3 {
|
||||
t.Fatalf("Tasks = (%d, %v)", len(tasks), err)
|
||||
}
|
||||
|
||||
limited, err := s.Tasks(ctx, 2)
|
||||
if err != nil || len(limited) != 2 {
|
||||
t.Fatalf("Tasks with limit = (%d, %v)", len(limited), err)
|
||||
}
|
||||
|
||||
counts, err := s.StatusCounts(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]int{workflow.StatusQueued: 1, workflow.StatusRunning: 1, workflow.StatusPROpen: 1}
|
||||
for status, n := range want {
|
||||
if counts[status] != n {
|
||||
t.Fatalf("StatusCounts[%s] = %d, want %d (all: %+v)", status, counts[status], n, counts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowStats(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%v, %v)", task, err)
|
||||
}
|
||||
task.Workflow = "implement"
|
||||
task.TokensUsed = 1000
|
||||
task.Iterations = 4
|
||||
if err := s.Update(ctx, task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusMerged, "merged"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stats, err := s.WorkflowStats(ctx)
|
||||
if err != nil || len(stats) != 1 {
|
||||
t.Fatalf("WorkflowStats = (%+v, %v)", stats, err)
|
||||
}
|
||||
got := stats[0]
|
||||
if got.Workflow != "implement" || got.Runs != 1 || got.Merged != 1 || got.AvgTokens != 1000 || got.TotalToken != 1000 {
|
||||
t.Fatalf("WorkflowStats[0] = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokensByDayFallsBackToTaskTotals(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%v, %v)", task, err)
|
||||
}
|
||||
task.TokensUsed = 904901
|
||||
if err := s.Update(ctx, task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE tasks SET updated_at=? WHERE id=?`, "2026-08-14 12:00:00", task.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := s.TokensByDay(ctx, 14)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []DayTokens{{Day: "2026-08-14", Tokens: 904901}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("TokensByDay = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokensByDayUsesEventsAndIgnoresMalformedTokenFields(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "event task", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventTask, err := s.ClaimNext(ctx)
|
||||
if err != nil || eventTask == nil {
|
||||
t.Fatalf("claim event task = (%v, %v)", eventTask, err)
|
||||
}
|
||||
eventTask.TokensUsed = 999
|
||||
if err := s.Update(ctx, eventTask); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE tasks SET updated_at=? WHERE id=?`, "2026-08-14 12:00:00", eventTask.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, ev := range []events.Event{
|
||||
{Kind: events.KindAgentFinish, TaskID: eventTask.ID, At: time.Date(2026, 8, 13, 12, 0, 0, 0, time.UTC), Data: map[string]any{"tokens": 30}},
|
||||
{Kind: events.KindAgentFinish, TaskID: eventTask.ID, At: time.Date(2026, 8, 14, 12, 0, 0, 0, time.UTC), Data: map[string]any{"tokens": 70}},
|
||||
{Kind: events.KindAgentFinish, TaskID: eventTask.ID, At: time.Date(2026, 8, 14, 13, 0, 0, 0, time.UTC), Data: map[string]any{"tokens": "bad"}},
|
||||
} {
|
||||
if _, err := s.InsertEvent(ctx, ev); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := s.TokensByDay(ctx, 14)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []DayTokens{{Day: "2026-08-14", Tokens: 70}, {Day: "2026-08-13", Tokens: 30}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("TokensByDay = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokensByDayReturnsEmptySlice(t *testing.T) {
|
||||
s := openTest(t)
|
||||
got, err := s.TokensByDay(t.Context(), 14)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got == nil || len(got) != 0 {
|
||||
t.Fatalf("TokensByDay = %#v, want non-nil empty slice", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStageStats(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Insert 6 stage_finish events across 2 workflows × 3 stage groups.
|
||||
// plan/draft: 3 runs, avg 400 ms, 1 error
|
||||
// plan/review: 1 run, avg 300 ms, 0 errors
|
||||
// implement/build: 2 runs, avg 1500 ms, 1 error
|
||||
|
||||
type stageEvent struct {
|
||||
workflow string
|
||||
stage string
|
||||
durationMs int
|
||||
errorMsg string // empty means no error key
|
||||
}
|
||||
|
||||
inputs := []stageEvent{
|
||||
{workflow: "plan", stage: "draft", durationMs: 200},
|
||||
{workflow: "plan", stage: "draft", durationMs: 400},
|
||||
{workflow: "plan", stage: "draft", durationMs: 600, errorMsg: "something broke"},
|
||||
|
||||
{workflow: "plan", stage: "review", durationMs: 300},
|
||||
|
||||
{workflow: "implement", stage: "build", durationMs: 1000},
|
||||
{workflow: "implement", stage: "build", durationMs: 2000, errorMsg: "build failed"},
|
||||
}
|
||||
|
||||
for _, in := range inputs {
|
||||
data := map[string]any{"duration_ms": in.durationMs}
|
||||
if in.errorMsg != "" {
|
||||
data["error"] = in.errorMsg
|
||||
}
|
||||
if _, err := s.InsertEvent(ctx, events.Event{
|
||||
Kind: "stage_finish",
|
||||
Workflow: in.workflow,
|
||||
Stage: in.stage,
|
||||
Data: data,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
stats, err := s.StageStats(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(stats) != 3 {
|
||||
t.Fatalf("StageStats: got %d groups, want 3: %+v", len(stats), stats)
|
||||
}
|
||||
|
||||
// Verify ordering: workflow then stage.
|
||||
want := []struct {
|
||||
workflow string
|
||||
stage string
|
||||
runs int
|
||||
avgMs int
|
||||
errors int
|
||||
}{
|
||||
{workflow: "implement", stage: "build", runs: 2, avgMs: 1500, errors: 1},
|
||||
{workflow: "plan", stage: "draft", runs: 3, avgMs: 400, errors: 1},
|
||||
{workflow: "plan", stage: "review", runs: 1, avgMs: 300, errors: 0},
|
||||
}
|
||||
|
||||
for i, w := range want {
|
||||
got := stats[i]
|
||||
if got.Workflow != w.workflow {
|
||||
t.Fatalf("stats[%d].Workflow = %q, want %q", i, got.Workflow, w.workflow)
|
||||
}
|
||||
if got.Stage != w.stage {
|
||||
t.Fatalf("stats[%d].Stage = %q, want %q", i, got.Stage, w.stage)
|
||||
}
|
||||
if got.Runs != w.runs {
|
||||
t.Fatalf("stats[%d].Runs = %d, want %d", i, got.Runs, w.runs)
|
||||
}
|
||||
if got.AvgMs != w.avgMs {
|
||||
t.Fatalf("stats[%d].AvgMs = %d, want %d", i, got.AvgMs, w.avgMs)
|
||||
}
|
||||
if got.Errors != w.errors {
|
||||
t.Fatalf("stats[%d].Errors = %d, want %d", i, got.Errors, w.errors)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// prOpenTask inserts a task and walks it to the state a reaction resolves
|
||||
// against: the task opened its PR and is idle waiting for review.
|
||||
func prOpenTask(t *testing.T, s *Store, owner, repo string, prNumber int) int64 {
|
||||
t.Helper()
|
||||
ok, err := s.EnqueueIssue(t.Context(), owner, repo, 1, "title", "body", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("enqueue: task already tracked")
|
||||
}
|
||||
task, err := s.TaskByIssue(t.Context(), owner, repo, 1)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue = (%v, %v)", task, err)
|
||||
}
|
||||
task.PRNumber = prNumber
|
||||
task.Status = workflow.StatusPROpen
|
||||
if err := s.Transition(t.Context(), task.ID, workflow.StatusQueued, workflow.StatusPROpen, "PR opened"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Update(t.Context(), task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return task.ID
|
||||
}
|
||||
|
||||
func TestBeginRemediationMovesAnOpenPRTaskToQueuedRemediate(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
|
||||
if err := s.BeginRemediation(t.Context(), id, `{"review_id":7}`); err != nil {
|
||||
t.Fatalf("BeginRemediation: %v", err)
|
||||
}
|
||||
got, err := s.TaskByID(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Status != workflow.StatusQueued {
|
||||
t.Errorf("status = %q, want queued", got.Status)
|
||||
}
|
||||
if got.Workflow != "remediate" {
|
||||
t.Errorf("workflow = %q, want remediate", got.Workflow)
|
||||
}
|
||||
if got.ReviewPayload != `{"review_id":7}` {
|
||||
t.Errorf("review_payload = %q, want the carried unit", got.ReviewPayload)
|
||||
}
|
||||
if got.ParkReason != "" || got.Stage != "" {
|
||||
t.Errorf("park_reason/stage = %q/%q, want both empty", got.ParkReason, got.Stage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBeginRemediationRefusesAnAlreadyClaimedTask(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
if err := s.BeginRemediation(t.Context(), id, `{"review_id":7}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.BeginRemediation(t.Context(), id, `{"review_id":7}`); !errors.Is(err, ErrStaleTransition) {
|
||||
t.Errorf("second BeginRemediation err = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBeginRemediationRefusesNonOpenPRStatuses(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
if err := s.Transition(t.Context(), id, workflow.StatusPROpen, workflow.StatusParked, "gate failed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.BeginRemediation(t.Context(), id, `{"review_id":7}`); !errors.Is(err, ErrStaleTransition) {
|
||||
t.Errorf("BeginRemediation on parked err = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateReviewPayloadAppendsOnlyWhileARemediationIsQueued(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
if err := s.BeginRemediation(t.Context(), id, `{"review_id":7}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := s.UpdateReviewPayload(t.Context(), id, `{"review_id":7,"comments":[{"comment_id":9}]}`); err != nil {
|
||||
t.Fatalf("UpdateReviewPayload while queued: %v", err)
|
||||
}
|
||||
got, err := s.TaskByID(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.ReviewPayload != `{"review_id":7,"comments":[{"comment_id":9}]}` {
|
||||
t.Errorf("review_payload = %q, want the appended unit", got.ReviewPayload)
|
||||
}
|
||||
|
||||
// Claim the task: the remediation is running, and a payload written under
|
||||
// it would race the builder's input.
|
||||
if _, err := s.ClaimNext(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.UpdateReviewPayload(t.Context(), id, `{"review_id":7,"comments":[{"comment_id":10}]}`); !errors.Is(err, ErrStaleTransition) {
|
||||
t.Errorf("UpdateReviewPayload while running err = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParkTaskWritesItsClassification pins the classified park write: the
|
||||
// class is recorded at the park site in the same guarded write, and an
|
||||
// unclassified park defaults to needs_human.
|
||||
func TestParkTaskWritesItsClassification(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widgets", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, _ := s.TaskByIssue(ctx, "acme", "widgets", 1)
|
||||
if _, err := s.ClaimNext(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := s.ParkTask(ctx, task.ID, workflow.StatusRunning, "container acquire failed", "transient"); err != nil {
|
||||
t.Fatalf("ParkTask: %v", err)
|
||||
}
|
||||
got, err := s.TaskByID(ctx, task.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Status != workflow.StatusParked || got.ParkReason != "container acquire failed" {
|
||||
t.Errorf("park row = status:%q reason:%q", got.Status, got.ParkReason)
|
||||
}
|
||||
if got.ParkClass != "transient" {
|
||||
t.Errorf("park_class = %q, want transient", got.ParkClass)
|
||||
}
|
||||
|
||||
// Requeue clears the class with the reason: a stale class on a queued
|
||||
// task would group a live task with parked ones.
|
||||
if err := s.Requeue(ctx, task.ID, workflow.StatusParked, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = s.TaskByID(ctx, task.ID)
|
||||
if got.ParkClass != "needs_human" || got.ParkReason != "" {
|
||||
t.Errorf("after requeue: class=%q reason=%q, want cleared", got.ParkClass, got.ParkReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParkTaskRejectsAnUnknownClass(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widgets", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, _ := s.TaskByIssue(ctx, "acme", "widgets", 1)
|
||||
if _, err := s.ClaimNext(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// An unknown class normalizes to needs_human rather than persisting a
|
||||
// value no consumer knows how to group.
|
||||
if err := s.ParkTask(ctx, task.ID, workflow.StatusRunning, "x", "urgent"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ := s.TaskByID(ctx, task.ID)
|
||||
if got.ParkClass != "needs_human" {
|
||||
t.Errorf("park_class = %q, want the needs_human fallback", got.ParkClass)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParkTaskStaleGuard(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widgets", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, _ := s.TaskByIssue(ctx, "acme", "widgets", 1)
|
||||
// Still queued, not running: the from guard must refuse.
|
||||
if err := s.ParkTask(ctx, task.ID, workflow.StatusRunning, "x", "transient"); !errors.Is(err, ErrStaleTransition) {
|
||||
t.Errorf("ParkTask stale err = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransitionToParkedDefaultsTheClass(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widgets", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, _ := s.TaskByIssue(ctx, "acme", "widgets", 1)
|
||||
if _, err := s.ClaimNext(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The generic transition is the workflow engine's park path: every
|
||||
// unclassified park reads as operator-actionable.
|
||||
if err := s.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusParked, "gate failed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ := s.TaskByID(ctx, task.ID)
|
||||
if got.ParkClass != "needs_human" {
|
||||
t.Errorf("park_class = %q, want the needs_human default", got.ParkClass)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCarriesRemediationRounds(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widgets", 1, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, _ := s.TaskByIssue(ctx, "acme", "widgets", 1)
|
||||
task.RemediationRounds = 2
|
||||
if err := s.Update(ctx, task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ := s.TaskByID(ctx, task.ID)
|
||||
if got.RemediationRounds != 2 {
|
||||
t.Errorf("remediation_rounds = %d, want 2", got.RemediationRounds)
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"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 = storecontract.ErrResourceNotFound
|
||||
ErrResourceVersionConflict = storecontract.ErrResourceVersionConflict
|
||||
)
|
||||
|
||||
type Resource = storecontract.Resource
|
||||
|
||||
type ResourceWrite = storecontract.ResourceWrite
|
||||
|
||||
const resourcesSchema = `
|
||||
CREATE TABLE IF NOT EXISTS resources (
|
||||
kind TEXT PRIMARY KEY, value BLOB NOT NULL, version INTEGER NOT NULL, updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS resource_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, value BLOB NOT NULL,
|
||||
version INTEGER NOT NULL, actor TEXT NOT NULL, source TEXT NOT NULL,
|
||||
request_id TEXT NOT NULL, expected_version INTEGER NOT NULL,
|
||||
current_version INTEGER NOT NULL, at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_resource_history_kind_version ON resource_history(kind, version);
|
||||
-- The request-ID idempotency key is per kind, not global: a resource write
|
||||
-- replays only the same kind's prior write (resourceByRequest). A database
|
||||
-- written before v4 carries the old global UNIQUE as a column constraint;
|
||||
-- migrateResourceHistoryToPerKindRequestIDs rebuilds it to this shape.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_resource_history_kind_request ON resource_history(kind, request_id);
|
||||
`
|
||||
|
||||
func (s *Store) Resource(ctx context.Context, kind string) (Resource, error) {
|
||||
var r Resource
|
||||
err := s.db.QueryRowContext(ctx, `SELECT kind,value,version,updated_at FROM resources WHERE kind=?`, kind).
|
||||
Scan(&r.Kind, &r.Value, &r.Version, sqliteTime{&r.At})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Resource{}, ErrResourceNotFound
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
// ResourceHistory returns kind's revisions newest first, each carrying the
|
||||
// audit record written with it. limit caps the rows; zero or less returns
|
||||
// every revision. An unknown kind has no history, which is not an error.
|
||||
func (s *Store) ResourceHistory(ctx context.Context, kind string, limit int) ([]Resource, error) {
|
||||
if limit <= 0 {
|
||||
limit = -1
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history WHERE kind=? ORDER BY version DESC LIMIT ?`, kind, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var history []Resource
|
||||
for rows.Next() {
|
||||
var r Resource
|
||||
if err := rows.Scan(&r.Kind, &r.Value, &r.Version, &r.Actor, &r.Source, &r.RequestID, &r.ExpectedVersion, &r.CurrentVersion, sqliteTime{&r.At}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
history = append(history, r)
|
||||
}
|
||||
return history, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) PutResource(ctx context.Context, w ResourceWrite) (_ Resource, retErr error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if existing, ok, err := resourceByRequest(ctx, tx, w.Kind, w.RequestID); err != nil {
|
||||
return Resource{}, err
|
||||
} else if ok {
|
||||
return existing, nil
|
||||
}
|
||||
var current int64
|
||||
err = tx.QueryRowContext(ctx, `SELECT version FROM resources WHERE kind=?`, w.Kind).Scan(¤t)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return Resource{}, err
|
||||
}
|
||||
if current != w.ExpectedVersion {
|
||||
return Resource{}, fmt.Errorf("%w: expected %d, current %d", ErrResourceVersionConflict, w.ExpectedVersion, current)
|
||||
}
|
||||
if w.At.IsZero() {
|
||||
w.At = time.Now().UTC()
|
||||
}
|
||||
next := current + 1
|
||||
at := w.At.UTC().Format(time.RFC3339Nano)
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO resources(kind,value,version,updated_at) VALUES(?,?,?,?) ON CONFLICT(kind) DO UPDATE SET value=excluded.value,version=excluded.version,updated_at=excluded.updated_at`, w.Kind, w.Value, next, at); err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO resource_history(kind,value,version,actor,source,request_id,expected_version,current_version,at) VALUES(?,?,?,?,?,?,?,?,?)`, w.Kind, w.Value, next, w.Actor, w.Source, w.RequestID, w.ExpectedVersion, current, at); err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
return Resource{Kind: w.Kind, Value: append([]byte(nil), w.Value...), Version: next, Actor: w.Actor, Source: w.Source, RequestID: w.RequestID, ExpectedVersion: w.ExpectedVersion, CurrentVersion: current, At: w.At.UTC()}, nil
|
||||
}
|
||||
|
||||
// resourceByRequest replays one prior write by its request ID, so a retried
|
||||
// replace is idempotent instead of a second revision. The key is (kind,
|
||||
// request ID), not the request ID alone: the API is per kind, so an ID reused
|
||||
// across kinds must write the new kind rather than silently replay the first
|
||||
// kind's resource (archie-core-fcvd).
|
||||
func resourceByRequest(ctx context.Context, tx *sql.Tx, kind, requestID string) (Resource, bool, error) {
|
||||
var r Resource
|
||||
err := tx.QueryRowContext(ctx, `SELECT kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history WHERE kind=? AND request_id=?`, kind, requestID).
|
||||
Scan(&r.Kind, &r.Value, &r.Version, &r.Actor, &r.Source, &r.RequestID, &r.ExpectedVersion, &r.CurrentVersion, sqliteTime{&r.At})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Resource{}, false, nil
|
||||
}
|
||||
return r, err == nil, err
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPutResourceRejectsStaleVersion(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
ctx := t.Context()
|
||||
first, err := s.PutResource(ctx, ResourceWrite{Kind: "settings", Value: []byte(`{"v":1}`), Actor: "a", Source: "test", RequestID: "r1", ExpectedVersion: 0, At: time.Now()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = s.PutResource(ctx, ResourceWrite{Kind: "settings", Value: []byte(`{"v":2}`), Actor: "a", Source: "test", RequestID: "r2", ExpectedVersion: 0, At: time.Now()})
|
||||
if !errors.Is(err, ErrResourceVersionConflict) {
|
||||
t.Fatalf("PutResource() error = %v, want ErrResourceVersionConflict (current %d)", err, first.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutResourceDuplicateRequestIsIdempotent(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
ctx := t.Context()
|
||||
write := ResourceWrite{Kind: "settings", Value: []byte(`{"v":1}`), Actor: "a", Source: "test", RequestID: "same", ExpectedVersion: 0, At: time.Now()}
|
||||
first, err := s.PutResource(ctx, write)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write.Value = []byte(`{"v":999}`)
|
||||
second, err := s.PutResource(ctx, write)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.Version != second.Version || string(second.Value) != string(first.Value) {
|
||||
t.Fatalf("duplicate = (%d, %s), want (%d, %s)", second.Version, second.Value, first.Version, first.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPutResourceRequestIDIsScopedToTheKind: the request ID makes one write
|
||||
// idempotent for the kind it wrote: the dedup key is (kind, request ID), so a
|
||||
// request ID reused across two kinds writes the second instead of silently
|
||||
// replaying the first kind's resource (archie-core-fcvd).
|
||||
func TestPutResourceRequestIDIsScopedToTheKind(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
ctx := t.Context()
|
||||
if _, err := s.PutResource(ctx, ResourceWrite{Kind: "settings", Value: []byte(`{"v":1}`), Actor: "a", Source: "test", RequestID: "shared", ExpectedVersion: 0, At: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := s.PutResource(ctx, ResourceWrite{Kind: "other", Value: []byte(`{"v":9}`), Actor: "a", Source: "test", RequestID: "shared", ExpectedVersion: 0, At: time.Now()})
|
||||
if err != nil {
|
||||
t.Fatalf("PutResource with a request ID another kind already used: %v", err)
|
||||
}
|
||||
if second.Kind != "other" || second.Version != 1 {
|
||||
t.Fatalf("second write = (%s, v%d), want the %q write itself, not a replay of the first kind's resource", second.Kind, second.Version, "other")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceHistoryReturnsEveryRevisionNewestFirst(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
ctx := t.Context()
|
||||
for i, write := range []ResourceWrite{
|
||||
{Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui", RequestID: "r1", ExpectedVersion: 0},
|
||||
{Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging", RequestID: "r2", ExpectedVersion: 1},
|
||||
{Kind: "other", Value: []byte(`{"v":9}`), Actor: "operator", Source: "archie-ui", RequestID: "r3", ExpectedVersion: 0},
|
||||
} {
|
||||
write.At = time.Now().UTC()
|
||||
if _, err := s.PutResource(ctx, write); err != nil {
|
||||
t.Fatalf("write %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
history, err := s.ResourceHistory(ctx, "settings", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("history = %d revisions, want 2 (other kinds must not leak in)", len(history))
|
||||
}
|
||||
if history[0].Version != 2 || string(history[0].Value) != `{"v":2}` || history[0].Source != "messaging" {
|
||||
t.Fatalf("newest revision = %+v, want version 2 from messaging", history[0])
|
||||
}
|
||||
if history[1].Version != 1 || history[1].Actor != "operator" || history[1].At.IsZero() {
|
||||
t.Fatalf("oldest revision = %+v, want version 1 with its audit intact", history[1])
|
||||
}
|
||||
|
||||
limited, err := s.ResourceHistory(ctx, "settings", 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(limited) != 1 || limited[0].Version != 2 {
|
||||
t.Fatalf("limited history = %+v, want the newest revision alone", limited)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceHistoryForUnknownKindIsEmpty(t *testing.T) {
|
||||
s := OpenTest(t)
|
||||
defer s.Close()
|
||||
history, err := s.ResourceHistory(t.Context(), "missing", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("history for an unknown kind must not error: %v", err)
|
||||
}
|
||||
if len(history) != 0 {
|
||||
t.Fatalf("history = %+v, want empty", history)
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// TestOpenPRsCarriesReviewCursors pins the poll backstop's read side: the
|
||||
// per-PR scan reads each task's persisted review and comment cursors, so a
|
||||
// daemon restart resumes where the last scan stopped instead of re-listing
|
||||
// every review the forge has ever returned.
|
||||
func TestOpenPRsCarriesReviewCursors(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
if err := s.SetReviewCursors(t.Context(), id, 100, 200); err != nil {
|
||||
t.Fatalf("SetReviewCursors: %v", err)
|
||||
}
|
||||
|
||||
tasks, err := s.OpenPRs(t.Context())
|
||||
if err != nil || len(tasks) != 1 {
|
||||
t.Fatalf("OpenPRs = (%d tasks, %v), want 1", len(tasks), err)
|
||||
}
|
||||
if tasks[0].ReviewCursor != 100 {
|
||||
t.Errorf("ReviewCursor = %d, want 100", tasks[0].ReviewCursor)
|
||||
}
|
||||
if tasks[0].WatchCommentID != 200 {
|
||||
t.Errorf("WatchCommentID = %d, want 200 (the review-comment cursor)", tasks[0].WatchCommentID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetReviewCursorsGuardsOnStatus pins the write side: cursors only move
|
||||
// while the task is idle in pr_open. A cursor written under a running or
|
||||
// parked task would desync from what its remediation run has consumed.
|
||||
func TestSetReviewCursorsRefusesNonOpenPRStatuses(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id := prOpenTask(t, s, "acme", "widgets", 42)
|
||||
if err := s.Transition(t.Context(), id, workflow.StatusPROpen, workflow.StatusParked, "gate failed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.SetReviewCursors(t.Context(), id, 5, 6); !errors.Is(err, ErrStaleTransition) {
|
||||
t.Errorf("SetReviewCursors on parked err = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
}
|
||||
@@ -1,929 +0,0 @@
|
||||
// Package store is archied's SQLite state: one row per task (a GitHub
|
||||
// issue picked up for work), with every lifecycle transition recorded.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"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/taskstate"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// taskSchemaVersion is the newest schema this binary opens. ValidateFile
|
||||
// refuses a database whose version is newer, so a binary and the file it
|
||||
// opens move together.
|
||||
const taskSchemaVersion = 4
|
||||
|
||||
// OpenOption configures the store at open time.
|
||||
type OpenOption func(*openOptions)
|
||||
|
||||
type openOptions struct{}
|
||||
|
||||
// Open opens (creating if needed) the SQLite database and its schema.
|
||||
func Open(ctx context.Context, path string, opts ...OpenOption) (*Store, error) {
|
||||
var o openOptions
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := sql.Open("sqlite", sqliteDSN(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.ExecContext(ctx, schema+eventsSchema+configSnapshotSchema+channelStatusSchema+applyStatusSchema+resourcesSchema+identitiesSchema); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("store: init schema: %w", err), db.Close())
|
||||
}
|
||||
if err := migrateTasks(ctx, db); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("store: migrate: %w", err), db.Close())
|
||||
}
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
func sqliteDSN(path string) string {
|
||||
separator := "?"
|
||||
if strings.HasSuffix(path, "?") || strings.HasSuffix(path, "&") {
|
||||
separator = ""
|
||||
} else if strings.Contains(path, "?") {
|
||||
separator = "&"
|
||||
}
|
||||
return path + separator + "_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
|
||||
}
|
||||
|
||||
func migrateTasks(ctx context.Context, db *sql.DB) error {
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var version int
|
||||
if err := tx.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&version); err != nil {
|
||||
return err
|
||||
}
|
||||
if version > taskSchemaVersion {
|
||||
return fmt.Errorf("database schema version %d is newer than supported version %d", version, taskSchemaVersion)
|
||||
}
|
||||
columns, err := taskColumns(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// events is created by `CREATE TABLE IF NOT EXISTS` (eventsSchema), which
|
||||
// is a no-op against a database that already has the table. On an existing
|
||||
// archie.db this read plus the `events` arm below is therefore the ONLY
|
||||
// thing that adds events.attempt; without it the first insert fails with
|
||||
// `table events has no column named attempt`.
|
||||
eventsColumns, err := tableColumns(ctx, tx, "events")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
migrations := []struct {
|
||||
table string
|
||||
column string
|
||||
sql string
|
||||
}{
|
||||
{"tasks", "watch_comment_id", `ALTER TABLE tasks ADD COLUMN watch_comment_id INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "review_cursor", `ALTER TABLE tasks ADD COLUMN review_cursor INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "park_class", `ALTER TABLE tasks ADD COLUMN park_class TEXT NOT NULL DEFAULT 'needs_human'`},
|
||||
{"tasks", "remediation_rounds", `ALTER TABLE tasks ADD COLUMN remediation_rounds INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "retry_count", `ALTER TABLE tasks ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "source", `ALTER TABLE tasks ADD COLUMN source TEXT NOT NULL DEFAULT 'forge'`},
|
||||
{"tasks", "identity", `ALTER TABLE tasks ADD COLUMN identity TEXT NOT NULL DEFAULT ''`},
|
||||
{"tasks", "binding_id", `ALTER TABLE tasks ADD COLUMN binding_id TEXT NOT NULL DEFAULT ''`},
|
||||
{"tasks", "binding_version", `ALTER TABLE tasks ADD COLUMN binding_version INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "review_payload", `ALTER TABLE tasks ADD COLUMN review_payload TEXT NOT NULL DEFAULT ''`},
|
||||
{"tasks", "workflow_definition_version", `ALTER TABLE tasks ADD COLUMN workflow_definition_version INTEGER NOT NULL DEFAULT 0`},
|
||||
{"tasks", "workflow_definition_digest", `ALTER TABLE tasks ADD COLUMN workflow_definition_digest TEXT NOT NULL DEFAULT ''`},
|
||||
{"tasks", "workflow_definition_yaml", `ALTER TABLE tasks ADD COLUMN workflow_definition_yaml TEXT NOT NULL DEFAULT ''`},
|
||||
{"events", "attempt", `ALTER TABLE events ADD COLUMN attempt INTEGER NOT NULL DEFAULT 0`},
|
||||
{"events", "actor_id", `ALTER TABLE events ADD COLUMN actor_id TEXT NOT NULL DEFAULT ''`},
|
||||
{"events", "actor_kind", `ALTER TABLE events ADD COLUMN actor_kind TEXT NOT NULL DEFAULT ''`},
|
||||
{"events", "principal_id", `ALTER TABLE events ADD COLUMN principal_id TEXT NOT NULL DEFAULT ''`},
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
// Only tasks and events remain here; the event-capture tables moved
|
||||
// to internal/infrastructure/edastore, which owns their schema.
|
||||
present := columns
|
||||
if migration.table == "events" {
|
||||
present = eventsColumns
|
||||
}
|
||||
if present[migration.column] {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, migration.sql); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := migrateResourceHistoryToPerKindRequestIDs(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := migrateEventTimestamps(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return finishTaskMigration(ctx, tx, columns)
|
||||
}
|
||||
|
||||
// migrateResourceHistoryToPerKindRequestIDs is the v4 step: the history
|
||||
// table's global request_id UNIQUE becomes per-kind uniqueness, the key the
|
||||
// resource write path actually dedups on (resourceByRequest). SQLite cannot
|
||||
// drop a column constraint, so the table is rebuilt in place; a global
|
||||
// constraint is strictly stricter than the per-kind one, so no existing row
|
||||
// can fail the new shape. Detection is the auto-index the old column
|
||||
// constraint created -- sqlite_master carries it with a NULL sql -- which the
|
||||
// rebuilt table leaves no copy of, so the step is a no-op on a database that
|
||||
// already has the v4 shape (including one resourcesSchema created fresh).
|
||||
func migrateResourceHistoryToPerKindRequestIDs(ctx context.Context, tx *sql.Tx) error {
|
||||
var autoIndexes int
|
||||
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type='index' AND tbl_name='resource_history' AND sql IS NULL`).Scan(&autoIndexes); err != nil {
|
||||
return err
|
||||
}
|
||||
if autoIndexes == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE resource_history_per_kind_request (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, value BLOB NOT NULL,
|
||||
version INTEGER NOT NULL, actor TEXT NOT NULL, source TEXT NOT NULL,
|
||||
request_id TEXT NOT NULL, expected_version INTEGER NOT NULL,
|
||||
current_version INTEGER NOT NULL, at TEXT NOT NULL
|
||||
)`,
|
||||
`INSERT INTO resource_history_per_kind_request (id,kind,value,version,actor,source,request_id,expected_version,current_version,at)
|
||||
SELECT id,kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history`,
|
||||
`DROP TABLE resource_history`,
|
||||
`ALTER TABLE resource_history_per_kind_request RENAME TO resource_history`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_resource_history_kind_version ON resource_history(kind, version)`,
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS idx_resource_history_kind_request ON resource_history(kind, request_id)`,
|
||||
} {
|
||||
if _, err := tx.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("rebuild resource_history for per-kind request IDs: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// migrateEventTimestamps normalises the events.at column to the fixed-width
|
||||
// layout the EventsSince cursor sorts on. Rows written before the cursor
|
||||
// change used time.RFC3339Nano, which trims trailing zeros, so a whole-second
|
||||
// row (20 chars) and a full-precision row (30 chars) would compare out of
|
||||
// chronological order as strings. The ALTER-only migration arms never UPDATE
|
||||
// values, so this is a separate idempotent step (the v4 precedent): it
|
||||
// re-evaluates on every open, detects an already-normalised file by length,
|
||||
// and re-runs cleanly on one that has never been migrated.
|
||||
func migrateEventTimestamps(ctx context.Context, tx *sql.Tx) error {
|
||||
updates, err := scanLegacyEventTimestamps(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, u := range updates {
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE events SET at = ? WHERE id = ?`, u.at, u.id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// eventTimestampUpdate is one event row whose at predates the fixed-width
|
||||
// layout, re-rendered into that layout.
|
||||
type eventTimestampUpdate struct {
|
||||
id int64
|
||||
at string
|
||||
}
|
||||
|
||||
// scanLegacyEventTimestamps reads every event row whose at predates the
|
||||
// fixed-width layout, parses it, and re-renders it. The read cursor is closed
|
||||
// by the defer before this function returns, so the caller's UPDATEs run on a
|
||||
// free connection -- the store opens with SetMaxOpenConns(1), and a still-open
|
||||
// read cursor would hold that single connection and deadlock the writes.
|
||||
func scanLegacyEventTimestamps(ctx context.Context, tx *sql.Tx) ([]eventTimestampUpdate, error) {
|
||||
// A fixed-width row is exactly len(EventCursorLayout) characters; anything
|
||||
// else predates the layout and needs re-formatting.
|
||||
fixed := strings.Repeat("?", len(storecontract.EventCursorLayout))
|
||||
rows, err := tx.QueryContext(ctx, `SELECT id, at FROM events WHERE at NOT GLOB '`+fixed+`'`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var updates []eventTimestampUpdate
|
||||
for rows.Next() {
|
||||
var u eventTimestampUpdate
|
||||
if err := rows.Scan(&u.id, &u.at); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339Nano, u.at)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("normalise event timestamp %q: %w", u.at, err)
|
||||
}
|
||||
u.at = parsed.UTC().Format(storecontract.EventCursorLayout)
|
||||
updates = append(updates, u)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return updates, nil
|
||||
}
|
||||
|
||||
func finishTaskMigration(ctx context.Context, tx *sql.Tx, columns map[string]bool) error {
|
||||
if columns["owner"] && columns["repo"] && columns["pr_number"] {
|
||||
if _, err := tx.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS idx_tasks_pr ON tasks(owner, repo, pr_number)`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`PRAGMA user_version = %d`, taskSchemaVersion)); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func taskColumns(ctx context.Context, tx *sql.Tx) (_ map[string]bool, retErr error) {
|
||||
return tableColumns(ctx, tx, "tasks")
|
||||
}
|
||||
|
||||
func tableColumns(ctx context.Context, tx *sql.Tx, table string) (_ map[string]bool, retErr error) {
|
||||
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`PRAGMA table_info(%s)`, table))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { retErr = errors.Join(retErr, rows.Close()) }()
|
||||
columns := make(map[string]bool)
|
||||
for rows.Next() {
|
||||
var cid, notNull, primaryKey int
|
||||
var name, columnType string
|
||||
var defaultValue any
|
||||
if err := rows.Scan(&cid, &name, &columnType, ¬Null, &defaultValue, &primaryKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
columns[name] = true
|
||||
}
|
||||
return columns, rows.Err()
|
||||
}
|
||||
|
||||
const schema = `
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
owner TEXT NOT NULL,
|
||||
repo TEXT NOT NULL,
|
||||
issue_number INTEGER NOT NULL,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
labels TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT '` + workflow.StatusQueued + `',
|
||||
workflow TEXT NOT NULL DEFAULT '',
|
||||
stage TEXT NOT NULL DEFAULT '',
|
||||
branch TEXT NOT NULL DEFAULT '',
|
||||
plan TEXT NOT NULL DEFAULT '',
|
||||
notes TEXT NOT NULL DEFAULT '',
|
||||
pr_number INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_used INTEGER NOT NULL DEFAULT 0,
|
||||
iterations INTEGER NOT NULL DEFAULT 0,
|
||||
attempt INTEGER NOT NULL DEFAULT 0,
|
||||
park_reason TEXT NOT NULL DEFAULT '',
|
||||
watch_comment_id INTEGER NOT NULL DEFAULT 0,
|
||||
park_class TEXT NOT NULL DEFAULT 'needs_human',
|
||||
remediation_rounds INTEGER NOT NULL DEFAULT 0,
|
||||
retry_count INTEGER NOT NULL DEFAULT 0,
|
||||
source TEXT NOT NULL DEFAULT 'forge',
|
||||
identity TEXT NOT NULL DEFAULT '',
|
||||
-- binding_id carries the id of the edastore binding that produced this
|
||||
-- task, or "" for a task nobody bound. It is TEXT because bindings are
|
||||
-- PocketBase records; a database created before that stores the same
|
||||
-- values in an INTEGER-affinity column, which SQLite keeps as TEXT.
|
||||
binding_id TEXT NOT NULL DEFAULT '',
|
||||
binding_version INTEGER NOT NULL DEFAULT 0,
|
||||
review_payload TEXT NOT NULL DEFAULT '',
|
||||
workflow_definition_version INTEGER NOT NULL DEFAULT 0,
|
||||
workflow_definition_digest TEXT NOT NULL DEFAULT '',
|
||||
workflow_definition_yaml TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(owner, repo, issue_number)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS transitions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
task_id INTEGER NOT NULL,
|
||||
at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
from_status TEXT NOT NULL,
|
||||
to_status TEXT NOT NULL,
|
||||
detail TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
`
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
// DB exposes the underlying *sql.DB so tests outside the store
|
||||
// package can drive fixtures that the public surface does not (yet)
|
||||
// cover -- e.g. advancing a binding draft -> pending_approval without
|
||||
// going through the operator edit flow. Production callers must NOT
|
||||
// use this; the structured methods above are the contract.
|
||||
func (s *Store) DB() *sql.DB { return s.db }
|
||||
|
||||
// OpenTest opens an in-memory SQLite store suitable for tests.
|
||||
func OpenTest(t interface {
|
||||
TempDir() string
|
||||
Context() context.Context
|
||||
},
|
||||
) *Store {
|
||||
s, err := Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// EnqueueIssue inserts a new queued task for the issue; returns false if
|
||||
// the issue is already tracked (the idempotency key is owner/repo/number).
|
||||
// The task's Source defaults to "forge" (the SQLite column default).
|
||||
// identity is the archie identity whose forge poll discovered this issue
|
||||
// (empty for single-identity deployments); it scopes which identity's
|
||||
// forge client, worktree manager, and config downstream processing uses.
|
||||
func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(owner, repo, issue_number) DO NOTHING`,
|
||||
owner, repo, number, title, body, labels, identity)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// EnqueueChatTask inserts a new queued task with Source "chat" and no
|
||||
// backing forge issue, returning the full created row (with its real
|
||||
// database ID). The store allocates the synthetic issue number durably so
|
||||
// multiple processes sharing the database cannot generate the same value.
|
||||
// Workflow stages and daemon reconciliation must check workflow.Task.IsForgeBacked()
|
||||
// before treating it as a real forge issue number.
|
||||
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity string) (*workflow.Task, error) {
|
||||
const syntheticIssueNumberBase = 1_000_000_000_000_000
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
|
||||
VALUES (?, ?, COALESCE((
|
||||
SELECT MAX(issue_number) FROM tasks
|
||||
WHERE owner = ? AND repo = ? AND source = 'chat'
|
||||
), ?) + 1, ?, ?, 'chat', ?, 'chat', ?)
|
||||
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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at`,
|
||||
owner, repo, owner, repo, syntheticIssueNumberBase-1, title, body, wf, identity)
|
||||
return scanTask(row)
|
||||
}
|
||||
|
||||
// EnqueueBindingTask enqueues a task triggered by a playbook binding
|
||||
// dispatch (t2db.4 Phase B). It builds the row through EnqueueChatTask
|
||||
// (so the synthetic issue-number allocator stays the single source of
|
||||
// truth for chat-sourced tasks) and then stamps binding_id and
|
||||
// binding_version on the new row in a second statement.
|
||||
//
|
||||
// A crash between the two writes leaves the task without provenance
|
||||
// (binding_id == 0); the binding_dispatches ledger row written by the
|
||||
// dispatch loop still records the (binding, capture, task) triple, so
|
||||
// a future repair pass could backfill. This is the same
|
||||
// best-effort-provenance pattern the rest of the task lifecycle uses
|
||||
// for fields added after the row's primary insert.
|
||||
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.db.ExecContext(ctx,
|
||||
`UPDATE tasks SET binding_id=?, binding_version=? WHERE id=?`,
|
||||
bindingID, bindingVersion, t.ID); 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) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
UPDATE tasks SET status=?, attempt=attempt+1, updated_at=datetime('now')
|
||||
WHERE id = (SELECT id FROM tasks WHERE status=? ORDER BY id 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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at`,
|
||||
workflow.StatusRunning, workflow.StatusQueued)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
func scanTask(row *sql.Row) (*workflow.Task, error) {
|
||||
var t workflow.Task
|
||||
err := row.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title, &t.Body,
|
||||
&t.Labels, &t.Status, &t.Workflow, &t.Stage, &t.Branch, &t.Plan, &t.Notes,
|
||||
&t.PRNumber, &t.TokensUsed, &t.Iterations, &t.Attempt, &t.ParkReason,
|
||||
&t.WatchCommentID, &t.RetryCount, &t.Source, &t.Identity,
|
||||
&t.BindingID, &t.BindingVersion, &t.ReviewPayload,
|
||||
&t.WorkflowDefinitionVersion, &t.WorkflowDefinitionDigest, &t.WorkflowDefinitionYAML,
|
||||
&t.ParkClass, &t.RemediationRounds,
|
||||
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
// ClaimByIssue atomically claims a queued task by owner/repo/issue_number.
|
||||
// Returns nil if the task is not in queued state (already claimed, parked,
|
||||
// or terminal). Used by the NATS consumer path where the task was just
|
||||
// inserted via EnqueueIssue and needs an immediate targeted claim.
|
||||
func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
UPDATE tasks SET status=?, attempt=attempt+1, updated_at=datetime('now')
|
||||
WHERE owner=? AND repo=? AND issue_number=? AND status=?
|
||||
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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at`,
|
||||
workflow.StatusRunning, owner, repo, number, workflow.StatusQueued)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
// ErrStaleTransition is returned when a guarded transition fails because
|
||||
// the task's current status does not match the expected from status.
|
||||
|
||||
// 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. Detail used to exist only in the
|
||||
// timeline, which left daemon-side parks looking reasonless in task APIs and
|
||||
// the dashboard.
|
||||
func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE tasks
|
||||
SET status=?,
|
||||
park_reason=CASE WHEN ?=? THEN ? ELSE park_reason END,
|
||||
park_class=CASE WHEN ?=? THEN ? ELSE park_class END,
|
||||
updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`,
|
||||
to, to, workflow.StatusParked, clip(detail, 4000),
|
||||
to, to, taskstate.ParkNeedsHuman, taskID, from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx,
|
||||
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
||||
taskID, from, to, clip(detail, 4000))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ParkTask is the classified park write: the same guarded running->parked
|
||||
// transition Transition performs, carrying the park class the site chose.
|
||||
// The class is normalized here so an unknown value persists as needs_human
|
||||
// -- the safe misread is "an operator should look at this".
|
||||
func (s *Store) ParkTask(ctx context.Context, taskID int64, from, detail, class string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE tasks
|
||||
SET status=?,
|
||||
park_reason=?,
|
||||
park_class=?,
|
||||
updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`,
|
||||
workflow.StatusParked, clip(detail, 4000),
|
||||
taskstate.NormalizeParkClass(class), taskID, from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
||||
taskID, from, workflow.StatusParked, clip(detail, 4000)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Update persists mutable task fields written by workflows.
|
||||
func (s *Store) Update(ctx context.Context, t *workflow.Task) error {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET workflow=?, stage=?, branch=?, plan=?, notes=?,
|
||||
pr_number=?, tokens_used=?, iterations=?, park_reason=?,
|
||||
watch_comment_id=?, retry_count=?, remediation_rounds=?, review_payload=?, workflow_definition_version=?,
|
||||
workflow_definition_digest=?, workflow_definition_yaml=?, updated_at=datetime('now')
|
||||
WHERE id=?`,
|
||||
t.Workflow, t.Stage, t.Branch, t.Plan, t.Notes,
|
||||
t.PRNumber, t.TokensUsed, t.Iterations, clip(t.ParkReason, 4000),
|
||||
t.WatchCommentID, t.RetryCount, t.RemediationRounds, t.ReviewPayload, t.WorkflowDefinitionVersion,
|
||||
t.WorkflowDefinitionDigest, t.WorkflowDefinitionYAML, t.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
// BeginRemediation transitions an Archie-owned open-PR task into a queued
|
||||
// remediate run carrying the JSON-encoded review unit. The guarded update is
|
||||
// the reaction consumer's dedup: a re-delivered reaction, or a second
|
||||
// consumer racing the first, fails the status guard instead of starting a
|
||||
// second round. The unit is written in the same transaction, so a task can
|
||||
// never be claimed into a remediate run whose payload has not landed.
|
||||
func (s *Store) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE tasks SET status=?, workflow=?, stage='', park_reason='', park_class='needs_human', review_payload=?,
|
||||
updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`, workflow.StatusQueued, "remediate", clip(payload, 4000), taskID, workflow.StatusPROpen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
||||
taskID, workflow.StatusPROpen, workflow.StatusQueued, "review reaction queued remediation"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// UpdateReviewPayload replaces a queued remediation's review unit: the
|
||||
// reaction consumer appends late-arriving comments of the same review while
|
||||
// the unit is still pending. The guarded update is the boundary — once a
|
||||
// run is claimed, its input is frozen; a late comment is dropped by the
|
||||
// caller rather than racing the builder's mission.
|
||||
func (s *Store) UpdateReviewPayload(ctx context.Context, taskID int64, payload string) error {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET review_payload=?, updated_at=datetime('now')
|
||||
WHERE id=? AND status=? AND workflow=?`,
|
||||
clip(payload, 4000), taskID, workflow.StatusQueued, "remediate")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetReviewCursors persists the poll backstop's per-task high-water marks.
|
||||
// The pr_open guard keeps a cursor from moving while a remediation run owns
|
||||
// the task: the run's next round re-reads the cursors the scan wrote, and a
|
||||
// write under a running task would desync them from what was consumed.
|
||||
func (s *Store) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET review_cursor=?, watch_comment_id=?, updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`,
|
||||
reviewCursor, commentCursor, taskID, workflow.StatusPROpen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
return 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) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at
|
||||
FROM tasks WHERE owner=? AND repo=? AND issue_number=?`, owner, repo, number)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
// 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) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE owner=? AND repo=? AND pr_number=? AND status=?`,
|
||||
owner, repo, number, workflow.StatusPROpen)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
// TaskByID returns the task with the given database ID, or nil. Used by
|
||||
// chat controls (/approve, /cancel) that reference a task by its real
|
||||
// ID rather than a forge issue number.
|
||||
func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, error) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
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, retry_count,
|
||||
source, identity, binding_id, binding_version, review_payload,
|
||||
workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, park_class, remediation_rounds, created_at, updated_at
|
||||
FROM tasks WHERE id=?`, taskID)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
// Requeue puts a task back on the queue. A non-empty workflow forces it
|
||||
// (the waiting_human → approved → implement handoff); empty keeps the
|
||||
// task's current workflow (retrying a parked task).
|
||||
// The fromStatus acts as a guard: the UPDATE only affects rows whose
|
||||
// current status matches fromStatus. If no row matches, ErrStaleTransition
|
||||
// is returned and no audit row is written. Both statements execute in a
|
||||
// single transaction.
|
||||
func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE tasks SET status=?,
|
||||
workflow=CASE WHEN ?='' THEN workflow ELSE ? END,
|
||||
stage='', park_reason='', park_class='needs_human', updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`, workflow.StatusQueued, wf, wf, taskID, fromStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx,
|
||||
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
||||
taskID, fromStatus, workflow.StatusQueued, "requeued "+wf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// RetryTask requeues a task and increments retry_count in the same guarded
|
||||
// transaction. A queued task with an uncounted retry would make max_retries a
|
||||
// suggestion rather than a cap.
|
||||
func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE tasks SET status=?, retry_count=retry_count+1,
|
||||
workflow=CASE WHEN ?='' THEN workflow ELSE ? END,
|
||||
stage='', park_reason='', park_class='needs_human', updated_at=datetime('now')
|
||||
WHERE id=? AND status=?`, workflow.StatusQueued, wf, wf, taskID, fromStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrStaleTransition
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
||||
taskID, fromStatus, workflow.StatusQueued, "retried "+wf); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ArchiveTask removes one terminal task from the active task board. The
|
||||
// expected status is part of the delete predicate so a stale browser cannot
|
||||
// remove a task that has since resumed or otherwise changed state. Historical
|
||||
// transition and activity rows remain as the operator audit trail.
|
||||
func (s *Store) ArchiveTask(
|
||||
ctx context.Context,
|
||||
taskID int64,
|
||||
fromStatus string,
|
||||
audit events.Event,
|
||||
) (int64, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
eventID, err := insertEvent(ctx, tx, audit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, `DELETE FROM tasks WHERE id=? AND status=?`, taskID, fromStatus)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, ErrStaleTransition
|
||||
}
|
||||
if err := tx.Commit(); 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) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`UPDATE tasks SET status=?, updated_at=datetime('now') WHERE status=?`,
|
||||
workflow.StatusQueued, workflow.StatusRunning)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// OpenPRs returns tasks whose PR state should be reconciled with GitHub.
|
||||
//
|
||||
// It carries attempt deliberately: the reconcile loop attributes pr_merged and
|
||||
// pr_rejected to the attempt that opened the PR, and it reaches the state store
|
||||
// over the wire, so the row must carry the value for taskProto to publish it.
|
||||
// Narrowing this projection back silently sends those events out unattributed.
|
||||
func (s *Store) OpenPRs(ctx context.Context) (tasks []workflow.Task, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt,
|
||||
review_cursor, watch_comment_id
|
||||
FROM tasks WHERE status=?`, workflow.StatusPROpen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
retErr = errors.Join(retErr, rows.Close())
|
||||
}()
|
||||
for rows.Next() {
|
||||
var t workflow.Task
|
||||
if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.PRNumber,
|
||||
&t.Status, &t.Source, &t.Identity, &t.Attempt,
|
||||
&t.ReviewCursor, &t.WatchCommentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks = append(tasks, t)
|
||||
}
|
||||
return tasks, rows.Err()
|
||||
}
|
||||
|
||||
// ClearTerminalTasks deletes tasks whose status is terminal. Parked work is
|
||||
// deliberately excluded because it is recoverable.
|
||||
func (s *Store) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM tasks WHERE status IN (?, ?, ?, ?)`,
|
||||
workflow.StatusMerged, workflow.StatusRejected, workflow.StatusDead, workflow.StatusClosedWontDo)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
func clip(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
// Walk forward by runes; stop when the next rune would exceed n bytes.
|
||||
pos := 0
|
||||
for pos < len(s) {
|
||||
_, size := utf8.DecodeRuneInString(s[pos:])
|
||||
if pos+size > n {
|
||||
break
|
||||
}
|
||||
pos += size
|
||||
}
|
||||
return s[:pos]
|
||||
}
|
||||
|
||||
// sqliteTime scans SQLite's TEXT datetime columns into a time.Time.
|
||||
//
|
||||
// The tasks table stores timestamps as TEXT via datetime('now') rather than a
|
||||
// typed column, so the driver hands back a string and the standard time.Time
|
||||
// scan fails. Parsing here keeps the column definition untouched -- changing
|
||||
// it would need a migration over existing rows for no behavioural gain.
|
||||
type sqliteTime struct{ t *time.Time }
|
||||
|
||||
// sqliteTimeLayout is what datetime('now') produces: UTC, no zone suffix.
|
||||
const sqliteTimeLayout = "2006-01-02 15:04:05"
|
||||
|
||||
func (s sqliteTime) Scan(v any) error {
|
||||
switch value := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case time.Time:
|
||||
*s.t = value.UTC()
|
||||
return nil
|
||||
case []byte:
|
||||
return s.parse(string(value))
|
||||
case string:
|
||||
return s.parse(value)
|
||||
default:
|
||||
return fmt.Errorf("store: cannot scan %T into time", v)
|
||||
}
|
||||
}
|
||||
|
||||
func (s sqliteTime) parse(v string) error {
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
// RFC3339 first: rows written by other paths may carry a zone.
|
||||
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, sqliteTimeLayout} {
|
||||
if parsed, err := time.Parse(layout, v); err == nil {
|
||||
*s.t = parsed.UTC()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("store: unrecognised time %q", v)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,81 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// Tasks() is what the dashboard's task list is built from, and the UI reads
|
||||
// two fields the query never selected: Plan, which gates the "Decision
|
||||
// required" panel on a waiting_human task, and Source, which decides whether
|
||||
// to render a forge issue link at all.
|
||||
//
|
||||
// Both came back as the zero value, so the panel could never appear and a
|
||||
// chat-sourced task -- whose issue_number is synthetic -- rendered a link to
|
||||
// an issue that does not exist. Neither failed loudly; the features were
|
||||
// simply dead.
|
||||
func TestTasksSelectsFieldsTheUIReads(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := OpenTest(t)
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "o", "r", 7, "forge task", "body", "", ""); err != nil {
|
||||
t.Fatalf("EnqueueIssue: %v", err)
|
||||
}
|
||||
forgeTaskRow, err := s.TaskByIssue(ctx, "o", "r", 7)
|
||||
if err != nil || forgeTaskRow == nil {
|
||||
t.Fatalf("TaskByIssue: %v", err)
|
||||
}
|
||||
forgeID := forgeTaskRow.ID
|
||||
|
||||
chatTaskRow, err := s.EnqueueChatTask(ctx, "o", "r", "chat task", "body", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("EnqueueChatTask: %v", err)
|
||||
}
|
||||
chatID := chatTaskRow.ID
|
||||
|
||||
// A plan is what an operator approves or rejects, so it has to survive
|
||||
// the read the approval UI is built on.
|
||||
const plan = "1. add the thing\n2. test the thing"
|
||||
forgeTaskRow.Plan = plan
|
||||
if err := s.Update(ctx, forgeTaskRow); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
|
||||
tasks, err := s.Tasks(ctx, 50)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
|
||||
byID := make(map[int64]workflow.Task, len(tasks))
|
||||
for _, task := range tasks {
|
||||
byID[task.ID] = task
|
||||
}
|
||||
|
||||
forgeTask, ok := byID[forgeID]
|
||||
if !ok {
|
||||
t.Fatalf("forge task %d missing from Tasks()", forgeID)
|
||||
}
|
||||
if forgeTask.Plan != plan {
|
||||
t.Errorf("Plan = %q, want %q: the approval panel is gated on this and "+
|
||||
"can never render while it is empty", forgeTask.Plan, plan)
|
||||
}
|
||||
if forgeTask.Source != workflow.SourceForge {
|
||||
t.Errorf("Source = %q, want %q", forgeTask.Source, workflow.SourceForge)
|
||||
}
|
||||
|
||||
chatTask, ok := byID[chatID]
|
||||
if !ok {
|
||||
t.Fatalf("chat task %d missing from Tasks()", chatID)
|
||||
}
|
||||
if chatTask.Source != workflow.SourceChat {
|
||||
t.Errorf("Source = %q, want %q: without it the UI links a synthetic "+
|
||||
"issue number to a forge issue that does not exist",
|
||||
chatTask.Source, workflow.SourceChat)
|
||||
}
|
||||
if chatTask.IsForgeBacked() {
|
||||
t.Error("IsForgeBacked() is true for a chat task read through Tasks()")
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestTaskTimestampsRoundTrip pins that a task carries usable created/updated
|
||||
// times through every read path. They are what the dashboard shows a task's
|
||||
// age from; before this they existed as SQLite columns but were never selected,
|
||||
// so the API could not answer "how long has this been running".
|
||||
func TestTaskTimestampsRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := OpenTest(t)
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
|
||||
before := time.Now().UTC().Add(-2 * time.Second)
|
||||
if _, err := s.EnqueueIssue(ctx, "o", "r", 1, "title", "body", "", ""); err != nil {
|
||||
t.Fatalf("EnqueueIssue: %v", err)
|
||||
}
|
||||
after := time.Now().UTC().Add(2 * time.Second)
|
||||
|
||||
inRange := func(t *testing.T, label string, got time.Time) {
|
||||
t.Helper()
|
||||
if got.IsZero() {
|
||||
t.Errorf("%s is zero; the column was not selected", label)
|
||||
return
|
||||
}
|
||||
if got.Before(before) || got.After(after) {
|
||||
t.Errorf("%s = %s, want within [%s, %s]", label, got, before, after)
|
||||
}
|
||||
}
|
||||
|
||||
// Every read path that returns a Task must populate them.
|
||||
t.Run("TaskByIssue", func(t *testing.T) {
|
||||
task, err := s.TaskByIssue(ctx, "o", "r", 1)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue = (%v, %v)", task, err)
|
||||
}
|
||||
inRange(t, "CreatedAt", task.CreatedAt)
|
||||
inRange(t, "UpdatedAt", task.UpdatedAt)
|
||||
})
|
||||
|
||||
t.Run("Tasks list", func(t *testing.T) {
|
||||
tasks, err := s.Tasks(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 1 {
|
||||
t.Fatalf("got %d tasks, want 1", len(tasks))
|
||||
}
|
||||
inRange(t, "CreatedAt", tasks[0].CreatedAt)
|
||||
inRange(t, "UpdatedAt", tasks[0].UpdatedAt)
|
||||
})
|
||||
|
||||
t.Run("ClaimNext", func(t *testing.T) {
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("ClaimNext = (%v, %v)", task, err)
|
||||
}
|
||||
inRange(t, "CreatedAt", task.CreatedAt)
|
||||
inRange(t, "UpdatedAt", task.UpdatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
// TestSQLiteTimeScan pins the scanner that makes the above possible: SQLite
|
||||
// stores these as TEXT via datetime('now'), so the driver hands back a string
|
||||
// and a plain time.Time scan fails.
|
||||
func TestSQLiteTimeScan(t *testing.T) {
|
||||
want := time.Date(2026, 8, 4, 9, 30, 15, 0, time.UTC)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input any
|
||||
want time.Time
|
||||
wantErr bool
|
||||
}{
|
||||
{"datetime('now') text", "2026-08-04 09:30:15", want, false},
|
||||
{"same value as bytes", []byte("2026-08-04 09:30:15"), want, false},
|
||||
{"rfc3339 from another writer", "2026-08-04T09:30:15Z", want, false},
|
||||
{"driver already gave a time", want, want, false},
|
||||
{"null leaves the zero value", nil, time.Time{}, false},
|
||||
{"empty string leaves the zero value", "", time.Time{}, false},
|
||||
{"unrecognised text is an error", "not a time", time.Time{}, true},
|
||||
{"unsupported type is an error", 42, time.Time{}, true},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var got time.Time
|
||||
err := sqliteTime{&got}.Scan(tc.input)
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("Scan(%v) = nil error, want an error", tc.input)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Scan(%v): %v", tc.input, err)
|
||||
}
|
||||
if !got.Equal(tc.want) {
|
||||
t.Errorf("got %s, want %s", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -43,49 +43,17 @@ configured_agent_image() {
|
||||
printf '%s' "$image"
|
||||
}
|
||||
|
||||
# The task store is `<db_path>-tasks.sqlite`, not db_path itself: the daemon
|
||||
# derives it with taskDBPath() in internal/app/archied/main.go, and the
|
||||
# configured path may be a zero-byte placeholder while the real store sits
|
||||
# beside it. Backing up the literal value would produce an empty backup, and a
|
||||
# rollback would then restore that empty file over the database and delete its
|
||||
# -wal/-shm -- destroying the task store it exists to protect.
|
||||
#
|
||||
# Only the top-level key counts. db_path can also appear under a TOML table
|
||||
# (the retired [indexing] section once owned its own database), and the first
|
||||
# match in the file can
|
||||
# easily be one of those.
|
||||
configured_task_db_path() {
|
||||
local base
|
||||
base="$(awk '
|
||||
BEGIN { top = 1 }
|
||||
/^[[:space:]]*\[/ { top = 0 }
|
||||
top && /^[[:space:]]*db_path[[:space:]]*=/ {
|
||||
value = $0
|
||||
sub(/^[[:space:]]*db_path[[:space:]]*=[[:space:]]*/, "", value)
|
||||
sub(/[[:space:]]*(#.*)?$/, "", value)
|
||||
if (value ~ /^".*"$/) { sub(/^"/, "", value); sub(/"$/, "", value); print value }
|
||||
else if (length(value) >= 2 && substr(value, 1, 1) == sprintf("%c", 39) && substr(value, length(value), 1) == sprintf("%c", 39)) print substr(value, 2, length(value) - 2)
|
||||
exit
|
||||
}
|
||||
' "$CONFIG_PATH" 2>/dev/null || true)"
|
||||
[ -n "$base" ] || return 0
|
||||
printf '%s' "$base-tasks.sqlite"
|
||||
}
|
||||
|
||||
AGENT_IMAGE="${ARCHIE_AGENT_IMAGE:-$(configured_agent_image)}"
|
||||
AGENT_IMAGE="${AGENT_IMAGE:-ghcr.io/samcharles93/archie-agent:latest}"
|
||||
AGENT_IMAGE_BACKUP="$BIN_DIR/archie-agent-image.prev.tar"
|
||||
# State for the managed image, independent of archied's daemon build stamp.
|
||||
AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
|
||||
AGENT_VERSION_BACKUP="${ARCHIE_AGENT_VERSION_BACKUP:-$BIN_DIR/archie-agent.version.prev}"
|
||||
TASK_DB_PATH="${ARCHIE_TASK_DB_PATH:-$(configured_task_db_path)}"
|
||||
TASK_DB_BACKUP="${ARCHIE_TASK_DB_BACKUP:-$BIN_DIR/archie-task-db.prev.sqlite}"
|
||||
DB_SNAPSHOT="${ARCHIE_DB_SNAPSHOT:-$BIN_DIR/archie-db.pre-update.dump}"
|
||||
# The address the daemon dials for its state, so the watchdog can wait for it to
|
||||
# accept connections before it starts archied. Read with the same narrow awk as
|
||||
# the task database path and for the same reason: the daemon stays the authority
|
||||
# on validation, and this script deliberately understands one scalar rather than
|
||||
# parsing TOML. Scoped to [services.state] so the Gateway's own target cannot be
|
||||
# accept connections before it starts archied. Read with a narrow awk: the
|
||||
# daemon stays the authority on validation, and this script deliberately
|
||||
# understands one scalar rather than parsing TOML. Scoped to [services.state] so the Gateway's own target cannot be
|
||||
# mistaken for it, and exact on the key so target_token is not.
|
||||
configured_state_store_target() {
|
||||
awk '
|
||||
@@ -263,35 +231,14 @@ if [[ ",$components," == *,daemon,* ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Pre-flight, before any write to the host. The task database backup is the
|
||||
# watchdog's only route back from a forward schema migration, so an update
|
||||
# that cannot take one must not start: once binaries are replaced, a failure
|
||||
# leaves the previous release unable to read the store it just migrated. This
|
||||
# runs ahead of the adapter refresh below so a refusal changes nothing at all.
|
||||
#
|
||||
# The snapshot is taken by the recovery subcommand of the release being
|
||||
# installed, from its own checkout, rather than by hand-rolled sqlite3 shell:
|
||||
# the operator running `archie-state-store backup` by hand and the installer
|
||||
# are then the same implementation, and the two cannot drift. The installed
|
||||
# binary is the previous release, which may predate the subcommand, so it
|
||||
# cannot be the one asked -- and a release whose recovery path will not build is a
|
||||
# release that cannot back up, which is exactly what this pre-flight refuses.
|
||||
if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_DB_PATH" ]; then
|
||||
echo "==> backing up task database for schema-safe rollback"
|
||||
mkdir -p "$(dirname "$TASK_DB_BACKUP")"
|
||||
if ! go run ./cmd/archie-state-store backup -db "$TASK_DB_PATH" -out "$TASK_DB_BACKUP"; then
|
||||
echo "refusing to install $GW: could not back up the task database at $TASK_DB_PATH, so a failed update could not be rolled back" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# The pre-migration snapshot of the PostgreSQL database: the whole database,
|
||||
# since every service shares it. The new State Store migrates on start and a
|
||||
# migration is never undone automatically (goose Down drops tables), so this
|
||||
# snapshot is the only way back, restored by an operator with every service
|
||||
# stopped. The recovery subcommand resolves database_url from the same config
|
||||
# the services boot with, so the connection string never passes through this
|
||||
# script. No snapshot, no update.
|
||||
# script. No snapshot, no update: this pre-flight runs before any write to the
|
||||
# host, so a refusal changes nothing at all.
|
||||
if [[ ",$components," == *,daemon,* ]]; then
|
||||
echo "==> snapshotting the database before the update migrates it"
|
||||
mkdir -p "$(dirname "$DB_SNAPSHOT")"
|
||||
@@ -392,8 +339,6 @@ systemd-run --user --unit=archied-update-restart --collect --quiet \
|
||||
--setenv=ARCHIE_AGENT_VERSION_FILE="$AGENT_VERSION_FILE" \
|
||||
--setenv=ARCHIE_AGENT_VERSION_BACKUP="$AGENT_VERSION_BACKUP" \
|
||||
--setenv=ARCHIE_AGENT_VERSION_PREVIOUS_EXISTS="${AGENT_VERSION_PREVIOUS_EXISTS:-0}" \
|
||||
--setenv=ARCHIE_TASK_DB_PATH="$TASK_DB_PATH" \
|
||||
--setenv=ARCHIE_TASK_DB_BACKUP="$TASK_DB_BACKUP" \
|
||||
--setenv=ARCHIE_DB_SNAPSHOT="$DB_SNAPSHOT" \
|
||||
--setenv=ARCHIE_CONFIG_PATH="$CONFIG_PATH" \
|
||||
--setenv=ARCHIE_UPDATE_STATE_STORE_TARGET="$STATE_STORE_TARGET" \
|
||||
|
||||
@@ -13,8 +13,6 @@ AGENT_IMAGE_BACKUP="${ARCHIE_AGENT_IMAGE_BACKUP:-$BIN_DIR/archie-agent-image.pre
|
||||
AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
|
||||
AGENT_VERSION_BACKUP="${ARCHIE_AGENT_VERSION_BACKUP:-$BIN_DIR/archie-agent.version.prev}"
|
||||
AGENT_VERSION_PREVIOUS_EXISTS="${ARCHIE_AGENT_VERSION_PREVIOUS_EXISTS:-0}"
|
||||
TASK_DB_PATH="${ARCHIE_TASK_DB_PATH:-}"
|
||||
TASK_DB_BACKUP="${ARCHIE_TASK_DB_BACKUP:-$BIN_DIR/archie-task-db.prev.sqlite}"
|
||||
DB_SNAPSHOT="${ARCHIE_DB_SNAPSHOT:-$BIN_DIR/archie-db.pre-update.dump}"
|
||||
CONFIG_PATH="${ARCHIE_CONFIG_PATH:-${XDG_CONFIG_HOME:-$HOME/.config}/archie/config.toml}"
|
||||
# The daemon exports its own health address (releaseupdate.CommandInstaller);
|
||||
@@ -206,20 +204,6 @@ rollback_agent_version() {
|
||||
fi
|
||||
}
|
||||
|
||||
rollback_task_db() {
|
||||
if [ -z "$TASK_DB_PATH" ]; then
|
||||
echo "==> no task database path; cannot restore schema-compatible state" >&2
|
||||
return 1
|
||||
fi
|
||||
if [ ! -f "$TASK_DB_BACKUP" ]; then
|
||||
echo "==> no task database backup; cannot restore schema-compatible state" >&2
|
||||
return 1
|
||||
fi
|
||||
sqlite3 "$TASK_DB_BACKUP" "PRAGMA wal_checkpoint(TRUNCATE);" >/dev/null 2>&1 || true
|
||||
cp -f "$TASK_DB_BACKUP" "$TASK_DB_PATH"
|
||||
rm -f "$TASK_DB_PATH-wal" "$TASK_DB_PATH-shm"
|
||||
}
|
||||
|
||||
# explain_database_recovery states what a daemon rollback does not do. The
|
||||
# PostgreSQL database is never restored automatically: goose Down drops tables,
|
||||
# and a restore is destructive, loses every write after the snapshot and needs
|
||||
@@ -240,15 +224,14 @@ fi
|
||||
|
||||
echo "==> health check failed after ${HEALTH_TIMEOUT_SECONDS}s; rolling back changed components"
|
||||
rolled_back=false
|
||||
# Stop before touching either the executable or its database. The service
|
||||
# uses Restart=on-failure in the supported unit, so copying rollback state
|
||||
# while a crash-loop child is alive can race with SQLite and execute the new
|
||||
# binary after its files have been replaced.
|
||||
# Stop before touching the executable. The service uses Restart=on-failure in
|
||||
# the supported unit, so copying rollback state while a crash-loop child is
|
||||
# alive can execute the new binary after its files have been replaced.
|
||||
if stop_units; then
|
||||
case "$COMPONENTS" in
|
||||
daemon)
|
||||
if [ -f "$BIN_DIR/archied.prev" ] && { [ -z "$TASK_DB_PATH" ] || [ -f "$TASK_DB_BACKUP" ]; }; then
|
||||
if { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_gateway; then
|
||||
if [ -f "$BIN_DIR/archied.prev" ]; then
|
||||
if rollback_gateway; then
|
||||
rolled_back=true
|
||||
fi
|
||||
else
|
||||
@@ -267,8 +250,8 @@ if stop_units; then
|
||||
daemon,agent)
|
||||
# Preserve the old all-or-nothing rollback guarantee: never restore one
|
||||
# component and leave the other on the failed release.
|
||||
if [ -f "$BIN_DIR/archied.prev" ] && [ -f "$AGENT_IMAGE_BACKUP" ] && { [ -z "$TASK_DB_PATH" ] || [ -f "$TASK_DB_BACKUP" ]; }; then
|
||||
if rollback_agent_image && rollback_agent_version && { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_gateway; then
|
||||
if [ -f "$BIN_DIR/archied.prev" ] && [ -f "$AGENT_IMAGE_BACKUP" ]; then
|
||||
if rollback_agent_image && rollback_agent_version && rollback_gateway; then
|
||||
rolled_back=true
|
||||
fi
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user