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