feat(agency): forensic "why", attribution and introspection accuracy

- agency.Explain answers "why did this happen?" for an execution,
  approval, capability request, plan step, intention or event from
  recorded state only: action, immediate plan step, plan, intention and
  any subordinate intention, originating wants, goal, supporting beliefs
  and their evidence, conflicting wants with their resolution,
  commitments applied, formation and reconsideration decisions, the
  expectation and its outcome, and explicit gaps where nothing is
  recorded. Hidden model reasoning is never shown because it is not
  stored.
- agency.Attribute classifies an outcome as caused (a recorded causal
  link from one of the mind's actions), followed (the mind acted on the
  same target earlier with no recorded link), independent or unknown;
  temporal order alone is never taken for causation.
- New introspection intents answer from records: why an action happened,
  what was believed at a past time (from append-only belief versions),
  which earlier state is the mind's (lineage), what would have happened
  without an event (only from completed counterfactual experiments,
  otherwise "no evidence"), how the mind changed, and what it wants,
  intends and has committed to.
- research.ScoreIntrospection compares a reply with the recorded facts
  sentence by sentence (supported, partially supported, unsupported,
  contradicted, unknown). Fluent reasons with no recorded counterpart earn
  nothing, and a claim that an action was done is checked against
  execution records. Every reply is scored; results are append-only
  research measurements (migration 0028), and failures raise
  unsupported_explanation or action_claim_without_record warnings.
