feat: implement global workspace, working memory and the cognitive cycle

Phase 4 (ADR-0001). internal/attention scores thought candidates as a
normalised weighted sum over ten named features with a per-term
breakdown, ranked by (score, id) so ordering is total and replayable.
internal/workspace holds the capacity-limited workspace (refresh by
subject, decayed competition, explained admit/reject outcomes) and the
decaying working memory.

internal/brain runs one runtime per active mind: debounced wake-ups from
the event bus (own output never re-triggers), a settle window so
concurrently committing events are not skipped, bounded concurrent
Observe calls with per-process isolation and timeouts, deterministic
scoring, a loop detector that suppresses recurring thoughts and raises a
metacognition warning, executive dispatch by thought kind, and one
transaction per cycle whose events are published only after commit.
Every candidate, selection, attention shift and broadcast is an event
with provenance back to the triggering observation. Operators can run a
cycle, pause, resume, reset transient state and change attention
weights live; the manager reconciles runtimes with mind status.

cognition/attend is the first process: deterministic candidates from
external events using an urgency lexicon, novelty against context and
trust-based operator priority.
This commit is contained in:
Andy Dixon
2026-06-18 22:54:07 +01:00
parent 85d769b245
commit e9c1b25c80
28 changed files with 2289 additions and 12 deletions
+57
View File
@@ -0,0 +1,57 @@
package brain
// loopDetector spots candidates that keep being admitted without the world
// changing (T20). It keeps a ring of recent admitted fingerprints; when one
// recurs threshold times within the window it is suppressed and a
// metacognition warning is raised once per streak.
type loopDetector struct {
window int
threshold int
ring []string
warned map[string]bool
}
func newLoopDetector(window, threshold int) *loopDetector {
return &loopDetector{window: window, threshold: threshold, warned: map[string]bool{}}
}
func fingerprint(c Candidate) string { return c.Process + "|" + c.Kind + "|" + c.Subject }
// count returns how many times fp appears in the window.
func (l *loopDetector) count(fp string) int {
n := 0
for _, x := range l.ring {
if x == fp {
n++
}
}
return n
}
// looping reports whether admitting fp again would exceed the threshold.
func (l *loopDetector) looping(fp string) bool { return l.count(fp) >= l.threshold }
// record notes an admission.
func (l *loopDetector) record(fp string) {
l.ring = append(l.ring, fp)
if len(l.ring) > l.window {
l.ring = l.ring[len(l.ring)-l.window:]
}
if l.count(fp) < l.threshold {
delete(l.warned, fp)
}
}
// shouldWarn returns true the first time a fingerprint is suppressed in a streak.
func (l *loopDetector) shouldWarn(fp string) bool {
if l.warned[fp] {
return false
}
l.warned[fp] = true
return true
}
// snapshot exports the ring for persistence.
func (l *loopDetector) snapshot() []string { return append([]string{}, l.ring...) }
func (l *loopDetector) restore(ring []string) { l.ring = append([]string{}, ring...) }