# ADR-076: UOK Memory Integration for Autonomous Learning

**Status:** Accepted  
**Date:** 2026-05-07  
**Supersedes:** None  
**Related:** ADR-0075 (UOK Gate Architecture), ADR-008 (SF Tools Over MCP)

## Decision

SF's autonomous dispatch and UOK kernel integrate with the existing SQLite-backed memory system for pattern learning and context-aware decision-making. Memory operations use fire-and-forget async to never block dispatch.

## Problem

SF's dispatch and UOK execution had no feedback loop for learning. Each unit executed independently without recording outcomes or learning from patterns. This prevented:
- Learning which unit types succeed or fail
- Understanding task dependencies
- Improving dispatch decisions over time
- Detecting recurring issues (gotchas)

## Solution

### Three Integration Points

**Phase 1: Unit Outcome Recording**
- `recordUnitOutcomeInMemory(unit, status, result)` in unit-runtime.js
- Records every unit completion as a learned pattern
- Success: 0.9 confidence (strong signal)
- Failure: 0.5 confidence (weaker signal, more variability)
- Fire-and-forget async; never blocks execution

**Phase 2: Dispatch Ranking Enhancement**
- `enhanceUnitRankingWithMemory(units, baseScores)` in auto-dispatch.js
- Queries memory for similar unit types
- Boosts matching candidates by up to 15% of pattern confidence
- Deterministic embeddings ensure consistent ranking
- Gracefully degrades if DB unavailable

**Phase 3: Gate Context Enrichment**
- `enrichGateResultWithMemory(gateResult, gateId)` in gate-runner.js
- Enriches gate failures with historical pattern diagnostics
- Pure diagnostic; never changes gate pass/fail decisions
- Helps operators understand recurring issues

### Architecture

```
UOK Kernel (executes units)
  ↓ records outcomes via
Unit Runtime (recordUnitOutcomeInMemory)
  ↓ stores patterns in
Memory System (SQLite, Node 26 native)
  ↓ queried by
Dispatch (enhanceUnitRankingWithMemory)
  ↓ boosts scores for matching patterns
  ↓ selected unit executes
  ↓ outcome recorded → feedback loop
```

### Memory Categories

- `pattern` — Unit type completion patterns (success/failure)
- `gotcha` — Recurring issues discovered
- `architecture` — Design decisions
- `convention` — Coding standards
- `environment` — Configuration, setup
- `preference` — Optimization decisions

## Rationale

1. **Maximize kernel + DB** — Single UOK kernel, memory as DB layer, no multiplication
2. **Fire-and-forget async** — Memory never blocks critical path; safe degradation
3. **Existing infrastructure** — SF already has 10 memory modules; no duplication
4. **Node 26 native SQLite** — No external dependencies; efficient storage
5. **Confidence scoring** — Learned patterns inform but don't dominate decisions
6. **Pure diagnostic gates** — Gate failures become learning opportunities, not gate logic change

## Consequences

### Benefits
- Autonomous pattern discovery
- Better dispatch ranking over time
- Recurring issues visible to operators
- Fire-and-forget prevents latency impact
- Graceful degradation if DB unavailable
- No external service dependencies

### Drawbacks
- Memory DB growth over time (mitigated by decay/supersession)
- Embeddings require compute (mitigated by deterministic hashing)
- Learning only visible over multiple runs

## Implementation Details

### Confidence Strategy
- **Success patterns:** 0.9 confidence (strong signal)
- **Failure patterns:** 0.5 confidence (weaker, more variability)
- **Memory boost:** Max 15% of pattern confidence (conservative to avoid over-fitting)
- **Threshold:** No minimum; filtering happens at query time via confidence scoring

### Graceful Degradation
All memory operations fail silently without blocking:
- DB unavailable → dispatch continues without boost
- Memory lookup fails → continue with base scores
- Embedding computation fails → use default embedding
- Gate enrichment fails → return original result

### Vector Strategy
- 128-dimensional deterministic embeddings
- Hash-based (character codes + sine waves)
- Normalized to unit length (cosine similarity)
- Recomputed per dispatch (acceptable latency <10ms)

## Validation

**Phase 1 Tests:** 18 test cases (all passing ✅)
- Record success/failure patterns
- Confidence scoring (0.9 vs 0.5)
- Graceful DB degradation
- Category assignment
- Unit type extraction

**Phase 2 Tests:** 21 test cases (syntax correct, require Node 26.1)
- Memory-enhanced ranking
- Embedding computation
- Score boosting formula
- Multiple dispatch candidates
- Fallback chains

**Phase 3 Tests:** 17 test cases (all passing ✅)
- Gate enrichment with memory context
- Diagnostic-only (never changes gate decision)
- Similar failure detection
- Property preservation
- Graceful degradation

**Total:** 56 new tests validating integration

## Alternatives Considered

1. **Vector database (e.g., Pinecone)** — Rejected: adds external service, SF is client only
2. **New memory kernel** — Rejected: SF has 10 complete memory modules already
3. **Block on memory operations** — Rejected: fire-and-forget is safer for critical path
4. **Complex ML model** — Rejected: simple confidence scoring sufficient for learning signal

## Related Decisions

- **ADR-0000:** Purpose-to-Software Compiler (SF is autonomous learner)
- **ADR-0075:** UOK Gate Architecture (gates are pure functions, not learning)
- **ADR-008:** SF Tools Over MCP (memory is internal, not exposed as service)

## Future Work

1. **Integrated dispatch rules** — Use `enhanceUnitRankingWithMemory()` in actual dispatch rules
2. **Memory telemetry** — Track which patterns influence decisions
3. **Pattern clustering** — Auto-group similar memories
4. **Distributed learning** — Share patterns across SF instances
5. **Performance tuning** — Cache embeddings if reused repeatedly

## Documentation

- `docs/dev/MEMORY-SYSTEM-ARCHITECTURE.md` — Full architecture reference
- `docs/dev/MEMORY-SYSTEM-INTEGRATION-GUIDE.md` — Quick-start guide for developers
- `src/resources/extensions/sf/uok/unit-runtime.js` — Phase 1 implementation
- `src/resources/extensions/sf/auto-dispatch.js` — Phase 2 implementation
- `src/resources/extensions/sf/uok/gate-runner.js` — Phase 3 implementation
