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.
This commit is contained in:
sam committed 2026-09-15 04:35:00 +10:00
1 parent 0e64573b04
commit 13ef3bd8ad
2 files changed
+29 -3

No files matched your search

+5 -3
View File
@@ -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
}
+24
View File
@@ -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