@narumitw/pi-plan-mode 0.60.1 → 0.60.3

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
@@ -48,7 +48,7 @@ import {
48
48
  snapshotAvailableImplementationModels,
49
49
  snapshotPlanModeSelectedNames,
50
50
  updatePlanModeSettings
51
- } from "./chunks/chunk-UJ6TV2YV.ts";
51
+ } from "./chunks/chunk-OB2K6QCX.ts";
52
52
 
53
53
  // src/plan-mode.ts
54
54
  import { randomUUID as randomUUID2 } from "node:crypto";
@@ -1499,6 +1499,35 @@ async function preflightSavedPlanImplementation(ctx, isCurrent) {
1499
1499
  return true;
1500
1500
  }
1501
1501
 
1502
+ // src/state-persistence.ts
1503
+ import { isDeepStrictEqual } from "node:util";
1504
+ function createPlanModeStatePersistence(stateEntryType, append) {
1505
+ let persisted = JSON.stringify({ enabled: false, awaitingAction: false });
1506
+ let appendFailed = false;
1507
+ return {
1508
+ restore(state) {
1509
+ persisted = JSON.stringify(state);
1510
+ appendFailed = false;
1511
+ },
1512
+ persist(state, branch) {
1513
+ const serialized = JSON.stringify(state);
1514
+ const payload = JSON.parse(serialized);
1515
+ if (!appendFailed && isDeepStrictEqual(payload, JSON.parse(persisted))) {
1516
+ const clearsRecoveredPlan = state.enabled && !state.latestPlan && restorePlanModeState(branch, stateEntryType).latestPlan !== void 0;
1517
+ if (!clearsRecoveredPlan) return;
1518
+ }
1519
+ try {
1520
+ append(payload);
1521
+ } catch (error) {
1522
+ appendFailed = true;
1523
+ throw error;
1524
+ }
1525
+ persisted = serialized;
1526
+ appendFailed = false;
1527
+ }
1528
+ };
1529
+ }
1530
+
1502
1531
  // src/workflow-mutex.ts
1503
1532
  var WORKFLOW_MUTEX_CHANNEL = "workflow:mutex:v1";
1504
1533
  var AGENT_WORKFLOW_GROUP = "agent-workflow";
