@narumitw/pi-plan-mode 0.58.0 → 0.58.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/dist/chunks/{chunk-QHHHBTDO.js → chunk-L2CXUWCH.ts} +29 -111
- package/dist/chunks/chunk-L2CXUWCH.ts.map +7 -0
- package/dist/chunks/{interactive-ui-UNJGC2RQ.js → interactive-ui-VHZ5GODE.ts} +13 -41
- package/dist/chunks/interactive-ui-VHZ5GODE.ts.map +7 -0
- package/dist/index.ts +25 -82
- package/dist/index.ts.map +2 -2
- package/package.json +50 -51
- package/src/active-implementation-menu.ts +57 -60
- package/src/command.ts +22 -22
- package/src/completion-tool.ts +53 -56
- package/src/extension-runtime.ts +5 -5
- package/src/finalization-request.ts +40 -44
- package/src/fresh-handoff-coordinator.ts +38 -41
- package/src/fresh-implementation.ts +293 -310
- package/src/implementation-models.ts +25 -30
- package/src/implementation-retention.ts +109 -123
- package/src/message-transform.ts +218 -252
- package/src/mode-contract.ts +46 -49
- package/src/plan-action-controller.ts +134 -155
- package/src/plan-action-menus.ts +390 -408
- package/src/plan-export-controller.ts +23 -28
- package/src/plan-export-screen.ts +13 -13
- package/src/plan-export.ts +85 -102
- package/src/plan-launch-menu.ts +119 -119
- package/src/plan-mode.ts +2017 -2098
- package/src/presentation.ts +84 -108
- package/src/prompt.ts +1 -1
- package/src/question-tool.ts +239 -267
- package/src/required-tools.ts +15 -19
- package/src/saved-plan-menu.ts +82 -84
- package/src/saved-plan-preflight.ts +29 -33
- package/src/settings-menu.ts +470 -513
- package/src/settings.ts +393 -452
- package/src/state.ts +167 -198
- package/src/tool-policy.ts +634 -699
- package/src/tool-selection.ts +39 -44
- package/src/workflow-mutex.ts +66 -66
- package/dist/chunks/chunk-QHHHBTDO.js.map +0 -7
- package/dist/chunks/interactive-ui-UNJGC2RQ.js.map +0 -7
package/src/settings.ts
CHANGED
|
@@ -4,93 +4,78 @@ 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
|
-
type ImplementationModelOverride,
|
|
9
|
-
isPendingImplementationModelIdentifier,
|
|
10
|
-
} from "./implementation-models.js";
|
|
7
|
+
import { type ImplementationModelOverride, isPendingImplementationModelIdentifier } from "./implementation-models.js";
|
|
11
8
|
import type { SafeSubcommands } from "./tool-policy.js";
|
|
12
9
|
|
|
13
10
|
export const PLAN_MODE_SETTINGS_FILE = "pi-plan-mode.json";
|
|
14
11
|
const LEGACY_PLAN_MODE_SETTINGS_FILE = "plan-mode.json";
|
|
15
12
|
const MAX_SETTINGS_BYTES = 64 * 1024;
|
|
16
13
|
export const PLAN_MODE_THINKING_LEVELS = [
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
] as const;
|
|
26
|
-
export const IMPLEMENTATION_THINKING_LEVELS = [
|
|
27
|
-
"off",
|
|
28
|
-
"minimal",
|
|
29
|
-
"low",
|
|
30
|
-
"medium",
|
|
31
|
-
"high",
|
|
32
|
-
"xhigh",
|
|
33
|
-
"max",
|
|
34
|
-
] as const;
|
|
35
|
-
export const IMPLEMENTATION_PLAN_RETENTIONS = [
|
|
36
|
-
"clear-on-start",
|
|
37
|
-
"clear-after-first-run",
|
|
38
|
-
"keep",
|
|
14
|
+
"inherit",
|
|
15
|
+
"off",
|
|
16
|
+
"minimal",
|
|
17
|
+
"low",
|
|
18
|
+
"medium",
|
|
19
|
+
"high",
|
|
20
|
+
"xhigh",
|
|
21
|
+
"max",
|
|
39
22
|
] as const;
|
|
23
|
+
export const IMPLEMENTATION_THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
|
|
24
|
+
export const IMPLEMENTATION_PLAN_RETENTIONS = ["clear-on-start", "clear-after-first-run", "keep"] as const;
|
|
40
25
|
export const DEFAULT_PLAN_EXPORT_PATH = "PLAN.md";
|
|
41
26
|
const MODIFIERS = new Set(["ctrl", "shift", "alt", "super"]);
|
|
42
27
|
const BASE_KEYS = new Set([
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
28
|
+
..."abcdefghijklmnopqrstuvwxyz0123456789",
|
|
29
|
+
"`",
|
|
30
|
+
"-",
|
|
31
|
+
"=",
|
|
32
|
+
"[",
|
|
33
|
+
"]",
|
|
34
|
+
"\\",
|
|
35
|
+
";",
|
|
36
|
+
"'",
|
|
37
|
+
",",
|
|
38
|
+
".",
|
|
39
|
+
"/",
|
|
40
|
+
"!",
|
|
41
|
+
"@",
|
|
42
|
+
"#",
|
|
43
|
+
"$",
|
|
44
|
+
"%",
|
|
45
|
+
"^",
|
|
46
|
+
"&",
|
|
47
|
+
"*",
|
|
48
|
+
"(",
|
|
49
|
+
")",
|
|
50
|
+
"_",
|
|
51
|
+
"+",
|
|
52
|
+
"|",
|
|
53
|
+
"~",
|
|
54
|
+
"{",
|
|
55
|
+
"}",
|
|
56
|
+
":",
|
|
57
|
+
"<",
|
|
58
|
+
">",
|
|
59
|
+
"?",
|
|
60
|
+
"escape",
|
|
61
|
+
"esc",
|
|
62
|
+
"enter",
|
|
63
|
+
"return",
|
|
64
|
+
"tab",
|
|
65
|
+
"space",
|
|
66
|
+
"backspace",
|
|
67
|
+
"delete",
|
|
68
|
+
"insert",
|
|
69
|
+
"clear",
|
|
70
|
+
"home",
|
|
71
|
+
"end",
|
|
72
|
+
"pageup",
|
|
73
|
+
"pagedown",
|
|
74
|
+
"up",
|
|
75
|
+
"down",
|
|
76
|
+
"left",
|
|
77
|
+
"right",
|
|
78
|
+
...Array.from({ length: 12 }, (_unused, index) => `f${index + 1}`),
|
|
94
79
|
]);
|
|
95
80
|
const MAX_PLAN_EXPORT_PATH_LENGTH = 4096;
|
|
96
81
|
|
|
@@ -98,470 +83,426 @@ export type PlanModeThinkingLevel = (typeof PLAN_MODE_THINKING_LEVELS)[number];
|
|
|
98
83
|
export type ImplementationPlanRetention = (typeof IMPLEMENTATION_PLAN_RETENTIONS)[number];
|
|
99
84
|
export type PlanModeFixedThinkingLevel = (typeof IMPLEMENTATION_THINKING_LEVELS)[number];
|
|
100
85
|
export interface PlanModeSettings {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
86
|
+
thinkingLevel: PlanModeThinkingLevel;
|
|
87
|
+
defaultPlanTools?: string[];
|
|
88
|
+
implementationPlanRetention?: ImplementationPlanRetention;
|
|
89
|
+
defaultImplementationModel?: ImplementationModelOverride;
|
|
90
|
+
defaultImplementationThinkingLevel?: PlanModeFixedThinkingLevel;
|
|
91
|
+
defaultPlanExportPath?: string;
|
|
92
|
+
safeSubcommands?: SafeSubcommands;
|
|
93
|
+
toggleShortcut?: KeyId;
|
|
109
94
|
}
|
|
110
95
|
export interface PlanModeSettingsPatch {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
96
|
+
thinkingLevel?: PlanModeThinkingLevel;
|
|
97
|
+
defaultPlanTools?: readonly string[] | null;
|
|
98
|
+
implementationPlanRetention?: ImplementationPlanRetention;
|
|
99
|
+
defaultImplementationModel?: ImplementationModelOverride | null;
|
|
100
|
+
defaultImplementationThinkingLevel?: PlanModeFixedThinkingLevel | null;
|
|
101
|
+
defaultPlanExportPath?: string | null;
|
|
102
|
+
toggleShortcut?: KeyId | null;
|
|
118
103
|
}
|
|
119
104
|
export interface UpdatePlanModeSettingsOptions {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
105
|
+
settingsPath?: string;
|
|
106
|
+
legacySettingsPath?: string;
|
|
107
|
+
signal?: AbortSignal;
|
|
108
|
+
beforeRename?: (temporaryPath: string, settingsPath: string) => Promise<void>;
|
|
124
109
|
}
|
|
125
110
|
export type PlanModeSettingsLoadResult =
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
111
|
+
| { kind: "missing"; notice?: string }
|
|
112
|
+
| { kind: "invalid"; reason: string; notice?: string }
|
|
113
|
+
| { kind: "loaded"; settings: PlanModeSettings; notice?: string };
|
|
129
114
|
|
|
130
115
|
type SettingsDocument = Record<string, unknown>;
|
|
131
116
|
type SettingsSnapshot = {
|
|
132
|
-
|
|
133
|
-
|
|
117
|
+
result: PlanModeSettingsLoadResult;
|
|
118
|
+
document?: SettingsDocument;
|
|
134
119
|
};
|
|
135
120
|
|
|
136
121
|
const mutationQueues = new Map<string, Promise<void>>();
|
|
137
122
|
|
|
138
123
|
export function planModeSettingsPath() {
|
|
139
|
-
|
|
124
|
+
return join(getAgentDir(), PLAN_MODE_SETTINGS_FILE);
|
|
140
125
|
}
|
|
141
126
|
|
|
142
127
|
function legacyPlanModeSettingsPath() {
|
|
143
|
-
|
|
128
|
+
return join(getAgentDir(), LEGACY_PLAN_MODE_SETTINGS_FILE);
|
|
144
129
|
}
|
|
145
130
|
|
|
146
131
|
export function normalizePlanModeSettings(value: unknown): PlanModeSettings | undefined {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
if (Object.hasOwn(value, "defaultPlanExportPath")) {
|
|
197
|
-
const defaultPlanExportPath = normalizePlanExportPath(
|
|
198
|
-
Reflect.get(value, "defaultPlanExportPath"),
|
|
199
|
-
);
|
|
200
|
-
if (!defaultPlanExportPath) return undefined;
|
|
201
|
-
settings.defaultPlanExportPath = defaultPlanExportPath;
|
|
202
|
-
}
|
|
203
|
-
if (Object.hasOwn(value, "toggleShortcut")) {
|
|
204
|
-
const toggleShortcut = normalizeKeyId(Reflect.get(value, "toggleShortcut"));
|
|
205
|
-
if (!toggleShortcut) return undefined;
|
|
206
|
-
settings.toggleShortcut = toggleShortcut;
|
|
207
|
-
}
|
|
208
|
-
if (Object.hasOwn(value, "safeSubcommands")) {
|
|
209
|
-
const safeSubcommands = normalizeSafeSubcommands(Reflect.get(value, "safeSubcommands"));
|
|
210
|
-
if (!safeSubcommands) return undefined;
|
|
211
|
-
settings.safeSubcommands = safeSubcommands;
|
|
212
|
-
}
|
|
213
|
-
return settings;
|
|
132
|
+
if (!isSettingsDocument(value)) return undefined;
|
|
133
|
+
const thinkingLevel = Object.hasOwn(value, "thinkingLevel") ? Reflect.get(value, "thinkingLevel") : "inherit";
|
|
134
|
+
if (!PLAN_MODE_THINKING_LEVELS.includes(thinkingLevel as PlanModeThinkingLevel)) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
const settings: PlanModeSettings = {
|
|
138
|
+
thinkingLevel: thinkingLevel as PlanModeThinkingLevel,
|
|
139
|
+
};
|
|
140
|
+
if (Object.hasOwn(value, "defaultPlanTools")) {
|
|
141
|
+
const defaultPlanTools = normalizeToolNames(Reflect.get(value, "defaultPlanTools"));
|
|
142
|
+
if (!defaultPlanTools) return undefined;
|
|
143
|
+
settings.defaultPlanTools = defaultPlanTools;
|
|
144
|
+
}
|
|
145
|
+
if (Object.hasOwn(value, "implementationPlanRetention")) {
|
|
146
|
+
const implementationPlanRetention = Reflect.get(value, "implementationPlanRetention");
|
|
147
|
+
if (!IMPLEMENTATION_PLAN_RETENTIONS.includes(implementationPlanRetention as ImplementationPlanRetention)) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
settings.implementationPlanRetention = implementationPlanRetention as ImplementationPlanRetention;
|
|
151
|
+
}
|
|
152
|
+
if (Object.hasOwn(value, "defaultImplementationModel")) {
|
|
153
|
+
const defaultImplementationModel = normalizeImplementationModel(Reflect.get(value, "defaultImplementationModel"));
|
|
154
|
+
if (!defaultImplementationModel) return undefined;
|
|
155
|
+
settings.defaultImplementationModel = defaultImplementationModel;
|
|
156
|
+
}
|
|
157
|
+
if (Object.hasOwn(value, "defaultImplementationThinkingLevel")) {
|
|
158
|
+
const defaultImplementationThinkingLevel = Reflect.get(value, "defaultImplementationThinkingLevel");
|
|
159
|
+
if (!IMPLEMENTATION_THINKING_LEVELS.includes(defaultImplementationThinkingLevel as PlanModeFixedThinkingLevel)) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
settings.defaultImplementationThinkingLevel = defaultImplementationThinkingLevel as PlanModeFixedThinkingLevel;
|
|
163
|
+
}
|
|
164
|
+
if (Object.hasOwn(value, "defaultPlanExportPath")) {
|
|
165
|
+
const defaultPlanExportPath = normalizePlanExportPath(Reflect.get(value, "defaultPlanExportPath"));
|
|
166
|
+
if (!defaultPlanExportPath) return undefined;
|
|
167
|
+
settings.defaultPlanExportPath = defaultPlanExportPath;
|
|
168
|
+
}
|
|
169
|
+
if (Object.hasOwn(value, "toggleShortcut")) {
|
|
170
|
+
const toggleShortcut = normalizeKeyId(Reflect.get(value, "toggleShortcut"));
|
|
171
|
+
if (!toggleShortcut) return undefined;
|
|
172
|
+
settings.toggleShortcut = toggleShortcut;
|
|
173
|
+
}
|
|
174
|
+
if (Object.hasOwn(value, "safeSubcommands")) {
|
|
175
|
+
const safeSubcommands = normalizeSafeSubcommands(Reflect.get(value, "safeSubcommands"));
|
|
176
|
+
if (!safeSubcommands) return undefined;
|
|
177
|
+
settings.safeSubcommands = safeSubcommands;
|
|
178
|
+
}
|
|
179
|
+
return settings;
|
|
214
180
|
}
|
|
215
181
|
|
|
216
182
|
function normalizeImplementationModel(value: unknown): ImplementationModelOverride | undefined {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
!isPendingImplementationModelIdentifier(provider) ||
|
|
227
|
-
!isPendingImplementationModelIdentifier(modelId)
|
|
228
|
-
) {
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
return { provider, modelId };
|
|
183
|
+
if (!isSettingsDocument(value) || Object.keys(value).some((key) => key !== "provider" && key !== "modelId")) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
const provider = typeof value.provider === "string" ? value.provider.trim() : value.provider;
|
|
187
|
+
const modelId = typeof value.modelId === "string" ? value.modelId.trim() : value.modelId;
|
|
188
|
+
if (!isPendingImplementationModelIdentifier(provider) || !isPendingImplementationModelIdentifier(modelId)) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
return { provider, modelId };
|
|
232
192
|
}
|
|
233
193
|
|
|
234
194
|
function normalizeToolNames(value: unknown) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
195
|
+
if (
|
|
196
|
+
!Array.isArray(value) ||
|
|
197
|
+
!value.every((item): item is string => typeof item === "string" && item.trim().length > 0)
|
|
198
|
+
) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
return Array.from(new Set(value));
|
|
242
202
|
}
|
|
243
203
|
|
|
244
204
|
function normalizePlanExportPath(value: unknown) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
205
|
+
if (typeof value !== "string") return undefined;
|
|
206
|
+
const normalized = value.trim();
|
|
207
|
+
if (
|
|
208
|
+
!normalized ||
|
|
209
|
+
normalized.length > MAX_PLAN_EXPORT_PATH_LENGTH ||
|
|
210
|
+
!/[^@\s]/u.test(normalized) ||
|
|
211
|
+
[...normalized].some((character) => {
|
|
212
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
213
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
|
|
214
|
+
})
|
|
215
|
+
) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
return normalized;
|
|
259
219
|
}
|
|
260
220
|
|
|
261
221
|
export function normalizeKeyId(value: unknown): KeyId | undefined {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
222
|
+
if (typeof value !== "string") return undefined;
|
|
223
|
+
const normalized = value.trim().toLowerCase();
|
|
224
|
+
const base = [...BASE_KEYS]
|
|
225
|
+
.sort((left, right) => right.length - left.length)
|
|
226
|
+
.find((candidate) => normalized === candidate || normalized.endsWith(`+${candidate}`));
|
|
227
|
+
if (!base) return undefined;
|
|
228
|
+
const prefix = normalized.slice(0, normalized.length - base.length);
|
|
229
|
+
if (!prefix) return base as KeyId;
|
|
230
|
+
if (/^f(?:[1-9]|1[0-2])$/.test(base) || !prefix.endsWith("+")) return undefined;
|
|
231
|
+
const modifiers = prefix.slice(0, -1).split("+");
|
|
232
|
+
if (
|
|
233
|
+
modifiers.length === 0 ||
|
|
234
|
+
modifiers.some((modifier) => !MODIFIERS.has(modifier)) ||
|
|
235
|
+
new Set(modifiers).size !== modifiers.length
|
|
236
|
+
) {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
return normalized as KeyId;
|
|
280
240
|
}
|
|
281
241
|
|
|
282
242
|
function normalizeSafeSubcommands(value: unknown): SafeSubcommands | undefined {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (existing) existing[1] = Array.from(new Set([...existing[1], ...normalizedSubcommands]));
|
|
301
|
-
else entries.push([normalizedCommand, normalizedSubcommands]);
|
|
302
|
-
}
|
|
303
|
-
return Object.fromEntries(entries);
|
|
243
|
+
if (!isSettingsDocument(value)) return undefined;
|
|
244
|
+
const entries: [string, string[]][] = [];
|
|
245
|
+
for (const [command, subcommands] of Object.entries(value)) {
|
|
246
|
+
const normalizedCommand = command.trim();
|
|
247
|
+
if (
|
|
248
|
+
!normalizedCommand ||
|
|
249
|
+
!Array.isArray(subcommands) ||
|
|
250
|
+
!subcommands.every((item): item is string => typeof item === "string" && item.trim().length > 0)
|
|
251
|
+
) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
const normalizedSubcommands = Array.from(new Set(subcommands.map((subcommand) => subcommand.trim())));
|
|
255
|
+
const existing = entries.find(([existingCommand]) => existingCommand === normalizedCommand);
|
|
256
|
+
if (existing) existing[1] = Array.from(new Set([...existing[1], ...normalizedSubcommands]));
|
|
257
|
+
else entries.push([normalizedCommand, normalizedSubcommands]);
|
|
258
|
+
}
|
|
259
|
+
return Object.fromEntries(entries);
|
|
304
260
|
}
|
|
305
261
|
|
|
306
|
-
export async function readPlanModeSettings(
|
|
307
|
-
|
|
308
|
-
)
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
: legacy.result;
|
|
262
|
+
export async function readPlanModeSettings(settingsPath?: string): Promise<PlanModeSettingsLoadResult> {
|
|
263
|
+
if (settingsPath) {
|
|
264
|
+
await awaitPlanModeSettingsWrites(settingsPath);
|
|
265
|
+
return (await readSettingsSnapshot(settingsPath)).result;
|
|
266
|
+
}
|
|
267
|
+
const canonicalPath = planModeSettingsPath();
|
|
268
|
+
await awaitPlanModeSettingsWrites(canonicalPath);
|
|
269
|
+
const canonical = await readSettingsSnapshot(canonicalPath);
|
|
270
|
+
const legacyPath = legacyPlanModeSettingsPath();
|
|
271
|
+
if (canonical.result.kind !== "missing") {
|
|
272
|
+
return (await pathExists(legacyPath))
|
|
273
|
+
? {
|
|
274
|
+
...canonical.result,
|
|
275
|
+
notice: `${LEGACY_PLAN_MODE_SETTINGS_FILE} ignored because ${PLAN_MODE_SETTINGS_FILE} takes precedence.`,
|
|
276
|
+
}
|
|
277
|
+
: canonical.result;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const legacy = await readSettingsSnapshot(legacyPath);
|
|
281
|
+
const raced = await readSettingsSnapshot(canonicalPath);
|
|
282
|
+
if (raced.result.kind !== "missing") return raced.result;
|
|
283
|
+
return legacy.result.kind === "loaded"
|
|
284
|
+
? {
|
|
285
|
+
...legacy.result,
|
|
286
|
+
notice: `Using legacy ${LEGACY_PLAN_MODE_SETTINGS_FILE}; rename it to ${PLAN_MODE_SETTINGS_FILE}. The legacy file was not modified.`,
|
|
287
|
+
}
|
|
288
|
+
: legacy.result;
|
|
335
289
|
}
|
|
336
290
|
|
|
337
291
|
export function updatePlanModeSettings(
|
|
338
|
-
|
|
339
|
-
|
|
292
|
+
patch: PlanModeSettingsPatch,
|
|
293
|
+
options: UpdatePlanModeSettingsOptions = {},
|
|
340
294
|
): Promise<PlanModeSettings> {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
295
|
+
const settingsPath = options.settingsPath ?? planModeSettingsPath();
|
|
296
|
+
const legacySettingsPath =
|
|
297
|
+
options.legacySettingsPath ?? (options.settingsPath ? undefined : legacyPlanModeSettingsPath());
|
|
298
|
+
return enqueueMutation(settingsPath, async () => {
|
|
299
|
+
options.signal?.throwIfAborted();
|
|
300
|
+
const current = await readSettingsDocumentForUpdate(settingsPath, legacySettingsPath);
|
|
301
|
+
const updated: SettingsDocument = { ...current };
|
|
302
|
+
if (patch.thinkingLevel !== undefined) updated.thinkingLevel = patch.thinkingLevel;
|
|
303
|
+
if (patch.defaultPlanTools === null) delete updated.defaultPlanTools;
|
|
304
|
+
else if (patch.defaultPlanTools !== undefined) {
|
|
305
|
+
updated.defaultPlanTools = [...patch.defaultPlanTools];
|
|
306
|
+
}
|
|
307
|
+
if (patch.implementationPlanRetention !== undefined) {
|
|
308
|
+
updated.implementationPlanRetention = patch.implementationPlanRetention;
|
|
309
|
+
}
|
|
310
|
+
if (patch.defaultImplementationModel === null) delete updated.defaultImplementationModel;
|
|
311
|
+
else if (patch.defaultImplementationModel !== undefined) {
|
|
312
|
+
const model = normalizeImplementationModel(patch.defaultImplementationModel);
|
|
313
|
+
if (!model) throw invalidSettingsError(settingsPath, "invalid implementation model");
|
|
314
|
+
updated.defaultImplementationModel = model;
|
|
315
|
+
}
|
|
316
|
+
if (patch.defaultImplementationThinkingLevel === null) {
|
|
317
|
+
delete updated.defaultImplementationThinkingLevel;
|
|
318
|
+
} else if (patch.defaultImplementationThinkingLevel !== undefined) {
|
|
319
|
+
updated.defaultImplementationThinkingLevel = patch.defaultImplementationThinkingLevel;
|
|
320
|
+
}
|
|
321
|
+
if (patch.defaultPlanExportPath === null) delete updated.defaultPlanExportPath;
|
|
322
|
+
else if (patch.defaultPlanExportPath !== undefined) {
|
|
323
|
+
updated.defaultPlanExportPath = patch.defaultPlanExportPath;
|
|
324
|
+
}
|
|
325
|
+
if (patch.toggleShortcut === null) delete updated.toggleShortcut;
|
|
326
|
+
else if (patch.toggleShortcut !== undefined) {
|
|
327
|
+
updated.toggleShortcut = patch.toggleShortcut;
|
|
328
|
+
}
|
|
329
|
+
const settings = normalizePlanModeSettings(updated);
|
|
330
|
+
if (!settings) throw invalidSettingsError(settingsPath, "invalid settings shape");
|
|
331
|
+
await publishSettings(settingsPath, updated, options.signal, options.beforeRename);
|
|
332
|
+
return settings;
|
|
333
|
+
});
|
|
380
334
|
}
|
|
381
335
|
|
|
382
|
-
export async function awaitPlanModeSettingsWrites(
|
|
383
|
-
|
|
384
|
-
): Promise<void> {
|
|
385
|
-
await mutationQueues.get(settingsPath);
|
|
336
|
+
export async function awaitPlanModeSettingsWrites(settingsPath = planModeSettingsPath()): Promise<void> {
|
|
337
|
+
await mutationQueues.get(settingsPath);
|
|
386
338
|
}
|
|
387
339
|
|
|
388
340
|
function enqueueMutation<T>(settingsPath: string, mutation: () => Promise<T>): Promise<T> {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
341
|
+
const previous = mutationQueues.get(settingsPath) ?? Promise.resolve();
|
|
342
|
+
const result = previous.then(mutation, mutation);
|
|
343
|
+
const settled = result.then(
|
|
344
|
+
() => undefined,
|
|
345
|
+
() => undefined,
|
|
346
|
+
);
|
|
347
|
+
mutationQueues.set(settingsPath, settled);
|
|
348
|
+
void settled.finally(() => {
|
|
349
|
+
if (mutationQueues.get(settingsPath) === settled) mutationQueues.delete(settingsPath);
|
|
350
|
+
});
|
|
351
|
+
return result;
|
|
400
352
|
}
|
|
401
353
|
|
|
402
354
|
async function readSettingsDocumentForUpdate(
|
|
403
|
-
|
|
404
|
-
|
|
355
|
+
settingsPath: string,
|
|
356
|
+
legacySettingsPath: string | undefined,
|
|
405
357
|
): Promise<SettingsDocument> {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
358
|
+
const canonical = await readSettingsSnapshot(settingsPath);
|
|
359
|
+
if (canonical.result.kind === "loaded") return canonical.document ?? {};
|
|
360
|
+
if (canonical.result.kind === "invalid") {
|
|
361
|
+
throw invalidSettingsError(settingsPath, canonical.result.reason);
|
|
362
|
+
}
|
|
363
|
+
if (!legacySettingsPath) return {};
|
|
364
|
+
|
|
365
|
+
const legacy = await readSettingsSnapshot(legacySettingsPath);
|
|
366
|
+
const raced = await readSettingsSnapshot(settingsPath);
|
|
367
|
+
if (raced.result.kind === "loaded") return raced.document ?? {};
|
|
368
|
+
if (raced.result.kind === "invalid") {
|
|
369
|
+
throw invalidSettingsError(settingsPath, raced.result.reason);
|
|
370
|
+
}
|
|
371
|
+
if (legacy.result.kind === "invalid") {
|
|
372
|
+
throw invalidSettingsError(legacySettingsPath, legacy.result.reason);
|
|
373
|
+
}
|
|
374
|
+
return legacy.document ?? {};
|
|
423
375
|
}
|
|
424
376
|
|
|
425
377
|
async function readSettingsSnapshot(settingsPath: string): Promise<SettingsSnapshot> {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
378
|
+
let contents: string;
|
|
379
|
+
try {
|
|
380
|
+
contents = await readSettingsContents(settingsPath);
|
|
381
|
+
} catch (error: unknown) {
|
|
382
|
+
if (isNodeError(error) && error.code === "ENOENT") return { result: { kind: "missing" } };
|
|
383
|
+
return { result: { kind: "invalid", reason: safeReadError(error) } };
|
|
384
|
+
}
|
|
385
|
+
let parsed: unknown;
|
|
386
|
+
try {
|
|
387
|
+
parsed = JSON.parse(contents) as unknown;
|
|
388
|
+
} catch {
|
|
389
|
+
return { result: { kind: "invalid", reason: "invalid JSON" } };
|
|
390
|
+
}
|
|
391
|
+
const settings = normalizePlanModeSettings(parsed);
|
|
392
|
+
if (!settings || !isSettingsDocument(parsed)) {
|
|
393
|
+
return { result: { kind: "invalid", reason: "invalid settings shape" } };
|
|
394
|
+
}
|
|
395
|
+
return { document: parsed, result: { kind: "loaded", settings } };
|
|
444
396
|
}
|
|
445
397
|
|
|
446
398
|
async function readSettingsContents(settingsPath: string): Promise<string> {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
await handle.close();
|
|
474
|
-
}
|
|
399
|
+
const flags = constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0);
|
|
400
|
+
const handle = await open(settingsPath, flags);
|
|
401
|
+
try {
|
|
402
|
+
const stats = await handle.stat();
|
|
403
|
+
if (!stats.isFile()) throw new Error("settings path is not a regular file");
|
|
404
|
+
if (stats.size > MAX_SETTINGS_BYTES) {
|
|
405
|
+
throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
|
|
406
|
+
}
|
|
407
|
+
const buffer = Buffer.alloc(MAX_SETTINGS_BYTES + 1);
|
|
408
|
+
let offset = 0;
|
|
409
|
+
while (offset < buffer.byteLength) {
|
|
410
|
+
const { bytesRead } = await handle.read(buffer, offset, buffer.byteLength - offset, offset);
|
|
411
|
+
if (bytesRead === 0) break;
|
|
412
|
+
offset += bytesRead;
|
|
413
|
+
}
|
|
414
|
+
if (offset > MAX_SETTINGS_BYTES) {
|
|
415
|
+
throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
|
|
416
|
+
}
|
|
417
|
+
try {
|
|
418
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buffer.subarray(0, offset));
|
|
419
|
+
} catch {
|
|
420
|
+
throw new Error("settings file is not valid UTF-8");
|
|
421
|
+
}
|
|
422
|
+
} finally {
|
|
423
|
+
await handle.close();
|
|
424
|
+
}
|
|
475
425
|
}
|
|
476
426
|
|
|
477
427
|
async function publishSettings(
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
428
|
+
settingsPath: string,
|
|
429
|
+
document: SettingsDocument,
|
|
430
|
+
signal?: AbortSignal,
|
|
431
|
+
beforeRename?: (temporaryPath: string, settingsPath: string) => Promise<void>,
|
|
482
432
|
): Promise<void> {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
} finally {
|
|
506
|
-
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
507
|
-
}
|
|
433
|
+
signal?.throwIfAborted();
|
|
434
|
+
const contents = `${JSON.stringify(document, null, 2)}\n`;
|
|
435
|
+
if (Buffer.byteLength(contents, "utf8") > MAX_SETTINGS_BYTES) {
|
|
436
|
+
throw new Error(`settings document exceeds ${MAX_SETTINGS_BYTES} bytes`);
|
|
437
|
+
}
|
|
438
|
+
const directory = dirname(settingsPath);
|
|
439
|
+
await mkdir(directory, { recursive: true });
|
|
440
|
+
signal?.throwIfAborted();
|
|
441
|
+
const temporaryPath = join(directory, `.${basename(settingsPath)}.${process.pid}.${randomUUID()}.tmp`);
|
|
442
|
+
try {
|
|
443
|
+
await writeFile(temporaryPath, contents, {
|
|
444
|
+
encoding: "utf8",
|
|
445
|
+
flag: "wx",
|
|
446
|
+
mode: 0o600,
|
|
447
|
+
signal,
|
|
448
|
+
});
|
|
449
|
+
await beforeRename?.(temporaryPath, settingsPath);
|
|
450
|
+
signal?.throwIfAborted();
|
|
451
|
+
await rename(temporaryPath, settingsPath);
|
|
452
|
+
} finally {
|
|
453
|
+
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
454
|
+
}
|
|
508
455
|
}
|
|
509
456
|
|
|
510
457
|
function isSettingsDocument(value: unknown): value is SettingsDocument {
|
|
511
|
-
|
|
458
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
512
459
|
}
|
|
513
460
|
|
|
514
461
|
async function pathExists(path: string) {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
462
|
+
try {
|
|
463
|
+
const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
|
|
464
|
+
await handle.close();
|
|
465
|
+
return true;
|
|
466
|
+
} catch (error: unknown) {
|
|
467
|
+
return !(isNodeError(error) && error.code === "ENOENT");
|
|
468
|
+
}
|
|
522
469
|
}
|
|
523
470
|
|
|
524
471
|
function invalidSettingsError(settingsPath: string, reason: string) {
|
|
525
|
-
|
|
472
|
+
return new Error(`pi-plan-mode settings at ${settingsPath} are invalid: ${reason}`);
|
|
526
473
|
}
|
|
527
474
|
|
|
528
475
|
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
529
|
-
|
|
476
|
+
return error instanceof Error && "code" in error;
|
|
530
477
|
}
|
|
531
478
|
|
|
532
479
|
function safeReadError(error: unknown) {
|
|
533
|
-
|
|
534
|
-
|
|
480
|
+
if (isNodeError(error) && error.code === "ELOOP") return "settings path is not a regular file";
|
|
481
|
+
return error instanceof Error ? error.message : String(error);
|
|
535
482
|
}
|
|
536
483
|
|
|
537
|
-
export function configuredThinkingLevel(
|
|
538
|
-
|
|
539
|
-
): PlanModeFixedThinkingLevel | undefined {
|
|
540
|
-
return settings.thinkingLevel === "inherit" ? undefined : settings.thinkingLevel;
|
|
484
|
+
export function configuredThinkingLevel(settings: PlanModeSettings): PlanModeFixedThinkingLevel | undefined {
|
|
485
|
+
return settings.thinkingLevel === "inherit" ? undefined : settings.thinkingLevel;
|
|
541
486
|
}
|
|
542
487
|
|
|
543
|
-
export function configuredImplementationPlanRetention(
|
|
544
|
-
|
|
545
|
-
): ImplementationPlanRetention {
|
|
546
|
-
return settings.implementationPlanRetention ?? "clear-on-start";
|
|
488
|
+
export function configuredImplementationPlanRetention(settings: PlanModeSettings): ImplementationPlanRetention {
|
|
489
|
+
return settings.implementationPlanRetention ?? "clear-on-start";
|
|
547
490
|
}
|
|
548
491
|
|
|
549
|
-
export function configuredImplementationModel(
|
|
550
|
-
|
|
551
|
-
): ImplementationModelOverride | undefined {
|
|
552
|
-
return settings.defaultImplementationModel;
|
|
492
|
+
export function configuredImplementationModel(settings: PlanModeSettings): ImplementationModelOverride | undefined {
|
|
493
|
+
return settings.defaultImplementationModel;
|
|
553
494
|
}
|
|
554
495
|
|
|
555
496
|
export function configuredImplementationThinkingLevel(
|
|
556
|
-
|
|
497
|
+
settings: PlanModeSettings,
|
|
557
498
|
): PlanModeFixedThinkingLevel | undefined {
|
|
558
|
-
|
|
499
|
+
return settings.defaultImplementationThinkingLevel;
|
|
559
500
|
}
|
|
560
501
|
|
|
561
502
|
export function configuredPlanExportPath(settings: PlanModeSettings) {
|
|
562
|
-
|
|
503
|
+
return settings.defaultPlanExportPath ?? DEFAULT_PLAN_EXPORT_PATH;
|
|
563
504
|
}
|
|
564
505
|
|
|
565
506
|
export function configuredPlanModeToggleShortcut(settings: PlanModeSettings): KeyId | undefined {
|
|
566
|
-
|
|
507
|
+
return settings.toggleShortcut;
|
|
567
508
|
}
|