repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/goal/README.md

Raw

goal

Persistent /goal loop for Pi sessions.

Install / load

This extension is loaded through the root pi-ext package. See ../../README.md.

Demo

Goal loop

Bench

Run mise run bench here, or mise //extensions/goal:bench from the repository root, to benchmark goal state replay.

Commands

Subcommands autocomplete. /goal /... autocompletes prompt templates. /goal remove autocompletes criterion numbers.

/goal
/goal <objective>
/goal /prompt-template [args]
/goal status
/goal pause
/goal resume
/goal complete [reason]
/goal clear
/goal add <criterion>
/goal remove <n>
/goal criteria
/goal clear-criteria
/goal widget

Debug command, registered only when PI_GOAL_PROBE=1:

/goal-probe direct|microtask|timeout

Tool

update_goal({ status: "complete" | "blocked", reason: string })

Marks the active goal complete or blocked.

Hidden until a continue verdict increments turnsUsed, then hidden again when the goal is complete, blocked, paused, cleared, or resumed into a fresh 0-turn state.

If invoked while stale, it errors when no active goal exists, the goal has not been judged, or the goal belongs to another session.

Settings

{
  "goal": {
    "judgeModel": "provider/model",
    "plannerModel": "provider/model",
    "plannerThinkingLevel": "high",
    "tailBytes": 4096,
    "tokenBudget": "100k",
    "timeBudgetSeconds": 1800,
    "timeBudget": "30m"
  }
}

Hooks

session_start
input
before_agent_start
context
agent_start
turn_end
agent_end
session_before_compact
session_compact
session_shutdown

Behavior

Markdown source
File outline
# goal

Persistent `/goal` loop for Pi sessions.

## Install / load

This extension is loaded through the root pi-ext package.
See [../../README.md](../../README.md).

## Demo

<!-- demo:goal:start -->

![Goal loop](assets/goal.gif)

<!-- demo:goal:end -->

## Bench

Run `mise run bench` here, or `mise //extensions/goal:bench` from the repository root, to benchmark goal state replay.

## Commands

Subcommands autocomplete.
`/goal /...` autocompletes prompt templates.
`/goal remove` autocompletes criterion numbers.

```text
/goal
/goal <objective>
/goal /prompt-template [args]
/goal status
/goal pause
/goal resume
/goal complete [reason]
/goal clear
/goal add <criterion>
/goal remove <n>
/goal criteria
/goal clear-criteria
/goal widget
```

Debug command, registered only when `PI_GOAL_PROBE=1`:

```text
/goal-probe direct|microtask|timeout
```

## Tool

```text
update_goal({ status: "complete" | "blocked", reason: string })
```

Marks the active goal complete or blocked.

Hidden until a continue verdict increments `turnsUsed`, then hidden again when
the goal is complete, blocked, paused, cleared, or resumed into a fresh 0-turn
state.

If invoked while stale, it errors when no active goal exists, the goal has not
been judged, or the goal belongs to another session.

## Settings

```json
{
  "goal": {
    "judgeModel": "provider/model",
    "plannerModel": "provider/model",
    "plannerThinkingLevel": "high",
    "tailBytes": 4096,
    "tokenBudget": "100k",
    "timeBudgetSeconds": 1800,
    "timeBudget": "30m"
  }
}
```

- `judgeModel`:
  optional Pi model ref, `provider/model`.
  When unset, the current session model is used.
- `plannerModel`:
  optional Pi model ref for automatic goal planning.
  When unset, the current session model is used.
- `plannerThinkingLevel`:
  planner thinking level.
  Default `high`; allowed `off`, `low`, `medium`, `high`, `xhigh`.
- `tailBytes`:
  bytes read from latest assistant/tool output for judgment and evidence.
  Default `4096`, clamped to `512..65536`.
- `tokenBudget`:
  optional approximate token budget for a new goal.
  Accepts numbers or strings like `100k`.
- `timeBudgetSeconds` / `timeBudget`:
  optional wall-clock budget for a new goal.
  Accepts seconds or strings like `30m`, `2h`.

## Hooks

```text
session_start
input
before_agent_start
context
agent_start
turn_end
agent_end
session_before_compact
session_compact
session_shutdown
```

## Behavior

- `/goal` and `/goal status` print current goal status.
- `/goal <objective>` creates an active goal with empty criteria and current
  session ownership.
  It sends a kickoff prompt immediately.
- `/goal /prompt-template [args]` expands a prompt template and uses the expanded
  content as the objective.
  Prompt templates autocomplete after `/goal /`.
