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.
164 lines
6.3 KiB
Go
164 lines
6.3 KiB
Go
package minds
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// CognitiveConfig holds per-mind tunables consumed by the brain. Defaults are
|
|
// conservative; every field is exposed in the UI in later phases.
|
|
type CognitiveConfig struct {
|
|
WorkspaceCapacity int `json:"workspace_capacity"`
|
|
WorkingMemoryCapacity int `json:"working_memory_capacity"`
|
|
AttentionWeights map[string]float64 `json:"attention_weights"`
|
|
CycleMinInterval Duration `json:"cycle_min_interval"`
|
|
IdleCycleInterval Duration `json:"idle_cycle_interval"`
|
|
MaxCyclesPerMinute int `json:"max_cycles_per_minute"`
|
|
LLMCallsPerCycle int `json:"llm_calls_per_cycle"`
|
|
DailyTokenBudget int64 `json:"daily_token_budget"`
|
|
WorkingMemoryDecay float64 `json:"working_memory_decay"` // activation multiplier per cycle
|
|
LoopDetectWindow int `json:"loop_detect_window"`
|
|
LoopDetectThreshold int `json:"loop_detect_threshold"`
|
|
|
|
// Background cognition (spec §9, §18, §39). Each subsystem has a switch,
|
|
// a schedule and a model-call budget so idle minds cannot spend freely.
|
|
ConsolidationEnabled bool `json:"consolidation_enabled"`
|
|
ConsolidationInterval Duration `json:"consolidation_interval"`
|
|
ConsolidationMinEpisodes int `json:"consolidation_min_episodes"`
|
|
ConsolidationLLMCalls int `json:"consolidation_llm_calls"`
|
|
ReflectionEnabled bool `json:"reflection_enabled"`
|
|
ReflectionInterval Duration `json:"reflection_interval"`
|
|
ReflectionLLMCalls int `json:"reflection_llm_calls"`
|
|
NarrativeLLM bool `json:"narrative_llm"`
|
|
NotifyMinConfidence float64 `json:"notify_min_confidence"`
|
|
NotifyMinSalience float64 `json:"notify_min_salience"`
|
|
NotifyCooldown Duration `json:"notify_cooldown"`
|
|
}
|
|
|
|
// AttentionFeatures is the fixed set of scoring features (spec §6). Weights are
|
|
// per mind; features are computed deterministically by the attention package.
|
|
var AttentionFeatures = []string{"salience", "urgency", "novelty", "affect", "goal_relevance", "consequence", "confidence", "contradiction", "operator_priority", "context_relevance"}
|
|
|
|
// DefaultConfig returns the baseline cognitive configuration.
|
|
func DefaultConfig() CognitiveConfig {
|
|
w := map[string]float64{"salience": 1.0, "urgency": 1.2, "novelty": 0.8, "affect": 0.6, "goal_relevance": 1.1,
|
|
"consequence": 0.9, "confidence": 0.4, "contradiction": 1.0, "operator_priority": 1.5, "context_relevance": 0.9}
|
|
return CognitiveConfig{WorkspaceCapacity: 7, WorkingMemoryCapacity: 9, AttentionWeights: w,
|
|
CycleMinInterval: Duration(500 * time.Millisecond), IdleCycleInterval: Duration(60 * time.Second), MaxCyclesPerMinute: 60,
|
|
LLMCallsPerCycle: 3, DailyTokenBudget: 500_000, WorkingMemoryDecay: 0.85, LoopDetectWindow: 20, LoopDetectThreshold: 5,
|
|
ConsolidationEnabled: true, ConsolidationInterval: Duration(6 * time.Hour), ConsolidationMinEpisodes: 3, ConsolidationLLMCalls: 5,
|
|
ReflectionEnabled: true, ReflectionInterval: Duration(12 * time.Hour), ReflectionLLMCalls: 2, NarrativeLLM: false,
|
|
NotifyMinConfidence: 0.85, NotifyMinSalience: 0.7, NotifyCooldown: Duration(time.Hour)}
|
|
}
|
|
|
|
// Normalise fills gaps from defaults and clamps ranges.
|
|
func (c *CognitiveConfig) Normalise() {
|
|
d := DefaultConfig()
|
|
if c.WorkspaceCapacity < 1 || c.WorkspaceCapacity > 32 {
|
|
c.WorkspaceCapacity = d.WorkspaceCapacity
|
|
}
|
|
if c.WorkingMemoryCapacity < 1 || c.WorkingMemoryCapacity > 64 {
|
|
c.WorkingMemoryCapacity = d.WorkingMemoryCapacity
|
|
}
|
|
if c.AttentionWeights == nil {
|
|
c.AttentionWeights = map[string]float64{}
|
|
}
|
|
for _, f := range AttentionFeatures {
|
|
v, ok := c.AttentionWeights[f]
|
|
if !ok || v < 0 || v > 5 {
|
|
c.AttentionWeights[f] = d.AttentionWeights[f]
|
|
}
|
|
}
|
|
for k := range c.AttentionWeights {
|
|
if !contains(AttentionFeatures, k) {
|
|
delete(c.AttentionWeights, k)
|
|
}
|
|
}
|
|
if c.CycleMinInterval <= 0 {
|
|
c.CycleMinInterval = d.CycleMinInterval
|
|
}
|
|
if c.IdleCycleInterval <= 0 {
|
|
c.IdleCycleInterval = d.IdleCycleInterval
|
|
}
|
|
if c.MaxCyclesPerMinute < 1 || c.MaxCyclesPerMinute > 600 {
|
|
c.MaxCyclesPerMinute = d.MaxCyclesPerMinute
|
|
}
|
|
if c.LLMCallsPerCycle < 0 || c.LLMCallsPerCycle > 20 {
|
|
c.LLMCallsPerCycle = d.LLMCallsPerCycle
|
|
}
|
|
if c.DailyTokenBudget <= 0 {
|
|
c.DailyTokenBudget = d.DailyTokenBudget
|
|
}
|
|
if c.WorkingMemoryDecay <= 0 || c.WorkingMemoryDecay >= 1 {
|
|
c.WorkingMemoryDecay = d.WorkingMemoryDecay
|
|
}
|
|
if c.LoopDetectWindow < 5 {
|
|
c.LoopDetectWindow = d.LoopDetectWindow
|
|
}
|
|
if c.LoopDetectThreshold < 2 {
|
|
c.LoopDetectThreshold = d.LoopDetectThreshold
|
|
}
|
|
if c.ConsolidationInterval < Duration(10*time.Minute) {
|
|
c.ConsolidationInterval = d.ConsolidationInterval
|
|
}
|
|
if c.ConsolidationMinEpisodes < 2 {
|
|
c.ConsolidationMinEpisodes = d.ConsolidationMinEpisodes
|
|
}
|
|
if c.ConsolidationLLMCalls < 0 || c.ConsolidationLLMCalls > 50 {
|
|
c.ConsolidationLLMCalls = d.ConsolidationLLMCalls
|
|
}
|
|
if c.ReflectionInterval < Duration(10*time.Minute) {
|
|
c.ReflectionInterval = d.ReflectionInterval
|
|
}
|
|
if c.ReflectionLLMCalls < 0 || c.ReflectionLLMCalls > 20 {
|
|
c.ReflectionLLMCalls = d.ReflectionLLMCalls
|
|
}
|
|
if c.NotifyMinConfidence <= 0 || c.NotifyMinConfidence > 1 {
|
|
c.NotifyMinConfidence = d.NotifyMinConfidence
|
|
}
|
|
if c.NotifyMinSalience <= 0 || c.NotifyMinSalience > 1 {
|
|
c.NotifyMinSalience = d.NotifyMinSalience
|
|
}
|
|
if c.NotifyCooldown < Duration(time.Minute) {
|
|
c.NotifyCooldown = d.NotifyCooldown
|
|
}
|
|
}
|
|
|
|
func contains(xs []string, x string) bool {
|
|
for _, y := range xs {
|
|
if y == x {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Duration marshals as a Go duration string in JSON ("500ms") for readability.
|
|
type Duration time.Duration
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
return []byte(fmt.Sprintf("%q", time.Duration(d).String())), nil
|
|
}
|
|
|
|
// UnmarshalJSON accepts a duration string or a number of nanoseconds.
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
if len(b) > 0 && b[0] == '"' {
|
|
v, err := time.ParseDuration(string(b[1 : len(b)-1]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*d = Duration(v)
|
|
return nil
|
|
}
|
|
var n int64
|
|
if _, err := fmt.Sscan(string(b), &n); err != nil {
|
|
return err
|
|
}
|
|
*d = Duration(n)
|
|
return nil
|
|
}
|
|
|
|
// D returns the time.Duration.
|
|
func (d Duration) D() time.Duration { return time.Duration(d) }
|