@narumitw/pi-plan-mode 0.53.0 → 0.55.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/dist/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // @generated by scripts/build-runtime.mjs; do not edit.
2
- // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
2
+ // @ts-nocheck -- the generated entry uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
4
  IMPLEMENTATION_PLAN_RETENTIONS,
5
5
  PLAN_HISTORY_IMPLEMENTATION_PROMPT,
@@ -16,12 +16,12 @@ import {
16
16
  configuredImplementationPlanRetention,
17
17
  configuredPlanExportPath,
18
18
  configuredPlanModeToggleShortcut,
19
- configuredPlanModeToolVisibility,
20
19
  configuredThinkingLevel,
21
20
  createImplementationRetentionCoordinator,
22
21
  exportStoredPlan,
23
22
  filterAvailableSelectedToolNames,
24
23
  findBlockedCommandSegment,
24
+ findBlockedPowerShellCommandSegment,
25
25
  implementationRetentionPreview,
26
26
  invalidPlanMessage,
27
27
  latestAssistantStopReason,
@@ -42,7 +42,7 @@ import {
42
42
  toolPolicyLabel,
43
43
  unique,
44
44
  updatePlanModeSettings
45
- } from "./chunks/chunk-D2V3VWND.js";
45
+ } from "./chunks/chunk-KSGLZ4S2.js";
46
46
 
47
47
  // src/plan-mode.ts
48
48
  import { randomUUID as randomUUID2 } from "node:crypto";
@@ -442,114 +442,6 @@ function safeErrorDetail(error) {
442
442
  return characters.length > 500 ? `${characters.slice(0, 499).join("")}\u2026` : normalized;
443
443
  }
444
444
 
445
- // src/helper-tool-visibility.ts
446
- var PLAN_MODE_HELPER_TOOL_NAMES = [
447
- PLAN_MODE_QUESTION_TOOL_NAME,
448
- PLAN_MODE_COMPLETE_TOOL_NAME
449
- ];
450
- var PlanModeHelperVisibilityPolicy = class {
451
- constructor(pi) {
452
- this.pi = pi;
453
- }
454
- pi;
455
- unlocked = false;
456
- hiddenByPolicy = /* @__PURE__ */ new Set();
457
- isUnlocked() {
458
- return this.unlocked;
459
- }
460
- hasHiddenTools() {
461
- return this.hiddenByPolicy.size > 0;
462
- }
463
- toolsAvailable() {
464
- const active = new Set(this.pi.getActiveTools());
465
- return PLAN_MODE_HELPER_TOOL_NAMES.every((name) => active.has(name));
466
- }
467
- prepareSessionStart(visibility, previous) {
468
- if (visibility === "after-first-plan" && previous === "always") this.lock();
469
- if (visibility === "always") this.reveal();
470
- }
471
- reconcileInactiveState(visibility) {
472
- if (visibility === "after-first-plan" && !this.unlocked) this.hideIfLocked();
473
- }
474
- deferVisibilityChange(visibility) {
475
- if (visibility === "after-first-plan") this.lock();
476
- }
477
- prepareActivation(ctx) {
478
- if (!this.toolsAvailable() && ctx.isIdle?.() !== true) {
479
- throw new Error("wait until Pi is idle before revealing the Plan tools");
480
- }
481
- this.reveal();
482
- }
483
- applyVisibilityChange(previous, next, ctx) {
484
- if (previous === next) return;
485
- if (next === "always") {
486
- if (!this.toolsAvailable() && ctx.isIdle?.() !== true) {
487
- throw new Error("Wait for Pi to become idle before revealing Plan tools.");
488
- }
489
- this.reveal();
490
- return;
491
- }
492
- if (ctx.isIdle?.() !== true) {
493
- throw new Error("Wait for Pi to become idle before hiding Plan tools.");
494
- }
495
- this.lock();
496
- this.hideIfLocked();
497
- }
498
- hideIfLocked() {
499
- if (this.unlocked) return;
500
- const active = this.pi.getActiveTools();
501
- const hidden = active.filter((name) => this.isHelperTool(name));
502
- if (hidden.length === 0) return;
503
- this.pi.setActiveTools(active.filter((name) => !this.isHelperTool(name)));
504
- for (const name of hidden) this.hiddenByPolicy.add(name);
505
- }
506
- snapshot() {
507
- return {
508
- activeTools: this.pi.getActiveTools(),
509
- helperToolsUnlocked: this.unlocked,
510
- helperToolsHiddenByPolicy: [...this.hiddenByPolicy]
511
- };
512
- }
513
- restore(snapshot) {
514
- this.pi.setActiveTools(snapshot.activeTools);
515
- this.unlocked = snapshot.helperToolsUnlocked;
516
- this.hiddenByPolicy.clear();
517
- for (const name of snapshot.helperToolsHiddenByPolicy) this.hiddenByPolicy.add(name);
518
- }
519
- reveal() {
520
- const snapshot = this.snapshot();
521
- try {
522
- const active = this.pi.getActiveTools();
523
- const canonical = [
524
- ...active.filter((name) => !this.isHelperTool(name)),
525
- ...PLAN_MODE_HELPER_TOOL_NAMES
526
- ];
527
- if (canonical.length !== active.length || canonical.some((name, index) => name !== active[index])) {
528
- this.pi.setActiveTools(canonical);
529
- }
530
- if (!this.toolsAvailable()) {
531
- throw new Error(
532
- "plan_mode_question and plan_mode_complete are unavailable; leave the restrictive tool mode before starting Plan mode"
533
- );
534
- }
535
- this.unlockAndForgetHidden();
536
- } catch (error) {
537
- this.restore(snapshot);
538
- throw error;
539
- }
540
- }
541
- lock() {
542
- this.unlocked = false;
543
- }
544
- unlockAndForgetHidden() {
545
- this.unlocked = true;
546
- this.hiddenByPolicy.clear();
547
- }
548
- isHelperTool(name) {
549
- return PLAN_MODE_HELPER_TOOL_NAMES.includes(name);
550
- }
551
- };
552
-
553
445
  // src/plan-action-controller.ts
