From eaa58dfe73126eeb4214ac23832bb8c2619df9ca Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 04:35:00 +1000 Subject: [PATCH] fix(daemon): guard binding dispatch loop against nil dispatcher/creator BindingDispatcher and BindingTaskCreator are documented "optional: nil disables the dispatch loop", same as Bindings, but dispatchBindings only checked Bindings before dereferencing the other two. Any composition that wires Bindings without also wiring both handles panicked on the first dispatch cycle. --- internal/daemon/daemon.go | 8 +++++--- internal/daemon/dispatch_bindings_test.go | 24 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 588c95beb..f814669f3 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -529,10 +529,12 @@ const bindingDispatchBatchLimit = 100 // pair, and dispatchOneBinding treats that as a normal "another cycle // raced us" outcome rather than an error. // -// Nil Bindings disables the loop (legacy behaviour: daemons built -// before t2db.4 Phase E had no playbook bindings). +// Nil Bindings, BindingDispatcher or BindingTaskCreator disables the +// loop (legacy behaviour: daemons built before t2db.4 Phase E had no +// playbook bindings, and each field's own doc comment promises this +// degrade-not-panic contract independently). func (d *Daemon) dispatchBindings(ctx context.Context) { - if d.Bindings == nil { + if d.Bindings == nil || d.BindingDispatcher == nil || d.BindingTaskCreator == nil { return } diff --git a/internal/daemon/dispatch_bindings_test.go b/internal/daemon/dispatch_bindings_test.go index 32994f344..e55951eaa 100644 --- a/internal/daemon/dispatch_bindings_test.go +++ b/internal/daemon/dispatch_bindings_test.go @@ -216,6 +216,30 @@ func TestDispatchBindingsNilBindingsIsNoOp(t *testing.T) { d.dispatchBindings(t.Context()) } +// TestDispatchBindingsNilDispatcherIsNoOp exercises the composition +// gap the daemon field comments claim is safe: Bindings is set (so an +// armed binding + a matching capture exist) but BindingDispatcher is +// nil. Both fields are documented "optional: nil disables the +// dispatch loop", so this must degrade silently rather than panic on +// the nil-interface call to ListUndispatchedCaptures. +func TestDispatchBindingsNilDispatcherIsNoOp(t *testing.T) { + s := openDispatchTestStore(t) + mappingID := seedMapping(t, s, "sentry", mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString}) + seedArmedBinding(t, s, "sentry", mappingID) + seedCapture(t, s, "sentry", true, `{"title":"hello"}`) + + d := &Daemon{ + Cfg: config.NewHolder(config.Config{Repos: []config.Repo{{Owner: "acme", Name: "widget"}}}), + Store: s, + Mappings: s, + Bindings: s, + // BindingDispatcher and BindingTaskCreator intentionally nil. + Log: slog.New(slog.NewTextHandler(os.Stderr, nil)), + } + // Should not panic. + d.dispatchBindings(t.Context()) +} + // ── helpers ────────────────────────────────────────────────────── // openDispatchTestStore creates a fresh in-memory SQLite store for