# Phase 5: Continuous Operation (runs forever)

**This phase begins after Phase 4 handoff and never ends.**
The agent operates in a continuous loop, using a small operational prompt set where that meaningfully improves loop closure.

If self-test mode is active, STOP after Phase 4 handoff instead of entering
this loop.

## Daily operation loop

```
+----------------------------------------------+
|  1. Read context (AGENTS.md -> prompt.md)     |
|  2. Pick next deliverable from plan.md       |
|  3. Run $iterate sub-skill                   |
|     -> implement -> verify -> record          |
|  4. After every 3 iterations:                |
|     -> Run $quality-audit sub-skill          |
|  5. When milestone complete:                 |
|     -> Run $milestone-promote sub-skill      |
|        -> capture review packet + doc sync   |
|  6. Loop back to step 1                      |
+----------------------------------------------+
```

## Compaction rules

When the agent detects context pressure (long session, many iterations):
1. Read `docs/compaction-strategy.md` for what to preserve vs drop
2. Preserve: current milestone ID, active deliverable, last 3 iteration results,
   all hard constraints, quality gate status
3. Drop: completed iteration details older than 3 iterations, resolved needs,
   completed milestone details (they're in docs/documentation.md)
4. After compaction: re-read AGENTS.md -> prompt.md -> plan.md to rebuild context

## Recovery strategies

When something fails, the agent uses escalating recovery:

| Level | Trigger | Action |
|---|---|---|
| **L1: Retry** | First failure | Re-run the same approach with minor adjustment |
| **L2: Adjust** | Same failure after L1 | Change approach, try alternative implementation |
| **L3: Rollback + Replan** | Same failure after L2 | Revert only the files from the active iteration, then decompose the deliverable into smaller pieces |
| **L4: Escalate** | Same failure after L3, or any hard constraint violation | Stop and notify human with full context |

## Harness evolution (the environment itself iterates)

The harness -- documents, scripts, constraints, and operational sub-skills -- is not frozen after
Phase 4. It is a living system that improves through use.

**Triggers for harness evolution:**

1. **After every L2/L3 recovery**: ask "What was missing from the environment that
   caused this struggle?" Possible answers:
   - Missing documentation -> add or update the relevant doc
   - Missing tool or script -> create it or recommend installation
   - Missing constraint -> add a lint rule, CI check, or AGENTS.md guardrail
   - Missing context -> update AGENTS.md, compaction strategy, or Required Reading
   - **Uncertain what's missing -> ask the human**
2. **After every milestone**: review whether the harness itself needs an upgrade
   (new quality dimension, stronger enforcement, better compaction triggers, etc.)
3. **After every quality-audit**: check harness health -- is the agent struggling
   more than expected? Are the same failure categories recurring without prevention?

**Recording harness changes:**

- Log every harness improvement in `docs/documentation.md` Decision Log
- Update `docs/failures/taxonomy.md` with prevention actions (not just classification)
- If a harness change is project-specific, keep it local
- If a harness change is universal, flag it as `harness-improvement-candidate`

## Entropy management (garbage collection)

Agent-generated code drifts over time. To counter this:

1. **During normal iteration**: when doc validation or code changes reveal drift, fix the affected docs in the same loop instead of deferring it to a separate maintenance skill
2. **After every 3 milestones**: run `$quality-audit` for full tech debt scan
3. **Quality score tracking**: each audit produces a quality score in `docs/plan.md`;
   if score drops below threshold, next iteration must be debt cleanup, not feature work
4. **Pattern enforcement**: when the agent detects repeated similar code (copy-paste drift),
   it extracts to a shared utility and updates the architecture doc

## Multi-session continuity

When resuming after a break (new session, different agent instance):

1. Read `AGENTS.md` (the map)
2. Read `docs/prompt.md` (current milestone contract)
3. Read `docs/plan.md` (find where the marker is)
4. Read last 3 entries in `docs/documentation.md` (recent history)
5. Run the primary quality gate command from `AGENTS.md` (verify current state)
6. Resume the loop from step 2 of the daily operation loop
