docs/cache-harness.md

Cache Harness & Prompt-Cache Probe

Source Specs: v2-cache-harness-prd.md, docs/18-cache-harness.md, lean-context-prompt.md, docs/cache-harness-threat-model.md

Picasso treats prompt caching and context reduction as a unified harness problem across packages/cache-harness, packages/picasso-cache-probe, and packages/core/src/dsh/project.ts.

---

1. Architecture & Packages

ComponentPathResponsibility
Cache Harnesspackages/cache-harnessOwns canonical prompt section ordering, stable-prefix vs. dynamic-suffix hashing, token-budgeted context packing, repo-map slicing, privacy/secret redaction filters, provider cache policy translation (Anthropic cache_control breakpoints, OpenAI prompt_cache_key, Gemini cached content handles, vLLM workspace-scoped cache_salt), and cache-miss attribution.
Runtime Cache Probepackages/picasso-cache-probeCordis plugin mounted in the engine that intercepts the llm/stream waterfall and tools/result / tool/result hooks to log request metadata, stream usage chunks (cached_tokens, cache read/write tokens), and tool calls to $DSH_HOME/picasso-probe/probe.jsonl, plus registers the picasso_cache_probe inspection tool.
Event Projectorpackages/core/src/dsh/project.tsProjects the engine's token-meter usage chunks into normalized cache-usage LoopEvent records so picasso run --cache observe --cache-report, `picasso cache statusstatsinspect`, and the Mac Thread Inspector report cached vs. uncached input tokens.
Scenario Evaluation Suitescripts/cache/*Offline H0–H3 benchmark scenarios that validate stable-prefix hashes, token budgets, trajectory safety, and before/after context savings.

---

2. Prompt-Cache Invariants

  1. Canonical Prefix Ordering (`tools -> system -> Tier 1 index`):
  1. Dynamic Suffix Isolation:
  1. Zero Prefix Drops Across On-Demand Loads & Modes:
  1. Privacy & Isolation Guardrails:

---

3. Measuring & Verifying Lean Context

When modifying instruction loaders, skills, tool gateways, MCP proxies, or memory recall:

  1. Capture first-turn request payloads on both TUI and Mac relay for hello and a coding prompt using packages/picasso-cache-probe ($DSH_HOME/picasso-probe/probe.jsonl) or packages/cache-harness.
  2. Compare byte/token breakdowns (Tool schemas, System prompt, Instruction files, Skills, Memory, MCP) against reports/lean-context/baseline.md and record updated figures in reports/lean-context/after.md.
  3. Verify that turn 2+ cached_tokens do not drop after loading a Tier 2 tool, skill, or memory body.