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.
92 lines
3.2 KiB
Go
92 lines
3.2 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/logging"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
// A decoded task-log page must never carry a nil slice for a collection field.
|
|
// Go marshals a nil slice to JSON null, and null is not an empty list: a
|
|
// client that reads the field as an array sees a different type than the same
|
|
// route serves when the collection is non-empty.
|
|
//
|
|
// This is the nil-slice class the contract-conformance audit deferred (its
|
|
// named revisit condition is a handler that can produce nil). The local reader
|
|
// already normalises both fields; the wire mapping did not, because protobuf
|
|
// decodes an empty `repeated` field to nil, so the grpc path regressed what the
|
|
// local path got right. Asserting the JSON shape rather than the slice catches
|
|
// it on both paths without depending on which one is under test.
|
|
func TestTaskLogPageCarriesNoNilCollectionFields(t *testing.T) {
|
|
// Both collections must be exercised empty. A log with entries but no
|
|
// structured "component" field leaves only Components empty; an attempt
|
|
// whose log file exists but holds no lines leaves only Entries empty, and
|
|
// that second shape is what catches an empty-slice mapping regressing on
|
|
// the entries side.
|
|
for _, tc := range []struct {
|
|
name string
|
|
write bool
|
|
}{
|
|
{name: "entries present, components empty", write: true},
|
|
{name: "entries empty, components empty", write: false},
|
|
} {
|
|
for _, mode := range []string{"local", "grpc"} {
|
|
t.Run(tc.name+"/"+mode, func(t *testing.T) {
|
|
ctx := t.Context()
|
|
baseDir := filepath.Join(t.TempDir(), "logs", "tasks")
|
|
reader := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
|
|
|
|
const taskID = 7
|
|
if err := reader.Open(taskID, 0); err != nil {
|
|
t.Fatalf("open sink: %v", err)
|
|
}
|
|
if tc.write {
|
|
// No "component" field at all: Components stays empty while
|
|
// Entries does not.
|
|
reader.Write(ctx, taskID, logging.Entry{Level: "INFO", Message: "starting"})
|
|
}
|
|
if err := reader.Close(taskID); err != nil {
|
|
t.Fatalf("close sink: %v", err)
|
|
}
|
|
|
|
var c taskLogContract = reader
|
|
if mode == "grpc" {
|
|
c = remoteTaskStore(t, store.OpenTest(t), reader, nil)
|
|
}
|
|
|
|
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{})
|
|
if err != nil {
|
|
t.Fatalf("TaskLog: %v", err)
|
|
}
|
|
if !page.Found {
|
|
t.Fatal("Found = false for an attempt whose log file exists")
|
|
}
|
|
if tc.write && len(page.Entries) != 1 {
|
|
t.Fatalf("entries = %d, want the line the sink wrote", len(page.Entries))
|
|
}
|
|
if !tc.write && len(page.Entries) != 0 {
|
|
t.Fatalf("entries = %d, want none for a log with no lines", len(page.Entries))
|
|
}
|
|
|
|
raw, err := json.Marshal(page)
|
|
if err != nil {
|
|
t.Fatalf("marshal page: %v", err)
|
|
}
|
|
var decoded map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &decoded); err != nil {
|
|
t.Fatalf("unmarshal page: %v", err)
|
|
}
|
|
for _, field := range []string{"entries", "components"} {
|
|
if got := string(decoded[field]); got == "null" {
|
|
t.Errorf("%s marshalled to null, want an empty list: the field's type must not depend on whether it has contents",
|
|
field)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|