# WO-01 — Exact Request and Budget Compiler

**Status:** in progress  
**Primary modules:** `packages/orchestrator/src/agenticRunner.ts`,
`contextWindowDump.ts`, `context-compiler.ts`, `context-fabric.ts`  
**Depends on:** none

## Problem

The old compaction path estimated only durable history. The actual outgoing
request subsequently appended system text, tool schemas, dynamic controller
frames, active steering, evidence, and output reservation. A history that
appeared safe could therefore send an overfull or low-signal request; the
inverse caused needless compaction.

## Contract

Create one pure `compileOutboundRequestBudget()` path that receives the exact
request payload and returns a reproducible ledger:

```ts
interface OutboundRequestBudget {
  requestFingerprint: string;
  modelContextTokens: number;
  stableSystemTokens: number;
  toolSchemaTokens: number;
  authorityTokens: number;
  taskAndWorkingSetTokens: number;
  evidenceTokens: number;
  historyTokens: number;
  outputReservationTokens: number;
  safetyMarginTokens: number;
  totalInputTokens: number;
  projectedTotalTokens: number;
  freeTokens: number;
  freeRatio: number;
  compactionEligible: boolean;
}
```

`compactionEligible` is false while `freeRatio >= 0.40`. It must be based on
the serialized request, with a conservative estimator used only where the
backend tokenizer is unavailable. The fingerprint covers all materialized
segments and their hashes, not timestamps.

## Todos

- [ ] Extract request assembly into a side-effect-free intermediate request
  model, preserving current request behavior.
- [x] Count each rendered segment separately, including tool schemas and output
  reservation.
- [ ] Add a tokenizer adapter with deterministic conservative fallback and
  expose estimator provenance in dumps.
- [ ] Make automatic and manual compaction consume this budget only.
- [x] Use request fingerprints to suppress duplicate analysis while all inputs
  are identical.
- [ ] Extend context-window dump and TUI compaction box with the segment ledger,
  projected headroom, threshold decision, and fingerprint.
- [ ] Remove duplicated post-assembly accounting paths after parity tests pass.

## Acceptance tests

- A large tool schema or dynamic frame changes eligibility even if `messages[]`
  does not change.
- At 40.0% or more free real context, both auto and manual compaction hold.
- At 39.9% free context, the compiler can request compaction but does not alter
  agent/tool permissions.
- Two byte-equivalent requests produce the same fingerprint and one analyst
  invocation.
- A changed active steering slot, file hash, tool schema, or output reservation
  produces a new fingerprint.
- The dump totals reconcile to the exact request estimate within documented
  tokenizer rounding.

## Definition of done

Focused unit and integration tests pass, the orchestrator typecheck passes, and
one fixture demonstrates a previous history-only false decision corrected by
final-request accounting.
