Files
archie/internal/app/archie/operations.go
T
sam e3130d53e7 Merge branch 'worktree-agent-a82ec64b6cc1a2809'
# Conflicts:
#	internal/app/archie/operations.go
#	modules/events/events.go
#	modules/governance/accounts_api.go
#	modules/governance/governance.go
2026-11-22 02:16:56 +11:00

150 lines
5.7 KiB
Go

package archie
import (
"context"
"fmt"
"log/slog"
"sync"
"time"
"github.com/samcharles93/archie-core/internal/platform/health"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/chat"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/deliveries"
"github.com/samcharles93/archie-core/modules/events"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/memory"
"github.com/samcharles93/archie-core/modules/runs"
)
// operations is everything the gate holds back: the connectors, the access
// chain administrative writes are checked against, the workflow that ships with
// the binary, and the loops that do work. Run wires it at boot and Start brings
// it up, once, after owner setup has committed.
type operations struct {
log *slog.Logger
checks *health.Registry
settings *governance.Module
conns *connectors.Module
engine *runs.Engine
hear *events.Module
tell *deliveries.Module
talk *chat.Module
judge *judgement.Module
allow *approvals.Module
remember *memory.Module
// completeRecord finishes the record of a committed deletion whose entry could
// not be written.
completeRecord func(context.Context) (int, error)
workers sync.WaitGroup
}
// Start brings up every part of the binary that does work, in the order the
// other parts depend on: the role policies and access chain, the workflow that
// ships with the binary, the connectors, then the loops.
//
// Calling it again after it returns an error is safe. Every step that can fail
// runs before a connector launches, and connectors.Module.Start fails only for
// the read of accepted connectors, before launching any. An error therefore
// means nothing is running.
func (o *operations) Start(ctx context.Context) error {
access, err := startAccess(ctx, o.log, o.settings)
if err != nil {
return err
}
// Every entry point asks the same chain, and each refusal is stored as a
// denial.
checked := governance.Recording(access, o.settings, o.log.With("module", "governance"))
// A workflow takes its context from the run handle, as a handler takes it
// from the request.
o.engine.Use(runs.Deps{
Settings: o.settings, Toolbox: o.conns, Judge: o.judge, Approver: o.allow,
Notifier: o.tell, Messenger: o.tell, Memory: o.remember, Capabilities: o.conns,
Access: checked,
})
if err := installStarter(ctx, o.engine); err != nil {
return err
}
// A connector's new declarations are re-checked against the saved
// workflows that reference it, so an update that would break them is
// refused instead of failing a run later.
o.conns.Use(func(ctx context.Context, connector string, caps connectors.Capabilities) error {
return o.engine.RecheckConnector(ctx, orgID, connector, caps)
})
if err := o.conns.Start(ctx); err != nil {
return fmt.Errorf("read accepted connectors: %w", err)
}
// Nothing below can fail, so a retry cannot launch a connector or a loop twice.
o.checks.Register("connectors", o.conns.Ready)
// Administrative writes, and every API request, are checked against the same
// live policy chain as chat and background work. The webhook intake applies
// each source's delivery policy from it.
o.settings.UseAuthorizer(checked)
o.allow.UseAuthorizer(checked)
o.tell.UseAccess(checked)
o.hear.UseDeliveryPolicy(access)
o.talk.Use(chat.Deps{
People: o.settings, Access: access, Settings: o.settings, Toolbox: o.conns, Judge: o.judge,
Approver: o.allow, Memory: o.remember, Runs: o.engine, Notifier: o.tell,
})
for _, loop := range []func(context.Context){o.engine.Work, o.hear.Work, o.tell.Work, o.talk.Work, o.retryDeletionRecord} {
o.workers.Go(func() { loop(ctx) })
}
return nil
}
// retryDeletionRecord keeps trying to complete the record of a deletion that
// committed while its entry could not be written, so the instance heals without a
// restart. It does nothing while the record is complete.
func (o *operations) retryDeletionRecord(ctx context.Context) {
ticker := time.NewTicker(deletionRecordRetry)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if !o.remember.RecordPending() || o.completeRecord == nil {
continue
}
// The mark is taken before the reconcile: a failure recorded while it
// runs must still leave the instance unhealthy.
mark := o.remember.FailureMark()
added, err := o.completeRecord(ctx)
if err != nil {
o.log.ErrorContext(ctx, "complete the record of deletions", "error", err)
continue
}
o.remember.MarkRecordComplete(mark)
o.log.WarnContext(ctx, "completed the record of deletions", "entries", added)
}
}
}
// Wait blocks until every loop Start began has returned.
func (o *operations) Wait() {
o.workers.Wait()
}
// installStarter puts the workflow that ships with the binary in place. It
// belongs to the organisation, so it waits until setup has created one.
func installStarter(ctx context.Context, engine *runs.Engine) error {
if _, err := engine.Install(ctx, orgID, "investigate", runs.InvestigateDocument(), "starter"); err != nil {
return fmt.Errorf("install the investigate workflow: %w", err)
}
return nil
}
// startAccess seeds the operating org's shipped role policies and returns the
// access policy chain, kept current as policies change.
func startAccess(ctx context.Context, log *slog.Logger, settings *governance.Module) (*governance.Live, error) {
if err := settings.EnsureShippedOrgPolicies(ctx, governance.DefaultOrgID); err != nil {
return nil, fmt.Errorf("seed the role policies: %w", err)
}
return governance.StartLive(ctx, settings.ListPolicies, settings.PoliciesVersion, log.With("module", "governance")), nil
}