@narumitw/pi-goal 0.52.2 → 0.53.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.
@@ -629,6 +629,8 @@ var GoalToolPolicy = class {
629
629
  this.reveal();
630
630
  return;
631
631
  }
632
+ if (this.hasHiddenTools()) this.restoreHidden();
633
+ this.unlock();
632
634
  this.assertAvailable();
633
635
  }
634
636
  prepareSessionStart(visibility, previous) {
@@ -700,6 +702,69 @@ var GoalToolPolicy = class {
700
702
  }
701
703
  };
702
704
 
705
+ // src/workflow-mutex.ts
706
+ var WORKFLOW_MUTEX_CHANNEL = "workflow:mutex:v1";
707
+ var AGENT_WORKFLOW_GROUP = "agent-workflow";
708
+ var WorkflowMutex = class {
709
+ session;
710
+ heldGroups = /* @__PURE__ */ new Map();
711
+ generation = 0;
712
+ pi;
713
+ constructor(pi) {
714
+ this.pi = pi;
715
+ pi.events.on(WORKFLOW_MUTEX_CHANNEL, (payload) => {
716
+ this.answer(payload);
717
+ });
718
+ }
719
+ bindSession(session) {
720
+ this.generation += 1;
721
+ this.heldGroups.clear();
722
+ this.session = session;
723
+ }
724
+ unbindSession(session) {
725
+ if (this.session !== session) return;
726
+ this.generation += 1;
727
+ this.heldGroups.clear();
728
+ this.session = void 0;
729
+ }
730
+ acquire(group = AGENT_WORKFLOW_GROUP) {
731
+ const session = this.session;
732
+ const generation = this.generation;
733
+ if (!session || this.heldGroups.has(group)) return void 0;
734
+ const attempt = { session, group, busy: false };
735
+ try {
736
+ this.pi.events.emit(WORKFLOW_MUTEX_CHANNEL, attempt);
737
+ } catch {
738
+ return void 0;
739
+ }
740
+ if (this.session !== session || this.generation !== generation || attempt.session !== session || attempt.group !== group || attempt.busy !== false || this.heldGroups.has(group)) {
741
+ return void 0;
742
+ }
743
+ const owner = Symbol(group);
744
+ this.heldGroups.set(group, owner);
745
+ return owner;
746
+ }
747
+ isOwner(owner, group = AGENT_WORKFLOW_GROUP) {
748
+ return owner !== void 0 && this.heldGroups.get(group) === owner;
749
+ }
750
+ release(owner, group = AGENT_WORKFLOW_GROUP) {
751
+ if (!this.isOwner(owner, group)) return;
752
+ this.heldGroups.delete(group);
753
+ }
754
+ answer(payload) {
755
+ try {
756
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return;
757
+ const attempt = payload;
758
+ if (attempt.session !== this.session) return;
759
+ if (attempt.group !== AGENT_WORKFLOW_GROUP) return;
760
+ if (typeof attempt.busy !== "boolean") return;
761
+ if (!this.heldGroups.has(attempt.group)) return;
762
+ attempt.busy = true;
763
+ } catch {
764
+ }
765
+ }
766
+ };
767
+
703
768
  // src/runtime.ts
704
769
  var STATUS_KEY = "goal";
705
770
  var GOAL_STATE_ENTRY_TYPE2 = "goal-state";
