@narumitw/pi-goal 0.46.0 → 0.49.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/src/menu.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
- import { type ActionMenuItem, defineMenu, runMenu } from "@narumitw/pi-tui-kit";
3
- import { formatDuration } from "./accounting.js";
2
+ import type { ActionMenuItem } from "@narumitw/pi-tui-kit";
3
+ import { formatTokenCount as formatCompactTokenCount, formatDuration } from "./accounting.js";
4
4
  import { parseTokenBudget } from "./command.js";
5
5
  import type { GoalCommandController } from "./commands.js";
6
6
  import type { ActiveGoal, PendingQueueAction } from "./persistence.js";
@@ -12,6 +12,7 @@ export const GOAL_MENU_ACTIONS = {
12
12
  startBudget: "Start with token budget…",
13
13
  pause: "Pause goal",
14
14
  resume: "Resume goal",
15
+ reviewSafety: "Review and continue…",
15
16
  increaseBudget: "Increase budget and resume…",
16
17
  edit: "Edit goal…",
17
18
  replace: "Replace goal…",
@@ -47,14 +48,25 @@ export interface GoalMenuState {
47
48
  actions: string[];
48
49
  }
49
50
 
50
- type ShowSettings = (ctx: ExtensionCommandContext) => Promise<void>;
51
- type GoalMenuScreen = "main" | "queue" | "status" | "help";
51
+ type ShowSettings = (ctx: ExtensionCommandContext, target?: "automatic") => Promise<void>;
52
+ type GoalMenuScreen =
53
+ | "main"
54
+ | "start-budget"
55
+ | "start-custom-budget"
56
+ | "increase-budget"
57
+ | "safety"
58
+ | "queue"
59
+ | "status"
60
+ | "help";
52
61
  type GoalMenuAction =
53
62
  | "start"
54
- | "start-budget"
63
+ | "start-with-budget"
64
+ | "start-with-custom-budget"
65
+ | "submit-increase-budget"
55
66
  | "pause"
56
67
  | "resume"
57
- | "increase-budget"
68
+ | "safety-resume"
69
+ | "safety-settings"
58
70
  | "edit"
59
71
  | "replace"
60
72
  | "settings"
@@ -68,28 +80,45 @@ type GoalMenuAction =
68
80
  export function buildGoalMenuState(runtime: GoalMenuRuntimeView): GoalMenuState {
69
81
  const goal = runtime.activeGoal;
70
82
  const queueCount = runtime.queuedGoals.length;
83
+ const pausedByAutomaticLimit =
84
+ goal?.status === "paused" && goal.safetyPauseCause === "continuation_limit";
71
85
  const state = runtime.queueFrozen
72
86
  ? "Queue frozen"
73
87
  : runtime.pendingQueueAction
74
88
  ? "Waiting for Pi to settle"
75
- : displayStatus(goal?.status);
89
+ : pausedByAutomaticLimit
90
+ ? "Paused — automatic-work limit reached"
91
+ : displayStatus(goal?.status);
76
92
  const automaticTurnLimit = runtime.settings.continuationLimits.automaticTurns;
93
+ const used = goal?.automaticModelTurns ?? 0;
77
94
  const automaticResponses =
78
95
  automaticTurnLimit === null
79
- ? `${goal?.automaticModelTurns ?? 0} automatic responses · Unlimited`
80
- : `${goal?.automaticModelTurns ?? 0}/${automaticTurnLimit} automatic responses`;
81
- const details = goal
96
+ ? `Automatic work: ${used} responses · Unlimited`
97
+ : `Automatic work: ${used} of ${automaticTurnLimit} responses${
98
+ used < automaticTurnLimit ? ` · ${automaticTurnLimit - used} remaining` : ""
99
+ }`;
100
+ const title = goal
82
101
  ? [
83
- goal.tokenBudget === undefined
84
- ? formatDuration(goal.timeUsedSeconds)
85
- : `${formatTokenCount(goal.tokensUsed)}/${formatTokenCount(goal.tokenBudget)}`,
102
+ `Goal · ${state}`,
103
+ safeGoalMenuText(goal.text),
104
+ `Usage: ${
105
+ goal.tokenBudget === undefined
106
+ ? formatDuration(goal.timeUsedSeconds)
107
+ : `${formatTokenCount(goal.tokensUsed)}/${formatTokenCount(goal.tokenBudget)}`
108
+ }`,
86
109
  automaticResponses,
87
- ...(queueCount > 0 ? [`${queueCount} queued`] : []),
88
- ].join(" · ")
89
- : "No goal is currently set";
90
- const title = goal
91
- ? `Goal · ${state}\n${safeGoalMenuText(goal.text)}\n${details}`
92
- : `Goal · ${state}\n${details}`;
110
+ ...(pausedByAutomaticLimit
111
+ ? ["Progress is saved. Review the safety limit before continuing."]
112
+ : []),
113
+ ...(queueCount > 0 ? [`Queue: ${queueCount} queued`] : []),
114
+ ].join("\n")
115
+ : [
116
+ `Goal · ${state}`,
117
+ "No goal is currently set",
118
+ automaticTurnLimit === null
119
+ ? "Automatic work is configured as Unlimited."
120
+ : `Automatic work is configured to pause after ${automaticTurnLimit} responses.`,
121
+ ].join("\n");
93
122
 
94
123
  if (runtime.queueFrozen || runtime.pendingQueueAction) {
95
124
  return {
@@ -111,6 +140,8 @@ export function buildGoalMenuState(runtime: GoalMenuRuntimeView): GoalMenuState
111
140
  actions.push(GOAL_MENU_ACTIONS.pause);
112
141
  } else if (goal.status === "budget_limited") {
113
142
  actions.push(GOAL_MENU_ACTIONS.increaseBudget);
143
+ } else if (pausedByAutomaticLimit) {
144
+ actions.push(GOAL_MENU_ACTIONS.reviewSafety);
114
145
  } else {
115
146
  actions.push(GOAL_MENU_ACTIONS.resume);
116
147
  }
@@ -140,7 +171,17 @@ export async function showGoalManager(
140
171
  const owner = runtime as GoalRuntime;
141
172
  const generation = owner.menuGeneration;
142
173
  const ownerSignal = owner.menuController?.signal;
174
+ const isMenuCurrent = () =>
175
+ owner.menuController === undefined ||
176
+ (generation === owner.menuGeneration && !owner.menuController.signal.aborted);
177
+ const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
178
+ if (!isMenuCurrent()) return;
143
179
  let displayedGoal: ActiveGoal | undefined;
180
+ let startBudgetQueueIdentity = currentGoalQueueIdentity(runtime);
181
+ let displayedBudgetGoal: ActiveGoal | undefined;
182
+ let displayedBudgetValue: number | undefined;
183
+ let displayedBudgetUsage: number | undefined;
184
+ let displayedBudgetStatus: ActiveGoal["status"] | undefined;
144
185
  let displayedQueueHead: ActiveGoal | undefined;
145
186
  let displayedQueueFirst: ActiveGoal | undefined;
146
187
  let displayedQueueLast: ActiveGoal | undefined;
@@ -151,6 +192,7 @@ export async function showGoalManager(
151
192
  refreshGoalMenuState(runtime, ctx);
152
193
  const state = buildGoalMenuState(runtime);
153
194
  displayedGoal = runtime.activeGoal;
195
+ startBudgetQueueIdentity = currentGoalQueueIdentity(runtime);
154
196
  return {
155
197
  kind: "actions",
156
198
  title: "Goal",
@@ -159,6 +201,118 @@ export async function showGoalManager(
159
201
  hint: "close",
160
202
  };
161
203
  },
204
+ "start-budget": () => {
205
+ return {
206
+ kind: "actions",
207
+ title: "Choose token budget",
208
+ lines: tokenBudgetGuidance(runtime.settings.continuationLimits.automaticTurns),
209
+ items: [
210
+ {
211
+ id: "25k",
212
+ label: "25k — Lower token ceiling",
213
+ description: "Set the cumulative token limit to 25k.",
214
+ action: "start-with-budget",
215
+ },
216
+ {
217
+ id: "100k",
218
+ label: "100k — Suggested",
219
+ description: "Set the cumulative token limit to 100k.",
220
+ action: "start-with-budget",
221
+ },
222
+ {
223
+ id: "300k",
224
+ label: "300k — Higher token ceiling",
225
+ description: "Set the cumulative token limit to 300k.",
226
+ action: "start-with-budget",
227
+ },
228
+ {
229
+ id: "custom",
230
+ label: "Set a custom budget…",
231
+ description: "Enter an exact cumulative token limit.",
232
+ to: "start-custom-budget",
233
+ },
234
+ { id: "back", label: "Back", action: "back" },
235
+ ],
236
+ hint: "back",
237
+ };
238
+ },
239
+ "start-custom-budget": () => ({
240
+ kind: "input",
241
+ title: "Custom token budget",
242
+ lines: customTokenBudgetGuidance(runtime.settings.continuationLimits.automaticTurns),
243
+ placeholder: "100k",
244
+ action: "start-with-custom-budget",
245
+ hint: "back",
246
+ }),
247
+ "increase-budget": () => {
248
+ const goal = runtime.activeGoal;
249
+ displayedBudgetGoal = goal;
250
+ displayedBudgetValue = goal?.tokenBudget;
251
+ displayedBudgetUsage = goal?.tokensUsed;
252
+ displayedBudgetStatus = goal?.status;
253
+ if (goal && goal.tokensUsed >= Number.MAX_SAFE_INTEGER) {
254
+ return {
255
+ kind: "detail",
256
+ title: "Increase token budget unavailable",
257
+ lines: [
258
+ `Current usage: ${formatBudgetDecisionValue(goal.tokensUsed)}`,
259
+ "No larger safe whole-number token budget is available. Progress remains saved; choose Back and clear or replace the goal when ready.",
260
+ ],
261
+ hint: "back",
262
+ };
263
+ }
264
+ return {
265
+ kind: "input",
266
+ title: "Increase token budget",
267
+ lines: goal
268
+ ? increaseTokenBudgetGuidance(goal, runtime.settings.continuationLimits.automaticTurns)
269
+ : ["The budget-limited goal is no longer available. Return to the Goal menu."],
270
+ placeholder: goal ? suggestedIncreasedBudget(goal) : "300k",
271
+ action: "submit-increase-budget",
272
+ hint: "back",
273
+ };
274
+ },
275
+ safety: () => {
276
+ const goal = runtime.activeGoal;
277
+ displayedGoal = goal;
278
+ const limit = runtime.settings.continuationLimits.automaticTurns;
279
+ const used = goal?.automaticModelTurns ?? 0;
280
+ const queueCount = runtime.queuedGoals.length;
281
+ return {
282
+ kind: "actions",
283
+ title: "Automatic work paused",
284
+ lines: goal
285
+ ? [
286
+ automaticPauseSummary(used, limit),
287
+ `${safeGoalMenuText(goal.text)} is preserved.`,
288
+ `${formatInteger(goal.tokensUsed)} cumulative tokens and ${formatDuration(goal.timeUsedSeconds)} active time are preserved.`,
289
+ `The objective, usage, and ${queueCount} queued ${queueCount === 1 ? "goal is" : "goals are"} preserved.`,
290
+ limit === null
291
+ ? "Continuing resets the counter to 0 and resumes with Unlimited automatic work."
292
+ : `Continuing resets the counter to 0 and allows up to ${limit} more automatic model responses.`,
293
+ ]
294
+ : ["The paused goal is no longer available. Return to the Goal menu."],
295
+ items: goal
296
+ ? [
297
+ {
298
+ id: "continue",
299
+ label:
300
+ limit === null
301
+ ? "Continue — Unlimited"
302
+ : `Continue — up to ${limit} more responses`,
303
+ action: "safety-resume" as const,
304
+ },
305
+ {
306
+ id: "settings",
307
+ label: "Change automatic-work limit…",
308
+ action: "safety-settings" as const,
309
+ },
310
+ { id: "back", label: "Back", action: "back" as const },
311
+ ]
312
+ : [{ id: "back", label: "Back", action: "back" as const }],
313
+ hint: "back",
314
+ };
315
+ },
162
316
  queue: () => {
163
317
  displayedQueueHead = runtime.activeGoal;
164
318
  displayedQueueFirst = runtime.queuedGoals[0];
@@ -200,6 +354,7 @@ export async function showGoalManager(
200
354
  runtime.settings.experimental.goals,
201
355
  runtime.queueFrozen,
202
356
  runtime.pendingQueueAction,
357
+ runtime.settings.continuationLimits.automaticTurns,
203
358
  ).split("\n")
204
359
  : ["No goal is currently set."],
205
360
  hint: "back",
@@ -216,8 +371,91 @@ export async function showGoalManager(
216
371
  await startFromMenu(commands, ctx);
217
372
  return { kind: "close" };
218
373
  },
219
- "start-budget": async () => {
220
- await startFromMenu(commands, ctx, true);
374
+ "start-with-budget": async ({ itemId, signal }) => {
375
+ const budget = parseTokenBudget(itemId);
376
+ if (budget === undefined) return { kind: "rejected" };
377
+ return startBudgetedGoal(
378
+ runtime,
379
+ commands,
380
+ ctx,
381
+ budget,
382
+ runtime.settings.continuationLimits.automaticTurns,
383
+ startBudgetQueueIdentity,
384
+ signal,
385
+ isMenuCurrent,
386
+ "stay",
387
+ );
388
+ },
389
+ "start-with-custom-budget": async ({ value, signal }) => {
390
+ const budget = parseTokenBudget(value ?? "");
391
+ if (budget === undefined) {
392
+ ctx.ui.notify(
393
+ "Enter a positive token amount, for example 25k, 300k, or 1.5m.",
394
+ "warning",
395
+ );
396
+ return { kind: "rejected" };
397
+ }
398
+ return startBudgetedGoal(
399
+ runtime,
400
+ commands,
401
+ ctx,
402
+ budget,
403
+ runtime.settings.continuationLimits.automaticTurns,
404
+ startBudgetQueueIdentity,
405
+ signal,
406
+ isMenuCurrent,
407
+ "back",
408
+ );
409
+ },
410
+ "submit-increase-budget": async ({ value, signal }) => {
411
+ const goal = displayedBudgetGoal;
412
+ const budget = parseTokenBudget(value ?? "");
413
+ if (budget === undefined) {
414
+ ctx.ui.notify(
415
+ "Enter a positive token amount, for example 300k, 1.5m, or 300000.",
416
+ "warning",
417
+ );
418
+ return { kind: "rejected" };
419
+ }
420
+ if (
421
+ !goal ||
422
+ !requireCurrentBudgetPreview(
423
+ runtime,
424
+ goal,
425
+ displayedBudgetValue,
426
+ displayedBudgetUsage,
427
+ displayedBudgetStatus,
428
+ ctx,
429
+ )
430
+ ) {
431
+ return { kind: "close" };
432
+ }
433
+ if (budget <= goal.tokensUsed) {
434
+ ctx.ui.notify(
435
+ `Enter a new cumulative total greater than current usage (${formatCompactTokenCount(goal.tokensUsed)}).`,
436
+ "warning",
437
+ );
438
+ return { kind: "rejected" };
439
+ }
440
+ const confirmed = await ctx.ui.confirm(
441
+ "Increase goal budget?",
442
+ increaseBudgetPreview(goal, budget, runtime.settings.continuationLimits.automaticTurns),
443
+ );
444
+ if (signal.aborted || !isMenuCurrent()) return { kind: "close" };
445
+ if (!confirmed) return { kind: "rejected" };
446
+ if (
447
+ !requireCurrentBudgetPreview(
448
+ runtime,
449
+ goal,
450
+ displayedBudgetValue,
451
+ displayedBudgetUsage,
452
+ displayedBudgetStatus,
453
+ ctx,
454
+ )
455
+ ) {
456
+ return { kind: "close" };
457
+ }
458
+ await commands.editGoal(goal.text, budget, ctx);
221
459
  return { kind: "close" };
222
460
  },
223
461
  pause: async () => {
@@ -234,13 +472,23 @@ export async function showGoalManager(
234
472
  await commands.resumeGoal(ctx);
235
473
  return { kind: "close" };
236
474
  },
237
- "increase-budget": async () => {
475
+ "safety-resume": async () => {
238
476
  if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) {
239
477
  return { kind: "stay" };
240
478
  }
241
- await increaseBudget(runtime, commands, ctx);
479
+ await commands.resumeGoal(ctx);
242
480
  return { kind: "close" };
243
481
  },
482
+ "safety-settings": async () => {
483
+ if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) {
484
+ return { kind: "stay" };
485
+ }
486
+ const expectedGoal = displayedGoal;
487
+ await showSettings(ctx, "automatic");
488
+ if (!isMenuCurrent()) return { kind: "close" };
489
+ if (!requireCurrentMenuGoal(runtime, expectedGoal, ctx)) return { kind: "stay" };
490
+ return { kind: "stay" };
491
+ },
244
492
  edit: async () => {
245
493
  if (!displayedGoal || !requireCurrentMenuGoal(runtime, displayedGoal, ctx)) {
246
494
  return { kind: "stay" };
@@ -332,23 +580,28 @@ export async function showGoalManager(
332
580
  await runMenu(ctx, menu, {
333
581
  getState: () => undefined,
334
582
  signal: ownerSignal,
335
- isCurrent: () =>
336
- owner.menuController === undefined ||
337
- (generation === owner.menuGeneration && !owner.menuController.signal.aborted),
583
+ isCurrent: isMenuCurrent,
338
584
  });
339
585
  }
340
586
 
341
587
  function goalMainMenuItem(label: string): ActionMenuItem<GoalMenuScreen, GoalMenuAction> {
342
588
  if (label === GOAL_MENU_ACTIONS.status) return { id: "status", label, to: "status" as const };
589
+ if (label === GOAL_MENU_ACTIONS.startBudget) {
590
+ return { id: "start-budget", label, to: "start-budget" as const };
591
+ }
592
+ if (label === GOAL_MENU_ACTIONS.increaseBudget) {
593
+ return { id: "increase-budget", label, to: "increase-budget" as const };
594
+ }
595
+ if (label === GOAL_MENU_ACTIONS.reviewSafety) {
596
+ return { id: "review-safety", label, to: "safety" as const };
597
+ }
343
598
  if (label === GOAL_MENU_ACTIONS.queue) return { id: "queue", label, to: "queue" as const };
344
599
  if (label === GOAL_MENU_ACTIONS.help) return { id: "help", label, to: "help" as const };
345
600
  if (label === GOAL_MENU_ACTIONS.close) return { id: "close", label, close: true as const };
346
601
  const actions = new Map<string, GoalMenuAction>([
347
602
  [GOAL_MENU_ACTIONS.start, "start"],
348
- [GOAL_MENU_ACTIONS.startBudget, "start-budget"],
349
603
  [GOAL_MENU_ACTIONS.pause, "pause"],
350
604
  [GOAL_MENU_ACTIONS.resume, "resume"],
351
- [GOAL_MENU_ACTIONS.increaseBudget, "increase-budget"],
352
605
  [GOAL_MENU_ACTIONS.edit, "edit"],
353
606
  [GOAL_MENU_ACTIONS.replace, "replace"],
354
607
  [GOAL_MENU_ACTIONS.settings, "settings"],
@@ -377,16 +630,108 @@ export function safeGoalMenuText(value: string, maxCharacters = 120) {
377
630
  : `${characters.slice(0, maxCharacters).join("")}…`;
378
631
  }
379
632
 
380
- async function startFromMenu(
633
+ async function startFromMenu(commands: GoalCommandController, ctx: ExtensionCommandContext) {
634
+ const objective = (await ctx.ui.editor("Goal objective", ""))?.trim();
635
+ if (!objective) return;
636
+ await commands.startGoal(objective, undefined, ctx);
637
+ }
638
+
639
+ async function startBudgetedGoal(
640
+ runtime: GoalMenuRuntimeView,
381
641
  commands: GoalCommandController,
382
642
  ctx: ExtensionCommandContext,
383
- withBudget = false,
643
+ budget: number,
644
+ automaticLimit: number | null,
645
+ expectedQueueIdentity: string,
646
+ signal: AbortSignal,
647
+ isMenuCurrent: () => boolean,
648
+ cancelTransition: "stay" | "back",
384
649
  ) {
385
- const objective = (await ctx.ui.editor("Goal objective", ""))?.trim();
386
- if (!objective) return;
387
- const tokenBudget = withBudget ? await askTokenBudget(ctx) : undefined;
388
- if (withBudget && tokenBudget === undefined) return;
389
- await commands.startGoal(objective, tokenBudget, ctx);
650
+ if (!requireCurrentStartBudgetQueue(runtime, expectedQueueIdentity, ctx)) {
651
+ return { kind: "rejected" as const };
652
+ }
653
+ const objective = (
654
+ await ctx.ui.editor(
655
+ `Goal objective · Token budget ${formatCompactTokenCount(budget)} · ${automaticLimit === null ? "Automatic Unlimited" : `Automatic limit ${automaticLimit}`}`,
656
+ "",
657
+ )
658
+ )?.trim();
659
+ if (signal.aborted || !isMenuCurrent()) return { kind: "close" as const };
660
+ if (!objective) return { kind: cancelTransition } as const;
661
+ if (!requireCurrentStartBudgetQueue(runtime, expectedQueueIdentity, ctx)) {
662
+ return { kind: "rejected" as const };
663
+ }
664
+ await commands.startGoal(
665
+ objective,
666
+ budget,
667
+ ctx,
668
+ undefined,
669
+ () => !signal.aborted && isMenuCurrent(),
670
+ () => !signal.aborted && isMenuCurrent(),
671
+ );
672
+ return { kind: "close" as const };
673
+ }
674
+
675
+ function tokenBudgetGuidance(automaticLimit: number | null) {
676
+ return [
677
+ "Set the maximum cumulative token usage for this goal.",
678
+ "The final model call may exceed the limit; this is not a dollar-cost cap.",
679
+ automaticBudgetGuidance(automaticLimit),
680
+ ];
681
+ }
682
+
683
+ function customTokenBudgetGuidance(automaticLimit: number | null) {
684
+ return [
685
+ "Enter the maximum cumulative token usage for this goal.",
686
+ "Examples: 25k, 300k, 1.5m, or 300000.",
687
+ "The final model call may exceed this value; this is not a dollar-cost cap.",
688
+ automaticBudgetGuidance(automaticLimit),
689
+ ];
690
+ }
691
+
692
+ function automaticBudgetGuidance(automaticLimit: number | null) {
693
+ return automaticLimit === null
694
+ ? "Automatic work has no response-count cap."
695
+ : `Automatic work will also pause after ${automaticLimit} responses.`;
696
+ }
697
+
698
+ function increaseTokenBudgetGuidance(goal: ActiveGoal, automaticLimit: number | null) {
699
+ return [
700
+ `Current budget: ${formatBudgetDecisionValue(goal.tokenBudget ?? 0)}`,
701
+ `Current usage: ${formatBudgetDecisionValue(goal.tokensUsed)}`,
702
+ `Enter a new cumulative total greater than ${formatBudgetDecisionValue(goal.tokensUsed)}.`,
703
+ "Examples: 300k, 1.5m, or 300000.",
704
+ "The final model call may exceed the limit; this is not a dollar-cost cap.",
705
+ automaticBudgetGuidance(automaticLimit),
706
+ ];
707
+ }
708
+
709
+ function suggestedIncreasedBudget(goal: ActiveGoal) {
710
+ const floor = Math.max(goal.tokensUsed, goal.tokenBudget ?? 0);
711
+ for (const suggestion of [25_000, 100_000, 300_000, 500_000, 1_000_000]) {
712
+ if (suggestion > floor) return formatCompactTokenCount(suggestion);
713
+ }
714
+ return formatCompactTokenCount(
715
+ Math.min(Number.MAX_SAFE_INTEGER, Math.max(Math.floor(floor) + 1, Math.ceil(floor * 2))),
716
+ );
717
+ }
718
+
719
+ function formatBudgetDecisionValue(value: number) {
720
+ const compact = formatCompactTokenCount(value);
721
+ if (value < 1_000 || value % 1_000 === 0) return compact;
722
+ return `${compact} (${formatInteger(value)} tokens)`;
723
+ }
724
+
725
+ function increaseBudgetPreview(goal: ActiveGoal, budget: number, automaticLimit: number | null) {
726
+ return [
727
+ `Goal: ${safeGoalMenuText(goal.text, 4_000)}`,
728
+ `Budget: ${formatCompactTokenCount(goal.tokenBudget ?? 0)} → ${formatCompactTokenCount(budget)}`,
729
+ `Current usage: ${formatCompactTokenCount(goal.tokensUsed)}`,
730
+ automaticLimit === null
731
+ ? "Automatic work: Unlimited after resume"
732
+ : `Automatic work: up to ${automaticLimit} more responses after resume`,
733
+ "The goal will resume immediately.",
734
+ ].join("\n");
390
735
  }
391
736
 
392
737
  async function editFromMenu(
@@ -409,42 +754,6 @@ async function editFromMenu(
409
754
  await commands.editGoal(objective, undefined, ctx);
410
755
  }
411
756
 
412
- async function increaseBudget(
413
- runtime: GoalMenuRuntimeView,
414
- commands: GoalCommandController,
415
- ctx: ExtensionCommandContext,
416
- ) {
417
- const goal = runtime.activeGoal;
418
- if (!goal) return;
419
- const budget = await askTokenBudget(ctx, goal.tokenBudget);
420
- if (budget === undefined || !requireCurrentMenuGoal(runtime, goal, ctx)) return;
421
- if (budget <= goal.tokensUsed) {
422
- ctx.ui.notify(
423
- `Token budget must be greater than current usage (${formatTokenCount(goal.tokensUsed)}).`,
424
- "warning",
425
- );
426
- return;
427
- }
428
- const confirmed = await ctx.ui.confirm(
429
- "Increase goal budget?",
430
- `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.`,
431
- );
432
- if (!confirmed || !requireCurrentMenuGoal(runtime, goal, ctx)) return;
433
- await commands.editGoal(goal.text, budget, ctx);
434
- }
435
-
436
- async function askTokenBudget(ctx: ExtensionCommandContext, current?: number) {
437
- const raw = await ctx.ui.input(
438
- "Token budget",
439
- current === undefined ? "100k" : formatTokenCount(current),
440
- );
441
- if (raw === undefined) return undefined;
442
- const budget = parseTokenBudget(raw);
443
- if (budget === undefined)
444
- ctx.ui.notify(`Invalid token budget: ${safeGoalMenuText(raw)}`, "warning");
445
- return budget;
446
- }
447
-
448
757
  async function confirmClear(runtime: GoalMenuRuntimeView, ctx: ExtensionCommandContext) {
449
758
  const goals = [runtime.activeGoal, ...runtime.queuedGoals].filter(
450
759
  (goal): goal is ActiveGoal => goal !== undefined,
@@ -466,6 +775,49 @@ async function confirmClear(runtime: GoalMenuRuntimeView, ctx: ExtensionCommandC
466
775
  );
467
776
  }
468
777
 
778
+ function currentGoalQueueIdentity(runtime: GoalMenuRuntimeView) {
779
+ return goalQueueIdentity(runtime.activeGoal, runtime.queuedGoals, runtime.pendingQueueAction);
780
+ }
781
+
782
+ function requireCurrentStartBudgetQueue(
783
+ runtime: GoalMenuRuntimeView,
784
+ expectedIdentity: string,
785
+ ctx: ExtensionCommandContext,
786
+ ) {
787
+ if (currentGoalQueueIdentity(runtime) === expectedIdentity) {
788
+ return true;
789
+ }
790
+ ctx.ui.notify(
791
+ "The goal queue changed while the token budget flow was open. Reopen /goal and try again.",
792
+ "warning",
793
+ );
794
+ return false;
795
+ }
796
+
797
+ function requireCurrentBudgetPreview(
798
+ runtime: GoalMenuRuntimeView,
799
+ expectedGoal: ActiveGoal,
800
+ expectedBudget: number | undefined,
801
+ expectedUsage: number | undefined,
802
+ expectedStatus: ActiveGoal["status"] | undefined,
803
+ ctx: ExtensionCommandContext,
804
+ ) {
805
+ const current = runtime.activeGoal;
806
+ if (
807
+ current?.id === expectedGoal.id &&
808
+ current.tokenBudget === expectedBudget &&
809
+ current.tokensUsed === expectedUsage &&
810
+ current.status === expectedStatus
811
+ ) {
812
+ return true;
813
+ }
814
+ ctx.ui.notify(
815
+ "The goal changed or its usage changed while the budget dialog was open. Reopen /goal and try again.",
816
+ "warning",
817
+ );
818
+ return false;
819
+ }
820
+
469
821
  function requireCurrentQueueHead(
470
822
  runtime: GoalMenuRuntimeView,
471
823
  expectedGoal: ActiveGoal,
@@ -527,6 +879,20 @@ function formatTokenCount(tokens: number) {
527
879
  return String(tokens);
528
880
  }
529
881
 
882
+ function formatInteger(value: number) {
883
+ return value.toLocaleString("en-US", { maximumFractionDigits: 0 });
884
+ }
885
+
886
+ function automaticPauseSummary(used: number, limit: number | null) {
887
+ if (limit === null) {
888
+ return `Goal paused after ${used} responses at its previous safety limit. Current limit: Unlimited.`;
889
+ }
890
+ if (used < limit) {
891
+ return `Goal paused after ${used} responses at its previous safety limit. Current automatic-work limit: ${limit}.`;
892
+ }
893
+ return `Goal reached its ${used}-of-${limit} safety limit.`;
894
+ }
895
+
530
896
  function isTerminalControl(character: string) {
531
897
  const codePoint = character.codePointAt(0) ?? 0;
532
898
  return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);