@narumitw/pi-plan-mode 0.55.2 → 0.56.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.55.2",
3
+ "version": "0.56.0",
4
4
  "description": "Pi extension that adds a Codex-like read-only /plan collaboration mode.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,9 +23,6 @@
23
23
  "./dist/index.ts"
24
24
  ]
25
25
  },
26
- "piExtension": {
27
- "lifecycle": "stable"
28
- },
29
26
  "scripts": {
30
27
  "build": "node scripts/build-runtime.mjs",
31
28
  "check": "npm run build && biome check . && npm run typecheck",
@@ -38,9 +35,9 @@
38
35
  "@earendil-works/pi-tui": "*"
39
36
  },
40
37
  "devDependencies": {
41
- "@biomejs/biome": "2.5.10",
42
- "@earendil-works/pi-coding-agent": "0.84.3",
43
- "@earendil-works/pi-tui": "0.84.3",
38
+ "@biomejs/biome": "2.5.11",
39
+ "@earendil-works/pi-coding-agent": "0.84.4",
40
+ "@earendil-works/pi-tui": "0.84.4",
44
41
  "esbuild": "0.28.2",
45
42
  "typescript": "7.0.2"
46
43
  },
package/src/plan-mode.ts CHANGED
@@ -542,7 +542,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
542
542
  }
543
543
  });
544
544
 
