@narumitw/pi-plan-mode 0.51.1 → 0.52.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/README.md +30 -0
- package/dist/index.ts +275 -75
- package/dist/index.ts.map +3 -3
- package/package.json +1 -1
- package/src/plan-mode.ts +233 -70
- package/src/workflow-mutex.ts +87 -0
package/src/plan-mode.ts
CHANGED
|
@@ -72,6 +72,7 @@ import {
|
|
|
72
72
|
snapshotPlanModeToolNames,
|
|
73
73
|
toolPolicyLabel,
|
|
74
74
|
} from "./tool-selection.js";
|
|
75
|
+
import { WorkflowMutex, type WorkflowMutexOwner } from "./workflow-mutex.js";
|
|
75
76
|
|
|
76
77
|
const STATE_ENTRY_TYPE = "plan-mode-state";
|
|
77
78
|
const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
|
|
@@ -89,7 +90,13 @@ interface PlanModeDependencies {
|
|
|
89
90
|
settingsPath?: string;
|
|
90
91
|
loadInteractiveUi?(): Promise<InteractiveUi>;
|
|
91
92
|
}
|
|
93
|
+
|
|
94
|
+
// Keep session state, persistence, tool, thinking, and mutex commits in this one closure so an
|
|
95
|
+
// activation path cannot bypass the same atomic transition by crossing module-owned state.
|
|
92
96
|
export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDependencies = {}) {
|
|
97
|
+
const workflowMutex = new WorkflowMutex(pi);
|
|
98
|
+
let workflowOwner: WorkflowMutexOwner | undefined;
|
|
99
|
+
let currentSession: object | undefined;
|
|
93
100
|
let interactiveUiPromise: Promise<InteractiveUi> | undefined;
|
|
94
101
|
const loadInteractiveUi = () => {
|
|
95
102
|
if (dependencies.loadInteractiveUi) return dependencies.loadInteractiveUi();
|
|
@@ -165,7 +172,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
165
172
|
],
|
|
166
173
|
parameters: PLAN_MODE_QUESTION_PARAMS,
|
|
167
174
|
async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
|
|
168
|
-
if (!state.enabled) {
|
|
175
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
|
|
169
176
|
return planModeQuestionCancelled(
|
|
170
177
|
[],
|
|
171
178
|
"plan_mode_inactive",
|
|
@@ -188,9 +195,12 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
188
195
|
|
|
189
196
|
const sessionGeneration = menuGeneration;
|
|
190
197
|
const questionWorkflowGeneration = workflowGeneration;
|
|
198
|
+
const questionOwner = workflowOwner;
|
|
191
199
|
return answerPlanModeQuestions(parsed.questions, ctx, {
|
|
192
200
|
isCurrent: () =>
|
|
193
|
-
sessionGeneration === menuGeneration &&
|
|
201
|
+
sessionGeneration === menuGeneration &&
|
|
202
|
+
questionWorkflowGeneration === workflowGeneration &&
|
|
203
|
+
workflowMutex.isOwner(questionOwner),
|
|
194
204
|
isEnabled: () => state.enabled,
|
|
195
205
|
});
|
|
196
206
|
},
|
|
@@ -208,7 +218,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
208
218
|
parameters: PLAN_MODE_COMPLETE_PARAMS,
|
|
209
219
|
renderResult: renderPlanModeCompletion,
|
|
210
220
|
async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
|
|
211
|
-
if (!state.enabled) {
|
|
221
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
|
|
212
222
|
throw new Error("plan_mode_complete is only available while Plan mode is active");
|
|
213
223
|
}
|
|
214
224
|
const parsed = normalizePlanModeCompletion(params);
|
|
@@ -233,8 +243,12 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
233
243
|
ctx.ui.notify("Plan mode is already active.", "info");
|
|
234
244
|
return;
|
|
235
245
|
}
|
|
236
|
-
enterPlanMode(ctx)
|
|
237
|
-
|
|
246
|
+
if (enterPlanMode(ctx)) {
|
|
247
|
+
ctx.ui.notify(
|
|
248
|
+
"Plan mode enabled. I will explore and plan, but not modify files.",
|
|
249
|
+
"info",
|
|
250
|
+
);
|
|
251
|
+
}
|
|
238
252
|
return;
|
|
239
253
|
}
|
|
240
254
|
if (command === "show") {
|
|
@@ -405,37 +419,39 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
405
419
|
|
|
406
420
|
pi.on("session_start", async (event, ctx) => {
|
|
407
421
|
const generation = ++menuGeneration;
|
|
422
|
+
currentSession = ctx.sessionManager;
|
|
423
|
+
workflowOwner = undefined;
|
|
424
|
+
workflowMutex.bindSession(ctx.sessionManager);
|
|
408
425
|
refreshStateBeforeFirstAgentStart = event.reason === "new";
|
|
409
426
|
menuController.abort(new DOMException("Plan-mode session replaced", "AbortError"));
|
|
410
427
|
menuController = new AbortController();
|
|
411
428
|
readyPresentationIntent = undefined;
|
|
412
429
|
latestCommandContext = undefined;
|
|
430
|
+
previousTools = undefined;
|
|
413
431
|
implementationRetention.reset();
|
|
414
432
|
settings = { thinkingLevel: "inherit" };
|
|
415
|
-
|
|
416
|
-
|
|
433
|
+
const restoredState = readRestoredState(ctx);
|
|
434
|
+
state = { enabled: false, awaitingAction: false };
|
|
417
435
|
await applyPlanModeSettings(generation, ctx, true);
|
|
418
436
|
if (generation !== menuGeneration || menuController.signal.aborted) return;
|
|
419
437
|
startPlanModeSettingsWatch(generation);
|
|
420
|
-
const persistFlagActivation = pi.getFlag("plan") === true && !
|
|
421
|
-
|
|
422
|
-
|
|
438
|
+
const persistFlagActivation = pi.getFlag("plan") === true && !restoredState.enabled;
|
|
439
|
+
const candidate = persistFlagActivation
|
|
440
|
+
? restoredState.savedPlan
|
|
423
441
|
? {
|
|
424
|
-
...
|
|
442
|
+
...restoredState,
|
|
425
443
|
enabled: true,
|
|
426
|
-
latestPlan:
|
|
427
|
-
latestPlanSource:
|
|
444
|
+
latestPlan: restoredState.savedPlan.plan,
|
|
445
|
+
latestPlanSource: restoredState.savedPlan.source,
|
|
428
446
|
awaitingAction: true,
|
|
429
447
|
savedPlan: undefined,
|
|
430
448
|
activeImplementation: undefined,
|
|
431
449
|
}
|
|
432
|
-
: { ...
|
|
433
|
-
|
|
434
|
-
if (
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
} else deactivatePlanModeQuestionTool();
|
|
438
|
-
if (persistFlagActivation) persistState();
|
|
450
|
+
: { ...restoredState, enabled: true, activeImplementation: undefined }
|
|
451
|
+
: restoredState;
|
|
452
|
+
if (!installRestoredState(candidate, ctx)) return;
|
|
453
|
+
implementationRetention.restore(state.activeImplementation);
|
|
454
|
+
if (persistFlagActivation && state.enabled) persistState();
|
|
439
455
|
updateUi(ctx);
|
|
440
456
|
});
|
|
441
457
|
|
|
@@ -453,6 +469,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
453
469
|
});
|
|
454
470
|
|
|
455
471
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
472
|
+
const shutdownSession = ctx.sessionManager;
|
|
456
473
|
menuGeneration += 1;
|
|
457
474
|
menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
|
|
458
475
|
readyPresentationIntent = undefined;
|
|
@@ -460,6 +477,10 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
460
477
|
refreshStateBeforeFirstAgentStart = false;
|
|
461
478
|
implementationRetention.reset();
|
|
462
479
|
await awaitPlanModeSettingsWrites(dependencies.settingsPath);
|
|
480
|
+
if (currentSession !== undefined && currentSession !== shutdownSession) {
|
|
481
|
+
workflowMutex.unbindSession(shutdownSession);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
463
484
|
captureManualThinkingLevel();
|
|
464
485
|
persistState();
|
|
465
486
|
if (state.enabled) {
|
|
@@ -468,10 +489,13 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
468
489
|
}
|
|
469
490
|
stopPlanModeSettingsWatch();
|
|
470
491
|
clearUi(ctx);
|
|
492
|
+
releaseWorkflowOwner();
|
|
493
|
+
workflowMutex.unbindSession(ctx.sessionManager);
|
|
494
|
+
if (currentSession === ctx.sessionManager) currentSession = undefined;
|
|
471
495
|
});
|
|
472
496
|
|
|
473
497
|
pi.on("tool_call", async (event) => {
|
|
474
|
-
if (!state.enabled) return;
|
|
498
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
475
499
|
if (event.toolName === "update_plan") {
|
|
476
500
|
return {
|
|
477
501
|
block: true,
|
|
@@ -515,16 +539,12 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
515
539
|
pi.on("before_agent_start", (event, ctx) => {
|
|
516
540
|
if (refreshStateBeforeFirstAgentStart) {
|
|
517
541
|
refreshStateBeforeFirstAgentStart = false;
|
|
518
|
-
restoreState(ctx);
|
|
519
542
|
implementationRetention.reset();
|
|
543
|
+
if (!installRestoredState(readRestoredState(ctx), ctx)) return;
|
|
520
544
|
implementationRetention.restore(state.activeImplementation);
|
|
521
|
-
if (state.enabled) {
|
|
522
|
-
activatePlanModeTools();
|
|
523
|
-
applyPlanThinkingLevel();
|
|
524
|
-
} else deactivatePlanModeQuestionTool();
|
|
525
545
|
updateUi(ctx);
|
|
526
546
|
}
|
|
527
|
-
if (!state.enabled) return;
|
|
547
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
528
548
|
if (state.latestPlan || state.awaitingAction) {
|
|
529
549
|
readyPresentationIntent = undefined;
|
|
530
550
|
state = {
|
|
@@ -543,7 +563,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
543
563
|
});
|
|
544
564
|
|
|
545
565
|
pi.on("agent_end", async (event, ctx) => {
|
|
546
|
-
if (!state.enabled) return;
|
|
566
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
547
567
|
|
|
548
568
|
const text = latestAssistantText(event.messages);
|
|
549
569
|
const parsedPlan = parseProposedPlan(text);
|
|
@@ -588,37 +608,53 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
588
608
|
}
|
|
589
609
|
});
|
|
590
610
|
|
|
591
|
-
function enterPlanMode(
|
|
611
|
+
function enterPlanMode(
|
|
612
|
+
ctx: ExtensionContext,
|
|
613
|
+
candidate: Pick<PlanModeState, "selectedToolNames" | "selectedToolKeys"> = state,
|
|
614
|
+
) {
|
|
615
|
+
bindWorkflowSessionIfNeeded(ctx);
|
|
616
|
+
if (state.enabled) return workflowMutex.isOwner(workflowOwner);
|
|
617
|
+
const owner = workflowMutex.acquire();
|
|
618
|
+
if (!owner) return reportWorkflowBusy(ctx);
|
|
619
|
+
workflowOwner = owner;
|
|
620
|
+
|
|
621
|
+
const previousState = state;
|
|
622
|
+
const previousToolSnapshot = previousTools;
|
|
592
623
|
workflowGeneration += 1;
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
624
|
+
try {
|
|
625
|
+
previousTools = withoutRequiredPlanModeTools(safeGetActiveTools());
|
|
626
|
+
state = {
|
|
627
|
+
...state,
|
|
628
|
+
enabled: true,
|
|
629
|
+
awaitingAction: false,
|
|
630
|
+
savedPlan: undefined,
|
|
631
|
+
activeImplementation: undefined,
|
|
632
|
+
selectedToolNames: candidate.selectedToolNames,
|
|
633
|
+
selectedToolKeys: candidate.selectedToolKeys,
|
|
634
|
+
};
|
|
635
|
+
activatePlanModeTools();
|
|
636
|
+
applyPlanThinkingLevel();
|
|
637
|
+
persistState();
|
|
638
|
+
updateUi(ctx);
|
|
639
|
+
return true;
|
|
640
|
+
} catch (error: unknown) {
|
|
641
|
+
rollbackNewActivation(previousState, previousToolSnapshot, ctx);
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
605
644
|
}
|
|
606
645
|
|
|
607
646
|
function enterPlanModeWithPrompt(prompt: string, ctx: ExtensionContext) {
|
|
608
647
|
const previousState = state;
|
|
648
|
+
const previousOwner = workflowOwner;
|
|
649
|
+
const previousToolSnapshot = previousTools;
|
|
609
650
|
const wasEnabled = state.enabled;
|
|
610
|
-
enterPlanMode(ctx);
|
|
651
|
+
if (!enterPlanMode(ctx)) return;
|
|
611
652
|
if (!wasEnabled) {
|
|
612
653
|
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
613
654
|
}
|
|
614
655
|
if (sendPlanModeUserMessage(prompt, ctx)) return;
|
|
615
|
-
if (
|
|
616
|
-
|
|
617
|
-
restoreThinkingLevel();
|
|
618
|
-
}
|
|
619
|
-
state = previousState;
|
|
620
|
-
persistState();
|
|
621
|
-
updateUi(ctx);
|
|
656
|
+
if (wasEnabled) return;
|
|
657
|
+
rollbackNewActivation(previousState, previousToolSnapshot, ctx, previousOwner);
|
|
622
658
|
}
|
|
623
659
|
|
|
624
660
|
function exitPlanMode(ctx: ExtensionContext) {
|
|
@@ -642,6 +678,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
642
678
|
}
|
|
643
679
|
persistState();
|
|
644
680
|
updateUi(ctx);
|
|
681
|
+
if (wasEnabled) releaseWorkflowOwner();
|
|
645
682
|
}
|
|
646
683
|
|
|
647
684
|
function sendPlanModeUserMessage(message: string, ctx: ExtensionContext) {
|
|
@@ -690,6 +727,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
690
727
|
function completedPlanIsCurrent(intent: ReadyPresentationIntent) {
|
|
691
728
|
return (
|
|
692
729
|
state.enabled &&
|
|
730
|
+
workflowMutex.isOwner(workflowOwner) &&
|
|
693
731
|
state.awaitingAction &&
|
|
694
732
|
state.latestPlan === intent.plan &&
|
|
695
733
|
state.latestPlanSource === intent.source
|
|
@@ -707,8 +745,9 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
707
745
|
return;
|
|
708
746
|
}
|
|
709
747
|
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined)) return;
|
|
710
|
-
enterPlanMode(ctx)
|
|
711
|
-
|
|
748
|
+
if (enterPlanMode(ctx)) {
|
|
749
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
750
|
+
}
|
|
712
751
|
}
|
|
713
752
|
|
|
714
753
|
function planModeDisableNotification() {
|
|
@@ -759,6 +798,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
759
798
|
state = { ...state, manualThinkingLevel: undefined };
|
|
760
799
|
persistState();
|
|
761
800
|
updateUi(ctx);
|
|
801
|
+
releaseWorkflowOwner();
|
|
762
802
|
ctx.ui.notify("Plan saved for later. Plan mode disabled.", "info");
|
|
763
803
|
}
|
|
764
804
|
|
|
@@ -794,6 +834,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
794
834
|
|
|
795
835
|
workflowGeneration += 1;
|
|
796
836
|
const previousState = state;
|
|
837
|
+
const previousIntent = readyPresentationIntent;
|
|
838
|
+
const previousToolSnapshot = previousTools;
|
|
797
839
|
const wasEnabled = state.enabled;
|
|
798
840
|
readyPresentationIntent = undefined;
|
|
799
841
|
state = {
|
|
@@ -822,16 +864,18 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
822
864
|
|
|
823
865
|
const sent = sendPlanModeUserMessage(formatImplementationHandoff(plan), ctx);
|
|
824
866
|
if (!sent) {
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
867
|
+
state = previousState;
|
|
868
|
+
readyPresentationIntent = previousIntent;
|
|
869
|
+
if (wasEnabled) {
|
|
870
|
+
previousTools = previousToolSnapshot;
|
|
871
|
+
applyPlanModeTools();
|
|
830
872
|
applyPlanThinkingLevel();
|
|
831
873
|
}
|
|
832
874
|
persistState();
|
|
833
875
|
updateUi(ctx);
|
|
876
|
+
return;
|
|
834
877
|
}
|
|
878
|
+
if (wasEnabled) releaseWorkflowOwner();
|
|
835
879
|
}
|
|
836
880
|
|
|
837
881
|
function clearActiveImplementation(id: string, ctx: ExtensionContext) {
|
|
@@ -870,18 +914,19 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
870
914
|
...lifecycle,
|
|
871
915
|
start: (signal) => {
|
|
872
916
|
if (signal.aborted || !lifecycle.isCurrent()) return;
|
|
873
|
-
enterPlanMode(ctx)
|
|
874
|
-
|
|
917
|
+
if (enterPlanMode(ctx)) {
|
|
918
|
+
ctx.ui.notify(
|
|
919
|
+
"Plan mode enabled. I will explore and plan, but not modify files.",
|
|
920
|
+
"info",
|
|
921
|
+
);
|
|
922
|
+
}
|
|
875
923
|
},
|
|
876
924
|
startWithTools: (names, signal) => {
|
|
877
925
|
if (signal.aborted || !lifecycle.isCurrent()) return;
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
};
|
|
883
|
-
enterPlanMode(ctx);
|
|
884
|
-
ctx.ui.notify("Plan mode enabled with the selected tools.", "info");
|
|
926
|
+
const selectedToolNames = filterAvailableSelectedToolNames(names, tools);
|
|
927
|
+
if (enterPlanMode(ctx, { selectedToolNames, selectedToolKeys: undefined })) {
|
|
928
|
+
ctx.ui.notify("Plan mode enabled with the selected tools.", "info");
|
|
929
|
+
}
|
|
885
930
|
},
|
|
886
931
|
settings: (signal) => showSettings(ctx, signal, lifecycle.isCurrent),
|
|
887
932
|
});
|
|
@@ -905,8 +950,12 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
905
950
|
exportPlan: (path, signal) => planExports.export(path, ctx, signal, lifecycle.isCurrent),
|
|
906
951
|
settings: (signal) => showSettings(ctx, signal, lifecycle.isCurrent),
|
|
907
952
|
startNew: () => {
|
|
908
|
-
enterPlanMode(ctx)
|
|
909
|
-
|
|
953
|
+
if (enterPlanMode(ctx)) {
|
|
954
|
+
ctx.ui.notify(
|
|
955
|
+
"Plan mode enabled. I will explore and plan, but not modify files.",
|
|
956
|
+
"info",
|
|
957
|
+
);
|
|
958
|
+
}
|
|
910
959
|
},
|
|
911
960
|
clear: () => {
|
|
912
961
|
exitPlanMode(ctx);
|
|
@@ -943,13 +992,15 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
943
992
|
function captureMenuLifecycle() {
|
|
944
993
|
const sessionGeneration = menuGeneration;
|
|
945
994
|
const planWorkflowGeneration = workflowGeneration;
|
|
995
|
+
const owner = workflowOwner;
|
|
946
996
|
const controller = menuController;
|
|
947
997
|
return {
|
|
948
998
|
signal: controller.signal,
|
|
949
999
|
isCurrent: () =>
|
|
950
1000
|
sessionGeneration === menuGeneration &&
|
|
951
1001
|
planWorkflowGeneration === workflowGeneration &&
|
|
952
|
-
!controller.signal.aborted
|
|
1002
|
+
!controller.signal.aborted &&
|
|
1003
|
+
(!state.enabled || workflowMutex.isOwner(owner)),
|
|
953
1004
|
};
|
|
954
1005
|
}
|
|
955
1006
|
|
|
@@ -1075,8 +1126,120 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1075
1126
|
}
|
|
1076
1127
|
}
|
|
1077
1128
|
|
|
1078
|
-
function
|
|
1079
|
-
|
|
1129
|
+
function readRestoredState(ctx: ExtensionContext) {
|
|
1130
|
+
return restorePlanModeState(ctx.sessionManager.getBranch(), STATE_ENTRY_TYPE);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
function installRestoredState(candidate: PlanModeState, ctx: ExtensionContext) {
|
|
1134
|
+
const previousState = state;
|
|
1135
|
+
const previousToolSnapshot = previousTools;
|
|
1136
|
+
const previousOwner = workflowOwner;
|
|
1137
|
+
const wasEnabled = state.enabled;
|
|
1138
|
+
if (candidate.enabled && !workflowMutex.isOwner(workflowOwner)) {
|
|
1139
|
+
const owner = workflowMutex.acquire();
|
|
1140
|
+
if (!owner) {
|
|
1141
|
+
state = { enabled: false, awaitingAction: false };
|
|
1142
|
+
previousTools = undefined;
|
|
1143
|
+
reportRestoredWorkflowBusy(ctx);
|
|
1144
|
+
return false;
|
|
1145
|
+
}
|
|
1146
|
+
workflowOwner = owner;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
try {
|
|
1150
|
+
if (wasEnabled && !candidate.enabled) {
|
|
1151
|
+
readyPresentationIntent = undefined;
|
|
1152
|
+
restoreTools();
|
|
1153
|
+
restoreThinkingLevel();
|
|
1154
|
+
}
|
|
1155
|
+
state = candidate;
|
|
1156
|
+
if (state.enabled) {
|
|
1157
|
+
if (!wasEnabled) previousTools = withoutRequiredPlanModeTools(safeGetActiveTools());
|
|
1158
|
+
activatePlanModeTools();
|
|
1159
|
+
applyPlanThinkingLevel();
|
|
1160
|
+
} else {
|
|
1161
|
+
deactivatePlanModeQuestionTool();
|
|
1162
|
+
if (wasEnabled) releaseWorkflowOwner();
|
|
1163
|
+
}
|
|
1164
|
+
return true;
|
|
1165
|
+
} catch (error: unknown) {
|
|
1166
|
+
try {
|
|
1167
|
+
if (!wasEnabled && state.enabled) {
|
|
1168
|
+
try {
|
|
1169
|
+
restoreTools();
|
|
1170
|
+
} finally {
|
|
1171
|
+
restoreThinkingLevel();
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
} finally {
|
|
1175
|
+
state = previousState;
|
|
1176
|
+
previousTools = previousToolSnapshot;
|
|
1177
|
+
if (workflowOwner !== previousOwner) {
|
|
1178
|
+
workflowMutex.release(workflowOwner);
|
|
1179
|
+
workflowOwner = previousOwner;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
throw error;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
function rollbackNewActivation(
|
|
1187
|
+
previousState: PlanModeState,
|
|
1188
|
+
previousToolSnapshot: string[] | undefined,
|
|
1189
|
+
ctx: ExtensionContext,
|
|
1190
|
+
previousOwner?: WorkflowMutexOwner,
|
|
1191
|
+
) {
|
|
1192
|
+
const activatedOwner = workflowOwner;
|
|
1193
|
+
readyPresentationIntent = undefined;
|
|
1194
|
+
try {
|
|
1195
|
+
if (state.enabled) {
|
|
1196
|
+
try {
|
|
1197
|
+
restoreTools();
|
|
1198
|
+
} finally {
|
|
1199
|
+
restoreThinkingLevel();
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
} finally {
|
|
1203
|
+
state = previousState;
|
|
1204
|
+
previousTools = previousToolSnapshot;
|
|
1205
|
+
try {
|
|
1206
|
+
persistState();
|
|
1207
|
+
updateUi(ctx);
|
|
1208
|
+
} finally {
|
|
1209
|
+
if (activatedOwner !== previousOwner) {
|
|
1210
|
+
workflowMutex.release(activatedOwner);
|
|
1211
|
+
workflowOwner = previousOwner;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
function bindWorkflowSessionIfNeeded(ctx: ExtensionContext) {
|
|
1218
|
+
if (currentSession === ctx.sessionManager) return;
|
|
1219
|
+
currentSession = ctx.sessionManager;
|
|
1220
|
+
workflowOwner = undefined;
|
|
1221
|
+
workflowMutex.bindSession(ctx.sessionManager);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
function releaseWorkflowOwner() {
|
|
1225
|
+
const owner = workflowOwner;
|
|
1226
|
+
workflowMutex.release(owner);
|
|
1227
|
+
if (!workflowMutex.isOwner(owner)) workflowOwner = undefined;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
function reportWorkflowBusy(ctx: ExtensionContext) {
|
|
1231
|
+
const message = "Another workflow is active in this session. End it before starting Plan mode.";
|
|
1232
|
+
if (!ctx.hasUI) throw new Error(message);
|
|
1233
|
+
ctx.ui.notify(message, "warning");
|
|
1234
|
+
return false;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
function reportRestoredWorkflowBusy(ctx: ExtensionContext) {
|
|
1238
|
+
if (!ctx.hasUI) return;
|
|
1239
|
+
ctx.ui.notify(
|
|
1240
|
+
"Plan mode was not restored because another workflow is active in this session. Reload or start Plan mode after it ends.",
|
|
1241
|
+
"warning",
|
|
1242
|
+
);
|
|
1080
1243
|
}
|
|
1081
1244
|
|
|
1082
1245
|
function updateUi(ctx: ExtensionContext) {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export const WORKFLOW_MUTEX_CHANNEL = "workflow:mutex:v1";
|
|
4
|
+
export const AGENT_WORKFLOW_GROUP = "agent-workflow";
|
|
5
|
+
|
|
6
|
+
export type WorkflowMutexOwner = symbol;
|
|
7
|
+
|
|
8
|
+
interface WorkflowMutexAttemptV1 {
|
|
9
|
+
session: object;
|
|
10
|
+
group: string;
|
|
11
|
+
busy: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class WorkflowMutex {
|
|
15
|
+
private session: object | undefined;
|
|
16
|
+
private readonly heldGroups = new Map<string, WorkflowMutexOwner>();
|
|
17
|
+
private generation = 0;
|
|
18
|
+
|
|
19
|
+
constructor(private readonly pi: Pick<ExtensionAPI, "events">) {
|
|
20
|
+
pi.events.on(WORKFLOW_MUTEX_CHANNEL, (payload) => {
|
|
21
|
+
this.answer(payload);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
bindSession(session: object): void {
|
|
26
|
+
this.generation += 1;
|
|
27
|
+
this.heldGroups.clear();
|
|
28
|
+
this.session = session;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
unbindSession(session: object): void {
|
|
32
|
+
if (this.session !== session) return;
|
|
33
|
+
this.generation += 1;
|
|
34
|
+
this.heldGroups.clear();
|
|
35
|
+
this.session = undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
acquire(group = AGENT_WORKFLOW_GROUP): WorkflowMutexOwner | undefined {
|
|
39
|
+
const session = this.session;
|
|
40
|
+
const generation = this.generation;
|
|
41
|
+
if (!session || this.heldGroups.has(group)) return undefined;
|
|
42
|
+
|
|
43
|
+
const attempt: WorkflowMutexAttemptV1 = { session, group, busy: false };
|
|
44
|
+
try {
|
|
45
|
+
this.pi.events.emit(WORKFLOW_MUTEX_CHANNEL, attempt);
|
|
46
|
+
} catch {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (
|
|
50
|
+
this.session !== session ||
|
|
51
|
+
this.generation !== generation ||
|
|
52
|
+
attempt.session !== session ||
|
|
53
|
+
attempt.group !== group ||
|
|
54
|
+
attempt.busy !== false ||
|
|
55
|
+
this.heldGroups.has(group)
|
|
56
|
+
) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const owner = Symbol(group);
|
|
61
|
+
this.heldGroups.set(group, owner);
|
|
62
|
+
return owner;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
isOwner(owner: WorkflowMutexOwner | undefined, group = AGENT_WORKFLOW_GROUP): boolean {
|
|
66
|
+
return owner !== undefined && this.heldGroups.get(group) === owner;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
release(owner: WorkflowMutexOwner | undefined, group = AGENT_WORKFLOW_GROUP): void {
|
|
70
|
+
if (!this.isOwner(owner, group)) return;
|
|
71
|
+
this.heldGroups.delete(group);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private answer(payload: unknown): void {
|
|
75
|
+
try {
|
|
76
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return;
|
|
77
|
+
const attempt = payload as Partial<WorkflowMutexAttemptV1>;
|
|
78
|
+
if (attempt.session !== this.session) return;
|
|
79
|
+
if (attempt.group !== AGENT_WORKFLOW_GROUP) return;
|
|
80
|
+
if (typeof attempt.busy !== "boolean") return;
|
|
81
|
+
if (!this.heldGroups.has(attempt.group)) return;
|
|
82
|
+
attempt.busy = true;
|
|
83
|
+
} catch {
|
|
84
|
+
// Protocol listeners must ignore unusual objects without interrupting sibling listeners.
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|