omp packages

pi-loop-core0.5.0

Agent-owned continuation for the Pi coding agent: a grow_loop tool with no required timers and no required magic strings, customizable first/next/child continuation prompts, plus an on/off switch, one-call timed appointments and a 'you forgot to call it'

xuqianhui Published 22 hours ago MIT ~1.7K downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-loop-core

npm ci license

Agent-owned continuation for the Pi coding agent: one tool the agent calls when it wants another turn, one switch you can flip, and a reminder when the agent says "I'll continue" but never actually schedules it.

pi install npm:pi-loop-core

0.3.0: 一次调用就能「开开关 + 预约一轮」——loop_switch({action:"on", after_seconds:1500}) / /loop once 25m(现在不发轮,到点只发一轮)。

0.2.0: /loop 现在可以定时启动——除了 /loop on 立刻开始,还可以 /loop at 09:30 或 /loop in 30m, 到点才开开关并发一轮;/loop cancel 取消,/loop off 一并撤。

已发布到 npm 的版本带着 repository / homepage / bugs 字段,npm 包里指向 github.com/xiaquwan1001/pi-loop-core——反馈走 Issues。

Then just ask for a loop ("grow loop", "keep going until the backlog is empty"). The agent calls grow_loop itself; nothing continues that the agent did not ask for.