554
446
  function createPlanActionController(options) {
555
447
  const freshAction = (ctx, lifecycle, signal) => options.implementFresh(ctx, () => lifecycle.isCurrent() && !signal.aborted);
@@ -643,34 +535,43 @@ function createPlanExportController(options) {
643
535
  }
644
536
 
645
537
  // src/presentation.ts
538
+ import { stripTerminalSequences, truncateToWidth } from "@earendil-works/pi-tui";
646
539
  var STATUS_KEY = "plan-mode";
647
540
  var PLAN_WIDGET_KEY = "plan-mode-plan";
541
+ var BIDI_CONTROLS = /[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu;
648
542
  function updatePlanModeUi(ctx, state, toolSummary) {
649
543
  ctx.ui.setStatus(STATUS_KEY, formatStatus(state));
544
+ let lines;
650
545
  if (state.enabled && state.latestPlan) {
651
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
652
- "Proposed plan ready",
653
- "Use /plan to implement, save, revise, or exit Plan mode."
654
- ]);
546
+ lines = ["Proposed plan ready", "Use /plan to implement, save, revise, or exit Plan mode."];
655
547
  } else if (state.enabled) {
656
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
548
+ lines = [
657
549
  "Plan mode: planning",
658
550
  toolSummary(),
659
551
  "Finish with plan_mode_complete when decision-ready."
660
- ]);
552
+ ];
661
553
  } else if (state.savedPlan) {
662
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
663
- "Plan saved for later",
664
- "Use /plan to show, implement, or clear it."
665
- ]);
554
+ lines = ["Plan saved for later", "Use /plan to show, implement, or clear it."];
666
555
  } else if (state.activeImplementation) {
667
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
668
- "Implementation plan active",
669
- "Use /plan to show, replace, or clear it."
670
- ]);
671
- } else {
672
- ctx.ui.setWidget(PLAN_WIDGET_KEY, void 0);
556
+ lines = ["Implementation plan active", "Use /plan to show, replace, or clear it."];
673
557
  }