@@ -1573,12 +1602,14 @@ function planMode(pi, dependencies = {}) {
1573
1602
  let workflowOwner;
1574
1603
  let currentSession;
1575
1604
  let currentSessionContext;
1605
+ let shutdownStarted = false;
1606
+ let shutdownTask;
1576
1607
  let widgetState = { collapsed: false };
1577
1608
  let interactiveUiPromise;
1578
1609
  const loadInteractiveUi = () => {
1579
1610
  if (dependencies.loadInteractiveUi) return dependencies.loadInteractiveUi();
1580
1611
  if (!interactiveUiPromise) {
1581
- interactiveUiPromise = import("./chunks/interactive-ui-EMUWYNHT.ts").catch((error) => {
1612
+ interactiveUiPromise = import("./chunks/interactive-ui-KZ7UGELT.ts").catch((error) => {
1582
1613
  interactiveUiPromise = void 0;
1583
1614
  throw error;
1584
1615
  });
@@ -1611,7 +1642,11 @@ function planMode(pi, dependencies = {}) {
1611
1642
  let settingsReloadTimer;
1612
1643
  const implementationRetention = createImplementationRetentionCoordinator();
1613
1644
  const finalizationRequest = createFinalizationRequestCoordinator();
1614
- const persistState = () => pi.appendEntry(STATE_ENTRY_TYPE, state);
1645
+ const statePersistence = createPlanModeStatePersistence(
1646
+ STATE_ENTRY_TYPE,
1647
+ (snapshot) => pi.appendEntry(STATE_ENTRY_TYPE, snapshot)
1648
+ );
1649
+ const persistState = () => statePersistence.persist(state, currentSessionContext?.sessionManager.getBranch() ?? []);
1615
1650
  const interaction = createInteractionCoordinator(pi, {
1616
1651
  getState: () => state,
1617
1652
  setState: (next) => {
@@ -1895,6 +1930,8 @@ function planMode(pi, dependencies = {}) {
1895
1930
  widgetState = { collapsed: false };
1896
1931
  currentSession = ctx.sessionManager;
1897
1932
  currentSessionContext = ctx;
1933
+ shutdownStarted = false;
1934
+ shutdownTask = void 0;
1898
1935
  workflowOwner = void 0;
1899
1936
  workflowMutex.bindSession(ctx.sessionManager);
1900
1937
  refreshStateBeforeFirstAgentStart = event.reason === "new";
@@ -1910,6 +1947,7 @@ function planMode(pi, dependencies = {}) {
1910
1947
  settings = { thinkingLevel: "inherit" };
1911
1948
  const branch = ctx.sessionManager.getBranch();
1912
1949
  const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
1950
+ statePersistence.restore(restoredState);
1913
1951
  restoreModeContractTracking(branch, restoredState);
1914
1952
  state = { enabled: false, awaitingAction: false };
1915
1953
  await applyPlanModeSettings(generation, ctx, true);
@@ -1958,6 +1996,7 @@ function planMode(pi, dependencies = {}) {
1958
1996
  const branch = ctx.sessionManager.getBranch();
1959
1997
  const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
1960
1998
  restoreModeContractTracking(branch, restoredState);
1999
+ statePersistence.restore(restoredState);
1961
2000
  if (!installRestoredState(restoredState, ctx)) return;
1962
2001
  implementationRetention.restore(state.activeImplementation);
1963
2002
  startPlanModeSettingsWatch(menuGeneration);
@@ -1965,7 +2004,7 @@ function planMode(pi, dependencies = {}) {
1965
2004
  await applyPendingImplementationRuntime(ctx);
1966
2005
  });
1967
2006
  pi.on("thinking_level_select", (event) => {
1968
- if (!state.enabled || !state.appliedThinkingLevel) return;
2007
+ if (shutdownStarted || !state.enabled || !state.appliedThinkingLevel) return;
1969
2008
  if (event.level !== state.appliedThinkingLevel) {
1970
2009
  state = {
1971
2010
  ...state,
@@ -1976,11 +2015,16 @@ function planMode(pi, dependencies = {}) {
1976
2015
  persistState();
1977
2016
  }
1978
2017
  });
1979
- pi.on("session_shutdown", async (_event, ctx) => {
2018
+ pi.on("session_shutdown", (_event, ctx) => {
1980
2019
  if (currentSession !== void 0 && currentSession !== ctx.sessionManager) {
1981
2020
  workflowMutex.unbindSession(ctx.sessionManager);
1982
2021
  return;
1983
2022
  }
2023
+ shutdownTask ??= shutdownPlanModeSession(ctx);
2024
+ return shutdownTask;
2025
+ });
2026
+ async function shutdownPlanModeSession(ctx) {
2027
+ shutdownStarted = true;
1984
2028
  interaction.disposeSession();
1985
2029
  invalidateReadyMenu();
1986
2030
  cancelDeferredFreshImplementation();
@@ -1999,7 +2043,7 @@ function planMode(pi, dependencies = {}) {
1999
2043
  );
2000
2044
  }
2001
2045
  finalizationRequest.reset();
2002
- menuGeneration += 1;
2046
+ const shutdownGeneration = ++menuGeneration;
2003
2047
  menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
2004
2048
  readyPresentationIntent = void 0;
2005
2049
  latestCommandContext = void 0;
@@ -2009,29 +2053,36 @@ function planMode(pi, dependencies = {}) {
2009
2053
  workflowAllowedToolNames = void 0;
2010
2054
  pendingWorkflowToolPolicy = void 0;
2011
2055
  implementationRetention.reset();
2056
+ const shutdownIsCurrent = () => shutdownGeneration === menuGeneration && (currentSession === void 0 || currentSession === shutdownSession);
2012
2057
  if (runtimeApplication) await runtimeApplication.completion;
2013
- if (currentSession !== void 0 && currentSession !== shutdownSession) {
2014
- workflowMutex.unbindSession(shutdownSession);
2058
+ if (!shutdownIsCurrent()) {
2059
+ if (currentSession !== shutdownSession) workflowMutex.unbindSession(shutdownSession);
2015
2060
  return;
2016
2061
  }
2017
2062
  await awaitPlanModeSettingsWrites(dependencies.settingsPath);
2018
- if (currentSession !== void 0 && currentSession !== shutdownSession) {
2019
- workflowMutex.unbindSession(shutdownSession);
2063
+ if (!shutdownIsCurrent()) {
2064
+ if (currentSession !== shutdownSession) workflowMutex.unbindSession(shutdownSession);
2020
2065
  return;
2021
2066
  }
2022
- captureManualThinkingLevel();
2023
- persistState();
2024
- if (state.enabled) restoreThinkingLevel();
2025
- stopPlanModeSettingsWatch();
2026
- clearUi(ctx);
2027
- releaseWorkflowOwner();
2028
- workflowMutex.unbindSession(ctx.sessionManager);
2029
- if (currentSession === ctx.sessionManager) {
2030
- currentSession = void 0;
2031
- currentSessionContext = void 0;
2032
- widgetState = { collapsed: false };
2067
+ try {
2068
+ captureManualThinkingLevel();
2069
+ persistState();
2070
+ } finally {
2071
+ try {
2072
+ if (state.enabled) restoreThinkingLevel();
2073
+ } finally {
2074
+ stopPlanModeSettingsWatch();
2075
+ clearUi(ctx);
2076
+ releaseWorkflowOwner();
2077
+ workflowMutex.unbindSession(ctx.sessionManager);
2078
+ if (currentSession === ctx.sessionManager) {
2079
+ currentSession = void 0;
2080
+ currentSessionContext = void 0;
2081
+ widgetState = { collapsed: false };
2082
+ }
2083
+ }
2033
2084
  }
2034
- });
2085
+ }
2035
2086
  pi.on("tool_call", async (event, ctx) => {
2036
2087
  const requiredHelper = event.toolName === PLAN_MODE_QUESTION_TOOL_NAME || event.toolName === PLAN_MODE_COMPLETE_TOOL_NAME;
2037
2088
  if (!state.enabled) {
@@ -2922,6 +2973,7 @@ ${intent.plan}`,
2922
2973
  implementationRetention.reset();
2923
2974
  const branch = ctx.sessionManager.getBranch();
2924
2975
  const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
2976
+ statePersistence.restore(restoredState);
2925
2977
  restoreModeContractTracking(branch, restoredState);
2926
2978
  if (!installRestoredState(restoredState, ctx)) return;
2927
2979
  implementationRetention.restore(state.activeImplementation);