How the Continual Harness Manages and Refines Prompts and Memories: A Deep Dive into Prime Agent's Architecture

The Continual Harness is Prime Agent's layered, durable knowledge store that keeps prompts, memories, skills, and sub-agent specifications outside the token-limited conversation window, enabling continuous learning across sessions.

The Continual Harness solves a fundamental problem in long-running AI agents: how to preserve and improve reusable knowledge without stuffing an ever-growing conversation history into the context window. In Prime Agent, this system lives in the harness directory and provides structured CRUD operations, automatic refinement workflows, and precise scope control. Let's examine how it works under the hood.

Storage Architecture and Data Model

The harness persists to disk in harness_state.json and logs every change to refinements.jsonl. The helper getHarnessStatePath() constructs the filesystem location, while saveHarnessState() writes merged state atomically.

The core data structure is HarnessState (schema version 1), defined in [packages/coding-agent/src/core/refinement/refinement.ts](https://github.com/PrimeIntellect-ai/prime-agent/blob/main/packages/coding-agent/src/core/refinement/refinement.ts#L30-L63):

  • entries: A map keyed by RefinementKind (prompt, memory, skill, subagent)
  • HarnessEntry: Contains a stable id, human-readable title, actual content, optional path, and scope metadata
  • HarnessRefinementEvent: Records the full history of edits for auditing and rollback

Each artifact type serves a distinct purpose:

  • Prompt notes: Reusable instruction fragments
  • Memories: Factual knowledge and lessons learned
  • Skills: Executable capabilities or code patterns
  • Sub-agent specifications: Configurations for delegated agents

Scope Control: Local vs. Global Changes

The default scope is local—changes affect only the current Prime Agent session. This prevents experimental edits from polluting cross-session knowledge.

Global edits require explicit opt-in via the global flag in RefineOptions. The REFINEMENT_SYSTEM_PROMPT ([refinement.ts](https://github.com/PrimeIntellect-ai/prime-agent/blob/main/packages/coding-agent/src/core/refinement/refinement.ts#L123-L133)) instructs the LLM:

"Global edits must represent stable cross-session lessons backed by clear evidence."

This design balances rapid experimentation with durable knowledge accumulation.

Injecting Harness State into LLM Prompts

Every model request includes a compact harness overview via formatHarnessStateForPrompt(), implemented in [packages/coding-agent/src/core/system-prompt.ts](https://github.com/PrimeIntellect-ai/prime-agent/blob/main/packages/coding-agent/src/core/system-prompt.ts#L35-L41):


# Continual Harness State

[most recent memories, skills, prompt notes, and sub-agents]

The function respects token budgets and entry limits, surfacing only the most relevant artifacts. This gives the model access to reusable knowledge without unbounded context growth.

The Refinement Pipeline: How Memories Evolve

The five-stage refinement flow in refinement.ts automates knowledge improvement:

  1. Trigger: Automatic (turn-interval or post-compaction) or explicit via await refine.run()
  2. Review gate: LLM evaluates whether refinement is warranted using AUTO_REFINE_REVIEW_SYSTEM_PROMPT
  3. Edit generation: LLM outputs structured JSON matching the schema in REFINEMENT_SYSTEM_PROMPT—summary, rationale, edits, expected outcome
  4. Application: applyRefinement() validates each RefinementEdit and calls CRUD helpers (create_memory, update_memory, delete_memory, etc.)
  5. Recording: appendGlobalRefinement() logs the RefinementResult to refinements.jsonl

Output limits keep the pipeline reliable: REFINEMENT_MAX_OUTPUT_TOKENS (32k) and AUTO_REFINE_REVIEW_MAX_OUTPUT_TOKENS (4k).

CRUD API for Runtime Use

The RLM (Runtime Language Model) exposes typed harness operations:


# Create a local memory entry (session-specific)

await rlm.harness.create_memory(
    title="Project-X build steps",
    content="Use `make` after installing dependencies.",
    path="project-x/build",
    scope="local",  # default; can be omitted

)

# Update existing memory by id

await rlm.harness.update_memory(
    id="mem_12345",
    content="Run `make clean` before `make` to avoid stale artifacts.",
    reason="Observed repeated build failures",
)

# Get compact overview for inspection

overview = await rlm.harness.overview(limit=5)

# Trigger explicit refinement

await refine.run(
    instructions="Capture why the build kept failing and turn it into a reusable memory."
)

# Inspect refinement history

history = await rlm.harness.record_refinement()

These calls delegate to internal helpers like createMemory() and updateMemory() defined in refinement.ts.

Rollback and Versioning Safety

Every refinement stores a version and harnessStatePath. If a refinement corrupts state or produces poor results, the system reverts to the prior state.

In [packages/coding-agent/src/core/agent-session.ts](https://github.com/PrimeIntellect-ai/prime-agent/blob/main/packages/coding-agent/src/core/agent-session.ts#L7832-L7893), rollback logic checks rollbackTarget?.harnessStatePath and restores the file:

// Simplified from agent-session.ts
if (rollbackTarget?.harnessStatePath) {
  await restoreHarnessState(rollbackTarget.harnessStatePath);
}

This versioning ensures that autonomous refinement remains safe—even bad edits can be undone.

Key Source Files

File Purpose
packages/coding-agent/src/core/refinement/refinement.ts Core data structures, persistence, LLM prompts, and the full /refine pipeline
packages/coding-agent/src/core/system-prompt.ts Formats HarnessState for every model request
packages/coding-agent/src/core/agent-session.ts Session lifecycle, harness loading, and rollback handling
packages/coding-agent/test/system-prompt.test.ts Tests and documents the public RLM harness API
packages/coding-agent/test/suite/serialized-refine-config-integration.test.ts Verifies refinement durability across checkpoint/restore cycles

Summary

  • The Continual Harness is a structured, versioned knowledge base with four artifact types: prompts, memories, skills, and sub-agent specs
  • Storage uses JSON state plus append-only refinement logs in the harness directory
  • Scope control defaults to local (session-only) with explicit global opt-in for cross-session lessons
  • Prompt injection via formatHarnessStateForPrompt() keeps the model informed without context bloat
  • Refinement pipeline uses LLM-driven review, structured edit generation, and validated application
  • Rollback capability ensures safe experimentation through versioning and state restoration

Frequently Asked Questions

How does Prime Agent prevent the Continual Harness from growing too large?

The formatHarnessStateForPrompt() function applies entry limits and token budgets when rendering harness content for the LLM. Additionally, the refinement pipeline can trigger compaction events that consolidate or archive older entries while preserving the most valuable knowledge.

Can multiple Prime Agent sessions share the same harness state?

Yes, through global scope edits. When the global flag is set in RefineOptions, changes are written to the shared harness_state.json and become visible to all sessions. Local-scope changes remain isolated to the originating session.

What happens if a refinement produces incorrect or harmful content?

The system records the pre-refinement state path with each RefinementResult. If validation fails or a rollback is requested, agent-session.ts restores the previous harnessStatePath. The refinements.jsonl log also enables manual audit and selective reversion of specific edits.

How can developers trigger harness refinements programmatically?

Call await refine.run() with optional instructions to focus the LLM on specific improvement goals. For direct manipulation, use the RLM harness API methods like create_memory(), update_memory(), or delete_memory() with appropriate scope parameters.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →