@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-plan-mode",
3
- "version": "0.53.0",
3
+ "version": "0.55.0",
4
4
  "description": "Pi extension that adds a Codex-like read-only /plan collaboration mode.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,9 +38,9 @@
38
38
  "@earendil-works/pi-tui": "*"
39
39
  },
40
40
  "devDependencies": {
41
- "@biomejs/biome": "2.5.9",
42
- "@earendil-works/pi-coding-agent": "0.84.2",
43
- "@earendil-works/pi-tui": "0.84.2",
41
+ "@biomejs/biome": "2.5.10",
42
+ "@earendil-works/pi-coding-agent": "0.84.3",
43
+ "@earendil-works/pi-tui": "0.84.3",
44
44
  "esbuild": "0.28.2",
45
45
  "typescript": "7.0.2"
46
46
  },
@@ -50,6 +50,6 @@
50
50
  "directory": "packages/pi-plan-mode"
51
51
  },
52
52
  "dependencies": {
53
- "@narumitw/pi-tui-kit": "^0.57.1"
53
+ "@narumitw/pi-tui-kit": "^0.58.0"
54
54
  }
55
55
  }
package/src/plan-mode.ts CHANGED
@@ -27,10 +27,6 @@ import {
27
27
  formatTransferredPlanPrompt,
28
28
  startFreshImplementationFromState,
29
29
  } from "./fresh-implementation.js";
