Files
noema/internal/cognition/motivation.go
T
Andy Dixon 9eb873cbe6 feat: add goals, drives, affect state, predictions and curiosity
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.
2026-06-27 20:41:03 +01:00

114 lines
3.1 KiB
Go

package cognition
import (
"context"
"fmt"
"math"
"github.com/andydixon/noema/internal/brain"
"github.com/andydixon/noema/internal/motivation"
"github.com/andydixon/noema/pkg/events"
)
// MotivationHook applies the deterministic affect and drive rules to the
// events a cycle produced, persists the result, and emits emotion_changed or
// drive_changed events only when something moved noticeably.
type MotivationHook struct{ Store *motivation.Store }
// Name implements brain.Hook.
func (MotivationHook) Name() string { return "motivation" }
// AfterCycle implements brain.Hook.
func (h MotivationHook) AfterCycle(ctx context.Context, c *brain.Cycle, emitted []events.Event) error {
if h.Store == nil {
return nil
}
st := c.State.Motiv
if len(st.Drives) == 0 {
return nil
}
before := st
before.Drives = append([]motivation.Drive{}, st.Drives...)
var reasons []string
all := append(append([]events.Event{}, c.State.NewEvents...), emitted...)
for _, ev := range all {
if d, ok := motivation.Rules(ev); ok {
motivation.Apply(&st, d)
reasons = append(reasons, d.Reason)
}
}
st.Affect.UpdatedAt = c.State.Now
if err := h.Store.SaveAffect(ctx, c.Tx, c.State.Mind.ID, st.Affect); err != nil {
return err
}
for i, d := range st.Drives {
if math.Abs(d.Value-before.Drives[i].Value) > 1e-9 {
if err := h.Store.SaveDrive(ctx, c.Tx, c.State.Mind.ID, d); err != nil {
return err
}
if math.Abs(d.Value-before.Drives[i].Value) >= 0.1 {
c.Emit(events.New{Type: events.DriveChanged, Source: "brain.motivation", Summary: fmt.Sprintf("drive %s %.2f → %.2f", d.Name, before.Drives[i].Value, d.Value), Salience: 0.2, Confidence: 1,
Metadata: map[string]any{"drive": d.Name, "from": before.Drives[i].Value, "to": d.Value}, Payload: map[string]any{"drive": d, "reasons": uniq(reasons)}})
}
}
}
if key, sig := motivation.Significant(before.Affect, st.Affect); sig {
v := st.Affect.Valence
a := st.Affect.Arousal
c.Emit(events.New{Type: events.EmotionChanged, Source: "brain.motivation", Summary: fmt.Sprintf("cognitive state shifted: %s now %.2f (%s)", key, field(st.Affect, key), joinReasons(reasons)),
Salience: 0.3, Confidence: 1, Valence: &v, Arousal: &a, Metadata: map[string]any{"field": key}, Payload: map[string]any{"before": before.Affect, "after": st.Affect, "reasons": uniq(reasons)}})
}
return nil
}
func field(a motivation.Affect, key string) float64 {
switch key {
case "valence":
return a.Valence
case "arousal":
return a.Arousal
case "curiosity":
return a.Curiosity
case "concern":
return a.Concern
case "frustration":
return a.Frustration
case "confidence":
return a.Confidence
case "uncertainty":
return a.Uncertainty
case "engagement":
return a.Engagement
case "task_pressure":
return a.TaskPressure
}
return a.Load
}
func uniq(xs []string) []string {
seen := map[string]bool{}
var out []string
for _, x := range xs {
if !seen[x] {
seen[x] = true
out = append(out, x)
}
}
return out
}
func joinReasons(xs []string) string {
u := uniq(xs)
if len(u) > 3 {
u = append(u[:3], "…")
}
s := ""
for i, x := range u {
if i > 0 {
s += ", "
}
s += x
}
return s
}