545
- pi.on("tool_call", async (event) => {
545
+ pi.on("tool_call", async (event, ctx) => {
546
546
  const requiredHelper =
547
547
  event.toolName === PLAN_MODE_QUESTION_TOOL_NAME ||
548
548
  event.toolName === PLAN_MODE_COMPLETE_TOOL_NAME;
@@ -570,22 +570,45 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
570
570
  }
571
571
  if (requiredHelper) return;
572
572
 
573
+ const calledTool = toolByName(event.toolName);
574
+ const activeToolNames = new Set(safeGetActiveTools());
575
+ if (!calledTool) {
576
+ return {
577
+ block: true,
578
+ reason: activeToolNames.has(event.toolName)
579
+ ? `Plan mode blocks tool '${event.toolName}' because its safe policy metadata is unavailable.`
580
+ : `Plan mode blocks tool '${event.toolName}' because it is not registered or active. Register and activate it before starting the next Plan workflow.`,
581
+ };
582
+ }
583
+ if (classifyPlanModeTool(calledTool) === "blocked") {
584
+ return {
585
+ block: true,
586
+ reason: `Plan mode blocks tool '${event.toolName}' because its built-in policy is blocked and settings cannot enable it.`,
587
+ };
588
+ }
573
589
  const allowedToolNames = new Set(planModePolicyToolNames());
574
- if (!allowedToolNames.has(event.toolName)) {
590
+ if (!activeToolNames.has(event.toolName)) {
575
591
  return {
576
592
  block: true,
577
- reason: `Plan mode blocks tool '${event.toolName}' because it is unavailable or not selected by the Plan policy.`,
593
+ reason: allowedToolNames.has(event.toolName)
594
+ ? `Plan mode blocks tool '${event.toolName}' because it was admitted to the active Plan workflow but is currently inactive. Reactivate it to continue without restarting.`
595
+ : `Plan mode blocks tool '${event.toolName}' because it is registered but inactive. Activate it before starting the next Plan workflow.`,
578
596
  };
579
597
  }
580
- const calledTool = toolByName(event.toolName);
581
- if (!calledTool || classifyPlanModeTool(calledTool) === "blocked") {
598
+ if (!allowedToolNames.has(event.toolName)) {
582
599
  return {
583
600
  block: true,
584
- reason: `Plan mode blocks tool '${event.toolName}' because its safe policy metadata is unavailable.`,
601
+ reason: workflowDesiredToolNames().has(event.toolName)
602
+ ? `Plan mode blocks tool '${event.toolName}' because it was not available when the active Plan workflow froze its tool policy. Exit Plan mode, then start again after the tool is active.`
603
+ : `Plan mode blocks tool '${event.toolName}' because it is not selected by the Plan policy. Exit Plan mode, then enable it with /plan tools or defaultPlanTools before starting again.`,
585
604
  };
586
605
  }
587
606
  if (event.toolName === "bash") {
588
- const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands);
607
+ const blocked = findBlockedCommandSegment(
608
+ readCommand(event.input),
609
+ settings.safeSubcommands,
610
+ ctx.cwd,
611
+ );
589
612
  if (blocked !== undefined) {
590
613
  return {
591
614
  block: true,
@@ -597,6 +620,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
597
620
  const blocked = findBlockedPowerShellCommandSegment(
598
621
  readCommand(event.input),
599
622
  settings.safeSubcommands,
623
+ ctx.cwd,
600
624
  );
601
625
  if (blocked !== undefined) {
602
626
  return {
@@ -1284,6 +1308,14 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
1284
1308
  return computePlanModePolicyToolNames();
1285
1309
  }
1286
1310
 
1311
+ function workflowDesiredToolNames() {
1312
+ const policy = state.workflowToolPolicy;
1313
+ if (!state.enabled || !policy) return new Set<string>();
1314
+ return new Set(
1315
+ policy.kind === "automatic" ? automaticPlanModeToolNames() : (policy.desiredNames ?? []),
1316
+ );
1317
+ }
1318
+
1287
1319
  function beginWorkflowToolPolicy() {
1288
1320
  const kind = toolPolicySelectionIsExplicit() ? "explicit" : "automatic";
1289
1321
  const desiredNames = desiredPlanModeToolNames();
package/src/settings.ts CHANGED
@@ -4,13 +4,7 @@ import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";
4
4
  import { basename, dirname, join } from "node:path";
5
5
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
6
6
  import type { KeyId } from "@earendil-works/pi-tui";
7
- import {
8
- SAFE_GH_SUBCOMMAND_PATHS,
9
- SAFE_GIT_SUBCOMMANDS,
10
- type SafeGhSubcommandPath,
11
- type SafeGitSubcommand,
12
- type SafeSubcommands,
13
- } from "./tool-policy.js";
7
+ import type { SafeSubcommands } from "./tool-policy.js";
14
8
 
15
9
  export const PLAN_MODE_SETTINGS_FILE = "pi-plan-mode.json";
16
10
  const LEGACY_PLAN_MODE_SETTINGS_FILE = "plan-mode.json";
@@ -230,30 +224,26 @@ export function normalizeKeyId(value: unknown): KeyId | undefined {
230
224
 
231
225
  function normalizeSafeSubcommands(value: unknown): SafeSubcommands | undefined {
232
226
  if (!isSettingsDocument(value)) return undefined;
233
- if (Object.keys(value).some((key) => key !== "git" && key !== "gh")) return undefined;
234
-
235
- const safeSubcommands: SafeSubcommands = {};
236
- if (Object.hasOwn(value, "git")) {
237
- const git = normalizeKnownValues(Reflect.get(value, "git"), SAFE_GIT_SUBCOMMANDS);
238
- if (!git) return undefined;
239
- safeSubcommands.git = git as SafeGitSubcommand[];
240
- }
241
- if (Object.hasOwn(value, "gh")) {
242
- const gh = normalizeKnownValues(Reflect.get(value, "gh"), SAFE_GH_SUBCOMMAND_PATHS);
243
- if (!gh) return undefined;
244
- safeSubcommands.gh = gh as SafeGhSubcommandPath[];
245
- }
246
- return safeSubcommands;
247
- }
248
-
249
- function normalizeKnownValues(value: unknown, supported: readonly string[]) {
250
- if (
251
- !Array.isArray(value) ||
252
- !value.every((item): item is string => typeof item === "string" && supported.includes(item))
253
- ) {
254
- return undefined;
227
+ const entries: Array<[string, string[]]> = [];
228
+ for (const [command, subcommands] of Object.entries(value)) {
229
+ const normalizedCommand = command.trim();
230
+ if (
231
+ !normalizedCommand ||
232
+ !Array.isArray(subcommands) ||
233
+ !subcommands.every(
234
+ (item): item is string => typeof item === "string" && item.trim().length > 0,
235
+ )
236
+ ) {
237
+ return undefined;
238
+ }
239
+ const normalizedSubcommands = Array.from(
240
+ new Set(subcommands.map((subcommand) => subcommand.trim())),
241
+ );
242
+ const existing = entries.find(([existingCommand]) => existingCommand === normalizedCommand);
243
+ if (existing) existing[1] = Array.from(new Set([...existing[1], ...normalizedSubcommands]));
244
+ else entries.push([normalizedCommand, normalizedSubcommands]);
255
245
  }
256
- return Array.from(new Set(value));
246
+ return Object.fromEntries(entries);
257
247
  }
258
248
 
259
249
  export async function readPlanModeSettings(
@@ -1,3 +1,4 @@
1
+ import { isAbsolute, normalize } from "node:path";
1
2
  import type { ToolInfo } from "@earendil-works/pi-coding-agent";
2
3
 
3
4
  export const BUILTIN_SAFE_GIT_SUBCOMMANDS = [
@@ -29,8 +30,7 @@ export type ConfigurableSafeGitSubcommand = (typeof CONFIGURABLE_SAFE_GIT_SUBCOM
29
30
  export type SafeGitSubcommand = (typeof SAFE_GIT_SUBCOMMANDS)[number];
30
31
  export type SafeGhSubcommandPath = (typeof SAFE_GH_SUBCOMMAND_PATHS)[number];
31
32
  export interface SafeSubcommands {
32
- git?: SafeGitSubcommand[];
33
- gh?: SafeGhSubcommandPath[];
33
+ [command: string]: string[] | undefined;
34
34
  }
35
35
 
36
36
  export const SAFE_BUILTIN_PLAN_TOOLS = new Set([
@@ -146,27 +146,63 @@ export function readCommand(input: unknown) {
146
146
  export function findBlockedCommandSegment(
147
147
  command: string,
148
148
  safeSubcommands: SafeSubcommands = {},
149
+ workingDirectory?: string,
150
+ platform: NodeJS.Platform = process.platform,
149
151
  ): string | undefined {
152
+ if (matchesConfiguredSafeSubcommand(command, safeSubcommands)) return undefined;
150
153
  const segments = splitShellSegments(command);
151
154
  if (!segments || segments.length === 0) return command.trim() || "(empty command)";
152
- return segments.find((segment) => !isSafeSegment(segment, safeSubcommands));
155
+ return segments.find(
156
+ (segment) => !isSafeSegment(segment, safeSubcommands, workingDirectory, platform),
157
+ );
153
158
  }
154
159
 
155
- export function isSafeCommand(command: string, safeSubcommands: SafeSubcommands = {}) {
156
- return findBlockedCommandSegment(command, safeSubcommands) === undefined;
160
+ export function isSafeCommand(
161
+ command: string,
162
+ safeSubcommands: SafeSubcommands = {},
163
+ workingDirectory?: string,
164
+ platform: NodeJS.Platform = process.platform,
165
+ ) {
166
+ return (
167
+ findBlockedCommandSegment(command, safeSubcommands, workingDirectory, platform) === undefined
168
+ );
157
169
  }
158
170
 
159
171
  export function findBlockedPowerShellCommandSegment(
160
172
  command: string,
161
173
  safeSubcommands: SafeSubcommands = {},
174
+ workingDirectory?: string,
162
175
  ): string | undefined {
176
+ if (matchesConfiguredSafeSubcommand(command, safeSubcommands)) return undefined;
163
177
  const segments = splitPowerShellSegments(command);
164
178
  if (!segments || segments.length === 0) return command.trim() || "(empty command)";
165
- return segments.find((segment) => !isSafePowerShellSegment(segment, safeSubcommands));
179
+ return segments.find(
180
+ (segment) => !isSafePowerShellSegment(segment, safeSubcommands, workingDirectory),
181
+ );
182
+ }
183
+
184
+ export function isSafePowerShellCommand(
185
+ command: string,
186
+ safeSubcommands: SafeSubcommands = {},
187
+ workingDirectory?: string,
188
+ ) {
189
+ return (
190
+ findBlockedPowerShellCommandSegment(command, safeSubcommands, workingDirectory) === undefined
191
+ );
166
192
  }
167
193
 
168
- export function isSafePowerShellCommand(command: string, safeSubcommands: SafeSubcommands = {}) {
169
- return findBlockedPowerShellCommandSegment(command, safeSubcommands) === undefined;
194
+ function matchesConfiguredSafeSubcommand(command: string, safeSubcommands: SafeSubcommands) {
195
+ const candidate = command.trimStart();
196
+ return Object.entries(safeSubcommands).some(([configuredCommand, subcommands]) =>
197
+ subcommands?.some((subcommand) => {
198
+ const prefix = `${configuredCommand.trim()} ${subcommand.trim()}`;
199
+ if (!configuredCommand.trim() || !subcommand.trim() || !candidate.startsWith(prefix)) {
200
+ return false;
201
+ }
202
+ const boundary = candidate[prefix.length];
203
+ return boundary === undefined || /[\s;&|<>()]/.test(boundary);
204
+ }),
205
+ );
170
206
  }
171
207
 
172
208
  function splitPowerShellSegments(command: string): string[] | undefined {
@@ -216,15 +252,21 @@ function splitPowerShellSegments(command: string): string[] | undefined {
216
252
  return segments;
217
253
  }
218
254
 
219
- function isSafePowerShellSegment(segment: string, safeSubcommands: SafeSubcommands) {
255
+ function isSafePowerShellSegment(
256
+ segment: string,
257
+ safeSubcommands: SafeSubcommands,
258
+ workingDirectory?: string,
259
+ ) {
220
260
  const tokens = powerShellWords(segment);
221
261
  if (!tokens || tokens.length === 0 || tokens.includes("--%")) return false;
222
262
  const command = tokens[0]?.toLowerCase();
223
263
  if (!command) return false;
224
264
  const args = tokens.slice(1);
225
265
  if (READ_ONLY_POWERSHELL_COMMANDS.has(command)) return true;
266
+ if (command === "get-process") return isSafeGetProcessArguments(args);
267
+ if (command === "get-service") return isSafeGetServiceArguments(args);
226
268
  if (command !== "git" && command !== "gh") return false;
227
- return isSafeStructuredCommand(command, args, safeSubcommands);
269
+ return isSafeStructuredCommand(command, args, safeSubcommands, workingDirectory);
228
270
  }
229
271
 
230
272
  function powerShellWords(segment: string): string[] | undefined {
@@ -322,7 +364,12 @@ function splitShellSegments(command: string): string[] | undefined {
322
364
  return segments;
323
365
  }
324
366
 
325
- function isSafeSegment(segment: string, safeSubcommands: SafeSubcommands) {
367
+ function isSafeSegment(
368
+ segment: string,
369
+ safeSubcommands: SafeSubcommands,
370
+ workingDirectory: string | undefined,
371
+ platform: NodeJS.Platform,
372
+ ) {
326
373
  if (hasShellExpansion(segment) || /(^|\s)[A-Za-z_][A-Za-z0-9_]*=/.test(segment)) {
327
374
  return false;
328
375
  }
@@ -332,8 +379,10 @@ function isSafeSegment(segment: string, safeSubcommands: SafeSubcommands) {
332
379
  if (!command || MUTATING_COMMANDS.has(command)) return false;
333
380
  const args = tokens.slice(1);
334
381
  if (!hasSafeArguments(command, args)) return false;
382
+ if (command === "hostname") return args.length === 0;
383
+ if (command === "tasklist") return platform === "win32" && isSafeTasklistArguments(args);
335
384
  if (READ_ONLY_COMMANDS.has(command)) return true;
336
- return isSafeStructuredCommand(command, args, safeSubcommands);
385
+ return isSafeStructuredCommand(command, args, safeSubcommands, workingDirectory);
337
386
  }
338
387
 
339
388
  function hasShellExpansion(segment: string) {
@@ -510,8 +559,9 @@ function isSafeStructuredCommand(
510
559
  command: string,
511
560
  args: string[],
512
561
  safeSubcommands: SafeSubcommands,
562
+ workingDirectory?: string,
513
563
  ) {
514
- if (command === "git") return isSafeGitCommand(args, safeSubcommands);
564
+ if (command === "git") return isSafeGitCommand(args, safeSubcommands, workingDirectory);
515
565
  if (command === "gh") return isSafeGhCommand(args, safeSubcommands);
516
566
 
517
567
  const subcommandIndex = args.findIndex((argument) => !argument.startsWith("-"));
@@ -560,9 +610,79 @@ function isSafeStructuredCommand(
560
610
  return false;
561
611
  }
562
612
 
563
- function isSafeGitCommand(args: string[], safeSubcommands: SafeSubcommands) {
564
- let subcommandIndex = 0;
565
- while (args[subcommandIndex] === "--no-pager") subcommandIndex += 1;
613
+ function isSafeTasklistArguments(args: string[]) {
614
+ for (let index = 0; index < args.length; index += 1) {
615
+ const argument = args[index]?.toLowerCase();
616
+ if (["/v", "/nh", "/svc"].includes(argument ?? "")) continue;
617
+ if (argument === "/fo") {
618
+ const format = args[index + 1]?.toLowerCase();
619
+ if (!format || !["table", "list", "csv"].includes(format)) return false;
620
+ index += 1;
621
+ continue;
622
+ }
623
+ if (argument === "/fi") {
624
+ const filter = args[index + 1];
625
+ if (!filter || filter.startsWith("/")) return false;
626
+ index += 1;
627
+ continue;
628
+ }
629
+ if (argument === "/m") {
630
+ const module = args[index + 1];
631
+ if (module && !module.startsWith("/")) index += 1;
632
+ continue;
633
+ }
634
+ return false;
635
+ }
636
+ return true;
637
+ }
638
+
639
+ function isSafeGetProcessArguments(args: string[]) {
640
+ return hasSafePowerShellQueryArguments(
641
+ args,
642
+ new Set(["-module", "-fileversioninfo", "-includeusername"]),
643
+ new Set(["-name", "-id"]),
644
+ );
645
+ }
646
+
647
+ function isSafeGetServiceArguments(args: string[]) {
648
+ return hasSafePowerShellQueryArguments(
649
+ args,
650
+ new Set(["-dependentservices", "-requiredservices"]),
651
+ new Set(["-name", "-displayname", "-include", "-exclude"]),
652
+ );
653
+ }
654
+
655
+ function hasSafePowerShellQueryArguments(
656
+ args: string[],
657
+ switches: ReadonlySet<string>,
658
+ valueOptions: ReadonlySet<string>,
659
+ ) {
660
+ let positionalValues = 0;
661
+ for (let index = 0; index < args.length; index += 1) {
662
+ const argument = args[index];
663
+ const normalized = argument?.toLowerCase();
664
+ if (!argument || !normalized) return false;
665
+ if (!argument.startsWith("-")) {
666
+ positionalValues += 1;
667
+ if (positionalValues > 1) return false;
668
+ continue;
669
+ }
670
+ if (switches.has(normalized)) continue;
671
+ if (!valueOptions.has(normalized)) return false;
672
+ const value = args[index + 1];
673
+ if (!value || value.startsWith("-")) return false;
674
+ index += 1;
675
+ }
676
+ return true;
677
+ }
678
+
679
+ function isSafeGitCommand(
680
+ args: string[],
681
+ safeSubcommands: SafeSubcommands,
682
+ workingDirectory?: string,
683
+ ) {
684
+ const subcommandIndex = parseGitGlobalOptions(args, workingDirectory);
685
+ if (subcommandIndex === undefined) return false;
566
686
  const subcommand = args[subcommandIndex]?.toLowerCase();
567
687
  if (!subcommand || subcommand.startsWith("-")) return false;
568
688
  const subcommandArgs = args.slice(subcommandIndex + 1);
@@ -581,6 +701,28 @@ function isSafeGitCommand(args: string[], safeSubcommands: SafeSubcommands) {
581
701
  );
582
702
  }
583
703
 
704
+ function parseGitGlobalOptions(args: string[], workingDirectory?: string) {
705
+ let index = 0;
706
+ while (index < args.length) {
707
+ const argument = args[index];
708
+ if (argument === "--no-pager") {
709
+ index += 1;
710
+ continue;
711
+ }
712
+ if (argument !== "-C") break;
713
+ const directory = args[index + 1];
714
+ if (!directory || !isCurrentWorkingDirectory(directory, workingDirectory)) return undefined;
715
+ index += 2;
716
+ }
717
+ return index;
718
+ }
719
+
720
+ function isCurrentWorkingDirectory(directory: string, workingDirectory?: string) {
721
+ if (!workingDirectory || directory.split(/[\\/]+/u).includes("..")) return false;
722
+ if (normalize(directory) === ".") return true;
723
+ return isAbsolute(directory) && normalize(directory) === normalize(workingDirectory);
724
+ }
725
+
584
726
  function hasSafeGitArguments(subcommand: string, args: string[]) {
585
727
  return !args.some(
586
728
  (argument) =>