# Milestones

- Drove a real SDK session against a fake provider through create, work, pause, resume, wait cycles, settings changes, completion and a second goal. Compared each request with the one before it.
- Found one real break: when the goal completed, the context hook cleared the retained blocks, so the next request lost everything after the first block. Pause, resume, waits, task updates and settings changes were already stable.
- Fix: `LiveTailRetention.retire` keeps the blocks and appends an ended or on-hold block. Stale and unfocused text is an interim state with no scope of its own, so the goal after it needs no reset.
- Also fixed while auditing: a non-copyable message gave a null snapshot that matched nothing and reset every request; a host with no session id retained nothing; the session map evicted by age, not use.
- Could not reproduce a constant miss on every request in 0.35.3 on Pi 1.4.0 or 1.7.0. Asked the reporter for the Pi version, provider and the `/goal-status verbose` cache line.
- Tests: `tests/goal-cache-lifecycle.test.ts` and the real SDK run in `tests/e2e/goal-prefix-lifecycle-sdk.test.ts`. The SDK run fails on the code before the fix.

## Round 2

- Real SDK run with nine pause/resume cycles and a user message in each pause: every sixth cycle reached the 12-block limit and reset the whole prefix. Fix: a policy that returns to an earlier text adds a one-line `[PI GOAL RESTORED]` pointer instead of the full block. Pointers have their own bound (200).
- A `/reload` loaded a new extension instance with empty retention, so the next request lost its blocks. Retention now lives on `globalThis` (versioned) and only a start that is not a reload clears it. The history check still drops it if anything changed.
- Checked and already fine: tool list and order (same bytes across sessions), request-only text (usage, run counts, waits, stall notes) only in the last block, wait cycles, task updates, settings changes, compaction (one reset, then stable), Pi 1.4.0 and 1.7.0.
- Left alone on purpose: toggling `disableTasks` changes the tool list, so it is one miss that the user caused. A goal switching straight to another goal resets.
- Not done: the retained blocks are not saved with the session, so a process restart or `/resume` in a new process starts without them.

## Round 3
- Done: goal blocks are saved with the session (`goal-live-persist.ts`), so restart and /resume keep the cache; a mismatch with the history starts clean.
- Done: a goal switch or objective edit appends a new policy block and task list instead of resetting.
- Kept: toggling `disableTasks` changes the tool list, which is part of the cached prefix. Keeping the task tools always listed would add about 700 tokens to every request for everyone who turned them off, so one miss on a deliberate toggle is the better trade.

## Round 4 (issue #187 follow-up)
- Found: the history check reset the goal blocks whenever any earlier message differed from the last request, so an extension that edits the newest message each request moved the blocks to the end every time. Fixed: the same message with new text (same role and time) keeps them in place; more than two edits, a missing time, or a different message still resets.
- Added: the retention records each restart with its reason, shown in `/goal-status verbose` and in the cache-drop warning.
- Tested with real SDK sessions: the Anthropic API with thinking blocks (simulated 20-block lookback, breakpoints stay on reusable blocks), task-list churn in tool loops, and a second extension editing the newest message.

## Round 5 (random testing)
- Added `tests/goal-cache-fuzz.test.ts`: random walks over history changes, goal states, other extensions' edits, restarts and several sessions, with rules checked on every request (real messages untouched, no block inside a tool call and its result, the newest block is the current one, blocks do not move without a rewrite, a restart gives the same request as no restart). Fails when the tolerance, the generation counter or the leading reset is removed.
- Added real-SDK random walks (`prefix-fuzz-sdk-worker.mjs`: aborts, provider errors, steering, model switches, compaction, tree moves, restarts, an editing extension) and two sessions in one process.
- Fixed three saving bugs the walks found (see CHANGELOG).
- Not done: a live check against the real API. It needs an API key, which this environment does not have; `/goal-status verbose` shows the provider's own cache numbers.

## Round 6 (mock API and cold review)
- Added `tests/support/mock-anthropic-cache.mjs`: an Anthropic-compatible server with a cache model (prefix matching at breakpoints, 20-block lookback, 4-breakpoint limit, 1024-token minimum, 5-minute and 1-hour TTL, hits renew). It reports cache read and creation tokens like the API. `tests/mock-anthropic-cache.test.ts` checks the model; the fuzz worker runs with `API=anthropic` and asserts that each request reads what the one before cached. 24 seeds of 40 steps: no lost tokens beyond the newest message.
- A cold review of the code found five defects, all fixed with tests: another session starting wiped this one's blocks; restore rewrote every block on the next save; a goal switch after a stand-in left the old task list; the restored pointer named an outdated block; a draft or hidden reminder dropped all blocks (the docs already promised `[PI GOAL ON HOLD]`).
- Known, left alone: re-hashing long strings in history costs about 1 ms per MB per request; the 24 KiB task-list cap still resets on very long task histories.

## Round 7 (every provider, long walks)
- Correction: in round 6 the real-SDK worker printed "no lost tokens" but the check was not wired in (a replace that matched nothing). It is now, with a slack for the newest message and for a stamping extension, and the walks assert the numbers the mock returns.
- Added models and payload builders for OpenAI (Responses and Completions), Gemini, DeepSeek, xAI, Bedrock and OpenRouter (to Anthropic and to OpenAI-style models), and a walk engine (`tests/support/cache-walk.ts`) with long walks, shrinking, tool batches with parallel calls, huge/Unicode/image content, clock jumps, failed saves and damaged session files. Mutation checked: removing the marker relocation fails it in two operations.
- Ran 9 providers x 1,000 seeds x 200 steps, and real Pi sessions through the OpenAI-style, DeepSeek, xAI and Gemini mocks.
- Fixed: a failed save left the saved-state record wrong; damaged or reordered entries could throw or misplace a block.
- Walks of the goal tools and commands (create, tasks, pause, resume, complete with the auditor, focus, clear, reload, restart) check state, files on disk and restarts. They found no defect.
- Not covered: OpenAI Responses and Bedrock through a real SDK session (their wire formats need more of a fake server); explicit Gemini or OpenAI caches that Pi does not create; providers' real tokenizers (the models count four characters per token).
- Considered and dropped: a spare cache breakpoint before the newest block, to survive an extension that rewrites the newest message every request. It would only help while a goal is active on Anthropic or Bedrock.