- A raw prompt containing `standing goal` plus an `<objective>...</objective>`
  block is adopted as an active goal when no goal exists.
- New goals require an idle agent.
  Subcommands can run while the agent is busy.
- Goal state is stored as append-only v2 custom `goal-state` entries.
  Replay scans backward; newest valid state or clear tombstone wins.
  Older schemas are ignored.
- A deterministic `goal-dossier` entry is created with each new/adopted goal.
  It stores hidden control data:
  invariants, planner plan, soft subgoals, evidence, risks, and open questions.
- A planner session refreshes the dossier automatically for new goals, resumes,
  user steering, weak proof, blocked-risk, and periodic long-goal turns.
  The planner gets `read`, `grep`, `find`, `ls`, and `bash`, and uses
  `plannerModel` / `plannerThinkingLevel`.
- `before_agent_start` injects the active goal, criteria, and compact dossier
  control data into the system prompt for the owning session.
- After each owned active goal turn, the extension writes a `goal-judge` start
  entry, shows `goal judging Nt` in persistent UI, then asks a judge model for
  JSON:
  `done`, `blocked`, `reason`.
- The adversarial judge runs as a separate Pi session with project/user
  extensions loaded.
  It gets `read`, `grep`, `find`, `ls`, and `bash`, plus the objective,
  criteria, dossier tail, latest assistant text, stop reason/error message, and
  last-turn tool call/result excerpts.
- Judge completion writes a `goal-judge` verdict entry and accumulates judge
  verdict glyphs/reasons in the goal widget.
- Continue verdicts stay quiet except for persistent widget/working UI.
  Complete, blocked, paused, and repeated judge-failed verdicts notify.
- Done verdict marks the goal complete and clears persistent
  status/widget/footer.
- Assistant/provider transport, server, rate-limit, stream, parse, unknown, and
  context-overflow errors retry instead of pausing.
  Context overflow uses Pi core compaction/retry.
  Auth, quota, bad-request, and user-cancel errors pause.
- Judge errors retry the goal loop, then pause after repeated failures.
- Blocked verdict must repeat the same blocker for 3 goal turns before the goal
  is marked blocked.
- Incomplete verdict increments `turnsUsed`; exhausted token/time budget pauses
  the goal.
  There is no max-turn budget.
- The elapsed timer tracks the whole active goal run, pauses on `/goal pause`,
  resumes on `/goal resume`, and keeps running during retryable errors.
- If still active, continuation is queued as a hidden `goal-continuation` custom
  message with `deliverAs: "followUp"` and `triggerTurn: true`.
- `update_goal` is hidden until the first continue verdict increments
  `turnsUsed`, then hidden again for terminal or paused goals.
- `/goal pause` only pauses active owned goals.
- `/goal resume` rebinds a goal to the current session, sets it active, resets
  `turnsUsed`, and schedules continuation.
- `/goal complete [reason]` is a user override.
  Default reason:
  `marked complete by user`.
- `/goal clear` writes a clear tombstone and removes status.
- `/goal add`, `/goal remove`, and `/goal clear-criteria` edit criteria on the
  current owned goal.
- `/goal criteria` lists criteria.
  Empty criteria print `No goal criteria.`
- `/goal status` emits a compact notify summary:
  phase, elapsed/budget, objective, criteria preview, and last judge
  glyph/reason.
- The `goal` status/footer segment is cleared; the widget is the primary
  persistent UI.
- UI widget key:
  `goal`.
  Active, queued, judging, paused, and blocked goals show a compact above-editor
  icon line by default.
  The widget is registered once per goal display to keep ordering stable
  relative to other widgets while live timer updates continue.
  `/goal widget` or `alt+g` toggles expanded mode with wrapped objective,
  elapsed/budget info, criteria preview, and accumulated judge verdict
  glyphs/reasons.
  Widget text wraps instead of truncating.
- Active/queued/judging goals set the streaming working message and spinner
  frames as a heartbeat; terminal/passive states restore the default loader.
- Goals owned by another session show `owned by another session`; mutating
  commands require `/goal resume` first.
- User steering while a goal turn is running is added as a new criterion rather
  than stopping the goal run, and triggers planner refresh.
- `session_before_compact` / `session_compact` write deterministic goal summary
  entries.
- `context` prunes stale `goal-continuation` messages, retaining only the latest
  current-goal continuation.
- `PI_GOAL_LOG=1` writes custom `goal-debug` scheduling/judge entries.
- `PI_GOAL_PROBE=1` adds `/goal-probe` and writes custom `goal-probe` entries
  for continuation scheduling checks.