@narumitw/pi-plan-mode 0.60.1 → 0.60.2
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/CHANGELOG.md +6 -0
- package/README.md +7 -0
- package/dist/index.ts +72 -20
- package/dist/index.ts.map +3 -3
- package/package.json +1 -1
- package/src/plan-mode.ts +45 -20
- package/src/state-persistence.ts +34 -0
package/package.json
CHANGED
package/src/plan-mode.ts
CHANGED
|
@@ -91,6 +91,7 @@ import {
|
|
|
91
91
|
type PlanModeWorkflowToolPolicy,
|
|
92
92
|
restorePlanModeState,
|
|
93
93
|
} from "./state.js";
|
|
94
|
+
import { createPlanModeStatePersistence } from "./state-persistence.js";
|
|
94
95
|
import { planModeToolAvailability } from "./tool-availability.js";
|
|
95
96
|
import {
|
|
96
97
|
canSelectToolInPlanMode,
|
|
@@ -168,6 +169,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
168
169
|
let workflowOwner: WorkflowMutexOwner | undefined;
|
|
169
170
|
let currentSession: object | undefined;
|
|
170
171
|
let currentSessionContext: ExtensionContext | undefined;
|
|
172
|
+
let shutdownStarted = false;
|
|
173
|
+
let shutdownTask: Promise<void> | undefined;
|
|
171
174
|
let widgetState: EditorStatusWidgetState = { collapsed: false };
|
|
172
175
|
let interactiveUiPromise: Promise<InteractiveUi> | undefined;
|
|
173
176
|
const loadInteractiveUi = () => {
|
|
@@ -206,7 +209,10 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
206
209
|
let settingsReloadTimer: ReturnType<typeof setTimeout> | undefined;
|
|
207
210
|
const implementationRetention = createImplementationRetentionCoordinator();
|
|
208
211
|
const finalizationRequest = createFinalizationRequestCoordinator();
|
|
209
|
-
const
|
|
212
|
+
const statePersistence = createPlanModeStatePersistence(STATE_ENTRY_TYPE, (snapshot) =>
|
|
213
|
+
pi.appendEntry<PlanModeState>(STATE_ENTRY_TYPE, snapshot),
|
|
214
|
+
);
|
|
215
|
+
const persistState = () => statePersistence.persist(state, currentSessionContext?.sessionManager.getBranch() ?? []);
|
|
210
216
|
const interaction = createInteractionCoordinator(pi, {
|
|
211
217
|
getState: () => state,
|
|
212
218
|
setState: (next) => {
|
|
@@ -525,6 +531,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
525
531
|
widgetState = { collapsed: false };
|
|
526
532
|
currentSession = ctx.sessionManager;
|
|
527
533
|
currentSessionContext = ctx;
|
|
534
|
+
shutdownStarted = false;
|
|
535
|
+
shutdownTask = undefined;
|
|
528
536
|
workflowOwner = undefined;
|
|
529
537
|
workflowMutex.bindSession(ctx.sessionManager);
|
|
530
538
|
refreshStateBeforeFirstAgentStart = event.reason === "new";
|
|
@@ -540,6 +548,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
540
548
|
settings = { thinkingLevel: "inherit" };
|
|
541
549
|
const branch = ctx.sessionManager.getBranch();
|
|
542
550
|
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
551
|
+
statePersistence.restore(restoredState);
|
|
543
552
|
restoreModeContractTracking(branch, restoredState);
|
|
544
553
|
state = { enabled: false, awaitingAction: false };
|
|
545
554
|
await applyPlanModeSettings(generation, ctx, true);
|
|
@@ -596,6 +605,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
596
605
|
const branch = ctx.sessionManager.getBranch();
|
|
597
606
|
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
598
607
|
restoreModeContractTracking(branch, restoredState);
|
|
608
|
+
statePersistence.restore(restoredState);
|
|
599
609
|
if (!installRestoredState(restoredState, ctx)) return;
|
|
600
610
|
implementationRetention.restore(state.activeImplementation);
|
|
601
611
|
startPlanModeSettingsWatch(menuGeneration);
|
|
@@ -604,7 +614,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
604
614
|
});
|
|
605
615
|
|
|
606
616
|
pi.on("thinking_level_select", (event) => {
|
|
607
|
-
if (!state.enabled || !state.appliedThinkingLevel) return;
|
|
617
|
+
if (shutdownStarted || !state.enabled || !state.appliedThinkingLevel) return;
|
|
608
618
|
if (event.level !== state.appliedThinkingLevel) {
|
|
609
619
|
state = {
|
|
610
620
|
...state,
|
|
@@ -616,11 +626,17 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
616
626
|
}
|
|
617
627
|
});
|
|
618
628
|
|
|
619
|
-
pi.on("session_shutdown",
|
|
629
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
620
630
|
if (currentSession !== undefined && currentSession !== ctx.sessionManager) {
|
|
621
631
|
workflowMutex.unbindSession(ctx.sessionManager);
|
|
622
632
|
return;
|
|
623
633
|
}
|
|
634
|
+
shutdownTask ??= shutdownPlanModeSession(ctx);
|
|
635
|
+
return shutdownTask;
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
async function shutdownPlanModeSession(ctx: ExtensionContext) {
|
|
639
|
+
shutdownStarted = true;
|
|
624
640
|
interaction.disposeSession();
|
|
625
641
|
invalidateReadyMenu();
|
|
626
642
|
cancelDeferredFreshImplementation();
|
|
@@ -643,7 +659,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
643
659
|
);
|
|
644
660
|
}
|
|
645
661
|
finalizationRequest.reset();
|
|
646
|
-
|
|
662
|
+
const shutdownGeneration = ++menuGeneration;
|
|
647
663
|
menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
|
|
648
664
|
readyPresentationIntent = undefined;
|
|
649
665
|
latestCommandContext = undefined;
|
|
@@ -653,29 +669,37 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
653
669
|
workflowAllowedToolNames = undefined;
|
|
654
670
|
pendingWorkflowToolPolicy = undefined;
|
|
655
671
|
implementationRetention.reset();
|
|
672
|
+
const shutdownIsCurrent = () =>
|
|
673
|
+
shutdownGeneration === menuGeneration && (currentSession === undefined || currentSession === shutdownSession);
|
|
656
674
|
if (runtimeApplication) await runtimeApplication.completion;
|
|
657
|
-
if (
|
|
658
|
-
workflowMutex.unbindSession(shutdownSession);
|
|
675
|
+
if (!shutdownIsCurrent()) {
|
|
676
|
+
if (currentSession !== shutdownSession) workflowMutex.unbindSession(shutdownSession);
|
|
659
677
|
return;
|
|
660
678
|
}
|
|
661
679
|
await awaitPlanModeSettingsWrites(dependencies.settingsPath);
|
|
662
|
-
if (
|
|
663
|
-
workflowMutex.unbindSession(shutdownSession);
|
|
680
|
+
if (!shutdownIsCurrent()) {
|
|
681
|
+
if (currentSession !== shutdownSession) workflowMutex.unbindSession(shutdownSession);
|
|
664
682
|
return;
|
|
665
683
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
684
|
+
try {
|
|
685
|
+
captureManualThinkingLevel();
|
|
686
|
+
persistState();
|
|
687
|
+
} finally {
|
|
688
|
+
try {
|
|
689
|
+
if (state.enabled) restoreThinkingLevel();
|
|
690
|
+
} finally {
|
|
691
|
+
stopPlanModeSettingsWatch();
|
|
692
|
+
clearUi(ctx);
|
|
693
|
+
releaseWorkflowOwner();
|
|
694
|
+
workflowMutex.unbindSession(ctx.sessionManager);
|
|
695
|
+
if (currentSession === ctx.sessionManager) {
|
|
696
|
+
currentSession = undefined;
|
|
697
|
+
currentSessionContext = undefined;
|
|
698
|
+
widgetState = { collapsed: false };
|
|
699
|
+
}
|
|
700
|
+
}
|
|
677
701
|
}
|
|
678
|
-
}
|
|
702
|
+
}
|
|
679
703
|
|
|
680
704
|
pi.on("tool_call", async (event, ctx) => {
|
|
681
705
|
const requiredHelper =
|
|
@@ -1770,6 +1794,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1770
1794
|
implementationRetention.reset();
|
|
1771
1795
|
const branch = ctx.sessionManager.getBranch();
|
|
1772
1796
|
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
1797
|
+
statePersistence.restore(restoredState);
|
|
1773
1798
|
restoreModeContractTracking(branch, restoredState);
|
|
1774
1799
|
if (!installRestoredState(restoredState, ctx)) return;
|
|
1775
1800
|
implementationRetention.restore(state.activeImplementation);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { type PlanModeState, restorePlanModeState } from "./state.js";
|
|
3
|
+
|
|
4
|
+
export function createPlanModeStatePersistence(stateEntryType: string, append: (state: PlanModeState) => void) {
|
|
5
|
+
let persisted = JSON.stringify({ enabled: false, awaitingAction: false });
|
|
6
|
+
let appendFailed = false;
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
restore(state: PlanModeState) {
|
|
10
|
+
persisted = JSON.stringify(state);
|
|
11
|
+
appendFailed = false;
|
|
12
|
+
},
|
|
13
|
+
persist(state: PlanModeState, branch: unknown[]) {
|
|
14
|
+
const serialized = JSON.stringify(state);
|
|
15
|
+
// Detach the append payload from runtime state. Pi retains this object in memory.
|
|
16
|
+
const payload = JSON.parse(serialized) as PlanModeState;
|
|
17
|
+
if (!appendFailed && isDeepStrictEqual(payload, JSON.parse(persisted))) {
|
|
18
|
+
// A repeated no-plan entry can still be needed to stop recovery from a later tool result.
|
|
19
|
+
const clearsRecoveredPlan =
|
|
20
|
+
state.enabled && !state.latestPlan && restorePlanModeState(branch, stateEntryType).latestPlan !== undefined;
|
|
21
|
+
if (!clearsRecoveredPlan) return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
append(payload);
|
|
25
|
+
} catch (error: unknown) {
|
|
26
|
+
// A failed append can leave the in-memory branch uncertain. Let rollback repair it.
|
|
27
|
+
appendFailed = true;
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
persisted = serialized;
|
|
31
|
+
appendFailed = false;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|