diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 8ccca99c7..37870ec3f 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -370,22 +370,18 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence if err := b.openStores(ctx); err != nil { return 1 } - // Resolve the State Store contract adapter as the remote *staterpc.Client - // dialed to [services.state].target; the daemon no longer owns archie.db - // in-process, so an empty target is a composition error - // (docs/prds/state-store-contract.md §12 step 7). This runs after - // openStores has resolved b.secrets and before setupObservability wires the - // dashboard's storage surfaces (§10). - if err := b.openStateStoreAdapter(); err != nil { - return 1 - } - // The daemon resolves workflow step types through its workflow-definitions - // client, so it registers the step vocabulary here, at its own composition - // root, before buildDaemon captures the client. The gateway root shares - // openStateStoreAdapter and resolves no step type, so the vocabulary is built - // by this root alone (step_vocabulary_test.go pins the cut). - if err := b.openDaemonWorkflowDefinitions(); err != nil { - b.log.Error("workflow definitions client", "err", err) + // Resolve the State Store surfaces before setupObservability wires the + // dashboard's storage surfaces (§10): the shared contract adapter (the + // remote *staterpc.Client dialed to [services.state].target; the daemon no + // longer owns archie.db in-process, so an empty target is a composition + // error, docs/prds/state-store-contract.md §12 step 7) and then, daemon + // root only, the workflow-definitions client on this process's step + // vocabulary. The daemon resolves step types through that client, so it is + // built here -- after the adapter, whose transport it wraps -- and before + // buildDaemon captures it. The gateway root shares only the adapter and + // resolves no step type, so openDaemonStateSurfaces owns the order and + // step_vocabulary_test.go pins the cut. + if err := b.openDaemonStateSurfaces(); err != nil { return 1 } if err := b.loadRuntimeConfig(ctx); err != nil { diff --git a/internal/app/archied/step_vocabulary.go b/internal/app/archied/step_vocabulary.go index 76eef2c2c..8815c15ce 100644 --- a/internal/app/archied/step_vocabulary.go +++ b/internal/app/archied/step_vocabulary.go @@ -55,3 +55,28 @@ func (b *boot) openDaemonWorkflowDefinitions() error { b.workflowDefinitions = definitions return nil } + +// openDaemonStateSurfaces opens the daemon's State Store surfaces in one step: +// the adapter shared with every other root (openStateStoreAdapter) and, on top +// of it, the daemon-only workflow-definitions client +// (openDaemonWorkflowDefinitions). +// +// It exists because the daemon root is a flat composition sequence with no +// complexity budget left to spend on a second adjacent guard, and the order the +// two calls owe each other is a property of the pair, not of that sequence: the +// definitions client wraps the control-plane transport the adapter dials, so it +// cannot be built first, and buildDaemon captures the client, so it cannot be +// built later. step_vocabulary_test.go pins exactly that order here. +// +// The gateway root calls openStateStoreAdapter directly and stops there; the +// daemon-only half stays out of that function for the reason above it. +func (b *boot) openDaemonStateSurfaces() error { + if err := b.openStateStoreAdapter(); err != nil { + return err + } + if err := b.openDaemonWorkflowDefinitions(); err != nil { + b.log.Error("workflow definitions client", "err", err) + return err + } + return nil +} diff --git a/internal/app/archied/step_vocabulary_test.go b/internal/app/archied/step_vocabulary_test.go index dca53146d..ed0b068c4 100644 --- a/internal/app/archied/step_vocabulary_test.go +++ b/internal/app/archied/step_vocabulary_test.go @@ -138,18 +138,36 @@ func TestOnlyTheDaemonRootRegistersAStepVocabulary(t *testing.T) { t.Error("the gateway root file mentions stepVocabulary; the gateway resolves no workflow step type") } + // The daemon root opens both halves -- the shared adapter and the + // daemon-only definitions client -- through one call, so the sequence costs + // Run a single branch. The guarantee is the one this pin always held, + // relocated to where the sequence now lives: adapter first, then the client + // built on its transport, both before buildDaemon captures the client. run := parsedBody(t, "main.go", "Run") - adapter := methodCallPosition(run, "openStateStoreAdapter") - wiring := methodCallPosition(run, "openDaemonWorkflowDefinitions") + surfaces := methodCallPosition(run, "openDaemonStateSurfaces") build := methodCallPosition(run, "buildDaemon") + if surfaces == token.NoPos { + t.Fatal("Run never opens the daemon's State Store surfaces; every task pin would fall back to the shipped definitions") + } + if build == token.NoPos || surfaces > build { + t.Errorf("Run opens the daemon's State Store surfaces at %v, after buildDaemon at %v: the daemon would capture no definitions surface", surfaces, build) + } + // Neither half may also be opened from Run directly: a second call site + // would be a copy of the sequence outside the order asserted below. + for _, direct := range []string{"openStateStoreAdapter", "openDaemonWorkflowDefinitions"} { + if pos := methodCallPosition(run, direct); pos != token.NoPos { + t.Errorf("Run calls %s directly at %v; the daemon's State Store surfaces open through openDaemonStateSurfaces alone", direct, pos) + } + } + + sequence := parsedBody(t, "step_vocabulary.go", "openDaemonStateSurfaces") + adapter := methodCallPosition(sequence, "openStateStoreAdapter") + wiring := methodCallPosition(sequence, "openDaemonWorkflowDefinitions") if wiring == token.NoPos { - t.Fatal("Run never builds the daemon's workflow-definitions client; every task pin would fall back to the shipped definitions") + t.Fatal("openDaemonStateSurfaces never builds the daemon's workflow-definitions client; every task pin would fall back to the shipped definitions") } if adapter == token.NoPos || wiring < adapter { - t.Errorf("Run builds the workflow-definitions client at %v, at or before openStateStoreAdapter at %v: it would wrap the transport the adapter has not opened yet", wiring, adapter) - } - if build == token.NoPos || wiring > build { - t.Errorf("Run builds the workflow-definitions client at %v, after buildDaemon at %v: the daemon would capture no definitions surface", wiring, build) + t.Errorf("openDaemonStateSurfaces builds the workflow-definitions client at %v, at or before openStateStoreAdapter at %v: it would wrap the transport the adapter has not opened yet", wiring, adapter) } }