# pi-continual

A minimal self-improving harness for [pi](https://github.com/earendil-works/pi), inspired by
[Prime Agent](https://www.primeintellect.ai/blog/prime-agent)'s RLM + Continual Harness
abstractions — implemented entirely as a pi extension, no changes to pi (or
[pi-pod](https://github.com/pi-pod/pi-pod)) required.

## What you get

**`repl` tool — persistent Python kernel (RLM-style programmatic tool calling).**
One kernel per session; variables and imports survive across calls. Pre-loaded globals:

- `rlm(task, name=None, agent=None, model=None)` — spawn a *persistent sub-agent*: a full
  `pi -p` session with its own session file under `.pi/harness/subagents/<name>/`. Returns a
  handle immediately (non-blocking) so the model can fan out parallel work. Calling `rlm()`
  again with the same `name` sends a follow-up turn into the same sub-agent session — it keeps
  its context. `handle.wait()`, `handle.result()`, `handle.running()`, `subagents()`.
- `harness` — CRUD over the harness's own state, stored as markdown files:
  | kind | where | effect |
  | --- | --- | --- |
  | `memory` | `.pi/harness/memory/` | injected into the system prompt every turn |
  | `prompt` | `.pi/harness/prompts/` | injected into the system prompt every turn |
  | `agent` | `.pi/harness/agents/` | sub-agent spec, used via `rlm(..., agent=name)` |
  | `skill` | `.pi/skills/` | native pi skill (SKILL.md with frontmatter) |
- `history(n=None)` — this session's own JSONL entries, including context that was
  compacted away: the model has programmatic access to its full past.

**`/refine` command + `refine` tool — evidence-backed self-improvement.**
Runs a background `pi -p` agent over the current session's trajectory that applies the
*smallest* useful edit to the harness state (create/update one memory, prompt note, agent
spec, or skill) and appends a record to `.pi/harness/refine-log.jsonl`. Non-blocking; the
agent itself can call `refine` mid-task when it notices a repeated failure or reusable tactic.

**`/goal`, `/gate`, `goal_complete` tool, `--goal` flag — bounded autonomous mode.**
`/goal <text>` sets a persistent objective; after every settled turn the harness re-prompts
the agent to continue (capped at 12 turns). The agent ends the run by calling
`goal_complete`; if `/gate <cmd>` is set, the gate command must exit 0 first — a failing
gate returns its output to the agent for another attempt.

## Install

```bash
pi install npm:pi-continual            # user-wide (recommended)
pi install -l npm:pi-continual         # project (shared via .pi/settings.json)
pi -e npm:pi-continual                 # try once without installing

# or from git:
pi install git:github.com/pi-pod/pi-continual
```

Requires `python3` on PATH.

## Self-improvement that travels through git

All harness state lives in the repository (`.pi/harness/`, `.pi/skills/`). Commit it and
refinements become reviewable diffs that follow the repo — including into fresh
[pi-pod](https://github.com/pi-pod/pi-pod) pods, where each session starts from a clean
clone. Nothing here needs pod-side support.

## Layout

```
extensions/continual.ts   # the extension: repl tool, prompt injection, /refine, goals
extensions/kernel.py      # persistent Python kernel: rlm(), harness, history()
```

## Notes / limits (deliberately minimal)

- The kernel restarts (state lost) on repl timeout or abort; sub-agent sessions survive on disk.
- Goal state is session-scoped and not persisted across restarts.
- Sub-agent messaging is parent→child only (`rlm()` follow-up turns); there is no
  cross-session daemon. Inside a pi-pod pod that boundary is intentional.
