bootstrap

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidConfig = errors.New("config: invalid configuration")

ErrInvalidConfig is returned when AppConfig.Validate finds an inconsistent or missing required field.

Functions

func SeedDemo

func SeedDemo(ctx context.Context, repos *store.Repositories) error

SeedDemo inserts a minimal demonstration dataset that makes the MIDAS Explorer sample scenarios work immediately.

The seed is idempotent: if the demo surface already exists the function returns nil without modifying any data. This makes it safe to call on every startup.

Demo dataset:

Surface  surf-payments-approval  — payments approval decision surface
Agent    agent-payments-bot      — AI agent authorised to approve payments
Profile  profile-payments-std    — standard authority limits
Grant    grant-payments-bot-std  — links agent to profile

Authority thresholds:

Confidence ≥ 0.85   (Explorer Execute scenario sends 0.95 — passes)
Consequence ≤ 1000  (Explorer Execute sends 100 — passes; escalate sends 1,000,000 — escalates)

Types

type AppConfig

type AppConfig struct {
	Dispatcher DispatcherConfig
	Kafka      KafkaConfig
}

AppConfig is the top-level runtime configuration for a MIDAS process.

func LoadAppConfig

func LoadAppConfig() (AppConfig, error)

LoadAppConfig reads AppConfig from environment variables. Unset variables fall back to safe defaults. Validation is separate (call Validate).

Environment variables:

MIDAS_DISPATCHER_ENABLED       bool   (default: false)
MIDAS_DISPATCHER_PUBLISHER     string (default: "none"; valid: "none", "kafka")
MIDAS_DISPATCHER_BATCH_SIZE    int    (default: 100)
MIDAS_DISPATCHER_POLL_INTERVAL string (default: "2s"; Go duration)
MIDAS_DISPATCHER_MAX_BACKOFF   string (default: "30s"; Go duration)

MIDAS_KAFKA_BROKERS            string (comma-separated host:port; required when publisher=kafka)
MIDAS_KAFKA_CLIENT_ID          string (default: "midas")
MIDAS_KAFKA_REQUIRED_ACKS      int    (default: -1)
MIDAS_KAFKA_WRITE_TIMEOUT      string (default: ""; zero means no timeout)

func (AppConfig) Validate

func (c AppConfig) Validate() error

Validate returns ErrInvalidConfig (wrapped) for any configuration that cannot lead to a valid runtime state. Validation is purely structural and does not open network connections.

Dispatcher semantics:

  • DispatcherEnabled=false: always passes regardless of publisher fields.
  • DispatcherEnabled=true: a real publisher must be configured; "none" and empty publisher values are rejected because there is no valid runtime state where the dispatcher is enabled but has no outbound transport.

type DispatcherConfig

type DispatcherConfig struct {
	// Enabled controls whether the dispatcher goroutine is started.
	// When false the outbox is written but never polled.
	Enabled bool

	// Publisher selects the broker implementation. Ignored when Enabled is false.
	Publisher PublisherType

	// BatchSize is the maximum number of outbox rows claimed per poll cycle.
	// Defaults to 100.
	BatchSize int

	// PollInterval is the sleep duration between poll cycles when the queue
	// is empty. Defaults to 2 seconds.
	PollInterval time.Duration

	// MaxBackoff is the upper bound for exponential sleep on consecutive poll
	// errors. Defaults to 30 seconds.
	MaxBackoff time.Duration
}

DispatcherConfig controls whether the outbox dispatcher runs and how it behaves. All durations are wall-clock values; zero means use the default.

type DispatcherWiring

type DispatcherWiring struct {
	// Dispatcher is the configured Dispatcher, or nil when the dispatcher is
	// disabled.
	Dispatcher *dispatch.Dispatcher

	// KafkaPublisher is the Kafka-backed publisher, or nil when the publisher
	// type is not "kafka". Caller must call Close() during shutdown.
	KafkaPublisher *kafka.KafkaPublisher
}

DispatcherWiring holds everything the caller needs to run and shut down the outbox dispatcher. Fields are nil only when the dispatcher is disabled (cfg.Dispatcher.Enabled == false).

func BuildDispatcher

func BuildDispatcher(cfg AppConfig, repo outbox.Repository) (*DispatcherWiring, error)

BuildDispatcher constructs a Dispatcher and, when required, a KafkaPublisher from the supplied AppConfig. repo is the outbox.Repository the dispatcher will poll.

Returns a DispatcherWiring with nil Dispatcher only when cfg.Dispatcher.Enabled is false. Every other code path either returns a fully wired Dispatcher or a non-nil error.

Callers must call cfg.Validate() before BuildDispatcher to catch invalid config combinations early. BuildDispatcher does not duplicate that validation, but it does enforce the repo requirement which Validate cannot check (repo availability depends on the selected store backend).

Error cases when Enabled is true:

  • publisher is "none" or empty: no valid outbound transport
  • publisher is an unrecognised value
  • repo is nil: no durable outbox repository is available
  • publisher=kafka and broker construction fails
  • dispatcher construction fails (invalid batch size or poll interval)

func (*DispatcherWiring) Close

func (w *DispatcherWiring) Close()

Close releases resources held by the DispatcherWiring. It must be called after the dispatcher goroutine has exited (i.e. after the context has been cancelled and the goroutine's done channel has been closed).

type KafkaConfig

type KafkaConfig struct {
	// Brokers is the list of seed broker addresses in "host:port" form.
	// Required when PublisherType is "kafka".
	Brokers []string

	// ClientID is an optional identifier sent to the broker for observability.
	ClientID string

	// RequiredAcks controls acknowledgement level: -1=all ISRs, 0=none, 1=leader.
	// Defaults to -1 (strongest).
	RequiredAcks int

	// WriteTimeout bounds the per-message publish call. Zero means no timeout.
	WriteTimeout time.Duration
}

KafkaConfig holds broker-level settings for the Kafka publisher. Only meaningful when DispatcherConfig.Publisher == PublisherTypeKafka.

type PublisherType

type PublisherType string

PublisherType identifies which message broker implementation the dispatcher will use. When DispatcherConfig.Enabled is true, a real publisher must be configured; "none" is only valid when the dispatcher is disabled.

const (
	// PublisherTypeNone is the zero value. Valid only when
	// DispatcherConfig.Enabled is false; it signals that no outbound transport
	// is configured. Validate rejects this value when Enabled is true.
	PublisherTypeNone PublisherType = "none"

	// PublisherTypeKafka wires the Kafka publisher. Requires non-empty
	// KafkaConfig.Brokers.
	PublisherTypeKafka PublisherType = "kafka"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL