@narumitw/pi-goal 0.54.15 → 0.54.17

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/dist/index.ts CHANGED
@@ -43,7 +43,7 @@ import {
43
43
  stoppedStatusLabel,
44
44
  transitionGoal,
45
45
  validateObjective
46
- } from "./chunks/chunk-SL6SIIVB.ts";
46
+ } from "./chunks/chunk-MHW7VEET.ts";
47
47
  import {
48
48
  DEFAULT_GOAL_SETTINGS,
49
49
  currentTokenTotal,
@@ -59,12 +59,12 @@ import {
59
59
  safeGoalMenuText,
60
60
  safeTerminalText,
61
61
  truncateNotification
62
- } from "./chunks/chunk-RNWM7MOG.ts";
62
+ } from "./chunks/chunk-ZLKWPYIF.ts";
63
63
 
64
64
  // src/command-registration.ts
65
65
  function registerGoalCommand(pi, runtime, commands, options = {}) {
66
- const loadGoalManager = cachedModuleLoader(options.loadGoalManager ?? (() => import("./chunks/menu-H43NPYIW.ts")));
67
- const loadGoalSettings = cachedModuleLoader(options.loadGoalSettings ?? (() => import("./chunks/settings-ui-T77VFPTK.ts")));
66
+ const loadGoalManager = cachedModuleLoader(options.loadGoalManager ?? (() => import("./chunks/menu-3AYWQ637.ts")));
67
+ const loadGoalSettings = cachedModuleLoader(options.loadGoalSettings ?? (() => import("./chunks/settings-ui-RVQTWGJ5.ts")));
68
68
  pi.registerCommand("goal", {
69
69
  description: "Run a goal to completion: /goal [--tokens 100k] <goal_to_complete>",
70
70
  getArgumentCompletions: (prefix) => completeGoalArguments(prefix),
@@ -584,8 +584,11 @@ var REMOVED_QUEUE_SETTING_WARNING = "Ordered goal queue has been removed. Use /g
584
584
  var REMOVED_PERSISTED_QUEUE_WARNING = "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.";
585
585
  function registerGoalLifecycle(pi, runtime, runController, options = {}) {
586
586
  let sessionActive = true;
587
+ let endedGoalRun;
587
588
  pi.on("session_start", async (_event, ctx) => {
588
589
  sessionActive = true;
590
+ runtime.pendingLengthRecovery = void 0;
591
+ endedGoalRun = void 0;
589
592
  runtime.bindWorkflowSession(ctx.sessionManager);
590
593
  runtime.replaceMenuSession();
591
594
  runtime.clearCompletionStatusTimer();
@@ -668,6 +671,8 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
668
671
  });
669
672
  pi.on("session_shutdown", (_event, ctx) => {
670
673
  sessionActive = false;
674
+ runtime.pendingLengthRecovery = void 0;
675
+ endedGoalRun = void 0;
671
676
  const shutdownSession = ctx.sessionManager;
672
677
  runController.unbindSession();
673
678
  runtime.closeMenuSession();
@@ -704,11 +709,17 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
704
709
  return;
705
710
  }
706
711
  if (!runtime.recordGoalUsage(runtime.activeGoal, ctx)) return;
712
+ if (event.willRetry === true) claimCompactionRecovery(runtime.activeGoal.id);
707
713
  runtime.cancelContinuationWork();
708
714
  runtime.persistGoal(runtime.activeGoal);
709
715
  runtime.updateStatus(ctx, runtime.activeGoal);
710
716
  if (runtime.limitActiveGoalForBudget(ctx, false)) return { cancel: true };
711
717
  });
718
+ pi.on("session_compact_failed", (event) => {
719
+ if (!sessionActive || !runtime.ownsWorkflow() || !runtime.activeGoal) return;
720
+ if (event.reason === "overflow") claimCompactionRecovery(runtime.activeGoal.id);
721
+ if (event.aborted && endedGoalRun?.goalId === runtime.activeGoal.id) endedGoalRun.aborted = true;
722
+ });
712
723
  pi.on("session_compact", async (event, ctx) => {
713
724
  if (!sessionActive) return;
714
725
  if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
@@ -733,7 +744,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
733
744
  }
734
745
  if (!usageRecorded) return;
735
746
  const wasPiRetry = runtime.isPiOwnedCompactionRetry(event, runtime.activeGoal.id);
736
- if (wasPiRetry) return;
747
+ if (wasPiRetry || runtime.hasActiveGoalRecovery()) return;
737
748
  runtime.clearGoalRecoveryForGoal(runtime.activeGoal.id);
738
749
  runtime.requestContinuation(runtime.activeGoal);
739
750
  runtime.scheduleContinuationDispatch(ctx, runtime.activeGoal.id);
@@ -750,6 +761,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
750
761
  if (event.streamingBehavior === "steer" || event.streamingBehavior === "followUp") {
751
762
  runtime.noteQueuedNonGoalInput(event.text, event.streamingBehavior);
752
763
  }
764
+ runtime.pendingLengthRecovery = void 0;
753
765
  runtime.clearGoalRecovery();
754
766
  return;
755
767
  }
@@ -762,6 +774,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
762
774
  if (event.streamingBehavior === "steer") {
763
775
  runtime.noteQueuedNonGoalInput(event.text, "steer");
764
776
  }
777
+ runtime.pendingLengthRecovery = void 0;
765
778
  runtime.clearGoalRecovery();
766
779
  runtime.clearBudgetWrapUp();
767
780
  runtime.clearStaleGoalToolCallBlock();
@@ -858,6 +871,8 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
858
871
  });
859
872
  pi.on("before_agent_start", (event, ctx) => {
860
873
  if (!sessionActive || event.messageType === "custom") return;
874
+ runtime.pendingLengthRecovery = void 0;
875
+ endedGoalRun = void 0;
861
876
  runtime.clearAgentRun();
862
877
  if (runtime.guardAbortGoalId) runtime.guardAbortGoalId = void 0;
863
878
  const goalPrompt = runtime.consumeOwnedGoalPrompt(event.prompt);
@@ -914,6 +929,8 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
914
929
  });
915
930
  pi.on("agent_start", (_event, _ctx) => {
916
931
  if (!sessionActive) return;
932
+ runtime.pendingLengthRecovery = void 0;
933
+ endedGoalRun = void 0;
917
934
  const activeGoal = runtime.activeGoal;
918
935
  if (activeGoal && runtime.guardAbortGoalId === activeGoal.id && activeGoal.status === "paused") {
919
936
  if (runtime.consumeQueuedNonGoalFollowUpForAgentStart()) {
@@ -934,6 +951,8 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
934
951
  });
935
952
  pi.on("agent_end", (event, ctx) => {
936
953
  if (!sessionActive) return;
954
+ runtime.pendingLengthRecovery = void 0;
955
+ endedGoalRun = void 0;
937
956
  const run = runtime.finishAgentRun();
938
957
  if (run.goalId === null) return;
939
958
  if (!runtime.canRecordGoalUsage() && !runtime.hasActiveBudgetWrapUp()) return;
@@ -950,6 +969,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
950
969
  return;
951
970
  }
952
971
  const goalId = runtime.activeGoal.id;
972
+ endedGoalRun = { goalId, aborted: false };
953
973
  const alreadyAwaitingContinuation = runtime.hasContinuationWorkForGoal(goalId);
954
974
  const finalAssistant = findFinalAssistantMessage(event.messages);
955
975
  if (!alreadyAwaitingContinuation) runtime.activeGoal = incrementGoal(runtime.activeGoal);
@@ -960,31 +980,31 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
960
980
  return;
961
981
  }
962
982
  if (finalAssistant?.stopReason === "error") {
963
- if (isRetryableGoalInterruption(finalAssistant)) {
964
- if (run.origin === "automatic" && runtime.enforceAutomaticTurnLimit(ctx, true)) return;
965
- if (runtime.limitActiveGoalForBudget(ctx, false)) return;
966
- if (!runtime.goalToolsAvailable()) {
967
- runtime.pauseGoalForUnavailableTools(ctx);
968
- return;
969
- }
970
- runtime.goalRecovery = {
971
- goalId,
972
- kind: isGoalContextOverflow(finalAssistant) ? "compaction_retry" : "provider_retry",
973
- automaticOwner: run.origin === "automatic",
974
- errorMessage: finalAssistant.errorMessage
975
- };
976
- runtime.cancelContinuationWork();
977
- runtime.persistGoal(runtime.activeGoal);
978
- runtime.updateStatus(ctx, runtime.activeGoal);
983
+ const nativeRecovery = isRetryableGoalInterruption(finalAssistant);
984
+ if (!nativeRecovery && isUsageLimitedGoalInterruption(finalAssistant)) {
985
+ runtime.clearGoalRecoveryForGoal(goalId);
986
+ stopGoalAfterAgentEnd(ctx, runtime.activeGoal, finalAssistant, "usage_limited");
979
987
  return;
980
988
  }
981
- runtime.clearGoalRecoveryForGoal(goalId);
982
- stopGoalAfterAgentEnd(
983
- ctx,
984
- runtime.activeGoal,
985
- finalAssistant,
986
- isUsageLimitedGoalInterruption(finalAssistant) ? "usage_limited" : "blocked"
987
- );
989
+ if (run.origin === "automatic" && (runtime.enforceAutomaticTurnLimit(ctx, true) || runtime.enforceNoProgressLimit(ctx, true))) {
990
+ return;
991
+ }
992
+ if (runtime.limitActiveGoalForBudget(ctx, false)) return;
993
+ if (!runtime.goalToolsAvailable()) {
994
+ runtime.pauseGoalForUnavailableTools(ctx);
995
+ return;
996
+ }
997
+ runtime.goalRecovery = {
998
+ goalId,
999
+ kind: isGoalContextOverflow(finalAssistant) ? "compaction_retry" : nativeRecovery ? "provider_retry" : "provider_error",
1000
+ automaticOwner: run.origin === "automatic",
1001
+ errorMessage: finalAssistant.errorMessage
1002
+ };
1003
+ runtime.cancelContinuationWork();
1004
+ runtime.persistGoal(runtime.activeGoal);
1005
+ if (runtime.activeGoal?.id === goalId && runtime.ownsWorkflow(runtime.activeGoal)) {
1006
+ runtime.updateStatus(ctx, runtime.activeGoal);
1007
+ }
988
1008
  return;
989
1009
  }
990
1010
  runtime.clearGoalRecoveryForGoal(goalId);
@@ -993,6 +1013,13 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
993
1013
  runtime.pauseGoalForUnavailableTools(ctx);
994
1014
  return;
995
1015
  }
1016
+ if (finalAssistant?.stopReason === "length") {
1017
+ runtime.pendingLengthRecovery = {
1018
+ goalId,
1019
+ kind: "compaction_retry",
1020
+ automaticOwner: run.origin === "automatic"
1021
+ };
1022
+ }
996
1023
  if (run.origin === "automatic" && runtime.recordAutomaticRunProgress(
997
1024
  ctx,
998
1025
  goalId,
@@ -1001,30 +1028,44 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
1001
1028
  )) {
1002
1029
  return;
1003
1030
  }
1004
- runtime.persistGoal(runtime.activeGoal);
1005
- runtime.updateStatus(ctx, runtime.activeGoal);
1006
1031
  const currentGoal = runtime.activeGoal;
1007
- if (!currentGoal || currentGoal.id !== goalId || currentGoal.status !== "active") return;
1008
- runtime.requestContinuation(currentGoal);
1032
+ if (!currentGoal || currentGoal.id !== goalId || !runtime.ownsWorkflow(currentGoal)) return;
1033
+ runtime.persistGoal(currentGoal);
1034
+ const persistedGoal = runtime.activeGoal;
1035
+ if (!persistedGoal || persistedGoal.id !== goalId || !runtime.ownsWorkflow(persistedGoal)) return;
1036
+ runtime.updateStatus(ctx, persistedGoal);
1037
+ runtime.requestContinuation(persistedGoal);
1009
1038
  });
1010
- pi.on("agent_settled", (_event, ctx) => {
1039
+ pi.on("agent_settled", (event, ctx) => {
1011
1040
  if (!sessionActive) return;
1041
+ runtime.pendingLengthRecovery = void 0;
1042
+ const settledRun = endedGoalRun;
1043
+ endedGoalRun = void 0;
1012
1044
  if (runtime.activeGoal?.status === "active" && !runtime.ownsWorkflow(runtime.activeGoal)) {
1013
1045
  runtime.cancelContinuationWork();
1014
1046
  runtime.clearGoalRecovery();
1015
1047
  runtime.clearSettledSafetyTracking();
1016
1048
  return;
1017
1049
  }
1018
- runtime.finalizeSettledRecovery(ctx);
1050
+ runtime.finalizeSettledRecovery(ctx, event.aborted === true || settledRun?.aborted === true, settledRun?.goalId);
1019
1051
  const resumedWait = runtime.dispatchDueGoalWait(ctx);
1020
1052
  if (!resumedWait) runtime.dispatchContinuationIfSettled(ctx);
1021
1053
  runtime.clearSettledSafetyTracking();
1022
1054
  });
1055
+ function claimCompactionRecovery(goalId) {
1056
+ const lengthRecovery = runtime.pendingLengthRecovery?.goalId === goalId ? runtime.pendingLengthRecovery : void 0;
1057
+ if (lengthRecovery) runtime.pendingLengthRecovery = void 0;
1058
+ const recovery = lengthRecovery ?? (runtime.goalRecovery?.goalId === goalId ? runtime.goalRecovery : void 0);
1059
+ if (!recovery) return;
1060
+ runtime.goalRecovery = { ...recovery, kind: "compaction_retry" };
1061
+ runtime.cancelContinuationWork();
1062
+ }
1023
1063
  function goalContractBoundaryResult(ctx, goal2) {
1024
1064
  const message = runtime.goalContextContractForPrompt(ctx, goal2);
1025
1065
  return message ? { message } : void 0;
1026
1066
  }
1027
1067
  function beginNonGoalFollowUp(ctx, resetSafetyEpoch) {
1068
+ runtime.pendingLengthRecovery = void 0;
1028
1069
  runtime.clearGoalRecovery();
1029
1070
  runtime.clearStaleGoalToolCallBlock();
1030
1071
  if (resetSafetyEpoch) runtime.clearBudgetWrapUp();
@@ -1045,17 +1086,9 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
1045
1086
  notifyTerminal(ctx.ui, `Goal paused after interruption${details}. Run /goal resume to continue.`, "warning");
1046
1087
  return;
1047
1088
  }
1048
- if (status === "usage_limited") {
1049
- notifyTerminal(
1050
- ctx.ui,
1051
- `Goal stopped after provider usage limit${details}. Run /goal resume when usage is available.`,
1052
- "warning"
1053
- );
1054
- return;
1055
- }
1056
1089
  notifyTerminal(
1057
1090
  ctx.ui,
1058
- `Goal blocked after agent error${details}. Resolve the blocker or run /goal resume to retry.`,
1091
+ `Goal stopped after provider usage limit${details}. Run /goal resume when usage is available.`,
1059
1092
  "warning"
1060
1093
  );
1061
1094
  }