Design rules

  1. The agent decides. The runtime never schedules a turn the agent did not request. No hidden queue, no fixed workflow, no runtime state machine that outvotes the agent's own scope/stop judgement.
  2. No timers by default. grow_loop() (no after_seconds) means "continue when this run settles". It is delivered at agent_before_settle — so there is no 3-second window, no stale timer surviving an abort, and no race with compaction or retries. A timer exists only when you explicitly ask to wait for external work, and it stays retractable until it fires.
  3. The agent owns pacing and stop. When an interval is set, grow_loop() waits it out; after_seconds overrides it for one turn; loop_switch({action:"off"}) turns the loop off. A switch turned on by the user never removes this control — it only permits continuation.
  4. No required magic strings. The final-message marker (⟳ 续轮 … / ■ 结束) is a convention, not a scheduling instruction. The runtime only reads it to (a) remind the agent once when it claimed a continuation but never called the tool, and (b) honour a stop proof. Agents that never write it lose nothing.
  5. Stop is retractable, not sticky. Writing ■ 结束 in the final message (or pressing Esc) cancels the iteration you already scheduled and suppresses a continuation prompt that is already queued. This exists because the alternative — a timer you cannot retract — produces "the agent said it was done, then a continuation popped up 3 s later" (19 cases in our own transcript audit of 3,712 sessions).
  6. Subagents may not schedule their own loops. A child that keeps looping never reports a terminal state, which leaves the parent waiting until timeout. PI_SUBAGENT_CHILD/PI_SUBAGENT_DEPTH are blocked at the tool-call boundary, and the switch/interval a child sees are process-local (so parent and child can never corrupt each other's wiring).

Tools

grow_loop({ after_seconds? })

after_seconds Meaning
omitted Use the default interval (/loop every, loop_switch({action:"interval"})); with no interval set, continue immediately after this run settles.
0 (explicit) Continue immediately after this run settles (no timer) — overrides the interval.
> 0 (max 3600) Arm an unref'd timer; when it fires and Pi is idle, queue the continuation prompt. Retractable until it fires. Use 60 s as the usual tick, 900–1800 s for slow async work.

Both paths are blocked when the switch is off or in a subagent child.

loop_switch({ action, after_seconds? })

status / on / off / toggle / interval, backed by the same state file as /loop.

interval sets the default wait used by grow_loop() when after_seconds is omitted (loop_switch({action:"interval", after_seconds:600}), 0 clears). The agent owns both knobs: it can pace the next turn while children/CI are still working and turn the loop off when the work is done — an enabled switch never forces a turn by itself.

Children keep a process-local switch and interval: they start off, may turn themselves off (and set their own interval), and can never read or write the parent's switch, interval or scheduled start — a child in the same cwd must not eat the parent's alarm or flip its switch.

With action: "on" (or a toggle that turns it on), after_seconds > 0 books exactly one turn that many seconds later instead of sending one now: the switch is turned on silently, the appointment is written to the state file (survives a restart), and /loop cancel retracts it. One call does the whole "switch off → enable and run one bounded pass later" dance, so the blocked-grow_loop message points here.

Command

/loop on|start [skill…]   # enable; sends one turn if nothing is scheduled
/loop off|stop            # disable: blocks grow_loop, swallows queued continuations, cancels a scheduled start
/loop toggle
/loop at 09:30            # 定时启动:today 09:30, tomorrow if already past (absolute forms below)
/loop in 30m              # 定时启动:30 s / 2h / 1h30m / 45 (= minutes)
/loop once 25m            # 预约一次:开开关但不发轮,到点只发一轮(也收 09:30)
/loop cancel              # drop a scheduled start; the switch is left alone
/loop every 10m           # 默认间隔:grow_loop() 不带 after_seconds 时按它等;/loop every off 清除
/loop prompt [global] <first|next|child|all> <text…>   # 自定义续轮提示词(见下)
/loop prompt reset [first|next|child|all]             # 恢复内置默认
/loop prompt explicit <text…> | explicit reset        # 一个文本覆盖 first/next
/loop status              # switch source, armed schedule, counters, state file path
/loop kick                # send one continuation now

In the TUI, /loop opens a subcommand popup (the same getArgumentCompletions channel /model uses); /loop on continues into skill names.

State lives in $PI_CODING_AGENT_DIR/state/loop-core/<cwd>.json; writing {"enabled": false} from a script takes effect immediately (a missing file means enabled). Auditing goes to state/loop-core/audit.log.

Scheduled start

/loop on starts now; /loop at / /loop in start later; /loop once enables the switch but only books a single turn (same code path as loop_switch({action:"on", after_seconds:N})). A scheduled start is not a queued continuation (that is grow_loop(after_seconds)), it is an alarm that calls "on" when it fires:

Spec Fires at
/loop at 09:30 today 09:30, tomorrow if that time already passed
/loop at 9:30:15 same, with seconds
/loop at 2026-12-01T09:30 absolute (space instead of T also works; date-only = 00:00)
/loop at +30m now + 30 min (same as /loop in 30m)
/loop in 30m now + 30 min: 30s, 2h, 1h30m, 1.5h, 45 (= minutes)

Behaviour at fire time:

  • switch off → it is turned on (by: schedule:<source>) and one continuation prompt is sent;
  • switch on and nothing scheduled → one continuation prompt is sent;
  • already running or a continuation is already queued → the alarm is dropped with a notification (no double turn);
  • Pi is busy (mid-run / pending message) → the alarm waits, status shows ⏰due, and it fires on the first idle tick.

Window: 1 s – 30 days. The alarm is persisted in the state file, so a restarted session re-arms it at session_start; if the time already passed while Pi was down, it fires as soon as Pi is idle. /loop cancel drops it without touching the switch; /loop off drops it too (off is a stop).

Continuation prompt (three tiers)

The text delivered as the next turn is not a fixed constant. It is chosen per process:

Tier When Built-in default
first the first loop turn this process sends (/loop on, /loop once firing, …) 独立规划并派发,能自主决策就不等待用户;没有实质工作了就写 ■ 结束
next every later turn in the same process (including settle-boundary continuations) 继续规划任务并派发;判断不需要再续就写 ■ 结束,不要惯性调 grow_loop
child subagent children (PI_SUBAGENT_CHILD / PI_SUBAGENT_DEPTH), always loop 可能让子进程无法结束,建议关闭 loop(做完这一轮就带证据收口汇报)

Every tier spells out that stopping is allowed: an injected prompt that reads like "keep looping forever" takes the parent's own stop judgement away. child cannot be overridden by an explicit /loop on <text> — a child that loops never reports a terminal state.

Resolution order (later wins): built-in → global file state/loop-core/prompts.json → the cwd's prompts field in its state file → an explicit override (/loop on <text>, /loop prompt explicit), which covers first and next in the parent only.

/loop prompt                                    # effective text + where each tier comes from
/loop prompt next 继续推进 lane 清理              # write the cwd override
/loop prompt global first 先规划再派发            # write the global default
/loop prompt reset [first|next|child|all]       # drop cwd overrides
/loop prompt global reset [first|next|child|all]# drop global overrides
/loop prompt explicit <text…> | explicit reset  # one text for first+next

Placeholders are substituted in every tier: {mode}, {turn} (1-based loop turn in this process), {iteration} (grow_loop calls), {count} (continuations served), {cwd}, {at} (HH:MM). Unknown placeholders are left verbatim.

Status line

The status line is ASCII only: loop:on / loop:off is the switch, and nothing else is written when a loop is idle (there is no separate "idle" word — on only permits continuation, it never drives it). ⏱ means a turn is running right now; →/⏰ are countdowns, never wall-clock times, so they read the same way as grow_loop({after_seconds}).

Status Meaning
loop:on ∞3 ⏱12.4s →next running, and the next turn is scheduled for the end of this run
loop:on ∞3 ⏱12.4s a turn is running, nothing else armed
loop:on ∞3 → 8.1s idle, waiting out an after_seconds (or default-interval) delay
loop:on ∞3 ⏲10m switch on, default interval set: grow_loop() without after_seconds will wait 10m
loop:on* ∞1 a subagent child's process-local switch (never the parent's)
loop:on ∞3 ⏱12.4s ⏰→4m59s →next running; a scheduled start is also armed, 4m59s to go
loop:off ⏰→30m00s off, but a scheduled start will turn it on in 30m00s
loop:on ∞3 ⏰due scheduled start is due but Pi is busy — it fires on the next idle tick
loop:on ∞3 switch on, nothing scheduled, nothing running — the loop stopped here
loop:off ⊘2 off; two continuations blocked in this process