30
- import {
31
- PlanModeHelperVisibilityPolicy,
32
- type PlanModeHelperVisibilitySnapshot,
33
- } from "./helper-tool-visibility.js";
34
30
  import {
35
31
  createImplementationRetentionCoordinator,
36
32
  implementationRetentionPreview,
@@ -65,7 +61,11 @@ import {
65
61
  PLAN_MODE_QUESTION_TOOL_NAME,
66
62
  planModeQuestionCancelled,
67
63
  } from "./question-tool.js";
68
- import { withRequiredPlanModeTools } from "./required-tools.js";
64
+ import {
65
+ assertPlanModeHelperToolsAvailable,
66
+ planModeHelperToolsAvailable,
67
+ withRequiredPlanModeTools,
68
+ } from "./required-tools.js";
69
69
  import {
70
70
  preflightSavedPlanImplementation,
71
71
  savedPlanBlocksNewWorkflow,
@@ -74,11 +74,9 @@ import {
74
74
  awaitPlanModeSettingsWrites,
75
75
  configuredImplementationPlanRetention,
76
76
  configuredPlanModeToggleShortcut,
77
- configuredPlanModeToolVisibility,
78
77
  configuredThinkingLevel,
79
78
  type PlanModeSettings,
80
79
  type PlanModeSettingsPatch,
81
- type PlanModeToolVisibility,
82
80
  planModeSettingsPath,
83
81
  readPlanModeSettings,
84
82
  type UpdatePlanModeSettingsOptions,
@@ -89,6 +87,7 @@ import {
89
87
  canSelectToolInPlanMode,
90
88
  classifyPlanModeTool,
91
89
  findBlockedCommandSegment,
90
+ findBlockedPowerShellCommandSegment,
92
91
  readCommand,
93
92
  } from "./tool-policy.js";
94
93
  import {
@@ -124,7 +123,6 @@ interface PlanModeDependencies {
124
123
  // activation path cannot bypass the same atomic transition by crossing module-owned state.
125
124
  export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDependencies = {}) {
126
125
  const workflowMutex = new WorkflowMutex(pi);
127
- const helperVisibility = new PlanModeHelperVisibilityPolicy(pi);
128
126
  let workflowOwner: WorkflowMutexOwner | undefined;
129
127
  let currentSession: object | undefined;
130
128
  let currentSessionContext: ExtensionContext | undefined;
@@ -196,11 +194,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
196
194
  name: PLAN_MODE_QUESTION_TOOL_NAME,
197
195
  label: "Plan question",
198
196
  description:
199
- "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.",
200
- promptSnippet: "Ask user decision questions while Plan mode is active",
201
- promptGuidelines: [
202
- "In Plan mode, use plan_mode_question for important preferences, tradeoffs, or assumptions that cannot be discovered from read-only exploration.",
203
- ],
197
+ "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.",
204
198
  parameters: PLAN_MODE_QUESTION_PARAMS,
205
199
  async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
206
200
  if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
@@ -242,11 +236,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
242
236
  name: PLAN_MODE_COMPLETE_TOOL_NAME,
243
237
  label: "Complete plan",
244
238
  description:
245
- "Submit the complete decision-ready implementation plan for user review. Only available while Plan mode is active, and must be the final standalone action.",
246
- promptSnippet: "Submit the final Plan-mode implementation plan",
247
- promptGuidelines: [
248
- "Call plan_mode_complete alone as the final action only after the implementation plan is decision-complete.",
249
- ],
239
+ "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.",
250
240
  parameters: PLAN_MODE_COMPLETE_PARAMS,
251
241
  renderResult: renderPlanModeCompletion,
252
242
  async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
@@ -387,21 +377,15 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
387
377
  generation: number,
388
378
  ctx: ExtensionContext | undefined,
389
379
  showWarnings: boolean,
390
- applyWatchedVisibility = false,
391
380
  ) => {
392
381
  const loadedSettings = await readPlanModeRuntimeSettings();
393
382
  if (generation !== menuGeneration || menuController.signal.aborted) {
394
383
  return undefined;
395
384
  }
396
- const previousSettings = settings;
397
- const nextSettings =
385
+ settings =
398
386
  loadedSettings.kind === "loaded"
399
387
  ? loadedSettings.settings
400
388
  : ({ thinkingLevel: "inherit" } satisfies PlanModeSettings);
401
- if (applyWatchedVisibility && ctx) {
402
- applyWatchedHelperVisibility(previousSettings, nextSettings, ctx);
403
- }
404
- settings = nextSettings;
405
389
  applyPlanModeShortcut(configuredPlanModeToggleShortcut(settings));
406
390
  if (!ctx || !showWarnings) return loadedSettings;
407
391
  if (loadedSettings.kind === "invalid") {
@@ -429,7 +413,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
429
413
  }
430
414
  settingsReloadTimer = setTimeout(() => {
431
415
  settingsReloadTimer = undefined;
432
- void applyPlanModeSettings(generation, currentSessionContext, false, true);
416
+ void applyPlanModeSettings(generation, currentSessionContext, false);
433
417
  }, 75);
434
418
  };
435
419
 
@@ -456,7 +440,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
456
440
 
457
441
  pi.on("session_start", async (event, ctx) => {
458
442
  const generation = ++menuGeneration;
459
- const previousToolVisibility = configuredPlanModeToolVisibility(settings);
460
443
  finalizationRequest.reset();
461
444
  currentSession = ctx.sessionManager;
462
445
  currentSessionContext = ctx;
@@ -478,12 +461,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
478
461
  await applyPlanModeSettings(generation, ctx, true);
479
462
  if (generation !== menuGeneration || menuController.signal.aborted) return;
480
463
  startPlanModeSettingsWatch(generation);
481
- if (restoredState.enabled) {
482
- if (!installRestoredState(restoredState, ctx, previousToolVisibility)) return;
483
- } else {
484
- reconcileInactiveHelperVisibility(previousToolVisibility, ctx);
485
- if (!installRestoredState(restoredState, ctx)) return;
486
- }
464
+ if (!installRestoredState(restoredState, ctx)) return;
487
465
  implementationRetention.restore(state.activeImplementation);
488
466
  updateUi(ctx);
489
467
  });
@@ -601,14 +579,26 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
601
579
  reason: `Plan mode blocks tool '${event.toolName}' because its safe policy metadata is unavailable.`,
602
580
  };
603
581
  }
604
- if (event.toolName !== "bash") return;
605
-
606
- const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands);
607
- if (blocked !== undefined) {
608
- return {
609
- block: true,
610
- reason: `Plan mode blocks bash commands outside its reviewed inspection policy or containing explicitly unsafe arguments.\nBlocked command: ${blocked}`,
611
- };
582
+ if (event.toolName === "bash") {
583
+ const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands);
584
+ if (blocked !== undefined) {
585
+ return {
586
+ block: true,
587
+ reason: `Plan mode blocks bash commands outside its reviewed inspection policy or containing explicitly unsafe arguments.\nBlocked command: ${blocked}`,
588
+ };
589
+ }
590
+ }
591
+ if (event.toolName === "powershell") {
592
+ const blocked = findBlockedPowerShellCommandSegment(
593
+ readCommand(event.input),
594
+ settings.safeSubcommands,
595
+ );
596
+ if (blocked !== undefined) {
597
+ return {
598
+ block: true,
599
+ reason: `Plan mode blocks PowerShell commands outside its reviewed inspection policy or containing explicitly unsafe syntax.\nBlocked command: ${blocked}`,
600
+ };
601
+ }
612
602
  }
613
603
  });
614
604
 
@@ -737,16 +727,13 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
737
727
  workflowOwner = owner;
738
728
 
739
729
  const previousState = state;
740
- const previousHelperVisibility = helperVisibility.snapshot();
741
730
  try {
742
- helperVisibility.prepareActivation(ctx);
731
+ assertPlanModeHelperToolsAvailable(safeGetActiveTools());
743
732
  if (!publishModeContract("plan", ctx)) {
744
- helperVisibility.restore(previousHelperVisibility);
745
733
  releaseWorkflowOwner();
746
734
  return false;
747
735
  }
748
736
  } catch (error: unknown) {
749
- helperVisibility.restore(previousHelperVisibility);
750
737
  releaseWorkflowOwner();
751
738
  return reportHelperActivationFailure(ctx, error);
752
739
  }
@@ -768,7 +755,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
768
755
  updateUi(ctx);
769
756
  return true;
770
757
  } catch (error: unknown) {
771
- rollbackNewActivation(previousState, ctx, undefined, previousHelperVisibility);
758
+ rollbackNewActivation(previousState, ctx);
772
759
  throw error;
773
760
  }
774
761
  }
@@ -776,7 +763,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
776
763
  function enterPlanModeWithPrompt(prompt: string, ctx: ExtensionContext) {
777
764
  const previousState = state;
778
765
  const previousOwner = workflowOwner;
779
- const previousHelperVisibility = helperVisibility.snapshot();
780
766
  const wasEnabled = state.enabled;
781
767
  if (!enterPlanMode(ctx)) return;
782
768
  if (!wasEnabled) {
@@ -784,7 +770,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
784
770
  }
785
771
  if (sendPlanModeUserMessage(prompt, ctx)) return;
786
772
  if (wasEnabled) return;
787
- rollbackNewActivation(previousState, ctx, previousOwner, previousHelperVisibility);
773
+ rollbackNewActivation(previousState, ctx, previousOwner);
788
774
  }
789
775
 
