@narumitw/pi-goal 0.15.0 → 0.16.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.
@@ -0,0 +1,592 @@
1
+ import { currentTokenTotal, updateGoalUsage } from "./accounting.js";
2
+ import { validateObjective } from "./command.js";
3
+ import { buildGoalPrompt, buildObjectiveUpdatedPrompt, buildResumePrompt } from "./prompts.js";
4
+ import {
5
+ activateQueuedGoal,
6
+ appendGoal,
7
+ createQueuedGoal,
8
+ dropLastGoal as dropLastQueuedGoal,
9
+ prioritizeGoal as prioritizeQueuedGoal,
10
+ skipGoal as skipQueuedGoal,
11
+ } from "./queue.js";
12
+ import {
13
+ abortCurrentTurn,
14
+ blocksStaleGoalToolCalls,
15
+ createGoal,
16
+ editedGoalStatus,
17
+ formatBudget,
18
+ formatError,
19
+ type GoalRuntime,
20
+ goalSummary,
21
+ hasPendingMessages,
22
+ isResumableGoalStatus,
23
+ nextGoalInstance,
24
+ STATUS_KEY,
25
+ type StatusContext,
26
+ stoppedStatusLabel,
27
+ transitionGoal,
28
+ } from "./runtime.js";
29
+
30
+ // User-command mutations are kept separate from Pi event wiring. Every controller
31
+ // receives exactly one per-factory GoalRuntime, preserving session isolation.
32
+ export class GoalCommandController {
33
+ constructor(private readonly runtime: GoalRuntime) {}
34
+
35
+ async startGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
36
+ const validationError = validateObjective(objective);
37
+ if (validationError) {
38
+ ctx.ui.notify(validationError, "warning");
39
+ return;
40
+ }
41
+
42
+ const existingGoal =
43
+ this.runtime.activeGoal?.status !== "complete" ? this.runtime.activeGoal : undefined;
44
+ const existingQueuedGoals = [...this.runtime.queuedGoals];
45
+ if (existingGoal) {
46
+ const shouldReplace = await ctx.ui.confirm(
47
+ "Replace goal?",
48
+ `Current goal: ${existingGoal.text}\n\nNew goal: ${objective}`,
49
+ );
50
+ if (!shouldReplace) {
51
+ ctx.ui.notify(`Goal kept: ${existingGoal.text}`, "info");
52
+ return;
53
+ }
54
+ }
55
+
56
+ // Unlock lazy visibility only for a real activation. In always mode, a
57
+ // missing tool means another policy or allowlist intentionally removed it.
58
+ const goalToolVisibilityBeforeActivation = this.runtime.snapshotGoalToolVisibility();
59
+ try {
60
+ this.runtime.prepareGoalToolsForActivation(ctx);
61
+ } catch (error) {
62
+ ctx.ui.notify(`Cannot start /goal: ${formatError(error)}`, "error");
63
+ if (existingGoal?.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
64
+ return;
65
+ }
66
+
67
+ this.runtime.cancelContinuationWork();
68
+ this.runtime.clearGoalRecovery();
69
+ this.runtime.clearBudgetWrapUp();
70
+ this.runtime.clearStaleGoalToolCallBlock();
71
+ this.runtime.queuedGoals = [];
72
+ this.runtime.pendingQueueAction = undefined;
73
+ this.runtime.activeGoal = createGoal(objective, tokenBudget, currentTokenTotal(ctx));
74
+ this.runtime.persistGoal(this.runtime.activeGoal);
75
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
76
+ const startedGoal = this.runtime.activeGoal;
77
+ const sent = await this.runtime.sendOwnedGoalPrompt(
78
+ ctx,
79
+ startedGoal.id,
80
+ buildGoalPrompt(startedGoal),
81
+ );
82
+ if (!sent) {
83
+ let rolledBackStartedGoal = false;
84
+ if (this.runtime.activeGoal?.id === startedGoal.id) {
85
+ rolledBackStartedGoal = true;
86
+ if (existingGoal) {
87
+ this.runtime.queuedGoals = existingQueuedGoals;
88
+ updateGoalUsage(existingGoal, ctx);
89
+ if (existingGoal.status === "active") {
90
+ abortCurrentTurn(ctx);
91
+ this.runtime.activeGoal = transitionGoal(existingGoal, "paused");
92
+ this.runtime.blockStaleGoalToolCalls();
93
+ } else {
94
+ this.runtime.activeGoal = existingGoal;
95
+ if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
96
+ this.runtime.blockStaleGoalToolCalls();
97
+ } else {
98
+ this.runtime.clearStaleGoalToolCallBlock();
99
+ }
100
+ }
101
+ this.runtime.persistGoal(this.runtime.activeGoal);
102
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
103
+ } else {
104
+ this.runtime.clearActiveGoal(ctx);
105
+ }
106
+ }
107
+ if (rolledBackStartedGoal) {
108
+ this.runtime.restoreGoalToolVisibility(goalToolVisibilityBeforeActivation);
109
+ }
110
+ return;
111
+ }
112
+ ctx.ui.notify(
113
+ existingGoal ? `Goal replaced: ${objective}` : `Goal started: ${objective}`,
114
+ "info",
115
+ );
116
+ }
117
+
118
+ async addGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
119
+ const validationError = validateObjective(objective);
120
+ if (validationError) {
121
+ ctx.ui.notify(validationError, "warning");
122
+ return;
123
+ }
124
+ if (!this.runtime.activeGoal) {
125
+ await this.startGoal(objective, tokenBudget, ctx);
126
+ return;
127
+ }
128
+ this.runtime.queuedGoals = appendGoal(
129
+ this.runtime.queuedGoals,
130
+ createQueuedGoal(objective, tokenBudget),
131
+ );
132
+ this.runtime.persistGoal(this.runtime.activeGoal);
133
+ ctx.ui.notify(
134
+ `Goal added at position ${this.runtime.queuedGoals.length + 1}: ${objective}`,
135
+ "info",
136
+ );
137
+ }
138
+
139
+ async prioritizeGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
140
+ const validationError = validateObjective(objective);
141
+ if (validationError) {
142
+ ctx.ui.notify(validationError, "warning");
143
+ return;
144
+ }
145
+ if (!this.runtime.activeGoal) {
146
+ await this.startGoal(objective, tokenBudget, ctx);
147
+ return;
148
+ }
149
+ this.runtime.cancelContinuationWork();
150
+ this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
151
+ this.runtime.persistGoal(this.runtime.activeGoal);
152
+ if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) {
153
+ ctx.ui.notify(`Priority goal queued until Pi settles: ${objective}`, "info");
154
+ return;
155
+ }
156
+ await this.dispatchPendingQueueActionIfSettled(ctx);
157
+ }
158
+
159
+ dropLastGoal(ctx: StatusContext) {
160
+ const currentGoal = this.runtime.activeGoal;
161
+ if (!currentGoal) {
162
+ ctx.ui.notify("No goals to drop.", "info");
163
+ return;
164
+ }
165
+ const result = dropLastQueuedGoal(currentGoal, this.runtime.queuedGoals);
166
+ if (!result.goal) {
167
+ this.runtime.clearActiveGoal(ctx);
168
+ ctx.ui.notify(`Goal dropped: ${result.removed?.text ?? currentGoal.text}`, "warning");
169
+ return;
170
+ }
171
+ this.runtime.queuedGoals = result.queue;
172
+ this.runtime.persistGoal(result.goal);
173
+ ctx.ui.notify(`Goal dropped: ${result.removed?.text ?? "unknown goal"}`, "warning");
174
+ }
175
+
176
+ async skipGoal(ctx: StatusContext) {
177
+ const currentGoal = this.runtime.activeGoal;
178
+ if (!currentGoal) {
179
+ ctx.ui.notify("No goals to skip.", "info");
180
+ return;
181
+ }
182
+ if (this.runtime.queuedGoals.length === 0) {
183
+ this.runtime.clearActiveGoal(ctx);
184
+ ctx.ui.notify(`Goal skipped: ${currentGoal.text}. No goals remain.`, "warning");
185
+ return;
186
+ }
187
+ if (currentGoal.status === "active") updateGoalUsage(currentGoal, ctx);
188
+ this.runtime.cancelContinuationWork();
189
+ this.runtime.clearGoalRecovery();
190
+ this.runtime.clearBudgetWrapUp();
191
+ this.runtime.clearStaleGoalToolCallBlock();
192
+ this.runtime.pendingQueueAction = {
193
+ kind: "advance",
194
+ goalId: currentGoal.id,
195
+ reason: "skip",
196
+ completedText: currentGoal.text,
197
+ };
198
+ this.runtime.persistGoal(currentGoal);
199
+ ctx.ui.notify(`Goal skip queued until Pi settles: ${currentGoal.text}`, "info");
200
+ if (ctx.isIdle?.() === true && !hasPendingMessages(ctx)) {
201
+ await this.dispatchPendingQueueActionIfSettled(ctx);
202
+ }
203
+ }
204
+
205
+ async dispatchPendingQueueActionIfSettled(ctx: StatusContext) {
206
+ const pending = this.runtime.pendingQueueAction;
207
+ if (!pending || this.runtime.queueFrozen) return false;
208
+ if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) return false;
209
+ if (pending.kind === "prioritize") {
210
+ this.runtime.pendingQueueAction = undefined;
211
+ return this.activatePrioritizedGoal(pending.objective, pending.tokenBudget, ctx);
212
+ }
213
+ if (
214
+ !this.runtime.activeGoal ||
215
+ this.runtime.activeGoal.id !== pending.goalId ||
216
+ (this.runtime.activeGoal.status !== "complete" && pending.reason === "complete")
217
+ ) {
218
+ this.runtime.pendingQueueAction = undefined;
219
+ if (this.runtime.activeGoal) this.runtime.persistGoal(this.runtime.activeGoal);
220
+ return false;
221
+ }
222
+
223
+ const previousText = pending.completedText;
224
+ const reason = pending.reason;
225
+ this.runtime.pendingQueueAction = undefined;
226
+ this.runtime.cancelContinuationWork();
227
+ this.runtime.clearGoalRecovery();
228
+ this.runtime.clearBudgetWrapUp();
229
+ this.runtime.clearStaleGoalToolCallBlock();
230
+ const next = skipQueuedGoal(this.runtime.queuedGoals);
231
+ this.runtime.queuedGoals = next.queue;
232
+ this.runtime.activeGoal = next.goal
233
+ ? activateQueuedGoal(next.goal, currentTokenTotal(ctx))
234
+ : undefined;
235
+ if (!this.runtime.activeGoal) {
236
+ this.runtime.clearActiveGoal(ctx);
237
+ ctx.ui.notify(
238
+ reason === "complete"
239
+ ? `Goal complete: ${previousText}. No goals remain.`
240
+ : `Goal skipped: ${previousText}. No goals remain.`,
241
+ "info",
242
+ );
243
+ return true;
244
+ }
245
+
246
+ this.runtime.persistGoal(this.runtime.activeGoal);
247
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
248
+ if (this.runtime.activeGoal.status !== "active") {
249
+ if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
250
+ this.runtime.blockStaleGoalToolCalls();
251
+ }
252
+ ctx.ui.notify(
253
+ `${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Next goal remains ${this.runtime.activeGoal.status}: ${this.runtime.activeGoal.text}`,
254
+ "info",
255
+ );
256
+ return true;
257
+ }
258
+
259
+ try {
260
+ this.runtime.prepareGoalToolsForActivation(ctx);
261
+ } catch (error) {
262
+ this.runtime.activeGoal = transitionGoal(this.runtime.activeGoal, "paused");
263
+ this.runtime.blockStaleGoalToolCalls();
264
+ this.runtime.persistGoal(this.runtime.activeGoal);
265
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
266
+ ctx.ui.notify(`Cannot start the next /goal: ${formatError(error)}`, "error");
267
+ return false;
268
+ }
269
+ const activatedGoal = this.runtime.activeGoal;
270
+ const sent = await this.runtime.sendOwnedGoalPrompt(
271
+ ctx,
272
+ activatedGoal.id,
273
+ buildGoalPrompt(activatedGoal),
274
+ );
275
+ if (!sent && this.runtime.activeGoal?.id === activatedGoal.id) {
276
+ this.runtime.activeGoal = transitionGoal(activatedGoal, "paused");
277
+ this.runtime.blockStaleGoalToolCalls();
278
+ this.runtime.persistGoal(this.runtime.activeGoal);
279
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
280
+ ctx.ui.notify(
281
+ `Next goal paused after prompt delivery failed: ${activatedGoal.text}`,
282
+ "warning",
283
+ );
284
+ return false;
285
+ }
286
+ ctx.ui.notify(
287
+ `${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Started next goal: ${activatedGoal.text}`,
288
+ "info",
289
+ );
290
+ return true;
291
+ }
292
+
293
+ notifyFrozenQueue(ctx: StatusContext) {
294
+ ctx.ui.notify(
295
+ "The experimental goal queue is frozen. Re-enable experimental.goals in pi-goal.json and run /reload, or use /goal clear.",
296
+ "warning",
297
+ );
298
+ }
299
+
300
+ pauseGoal(ctx: StatusContext) {
301
+ if (!this.runtime.activeGoal) {
302
+ ctx.ui.notify("No active goal.", "info");
303
+ return;
304
+ }
305
+ if (this.runtime.activeGoal.status !== "active") {
306
+ ctx.ui.notify(
307
+ `Goal is ${this.runtime.activeGoal.status}; only active goals can be paused.`,
308
+ "warning",
309
+ );
310
+ return;
311
+ }
312
+ updateGoalUsage(this.runtime.activeGoal, ctx);
313
+ this.runtime.cancelContinuationWork();
314
+ this.runtime.clearBudgetWrapUp();
315
+ this.runtime.blockStaleGoalToolCalls();
316
+ abortCurrentTurn(ctx);
317
+ this.runtime.activeGoal = transitionGoal(this.runtime.activeGoal, "paused");
318
+ this.runtime.persistGoal(this.runtime.activeGoal);
319
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
320
+ ctx.ui.notify(`Goal paused: ${this.runtime.activeGoal.text}`, "info");
321
+ }
322
+
323
+ async resumeGoal(ctx: StatusContext) {
324
+ if (!this.runtime.activeGoal) {
325
+ ctx.ui.notify("No active goal.", "info");
326
+ return;
327
+ }
328
+ if (!isResumableGoalStatus(this.runtime.activeGoal.status)) {
329
+ ctx.ui.notify(
330
+ `Goal is ${this.runtime.activeGoal.status}; only paused, blocked, usage-limited, or budget-limited goals can be resumed.`,
331
+ "warning",
332
+ );
333
+ return;
334
+ }
335
+ if (
336
+ this.runtime.activeGoal.tokenBudget !== undefined &&
337
+ this.runtime.activeGoal.tokensUsed >= this.runtime.activeGoal.tokenBudget
338
+ ) {
339
+ ctx.ui.notify(
340
+ `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
341
+ "warning",
342
+ );
343
+ return;
344
+ }
345
+ const goalToolVisibilityBeforeActivation = this.runtime.snapshotGoalToolVisibility();
346
+ try {
347
+ this.runtime.prepareGoalToolsForActivation(ctx);
348
+ } catch (error) {
349
+ ctx.ui.notify(`Cannot resume /goal: ${formatError(error)}`, "error");
350
+ return;
351
+ }
352
+ const stoppedGoal = this.runtime.activeGoal;
353
+ const stoppedStatus = stoppedGoal.status;
354
+ this.runtime.cancelContinuationWork();
355
+ this.runtime.clearGoalRecovery();
356
+ this.runtime.clearBudgetWrapUp();
357
+ this.runtime.clearStaleGoalToolCallBlock();
358
+ this.runtime.activeGoal = transitionGoal(nextGoalInstance(this.runtime.activeGoal), "active");
359
+ this.runtime.persistGoal(this.runtime.activeGoal);
360
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
361
+ if (this.runtime.activeGoal.status !== "active") {
362
+ ctx.ui.notify(
363
+ `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
364
+ "warning",
365
+ );
366
+ return;
367
+ }
368
+ const resumedGoal = this.runtime.activeGoal;
369
+ const sent = await this.runtime.sendOwnedGoalPrompt(
370
+ ctx,
371
+ resumedGoal.id,
372
+ buildResumePrompt(resumedGoal, stoppedStatus),
373
+ );
374
+ if (!sent) {
375
+ if (
376
+ this.runtime.activeGoal?.id === resumedGoal.id &&
377
+ this.runtime.activeGoal.status === "active"
378
+ ) {
379
+ this.runtime.activeGoal = stoppedGoal;
380
+ this.runtime.persistGoal(this.runtime.activeGoal);
381
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
382
+ if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
383
+ this.runtime.blockStaleGoalToolCalls();
384
+ }
385
+ this.runtime.restoreGoalToolVisibility(goalToolVisibilityBeforeActivation);
386
+ }
387
+ return;
388
+ }
389
+ ctx.ui.notify(
390
+ `Goal resumed from ${stoppedStatusLabel(stoppedStatus)}: ${resumedGoal.text}`,
391
+ "info",
392
+ );
393
+ }
394
+
395
+ clearGoal(ctx: StatusContext) {
396
+ if (!this.runtime.activeGoal) {
397
+ ctx.ui.notify("No active goal.", "info");
398
+ this.runtime.cancelContinuationWork();
399
+ this.runtime.clearGoalRecovery();
400
+ this.runtime.clearBudgetWrapUp();
401
+ this.runtime.clearStaleGoalToolCallBlock();
402
+ this.runtime.clearPersistedGoal(ctx.cwd);
403
+ ctx.ui.setStatus(STATUS_KEY, undefined);
404
+ return;
405
+ }
406
+
407
+ const stoppedGoal = this.runtime.activeGoal.text;
408
+ this.runtime.clearActiveGoal(ctx);
409
+ ctx.ui.notify(`Goal cleared: ${stoppedGoal}`, "warning");
410
+ }
411
+
412
+ async editGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
413
+ const validationError = validateObjective(objective);
414
+ if (validationError) {
415
+ ctx.ui.notify(validationError, "warning");
416
+ return;
417
+ }
418
+ if (!this.runtime.activeGoal) {
419
+ ctx.ui.notify("No active goal. Use /goal <objective> to start one.", "warning");
420
+ return;
421
+ }
422
+
423
+ updateGoalUsage(this.runtime.activeGoal, ctx);
424
+ const previousGoal = { ...this.runtime.activeGoal };
425
+ this.runtime.cancelContinuationWork();
426
+ this.runtime.clearGoalRecovery();
427
+ this.runtime.clearBudgetWrapUp();
428
+ const previousStatus = this.runtime.activeGoal.status;
429
+ const nextGoal = transitionGoal(
430
+ {
431
+ ...nextGoalInstance(this.runtime.activeGoal),
432
+ text: objective,
433
+ tokenBudget: tokenBudget ?? this.runtime.activeGoal.tokenBudget,
434
+ },
435
+ editedGoalStatus(previousStatus),
436
+ );
437
+ const goalToolVisibilityBeforeActivation =
438
+ nextGoal.status === "active" ? this.runtime.snapshotGoalToolVisibility() : undefined;
439
+ if (nextGoal.status === "active") {
440
+ try {
441
+ this.runtime.prepareGoalToolsForActivation(ctx);
442
+ } catch (error) {
443
+ ctx.ui.notify(`Cannot reactivate /goal: ${formatError(error)}`, "error");
444
+ if (this.runtime.activeGoal?.status === "active") {
445
+ this.runtime.pauseGoalForUnavailableTools(ctx);
446
+ }
447
+ return;
448
+ }
449
+ }
450
+ this.runtime.activeGoal = nextGoal;
451
+ this.runtime.persistGoal(this.runtime.activeGoal);
452
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
453
+ const editedGoal = this.runtime.activeGoal;
454
+ if (!editedGoal) return;
455
+ if (editedGoal.status === "active") {
456
+ this.runtime.clearStaleGoalToolCallBlock();
457
+ const sent = await this.runtime.sendOwnedGoalPrompt(
458
+ ctx,
459
+ editedGoal.id,
460
+ buildObjectiveUpdatedPrompt(editedGoal),
461
+ );
462
+ if (!sent) {
463
+ if (this.runtime.activeGoal?.id === editedGoal.id) {
464
+ if (previousStatus === "active") {
465
+ abortCurrentTurn(ctx);
466
+ this.runtime.activeGoal = transitionGoal(previousGoal, "paused");
467
+ this.runtime.blockStaleGoalToolCalls();
468
+ } else {
469
+ this.runtime.activeGoal = previousGoal;
470
+ if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
471
+ this.runtime.blockStaleGoalToolCalls();
472
+ } else {
473
+ this.runtime.clearStaleGoalToolCallBlock();
474
+ }
475
+ }
476
+ this.runtime.persistGoal(this.runtime.activeGoal);
477
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
478
+ if (goalToolVisibilityBeforeActivation) {
479
+ this.runtime.restoreGoalToolVisibility(goalToolVisibilityBeforeActivation);
480
+ }
481
+ }
482
+ return;
483
+ }
484
+ } else if (blocksStaleGoalToolCalls(editedGoal.status)) {
485
+ this.runtime.blockStaleGoalToolCalls();
486
+ } else {
487
+ this.runtime.clearStaleGoalToolCallBlock();
488
+ }
489
+ ctx.ui.notify(`Goal updated: ${objective}`, "info");
490
+ }
491
+
492
+ showGoal(ctx: StatusContext) {
493
+ if (!this.runtime.activeGoal) {
494
+ ctx.ui.notify("Usage: /goal <objective>\nNo goal is currently set.", "info");
495
+ ctx.ui.setStatus(STATUS_KEY, undefined);
496
+ return;
497
+ }
498
+ if (!this.runtime.queueFrozen) {
499
+ updateGoalUsage(this.runtime.activeGoal, ctx);
500
+ this.runtime.persistGoal(this.runtime.activeGoal);
501
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
502
+ }
503
+ ctx.ui.notify(
504
+ goalSummary(
505
+ this.runtime.activeGoal,
506
+ this.runtime.queuedGoals,
507
+ this.runtime.settings.experimental.goals,
508
+ this.runtime.queueFrozen,
509
+ ),
510
+ "info",
511
+ );
512
+ }
513
+
514
+ private async activatePrioritizedGoal(
515
+ objective: string,
516
+ tokenBudget: number | undefined,
517
+ ctx: StatusContext,
518
+ ) {
519
+ const currentGoal = this.runtime.activeGoal;
520
+ if (!currentGoal) {
521
+ await this.startGoal(objective, tokenBudget, ctx);
522
+ return true;
523
+ }
524
+ if (currentGoal.status === "active") updateGoalUsage(currentGoal, ctx);
525
+ const previousGoal = { ...currentGoal };
526
+ const previousQueue = [...this.runtime.queuedGoals];
527
+ const visibilityBeforeActivation = this.runtime.snapshotGoalToolVisibility();
528
+ try {
529
+ this.runtime.prepareGoalToolsForActivation(ctx);
530
+ } catch (error) {
531
+ ctx.ui.notify(`Cannot prioritize /goal: ${formatError(error)}`, "error");
532
+ if (currentGoal.status === "complete") {
533
+ // Completion already committed, so retain the priority intent for a
534
+ // later /reload after the tool policy is restored.
535
+ this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
536
+ this.runtime.persistGoal(currentGoal);
537
+ } else {
538
+ // Roll back an activation that never started. An active displaced goal
539
+ // cannot continue safely without its terminal tools, so make it resumable.
540
+ this.runtime.pendingQueueAction = undefined;
541
+ if (currentGoal.status === "active") {
542
+ this.runtime.pauseGoalForUnavailableTools(ctx);
543
+ } else {
544
+ this.runtime.persistGoal(currentGoal);
545
+ }
546
+ }
547
+ return false;
548
+ }
549
+
550
+ this.runtime.cancelContinuationWork();
551
+ this.runtime.clearGoalRecovery();
552
+ this.runtime.clearBudgetWrapUp();
553
+ this.runtime.clearStaleGoalToolCallBlock();
554
+ const prioritized = createGoal(objective, tokenBudget, currentTokenTotal(ctx));
555
+ const next =
556
+ currentGoal.status === "complete"
557
+ ? { goal: prioritized, queue: [...this.runtime.queuedGoals] }
558
+ : prioritizeQueuedGoal(currentGoal, this.runtime.queuedGoals, prioritized);
559
+ this.runtime.activeGoal = next.goal;
560
+ this.runtime.queuedGoals = next.queue;
561
+ this.runtime.pendingQueueAction = undefined;
562
+ if (!this.runtime.activeGoal) return false;
563
+ this.runtime.persistGoal(this.runtime.activeGoal);
564
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
565
+ const sent = await this.runtime.sendOwnedGoalPrompt(
566
+ ctx,
567
+ this.runtime.activeGoal.id,
568
+ buildGoalPrompt(this.runtime.activeGoal),
569
+ );
570
+ if (!sent && this.runtime.activeGoal.id === prioritized.id) {
571
+ this.runtime.queuedGoals = previousQueue;
572
+ if (previousGoal.status === "active") {
573
+ abortCurrentTurn(ctx);
574
+ this.runtime.activeGoal = transitionGoal(previousGoal, "paused");
575
+ this.runtime.blockStaleGoalToolCalls();
576
+ } else {
577
+ this.runtime.activeGoal = previousGoal;
578
+ if (previousGoal.status === "complete") {
579
+ this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
580
+ } else if (blocksStaleGoalToolCalls(previousGoal.status)) {
581
+ this.runtime.blockStaleGoalToolCalls();
582
+ }
583
+ }
584
+ this.runtime.persistGoal(this.runtime.activeGoal);
585
+ this.runtime.updateStatus(ctx, this.runtime.activeGoal);
586
+ this.runtime.restoreGoalToolVisibility(visibilityBeforeActivation);
587
+ return false;
588
+ }
589
+ ctx.ui.notify(`Goal prioritized: ${objective}`, "info");
590
+ return true;
591
+ }
592
+ }