@narumitw/pi-goal 0.27.2 → 0.31.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/README.md +13 -11
- package/package.json +11 -6
- package/src/accounting.ts +1 -3
- package/src/commands.ts +68 -5
- package/src/goal.ts +22 -3
- package/src/index.ts +1 -0
- package/src/menu.ts +431 -0
- package/src/queue.ts +25 -1
- package/src/runtime.ts +81 -7
- package/src/settings-ui.ts +430 -0
- package/src/settings.ts +70 -1
package/src/menu.ts
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { formatDuration } from "./accounting.js";
|
|
3
|
+
import { parseTokenBudget } from "./command.js";
|
|
4
|
+
import type { GoalCommandController } from "./commands.js";
|
|
5
|
+
import type { ActiveGoal, PendingQueueAction } from "./persistence.js";
|
|
6
|
+
import { goalQueueIdentity } from "./queue.js";
|
|
7
|
+
import type { GoalRuntime } from "./runtime.js";
|
|
8
|
+
|
|
9
|
+
export const GOAL_MENU_ACTIONS = {
|
|
10
|
+
start: "Start a goal…",
|
|
11
|
+
startBudget: "Start with token budget…",
|
|
12
|
+
pause: "Pause goal",
|
|
13
|
+
resume: "Resume goal",
|
|
14
|
+
increaseBudget: "Increase budget and resume…",
|
|
15
|
+
edit: "Edit goal…",
|
|
16
|
+
replace: "Replace goal…",
|
|
17
|
+
status: "View full status",
|
|
18
|
+
queue: "Queue…",
|
|
19
|
+
settings: "Settings…",
|
|
20
|
+
help: "Help",
|
|
21
|
+
clear: "Clear goal…",
|
|
22
|
+
close: "Close",
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
const QUEUE_ACTIONS = {
|
|
26
|
+
add: "Add goal…",
|
|
27
|
+
prioritize: "Prioritize goal…",
|
|
28
|
+
skip: "Skip current goal…",
|
|
29
|
+
dropLast: "Drop last goal…",
|
|
30
|
+
back: "Back",
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
interface GoalMenuRuntimeView {
|
|
34
|
+
activeGoal?: ActiveGoal;
|
|
35
|
+
queuedGoals: ActiveGoal[];
|
|
36
|
+
pendingQueueAction?: PendingQueueAction;
|
|
37
|
+
queueFrozen: boolean;
|
|
38
|
+
settings: GoalRuntime["settings"];
|
|
39
|
+
recordGoalUsage?: GoalRuntime["recordGoalUsage"];
|
|
40
|
+
persistGoal?: GoalRuntime["persistGoal"];
|
|
41
|
+
updateStatus?: GoalRuntime["updateStatus"];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface GoalMenuState {
|
|
45
|
+
title: string;
|
|
46
|
+
actions: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type ShowSettings = (ctx: ExtensionCommandContext) => Promise<void>;
|
|
50
|
+
|
|
51
|
+
export function buildGoalMenuState(runtime: GoalMenuRuntimeView): GoalMenuState {
|
|
52
|
+
const goal = runtime.activeGoal;
|
|
53
|
+
const queueCount = runtime.queuedGoals.length;
|
|
54
|
+
const state = runtime.queueFrozen
|
|
55
|
+
? "Queue frozen"
|
|
56
|
+
: runtime.pendingQueueAction
|
|
57
|
+
? "Waiting for Pi to settle"
|
|
58
|
+
: displayStatus(goal?.status);
|
|
59
|
+
const details = goal
|
|
60
|
+
? [
|
|
61
|
+
goal.tokenBudget === undefined
|
|
62
|
+
? formatDuration(goal.timeUsedSeconds)
|
|
63
|
+
: `${formatTokenCount(goal.tokensUsed)}/${formatTokenCount(goal.tokenBudget)}`,
|
|
64
|
+
`${goal.automaticModelTurns} automatic responses`,
|
|
65
|
+
...(queueCount > 0 ? [`${queueCount} queued`] : []),
|
|
66
|
+
].join(" · ")
|
|
67
|
+
: "No goal is currently set";
|
|
68
|
+
const title = goal
|
|
69
|
+
? `Goal · ${state}\n${safeGoalMenuText(goal.text)}\n${details}`
|
|
70
|
+
: `Goal · ${state}\n${details}`;
|
|
71
|
+
|
|
72
|
+
if (runtime.queueFrozen || runtime.pendingQueueAction) {
|
|
73
|
+
return {
|
|
74
|
+
title,
|
|
75
|
+
actions: [
|
|
76
|
+
GOAL_MENU_ACTIONS.status,
|
|
77
|
+
GOAL_MENU_ACTIONS.settings,
|
|
78
|
+
GOAL_MENU_ACTIONS.help,
|
|
79
|
+
GOAL_MENU_ACTIONS.clear,
|
|
80
|
+
GOAL_MENU_ACTIONS.close,
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const actions: string[] = [];
|
|
86
|
+
if (!goal || goal.status === "complete") {
|
|
87
|
+
actions.push(GOAL_MENU_ACTIONS.start, GOAL_MENU_ACTIONS.startBudget);
|
|
88
|
+
} else if (goal.status === "active") {
|
|
89
|
+
actions.push(GOAL_MENU_ACTIONS.pause);
|
|
90
|
+
} else if (goal.status === "budget_limited") {
|
|
91
|
+
actions.push(GOAL_MENU_ACTIONS.increaseBudget);
|
|
92
|
+
} else {
|
|
93
|
+
actions.push(GOAL_MENU_ACTIONS.resume);
|
|
94
|
+
}
|
|
95
|
+
if (goal && goal.status !== "complete") {
|
|
96
|
+
actions.push(GOAL_MENU_ACTIONS.edit, GOAL_MENU_ACTIONS.replace);
|
|
97
|
+
}
|
|
98
|
+
if (goal) actions.push(GOAL_MENU_ACTIONS.status);
|
|
99
|
+
if (goal && (runtime.settings.experimental.goals || queueCount > 0)) {
|
|
100
|
+
actions.push(GOAL_MENU_ACTIONS.queue);
|
|
101
|
+
}
|
|
102
|
+
actions.push(GOAL_MENU_ACTIONS.settings, GOAL_MENU_ACTIONS.help);
|
|
103
|
+
if (goal) actions.push(GOAL_MENU_ACTIONS.clear);
|
|
104
|
+
actions.push(GOAL_MENU_ACTIONS.close);
|
|
105
|
+
return { title, actions };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function showGoalManager(
|
|
109
|
+
runtime: GoalMenuRuntimeView,
|
|
110
|
+
commands: GoalCommandController,
|
|
111
|
+
ctx: ExtensionCommandContext,
|
|
112
|
+
showSettings: ShowSettings,
|
|
113
|
+
): Promise<void> {
|
|
114
|
+
if (ctx.mode !== "tui") {
|
|
115
|
+
commands.showGoal(ctx);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
while (true) {
|
|
119
|
+
refreshGoalMenuState(runtime, ctx);
|
|
120
|
+
const state = buildGoalMenuState(runtime);
|
|
121
|
+
const displayedGoal = runtime.activeGoal;
|
|
122
|
+
const selected = await ctx.ui.select(state.title, state.actions);
|
|
123
|
+
if (!selected || selected === GOAL_MENU_ACTIONS.close) return;
|
|
124
|
+
switch (selected) {
|
|
125
|
+
case GOAL_MENU_ACTIONS.start:
|
|
126
|
+
await startFromMenu(commands, ctx);
|
|
127
|
+
return;
|
|
128
|
+
case GOAL_MENU_ACTIONS.startBudget:
|
|
129
|
+
await startFromMenu(commands, ctx, true);
|
|
130
|
+
return;
|
|
131
|
+
case GOAL_MENU_ACTIONS.pause:
|
|
132
|
+
if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) continue;
|
|
133
|
+
commands.pauseGoal(ctx);
|
|
134
|
+
return;
|
|
135
|
+
case GOAL_MENU_ACTIONS.resume:
|
|
136
|
+
if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) continue;
|
|
137
|
+
await commands.resumeGoal(ctx);
|
|
138
|
+
return;
|
|
139
|
+
case GOAL_MENU_ACTIONS.increaseBudget:
|
|
140
|
+
if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) continue;
|
|
141
|
+
await increaseBudget(runtime, commands, ctx);
|
|
142
|
+
return;
|
|
143
|
+
case GOAL_MENU_ACTIONS.edit:
|
|
144
|
+
if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) continue;
|
|
145
|
+
await editFromMenu(runtime, commands, ctx);
|
|
146
|
+
return;
|
|
147
|
+
case GOAL_MENU_ACTIONS.replace:
|
|
148
|
+
await startFromMenu(commands, ctx);
|
|
149
|
+
return;
|
|
150
|
+
case GOAL_MENU_ACTIONS.status:
|
|
151
|
+
commands.showGoal(ctx);
|
|
152
|
+
return;
|
|
153
|
+
case GOAL_MENU_ACTIONS.queue:
|
|
154
|
+
if ((await showQueueMenu(runtime, commands, ctx)) === "back") continue;
|
|
155
|
+
return;
|
|
156
|
+
case GOAL_MENU_ACTIONS.settings:
|
|
157
|
+
await showSettings(ctx);
|
|
158
|
+
continue;
|
|
159
|
+
case GOAL_MENU_ACTIONS.help:
|
|
160
|
+
ctx.ui.notify(goalHelp(), "info");
|
|
161
|
+
return;
|
|
162
|
+
case GOAL_MENU_ACTIONS.clear: {
|
|
163
|
+
const previewedQueue = goalQueueIdentity(
|
|
164
|
+
runtime.activeGoal,
|
|
165
|
+
runtime.queuedGoals,
|
|
166
|
+
runtime.pendingQueueAction,
|
|
167
|
+
);
|
|
168
|
+
if (!(await confirmClear(runtime, ctx))) continue;
|
|
169
|
+
if (
|
|
170
|
+
goalQueueIdentity(runtime.activeGoal, runtime.queuedGoals, runtime.pendingQueueAction) !==
|
|
171
|
+
previewedQueue
|
|
172
|
+
) {
|
|
173
|
+
ctx.ui.notify(
|
|
174
|
+
"The goal queue changed while the dialog was open. Reopen /goal and try again.",
|
|
175
|
+
"warning",
|
|
176
|
+
);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
commands.clearGoal(ctx);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function refreshGoalMenuState(runtime: GoalMenuRuntimeView, ctx: ExtensionCommandContext) {
|
|
187
|
+
const goal = runtime.activeGoal;
|
|
188
|
+
if (!goal || runtime.queueFrozen) return;
|
|
189
|
+
runtime.recordGoalUsage?.(goal, ctx);
|
|
190
|
+
runtime.persistGoal?.(goal);
|
|
191
|
+
runtime.updateStatus?.(ctx, goal);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function safeGoalMenuText(value: string, maxCharacters = 120) {
|
|
195
|
+
const sanitized = [...value]
|
|
196
|
+
.map((character) => (isTerminalControl(character) ? " " : character))
|
|
197
|
+
.join("")
|
|
198
|
+
.replace(/\s+/gu, " ")
|
|
199
|
+
.trim();
|
|
200
|
+
const characters = [...sanitized];
|
|
201
|
+
return characters.length <= maxCharacters
|
|
202
|
+
? sanitized
|
|
203
|
+
: `${characters.slice(0, maxCharacters).join("")}…`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function startFromMenu(
|
|
207
|
+
commands: GoalCommandController,
|
|
208
|
+
ctx: ExtensionCommandContext,
|
|
209
|
+
withBudget = false,
|
|
210
|
+
) {
|
|
211
|
+
const objective = (await ctx.ui.editor("Goal objective", ""))?.trim();
|
|
212
|
+
if (!objective) return;
|
|
213
|
+
const tokenBudget = withBudget ? await askTokenBudget(ctx) : undefined;
|
|
214
|
+
if (withBudget && tokenBudget === undefined) return;
|
|
215
|
+
await commands.startGoal(objective, tokenBudget, ctx);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function editFromMenu(
|
|
219
|
+
runtime: GoalMenuRuntimeView,
|
|
220
|
+
commands: GoalCommandController,
|
|
221
|
+
ctx: ExtensionCommandContext,
|
|
222
|
+
) {
|
|
223
|
+
const goal = runtime.activeGoal;
|
|
224
|
+
if (!goal) return;
|
|
225
|
+
const objective = (await ctx.ui.editor("Edit goal objective", goal.text))?.trim();
|
|
226
|
+
if (!objective || objective === goal.text) return;
|
|
227
|
+
if (!requireCurrentMenuGoal(runtime, goal, ctx)) return;
|
|
228
|
+
if (goal.status === "active") {
|
|
229
|
+
const confirmed = await ctx.ui.confirm(
|
|
230
|
+
"Apply goal edit?",
|
|
231
|
+
`Current goal:\n${safeGoalMenuText(goal.text, 4_000)}\n\nUpdated goal:\n${safeGoalMenuText(objective, 4_000)}\n\nApplying this edit starts a new guarded goal instance.`,
|
|
232
|
+
);
|
|
233
|
+
if (!confirmed || !requireCurrentMenuGoal(runtime, goal, ctx)) return;
|
|
234
|
+
}
|
|
235
|
+
await commands.editGoal(objective, undefined, ctx);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function increaseBudget(
|
|
239
|
+
runtime: GoalMenuRuntimeView,
|
|
240
|
+
commands: GoalCommandController,
|
|
241
|
+
ctx: ExtensionCommandContext,
|
|
242
|
+
) {
|
|
243
|
+
const goal = runtime.activeGoal;
|
|
244
|
+
if (!goal) return;
|
|
245
|
+
const budget = await askTokenBudget(ctx, goal.tokenBudget);
|
|
246
|
+
if (budget === undefined || !requireCurrentMenuGoal(runtime, goal, ctx)) return;
|
|
247
|
+
if (budget <= goal.tokensUsed) {
|
|
248
|
+
ctx.ui.notify(
|
|
249
|
+
`Token budget must be greater than current usage (${formatTokenCount(goal.tokensUsed)}).`,
|
|
250
|
+
"warning",
|
|
251
|
+
);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const confirmed = await ctx.ui.confirm(
|
|
255
|
+
"Increase goal budget?",
|
|
256
|
+
`Goal: ${safeGoalMenuText(goal.text, 4_000)}\n\nBudget: ${formatTokenCount(goal.tokenBudget ?? 0)} → ${formatTokenCount(budget)}\nCurrent usage: ${formatTokenCount(goal.tokensUsed)}\n\nThe goal will resume immediately.`,
|
|
257
|
+
);
|
|
258
|
+
if (!confirmed || !requireCurrentMenuGoal(runtime, goal, ctx)) return;
|
|
259
|
+
await commands.editGoal(goal.text, budget, ctx);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function askTokenBudget(ctx: ExtensionCommandContext, current?: number) {
|
|
263
|
+
const raw = await ctx.ui.input(
|
|
264
|
+
"Token budget",
|
|
265
|
+
current === undefined ? "100k" : formatTokenCount(current),
|
|
266
|
+
);
|
|
267
|
+
if (raw === undefined) return undefined;
|
|
268
|
+
const budget = parseTokenBudget(raw);
|
|
269
|
+
if (budget === undefined)
|
|
270
|
+
ctx.ui.notify(`Invalid token budget: ${safeGoalMenuText(raw)}`, "warning");
|
|
271
|
+
return budget;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function showQueueMenu(
|
|
275
|
+
runtime: GoalMenuRuntimeView,
|
|
276
|
+
commands: GoalCommandController,
|
|
277
|
+
ctx: ExtensionCommandContext,
|
|
278
|
+
) {
|
|
279
|
+
const goal = runtime.activeGoal;
|
|
280
|
+
if (!goal) return;
|
|
281
|
+
const actions: string[] = [QUEUE_ACTIONS.add, QUEUE_ACTIONS.prioritize];
|
|
282
|
+
if (runtime.queuedGoals.length > 0) actions.push(QUEUE_ACTIONS.skip, QUEUE_ACTIONS.dropLast);
|
|
283
|
+
actions.push(QUEUE_ACTIONS.back);
|
|
284
|
+
const selected = await ctx.ui.select(
|
|
285
|
+
`Goal queue · ${runtime.queuedGoals.length + 1} total\nCurrent: ${safeGoalMenuText(goal.text)}`,
|
|
286
|
+
actions,
|
|
287
|
+
);
|
|
288
|
+
if (!selected || selected === QUEUE_ACTIONS.back) return "back" as const;
|
|
289
|
+
if (selected === QUEUE_ACTIONS.add) {
|
|
290
|
+
const objective = (await ctx.ui.editor("Add goal to queue", ""))?.trim();
|
|
291
|
+
if (objective) await commands.addGoal(objective, undefined, ctx);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (selected === QUEUE_ACTIONS.prioritize) {
|
|
295
|
+
const objective = (await ctx.ui.editor("Prioritize goal", ""))?.trim();
|
|
296
|
+
if (!objective || !requireCurrentQueueHead(runtime, goal, ctx)) return;
|
|
297
|
+
const confirmed = await ctx.ui.confirm(
|
|
298
|
+
"Prioritize goal?",
|
|
299
|
+
`New priority goal:\n${safeGoalMenuText(objective, 4_000)}\n\nCurrent goal moved to the queue:\n${safeGoalMenuText(goal.text, 4_000)}`,
|
|
300
|
+
);
|
|
301
|
+
if (confirmed && requireCurrentQueueHead(runtime, goal, ctx)) {
|
|
302
|
+
await commands.prioritizeGoal(objective, undefined, ctx);
|
|
303
|
+
}
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (selected === QUEUE_ACTIONS.skip) {
|
|
307
|
+
const next = runtime.queuedGoals[0];
|
|
308
|
+
const nextEffect = !next
|
|
309
|
+
? "No goal remains"
|
|
310
|
+
: next.status === "queued"
|
|
311
|
+
? `Start next goal:\n${safeGoalMenuText(next.text, 4_000)}`
|
|
312
|
+
: `Next goal remains ${displayStatus(next.status).toLowerCase()}:\n${safeGoalMenuText(next.text, 4_000)}`;
|
|
313
|
+
const confirmed = await ctx.ui.confirm(
|
|
314
|
+
"Skip current goal?",
|
|
315
|
+
`Remove current goal:\n${safeGoalMenuText(goal.text, 4_000)}\n\n${nextEffect}`,
|
|
316
|
+
);
|
|
317
|
+
if (confirmed && requireCurrentQueueSelection(runtime, goal, next, "first", ctx)) {
|
|
318
|
+
await commands.skipGoal(ctx);
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (selected === QUEUE_ACTIONS.dropLast) {
|
|
323
|
+
const last = runtime.queuedGoals.at(-1) ?? goal;
|
|
324
|
+
const confirmed = await ctx.ui.confirm(
|
|
325
|
+
"Drop last goal?",
|
|
326
|
+
`Remove from queue:\n${safeGoalMenuText(last.text, 4_000)}`,
|
|
327
|
+
);
|
|
328
|
+
if (confirmed && requireCurrentQueueSelection(runtime, goal, last, "last", ctx)) {
|
|
329
|
+
commands.dropLastGoal(ctx);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async function confirmClear(runtime: GoalMenuRuntimeView, ctx: ExtensionCommandContext) {
|
|
335
|
+
const goals = [runtime.activeGoal, ...runtime.queuedGoals].filter(
|
|
336
|
+
(goal): goal is ActiveGoal => goal !== undefined,
|
|
337
|
+
);
|
|
338
|
+
const pendingPriority =
|
|
339
|
+
runtime.pendingQueueAction?.kind === "prioritize"
|
|
340
|
+
? runtime.pendingQueueAction.objective
|
|
341
|
+
: undefined;
|
|
342
|
+
const summaries = [
|
|
343
|
+
...goals.map((goal) => safeGoalMenuText(goal.text, 4_000)),
|
|
344
|
+
...(pendingPriority ? [`Pending priority: ${safeGoalMenuText(pendingPriority, 4_000)}`] : []),
|
|
345
|
+
];
|
|
346
|
+
if (summaries.length === 0) return false;
|
|
347
|
+
return ctx.ui.confirm(
|
|
348
|
+
summaries.length > 1 ? "Clear goal queue?" : "Clear goal?",
|
|
349
|
+
`Remove ${summaries.length === 1 ? "this goal" : `all ${summaries.length} goals`}:\n\n${summaries
|
|
350
|
+
.map((summary, index) => `${index + 1}. ${summary}`)
|
|
351
|
+
.join("\n")}\n\nThis cannot be undone.`,
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function requireCurrentQueueHead(
|
|
356
|
+
runtime: GoalMenuRuntimeView,
|
|
357
|
+
expectedGoal: ActiveGoal,
|
|
358
|
+
ctx: ExtensionCommandContext,
|
|
359
|
+
) {
|
|
360
|
+
if (runtime.activeGoal?.id === expectedGoal.id) return true;
|
|
361
|
+
ctx.ui.notify(
|
|
362
|
+
"The goal queue changed while the dialog was open. Reopen /goal and try again.",
|
|
363
|
+
"warning",
|
|
364
|
+
);
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function requireCurrentQueueSelection(
|
|
369
|
+
runtime: GoalMenuRuntimeView,
|
|
370
|
+
expectedGoal: ActiveGoal,
|
|
371
|
+
expectedQueuedGoal: ActiveGoal | undefined,
|
|
372
|
+
position: "first" | "last",
|
|
373
|
+
ctx: ExtensionCommandContext,
|
|
374
|
+
) {
|
|
375
|
+
const currentQueuedGoal =
|
|
376
|
+
position === "first"
|
|
377
|
+
? runtime.queuedGoals[0]
|
|
378
|
+
: (runtime.queuedGoals.at(-1) ?? runtime.activeGoal);
|
|
379
|
+
if (
|
|
380
|
+
runtime.activeGoal?.id === expectedGoal.id &&
|
|
381
|
+
currentQueuedGoal?.id === expectedQueuedGoal?.id
|
|
382
|
+
) {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
ctx.ui.notify(
|
|
386
|
+
"The goal queue changed while the dialog was open. Reopen /goal and try again.",
|
|
387
|
+
"warning",
|
|
388
|
+
);
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function requireCurrentMenuGoal(
|
|
393
|
+
runtime: GoalMenuRuntimeView,
|
|
394
|
+
expected: ActiveGoal,
|
|
395
|
+
ctx: ExtensionCommandContext,
|
|
396
|
+
) {
|
|
397
|
+
if (runtime.activeGoal?.id === expected.id) return true;
|
|
398
|
+
ctx.ui.notify(
|
|
399
|
+
"The active goal changed while the dialog was open. Reopen /goal and try again.",
|
|
400
|
+
"warning",
|
|
401
|
+
);
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function displayStatus(status?: ActiveGoal["status"]) {
|
|
406
|
+
if (!status) return "No goal";
|
|
407
|
+
if (status === "usage_limited") return "Usage limited";
|
|
408
|
+
if (status === "budget_limited") return "Budget limited";
|
|
409
|
+
return status[0]?.toUpperCase() + status.slice(1);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function formatTokenCount(tokens: number) {
|
|
413
|
+
return String(tokens);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function isTerminalControl(character: string) {
|
|
417
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
418
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function goalHelp() {
|
|
422
|
+
return [
|
|
423
|
+
"Goal menu",
|
|
424
|
+
"Use the menu for guided status, edits, queue management, settings, and confirmations.",
|
|
425
|
+
"Direct routes remain available for deterministic workflows:",
|
|
426
|
+
"/goal <objective>",
|
|
427
|
+
"/goal status | pause | resume | edit | clear",
|
|
428
|
+
"/goal --tokens 100k <objective>",
|
|
429
|
+
"Escape cancels the current menu or input without changing goal state.",
|
|
430
|
+
].join("\n");
|
|
431
|
+
}
|
package/src/queue.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { checkpointGoalActiveTime } from "./accounting.js";
|
|
3
|
-
import type { ActiveGoal } from "./persistence.js";
|
|
3
|
+
import type { ActiveGoal, PendingQueueAction } from "./persistence.js";
|
|
4
4
|
import { resetGoalSafetyEpoch } from "./safety.js";
|
|
5
5
|
|
|
6
6
|
export interface GoalQueueResult {
|
|
@@ -12,6 +12,30 @@ export interface DropLastGoalResult extends GoalQueueResult {
|
|
|
12
12
|
removed: ActiveGoal | undefined;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
export function goalQueueIdentity(
|
|
16
|
+
goal: ActiveGoal | undefined,
|
|
17
|
+
queue: readonly ActiveGoal[],
|
|
18
|
+
pendingAction: PendingQueueAction | undefined,
|
|
19
|
+
) {
|
|
20
|
+
const pendingIdentity =
|
|
21
|
+
pendingAction?.kind === "prioritize"
|
|
22
|
+
? [
|
|
23
|
+
pendingAction.kind,
|
|
24
|
+
pendingAction.objective,
|
|
25
|
+
pendingAction.tokenBudget,
|
|
26
|
+
pendingAction.displacedUsageFinalized,
|
|
27
|
+
]
|
|
28
|
+
: pendingAction
|
|
29
|
+
? [
|
|
30
|
+
pendingAction.kind,
|
|
31
|
+
pendingAction.goalId,
|
|
32
|
+
pendingAction.reason,
|
|
33
|
+
pendingAction.completedText,
|
|
34
|
+
]
|
|
35
|
+
: undefined;
|
|
36
|
+
return JSON.stringify([goal?.id, queue.map((queuedGoal) => queuedGoal.id), pendingIdentity]);
|
|
37
|
+
}
|
|
38
|
+
|
|
15
39
|
export function createQueuedGoal(
|
|
16
40
|
text: string,
|
|
17
41
|
tokenBudget: number | undefined,
|
package/src/runtime.ts
CHANGED
|
@@ -57,6 +57,7 @@ export interface CompletedGoalRun {
|
|
|
57
57
|
|
|
58
58
|
export interface StatusContext {
|
|
59
59
|
cwd: string;
|
|
60
|
+
mode?: "tui" | "rpc" | "json" | "print";
|
|
60
61
|
ui: {
|
|
61
62
|
confirm: (title: string, message: string) => Promise<boolean>;
|
|
62
63
|
notify: (message: string, level?: "info" | "warning" | "error") => void;
|
|
@@ -122,6 +123,22 @@ interface GoalTerminalDetails {
|
|
|
122
123
|
reason?: string;
|
|
123
124
|
}
|
|
124
125
|
|
|
126
|
+
export interface GoalSettingsRuntimeSnapshot {
|
|
127
|
+
settings: GoalSettings;
|
|
128
|
+
activeGoal?: ActiveGoal;
|
|
129
|
+
queueFrozen: boolean;
|
|
130
|
+
queueFreezeAwaitingSettle: boolean;
|
|
131
|
+
continuationIntent?: ContinuationTicket;
|
|
132
|
+
continuationDelivery?: ContinuationTicket;
|
|
133
|
+
goalRecovery?: GoalRecovery;
|
|
134
|
+
budgetWrapUp?: BudgetWrapUp;
|
|
135
|
+
guardAbortGoalId?: string;
|
|
136
|
+
staleGoalToolCallsBlocked: boolean;
|
|
137
|
+
cancelledContinuationMarkers: string[];
|
|
138
|
+
terminalDetails?: GoalTerminalDetails;
|
|
139
|
+
toolVisibility: GoalToolVisibilitySnapshot;
|
|
140
|
+
}
|
|
141
|
+
|
|
125
142
|
interface PendingGoalPrompt {
|
|
126
143
|
goalId: string;
|
|
127
144
|
resetSafetyEpoch: boolean;
|
|
@@ -146,6 +163,8 @@ const CONTRADICTORY_COMPLETION_PATTERNS = [
|
|
|
146
163
|
] as const;
|
|
147
164
|
// One instance belongs to one extension factory. It owns all mutable session state
|
|
148
165
|
// and the cross-cutting invariants used by command and lifecycle orchestration.
|
|
166
|
+
// Keep this state machine cohesive despite its size: prompt ownership, continuation,
|
|
167
|
+
// budget, safety, and tool-policy transitions share ordering-sensitive invariants.
|
|
149
168
|
export class GoalRuntime {
|
|
150
169
|
settings: GoalSettings = DEFAULT_GOAL_SETTINGS;
|
|
151
170
|
activeGoal?: ActiveGoal;
|
|
@@ -154,6 +173,7 @@ export class GoalRuntime {
|
|
|
154
173
|
queuedGoals: ActiveGoal[] = [];
|
|
155
174
|
pendingQueueAction?: PendingQueueAction;
|
|
156
175
|
queueFrozen = false;
|
|
176
|
+
queueFreezeAwaitingSettle = false;
|
|
157
177
|
completionStatusTimer?: NodeJS.Timeout;
|
|
158
178
|
continuationIntent?: ContinuationTicket;
|
|
159
179
|
continuationDelivery?: ContinuationTicket;
|
|
@@ -461,13 +481,13 @@ export class GoalRuntime {
|
|
|
461
481
|
return this.pauseGoalForSafety(ctx, "continuation_limit", abortTurn);
|
|
462
482
|
}
|
|
463
483
|
|
|
464
|
-
enforceNoProgressLimit(ctx: StatusContext) {
|
|
484
|
+
enforceNoProgressLimit(ctx: StatusContext, abortTurn = false) {
|
|
465
485
|
const goal = this.activeGoal;
|
|
466
486
|
const limit = this.settings.continuationLimits.noProgressTurns;
|
|
467
487
|
if (goal?.status !== "active" || limit === null || goal.toolFreeRepeatCount < limit) {
|
|
468
488
|
return false;
|
|
469
489
|
}
|
|
470
|
-
return this.pauseGoalForSafety(ctx, "no_progress",
|
|
490
|
+
return this.pauseGoalForSafety(ctx, "no_progress", abortTurn);
|
|
471
491
|
}
|
|
472
492
|
|
|
473
493
|
pauseGoalForSafety(ctx: StatusContext, cause: SafetyPauseCause, abortTurn: boolean) {
|
|
@@ -734,6 +754,7 @@ export class GoalRuntime {
|
|
|
734
754
|
this.queuedGoals = [];
|
|
735
755
|
this.pendingQueueAction = undefined;
|
|
736
756
|
this.queueFrozen = false;
|
|
757
|
+
this.queueFreezeAwaitingSettle = false;
|
|
737
758
|
this.clearPersistedGoal(ctx.cwd, clearedGoal, reason);
|
|
738
759
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
739
760
|
// Do not clear goalToolsUnlocked: after first activation, keep tools visible
|
|
@@ -831,6 +852,50 @@ export class GoalRuntime {
|
|
|
831
852
|
};
|
|
832
853
|
}
|
|
833
854
|
|
|
855
|
+
snapshotSettingsApplicationState(): GoalSettingsRuntimeSnapshot {
|
|
856
|
+
return {
|
|
857
|
+
settings: structuredClone(this.settings),
|
|
858
|
+
activeGoal: this.activeGoal ? structuredClone(this.activeGoal) : undefined,
|
|
859
|
+
queueFrozen: this.queueFrozen,
|
|
860
|
+
queueFreezeAwaitingSettle: this.queueFreezeAwaitingSettle,
|
|
861
|
+
continuationIntent: this.continuationIntent
|
|
862
|
+
? structuredClone(this.continuationIntent)
|
|
863
|
+
: undefined,
|
|
864
|
+
continuationDelivery: this.continuationDelivery
|
|
865
|
+
? structuredClone(this.continuationDelivery)
|
|
866
|
+
: undefined,
|
|
867
|
+
goalRecovery: this.goalRecovery ? structuredClone(this.goalRecovery) : undefined,
|
|
868
|
+
budgetWrapUp: this.budgetWrapUp ? structuredClone(this.budgetWrapUp) : undefined,
|
|
869
|
+
guardAbortGoalId: this.guardAbortGoalId,
|
|
870
|
+
staleGoalToolCallsBlocked: this.staleGoalToolCallsBlocked,
|
|
871
|
+
cancelledContinuationMarkers: [...this.cancelledContinuationMarkers],
|
|
872
|
+
terminalDetails: this.terminalDetails ? structuredClone(this.terminalDetails) : undefined,
|
|
873
|
+
toolVisibility: this.snapshotGoalToolVisibility(),
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
restoreSettingsApplicationState(snapshot: GoalSettingsRuntimeSnapshot) {
|
|
878
|
+
this.settings = structuredClone(snapshot.settings);
|
|
879
|
+
this.activeGoal = snapshot.activeGoal ? structuredClone(snapshot.activeGoal) : undefined;
|
|
880
|
+
this.queueFrozen = snapshot.queueFrozen;
|
|
881
|
+
this.queueFreezeAwaitingSettle = snapshot.queueFreezeAwaitingSettle;
|
|
882
|
+
this.continuationIntent = snapshot.continuationIntent
|
|
883
|
+
? structuredClone(snapshot.continuationIntent)
|
|
884
|
+
: undefined;
|
|
885
|
+
this.continuationDelivery = snapshot.continuationDelivery
|
|
886
|
+
? structuredClone(snapshot.continuationDelivery)
|
|
887
|
+
: undefined;
|
|
888
|
+
this.goalRecovery = snapshot.goalRecovery ? structuredClone(snapshot.goalRecovery) : undefined;
|
|
889
|
+
this.budgetWrapUp = snapshot.budgetWrapUp ? structuredClone(snapshot.budgetWrapUp) : undefined;
|
|
890
|
+
this.guardAbortGoalId = snapshot.guardAbortGoalId;
|
|
891
|
+
this.staleGoalToolCallsBlocked = snapshot.staleGoalToolCallsBlocked;
|
|
892
|
+
this.cancelledContinuationMarkers = new Set(snapshot.cancelledContinuationMarkers);
|
|
893
|
+
this.terminalDetails = snapshot.terminalDetails
|
|
894
|
+
? structuredClone(snapshot.terminalDetails)
|
|
895
|
+
: undefined;
|
|
896
|
+
this.restoreGoalToolVisibility(snapshot.toolVisibility);
|
|
897
|
+
}
|
|
898
|
+
|
|
834
899
|
restoreGoalToolVisibility(snapshot: GoalToolVisibilitySnapshot) {
|
|
835
900
|
this.pi.setActiveTools(snapshot.activeTools);
|
|
836
901
|
this.goalToolsUnlocked = snapshot.goalToolsUnlocked;
|
|
@@ -998,6 +1063,7 @@ export function goalSummary(
|
|
|
998
1063
|
queuedGoals: readonly ActiveGoal[] = [],
|
|
999
1064
|
experimentalGoals = false,
|
|
1000
1065
|
queueFrozen = false,
|
|
1066
|
+
pendingAction?: PendingQueueAction,
|
|
1001
1067
|
) {
|
|
1002
1068
|
const summary = [
|
|
1003
1069
|
`Goal: ${goal.text}`,
|
|
@@ -1012,12 +1078,20 @@ export function goalSummary(
|
|
|
1012
1078
|
`Safety pause: ${goal.safetyPauseCause === "continuation_limit" ? "automatic response limit" : "no progress"}`,
|
|
1013
1079
|
);
|
|
1014
1080
|
}
|
|
1015
|
-
if (experimentalGoals || queuedGoals.length > 0 || queueFrozen) {
|
|
1081
|
+
if (experimentalGoals || queuedGoals.length > 0 || queueFrozen || pendingAction) {
|
|
1082
|
+
const orderedGoals = [
|
|
1083
|
+
`[${goal.status}] ${goal.text}`,
|
|
1084
|
+
...(pendingAction?.kind === "prioritize" ? [`[pending] ${pendingAction.objective}`] : []),
|
|
1085
|
+
...queuedGoals.map((queuedGoal) => `[${queuedGoal.status}] ${queuedGoal.text}`),
|
|
1086
|
+
];
|
|
1087
|
+
summary.push(
|
|
1088
|
+
`Goals (${orderedGoals.length}):`,
|
|
1089
|
+
...orderedGoals.map((queuedGoal, index) => `${index + 1}. ${queuedGoal}`),
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
if (pendingAction?.kind === "advance") {
|
|
1016
1093
|
summary.push(
|
|
1017
|
-
`
|
|
1018
|
-
...[goal, ...queuedGoals].map(
|
|
1019
|
-
(queuedGoal, index) => `${index + 1}. [${queuedGoal.status}] ${queuedGoal.text}`,
|
|
1020
|
-
),
|
|
1094
|
+
`Pending queue action: ${pendingAction.reason === "complete" ? "complete" : "skip"} current goal when Pi settles.`,
|
|
1021
1095
|
);
|
|
1022
1096
|
}
|
|
1023
1097
|
if (queueFrozen) {
|