How continuation is delivered

agent calls grow_loop()          pending = {delayMs: 0}
… run finishes (no tool calls) …
agent_before_settle              return {entries: [...entries, continuation], continue: true}
agent_start                      <- next turn starts from the boundary, no timer involved

agent_before_settle is Pi's last actionable boundary, which is why the default path needs no timer: the decision happens once, at the moment the run would otherwise go idle, and the stop proof in the final message is visible there. With after_seconds > 0 the same decision is deferred to an unref'd timer that can still be cancelled by a stop proof, Escape, a user message, or /loop off.

Verified end to end on Pi 1.6.1 (python3 scripts/rpc-smoke.py):

TOOL grow_loop
turn_end       STEP1 done
agent_start            <- continuation started at the settle boundary, no timer
turn_end       ■ 结束(smoke ok)
agent_settled          <- exactly once: the stop proof ended it

Migration from @llblab/pi-grow-loop

pi uninstall npm:@llblab/pi-grow-loop
pi install npm:pi-loop-core

What changes for you:

  • The tool name (grow_loop) is unchanged. The continuation prompt is now three customizable tiers (see above); if you need the old while true | grow loop text back, set it explicitly: /loop prompt all "while true | grow loop".
  • after_seconds keeps its meaning, but omitting it no longer means "3 s later" — it means "when this run settles" (strictly earlier, and no timer).
  • State moves from state/loop-switch/ / grow-loop's own status to state/loop-core/. Scripts that write {"enabled": false} need the new path (one sed).
  • Text patches into the package's dist/ (a common workaround for the old prompt guideline) are unnecessary: the switch, the reminder and the child guard are first-class here.

Why not a third-party loop plugin?

We migrated from @llblab/pi-grow-loop (last commit 2026-11-11, no ecosystem, a promptGuidelines string that every host had to text-patch to get visible checkpoints; we filed llblab/pi-grow-loop#1 with the measurements). The alternatives we evaluated are ralph-style checklist loops (@tmustier/pi-ralph-wiggum, @lnilluv/pi-ralph-loop) — a different, more prescriptive model: an up-front .ralph/*.md checklist, ralph_done per item, <promise>COMPLETE</promise>, per-loop buffers.

If you want that checklist-driven shape, use them; they are good at it. pi-loop-core is for the other shape: the agent reads project reality each turn and decides whether another bounded slice is worth running.

Development

npm install              # typebox only
npm test                 # 42 tests against a fake Pi host (no Pi process needed)
npm run check            # import the extension
npm run validate         # test + check + npm pack --dry-run
python3 scripts/rpc-smoke.py   # real Pi over RPC (~10 s): grow_loop -> next run -> ■ 结束

Layout:

extensions/pi-loop-core.ts   the whole plugin (~760 lines, no build step)
skills/autonomous-loop/      loop discipline the agent reads when the user says "grow loop"
test/                        fake-host harness + assertions
scripts/rpc-smoke.py         end-to-end smoke against a real Pi process

The extension is plain TypeScript loaded by Pi directly — there is no build step, so what you read is what runs.

License

MIT