Files
archie/internal/infrastructure/edastore/contracts.go
T
sam ed1557c12e feat(edastore)!: move event capture and bindings onto PocketBase collections
The event-capture tables (captures, mappings, bindings, binding_dispatches,
playbook_dispatches) now live in PocketBase-owned collections and their ids
are opaque record ids rather than autoincrement integers. The task lifecycle
stays on SQLite: tasks/transitions/events carry FIFO claim ordering, id-cursor
pagination and the task-grant map, none of which gain anything from being
hand-editable. binding_dispatches.task_id is the single seam and stays an
integer addressing that store.

BREAKING: CapturedEvent.Id, Mapping.Id, Binding.Id and Binding.MappingId are
strings on the wire, as are the ids in the capture/mapping/binding requests.
RecordDispatchRequest.task_id is deliberately unchanged.

What the collections buy, beyond storage: a payload nobody has modelled yet is
inspectable, a mapping is editable at runtime, and a binding's relation to its
mapping is enforced. The old schema declared no FOREIGN KEY on that link, so a
dangling mapping_id was previously possible and is now refused.

Behaviour preserved deliberately, each recovered from the SQLite version
rather than reimplemented from memory:
  - draft -> pending_approval -> armed, with approval only from
    pending_approval and an edit always returning a binding for re-approval
  - one binding per source, guarded on insert, update and approve
  - an empty secret on update keeps the stored one, so an edit form that does
    not echo it back cannot silently disarm a binding
  - ErrMappingNotFound / ErrBindingNotFound on a missing delete target
  - sentinels are re-exported from storecontract, never redefined: staterpc
    matches on exact canonical message to rehydrate them, so a second
    definition would break errors.Is across the wire invisibly

Two implementation notes worth keeping:
  - at-most-once dispatch is now a unique index rather than INSERT OR IGNORE.
    Only the unique-violation maps to ErrAlreadyDispatched; any other save
    error is returned, because treating them all as "already recorded" would
    turn the ledger into a silent at-least-once dispatcher.
  - captures store received_at themselves at nanosecond precision in a
    fixed-width layout. PocketBase autodates are millisecond and record ids
    are random, so same-millisecond captures would otherwise order
    arbitrarily; the old store got insertion order free from autoincrement.
  - ListUndispatchedCaptures is raw SQL: the anti-join is a subquery and
    PocketBase's filter DSL is not SQL. A read fires no hooks either way.

Adds tool_calls, which had no persistence at all: tool calls only ever reached
the on-disk task log, readable as text but never queryable or watchable.
Nothing writes it yet.

The binding secret cipher moves with the bindings it protects.
2026-09-23 02:19:28 +10:00

191 lines
6.6 KiB
Go

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,
}
}