@narumitw/pi-plan-mode 0.55.0 → 0.55.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-plan-mode",
3
- "version": "0.55.0",
3
+ "version": "0.55.2",
4
4
  "description": "Pi extension that adds a Codex-like read-only /plan collaboration mode.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -50,6 +50,6 @@
50
50
  "directory": "packages/pi-plan-mode"
51
51
  },
52
52
  "dependencies": {
53
- "@narumitw/pi-tui-kit": "^0.58.0"
53
+ "@narumitw/pi-tui-kit": "^0.59.0"
54
54
  }
55
55
  }
@@ -3,6 +3,7 @@ import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
3
3
 
4
4
  export interface PlanLaunchTool {
5
5
  name: string;
6
+ label?: string;
6
7
  description: string;
7
8
  searchText: string;
8
9
  disabled: boolean;
@@ -26,6 +27,11 @@ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLau
26
27
  type Screen = "main" | "tools" | "help";
27
28
  type Action = "start" | "toggle-tool" | "start-with-tools" | "settings";
28
29
  const selectedNames = new Set(options.getSelectedNames());
30
+ const toolItems = options.tools.map((tool, index) => ({
31
+ id: `plan-tool:${index}`,
32
+ tool,
33
+ }));
34
+ const toolsByItemId = new Map(toolItems.map(({ id, tool }) => [id, tool]));
29
35
  let draftChanged = false;
30
36
  const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
31
37
  start: options.initialScreen ?? "main",
@@ -47,13 +53,15 @@ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLau
47
53
  title: "Choose Plan policy allowlist",
48
54
  lines: [
49
55
  "Policy changes apply only when you start Plan mode; first use may also reveal Plan helpers.",
50
- "Only tools already active in Pi can be allowed; non-built-ins run at user risk.",
56
+ options.toolSummary(selectedNames),
57
+ "Active tools can be chosen now; retained names resolve before the first request.",
58
+ "Plan mode never activates tools, and non-built-ins run at user risk.",
51
59
  ],
52
60
  enableSearch: true,
53
61
  viewportSize: 10,
54
- items: options.tools.map((tool) => ({
55
- id: tool.name,
56
- label: tool.name,
62
+ items: toolItems.map(({ id, tool }) => ({
63
+ id,
64
+ label: tool.label ?? tool.name,
57
65
  description: tool.description,
58
66
  searchText: tool.searchText,
59
67
  selected: selectedNames.has(tool.name),
@@ -89,7 +97,7 @@ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLau
89
97
  },
90
98
  "toggle-tool": async ({ itemId, selected, signal }) => {
91
99
  if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
92
- const tool = options.tools.find((candidate) => candidate.name === itemId);
100
+ const tool = itemId ? toolsByItemId.get(itemId) : undefined;
93
101
  if (!tool || tool.disabled) return { kind: "rejected" };
94
102
  if (selected) selectedNames.add(tool.name);
95
103
  else selectedNames.delete(tool.name);
package/src/plan-mode.ts CHANGED
@@ -64,7 +64,6 @@ import {
64
64
  import {
65
65
  assertPlanModeHelperToolsAvailable,
66
66
  planModeHelperToolsAvailable,
67
- withRequiredPlanModeTools,
68
67
  } from "./required-tools.js";
69
68
  import {
70
69
  preflightSavedPlanImplementation,
@@ -82,7 +81,12 @@ import {
82
81
  type UpdatePlanModeSettingsOptions,
83
82
  updatePlanModeSettings,
84
83
  } from "./settings.js";
85
- import { type PlanCompletionSource, type PlanModeState, restorePlanModeState } from "./state.js";
84
+ import {
85
+ type PlanCompletionSource,
86
+ type PlanModeState,
87
+ type PlanModeWorkflowToolPolicy,
88
+ restorePlanModeState,
89
+ } from "./state.js";
86
90
  import {
87
91
  canSelectToolInPlanMode,
88
92
  classifyPlanModeTool,
@@ -90,12 +94,7 @@ import {
90
94
  findBlockedPowerShellCommandSegment,
91
95
  readCommand,
92
96
  } from "./tool-policy.js";
93
- import {
94
- compareTools,
95
- filterAvailableSelectedToolNames,
96
- snapshotPlanModeSelectedNames,
97
- toolPolicyLabel,
98
- } from "./tool-selection.js";
97
+ import { compareTools, snapshotPlanModeSelectedNames, toolPolicyLabel } from "./tool-selection.js";
99
98
  import { WorkflowMutex, type WorkflowMutexOwner } from "./workflow-mutex.js";
100
99
 
101
100
  const STATE_ENTRY_TYPE = "plan-mode-state";
@@ -107,6 +106,10 @@ interface ReadyPresentationIntent {
107
106
  plan: string;
108
107
  source: PlanCompletionSource;
109
108
  }
109
+ interface PendingWorkflowToolPolicy {
110
+ generation: number;
111
+ mode: "resolve" | "revalidate";
112
+ }
110
113
  type InteractiveUi = typeof import("./interactive-ui.js");
111
114
 
112
115
  interface PlanModeDependencies {
@@ -142,8 +145,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
142
145
  let settings: PlanModeSettings = { thinkingLevel: "inherit" };
143
146
  let toggleShortcut: ReturnType<typeof configuredPlanModeToggleShortcut>;
144
147
  const clearPlanModeShortcutHandler = () => {};
145
- let activeToolBaseline: string[] = [];
146
148
  let workflowAllowedToolNames: string[] | undefined;
149
+ let pendingWorkflowToolPolicy: PendingWorkflowToolPolicy | undefined;
147
150
  let publishedContractMode: PlanModeContract | undefined;
148
151
  let modeContractsRelevant = false;
149
152
  let readyPresentationIntent: ReadyPresentationIntent | undefined;
@@ -445,13 +448,13 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
445
448
  currentSessionContext = ctx;
446
449
  workflowOwner = undefined;
447
450
  workflowMutex.bindSession(ctx.sessionManager);
448
- captureToolBaseline();
449
451
  refreshStateBeforeFirstAgentStart = event.reason === "new";
450
452
  menuController.abort(new DOMException("Plan-mode session replaced", "AbortError"));
451
453
  menuController = new AbortController();
452
454
  readyPresentationIntent = undefined;
453
455
  latestCommandContext = undefined;
454
456
  workflowAllowedToolNames = undefined;
457
+ pendingWorkflowToolPolicy = undefined;
455
458
  implementationRetention.reset();
456
459
  settings = { thinkingLevel: "inherit" };
457
460
  const branch = ctx.sessionManager.getBranch();
@@ -518,6 +521,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
518
521
  readyPresentationIntent = undefined;
519
522
  latestCommandContext = undefined;
520
523
  refreshStateBeforeFirstAgentStart = false;
524
+ workflowAllowedToolNames = undefined;
525
+ pendingWorkflowToolPolicy = undefined;
521
526
  implementationRetention.reset();
522
527
  await awaitPlanModeSettingsWrites(dependencies.settingsPath);
523
528
  if (currentSession !== undefined && currentSession !== shutdownSession) {
@@ -613,6 +618,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
613
618
  });
614
619
 
615
620
  pi.on("context", async (event, ctx) => {
621
+ resolvePendingWorkflowToolPolicy(ctx);
616
622
  const result = implementationRetention.transformContext(event.messages, state);
617
623
  if (result.clearActiveImplementationId) {
618
624
  clearActiveImplementation(result.clearActiveImplementationId, ctx);
@@ -749,7 +755,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
749
755
  selectedToolNames: candidate.selectedToolNames,
750
756
  selectedToolKeys: candidate.selectedToolKeys,
751
757
  };
752
- workflowAllowedToolNames = computePlanModePolicyToolNames();
758
+ beginWorkflowToolPolicy();
753
759
  applyPlanThinkingLevel();
754
760
  persistState();
755
761
  updateUi(ctx);
@@ -790,6 +796,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
790
796
  awaitingAction: false,
791
797
  savedPlan: undefined,
792
798
  activeImplementation: undefined,
799
+ workflowToolPolicy: undefined,
793
800
  manualThinkingLevel: undefined,
794
801
  };
795
802
  if (wasEnabled) {
@@ -940,6 +947,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
940
947
  awaitingAction: false,
941
948
  savedPlan: { plan, source },
942
949
  activeImplementation: undefined,
950
+ workflowToolPolicy: undefined,
943
951
  manualThinkingLevel: undefined,
944
952
  };
945
953
  restoreThinkingLevel();
@@ -1009,6 +1017,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1009
1017
  startedAt: Date.now(),
1010
1018
  retention,
1011
1019
  },
1020
+ workflowToolPolicy: undefined,
1012
1021
  manualThinkingLevel: undefined,
1013
1022
  };
1014
1023
  if (wasEnabled) {
@@ -1028,8 +1037,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1028
1037
  state = previousState;
1029
1038
  readyPresentationIntent = previousIntent;
1030
1039
  if (wasEnabled) {
1040
+ restoreWorkflowToolPolicy(state.workflowToolPolicy);
1031
1041
  publishModeContract("plan", ctx);
1032
- workflowAllowedToolNames = computePlanModePolicyToolNames();
1033
1042
  applyPlanThinkingLevel();
1034
1043
  }
1035
1044
  persistState();
@@ -1069,6 +1078,19 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1069
1078
  const ui = await loadInteractiveUi();
1070
1079
  if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
1071
1080
  const tools = selectableTools();
1081
+ const activeToolNames = new Set(safeGetActiveTools());
1082
+ const initialSelectedNames = snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot());
1083
+ const retainsInactiveSelection =
1084
+ state.selectedToolNames !== undefined ||
1085
+ state.selectedToolKeys !== undefined ||
1086
+ settings.defaultPlanTools !== undefined;
1087
+ const retainedInactiveNames = retainsInactiveSelection
1088
+ ? initialSelectedNames
1089
+ : new Set<string>();
1090
+ const registeredNames = new Set(tools.map((tool) => tool.name));
1091
+ const pendingNames = Array.from(retainedInactiveNames).filter(
1092
+ (name) => !registeredNames.has(name),
1093
+ );
1072
1094
  await ui.showPlanLaunchMenu(ctx, {
1073
1095
  statusText: planModeHelperToolsAvailable(safeGetActiveTools())
1074
1096
  ? "Status: Off — visible Plan helpers stay inactive until /plan starts."
@@ -1079,30 +1101,65 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1079
1101
  const allowed = tools
1080
1102
  .filter(
1081
1103
  (tool) =>
1082
- toolIsActive(tool.name) &&
1104
+ activeToolNames.has(tool.name) &&
1083
1105
  selectedNames.has(tool.name) &&
1084
1106
  canSelectToolInPlanMode(tool),
1085
1107
  )
1086
1108
  .map((tool) => tool.name);
1087
- return `Plan policy will allow: ${allowed.length > 0 ? allowed.join(", ") : "none"}.`;
1109
+ const pending = pendingNames
1110
+ .filter((name) => selectedNames.has(name))
1111
+ .map(terminalToolName);
1112
+ const visiblePending = pending.slice(0, 3);
1113
+ const pendingSuffix =
1114
+ pending.length > visiblePending.length
1115
+ ? `, +${pending.length - visiblePending.length} more`
1116
+ : "";
1117
+ return [
1118
+ `Plan policy will allow: ${allowed.length > 0 ? allowed.join(", ") : "none"}.`,
1119
+ ...(pending.length > 0
1120
+ ? [`Pending registration: ${visiblePending.join(", ")}${pendingSuffix}.`]
1121
+ : []),
1122
+ ].join(" ");
1088
1123
  },
1089
- tools: tools.map((tool) => {
1090
- const selectable = canSelectToolInPlanMode(tool);
1091
- const active = toolIsActive(tool.name);
1092
- const policy = active ? toolPolicyLabel(tool) : "not active in this Pi session";
1093
- const description = tool.description ?? "No description available";
1094
- return {
1095
- name: tool.name,
1096
- description: `${policy} · ${description}`,
1097
- searchText: [policy, description].join(" "),
1098
- disabled: !selectable || !active,
1099
- disabledReason: !active
1100
- ? "Not active in Pi; Plan mode will not activate it"
1101
- : selectable
1102
- ? undefined
1103
- : "Blocked by Plan-mode policy",
1104
- };
1105
- }),
1124
+ tools: [
1125
+ ...tools.map((tool) => {
1126
+ const selectable = canSelectToolInPlanMode(tool);
1127
+ const active = activeToolNames.has(tool.name);
1128
+ const retained = retainedInactiveNames.has(tool.name);
1129
+ const policy = active
1130
+ ? toolPolicyLabel(tool)
1131
+ : retained
1132
+ ? "not active yet; retained for first-request resolution"
1133
+ : "not active in this Pi session";
1134
+ const description = tool.description ?? "No description available";
1135
+ return {
1136
+ name: tool.name,
1137
+ description: `${policy} · ${description}`,
1138
+ searchText: [policy, description].join(" "),
1139
+ disabled: !selectable || !active,
1140
+ disabledReason: !active
1141
+ ? retained
1142
+ ? "Not active yet; retained and resolved before the first request"
1143
+ : "Not active in Pi; Plan mode will not activate it"
1144
+ : selectable
1145
+ ? undefined
1146
+ : "Blocked by Plan-mode policy",
1147
+ };
1148
+ }),
1149
+ ...pendingNames.map((name) => {
1150
+ const label = terminalToolName(name);
1151
+ return {
1152
+ name,
1153
+ label,
1154
+ description:
1155
+ "pending registration · Retained and resolved before the first Plan request",
1156
+ searchText: `${label} pending registration retained first Plan request`,
1157
+ disabled: true,
1158
+ disabledReason:
1159
+ "Not registered yet; Plan mode will not activate it and will resolve it before the first request",
1160
+ };
1161
+ }),
1162
+ ],
1106
1163
  ...lifecycle,
1107
1164
  start: (signal) => {
1108
1165
  if (signal.aborted || !lifecycle.isCurrent()) return;
@@ -1115,7 +1172,11 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1115
1172
  },
1116
1173
  startWithTools: (names, signal) => {
1117
1174
  if (signal.aborted || !lifecycle.isCurrent()) return;
1118
- const selectedToolNames = filterAvailableSelectedToolNames(names, activePlanPolicyTools());
1175
+ const selectedToolNames = Array.from(
1176
+ new Set(
1177
+ names.filter((name) => activeToolNames.has(name) || retainedInactiveNames.has(name)),
1178
+ ),
1179
+ );
1119
1180
  if (enterPlanMode(ctx, { selectedToolNames, selectedToolKeys: undefined })) {
1120
1181
  ctx.ui.notify("Plan mode enabled with the selected tools.", "info");
1121
1182
  }
@@ -1165,7 +1226,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1165
1226
  if (!isCurrent() || signal.aborted) return false;
1166
1227
  const result = await ui.showPlanModeSettings(ctx, {
1167
1228
  tools: selectableTools(),
1168
- activeToolNames: activeToolBaseline,
1229
+ activeToolNames: safeGetActiveTools(),
1169
1230
  signal,
1170
1231
  isCurrent,
1171
1232
  settingsPath: dependencies.settingsPath,
@@ -1192,6 +1253,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1192
1253
 
1193
1254
  function advanceWorkflowGeneration() {
1194
1255
  workflowGeneration += 1;
1256
+ pendingWorkflowToolPolicy = undefined;
1195
1257
  finalizationRequest.reset();
1196
1258
  }
1197
1259
 
@@ -1217,18 +1279,143 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1217
1279
  };
1218
1280
  }
1219
1281
 
1220
- function captureToolBaseline() {
1221
- activeToolBaseline = withRequiredPlanModeTools(safeGetActiveTools());
1282
+ function planModePolicyToolNames() {
1283
+ if (state.enabled) return workflowAllowedToolNames ?? [];
1284
+ return computePlanModePolicyToolNames();
1222
1285
  }
1223
1286
 
1224
- function planModePolicyToolNames() {
1225
- return workflowAllowedToolNames ?? computePlanModePolicyToolNames();
1287
+ function beginWorkflowToolPolicy() {
1288
+ const kind = toolPolicySelectionIsExplicit() ? "explicit" : "automatic";
1289
+ const desiredNames = desiredPlanModeToolNames();
1290
+ const allowedNames = resolvePlanModePolicyToolNames(desiredNames);
1291
+ const policy: PlanModeWorkflowToolPolicy = {
1292
+ kind,
1293
+ ...(kind === "explicit" ? { desiredNames } : {}),
1294
+ allowedNames,
1295
+ resolved: false,
1296
+ };
1297
+ state = { ...state, workflowToolPolicy: policy };
1298
+ pendingWorkflowToolPolicy = { generation: workflowGeneration, mode: "resolve" };
1299
+ workflowAllowedToolNames = allowedNames;
1226
1300
  }
1227
1301
 
1228
- function computePlanModePolicyToolNames() {
1302
+ function resolvePendingWorkflowToolPolicy(ctx: ExtensionContext) {
1303
+ const pending = pendingWorkflowToolPolicy;
1304
+ if (!pending) return;
1305
+ if (
1306
+ pending.generation !== workflowGeneration ||
1307
+ !state.enabled ||
1308
+ !workflowMutex.isOwner(workflowOwner)
1309
+ ) {
1310
+ pendingWorkflowToolPolicy = undefined;
1311
+ return;
1312
+ }
1313
+ const policy = state.workflowToolPolicy;
1314
+ const expectedResolved = pending.mode === "revalidate";
1315
+ if (!policy || policy.resolved !== expectedResolved) {
1316
+ pendingWorkflowToolPolicy = undefined;
1317
+ return;
1318
+ }
1319
+ const allowedNames =
1320
+ pending.mode === "resolve"
1321
+ ? resolveWorkflowToolPolicy(policy)
1322
+ : revalidateFrozenWorkflowToolPolicy(policy);
1323
+ const policyChanged = !policy.resolved || !arrayEquals(policy.allowedNames, allowedNames);
1324
+ workflowAllowedToolNames = allowedNames;
1325
+ state = {
1326
+ ...state,
1327
+ workflowToolPolicy: { ...policy, allowedNames, resolved: true },
1328
+ };
1329
+ pendingWorkflowToolPolicy = undefined;
1330
+ if (policyChanged) persistState();
1331
+ updateUi(ctx);
1332
+ }
1333
+
1334
+ function toolPolicySelectionIsExplicit() {
1335
+ return (
1336
+ state.selectedToolNames !== undefined ||
1337
+ state.selectedToolKeys !== undefined ||
1338
+ settings.defaultPlanTools !== undefined
1339
+ );
1340
+ }
1341
+
1342
+ function desiredPlanModeToolNames() {
1229
1343
  const tools = activePlanPolicyTools();
1230
- const selectedNames = snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot());
1231
- return tools
1344
+ return Array.from(snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot()));
1345
+ }
1346
+
1347
+ function automaticPlanModeToolNames() {
1348
+ return Array.from(snapshotPlanModeSelectedNames(activePlanPolicyTools(), {}));
1349
+ }
1350
+
1351
+ function resolveWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy) {
1352
+ return resolvePlanModePolicyToolNames(
1353
+ policy.kind === "automatic" ? automaticPlanModeToolNames() : (policy.desiredNames ?? []),
1354
+ );
1355
+ }
1356
+
1357
+ function revalidateFrozenWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy) {
1358
+ const currentlyAllowed = new Set(
1359
+ policy.kind === "automatic"
1360
+ ? resolvePlanModePolicyToolNames(automaticPlanModeToolNames())
1361
+ : resolvePlanModePolicyToolNames(policy.allowedNames),
1362
+ );
1363
+ return policy.allowedNames.filter((name) => currentlyAllowed.has(name));
1364
+ }
1365
+
1366
+ function restoreWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy | undefined) {
1367
+ let nextPolicy: PlanModeWorkflowToolPolicy;
1368
+ if (!policy) {
1369
+ const kind = toolPolicySelectionIsExplicit() ? "explicit" : "automatic";
1370
+ const desiredNames = desiredPlanModeToolNames();
1371
+ nextPolicy = {
1372
+ kind,
1373
+ ...(kind === "explicit" ? { desiredNames } : {}),
1374
+ allowedNames: resolvePlanModePolicyToolNames(desiredNames),
1375
+ resolved: true,
1376
+ };
1377
+ } else if (policy.resolved) {
1378
+ nextPolicy = policy;
1379
+ } else {
1380
+ nextPolicy = {
1381
+ ...policy,
1382
+ allowedNames: resolveWorkflowToolPolicy(policy),
1383
+ };
1384
+ }
1385
+ state = { ...state, workflowToolPolicy: nextPolicy };
1386
+ workflowAllowedToolNames = nextPolicy.resolved
1387
+ ? revalidateFrozenWorkflowToolPolicy(nextPolicy)
1388
+ : nextPolicy.allowedNames;
1389
+ pendingWorkflowToolPolicy = {
1390
+ generation: workflowGeneration,
1391
+ mode: nextPolicy.resolved ? "revalidate" : "resolve",
1392
+ };
1393
+ return !workflowToolPoliciesEqual(policy, nextPolicy);
1394
+ }
1395
+
1396
+ function workflowToolPoliciesEqual(
1397
+ left: PlanModeWorkflowToolPolicy | undefined,
1398
+ right: PlanModeWorkflowToolPolicy,
1399
+ ) {
1400
+ return (
1401
+ left?.kind === right.kind &&
1402
+ left.resolved === right.resolved &&
1403
+ arrayEquals(left.allowedNames, right.allowedNames) &&
1404
+ arrayEquals(left.desiredNames ?? [], right.desiredNames ?? [])
1405
+ );
1406
+ }
1407
+
1408
+ function arrayEquals(left: readonly string[], right: readonly string[]) {
1409
+ return left.length === right.length && left.every((value, index) => value === right[index]);
1410
+ }
1411
+
1412
+ function computePlanModePolicyToolNames() {
1413
+ return resolvePlanModePolicyToolNames(desiredPlanModeToolNames());
1414
+ }
1415
+
1416
+ function resolvePlanModePolicyToolNames(desiredNames: readonly string[]) {
1417
+ const selectedNames = new Set(desiredNames);
1418
+ return activePlanPolicyTools()
1232
1419
  .filter((tool) => selectedNames.has(tool.name) && canSelectToolInPlanMode(tool))
1233
1420
  .map((tool) => tool.name);
1234
1421
  }
@@ -1251,14 +1438,10 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1251
1438
  }
1252
1439
 
1253
1440
  function activePlanPolicyTools() {
1254
- const activeNames = new Set(activeToolBaseline);
1441
+ const activeNames = new Set(safeGetActiveTools());
1255
1442
  return selectableTools().filter((tool) => activeNames.has(tool.name));
1256
1443
  }
1257
1444
 
1258
- function toolIsActive(toolName: string) {
1259
- return activeToolBaseline.includes(toolName);
1260
- }
1261
-
1262
1445
  function safeGetAllTools() {
1263
1446
  try {
1264
1447
  return pi.getAllTools();
@@ -1325,6 +1508,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1325
1508
  function installRestoredState(candidate: PlanModeState, ctx: ExtensionContext) {
1326
1509
  const previousState = state;
1327
1510
  const previousWorkflowAllowedToolNames = workflowAllowedToolNames;
1511
+ const previousPendingWorkflowToolPolicy = pendingWorkflowToolPolicy;
1328
1512
  const previousOwner = workflowOwner;
1329
1513
  const wasEnabled = state.enabled;
1330
1514
  if (candidate.enabled && !workflowMutex.isOwner(workflowOwner)) {
@@ -1332,6 +1516,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1332
1516
  if (!owner) {
1333
1517
  state = { enabled: false, awaitingAction: false };
1334
1518
  workflowAllowedToolNames = undefined;
1519
+ pendingWorkflowToolPolicy = undefined;
1335
1520
  reportRestoredWorkflowBusy(ctx);
1336
1521
  return false;
1337
1522
  }
@@ -1345,6 +1530,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1345
1530
  } catch {
1346
1531
  state = { enabled: false, awaitingAction: false };
1347
1532
  workflowAllowedToolNames = undefined;
1533
+ pendingWorkflowToolPolicy = undefined;
1348
1534
  if (workflowOwner !== previousOwner) {
1349
1535
  workflowMutex.release(workflowOwner);
1350
1536
  workflowOwner = previousOwner;
@@ -1358,7 +1544,14 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1358
1544
  restoreThinkingLevel();
1359
1545
  }
1360
1546
  state = candidate;
1361
- workflowAllowedToolNames = state.enabled ? computePlanModePolicyToolNames() : undefined;
1547
+ const policyChanged = state.enabled
1548
+ ? restoreWorkflowToolPolicy(state.workflowToolPolicy)
1549
+ : false;
1550
+ if (!state.enabled) {
1551
+ workflowAllowedToolNames = undefined;
1552
+ pendingWorkflowToolPolicy = undefined;
1553
+ }
1554
+ if (policyChanged) persistState();
1362
1555
  if (state.enabled) applyPlanThinkingLevel();
1363
1556
  else if (wasEnabled) releaseWorkflowOwner();
1364
1557
  return true;
@@ -1368,6 +1561,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1368
1561
  } finally {
1369
1562
  state = previousState;
1370
1563
  workflowAllowedToolNames = previousWorkflowAllowedToolNames;
1564
+ pendingWorkflowToolPolicy = previousPendingWorkflowToolPolicy;
1371
1565
  if (workflowOwner !== previousOwner) {
1372
1566
  workflowMutex.release(workflowOwner);
1373
1567
  workflowOwner = previousOwner;
@@ -1392,6 +1586,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1392
1586
  } finally {
1393
1587
  state = previousState;
1394
1588
  workflowAllowedToolNames = undefined;
1589
+ pendingWorkflowToolPolicy = undefined;
1395
1590
  try {
1396
1591
  persistState();
1397
1592
  updateUi(ctx);
@@ -1473,6 +1668,11 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1473
1668
  return safeGetAllTools().find((candidate) => candidate.name === toolName);
1474
1669
  }
1475
1670
 
1671
+ function terminalToolName(value: string) {
1672
+ const safe = safeTerminalText(value) || "(unnamed tool)";
1673
+ return safe.length > 120 ? `${safe.slice(0, 119)}…` : safe;
1674
+ }
1675
+
1476
1676
  function safeTerminalText(value: string) {
1477
1677
  return [...value]
1478
1678
  .map((character) => {
@@ -70,6 +70,10 @@ export async function showPlanModeSettings(
70
70
  (tool) =>
71
71
  tool.name !== PLAN_MODE_QUESTION_TOOL_NAME && tool.name !== PLAN_MODE_COMPLETE_TOOL_NAME,
72
72
  );
73
+ const toolItemIds = new Map(
74
+ tools.map((tool, index) => [tool.name, `plan-settings-tool:${index}`]),
75
+ );
76
+ const toolsByItemId = new Map(tools.map((tool) => [toolItemIds.get(tool.name) as string, tool]));
73
77
 
74
78
  const loadState = async (): Promise<SettingsMenuState> => {
75
79
  const loaded = await readSettings(options.settingsPath);
@@ -111,7 +115,7 @@ export async function showPlanModeSettings(
111
115
  id: "defaultPlanTools",
112
116
  label: "Plan policy tools",
113
117
  description:
114
- "Choose which already-active tools Plan mode may execute by default.",
118
+ "Choose active tools or retain names to resolve before the first request.",
115
119
  currentValue: defaultToolsValue(state.settings.defaultPlanTools),
116
120
  action: "open-tools",
117
121
  },
@@ -147,11 +151,17 @@ export async function showPlanModeSettings(
147
151
  title: "Default Plan policy allowlist",
148
152
  lines: [
149
153
  "Changes apply when a later Plan workflow starts; model-visible tools stay unchanged.",
150
- "Only tools already active in Pi can be allowed; non-built-ins run at user risk.",
154
+ "Retained inactive names resolve before that workflow's first request.",
155
+ "Plan mode never activates tools, and non-built-ins run at user risk.",
151
156
  ],
152
157
  enableSearch: true,
153
158
  viewportSize: 10,
154
- items: defaultToolItems(tools, state.settings.defaultPlanTools, activeToolNames),
159
+ items: defaultToolItems(
160
+ tools,
161
+ state.settings.defaultPlanTools,
162
+ activeToolNames,
163
+ toolItemIds,
164
+ ),
155
165
  action: "toggle-tool",
156
166
  actions: [
157
167
  {
@@ -252,7 +262,7 @@ export async function showPlanModeSettings(
252
262
  return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
253
263
  },
254
264
  "toggle-tool": async ({ ctx: actionCtx, state, itemId, selected, signal }) => {
255
- const tool = tools.find((candidate) => candidate.name === itemId);
265
+ const tool = itemId ? toolsByItemId.get(itemId) : undefined;
256
266
  if (!tool || !activeToolNames.has(tool.name) || !canSelectToolInPlanMode(tool)) {
257
267
  return { kind: "rejected" };
258
268
  }
@@ -349,38 +359,46 @@ function defaultToolItems(
349
359
  tools: readonly ToolInfo[],
350
360
  configured: string[] | undefined,
351
361
  activeToolNames: ReadonlySet<string>,
362
+ toolItemIds: ReadonlyMap<string, string>,
352
363
  ) {
353
364
  const selected = new Set(explicitToolNames(tools, configured));
354
365
  const availableNames = new Set(tools.map((tool) => tool.name));
355
366
  const items = tools.map((tool) => {
356
367
  const active = activeToolNames.has(tool.name);
357
368
  const selectable = active && canSelectToolInPlanMode(tool);
358
- const policy = active ? toolPolicyLabel(tool) : "not active in this Pi session";
369
+ const policy = active
370
+ ? toolPolicyLabel(tool)
371
+ : selected.has(tool.name)
372
+ ? "not active yet; retained for first-request resolution"
373
+ : "not active in this Pi session";
359
374
  const description = tool.description ?? "No description available";
360
375
  return {
361
- id: tool.name,
376
+ id: toolItemIds.get(tool.name) as string,
362
377
  label: tool.name,
363
378
  description: `${policy} · ${description}`,
364
379
  searchText: `${policy} ${description}`,
365
380
  selected: selected.has(tool.name),
366
381
  disabled: !selectable,
367
382
  disabledReason: !active
368
- ? "Not active in Pi; Plan mode will not activate it"
383
+ ? selected.has(tool.name)
384
+ ? "Not active yet; retained and resolved before the first request"
385
+ : "Not active in Pi; Plan mode will not activate it"
369
386
  : selectable
370
387
  ? undefined
371
388
  : "Blocked by Plan-mode policy",
372
389
  };
373
390
  });
374
- for (const name of configured ?? []) {
391
+ for (const [index, name] of (configured ?? []).entries()) {
375
392
  if (availableNames.has(name)) continue;
393
+ const label = terminalToolName(name);
376
394
  items.push({
377
- id: name,
378
- label: name,
379
- description: "unavailable · Retained in settings but unavailable in this session",
380
- searchText: "unavailable retained settings",
395
+ id: `plan-settings-pending:${index}`,
396
+ label,
397
+ description: "pending registration · Retained and resolved before the first request",
398
+ searchText: `${label} pending registration retained settings first request`,
381
399
  selected: true,
382
400
  disabled: true,
383
- disabledReason: "Unavailable in this session; reset defaults to remove unavailable names",
401
+ disabledReason: "Not registered yet; reset defaults to remove retained names",
384
402
  });
385
403
  }
386
404
  return items;
@@ -392,6 +410,11 @@ function explicitToolNames(tools: readonly ToolInfo[], configured: string[] | un
392
410
  : [...configured];
393
411
  }
394
412
 
413
+ function terminalToolName(value: string) {
414
+ const safe = safeTerminalText(value) || "(unnamed tool)";
415
+ return safe.length > 120 ? `${safe.slice(0, 119)}…` : safe;
416
+ }
417
+
395
418
  function safeTerminalText(value: string) {
396
419
  return [...value]
397
420
  .map((character) => {