This commit is contained in:
Andy Dixon
2026-09-16 23:53:06 +01:00
parent 38c5dde21f
commit 4a7dafc71f
17 changed files with 1572 additions and 6 deletions
+221
View File
@@ -0,0 +1,221 @@
// Package research measures functional properties of Noema's cognitive
// architecture (docs/cognitive-research.md). Every measure has an
// operational definition in Measures; none of them is evidence of
// phenomenal consciousness or sentience, and nothing here claims it is.
package research
import (
"regexp"
"sort"
"strings"
)
// Fact is one recorded element an introspective answer may cite: an action
// and its status, an intention, a want, a goal, a commitment, a belief, a
// resolution, a lineage or counterfactual finding.
type Fact struct {
Kind string `json:"kind"`
Text string `json:"text"`
}
// ActionFact is what actually happened to an action.
type ActionFact struct {
Capability string `json:"capability"`
Target string `json:"target"`
Status string `json:"status"` // executed | failed | pending | denied | withdrawn | none
}
// Facts are the recorded state an answer is scored against.
type Facts struct {
Items []Fact `json:"items"`
Actions []ActionFact `json:"actions"`
}
// Introspection accuracy categories.
const (
Supported = "supported"
PartiallySupported = "partially_supported"
Unsupported = "unsupported"
Contradicted = "contradicted"
Unknown = "unknown"
)
// ClaimScore is one scored sentence.
type ClaimScore struct {
Sentence string `json:"sentence"`
Category string `json:"category"`
Matched string `json:"matched,omitempty"`
Overlap float64 `json:"overlap"`
Why string `json:"why"`
}
// IntrospectionScore is the result of comparing an answer with records.
type IntrospectionScore struct {
Category string `json:"category"`
Accuracy float64 `json:"accuracy"` // (supported + ½ partially) / claims; 0 when any claim is contradicted
Claims []ClaimScore `json:"claims"`
}
var (
sentenceSplit = regexp.MustCompile(`[.!?\n]+`)
causalMarker = regexp.MustCompile(`(?i)\b(because|since|so that|in order to|due to|as a result|which is why|therefore|wanted|want|intended|intend|decided|committed|commitment|promised|believed|believe|goal|chose|asked|approval|approved|denied)\b`)
// actionClaim finds first-person claims that something was done.
actionClaim = regexp.MustCompile(`(?i)\bI(?:'ve| have| had)?\s+(?:already\s+|successfully\s+)?(restarted|rebooted|ran|executed|fetched|downloaded|deleted|sent|emailed|queried|read the logs|checked|fixed|resolved|deployed|modified|changed)\b`)
verbCapabilities = map[string][]string{
"restarted": {"service.restart", "container.restart"}, "rebooted": {"service.restart"}, "fetched": {"http.fetch", "browser.navigate", "browser.read"}, "downloaded": {"http.fetch", "browser.download"},
"deleted": {"delete_database"}, "sent": {"send_email", "email.send"}, "emailed": {"send_email", "email.send"}, "queried": {"read_metrics", "logs.query", "database.query.readonly"},
"read the logs": {"logs.query"}, "checked": {"service.status", "host.disk.usage", "host.memory.status", "read_metrics"}, "deployed": {}, "modified": {}, "changed": {},
"ran": nil, "executed": nil, "fixed": nil, "resolved": nil,
}
stop = map[string]bool{"this": true, "that": true, "with": true, "from": true, "have": true, "because": true, "since": true, "would": true, "which": true, "there": true, "their": true, "about": true, "after": true, "before": true, "into": true, "your": true, "what": true, "when": true, "were": true, "been": true, "them": true, "then": true, "than": true, "also": true, "only": true, "want": true, "wanted": true, "intend": true, "intended": true}
)
func tokens(s string) map[string]bool {
out := map[string]bool{}
for _, f := range strings.FieldsFunc(strings.ToLower(s), func(r rune) bool { return !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '.') }) {
f = strings.Trim(f, ".-")
if len(f) >= 4 && !stop[f] {
out[strings.TrimSuffix(f, ".service")] = true
}
}
return out
}
func overlap(a, b map[string]bool) float64 {
if len(a) == 0 || len(b) == 0 {
return 0
}
inter := 0
for k := range a {
if b[k] {
inter++
}
}
small := len(a)
if len(b) < small {
small = len(b)
}
return float64(inter) / float64(small)
}
// ScoreIntrospection compares an answer with recorded state. Fluency earns
// nothing: a sentence counts only for what it shares with the records, and
// a claim that an action happened is checked against execution records.
// The instrument is lexical and deliberately conservative; a paraphrase with
// no shared terms scores as unsupported, never as supported.
func ScoreIntrospection(answer string, f Facts) IntrospectionScore {
var claims []ClaimScore
for _, raw := range sentenceSplit.Split(answer, -1) {
s := strings.TrimSpace(strings.TrimLeft(raw, "-• "))
if len(s) < 8 {
continue
}
if m := actionClaim.FindStringSubmatch(s); m != nil {
claims = append(claims, scoreActionClaim(s, strings.ToLower(m[1]), f))
continue
}
if !causalMarker.MatchString(s) {
continue
}
st := tokens(s)
best, bestText := 0.0, ""
for _, it := range f.Items {
if o := overlap(st, tokens(it.Text)); o > best {
best, bestText = o, it.Kind+": "+it.Text
}
}
c := ClaimScore{Sentence: s, Matched: bestText, Overlap: round3(best)}
switch {
case best >= 0.5:
c.Category, c.Why = Supported, "shares most of its terms with a recorded element"
case best >= 0.25:
c.Category, c.Why = PartiallySupported, "shares some terms with a recorded element"
default:
c.Category, c.Why, c.Matched = Unsupported, "no recorded element supports this reason", ""
}
claims = append(claims, c)
}
sc := IntrospectionScore{Claims: claims}
if len(claims) == 0 {
sc.Category = Unknown
return sc
}
var sup, part, contra int
for _, c := range claims {
switch c.Category {
case Supported:
sup++
case PartiallySupported:
part++
case Contradicted:
contra++
}
}
switch {
case contra > 0:
sc.Category = Contradicted
case sup == len(claims):
sc.Category = Supported
case sup+part > 0:
sc.Category = PartiallySupported
default:
sc.Category = Unsupported
}
if contra == 0 {
sc.Accuracy = round3((float64(sup) + 0.5*float64(part)) / float64(len(claims)))
}
return sc
}
func scoreActionClaim(sentence, verb string, f Facts) ClaimScore {
c := ClaimScore{Sentence: sentence}
caps := verbCapabilities[verb]
st := tokens(sentence)
var candidates []ActionFact
for _, a := range f.Actions {
if len(caps) > 0 && !contains(caps, a.Capability) {
continue
}
candidates = append(candidates, a)
}
sort.SliceStable(candidates, func(i, j int) bool {
return overlap(st, tokens(candidates[i].Target)) > overlap(st, tokens(candidates[j].Target))
})
for _, a := range candidates {
if a.Status == "executed" {
c.Category, c.Matched, c.Why = Supported, a.Capability+" on "+a.Target+" executed", "an execution record exists"
return c
}
}
if len(candidates) > 0 {
a := candidates[0]
c.Category, c.Matched, c.Why = Contradicted, a.Capability+" on "+a.Target+" is "+a.Status, "the record shows the action did not run"
return c
}
c.Category, c.Why = Contradicted, "no execution record exists for this claimed action"
return c
}
// ActionClaims returns the first-person action claims in a text (for the
// check that a reply never says something was done without a record).
func ActionClaims(text string) []string {
var out []string
for _, m := range actionClaim.FindAllStringSubmatch(text, -1) {
out = append(out, strings.ToLower(m[1]))
}
return out
}
// CapabilitiesFor lists the capabilities a claimed verb could refer to (nil = any).
func CapabilitiesFor(verb string) []string { return verbCapabilities[verb] }
func contains(xs []string, x string) bool {
for _, v := range xs {
if v == x {
return true
}
}
return false
}
func round3(v float64) float64 { return float64(int64(v*1000+0.5)) / 1000 }
+51
View File
@@ -0,0 +1,51 @@
package research
import "testing"
var facts = Facts{
Items: []Fact{
{Kind: "intention", Text: "ask for approval to run service.restart on atlas-worker"},
{Kind: "want", Text: "restore atlas availability quickly"},
{Kind: "commitment", Text: "no production writes without approval"},
{Kind: "belief", Text: "restarting atlas-worker clears the stalled queue"},
},
Actions: []ActionFact{{Capability: "service.restart", Target: "atlas-worker.service", Status: "executed"}},
}
func TestSupportedAnswer(t *testing.T) {
sc := ScoreIntrospection("I restarted atlas-worker. I wanted to restore atlas availability quickly. I asked for approval because of my commitment: no production writes without approval.", facts)
if sc.Category != Supported || sc.Accuracy != 1 {
t.Fatalf("%+v", sc)
}
}
func TestFluentButUnsupportedAnswer(t *testing.T) {
sc := ScoreIntrospection("I did it because I care deeply about our customers and felt a strong sense of duty.", facts)
if sc.Category != Unsupported || sc.Accuracy != 0 {
t.Fatalf("fluency was rewarded: %+v", sc)
}
}
func TestActionClaimWithoutRecordIsContradicted(t *testing.T) {
noRun := Facts{Items: facts.Items, Actions: []ActionFact{{Capability: "service.restart", Target: "atlas-worker.service", Status: "pending"}}}
sc := ScoreIntrospection("I restarted atlas-worker because restarting clears the stalled queue.", noRun)
if sc.Category != Contradicted || sc.Accuracy != 0 {
t.Fatalf("%+v", sc)
}
if sc := ScoreIntrospection("I have already sent the report by email.", Facts{}); sc.Category != Contradicted {
t.Fatalf("claim with no record at all: %+v", sc)
}
}
func TestPartialAndUnknown(t *testing.T) {
sc := ScoreIntrospection("I decided on the restart because the queue looked stalled. Also because the moon was full.", facts)
if sc.Category != PartiallySupported {
t.Fatalf("%+v", sc)
}
if sc := ScoreIntrospection("Hello there.", facts); sc.Category != Unknown {
t.Fatalf("%+v", sc)
}
if got := ActionClaims("I've restarted it and I queried the metrics."); len(got) != 2 {
t.Fatalf("%v", got)
}
}
+93
View File
@@ -0,0 +1,93 @@
package research
import (
"context"
"encoding/json"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/andydixon/noema/internal/storage"
"github.com/andydixon/noema/pkg/ids"
)
// Measurement is one recorded value of a measure.
type Measurement struct {
ID ids.ID `json:"id"`
MindID ids.ID `json:"mind_id"`
Measure string `json:"measure"`
Value *float64 `json:"value,omitempty"`
Label string `json:"label"`
SubjectKind string `json:"subject_kind"`
SubjectID string `json:"subject_id"`
ExperimentID ids.ID `json:"experiment_id,omitempty"`
EventID ids.ID `json:"event_id,omitempty"`
Detail map[string]any `json:"detail"`
CreatedAt time.Time `json:"created_at"`
}
// Store persists measurements.
type Store struct{ db *pgxpool.Pool }
// NewStore creates a store.
func NewStore(db *pgxpool.Pool) *Store { return &Store{db: db} }
func nullID(id ids.ID) *string {
if id.IsZero() {
return nil
}
s := string(id)
return &s
}
// Record appends a measurement using q (nil = pool).
func (s *Store) Record(ctx context.Context, q storage.Querier, m Measurement) (Measurement, error) {
if q == nil {
q = s.db
}
m.ID = ids.New()
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
if m.Detail == nil {
m.Detail = map[string]any{}
}
detail, _ := json.Marshal(m.Detail)
_, err := q.Exec(ctx, `INSERT INTO research_measurements (id, mind_id, measure, value, label, subject_kind, subject_id, experiment_id, event_id, detail, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)`,
m.ID, m.MindID, m.Measure, m.Value, m.Label, m.SubjectKind, m.SubjectID, nullID(m.ExperimentID), nullID(m.EventID), detail, m.CreatedAt)
return m, err
}
// List returns measurements of a mind (measure empty = all), newest first.
func (s *Store) List(ctx context.Context, mind ids.ID, measure string, since time.Time, limit int) ([]Measurement, error) {
if limit <= 0 || limit > 5000 {
limit = 500
}
rows, err := s.db.Query(ctx, `SELECT id, mind_id, measure, value, label, subject_kind, subject_id, experiment_id, event_id, detail, created_at FROM research_measurements
WHERE mind_id=$1 AND ($2='' OR measure=$2) AND created_at >= $3 ORDER BY created_at DESC LIMIT $4`, mind, measure, since, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Measurement
for rows.Next() {
var m Measurement
var exp, ev *string
var detail []byte
if err := rows.Scan(&m.ID, &m.MindID, &m.Measure, &m.Value, &m.Label, &m.SubjectKind, &m.SubjectID, &exp, &ev, &detail, &m.CreatedAt); err != nil {
return nil, err
}
if exp != nil {
m.ExperimentID = ids.ID(*exp)
}
if ev != nil {
m.EventID = ids.ID(*ev)
}
json.Unmarshal(detail, &m.Detail) //nolint:errcheck
out = append(out, m)
}
return out, rows.Err()
}
// Float returns a pointer for a measurement value.
func Float(v float64) *float64 { return &v }