Files
archie/internal/webui/api_mapping.go
T
sam ba52a34d83 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

234 lines
7.5 KiB
Go

package webui
import (
"context"
"encoding/json"
"errors"
"net/http"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// mappingRequest is the wire shape POST /api/mappings and
// PATCH /api/mappings/{id} both accept. ID/CreatedAt/UpdatedAt are
// server-assigned, never taken from the request body.
type mappingRequest struct {
Name string `json:"name"`
SourceHint string `json:"source_hint"`
Fields []mapping.Field `json:"fields"`
}
func (s *Server) handleMappingsList(w http.ResponseWriter, r *http.Request) {
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
mappings, err := s.Mappings.ListMappings(r.Context())
if err != nil {
s.Log.Error("list mappings", "err", err)
http.Error(w, "list mappings failed", http.StatusInternalServerError)
return
}
writeJSON(w, map[string]any{"mappings": mappings})
}
func (s *Server) handleMappingCreate(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
var req mappingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
m := mapping.Mapping{Name: req.Name, SourceHint: req.SourceHint, Fields: req.Fields}
if err := m.Validate(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := s.Mappings.InsertMapping(r.Context(), m)
if err != nil {
s.Log.Error("insert mapping", "err", err)
http.Error(w, "create mapping failed", http.StatusInternalServerError)
return
}
created, err := s.Mappings.GetMapping(r.Context(), id)
if err != nil || created == nil {
s.Log.Error("get mapping after insert", "err", err, "id", id)
http.Error(w, "create mapping failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
writeJSON(w, created)
}
func (s *Server) handleMappingGet(w http.ResponseWriter, r *http.Request) {
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
id := r.PathValue("id")
if id == "" {
http.Error(w, "invalid mapping id", http.StatusBadRequest)
return
}
m, err := s.Mappings.GetMapping(r.Context(), id)
if err != nil {
s.Log.Error("get mapping", "err", err, "id", id)
http.Error(w, "get mapping failed", http.StatusInternalServerError)
return
}
if m == nil {
http.Error(w, "mapping not found", http.StatusNotFound)
return
}
writeJSON(w, m)
}
func (s *Server) handleMappingUpdate(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
id := r.PathValue("id")
if id == "" {
http.Error(w, "invalid mapping id", http.StatusBadRequest)
return
}
var req mappingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
m := mapping.Mapping{ID: id, Name: req.Name, SourceHint: req.SourceHint, Fields: req.Fields}
if err := m.Validate(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := s.Mappings.UpdateMapping(r.Context(), m); err != nil {
if errors.Is(err, storecontract.ErrMappingNotFound) {
http.Error(w, "mapping not found", http.StatusNotFound)
return
}
s.Log.Error("update mapping", "err", err, "id", id)
http.Error(w, "update mapping failed", http.StatusInternalServerError)
return
}
updated, err := s.Mappings.GetMapping(r.Context(), id)
if err != nil || updated == nil {
s.Log.Error("get mapping after update", "err", err, "id", id)
http.Error(w, "update mapping failed", http.StatusInternalServerError)
return
}
writeJSON(w, updated)
}
func (s *Server) handleMappingDelete(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
id := r.PathValue("id")
if id == "" {
http.Error(w, "invalid mapping id", http.StatusBadRequest)
return
}
if err := s.Mappings.DeleteMapping(r.Context(), id); err != nil {
if errors.Is(err, storecontract.ErrMappingNotFound) {
http.Error(w, "mapping not found", http.StatusNotFound)
return
}
s.Log.Error("delete mapping", "err", err, "id", id)
http.Error(w, "delete mapping failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// mappingPreviewRequest lets an operator resolve a candidate set of fields
// against a real captured event before saving anything -- Fields is loose
// input, not a saved mapping ID, per docs/prds/payload-field-mapping.md.
type mappingPreviewRequest struct {
CaptureID string `json:"capture_id"`
Fields []mapping.Field `json:"fields"`
}
func (s *Server) handleMappingPreview(w http.ResponseWriter, r *http.Request) {
if s.Mappings == nil {
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
return
}
if s.Captures == nil {
http.Error(w, "captures not configured", http.StatusServiceUnavailable)
return
}
var req mappingPreviewRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
capture, err := s.captureByID(r.Context(), req.CaptureID)
if err != nil {
s.Log.Error("get capture for preview", "err", err, "capture_id", req.CaptureID)
http.Error(w, "preview failed", http.StatusInternalServerError)
return
}
if capture == nil {
http.Error(w, "capture not found", http.StatusNotFound)
return
}
values, failures := mapping.Resolve(req.Fields, []byte(capture.Body))
if failures == nil {
failures = []mapping.Failure{}
}
writeJSON(w, map[string]any{"values": values, "failures": failures})
}
// captureByID finds one captured event by ID. CaptureStore exposes only
// ListCaptures (see docs/prds/event-capture-storage.md -- capture has
// exactly one reader, the dashboard list view, so a by-ID lookup was never
// needed until preview), so this scans the newest window rather than
// adding a new store method for a single low-volume caller.
func (s *Server) captureByID(ctx context.Context, id string) (*storecontract.CapturedEvent, error) {
captures, err := s.Captures.ListCaptures(ctx, mappingCaptureScanWindow(s.CaptureMaxEvents))
if err != nil {
return nil, err
}
for i := range captures {
if captures[i].ID == id {
return &captures[i], nil
}
}
return nil, nil
}
// captureScanWindowDefault bounds captureByID's linear scan when
// CaptureMaxEvents is unset (zero or negative, matching CaptureConfig's own
// "zero means default" convention). Matches CaptureConfig's documented
// default so an unconfigured deployment's scan window still covers its
// whole table.
const captureScanWindowDefault = 5000
// mappingCaptureScanWindow ties captureByID's scan bound to the operator's
// actual configured retention (CaptureMaxEvents), rather than a constant
// that silently drifts out of sync with it -- an operator raising
// [capture] max_events above the old hardcoded 5000 would otherwise make a
// genuinely still-retained capture 404 as "not found" during preview.
func mappingCaptureScanWindow(configuredMaxEvents int) int {
if configuredMaxEvents <= 0 {
return captureScanWindowDefault
}
return configuredMaxEvents
}