feat: add metacognition, reflection, consolidation, narrative and notifications
Phase 10. Metacognition is a cognitive process with deterministic self-checks (insufficient evidence, single-source reliance, model output without evidence, repeated failed hypotheses, stale contradictions, calibration overconfidence); its warnings compete for attention and, when admitted, add counter-evidence or open an investigation goal. Reflection reviews the period since the last run (prediction errors, rejected beliefs, corrections, status changes, completed goals, contradictions, calibration), turns repeated failures into procedural lessons written once, and records a reflection event; a model may only word the prose within the mind's budget. Consolidation clusters unconsolidated episodes by shared stemmed terms, creates a belief whose confidence is computed from the episodes as evidence and a semantic memory linked to each episode by derived_from, and records jobs with inputs and outputs. Wording may come from the consolidate prompt within a per-run model budget; the job says which. Operators can run, pause and reject; originals are never touched. The narrative hook keeps a five-part first-person narrative built from state and stored with narrative_updated events. The notification hook applies explicit thresholded rules, aggregates repeats within a cooldown and records suppressions with reasons. A scheduled job runs consolidation and reflection per mind, switching the runtime mode while it works. API, Background tab, notifications page with unread badge, overview narrative and warnings, CLI commands, docs/cognition.md.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/consolidation"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
func (u *UI) cognitionRoutes(m *http.ServeMux) {
|
||||
m.Handle("GET /notifications", u.require(auth.RoleObserver, u.notificationsPage))
|
||||
m.Handle("POST /notifications/{id}/read", u.require(auth.RoleObserver, u.notificationRead))
|
||||
m.Handle("POST /notifications/read-all", u.require(auth.RoleObserver, u.notificationsReadAll))
|
||||
m.Handle("GET /minds/{mind}/consolidation", u.require(auth.RoleObserver, u.consolidationPage))
|
||||
m.Handle("POST /minds/{mind}/consolidation/run", u.require(auth.RoleOperator, u.consolidationRun))
|
||||
m.Handle("POST /minds/{mind}/consolidation/reject", u.require(auth.RoleOperator, u.consolidationReject))
|
||||
m.Handle("POST /consolidation/pause", u.require(auth.RoleAdministrator, u.consolidationPause))
|
||||
m.Handle("POST /minds/{mind}/reflections/run", u.require(auth.RoleOperator, u.reflectionRun))
|
||||
}
|
||||
|
||||
func (u *UI) notificationsPage(w http.ResponseWriter, r *http.Request) {
|
||||
list, _ := u.deps.Notifications.List(r.Context(), ids.Nil, r.URL.Query().Get("status"), 200)
|
||||
mindsList, _ := u.deps.Minds.List(r.Context(), true)
|
||||
names := map[ids.ID]string{}
|
||||
for _, m := range mindsList {
|
||||
names[m.ID] = m.DisplayName
|
||||
}
|
||||
u.render(w, r, "notifications", "Notifications", map[string]any{"Items": list, "MindNames": names})
|
||||
}
|
||||
|
||||
func (u *UI) notificationRead(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := ids.Parse(r.PathValue("id"))
|
||||
if err == nil {
|
||||
u.deps.Notifications.MarkRead(r.Context(), ids.Nil, id) //nolint:errcheck
|
||||
}
|
||||
http.Redirect(w, r, "/notifications", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (u *UI) notificationsReadAll(w http.ResponseWriter, r *http.Request) {
|
||||
u.deps.Notifications.MarkAllRead(r.Context(), ids.Nil) //nolint:errcheck
|
||||
u.flash(w, r, "ok", "All notifications marked read.", "/notifications")
|
||||
}
|
||||
|
||||
func (u *UI) consolidationPage(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
jobs, _ := u.deps.Consolidation.Jobs(r.Context(), m.ID, 20)
|
||||
refl, _ := u.deps.Reflection.List(r.Context(), m.ID, 5)
|
||||
u.render(w, r, "consolidation", m.DisplayName+" · Consolidation", map[string]any{"Mind": m, "Jobs": jobs, "Reflections": refl, "Paused": consolidation.IsPaused()})
|
||||
}
|
||||
|
||||
func (u *UI) consolidationRun(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
job, err := u.consolF(r, m.ID)
|
||||
if err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/consolidation")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Consolidation "+job.Status+": "+itoa(job.Produced)+" generalisation(s) from "+itoa(job.Clusters)+" cluster(s).", "/minds/"+m.Slug+"/consolidation")
|
||||
}
|
||||
|
||||
func (u *UI) consolidationReject(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := ids.Parse(r.PostFormValue("memory_id"))
|
||||
if err != nil {
|
||||
u.flash(w, r, "error", "Bad memory id.", "/minds/"+m.Slug+"/consolidation")
|
||||
return
|
||||
}
|
||||
if err := u.deps.Consolidation.Reject(r.Context(), m, id, r.PostFormValue("reason"), username(r)); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/consolidation")
|
||||
return
|
||||
}
|
||||
u.deps.Audit.Log(r.Context(), "consolidation.reject", "memory", id.String(), map[string]any{"reason": r.PostFormValue("reason")})
|
||||
u.flash(w, r, "ok", "Generalisation rejected; episodes untouched.", "/minds/"+m.Slug+"/consolidation")
|
||||
}
|
||||
|
||||
func (u *UI) consolidationPause(w http.ResponseWriter, r *http.Request) {
|
||||
paused := r.PostFormValue("paused") == "1"
|
||||
consolidation.SetPaused(paused)
|
||||
u.deps.Audit.Log(r.Context(), "consolidation.pause", "system", "", map[string]any{"paused": paused})
|
||||
u.flash(w, r, "ok", "Scheduled consolidation "+map[bool]string{true: "paused", false: "resumed"}[paused]+".", r.Referer())
|
||||
}
|
||||
|
||||
func (u *UI) reflectionRun(w http.ResponseWriter, r *http.Request) {
|
||||
m, ok := u.mind(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, err := u.reflF(r, m.ID); err != nil {
|
||||
u.flash(w, r, "error", err.Error(), "/minds/"+m.Slug+"/consolidation")
|
||||
return
|
||||
}
|
||||
u.flash(w, r, "ok", "Reflection recorded.", "/minds/"+m.Slug+"/consolidation")
|
||||
}
|
||||
|
||||
func itoa(n int) string {
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
var b []byte
|
||||
for n > 0 {
|
||||
b = append([]byte{byte('0' + n%10)}, b...)
|
||||
n /= 10
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
evstore "github.com/andydixon/noema/internal/events"
|
||||
"github.com/andydixon/noema/internal/minds"
|
||||
"github.com/andydixon/noema/internal/motivation"
|
||||
"github.com/andydixon/noema/internal/storage"
|
||||
"github.com/andydixon/noema/pkg/events"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
)
|
||||
|
||||
@@ -109,7 +111,9 @@ func (u *UI) mindOverview(w http.ResponseWriter, r *http.Request) {
|
||||
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})
|
||||
narr, _ := u.deps.Narrative.Get(r.Context(), m.ID)
|
||||
warnings, _ := u.deps.Events.Query(r.Context(), evstore.Filter{MindID: m.ID, Types: []events.Type{events.Metacognition}, Since: time.Now().Add(-7 * 24 * time.Hour), Limit: 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, "Narrative": narr, "Warnings": warnings})
|
||||
}
|
||||
|
||||
func (u *UI) mindStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
+29
-13
@@ -21,12 +21,14 @@ import (
|
||||
"github.com/andydixon/noema/internal/attention"
|
||||
"github.com/andydixon/noema/internal/auth"
|
||||
"github.com/andydixon/noema/internal/beliefs"
|
||||
"github.com/andydixon/noema/internal/consolidation"
|
||||
"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/reflection"
|
||||
"github.com/andydixon/noema/internal/version"
|
||||
"github.com/andydixon/noema/pkg/concepts"
|
||||
"github.com/andydixon/noema/pkg/ids"
|
||||
@@ -50,23 +52,27 @@ type UI struct {
|
||||
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)
|
||||
consolF func(r *http.Request, mind ids.ID) (consolidation.Job, error)
|
||||
reflF func(r *http.Request, mind ids.ID) (reflection.Reflection, 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)
|
||||
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)
|
||||
Consolidate func(r *http.Request, mind ids.ID) (consolidation.Job, error)
|
||||
Reflect func(r *http.Request, mind ids.ID) (reflection.Reflection, error)
|
||||
}
|
||||
|
||||
// View is the data passed to every template.
|
||||
@@ -77,6 +83,7 @@ type View struct {
|
||||
CSRF string
|
||||
Flash *Flash
|
||||
Version string
|
||||
Unread int64 // unread notifications, for the navigation badge
|
||||
Data any
|
||||
}
|
||||
|
||||
@@ -95,7 +102,7 @@ func New(deps app.Deps, a Actions) (*UI, error) {
|
||||
deps.Log.Info("ui: serving templates and static files from ./web (development)")
|
||||
}
|
||||
}
|
||||
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}
|
||||
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, consolF: a.Consolidate, reflF: a.Reflect}
|
||||
if err := u.parse(tmplFS); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -148,6 +155,11 @@ func (u *UI) parse(fsys fs.FS) error {
|
||||
"relations": func() []concepts.Relation { return concepts.AllRelations },
|
||||
"llmRoles": func() []llm.RoleInfo { return llm.Roles },
|
||||
"pretty": prettyRaw,
|
||||
"outputs": func(raw json.RawMessage) []consolidation.Output {
|
||||
var out []consolidation.Output
|
||||
json.Unmarshal(raw, &out) //nolint:errcheck
|
||||
return out
|
||||
},
|
||||
"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}
|
||||
},
|
||||
@@ -264,6 +276,9 @@ func (u *UI) render(w http.ResponseWriter, r *http.Request, page, title string,
|
||||
if tok, err := u.deps.Auth.CSRFToken(r.Context(), p.SessionID); err == nil {
|
||||
v.CSRF = tok
|
||||
}
|
||||
if u.deps.Notifications != nil {
|
||||
v.Unread, _ = u.deps.Notifications.CountNew(r.Context(), ids.Nil)
|
||||
}
|
||||
}
|
||||
if c, err := r.Cookie(flashCookie); err == nil && c.Value != "" {
|
||||
if raw, err := base64.RawURLEncoding.DecodeString(c.Value); err == nil {
|
||||
@@ -342,6 +357,7 @@ func (u *UI) Register(m *http.ServeMux) {
|
||||
u.llmRoutes(m)
|
||||
u.conversationRoutes(m)
|
||||
u.motivationRoutes(m)
|
||||
u.cognitionRoutes(m)
|
||||
|
||||
for _, p := range []struct{ path, title, note string }{
|
||||
{"/approvals", "Approvals", "The approval centre arrives with the capability system."},
|
||||
|
||||
Reference in New Issue
Block a user