@@ -749,6 +814,9 @@ var GoalRuntime = class {
749
814
  guardAbortGoalId;
750
815
  staleGoalToolCallsBlocked = false;
751
816
  toolPolicy;
817
+ workflowMutex;
818
+ workflowOwner;
819
+ workflowSession;
752
820
  pendingGoalPromptMarkers = /* @__PURE__ */ new Map();
753
821
  claimedGoalPromptMarkers = /* @__PURE__ */ new Map();
754
822
  cancelledGoalPromptMarkers = /* @__PURE__ */ new Map();
@@ -760,8 +828,61 @@ var GoalRuntime = class {
760
828
  pi;
761
829
  constructor(pi) {
762
830
  this.pi = pi;
831
+ this.workflowMutex = new WorkflowMutex(pi);
763
832
  this.toolPolicy = new GoalToolPolicy(pi);
764
833
  }
834
+ bindWorkflowSession(session) {
835
+ this.workflowSession = session;
836
+ this.workflowOwner = void 0;
837
+ this.workflowMutex.bindSession(session);
838
+ }
839
+ unbindWorkflowSession(session) {
840
+ if (this.workflowSession !== session) return;
841
+ this.workflowMutex.unbindSession(session);
842
+ this.workflowOwner = void 0;
843
+ this.workflowSession = void 0;
844
+ }
845
+ acquireWorkflow(session) {
846
+ if (session && typeof session === "object" && this.workflowSession !== session) {
847
+ this.bindWorkflowSession(session);
848
+ }
849
+ if (this.workflowMutex.isOwner(this.workflowOwner)) return true;
850
+ const owner = this.workflowMutex.acquire();
851
+ if (!owner) return false;
852
+ this.workflowOwner = owner;
853
+ return true;
854
+ }
855
+ ownsWorkflow(goal = this.activeGoal) {
856
+ return goal?.status === "active" && this.workflowMutex.isOwner(this.workflowOwner);
857
+ }
858
+ releaseWorkflow() {
859
+ const owner = this.workflowOwner;
860
+ this.workflowMutex.release(owner);
861
+ if (!this.workflowMutex.isOwner(owner)) this.workflowOwner = void 0;
862
+ }
863
+ beginTemporaryWorkflowAccess(session) {
864
+ if (session && typeof session === "object" && this.workflowSession !== session) {
865
+ this.bindWorkflowSession(session);
866
+ }
867
+ const alreadyOwned = this.workflowMutex.isOwner(this.workflowOwner);
868
+ if (!alreadyOwned && !this.acquireWorkflow()) return void 0;
869
+ let released = false;
870
+ return () => {
871
+ if (released) return;
872
+ released = true;
873
+ if (!alreadyOwned) this.releaseWorkflow();
874
+ };
875
+ }
876
+ withTemporaryWorkflowAccess(action, session) {
877
+ const release = this.beginTemporaryWorkflowAccess(session);
878
+ if (!release) return false;
879
+ try {
880
+ action();
881
+ return true;
882
+ } finally {
883
+ release();
884
+ }
885
+ }
765
886
  hasLegacyQueueInterface() {
766
887
  return this.legacyExperimentalGoalsSetting || this.legacyQueueState !== void 0;
767
888
  }
@@ -832,6 +953,7 @@ var GoalRuntime = class {
832
953
  return true;
833
954
  }
834
955
  requestContinuation(goal) {
956
+ if (!this.ownsWorkflow(goal)) return false;
835
957
  if (goal.waiting || this.hasContinuationWorkForGoal(goal.id)) return false;
836
958
  const marker = continuationMarker(goal);
837
959
  this.continuationIntent = {
@@ -844,6 +966,10 @@ var GoalRuntime = class {
844
966
  }
845
967
  dispatchContinuationIfSettled(ctx) {
846
968
  const intent = this.continuationIntent;
969
+ if (!this.ownsWorkflow()) {
970
+ this.cancelContinuationWork();
971
+ return false;
972
+ }
847
973
  if (!intent) return false;
848
974
  if (this.activeGoal?.status === "active" && !this.toolPolicy.toolsAvailable()) {
849
975
  this.pauseGoalForUnavailableTools(ctx);
@@ -879,7 +1005,7 @@ var GoalRuntime = class {
879
1005
  }
880
1006
  enterGoalWait(ctx, goalId, waiting) {
881
1007
  const goal = this.activeGoal;
882
- if (!goal || goal.id !== goalId || goal.status !== "active") return void 0;
1008
+ if (!goal || goal.id !== goalId || !this.ownsWorkflow(goal)) return void 0;
883
1009
  this.recordGoalUsage(goal, ctx, false);
884
1010
  this.cancelContinuationWork();
885
1011
  this.clearGoalRecoveryForGoal(goal.id);
@@ -897,7 +1023,7 @@ var GoalRuntime = class {
897
1023
  }
898
1024
  clearGoalWait(ctx, goalId) {
899
1025
  const goal = this.activeGoal;
900
- if (!goal || goal.id !== goalId || goal.status !== "active" || !goal.waiting) return false;
1026
+ if (!goal || goal.id !== goalId || !this.ownsWorkflow(goal) || !goal.waiting) return false;
901
1027
  this.clearGoalWaitTimer();
902
1028
  const { waiting: _waiting, ...nextGoal } = goal;
903
1029
  const now = Date.now();
@@ -911,6 +1037,7 @@ var GoalRuntime = class {
911
1037
  restoreGoalWaitTimer(ctx) {
912
1038
  this.clearGoalWaitTimer();
913
1039
  const goal = this.activeGoal;
1040
+ if (!this.ownsWorkflow(goal)) return false;
914
1041
  const resumeAt = goal?.status === "active" ? goal.waiting?.resumeAt : void 0;
915
1042
  if (!goal || resumeAt === void 0) return false;
916
1043
  this.scheduleGoalWaitTimer(ctx, goal.id, resumeAt);
@@ -918,6 +1045,7 @@ var GoalRuntime = class {
918
1045
  }
919
1046
  dispatchDueGoalWait(ctx) {
920
1047
  const goal = this.activeGoal;
1048
+ if (!this.ownsWorkflow(goal)) return false;
921
1049
  const waiting = goal?.status === "active" ? goal.waiting : void 0;
922
1050
  const resumeAt = waiting?.resumeAt;
923
1051
  if (!goal || !waiting || resumeAt === void 0) return false;
@@ -947,7 +1075,9 @@ var GoalRuntime = class {
947
1075
  scheduleGoalWaitTimer(ctx, goalId, wakeAt) {
948
1076
  const generation = this.menuGeneration;
949
1077
  this.goalWaitTimer.schedule(wakeAt, () => {
950
- if (generation !== this.menuGeneration || this.activeGoal?.id !== goalId) return;
1078
+ if (generation !== this.menuGeneration || this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) {
1079
+ return;
1080
+ }
951
1081
  try {
952
1082
  this.dispatchDueGoalWait(ctx);
953
1083
  } catch (error) {
@@ -1008,6 +1138,8 @@ var GoalRuntime = class {
1008
1138
  this.cancelContinuationWork();
1009
1139
  this.clearGoalRecoveryForGoal(goal.id);
1010
1140
  this.clearBudgetWrapUp();
1141
+ this.blockStaleGoalToolCalls();
1142
+ abortCurrentTurn(ctx);
1011
1143
  status = "budget_limited";
1012
1144
  terminalReason = request.reason;
1013
1145
  break;
@@ -1075,6 +1207,7 @@ var GoalRuntime = class {
1075
1207
  this.persistGoal(stoppedGoal);
1076
1208
  if (this.activeGoal?.id === stoppedGoal.id && this.activeGoal.status === stoppedGoal.status) {
1077
1209
  this.updateStatus(ctx, stoppedGoal);
1210
+ this.releaseWorkflow();
1078
1211
  }
1079
1212
  return stoppedGoal;
1080
1213
  }
@@ -1113,6 +1246,7 @@ var GoalRuntime = class {
1113
1246
  return this.isActiveBudgetWrapUpMessage(message);
1114
1247
  }
1115
1248
  queueBudgetWrapUp(ctx, goal) {
1249
+ if (!this.ownsWorkflow(goal)) return false;
1116
1250
  if (!this.budgetWrapUp || this.budgetWrapUp.goalId !== goal.id) {
1117
1251
  this.budgetWrapUp = { goalId: goal.id, delivered: false };
1118
1252
  }
@@ -1268,9 +1402,10 @@ var GoalRuntime = class {
1268
1402
  this.pendingNonGoalInputs = [];
1269
1403
  }
1270
1404
  async sendOwnedGoalPrompt(ctx, goalId, prompt, resetSafetyEpoch = true, isCurrent) {
1405
+ if (this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) return false;
1271
1406
  const pending = this.rememberPendingGoalPrompt(goalId, prompt, resetSafetyEpoch);
1272
1407
  const sent = await sendPrompt(this.pi, ctx, pending.prompt, isCurrent);
1273
- if (!sent || isCurrent && !isCurrent()) {
1408
+ if (!sent || isCurrent && !isCurrent() || this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) {
1274
1409
  this.pendingGoalPromptMarkers.delete(pending.marker);
1275
1410
  return false;
1276
1411
  }
@@ -1289,7 +1424,7 @@ var GoalRuntime = class {
1289
1424
  const generation = this.menuGeneration;
1290
1425
  this.continuationDispatchTimer = setTimeout(() => {
1291
1426
  this.continuationDispatchTimer = void 0;
1292
- if (generation !== this.menuGeneration || this.activeGoal?.id !== goalId || this.activeGoal.status !== "active") {
1427
+ if (generation !== this.menuGeneration || this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) {
1293
1428
  return;
1294
1429
  }
1295
1430
  this.dispatchContinuationIfSettled(ctx);
@@ -1455,7 +1590,7 @@ var GoalRuntime = class {
1455
1590
  this.clearTerminalDetails();
1456
1591
  clearLegacyPersistedGoal(cwd);
1457
1592
  }
1458
- clearActiveGoal(ctx, reason = "goal cleared") {
1593
+ clearActiveGoal(ctx, reason = "goal cleared", releaseWorkflow = true) {
1459
1594
  const clearedGoal = this.activeGoal;
1460
1595
  this.clearGoalWaitTimer();
1461
1596
  this.cancelContinuationWork();
@@ -1466,6 +1601,7 @@ var GoalRuntime = class {
1466
1601
  this.legacyQueueState = void 0;
1467
1602
  this.clearPersistedGoal(ctx.cwd, clearedGoal, reason);
1468
1603
  ctx.ui.setStatus(STATUS_KEY, void 0);
1604
+ if (releaseWorkflow) this.releaseWorkflow();
1469
1605
  }
1470
1606
  snapshotSettingsApplicationState() {
1471
1607
  return {
@@ -1485,6 +1621,9 @@ var GoalRuntime = class {
1485
1621
  };
1486
1622
  }
1487
1623
  restoreSettingsApplicationState(snapshot) {
1624
+ if (snapshot.activeGoal?.status === "active" && !this.acquireWorkflow()) {
1625
+ throw new Error("another workflow became active before Goal settings could roll back");
1626
+ }
1488
1627
  this.settings = structuredClone(snapshot.settings);
1489
1628
  this.activeGoal = snapshot.activeGoal ? structuredClone(snapshot.activeGoal) : void 0;
1490
1629
  this.legacyQueueState = snapshot.legacyQueueState ? structuredClone(snapshot.legacyQueueState) : void 0;
@@ -1684,10 +1823,10 @@ function abortCurrentTurn(ctx) {
1684
1823
  }
1685
1824
  }
1686
1825
  function blocksStaleGoalToolCalls(status) {
1687
- return status === "paused" || status === "blocked" || status === "usage_limited";
1826
+ return status === "paused" || status === "blocked" || status === "usage_limited" || status === "budget_limited";
1688
1827
  }
1689
1828
  function isResumableGoalStatus(status) {
1690
- return blocksStaleGoalToolCalls(status) || status === "budget_limited";
1829
+ return blocksStaleGoalToolCalls(status);
1691
1830
  }
1692
1831
  function stoppedStatusLabel2(status) {
1693
1832
  if (status === "usage_limited") return "usage-limited";
@@ -1779,4 +1918,4 @@ export {
1779
1918
  isContradictoryCompletionSummary,
1780
1919
  goalIdRejectionReason
1781
1920
  };
1782
- //# sourceMappingURL=chunk-ODVXYZXS.ts.map
1921
+ //# sourceMappingURL=chunk-W7AFYZ6J.ts.map