fix(app): serve only setup until the owner setup transaction commits

The binary started connectors, module routes, the starter workflow and the
worker loops at boot whatever the database said, so an instance with no owner
answered operational requests, the bootstrap admin token among them. The
governance module now signals once the bootstrap registration has committed,
and the composition root holds every operational route, connector and loop back
until it does. Liveness stays up and readiness reports the missing owner.
This commit is contained in:
sam committed 2026-11-21 03:40:02 +11:00
1 parent 98c0c11cbb
commit ae3bbc6cfe
7 files changed
+761 -63

No files matched your search

+2 -1
View File
@@ -257,12 +257,13 @@ type Module struct {
credentials Credentials
authorizer Authorizer
oidc *oidcLogin
setup *SetupSignal
}
// NewModule returns the governance module for orgID. The ai-sdk provider
// classes must already be registered.
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string) *Module {
return &Module{orgID: orgID, store: store.New(db), log: log, oidc: &oidcLogin{}}
return &Module{orgID: orgID, store: store.New(db), log: log, oidc: &oidcLogin{}, setup: NewSetupSignal()}
}
// UseCredentials gives the module the sealed store a model's key reference
+34
View File
@@ -0,0 +1,34 @@
package governance
import "sync"
// SetupSignal reports that the owner setup transaction has committed. The
// composition root waits on it instead of polling the database for the change,
// and it fires once however many callers ask.
type SetupSignal struct {
once sync.Once
done chan struct{}
}
// NewSetupSignal returns a signal for a setup that has not committed yet.
func NewSetupSignal() *SetupSignal {
return &SetupSignal{done: make(chan struct{})}
}
// Complete records the commit. A second call, sequential or concurrent,
// changes nothing. A nil signal is a module built without durable state, as
// the API document is, and records nothing.
func (s *SetupSignal) Complete() {
if s == nil {
return
}
s.once.Do(func() { close(s.done) })
}
// Done is closed once the setup has committed.
func (s *SetupSignal) Done() <-chan struct{} {
if s == nil {
return nil
}
return s.done
}
+11
View File
@@ -344,6 +344,11 @@ func (m *Module) AccountSetupRequired(ctx context.Context) (bool, error) {
return m.store.AccountSetupRequired(ctx)
}
// SetupDone is closed once the owner setup transaction has committed.
func (m *Module) SetupDone() <-chan struct{} {
return m.setup.Done()
}
// RegisterAccount creates a user account, bootstrapping the operating org when bootstrap is true.
func (m *Module) RegisterAccount(ctx context.Context, r AccountRegistration, bootstrap bool) (Identity, error) {
role, orgID, err := registrationScope(ctx, &r, bootstrap)
@@ -375,6 +380,12 @@ func (m *Module) RegisterAccount(ctx context.Context, r AccountRegistration, boo
if err != nil {
return Identity{}, mapIdentityErr(err)
}
if bootstrap {
// The owner is in durable state now. Whatever waits on this signal starts
// operations, so it is sent only after the transaction committed: a failed
// attempt returns above and leaves it closed.
m.setup.Complete()
}
return identityFromRow(row), nil
}