Commit Graph
3 Commits
Author SHA1 Message Date
sam 912db8f590 fix(binding): split ValidateForUpdate so edits can preserve the secret
Real bug found via browser-testing the new bindings UI (t2db.8):
UpdateBinding's own store-layer contract documents that an empty
Secret means "preserve the existing one" (COALESCE against NULLIF),
but handleBindingUpdate validated with the create-time Validate(),
which unconditionally requires a full-length secret -- rejecting the
exact "leave blank to keep it" edit flow the store was built to allow.

Root-cause fix, not a UI workaround: Binding.Validate() (create,
unchanged, still requires a real secret -- there's nothing to fall
back to on insert) now delegates shared field checks to a private
validateCommon(); a new ValidateForUpdate() reuses the same checks but
only enforces the secret-length floor when the caller actually
supplied a new one. handleBindingUpdate now calls ValidateForUpdate.

3 new domain tests + 1 new API-layer test pin: empty secret accepted
on update, a genuinely-short new secret still rejected on update,
empty secret still rejected on create.
2026-09-03 06:17:43 +10:00
sam 7705fda777 fix(binding): add Owner/Repo pin, fixing multi-repo dispatch
Root-cause fix for the correctness gap named in archie-core-t2db.8:
resolveBindingRepo refused to dispatch with zero or multiple configured
repos, meaning a multi-repo deployment could never use bindings at all.

- binding.Binding gains optional Owner/Repo fields; Validate rejects
  setting only one (a pin is a complete pair or not a pin at all).
- Persisted as new owner/repo columns (NOT NULL DEFAULT '', additive --
  no migration needed since bindings is a CREATE TABLE IF NOT EXISTS
  with no existing deployment yet).
- resolveBindingRepo now takes the binding: a pinned binding dispatches
  to its own owner/repo unconditionally; an unpinned binding keeps
  today's single-configured-repo fallback (0 or 2+ repos still refuses,
  unchanged -- verified by a dedicated regression test, not just
  assumed preserved).
- Wired through the API request/response shape (create/update accept
  and return owner/repo); no repo-existence validation against config
  added -- smallest workable change, not the named bug.

Red-green throughout: 8 new tests across domain/store/daemon/webui
layers, each proven to fail before the corresponding implementation
step. Zero regressions in existing binding/dispatch test suites.
Dashboard UI to expose this (the other half of t2db.8) is unstarted --
this closes the backend correctness gap only.
2026-09-03 04:38:45 +10:00
sam 543c10ac9a feat(webui): playbook bindings -- matcher + mapping + workflow, HMAC-gated
Implements archie-core-t2db.4: the runtime-editable binding entity that
turns a captured webhook into an archie task, plus the per-source HMAC
verification at handleCapture that makes t2db.5's 'unauthenticated event
cannot trigger a binding' invariant structurally enforceable.

Captures, mappings and bindings are now separate concerns on the same
single *store.Store; the new BindingStore / BindingDispatcher /
BindingTaskCreator split keeps each interface under the interfacebloat
limit (8 methods).

State machine: draft -> pending_approval -> armed; any edit on an armed
binding drops it back to pending_approval, enforced in SQL via
status='pending_approval' on UPDATE so the rule cannot be bypassed by a
direct status field in the request body. Approve is the only sanctioned
transition into armed (WHERE status='pending_approval' guard).

Overlap: two bindings may not share a source in any status; enforced at
InsertBinding and UpdateBinding (transaction-local SELECT). Approve has
a separate TOCTOU guard so a race between Insert and Approve cannot
// bypass the check.

HMAC at handleCapture: reads X-Hub-Signature-256 (then X-Signature-256)
// fallback, verifies via webhookguard.VerifyHMAC against the binding's
// per-source secret, marks CapturedEvent.Authenticated=true on match.
// Captures with no armed binding, missing signature, or wrong signature
all persist as authenticated=false -- the dispatch loop's
binding.Matcher.Matches gate (requires authenticated) is what stops
them from spawning a task. Two armed bindings for one source returns
409 at capture time as a belt-and-braces TOCTOU guard.

Dispatch loop: added Daemon.dispatchBindings, called from both Cycle
and maintainAndDrain between poll and drain. Walks
captured_events where authenticated=1 AND source IN armed bindings
AND id NOT IN binding_dispatches, runs the binding's matcher, resolves
the mapping, enqueues a chat-style task with binding_id /
// binding_version provenance stamped in the same transaction as the
// row insert. The binding_dispatches ledger (composite PK on
(binding_id, capture_id)) is the at-most-once guarantee across cycles
// and daemon restarts.

Tasks schema migration: binding_id INTEGER NOT NULL DEFAULT 0 +
binding_version INTEGER NOT NULL DEFAULT 0 (taskSchemaVersion bumped
1 -> 2). Existing tasks keep 0 / 0.

Out of scope (documented follow-ups):
- secret encryption at rest (plaintext today)
- full dashboard HTML for the binding editor (CRUD API only this bead)
- per-binding rate limits (existing per-remote-address limit applies)
- binding.Owner / binding.Repo fields (dispatch falls back to the
  single configured repo; multi-repo deployments log and skip)
2026-09-02 23:09:25 +10:00