@narumitw/pi-plan-mode 0.47.1 → 0.49.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/src/settings.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { access, readFile } from "node:fs/promises";
2
- import { join } from "node:path";
1
+ import { randomUUID } from "node:crypto";
2
+ import { constants } from "node:fs";
3
+ import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";
4
+ import { basename, dirname, join } from "node:path";
3
5
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
4
6
  import {
5
7
  SAFE_GH_SUBCOMMAND_PATHS,
@@ -11,6 +13,7 @@ import {
11
13
 
12
14
  export const PLAN_MODE_SETTINGS_FILE = "pi-plan-mode.json";
13
15
  const LEGACY_PLAN_MODE_SETTINGS_FILE = "plan-mode.json";
16
+ const MAX_SETTINGS_BYTES = 64 * 1024;
14
17
  export const PLAN_MODE_THINKING_LEVELS = [
15
18
  "inherit",
16
19
  "off",
@@ -21,21 +24,59 @@ export const PLAN_MODE_THINKING_LEVELS = [
21
24
  "xhigh",
22
25
  "max",
23
26
  ] as const;
27
+ export const IMPLEMENTATION_PLAN_RETENTIONS = [
28
+ "keep",
29
+ "clear-on-start",
30
+ "clear-after-first-run",
31
+ ] as const;
32
+ export const DEFAULT_PLAN_EXPORT_PATH = "PLAN.md";
33
+ const MAX_PLAN_EXPORT_PATH_LENGTH = 4096;
24
34
 
25
35
  export type PlanModeThinkingLevel = (typeof PLAN_MODE_THINKING_LEVELS)[number];
36
+ export type ImplementationPlanRetention = (typeof IMPLEMENTATION_PLAN_RETENTIONS)[number];
26
37
  export type PlanModeFixedThinkingLevel = Exclude<PlanModeThinkingLevel, "inherit">;
27
38
  export interface PlanModeSettings {
28
39
  thinkingLevel: PlanModeThinkingLevel;
29
40
  defaultPlanTools?: string[];
41
+ implementationPlanRetention?: ImplementationPlanRetention;
42
+ defaultPlanExportPath?: string;
30
43
  safeSubcommands?: SafeSubcommands;
31
44
  }
45
+ export interface PlanModeSettingsPatch {
46
+ thinkingLevel?: PlanModeThinkingLevel;
47
+ defaultPlanTools?: readonly string[] | null;
48
+ implementationPlanRetention?: ImplementationPlanRetention;
49
+ defaultPlanExportPath?: string | null;
50
+ }
51
+ export interface UpdatePlanModeSettingsOptions {
52
+ settingsPath?: string;
53
+ legacySettingsPath?: string;
54
+ signal?: AbortSignal;
55
+ beforeRename?: (temporaryPath: string, settingsPath: string) => Promise<void>;
56
+ }
32
57
  export type PlanModeSettingsLoadResult =
33
58
  | { kind: "missing"; notice?: string }
34
59
  | { kind: "invalid"; reason: string; notice?: string }
35
60
  | { kind: "loaded"; settings: PlanModeSettings; notice?: string };
36
61
 
62
+ type SettingsDocument = Record<string, unknown>;
63
+ type SettingsSnapshot = {
64
+ result: PlanModeSettingsLoadResult;
65
+ document?: SettingsDocument;
66
+ };
67
+
68
+ const mutationQueues = new Map<string, Promise<void>>();
69
+
70
+ export function planModeSettingsPath() {
71
+ return join(getAgentDir(), PLAN_MODE_SETTINGS_FILE);
72
+ }
73
+
74
+ function legacyPlanModeSettingsPath() {
75
+ return join(getAgentDir(), LEGACY_PLAN_MODE_SETTINGS_FILE);
76
+ }
77
+
37
78
  export function normalizePlanModeSettings(value: unknown): PlanModeSettings | undefined {
38
- if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
79
+ if (!isSettingsDocument(value)) return undefined;
39
80
  const thinkingLevel = Object.hasOwn(value, "thinkingLevel")
40
81
  ? Reflect.get(value, "thinkingLevel")
41
82
  : "inherit";
@@ -50,6 +91,25 @@ export function normalizePlanModeSettings(value: unknown): PlanModeSettings | un
50
91
  if (!defaultPlanTools) return undefined;
51
92
  settings.defaultPlanTools = defaultPlanTools;
52
93
  }
94
+ if (Object.hasOwn(value, "implementationPlanRetention")) {
95
+ const implementationPlanRetention = Reflect.get(value, "implementationPlanRetention");
96
+ if (
97
+ !IMPLEMENTATION_PLAN_RETENTIONS.includes(
98
+ implementationPlanRetention as ImplementationPlanRetention,
99
+ )
100
+ ) {
101
+ return undefined;
102
+ }
103
+ settings.implementationPlanRetention =
104
+ implementationPlanRetention as ImplementationPlanRetention;
105
+ }
106
+ if (Object.hasOwn(value, "defaultPlanExportPath")) {
107
+ const defaultPlanExportPath = normalizePlanExportPath(
108
+ Reflect.get(value, "defaultPlanExportPath"),
109
+ );
110
+ if (!defaultPlanExportPath) return undefined;
111
+ settings.defaultPlanExportPath = defaultPlanExportPath;
112
+ }
53
113
  if (Object.hasOwn(value, "safeSubcommands")) {
54
114
  const safeSubcommands = normalizeSafeSubcommands(Reflect.get(value, "safeSubcommands"));
55
115
  if (!safeSubcommands) return undefined;
@@ -68,8 +128,25 @@ function normalizeToolNames(value: unknown) {
68
128
  return Array.from(new Set(value));
69
129
  }
70
130
 
131
+ function normalizePlanExportPath(value: unknown) {
132
+ if (typeof value !== "string") return undefined;
133
+ const normalized = value.trim();
134
+ if (
135
+ !normalized ||
136
+ normalized.length > MAX_PLAN_EXPORT_PATH_LENGTH ||
137
+ !/[^@\s]/u.test(normalized) ||
138
+ [...normalized].some((character) => {
139
+ const codePoint = character.codePointAt(0) ?? 0;
140
+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
141
+ })
142
+ ) {
143
+ return undefined;
144
+ }
145
+ return normalized;
146
+ }
147
+
71
148
  function normalizeSafeSubcommands(value: unknown): SafeSubcommands | undefined {
72
- if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
149
+ if (!isSettingsDocument(value)) return undefined;
73
150
  if (Object.keys(value).some((key) => key !== "git" && key !== "gh")) return undefined;
74
151
 
75
152
  const safeSubcommands: SafeSubcommands = {};
@@ -99,77 +176,231 @@ function normalizeKnownValues(value: unknown, supported: readonly string[]) {
99
176
  export async function readPlanModeSettings(
100
177
  settingsPath?: string,
101
178
  ): Promise<PlanModeSettingsLoadResult> {
102
- if (settingsPath) return readSettingsFile(settingsPath);
103
- const canonicalPath = join(getAgentDir(), PLAN_MODE_SETTINGS_FILE);
104
- const canonical = await readSettingsFile(canonicalPath);
105
- const legacyPath = join(getAgentDir(), LEGACY_PLAN_MODE_SETTINGS_FILE);
106
- if (canonical.kind !== "missing") {
107
- return (await exists(legacyPath))
179
+ if (settingsPath) {
180
+ await awaitPlanModeSettingsWrites(settingsPath);
181
+ return (await readSettingsSnapshot(settingsPath)).result;
182
+ }
183
+ const canonicalPath = planModeSettingsPath();
184
+ await awaitPlanModeSettingsWrites(canonicalPath);
185
+ const canonical = await readSettingsSnapshot(canonicalPath);
186
+ const legacyPath = legacyPlanModeSettingsPath();
187
+ if (canonical.result.kind !== "missing") {
188
+ return (await pathExists(legacyPath))
108
189
  ? {
109
- ...canonical,
190
+ ...canonical.result,
110
191
  notice: `${LEGACY_PLAN_MODE_SETTINGS_FILE} ignored because ${PLAN_MODE_SETTINGS_FILE} takes precedence.`,
111
192
  }
112
- : canonical;
193
+ : canonical.result;
113
194
  }
114
195
 
115
- const legacy = await readSettingsFile(legacyPath);
116
- const raced = await readSettingsFile(canonicalPath);
117
- if (raced.kind !== "missing") return raced;
118
- return legacy.kind === "loaded"
196
+ const legacy = await readSettingsSnapshot(legacyPath);
197
+ const raced = await readSettingsSnapshot(canonicalPath);
198
+ if (raced.result.kind !== "missing") return raced.result;
199
+ return legacy.result.kind === "loaded"
119
200
  ? {
120
- ...legacy,
201
+ ...legacy.result,
121
202
  notice: `Using legacy ${LEGACY_PLAN_MODE_SETTINGS_FILE}; rename it to ${PLAN_MODE_SETTINGS_FILE}. The legacy file was not modified.`,
122
203
  }
123
- : legacy;
204
+ : legacy.result;
124
205
  }
125
206
 
126
- async function readSettingsFile(settingsPath: string): Promise<PlanModeSettingsLoadResult> {
127
- return (await readSettingsSnapshot(settingsPath)).result;
207
+ export function updatePlanModeSettings(
208
+ patch: PlanModeSettingsPatch,
209
+ options: UpdatePlanModeSettingsOptions = {},
210
+ ): Promise<PlanModeSettings> {
211
+ const settingsPath = options.settingsPath ?? planModeSettingsPath();
212
+ const legacySettingsPath =
213
+ options.legacySettingsPath ?? (options.settingsPath ? undefined : legacyPlanModeSettingsPath());
214
+ return enqueueMutation(settingsPath, async () => {
215
+ options.signal?.throwIfAborted();
216
+ const current = await readSettingsDocumentForUpdate(settingsPath, legacySettingsPath);
217
+ const updated: SettingsDocument = { ...current };
218
+ if (patch.thinkingLevel !== undefined) updated.thinkingLevel = patch.thinkingLevel;
219
+ if (patch.defaultPlanTools === null) delete updated.defaultPlanTools;
220
+ else if (patch.defaultPlanTools !== undefined) {
221
+ updated.defaultPlanTools = [...patch.defaultPlanTools];
222
+ }
223
+ if (patch.implementationPlanRetention !== undefined) {
224
+ updated.implementationPlanRetention = patch.implementationPlanRetention;
225
+ }
226
+ if (patch.defaultPlanExportPath === null) delete updated.defaultPlanExportPath;
227
+ else if (patch.defaultPlanExportPath !== undefined) {
228
+ updated.defaultPlanExportPath = patch.defaultPlanExportPath;
229
+ }
230
+ const settings = normalizePlanModeSettings(updated);
231
+ if (!settings) throw invalidSettingsError(settingsPath, "invalid settings shape");
232
+ await publishSettings(settingsPath, updated, options.signal, options.beforeRename);
233
+ return settings;
234
+ });
128
235
  }
129
236
 
130
- async function readSettingsSnapshot(settingsPath: string): Promise<{
131
- result: PlanModeSettingsLoadResult;
132
- contents?: string;
133
- }> {
237
+ export async function awaitPlanModeSettingsWrites(
238
+ settingsPath = planModeSettingsPath(),
239
+ ): Promise<void> {
240
+ await mutationQueues.get(settingsPath);
241
+ }
242
+
243
+ function enqueueMutation<T>(settingsPath: string, mutation: () => Promise<T>): Promise<T> {
244
+ const previous = mutationQueues.get(settingsPath) ?? Promise.resolve();
245
+ const result = previous.then(mutation, mutation);
246
+ const settled = result.then(
247
+ () => undefined,
248
+ () => undefined,
249
+ );
250
+ mutationQueues.set(settingsPath, settled);
251
+ void settled.finally(() => {
252
+ if (mutationQueues.get(settingsPath) === settled) mutationQueues.delete(settingsPath);
253
+ });
254
+ return result;
255
+ }
256
+
257
+ async function readSettingsDocumentForUpdate(
258
+ settingsPath: string,
259
+ legacySettingsPath: string | undefined,
260
+ ): Promise<SettingsDocument> {
261
+ const canonical = await readSettingsSnapshot(settingsPath);
262
+ if (canonical.result.kind === "loaded") return canonical.document ?? {};
263
+ if (canonical.result.kind === "invalid") {
264
+ throw invalidSettingsError(settingsPath, canonical.result.reason);
265
+ }
266
+ if (!legacySettingsPath) return {};
267
+
268
+ const legacy = await readSettingsSnapshot(legacySettingsPath);
269
+ const raced = await readSettingsSnapshot(settingsPath);
270
+ if (raced.result.kind === "loaded") return raced.document ?? {};
271
+ if (raced.result.kind === "invalid") {
272
+ throw invalidSettingsError(settingsPath, raced.result.reason);
273
+ }
274
+ if (legacy.result.kind === "invalid") {
275
+ throw invalidSettingsError(legacySettingsPath, legacy.result.reason);
276
+ }
277
+ return legacy.document ?? {};
278
+ }
279
+
280
+ async function readSettingsSnapshot(settingsPath: string): Promise<SettingsSnapshot> {
134
281
  let contents: string;
135
282
  try {
136
- contents = await readFile(settingsPath, "utf8");
283
+ contents = await readSettingsContents(settingsPath);
137
284
  } catch (error: unknown) {
138
285
  if (isNodeError(error) && error.code === "ENOENT") return { result: { kind: "missing" } };
139
- return { result: { kind: "invalid", reason: formatError(error) } };
286
+ return { result: { kind: "invalid", reason: safeReadError(error) } };
140
287
  }
288
+ let parsed: unknown;
141
289
  try {
142
- const settings = normalizePlanModeSettings(JSON.parse(contents) as unknown);
143
- return {
144
- contents,
145
- result: settings
146
- ? { kind: "loaded", settings }
147
- : { kind: "invalid", reason: "invalid settings shape" },
148
- };
149
- } catch (error: unknown) {
150
- return { contents, result: { kind: "invalid", reason: formatError(error) } };
290
+ parsed = JSON.parse(contents) as unknown;
291
+ } catch {
292
+ return { result: { kind: "invalid", reason: "invalid JSON" } };
293
+ }
294
+ const settings = normalizePlanModeSettings(parsed);
295
+ if (!settings || !isSettingsDocument(parsed)) {
296
+ return { result: { kind: "invalid", reason: "invalid settings shape" } };
151
297
  }
298
+ return { document: parsed, result: { kind: "loaded", settings } };
152
299
  }
153
300
 
154
- export function configuredThinkingLevel(
155
- settings: PlanModeSettings,
156
- ): PlanModeFixedThinkingLevel | undefined {
157
- return settings.thinkingLevel === "inherit" ? undefined : settings.thinkingLevel;
301
+ async function readSettingsContents(settingsPath: string): Promise<string> {
302
+ const flags = constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0);
303
+ const handle = await open(settingsPath, flags);
304
+ try {
305
+ const stats = await handle.stat();
306
+ if (!stats.isFile()) throw new Error("settings path is not a regular file");
307
+ if (stats.size > MAX_SETTINGS_BYTES) {
308
+ throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
309
+ }
310
+ const buffer = Buffer.alloc(MAX_SETTINGS_BYTES + 1);
311
+ let offset = 0;
312
+ while (offset < buffer.byteLength) {
313
+ const { bytesRead } = await handle.read(buffer, offset, buffer.byteLength - offset, offset);
314
+ if (bytesRead === 0) break;
315
+ offset += bytesRead;
316
+ }
317
+ if (offset > MAX_SETTINGS_BYTES) {
318
+ throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
319
+ }
320
+ try {
321
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
322
+ buffer.subarray(0, offset),
323
+ );
324
+ } catch {
325
+ throw new Error("settings file is not valid UTF-8");
326
+ }
327
+ } finally {
328
+ await handle.close();
329
+ }
330
+ }
331
+
332
+ async function publishSettings(
333
+ settingsPath: string,
334
+ document: SettingsDocument,
335
+ signal?: AbortSignal,
336
+ beforeRename?: (temporaryPath: string, settingsPath: string) => Promise<void>,
337
+ ): Promise<void> {
338
+ signal?.throwIfAborted();
339
+ const contents = `${JSON.stringify(document, null, 2)}\n`;
340
+ if (Buffer.byteLength(contents, "utf8") > MAX_SETTINGS_BYTES) {
341
+ throw new Error(`settings document exceeds ${MAX_SETTINGS_BYTES} bytes`);
342
+ }
343
+ const directory = dirname(settingsPath);
344
+ await mkdir(directory, { recursive: true });
345
+ signal?.throwIfAborted();
346
+ const temporaryPath = join(
347
+ directory,
348
+ `.${basename(settingsPath)}.${process.pid}.${randomUUID()}.tmp`,
349
+ );
350
+ try {
351
+ await writeFile(temporaryPath, contents, {
352
+ encoding: "utf8",
353
+ flag: "wx",
354
+ mode: 0o600,
355
+ signal,
356
+ });
357
+ await beforeRename?.(temporaryPath, settingsPath);
358
+ signal?.throwIfAborted();
359
+ await rename(temporaryPath, settingsPath);
360
+ } finally {
361
+ await rm(temporaryPath, { force: true }).catch(() => undefined);
362
+ }
363
+ }
364
+
365
+ function isSettingsDocument(value: unknown): value is SettingsDocument {
366
+ return typeof value === "object" && value !== null && !Array.isArray(value);
158
367
  }
159
368
 
160
- async function exists(path: string) {
369
+ async function pathExists(path: string) {
161
370
  try {
162
- await access(path);
371
+ const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
372
+ await handle.close();
163
373
  return true;
164
- } catch {
165
- return false;
374
+ } catch (error: unknown) {
375
+ return !(isNodeError(error) && error.code === "ENOENT");
166
376
  }
167
377
  }
168
378
 
379
+ function invalidSettingsError(settingsPath: string, reason: string) {
380
+ return new Error(`pi-plan-mode settings at ${settingsPath} are invalid: ${reason}`);
381
+ }
382
+
169
383
  function isNodeError(error: unknown): error is NodeJS.ErrnoException {
170
384
  return error instanceof Error && "code" in error;
171
385
  }
172
386
 
173
- function formatError(error: unknown) {
387
+ function safeReadError(error: unknown) {
388
+ if (isNodeError(error) && error.code === "ELOOP") return "settings path is not a regular file";
174
389
  return error instanceof Error ? error.message : String(error);
175
390
  }
391
+
392
+ export function configuredThinkingLevel(
393
+ settings: PlanModeSettings,
394
+ ): PlanModeFixedThinkingLevel | undefined {
395
+ return settings.thinkingLevel === "inherit" ? undefined : settings.thinkingLevel;
396
+ }
397
+
398
+ export function configuredImplementationPlanRetention(
399
+ settings: PlanModeSettings,
400
+ ): ImplementationPlanRetention {
401
+ return settings.implementationPlanRetention ?? "keep";
402
+ }
403
+
404
+ export function configuredPlanExportPath(settings: PlanModeSettings) {
405
+ return settings.defaultPlanExportPath ?? DEFAULT_PLAN_EXPORT_PATH;
406
+ }
package/src/state.ts CHANGED
@@ -3,7 +3,12 @@ import {
3
3
  PLAN_MODE_COMPLETE_TOOL_NAME,
4
4
  planFromCompletionDetails,
5
5
  } from "./completion-tool.js";
6
- import { PLAN_MODE_THINKING_LEVELS, type PlanModeFixedThinkingLevel } from "./settings.js";
6
+ import {
7
+ IMPLEMENTATION_PLAN_RETENTIONS,
8
+ type ImplementationPlanRetention,
9
+ PLAN_MODE_THINKING_LEVELS,
10
+ type PlanModeFixedThinkingLevel,
11
+ } from "./settings.js";
7
12
 
8
13
  export type PlanCompletionSource = typeof PLAN_MODE_COMPLETE_TOOL_NAME | "legacy_proposed_plan";
9
14
 
@@ -12,6 +17,7 @@ export interface ActiveImplementationPlan {
12
17
  plan: string;
13
18
  source: PlanCompletionSource;
14
19
  startedAt: number;
20
+ retention?: ImplementationPlanRetention;
15
21
  }
16
22
 
17
23
  export interface SavedPlan {
@@ -111,7 +117,12 @@ function normalizeActiveImplementation(value: unknown): ActiveImplementationPlan
111
117
  ? value.startedAt
112
118
  : undefined;
113
119
  if (!id || !source || !normalized.ok || startedAt === undefined) return undefined;
114
- return { id, plan: normalized.plan, source, startedAt };
120
+ const retention = IMPLEMENTATION_PLAN_RETENTIONS.includes(
121
+ value.retention as ImplementationPlanRetention,
122
+ )
123
+ ? (value.retention as ImplementationPlanRetention)
124
+ : "keep";
125
+ return { id, plan: normalized.plan, source, startedAt, retention };
115
126
  }
116
127
 
117
128
  function normalizePersistedPlan(value: unknown) {
@@ -1,5 +1,13 @@
1
1
  import type { ToolInfo } from "@earendil-works/pi-coding-agent";
2
- import { classifyPlanModeTool, isBuiltinTool } from "./tool-policy.js";
2
+ import { PLAN_MODE_COMPLETE_TOOL_NAME } from "./completion-tool.js";
3
+ import { PLAN_MODE_QUESTION_TOOL_NAME } from "./question-tool.js";
4
+ import { withRequiredPlanModeTools } from "./required-tools.js";
5
+ import {
6
+ canSelectToolInPlanMode,
7
+ classifyPlanModeTool,
8
+ isBuiltinTool,
9
+ SAFE_BUILTIN_PLAN_TOOLS,
10
+ } from "./tool-policy.js";
3
11
 
4
12
  export function toolNameFromLegacyKey(key: string, tools: ToolInfo[]) {
5
13
  const directName = tools.find((tool) => tool.name === key)?.name;
@@ -32,3 +40,59 @@ function toolSourceLabel(tool: ToolInfo) {
32
40
  export function unique(values: string[]) {
33
41
  return Array.from(new Set(values));
34
42
  }
43
+
44
+ export function filterAvailableSelectedToolNames(names: string[], tools: ToolInfo[]) {
45
+ const availableNames = new Set(tools.filter(canSelectToolInPlanMode).map((tool) => tool.name));
46
+ return unique(names.filter((name) => availableNames.has(name)));
47
+ }
48
+
49
+ export function defaultPlanModeToolNames(tools: ToolInfo[], configuredNames: string[] | undefined) {
50
+ if (configuredNames !== undefined) {
51
+ return filterAvailableSelectedToolNames(configuredNames, tools);
52
+ }
53
+ return tools
54
+ .filter((tool) => isBuiltinTool(tool) && SAFE_BUILTIN_PLAN_TOOLS.has(tool.name))
55
+ .map((tool) => tool.name);
56
+ }
57
+
58
+ interface PlanModeToolSelectionSnapshot {
59
+ selectedToolNames?: string[];
60
+ selectedToolKeys?: string[];
61
+ defaultPlanTools?: string[];
62
+ }
63
+
64
+ export function snapshotPlanModeSelectedNames(
65
+ tools: ToolInfo[],
66
+ selection: PlanModeToolSelectionSnapshot,
67
+ ) {
68
+ const selectedToolNames =
69
+ selection.selectedToolNames ??
70
+ selection.selectedToolKeys
71
+ ?.map((key) => toolNameFromLegacyKey(key, tools))
72
+ .filter((name): name is string => name !== undefined);
73
+ return new Set(
74
+ selectedToolNames === undefined
75
+ ? defaultPlanModeToolNames(tools, selection.defaultPlanTools)
76
+ : filterAvailableSelectedToolNames(selectedToolNames, tools),
77
+ );
78
+ }
79
+
80
+ export function snapshotPlanModeToolNames(
81
+ tools: ToolInfo[],
82
+ selectedNames: ReadonlySet<string>,
83
+ selection: PlanModeToolSelectionSnapshot,
84
+ ) {
85
+ if (
86
+ tools.length === 0 &&
87
+ selection.selectedToolNames === undefined &&
88
+ selection.selectedToolKeys === undefined &&
89
+ selection.defaultPlanTools === undefined
90
+ ) {
91
+ return ["read", "bash", PLAN_MODE_QUESTION_TOOL_NAME, PLAN_MODE_COMPLETE_TOOL_NAME];
92
+ }
93
+ return withRequiredPlanModeTools(
94
+ tools
95
+ .filter((tool) => selectedNames.has(tool.name) && canSelectToolInPlanMode(tool))
96
+ .map((tool) => tool.name),
97
+ );
98
+ }