@narumitw/pi-goal 0.54.3 → 0.54.5

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.
Files changed (39) hide show
  1. package/README.md +117 -190
  2. package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
  3. package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
  4. package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
  5. package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
  6. package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
  7. package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
  8. package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
  9. package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
  10. package/dist/index.ts +111 -126
  11. package/dist/index.ts.map +2 -2
  12. package/docs/managing-goals.md +47 -0
  13. package/docs/settings.md +91 -0
  14. package/package.json +55 -58
  15. package/src/accounting.ts +62 -66
  16. package/src/command-registration.ts +104 -102
  17. package/src/command.ts +101 -112
  18. package/src/commands.ts +465 -501
  19. package/src/errors.ts +127 -122
  20. package/src/goal-contract.ts +64 -64
  21. package/src/goal.ts +25 -25
  22. package/src/lifecycle.ts +615 -628
  23. package/src/markers.ts +6 -6
  24. package/src/menu.ts +565 -591
  25. package/src/persistence.ts +177 -192
  26. package/src/prompts.ts +53 -67
  27. package/src/run-protocol.ts +299 -325
  28. package/src/runtime.ts +1422 -1495
  29. package/src/safety.ts +51 -51
  30. package/src/settings-ui.ts +366 -393
  31. package/src/settings.ts +134 -147
  32. package/src/tool-policy.ts +6 -6
  33. package/src/tools.ts +371 -347
  34. package/src/wait.ts +47 -51
  35. package/src/workflow-mutex.ts +68 -68
  36. package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
  37. package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
  38. package/dist/chunks/menu-WEFDREGC.js.map +0 -7
  39. package/dist/chunks/settings-ui-MVYTTBHW.js.map +0 -7
@@ -7,26 +7,18 @@ import {
7
7
  formatError,
8
8
  formatTokenCount,
9
9
  isNonNegativeFiniteNumber,
10
+ isStaleContextError,
10
11
  nonNegativeFiniteNumber,
11
12
  normalizeTokenBudget,
12
13
  notifyTerminal,
13
14
  safeGoalMenuText,
14
15
  truncateNotification,
15
16
  updateGoalUsage
16
- } from "./chunk-UQRRW4CY.js";
17
+ } from "./chunk-WQIA35U2.js";
17
18
 
18
19
  // src/command.ts
19
20
  var MAX_OBJECTIVE_LENGTH = 4e3;
