Phase 9. Goals form a mind-scoped tree with kind, priority, progress and blockers; their deterministic relevance to a candidate feeds the goal_relevance attention feature and a goal monitor raises stalled goals and observations that touch one. Drives are a closed set of eight scalars with operator-configured ranges and personality-derived baselines; there is no self-preservation, replication or acquisition drive and no way to add one at runtime. Affect is ten scalars moved by an explicit rule table over events and decaying towards baseline; both modulate attention weights each cycle and the effective weights are recorded with every cycle and shown beside the configured ones. Predictions carry a window and machine-checkable criteria (event terms occurring or staying absent, a belief reaching a confidence, or operator judgement). A resolver job judges them deterministically, keeps an append-only outcome history for calibration (accuracy, Brier score, confidence buckets, overconfidence flag), emits prediction_error events scaled by how confident the miss was, and feeds the outcome back into the linked belief as evidence. Causal hypotheses derive predictions automatically. Curiosity turns misses, contradictions and unexplained observations into questions that compete for attention and become investigation goals when admitted. Brain gains after-cycle hooks and goal/motivation state. API, UI (goals, prediction dashboard, cognitive state panel, effective weights) and CLI (goal, prediction, state). Demo scenario 4 verified end to end.
234 lines
9.3 KiB
Go
234 lines
9.3 KiB
Go
// Command noemad is the Noema daemon: API server, web UI, scheduler,
|
|
// integration manager and cognitive workers in one process.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/andydixon/noema/internal/api"
|
|
"github.com/andydixon/noema/internal/app"
|
|
"github.com/andydixon/noema/internal/audit"
|
|
"github.com/andydixon/noema/internal/auth"
|
|
"github.com/andydixon/noema/internal/beliefs"
|
|
"github.com/andydixon/noema/internal/brain"
|
|
"github.com/andydixon/noema/internal/cognition"
|
|
"github.com/andydixon/noema/internal/config"
|
|
"github.com/andydixon/noema/internal/conversation"
|
|
evstore "github.com/andydixon/noema/internal/events"
|
|
"github.com/andydixon/noema/internal/goals"
|
|
"github.com/andydixon/noema/internal/llm/prompts"
|
|
"github.com/andydixon/noema/internal/llm/router"
|
|
"github.com/andydixon/noema/internal/memory"
|
|
"github.com/andydixon/noema/internal/minds"
|
|
"github.com/andydixon/noema/internal/motivation"
|
|
"github.com/andydixon/noema/internal/observability"
|
|
"github.com/andydixon/noema/internal/predictions"
|
|
"github.com/andydixon/noema/internal/scheduler"
|
|
"github.com/andydixon/noema/internal/security/secrets"
|
|
"github.com/andydixon/noema/internal/storage"
|
|
"github.com/andydixon/noema/internal/ui"
|
|
"github.com/andydixon/noema/internal/version"
|
|
"github.com/andydixon/noema/migrations"
|
|
"github.com/andydixon/noema/pkg/events"
|
|
"github.com/andydixon/noema/pkg/ids"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "noemad:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
var (
|
|
cfgPath = flag.String("config", "", "path to JSON config file (optional)")
|
|
migrate = flag.Bool("migrate", true, "apply pending migrations at start")
|
|
show = flag.Bool("version", false, "print version and exit")
|
|
)
|
|
flag.Parse()
|
|
if *show {
|
|
fmt.Println(version.Version)
|
|
return nil
|
|
}
|
|
cfg, err := config.Load(*cfgPath, os.LookupEnv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log := observability.NewLogger(os.Stderr, cfg.LogLevel, cfg.LogFormat)
|
|
log.Info("starting noemad", "version", version.Version, "env", cfg.Env, "addr", cfg.HTTPAddr)
|
|
log.Debug("configuration", "config", fmt.Sprintf("%+v", cfg.Redacted()))
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
db, err := storage.Open(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
if *migrate {
|
|
applied, err := storage.MigrateUp(ctx, db, migrations.FS)
|
|
if err != nil {
|
|
return fmt.Errorf("migrate: %w", err)
|
|
}
|
|
if len(applied) > 0 {
|
|
log.Info("applied migrations", "migrations", applied)
|
|
}
|
|
}
|
|
|
|
metrics := observability.NewMetrics()
|
|
authSvc := auth.NewService(db, cfg.SessionIdleTTL, cfg.SessionAbsoluteTTL)
|
|
auditLog := audit.New(db, log)
|
|
deps := app.Deps{Config: cfg, Log: log, DB: db, Auth: authSvc, Audit: auditLog, Metrics: metrics, Minds: minds.NewStore(db), Events: evstore.NewStore(db, evstore.NewBus(200), metrics), Migrations: migrations.FS, Started: time.Now()}
|
|
|
|
if err := bootstrapAdmin(ctx, deps); err != nil {
|
|
return err
|
|
}
|
|
|
|
group := scheduler.NewGroup(ctx, log)
|
|
sec, err := secrets.New(cfg.MasterKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
deps.Secrets = sec
|
|
if !sec.CanEncrypt() {
|
|
log.Warn("NOEMA_MASTER_KEY not set: API keys must be given as env: or file: references")
|
|
}
|
|
deps.LLMStore = router.NewStore(db)
|
|
deps.LLM = router.NewRouter(deps.LLMStore, sec, log, metrics, cfg.MaxConcurrentLLM)
|
|
deps.LLM.DailyTokenBudget = cfg.DailyTokenBudget
|
|
deps.LLM.MindBudget = func(id ids.ID) int64 {
|
|
m, err := deps.Minds.Get(ctx, id)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return m.Config.DailyTokenBudget
|
|
}
|
|
deps.Prompts = prompts.NewStore(db)
|
|
if n, err := deps.Prompts.SeedDefaults(ctx); err != nil {
|
|
return fmt.Errorf("seed prompts: %w", err)
|
|
} else if n > 0 {
|
|
log.Info("installed default prompts", "count", n)
|
|
}
|
|
deps.Memory = memory.NewStore(ctx, db, deps.LLM.Embedder(ctx))
|
|
deps.Beliefs = beliefs.NewStore(db)
|
|
deps.Conversations = conversation.NewStore(db)
|
|
deps.Goals = goals.NewStore(db)
|
|
deps.Motivation = motivation.NewStore(db)
|
|
deps.Predictions = predictions.NewStore(db)
|
|
deps.Resolver = &predictions.Resolver{Store: deps.Predictions, Events: deps.Events, Beliefs: deps.Beliefs, Log: log, Metrics: metrics}
|
|
registry := brain.NewRegistry().AddProcess(cognition.Attend{}).AddProcess(cognition.Retrieve{}).AddProcess(cognition.Associate{}).AddProcess(cognition.Perceive{}).
|
|
AddProcess(cognition.GoalMonitor{}).AddProcess(cognition.Curiosity{}).AddHook(cognition.MotivationHook{Store: deps.Motivation})
|
|
cognition.Handlers{Memory: deps.Memory, Beliefs: deps.Beliefs, Conversations: deps.Conversations}.Register(registry)
|
|
deps.Brain = brain.NewManager(brain.Config{DB: db, Events: deps.Events, Minds: deps.Minds, Registry: registry, Pool: scheduler.NewPool(cfg.MaxWorkers),
|
|
Log: log, Metrics: metrics, Services: brain.Services{Events: deps.Events, Memory: deps.Memory, Beliefs: deps.Beliefs, LLM: deps.LLM, Prompts: deps.Prompts,
|
|
Goals: deps.Goals, Motivation: deps.Motivation, Predictions: deps.Predictions}}, group)
|
|
group.Every("predictions.resolve", time.Minute, deps.Resolver.Run)
|
|
group.Every("memory.decay", 6*time.Hour, func(ctx context.Context) error { return decayMemories(ctx, deps) })
|
|
group.Every("memory.embed", time.Minute, func(ctx context.Context) error { _, err := deps.Memory.EmbedMissing(ctx, 50); return err })
|
|
if err := deps.Brain.Start(ctx); err != nil {
|
|
return fmt.Errorf("start minds: %w", err)
|
|
}
|
|
|
|
var srv *api.Server
|
|
uiHandler, err := ui.New(deps, ui.Actions{
|
|
Status: func(r *http.Request) any { return srv.Status(r.Context()) },
|
|
Context: func(r *http.Request, id ids.ID) (api.EventContext, error) { return srv.Context(r, id) },
|
|
Workspace: func(r *http.Request, m minds.Mind) (api.WorkspaceView, error) { return srv.Workspace(r.Context(), m) },
|
|
Memory: func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error) { return srv.MemoryDetail(r, mind, id) },
|
|
Belief: func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error) { return srv.BeliefDetail(r, mind, id) },
|
|
Emitter: func() beliefs.Emitter { return srv.Emitter() },
|
|
Post: func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error) {
|
|
return srv.PostMessage(r, mind, conv, text, who)
|
|
},
|
|
Goal: func(r *http.Request, n goals.New) (goals.Goal, error) { return srv.CreateGoal(r, n) },
|
|
GoalUpdate: func(r *http.Request, mind, id ids.ID, u goals.Update) (goals.Goal, error) {
|
|
return srv.UpdateGoal(r, mind, id, u)
|
|
},
|
|
Predict: func(r *http.Request, n predictions.New) (predictions.Prediction, error) {
|
|
return srv.CreatePrediction(r, n)
|
|
},
|
|
Resolve: func(r *http.Request, mind, id ids.ID, outcome predictions.Status, reason string) (predictions.Result, error) {
|
|
return srv.ResolvePrediction(r, mind, id, outcome, reason)
|
|
},
|
|
ResetState: func(r *http.Request, mind ids.ID) (motivation.State, error) { return srv.ResetState(r, mind) },
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
srv = api.New(deps, uiHandler)
|
|
|
|
httpSrv := &http.Server{Addr: cfg.HTTPAddr, Handler: srv.Handler(), ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 30 * time.Second, IdleTimeout: 120 * time.Second, MaxHeaderBytes: 64 << 10}
|
|
group.Go("http", func(ctx context.Context) error {
|
|
errc := make(chan error, 1)
|
|
go func() { errc <- httpSrv.ListenAndServe() }() // owned: joined via errc below
|
|
select {
|
|
case err := <-errc:
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
return nil
|
|
}
|
|
return err
|
|
case <-ctx.Done():
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
return httpSrv.Shutdown(shutdownCtx)
|
|
}
|
|
})
|
|
group.Every("auth.sweep", 5*time.Minute, func(context.Context) error { authSvc.SweepLimiter(); return nil })
|
|
group.Every("auth.purge", time.Hour, authSvc.PurgeExpiredSessions)
|
|
|
|
log.Info("noemad ready", "url", "http://"+cfg.HTTPAddr)
|
|
<-ctx.Done()
|
|
log.Info("shutting down")
|
|
return group.Stop()
|
|
}
|
|
|
|
// bootstrapAdmin creates the first administrator from the environment when the
|
|
// users table is empty, so headless deployments can start without the setup page.
|
|
func bootstrapAdmin(ctx context.Context, d app.Deps) error {
|
|
n, err := d.Auth.CountUsers(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n > 0 || d.Config.BootstrapAdminPassword == "" {
|
|
return nil
|
|
}
|
|
u, err := d.Auth.CreateUser(ctx, d.Config.BootstrapAdminUser, d.Config.BootstrapAdminPassword, auth.RoleAdministrator)
|
|
if err != nil {
|
|
return fmt.Errorf("bootstrap admin: %w", err)
|
|
}
|
|
d.Audit.Log(ctx, "user.bootstrap", "user", u.ID.String(), map[string]any{"username": u.Username, "via": "environment"})
|
|
d.Log.Info("created bootstrap administrator", "username", u.Username)
|
|
return nil
|
|
}
|
|
|
|
// decayMemories lowers activation of unused memories for every mind and leaves
|
|
// one summary event per mind so the decay is visible without spamming.
|
|
func decayMemories(ctx context.Context, d app.Deps) error {
|
|
list, err := d.Minds.List(ctx, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, m := range list {
|
|
n, err := d.Memory.Decay(ctx, m.ID, 14*24*time.Hour)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n > 0 {
|
|
d.Events.Append(ctx, events.New{MindID: m.ID, Type: events.MemoryDecayed, Source: "memory.decay", Summary: fmt.Sprintf("activation decayed on %d memories", n), //nolint:errcheck
|
|
Salience: 0.05, Confidence: 1, Metadata: map[string]any{"count": n}})
|
|
}
|
|
}
|
|
return nil
|
|
}
|