790
776
  function exitPlanMode(ctx: ExtensionContext) {
@@ -1084,9 +1070,9 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1084
1070
  if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
1085
1071
  const tools = selectableTools();
1086
1072
  await ui.showPlanLaunchMenu(ctx, {
1087
- statusText: helperVisibility.toolsAvailable()
1088
- ? "Status: Off — Plan helper tools are visible for this runtime."
1089
- : "Status: Off — Plan helper tools load on the first Plan start.",
1073
+ statusText: planModeHelperToolsAvailable(safeGetActiveTools())
1074
+ ? "Status: Off — visible Plan helpers stay inactive until /plan starts."
1075
+ : "Status: Off — required Plan helpers are unavailable under the active tool policy.",
1090
1076
  initialScreen,
1091
1077
  getSelectedNames: () => snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot()),
1092
1078
  toolSummary: (selectedNames) => {
@@ -1183,7 +1169,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1183
1169
  signal,
1184
1170
  isCurrent,
1185
1171
  settingsPath: dependencies.settingsPath,
1186
- updateSettings: (patch, options) => updateSettingsWithRuntime(patch, options, ctx, isCurrent),
1172
+ updateSettings: dependencies.updateSettings ?? updatePlanModeSettings,
1187
1173
  onSaved: (saved) => {
1188
1174
  if (!isCurrent()) return;
1189
1175
  settings = saved;
@@ -1196,63 +1182,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1196
1182
  return result.kind === "closed" && "reason" in result && result.reason === "close";
1197
1183
  }
1198
1184
 
1199
- async function updateSettingsWithRuntime(
1200
- patch: PlanModeSettingsPatch,
1201
- options: UpdatePlanModeSettingsOptions | undefined,
1202
- ctx: ExtensionContext,
1203
- isCurrent: () => boolean,
1204
- ) {
1205
- const previousVisibility = configuredPlanModeToolVisibility(settings);
1206
- const nextVisibility = patch.toolVisibility ?? previousVisibility;
1207
- const persistSettings = dependencies.updateSettings ?? updatePlanModeSettings;
1208
- if (nextVisibility === previousVisibility) {
1209
- return persistSettings(patch, options);
1210
- }
1211
- if (!ctx.isIdle()) {
1212
- throw new Error("Wait for Pi to become idle before changing Plan tool visibility.");
1213
- }
1214
- bindWorkflowSessionIfNeeded(ctx);
1215
- const applicationSession = ctx.sessionManager;
1216
- const applicationGeneration = menuGeneration;
1217
- const applicationIsCurrent = () =>
1218
- currentSession === applicationSession &&
1219
- menuGeneration === applicationGeneration &&
1220
- isCurrent();
1221
- const retainedOwner = workflowMutex.isOwner(workflowOwner);
1222
- const temporaryOwner = retainedOwner ? workflowOwner : workflowMutex.acquire();
1223
- if (!temporaryOwner) {
1224
- throw new Error(
1225
- "Another workflow is active in this session. Plan tool visibility was not changed.",
1226
- );
1227
- }
1228
- const visibilitySnapshot = helperVisibility.snapshot();
1229
- try {
1230
- helperVisibility.applyVisibilityChange(previousVisibility, nextVisibility, ctx);
1231
- const saved = await persistSettings(patch, options);
1232
- if (!applicationIsCurrent()) {
1233
- throw new DOMException("Plan settings session replaced", "AbortError");
1234
- }
1235
- settings = saved;
1236
- applyPlanModeShortcut(configuredPlanModeToggleShortcut(saved));
1237
- return saved;
1238
- } catch (error) {
1239
- if (applicationIsCurrent()) {
1240
- try {
1241
- helperVisibility.restore(visibilitySnapshot);
1242
- } catch (rollbackError) {
1243
- throw new AggregateError(
1244
- [error, rollbackError],
1245
- "Plan tool visibility settings failed and runtime rollback was incomplete.",
1246
- );
1247
- }
1248
- }
1249
- throw error;
1250
- } finally {
1251
- if (!retainedOwner) workflowMutex.release(temporaryOwner);
1252
- if (!isCurrent()) latestCommandContext = undefined;
1253
- }
1254
- }
1255
-
1256
1185
  function allowModeTransition(ctx: ExtensionContext, action: string) {
1257
1186
  if (ctx.isIdle()) return true;
1258
1187
  const message = `Cannot ${action} while an agent run is active. Wait for the run to settle, then retry.`;
@@ -1292,82 +1221,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1292
1221
  activeToolBaseline = withRequiredPlanModeTools(safeGetActiveTools());
1293
1222
  }
1294
1223
 
1295
- function applyWatchedHelperVisibility(
1296
- previousSettings: PlanModeSettings,
1297
- nextSettings: PlanModeSettings,
1298
- ctx: ExtensionContext,
1299
- ) {
1300
- const previousVisibility = configuredPlanModeToolVisibility(previousSettings);
1301
- const nextVisibility = configuredPlanModeToolVisibility(nextSettings);
1302
- if (previousVisibility === nextVisibility) return;
1303
- if (state.enabled) {
1304
- helperVisibility.deferVisibilityChange(nextVisibility);
1305
- return;
1306
- }
1307
- try {
1308
- if (!ctx.isIdle()) {
1309
- helperVisibility.deferVisibilityChange(nextVisibility);
1310
- return;
1311
- }
1312
- } catch {
1313
- helperVisibility.deferVisibilityChange(nextVisibility);
1314
- return;
1315
- }
1316
- const owner = workflowMutex.acquire();
1317
- if (!owner) {
1318
- helperVisibility.deferVisibilityChange(nextVisibility);
1319
- return;
1320
- }
1321
- const snapshot = helperVisibility.snapshot();
1322
- try {
1323
- helperVisibility.applyVisibilityChange(previousVisibility, nextVisibility, ctx);
1324
- } catch (error: unknown) {
1325
- helperVisibility.restore(snapshot);
1326
- helperVisibility.deferVisibilityChange(nextVisibility);
1327
- if (ctx.hasUI) {
1328
- const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
1329
- ctx.ui.notify(
1330
- `Could not apply reloaded Plan tool visibility; the current tool envelope remains unchanged until a later safe boundary: ${detail}`,
1331
- "warning",
1332
- );
1333
- }
1334
- } finally {
1335
- workflowMutex.release(owner);
1336
- }
1337
- }
1338
-
1339
- function reconcileInactiveHelperVisibility(
1340
- previousVisibility: PlanModeToolVisibility,
1341
- ctx: ExtensionContext,
1342
- ) {
1343
- const owner = workflowMutex.acquire();
1344
- if (!owner) {
1345
- if (ctx.hasUI) {
1346
- ctx.ui.notify(
1347
- "Plan tool visibility was deferred because another workflow is active in this session.",
1348
- "warning",
1349
- );
1350
- }
1351
- return false;
1352
- }
1353
- const snapshot = helperVisibility.snapshot();
1354
- try {
1355
- const visibility = configuredPlanModeToolVisibility(settings);
1356
- helperVisibility.prepareSessionStart(visibility, previousVisibility);
1357
- helperVisibility.reconcileInactiveState(visibility);
1358
- return true;
1359
- } catch (error: unknown) {
1360
- helperVisibility.restore(snapshot);
1361
- if (ctx.hasUI) {
1362
- const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
1363
- ctx.ui.notify(`Could not apply Plan tool visibility: ${detail}`, "error");
1364
- }
1365
- return false;
1366
- } finally {
1367
- workflowMutex.release(owner);
1368
- }
1369
- }
1370
-
1371
1224
  function planModePolicyToolNames() {
1372
1225
  return workflowAllowedToolNames ?? computePlanModePolicyToolNames();
1373
1226
  }
@@ -1469,15 +1322,10 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1469
1322
  }
1470
1323
  }
1471
1324
 
1472
- function installRestoredState(
1473
- candidate: PlanModeState,
1474
- ctx: ExtensionContext,
1475
- previousToolVisibility?: PlanModeToolVisibility,
1476
- ) {
1325
+ function installRestoredState(candidate: PlanModeState, ctx: ExtensionContext) {
1477
1326
  const previousState = state;
1478
1327
  const previousWorkflowAllowedToolNames = workflowAllowedToolNames;
1479
1328
  const previousOwner = workflowOwner;
1480
- const previousHelperVisibility = helperVisibility.snapshot();
1481
1329
  const wasEnabled = state.enabled;
1482
1330
  if (candidate.enabled && !workflowMutex.isOwner(workflowOwner)) {
1483
1331
  const owner = workflowMutex.acquire();
@@ -1492,24 +1340,17 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1492
1340
 
1493
1341
  try {
1494
1342
  if (candidate.enabled) {
1495
- const visibility = configuredPlanModeToolVisibility(settings);
1496
- if (previousToolVisibility === undefined) {
1497
- helperVisibility.prepareActivation(ctx);
1498
- } else {
1499
- try {
1500
- helperVisibility.prepareSessionStart(visibility, previousToolVisibility);
1501
- helperVisibility.prepareActivation(ctx);
1502
- } catch {
1503
- helperVisibility.restore(previousHelperVisibility);
1504
- state = { enabled: false, awaitingAction: false };
1505
- workflowAllowedToolNames = undefined;
1506
- if (workflowOwner !== previousOwner) {
1507
- workflowMutex.release(workflowOwner);
1508
- workflowOwner = previousOwner;
1509
- }
1510
- reportRestoredHelpersUnavailable(ctx);
1511
- return false;
1343
+ try {
1344
+ assertPlanModeHelperToolsAvailable(safeGetActiveTools());
1345
+ } catch {
1346
+ state = { enabled: false, awaitingAction: false };
1347
+ workflowAllowedToolNames = undefined;
1348
+ if (workflowOwner !== previousOwner) {
1349
+ workflowMutex.release(workflowOwner);
1350
+ workflowOwner = previousOwner;
1512
1351
  }
1352
+ reportRestoredHelpersUnavailable(ctx);
1353
+ return false;
1513
1354
  }
1514
1355
  }
1515
1356
  if (wasEnabled && !candidate.enabled) {
@@ -1527,7 +1368,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1527
1368
  } finally {
1528
1369
  state = previousState;
1529
1370
  workflowAllowedToolNames = previousWorkflowAllowedToolNames;
1530
- helperVisibility.restore(previousHelperVisibility);
1531
1371
  if (workflowOwner !== previousOwner) {
1532
1372
  workflowMutex.release(workflowOwner);
1533
1373
  workflowOwner = previousOwner;
@@ -1541,7 +1381,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1541
1381
  previousState: PlanModeState,
1542
1382
  ctx: ExtensionContext,
1543
1383
  previousOwner?: WorkflowMutexOwner,
1544
- previousHelperVisibility?: PlanModeHelperVisibilitySnapshot,
1545
1384
  ) {
1546
1385
  const activatedOwner = workflowOwner;
1547
1386
  readyPresentationIntent = undefined;
@@ -1554,7 +1393,6 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1554
1393
  state = previousState;
1555
1394
  workflowAllowedToolNames = undefined;
1556
1395
  try {
1557
- if (previousHelperVisibility) helperVisibility.restore(previousHelperVisibility);
1558
1396
  persistState();
1559
1397
  updateUi(ctx);
1560
1398
  } finally {
@@ -1,8 +1,10 @@
1
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
1
+ import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
2
+ import { stripTerminalSequences, truncateToWidth } from "@earendil-works/pi-tui";
2
3
  import type { PlanModeState } from "./state.js";
3
4
 
4
5
  const STATUS_KEY = "plan-mode";
5
6
  const PLAN_WIDGET_KEY = "plan-mode-plan";
7
+ const BIDI_CONTROLS = /[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu;
6
8
 
7
9
  export function updatePlanModeUi(
8
10
  ctx: ExtensionContext,
@@ -10,30 +12,44 @@ export function updatePlanModeUi(
10
12
  toolSummary: () => string,
11
13
  ) {
12
14
  ctx.ui.setStatus(STATUS_KEY, formatStatus(state));
15
+ let lines: string[] | undefined;
13
16
  if (state.enabled && state.latestPlan) {
14
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
15
- "Proposed plan ready",
16
- "Use /plan to implement, save, revise, or exit Plan mode.",
17
- ]);
17
+ lines = ["Proposed plan ready", "Use /plan to implement, save, revise, or exit Plan mode."];
18
18
  } else if (state.enabled) {
19
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
19
+ lines = [
20
20
  "Plan mode: planning",
21
21
  toolSummary(),
22
22
  "Finish with plan_mode_complete when decision-ready.",
23
- ]);
23
+ ];
24
24
  } else if (state.savedPlan) {
25
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
26
- "Plan saved for later",
27
- "Use /plan to show, implement, or clear it.",
28
- ]);
25
+ lines = ["Plan saved for later", "Use /plan to show, implement, or clear it."];
29
26
  } else if (state.activeImplementation) {
30
- ctx.ui.setWidget(PLAN_WIDGET_KEY, [
31
- "Implementation plan active",
32
- "Use /plan to show, replace, or clear it.",
33
- ]);
34
- } else {
35
- ctx.ui.setWidget(PLAN_WIDGET_KEY, undefined);
27
+ lines = ["Implementation plan active", "Use /plan to show, replace, or clear it."];
28
+ }
29
+
30
+ publishPlanModeWidget(ctx, lines);
31
+ }
32
+
33
+ export function renderPlanModeWidget(
34
+ lines: readonly string[],
35
+ theme: Theme,
36
+ width: number,
37
+ ): string[] {
38
+ const renderWidth = Math.max(0, width);
39
+ return [
40
+ theme.fg("borderMuted", "─".repeat(renderWidth)),
41
+ ...lines.map((line) => truncateToWidth(sanitizePlanModeWidgetLine(line), renderWidth, "")),
42
+ ];
43
+ }
44
+
45
+ export function sanitizePlanModeWidgetLine(value: string): string {
46
+ let text = "";
47
+ for (const character of stripTerminalSequences(value).replace(BIDI_CONTROLS, "")) {
48
+ const codePoint = character.codePointAt(0) ?? 0;
49
+ const isControl = codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
50
+ text += isControl ? " " : character;
36
51
  }
52
+ return text;
37
53
  }
38
54
 
39
55
  export function clearPlanModeUi(ctx: ExtensionContext) {
@@ -97,6 +113,23 @@ export function planModeStatusText(state: PlanModeState, toolSummary: () => stri
97
113
  return "Plan mode is off.";
98
114
  }
99
115
 
116
+ function publishPlanModeWidget(ctx: ExtensionContext, lines: readonly string[] | undefined) {
117
+ if (!lines) {
118
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, undefined);
119
+ return;
120
+ }
121
+ if (ctx.mode !== "tui") {
122
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, [...lines]);
123
+ return;
124
+ }
125
+
126
+ const snapshot = [...lines];
127
+ ctx.ui.setWidget(PLAN_WIDGET_KEY, (_tui, theme) => ({
128
+ render: (width) => renderPlanModeWidget(snapshot, theme, width),
129
+ invalidate: () => {},
130
+ }));
131
+ }
132
+
100
133
  function formatStatus(state: PlanModeState) {
101
134
  if (state.enabled) {
102
135
  if (state.awaitingAction || state.latestPlan) return "plan ready";
@@ -2,6 +2,23 @@ import { PLAN_MODE_COMPLETE_TOOL_NAME } from "./completion-tool.js";
2
2
  import { PLAN_MODE_QUESTION_TOOL_NAME } from "./question-tool.js";
3
3
  import { unique } from "./tool-selection.js";
4
4
 
5
+ export const REQUIRED_PLAN_MODE_TOOL_NAMES = [
6
+ PLAN_MODE_QUESTION_TOOL_NAME,
7
+ PLAN_MODE_COMPLETE_TOOL_NAME,
8
+ ] as const;
9
+
10
+ export function planModeHelperToolsAvailable(toolNames: readonly string[]) {
11
+ const active = new Set(toolNames);
12
+ return REQUIRED_PLAN_MODE_TOOL_NAMES.every((name) => active.has(name));
13
+ }
14
+
15
+ export function assertPlanModeHelperToolsAvailable(toolNames: readonly string[]) {
16
+ if (planModeHelperToolsAvailable(toolNames)) return;
17
+ throw new Error(
18
+ "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",
19
+ );
20
+ }
21
+
5
22
  export function withRequiredPlanModeTools(toolNames: string[]) {
6
23
  return unique([
7
24
  ...withoutRequiredPlanModeTools(toolNames),
@@ -8,11 +8,9 @@ import {
8
8
  configuredImplementationPlanRetention,
9
9
  configuredPlanExportPath,
10
10
  configuredPlanModeToggleShortcut,
11
- configuredPlanModeToolVisibility,
12
11
  IMPLEMENTATION_PLAN_RETENTIONS,
13
12
  normalizeKeyId,
14
13
  PLAN_MODE_THINKING_LEVELS,
15
- PLAN_MODE_TOOL_VISIBILITIES,
16
14
  type PlanModeSettings,
17
15
  type PlanModeSettingsLoadResult,
18
16
  type PlanModeSettingsPatch,
@@ -49,7 +47,6 @@ export interface PlanModeSettingsMenuOptions {
49
47
  type Screen = "settings" | "tools" | "export" | "shortcut";
50
48
  type Action =
51
49
  | "set-thinking"
52
- | "set-tool-visibility"
53
50
  | "open-tools"
54
51
  | "toggle-tool"
55
52
  | "reset-tools"
@@ -143,17 +140,6 @@ export async function showPlanModeSettings(
143
140
  currentValue: configuredPlanModeToggleShortcut(state.settings) ?? "none",
144
141
  action: "open-shortcut",
145
142
  },
146
- {
147
- id: "toolVisibility",
148
- label: "Plan tools",
149
- description:
150
- "Keep Plan helper tools visible, or reveal them after the first plan.",
151
- currentValue: toolVisibilityLabel(
152
- configuredPlanModeToolVisibility(state.settings),
153
- ),
154
- values: PLAN_MODE_TOOL_VISIBILITIES.map(toolVisibilityLabel),
155
- action: "set-tool-visibility",
156
- },
157
143
  ],
158
144
  },
159
145
  tools: ({ state }) => ({
@@ -220,16 +206,6 @@ export async function showPlanModeSettings(
220
206
  `Plan mode thinking level: ${value}. Applies to the next Plan workflow.`,
221
207
  );
222
208
  },
223
- "set-tool-visibility": async ({ ctx: actionCtx, value, signal }) => {
224
- const toolVisibility = toolVisibilityFromLabel(value);
225
- if (!toolVisibility) return { kind: "rejected" };
226
- return savePatch(
227
- actionCtx,
228
- { toolVisibility },
229
- signal,
230
- `Plan tools: ${toolVisibilityLabel(toolVisibility)}.`,
231
- );
232
- },
233
209
  "open-tools": async () => ({ kind: "to", screen: "tools" }),
234
210
  "set-retention": async ({ ctx: actionCtx, value, signal }) => {
235
211
  const implementationPlanRetention = retentionFromLabel(value);
@@ -363,16 +339,6 @@ function retentionFromLabel(value: string | undefined) {
363
339
  return IMPLEMENTATION_PLAN_RETENTIONS.find((retention) => retentionLabel(retention) === value);
364
340
  }
365
341
 
366
- function toolVisibilityLabel(value: PlanModeSettings["toolVisibility"] | "after-first-plan") {
367
- return value === "always" ? "Always" : "After first plan";
368
- }
369
-
370
- function toolVisibilityFromLabel(value: string | undefined) {
371
- return PLAN_MODE_TOOL_VISIBILITIES.find(
372
- (visibility) => toolVisibilityLabel(visibility) === value,
373
- );
374
- }
375
-
376
342
  function defaultToolsValue(configured: string[] | undefined) {
377
343
  if (configured === undefined) return "Automatic safe built-ins";
378
344
  if (configured.length === 0) return "No optional tools";