20
- var REMOVED_QUEUE_COMMANDS = /* @__PURE__ */ new Set([
21
- "add",
22
- "prioritize",
23
- "drop-last",
24
- "skip",
25
- "push",
26
- "unshift",
27
- "pop",
28
- "shift"
29
- ]);
21
+ var REMOVED_QUEUE_COMMANDS = /* @__PURE__ */ new Set(["add", "prioritize", "drop-last", "skip", "push", "unshift", "pop", "shift"]);
30
22
  var TOKEN_BUDGET_COMPLETION = {
31
23
  value: "--tokens ",
32
24
  label: "--tokens",
@@ -66,8 +58,7 @@ function parseCommand(args) {
66
58
  const [first, ...rest] = tokens;
67
59
  if (first === "pause") return rest.length === 0 ? { kind: "pause" } : "Usage: /goal pause";
68
60
  if (first === "resume") return rest.length === 0 ? { kind: "resume" } : "Usage: /goal resume";
69
- if (first === "clear" || first === "stop")
70
- return rest.length === 0 ? { kind: "clear" } : "Usage: /goal clear";
61
+ if (first === "clear" || first === "stop") return rest.length === 0 ? { kind: "clear" } : "Usage: /goal clear";
71
62
  if (first === "status") return rest.length === 0 ? { kind: "show" } : "Usage: /goal status";
72
63
  if (first === "edit") return parseObjective("edit", rest);
73
64
  return parseObjective("start", tokens);
@@ -429,34 +420,31 @@ function loadGoalStateFromSession(ctx) {
429
420
  const entries = ctx.sessionManager?.getBranch?.() ?? ctx.sessionManager?.getEntries?.() ?? [];
430
421
  const canonicalEntry = entries.filter((entry) => entry.type === "custom" && entry.customType === GOAL_STATE_ENTRY_TYPE).pop();
431
422
  if (canonicalEntry) return loadCanonicalGoalState(canonicalEntry.data);
432
- const legacyEntry = entries.filter(
433
- (entry) => entry.type === "custom" && entry.customType === LEGACY_GOALS_STATE_ENTRY_TYPE
434
- ).pop();
435
- return legacyEntry ? loadLegacyGoalsState(legacyEntry.data) : emptyGoalState("none");
423
+ const legacyEntry = entries.filter((entry) => entry.type === "custom" && entry.customType === LEGACY_GOALS_STATE_ENTRY_TYPE).pop();
424
+ return legacyEntry ? loadLegacyGoalsState(legacyEntry.data) : emptyGoalState();
436
425
  }
437
426
  function loadCanonicalGoalState(data) {
438
- if (!isRecord2(data)) return emptyGoalState("canonical");
427
+ if (!isRecord2(data)) return emptyGoalState();
439
428
  const rawGoal = data.goal;
440
- if (rawGoal !== null && !isGoal(rawGoal)) return emptyGoalState("canonical");
429
+ if (rawGoal !== null && !isStoredGoal(rawGoal)) return emptyGoalState();
441
430
  const rawQueue = Object.hasOwn(data, "queue") ? data.queue : void 0;
442
431
  if (rawQueue !== void 0 && (!Array.isArray(rawQueue) || !rawQueue.every(isQueueGoal))) {
443
- return emptyGoalState("canonical");
432
+ return emptyGoalState();
444
433
  }
445
434
  const pendingAction = Object.hasOwn(data, "pendingAction") ? normalizeCanonicalPendingAction(data.pendingAction) : void 0;
446
- if (Object.hasOwn(data, "pendingAction") && !pendingAction) return emptyGoalState("canonical");
435
+ if (Object.hasOwn(data, "pendingAction") && !pendingAction) return emptyGoalState();
447
436
  const hasQueueFields = rawQueue !== void 0 || pendingAction !== void 0;
448
- if (hasQueueFields || isGoal(rawGoal) && rawGoal.status === "queued") {
449
- return legacyQueueState("canonical", {
450
- reason: "canonical-queue",
437
+ if (hasQueueFields || isStoredGoal(rawGoal) && rawGoal.status === "queued") {
438
+ return legacyQueueState({
451
439
  retainedGoals: countCanonicalLegacyGoals(rawGoal, rawQueue, pendingAction)
452
440
  });
453
441
  }
454
- let goal = rawGoal === null ? void 0 : normalizeLoadedGoal(rawGoal);
442
+ let goal = rawGoal === null || !isCanonicalGoal(rawGoal) ? void 0 : normalizeLoadedGoal(rawGoal);
455
443
  if (goal?.status === "complete") goal = void 0;
456
- return { goal, legacyQueueState: void 0, source: "canonical" };
444
+ return { goal, legacyQueueState: void 0 };
457
445
  }
458
446
  function countCanonicalLegacyGoals(goal, queue, pendingAction) {
459
- let count = isGoal(goal) && goal.status !== "complete" ? 1 : 0;
447
+ let count = isStoredGoal(goal) && goal.status !== "complete" ? 1 : 0;
460
448
  count += (queue ?? []).filter((queuedGoal) => queuedGoal.status !== "complete").length;
461
449
  if (pendingAction?.kind === "prioritize") count += 1;
462
450
  return count;
@@ -470,27 +458,26 @@ function normalizeCanonicalPendingAction(value) {
470
458
  return void 0;
471
459
  }
472
460
  function loadLegacyGoalsState(data) {
473
- if (!isRecord2(data)) return emptyGoalState("legacy-goals");
461
+ if (!isRecord2(data)) return emptyGoalState();
474
462
  let rawGoals;
475
463
  if (Array.isArray(data.goals)) {
476
- if (!data.goals.every(isGoal)) return emptyGoalState("legacy-goals");
464
+ if (!data.goals.every(isStoredGoal)) return emptyGoalState();
477
465
  rawGoals = data.goals.filter((goal) => goal.status !== "complete");
478
- } else if (isGoal(data.goal) && data.goal.status !== "complete") {
466
+ } else if (isStoredGoal(data.goal) && data.goal.status !== "complete") {
479
467
  rawGoals = [data.goal];
480
468
  } else {
481
469
  rawGoals = [];
482
470
  }
483
471
  const pendingAction = normalizeLegacyPendingPrioritize(data.pendingUnshift);
484
- if (rawGoals.length === 1 && rawGoals[0]?.status !== "queued" && pendingAction === void 0) {
472
+ const onlyGoal = rawGoals.length === 1 ? rawGoals[0] : void 0;
473
+ if (onlyGoal && isCanonicalGoal(onlyGoal) && pendingAction === void 0) {
485
474
  return {
486
- goal: normalizeLoadedGoal(rawGoals[0]),
487
- legacyQueueState: void 0,
488
- source: "legacy-goals"
475
+ goal: normalizeLoadedGoal(onlyGoal),
476
+ legacyQueueState: void 0
489
477
  };
490
478
  }
491
- if (rawGoals.length === 0 && pendingAction === void 0) return emptyGoalState("legacy-goals");
492
- return legacyQueueState("legacy-goals", {
493
- reason: "legacy-goals",
479
+ if (rawGoals.length === 0 && pendingAction === void 0) return emptyGoalState();
480
+ return legacyQueueState({
494
481
  retainedGoals: rawGoals.length + (pendingAction ? 1 : 0)
495
482
  });
496
483
  }
@@ -548,36 +535,31 @@ function readState() {
548
535
  return {};
549
536
  }
550
537
  }
551
- function isGoal(value) {
538
+ function isStoredGoal(value) {
552
539
  if (!isRecord2(value)) return false;
553
- return typeof value.id === "string" && Boolean(value.id) && value.id === value.id.trim() && validObjective(value.text) && [
554
- "active",
555
- "queued",
556
- "paused",
557
- "blocked",
558
- "usage_limited",
559
- "budget_limited",
560
- "complete"
561
- ].includes(String(value.status)) && typeof value.startedAt === "number" && typeof value.updatedAt === "number" && typeof value.iteration === "number" && typeof value.tokensUsed === "number" && typeof value.timeUsedSeconds === "number" && typeof value.baselineTokens === "number" && (value.activeStartedAt === void 0 || typeof value.activeStartedAt === "number") && (value.safetyResetPending === void 0 || typeof value.safetyResetPending === "boolean");
540
+ return typeof value.id === "string" && Boolean(value.id) && value.id === value.id.trim() && validObjective(value.text) && ["active", "queued", "paused", "blocked", "usage_limited", "budget_limited", "complete"].includes(
541
+ String(value.status)
542
+ ) && typeof value.startedAt === "number" && typeof value.updatedAt === "number" && typeof value.iteration === "number" && typeof value.tokensUsed === "number" && typeof value.timeUsedSeconds === "number" && typeof value.baselineTokens === "number" && (value.activeStartedAt === void 0 || typeof value.activeStartedAt === "number") && (value.safetyResetPending === void 0 || typeof value.safetyResetPending === "boolean");
543
+ }
544
+ function isCanonicalGoal(goal) {
545
+ return goal.status !== "queued";
562
546
  }
563
547
  function isQueueGoal(value) {
564
- return isGoal(value) && value.status !== "complete";
548
+ return isStoredGoal(value) && value.status !== "complete";
565
549
  }
566
550
  function isRecord2(value) {
567
551
  return typeof value === "object" && value !== null && !Array.isArray(value);
568
552
  }
569
- function emptyGoalState(source) {
553
+ function emptyGoalState() {
570
554
  return {
571
555
  goal: void 0,
572
- legacyQueueState: void 0,
573
- source
556
+ legacyQueueState: void 0
574
557
  };
575
558
  }
576
- function legacyQueueState(source, legacyQueue) {
559
+ function legacyQueueState(legacyQueue) {
577
560
  return {
578
561
  goal: void 0,
579
- legacyQueueState: legacyQueue,
580
- source
562
+ legacyQueueState: legacyQueue
581
563
  };
582
564
  }
583
565
 
@@ -719,7 +701,7 @@ var STATUS_KEY = "goal";
719
701
  var GOAL_STATE_ENTRY_TYPE2 = "goal-state";
720
702
  var MAX_GOAL_ID_LENGTH = 128;
721
703
  function isTerminalGoalStatus(status) {
722
- return status !== "active" && status !== "queued";
704
+ return status !== "active";
723
705
  }
724
706
  function buildGoalStateSnapshot(goal, summary, reason) {
725
707
  const snapshot = { goalId: goal.id, status: goal.status };
@@ -926,7 +908,7 @@ var GoalRuntime = class {
926
908
  if (this.activeGoal?.id === intent.goalId && this.activeGoal.status === "active") {
927
909
  this.continuationIntent = intent;
928
910
  }
929
- notifyTerminal(ctx.ui, `Goal prompt failed: ${formatError(error)}`, "error");
911
+ notifyWhenSessionAlive(ctx, `Goal prompt failed: ${formatError(error)}`, "error");
930
912
  return false;
931
913
  }
932
914
  }
@@ -1011,7 +993,7 @@ var GoalRuntime = class {
1011
993
  try {
1012
994
  this.dispatchDueGoalWait(ctx);
1013
995
  } catch (error) {
1014
- notifyTerminal(ctx.ui, `Goal wait deadline failed: ${formatError(error)}`, "error");
996
+ notifyWhenSessionAlive(ctx, `Goal wait deadline failed: ${formatError(error)}`, "error");
1015
997
  }
1016
998
  });
1017
999
  }
@@ -1042,10 +1024,7 @@ var GoalRuntime = class {
1042
1024
  }
1043
1025
  updateStatus(ctx, goal) {
1044
1026
  this.clearCompletionStatusTimer();
1045
- ctx.ui.setStatus(
1046
- STATUS_KEY,
1047
- formatStatus(goal, this.settings.continuationLimits.automaticTurns)
1048
- );
1027
+ ctx.ui.setStatus(STATUS_KEY, formatStatus(goal, this.settings.continuationLimits.automaticTurns));
1049
1028
  }
1050
1029
  stopActiveGoal(ctx, request) {
1051
1030
  const currentGoal = this.activeGoal;
@@ -1196,7 +1175,7 @@ var GoalRuntime = class {
1196
1175
  return true;
1197
1176
  } catch (error) {
1198
1177
  this.budgetWrapUp.delivered = false;
1199
- notifyTerminal(ctx.ui, `Goal budget wrap-up failed: ${formatError(error)}`, "error");
1178
+ notifyWhenSessionAlive(ctx, `Goal budget wrap-up failed: ${formatError(error)}`, "error");
1200
1179
  return false;
1201
1180
  }
1202
1181
  }
@@ -1499,9 +1478,7 @@ var GoalRuntime = class {
1499
1478
  );
1500
1479
  if (exactIndex >= 0) return this.pendingNonGoalInputs.splice(exactIndex, 1)[0];
1501
1480
  if (!allowDeliveryFallback) return void 0;
1502
- const fallbackSteerIndex = this.pendingNonGoalInputs.findIndex(
1503
- (pending) => pending.behavior === "steer"
1504
- );
1481
+ const fallbackSteerIndex = this.pendingNonGoalInputs.findIndex((pending) => pending.behavior === "steer");
1505
1482
  const fallbackIndex = fallbackSteerIndex >= 0 ? fallbackSteerIndex : this.pendingNonGoalInputs.findIndex((pending) => pending.behavior === "followUp");
1506
1483
  if (fallbackIndex < 0) return void 0;
1507
1484
  return this.pendingNonGoalInputs.splice(fallbackIndex, 1)[0];
@@ -1540,9 +1517,7 @@ var GoalRuntime = class {
1540
1517
  this.clearTerminalDetails();
1541
1518
  }
1542
1519
  this.pi.appendEntry(GOAL_STATE_ENTRY_TYPE2, serializeGoalState(goal));
1543
- this.publishGoalState(
1544
- buildGoalStateSnapshot(goal, this.terminalDetails?.summary, this.terminalDetails?.reason)
1545
- );
1520
+ this.publishGoalState(buildGoalStateSnapshot(goal, this.terminalDetails?.summary, this.terminalDetails?.reason));
1546
1521
  }
1547
1522
  clearPersistedGoal(cwd, clearedGoal, reason = "goal cleared") {
1548
1523
  this.pi.appendEntry(GOAL_STATE_ENTRY_TYPE2, serializeGoalState(void 0));
@@ -1740,7 +1715,6 @@ function formatStatus(goal, automaticTurnLimit = DEFAULT_GOAL_SETTINGS.continuat
1740
1715
  if (!goal) return void 0;
1741
1716
  if (goal.status === "complete") return "complete";
1742
1717
  const automatic = automaticTurnLimit === null ? "automatic Unlimited" : `automatic ${goal.automaticModelTurns}/${automaticTurnLimit}`;
1743
- if (goal.status === "queued") return `queued \xB7 ${automatic}`;
1744
1718
  if (goal.waiting) {
1745
1719
  return `waiting ${safeGoalMenuText(goal.waiting.reason)} \xB7 ${automatic}`;
1746
1720
  }
@@ -1833,23 +1807,29 @@ async function sendPrompt(pi, ctx, prompt, isCurrent) {
1833
1807
  return true;
1834
1808
  } catch (error) {
1835
1809
  if (!isCurrent || isCurrent()) {
1836
- notifyTerminal(ctx.ui, `Goal prompt failed: ${formatError(error)}`, "error");
1810
+ notifyWhenSessionAlive(ctx, `Goal prompt failed: ${formatError(error)}`, "error");
1837
1811
  }
1838
1812
  return false;
1839
1813
  }
1840
1814
  }
1841
- function goalCommandHint(goal, experimentalGoals = false) {
1842
- const queueCommands = experimentalGoals ? ", /goal add <objective>, /goal prioritize <objective>, /goal drop-last, /goal skip" : "";
1815
+ function notifyWhenSessionAlive(ctx, message, level) {
1816
+ try {
1817
+ notifyTerminal(ctx.ui, message, level);
1818
+ } catch (error) {
1819
+ if (!isStaleContextError(error)) throw error;
1820
+ }
1821
+ }
1822
+ function goalCommandHint(goal) {
1843
1823
  if (goal.waiting) {
1844
- return `/goal resume, /goal edit <objective>, /goal pause, /goal clear${queueCommands}`;
1824
+ return "/goal resume, /goal edit <objective>, /goal pause, /goal clear";
1845
1825
  }
1846
1826
  if (goal.status === "active") {
1847
- return `/goal edit <objective>, /goal pause, /goal clear${queueCommands}`;
1827
+ return "/goal edit <objective>, /goal pause, /goal clear";
1848
1828
  }
1849
1829
  if (isResumableGoalStatus(goal.status)) {
1850
- return `/goal edit <objective>, /goal resume, /goal clear${queueCommands}`;
1830
+ return "/goal edit <objective>, /goal resume, /goal clear";
1851
1831
  }
1852
- return `/goal edit <objective>, /goal clear${queueCommands}`;
1832
+ return "/goal edit <objective>, /goal clear";
1853
1833
  }
1854
1834
  function continuationMarker(goal) {
1855
1835
  return `${goal.id}:${goal.iteration}:${randomUUID()}`;
@@ -1900,4 +1880,4 @@ export {
1900
1880
  isContradictoryCompletionSummary,
1901
1881
  goalIdRejectionReason
1902
1882
  };
1903
- //# sourceMappingURL=chunk-QWIUWKRW.js.map
1883
+ //# sourceMappingURL=chunk-VJ5LR26S.js.map