Harness state management

From Intense-Visions/harness-engineering · View interactive version

Harness state management is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Manage persistent state across agent sessions so that context, decisions, progress, and learnings survive context resets. Load state at session start, track position and decisions throughout, and sa

How to use this skill

Skill file

Harness State Management

Manage persistent state across agent sessions so that context, decisions, progress, and learnings survive context resets. Load state at session start, track position and decisions throughout, and save state for the next session.

When to Use

Process

Phase 1: LOAD — Restore Context from Previous Sessions

  1. Resolve the stream. State is organized into streams — isolated directories under .harness/streams/<name>/. Before loading any state files:
  1. Read .harness/state.json. This is the primary state file. It contains:
  1. Run harness state show to get a formatted view of current state. This is equivalent to reading the JSON but formatted for readability.
  1. Read .harness/learnings.md. This is the append-only knowledge base. Scan for:
  1. Read .harness/failures.md if exists. Scan for active anti-patterns and dead ends.
  1. Read .harness/handoff.json if exists. Structured context from last skill.
  1. Check .harness/archive/ for historical failure logs.
  1. If no state exists, this is a fresh start. Create .harness/state.json with initial structure:

```json

{

"schemaVersion": 1,

"position": { "phase": "start", "task": null },

"progress": {},

"decisions": [],

"blockers": [],

"lastSession": { "date": null, "summary": null }

}

```

  1. Announce the loaded context. Briefly summarize: "Resuming from [position]. [N] tasks complete. [N] blockers. Key learnings: [summary]." This confirms the state was loaded and gives the human visibility.

Phase 2: TRACK — Maintain State During the Session

  1. Update position when moving between phases or tasks. Every time work shifts to a new task or phase, update position in state:

```json

"position": { "phase": "execute", "task": "Task 3", "step": "writing tests" }

```

  1. Record decisions when they are made. Decisions are choices that affect future work. Record them immediately — do not wait until the end of the session:

```json

"decisions": [

{

"date": "2026-03-14",

"what": "Use WebSocket instead of SSE for real-time notifications",

"why": "SSE does not support bidirectional communication, which Task 5 requires"

}

]

```

  1. Log blockers when encountered. A blocker is anything that prevents the current task from completing:

```json

"blockers": [

{

"date": "2026-03-14",

"task": "Task 4",

"description": "Payment gateway API returns 403 — API key may be expired",

"status": "open"

}

]

```

  1. Update progress after each completed task:

```json

"progress": {

"Task 1": "complete",

"Task 2": "complete",

"Task 3": "in_progress",

"Task 4": "blocked"

}

```

  1. Keep state concise. State is not a log. Each field should contain the current status, not a history of all changes. History belongs in .harness/learnings.md and git commits.

Phase 3: LEARN — Capture Knowledge for Future Sessions

  1. Identify learnings as they happen. A learning is anything that:
  1. Capture learnings with harness state learn:

```bash

harness state learn "Date comparison needs UTC normalization — use Date.now() not new Date()"

```

This appends to .harness/learnings.md with a timestamp.

  1. Or append directly to .harness/learnings.md with structured format:

```markdown

## 2026-03-14 — Task 3: Notification Expiry

application uses UTC epoch milliseconds. Always convert before comparing.

Use upsert (ON CONFLICT DO UPDATE) instead of plain INSERT.

ISO timestamp for consistency with the rest of the codebase.

```

  1. Learnings are append-only. Never edit or delete previous learnings. They are a chronological record. Even if a learning turns out to be wrong, append a correction rather than modifying the original.
  1. What belongs in learnings vs. git commits:

Phase 4: SAVE — Persist State for Next Session

  1. Update .harness/state.json with final position, progress, and session summary:

```json

{

"schemaVersion": 1,

"position": { "phase": "execute", "task": "Task 4" },

"progress": {

"Task 1": "complete",

"Task 2": "complete",

"Task 3": "complete"

},

"decisions": [ ... ],

"blockers": [ ... ],

"lastSession": {

"date": "2026-03-14",

"summary": "Completed Tasks 2-3. Task 3 required UTC date normalization (see learnings). Starting Task 4 next session."

}

}

```

  1. Verify learnings were captured. Review .harness/learnings.md — were all non-obvious discoveries recorded? If something was tricky during the session, it should be in learnings.
  1. State is saved to the active stream. All writes (state, learnings, handoff, failures) go to the resolved stream's directory (e.g., .harness/streams/auth-rework/state.json). Switching to a different stream in the next session does not affect the current stream's files.
  1. Decide whether to commit state files. State files (.harness/streams/*/state.json, .harness/streams/*/learnings.md) should be committed to git so other team members and agents can access them. Commit state updates separately from code changes so they do not clutter code diffs.

Building Institutional Knowledge Over Time

The .harness/learnings.md file grows over the lifetime of the project. It becomes a valuable resource:

Treat learnings as a first-class project artifact. They are as valuable as tests and documentation.

Archival Workflow

Harness Integration

Success Criteria

Rationalizations to Reject

| Rationalization | Reality |

| --------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| "The session is short — I'll update state at the end rather than after each task." | Context resets happen without warning. A session that ends mid-task with no state update forces the next session to reconstruct position by reading git history and code, which takes longer and produces an inaccurate picture. State is updated after each task, not at the end of the session. |

| "This decision is obvious from the code — I don't need to record the rationale in state." | What is obvious to the agent that made the decision is opaque to the agent that resumes three weeks later with no memory of the session. Decisions are recorded because the context that made them obvious does not survive a context reset. The rationale is exactly what needs to be saved. |

| "The learnings file is getting long — I'll trim old entries that are no longer relevant." | Learnings are append-only by design. An entry that seems irrelevant may become relevant when a related pattern resurfaces. Trimming destroys the chronological record and the ability to understand why earlier decisions were made. Entries are never deleted, only supplemented with corrections. |

| "I can re-read the plan to figure out where I am — I don't need to update the position in state." | The plan describes what to do; state records what has been done. Re-reading the plan without state requires the next session to infer progress from code, which produces uncertain position. Uncertain position leads to re-executing completed tasks or skipping tasks that appear complete but are not. |

| "The stream auto-resolves from the branch — I don't need to explicitly verify which stream is active before writing." | Auto-resolution works when branch names match stream names and the index is current. When branches are renamed, stale, or when multiple streams exist for the same feature, auto-resolution can write to the wrong stream silently. Always announce the resolved stream before writing state. |

Examples

Example: Starting a New Session (Resuming Work)

LOAD:

Run: harness state show
Output:
  Position: execute / Task 3 (writing tests)
  Progress: Task 1 complete, Task 2 complete
  Blockers: none
  Last session: 2026-03-13 — "Completed Tasks 1-2. Task 2 required
    adding a new index on notifications.userId for query performance."

Read: .harness/learnings.md
  Most recent:
  - [gotcha]: notifications table needs index on userId — queries
    were timing out without it
  - [decision]: used partial index (WHERE deleted_at IS NULL) to
    avoid indexing soft-deleted rows

Summary: "Resuming from Task 3 (writing tests). Tasks 1-2 complete.
  Note: notifications table has a partial index on userId — see learnings."

Example: Recording a Decision Mid-Session

Context: Implementing Task 4, need to choose between polling and WebSocket.

Record decision:
  date: "2026-03-14"
  what: "Use WebSocket for real-time notification delivery"
  why: "Polling would require 1-second intervals for acceptable latency,
    which creates too much load. WebSocket gives instant delivery with
    one persistent connection per client."

Capture learning:
  harness state learn "WebSocket chosen over polling for notifications.
    Polling at 1s intervals = ~86k requests/day per client. WebSocket =
    1 persistent connection. See Task 4 decision in state."

Example: Ending a Session

SAVE:

Update .harness/state.json:
{
  "schemaVersion": 1,
  "position": { "phase": "execute", "task": "Task 5" },
  "progress": {
    "Task 1": "complete",
    "Task 2": "complete",
    "Task 3": "complete",
    "Task 4": "complete"
  },
  "decisions": [
    {
      "date": "2026-03-14",
      "what": "Use WebSocket for real-time notification delivery",
      "why": "Polling creates too much load at acceptable latency intervals"
    }
  ],
  "blockers": [],
  "lastSession": {
    "date": "2026-03-14",
    "summary": "Completed Tasks 3-4. Task 3 added expiry logic with UTC normalization. Task 4 implemented WebSocket delivery (chose over polling — see decisions). Starting Task 5 (UI integration) next session."
  }
}

Verify: .harness/learnings.md has entries for UTC normalization and WebSocket decision.
Commit: git add .harness/ && git commit -m "chore: update harness state after Tasks 3-4"

Example: What Belongs Where

| Information | Where It Goes | Why |

| ----------------------------------------------------- | ------------------------------- | ---------------------------------------------------- |

| "Added WebSocket handler in src/ws/" | Git commit message | Describes what changed in code |

| "Chose WebSocket over polling because..." | .harness/state.json decisions | Records the choice and rationale for future sessions |

| "WebSocket requires sticky sessions in load balancer" | .harness/learnings.md | Non-obvious operational concern future sessions need |

| "Task 4 complete" | .harness/state.json progress | Tracks execution position |

| "The WebSocket library auto-reconnects by default" | .harness/learnings.md | Gotcha that saves future debugging time |

| "Tried approach X, failed because Y" | .harness/failures.md | Active anti-pattern to avoid repeating |

| "Completed Tasks 1-3, Task 4 pending" | .harness/handoff.json | Structured context for next skill |

| "[PREPARE 10:30] Loaded 3 failures" | .harness/trace.md | Reasoning trace for debugging agent behavior |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.