@narumitw/pi-goal 0.50.0 → 0.52.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 +32 -49
- package/package.json +8 -9
- package/src/command-registration.ts +15 -38
- package/src/command.ts +25 -70
- package/src/commands.ts +51 -383
- package/src/goal.ts +1 -1
- package/src/lifecycle.ts +25 -138
- package/src/menu.ts +15 -242
- package/src/persistence.ts +72 -108
- package/src/runtime.ts +22 -64
- package/src/settings-ui.ts +3 -125
- package/src/settings.ts +14 -30
- package/src/tools.ts +0 -65
- package/src/queue.ts +0 -126
package/src/commands.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { currentTokenTotal, formatTokenCount } from "./accounting.js";
|
|
2
2
|
import { validateObjective } from "./command.js";
|
|
3
3
|
import { notifyTerminal, safeGoalMenuText } from "./errors.js";
|
|
4
4
|
import type { ActiveGoal } from "./persistence.js";
|
|
@@ -8,15 +8,6 @@ import {
|
|
|
8
8
|
buildResumePrompt,
|
|
9
9
|
buildWaitingResumePrompt,
|
|
10
10
|
} from "./prompts.js";
|
|
11
|
-
import {
|
|
12
|
-
activateQueuedGoal,
|
|
13
|
-
appendGoal,
|
|
14
|
-
createQueuedGoal,
|
|
15
|
-
dropLastGoal as dropLastQueuedGoal,
|
|
16
|
-
goalQueueIdentity,
|
|
17
|
-
prioritizeGoal as prioritizeQueuedGoal,
|
|
18
|
-
skipGoal as skipQueuedGoal,
|
|
19
|
-
} from "./queue.js";
|
|
20
11
|
import {
|
|
21
12
|
blocksStaleGoalToolCalls,
|
|
22
13
|
createGoal,
|
|
@@ -25,7 +16,6 @@ import {
|
|
|
25
16
|
formatError,
|
|
26
17
|
type GoalRuntime,
|
|
27
18
|
goalSummary,
|
|
28
|
-
hasPendingMessages,
|
|
29
19
|
isResumableGoalStatus,
|
|
30
20
|
nextGoalInstance,
|
|
31
21
|
queueGoalSafetyReset,
|
|
@@ -61,38 +51,25 @@ export class GoalCommandController {
|
|
|
61
51
|
|
|
62
52
|
const existingGoal =
|
|
63
53
|
this.runtime.activeGoal?.status !== "complete" ? this.runtime.activeGoal : undefined;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
this.runtime.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
54
|
+
const legacyQueueBeforeActivation = existingGoal
|
|
55
|
+
? undefined
|
|
56
|
+
: this.runtime.legacyQueueState
|
|
57
|
+
? structuredClone(this.runtime.legacyQueueState)
|
|
58
|
+
: undefined;
|
|
70
59
|
if (existingGoal) {
|
|
71
|
-
const queuedRemovalPreview =
|
|
72
|
-
existingQueuedGoals.length > 0
|
|
73
|
-
? `\n\nQueued goals also removed:\n${existingQueuedGoals
|
|
74
|
-
.map((goal, index) => `${index + 1}. ${safeGoalMenuText(goal.text, 4_000)}`)
|
|
75
|
-
.join("\n")}`
|
|
76
|
-
: "";
|
|
77
60
|
const shouldReplace = await ctx.ui.confirm(
|
|
78
61
|
"Replace goal?",
|
|
79
|
-
`Current goal: ${safeGoalMenuText(existingGoal.text, 4_000)}
|
|
62
|
+
`Current goal: ${safeGoalMenuText(existingGoal.text, 4_000)}\n\nNew goal: ${safeGoalMenuText(objective, 4_000)}`,
|
|
80
63
|
);
|
|
81
64
|
if (!shouldReplace) {
|
|
82
65
|
notifyTerminal(ctx.ui, `Goal kept: ${existingGoal.text}`, "info");
|
|
83
66
|
return;
|
|
84
67
|
}
|
|
85
68
|
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
86
|
-
if (
|
|
87
|
-
goalQueueIdentity(
|
|
88
|
-
this.runtime.activeGoal,
|
|
89
|
-
this.runtime.queuedGoals,
|
|
90
|
-
this.runtime.pendingQueueAction,
|
|
91
|
-
) !== existingQueueIdentity
|
|
92
|
-
) {
|
|
69
|
+
if (this.runtime.activeGoal?.id !== existingGoal.id) {
|
|
93
70
|
notifyTerminal(
|
|
94
71
|
ctx.ui,
|
|
95
|
-
"The goal
|
|
72
|
+
"The active goal changed while confirmation was open. Try again.",
|
|
96
73
|
"warning",
|
|
97
74
|
);
|
|
98
75
|
return;
|
|
@@ -116,12 +93,11 @@ export class GoalCommandController {
|
|
|
116
93
|
this.runtime.clearGoalRecovery();
|
|
117
94
|
this.runtime.clearBudgetWrapUp();
|
|
118
95
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
119
|
-
this.runtime.
|
|
120
|
-
this.runtime.pendingQueueAction = undefined;
|
|
96
|
+
if (!legacyQueueBeforeActivation) this.runtime.legacyQueueState = undefined;
|
|
121
97
|
this.runtime.activeGoal = createGoal(objective, tokenBudget, currentTokenTotal(ctx));
|
|
122
98
|
const startedGoal = this.runtime.activeGoal;
|
|
123
99
|
onActivated?.(startedGoal);
|
|
124
|
-
this.runtime.persistGoal(startedGoal);
|
|
100
|
+
if (!legacyQueueBeforeActivation) this.runtime.persistGoal(startedGoal);
|
|
125
101
|
if (
|
|
126
102
|
this.runtime.activeGoal?.id !== startedGoal.id ||
|
|
127
103
|
this.runtime.activeGoal.status !== "active"
|
|
@@ -136,13 +112,21 @@ export class GoalCommandController {
|
|
|
136
112
|
true,
|
|
137
113
|
() => (isRequestCurrent?.() ?? true) && (isActivationCurrent?.(startedGoal) ?? true),
|
|
138
114
|
);
|
|
115
|
+
if (
|
|
116
|
+
sent &&
|
|
117
|
+
legacyQueueBeforeActivation &&
|
|
118
|
+
this.runtime.activeGoal?.id === startedGoal.id &&
|
|
119
|
+
this.runtime.activeGoal.status === "active"
|
|
120
|
+
) {
|
|
121
|
+
this.runtime.legacyQueueState = undefined;
|
|
122
|
+
this.runtime.persistGoal(startedGoal);
|
|
123
|
+
}
|
|
139
124
|
if (isActivationCurrent && !isActivationCurrent(startedGoal)) return;
|
|
140
125
|
if (!sent) {
|
|
141
126
|
let rolledBackStartedGoal = false;
|
|
142
127
|
if (this.runtime.activeGoal?.id === startedGoal.id) {
|
|
143
128
|
rolledBackStartedGoal = true;
|
|
144
129
|
if (existingGoal) {
|
|
145
|
-
this.runtime.queuedGoals = existingQueuedGoals;
|
|
146
130
|
this.runtime.recordGoalUsage(existingGoal, ctx);
|
|
147
131
|
if (existingGoal.status === "active" && existingGoal.waiting) {
|
|
148
132
|
this.runtime.activeGoal = existingGoal;
|
|
@@ -167,6 +151,14 @@ export class GoalCommandController {
|
|
|
167
151
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
168
152
|
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
169
153
|
}
|
|
154
|
+
} else if (legacyQueueBeforeActivation) {
|
|
155
|
+
this.runtime.activeGoal = undefined;
|
|
156
|
+
this.runtime.legacyQueueState = legacyQueueBeforeActivation;
|
|
157
|
+
this.runtime.cancelContinuationWork();
|
|
158
|
+
this.runtime.clearGoalRecovery();
|
|
159
|
+
this.runtime.clearBudgetWrapUp();
|
|
160
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
161
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
170
162
|
} else {
|
|
171
163
|
this.runtime.clearActiveGoal(ctx);
|
|
172
164
|
}
|
|
@@ -198,241 +190,6 @@ export class GoalCommandController {
|
|
|
198
190
|
);
|
|
199
191
|
}
|
|
200
192
|
|
|
201
|
-
async addGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
202
|
-
const validationError = validateObjective(objective);
|
|
203
|
-
if (validationError) {
|
|
204
|
-
notifyTerminal(ctx.ui, validationError, "warning");
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
if (!this.runtime.activeGoal) {
|
|
208
|
-
await this.startGoal(objective, tokenBudget, ctx);
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
this.runtime.queuedGoals = appendGoal(
|
|
212
|
-
this.runtime.queuedGoals,
|
|
213
|
-
createQueuedGoal(objective, tokenBudget),
|
|
214
|
-
);
|
|
215
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
216
|
-
notifyTerminal(
|
|
217
|
-
ctx.ui,
|
|
218
|
-
`Goal added at position ${this.runtime.queuedGoals.length + 1}: ${objective}`,
|
|
219
|
-
"info",
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async prioritizeGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
224
|
-
const validationError = validateObjective(objective);
|
|
225
|
-
if (validationError) {
|
|
226
|
-
notifyTerminal(ctx.ui, validationError, "warning");
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if (!this.runtime.activeGoal) {
|
|
230
|
-
await this.startGoal(objective, tokenBudget, ctx);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
this.runtime.clearGoalWaitTimer();
|
|
234
|
-
this.runtime.cancelContinuationWork();
|
|
235
|
-
this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
|
|
236
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
237
|
-
if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) {
|
|
238
|
-
notifyTerminal(ctx.ui, `Priority goal queued until Pi settles: ${objective}`, "info");
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
await this.dispatchPendingQueueActionIfSettled(ctx);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
dropLastGoal(ctx: StatusContext) {
|
|
245
|
-
const currentGoal = this.runtime.activeGoal;
|
|
246
|
-
if (!currentGoal) {
|
|
247
|
-
notifyTerminal(ctx.ui, "No goals to drop.", "info");
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
const result = dropLastQueuedGoal(currentGoal, this.runtime.queuedGoals);
|
|
251
|
-
if (!result.goal) {
|
|
252
|
-
this.runtime.clearActiveGoal(ctx);
|
|
253
|
-
notifyTerminal(
|
|
254
|
-
ctx.ui,
|
|
255
|
-
`Goal dropped: ${result.removed?.text ?? currentGoal.text}`,
|
|
256
|
-
"warning",
|
|
257
|
-
);
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
this.runtime.queuedGoals = result.queue;
|
|
261
|
-
this.runtime.persistGoal(result.goal);
|
|
262
|
-
notifyTerminal(ctx.ui, `Goal dropped: ${result.removed?.text ?? "unknown goal"}`, "warning");
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
async skipGoal(ctx: StatusContext) {
|
|
266
|
-
const currentGoal = this.runtime.activeGoal;
|
|
267
|
-
if (!currentGoal) {
|
|
268
|
-
notifyTerminal(ctx.ui, "No goals to skip.", "info");
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
if (this.runtime.queuedGoals.length === 0) {
|
|
272
|
-
this.runtime.clearActiveGoal(ctx);
|
|
273
|
-
notifyTerminal(ctx.ui, `Goal skipped: ${currentGoal.text}. No goals remain.`, "warning");
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if (currentGoal.status === "active") this.runtime.recordGoalUsage(currentGoal, ctx);
|
|
277
|
-
this.runtime.clearGoalWaitTimer();
|
|
278
|
-
this.runtime.cancelContinuationWork();
|
|
279
|
-
this.runtime.clearGoalRecovery();
|
|
280
|
-
this.runtime.clearBudgetWrapUp();
|
|
281
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
282
|
-
this.runtime.pendingQueueAction = {
|
|
283
|
-
kind: "advance",
|
|
284
|
-
goalId: currentGoal.id,
|
|
285
|
-
reason: "skip",
|
|
286
|
-
completedText: currentGoal.text,
|
|
287
|
-
};
|
|
288
|
-
this.runtime.persistGoal(currentGoal);
|
|
289
|
-
notifyTerminal(ctx.ui, `Goal skip queued until Pi settles: ${currentGoal.text}`, "info");
|
|
290
|
-
if (ctx.isIdle?.() === true && !hasPendingMessages(ctx)) {
|
|
291
|
-
await this.dispatchPendingQueueActionIfSettled(ctx);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
async resumeQueueAfterUnfreeze(ctx: StatusContext) {
|
|
296
|
-
if (this.runtime.queueFreezeAwaitingSettle) return false;
|
|
297
|
-
this.runtime.queueFrozen = false;
|
|
298
|
-
this.runtime.queueFreezeAwaitingSettle = false;
|
|
299
|
-
this.runtime.guardAbortGoalId = undefined;
|
|
300
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
301
|
-
if (this.runtime.activeGoal) {
|
|
302
|
-
if (
|
|
303
|
-
this.runtime.activeGoal.status === "active" &&
|
|
304
|
-
!this.runtime.activeGoal.waiting &&
|
|
305
|
-
this.runtime.activeGoal.activeStartedAt === undefined
|
|
306
|
-
) {
|
|
307
|
-
const now = Date.now();
|
|
308
|
-
checkpointGoalActiveTime(this.runtime.activeGoal, now, true);
|
|
309
|
-
this.runtime.activeGoal.updatedAt = now;
|
|
310
|
-
}
|
|
311
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
312
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
313
|
-
this.runtime.restoreGoalWaitTimer(ctx);
|
|
314
|
-
} else {
|
|
315
|
-
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
316
|
-
}
|
|
317
|
-
if (this.runtime.pendingQueueAction) {
|
|
318
|
-
return this.dispatchPendingQueueActionIfSettled(ctx);
|
|
319
|
-
}
|
|
320
|
-
const goal = this.runtime.activeGoal;
|
|
321
|
-
if (goal?.status !== "active") return false;
|
|
322
|
-
this.runtime.requestContinuation(goal);
|
|
323
|
-
return this.runtime.dispatchContinuationIfSettled(ctx);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async dispatchPendingQueueActionIfSettled(ctx: StatusContext) {
|
|
327
|
-
const pending = this.runtime.pendingQueueAction;
|
|
328
|
-
if (!pending || this.runtime.queueFrozen) return false;
|
|
329
|
-
if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) return false;
|
|
330
|
-
if (pending.kind === "prioritize") {
|
|
331
|
-
this.runtime.pendingQueueAction = undefined;
|
|
332
|
-
return this.activatePrioritizedGoal(
|
|
333
|
-
pending.objective,
|
|
334
|
-
pending.tokenBudget,
|
|
335
|
-
ctx,
|
|
336
|
-
pending.displacedUsageFinalized === true,
|
|
337
|
-
);
|
|
338
|
-
}
|
|
339
|
-
if (
|
|
340
|
-
!this.runtime.activeGoal ||
|
|
341
|
-
this.runtime.activeGoal.id !== pending.goalId ||
|
|
342
|
-
(this.runtime.activeGoal.status !== "complete" && pending.reason === "complete")
|
|
343
|
-
) {
|
|
344
|
-
this.runtime.pendingQueueAction = undefined;
|
|
345
|
-
if (this.runtime.activeGoal) this.runtime.persistGoal(this.runtime.activeGoal);
|
|
346
|
-
return false;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const previousText = pending.completedText;
|
|
350
|
-
const reason = pending.reason;
|
|
351
|
-
this.runtime.pendingQueueAction = undefined;
|
|
352
|
-
this.runtime.cancelContinuationWork();
|
|
353
|
-
this.runtime.clearGoalRecovery();
|
|
354
|
-
this.runtime.clearBudgetWrapUp();
|
|
355
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
356
|
-
const next = skipQueuedGoal(this.runtime.queuedGoals);
|
|
357
|
-
this.runtime.queuedGoals = next.queue;
|
|
358
|
-
this.runtime.activeGoal = next.goal
|
|
359
|
-
? activateQueuedGoal(next.goal, currentTokenTotal(ctx))
|
|
360
|
-
: undefined;
|
|
361
|
-
if (!this.runtime.activeGoal) {
|
|
362
|
-
this.runtime.clearActiveGoal(ctx);
|
|
363
|
-
notifyTerminal(
|
|
364
|
-
ctx.ui,
|
|
365
|
-
reason === "complete"
|
|
366
|
-
? `Goal complete: ${previousText}. No goals remain.`
|
|
367
|
-
: `Goal skipped: ${previousText}. No goals remain.`,
|
|
368
|
-
"info",
|
|
369
|
-
);
|
|
370
|
-
return true;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
374
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
375
|
-
if (this.runtime.activeGoal.status !== "active") {
|
|
376
|
-
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
377
|
-
this.runtime.blockStaleGoalToolCalls();
|
|
378
|
-
}
|
|
379
|
-
notifyTerminal(
|
|
380
|
-
ctx.ui,
|
|
381
|
-
`${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Next goal remains ${this.runtime.activeGoal.status}: ${this.runtime.activeGoal.text}`,
|
|
382
|
-
"info",
|
|
383
|
-
);
|
|
384
|
-
return true;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
try {
|
|
388
|
-
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
389
|
-
} catch (error) {
|
|
390
|
-
this.runtime.stopActiveGoal(ctx, {
|
|
391
|
-
kind: "activation_rollback",
|
|
392
|
-
expectedGoalId: this.runtime.activeGoal.id,
|
|
393
|
-
restoreGoal: this.runtime.activeGoal,
|
|
394
|
-
abortTurn: false,
|
|
395
|
-
});
|
|
396
|
-
notifyTerminal(ctx.ui, `Cannot start the next /goal: ${formatError(error)}`, "error");
|
|
397
|
-
return false;
|
|
398
|
-
}
|
|
399
|
-
const activatedGoal = this.runtime.activeGoal;
|
|
400
|
-
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
401
|
-
ctx,
|
|
402
|
-
activatedGoal.id,
|
|
403
|
-
buildGoalPrompt(activatedGoal),
|
|
404
|
-
false, // Queue reactivation preserves its persisted safety epoch.
|
|
405
|
-
);
|
|
406
|
-
if (!sent && this.runtime.activeGoal?.id === activatedGoal.id) {
|
|
407
|
-
this.runtime.stopActiveGoal(ctx, {
|
|
408
|
-
kind: "activation_rollback",
|
|
409
|
-
expectedGoalId: activatedGoal.id,
|
|
410
|
-
restoreGoal: activatedGoal,
|
|
411
|
-
abortTurn: false,
|
|
412
|
-
});
|
|
413
|
-
notifyTerminal(
|
|
414
|
-
ctx.ui,
|
|
415
|
-
`Next goal paused after prompt delivery failed: ${activatedGoal.text}`,
|
|
416
|
-
"warning",
|
|
417
|
-
);
|
|
418
|
-
return false;
|
|
419
|
-
}
|
|
420
|
-
notifyTerminal(
|
|
421
|
-
ctx.ui,
|
|
422
|
-
`${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Started next goal: ${activatedGoal.text}`,
|
|
423
|
-
"info",
|
|
424
|
-
);
|
|
425
|
-
return true;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
notifyFrozenQueue(ctx: StatusContext) {
|
|
429
|
-
notifyTerminal(
|
|
430
|
-
ctx.ui,
|
|
431
|
-
"The experimental goal queue is frozen. Re-enable experimental.goals in pi-goal.json and run /reload, or use /goal clear.",
|
|
432
|
-
"warning",
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
193
|
pauseGoal(ctx: StatusContext) {
|
|
437
194
|
if (!this.runtime.activeGoal) {
|
|
438
195
|
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
@@ -571,13 +328,19 @@ export class GoalCommandController {
|
|
|
571
328
|
|
|
572
329
|
clearGoal(ctx: StatusContext) {
|
|
573
330
|
if (!this.runtime.activeGoal) {
|
|
574
|
-
|
|
331
|
+
const hadLegacyQueue = this.runtime.legacyQueueState !== undefined;
|
|
332
|
+
this.runtime.legacyQueueState = undefined;
|
|
575
333
|
this.runtime.cancelContinuationWork();
|
|
576
334
|
this.runtime.clearGoalRecovery();
|
|
577
335
|
this.runtime.clearBudgetWrapUp();
|
|
578
336
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
579
337
|
this.runtime.clearPersistedGoal(ctx.cwd);
|
|
580
338
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
339
|
+
notifyTerminal(
|
|
340
|
+
ctx.ui,
|
|
341
|
+
hadLegacyQueue ? "Removed legacy ordered goal queue state." : "No active goal.",
|
|
342
|
+
hadLegacyQueue ? "warning" : "info",
|
|
343
|
+
);
|
|
581
344
|
return;
|
|
582
345
|
}
|
|
583
346
|
|
|
@@ -684,29 +447,30 @@ export class GoalCommandController {
|
|
|
684
447
|
|
|
685
448
|
showGoal(ctx: StatusContext) {
|
|
686
449
|
if (!this.runtime.activeGoal) {
|
|
687
|
-
const message = "Usage: /goal <objective>\nNo goal is currently set.";
|
|
688
450
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
689
|
-
this.reportGoalStatus(ctx,
|
|
451
|
+
this.reportGoalStatus(ctx, this.emptyGoalMessage());
|
|
690
452
|
return;
|
|
691
453
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
696
|
-
}
|
|
454
|
+
this.runtime.recordGoalUsage(this.runtime.activeGoal, ctx);
|
|
455
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
456
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
697
457
|
this.reportGoalStatus(
|
|
698
458
|
ctx,
|
|
699
|
-
goalSummary(
|
|
700
|
-
this.runtime.activeGoal,
|
|
701
|
-
this.runtime.queuedGoals,
|
|
702
|
-
this.runtime.settings.experimental.goals,
|
|
703
|
-
this.runtime.queueFrozen,
|
|
704
|
-
this.runtime.pendingQueueAction,
|
|
705
|
-
this.runtime.settings.continuationLimits.automaticTurns,
|
|
706
|
-
),
|
|
459
|
+
goalSummary(this.runtime.activeGoal, this.runtime.settings.continuationLimits.automaticTurns),
|
|
707
460
|
);
|
|
708
461
|
}
|
|
709
462
|
|
|
463
|
+
private emptyGoalMessage() {
|
|
464
|
+
const legacy = this.runtime.legacyQueueState;
|
|
465
|
+
if (!legacy) return "Usage: /goal <objective>\nNo goal is currently set.";
|
|
466
|
+
return [
|
|
467
|
+
"Ordered goal queue has been removed.",
|
|
468
|
+
`Legacy queue state with ${legacy.retainedGoals} retained ${legacy.retainedGoals === 1 ? "goal" : "goals"} will not run automatically.`,
|
|
469
|
+
"Use /goal edit to reprioritize an active objective, start /goal <objectives>, or use /goal clear to discard the old queue state.",
|
|
470
|
+
'Example objective: "task b is complete; do task a next, then task c and task d."',
|
|
471
|
+
].join("\n");
|
|
472
|
+
}
|
|
473
|
+
|
|
710
474
|
private reportGoalStatus(ctx: StatusContext, message: string) {
|
|
711
475
|
if (ctx.mode === "print" || ctx.mode === "json") {
|
|
712
476
|
throw new Error(
|
|
@@ -715,100 +479,4 @@ export class GoalCommandController {
|
|
|
715
479
|
}
|
|
716
480
|
notifyTerminal(ctx.ui, message, "info");
|
|
717
481
|
}
|
|
718
|
-
|
|
719
|
-
private async activatePrioritizedGoal(
|
|
720
|
-
objective: string,
|
|
721
|
-
tokenBudget: number | undefined,
|
|
722
|
-
ctx: StatusContext,
|
|
723
|
-
displacedUsageFinalized = false,
|
|
724
|
-
) {
|
|
725
|
-
const currentGoal = this.runtime.activeGoal;
|
|
726
|
-
if (!currentGoal) {
|
|
727
|
-
await this.startGoal(objective, tokenBudget, ctx);
|
|
728
|
-
return true;
|
|
729
|
-
}
|
|
730
|
-
if (currentGoal.status === "active" && !displacedUsageFinalized) {
|
|
731
|
-
this.runtime.recordGoalUsage(currentGoal, ctx);
|
|
732
|
-
}
|
|
733
|
-
const previousGoal = { ...currentGoal };
|
|
734
|
-
const previousQueue = [...this.runtime.queuedGoals];
|
|
735
|
-
const visibilityBeforeActivation = this.runtime.toolPolicy.snapshot();
|
|
736
|
-
try {
|
|
737
|
-
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
738
|
-
} catch (error) {
|
|
739
|
-
notifyTerminal(ctx.ui, `Cannot prioritize /goal: ${formatError(error)}`, "error");
|
|
740
|
-
if (currentGoal.status === "complete") {
|
|
741
|
-
// Completion already committed, so retain the priority intent for a
|
|
742
|
-
// later /reload after the tool policy is restored.
|
|
743
|
-
this.runtime.pendingQueueAction = {
|
|
744
|
-
kind: "prioritize",
|
|
745
|
-
objective,
|
|
746
|
-
tokenBudget,
|
|
747
|
-
...(displacedUsageFinalized ? { displacedUsageFinalized: true } : {}),
|
|
748
|
-
};
|
|
749
|
-
this.runtime.persistGoal(currentGoal);
|
|
750
|
-
} else {
|
|
751
|
-
// Roll back an activation that never started. An active displaced goal
|
|
752
|
-
// cannot continue safely without its terminal tools, so make it resumable.
|
|
753
|
-
this.runtime.pendingQueueAction = undefined;
|
|
754
|
-
if (currentGoal.status === "active") {
|
|
755
|
-
this.runtime.pauseGoalForUnavailableTools(ctx, true, !displacedUsageFinalized);
|
|
756
|
-
} else {
|
|
757
|
-
this.runtime.persistGoal(currentGoal);
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
return false;
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
this.runtime.cancelContinuationWork();
|
|
764
|
-
this.runtime.clearGoalRecovery();
|
|
765
|
-
this.runtime.clearBudgetWrapUp();
|
|
766
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
767
|
-
const prioritized = createGoal(objective, tokenBudget, currentTokenTotal(ctx));
|
|
768
|
-
const next =
|
|
769
|
-
currentGoal.status === "complete"
|
|
770
|
-
? { goal: prioritized, queue: [...this.runtime.queuedGoals] }
|
|
771
|
-
: prioritizeQueuedGoal(currentGoal, this.runtime.queuedGoals, prioritized);
|
|
772
|
-
this.runtime.activeGoal = next.goal;
|
|
773
|
-
this.runtime.queuedGoals = next.queue;
|
|
774
|
-
this.runtime.pendingQueueAction = undefined;
|
|
775
|
-
if (!this.runtime.activeGoal) return false;
|
|
776
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
777
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
778
|
-
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
779
|
-
ctx,
|
|
780
|
-
this.runtime.activeGoal.id,
|
|
781
|
-
buildGoalPrompt(this.runtime.activeGoal),
|
|
782
|
-
);
|
|
783
|
-
if (!sent && this.runtime.activeGoal.id === prioritized.id) {
|
|
784
|
-
this.runtime.queuedGoals = previousQueue;
|
|
785
|
-
if (previousGoal.status === "active" && previousGoal.waiting) {
|
|
786
|
-
this.runtime.activeGoal = previousGoal;
|
|
787
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
788
|
-
this.runtime.persistGoal(previousGoal);
|
|
789
|
-
this.runtime.updateStatus(ctx, previousGoal);
|
|
790
|
-
this.runtime.restoreGoalWaitTimer(ctx);
|
|
791
|
-
} else if (previousGoal.status === "active") {
|
|
792
|
-
this.runtime.stopActiveGoal(ctx, {
|
|
793
|
-
kind: "activation_rollback",
|
|
794
|
-
expectedGoalId: prioritized.id,
|
|
795
|
-
restoreGoal: previousGoal,
|
|
796
|
-
abortTurn: true,
|
|
797
|
-
});
|
|
798
|
-
} else {
|
|
799
|
-
this.runtime.activeGoal = previousGoal;
|
|
800
|
-
if (previousGoal.status === "complete") {
|
|
801
|
-
this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
|
|
802
|
-
} else if (blocksStaleGoalToolCalls(previousGoal.status)) {
|
|
803
|
-
this.runtime.blockStaleGoalToolCalls();
|
|
804
|
-
}
|
|
805
|
-
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
806
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
807
|
-
}
|
|
808
|
-
this.runtime.toolPolicy.restore(visibilityBeforeActivation);
|
|
809
|
-
return false;
|
|
810
|
-
}
|
|
811
|
-
notifyTerminal(ctx.ui, `Goal prioritized: ${objective}`, "info");
|
|
812
|
-
return true;
|
|
813
|
-
}
|
|
814
482
|
}
|
package/src/goal.ts
CHANGED
|
@@ -20,7 +20,7 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
20
20
|
runController.register(pi);
|
|
21
21
|
registerGoalTools(pi, runtime);
|
|
22
22
|
registerGoalCommand(pi, runtime, commands, options);
|
|
23
|
-
registerGoalLifecycle(pi, runtime,
|
|
23
|
+
registerGoalLifecycle(pi, runtime, runController, options);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export default function goal(pi: ExtensionAPI, options: GoalOptions = {}) {
|