@hank-warren/pi-loop 0.9.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +35 -32
- package/package.json +1 -1
- package/skills/pi-loop/SKILL.md +64 -45
- package/src/command.ts +33 -187
- package/src/complete-tool.ts +1 -1
- package/src/index.ts +119 -87
- package/src/ledger.ts +2 -2
- package/src/loop-action-menus.ts +2 -1
- package/src/loop-launch-menu.ts +158 -0
- package/src/loop-manager-menu.ts +191 -0
- package/src/loop.ts +120 -25
- package/src/manager.ts +118 -104
- package/src/messages.ts +1 -1
- package/src/objective.ts +25 -0
- package/src/planning.ts +67 -23
- package/src/presentation.ts +55 -18
- package/src/progress-tool.ts +1 -1
- package/src/propose-tool.ts +35 -14
- package/src/settings.ts +27 -22
- package/src/state.ts +33 -0
- package/src/wait-tool.ts +1 -1
- package/src/widget.ts +7 -3
- package/src/inline-command.ts +0 -159
- package/src/inline-invocation.ts +0 -109
- package/src/start-tool.ts +0 -199
package/src/start-tool.ts
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `loop_start`: the model-invoked way into a loop.
|
|
3
|
-
*
|
|
4
|
-
* Pi only dispatches `/loop` when it starts the message, so a mid-prompt
|
|
5
|
-
* `quick check /loop 10m get CI green` arrives as ordinary prose. The
|
|
6
|
-
* inline-invocation hooks append a one-turn reminder to the system prompt for
|
|
7
|
-
* exactly that message, and this tool is what the reminder points at. It
|
|
8
|
-
* reuses `LoopController.startLoop`, the same path the `/loop` command takes,
|
|
9
|
-
* so replacement rules, the loop_complete availability guard, the ledger, the
|
|
10
|
-
* kickoff anchor, and persistence all behave identically.
|
|
11
|
-
*
|
|
12
|
-
* The hard armed-gate is the deliberate divergence from pi-goal's equivalent
|
|
13
|
-
* tool, which relied on prompt guidelines alone. A loop is *self-continuing*:
|
|
14
|
-
* a spurious start does not produce one unwanted answer, it produces turns
|
|
15
|
-
* until a cap. So the tool refuses outright unless the inline hint armed for
|
|
16
|
-
* the turn that is calling it.
|
|
17
|
-
*
|
|
18
|
-
* The one thing this path may decide that the `/loop` command cannot is the
|
|
19
|
-
* loop's completion criteria. They are otherwise a deterministic split of the
|
|
20
|
-
* objective's grammar, which turns a context sentence into a gate criterion;
|
|
21
|
-
* a model that read the objective can do better. It is accepted only *here*,
|
|
22
|
-
* at start, before any work exists to grade and with the user seeing the
|
|
23
|
-
* criteria echoed back — the point where the incentive to write an easy gate
|
|
24
|
-
* is weakest. After start they are immutable, exactly as a derived set is.
|
|
25
|
-
*
|
|
26
|
-
* Registered unconditionally, like the other loop tools: the tool set is part
|
|
27
|
-
* of the cached request prefix, so it never changes with loop state.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
31
|
-
import { Type } from "typebox";
|
|
32
|
-
import { LOOP_COMPLETE_TOOL } from "./complete-tool.js";
|
|
33
|
-
import { formatDuration, parseDuration, parseInterval } from "./interval.js";
|
|
34
|
-
import { MAX_CRITERIA, MAX_DESCRIPTION_LENGTH } from "./ledger.js";
|
|
35
|
-
import type { InlineInvocationState } from "./inline-invocation.js";
|
|
36
|
-
import type { LoopController } from "./loop.js";
|
|
37
|
-
|
|
38
|
-
export const LOOP_START_TOOL = "loop_start";
|
|
39
|
-
|
|
40
|
-
/** Long enough for a real objective, short enough to reject a pasted file. */
|
|
41
|
-
const MAX_OBJECTIVE_LENGTH = 4_000;
|
|
42
|
-
|
|
43
|
-
export function registerLoopStartTool(
|
|
44
|
-
pi: ExtensionAPI,
|
|
45
|
-
controller: LoopController,
|
|
46
|
-
invocation: InlineInvocationState,
|
|
47
|
-
) {
|
|
48
|
-
pi.registerTool(
|
|
49
|
-
defineTool({
|
|
50
|
-
name: LOOP_START_TOOL,
|
|
51
|
-
label: "Loop Start",
|
|
52
|
-
description:
|
|
53
|
-
"Start a /loop for an objective the user explicitly invoked with an inline /loop or loop: token in their message. Only for explicit invocations: never start a loop from general conversation, your own initiative, or an instruction that merely sounds loop-like. The objective is the text following the token.",
|
|
54
|
-
promptSnippet:
|
|
55
|
-
"Start a /loop when the user's message contains an explicit inline /loop or loop: invocation",
|
|
56
|
-
promptGuidelines: [
|
|
57
|
-
"Call loop_start only when the user's message contains an explicit `/loop <objective>` or `loop: <objective>` token. Never start a loop without that token, no matter how loop-like the request sounds; the tool refuses when the turn carries no inline invocation.",
|
|
58
|
-
"If the user is discussing, quoting, or documenting the /loop command rather than invoking it — asking how it works, pasting a transcript, or editing text that mentions it — do not call loop_start.",
|
|
59
|
-
"Pass the objective text that follows the token, without the token itself. A leading interval (`10m`, `2h`) and flags like `--max 5` or `--expires 3d` become the interval, max, and expires parameters, not part of the objective.",
|
|
60
|
-
"Call loop_start before doing any of the objective's work, then continue working toward it in the same turn.",
|
|
61
|
-
"Leave the criteria parameter out by default: the extension splits the objective into completion criteria on its own (bullets, else sentences, else the whole objective). Propose criteria only when that split would misfire — when the objective mixes requirements with context sentences (`fix CI. it has been red since Tuesday.`), or packs several requirements into one sentence.",
|
|
62
|
-
"Every criterion you propose must be a faithful restatement of something the user asked for: never fewer, weaker, or easier than the objective as typed, and never a requirement they did not state. They are echoed back to the user at start and frozen afterwards — you may only ever flip a criterion's passes field.",
|
|
63
|
-
"When in doubt, omit criteria and let the deterministic split stand.",
|
|
64
|
-
"Never call loop_complete in the same turn as loop_start: the starting turn has not done the work, and completion needs cited evidence per criterion.",
|
|
65
|
-
"Before your first loop_start this session, read the pi-loop skill: the objective is split into the completion criteria this loop will be gated on, so its wording is the leverage point.",
|
|
66
|
-
],
|
|
67
|
-
parameters: Type.Object({
|
|
68
|
-
objective: Type.String({
|
|
69
|
-
minLength: 1,
|
|
70
|
-
maxLength: MAX_OBJECTIVE_LENGTH,
|
|
71
|
-
description:
|
|
72
|
-
"The loop objective, including how the loop knows it is done: the user's text following the /loop or loop: token, verbatim, without the token, the interval, or flags.",
|
|
73
|
-
}),
|
|
74
|
-
interval: Type.Optional(
|
|
75
|
-
Type.String({
|
|
76
|
-
description:
|
|
77
|
-
"Fallback wake interval from the invocation, e.g. '10m' or '2h'. Omit when the user named none.",
|
|
78
|
-
}),
|
|
79
|
-
),
|
|
80
|
-
max: Type.Optional(
|
|
81
|
-
Type.Integer({
|
|
82
|
-
minimum: 1,
|
|
83
|
-
description:
|
|
84
|
-
"Cap on the turns the loop causes (continuations and pokes), from a --max flag in the invocation.",
|
|
85
|
-
}),
|
|
86
|
-
),
|
|
87
|
-
expires: Type.Optional(
|
|
88
|
-
Type.String({
|
|
89
|
-
description: "Loop lifetime from an --expires flag in the invocation, e.g. '3d'.",
|
|
90
|
-
}),
|
|
91
|
-
),
|
|
92
|
-
criteria: Type.Optional(
|
|
93
|
-
Type.Array(
|
|
94
|
-
Type.String({ minLength: 1, maxLength: MAX_DESCRIPTION_LENGTH }),
|
|
95
|
-
{
|
|
96
|
-
minItems: 1,
|
|
97
|
-
maxItems: MAX_CRITERIA,
|
|
98
|
-
description:
|
|
99
|
-
"Optional completion criteria for this loop, each one checkable requirement restated faithfully from the user's objective. Replaces the deterministic split of the objective, so omit it unless that split would misfire.",
|
|
100
|
-
},
|
|
101
|
-
),
|
|
102
|
-
),
|
|
103
|
-
}),
|
|
104
|
-
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
105
|
-
// The gate. Everything below is ordinary validation; this is the
|
|
106
|
-
// one check that makes a self-continuing tool safe to expose.
|
|
107
|
-
if (!invocation.invokedThisTurn) {
|
|
108
|
-
return refusal(
|
|
109
|
-
`${LOOP_START_TOOL} is only available on a turn whose user message contains an explicit inline /loop or loop: invocation. This turn has none, so no loop was started. If the user wants one, they can type /loop <interval> <objective>.`,
|
|
110
|
-
{},
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
const objective = params.objective.trim();
|
|
114
|
-
if (!objective) {
|
|
115
|
-
return refusal("Loop not started: the objective is empty.", {});
|
|
116
|
-
}
|
|
117
|
-
const criteria = params.criteria?.map((description) => description.trim());
|
|
118
|
-
const badCriteria = criteria && describeBadCriteria(criteria);
|
|
119
|
-
if (badCriteria) {
|
|
120
|
-
return refusal(
|
|
121
|
-
`Loop not started: ${badCriteria}. Pass one short checkable requirement per entry, or omit criteria to split the objective deterministically.`,
|
|
122
|
-
{ objective },
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
const existing = controller.state;
|
|
126
|
-
if (existing && existing.status !== "stopped") {
|
|
127
|
-
return refusal(
|
|
128
|
-
`Loop not started: a loop already exists in this session (${existing.status}). The user can replace it with /loop, or stop it with /loop stop.`,
|
|
129
|
-
{ existingLoopId: existing.id },
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
const intervalToken = params.interval?.trim() || controller.settings.defaultInterval;
|
|
133
|
-
const interval = parseInterval(intervalToken);
|
|
134
|
-
if (!interval) {
|
|
135
|
-
return refusal(
|
|
136
|
-
`Loop not started: invalid interval ${intervalToken}. Use <number><unit> with unit s, m, h, or d, e.g. 10m.`,
|
|
137
|
-
{ objective },
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
let expiresInMs: number | undefined;
|
|
141
|
-
if (params.expires !== undefined) {
|
|
142
|
-
expiresInMs = parseDuration(params.expires.trim());
|
|
143
|
-
if (expiresInMs === undefined) {
|
|
144
|
-
return refusal(
|
|
145
|
-
`Loop not started: invalid expiry ${params.expires}. Use <number><unit> with unit s, m, h, or d, e.g. 3d.`,
|
|
146
|
-
{ objective },
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const result = controller.startLoop(ctx, {
|
|
151
|
-
kind: "start",
|
|
152
|
-
requestedMs: interval.requestedMs,
|
|
153
|
-
intervalMs: interval.effectiveMs,
|
|
154
|
-
clamped: interval.clamped,
|
|
155
|
-
...(params.max === undefined ? {} : { maxTurns: params.max }),
|
|
156
|
-
...(expiresInMs === undefined ? {} : { expiresInMs }),
|
|
157
|
-
...(criteria === undefined ? {} : { criteria }),
|
|
158
|
-
prompt: objective,
|
|
159
|
-
});
|
|
160
|
-
if (!result.ok) return refusal(`Loop not started: ${result.message}`, { objective });
|
|
161
|
-
const loop = result.loop;
|
|
162
|
-
return {
|
|
163
|
-
content: toolContent(
|
|
164
|
-
`Loop started (loop_id ${loop.id}): ${objective}. Fallback wake every ${formatDuration(loop.intervalMs)}. Keep working the objective this turn; the loop continues at every idle boundary until you call ${LOOP_COMPLETE_TOOL} with this loop_id and cited evidence for every criterion, a cap is reached, or the user stops it. Do not call ${LOOP_COMPLETE_TOOL} in this turn.`,
|
|
165
|
-
),
|
|
166
|
-
details: { loopId: loop.id, objective, intervalMs: loop.intervalMs },
|
|
167
|
-
};
|
|
168
|
-
},
|
|
169
|
-
}),
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Why a proposed criteria list cannot be used, or undefined when it can.
|
|
175
|
-
*
|
|
176
|
-
* A list that says nothing is worse than no list: it would replace the
|
|
177
|
-
* deterministic split with a gate the model wrote and can pass by saying
|
|
178
|
-
* anything. So a malformed list refuses the start rather than falling back
|
|
179
|
-
* silently, which would leave the model believing its criteria were accepted.
|
|
180
|
-
*/
|
|
181
|
-
function describeBadCriteria(criteria: readonly string[]): string | undefined {
|
|
182
|
-
if (criteria.length === 0) return "the criteria list is empty";
|
|
183
|
-
if (criteria.length > MAX_CRITERIA) {
|
|
184
|
-
return `a loop takes at most ${MAX_CRITERIA} criteria and ${criteria.length} were given`;
|
|
185
|
-
}
|
|
186
|
-
if (criteria.some((description) => !description)) return "one of the criteria is blank";
|
|
187
|
-
if (criteria.some((description) => description.length > MAX_DESCRIPTION_LENGTH)) {
|
|
188
|
-
return `a criterion may be at most ${MAX_DESCRIPTION_LENGTH} characters`;
|
|
189
|
-
}
|
|
190
|
-
return undefined;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function refusal(text: string, details: Record<string, unknown>) {
|
|
194
|
-
return { content: toolContent(text), details, isError: true };
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function toolContent(text: string) {
|
|
198
|
-
return [{ type: "text" as const, text }];
|
|
199
|
-
}
|