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.
This commit is contained in:
@@ -5,6 +5,7 @@ All notable changes to Noema are recorded here. Format follows Keep a Changelog;
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Phase 9 motivation: goal tree with attention relevance, closed drive set and rule-driven affect state with decay and weight modulation, explicit predictions with deterministic resolution, prediction errors feeding beliefs, calibration dashboard, curiosity questions becoming investigation goals.
|
||||
- Phase 8 conversation (milestone M1): messages become observations, deterministic perception with optional model enrichment, claims become evidenced beliefs, grounded introspective answers from real state, model or deterministic wording with full provenance, chat UI, `noemactl ask`, sample mind Dani with a strict seed import, and an end-to-end acceptance test.
|
||||
- Phase 7 LLM layer: provider-neutral router with roles and fallbacks, OpenAI-compatible/Anthropic/Ollama providers, encrypted secret references, SSRF-safe HTTP client, budgets, full call recording with replay, versioned prompts with untrusted data blocks, configuration and prompt manager UI, CLI.
|
||||
- Phase 6 beliefs: evidence-derived confidence with inspectable arithmetic, statuses, alternatives, append-only history, deterministic contradiction detection, temporal-association hypotheses, operator corrections (wrong, do-not-infer, inaccurate memory), belief explorer and CLI.
|
||||
|
||||
@@ -126,11 +126,15 @@ An administrator can: start Noema → log in → create a mind → configure per
|
||||
|
||||
**Milestone M1 reached.** Remaining phases deepen the system; the vertical slice above is exercised end to end by `go test ./tests/m1/`.
|
||||
|
||||
### Phase 9 – Goals, drives, predictions
|
||||
- ☐ goals (tree, priority, progress, blockers, origin), goal monitor process
|
||||
- ☐ drives with ranges; affect model with baselines and decay; UI panels and reset
|
||||
- ☐ predictions, resolution scheduler, prediction error → confidence/credibility/calibration; prediction dashboard
|
||||
- ☐ Curiosity process
|
||||
### Phase 9 – Goals, drives, affect, predictions, curiosity ☑
|
||||
- ☑ `migrations/0009_motivation`: goals, drives, affect_states, predictions, append-only prediction_results
|
||||
- ☑ `internal/goals`: tree with kinds/status/priority/progress/blockers, stalled detection, deterministic goal relevance
|
||||
- ☑ `internal/motivation`: closed drive set (no self-preservation), personality-derived baselines, explicit event rule table, decay, attention weight modulation, significance thresholds
|
||||
- ☑ `internal/predictions`: criteria (event terms, belief confidence, operator), deterministic judging, append-only outcomes, calibration (accuracy, Brier, buckets, overconfidence flag), resolver job with prediction_error events and belief feedback
|
||||
- ☑ Brain: after-cycle hooks, goals and motivation in cycle state, effective weights recorded per cycle
|
||||
- ☑ Cognition: motivation hook, goal monitor, curiosity (questions from misses, contradictions, unexplained observations) with question → investigation goal handler, hypothesis-derived predictions, goal relevance in attend
|
||||
- ☑ API: goals, predictions (create/resolve/calibration), state (get/reset/drive ranges); UI: goals page, prediction dashboard with calibration, cognitive state panel on overview, effective weights on workspace; CLI goal/prediction/state
|
||||
- ☑ Docs: predictions.md, motivation.md. Demo scenario 4 (prediction error) verified live
|
||||
|
||||
### Phase 10 – Metacognition, reflection, consolidation, narrative, notifications
|
||||
- ☐ metacognitive checks and warnings; reflection scheduler; consolidation scheduler with operator controls; narrative generation; notification thresholds, suppression, aggregation, reasons
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
extraCommands = append(extraCommands,
|
||||
command{"goal", "list <mind>|add <mind> <title...> – goals", cmdGoal},
|
||||
command{"prediction", "list <mind>|calibration <mind> – predictions", cmdPrediction},
|
||||
command{"state", "show <mind>|reset <mind> – drives and affect", cmdState},
|
||||
)
|
||||
}
|
||||
|
||||
func cmdGoal(args []string) int {
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: noemactl goal list <mind> | add <mind> <title...>")
|
||||
return exitUsage
|
||||
}
|
||||
switch args[0] {
|
||||
case "list":
|
||||
var out struct{ Items []map[string]any }
|
||||
if code, err := apiRequest("GET", "/api/v1/minds/"+args[1]+"/goals", nil, &out); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
for _, g := range out.Items {
|
||||
fmt.Printf("%-10v %.2f %3.0f%% %s [%v]\n", g["status"], num(g["priority"]), num(g["progress"])*100, g["title"], g["kind"])
|
||||
}
|
||||
return exitOK
|
||||
case "add":
|
||||
var g map[string]any
|
||||
if code, err := apiRequest("POST", "/api/v1/minds/"+args[1]+"/goals", map[string]any{"title": strings.Join(args[2:], " "), "kind": "task", "priority": 0.6}, &g); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
fmt.Println("created goal", g["id"])
|
||||
return exitOK
|
||||
}
|
||||
return exitUsage
|
||||
}
|
||||
|
||||
func cmdPrediction(args []string) int {
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: noemactl prediction list <mind> | calibration <mind>")
|
||||
return exitUsage
|
||||
}
|
||||
switch args[0] {
|
||||
case "list":
|
||||
var out struct{ Items []map[string]any }
|
||||
if code, err := apiRequest("GET", "/api/v1/minds/"+args[1]+"/predictions", nil, &out); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
for _, p := range out.Items {
|
||||
fmt.Printf("%-10v %.2f %s (until %v)\n", p["status"], num(p["confidence"]), p["statement"], fmt.Sprint(p["window_end"])[:16])
|
||||
}
|
||||
return exitOK
|
||||
case "calibration":
|
||||
var c map[string]any
|
||||
if code, err := apiRequest("GET", "/api/v1/minds/"+args[1]+"/predictions/calibration", nil, &c); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
enc.Encode(c) //nolint:errcheck
|
||||
return exitOK
|
||||
}
|
||||
return exitUsage
|
||||
}
|
||||
|
||||
func cmdState(args []string) int {
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: noemactl state show <mind> | reset <mind>")
|
||||
return exitUsage
|
||||
}
|
||||
var out map[string]any
|
||||
switch args[0] {
|
||||
case "show":
|
||||
if code, err := apiRequest("GET", "/api/v1/minds/"+args[1]+"/state", nil, &out); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
case "reset":
|
||||
if code, err := apiRequest("POST", "/api/v1/minds/"+args[1]+"/state/reset", map[string]any{}, &out); err != nil {
|
||||
return exitFor(code, err)
|
||||
}
|
||||
fmt.Println("reset to baseline")
|
||||
return exitOK
|
||||
default:
|
||||
return exitUsage
|
||||
}
|
||||
st, _ := out["state"].(map[string]any)
|
||||
if aff, ok := st["affect"].(map[string]any); ok {
|
||||
fmt.Println("affect:")
|
||||
for _, k := range []string{"valence", "arousal", "curiosity", "concern", "frustration", "confidence", "uncertainty", "engagement", "task_pressure", "load"} {
|
||||
fmt.Printf(" %-14s %5.2f\n", k, num(aff[k]))
|
||||
}
|
||||
}
|
||||
if drives, ok := st["drives"].([]any); ok {
|
||||
fmt.Println("drives:")
|
||||
for _, d := range drives {
|
||||
dm, _ := d.(map[string]any)
|
||||
fmt.Printf(" %-22s %5.2f [%.2f–%.2f] baseline %.2f\n", dm["name"], num(dm["value"]), num(dm["min"]), num(dm["max"]), num(dm["baseline"]))
|
||||
}
|
||||
}
|
||||
return exitOK
|
||||
}
|
||||
+33
-10
@@ -23,11 +23,14 @@ import (
|
||||
"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"
|
||||
@@ -119,10 +122,17 @@ func run() error {
|
||||
deps.Memory = memory.NewStore(ctx, db, deps.LLM.Embedder(ctx))
|
||||
deps.Beliefs = beliefs.NewStore(db)
|
||||
deps.Conversations = conversation.NewStore(db)
|
||||
registry := brain.NewRegistry().AddProcess(cognition.Attend{}).AddProcess(cognition.Retrieve{}).AddProcess(cognition.Associate{}).AddProcess(cognition.Perceive{})
|
||||
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}}, group)
|
||||
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 {
|
||||
@@ -130,15 +140,28 @@ func run() error {
|
||||
}
|
||||
|
||||
var srv *api.Server
|
||||
uiHandler, err := ui.New(deps, func(r *http.Request) any { return srv.Status(r.Context()) },
|
||||
func(r *http.Request, id ids.ID) (api.EventContext, error) { return srv.Context(r, id) },
|
||||
func(r *http.Request, m minds.Mind) (api.WorkspaceView, error) { return srv.Workspace(r.Context(), m) },
|
||||
func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error) { return srv.MemoryDetail(r, mind, id) },
|
||||
func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error) { return srv.BeliefDetail(r, mind, id) },
|
||||
func() beliefs.Emitter { return srv.Emitter() },
|
||||
func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# Goals, drives and cognitive state
|
||||
|
||||
## Goals
|
||||
|
||||
Goals form a tree (`goals`) with kind (task, investigation, question, maintenance), status, priority, progress, blockers, owner and origin event. Operators create task goals; curiosity creates investigation goals from unexplained observations, prediction errors and contradictions; reflection (phase 10) adds maintenance goals.
|
||||
|
||||
Goals influence cognition through the `goal_relevance` attention feature: each candidate's text is compared with active goals (token overlap weighted by priority). The goal monitor process raises stalled goals and new observations that touch a goal. Goals never grant capabilities; acting still goes through the policy layer.
|
||||
|
||||
## Drives
|
||||
|
||||
Eight drives with operator-configured ranges: curiosity, uncertainty reduction, task completion, consistency, novelty, social coherence, exploration, rest. Baselines derive from personality traits and values. The set is closed in code; there is no self-preservation, replication or acquisition drive and no runtime way to add one (threat T25).
|
||||
|
||||
Drives modulate attention weights each cycle (`motivation.Modulate`): curiosity scales novelty, task completion scales goal relevance, uncertainty reduction scales contradiction, exploration lowers the pull of current context. The workspace page shows the effective weights next to the configured ones.
|
||||
|
||||
## Affect-like state
|
||||
|
||||
Ten scalars: valence, arousal, curiosity, concern, frustration, confidence, uncertainty, engagement, task pressure, load. They are updated by an explicit rule table (`motivation.Rules`) from events — contradictions raise concern and uncertainty, prediction errors raise arousal and lower confidence, completed goals raise valence and relieve pressure, and so on — then decay towards personality baselines (half-life one hour; six hours for drives). Changes above 0.1 emit `emotion_changed` / `drive_changed` events with the reasons.
|
||||
|
||||
This is a functional model, not a claim of feeling (docs/interpretation.md). Everything is visible on the mind overview and via `noemactl state show`, and operators can reset it to baseline or adjust drive ranges; both are recorded as operator actions.
|
||||
@@ -0,0 +1,32 @@
|
||||
# Prediction system
|
||||
|
||||
A prediction is an explicit expectation with a resolution window and criteria that code can check (spec §11). Predictions are how the mind finds out it was wrong.
|
||||
|
||||
## Structure
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| statement | what is expected, in words |
|
||||
| confidence | 0.05–0.95; certainty is refused |
|
||||
| window_start / window_end | when the expectation applies |
|
||||
| criteria | `event_match` (terms occur or stay absent in events of given types), `belief` (a belief reaches a confidence), or `operator` (a person judges) |
|
||||
| belief_id | the belief the prediction was derived from, if any |
|
||||
| status | open → confirmed, refuted, expired or withdrawn |
|
||||
|
||||
`prediction_results` is append-only and keeps every outcome with the stated confidence and the error, so calibration cannot be rewritten.
|
||||
|
||||
## Resolution
|
||||
|
||||
A scheduler runs the resolver every minute. For each open prediction it judges deterministically (`predictions.Judge`): event-match predictions look at the mind's events inside the window (excluding the prediction's own creation and other prediction events); belief predictions compare the current confidence; operator predictions expire a week after the window. Operators can also confirm, refute or withdraw from the UI, API or CLI.
|
||||
|
||||
Outcomes emit `prediction_resolved` or, for refutations, `prediction_error` with salience scaled by how confident the miss was. The result becomes evidence on the linked belief: confirmed adds support (weight 0.3), refuted adds opposition (weight 0.5), and the belief is re-evaluated with the usual arithmetic. The motivation rules raise arousal and uncertainty and lower confidence after a miss; curiosity turns the miss into a question that competes for attention and, if admitted, becomes an investigation goal.
|
||||
|
||||
## Where predictions come from
|
||||
|
||||
- Operators, through the Predictions page, the API or `noemactl`.
|
||||
- Causal hypotheses: when the associate process turns "A may have caused B" into a belief, a prediction is derived automatically — the effect should recur within 24 hours if the cause is real — with the belief's confidence, so a miss lowers the hypothesis.
|
||||
- Later phases add predictions from consolidation and reflection.
|
||||
|
||||
## Calibration
|
||||
|
||||
The dashboard shows accuracy (confirmed over decided), the Brier score, and buckets of stated confidence against actual hit rate over the last 90 days. A mind whose mean stated confidence exceeds its hit rate by more than 0.15 across at least five decided predictions is flagged as overconfident; metacognition (phase 10) uses the same signal to discount new confident claims.
|
||||
@@ -18,11 +18,14 @@ import (
|
||||
"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"
|
||||
@@ -39,8 +42,9 @@ func testServer(t *testing.T) (*httptest.Server, string) {
|
||||
llmStore := router.NewStore(pool)
|
||||
deps := app.Deps{Config: cfg, Log: log, DB: pool, Auth: auth.NewService(pool, time.Hour, 2*time.Hour), Secrets: sec, LLMStore: llmStore,
|
||||
LLM: router.NewRouter(llmStore, sec, log, nil, 2), Prompts: prompts.NewStore(pool),
|
||||
Audit: audit.New(pool, log), Metrics: observability.NewMetrics(), Minds: minds.NewStore(pool), Conversations: conversation.NewStore(pool), Beliefs: beliefs.NewStore(pool), Memory: memory.NewStore(context.Background(), pool, nil), Events: evstore.NewStore(pool, evstore.NewBus(50), nil), Migrations: migrations.FS, Started: time.Now()}
|
||||
Audit: audit.New(pool, log), Metrics: observability.NewMetrics(), Minds: minds.NewStore(pool), Conversations: conversation.NewStore(pool), Goals: goals.NewStore(pool), Motivation: motivation.NewStore(pool), Predictions: predictions.NewStore(pool), Beliefs: beliefs.NewStore(pool), Memory: memory.NewStore(context.Background(), pool, nil), Events: evstore.NewStore(pool, evstore.NewBus(50), nil), Migrations: migrations.FS, Started: time.Now()}
|
||||
ctx := context.Background()
|
||||
deps.Resolver = &predictions.Resolver{Store: deps.Predictions, Events: deps.Events, Beliefs: deps.Beliefs, Log: log}
|
||||
group := scheduler.NewGroup(ctx, log)
|
||||
t.Cleanup(func() { group.Stop() })
|
||||
deps.Brain = brain.NewManager(brain.Config{DB: pool, Events: deps.Events, Minds: deps.Minds, Registry: brain.NewRegistry(), Pool: scheduler.NewPool(2), Log: log, Metrics: deps.Metrics}, group)
|
||||
|
||||
@@ -0,0 +1,422 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/goals"
|
||||
"github.com/andydixon/noema/internal/motivation"
|
||||
"github.com/andydixon/noema/internal/predictions"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
func (s *Server) motivationRoutes() {
|
||||
m := s.mux
|
||||
m.Handle("GET /api/v1/minds/{mind}/goals", require(auth.RoleObserver, s.goalsList))
|
||||
m.Handle("POST /api/v1/minds/{mind}/goals", require(auth.RoleOperator, s.goalsCreate))
|
||||
m.Handle("GET /api/v1/minds/{mind}/goals/{id}", require(auth.RoleObserver, s.goalGet))
|
||||
m.Handle("PATCH /api/v1/minds/{mind}/goals/{id}", require(auth.RoleOperator, s.goalPatch))
|
||||
m.Handle("GET /api/v1/minds/{mind}/predictions", require(auth.RoleObserver, s.predictionsList))
|
||||
m.Handle("POST /api/v1/minds/{mind}/predictions", require(auth.RoleOperator, s.predictionsCreate))
|
||||
m.Handle("GET /api/v1/minds/{mind}/predictions/calibration", require(auth.RoleObserver, s.predictionsCalibration))
|
||||
m.Handle("GET /api/v1/minds/{mind}/predictions/{id}", require(auth.RoleObserver, s.predictionGet))
|
||||
m.Handle("POST /api/v1/minds/{mind}/predictions/{id}/resolve", require(auth.RoleOperator, s.predictionResolve))
|
||||
m.Handle("GET /api/v1/minds/{mind}/state", require(auth.RoleObserver, s.stateGet))
|
||||
m.Handle("POST /api/v1/minds/{mind}/state/reset", require(auth.RoleOperator, s.stateReset))
|
||||
m.Handle("PUT /api/v1/minds/{mind}/state/drives/{name}", require(auth.RoleAdministrator, s.driveSet))
|
||||
}
|
||||
|
||||
func goalErr(err error) error {
|
||||
if errors.Is(err, goals.ErrInvalid) || errors.Is(err, predictions.ErrInvalid) {
|
||||
return ValidationError(err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) goalsList(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var statuses []goals.Status
|
||||
for _, st := range strings.Split(r.URL.Query().Get("status"), ",") {
|
||||
if st = strings.TrimSpace(st); st != "" {
|
||||
statuses = append(statuses, goals.Status(st))
|
||||
}
|
||||
}
|
||||
list, err := s.deps.Goals.List(r.Context(), m.ID, statuses, 0)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
if list == nil {
|
||||
list = []goals.Goal{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, page[goals.Goal]{Items: list})
|
||||
}
|
||||
|
||||
// CreateGoal stores a goal and emits goal_created; shared with the UI.
|
||||
func (s *Server) CreateGoal(r *http.Request, n goals.New) (goals.Goal, error) {
|
||||
n.Owner = "operator"
|
||||
g, err := s.deps.Goals.Create(r.Context(), s.deps.DB, n)
|
||||
if err != nil {
|
||||
return g, goalErr(err)
|
||||
}
|
||||
s.deps.Events.Append(r.Context(), events.New{MindID: n.MindID, Type: events.GoalCreated, Source: "operator", Summary: "operator " + actor(r) + " set goal: " + g.Title, Salience: 0.5 + 0.4*g.Priority, Confidence: 1, //nolint:errcheck
|
||||
Evidence: []events.EvidenceRef{{Kind: "goal", ID: string(g.ID)}}, Metadata: map[string]any{"goal_id": string(g.ID), "kind": g.Kind, "priority": g.Priority, "actor": actor(r)}, Payload: g})
|
||||
s.deps.Audit.Log(r.Context(), "goal.create", "goal", g.ID.String(), map[string]any{"title": g.Title})
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func (s *Server) goalsCreate(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Kind goals.Kind `json:"kind"`
|
||||
Priority float64 `json:"priority"`
|
||||
ParentID ids.ID `json:"parent_id"`
|
||||
Subject string `json:"subject"`
|
||||
DueAt *time.Time `json:"due_at"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
g, err := s.CreateGoal(r, goals.New{MindID: m.ID, ParentID: in.ParentID, Title: in.Title, Description: in.Description, Kind: in.Kind, Priority: in.Priority, Subject: in.Subject, DueAt: in.DueAt})
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, g)
|
||||
}
|
||||
|
||||
func (s *Server) goalGet(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
fail(w, r, ValidationError("bad id"))
|
||||
return
|
||||
}
|
||||
g, err := s.deps.Goals.Get(r.Context(), m.ID, id)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, g)
|
||||
}
|
||||
|
||||
// UpdateGoal applies an update and emits the matching event; shared with the UI.
|
||||
func (s *Server) UpdateGoal(r *http.Request, mind, id ids.ID, u goals.Update) (goals.Goal, error) {
|
||||
before, err := s.deps.Goals.Get(r.Context(), mind, id)
|
||||
if err != nil {
|
||||
return goals.Goal{}, err
|
||||
}
|
||||
g, err := s.deps.Goals.Apply(r.Context(), s.deps.DB, mind, id, u)
|
||||
if err != nil {
|
||||
return g, goalErr(err)
|
||||
}
|
||||
typ, summary := events.GoalUpdated, "goal updated: "+g.Title
|
||||
switch {
|
||||
case g.Status == goals.StatusCompleted && before.Status != goals.StatusCompleted:
|
||||
typ, summary = events.GoalCompleted, "goal completed: "+g.Title
|
||||
case g.Status == goals.StatusAbandoned && before.Status != goals.StatusAbandoned:
|
||||
typ, summary = events.GoalAbandoned, "goal abandoned: "+g.Title
|
||||
}
|
||||
s.deps.Events.Append(r.Context(), events.New{MindID: mind, Type: typ, Source: "operator", Summary: summary, Salience: 0.4 + 0.3*g.Priority, Confidence: 1, //nolint:errcheck
|
||||
Evidence: []events.EvidenceRef{{Kind: "goal", ID: string(g.ID)}}, Metadata: map[string]any{"goal_id": string(g.ID), "status": g.Status, "progress": g.Progress, "actor": actor(r)},
|
||||
Payload: map[string]any{"before": map[string]any{"status": before.Status, "progress": before.Progress}, "after": g}})
|
||||
s.deps.Audit.Log(r.Context(), "goal.update", "goal", g.ID.String(), map[string]any{"status": g.Status, "progress": g.Progress})
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func (s *Server) goalPatch(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
fail(w, r, ValidationError("bad id"))
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Status *goals.Status `json:"status"`
|
||||
Progress *float64 `json:"progress"`
|
||||
Priority *float64 `json:"priority"`
|
||||
Blockers *[]string `json:"blockers"`
|
||||
Title *string `json:"title"`
|
||||
Desc *string `json:"description"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
g, err := s.UpdateGoal(r, m.ID, id, goals.Update{Status: in.Status, Progress: in.Progress, Priority: in.Priority, Blockers: in.Blockers, Title: in.Title, Desc: in.Desc})
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, g)
|
||||
}
|
||||
|
||||
func (s *Server) predictionsList(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var statuses []predictions.Status
|
||||
for _, st := range strings.Split(r.URL.Query().Get("status"), ",") {
|
||||
if st = strings.TrimSpace(st); st != "" {
|
||||
statuses = append(statuses, predictions.Status(st))
|
||||
}
|
||||
}
|
||||
list, err := s.deps.Predictions.List(r.Context(), m.ID, statuses, 0)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
if list == nil {
|
||||
list = []predictions.Prediction{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, page[predictions.Prediction]{Items: list})
|
||||
}
|
||||
|
||||
// CreatePrediction stores an operator prediction and emits prediction_created.
|
||||
func (s *Server) CreatePrediction(r *http.Request, n predictions.New) (predictions.Prediction, error) {
|
||||
n.Source = "operator"
|
||||
p, err := s.deps.Predictions.Create(r.Context(), s.deps.DB, n)
|
||||
if err != nil {
|
||||
return p, goalErr(err)
|
||||
}
|
||||
evs, _ := s.deps.Events.Append(r.Context(), events.New{MindID: n.MindID, Type: events.PredictionCreated, Source: "operator", Summary: "operator " + actor(r) + " predicted (" + trunc(p.Statement, 100) + ")", Salience: 0.4, Confidence: p.Confidence,
|
||||
Evidence: []events.EvidenceRef{{Kind: "prediction", ID: string(p.ID)}}, Metadata: map[string]any{"prediction_id": string(p.ID), "window_end": p.WindowEnd, "actor": actor(r)}, Payload: p})
|
||||
if len(evs) > 0 {
|
||||
s.deps.DB.Exec(r.Context(), `UPDATE predictions SET origin_event=$2 WHERE id=$1`, p.ID, evs[0].ID) //nolint:errcheck
|
||||
}
|
||||
if s.deps.Metrics != nil {
|
||||
s.deps.Metrics.Predictions.Inc()
|
||||
}
|
||||
s.deps.Audit.Log(r.Context(), "prediction.create", "prediction", p.ID.String(), map[string]any{"statement": p.Statement})
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (s *Server) predictionsCreate(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Statement string `json:"statement"`
|
||||
Subject string `json:"subject"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
WindowEnd time.Time `json:"window_end"`
|
||||
Criteria predictions.Criteria `json:"criteria"`
|
||||
BeliefID ids.ID `json:"belief_id"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
p, err := s.CreatePrediction(r, predictions.New{MindID: m.ID, Statement: in.Statement, Subject: in.Subject, Confidence: in.Confidence, WindowEnd: in.WindowEnd, Criteria: in.Criteria, BeliefID: in.BeliefID})
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, p)
|
||||
}
|
||||
|
||||
func (s *Server) predictionGet(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
fail(w, r, ValidationError("bad id"))
|
||||
return
|
||||
}
|
||||
p, err := s.deps.Predictions.Get(r.Context(), m.ID, id)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, p)
|
||||
}
|
||||
|
||||
// ResolvePrediction is the operator resolution path; shared with the UI.
|
||||
func (s *Server) ResolvePrediction(r *http.Request, mind, id ids.ID, outcome predictions.Status, reason string) (predictions.Result, error) {
|
||||
p, err := s.deps.Predictions.Get(r.Context(), mind, id)
|
||||
if err != nil {
|
||||
return predictions.Result{}, err
|
||||
}
|
||||
if outcome != predictions.StatusConfirmed && outcome != predictions.StatusRefuted && outcome != predictions.StatusWithdrawn {
|
||||
return predictions.Result{}, ValidationError("outcome must be confirmed, refuted or withdrawn")
|
||||
}
|
||||
var res predictions.Result
|
||||
var emitted []events.Event
|
||||
err = txDo(r, s, func(q txQuerier) error {
|
||||
var err error
|
||||
res, emitted, err = s.deps.Resolver.ResolveIn(r.Context(), q, p, predictions.Outcome{Status: outcome, Reason: "operator " + actor(r) + ": " + reason}, "operator")
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return res, goalErr(err)
|
||||
}
|
||||
s.deps.Events.Publish(emitted...)
|
||||
s.deps.Audit.Log(r.Context(), "prediction.resolve", "prediction", id.String(), map[string]any{"outcome": outcome, "reason": reason})
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *Server) predictionResolve(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
fail(w, r, ValidationError("bad id"))
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Outcome predictions.Status `json:"outcome"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
res, err := s.ResolvePrediction(r, m.ID, id, in.Outcome, in.Reason)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (s *Server) predictionsCalibration(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
window, err := time.ParseDuration(r.URL.Query().Get("window"))
|
||||
if err != nil {
|
||||
window = 90 * 24 * time.Hour
|
||||
}
|
||||
c, err := s.deps.Predictions.Calibrate(r.Context(), m.ID, time.Now().Add(-window))
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, c)
|
||||
}
|
||||
|
||||
func (s *Server) stateGet(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
st, err := s.deps.Motivation.Load(r.Context(), s.deps.DB, m)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
motivation.Decay(&st, motivation.Baselines(m.Personality), time.Since(st.Affect.UpdatedAt))
|
||||
writeJSON(w, http.StatusOK, map[string]any{"state": st, "baselines": motivation.Baselines(m.Personality), "effective_weights": motivation.Modulate(m.Config.AttentionWeights, st)})
|
||||
}
|
||||
|
||||
// ResetState returns drives and affect to baseline; shared with the UI.
|
||||
func (s *Server) ResetState(r *http.Request, mind ids.ID) (motivation.State, error) {
|
||||
m, err := s.deps.Minds.Get(r.Context(), mind)
|
||||
if err != nil {
|
||||
return motivation.State{}, err
|
||||
}
|
||||
st, err := s.deps.Motivation.Reset(r.Context(), s.deps.DB, m)
|
||||
if err != nil {
|
||||
return st, err
|
||||
}
|
||||
s.deps.Events.OperatorAction(r.Context(), mind, actor(r), "state.reset", "Operator "+actor(r)+" reset drives and affect to baseline", nil)
|
||||
s.deps.Audit.Log(r.Context(), "state.reset", "mind", mind.String(), nil)
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (s *Server) stateReset(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
st, err := s.ResetState(r, m.ID)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, st)
|
||||
}
|
||||
|
||||
func (s *Server) driveSet(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := s.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := r.PathValue("name")
|
||||
known := false
|
||||
for _, n := range motivation.DriveNames {
|
||||
if n == name {
|
||||
known = true
|
||||
}
|
||||
}
|
||||
if !known {
|
||||
fail(w, r, ValidationError("unknown drive; the set is fixed and excludes self-preservation by design"))
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Value *float64 `json:"value"`
|
||||
Min *float64 `json:"min"`
|
||||
Max *float64 `json:"max"`
|
||||
Baseline *float64 `json:"baseline"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
st, err := s.deps.Motivation.Load(r.Context(), s.deps.DB, m)
|
||||
if err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
var d motivation.Drive
|
||||
for _, x := range st.Drives {
|
||||
if x.Name == name {
|
||||
d = x
|
||||
}
|
||||
}
|
||||
if in.Min != nil {
|
||||
d.Min = *in.Min
|
||||
}
|
||||
if in.Max != nil {
|
||||
d.Max = *in.Max
|
||||
}
|
||||
if in.Baseline != nil {
|
||||
d.Baseline = *in.Baseline
|
||||
}
|
||||
if in.Value != nil {
|
||||
d.Value = *in.Value
|
||||
}
|
||||
if err := s.deps.Motivation.SaveDrive(r.Context(), s.deps.DB, m.ID, d); err != nil {
|
||||
fail(w, r, err)
|
||||
return
|
||||
}
|
||||
s.deps.Events.OperatorAction(r.Context(), m.ID, actor(r), "drive.set", "Operator "+actor(r)+" adjusted drive "+name, map[string]any{"drive": d})
|
||||
s.deps.Audit.Log(r.Context(), "drive.set", "mind", m.ID.String(), map[string]any{"drive": name, "value": d.Value, "min": d.Min, "max": d.Max})
|
||||
writeJSON(w, http.StatusOK, d)
|
||||
}
|
||||
@@ -60,6 +60,7 @@ func (s *Server) routes() {
|
||||
s.promptRoutes()
|
||||
s.conversationRoutes()
|
||||
s.importRoutes()
|
||||
s.motivationRoutes()
|
||||
|
||||
m.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "no such endpoint")
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
)
|
||||
|
||||
// txQuerier is what handlers may run inside an API transaction.
|
||||
type txQuerier = storage.Querier
|
||||
|
||||
// txDo runs fn in a transaction on the shared pool.
|
||||
func txDo(r *http.Request, s *Server, fn func(q txQuerier) error) error {
|
||||
return storage.Tx(r.Context(), s.deps.DB, func(tx pgx.Tx) error { return fn(tx) })
|
||||
}
|
||||
@@ -17,11 +17,14 @@ import (
|
||||
"github.com/andydixon/noema/internal/config"
|
||||
"github.com/andydixon/noema/internal/conversation"
|
||||
"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/security/secrets"
|
||||
)
|
||||
|
||||
@@ -39,6 +42,10 @@ type Deps struct {
|
||||
Memory *memory.Store
|
||||
Beliefs *beliefs.Store
|
||||
Conversations *conversation.Store
|
||||
Goals *goals.Store
|
||||
Motivation *motivation.Store
|
||||
Predictions *predictions.Store
|
||||
Resolver *predictions.Resolver
|
||||
LLM *router.Router
|
||||
LLMStore *router.Store
|
||||
Prompts *prompts.Store
|
||||
|
||||
+22
-3
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
evstore "github.com/andydixon/noema/internal/events"
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/motivation"
|
||||
"github.com/andydixon/noema/internal/observability"
|
||||
"github.com/andydixon/noema/internal/scheduler"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
@@ -412,6 +413,17 @@ func (r *Runtime) cycle(ctx context.Context, tx pgx.Tx, trigger string, start ti
|
||||
}
|
||||
sm, _ := r.minds.GetSelfModel(ctx, r.mind.ID)
|
||||
st := &State{Mind: r.mind, SelfModel: sm, Mode: Mode(r.state.Mode), Cycle: r.state.Cycle + 1, CycleID: cycleID, Now: start, NewEvents: newEvents, Workspace: r.state, Services: r.services}
|
||||
if r.services.Goals != nil {
|
||||
st.Goals, _ = r.services.Goals.Active(ctx, r.mind.ID)
|
||||
}
|
||||
st.Weights = r.mind.Config.AttentionWeights
|
||||
if r.services.Motivation != nil {
|
||||
if ms, err := r.services.Motivation.Load(ctx, tx, r.mind); err == nil {
|
||||
motivation.Decay(&ms, motivation.Baselines(r.mind.Personality), start.Sub(ms.Affect.UpdatedAt))
|
||||
st.Motiv = ms
|
||||
st.Weights = motivation.Modulate(r.mind.Config.AttentionWeights, ms)
|
||||
}
|
||||
}
|
||||
c := &Cycle{Runtime: r, State: st, Tx: tx}
|
||||
|
||||
// 1. Ask every process, concurrently and bounded.
|
||||
@@ -428,8 +440,8 @@ func (r *Runtime) cycle(ctx context.Context, tx pgx.Tx, trigger string, start ti
|
||||
}
|
||||
r.state.Cycle++
|
||||
|
||||
// 2. Score deterministically; suppress loops.
|
||||
weights := r.mind.Config.AttentionWeights
|
||||
// 2. Score deterministically with the cycle's effective weights; suppress loops.
|
||||
weights := st.Weights
|
||||
var scored []attention.Scored
|
||||
byID := map[ids.ID]*Candidate{}
|
||||
suppressed := map[ids.ID]string{}
|
||||
@@ -563,6 +575,11 @@ func (r *Runtime) cycle(ctx context.Context, tx pgx.Tx, trigger string, start ti
|
||||
}
|
||||
r.loops.record(fingerprint(Candidate{Process: t.Process, Kind: t.Kind, Subject: t.Subject}))
|
||||
}
|
||||
for _, h := range r.registry.hooks {
|
||||
if err := h.AfterCycle(ctx, c, c.emitted); err != nil {
|
||||
r.log.Warn("hook failed", "hook", h.Name(), "err", err)
|
||||
}
|
||||
}
|
||||
pruned := r.state.Prune(0.05)
|
||||
|
||||
// 7. Broadcast the resulting workspace and note attention shifts.
|
||||
@@ -575,7 +592,8 @@ func (r *Runtime) cycle(ctx context.Context, tx pgx.Tx, trigger string, start ti
|
||||
Salience: 0.2, Confidence: 1, Metadata: map[string]any{"thoughts": len(r.state.Workspace), "working_memory": len(r.state.WorkingMemory)},
|
||||
Payload: map[string]any{"workspace": r.state.Workspace, "working_memory": r.state.WorkingMemory, "displaced": displaced, "pruned": pruned}})
|
||||
c.Emit(events.New{Type: events.CycleCompleted, Source: "brain", Summary: fmt.Sprintf("cycle %d (%s): %d events, %d candidates, %d admitted, %d suppressed in %dms", r.state.Cycle, trigger, len(newEvents), len(scored), len(admittedThoughts), len(suppressed), time.Since(start).Milliseconds()),
|
||||
Salience: 0.1, Confidence: 1, Metadata: map[string]any{"trigger": trigger, "candidates": len(scored), "admitted": len(admittedThoughts), "suppressed": len(suppressed), "duration_ms": time.Since(start).Milliseconds()}})
|
||||
Salience: 0.1, Confidence: 1, Metadata: map[string]any{"trigger": trigger, "candidates": len(scored), "admitted": len(admittedThoughts), "suppressed": len(suppressed), "duration_ms": time.Since(start).Milliseconds()},
|
||||
Payload: map[string]any{"effective_weights": st.Weights}})
|
||||
}
|
||||
if err := c.flush(ctx); err != nil {
|
||||
return nil, err
|
||||
@@ -583,6 +601,7 @@ func (r *Runtime) cycle(ctx context.Context, tx pgx.Tx, trigger string, start ti
|
||||
|
||||
// 8. Persist and advance the cursor to the last *read* event.
|
||||
r.state.LastCycleID, r.state.LastCycleAt = cycleID, start
|
||||
r.state.EffectiveWeights = st.Weights
|
||||
if len(newEvents) > 0 {
|
||||
if err := r.events.SetCursor(ctx, tx, r.mind.ID, cursorConsumer, newEvents[len(newEvents)-1].Seq); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -11,10 +11,13 @@ import (
|
||||
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"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/predictions"
|
||||
"github.com/andydixon/noema/internal/workspace"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
@@ -66,7 +69,10 @@ type State struct {
|
||||
NewEvents []events.Event // since the last cycle, ascending
|
||||
Workspace workspace.State // snapshot before this cycle
|
||||
Services Services
|
||||
llmCalls atomic.Int32 // model calls made in this cycle; processes and handlers share the cap
|
||||
Goals []goals.Goal // active goals, for relevance scoring
|
||||
Motiv motivation.State // drives and affect at the start of the cycle
|
||||
Weights map[string]float64 // effective attention weights after modulation
|
||||
llmCalls atomic.Int32 // model calls made in this cycle; processes and handlers share the cap
|
||||
}
|
||||
|
||||
// AllowLLM reserves one model call against the mind's per-cycle cap and
|
||||
@@ -85,11 +91,14 @@ func (s *State) AllowLLM() bool {
|
||||
|
||||
// Services are read handles processes may use. Extended per phase.
|
||||
type Services struct {
|
||||
Events EventReader
|
||||
Memory *memory.Store
|
||||
Beliefs *beliefs.Store
|
||||
LLM *router.Router
|
||||
Prompts *prompts.Store
|
||||
Events EventReader
|
||||
Memory *memory.Store
|
||||
Beliefs *beliefs.Store
|
||||
LLM *router.Router
|
||||
Prompts *prompts.Store
|
||||
Goals *goals.Store
|
||||
Motivation *motivation.Store
|
||||
Predictions *predictions.Store
|
||||
}
|
||||
|
||||
// EventReader is the subset of the event store processes may call.
|
||||
@@ -102,10 +111,18 @@ type EventReader interface {
|
||||
// receive the cycle's emitter so provenance links are consistent.
|
||||
type Handler func(ctx context.Context, c *Cycle, t workspace.Thought) error
|
||||
|
||||
// Registry holds processes and handlers. It is immutable after construction.
|
||||
// Hook runs after the executive handlers of a cycle, with the events emitted
|
||||
// so far, for cross-cutting updates such as drives and affect.
|
||||
type Hook interface {
|
||||
Name() string
|
||||
AfterCycle(ctx context.Context, c *Cycle, emitted []events.Event) error
|
||||
}
|
||||
|
||||
// Registry holds processes, handlers and hooks. It is immutable after construction.
|
||||
type Registry struct {
|
||||
processes []Process
|
||||
handlers map[string]Handler
|
||||
hooks []Hook
|
||||
}
|
||||
|
||||
// NewRegistry builds a registry.
|
||||
@@ -117,5 +134,8 @@ func (r *Registry) AddProcess(p Process) *Registry { r.processes = append(r.proc
|
||||
// Handle registers the handler for a thought kind.
|
||||
func (r *Registry) Handle(kind string, h Handler) *Registry { r.handlers[kind] = h; return r }
|
||||
|
||||
// AddHook registers an after-cycle hook.
|
||||
func (r *Registry) AddHook(h Hook) *Registry { r.hooks = append(r.hooks, h); return r }
|
||||
|
||||
// Processes returns the registered processes.
|
||||
func (r *Registry) Processes() []Process { return r.processes }
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
"github.com/andydixon/noema/internal/brain"
|
||||
"github.com/andydixon/noema/internal/goals"
|
||||
"github.com/andydixon/noema/internal/workspace"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
@@ -56,6 +57,7 @@ func (Attend) Observe(_ context.Context, s *brain.State) ([]brain.Candidate, err
|
||||
f := attention.Features{Salience: ev.Salience, Confidence: ev.Confidence, Consequence: 0.4}
|
||||
f.Urgency = urgency(toks)
|
||||
f.ContextRelevance = workspace.Overlap(toks, ctxTokens)
|
||||
f.GoalRelevance, _ = goals.Relevance(text, s.Goals)
|
||||
f.Novelty = 1 - f.ContextRelevance*0.7 // things we are already holding are less novel
|
||||
switch trust {
|
||||
case events.TrustOperator:
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package cognition
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"github.com/andydixon/noema/internal/brain"
|
||||
"github.com/andydixon/noema/internal/memory"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// Curiosity proposes questions about unexplained observations, prediction
|
||||
// errors and contradictions. Its output is questions and investigation goals
|
||||
// only; it never grants or requests permission to act (spec §12).
|
||||
type Curiosity struct{}
|
||||
|
||||
// Name implements brain.Process.
|
||||
func (Curiosity) Name() string { return "curiosity" }
|
||||
|
||||
// Observe implements brain.Process.
|
||||
func (Curiosity) Observe(ctx context.Context, s *brain.State) ([]brain.Candidate, error) {
|
||||
drive := 0.5
|
||||
for _, d := range s.Motiv.Drives {
|
||||
if d.Name == "curiosity" {
|
||||
drive = d.Value
|
||||
}
|
||||
}
|
||||
var out []brain.Candidate
|
||||
for _, ev := range s.NewEvents {
|
||||
if strings.HasPrefix(ev.Source, "brain") && ev.Type != events.PredictionError && ev.Type != events.ContradictionDetected {
|
||||
continue
|
||||
}
|
||||
switch ev.Type {
|
||||
case events.PredictionError:
|
||||
stmt, _ := payloadField(ev, "statement")
|
||||
q := "Why was my prediction wrong: " + truncate(stmt, 120) + "?"
|
||||
out = append(out, curiosityCandidate(ev, q, "prediction:"+ev.Metadata["prediction_id"].(string), 0.8, drive))
|
||||
case events.ContradictionDetected:
|
||||
q := "Which is right: " + truncate(ev.Summary, 140) + "?"
|
||||
out = append(out, curiosityCandidate(ev, q, "contradiction:"+fmtID(ev.Metadata["belief_id"])+"+"+fmtID(ev.Metadata["other_id"]), 0.7, drive))
|
||||
case events.Observation:
|
||||
if ev.Metadata["conversation_id"] != nil || ev.Salience < 0.5 {
|
||||
continue
|
||||
}
|
||||
text := ev.Summary
|
||||
var p events.ObservationPayload
|
||||
if json.Unmarshal(ev.Payload, &p) == nil && p.Text != "" {
|
||||
text = p.Text
|
||||
}
|
||||
if urgency(tokenise(text)) < 0.5 {
|
||||
continue
|
||||
}
|
||||
// Unexplained: no belief and no memory speaks to it.
|
||||
explained := false
|
||||
if s.Services.Beliefs != nil {
|
||||
if bs, _ := s.Services.Beliefs.Search(ctx, s.Mind.ID, text, 3); hasLive(bs) {
|
||||
explained = true
|
||||
}
|
||||
}
|
||||
if !explained && s.Services.Memory != nil {
|
||||
if hits, _ := s.Services.Memory.Retrieve(ctx, s.Mind.ID, memory.Query{Text: text, Limit: 3, Now: s.Now}); len(hits) > 0 && hits[0].Score >= 0.6 && hits[0].Memory.OriginEvent != ev.ID {
|
||||
explained = true
|
||||
}
|
||||
}
|
||||
if explained {
|
||||
continue
|
||||
}
|
||||
q := "Why did this happen: " + truncate(text, 120) + "?"
|
||||
out = append(out, curiosityCandidate(ev, q, "unexplained:"+subjectKey(text), 0.5, drive))
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func curiosityCandidate(ev events.Event, question, subject string, weight, drive float64) brain.Candidate {
|
||||
return brain.Candidate{Kind: "question", Subject: subject, Summary: "question: " + question,
|
||||
Content: map[string]any{"question": question, "subject": subject, "source_event": string(ev.ID)},
|
||||
// A failed prediction or a contradiction is an unresolved conflict between what
|
||||
// was expected and what was seen, so the contradiction feature is set for both.
|
||||
Features: attention.Features{Salience: 0.4 + 0.45*weight, Novelty: 0.6 + 0.3*drive, Consequence: 0.5 + 0.3*weight, Urgency: 0.4 * weight, Confidence: 0.5,
|
||||
Contradiction: boolf(ev.Type == events.ContradictionDetected || ev.Type == events.PredictionError)},
|
||||
Confidence: 0.5, SourceEvents: []ids.ID{ev.ID}, Evidence: []events.EvidenceRef{{Kind: "event", ID: string(ev.ID), Note: "prompted the question"}}}
|
||||
}
|
||||
|
||||
func hasLive(bs []beliefs.Belief) bool {
|
||||
for _, b := range bs {
|
||||
if b.Status != beliefs.StatusRejected && b.Status != beliefs.StatusSuperseded {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func boolf(b bool) float64 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func fmtID(v any) string {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// subjectKey reduces text to its three most distinctive tokens so the same
|
||||
// unexplained situation yields the same question subject.
|
||||
func subjectKey(text string) string {
|
||||
toks := tokenise(text)
|
||||
var keep []string
|
||||
for _, t := range toks {
|
||||
if len(t) >= 4 && len(keep) < 3 {
|
||||
keep = append(keep, t)
|
||||
}
|
||||
}
|
||||
return strings.Join(keep, "+")
|
||||
}
|
||||
|
||||
func tokenise(text string) []string { return tokeniseWS(text) }
|
||||
@@ -0,0 +1,96 @@
|
||||
package cognition
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
"github.com/andydixon/noema/internal/brain"
|
||||
"github.com/andydixon/noema/internal/goals"
|
||||
"github.com/andydixon/noema/internal/workspace"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// GoalMonitor watches the goal tree: stalled goals are raised for attention,
|
||||
// and goals whose subject appears in new events get a progress nudge
|
||||
// candidate. It never completes a goal on its own; that needs evidence or
|
||||
// an operator.
|
||||
type GoalMonitor struct {
|
||||
StallAfter time.Duration
|
||||
}
|
||||
|
||||
// Name implements brain.Process.
|
||||
func (GoalMonitor) Name() string { return "goal-monitor" }
|
||||
|
||||
// Observe implements brain.Process.
|
||||
func (g GoalMonitor) Observe(ctx context.Context, s *brain.State) ([]brain.Candidate, error) {
|
||||
if s.Services.Goals == nil {
|
||||
return nil, nil
|
||||
}
|
||||
stall := g.StallAfter
|
||||
if stall <= 0 {
|
||||
stall = 24 * time.Hour
|
||||
}
|
||||
var out []brain.Candidate
|
||||
stalled, _ := s.Services.Goals.Stalled(ctx, s.Mind.ID, stall)
|
||||
for i, gl := range stalled {
|
||||
if i >= 3 {
|
||||
break
|
||||
}
|
||||
out = append(out, brain.Candidate{Kind: "goal", Subject: string(gl.ID), Summary: "goal stalled: " + gl.Title,
|
||||
Content: map[string]any{"goal_id": string(gl.ID), "reason": "no progress for " + stall.String(), "ref": string(gl.ID)},
|
||||
Features: attention.Features{Salience: 0.4, GoalRelevance: gl.Priority, Consequence: 0.5 * gl.Priority, Urgency: 0.3, Confidence: 1}, Confidence: 1,
|
||||
Evidence: []events.EvidenceRef{{Kind: "goal", ID: string(gl.ID)}}})
|
||||
}
|
||||
// New external events touching a goal's subject.
|
||||
for _, ev := range s.NewEvents {
|
||||
if strings.HasPrefix(ev.Source, "brain") || ev.Type != events.Observation {
|
||||
continue
|
||||
}
|
||||
if rel, id := goals.Relevance(ev.Summary, s.Goals); rel >= 0.5 && !id.IsZero() {
|
||||
out = append(out, brain.Candidate{Kind: "goal", Subject: string(id) + ":" + string(ev.ID), Summary: "evidence for a goal: " + truncate(ev.Summary, 100),
|
||||
Content: map[string]any{"goal_id": string(id), "reason": "new observation relevant to the goal", "source_event": string(ev.ID), "relevance": rel, "ref": string(id)},
|
||||
Features: attention.Features{Salience: ev.Salience * 0.7, GoalRelevance: rel, Consequence: 0.4, Confidence: 1}, Confidence: 1, SourceEvents: []ids.ID{ev.ID},
|
||||
Evidence: []events.EvidenceRef{{Kind: "goal", ID: string(id)}, {Kind: "event", ID: string(ev.ID)}}})
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// goal handles admitted goal thoughts: holds the goal in working memory and
|
||||
// records that it was attended (progress nudges are logged, not invented).
|
||||
func (h Handlers) goal(ctx context.Context, c *brain.Cycle, t workspace.Thought) error {
|
||||
gid, _ := t.Content["goal_id"].(string)
|
||||
reason, _ := t.Content["reason"].(string)
|
||||
src, _ := t.Content["source_event"].(string)
|
||||
c.Runtime.Hold(workspace.Item{Key: "goal:" + gid, Content: t.Summary, Kind: "goal", Ref: gid, SourceEvent: ids.ID(src)})
|
||||
c.Emit(events.New{Type: events.GoalUpdated, Source: "brain.goals", Summary: t.Summary + " (" + reason + ")", Salience: t.Score, Confidence: 1, CausationID: t.EventID,
|
||||
Evidence: t.Evidence, Metadata: map[string]any{"goal_id": gid, "attended": true}, Payload: map[string]any{"goal_id": gid, "reason": reason}})
|
||||
return nil
|
||||
}
|
||||
|
||||
// question handles admitted curiosity questions: an investigation goal is
|
||||
// created (once per subject) so the question persists beyond the workspace.
|
||||
func (h Handlers) question(ctx context.Context, c *brain.Cycle, t workspace.Thought) error {
|
||||
if c.State.Services.Goals == nil {
|
||||
return nil
|
||||
}
|
||||
subject, _ := t.Content["subject"].(string)
|
||||
question, _ := t.Content["question"].(string)
|
||||
src, _ := t.Content["source_event"].(string)
|
||||
if existing, err := c.State.Services.Goals.FindBySubject(ctx, c.Tx, c.State.Mind.ID, goals.KindInvestigation, subject); err == nil {
|
||||
c.Runtime.Hold(workspace.Item{Key: "goal:" + string(existing.ID), Content: existing.Title, Kind: "goal", Ref: string(existing.ID)})
|
||||
return nil
|
||||
}
|
||||
prio := 0.3 + 0.4*c.State.Motiv.Affect.Curiosity
|
||||
g, err := c.State.Services.Goals.Create(ctx, c.Tx, goals.New{MindID: c.State.Mind.ID, Title: question, Kind: goals.KindInvestigation, Priority: prio, Subject: subject, Owner: "mind", OriginEvent: t.EventID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Emit(events.New{Type: events.GoalCreated, Source: "brain.curiosity", Summary: "curious: " + question, Salience: 0.4 + 0.3*prio, Confidence: 0.6, CausationID: t.EventID, ParentIDs: []ids.ID{t.EventID, ids.ID(src)},
|
||||
Evidence: append(t.Evidence, events.EvidenceRef{Kind: "goal", ID: string(g.ID)}), Metadata: map[string]any{"goal_id": string(g.ID), "kind": g.Kind, "priority": prio}, Payload: g})
|
||||
c.Runtime.Hold(workspace.Item{Key: "goal:" + string(g.ID), Content: "investigating: " + question, Kind: "goal", Ref: string(g.ID), SourceEvent: ids.ID(src)})
|
||||
return nil
|
||||
}
|
||||
@@ -6,11 +6,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"github.com/andydixon/noema/internal/brain"
|
||||
"github.com/andydixon/noema/internal/conversation"
|
||||
"github.com/andydixon/noema/internal/memory"
|
||||
"github.com/andydixon/noema/internal/predictions"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/internal/workspace"
|
||||
"github.com/andydixon/noema/pkg/concepts"
|
||||
@@ -31,6 +33,8 @@ func (h Handlers) Register(r *brain.Registry) {
|
||||
r.Handle("retrieve", h.retrieve)
|
||||
r.Handle("hypothesis", h.hypothesis)
|
||||
r.Handle("perceive", h.perceive)
|
||||
r.Handle("goal", h.goal)
|
||||
r.Handle("question", h.question)
|
||||
}
|
||||
|
||||
// emitter adapts the cycle to the beliefs store's Emitter.
|
||||
@@ -77,9 +81,41 @@ func (h Handlers) hypothesis(ctx context.Context, c *brain.Cycle, t workspace.Th
|
||||
return err
|
||||
}
|
||||
c.Runtime.Hold(workspace.Item{Key: "belief:" + string(res.Belief.ID), Content: "hypothesis (" + fmt.Sprintf("%.2f", res.Belief.Confidence) + "): " + prop, Kind: "thought", Ref: string(res.Belief.ID), SourceEvent: t.EventID})
|
||||
// A causal hypothesis implies a checkable expectation: the effect recurs
|
||||
// after the cause. Its confidence is the belief's, so a miss will lower it.
|
||||
if c.State.Services.Predictions != nil {
|
||||
effect, _ := t.Content["effect_event"].(string)
|
||||
if ev, err := c.Runtime.Events().Get(ctx, ids.ID(effect)); err == nil {
|
||||
terms := distinctiveTerms(ev.Summary, 3)
|
||||
if len(terms) > 0 {
|
||||
p, err := c.State.Services.Predictions.Create(ctx, c.Tx, predictions.New{MindID: c.State.Mind.ID, Statement: "something like \"" + truncate(ev.Summary, 80) + "\" will recur within 24 hours if the cause is real",
|
||||
Subject: subjectKey(ev.Summary), Confidence: res.Belief.Confidence, WindowEnd: c.State.Now.Add(24 * time.Hour),
|
||||
Criteria: predictions.Criteria{Kind: "event_match", Expect: "occurs", Terms: terms, MinTerms: min(2, len(terms))}, Source: "brain.associate", OriginEvent: res.Events[0].ID, BeliefID: res.Belief.ID})
|
||||
if err == nil {
|
||||
c.Emit(events.New{Type: events.PredictionCreated, Source: "brain.associate", Summary: fmt.Sprintf("predicted (%.2f): %s", p.Confidence, p.Statement), Salience: 0.3, Confidence: p.Confidence, CausationID: res.Events[0].ID,
|
||||
Evidence: []events.EvidenceRef{{Kind: "prediction", ID: string(p.ID)}, {Kind: "belief", ID: string(res.Belief.ID)}}, Metadata: map[string]any{"prediction_id": string(p.ID), "belief_id": string(res.Belief.ID), "window_end": p.WindowEnd}, Payload: p})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// distinctiveTerms picks the longest tokens of a summary as match terms.
|
||||
func distinctiveTerms(text string, n int) []string {
|
||||
toks := tokeniseWS(text)
|
||||
var out []string
|
||||
for _, t := range toks {
|
||||
if len(t) >= 5 {
|
||||
out = append(out, t)
|
||||
}
|
||||
if len(out) == n {
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// attend encodes the attended event as a memory and holds it in working
|
||||
// memory. Only attended events become episodes: attention gates encoding.
|
||||
func (h Handlers) attend(ctx context.Context, c *brain.Cycle, t workspace.Thought) error {
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cognition
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/andydixon/noema/internal/workspace"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
)
|
||||
|
||||
func tokeniseWS(text string) []string { return workspace.Tokenise(text) }
|
||||
|
||||
// payloadField reads a string field from an event payload.
|
||||
func payloadField(ev events.Event, key string) (string, bool) {
|
||||
var m map[string]any
|
||||
if json.Unmarshal(ev.Payload, &m) != nil {
|
||||
return "", false
|
||||
}
|
||||
s, ok := m[key].(string)
|
||||
return s, ok
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
// Package goals stores a mind's goal tree: what it is trying to do, why, how
|
||||
// far it has got and what blocks it. Goals bias attention through the
|
||||
// goal-relevance feature and are created by operators, curiosity and
|
||||
// reflection; they never grant capabilities.
|
||||
package goals
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// Status of a goal.
|
||||
type Status string
|
||||
|
||||
// Statuses.
|
||||
const (
|
||||
StatusActive Status = "active"
|
||||
StatusBlocked Status = "blocked"
|
||||
StatusPaused Status = "paused"
|
||||
StatusCompleted Status = "completed"
|
||||
StatusAbandoned Status = "abandoned"
|
||||
)
|
||||
|
||||
// Kind of goal.
|
||||
type Kind string
|
||||
|
||||
// Kinds. Investigation and question goals come from curiosity; task goals
|
||||
// from operators; maintenance from reflection.
|
||||
const (
|
||||
KindTask Kind = "task"
|
||||
KindInvestigation Kind = "investigation"
|
||||
KindQuestion Kind = "question"
|
||||
KindMaintenance Kind = "maintenance"
|
||||
)
|
||||
|
||||
// Goal is one node of the tree.
|
||||
type Goal struct {
|
||||
ID ids.ID `json:"id"`
|
||||
MindID ids.ID `json:"mind_id"`
|
||||
ParentID ids.ID `json:"parent_id,omitempty"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Kind Kind `json:"kind"`
|
||||
Status Status `json:"status"`
|
||||
Priority float64 `json:"priority"`
|
||||
Progress float64 `json:"progress"`
|
||||
Owner string `json:"owner"`
|
||||
Blockers []string `json:"blockers"`
|
||||
OriginEvent ids.ID `json:"origin_event,omitempty"`
|
||||
Subject string `json:"subject"`
|
||||
DueAt *time.Time `json:"due_at,omitempty"`
|
||||
LastProgressAt time.Time `json:"last_progress_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
}
|
||||
|
||||
// ErrInvalid marks bad input.
|
||||
var ErrInvalid = errors.New("goals: invalid")
|
||||
|
||||
// Store persists goals.
|
||||
type Store struct{ db *pgxpool.Pool }
|
||||
|
||||
// NewStore creates a store.
|
||||
func NewStore(db *pgxpool.Pool) *Store { return &Store{db: db} }
|
||||
|
||||
// New describes a goal to create.
|
||||
type New struct {
|
||||
MindID ids.ID
|
||||
ParentID ids.ID
|
||||
Title string
|
||||
Description string
|
||||
Kind Kind
|
||||
Priority float64
|
||||
Owner string
|
||||
Subject string
|
||||
OriginEvent ids.ID
|
||||
DueAt *time.Time
|
||||
}
|
||||
|
||||
// Create inserts a goal using q.
|
||||
func (s *Store) Create(ctx context.Context, q storage.Querier, n New) (Goal, error) {
|
||||
n.Title = strings.TrimSpace(n.Title)
|
||||
if n.MindID.IsZero() || n.Title == "" || len(n.Title) > 300 {
|
||||
return Goal{}, fmt.Errorf("%w: title required (max 300 chars)", ErrInvalid)
|
||||
}
|
||||
switch n.Kind {
|
||||
case KindTask, KindInvestigation, KindQuestion, KindMaintenance:
|
||||
default:
|
||||
n.Kind = KindTask
|
||||
}
|
||||
if n.Owner == "" {
|
||||
n.Owner = "mind"
|
||||
}
|
||||
if n.Priority <= 0 || n.Priority > 1 {
|
||||
n.Priority = 0.5
|
||||
}
|
||||
g := Goal{ID: ids.New(), MindID: n.MindID, ParentID: n.ParentID, Title: n.Title, Description: n.Description, Kind: n.Kind, Status: StatusActive, Priority: n.Priority, Owner: n.Owner,
|
||||
Blockers: []string{}, OriginEvent: n.OriginEvent, Subject: n.Subject, DueAt: n.DueAt, LastProgressAt: time.Now(), CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||
_, err := q.Exec(ctx, `INSERT INTO goals (id, mind_id, parent_id, title, description, kind, status, priority, owner, origin_event, subject, due_at)
|
||||
SELECT $1,$2,$3,$4,$5,$6,'active',$7,$8,$9,$10,$11 WHERE $3::uuid IS NULL OR EXISTS (SELECT 1 FROM goals WHERE id=$3 AND mind_id=$2)`,
|
||||
g.ID, g.MindID, nullID(g.ParentID), g.Title, g.Description, string(g.Kind), g.Priority, g.Owner, nullID(g.OriginEvent), g.Subject, g.DueAt)
|
||||
if err != nil {
|
||||
return Goal{}, err
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func nullID(id ids.ID) *string {
|
||||
if id.IsZero() {
|
||||
return nil
|
||||
}
|
||||
s := string(id)
|
||||
return &s
|
||||
}
|
||||
|
||||
const cols = `id, mind_id, parent_id, title, description, kind, status, priority, progress, owner, blockers, origin_event, subject, due_at, last_progress_at, created_at, updated_at, completed_at`
|
||||
|
||||
func scan(row pgx.Row) (Goal, error) {
|
||||
var g Goal
|
||||
var parent, origin *string
|
||||
var kind, status string
|
||||
var blockers []byte
|
||||
if err := row.Scan(&g.ID, &g.MindID, &parent, &g.Title, &g.Description, &kind, &status, &g.Priority, &g.Progress, &g.Owner, &blockers, &origin, &g.Subject, &g.DueAt, &g.LastProgressAt, &g.CreatedAt, &g.UpdatedAt, &g.CompletedAt); err != nil {
|
||||
return g, storage.Translate(err)
|
||||
}
|
||||
g.Kind, g.Status = Kind(kind), Status(status)
|
||||
if parent != nil {
|
||||
g.ParentID = ids.ID(*parent)
|
||||
}
|
||||
if origin != nil {
|
||||
g.OriginEvent = ids.ID(*origin)
|
||||
}
|
||||
json.Unmarshal(blockers, &g.Blockers) //nolint:errcheck
|
||||
if g.Blockers == nil {
|
||||
g.Blockers = []string{}
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// Get fetches a goal scoped to a mind.
|
||||
func (s *Store) Get(ctx context.Context, mind, id ids.ID) (Goal, error) {
|
||||
return scan(s.db.QueryRow(ctx, `SELECT `+cols+` FROM goals WHERE id=$1 AND mind_id=$2`, id, mind))
|
||||
}
|
||||
|
||||
// List returns goals, optionally filtered by status, priority descending.
|
||||
func (s *Store) List(ctx context.Context, mind ids.ID, statuses []Status, limit int) ([]Goal, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
ss := make([]string, len(statuses))
|
||||
for i, st := range statuses {
|
||||
ss[i] = string(st)
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT `+cols+` FROM goals WHERE mind_id=$1 AND (cardinality($2::text[])=0 OR status = ANY($2)) ORDER BY priority DESC, created_at LIMIT $3`, mind, ss, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Goal
|
||||
for rows.Next() {
|
||||
g, err := scan(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, g)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Active returns live goals (active or blocked) for attention scoring.
|
||||
func (s *Store) Active(ctx context.Context, mind ids.ID) ([]Goal, error) {
|
||||
return s.List(ctx, mind, []Status{StatusActive, StatusBlocked}, 100)
|
||||
}
|
||||
|
||||
// Update changes mutable fields. Nil pointers leave a field untouched.
|
||||
type Update struct {
|
||||
Status *Status
|
||||
Progress *float64
|
||||
Priority *float64
|
||||
Blockers *[]string
|
||||
Title *string
|
||||
Desc *string
|
||||
}
|
||||
|
||||
// Apply updates a goal and returns the new row; status transitions set timestamps.
|
||||
func (s *Store) Apply(ctx context.Context, q storage.Querier, mind, id ids.ID, u Update) (Goal, error) {
|
||||
g, err := scan(q.QueryRow(ctx, `SELECT `+cols+` FROM goals WHERE id=$1 AND mind_id=$2`, id, mind))
|
||||
if err != nil {
|
||||
return Goal{}, err
|
||||
}
|
||||
if u.Status != nil {
|
||||
switch *u.Status {
|
||||
case StatusActive, StatusBlocked, StatusPaused, StatusCompleted, StatusAbandoned:
|
||||
g.Status = *u.Status
|
||||
default:
|
||||
return Goal{}, fmt.Errorf("%w: unknown status", ErrInvalid)
|
||||
}
|
||||
if g.Status == StatusCompleted {
|
||||
now := time.Now()
|
||||
g.CompletedAt, g.Progress = &now, 1
|
||||
}
|
||||
}
|
||||
if u.Progress != nil {
|
||||
g.Progress = clamp(*u.Progress)
|
||||
g.LastProgressAt = time.Now()
|
||||
}
|
||||
if u.Priority != nil {
|
||||
g.Priority = clamp(*u.Priority)
|
||||
}
|
||||
if u.Blockers != nil {
|
||||
g.Blockers = *u.Blockers
|
||||
if len(g.Blockers) > 0 && g.Status == StatusActive {
|
||||
g.Status = StatusBlocked
|
||||
} else if len(g.Blockers) == 0 && g.Status == StatusBlocked {
|
||||
g.Status = StatusActive
|
||||
}
|
||||
}
|
||||
if u.Title != nil && strings.TrimSpace(*u.Title) != "" {
|
||||
g.Title = strings.TrimSpace(*u.Title)
|
||||
}
|
||||
if u.Desc != nil {
|
||||
g.Description = *u.Desc
|
||||
}
|
||||
blockers, _ := json.Marshal(g.Blockers)
|
||||
_, err = q.Exec(ctx, `UPDATE goals SET status=$3, progress=$4, priority=$5, blockers=$6, title=$7, description=$8, last_progress_at=$9, completed_at=$10, updated_at=now() WHERE id=$1 AND mind_id=$2`,
|
||||
id, mind, string(g.Status), g.Progress, g.Priority, blockers, g.Title, g.Description, g.LastProgressAt, g.CompletedAt)
|
||||
return g, err
|
||||
}
|
||||
|
||||
func clamp(v float64) float64 {
|
||||
if v < 0 {
|
||||
return 0
|
||||
}
|
||||
if v > 1 {
|
||||
return 1
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Stalled returns active goals without progress for longer than d.
|
||||
func (s *Store) Stalled(ctx context.Context, mind ids.ID, d time.Duration) ([]Goal, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT `+cols+` FROM goals WHERE mind_id=$1 AND status='active' AND last_progress_at < $2 ORDER BY priority DESC LIMIT 20`, mind, time.Now().Add(-d))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Goal
|
||||
for rows.Next() {
|
||||
g, err := scan(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, g)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// FindBySubject returns a live goal about a subject, for de-duplication.
|
||||
func (s *Store) FindBySubject(ctx context.Context, q storage.Querier, mind ids.ID, kind Kind, subject string) (Goal, error) {
|
||||
return scan(q.QueryRow(ctx, `SELECT `+cols+` FROM goals WHERE mind_id=$1 AND kind=$2 AND subject=$3 AND status IN ('active','blocked','paused') ORDER BY created_at DESC LIMIT 1`, mind, string(kind), subject))
|
||||
}
|
||||
|
||||
// Stats counts goals by status.
|
||||
type Stats struct {
|
||||
ByStatus map[string]int64 `json:"by_status"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// Stats returns counts.
|
||||
func (s *Store) Stats(ctx context.Context, mind ids.ID) (Stats, error) {
|
||||
st := Stats{ByStatus: map[string]int64{}}
|
||||
rows, err := s.db.Query(ctx, `SELECT status, count(*) FROM goals WHERE mind_id=$1 GROUP BY status`, mind)
|
||||
if err != nil {
|
||||
return st, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var k string
|
||||
var n int64
|
||||
if err := rows.Scan(&k, &n); err != nil {
|
||||
return st, err
|
||||
}
|
||||
st.ByStatus[k] = n
|
||||
st.Total += n
|
||||
}
|
||||
return st, rows.Err()
|
||||
}
|
||||
|
||||
// Relevance scores how much text relates to the active goals: the best token
|
||||
// overlap with any goal, weighted by that goal's priority. Deterministic and
|
||||
// cheap; it feeds the goal_relevance attention feature.
|
||||
func Relevance(text string, active []Goal) (float64, ids.ID) {
|
||||
toks := tokenSet(text)
|
||||
if len(toks) == 0 {
|
||||
return 0, ids.Nil
|
||||
}
|
||||
var best float64
|
||||
var bestID ids.ID
|
||||
for _, g := range active {
|
||||
gt := tokenSet(g.Title + " " + g.Subject + " " + g.Description)
|
||||
if len(gt) == 0 {
|
||||
continue
|
||||
}
|
||||
inter := 0
|
||||
for w := range toks {
|
||||
if gt[w] {
|
||||
inter++
|
||||
}
|
||||
}
|
||||
if inter == 0 {
|
||||
continue
|
||||
}
|
||||
score := float64(inter) / float64(min(len(toks), len(gt))) * (0.5 + 0.5*g.Priority)
|
||||
if score > best {
|
||||
best, bestID = score, g.ID
|
||||
}
|
||||
}
|
||||
if best > 1 {
|
||||
best = 1
|
||||
}
|
||||
return best, bestID
|
||||
}
|
||||
|
||||
var stop = map[string]bool{"the": true, "a": true, "an": true, "is": true, "are": true, "of": true, "to": true, "in": true, "on": true, "at": true, "for": true, "and": true, "or": true, "that": true, "this": true, "it": true, "with": true, "why": true, "what": true, "how": true, "did": true, "does": true, "do": true}
|
||||
|
||||
func tokenSet(s string) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
var cur strings.Builder
|
||||
flush := func() {
|
||||
w := strings.Trim(cur.String(), ".-_")
|
||||
cur.Reset()
|
||||
if len(w) >= 3 && !stop[w] {
|
||||
out[w] = true
|
||||
}
|
||||
}
|
||||
for _, r := range strings.ToLower(s) {
|
||||
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '.' || r == '_' {
|
||||
cur.WriteRune(r)
|
||||
} else {
|
||||
flush()
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package goals
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/migrations"
|
||||
)
|
||||
|
||||
func TestGoalsLifecycleAndRelevance(t *testing.T) {
|
||||
pool := storage.TestPool(t, migrations.FS)
|
||||
ctx := context.Background()
|
||||
ms := minds.NewStore(pool)
|
||||
a, _ := ms.Create(ctx, minds.CreateParams{Slug: "a"})
|
||||
b, _ := ms.Create(ctx, minds.CreateParams{Slug: "b"})
|
||||
s := NewStore(pool)
|
||||
root, err := s.Create(ctx, pool, New{MindID: a.ID, Title: "Find the cause of atlas latency", Kind: KindInvestigation, Priority: 0.9, Subject: "atlas latency", Owner: "operator"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
child, err := s.Create(ctx, pool, New{MindID: a.ID, ParentID: root.ID, Title: "Check disk latency on atlas", Priority: 0.7})
|
||||
if err != nil || child.ParentID != root.ID {
|
||||
t.Fatalf("child: %v %+v", err, child)
|
||||
}
|
||||
if _, err := s.Create(ctx, pool, New{MindID: b.ID, ParentID: root.ID, Title: "cross-mind child"}); err == nil {
|
||||
if got, _ := s.List(ctx, b.ID, nil, 10); len(got) != 0 {
|
||||
t.Fatal("cross-mind parent accepted")
|
||||
}
|
||||
}
|
||||
rel, id := Relevance("query latency on atlas rose sharply", []Goal{root, child})
|
||||
if rel < 0.3 || id != root.ID {
|
||||
t.Fatalf("relevance wrong: %v %v", rel, id)
|
||||
}
|
||||
if rel0, _ := Relevance("weather is mild", []Goal{root}); rel0 != 0 {
|
||||
t.Fatalf("unrelated text should score 0, got %v", rel0)
|
||||
}
|
||||
p := 0.5
|
||||
g, err := s.Apply(ctx, pool, a.ID, child.ID, Update{Progress: &p, Blockers: &[]string{"no metrics access"}})
|
||||
if err != nil || g.Status != StatusBlocked || g.Progress != 0.5 {
|
||||
t.Fatalf("apply: %v %+v", err, g)
|
||||
}
|
||||
done := StatusCompleted
|
||||
g, _ = s.Apply(ctx, pool, a.ID, child.ID, Update{Status: &done})
|
||||
if g.Progress != 1 || g.CompletedAt == nil {
|
||||
t.Fatalf("completion wrong: %+v", g)
|
||||
}
|
||||
pool.Exec(ctx, `UPDATE goals SET last_progress_at = now() - interval '2 days' WHERE id=$1`, root.ID) //nolint:errcheck
|
||||
stalled, _ := s.Stalled(ctx, a.ID, 24*time.Hour)
|
||||
if len(stalled) != 1 || stalled[0].ID != root.ID {
|
||||
t.Fatalf("stalled wrong: %+v", stalled)
|
||||
}
|
||||
if _, err := s.FindBySubject(ctx, pool, a.ID, KindInvestigation, "atlas latency"); err != nil {
|
||||
t.Fatal("subject lookup failed")
|
||||
}
|
||||
st, _ := s.Stats(ctx, a.ID)
|
||||
if st.Total != 2 || st.ByStatus["completed"] != 1 {
|
||||
t.Fatalf("stats: %+v", st)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
// Package motivation holds a mind's drives and affect-like state: named
|
||||
// scalars updated by deterministic rules from events, decaying towards
|
||||
// personality-derived baselines. They bias attention and style and are fully
|
||||
// inspectable and resettable. Nothing here claims the software feels
|
||||
// anything; see docs/interpretation.md.
|
||||
package motivation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// DriveNames is the closed set of drives. There is deliberately no
|
||||
// self-preservation, replication or acquisition drive and no way to add one
|
||||
// at runtime (spec §13, threat T25).
|
||||
var DriveNames = []string{"curiosity", "uncertainty_reduction", "task_completion", "consistency", "novelty", "social_coherence", "exploration", "rest"}
|
||||
|
||||
// Drive is one motivational scalar with operator-configured range.
|
||||
type Drive struct {
|
||||
Name string `json:"name"`
|
||||
Value float64 `json:"value"`
|
||||
Min float64 `json:"min"`
|
||||
Max float64 `json:"max"`
|
||||
Baseline float64 `json:"baseline"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Affect is the emotion-like state vector (spec §14). Valence is in [-1,1];
|
||||
// everything else in [0,1].
|
||||
type Affect struct {
|
||||
Valence float64 `json:"valence"`
|
||||
Arousal float64 `json:"arousal"`
|
||||
Curiosity float64 `json:"curiosity"`
|
||||
Concern float64 `json:"concern"`
|
||||
Frustration float64 `json:"frustration"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Uncertainty float64 `json:"uncertainty"`
|
||||
Engagement float64 `json:"engagement"`
|
||||
TaskPressure float64 `json:"task_pressure"`
|
||||
Load float64 `json:"load"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Baselines derives the resting affect from personality traits.
|
||||
func Baselines(p minds.Personality) Affect {
|
||||
t := p.Traits
|
||||
return Affect{Valence: 0.1, Arousal: 0.2, Curiosity: t["curiosity"], Concern: 0.1 + 0.2*(1-t["risk_tolerance"]), Frustration: 0.05,
|
||||
Confidence: t["confidence"], Uncertainty: 0.3 + 0.3*t["scepticism"], Engagement: 0.3 + 0.3*t["sociability"], TaskPressure: 0.1, Load: 0.1}
|
||||
}
|
||||
|
||||
// DefaultDrives derives drive baselines from personality.
|
||||
func DefaultDrives(p minds.Personality) []Drive {
|
||||
t, v := p.Traits, p.Values
|
||||
base := map[string]float64{"curiosity": t["curiosity"], "uncertainty_reduction": 0.3 + 0.5*v["accuracy"], "task_completion": 0.3 + 0.5*v["persistence"], "consistency": 0.3 + 0.5*v["evidence"],
|
||||
"novelty": t["novelty_preference"], "social_coherence": 0.2 + 0.6*t["sociability"], "exploration": 0.2 + 0.6*t["curiosity"]*t["risk_tolerance"], "rest": 0.3}
|
||||
out := make([]Drive, 0, len(DriveNames))
|
||||
for _, n := range DriveNames {
|
||||
out = append(out, Drive{Name: n, Value: clamp01(base[n]), Min: 0, Max: 1, Baseline: clamp01(base[n]), UpdatedAt: time.Now()})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// State bundles drives and affect for a mind.
|
||||
type State struct {
|
||||
Drives []Drive `json:"drives"`
|
||||
Affect Affect `json:"affect"`
|
||||
}
|
||||
|
||||
// Store persists motivation state.
|
||||
type Store struct{ db *pgxpool.Pool }
|
||||
|
||||
// NewStore creates a store.
|
||||
func NewStore(db *pgxpool.Pool) *Store { return &Store{db: db} }
|
||||
|
||||
// Load returns the state, initialising it from personality on first use.
|
||||
func (s *Store) Load(ctx context.Context, q storage.Querier, m minds.Mind) (State, error) {
|
||||
var st State
|
||||
rows, err := q.Query(ctx, `SELECT name, value, min, max, baseline, updated_at FROM drives WHERE mind_id=$1`, m.ID)
|
||||
if err != nil {
|
||||
return st, err
|
||||
}
|
||||
byName := map[string]Drive{}
|
||||
for rows.Next() {
|
||||
var d Drive
|
||||
if err := rows.Scan(&d.Name, &d.Value, &d.Min, &d.Max, &d.Baseline, &d.UpdatedAt); err != nil {
|
||||
rows.Close()
|
||||
return st, err
|
||||
}
|
||||
byName[d.Name] = d
|
||||
}
|
||||
rows.Close()
|
||||
defaults := DefaultDrives(m.Personality)
|
||||
for _, d := range defaults {
|
||||
if have, ok := byName[d.Name]; ok {
|
||||
st.Drives = append(st.Drives, have)
|
||||
continue
|
||||
}
|
||||
if _, err := q.Exec(ctx, `INSERT INTO drives (mind_id, name, value, min, max, baseline) VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING`, m.ID, d.Name, d.Value, d.Min, d.Max, d.Baseline); err != nil {
|
||||
return st, err
|
||||
}
|
||||
st.Drives = append(st.Drives, d)
|
||||
}
|
||||
var raw []byte
|
||||
err = q.QueryRow(ctx, `SELECT state FROM affect_states WHERE mind_id=$1`, m.ID).Scan(&raw)
|
||||
switch {
|
||||
case errors.Is(err, pgx.ErrNoRows):
|
||||
st.Affect = Baselines(m.Personality)
|
||||
st.Affect.UpdatedAt = time.Now()
|
||||
if err := s.SaveAffect(ctx, q, m.ID, st.Affect); err != nil {
|
||||
return st, err
|
||||
}
|
||||
case err != nil:
|
||||
return st, err
|
||||
default:
|
||||
json.Unmarshal(raw, &st.Affect) //nolint:errcheck
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// SaveAffect stores the affect vector.
|
||||
func (s *Store) SaveAffect(ctx context.Context, q storage.Querier, mind ids.ID, a Affect) error {
|
||||
raw, _ := json.Marshal(a)
|
||||
_, err := q.Exec(ctx, `INSERT INTO affect_states (mind_id, state) VALUES ($1,$2) ON CONFLICT (mind_id) DO UPDATE SET state=excluded.state, updated_at=now()`, mind, raw)
|
||||
return err
|
||||
}
|
||||
|
||||
// SaveDrive stores one drive value (and optionally its range/baseline).
|
||||
func (s *Store) SaveDrive(ctx context.Context, q storage.Querier, mind ids.ID, d Drive) error {
|
||||
_, err := q.Exec(ctx, `INSERT INTO drives (mind_id, name, value, min, max, baseline, updated_at) VALUES ($1,$2,$3,$4,$5,$6,now())
|
||||
ON CONFLICT (mind_id, name) DO UPDATE SET value=excluded.value, min=excluded.min, max=excluded.max, baseline=excluded.baseline, updated_at=now()`, mind, d.Name, clampRange(d.Value, d.Min, d.Max), d.Min, d.Max, d.Baseline)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reset returns affect to baseline and drives to their baselines.
|
||||
func (s *Store) Reset(ctx context.Context, q storage.Querier, m minds.Mind) (State, error) {
|
||||
if _, err := q.Exec(ctx, `UPDATE drives SET value=baseline, updated_at=now() WHERE mind_id=$1`, m.ID); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
a := Baselines(m.Personality)
|
||||
a.UpdatedAt = time.Now()
|
||||
if err := s.SaveAffect(ctx, q, m.ID, a); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
return s.Load(ctx, q, m)
|
||||
}
|
||||
|
||||
// Delta is a change to apply to the state, produced by rules.
|
||||
type Delta struct {
|
||||
Affect map[string]float64 // field → additive change
|
||||
Drives map[string]float64 // drive → additive change
|
||||
Reason string
|
||||
}
|
||||
|
||||
// Rules maps an event to state changes. The table is the whole model: small,
|
||||
// explicit, testable.
|
||||
func Rules(ev events.Event) (Delta, bool) {
|
||||
d := Delta{Affect: map[string]float64{}, Drives: map[string]float64{}}
|
||||
switch ev.Type {
|
||||
case events.ContradictionDetected:
|
||||
d.Affect["concern"], d.Affect["uncertainty"], d.Affect["arousal"] = 0.15, 0.15, 0.1
|
||||
d.Drives["uncertainty_reduction"], d.Drives["consistency"] = 0.1, 0.1
|
||||
d.Reason = "contradiction"
|
||||
case events.PredictionError:
|
||||
d.Affect["arousal"], d.Affect["confidence"], d.Affect["uncertainty"], d.Affect["curiosity"] = 0.2, -0.15, 0.15, 0.1
|
||||
d.Drives["uncertainty_reduction"], d.Drives["curiosity"] = 0.15, 0.1
|
||||
d.Reason = "prediction error"
|
||||
case events.PredictionResolved:
|
||||
if ev.Metadata["outcome"] == "confirmed" {
|
||||
d.Affect["confidence"], d.Affect["valence"] = 0.1, 0.1
|
||||
d.Reason = "prediction confirmed"
|
||||
} else {
|
||||
return d, false
|
||||
}
|
||||
case events.GoalCompleted:
|
||||
d.Affect["valence"], d.Affect["task_pressure"], d.Affect["frustration"] = 0.2, -0.2, -0.1
|
||||
d.Drives["task_completion"], d.Drives["rest"] = -0.1, 0.1
|
||||
d.Reason = "goal completed"
|
||||
case events.GoalCreated:
|
||||
d.Affect["task_pressure"], d.Affect["engagement"] = 0.1, 0.1
|
||||
d.Drives["task_completion"] = 0.05
|
||||
d.Reason = "new goal"
|
||||
case events.GoalAbandoned:
|
||||
d.Affect["frustration"], d.Affect["valence"] = 0.1, -0.1
|
||||
d.Reason = "goal abandoned"
|
||||
case events.BeliefRetracted:
|
||||
d.Affect["uncertainty"], d.Affect["confidence"], d.Affect["frustration"] = 0.1, -0.1, 0.05
|
||||
d.Drives["consistency"] = 0.1
|
||||
d.Reason = "belief corrected by operator"
|
||||
case events.Observation:
|
||||
d.Affect["engagement"], d.Affect["arousal"] = 0.05, 0.05*ev.Salience
|
||||
if ev.Valence != nil {
|
||||
d.Affect["valence"] = 0.2 * *ev.Valence
|
||||
}
|
||||
d.Drives["novelty"] = 0.02
|
||||
d.Reason = "observation"
|
||||
case events.HypothesisCreated:
|
||||
d.Affect["curiosity"] = 0.1
|
||||
d.Drives["curiosity"], d.Drives["exploration"] = 0.05, 0.05
|
||||
d.Reason = "hypothesis"
|
||||
case events.Metacognition:
|
||||
d.Affect["uncertainty"], d.Affect["load"] = 0.05, 0.05
|
||||
d.Reason = "metacognitive warning"
|
||||
case events.CycleCompleted:
|
||||
if n, ok := ev.Metadata["candidates"].(int); ok && n > 8 {
|
||||
d.Affect["load"] = 0.1
|
||||
d.Reason = "busy cycle"
|
||||
} else {
|
||||
return d, false
|
||||
}
|
||||
default:
|
||||
return d, false
|
||||
}
|
||||
return d, true
|
||||
}
|
||||
|
||||
// Apply adds a delta to the state and clamps.
|
||||
func Apply(st *State, d Delta) {
|
||||
a := &st.Affect
|
||||
for k, v := range d.Affect {
|
||||
switch k {
|
||||
case "valence":
|
||||
a.Valence = clamp(a.Valence+v, -1, 1)
|
||||
case "arousal":
|
||||
a.Arousal = clamp01(a.Arousal + v)
|
||||
case "curiosity":
|
||||
a.Curiosity = clamp01(a.Curiosity + v)
|
||||
case "concern":
|
||||
a.Concern = clamp01(a.Concern + v)
|
||||
case "frustration":
|
||||
a.Frustration = clamp01(a.Frustration + v)
|
||||
case "confidence":
|
||||
a.Confidence = clamp01(a.Confidence + v)
|
||||
case "uncertainty":
|
||||
a.Uncertainty = clamp01(a.Uncertainty + v)
|
||||
case "engagement":
|
||||
a.Engagement = clamp01(a.Engagement + v)
|
||||
case "task_pressure":
|
||||
a.TaskPressure = clamp01(a.TaskPressure + v)
|
||||
case "load":
|
||||
a.Load = clamp01(a.Load + v)
|
||||
}
|
||||
}
|
||||
for i := range st.Drives {
|
||||
if v, ok := d.Drives[st.Drives[i].Name]; ok {
|
||||
st.Drives[i].Value = clampRange(st.Drives[i].Value+v, st.Drives[i].Min, st.Drives[i].Max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Decay moves state towards baselines by a fraction that depends on elapsed
|
||||
// time (half-life one hour for affect, six hours for drives).
|
||||
func Decay(st *State, base Affect, elapsed time.Duration) {
|
||||
if elapsed <= 0 {
|
||||
return
|
||||
}
|
||||
fa := 1 - math.Pow(0.5, elapsed.Hours())
|
||||
fd := 1 - math.Pow(0.5, elapsed.Hours()/6)
|
||||
a := &st.Affect
|
||||
a.Valence += (base.Valence - a.Valence) * fa
|
||||
a.Arousal += (base.Arousal - a.Arousal) * fa
|
||||
a.Curiosity += (base.Curiosity - a.Curiosity) * fa
|
||||
a.Concern += (base.Concern - a.Concern) * fa
|
||||
a.Frustration += (base.Frustration - a.Frustration) * fa
|
||||
a.Confidence += (base.Confidence - a.Confidence) * fa
|
||||
a.Uncertainty += (base.Uncertainty - a.Uncertainty) * fa
|
||||
a.Engagement += (base.Engagement - a.Engagement) * fa
|
||||
a.TaskPressure += (base.TaskPressure - a.TaskPressure) * fa
|
||||
a.Load += (base.Load - a.Load) * fa
|
||||
for i := range st.Drives {
|
||||
d := &st.Drives[i]
|
||||
d.Value += (d.Baseline - d.Value) * fd
|
||||
}
|
||||
}
|
||||
|
||||
// Modulate scales attention weights by drives and affect. The mapping is
|
||||
// explicit so the workspace page can show effective weights next to the
|
||||
// configured ones.
|
||||
func Modulate(weights map[string]float64, st State) map[string]float64 {
|
||||
out := make(map[string]float64, len(weights))
|
||||
for k, v := range weights {
|
||||
out[k] = v
|
||||
}
|
||||
drive := map[string]float64{}
|
||||
for _, d := range st.Drives {
|
||||
drive[d.Name] = d.Value
|
||||
}
|
||||
scale := func(key string, factor float64) {
|
||||
if v, ok := out[key]; ok {
|
||||
out[key] = round3(v * factor)
|
||||
}
|
||||
}
|
||||
scale("novelty", 0.6+0.8*drive["curiosity"]) // 0.6× .. 1.4×
|
||||
scale("goal_relevance", 0.6+0.8*drive["task_completion"])
|
||||
scale("contradiction", 0.6+0.8*drive["uncertainty_reduction"])
|
||||
scale("affect", 0.6+0.8*st.Affect.Arousal)
|
||||
scale("urgency", 0.8+0.4*st.Affect.Concern)
|
||||
scale("context_relevance", 0.7+0.6*(1-drive["exploration"]))
|
||||
return out
|
||||
}
|
||||
|
||||
// Significant reports whether two affect vectors differ enough to emit an event.
|
||||
func Significant(a, b Affect) (string, bool) {
|
||||
fields := map[string][2]float64{"valence": {a.Valence, b.Valence}, "arousal": {a.Arousal, b.Arousal}, "curiosity": {a.Curiosity, b.Curiosity}, "concern": {a.Concern, b.Concern},
|
||||
"frustration": {a.Frustration, b.Frustration}, "confidence": {a.Confidence, b.Confidence}, "uncertainty": {a.Uncertainty, b.Uncertainty}, "engagement": {a.Engagement, b.Engagement},
|
||||
"task_pressure": {a.TaskPressure, b.TaskPressure}, "load": {a.Load, b.Load}}
|
||||
best, bestKey := 0.0, ""
|
||||
for k, v := range fields {
|
||||
if d := math.Abs(v[0] - v[1]); d > best {
|
||||
best, bestKey = d, k
|
||||
}
|
||||
}
|
||||
return bestKey, best >= 0.1
|
||||
}
|
||||
|
||||
func clamp01(v float64) float64 { return clamp(v, 0, 1) }
|
||||
|
||||
func clamp(v, lo, hi float64) float64 {
|
||||
if math.IsNaN(v) || v < lo {
|
||||
return lo
|
||||
}
|
||||
if v > hi {
|
||||
return hi
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func clampRange(v, lo, hi float64) float64 {
|
||||
if hi < lo {
|
||||
hi = lo
|
||||
}
|
||||
return clamp(v, lo, hi)
|
||||
}
|
||||
|
||||
func round3(v float64) float64 { return math.Round(v*1000) / 1000 }
|
||||
@@ -0,0 +1,83 @@
|
||||
package motivation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/migrations"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
)
|
||||
|
||||
func TestRulesApplyDecayAndModulate(t *testing.T) {
|
||||
p := minds.DefaultPersonality("x")
|
||||
st := State{Drives: DefaultDrives(p), Affect: Baselines(p)}
|
||||
base := st.Affect
|
||||
d, ok := Rules(events.Event{Type: events.ContradictionDetected})
|
||||
if !ok {
|
||||
t.Fatal("contradiction should have a rule")
|
||||
}
|
||||
Apply(&st, d)
|
||||
if st.Affect.Concern <= base.Concern || st.Affect.Uncertainty <= base.Uncertainty {
|
||||
t.Fatalf("contradiction did not raise concern/uncertainty: %+v", st.Affect)
|
||||
}
|
||||
for i := 0; i < 50; i++ {
|
||||
Apply(&st, d)
|
||||
}
|
||||
if st.Affect.Concern > 1 || st.Affect.Uncertainty > 1 {
|
||||
t.Fatal("clamp failed")
|
||||
}
|
||||
if _, ok := Rules(events.Event{Type: events.ThoughtCandidate}); ok {
|
||||
t.Fatal("candidates must not move affect")
|
||||
}
|
||||
before := st.Affect.Concern
|
||||
Decay(&st, base, 3*time.Hour)
|
||||
if st.Affect.Concern >= before || st.Affect.Concern < base.Concern-0.01 {
|
||||
t.Fatalf("decay wrong: %v -> %v (base %v)", before, st.Affect.Concern, base.Concern)
|
||||
}
|
||||
w := map[string]float64{"novelty": 1, "goal_relevance": 1, "contradiction": 1, "salience": 1}
|
||||
for i := range st.Drives {
|
||||
if st.Drives[i].Name == "curiosity" {
|
||||
st.Drives[i].Value = 1
|
||||
}
|
||||
}
|
||||
m := Modulate(w, st)
|
||||
if m["novelty"] <= 1 || m["salience"] != 1 {
|
||||
t.Fatalf("modulation wrong: %v", m)
|
||||
}
|
||||
if k, sig := Significant(base, st.Affect); sig && k == "" {
|
||||
t.Fatal("significant without key")
|
||||
}
|
||||
// No forbidden drives can exist.
|
||||
for _, n := range DriveNames {
|
||||
if n == "self_preservation" || n == "replication" || n == "acquisition" {
|
||||
t.Fatal("forbidden drive present")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreInitialisesResetsAndPersists(t *testing.T) {
|
||||
pool := storage.TestPool(t, migrations.FS)
|
||||
ctx := context.Background()
|
||||
m, _ := minds.NewStore(pool).Create(ctx, minds.CreateParams{Slug: "mot"})
|
||||
s := NewStore(pool)
|
||||
st, err := s.Load(ctx, pool, m)
|
||||
if err != nil || len(st.Drives) != len(DriveNames) {
|
||||
t.Fatalf("load: %v %+v", err, st)
|
||||
}
|
||||
st.Affect.Concern = 0.9
|
||||
s.SaveAffect(ctx, pool, m.ID, st.Affect) //nolint:errcheck
|
||||
d := st.Drives[0]
|
||||
d.Value, d.Max = 0.95, 0.8
|
||||
s.SaveDrive(ctx, pool, m.ID, d) //nolint:errcheck
|
||||
st2, _ := s.Load(ctx, pool, m)
|
||||
if st2.Affect.Concern != 0.9 || st2.Drives[0].Value != 0.8 {
|
||||
t.Fatalf("persist/clamp wrong: %+v %+v", st2.Affect, st2.Drives[0])
|
||||
}
|
||||
st3, err := s.Reset(ctx, pool, m)
|
||||
if err != nil || st3.Affect.Concern == 0.9 || st3.Drives[0].Value != st3.Drives[0].Baseline {
|
||||
t.Fatalf("reset wrong: %v %+v", err, st3)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,456 @@
|
||||
// Package predictions stores explicit expectations with resolution windows
|
||||
// and machine-checkable criteria, resolves them deterministically, records
|
||||
// prediction errors and keeps an append-only calibration history (spec §11).
|
||||
package predictions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// Status of a prediction.
|
||||
type Status string
|
||||
|
||||
// Statuses.
|
||||
const (
|
||||
StatusOpen Status = "open"
|
||||
StatusConfirmed Status = "confirmed"
|
||||
StatusRefuted Status = "refuted"
|
||||
StatusExpired Status = "expired"
|
||||
StatusWithdrawn Status = "withdrawn"
|
||||
)
|
||||
|
||||
// Criteria says how a prediction is judged.
|
||||
//
|
||||
// event_match: terms must (expect=occurs) or must not (expect=absent) appear in an
|
||||
// event summary of the given types within the window.
|
||||
// belief: the referenced belief's confidence reaches min_confidence by the window end.
|
||||
// operator: only an operator can resolve it.
|
||||
type Criteria struct {
|
||||
Kind string `json:"kind"`
|
||||
Expect string `json:"expect,omitempty"` // occurs | absent
|
||||
Terms []string `json:"terms,omitempty"`
|
||||
MinTerms int `json:"min_terms,omitempty"` // how many terms must match (default: all, max 3)
|
||||
Types []string `json:"types,omitempty"`
|
||||
BeliefID ids.ID `json:"belief_id,omitempty"`
|
||||
MinConfidence float64 `json:"min_confidence,omitempty"`
|
||||
}
|
||||
|
||||
// Prediction is one expectation.
|
||||
type Prediction struct {
|
||||
ID ids.ID `json:"id"`
|
||||
MindID ids.ID `json:"mind_id"`
|
||||
Statement string `json:"statement"`
|
||||
Subject string `json:"subject"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
WindowStart time.Time `json:"window_start"`
|
||||
WindowEnd time.Time `json:"window_end"`
|
||||
Criteria Criteria `json:"criteria"`
|
||||
Status Status `json:"status"`
|
||||
Source string `json:"source"`
|
||||
OriginEvent ids.ID `json:"origin_event,omitempty"`
|
||||
BeliefID ids.ID `json:"belief_id,omitempty"`
|
||||
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
|
||||
Resolution json.RawMessage `json:"resolution,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Result is one append-only outcome row.
|
||||
type Result struct {
|
||||
ID ids.ID `json:"id"`
|
||||
PredictionID ids.ID `json:"prediction_id"`
|
||||
Outcome Status `json:"outcome"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Error float64 `json:"error"`
|
||||
Evidence json.RawMessage `json:"evidence"`
|
||||
EventID ids.ID `json:"event_id,omitempty"`
|
||||
ResolvedAt time.Time `json:"resolved_at"`
|
||||
}
|
||||
|
||||
// ErrInvalid marks bad input.
|
||||
var ErrInvalid = errors.New("predictions: invalid")
|
||||
|
||||
// Store persists predictions.
|
||||
type Store struct{ db *pgxpool.Pool }
|
||||
|
||||
// NewStore creates a store.
|
||||
func NewStore(db *pgxpool.Pool) *Store { return &Store{db: db} }
|
||||
|
||||
// New describes a prediction to create.
|
||||
type New struct {
|
||||
MindID ids.ID
|
||||
Statement string
|
||||
Subject string
|
||||
Confidence float64
|
||||
WindowStart time.Time
|
||||
WindowEnd time.Time
|
||||
Criteria Criteria
|
||||
Source string
|
||||
OriginEvent ids.ID
|
||||
BeliefID ids.ID
|
||||
}
|
||||
|
||||
func (n *New) validate() error {
|
||||
n.Statement = strings.TrimSpace(n.Statement)
|
||||
if n.MindID.IsZero() || n.Statement == "" || len(n.Statement) > 1000 {
|
||||
return fmt.Errorf("%w: statement required (max 1000 chars)", ErrInvalid)
|
||||
}
|
||||
if n.Confidence <= 0 || n.Confidence >= 1 {
|
||||
return fmt.Errorf("%w: confidence must be strictly between 0 and 1", ErrInvalid)
|
||||
}
|
||||
if n.WindowStart.IsZero() {
|
||||
n.WindowStart = time.Now()
|
||||
}
|
||||
if !n.WindowEnd.After(n.WindowStart) {
|
||||
return fmt.Errorf("%w: window_end must be after window_start", ErrInvalid)
|
||||
}
|
||||
switch n.Criteria.Kind {
|
||||
case "event_match":
|
||||
if n.Criteria.Expect != "occurs" && n.Criteria.Expect != "absent" {
|
||||
n.Criteria.Expect = "occurs"
|
||||
}
|
||||
var terms []string
|
||||
for _, t := range n.Criteria.Terms {
|
||||
if t = strings.ToLower(strings.TrimSpace(t)); len(t) >= 2 {
|
||||
terms = append(terms, t)
|
||||
}
|
||||
}
|
||||
if len(terms) == 0 {
|
||||
return fmt.Errorf("%w: event_match needs terms", ErrInvalid)
|
||||
}
|
||||
n.Criteria.Terms = terms
|
||||
if n.Criteria.MinTerms <= 0 || n.Criteria.MinTerms > len(terms) {
|
||||
n.Criteria.MinTerms = min(len(terms), 3)
|
||||
}
|
||||
case "belief":
|
||||
if n.Criteria.BeliefID.IsZero() || n.Criteria.MinConfidence <= 0 {
|
||||
return fmt.Errorf("%w: belief criteria need belief_id and min_confidence", ErrInvalid)
|
||||
}
|
||||
case "operator", "":
|
||||
n.Criteria.Kind = "operator"
|
||||
default:
|
||||
return fmt.Errorf("%w: unknown criteria kind", ErrInvalid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create inserts a prediction.
|
||||
func (s *Store) Create(ctx context.Context, q storage.Querier, n New) (Prediction, error) {
|
||||
if err := n.validate(); err != nil {
|
||||
return Prediction{}, err
|
||||
}
|
||||
crit, _ := json.Marshal(n.Criteria)
|
||||
p := Prediction{ID: ids.New(), MindID: n.MindID, Statement: n.Statement, Subject: n.Subject, Confidence: n.Confidence, WindowStart: n.WindowStart, WindowEnd: n.WindowEnd, Criteria: n.Criteria,
|
||||
Status: StatusOpen, Source: n.Source, OriginEvent: n.OriginEvent, BeliefID: n.BeliefID, CreatedAt: time.Now()}
|
||||
_, err := q.Exec(ctx, `INSERT INTO predictions (id, mind_id, statement, subject, confidence, window_start, window_end, criteria, status, source, origin_event, belief_id)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,'open',$9,$10,$11)`, p.ID, p.MindID, p.Statement, p.Subject, p.Confidence, p.WindowStart, p.WindowEnd, crit, p.Source, nullID(p.OriginEvent), nullID(p.BeliefID))
|
||||
if err != nil {
|
||||
return Prediction{}, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func nullID(id ids.ID) *string {
|
||||
if id.IsZero() {
|
||||
return nil
|
||||
}
|
||||
s := string(id)
|
||||
return &s
|
||||
}
|
||||
|
||||
const cols = `id, mind_id, statement, subject, confidence, window_start, window_end, criteria, status, source, origin_event, belief_id, resolved_at, resolution, created_at`
|
||||
|
||||
func scan(row pgx.Row) (Prediction, error) {
|
||||
var p Prediction
|
||||
var crit []byte
|
||||
var status string
|
||||
var origin, belief *string
|
||||
var res *json.RawMessage
|
||||
if err := row.Scan(&p.ID, &p.MindID, &p.Statement, &p.Subject, &p.Confidence, &p.WindowStart, &p.WindowEnd, &crit, &status, &p.Source, &origin, &belief, &p.ResolvedAt, &res, &p.CreatedAt); err != nil {
|
||||
return p, storage.Translate(err)
|
||||
}
|
||||
p.Status = Status(status)
|
||||
json.Unmarshal(crit, &p.Criteria) //nolint:errcheck
|
||||
if origin != nil {
|
||||
p.OriginEvent = ids.ID(*origin)
|
||||
}
|
||||
if belief != nil {
|
||||
p.BeliefID = ids.ID(*belief)
|
||||
}
|
||||
if res != nil {
|
||||
p.Resolution = *res
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Get fetches a prediction scoped to a mind.
|
||||
func (s *Store) Get(ctx context.Context, mind, id ids.ID) (Prediction, error) {
|
||||
return scan(s.db.QueryRow(ctx, `SELECT `+cols+` FROM predictions WHERE id=$1 AND mind_id=$2`, id, mind))
|
||||
}
|
||||
|
||||
// List returns predictions, newest first, optionally by status.
|
||||
func (s *Store) List(ctx context.Context, mind ids.ID, statuses []Status, limit int) ([]Prediction, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
ss := make([]string, len(statuses))
|
||||
for i, st := range statuses {
|
||||
ss[i] = string(st)
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT `+cols+` FROM predictions WHERE mind_id=$1 AND (cardinality($2::text[])=0 OR status = ANY($2)) ORDER BY created_at DESC LIMIT $3`, mind, ss, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Prediction
|
||||
for rows.Next() {
|
||||
p, err := scan(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Open returns open predictions for a mind (all minds when mind is Nil).
|
||||
func (s *Store) Open(ctx context.Context, mind ids.ID) ([]Prediction, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT `+cols+` FROM predictions WHERE status='open' AND ($1::uuid IS NULL OR mind_id=$1) ORDER BY window_end LIMIT 1000`, nullID(mind))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Prediction
|
||||
for rows.Next() {
|
||||
p, err := scan(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Matches reports whether an event satisfies event_match terms.
|
||||
func (c Criteria) Matches(ev events.Event) bool {
|
||||
if c.Kind != "event_match" {
|
||||
return false
|
||||
}
|
||||
if len(c.Types) > 0 {
|
||||
ok := false
|
||||
for _, t := range c.Types {
|
||||
if events.Type(t) == ev.Type {
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
} else if ev.Type != events.Observation && ev.Type != events.Perception && ev.Type != events.ToolExecuted && ev.Type != events.ToolFailed {
|
||||
return false
|
||||
}
|
||||
text := strings.ToLower(ev.Summary)
|
||||
if raw := string(ev.Payload); len(raw) < 4000 {
|
||||
text += " " + strings.ToLower(raw)
|
||||
}
|
||||
n := 0
|
||||
for _, t := range c.Terms {
|
||||
if strings.Contains(text, t) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n >= c.MinTerms
|
||||
}
|
||||
|
||||
// Outcome is the deterministic verdict for a prediction.
|
||||
type Outcome struct {
|
||||
Status Status
|
||||
Evidence []events.EvidenceRef
|
||||
Reason string
|
||||
}
|
||||
|
||||
// Judge decides a prediction against events in its window and the current
|
||||
// time. It returns ok=false when the prediction cannot be judged yet.
|
||||
func Judge(p Prediction, now time.Time, window []events.Event, beliefConfidence *float64) (Outcome, bool) {
|
||||
switch p.Criteria.Kind {
|
||||
case "event_match":
|
||||
for _, ev := range window {
|
||||
if p.Criteria.Matches(ev) {
|
||||
refs := []events.EvidenceRef{{Kind: "event", ID: string(ev.ID), Note: "matched terms"}}
|
||||
if p.Criteria.Expect == "absent" {
|
||||
return Outcome{Status: StatusRefuted, Evidence: refs, Reason: "an event matching the terms occurred: " + ev.Summary}, true
|
||||
}
|
||||
return Outcome{Status: StatusConfirmed, Evidence: refs, Reason: "matching event observed: " + ev.Summary}, true
|
||||
}
|
||||
}
|
||||
if now.After(p.WindowEnd) {
|
||||
if p.Criteria.Expect == "absent" {
|
||||
return Outcome{Status: StatusConfirmed, Reason: "window closed with no matching event"}, true
|
||||
}
|
||||
return Outcome{Status: StatusRefuted, Reason: "window closed without a matching event"}, true
|
||||
}
|
||||
return Outcome{}, false
|
||||
case "belief":
|
||||
if beliefConfidence != nil && *beliefConfidence >= p.Criteria.MinConfidence {
|
||||
return Outcome{Status: StatusConfirmed, Evidence: []events.EvidenceRef{{Kind: "belief", ID: string(p.Criteria.BeliefID)}}, Reason: fmt.Sprintf("belief reached %.2f", *beliefConfidence)}, true
|
||||
}
|
||||
if now.After(p.WindowEnd) {
|
||||
return Outcome{Status: StatusRefuted, Evidence: []events.EvidenceRef{{Kind: "belief", ID: string(p.Criteria.BeliefID)}}, Reason: "window closed below the confidence threshold"}, true
|
||||
}
|
||||
return Outcome{}, false
|
||||
default:
|
||||
if now.After(p.WindowEnd.Add(7 * 24 * time.Hour)) {
|
||||
return Outcome{Status: StatusExpired, Reason: "operator did not resolve it within a week of the window"}, true
|
||||
}
|
||||
return Outcome{}, false
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve records an outcome: status, resolution, append-only result row.
|
||||
// Error is |truth − confidence| where truth is 1 for confirmed, 0 for refuted.
|
||||
func (s *Store) Resolve(ctx context.Context, q storage.Querier, p Prediction, o Outcome, eventID ids.ID) (Result, error) {
|
||||
if p.Status != StatusOpen {
|
||||
return Result{}, fmt.Errorf("%w: prediction already %s", ErrInvalid, p.Status)
|
||||
}
|
||||
truth := -1.0
|
||||
switch o.Status {
|
||||
case StatusConfirmed:
|
||||
truth = 1
|
||||
case StatusRefuted:
|
||||
truth = 0
|
||||
}
|
||||
errv := 0.0
|
||||
if truth >= 0 {
|
||||
errv = math.Abs(truth - p.Confidence)
|
||||
}
|
||||
evJSON, _ := json.Marshal(o.Evidence)
|
||||
if o.Evidence == nil {
|
||||
evJSON = []byte("[]")
|
||||
}
|
||||
resJSON, _ := json.Marshal(map[string]any{"reason": o.Reason, "evidence": o.Evidence, "error": errv})
|
||||
tag, err := q.Exec(ctx, `UPDATE predictions SET status=$3, resolved_at=now(), resolution=$4 WHERE id=$1 AND mind_id=$2 AND status='open'`, p.ID, p.MindID, string(o.Status), resJSON)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return Result{}, fmt.Errorf("%w: already resolved", ErrInvalid)
|
||||
}
|
||||
r := Result{ID: ids.New(), PredictionID: p.ID, Outcome: o.Status, Confidence: p.Confidence, Error: errv, Evidence: evJSON, EventID: eventID, ResolvedAt: time.Now()}
|
||||
_, err = q.Exec(ctx, `INSERT INTO prediction_results (id, prediction_id, mind_id, outcome, confidence, error, evidence, event_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`,
|
||||
r.ID, r.PredictionID, p.MindID, string(r.Outcome), r.Confidence, r.Error, evJSON, nullID(eventID))
|
||||
return r, err
|
||||
}
|
||||
|
||||
// Bucket is one calibration bin.
|
||||
type Bucket struct {
|
||||
Low, High float64 `json:"-"`
|
||||
Label string `json:"label"`
|
||||
Count int `json:"count"`
|
||||
Confirmed int `json:"confirmed"`
|
||||
MeanConf float64 `json:"mean_confidence"`
|
||||
HitRate float64 `json:"hit_rate"`
|
||||
}
|
||||
|
||||
// Calibration summarises resolved predictions.
|
||||
type Calibration struct {
|
||||
Total int `json:"total"`
|
||||
Confirmed int `json:"confirmed"`
|
||||
Refuted int `json:"refuted"`
|
||||
Expired int `json:"expired"`
|
||||
Accuracy float64 `json:"accuracy"` // confirmed / (confirmed+refuted)
|
||||
Brier float64 `json:"brier"` // mean squared error over decided predictions
|
||||
Buckets []Bucket `json:"buckets"`
|
||||
Overconfident bool `json:"overconfident"` // mean confidence exceeds hit rate by > 0.15
|
||||
}
|
||||
|
||||
// Calibrate computes accuracy, Brier score and confidence buckets since a time.
|
||||
func (s *Store) Calibrate(ctx context.Context, mind ids.ID, since time.Time) (Calibration, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT outcome, confidence FROM prediction_results WHERE mind_id=$1 AND resolved_at >= $2`, mind, since)
|
||||
if err != nil {
|
||||
return Calibration{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
c := Calibration{}
|
||||
edges := []float64{0, 0.2, 0.4, 0.6, 0.8, 1.01}
|
||||
for i := 0; i+1 < len(edges); i++ {
|
||||
c.Buckets = append(c.Buckets, Bucket{Low: edges[i], High: edges[i+1], Label: fmt.Sprintf("%.0f–%.0f%%", edges[i]*100, math.Min(edges[i+1], 1)*100)})
|
||||
}
|
||||
var sumSq, sumConf float64
|
||||
for rows.Next() {
|
||||
var outcome string
|
||||
var conf float64
|
||||
if err := rows.Scan(&outcome, &conf); err != nil {
|
||||
return c, err
|
||||
}
|
||||
c.Total++
|
||||
switch Status(outcome) {
|
||||
case StatusConfirmed:
|
||||
c.Confirmed++
|
||||
sumSq += (1 - conf) * (1 - conf)
|
||||
case StatusRefuted:
|
||||
c.Refuted++
|
||||
sumSq += conf * conf
|
||||
default:
|
||||
c.Expired++
|
||||
continue
|
||||
}
|
||||
sumConf += conf
|
||||
for i := range c.Buckets {
|
||||
b := &c.Buckets[i]
|
||||
if conf >= b.Low && conf < b.High {
|
||||
b.Count++
|
||||
b.MeanConf += conf
|
||||
if Status(outcome) == StatusConfirmed {
|
||||
b.Confirmed++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
decided := c.Confirmed + c.Refuted
|
||||
if decided > 0 {
|
||||
c.Accuracy = float64(c.Confirmed) / float64(decided)
|
||||
c.Brier = sumSq / float64(decided)
|
||||
c.Overconfident = sumConf/float64(decided)-c.Accuracy > 0.15 && decided >= 5
|
||||
}
|
||||
for i := range c.Buckets {
|
||||
b := &c.Buckets[i]
|
||||
if b.Count > 0 {
|
||||
b.MeanConf /= float64(b.Count)
|
||||
b.HitRate = float64(b.Confirmed) / float64(b.Count)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(c.Buckets, func(i, j int) bool { return c.Buckets[i].Low < c.Buckets[j].Low })
|
||||
return c, rows.Err()
|
||||
}
|
||||
|
||||
// Stats counts predictions by status.
|
||||
func (s *Store) Stats(ctx context.Context, mind ids.ID) (map[string]int64, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT status, count(*) FROM predictions WHERE mind_id=$1 GROUP BY status`, mind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := map[string]int64{}
|
||||
for rows.Next() {
|
||||
var k string
|
||||
var n int64
|
||||
if err := rows.Scan(&k, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[k] = n
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package predictions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/migrations"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
func TestJudgeIsDeterministic(t *testing.T) {
|
||||
now := time.Now()
|
||||
p := Prediction{Criteria: Criteria{Kind: "event_match", Expect: "absent", Terms: []string{"server-01", "fail"}, MinTerms: 2}, WindowEnd: now.Add(time.Hour), Confidence: 0.8}
|
||||
if _, ok := Judge(p, now, nil, nil); ok {
|
||||
t.Fatal("should not be judgeable before the window ends")
|
||||
}
|
||||
fail := events.Event{ID: ids.New(), Type: events.Observation, Summary: "server-01 failed to boot"}
|
||||
o, ok := Judge(p, now, []events.Event{fail}, nil)
|
||||
if !ok || o.Status != StatusRefuted || len(o.Evidence) != 1 {
|
||||
t.Fatalf("absent-expectation should be refuted by a match: %+v", o)
|
||||
}
|
||||
o, ok = Judge(p, now.Add(2*time.Hour), nil, nil)
|
||||
if !ok || o.Status != StatusConfirmed {
|
||||
t.Fatalf("absent-expectation should confirm at window end: %+v", o)
|
||||
}
|
||||
occurs := p
|
||||
occurs.Criteria.Expect = "occurs"
|
||||
if o, ok := Judge(occurs, now.Add(2*time.Hour), nil, nil); !ok || o.Status != StatusRefuted {
|
||||
t.Fatalf("occurs-expectation should be refuted at window end: %+v", o)
|
||||
}
|
||||
unrelated := events.Event{Type: events.Observation, Summary: "weather is mild"}
|
||||
if _, ok := Judge(occurs, now, []events.Event{unrelated}, nil); ok {
|
||||
t.Fatal("unrelated event must not resolve")
|
||||
}
|
||||
conf := 0.9
|
||||
b := Prediction{Criteria: Criteria{Kind: "belief", BeliefID: ids.New(), MinConfidence: 0.8}, WindowEnd: now.Add(time.Hour)}
|
||||
if o, ok := Judge(b, now, nil, &conf); !ok || o.Status != StatusConfirmed {
|
||||
t.Fatalf("belief criteria wrong: %+v", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreResolveAndCalibration(t *testing.T) {
|
||||
pool := storage.TestPool(t, migrations.FS)
|
||||
ctx := context.Background()
|
||||
m, _ := minds.NewStore(pool).Create(ctx, minds.CreateParams{Slug: "p"})
|
||||
s := NewStore(pool)
|
||||
if _, err := s.Create(ctx, pool, New{MindID: m.ID, Statement: "x", Confidence: 1, WindowEnd: time.Now().Add(time.Hour), Criteria: Criteria{Kind: "operator"}}); err == nil {
|
||||
t.Fatal("certainty 1.0 accepted")
|
||||
}
|
||||
mk := func(conf float64) Prediction {
|
||||
p, err := s.Create(ctx, pool, New{MindID: m.ID, Statement: "server stays healthy", Confidence: conf, WindowEnd: time.Now().Add(time.Hour), Criteria: Criteria{Kind: "event_match", Expect: "absent", Terms: []string{"server", "fail"}}, Source: "test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
p1, p2, p3 := mk(0.9), mk(0.7), mk(0.3)
|
||||
if _, err := s.Resolve(ctx, pool, p1, Outcome{Status: StatusRefuted, Reason: "server failed"}, ids.Nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Resolve(ctx, pool, p1, Outcome{Status: StatusConfirmed}, ids.Nil); err == nil {
|
||||
t.Fatal("double resolution accepted")
|
||||
}
|
||||
s.Resolve(ctx, pool, p2, Outcome{Status: StatusConfirmed}, ids.Nil) //nolint:errcheck
|
||||
s.Resolve(ctx, pool, p3, Outcome{Status: StatusConfirmed}, ids.Nil) //nolint:errcheck
|
||||
c, err := s.Calibrate(ctx, m.ID, time.Now().Add(-time.Hour))
|
||||
if err != nil || c.Total != 3 || c.Confirmed != 2 || c.Refuted != 1 {
|
||||
t.Fatalf("calibration: %v %+v", err, c)
|
||||
}
|
||||
wantBrier := (0.9*0.9 + 0.3*0.3 + 0.7*0.7) / 3
|
||||
if d := c.Brier - wantBrier; d > 1e-9 || d < -1e-9 {
|
||||
t.Fatalf("brier %v want %v", c.Brier, wantBrier)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `DELETE FROM prediction_results`); err == nil {
|
||||
t.Fatal("results must be append-only")
|
||||
}
|
||||
open, _ := s.Open(ctx, ids.Nil)
|
||||
if len(open) != 0 {
|
||||
t.Fatalf("open predictions remain: %d", len(open))
|
||||
}
|
||||
got, _ := s.Get(ctx, m.ID, p1.ID)
|
||||
if got.Status != StatusRefuted || got.ResolvedAt == nil || len(got.Resolution) == 0 {
|
||||
t.Fatalf("resolution not stored: %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package predictions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
evstore "github.com/andydixon/noema/internal/events"
|
||||
"github.com/andydixon/noema/internal/observability"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
// Resolver judges open predictions against recorded events, records
|
||||
// outcomes, emits prediction_resolved / prediction_error events and feeds
|
||||
// the result back into the linked belief as evidence (spec §11).
|
||||
type Resolver struct {
|
||||
Store *Store
|
||||
Events *evstore.Store
|
||||
Beliefs *beliefs.Store
|
||||
DB interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
}
|
||||
Log *slog.Logger
|
||||
Metrics *observability.Metrics
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
// Run resolves every judgeable open prediction. Safe to call periodically.
|
||||
func (r *Resolver) Run(ctx context.Context) error {
|
||||
now := time.Now()
|
||||
if r.Now != nil {
|
||||
now = r.Now()
|
||||
}
|
||||
open, err := r.Store.Open(ctx, ids.Nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range open {
|
||||
if err := r.resolveOne(ctx, p, now); err != nil {
|
||||
r.Log.Warn("prediction resolution failed", "prediction", p.ID, "err", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Resolver) resolveOne(ctx context.Context, p Prediction, now time.Time) error {
|
||||
var window []events.Event
|
||||
if p.Criteria.Kind == "event_match" {
|
||||
var err error
|
||||
window, err = r.Events.Query(ctx, evstore.Filter{MindID: p.MindID, Since: p.WindowStart, Until: minTime(now, p.WindowEnd), Ascending: true, Limit: 1000})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Events that cite this prediction (its own creation) are not evidence about the world.
|
||||
filtered := window[:0]
|
||||
for _, ev := range window {
|
||||
if ev.ID != p.OriginEvent && ev.Category != events.CatPrediction {
|
||||
filtered = append(filtered, ev)
|
||||
}
|
||||
}
|
||||
window = filtered
|
||||
}
|
||||
var beliefConf *float64
|
||||
if p.Criteria.Kind == "belief" && r.Beliefs != nil {
|
||||
if b, err := r.Beliefs.Get(ctx, r.Store.db, p.MindID, p.Criteria.BeliefID); err == nil {
|
||||
beliefConf = &b.Confidence
|
||||
}
|
||||
}
|
||||
o, ok := Judge(p, now, window, beliefConf)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return storage.Tx(ctx, r.Store.db, func(tx pgx.Tx) error {
|
||||
res, emitted, err := r.ResolveIn(ctx, tx, p, o, "predictions.resolver")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = res
|
||||
r.Events.Publish(emitted...)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// ResolveIn records an outcome inside q and emits the events; shared by the
|
||||
// scheduler and operator resolution. Returned events must be published by
|
||||
// the caller after commit.
|
||||
func (r *Resolver) ResolveIn(ctx context.Context, q storage.Querier, p Prediction, o Outcome, source string) (Result, []events.Event, error) {
|
||||
typ := events.PredictionResolved
|
||||
salience := 0.4
|
||||
if o.Status == StatusRefuted {
|
||||
typ = events.PredictionError
|
||||
salience = 0.5 + 0.4*p.Confidence // a confident miss deserves attention
|
||||
}
|
||||
refs := append([]events.EvidenceRef{{Kind: "prediction", ID: string(p.ID)}}, o.Evidence...)
|
||||
evs, err := r.Events.AppendIn(ctx, q, events.New{MindID: p.MindID, Type: typ, Source: source, Summary: fmt.Sprintf("prediction %s (was %.2f): %s — %s", o.Status, p.Confidence, p.Statement, o.Reason),
|
||||
Salience: salience, Confidence: 1, CausationID: p.OriginEvent, Evidence: refs,
|
||||
Metadata: map[string]any{"prediction_id": string(p.ID), "outcome": string(o.Status), "confidence": p.Confidence, "belief_id": string(p.BeliefID)},
|
||||
Payload: map[string]any{"prediction_id": p.ID, "statement": p.Statement, "outcome": o.Status, "confidence": p.Confidence, "reason": o.Reason, "evidence": o.Evidence}})
|
||||
if err != nil {
|
||||
return Result{}, nil, err
|
||||
}
|
||||
res, err := r.Store.Resolve(ctx, q, p, o, evs[0].ID)
|
||||
if err != nil {
|
||||
return Result{}, nil, err
|
||||
}
|
||||
if r.Metrics != nil {
|
||||
if typ == events.PredictionError {
|
||||
r.Metrics.PredictionErr.Inc()
|
||||
}
|
||||
}
|
||||
// Feed back into the belief the prediction was derived from.
|
||||
if !p.BeliefID.IsZero() && r.Beliefs != nil && (o.Status == StatusConfirmed || o.Status == StatusRefuted) {
|
||||
dir, weight := "for", 0.3
|
||||
if o.Status == StatusRefuted {
|
||||
dir, weight = "against", 0.5
|
||||
}
|
||||
emit := func(ctx context.Context, q storage.Querier, n events.New) (events.Event, error) {
|
||||
out, err := r.Events.AppendIn(ctx, q, n)
|
||||
if err != nil {
|
||||
return events.Event{}, err
|
||||
}
|
||||
evs = append(evs, out[0])
|
||||
return out[0], nil
|
||||
}
|
||||
if _, err := r.Beliefs.AddEvidence(ctx, q, emit, p.MindID, p.BeliefID, beliefs.EvidenceInput{Direction: dir, Kind: "prediction", RefID: string(p.ID), Source: "prediction:" + string(p.ID), Weight: weight, Credibility: 0.9, Note: "prediction " + string(o.Status) + ": " + p.Statement}, "prediction "+string(o.Status), evs[0].ID, ids.Nil); err != nil {
|
||||
r.Log.Warn("belief feedback failed", "belief", p.BeliefID, "err", err)
|
||||
}
|
||||
}
|
||||
return res, evs, nil
|
||||
}
|
||||
|
||||
func minTime(a, b time.Time) time.Time {
|
||||
if a.Before(b) {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/motivation"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
@@ -102,7 +103,13 @@ func (u *UI) mindOverview(w http.ResponseWriter, r *http.Request) {
|
||||
ms, _ := u.deps.Memory.Stats(r.Context(), m.ID)
|
||||
bs, _ := u.deps.Beliefs.Stats(r.Context(), m.ID)
|
||||
top, _ := u.deps.Beliefs.List(r.Context(), m.ID, beliefs.Filter{Limit: 8})
|
||||
u.render(w, r, "mind", m.DisplayName, map[string]any{"Mind": m, "Brief": m.Personality.Brief(), "SelfModel": sm, "Stats": st, "MemStats": ms, "BeliefStats": bs, "Beliefs": top})
|
||||
motiv, _ := u.deps.Motivation.Load(r.Context(), u.deps.DB, m)
|
||||
motivation.Decay(&motiv, motivation.Baselines(m.Personality), time.Since(motiv.Affect.UpdatedAt))
|
||||
active, _ := u.deps.Goals.Active(r.Context(), m.ID)
|
||||
if len(active) > 6 {
|
||||
active = active[:6]
|
||||
}
|
||||
u.render(w, r, "mind", m.DisplayName, map[string]any{"Mind": m, "Brief": m.Personality.Brief(), "SelfModel": sm, "Stats": st, "MemStats": ms, "BeliefStats": bs, "Beliefs": top, "Motiv": motiv, "Goals": active})
|
||||
}
|
||||
|
||||
func (u *UI) mindStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/goals"
|
||||
"github.com/andydixon/noema/internal/predictions"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
func (u *UI) motivationRoutes(m *http.ServeMux) {
|
||||
m.Handle("GET /minds/{mind}/goals", u.require(auth.RoleObserver, u.goalsPage))
|
||||
m.Handle("POST /minds/{mind}/goals", u.require(auth.RoleOperator, u.goalCreate))
|
||||
m.Handle("POST /minds/{mind}/goals/{id}", u.require(auth.RoleOperator, u.goalUpdate))
|
||||
m.Handle("GET /minds/{mind}/predictions", u.require(auth.RoleObserver, u.predictionsPage))
|
||||
m.Handle("POST /minds/{mind}/predictions", u.require(auth.RoleOperator, u.predictionCreate))
|
||||
m.Handle("POST /minds/{mind}/predictions/{id}/resolve", u.require(auth.RoleOperator, u.predictionResolve))
|
||||
m.Handle("POST /minds/{mind}/state/reset", u.require(auth.RoleOperator, u.stateReset))
|
||||
}
|
||||
|
||||
func (u *UI) goalsPage(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
list, _ := u.deps.Goals.List(r.Context(), m.ID, nil, 0)
|
||||
st, _ := u.deps.Goals.Stats(r.Context(), m.ID)
|
||||
u.render(w, r, "goals", m.DisplayName+" · Goals", map[string]any{"Mind": m, "Goals": list, "Stats": st})
|
||||
}
|
||||
|
||||
func (u *UI) goalCreate(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
r.ParseForm() //nolint:errcheck
|
||||
prio, _ := strconv.ParseFloat(r.PostFormValue("priority"), 64)
|
||||
var parent ids.ID
|
||||
if v := r.PostFormValue("parent_id"); v != "" {
|
||||
parent, _ = ids.Parse(v)
|
||||
}
|
||||
if _, err := u.goalF(r, goals.New{MindID: m.ID, ParentID: parent, Title: r.PostFormValue("title"), Description: r.PostFormValue("description"), Kind: goals.Kind(r.PostFormValue("kind")), Priority: prio, Subject: r.PostFormValue("subject")}); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/goals")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Goal created.", "/minds/"+m.Slug+"/goals")
|
||||
}
|
||||
|
||||
func (u *UI) goalUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
u.flash(w, r, "error", "Bad goal.", "/minds/"+m.Slug+"/goals")
|
||||
return
|
||||
}
|
||||
r.ParseForm() //nolint:errcheck
|
||||
var up goals.Update
|
||||
switch r.PostFormValue("action") {
|
||||
case "progress":
|
||||
p, _ := strconv.ParseFloat(r.PostFormValue("progress"), 64)
|
||||
up.Progress = &p
|
||||
case "complete":
|
||||
st := goals.StatusCompleted
|
||||
up.Status = &st
|
||||
case "abandon":
|
||||
st := goals.StatusAbandoned
|
||||
up.Status = &st
|
||||
}
|
||||
if _, err := u.goalUpdF(r, m.ID, id, up); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/goals")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Goal updated.", "/minds/"+m.Slug+"/goals")
|
||||
}
|
||||
|
||||
func (u *UI) predictionsPage(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
list, _ := u.deps.Predictions.List(r.Context(), m.ID, nil, 200)
|
||||
stats, _ := u.deps.Predictions.Stats(r.Context(), m.ID)
|
||||
cal, _ := u.deps.Predictions.Calibrate(r.Context(), m.ID, time.Now().Add(-90*24*time.Hour))
|
||||
u.render(w, r, "predictions", m.DisplayName+" · Predictions", map[string]any{"Mind": m, "Predictions": list, "Stats": stats, "Calibration": cal})
|
||||
}
|
||||
|
||||
func (u *UI) predictionCreate(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
r.ParseForm() //nolint:errcheck
|
||||
conf, _ := strconv.ParseFloat(r.PostFormValue("confidence"), 64)
|
||||
hours, _ := strconv.Atoi(r.PostFormValue("hours"))
|
||||
if hours <= 0 {
|
||||
hours = 24
|
||||
}
|
||||
var terms []string
|
||||
for _, t := range strings.Split(r.PostFormValue("terms"), ",") {
|
||||
if t = strings.TrimSpace(t); t != "" {
|
||||
terms = append(terms, t)
|
||||
}
|
||||
}
|
||||
crit := predictions.Criteria{Kind: r.PostFormValue("kind"), Expect: r.PostFormValue("expect"), Terms: terms}
|
||||
if _, err := u.predF(r, predictions.New{MindID: m.ID, Statement: r.PostFormValue("statement"), Confidence: conf, WindowEnd: time.Now().Add(time.Duration(hours) * time.Hour), Criteria: crit}); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/predictions")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Prediction recorded.", "/minds/"+m.Slug+"/predictions")
|
||||
}
|
||||
|
||||
func (u *UI) predictionResolve(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
u.flash(w, r, "error", "Bad prediction.", "/minds/"+m.Slug+"/predictions")
|
||||
return
|
||||
}
|
||||
if _, err := u.resolveF(r, m.ID, id, predictions.Status(r.PostFormValue("outcome")), "resolved in the UI"); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/predictions")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Prediction resolved.", "/minds/"+m.Slug+"/predictions")
|
||||
}
|
||||
|
||||
func (u *UI) stateReset(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, err := u.resetF(r, m.ID); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug)
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Drives and affect reset to baseline.", "/minds/"+m.Slug)
|
||||
}
|
||||
+53
-12
@@ -22,8 +22,11 @@ import (
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"github.com/andydixon/noema/internal/conversation"
|
||||
"github.com/andydixon/noema/internal/goals"
|
||||
"github.com/andydixon/noema/internal/llm"
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/motivation"
|
||||
"github.com/andydixon/noema/internal/predictions"
|
||||
"github.com/andydixon/noema/internal/version"
|
||||
"github.com/andydixon/noema/pkg/concepts"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
@@ -32,16 +35,38 @@ import (
|
||||
|
||||
// UI holds handlers and the parsed template set.
|
||||
type UI struct {
|
||||
deps app.Deps
|
||||
pages map[string]*template.Template
|
||||
static fs.FS
|
||||
statusF func(r *http.Request) any // system status provider injected by api
|
||||
ctxF func(r *http.Request, id ids.ID) (api.EventContext, error) // event forensic context, shared with api
|
||||
wsF func(r *http.Request, m minds.Mind) (api.WorkspaceView, error)
|
||||
memF func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error)
|
||||
belF func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error)
|
||||
emitF func() beliefs.Emitter
|
||||
postF func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error)
|
||||
deps app.Deps
|
||||
pages map[string]*template.Template
|
||||
static fs.FS
|
||||
statusF func(r *http.Request) any // system status provider injected by api
|
||||
ctxF func(r *http.Request, id ids.ID) (api.EventContext, error) // event forensic context, shared with api
|
||||
wsF func(r *http.Request, m minds.Mind) (api.WorkspaceView, error)
|
||||
memF func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error)
|
||||
belF func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error)
|
||||
emitF func() beliefs.Emitter
|
||||
postF func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error)
|
||||
goalF func(r *http.Request, n goals.New) (goals.Goal, error)
|
||||
goalUpdF func(r *http.Request, mind, id ids.ID, u goals.Update) (goals.Goal, error)
|
||||
predF func(r *http.Request, n predictions.New) (predictions.Prediction, error)
|
||||
resolveF func(r *http.Request, mind, id ids.ID, outcome predictions.Status, reason string) (predictions.Result, error)
|
||||
resetF func(r *http.Request, mind ids.ID) (motivation.State, error)
|
||||
}
|
||||
|
||||
// Actions groups the server-side operations the UI delegates to the API layer
|
||||
// so both surfaces share one implementation and one audit path.
|
||||
type Actions struct {
|
||||
Status func(r *http.Request) any
|
||||
Context func(r *http.Request, id ids.ID) (api.EventContext, error)
|
||||
Workspace func(r *http.Request, m minds.Mind) (api.WorkspaceView, error)
|
||||
Memory func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error)
|
||||
Belief func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error)
|
||||
Emitter func() beliefs.Emitter
|
||||
Post func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error)
|
||||
Goal func(r *http.Request, n goals.New) (goals.Goal, error)
|
||||
GoalUpdate func(r *http.Request, mind, id ids.ID, u goals.Update) (goals.Goal, error)
|
||||
Predict func(r *http.Request, n predictions.New) (predictions.Prediction, error)
|
||||
Resolve func(r *http.Request, mind, id ids.ID, outcome predictions.Status, reason string) (predictions.Result, error)
|
||||
ResetState func(r *http.Request, mind ids.ID) (motivation.State, error)
|
||||
}
|
||||
|
||||
// View is the data passed to every template.
|
||||
@@ -62,7 +87,7 @@ const flashCookie = "noema_flash"
|
||||
|
||||
// New parses templates. In development, when ./web exists on disk, templates
|
||||
// and static files are read from disk so edits show without a rebuild.
|
||||
func New(deps app.Deps, statusF func(r *http.Request) any, ctxF func(r *http.Request, id ids.ID) (api.EventContext, error), wsF func(r *http.Request, m minds.Mind) (api.WorkspaceView, error), memF func(r *http.Request, mind, id ids.ID) (api.MemoryDetail, error), belF func(r *http.Request, mind, id ids.ID) (api.BeliefDetail, error), emitF func() beliefs.Emitter, postF func(r *http.Request, mind ids.ID, conv conversation.Conversation, text, who string) (conversation.Message, error)) (*UI, error) {
|
||||
func New(deps app.Deps, a Actions) (*UI, error) {
|
||||
tmplFS, staticFS := web.Templates(), web.Static()
|
||||
if !deps.Config.Production() {
|
||||
if st, err := os.Stat("web/templates"); err == nil && st.IsDir() {
|
||||
@@ -70,7 +95,7 @@ func New(deps app.Deps, statusF func(r *http.Request) any, ctxF func(r *http.Req
|
||||
deps.Log.Info("ui: serving templates and static files from ./web (development)")
|
||||
}
|
||||
}
|
||||
u := &UI{deps: deps, static: staticFS, statusF: statusF, ctxF: ctxF, wsF: wsF, memF: memF, belF: belF, emitF: emitF, postF: postF}
|
||||
u := &UI{deps: deps, static: staticFS, statusF: a.Status, ctxF: a.Context, wsF: a.Workspace, memF: a.Memory, belF: a.Belief, emitF: a.Emitter, postF: a.Post, goalF: a.Goal, goalUpdF: a.GoalUpdate, predF: a.Predict, resolveF: a.Resolve, resetF: a.ResetState}
|
||||
if err := u.parse(tmplFS); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -123,6 +148,21 @@ func (u *UI) parse(fsys fs.FS) error {
|
||||
"relations": func() []concepts.Relation { return concepts.AllRelations },
|
||||
"llmRoles": func() []llm.RoleInfo { return llm.Roles },
|
||||
"pretty": prettyRaw,
|
||||
"affectMap": func(a motivation.Affect) map[string]float64 {
|
||||
return map[string]float64{"valence": a.Valence, "arousal": a.Arousal, "curiosity": a.Curiosity, "concern": a.Concern, "frustration": a.Frustration, "confidence": a.Confidence, "uncertainty": a.Uncertainty, "engagement": a.Engagement, "task_pressure": a.TaskPressure, "load": a.Load}
|
||||
},
|
||||
"valencePos": func(v float64) float64 {
|
||||
if v < 0 {
|
||||
return 0.5 + v/2
|
||||
}
|
||||
return 0.5
|
||||
},
|
||||
"valenceWidth": func(v float64) float64 {
|
||||
if v < 0 {
|
||||
return -v / 2
|
||||
}
|
||||
return v / 2
|
||||
},
|
||||
"deref_id": func(id *ids.ID) ids.ID {
|
||||
if id == nil {
|
||||
return ids.Nil
|
||||
@@ -301,6 +341,7 @@ func (u *UI) Register(m *http.ServeMux) {
|
||||
u.beliefRoutes(m)
|
||||
u.llmRoutes(m)
|
||||
u.conversationRoutes(m)
|
||||
u.motivationRoutes(m)
|
||||
|
||||
for _, p := range []struct{ path, title, note string }{
|
||||
{"/approvals", "Approvals", "The approval centre arrives with the capability system."},
|
||||
|
||||
@@ -46,13 +46,14 @@ type Item struct {
|
||||
|
||||
// State is the persisted snapshot.
|
||||
type State struct {
|
||||
Cycle int64 `json:"cycle"`
|
||||
Mode string `json:"mode"`
|
||||
LastCycleID ids.ID `json:"last_cycle_id,omitempty"`
|
||||
LastCycleAt time.Time `json:"last_cycle_at"`
|
||||
LastStimulus time.Time `json:"last_stimulus_at"`
|
||||
Workspace []Thought `json:"workspace"`
|
||||
WorkingMemory []Item `json:"working_memory"`
|
||||
Cycle int64 `json:"cycle"`
|
||||
Mode string `json:"mode"`
|
||||
LastCycleID ids.ID `json:"last_cycle_id,omitempty"`
|
||||
LastCycleAt time.Time `json:"last_cycle_at"`
|
||||
LastStimulus time.Time `json:"last_stimulus_at"`
|
||||
Workspace []Thought `json:"workspace"`
|
||||
WorkingMemory []Item `json:"working_memory"`
|
||||
EffectiveWeights map[string]float64 `json:"effective_weights,omitempty"` // attention weights after drive/affect modulation in the last cycle
|
||||
}
|
||||
|
||||
// Outcome records what happened to one candidate during admission.
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS prediction_results;
|
||||
DROP TABLE IF EXISTS predictions;
|
||||
DROP TABLE IF EXISTS affect_states;
|
||||
DROP TABLE IF EXISTS drives;
|
||||
DROP TABLE IF EXISTS goals;
|
||||
@@ -0,0 +1,76 @@
|
||||
-- Goals, drives, affect and predictions.
|
||||
|
||||
CREATE TABLE goals (
|
||||
id uuid PRIMARY KEY,
|
||||
mind_id uuid NOT NULL REFERENCES minds(id),
|
||||
parent_id uuid REFERENCES goals(id),
|
||||
title text NOT NULL,
|
||||
description text NOT NULL DEFAULT '',
|
||||
kind text NOT NULL DEFAULT 'task' CHECK (kind IN ('task','investigation','question','maintenance')),
|
||||
status text NOT NULL DEFAULT 'active' CHECK (status IN ('active','blocked','paused','completed','abandoned')),
|
||||
priority double precision NOT NULL DEFAULT 0.5 CHECK (priority >= 0 AND priority <= 1),
|
||||
progress double precision NOT NULL DEFAULT 0 CHECK (progress >= 0 AND progress <= 1),
|
||||
owner text NOT NULL DEFAULT 'mind', -- mind | operator
|
||||
blockers jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||
origin_event uuid,
|
||||
subject text NOT NULL DEFAULT '',
|
||||
due_at timestamptz,
|
||||
last_progress_at timestamptz NOT NULL DEFAULT now(),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
completed_at timestamptz
|
||||
);
|
||||
CREATE INDEX goals_mind_status_idx ON goals (mind_id, status, priority DESC);
|
||||
CREATE INDEX goals_parent_idx ON goals (parent_id) WHERE parent_id IS NOT NULL;
|
||||
|
||||
CREATE TABLE drives (
|
||||
mind_id uuid NOT NULL REFERENCES minds(id),
|
||||
name text NOT NULL,
|
||||
value double precision NOT NULL,
|
||||
min double precision NOT NULL DEFAULT 0,
|
||||
max double precision NOT NULL DEFAULT 1,
|
||||
baseline double precision NOT NULL DEFAULT 0.5,
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (mind_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE affect_states (
|
||||
mind_id uuid PRIMARY KEY REFERENCES minds(id),
|
||||
state jsonb NOT NULL,
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE predictions (
|
||||
id uuid PRIMARY KEY,
|
||||
mind_id uuid NOT NULL REFERENCES minds(id),
|
||||
statement text NOT NULL,
|
||||
subject text NOT NULL DEFAULT '',
|
||||
confidence double precision NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
window_start timestamptz NOT NULL DEFAULT now(),
|
||||
window_end timestamptz NOT NULL,
|
||||
criteria jsonb NOT NULL, -- {"kind":"event_match","expect":"occurs|absent","terms":[...],"types":[...]} | {"kind":"belief","belief_id":..,"min_confidence":..} | {"kind":"operator"}
|
||||
status text NOT NULL DEFAULT 'open' CHECK (status IN ('open','confirmed','refuted','expired','withdrawn')),
|
||||
source text NOT NULL DEFAULT '',
|
||||
origin_event uuid,
|
||||
belief_id uuid REFERENCES beliefs(id),
|
||||
resolved_at timestamptz,
|
||||
resolution jsonb,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX predictions_mind_status_idx ON predictions (mind_id, status, window_end);
|
||||
|
||||
-- Append-only outcomes for calibration.
|
||||
CREATE TABLE prediction_results (
|
||||
id uuid PRIMARY KEY,
|
||||
prediction_id uuid NOT NULL REFERENCES predictions(id),
|
||||
mind_id uuid NOT NULL REFERENCES minds(id),
|
||||
outcome text NOT NULL CHECK (outcome IN ('confirmed','refuted','expired','withdrawn')),
|
||||
confidence double precision NOT NULL,
|
||||
error double precision NOT NULL, -- |outcome(1/0) - confidence| for confirmed/refuted
|
||||
evidence jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||
event_id uuid,
|
||||
resolved_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX prediction_results_mind_idx ON prediction_results (mind_id, resolved_at DESC);
|
||||
CREATE TRIGGER prediction_results_append_only BEFORE UPDATE OR DELETE ON prediction_results
|
||||
FOR EACH ROW EXECUTE FUNCTION noema_forbid_change();
|
||||
+8
-3
@@ -25,12 +25,15 @@ import (
|
||||
"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"
|
||||
"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"
|
||||
@@ -51,11 +54,12 @@ func start(t *testing.T, deps app.Deps, perceive, respond llm.LanguageModel) *ha
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
group := scheduler.NewGroup(ctx, deps.Log)
|
||||
registry := brain.NewRegistry().AddProcess(cognition.Attend{}).AddProcess(cognition.Retrieve{}).AddProcess(cognition.Associate{}).AddProcess(cognition.Perceive{})
|
||||
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.LLM.Override = map[llm.Role]llm.LanguageModel{llm.RolePerceive: perceive, llm.RoleRespond: respond}
|
||||
deps.Brain = brain.NewManager(brain.Config{DB: deps.DB, Events: deps.Events, Minds: deps.Minds, Registry: registry, Pool: scheduler.NewPool(4), Log: deps.Log, Metrics: deps.Metrics,
|
||||
Services: brain.Services{Events: deps.Events, Memory: deps.Memory, Beliefs: deps.Beliefs, LLM: deps.LLM, Prompts: deps.Prompts}}, group)
|
||||
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)
|
||||
if err := deps.Brain.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -102,7 +106,8 @@ func TestFirstMilestone(t *testing.T) {
|
||||
llmStore := router.NewStore(pool)
|
||||
deps := app.Deps{Config: cfg, Log: log, DB: pool, Auth: auth.NewService(pool, time.Hour, 2*time.Hour), Audit: audit.New(pool, log), Metrics: observability.NewMetrics(),
|
||||
Minds: minds.NewStore(pool), Events: evstore.NewStore(pool, evstore.NewBus(64), nil), Memory: memory.NewStore(ctx, pool, nil), Beliefs: beliefs.NewStore(pool),
|
||||
Conversations: conversation.NewStore(pool), Secrets: sec, LLMStore: llmStore, LLM: router.NewRouter(llmStore, sec, log, nil, 2), Prompts: prompts.NewStore(pool), Migrations: migrations.FS, Started: time.Now()}
|
||||
Conversations: conversation.NewStore(pool), Goals: goals.NewStore(pool), Motivation: motivation.NewStore(pool), Predictions: predictions.NewStore(pool), Secrets: sec, LLMStore: llmStore, LLM: router.NewRouter(llmStore, sec, log, nil, 2), Prompts: prompts.NewStore(pool), Migrations: migrations.FS, Started: time.Now()}
|
||||
deps.Resolver = &predictions.Resolver{Store: deps.Predictions, Events: deps.Events, Beliefs: deps.Beliefs, Log: log}
|
||||
if _, err := deps.Prompts.SeedDefaults(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -190,3 +190,14 @@ ol.messages { list-style: none; padding: 0; margin: 0 0 1rem; display: flex; fle
|
||||
.msg-head { margin-bottom: .2rem; }
|
||||
.chat-input input { min-width: 0; }
|
||||
.badge.small { font-size: .65rem; }
|
||||
|
||||
/* Goals, predictions, state */
|
||||
.badge.gs-active { background: var(--accent-soft); color: var(--accent); } .badge.gs-blocked { background: var(--warn-soft); color: var(--warn); } .badge.gs-completed { background: var(--ok-soft); color: var(--ok); } .badge.gs-abandoned, .badge.gs-paused { background: var(--surface-2); color: var(--muted); }
|
||||
.badge.ps-open { background: var(--accent-soft); color: var(--accent); } .badge.ps-confirmed { background: var(--ok-soft); color: var(--ok); } .badge.ps-refuted { background: var(--danger-soft); color: var(--danger); } .badge.ps-expired, .badge.ps-withdrawn { background: var(--surface-2); color: var(--muted); }
|
||||
.cal { position: relative; width: 140px; height: 10px; background: var(--surface-2); border-radius: 3px; }
|
||||
.cal-bar { position: absolute; left: 0; top: 0; height: 100%; background: var(--accent-soft); border-radius: 3px; }
|
||||
.cal-hit { position: absolute; top: -3px; width: 3px; height: 16px; background: var(--accent); margin-left: -1px; }
|
||||
.dim { display: grid; grid-template-columns: 8rem 1fr 6rem; align-items: center; gap: .5rem; font-size: .85rem; margin: .2rem 0; }
|
||||
.dim-bar { position: relative; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; } .dim-bar i { position: absolute; top: 0; height: 100%; background: var(--accent); left: 0; }
|
||||
.dim-bar.bipolar::after { content: ""; position: absolute; left: 50%; top: 0; width: 1px; height: 100%; background: var(--muted); }
|
||||
.dim-val { font-variant-numeric: tabular-nums; color: var(--muted); }
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
{{define "content"}}
|
||||
{{$m := .Data.Mind}}
|
||||
<div class="pagehead mindhead"><div><h1>{{$m.DisplayName}} <span class="muted small">goals</span></h1><p class="muted">{{range $k, $v := .Data.Stats.ByStatus}}<span class="badge gs-{{$k}}">{{$k}} {{$v}}</span> {{end}} Goals bias attention through goal relevance; they never grant capabilities.</p></div></div>
|
||||
{{template "mind_tabs" (dict "M" $m "Active" "goals")}}
|
||||
<section class="grid cols-3-1">
|
||||
<div class="card">
|
||||
{{if .Data.Goals}}
|
||||
<table class="table compact">
|
||||
<thead><tr><th>Goal</th><th>Kind</th><th>Status</th><th class="num">Priority</th><th>Progress</th><th>Owner</th><th>Progress at</th>{{if .Principal.User.Role.AtLeast "operator"}}<th></th>{{end}}</tr></thead>
|
||||
<tbody>{{range .Data.Goals}}<tr>
|
||||
<td>{{if .ParentID}}<span class="muted">↳ </span>{{end}}<strong>{{.Title}}</strong>{{if .Description}}<div class="small muted">{{.Description}}</div>{{end}}{{if .Blockers}}<div class="small danger">blocked: {{range .Blockers}}{{.}}; {{end}}</div>{{end}}{{if .OriginEvent}} <a class="small" href="/events/{{.OriginEvent}}">origin</a>{{end}}</td>
|
||||
<td class="small">{{.Kind}}</td><td><span class="badge gs-{{.Status}}">{{.Status}}</span></td><td class="num">{{conf .Priority}}</td>
|
||||
<td><span class="conf"><span class="conf-bar"><i style="width:{{pct .Progress}}"></i></span> {{pct .Progress}}</span></td><td class="small muted">{{.Owner}}</td><td class="small muted nowrap">{{ago .LastProgressAt}}</td>
|
||||
{{if $.Principal.User.Role.AtLeast "operator"}}<td class="nowrap"><form method="post" action="/minds/{{$m.Slug}}/goals/{{.ID}}" class="inline"><input type="hidden" name="_csrf" value="{{$.CSRF}}">
|
||||
<input type="number" name="progress" min="0" max="1" step="0.1" value="{{.Progress}}" size="3" aria-label="progress"><button class="btn small" name="action" value="progress">Set</button>
|
||||
{{if ne .Status "completed"}}<button class="btn small" name="action" value="complete">Done</button>{{end}}{{if ne .Status "abandoned"}}<button class="btn small btn-danger" name="action" value="abandon" data-confirm="Abandon this goal?">Abandon</button>{{end}}</form></td>{{end}}
|
||||
</tr>{{end}}</tbody>
|
||||
</table>
|
||||
{{else}}<p class="muted">No goals yet. Curiosity creates investigation goals from unexplained observations, prediction errors and contradictions; operators can set task goals here.</p>{{end}}
|
||||
</div>
|
||||
{{if .Principal.User.Role.AtLeast "operator"}}
|
||||
<div class="card">
|
||||
<h2>New goal</h2>
|
||||
<form method="post" action="/minds/{{$m.Slug}}/goals" class="stack">
|
||||
<input type="hidden" name="_csrf" value="{{.CSRF}}">
|
||||
<label>Title <input name="title" required maxlength="300"></label>
|
||||
<label>Description <textarea name="description" rows="2"></textarea></label>
|
||||
<div class="grid cols-2 tight"><label>Kind <select name="kind"><option value="task">task</option><option value="investigation">investigation</option><option value="question">question</option><option value="maintenance">maintenance</option></select></label>
|
||||
<label>Subject <input name="subject" placeholder="atlas" maxlength="80"></label></div>
|
||||
<label class="slider"><span>Priority</span><input type="range" name="priority" min="0.1" max="1" step="0.1" value="0.5" data-out><output>0.5</output></label>
|
||||
<label>Parent <select name="parent_id"><option value="">none</option>{{range .Data.Goals}}{{if eq .Status "active"}}<option value="{{.ID}}">{{trunc .Title 50}}</option>{{end}}{{end}}</select></label>
|
||||
<div><button class="btn btn-primary" type="submit">Create</button></div>
|
||||
</form>
|
||||
</div>
|
||||
{{end}}
|
||||
</section>
|
||||
{{end}}
|
||||
@@ -42,6 +42,13 @@
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section class="grid cols-2">
|
||||
{{template "state_panel" (dict "State" .Data.Motiv "Mind" $m "CSRF" .CSRF "CanOperate" (.Principal.User.Role.AtLeast "operator"))}}
|
||||
<div class="card">
|
||||
<h2>Active goals <span class="muted small"><a href="/minds/{{$m.Slug}}/goals">all</a></span></h2>
|
||||
{{if .Data.Goals}}<table class="table compact"><tbody>{{range .Data.Goals}}<tr><td><span class="badge gs-{{.Status}}">{{.Status}}</span></td><td>{{.Title}}<div class="muted small">{{.Kind}} · priority {{conf .Priority}}</div></td><td><span class="conf"><span class="conf-bar"><i style="width:{{pct .Progress}}"></i></span></span></td></tr>{{end}}</tbody></table>{{else}}<p class="muted small">No active goals.</p>{{end}}
|
||||
</div>
|
||||
</section>
|
||||
<section class="grid cols-2">
|
||||
<div class="card">
|
||||
<h2>Important beliefs <span class="muted small"><a href="/minds/{{$m.Slug}}/beliefs">{{.Data.BeliefStats.Total}} total</a></span></h2>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
{{define "content"}}
|
||||
{{$m := .Data.Mind}}{{$c := .Data.Calibration}}
|
||||
<div class="pagehead mindhead"><div><h1>{{$m.DisplayName}} <span class="muted small">predictions</span></h1><p class="muted">Explicit expectations with windows and checkable criteria. Misses become prediction errors that lower the linked belief.</p></div></div>
|
||||
{{template "mind_tabs" (dict "M" $m "Active" "predictions")}}
|
||||
<section class="grid cols-4">
|
||||
<div class="card stat"><div class="stat-label">Open</div><div class="stat-value">{{index .Data.Stats "open"}}</div></div>
|
||||
<div class="card stat"><div class="stat-label">Accuracy</div><div class="stat-value">{{if $c.Total}}{{pct $c.Accuracy}}{{else}}—{{end}}</div><div class="muted small">{{$c.Confirmed}} confirmed · {{$c.Refuted}} refuted · {{$c.Expired}} expired (90 days)</div></div>
|
||||
<div class="card stat"><div class="stat-label">Brier score</div><div class="stat-value">{{if $c.Total}}{{conf $c.Brier}}{{else}}—{{end}}</div><div class="muted small">0 is perfect, 0.25 is chance</div></div>
|
||||
<div class="card stat"><div class="stat-label">Calibration</div><div class="stat-value small">{{if $c.Overconfident}}<span class="warn">overconfident</span>{{else if $c.Total}}<span class="ok">in range</span>{{else}}—{{end}}</div></div>
|
||||
</section>
|
||||
<section class="grid cols-3-1">
|
||||
<div class="stack">
|
||||
<div class="card">
|
||||
<h2>Calibration by stated confidence</h2>
|
||||
{{if $c.Total}}<table class="table compact"><thead><tr><th>Stated</th><th class="num">n</th><th class="num">Mean stated</th><th class="num">Actual hit rate</th><th>Gap</th></tr></thead>
|
||||
<tbody>{{range $c.Buckets}}<tr><td>{{.Label}}</td><td class="num">{{.Count}}</td><td class="num">{{if .Count}}{{conf .MeanConf}}{{else}}—{{end}}</td><td class="num">{{if .Count}}{{conf .HitRate}}{{else}}—{{end}}</td>
|
||||
<td>{{if .Count}}<div class="cal"><span class="cal-bar" style="width:{{pct .MeanConf}}"></span><span class="cal-hit" style="left:{{pct .HitRate}}"></span></div>{{end}}</td></tr>{{end}}</tbody></table>
|
||||
<p class="muted small">Bar: mean stated confidence. Marker: fraction that came true. A well-calibrated mind has the marker at the end of the bar.</p>
|
||||
{{else}}<p class="muted">No resolved predictions yet.</p>{{end}}
|
||||
</div>
|
||||
<div class="card">
|
||||
<h2>Predictions</h2>
|
||||
{{if .Data.Predictions}}<table class="table compact"><thead><tr><th>Status</th><th>Statement</th><th class="num">Conf</th><th>Window</th><th>Criteria</th><th></th></tr></thead>
|
||||
<tbody>{{range .Data.Predictions}}<tr>
|
||||
<td><span class="badge ps-{{.Status}}">{{.Status}}</span></td>
|
||||
<td>{{.Statement}}<div class="small muted">{{.Source}}{{if .BeliefID}} · <a href="/minds/{{$m.Slug}}/beliefs/{{.BeliefID}}">belief</a>{{end}}{{if .OriginEvent}} · <a href="/events/{{.OriginEvent}}">origin</a>{{end}}{{if .Resolution}} · {{payloadField .Resolution "reason"}}{{end}}</div></td>
|
||||
<td class="num">{{conf .Confidence}}</td><td class="small nowrap">{{.WindowStart.Local.Format "01-02 15:04"}} → {{.WindowEnd.Local.Format "01-02 15:04"}}</td>
|
||||
<td class="small mono">{{.Criteria.Kind}}{{if .Criteria.Terms}} {{.Criteria.Expect}}: {{range .Criteria.Terms}}{{.}} {{end}}{{end}}</td>
|
||||
<td class="nowrap">{{if and (eq .Status "open") ($.Principal.User.Role.AtLeast "operator")}}<form method="post" action="/minds/{{$m.Slug}}/predictions/{{.ID}}/resolve" class="inline"><input type="hidden" name="_csrf" value="{{$.CSRF}}"><button class="btn small" name="outcome" value="confirmed">✓</button><button class="btn small" name="outcome" value="refuted">✗</button><button class="btn small btn-ghost" name="outcome" value="withdrawn">withdraw</button></form>{{end}}</td>
|
||||
</tr>{{end}}</tbody></table>
|
||||
{{else}}<p class="muted">None yet. Causal hypotheses create predictions automatically; operators can add them here.</p>{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{if .Principal.User.Role.AtLeast "operator"}}
|
||||
<div class="card">
|
||||
<h2>New prediction</h2>
|
||||
<form method="post" action="/minds/{{$m.Slug}}/predictions" class="stack">
|
||||
<input type="hidden" name="_csrf" value="{{.CSRF}}">
|
||||
<label>Statement <textarea name="statement" rows="2" required maxlength="1000" placeholder="server-01 will remain healthy for 24 hours"></textarea></label>
|
||||
<label class="slider"><span>Confidence</span><input type="range" name="confidence" min="0.05" max="0.95" step="0.05" value="0.7" data-out><output>0.7</output></label>
|
||||
<label>Window (hours) <input type="number" name="hours" min="1" max="8760" value="24"></label>
|
||||
<label>Criteria <select name="kind"><option value="event_match">event terms</option><option value="operator">operator judges</option></select></label>
|
||||
<div class="grid cols-2 tight"><label>Expect <select name="expect"><option value="occurs">terms occur</option><option value="absent">terms stay absent</option></select></label>
|
||||
<label>Terms <input name="terms" placeholder="server-01, fail" maxlength="200"></label></div>
|
||||
<div><button class="btn btn-primary" type="submit">Predict</button></div>
|
||||
</form>
|
||||
</div>
|
||||
{{end}}
|
||||
</section>
|
||||
{{end}}
|
||||
@@ -71,7 +71,8 @@
|
||||
<h2>Attention weights <span class="muted small">per feature; score = Σ value×weight / Σ weight</span></h2>
|
||||
<form method="post" action="/minds/{{$m.Slug}}/workspace/weights" class="stack">
|
||||
<input type="hidden" name="_csrf" value="{{.CSRF}}">
|
||||
<div class="weights">{{range $f := features}}<label>{{title $f}} <input type="number" name="w.{{$f}}" min="0" max="5" step="0.1" value="{{index $m.Config.AttentionWeights $f}}"></label>{{end}}</div>
|
||||
<div class="weights">{{range $f := features}}<label>{{title $f}} <input type="number" name="w.{{$f}}" min="0" max="5" step="0.1" value="{{index $m.Config.AttentionWeights $f}}">{{with $st.EffectiveWeights}}<span class="muted small">effective {{conf (index . $f)}}</span>{{end}}</label>{{end}}</div>
|
||||
<p class="muted small">Effective weights are the configured weights after drive and affect modulation in the last cycle.</p>
|
||||
<div class="grid cols-2 tight">
|
||||
<label>Workspace capacity <input type="number" name="workspace_capacity" min="1" max="32" value="{{$m.Config.WorkspaceCapacity}}"></label>
|
||||
<label>Working memory capacity <input type="number" name="working_memory_capacity" min="1" max="64" value="{{$m.Config.WorkingMemoryCapacity}}"></label>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{{define "state_panel"}}{{$s := .State}}{{$m := .Mind}}
|
||||
<div class="card">
|
||||
<h2>Cognitive state <span class="muted small">scalars updated by rule; not a claim of feeling</span></h2>
|
||||
<div class="grid cols-2 tight">
|
||||
<div><h3>Affect</h3>
|
||||
{{range $k, $v := (affectMap $s.Affect)}}<div class="dim"><span class="dim-name">{{title $k}}</span><span class="dim-bar {{if eq $k "valence"}}bipolar{{end}}"><i style="{{if eq $k "valence"}}left:{{pct (valencePos $v)}};width:{{pct (valenceWidth $v)}}{{else}}width:{{pct $v}}{{end}}"></i></span><span class="dim-val">{{conf $v}}</span></div>{{end}}
|
||||
</div>
|
||||
<div><h3>Drives</h3>
|
||||
{{range $s.Drives}}<div class="dim"><span class="dim-name">{{title .Name}}</span><span class="dim-bar"><i style="width:{{pct .Value}}"></i></span><span class="dim-val">{{conf .Value}} <span class="muted">[{{conf .Min}}–{{conf .Max}}]</span></span></div>{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{if .CanOperate}}<form method="post" action="/minds/{{$m.Slug}}/state/reset" class="row"><input type="hidden" name="_csrf" value="{{.CSRF}}"><button class="btn small" data-confirm="Reset drives and affect to their baselines?">Reset transient state</button></form>{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user