558
+ publishPlanModeWidget(ctx, lines);
559
+ }
560
+ function renderPlanModeWidget(lines, theme, width) {
561
+ const renderWidth = Math.max(0, width);
562
+ return [
563
+ theme.fg("borderMuted", "\u2500".repeat(renderWidth)),
564
+ ...lines.map((line) => truncateToWidth(sanitizePlanModeWidgetLine(line), renderWidth, ""))
565
+ ];
566
+ }
567
+ function sanitizePlanModeWidgetLine(value) {
568
+ let text = "";
569
+ for (const character of stripTerminalSequences(value).replace(BIDI_CONTROLS, "")) {
570
+ const codePoint = character.codePointAt(0) ?? 0;
571
+ const isControl = codePoint <= 31 || codePoint >= 127 && codePoint <= 159;
572
+ text += isControl ? " " : character;
573
+ }
574
+ return text;
674
575
  }
675
576
  function clearPlanModeUi(ctx) {
676
577
  ctx.ui.setStatus(STATUS_KEY, void 0);
@@ -722,6 +623,22 @@ function planModeStatusText(state, toolSummary) {
722
623
  if (state.activeImplementation) return "An implementation plan is active.";
723
624
  return "Plan mode is off.";
724
625
  }
626
+ function publishPlanModeWidget(ctx, lines) {
627
+ if (!lines) {
628
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, void 0);
629
+ return;
630
+ }
631
+ if (ctx.mode !== "tui") {
632
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, [...lines]);
633
+ return;
634
+ }
635
+ const snapshot = [...lines];
636
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, (_tui, theme) => ({
637
+ render: (width) => renderPlanModeWidget(snapshot, theme, width),
638
+ invalidate: () => {
639
+ }
640
+ }));
641
+ }
725
642
  function formatStatus(state) {
726
643
  if (state.enabled) {
727
644
  if (state.awaitingAction || state.latestPlan) return "plan ready";
@@ -733,6 +650,20 @@ function formatStatus(state) {
733
650
  }
734
651
 
735
652
  // src/required-tools.ts
653
+ var REQUIRED_PLAN_MODE_TOOL_NAMES = [
654
+ PLAN_MODE_QUESTION_TOOL_NAME,
655
+ PLAN_MODE_COMPLETE_TOOL_NAME
656
+ ];
657
+ function planModeHelperToolsAvailable(toolNames) {
658
+ const active = new Set(toolNames);
659
+ return REQUIRED_PLAN_MODE_TOOL_NAMES.every((name) => active.has(name));
660
+ }
661
+ function assertPlanModeHelperToolsAvailable(toolNames) {
662
+ if (planModeHelperToolsAvailable(toolNames)) return;
663
+ throw new Error(
664
+ "plan_mode_question and plan_mode_complete are unavailable; include them in the active tool allowlist or leave the restrictive tool mode before starting Plan mode"
665
+ );
666
+ }
736
667
  function withRequiredPlanModeTools(toolNames) {
737
668
  return unique([
738
669
  ...withoutRequiredPlanModeTools(toolNames),
@@ -931,7 +862,6 @@ var BLOCKED_MUTATING_TOOLS = /* @__PURE__ */ new Set(["edit", "write", "update_p
931
862
  var DEFAULT_TOOLS = ["read", "bash", "edit", "write"];
932
863
  function planMode(pi, dependencies = {}) {
933
864
  const workflowMutex = new WorkflowMutex(pi);
934
- const helperVisibility = new PlanModeHelperVisibilityPolicy(pi);
935
865
  let workflowOwner;
936
866
  let currentSession;
937
867
  let currentSessionContext;
@@ -939,7 +869,7 @@ function planMode(pi, dependencies = {}) {
939
869
  const loadInteractiveUi = () => {
940
870
  if (dependencies.loadInteractiveUi) return dependencies.loadInteractiveUi();
941
871
  if (!interactiveUiPromise) {
942
- interactiveUiPromise = import("./chunks/interactive-ui-VESWS25D.js").catch((error) => {
872
+ interactiveUiPromise = import("./chunks/interactive-ui-PSDKEVVG.js").catch((error) => {
943
873
  interactiveUiPromise = void 0;
944
874
  throw error;
945
875
  });
@@ -1002,11 +932,7 @@ function planMode(pi, dependencies = {}) {
1002
932
  pi.registerTool({
1003
933
  name: PLAN_MODE_QUESTION_TOOL_NAME,
1004
934
  label: "Plan question",
1005
- description: "Ask the user one to three Plan-mode clarification questions with meaningful options, then wait for the answer. Only available while Plan mode is active.",
1006
- promptSnippet: "Ask user decision questions while Plan mode is active",
1007
- promptGuidelines: [
1008
- "In Plan mode, use plan_mode_question for important preferences, tradeoffs, or assumptions that cannot be discovered from read-only exploration."
1009
- ],
935
+ description: "Ask one to three structured questions only when the latest effective Plan contract explicitly says /plan mode is active. Tool visibility alone does not activate Plan mode. Never call for ordinary planning requests, the writing-plans skill, roadmaps, checklists, or plan-file work.",
1010
936
  parameters: PLAN_MODE_QUESTION_PARAMS,
1011
937
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
1012
938
  if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
@@ -1040,11 +966,7 @@ function planMode(pi, dependencies = {}) {
1040
966
  pi.registerTool({
1041
967
  name: PLAN_MODE_COMPLETE_TOOL_NAME,
1042
968
  label: "Complete plan",
1043
- description: "Submit the complete decision-ready implementation plan for user review. Only available while Plan mode is active, and must be the final standalone action.",
1044
- promptSnippet: "Submit the final Plan-mode implementation plan",
1045
- promptGuidelines: [
1046
- "Call plan_mode_complete alone as the final action only after the implementation plan is decision-complete."
1047
- ],
969
+ description: "Submit a decision-ready plan only when the latest effective Plan contract explicitly says /plan mode is active, and call it alone as the final action. Tool visibility alone does not activate Plan mode. Never call for ordinary planning requests, the writing-plans skill, roadmaps, checklists, or plan-file work.",
1048
970
  parameters: PLAN_MODE_COMPLETE_PARAMS,
1049
971
  renderResult: renderPlanModeCompletion,
1050
972
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
@@ -1173,17 +1095,12 @@ function planMode(pi, dependencies = {}) {
1173
1095
  const readPlanModeRuntimeSettings = async () => {
1174
1096
  return dependencies.readSettings?.() ?? readPlanModeSettings(explicitPlanModeSettingsPath);
1175
1097
  };
1176
- const applyPlanModeSettings = async (generation, ctx, showWarnings, applyWatchedVisibility = false) => {
1098
+ const applyPlanModeSettings = async (generation, ctx, showWarnings) => {
1177
1099
  const loadedSettings = await readPlanModeRuntimeSettings();
1178
1100
  if (generation !== menuGeneration || menuController.signal.aborted) {
1179
1101
  return void 0;
1180
1102
  }
1181
- const previousSettings = settings;
1182
- const nextSettings = loadedSettings.kind === "loaded" ? loadedSettings.settings : { thinkingLevel: "inherit" };
1183
- if (applyWatchedVisibility && ctx) {
1184
- applyWatchedHelperVisibility(previousSettings, nextSettings, ctx);
1185
- }
1186
- settings = nextSettings;
1103
+ settings = loadedSettings.kind === "loaded" ? loadedSettings.settings : { thinkingLevel: "inherit" };
1187
1104
  applyPlanModeShortcut(configuredPlanModeToggleShortcut(settings));
1188
1105
  if (!ctx || !showWarnings) return loadedSettings;
1189
1106
  if (loadedSettings.kind === "invalid") {
@@ -1209,7 +1126,7 @@ function planMode(pi, dependencies = {}) {
1209
1126
  }
1210
1127
  settingsReloadTimer = setTimeout(() => {
1211
1128
  settingsReloadTimer = void 0;
1212
- void applyPlanModeSettings(generation, currentSessionContext, false, true);
1129
+ void applyPlanModeSettings(generation, currentSessionContext, false);
1213
1130
  }, 75);
1214
1131
  };
1215
1132
  const startPlanModeSettingsWatch = (generation) => {
@@ -1234,7 +1151,6 @@ function planMode(pi, dependencies = {}) {
1234
1151
  };
1235
1152
  pi.on("session_start", async (event, ctx) => {
1236
1153
  const generation = ++menuGeneration;
1237
- const previousToolVisibility = configuredPlanModeToolVisibility(settings);
1238
1154
  finalizationRequest.reset();
1239
1155
  currentSession = ctx.sessionManager;
1240
1156
  currentSessionContext = ctx;
@@ -1256,12 +1172,7 @@ function planMode(pi, dependencies = {}) {
1256
1172
  await applyPlanModeSettings(generation, ctx, true);
1257
1173
  if (generation !== menuGeneration || menuController.signal.aborted) return;
1258
1174
  startPlanModeSettingsWatch(generation);
1259
- if (restoredState.enabled) {
1260
- if (!installRestoredState(restoredState, ctx, previousToolVisibility)) return;
1261
- } else {
1262
- reconcileInactiveHelperVisibility(previousToolVisibility, ctx);
1263
- if (!installRestoredState(restoredState, ctx)) return;
1264
- }
1175
+ if (!installRestoredState(restoredState, ctx)) return;
1265
1176
  implementationRetention.restore(state.activeImplementation);
1266
1177
  updateUi(ctx);
1267
1178
  });
@@ -1368,14 +1279,28 @@ function planMode(pi, dependencies = {}) {
1368
1279
  reason: `Plan mode blocks tool '${event.toolName}' because its safe policy metadata is unavailable.`
1369
1280
  };
1370
1281
  }
1371
- if (event.toolName !== "bash") return;
1372
- const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands);
1373
- if (blocked !== void 0) {
1374
- return {
1375
- block: true,
1376
- reason: `Plan mode blocks bash commands outside its reviewed inspection policy or containing explicitly unsafe arguments.
1282
+ if (event.toolName === "bash") {
1283
+ const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands);
1284
+ if (blocked !== void 0) {
1285
+ return {
1286
+ block: true,
1287
+ reason: `Plan mode blocks bash commands outside its reviewed inspection policy or containing explicitly unsafe arguments.
1377
1288
  Blocked command: ${blocked}`
1378
- };
1289
+ };
1290
+ }
1291
+ }
1292
+ if (event.toolName === "powershell") {
1293
+ const blocked = findBlockedPowerShellCommandSegment(
1294
+ readCommand(event.input),
1295
+ settings.safeSubcommands
1296
+ );
1297
+ if (blocked !== void 0) {
1298
+ return {
1299
+ block: true,
1300
+ reason: `Plan mode blocks PowerShell commands outside its reviewed inspection policy or containing explicitly unsafe syntax.
1301
+ Blocked command: ${blocked}`
1302
+ };
1303
+ }
1379
1304
  }
1380
1305
  });
1381
1306
  pi.on("message_start", (event) => {
@@ -1481,16 +1406,13 @@ ${intent.plan}`,
1481
1406
  if (!owner) return reportWorkflowBusy(ctx);
1482
1407
  workflowOwner = owner;
1483
1408
  const previousState = state;
1484
- const previousHelperVisibility = helperVisibility.snapshot();
1485
1409
  try {
1486
- helperVisibility.prepareActivation(ctx);
1410
+ assertPlanModeHelperToolsAvailable(safeGetActiveTools());
1487
1411
  if (!publishModeContract("plan", ctx)) {
1488
- helperVisibility.restore(previousHelperVisibility);
1489
1412
  releaseWorkflowOwner();
1490
1413
  return false;
1491
1414
  }
1492
1415
  } catch (error) {
1493
- helperVisibility.restore(previousHelperVisibility);
1494
1416
  releaseWorkflowOwner();
1495
1417
  return reportHelperActivationFailure(ctx, error);
1496
1418
  }
@@ -1512,14 +1434,13 @@ ${intent.plan}`,
1512
1434
  updateUi(ctx);
1513
1435
  return true;
1514
1436
  } catch (error) {
1515
- rollbackNewActivation(previousState, ctx, void 0, previousHelperVisibility);
1437
+ rollbackNewActivation(previousState, ctx);
1516
1438
  throw error;
1517
1439
  }
1518
1440
  }
1519
1441
  function enterPlanModeWithPrompt(prompt, ctx) {
1520
1442
  const previousState = state;
1521
1443
  const previousOwner = workflowOwner;
1522
- const previousHelperVisibility = helperVisibility.snapshot();
1523
1444
  const wasEnabled = state.enabled;
1524
1445
  if (!enterPlanMode(ctx)) return;
1525
1446
  if (!wasEnabled) {
@@ -1527,7 +1448,7 @@ ${intent.plan}`,
1527
1448
  }
1528
1449
  if (sendPlanModeUserMessage(prompt, ctx)) return;
1529
1450
  if (wasEnabled) return;
1530
- rollbackNewActivation(previousState, ctx, previousOwner, previousHelperVisibility);
1451
+ rollbackNewActivation(previousState, ctx, previousOwner);
1531
1452
  }
1532
1453
  function exitPlanMode(ctx) {
1533
1454
  if (!allowModeTransition(ctx, "leave or clear Plan mode")) return false;
@@ -1770,7 +1691,7 @@ ${intent.plan}`,
1770
1691
  if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
1771
1692
  const tools = selectableTools();
1772
1693
  await ui.showPlanLaunchMenu(ctx, {
1773
- statusText: helperVisibility.toolsAvailable() ? "Status: Off \u2014 Plan helper tools are visible for this runtime." : "Status: Off \u2014 Plan helper tools load on the first Plan start.",
1694
+ statusText: planModeHelperToolsAvailable(safeGetActiveTools()) ? "Status: Off \u2014 visible Plan helpers stay inactive until /plan starts." : "Status: Off \u2014 required Plan helpers are unavailable under the active tool policy.",
1774
1695
  initialScreen,
1775
1696
  getSelectedNames: () => snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot()),
1776
1697
  toolSummary: (selectedNames) => {
@@ -1852,7 +1773,7 @@ ${intent.plan}`,
1852
1773
  signal,
1853
1774
  isCurrent,
1854
1775
  settingsPath: dependencies.settingsPath,
1855
- updateSettings: (patch, options) => updateSettingsWithRuntime(patch, options, ctx, isCurrent),
1776
+ updateSettings: dependencies.updateSettings ?? updatePlanModeSettings,
1856
1777
  onSaved: (saved) => {
1857
1778
  if (!isCurrent()) return;
1858
1779
  settings = saved;
@@ -1862,54 +1783,6 @@ ${intent.plan}`,
1862
1783
  });
1863
1784
  return result.kind === "closed" && "reason" in result && result.reason === "close";
1864
1785
  }
1865
- async function updateSettingsWithRuntime(patch, options, ctx, isCurrent) {
1866
- const previousVisibility = configuredPlanModeToolVisibility(settings);
1867
- const nextVisibility = patch.toolVisibility ?? previousVisibility;
1868
- const persistSettings = dependencies.updateSettings ?? updatePlanModeSettings;
1869
- if (nextVisibility === previousVisibility) {
1870
- return persistSettings(patch, options);
1871
- }
1872
- if (!ctx.isIdle()) {
1873
- throw new Error("Wait for Pi to become idle before changing Plan tool visibility.");
1874
- }
1875
- bindWorkflowSessionIfNeeded(ctx);
1876
- const applicationSession = ctx.sessionManager;
1877
- const applicationGeneration = menuGeneration;
1878
- const applicationIsCurrent = () => currentSession === applicationSession && menuGeneration === applicationGeneration && isCurrent();
1879
- const retainedOwner = workflowMutex.isOwner(workflowOwner);
1880
- const temporaryOwner = retainedOwner ? workflowOwner : workflowMutex.acquire();
1881
- if (!temporaryOwner) {
1882
- throw new Error(
1883
- "Another workflow is active in this session. Plan tool visibility was not changed."
1884
- );
1885
- }
1886
- const visibilitySnapshot = helperVisibility.snapshot();
1887
- try {
1888
- helperVisibility.applyVisibilityChange(previousVisibility, nextVisibility, ctx);
1889
- const saved = await persistSettings(patch, options);
1890
- if (!applicationIsCurrent()) {
1891
- throw new DOMException("Plan settings session replaced", "AbortError");
1892
- }
1893
- settings = saved;
1894
- applyPlanModeShortcut(configuredPlanModeToggleShortcut(saved));
1895
- return saved;
1896
- } catch (error) {
1897
- if (applicationIsCurrent()) {
1898
- try {
1899
- helperVisibility.restore(visibilitySnapshot);
1900
- } catch (rollbackError) {
1901
- throw new AggregateError(
1902
- [error, rollbackError],
1903
- "Plan tool visibility settings failed and runtime rollback was incomplete."
1904
- );
1905
- }
1906
- }
1907
- throw error;
1908
- } finally {
1909
- if (!retainedOwner) workflowMutex.release(temporaryOwner);
1910
- if (!isCurrent()) latestCommandContext = void 0;
1911
- }
1912
- }
1913
1786
  function allowModeTransition(ctx, action) {
1914
1787
  if (ctx.isIdle()) return true;
1915
1788
  const message = `Cannot ${action} while an agent run is active. Wait for the run to settle, then retry.`;
@@ -1940,73 +1813,6 @@ ${intent.plan}`,
1940
1813
  function captureToolBaseline() {
1941
1814
  activeToolBaseline = withRequiredPlanModeTools(safeGetActiveTools());
1942
1815
  }
1943
- function applyWatchedHelperVisibility(previousSettings, nextSettings, ctx) {
1944
- const previousVisibility = configuredPlanModeToolVisibility(previousSettings);
1945
- const nextVisibility = configuredPlanModeToolVisibility(nextSettings);
1946
- if (previousVisibility === nextVisibility) return;
1947
- if (state.enabled) {
1948
- helperVisibility.deferVisibilityChange(nextVisibility);
1949
- return;
1950
- }
1951
- try {
1952
- if (!ctx.isIdle()) {
1953
- helperVisibility.deferVisibilityChange(nextVisibility);
1954
- return;
1955
- }
1956
- } catch {
1957
- helperVisibility.deferVisibilityChange(nextVisibility);
1958
- return;
1959
- }
1960
- const owner = workflowMutex.acquire();
1961
- if (!owner) {
1962
- helperVisibility.deferVisibilityChange(nextVisibility);
1963
- return;
1964
- }
1965
- const snapshot = helperVisibility.snapshot();
1966
- try {
1967
- helperVisibility.applyVisibilityChange(previousVisibility, nextVisibility, ctx);
1968
- } catch (error) {
1969
- helperVisibility.restore(snapshot);
1970
- helperVisibility.deferVisibilityChange(nextVisibility);
1971
- if (ctx.hasUI) {
1972
- const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
1973
- ctx.ui.notify(
1974
- `Could not apply reloaded Plan tool visibility; the current tool envelope remains unchanged until a later safe boundary: ${detail}`,
1975
- "warning"
1976
- );
1977
- }
1978
- } finally {
1979
- workflowMutex.release(owner);
1980
- }
1981
- }
1982
- function reconcileInactiveHelperVisibility(previousVisibility, ctx) {
1983
- const owner = workflowMutex.acquire();
1984
- if (!owner) {
1985
- if (ctx.hasUI) {
1986
- ctx.ui.notify(
1987
- "Plan tool visibility was deferred because another workflow is active in this session.",
1988
- "warning"
1989
- );
1990
- }
1991
- return false;
1992
- }
1993
- const snapshot = helperVisibility.snapshot();
1994
- try {
1995
- const visibility = configuredPlanModeToolVisibility(settings);
1996
- helperVisibility.prepareSessionStart(visibility, previousVisibility);
1997
- helperVisibility.reconcileInactiveState(visibility);
1998
- return true;
1999
- } catch (error) {
2000
- helperVisibility.restore(snapshot);
2001
- if (ctx.hasUI) {
2002
- const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
2003
- ctx.ui.notify(`Could not apply Plan tool visibility: ${detail}`, "error");
2004
- }
2005
- return false;
2006
- } finally {
2007
- workflowMutex.release(owner);
2008
- }
2009
- }
2010
1816
  function planModePolicyToolNames() {
2011
1817
  return workflowAllowedToolNames ?? computePlanModePolicyToolNames();
2012
1818
  }
@@ -2088,11 +1894,10 @@ ${intent.plan}`,
2088
1894
  return DEFAULT_TOOLS;
2089
1895
  }
2090
1896
  }
2091
- function installRestoredState(candidate, ctx, previousToolVisibility) {
1897
+ function installRestoredState(candidate, ctx) {
2092
1898
  const previousState = state;
2093
1899
  const previousWorkflowAllowedToolNames = workflowAllowedToolNames;
2094
1900
  const previousOwner = workflowOwner;
2095
- const previousHelperVisibility = helperVisibility.snapshot();
2096
1901
  const wasEnabled = state.enabled;
2097
1902
  if (candidate.enabled && !workflowMutex.isOwner(workflowOwner)) {
2098
1903
  const owner = workflowMutex.acquire();
@@ -2106,24 +1911,17 @@ ${intent.plan}`,
2106
1911
  }
2107
1912
  try {
2108
1913
  if (candidate.enabled) {
2109
- const visibility = configuredPlanModeToolVisibility(settings);
2110
- if (previousToolVisibility === void 0) {
2111
- helperVisibility.prepareActivation(ctx);
2112
- } else {
2113
- try {
2114
- helperVisibility.prepareSessionStart(visibility, previousToolVisibility);
2115
- helperVisibility.prepareActivation(ctx);
2116
- } catch {
2117
- helperVisibility.restore(previousHelperVisibility);
2118
- state = { enabled: false, awaitingAction: false };
2119
- workflowAllowedToolNames = void 0;
2120
- if (workflowOwner !== previousOwner) {
2121
- workflowMutex.release(workflowOwner);
2122
- workflowOwner = previousOwner;
2123
- }
2124
- reportRestoredHelpersUnavailable(ctx);
2125
- return false;
1914
+ try {
1915
+ assertPlanModeHelperToolsAvailable(safeGetActiveTools());
1916
+ } catch {
1917
+ state = { enabled: false, awaitingAction: false };
1918
+ workflowAllowedToolNames = void 0;
1919
+ if (workflowOwner !== previousOwner) {
1920
+ workflowMutex.release(workflowOwner);
1921
+ workflowOwner = previousOwner;
2126
1922
  }
1923
+ reportRestoredHelpersUnavailable(ctx);
1924
+ return false;
2127
1925
  }
2128
1926
  }
2129
1927
  if (wasEnabled && !candidate.enabled) {
@@ -2141,7 +1939,6 @@ ${intent.plan}`,
2141
1939
  } finally {
2142
1940
  state = previousState;
2143
1941
  workflowAllowedToolNames = previousWorkflowAllowedToolNames;
2144
- helperVisibility.restore(previousHelperVisibility);
2145
1942
  if (workflowOwner !== previousOwner) {
2146
1943
  workflowMutex.release(workflowOwner);
2147
1944
  workflowOwner = previousOwner;
@@ -2150,7 +1947,7 @@ ${intent.plan}`,
2150
1947
  throw error;
2151
1948
  }
2152
1949
  }
2153
- function rollbackNewActivation(previousState, ctx, previousOwner, previousHelperVisibility) {
1950
+ function rollbackNewActivation(previousState, ctx, previousOwner) {
2154
1951
  const activatedOwner = workflowOwner;
2155
1952
  readyPresentationIntent = void 0;
2156
1953
  try {
@@ -2162,7 +1959,6 @@ ${intent.plan}`,
2162
1959
  state = previousState;
2163
1960
  workflowAllowedToolNames = void 0;
2164
1961
  try {
2165
- if (previousHelperVisibility) helperVisibility.restore(previousHelperVisibility);
2166
1962
  persistState();
2167
1963
  updateUi(ctx);
2168
1964
  } finally {