← all docs · north-os9 juil. 2026

Dynamic context cleaner — implementation plan

Status: Planned — no implementation started
Author: Claude (Fable), for Christophe
Date: 2026-07-09
Repo: north-os
Related: docs/superpowers/specs/2026-07-09-dynamic-context-cleaner-design.html (the design this implements — read it first; §-references below point there)


Five phases, each independently shippable and each ending with bun run typecheck + bun run lint green from root, the named tests passing, and a status flip when the phase changes the doc's state (bun docs/superpowers/status.ts set dynamic-context-cleaner in-progress in the Phase 0 commit; … implemented when Phase 4 lands). Phase 0 and Phase 2 change no live behavior (telemetry; dormant read path). The first behavior change is Phase 1's inline cleaning, and its rollout posture is explicit: active by default on deploy, with CONTEXT_CLEANER_DISABLED=1 as the opt-out kill switch (spec §4.5) — there is no "ships off" state.

Phase 0 — Baseline instrumentation (measure before touching anything)

Goal: the before/after in spec §5 is measured, not estimated. No behavior change.

Files: packages/agent-runtime/src/agents/legal-assistant/graph.ts (metric calls), packages/agent-runtime/package.json, small apps/web metrics route.
Tests: extend packages/agent-runtime graph tests — graph-metrics.test.ts: a fake-model turn over an oversized fixture increments the compaction + cap counters; a small turn increments neither (uses resetMetricsForTest).
Shippable: yes — pure telemetry. Run a week of dev + preprod traffic through it to fix the baseline numbers.

Phase 1 — assembleModelView + deterministic inline cleaning (no LLM, no worker, no DB)

Goal: the safe subset of the junk taxonomy (spec §1 categories 2 + 4) cleaned at prompt-build time; the seam for digest substitution exists.

Files: packages/agent-runtime/src/context/{keep-rules,inline-clean,assemble}.ts + graph.ts wiring.
Tests (all in packages/agent-runtime/src/context/):

Shippable: yes — deterministic-only cleaning, kill-switched. Watch Phase 0 metrics for the first real chars-saved.

Phase 2 — context_digest schema + hash-gated substitution (table empty ⇒ no-op)

Goal: the read path is complete and proven safe before any producer exists.

Files: packages/db/src/schema/context_digest.ts + generated migration + packages/db/scripts/provision-roles.ts; packages/chat-runtime/src/run.ts; packages/agent-runtime/src/context/assemble.ts.
Tests:

Shippable: yes — dormant read path; production behavior unchanged until a producer writes rows.

Phase 3 — apps/worker-context + the digest producer (the parallel cleaner goes live)

Goal: background digesting per spec §3.4/§3.5 — this is the phase that delivers "runs en parallèle des sessions".

Files: packages/agent-runtime/src/context/digest.ts + context-queue export; apps/worker-context/*; run.ts enqueue; root scripts/turbo registration.
Tests:

Shippable: yes — the full hybrid loop, kill-switched, with metrics. Enable on dev first, then preprod (morning window).

Phase 4 — Eval battery + tuning (the proof)

Goal: spec §4.6/§5 — no-regression proof wired into the standing gate, thresholds tuned on real data.

Files: packages/agent-runtime eval scripts + fixtures; root package.json.
Tests: the battery itself is the deliverable; bun run eval:gate green is the exit criterion.
Shippable: yes — this is the "cleaner is trusted" milestone.

Phase 5 — Follow-ups (explicitly out of v1, tracked, not started)

Rollout + risk notes