@narumitw/pi-goal 0.51.0 → 0.52.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/src/lifecycle.ts CHANGED
@@ -1,10 +1,8 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { currentTokenTotal } from "./accounting.js";
3
- import type { GoalCommandController } from "./commands.js";
4
3
  import { notifyTerminal } from "./errors.js";
5
4
  import { type ActiveGoal, loadGoalStateFromSession } from "./persistence.js";
6
- import { buildGoalPrompt, buildGoalSystemPrompt } from "./prompts.js";
7
- import { activateQueuedGoal } from "./queue.js";
5
+ import { buildGoalSystemPrompt } from "./prompts.js";
8
6
  import type { GoalRunController } from "./run-protocol.js";
9
7
  import {
10
8
  type AssistantMessageLike,
@@ -25,8 +23,10 @@ import {
25
23
  import { hasAssistantToolCall } from "./safety.js";
26
24
  import { DEFAULT_GOAL_SETTINGS, readGoalSettings } from "./settings.js";
27
25
 
28
- const EXPERIMENTAL_GOALS_WARNING =
29
- "Experimental ordered goals are enabled for pi-goal. Queue behavior and persisted state may change.";
26
+ const REMOVED_QUEUE_SETTING_WARNING =
27
+ "Ordered goal queue has been removed. Use /goal edit to reprioritize an active objective, or start /goal <objectives> if no active goal exists.";
28
+ const REMOVED_PERSISTED_QUEUE_WARNING =
29
+ "Ordered goal queue has been removed. Start /goal <objectives> to continue with one merged objective, or use /goal clear to discard the old queue state.";
30
30
 
31
31
  interface GoalLifecycleOptions {
32
32
  settingsPath?: string;
@@ -35,7 +35,6 @@ interface GoalLifecycleOptions {
35
35
  export function registerGoalLifecycle(
36
36
  pi: ExtensionAPI,
37
37
  runtime: GoalRuntime,
38
- commands: GoalCommandController,
39
38
  runController: GoalRunController,
40
39
  options: GoalLifecycleOptions = {},
41
40
  ) {
@@ -50,10 +49,8 @@ export function registerGoalLifecycle(
50
49
  runtime.clearGoalRecovery();
51
50
  runtime.clearBudgetWrapUp();
52
51
  runtime.clearStaleGoalToolCallBlock();
53
- runtime.queuedGoals = [];
54
- runtime.pendingQueueAction = undefined;
55
- runtime.queueFrozen = false;
56
- runtime.queueFreezeAwaitingSettle = false;
52
+ runtime.legacyQueueState = undefined;
53
+ runtime.legacyExperimentalGoalsSetting = false;
57
54
  runtime.clearTerminalDetails();
58
55
  const previousToolVisibility = runtime.settings.toolVisibility;
59
56
  const settingsResult = readGoalSettings(options.settingsPath);
@@ -67,9 +64,8 @@ export function registerGoalLifecycle(
67
64
  "warning",
68
65
  );
69
66
  }
70
- if (runtime.settings.experimental.goals) {
71
- notifyTerminal(ctx.ui, EXPERIMENTAL_GOALS_WARNING, "warning");
72
- }
67
+ runtime.legacyExperimentalGoalsSetting =
68
+ settingsResult.kind !== "invalid" && settingsResult.legacyExperimentalGoals;
73
69
  try {
74
70
  runtime.toolPolicy.prepareSessionStart(
75
71
  runtime.settings.toolVisibility,
@@ -85,30 +81,21 @@ export function registerGoalLifecycle(
85
81
 
86
82
  const loaded = loadGoalStateFromSession(ctx);
87
83
  runtime.activeGoal = loaded.goal;
88
- runtime.queuedGoals = loaded.queue;
89
- runtime.pendingQueueAction = loaded.pendingAction;
90
- runtime.queueFrozen = loaded.hasExperimentalQueueState && !runtime.settings.experimental.goals;
84
+ runtime.legacyQueueState = loaded.legacyQueueState;
91
85
  runController.bindSession(ctx);
92
- if (runtime.queueFrozen) {
93
- if (runtime.activeGoal) runtime.persistGoal(runtime.activeGoal);
94
- ctx.ui.setStatus(STATUS_KEY, "queue off");
95
- notifyTerminal(
96
- ctx.ui,
97
- "An experimental goal queue is frozen because experimental.goals is disabled. Re-enable it and run /reload to continue, or use /goal clear.",
98
- "warning",
99
- );
86
+ if (runtime.legacyQueueState) {
87
+ runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, false);
88
+ ctx.ui.setStatus(STATUS_KEY, undefined);
89
+ notifyTerminal(ctx.ui, REMOVED_PERSISTED_QUEUE_WARNING, "warning");
100
90
  return;
101
91
  }
102
-
103
- let startRestoredQueuedGoal = false;
104
- if (runtime.activeGoal?.status === "queued" && !runtime.pendingQueueAction) {
105
- runtime.activeGoal = activateQueuedGoal(runtime.activeGoal, currentTokenTotal(ctx));
106
- startRestoredQueuedGoal = runtime.activeGoal.status === "active";
92
+ if (runtime.legacyExperimentalGoalsSetting) {
93
+ notifyTerminal(ctx.ui, REMOVED_QUEUE_SETTING_WARNING, "warning");
107
94
  }
108
- if (runtime.pendingQueueAction) await commands.dispatchPendingQueueActionIfSettled(ctx);
95
+
109
96
  if (runtime.activeGoal) {
110
97
  if (runtime.activeGoal.status === "active" && runtime.activeGoal.safetyResetPending) {
111
- // Resume/edit activation is persisted before its queued prompt starts. A
98
+ // Resume/edit activation is persisted before its owned prompt starts. A
112
99
  // reload must commit that promised reset before enforcing the old limits.
113
100
  runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
114
101
  }
@@ -128,23 +115,6 @@ export function registerGoalLifecycle(
128
115
  runtime.persistGoal(runtime.activeGoal);
129
116
  runtime.updateStatus(ctx, runtime.activeGoal);
130
117
  runtime.restoreGoalWaitTimer(ctx);
131
- if (startRestoredQueuedGoal) {
132
- const restoredGoal = runtime.activeGoal;
133
- const sent = await runtime.sendOwnedGoalPrompt(
134
- ctx,
135
- restoredGoal.id,
136
- buildGoalPrompt(restoredGoal),
137
- false, // Reloaded queue activation preserves its persisted safety epoch.
138
- );
139
- if (!sent && runtime.activeGoal?.id === restoredGoal.id) {
140
- runtime.stopActiveGoal(ctx, {
141
- kind: "activation_rollback",
142
- expectedGoalId: restoredGoal.id,
143
- restoreGoal: restoredGoal,
144
- abortTurn: false,
145
- });
146
- }
147
- }
148
118
  } else {
149
119
  runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, false);
150
120
  ctx.ui.setStatus(STATUS_KEY, undefined);
@@ -156,7 +126,7 @@ export function registerGoalLifecycle(
156
126
  runtime.closeMenuSession();
157
127
  runtime.clearGoalWaitTimer();
158
128
  if (runtime.activeGoal) {
159
- if (!runtime.queueFrozen && runtime.activeGoal.status === "active") {
129
+ if (runtime.activeGoal.status === "active") {
160
130
  runtime.recordGoalUsage(runtime.activeGoal, ctx, false);
161
131
  }
162
132
  runtime.persistGoal(runtime.activeGoal);
@@ -169,17 +139,14 @@ export function registerGoalLifecycle(
169
139
  runtime.clearBudgetWrapUp();
170
140
  runtime.clearStaleGoalToolCallBlock();
171
141
  runtime.activeGoal = undefined;
172
- runtime.queuedGoals = [];
173
- runtime.pendingQueueAction = undefined;
174
- runtime.queueFrozen = false;
175
- runtime.queueFreezeAwaitingSettle = false;
142
+ runtime.legacyQueueState = undefined;
143
+ runtime.legacyExperimentalGoalsSetting = false;
176
144
  ctx.ui.setStatus(STATUS_KEY, undefined);
177
145
  runtime.clearCompletionStatusTimer();
178
146
  runtime.clearTerminalDetails();
179
147
  });
180
148
 
181
149
  pi.on("session_before_compact", (event, ctx) => {
182
- if (runtime.queueFrozen) return;
183
150
  if (runtime.activeGoal?.status === "budget_limited") {
184
151
  if ((event as { willRetry?: boolean }).willRetry === true) return { cancel: true as const };
185
152
  return;
@@ -189,33 +156,24 @@ export function registerGoalLifecycle(
189
156
  runtime.cancelContinuationWork();
190
157
  runtime.persistGoal(runtime.activeGoal);
191
158
  runtime.updateStatus(ctx, runtime.activeGoal);
192
- if (runtime.pendingQueueAction) return;
193
159
  if (runtime.limitActiveGoalForBudget(ctx, false)) return { cancel: true as const };
194
160
  });
195
161
 
196
162
  pi.on("session_compact", async (event, ctx) => {
197
- if (runtime.queueFrozen) return;
198
163
  if (runtime.activeGoal?.status !== "active") {
199
164
  runtime.clearGoalRecovery();
200
- if (runtime.pendingQueueAction) await commands.dispatchPendingQueueActionIfSettled(ctx);
201
165
  return;
202
166
  }
203
167
 
204
168
  const restoredState = loadGoalStateFromSession(ctx);
205
169
  if (restoredState.goal?.id === runtime.activeGoal.id) {
206
170
  runtime.activeGoal = restoredState.goal;
207
- runtime.queuedGoals = restoredState.queue;
208
- runtime.pendingQueueAction = restoredState.pendingAction;
209
171
  }
210
172
  const usageRecorded = runtime.recordGoalUsage(runtime.activeGoal, ctx);
211
173
  if (usageRecorded) {
212
174
  runtime.persistGoal(runtime.activeGoal);
213
175
  runtime.updateStatus(ctx, runtime.activeGoal);
214
176
  }
215
- if (runtime.pendingQueueAction) {
216
- await commands.dispatchPendingQueueActionIfSettled(ctx);
217
- return;
218
- }
219
177
  if (!usageRecorded) return;
220
178
  if (runtime.limitActiveGoalForBudget(ctx, false)) return;
221
179
 
@@ -239,7 +197,6 @@ export function registerGoalLifecycle(
239
197
  ) {
240
198
  return { action: "handled" as const };
241
199
  }
242
- if (runtime.queueFrozen) return;
243
200
  // Streaming input is queued before its model work starts. Keep owned
244
201
  // markers pending for message_start, and track non-goal delivery mode so a
245
202
  // steer cannot consume a later follow-up's cleanup protection.
@@ -252,7 +209,6 @@ export function registerGoalLifecycle(
252
209
  runtime.clearGoalRecovery();
253
210
  return;
254
211
  }
255
- if (runtime.queueFrozen) return;
256
212
  if (/^\/goal(?:\s|$)/u.test(event.text.trimStart())) return;
257
213
  if (runtime.activeGoal?.waiting) runtime.clearGoalWait(ctx, runtime.activeGoal.id);
258
214
  if (event.streamingBehavior === "followUp") {
@@ -340,17 +296,6 @@ export function registerGoalLifecycle(
340
296
 
341
297
  pi.on("tool_call", (event, ctx) => {
342
298
  runtime.markAgentToolAttempted();
343
- if (runtime.queueFrozen) {
344
- if (!runtime.toolPolicy.isGoalToolName(event.toolName)) return;
345
- // Blocking alone feeds an error tool result back to the model. Abort too so
346
- // stale Goal calls cannot loop while the experimental queue remains frozen.
347
- abortCurrentTurn(ctx);
348
- return {
349
- block: true,
350
- reason:
351
- "The experimental goal queue is frozen. Re-enable experimental.goals and run /reload, or use /goal clear.",
352
- };
353
- }
354
299
  if (
355
300
  runtime.activeGoal?.status === "budget_limited" &&
356
301
  runtime.budgetWrapUp?.goalId === runtime.activeGoal.id &&
@@ -380,7 +325,6 @@ export function registerGoalLifecycle(
380
325
  });
381
326
 
382
327
  pi.on("tool_execution_end", (_event, ctx) => {
383
- if (runtime.queueFrozen) return;
384
328
  if (
385
329
  runtime.activeGoal?.status === "budget_limited" &&
386
330
  runtime.budgetWrapUp?.goalId === runtime.activeGoal.id &&
@@ -402,7 +346,6 @@ export function registerGoalLifecycle(
402
346
 
403
347
  pi.on("before_agent_start", (event, ctx) => {
404
348
  runtime.clearAgentRun();
405
- if (runtime.queueFrozen) return;
406
349
  // Pi-owned retries emit agent_start directly. Reaching a normal prompt
407
350
  // boundary means cleanup no longer owns the next run, so the hard-cap guard
408
351
  // must not abort it.
@@ -434,40 +377,10 @@ export function registerGoalLifecycle(
434
377
  : activeGoalRecovery && runtime.goalRecovery?.automaticOwner
435
378
  ? "automatic"
436
379
  : "manual";
437
- if (
438
- runtime.pendingQueueAction?.kind === "prioritize" &&
439
- !activeBudgetWrapUp &&
440
- !activeGoalRecovery
441
- ) {
442
- // A turn that starts after priority intent is committed belongs to neither
443
- // the displaced goal nor the not-yet-activated urgent goal. Persist the
444
- // displaced goal's final accounting boundary so reload cannot absorb this run.
445
- if (!runtime.pendingQueueAction.displacedUsageFinalized) {
446
- if (runtime.activeGoal?.status === "active") {
447
- runtime.recordGoalUsage(runtime.activeGoal, ctx, false);
448
- }
449
- runtime.pendingQueueAction.displacedUsageFinalized = true;
450
- if (runtime.activeGoal) {
451
- runtime.persistGoal(runtime.activeGoal);
452
- runtime.updateStatus(ctx, runtime.activeGoal);
453
- }
454
- }
455
- runtime.beginAgentRun(null, undefined);
456
- if (ownedPromptGoalId) abortCurrentTurn(ctx);
457
- return;
458
- }
459
380
  if (activeBudgetWrapUp && runtime.activeGoal) {
460
381
  runtime.beginAgentRun(runtime.activeGoal.id, "manual");
461
382
  return;
462
383
  }
463
- if (
464
- runtime.pendingQueueAction?.kind === "advance" &&
465
- runtime.pendingQueueAction.goalId === runtime.activeGoal?.id
466
- ) {
467
- runtime.beginAgentRun(ownedPromptGoalId ?? runtime.activeGoal.id, runOrigin);
468
- if (ownedPromptGoalId) abortCurrentTurn(ctx);
469
- return;
470
- }
471
384
  if (ownedPromptGoalId && ownedPromptGoalId !== runtime.activeGoal?.id) {
472
385
  runtime.beginAgentRun(ownedPromptGoalId, runOrigin);
473
386
  if (runtime.activeGoal?.status === "active" && !runtime.toolPolicy.toolsAvailable()) {
@@ -494,7 +407,6 @@ export function registerGoalLifecycle(
494
407
  });
495
408
 
496
409
  pi.on("agent_start", (_event, _ctx) => {
497
- if (runtime.queueFrozen) return;
498
410
  const activeGoal = runtime.activeGoal;
499
411
  if (
500
412
  activeGoal &&
@@ -514,13 +426,12 @@ export function registerGoalLifecycle(
514
426
  });
515
427
 
516
428
  pi.on("turn_end", (event, ctx) => {
517
- if (runtime.queueFrozen) return;
518
429
  runtime.recordAutomaticTurn(ctx, event.message);
519
430
  });
520
431
 
521
432
  pi.on("agent_end", (event, ctx) => {
522
433
  const run = runtime.finishAgentRun();
523
- if (runtime.queueFrozen || run.goalId === null) return;
434
+ if (run.goalId === null) return;
524
435
  if (!runtime.canRecordGoalUsage() && !runtime.hasActiveBudgetWrapUp()) return;
525
436
  if (run.goalId && run.goalId !== runtime.activeGoal?.id) return;
526
437
  if (!runtime.activeGoal) return;
@@ -535,15 +446,6 @@ export function registerGoalLifecycle(
535
446
  return;
536
447
  }
537
448
  if (runtime.activeGoal.status !== "active") return;
538
- if (
539
- runtime.pendingQueueAction?.kind === "advance" &&
540
- runtime.pendingQueueAction.goalId === runtime.activeGoal.id
541
- ) {
542
- runtime.recordGoalUsage(runtime.activeGoal, ctx);
543
- runtime.persistGoal(runtime.activeGoal);
544
- runtime.updateStatus(ctx, runtime.activeGoal);
545
- return;
546
- }
547
449
 
548
450
  const goalId = runtime.activeGoal.id;
549
451
  const alreadyAwaitingContinuation = runtime.hasContinuationWorkForGoal(goalId);
@@ -611,28 +513,13 @@ export function registerGoalLifecycle(
611
513
 
612
514
  const currentGoal = runtime.activeGoal;
613
515
  if (!currentGoal || currentGoal.id !== goalId || currentGoal.status !== "active") return;
614
- if (runtime.pendingQueueAction?.kind === "prioritize") return;
615
516
  runtime.requestContinuation(currentGoal);
616
517
  });
617
518
 
618
- pi.on("agent_settled", async (_event, ctx) => {
619
- if (runtime.queueFrozen) {
620
- runtime.clearSettledSafetyTracking();
621
- runtime.queueFreezeAwaitingSettle = false;
622
- if (runtime.settings.experimental.goals) {
623
- await commands.resumeQueueAfterUnfreeze(ctx);
624
- }
625
- return;
626
- }
519
+ pi.on("agent_settled", (_event, ctx) => {
627
520
  runtime.finalizeSettledRecovery(ctx);
628
- let dispatchedQueueAction = false;
629
- if (runtime.pendingQueueAction) {
630
- dispatchedQueueAction = await commands.dispatchPendingQueueActionIfSettled(ctx);
631
- }
632
- if (!dispatchedQueueAction) {
633
- const resumedWait = runtime.dispatchDueGoalWait(ctx);
634
- if (!resumedWait) runtime.dispatchContinuationIfSettled(ctx);
635
- }
521
+ const resumedWait = runtime.dispatchDueGoalWait(ctx);
522
+ if (!resumedWait) runtime.dispatchContinuationIfSettled(ctx);
636
523
  runtime.clearSettledSafetyTracking();
637
524
  });
638
525