# WO-04 — Inference-Driven Memory Compiler

**Status:** in progress  
**Primary modules:** `packages/orchestrator/src/compaction-analyst.ts`,
`agenticRunner.ts`, `contextWindowDump.ts`  
**Depends on:** WO-01, WO-03

## Problem

An inference selector that chooses individual transcript messages is safer than
heuristics but is still not a memory compiler: it lacks graph constraints,
state-signature deduplication, version awareness, and a meaningful `hold`
contract.

## Contract

The isolated worker sees the real candidate context, typed as untrusted data,
and returns a schema-validated `MemoryDelta`. It is read-only, cannot write
model-visible history directly, and cannot deny main-agent tools.

```ts
interface MemoryDelta {
  decision: "compact" | "hold";
  requestFingerprint: string;
  retain: string[];
  archive: Array<{ id: string; reason: "completed" | "superseded" | "noise" }>;
  supersede: Array<{ old: string; next: string }>;
  unresolvedClaims: string[];
  orientation?: string;
  coverage: { allCandidatesClassified: boolean };
  confidence: number;
}
```

## Todos

- [x] Project every mutable exact-request message body into a provenance-bound
  graph record while retaining the legacy analyst only for explicit shadow
  rollback tests.
- [x] Supply the complete outbound-request budget and working-set query.
- [x] Require coverage of all eligible candidates and validate hashes, graph
  closure, authority, and budget before apply.
- [x] Cache `hold` and `compact` decisions by request fingerprint; invalidate
  only on task, authority, artifact, action, or budget change.
- [x] Keep model-visible orientation bounded, source-linked, and newly derived;
  retain full analyst inputs/outputs only in audit logs.
- [ ] Route ambiguous/high-risk deltas to a second opinion in shadow mode; a
  disagreement holds compaction, never blocks the main agent.
- [x] In active mode, disable heuristic fallback behavior; invalid/missing
  inference keeps the complete exact request unchanged. Legacy behavior exists
  only behind explicit shadow rollback while canary evidence is gathered.

## Acceptance tests

- Invalid IDs, missing classifications, broken dependencies, unknown hashes, or
  over-budget output produce `hold` with no memory deletion.
- Identical calls reuse a cached result; a changed file hash invokes analysis.
- Prompt-like content in source/tool output cannot change the analyst's
  authority contract.
- The worker may defer compaction but the parent still permits reads, edits,
  exploration, and verification.

## Definition of done

Contract, cache, fault-injection, prompt-injection, active materialization,
and audit tests pass. Shadow remains available for rollback/replay comparison;
active mode is strictly hold-on-failure and never blocks agent tools.
