@narumitw/pi-plan-mode 0.57.1 → 0.58.1
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/README.md +68 -4
- package/dist/chunks/{chunk-57OBPS7P.js → chunk-HF7ZEYIC.js} +169 -164
- package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
- package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UK7IGPOE.js} +312 -32
- package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
- package/dist/index.ts +670 -212
- package/dist/index.ts.map +4 -4
- package/docs/settings.md +26 -2
- package/package.json +50 -51
- package/src/active-implementation-menu.ts +57 -60
- package/src/command.ts +22 -21
- 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 +52 -0
- package/src/fresh-implementation.ts +312 -205
- package/src/implementation-models.ts +43 -0
- 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 +139 -89
- package/src/plan-action-menus.ts +414 -177
- 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 +2027 -1675
- 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 +489 -377
- package/src/settings.ts +403 -382
- package/src/state.ts +177 -146
- 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-57OBPS7P.js.map +0 -7
- package/dist/chunks/interactive-ui-N2ZKBVNE.js.map +0 -7
package/src/plan-mode.ts
CHANGED
|
@@ -1,1733 +1,2085 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { watch } from "node:fs";
|
|
3
3
|
import { basename, dirname } from "node:path";
|
|
4
|
+
import { stripVTControlCharacters } from "node:util";
|
|
4
5
|
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
ExtensionAPI,
|
|
7
|
+
ExtensionCommandContext,
|
|
8
|
+
ExtensionContext,
|
|
9
|
+
InputEvent,
|
|
10
|
+
InputSource,
|
|
8
11
|
} from "@earendil-works/pi-coding-agent";
|
|
9
12
|
import { completePlanArguments } from "./command.js";
|
|
10
13
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
normalizePlanModeCompletion,
|
|
15
|
+
PLAN_MODE_COMPLETE_PARAMS,
|
|
16
|
+
PLAN_MODE_COMPLETE_TOOL_NAME,
|
|
17
|
+
planModeCompleted,
|
|
18
|
+
renderPlanModeCompletion,
|
|
16
19
|
} from "./completion-tool.js";
|
|
17
20
|
import { isStaleExtensionContextError } from "./extension-runtime.js";
|
|
18
21
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
createFinalizationRequestCoordinator,
|
|
23
|
+
FINALIZE_PLAN_PROMPT,
|
|
24
|
+
type FinalizationRunOutcome,
|
|
25
|
+
RETRY_FINALIZE_PLAN_PROMPT,
|
|
23
26
|
} from "./finalization-request.js";
|
|
27
|
+
import { createDeferredFreshHandoffCoordinator } from "./fresh-handoff-coordinator.js";
|
|
24
28
|
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
formatHistoryImplementationPrompt,
|
|
30
|
+
formatImplementationHandoff,
|
|
31
|
+
formatTransferredPlanPrompt,
|
|
32
|
+
startFreshImplementationFromState,
|
|
29
33
|
} from "./fresh-implementation.js";
|
|
30
34
|
import {
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
createImplementationRetentionCoordinator,
|
|
36
|
+
implementationRetentionPreview,
|
|
33
37
|
} from "./implementation-retention.js";
|
|
34
38
|
import {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
invalidPlanMessage,
|
|
40
|
+
latestAssistantStopReason,
|
|
41
|
+
latestAssistantText,
|
|
42
|
+
messageTextContent,
|
|
43
|
+
parseProposedPlan,
|
|
40
44
|
} from "./message-transform.js";
|
|
41
45
|
import {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
createModeContractMessage,
|
|
47
|
+
hasModeContractArtifact,
|
|
48
|
+
latestModeContract,
|
|
49
|
+
MODE_CONTRACT_MESSAGE_TYPE,
|
|
50
|
+
type PlanModeContract,
|
|
51
|
+
reconcileModeContract,
|
|
48
52
|
} from "./mode-contract.js";
|
|
49
|
-
import { createPlanActionController } from "./plan-action-controller.js";
|
|
53
|
+
import { createPlanActionController, type FreshImplementationTiming } from "./plan-action-controller.js";
|
|
50
54
|
import { createPlanExportController } from "./plan-export-controller.js";
|
|
51
55
|
import {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
clearPlanModeUi,
|
|
57
|
+
planModeStatusText as formatPlanModeStatusText,
|
|
58
|
+
showStoredPlan,
|
|
59
|
+
updatePlanModeUi,
|
|
56
60
|
} from "./presentation.js";
|
|
57
61
|
import {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
answerPlanModeQuestions,
|
|
63
|
+
normalizePlanModeQuestionParams,
|
|
64
|
+
PLAN_MODE_QUESTION_PARAMS,
|
|
65
|
+
PLAN_MODE_QUESTION_TOOL_NAME,
|
|
66
|
+
planModeQuestionCancelled,
|
|
63
67
|
} from "./question-tool.js";
|
|
68
|
+
import { assertPlanModeHelperToolsAvailable, planModeHelperToolsAvailable } from "./required-tools.js";
|
|
69
|
+
import { preflightSavedPlanImplementation, savedPlanBlocksNewWorkflow } from "./saved-plan-preflight.js";
|
|
64
70
|
import {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
configuredThinkingLevel,
|
|
77
|
-
type PlanModeSettings,
|
|
78
|
-
type PlanModeSettingsPatch,
|
|
79
|
-
planModeSettingsPath,
|
|
80
|
-
readPlanModeSettings,
|
|
81
|
-
type UpdatePlanModeSettingsOptions,
|
|
82
|
-
updatePlanModeSettings,
|
|
71
|
+
awaitPlanModeSettingsWrites,
|
|
72
|
+
configuredImplementationPlanRetention,
|
|
73
|
+
configuredPlanModeToggleShortcut,
|
|
74
|
+
configuredThinkingLevel,
|
|
75
|
+
type ImplementationPlanRetention,
|
|
76
|
+
type PlanModeSettings,
|
|
77
|
+
type PlanModeSettingsPatch,
|
|
78
|
+
planModeSettingsPath,
|
|
79
|
+
readPlanModeSettings,
|
|
80
|
+
type UpdatePlanModeSettingsOptions,
|
|
81
|
+
updatePlanModeSettings,
|
|
83
82
|
} from "./settings.js";
|
|
84
83
|
import {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
type ImplementationRuntimeSelection,
|
|
85
|
+
type PlanCompletionSource,
|
|
86
|
+
type PlanModeState,
|
|
87
|
+
type PlanModeWorkflowToolPolicy,
|
|
88
|
+
restorePlanModeState,
|
|
89
89
|
} from "./state.js";
|
|
90
90
|
import {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
canSelectToolInPlanMode,
|
|
92
|
+
classifyPlanModeTool,
|
|
93
|
+
findBlockedCommandSegment,
|
|
94
|
+
findBlockedPowerShellCommandSegment,
|
|
95
|
+
readCommand,
|
|
96
96
|
} from "./tool-policy.js";
|
|
97
97
|
import { compareTools, snapshotPlanModeSelectedNames, toolPolicyLabel } from "./tool-selection.js";
|
|
98
98
|
import { WorkflowMutex, type WorkflowMutexOwner } from "./workflow-mutex.js";
|
|
99
99
|
|
|
100
100
|
const STATE_ENTRY_TYPE = "plan-mode-state";
|
|
101
101
|
const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
|
|
102
|
+
const RECOVERED_RUNTIME_ADMISSION_INPUT_MESSAGE_TYPE = "plan-mode-recovered-input";
|
|
102
103
|
const BLOCKED_MUTATING_TOOLS = new Set(["edit", "write", "update_plan"]);
|
|
103
104
|
const DEFAULT_TOOLS = ["read", "bash", "edit", "write"];
|
|
104
105
|
interface ReadyPresentationIntent {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
nonce: number;
|
|
107
|
+
plan: string;
|
|
108
|
+
source: PlanCompletionSource;
|
|
109
|
+
}
|
|
110
|
+
interface DeferredFreshImplementation {
|
|
111
|
+
ctx: ExtensionContext;
|
|
112
|
+
sourceSession: object;
|
|
113
|
+
menuGeneration: number;
|
|
114
|
+
workflowGeneration: number;
|
|
115
|
+
workflowOwner: WorkflowMutexOwner | undefined;
|
|
116
|
+
enabled: boolean;
|
|
117
|
+
plan: string;
|
|
118
|
+
source: PlanCompletionSource;
|
|
119
|
+
savedPlan: PlanModeState["savedPlan"];
|
|
120
|
+
retention: ImplementationPlanRetention;
|
|
121
|
+
runtime: ImplementationRuntimeSelection | undefined;
|
|
122
|
+
menuIsCurrent(): boolean;
|
|
108
123
|
}
|
|
109
124
|
interface PendingWorkflowToolPolicy {
|
|
110
|
-
|
|
111
|
-
|
|
125
|
+
generation: number;
|
|
126
|
+
mode: "resolve" | "revalidate";
|
|
127
|
+
}
|
|
128
|
+
type ImplementationRuntimeApplicationResult = "ready" | "blocked" | "stale";
|
|
129
|
+
interface ActiveImplementationRuntimeApplication {
|
|
130
|
+
sessionManager: ExtensionContext["sessionManager"];
|
|
131
|
+
completion: Promise<ImplementationRuntimeApplicationResult>;
|
|
132
|
+
drainOnShutdown: boolean;
|
|
133
|
+
holdForAdmission: boolean;
|
|
134
|
+
}
|
|
135
|
+
interface QueuedRuntimeAdmissionInput {
|
|
136
|
+
sessionManager: ExtensionContext["sessionManager"];
|
|
137
|
+
text: string;
|
|
138
|
+
images?: NonNullable<InputEvent["images"]>;
|
|
139
|
+
source: InputSource;
|
|
112
140
|
}
|
|
113
141
|
type InteractiveUi = typeof import("./interactive-ui.js");
|
|
114
142
|
|
|
115
143
|
interface PlanModeDependencies {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
144
|
+
readSettings?(): ReturnType<typeof readPlanModeSettings>;
|
|
145
|
+
updateSettings?(
|
|
146
|
+
patch: PlanModeSettingsPatch,
|
|
147
|
+
options?: UpdatePlanModeSettingsOptions,
|
|
148
|
+
): ReturnType<typeof updatePlanModeSettings>;
|
|
149
|
+
settingsPath?: string;
|
|
150
|
+
loadInteractiveUi?(): Promise<InteractiveUi>;
|
|
123
151
|
}
|
|
124
152
|
|
|
125
153
|
// Keep session state, persistence, tool, thinking, and mutex commits in this one closure so an
|
|
126
154
|
// activation path cannot bypass the same atomic transition by crossing module-owned state.
|
|
127
155
|
export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDependencies = {}) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
156
|
+
const workflowMutex = new WorkflowMutex(pi);
|
|
157
|
+
let workflowOwner: WorkflowMutexOwner | undefined;
|
|
158
|
+
let currentSession: object | undefined;
|
|
159
|
+
let currentSessionContext: ExtensionContext | undefined;
|
|
160
|
+
let interactiveUiPromise: Promise<InteractiveUi> | undefined;
|
|
161
|
+
const loadInteractiveUi = () => {
|
|
162
|
+
if (dependencies.loadInteractiveUi) return dependencies.loadInteractiveUi();
|
|
163
|
+
if (!interactiveUiPromise) {
|
|
164
|
+
interactiveUiPromise = import("./interactive-ui.js").catch((error) => {
|
|
165
|
+
interactiveUiPromise = undefined;
|
|
166
|
+
throw error;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return interactiveUiPromise;
|
|
170
|
+
};
|
|
171
|
+
const explicitPlanModeSettingsPath = dependencies.settingsPath;
|
|
172
|
+
let state: PlanModeState = { enabled: false, awaitingAction: false };
|
|
173
|
+
let settings: PlanModeSettings = { thinkingLevel: "inherit" };
|
|
174
|
+
let toggleShortcut: ReturnType<typeof configuredPlanModeToggleShortcut>;
|
|
175
|
+
const clearPlanModeShortcutHandler = () => {};
|
|
176
|
+
let workflowAllowedToolNames: string[] | undefined;
|
|
177
|
+
let pendingWorkflowToolPolicy: PendingWorkflowToolPolicy | undefined;
|
|
178
|
+
let publishedContractMode: PlanModeContract | undefined;
|
|
179
|
+
let modeContractsRelevant = false;
|
|
180
|
+
let readyPresentationIntent: ReadyPresentationIntent | undefined;
|
|
181
|
+
let latestCommandContext: ExtensionCommandContext | undefined;
|
|
182
|
+
let stagedFreshImplementation: DeferredFreshImplementation | undefined;
|
|
183
|
+
const deferredFreshHandoff = createDeferredFreshHandoffCoordinator();
|
|
184
|
+
let nextReadyPresentationNonce = 0;
|
|
185
|
+
let menuGeneration = 0;
|
|
186
|
+
let workflowGeneration = 0;
|
|
187
|
+
let refreshStateBeforeFirstAgentStart = false;
|
|
188
|
+
let activeImplementationRuntimeApplication: ActiveImplementationRuntimeApplication | undefined;
|
|
189
|
+
let pendingRuntimeAdmissionSession: object | undefined;
|
|
190
|
+
let queuedRuntimeAdmissionInputs: QueuedRuntimeAdmissionInput[] = [];
|
|
191
|
+
let menuController = new AbortController();
|
|
192
|
+
let settingsWatch: ReturnType<typeof watch> | undefined;
|
|
193
|
+
let settingsReloadTimer: ReturnType<typeof setTimeout> | undefined;
|
|
194
|
+
const implementationRetention = createImplementationRetentionCoordinator();
|
|
195
|
+
const finalizationRequest = createFinalizationRequestCoordinator();
|
|
196
|
+
const persistState = () => pi.appendEntry<PlanModeState>(STATE_ENTRY_TYPE, state);
|
|
197
|
+
const planExports = createPlanExportController({
|
|
198
|
+
getState: () => state,
|
|
199
|
+
getSettings: () => settings,
|
|
200
|
+
finishReady: (ctx) => {
|
|
201
|
+
exitPlanMode(ctx);
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
const planActions = createPlanActionController({
|
|
205
|
+
loadInteractiveUi,
|
|
206
|
+
getState: () => state,
|
|
207
|
+
captureLifecycle: captureMenuLifecycle,
|
|
208
|
+
statusText: planStatusText,
|
|
209
|
+
// ExtensionContext.thinkingLevel was added after the supported Pi 0.80.6 floor.
|
|
210
|
+
// ExtensionAPI has exposed the same runtime value throughout that compatibility range.
|
|
211
|
+
getThinkingLevel: () => pi.getThinkingLevel(),
|
|
212
|
+
getSettings: () => settings,
|
|
213
|
+
implementationOutcome,
|
|
214
|
+
getExportDestination: (ctx) => planExports.getDestination(ctx),
|
|
215
|
+
show: (ctx) => showStoredPlan(pi, ctx, state),
|
|
216
|
+
finalize: requestFinalPlan,
|
|
217
|
+
implementHere: startImplementation,
|
|
218
|
+
implementFresh: startFreshImplementation,
|
|
219
|
+
exportPlan: exportPlan,
|
|
220
|
+
settings: showSettings,
|
|
221
|
+
save: savePlanForLater,
|
|
222
|
+
stay: updateUi,
|
|
223
|
+
exitReady: (ctx) => {
|
|
224
|
+
if (exitPlanMode(ctx)) {
|
|
225
|
+
ctx.ui.notify("Plan mode disabled. Proposed plan discarded.", "info");
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
clearSaved: (ctx) => {
|
|
229
|
+
if (exitPlanMode(ctx)) ctx.ui.notify("Saved plan cleared.", "info");
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
pi.registerTool({
|
|
234
|
+
name: PLAN_MODE_QUESTION_TOOL_NAME,
|
|
235
|
+
label: "Plan question",
|
|
236
|
+
description:
|
|
237
|
+
"Ask one to three structured questions only when the latest effective Plan contract explicitly says /plan mode is active. Tool visibility alone does not activate Plan mode. Never call for ordinary planning requests, the writing-plans skill, roadmaps, checklists, or plan-file work.",
|
|
238
|
+
parameters: PLAN_MODE_QUESTION_PARAMS,
|
|
239
|
+
async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
|
|
240
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
|
|
241
|
+
return planModeQuestionCancelled(
|
|
242
|
+
[],
|
|
243
|
+
"plan_mode_inactive",
|
|
244
|
+
"Error: plan_mode_question is only available while Plan mode is active.",
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const parsed = normalizePlanModeQuestionParams(params);
|
|
249
|
+
if (!parsed.ok) {
|
|
250
|
+
return planModeQuestionCancelled([], "invalid_input", `Error: ${parsed.error}`);
|
|
251
|
+
}
|
|
252
|
+
finalizationRequest.satisfy();
|
|
253
|
+
|
|
254
|
+
if (!ctx.hasUI) {
|
|
255
|
+
return planModeQuestionCancelled(
|
|
256
|
+
parsed.questions,
|
|
257
|
+
"ui_unavailable",
|
|
258
|
+
"Unable to ask Plan-mode questions because interactive UI is not available.",
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const sessionGeneration = menuGeneration;
|
|
263
|
+
const questionWorkflowGeneration = workflowGeneration;
|
|
264
|
+
const questionOwner = workflowOwner;
|
|
265
|
+
return answerPlanModeQuestions(parsed.questions, ctx, {
|
|
266
|
+
isCurrent: () =>
|
|
267
|
+
sessionGeneration === menuGeneration &&
|
|
268
|
+
questionWorkflowGeneration === workflowGeneration &&
|
|
269
|
+
workflowMutex.isOwner(questionOwner),
|
|
270
|
+
isEnabled: () => state.enabled,
|
|
271
|
+
});
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
pi.registerTool({
|
|
276
|
+
name: PLAN_MODE_COMPLETE_TOOL_NAME,
|
|
277
|
+
label: "Complete plan",
|
|
278
|
+
description:
|
|
279
|
+
"Submit a decision-ready plan only when the latest effective Plan contract explicitly says /plan mode is active, and call it alone as the final action. Tool visibility alone does not activate Plan mode. Never call for ordinary planning requests, the writing-plans skill, roadmaps, checklists, or plan-file work.",
|
|
280
|
+
parameters: PLAN_MODE_COMPLETE_PARAMS,
|
|
281
|
+
renderResult: renderPlanModeCompletion,
|
|
282
|
+
async execute(_toolCallId, params: unknown, _signal, _onUpdate, ctx) {
|
|
283
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) {
|
|
284
|
+
throw new Error("plan_mode_complete is only available while Plan mode is active");
|
|
285
|
+
}
|
|
286
|
+
const parsed = normalizePlanModeCompletion(params);
|
|
287
|
+
if (!parsed.ok) throw new Error(parsed.error);
|
|
288
|
+
|
|
289
|
+
acceptCompletedPlan(parsed.plan, PLAN_MODE_COMPLETE_TOOL_NAME, ctx);
|
|
290
|
+
return planModeCompleted(parsed.plan);
|
|
291
|
+
},
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
pi.registerCommand("plan", {
|
|
295
|
+
description: "Enter or manage Codex-like Plan mode",
|
|
296
|
+
getArgumentCompletions: completePlanArguments,
|
|
297
|
+
handler: async (args, ctx) => {
|
|
298
|
+
latestCommandContext = ctx;
|
|
299
|
+
const prompt = args.trim();
|
|
300
|
+
const command = prompt.toLowerCase();
|
|
301
|
+
if (command === "start") {
|
|
302
|
+
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined && !state.enabled)) return;
|
|
303
|
+
if (state.enabled) {
|
|
304
|
+
ctx.ui.notify("Plan mode is already active.", "info");
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (enterPlanMode(ctx)) {
|
|
308
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
309
|
+
}
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (command === "show") {
|
|
313
|
+
showStoredPlan(pi, ctx, state);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (command === "finalize") {
|
|
317
|
+
requestFinalPlan(ctx);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (command === "implement") {
|
|
321
|
+
if (!(state.enabled && state.latestPlan?.trim()) && !state.savedPlan?.plan.trim()) {
|
|
322
|
+
ctx.ui.notify("No completed plan is available to implement.", "warning");
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
await startImplementation(ctx);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (command === "save") {
|
|
329
|
+
savePlanForLater(ctx);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (command === "settings") {
|
|
333
|
+
if (!ctx.hasUI) {
|
|
334
|
+
throw new Error("/plan settings requires TUI or RPC mode and is unavailable here.");
|
|
335
|
+
}
|
|
336
|
+
const lifecycle = captureMenuLifecycle();
|
|
337
|
+
await showSettings(ctx, lifecycle.signal, lifecycle.isCurrent);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const exportMatch = /^export(?:\s+([\s\S]+))?$/iu.exec(prompt);
|
|
341
|
+
if (exportMatch) {
|
|
342
|
+
const lifecycle = captureMenuLifecycle();
|
|
343
|
+
await exportPlan(ctx, exportMatch[1], lifecycle.signal, lifecycle.isCurrent);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (command === "exit" || command === "off") {
|
|
347
|
+
const notification = planModeDisableNotification();
|
|
348
|
+
if (exitPlanMode(ctx)) ctx.ui.notify(notification, "info");
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (command === "tools") {
|
|
352
|
+
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined && !state.enabled)) return;
|
|
353
|
+
if (state.enabled) {
|
|
354
|
+
const message =
|
|
355
|
+
"Plan-mode tools are locked while Planning is active. Exit Plan mode and choose tools before starting again.";
|
|
356
|
+
if (!ctx.hasUI) throw new Error(message);
|
|
357
|
+
ctx.ui.notify(message, "warning");
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (!ctx.hasUI) {
|
|
361
|
+
throw new Error("/plan tools requires TUI or RPC mode and is unavailable here.");
|
|
362
|
+
}
|
|
363
|
+
await showLaunchMenu(ctx, "tools");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (prompt) {
|
|
367
|
+
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined && !state.enabled)) return;
|
|
368
|
+
enterPlanModeWithPrompt(prompt, ctx);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (!ctx.hasUI) {
|
|
372
|
+
throw new Error(
|
|
373
|
+
"The interactive /plan menu is unavailable in print and JSON modes. Use /plan start or /plan <prompt>.",
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
if (!state.enabled) {
|
|
377
|
+
if (state.activeImplementation && ctx.hasUI) {
|
|
378
|
+
await showActivePlanMenu(ctx);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (state.savedPlan) {
|
|
382
|
+
await planActions.showSaved(ctx);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
await showLaunchMenu(ctx);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
await planActions.showCurrent(ctx);
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const applyPlanModeShortcut = (nextShortcut: ReturnType<typeof configuredPlanModeToggleShortcut>) => {
|
|
393
|
+
if (toggleShortcut && toggleShortcut !== nextShortcut) {
|
|
394
|
+
pi.registerShortcut(toggleShortcut, {
|
|
395
|
+
handler: clearPlanModeShortcutHandler,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if (!nextShortcut) {
|
|
399
|
+
toggleShortcut = undefined;
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (toggleShortcut === nextShortcut) return;
|
|
403
|
+
pi.registerShortcut(nextShortcut, {
|
|
404
|
+
description: "Toggle Plan mode",
|
|
405
|
+
handler: (ctx) => {
|
|
406
|
+
togglePlanMode(ctx);
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
toggleShortcut = nextShortcut;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const readPlanModeRuntimeSettings = async () => {
|
|
413
|
+
return dependencies.readSettings?.() ?? readPlanModeSettings(explicitPlanModeSettingsPath);
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const applyPlanModeSettings = async (
|
|
417
|
+
generation: number,
|
|
418
|
+
ctx: ExtensionContext | undefined,
|
|
419
|
+
showWarnings: boolean,
|
|
420
|
+
) => {
|
|
421
|
+
const loadedSettings = await readPlanModeRuntimeSettings();
|
|
422
|
+
if (generation !== menuGeneration || menuController.signal.aborted) {
|
|
423
|
+
return undefined;
|
|
424
|
+
}
|
|
425
|
+
settings =
|
|
426
|
+
loadedSettings.kind === "loaded"
|
|
427
|
+
? loadedSettings.settings
|
|
428
|
+
: ({ thinkingLevel: "inherit" } satisfies PlanModeSettings);
|
|
429
|
+
applyPlanModeShortcut(configuredPlanModeToggleShortcut(settings));
|
|
430
|
+
if (!ctx || !showWarnings) return loadedSettings;
|
|
431
|
+
if (loadedSettings.kind === "invalid") {
|
|
432
|
+
ctx.ui.notify(`pi-plan-mode settings ignored: ${loadedSettings.reason}`, "warning");
|
|
433
|
+
}
|
|
434
|
+
if (loadedSettings.notice) {
|
|
435
|
+
ctx.ui.notify(loadedSettings.notice, "warning");
|
|
436
|
+
}
|
|
437
|
+
return loadedSettings;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const stopPlanModeSettingsWatch = () => {
|
|
441
|
+
if (settingsReloadTimer) {
|
|
442
|
+
clearTimeout(settingsReloadTimer);
|
|
443
|
+
settingsReloadTimer = undefined;
|
|
444
|
+
}
|
|
445
|
+
settingsWatch?.close();
|
|
446
|
+
settingsWatch = undefined;
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
const schedulePlanModeSettingsReload = (generation: number) => {
|
|
450
|
+
if (settingsReloadTimer) {
|
|
451
|
+
clearTimeout(settingsReloadTimer);
|
|
452
|
+
settingsReloadTimer = undefined;
|
|
453
|
+
}
|
|
454
|
+
settingsReloadTimer = setTimeout(() => {
|
|
455
|
+
settingsReloadTimer = undefined;
|
|
456
|
+
void applyPlanModeSettings(generation, currentSessionContext, false);
|
|
457
|
+
}, 75);
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
const startPlanModeSettingsWatch = (generation: number) => {
|
|
461
|
+
stopPlanModeSettingsWatch();
|
|
462
|
+
if (dependencies.readSettings) return;
|
|
463
|
+
const pathToWatch = explicitPlanModeSettingsPath ?? planModeSettingsPath();
|
|
464
|
+
try {
|
|
465
|
+
const directory = dirname(pathToWatch);
|
|
466
|
+
const fileName = basename(pathToWatch);
|
|
467
|
+
const watcher = watch(directory, { persistent: false }, (event, changedFile) => {
|
|
468
|
+
if (event !== "rename" && event !== "change") return;
|
|
469
|
+
if (!changedFile || changedFile.toString() !== fileName) return;
|
|
470
|
+
schedulePlanModeSettingsReload(generation);
|
|
471
|
+
});
|
|
472
|
+
watcher.on("error", () => {
|
|
473
|
+
stopPlanModeSettingsWatch();
|
|
474
|
+
});
|
|
475
|
+
settingsWatch = watcher;
|
|
476
|
+
} catch {
|
|
477
|
+
stopPlanModeSettingsWatch();
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
pi.on("session_start", async (event, ctx) => {
|
|
482
|
+
cancelDeferredFreshImplementation();
|
|
483
|
+
const generation = ++menuGeneration;
|
|
484
|
+
finalizationRequest.reset();
|
|
485
|
+
currentSession = ctx.sessionManager;
|
|
486
|
+
currentSessionContext = ctx;
|
|
487
|
+
workflowOwner = undefined;
|
|
488
|
+
workflowMutex.bindSession(ctx.sessionManager);
|
|
489
|
+
refreshStateBeforeFirstAgentStart = event.reason === "new";
|
|
490
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
491
|
+
queuedRuntimeAdmissionInputs = [];
|
|
492
|
+
menuController.abort(new DOMException("Plan-mode session replaced", "AbortError"));
|
|
493
|
+
menuController = new AbortController();
|
|
494
|
+
readyPresentationIntent = undefined;
|
|
495
|
+
latestCommandContext = undefined;
|
|
496
|
+
workflowAllowedToolNames = undefined;
|
|
497
|
+
pendingWorkflowToolPolicy = undefined;
|
|
498
|
+
implementationRetention.reset();
|
|
499
|
+
settings = { thinkingLevel: "inherit" };
|
|
500
|
+
const branch = ctx.sessionManager.getBranch();
|
|
501
|
+
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
502
|
+
restoreModeContractTracking(branch, restoredState);
|
|
503
|
+
state = { enabled: false, awaitingAction: false };
|
|
504
|
+
await applyPlanModeSettings(generation, ctx, true);
|
|
505
|
+
if (generation !== menuGeneration || menuController.signal.aborted) return;
|
|
506
|
+
startPlanModeSettingsWatch(generation);
|
|
507
|
+
if (!installRestoredState(restoredState, ctx)) return;
|
|
508
|
+
implementationRetention.restore(state.activeImplementation);
|
|
509
|
+
updateUi(ctx);
|
|
510
|
+
// A new session receives its setup entries after session_start, so its input gate refreshes
|
|
511
|
+
// them. Resumed and forked sessions already have the intent and must apply it here because
|
|
512
|
+
// extension-triggered custom-message turns bypass both input and before_agent_start.
|
|
513
|
+
if (event.reason !== "new") await applyPendingImplementationRuntime(ctx);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
pi.on("session_before_tree", (event, ctx) => {
|
|
517
|
+
if (runtimeAdmissionIsPending(ctx.sessionManager)) {
|
|
518
|
+
if (ctx.hasUI) {
|
|
519
|
+
ctx.ui.notify(
|
|
520
|
+
"Wait for fresh implementation startup to admit the pending prompts before changing branches.",
|
|
521
|
+
"warning",
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
return { cancel: true };
|
|
525
|
+
}
|
|
526
|
+
const target = ctx.sessionManager.getEntry(event.preparation.targetId);
|
|
527
|
+
if (target?.type !== "custom_message" || target.customType !== MODE_CONTRACT_MESSAGE_TYPE) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
if (ctx.hasUI) {
|
|
531
|
+
ctx.ui.notify(
|
|
532
|
+
"Plan mode transition markers are internal. Select the adjacent conversation entry instead.",
|
|
533
|
+
"warning",
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
return { cancel: true };
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
540
|
+
cancelDeferredFreshImplementation();
|
|
541
|
+
advanceWorkflowGeneration();
|
|
542
|
+
menuGeneration += 1;
|
|
543
|
+
menuController.abort(new DOMException("Plan-mode tree branch changed", "AbortError"));
|
|
544
|
+
menuController = new AbortController();
|
|
545
|
+
readyPresentationIntent = undefined;
|
|
546
|
+
latestCommandContext = undefined;
|
|
547
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
548
|
+
queuedRuntimeAdmissionInputs = [];
|
|
549
|
+
implementationRetention.reset();
|
|
550
|
+
const branch = ctx.sessionManager.getBranch();
|
|
551
|
+
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
552
|
+
restoreModeContractTracking(branch, restoredState);
|
|
553
|
+
if (!installRestoredState(restoredState, ctx)) return;
|
|
554
|
+
implementationRetention.restore(state.activeImplementation);
|
|
555
|
+
startPlanModeSettingsWatch(menuGeneration);
|
|
556
|
+
updateUi(ctx);
|
|
557
|
+
await applyPendingImplementationRuntime(ctx);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
pi.on("thinking_level_select", (event) => {
|
|
561
|
+
if (!state.enabled || !state.appliedThinkingLevel) return;
|
|
562
|
+
if (event.level !== state.appliedThinkingLevel) {
|
|
563
|
+
state = {
|
|
564
|
+
...state,
|
|
565
|
+
manualThinkingLevel: event.level,
|
|
566
|
+
previousThinkingLevel: undefined,
|
|
567
|
+
appliedThinkingLevel: undefined,
|
|
568
|
+
};
|
|
569
|
+
persistState();
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
574
|
+
cancelDeferredFreshImplementation();
|
|
575
|
+
const shutdownSession = ctx.sessionManager;
|
|
576
|
+
const runtimeApplication =
|
|
577
|
+
activeImplementationRuntimeApplication?.sessionManager === shutdownSession &&
|
|
578
|
+
activeImplementationRuntimeApplication.drainOnShutdown
|
|
579
|
+
? activeImplementationRuntimeApplication
|
|
580
|
+
: undefined;
|
|
581
|
+
const queuedInputs = takeQueuedRuntimeAdmissionInputs(shutdownSession);
|
|
582
|
+
for (const queued of queuedInputs) {
|
|
583
|
+
pi.sendMessage(
|
|
584
|
+
{
|
|
585
|
+
customType: RECOVERED_RUNTIME_ADMISSION_INPUT_MESSAGE_TYPE,
|
|
586
|
+
content: runtimeAdmissionInputContent(queued),
|
|
587
|
+
display: true,
|
|
588
|
+
details: { source: queued.source },
|
|
589
|
+
},
|
|
590
|
+
{ triggerTurn: false },
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
finalizationRequest.reset();
|
|
594
|
+
menuGeneration += 1;
|
|
595
|
+
menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
|
|
596
|
+
readyPresentationIntent = undefined;
|
|
597
|
+
latestCommandContext = undefined;
|
|
598
|
+
refreshStateBeforeFirstAgentStart = false;
|
|
599
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
600
|
+
queuedRuntimeAdmissionInputs = [];
|
|
601
|
+
workflowAllowedToolNames = undefined;
|
|
602
|
+
pendingWorkflowToolPolicy = undefined;
|
|
603
|
+
implementationRetention.reset();
|
|
604
|
+
if (runtimeApplication) await runtimeApplication.completion;
|
|
605
|
+
if (currentSession !== undefined && currentSession !== shutdownSession) {
|
|
606
|
+
workflowMutex.unbindSession(shutdownSession);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
await awaitPlanModeSettingsWrites(dependencies.settingsPath);
|
|
610
|
+
if (currentSession !== undefined && currentSession !== shutdownSession) {
|
|
611
|
+
workflowMutex.unbindSession(shutdownSession);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
captureManualThinkingLevel();
|
|
615
|
+
persistState();
|
|
616
|
+
if (state.enabled) restoreThinkingLevel();
|
|
617
|
+
stopPlanModeSettingsWatch();
|
|
618
|
+
clearUi(ctx);
|
|
619
|
+
releaseWorkflowOwner();
|
|
620
|
+
workflowMutex.unbindSession(ctx.sessionManager);
|
|
621
|
+
if (currentSession === ctx.sessionManager) {
|
|
622
|
+
currentSession = undefined;
|
|
623
|
+
currentSessionContext = undefined;
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
628
|
+
const requiredHelper =
|
|
629
|
+
event.toolName === PLAN_MODE_QUESTION_TOOL_NAME || event.toolName === PLAN_MODE_COMPLETE_TOOL_NAME;
|
|
630
|
+
if (!state.enabled) {
|
|
631
|
+
if (!requiredHelper) return;
|
|
632
|
+
return {
|
|
633
|
+
block: true,
|
|
634
|
+
reason: `${event.toolName} is only available while Plan mode is active.`,
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
if (!workflowMutex.isOwner(workflowOwner)) {
|
|
638
|
+
return {
|
|
639
|
+
block: true,
|
|
640
|
+
reason: `Plan mode blocks tool '${event.toolName}' because workflow ownership is unavailable.`,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
if (BLOCKED_MUTATING_TOOLS.has(event.toolName)) {
|
|
644
|
+
return {
|
|
645
|
+
block: true,
|
|
646
|
+
reason:
|
|
647
|
+
event.toolName === "update_plan"
|
|
648
|
+
? "Plan mode blocks update_plan because it tracks execution progress rather than conversational planning."
|
|
649
|
+
: `Plan mode blocks mutating tool '${event.toolName}'.`,
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
if (requiredHelper) return;
|
|
653
|
+
|
|
654
|
+
const calledTool = toolByName(event.toolName);
|
|
655
|
+
const activeToolNames = new Set(safeGetActiveTools());
|
|
656
|
+
if (!calledTool) {
|
|
657
|
+
return {
|
|
658
|
+
block: true,
|
|
659
|
+
reason: activeToolNames.has(event.toolName)
|
|
660
|
+
? `Plan mode blocks tool '${event.toolName}' because its safe policy metadata is unavailable.`
|
|
661
|
+
: `Plan mode blocks tool '${event.toolName}' because it is not registered or active. Register and activate it before starting the next Plan workflow.`,
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
if (classifyPlanModeTool(calledTool) === "blocked") {
|
|
665
|
+
return {
|
|
666
|
+
block: true,
|
|
667
|
+
reason: `Plan mode blocks tool '${event.toolName}' because its built-in policy is blocked and settings cannot enable it.`,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
const allowedToolNames = new Set(planModePolicyToolNames());
|
|
671
|
+
if (!activeToolNames.has(event.toolName)) {
|
|
672
|
+
return {
|
|
673
|
+
block: true,
|
|
674
|
+
reason: allowedToolNames.has(event.toolName)
|
|
675
|
+
? `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.`
|
|
676
|
+
: `Plan mode blocks tool '${event.toolName}' because it is registered but inactive. Activate it before starting the next Plan workflow.`,
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
if (!allowedToolNames.has(event.toolName)) {
|
|
680
|
+
return {
|
|
681
|
+
block: true,
|
|
682
|
+
reason: workflowDesiredToolNames().has(event.toolName)
|
|
683
|
+
? `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.`
|
|
684
|
+
: `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.`,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
if (event.toolName === "bash") {
|
|
688
|
+
const blocked = findBlockedCommandSegment(readCommand(event.input), settings.safeSubcommands, ctx.cwd);
|
|
689
|
+
if (blocked !== undefined) {
|
|
690
|
+
return {
|
|
691
|
+
block: true,
|
|
692
|
+
reason: `Plan mode blocks bash commands outside its reviewed inspection policy or containing explicitly unsafe arguments.\nBlocked command: ${blocked}`,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (event.toolName === "powershell") {
|
|
697
|
+
const blocked = findBlockedPowerShellCommandSegment(readCommand(event.input), settings.safeSubcommands, ctx.cwd);
|
|
698
|
+
if (blocked !== undefined) {
|
|
699
|
+
return {
|
|
700
|
+
block: true,
|
|
701
|
+
reason: `Plan mode blocks PowerShell commands outside its reviewed inspection policy or containing explicitly unsafe syntax.\nBlocked command: ${blocked}`,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
pi.on("message_start", (event) => {
|
|
708
|
+
if (
|
|
709
|
+
state.enabled &&
|
|
710
|
+
event.message.role === "user" &&
|
|
711
|
+
messageTextContent(event.message).trim() === FINALIZE_PLAN_PROMPT
|
|
712
|
+
) {
|
|
713
|
+
finalizationRequest.request(workflowGeneration);
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
pi.on("context", async (event, ctx) => {
|
|
718
|
+
resolvePendingWorkflowToolPolicy(ctx);
|
|
719
|
+
const result = implementationRetention.transformContext(event.messages, state);
|
|
720
|
+
if (result.clearActiveImplementationId) {
|
|
721
|
+
clearActiveImplementation(result.clearActiveImplementationId, ctx);
|
|
722
|
+
}
|
|
723
|
+
const messages =
|
|
724
|
+
state.enabled || modeContractsRelevant
|
|
725
|
+
? reconcileModeContract(result.messages, state.enabled ? "plan" : "normal")
|
|
726
|
+
: result.messages;
|
|
727
|
+
return { messages: messages as typeof event.messages };
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
pi.on("input", async (event, ctx) => {
|
|
731
|
+
refreshStateForFirstPrompt(ctx);
|
|
732
|
+
const waitsForRuntimeAdmission =
|
|
733
|
+
activeImplementationRuntimeApplication?.sessionManager === ctx.sessionManager ||
|
|
734
|
+
pendingRuntimeAdmissionSession === ctx.sessionManager;
|
|
735
|
+
const queuedInput = waitsForRuntimeAdmission
|
|
736
|
+
? {
|
|
737
|
+
sessionManager: ctx.sessionManager,
|
|
738
|
+
text: event.text,
|
|
739
|
+
...(event.images ? { images: [...event.images] } : {}),
|
|
740
|
+
source: event.source,
|
|
741
|
+
}
|
|
742
|
+
: undefined;
|
|
743
|
+
if (queuedInput) queuedRuntimeAdmissionInputs.push(queuedInput);
|
|
744
|
+
const result = await applyPendingImplementationRuntime(ctx, true);
|
|
745
|
+
if (result !== "ready") {
|
|
746
|
+
if (result === "stale" && queuedInput) removeQueuedRuntimeAdmissionInput(queuedInput);
|
|
747
|
+
return { action: "handled" };
|
|
748
|
+
}
|
|
749
|
+
if (queuedInput) return { action: "handled" };
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
753
|
+
if (currentSession !== ctx.sessionManager) return;
|
|
754
|
+
if (pendingRuntimeAdmissionSession === ctx.sessionManager) {
|
|
755
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
756
|
+
}
|
|
757
|
+
const queuedInputs = takeQueuedRuntimeAdmissionInputs(ctx.sessionManager);
|
|
758
|
+
for (const queued of queuedInputs) {
|
|
759
|
+
pi.sendUserMessage(runtimeAdmissionInputContent(queued), {
|
|
760
|
+
deliverAs: "followUp",
|
|
761
|
+
expandPromptTemplates: queued.source !== "extension",
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
pi.on("before_agent_start", (_event, ctx) => {
|
|
767
|
+
refreshStateForFirstPrompt(ctx);
|
|
768
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
769
|
+
if (state.latestPlan || state.awaitingAction) {
|
|
770
|
+
cancelDeferredFreshImplementation();
|
|
771
|
+
readyPresentationIntent = undefined;
|
|
772
|
+
state = {
|
|
773
|
+
...state,
|
|
774
|
+
latestPlan: undefined,
|
|
775
|
+
latestPlanSource: undefined,
|
|
776
|
+
awaitingAction: false,
|
|
777
|
+
};
|
|
778
|
+
persistState();
|
|
779
|
+
updateUi(ctx);
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
pi.on("agent_end", async (event, ctx) => {
|
|
784
|
+
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
785
|
+
|
|
786
|
+
const text = latestAssistantText(event.messages);
|
|
787
|
+
const parsedPlan = parseProposedPlan(text);
|
|
788
|
+
if (parsedPlan.kind !== "valid") {
|
|
789
|
+
finalizationRequest.observeRunEnd(workflowGeneration, finalizationRunOutcome(event.messages));
|
|
790
|
+
if (parsedPlan.kind !== "absent") {
|
|
791
|
+
ctx.ui.notify(invalidPlanMessage(parsedPlan.kind), "warning");
|
|
792
|
+
}
|
|
793
|
+
persistState();
|
|
794
|
+
updateUi(ctx);
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
acceptCompletedPlan(parsedPlan.plan, "legacy_proposed_plan", ctx);
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
pi.on("agent_settled", async (_event, ctx) => {
|
|
801
|
+
const settledImplementationId = implementationRetention.implementationSettled(state.activeImplementation);
|
|
802
|
+
if (settledImplementationId) clearActiveImplementation(settledImplementationId, ctx);
|
|
803
|
+
|
|
804
|
+
if (finalizationRequest.hasPendingRequest() && state.enabled && workflowMutex.isOwner(workflowOwner)) {
|
|
805
|
+
if (!ctx.isIdle() || ctx.hasPendingMessages()) return;
|
|
806
|
+
const action = finalizationRequest.settle(workflowGeneration);
|
|
807
|
+
if (action === "retry") {
|
|
808
|
+
if (sendPlanModeUserMessage(RETRY_FINALIZE_PLAN_PROMPT, ctx)) return;
|
|
809
|
+
finalizationRequest.reset();
|
|
810
|
+
}
|
|
811
|
+
if (action === "failed") {
|
|
812
|
+
ctx.ui.notify(
|
|
813
|
+
"Plan finalization ended twice without a structured question or completed plan. Plan mode remains active; revise the plan or run /plan finalize again.",
|
|
814
|
+
"warning",
|
|
815
|
+
);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
const intent = readyPresentationIntent;
|
|
820
|
+
if (!intent || !readyPresentationIsCurrent(intent)) return;
|
|
821
|
+
if (!ctx.isIdle() || ctx.hasPendingMessages()) return;
|
|
822
|
+
|
|
823
|
+
readyPresentationIntent = undefined;
|
|
824
|
+
stagedFreshImplementation = undefined;
|
|
825
|
+
try {
|
|
826
|
+
if (intent.source === "legacy_proposed_plan") {
|
|
827
|
+
pi.sendMessage(
|
|
828
|
+
{
|
|
829
|
+
customType: PROPOSED_PLAN_MESSAGE_TYPE,
|
|
830
|
+
content: `**Proposed Plan**\n\n${intent.plan}`,
|
|
831
|
+
display: true,
|
|
832
|
+
},
|
|
833
|
+
{ triggerTurn: false },
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
if (ctx.hasUI && completedPlanIsCurrent(intent)) {
|
|
837
|
+
await planActions.showReady(latestCommandContext ?? ctx);
|
|
838
|
+
}
|
|
839
|
+
const request = stagedFreshImplementation;
|
|
840
|
+
stagedFreshImplementation = undefined;
|
|
841
|
+
if (request) armDeferredFreshImplementation(request);
|
|
842
|
+
} catch (error: unknown) {
|
|
843
|
+
stagedFreshImplementation = undefined;
|
|
844
|
+
if (!isStaleExtensionContextError(error)) throw error;
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
function enterPlanMode(
|
|
849
|
+
ctx: ExtensionContext,
|
|
850
|
+
candidate: Pick<PlanModeState, "selectedToolNames" | "selectedToolKeys"> = state,
|
|
851
|
+
) {
|
|
852
|
+
if (!state.enabled && !allowModeTransition(ctx, "start Plan mode")) return false;
|
|
853
|
+
bindWorkflowSessionIfNeeded(ctx);
|
|
854
|
+
if (state.enabled) return workflowMutex.isOwner(workflowOwner);
|
|
855
|
+
const owner = workflowMutex.acquire();
|
|
856
|
+
if (!owner) return reportWorkflowBusy(ctx);
|
|
857
|
+
workflowOwner = owner;
|
|
858
|
+
|
|
859
|
+
const previousState = state;
|
|
860
|
+
try {
|
|
861
|
+
assertPlanModeHelperToolsAvailable(safeGetActiveTools());
|
|
862
|
+
if (!publishModeContract("plan", ctx)) {
|
|
863
|
+
releaseWorkflowOwner();
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
} catch (error: unknown) {
|
|
867
|
+
releaseWorkflowOwner();
|
|
868
|
+
return reportHelperActivationFailure(ctx, error);
|
|
869
|
+
}
|
|
870
|
+
advanceWorkflowGeneration();
|
|
871
|
+
try {
|
|
872
|
+
modeContractsRelevant = true;
|
|
873
|
+
state = {
|
|
874
|
+
...state,
|
|
875
|
+
enabled: true,
|
|
876
|
+
awaitingAction: false,
|
|
877
|
+
savedPlan: undefined,
|
|
878
|
+
activeImplementation: undefined,
|
|
879
|
+
pendingImplementationRuntime: undefined,
|
|
880
|
+
selectedToolNames: candidate.selectedToolNames,
|
|
881
|
+
selectedToolKeys: candidate.selectedToolKeys,
|
|
882
|
+
};
|
|
883
|
+
beginWorkflowToolPolicy();
|
|
884
|
+
applyPlanThinkingLevel();
|
|
885
|
+
persistState();
|
|
886
|
+
updateUi(ctx);
|
|
887
|
+
return true;
|
|
888
|
+
} catch (error: unknown) {
|
|
889
|
+
rollbackNewActivation(previousState, ctx);
|
|
890
|
+
throw error;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
function enterPlanModeWithPrompt(prompt: string, ctx: ExtensionContext) {
|
|
895
|
+
const previousState = state;
|
|
896
|
+
const previousOwner = workflowOwner;
|
|
897
|
+
const wasEnabled = state.enabled;
|
|
898
|
+
if (!enterPlanMode(ctx)) return;
|
|
899
|
+
if (!wasEnabled) {
|
|
900
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
901
|
+
}
|
|
902
|
+
if (sendPlanModeUserMessage(prompt, ctx)) return;
|
|
903
|
+
if (wasEnabled) return;
|
|
904
|
+
rollbackNewActivation(previousState, ctx, previousOwner);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function exitPlanMode(ctx: ExtensionContext) {
|
|
908
|
+
if (!allowModeTransition(ctx, "leave or clear Plan mode")) return false;
|
|
909
|
+
const wasEnabled = state.enabled;
|
|
910
|
+
if ((wasEnabled || modeContractsRelevant) && !publishModeContract("normal", ctx)) {
|
|
911
|
+
return false;
|
|
912
|
+
}
|
|
913
|
+
advanceWorkflowGeneration();
|
|
914
|
+
readyPresentationIntent = undefined;
|
|
915
|
+
workflowAllowedToolNames = undefined;
|
|
916
|
+
state = {
|
|
917
|
+
...state,
|
|
918
|
+
enabled: false,
|
|
919
|
+
latestPlan: undefined,
|
|
920
|
+
latestPlanSource: undefined,
|
|
921
|
+
awaitingAction: false,
|
|
922
|
+
savedPlan: undefined,
|
|
923
|
+
activeImplementation: undefined,
|
|
924
|
+
pendingImplementationRuntime: undefined,
|
|
925
|
+
workflowToolPolicy: undefined,
|
|
926
|
+
manualThinkingLevel: undefined,
|
|
927
|
+
};
|
|
928
|
+
if (wasEnabled) {
|
|
929
|
+
restoreThinkingLevel();
|
|
930
|
+
state = { ...state, manualThinkingLevel: undefined };
|
|
931
|
+
}
|
|
932
|
+
persistState();
|
|
933
|
+
updateUi(ctx);
|
|
934
|
+
if (wasEnabled) releaseWorkflowOwner();
|
|
935
|
+
return true;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function restoreModeContractTracking(branch: unknown[], restoredState: PlanModeState) {
|
|
939
|
+
publishedContractMode = latestModeContract(branch)?.mode;
|
|
940
|
+
modeContractsRelevant =
|
|
941
|
+
hasModeContractArtifact(branch) ||
|
|
942
|
+
restoredState.enabled ||
|
|
943
|
+
restoredState.savedPlan !== undefined ||
|
|
944
|
+
restoredState.activeImplementation !== undefined ||
|
|
945
|
+
restoredState.pendingImplementationRuntime !== undefined;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function publishModeContract(mode: PlanModeContract, ctx: ExtensionContext) {
|
|
949
|
+
if (publishedContractMode === mode) return true;
|
|
950
|
+
const { role: _role, timestamp: _timestamp, ...message } = createModeContractMessage(mode);
|
|
951
|
+
try {
|
|
952
|
+
pi.sendMessage(message, { triggerTurn: false });
|
|
953
|
+
publishedContractMode = mode;
|
|
954
|
+
modeContractsRelevant = true;
|
|
955
|
+
return true;
|
|
956
|
+
} catch (error: unknown) {
|
|
957
|
+
const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
958
|
+
const notification = `Unable to publish the ${mode === "plan" ? "Plan" : "Normal"} mode contract: ${detail}`;
|
|
959
|
+
if (!ctx.hasUI) throw new Error(notification, { cause: error });
|
|
960
|
+
ctx.ui.notify(notification, "error");
|
|
961
|
+
return false;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
function sendPlanModeUserMessage(message: string, ctx: ExtensionContext) {
|
|
966
|
+
try {
|
|
967
|
+
if (ctx.isIdle()) pi.sendUserMessage(message);
|
|
968
|
+
else pi.sendUserMessage(message, { deliverAs: "followUp" });
|
|
969
|
+
return true;
|
|
970
|
+
} catch (error: unknown) {
|
|
971
|
+
const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
972
|
+
ctx.ui.notify(`Unable to send Plan-mode message: ${detail}`, "error");
|
|
973
|
+
return false;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function acceptCompletedPlan(plan: string, source: PlanCompletionSource, ctx: ExtensionContext) {
|
|
978
|
+
const normalized = normalizePlanModeCompletion({ plan });
|
|
979
|
+
if (!normalized.ok) {
|
|
980
|
+
ctx.ui.notify(`Proposed plan is not ready: ${normalized.error}.`, "warning");
|
|
981
|
+
persistState();
|
|
982
|
+
updateUi(ctx);
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
finalizationRequest.satisfy();
|
|
986
|
+
if (
|
|
987
|
+
state.enabled &&
|
|
988
|
+
state.awaitingAction &&
|
|
989
|
+
state.latestPlan === normalized.plan &&
|
|
990
|
+
state.latestPlanSource === source
|
|
991
|
+
) {
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
state = {
|
|
995
|
+
...state,
|
|
996
|
+
latestPlan: normalized.plan,
|
|
997
|
+
latestPlanSource: source,
|
|
998
|
+
awaitingAction: true,
|
|
999
|
+
};
|
|
1000
|
+
readyPresentationIntent = {
|
|
1001
|
+
nonce: ++nextReadyPresentationNonce,
|
|
1002
|
+
plan: normalized.plan,
|
|
1003
|
+
source,
|
|
1004
|
+
};
|
|
1005
|
+
persistState();
|
|
1006
|
+
updateUi(ctx);
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
function completedPlanIsCurrent(intent: ReadyPresentationIntent) {
|
|
1010
|
+
return (
|
|
1011
|
+
state.enabled &&
|
|
1012
|
+
workflowMutex.isOwner(workflowOwner) &&
|
|
1013
|
+
state.awaitingAction &&
|
|
1014
|
+
state.latestPlan === intent.plan &&
|
|
1015
|
+
state.latestPlanSource === intent.source
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
function readyPresentationIsCurrent(intent: ReadyPresentationIntent) {
|
|
1020
|
+
return completedPlanIsCurrent(intent) && readyPresentationIntent?.nonce === intent.nonce;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function togglePlanMode(ctx: ExtensionContext) {
|
|
1024
|
+
if (state.enabled) {
|
|
1025
|
+
const notification = planModeDisableNotification();
|
|
1026
|
+
if (exitPlanMode(ctx)) ctx.ui.notify(notification, "info");
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined)) return;
|
|
1030
|
+
if (enterPlanMode(ctx)) {
|
|
1031
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
function planModeDisableNotification() {
|
|
1036
|
+
return state.activeImplementation
|
|
1037
|
+
? "Active implementation plan cleared."
|
|
1038
|
+
: state.savedPlan
|
|
1039
|
+
? "Saved plan cleared."
|
|
1040
|
+
: state.latestPlan
|
|
1041
|
+
? "Plan mode disabled. Proposed plan discarded."
|
|
1042
|
+
: "Plan mode disabled.";
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
function requestFinalPlan(ctx: ExtensionContext) {
|
|
1046
|
+
if (!state.enabled) {
|
|
1047
|
+
ctx.ui.notify("Plan mode is not active. Use /plan first.", "warning");
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
finalizationRequest.request(workflowGeneration);
|
|
1051
|
+
if (!sendPlanModeUserMessage(FINALIZE_PLAN_PROMPT, ctx)) finalizationRequest.reset();
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
function savePlanForLater(ctx: ExtensionContext) {
|
|
1055
|
+
const plan = state.enabled ? state.latestPlan?.trim() : undefined;
|
|
1056
|
+
if (!plan) {
|
|
1057
|
+
const message = "No completed plan is available to save.";
|
|
1058
|
+
if (!ctx.hasUI) throw new Error(message);
|
|
1059
|
+
ctx.ui.notify(message, "warning");
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
const source = state.latestPlanSource ?? "legacy_proposed_plan";
|
|
1063
|
+
if (!allowModeTransition(ctx, "save the plan and leave Plan mode")) return;
|
|
1064
|
+
|
|
1065
|
+
if (!publishModeContract("normal", ctx)) return;
|
|
1066
|
+
advanceWorkflowGeneration();
|
|
1067
|
+
readyPresentationIntent = undefined;
|
|
1068
|
+
workflowAllowedToolNames = undefined;
|
|
1069
|
+
state = {
|
|
1070
|
+
...state,
|
|
1071
|
+
enabled: false,
|
|
1072
|
+
latestPlan: undefined,
|
|
1073
|
+
latestPlanSource: undefined,
|
|
1074
|
+
awaitingAction: false,
|
|
1075
|
+
savedPlan: { plan, source },
|
|
1076
|
+
activeImplementation: undefined,
|
|
1077
|
+
pendingImplementationRuntime: undefined,
|
|
1078
|
+
workflowToolPolicy: undefined,
|
|
1079
|
+
manualThinkingLevel: undefined,
|
|
1080
|
+
};
|
|
1081
|
+
restoreThinkingLevel();
|
|
1082
|
+
state = { ...state, manualThinkingLevel: undefined };
|
|
1083
|
+
persistState();
|
|
1084
|
+
updateUi(ctx);
|
|
1085
|
+
releaseWorkflowOwner();
|
|
1086
|
+
ctx.ui.notify("Plan saved for later. Plan mode disabled.", "info");
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
async function startFreshImplementation(
|
|
1090
|
+
ctx: ExtensionContext,
|
|
1091
|
+
menuIsCurrent: () => boolean,
|
|
1092
|
+
runtime: ImplementationRuntimeSelection | undefined,
|
|
1093
|
+
timing: FreshImplementationTiming,
|
|
1094
|
+
) {
|
|
1095
|
+
const retention = configuredImplementationPlanRetention(settings);
|
|
1096
|
+
if (timing === "immediate") {
|
|
1097
|
+
await startFreshImplementationFromState(ctx, {
|
|
1098
|
+
getState: () => state,
|
|
1099
|
+
menuIsCurrent,
|
|
1100
|
+
retention,
|
|
1101
|
+
stateEntryType: STATE_ENTRY_TYPE,
|
|
1102
|
+
runtime,
|
|
1103
|
+
});
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
const initialState = state;
|
|
1108
|
+
const savedPlan = initialState.enabled ? undefined : initialState.savedPlan;
|
|
1109
|
+
const plan = (initialState.enabled ? initialState.latestPlan : savedPlan?.plan)?.trim();
|
|
1110
|
+
const source = initialState.enabled ? initialState.latestPlanSource : savedPlan?.source;
|
|
1111
|
+
if (!plan || !source || !menuIsCurrent()) return;
|
|
1112
|
+
stagedFreshImplementation = {
|
|
1113
|
+
ctx,
|
|
1114
|
+
sourceSession: ctx.sessionManager,
|
|
1115
|
+
menuGeneration,
|
|
1116
|
+
workflowGeneration,
|
|
1117
|
+
workflowOwner,
|
|
1118
|
+
enabled: initialState.enabled,
|
|
1119
|
+
plan,
|
|
1120
|
+
source,
|
|
1121
|
+
savedPlan,
|
|
1122
|
+
retention,
|
|
1123
|
+
runtime: runtime
|
|
1124
|
+
? {
|
|
1125
|
+
...(runtime.model ? { model: { ...runtime.model } } : {}),
|
|
1126
|
+
...(runtime.thinkingLevel ? { thinkingLevel: runtime.thinkingLevel } : {}),
|
|
1127
|
+
}
|
|
1128
|
+
: undefined,
|
|
1129
|
+
menuIsCurrent,
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
function armDeferredFreshImplementation(request: DeferredFreshImplementation) {
|
|
1134
|
+
deferredFreshHandoff.schedule(
|
|
1135
|
+
async (taskIsCurrent) => {
|
|
1136
|
+
const isCurrent = () => taskIsCurrent() && deferredFreshImplementationIsCurrent(request);
|
|
1137
|
+
if (!isCurrent()) return;
|
|
1138
|
+
await startFreshImplementationFromState(request.ctx, {
|
|
1139
|
+
getState: () => state,
|
|
1140
|
+
menuIsCurrent: isCurrent,
|
|
1141
|
+
retention: request.retention,
|
|
1142
|
+
stateEntryType: STATE_ENTRY_TYPE,
|
|
1143
|
+
runtime: request.runtime,
|
|
1144
|
+
});
|
|
1145
|
+
},
|
|
1146
|
+
(error) => {
|
|
1147
|
+
if (!deferredFreshImplementationIsCurrent(request)) return;
|
|
1148
|
+
try {
|
|
1149
|
+
request.ctx.ui.notify(
|
|
1150
|
+
`Unable to start the deferred fresh implementation: ${terminalErrorDetail(error)}`,
|
|
1151
|
+
"error",
|
|
1152
|
+
);
|
|
1153
|
+
} catch {
|
|
1154
|
+
// The source context can become stale while a detached failure is reported.
|
|
1155
|
+
}
|
|
1156
|
+
},
|
|
1157
|
+
);
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
function deferredFreshImplementationIsCurrent(request: DeferredFreshImplementation) {
|
|
1161
|
+
if (
|
|
1162
|
+
currentSession !== request.sourceSession ||
|
|
1163
|
+
menuGeneration !== request.menuGeneration ||
|
|
1164
|
+
workflowGeneration !== request.workflowGeneration ||
|
|
1165
|
+
workflowOwner !== request.workflowOwner ||
|
|
1166
|
+
!request.menuIsCurrent() ||
|
|
1167
|
+
state.enabled !== request.enabled
|
|
1168
|
+
) {
|
|
1169
|
+
return false;
|
|
1170
|
+
}
|
|
1171
|
+
return request.enabled
|
|
1172
|
+
? workflowMutex.isOwner(request.workflowOwner) &&
|
|
1173
|
+
state.latestPlan === request.plan &&
|
|
1174
|
+
state.latestPlanSource === request.source
|
|
1175
|
+
: state.savedPlan === request.savedPlan;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
function cancelDeferredFreshImplementation() {
|
|
1179
|
+
stagedFreshImplementation = undefined;
|
|
1180
|
+
deferredFreshHandoff.cancel();
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
async function startImplementation(ctx: ExtensionContext) {
|
|
1184
|
+
const savedPlan = state.enabled ? undefined : state.savedPlan;
|
|
1185
|
+
const initialPlan = (state.enabled ? state.latestPlan : savedPlan?.plan)?.trim();
|
|
1186
|
+
if (!initialPlan) {
|
|
1187
|
+
ctx.ui.notify("Plan mode disabled. No proposed plan is available to implement.", "warning");
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
if (!allowModeTransition(ctx, "start plan implementation")) return;
|
|
1191
|
+
if (savedPlan) {
|
|
1192
|
+
const sessionGeneration = menuGeneration;
|
|
1193
|
+
const planWorkflowGeneration = workflowGeneration;
|
|
1194
|
+
const isCurrent = () =>
|
|
1195
|
+
sessionGeneration === menuGeneration &&
|
|
1196
|
+
planWorkflowGeneration === workflowGeneration &&
|
|
1197
|
+
!menuController.signal.aborted &&
|
|
1198
|
+
!state.enabled &&
|
|
1199
|
+
state.savedPlan === savedPlan;
|
|
1200
|
+
if (!(await preflightSavedPlanImplementation(ctx, isCurrent))) return;
|
|
1201
|
+
if (!allowModeTransition(ctx, "start plan implementation")) return;
|
|
1202
|
+
}
|
|
1203
|
+
const plan = (state.enabled ? state.latestPlan : savedPlan?.plan)?.trim();
|
|
1204
|
+
const source = (state.enabled ? state.latestPlanSource : savedPlan?.source) ?? "legacy_proposed_plan";
|
|
1205
|
+
if (!plan) return;
|
|
1206
|
+
|
|
1207
|
+
const previousState = state;
|
|
1208
|
+
const previousIntent = readyPresentationIntent;
|
|
1209
|
+
const wasEnabled = state.enabled;
|
|
1210
|
+
if (!publishModeContract("normal", ctx)) return;
|
|
1211
|
+
advanceWorkflowGeneration();
|
|
1212
|
+
const retention = configuredImplementationPlanRetention(settings);
|
|
1213
|
+
const usesConversationHistory = retention === "clear-on-start";
|
|
1214
|
+
readyPresentationIntent = undefined;
|
|
1215
|
+
workflowAllowedToolNames = undefined;
|
|
1216
|
+
state = {
|
|
1217
|
+
...state,
|
|
1218
|
+
enabled: false,
|
|
1219
|
+
latestPlan: undefined,
|
|
1220
|
+
latestPlanSource: undefined,
|
|
1221
|
+
awaitingAction: false,
|
|
1222
|
+
savedPlan: undefined,
|
|
1223
|
+
pendingImplementationRuntime: undefined,
|
|
1224
|
+
activeImplementation: usesConversationHistory
|
|
1225
|
+
? undefined
|
|
1226
|
+
: {
|
|
1227
|
+
id: randomUUID(),
|
|
1228
|
+
plan,
|
|
1229
|
+
source,
|
|
1230
|
+
startedAt: Date.now(),
|
|
1231
|
+
retention,
|
|
1232
|
+
},
|
|
1233
|
+
workflowToolPolicy: undefined,
|
|
1234
|
+
manualThinkingLevel: undefined,
|
|
1235
|
+
};
|
|
1236
|
+
if (wasEnabled) {
|
|
1237
|
+
restoreThinkingLevel();
|
|
1238
|
+
state = { ...state, manualThinkingLevel: undefined };
|
|
1239
|
+
}
|
|
1240
|
+
persistState();
|
|
1241
|
+
updateUi(ctx);
|
|
1242
|
+
|
|
1243
|
+
const handoff = usesConversationHistory
|
|
1244
|
+
? wasEnabled
|
|
1245
|
+
? formatHistoryImplementationPrompt()
|
|
1246
|
+
: formatTransferredPlanPrompt(plan, false)
|
|
1247
|
+
: formatImplementationHandoff(plan);
|
|
1248
|
+
const sent = sendPlanModeUserMessage(handoff, ctx);
|
|
1249
|
+
if (!sent) {
|
|
1250
|
+
state = previousState;
|
|
1251
|
+
readyPresentationIntent = previousIntent;
|
|
1252
|
+
if (wasEnabled) {
|
|
1253
|
+
restoreWorkflowToolPolicy(state.workflowToolPolicy);
|
|
1254
|
+
publishModeContract("plan", ctx);
|
|
1255
|
+
applyPlanThinkingLevel();
|
|
1256
|
+
}
|
|
1257
|
+
persistState();
|
|
1258
|
+
updateUi(ctx);
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
if (wasEnabled) releaseWorkflowOwner();
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
function clearActiveImplementation(id: string, ctx: ExtensionContext) {
|
|
1265
|
+
if (state.activeImplementation?.id !== id) return false;
|
|
1266
|
+
advanceWorkflowGeneration();
|
|
1267
|
+
state = { ...state, activeImplementation: undefined };
|
|
1268
|
+
persistState();
|
|
1269
|
+
updateUi(ctx);
|
|
1270
|
+
return true;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
async function exportPlan(
|
|
1274
|
+
ctx: ExtensionContext,
|
|
1275
|
+
path: string | undefined,
|
|
1276
|
+
signal: AbortSignal,
|
|
1277
|
+
isCurrent: () => boolean,
|
|
1278
|
+
) {
|
|
1279
|
+
const exitsReadyPlan = state.enabled && Boolean(state.latestPlan?.trim());
|
|
1280
|
+
if (exitsReadyPlan && !allowModeTransition(ctx, "export the ready plan and leave Plan mode")) {
|
|
1281
|
+
return false;
|
|
1282
|
+
}
|
|
1283
|
+
return planExports.export(path, ctx, signal, () => {
|
|
1284
|
+
return isCurrent() && (!exitsReadyPlan || ctx.isIdle());
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
async function showLaunchMenu(ctx: ExtensionContext, initialScreen: "main" | "tools" = "main") {
|
|
1289
|
+
const lifecycle = captureMenuLifecycle();
|
|
1290
|
+
if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
|
|
1291
|
+
const ui = await loadInteractiveUi();
|
|
1292
|
+
if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
|
|
1293
|
+
const tools = selectableTools();
|
|
1294
|
+
const activeToolNames = new Set(safeGetActiveTools());
|
|
1295
|
+
const initialSelectedNames = snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot());
|
|
1296
|
+
const retainsInactiveSelection =
|
|
1297
|
+
state.selectedToolNames !== undefined ||
|
|
1298
|
+
state.selectedToolKeys !== undefined ||
|
|
1299
|
+
settings.defaultPlanTools !== undefined;
|
|
1300
|
+
const retainedInactiveNames = retainsInactiveSelection ? initialSelectedNames : new Set<string>();
|
|
1301
|
+
const registeredNames = new Set(tools.map((tool) => tool.name));
|
|
1302
|
+
const pendingNames = Array.from(retainedInactiveNames).filter((name) => !registeredNames.has(name));
|
|
1303
|
+
await ui.showPlanLaunchMenu(ctx, {
|
|
1304
|
+
statusText: planModeHelperToolsAvailable(safeGetActiveTools())
|
|
1305
|
+
? "Status: Off — visible Plan helpers stay inactive until /plan starts."
|
|
1306
|
+
: "Status: Off — required Plan helpers are unavailable under the active tool policy.",
|
|
1307
|
+
initialScreen,
|
|
1308
|
+
getSelectedNames: () => snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot()),
|
|
1309
|
+
toolSummary: (selectedNames) => {
|
|
1310
|
+
const allowed = tools
|
|
1311
|
+
.filter(
|
|
1312
|
+
(tool) => activeToolNames.has(tool.name) && selectedNames.has(tool.name) && canSelectToolInPlanMode(tool),
|
|
1313
|
+
)
|
|
1314
|
+
.map((tool) => tool.name);
|
|
1315
|
+
const pending = pendingNames.filter((name) => selectedNames.has(name)).map(terminalToolName);
|
|
1316
|
+
const visiblePending = pending.slice(0, 3);
|
|
1317
|
+
const pendingSuffix =
|
|
1318
|
+
pending.length > visiblePending.length ? `, +${pending.length - visiblePending.length} more` : "";
|
|
1319
|
+
return [
|
|
1320
|
+
`Plan policy will allow: ${allowed.length > 0 ? allowed.join(", ") : "none"}.`,
|
|
1321
|
+
...(pending.length > 0 ? [`Pending registration: ${visiblePending.join(", ")}${pendingSuffix}.`] : []),
|
|
1322
|
+
].join(" ");
|
|
1323
|
+
},
|
|
1324
|
+
tools: [
|
|
1325
|
+
...tools.map((tool) => {
|
|
1326
|
+
const selectable = canSelectToolInPlanMode(tool);
|
|
1327
|
+
const active = activeToolNames.has(tool.name);
|
|
1328
|
+
const retained = retainedInactiveNames.has(tool.name);
|
|
1329
|
+
const policy = active
|
|
1330
|
+
? toolPolicyLabel(tool)
|
|
1331
|
+
: retained
|
|
1332
|
+
? "not active yet; retained for first-request resolution"
|
|
1333
|
+
: "not active in this Pi session";
|
|
1334
|
+
const description = tool.description ?? "No description available";
|
|
1335
|
+
return {
|
|
1336
|
+
name: tool.name,
|
|
1337
|
+
description: `${policy} · ${description}`,
|
|
1338
|
+
searchText: [policy, description].join(" "),
|
|
1339
|
+
disabled: !selectable || !active,
|
|
1340
|
+
disabledReason: !active
|
|
1341
|
+
? retained
|
|
1342
|
+
? "Not active yet; retained and resolved before the first request"
|
|
1343
|
+
: "Not active in Pi; Plan mode will not activate it"
|
|
1344
|
+
: selectable
|
|
1345
|
+
? undefined
|
|
1346
|
+
: "Blocked by Plan-mode policy",
|
|
1347
|
+
};
|
|
1348
|
+
}),
|
|
1349
|
+
...pendingNames.map((name) => {
|
|
1350
|
+
const label = terminalToolName(name);
|
|
1351
|
+
return {
|
|
1352
|
+
name,
|
|
1353
|
+
label,
|
|
1354
|
+
description: "pending registration · Retained and resolved before the first Plan request",
|
|
1355
|
+
searchText: `${label} pending registration retained first Plan request`,
|
|
1356
|
+
disabled: true,
|
|
1357
|
+
disabledReason:
|
|
1358
|
+
"Not registered yet; Plan mode will not activate it and will resolve it before the first request",
|
|
1359
|
+
};
|
|
1360
|
+
}),
|
|
1361
|
+
],
|
|
1362
|
+
...lifecycle,
|
|
1363
|
+
start: (signal) => {
|
|
1364
|
+
if (signal.aborted || !lifecycle.isCurrent()) return;
|
|
1365
|
+
if (enterPlanMode(ctx)) {
|
|
1366
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
1367
|
+
}
|
|
1368
|
+
},
|
|
1369
|
+
startWithTools: (names, signal) => {
|
|
1370
|
+
if (signal.aborted || !lifecycle.isCurrent()) return;
|
|
1371
|
+
const selectedToolNames = Array.from(
|
|
1372
|
+
new Set(names.filter((name) => activeToolNames.has(name) || retainedInactiveNames.has(name))),
|
|
1373
|
+
);
|
|
1374
|
+
if (enterPlanMode(ctx, { selectedToolNames, selectedToolKeys: undefined })) {
|
|
1375
|
+
ctx.ui.notify("Plan mode enabled with the selected tools.", "info");
|
|
1376
|
+
}
|
|
1377
|
+
},
|
|
1378
|
+
settings: (signal) => showSettings(ctx, signal, lifecycle.isCurrent),
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
async function showActivePlanMenu(ctx: ExtensionContext) {
|
|
1383
|
+
if (!ctx.hasUI) {
|
|
1384
|
+
ctx.ui.notify(planStatusText(), "info");
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
const lifecycle = captureMenuLifecycle();
|
|
1388
|
+
if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
|
|
1389
|
+
const ui = await loadInteractiveUi();
|
|
1390
|
+
if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
|
|
1391
|
+
await ui.showActiveImplementationMenu(ctx, {
|
|
1392
|
+
statusText: planStatusText(),
|
|
1393
|
+
getExportDestination: () => planExports.getDestination(ctx),
|
|
1394
|
+
signal: lifecycle.signal,
|
|
1395
|
+
isCurrent: lifecycle.isCurrent,
|
|
1396
|
+
show: () => showStoredPlan(pi, ctx, state),
|
|
1397
|
+
exportPlan: (path, signal) => planExports.export(path, ctx, signal, lifecycle.isCurrent),
|
|
1398
|
+
settings: (signal) => showSettings(ctx, signal, lifecycle.isCurrent),
|
|
1399
|
+
startNew: () => {
|
|
1400
|
+
if (enterPlanMode(ctx)) {
|
|
1401
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
1402
|
+
}
|
|
1403
|
+
},
|
|
1404
|
+
clear: () => {
|
|
1405
|
+
if (exitPlanMode(ctx)) ctx.ui.notify("Active implementation plan cleared.", "info");
|
|
1406
|
+
},
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
async function showSettings(ctx: ExtensionContext, signal: AbortSignal, isCurrent: () => boolean) {
|
|
1411
|
+
if (!isCurrent() || signal.aborted) return false;
|
|
1412
|
+
const ui = await loadInteractiveUi();
|
|
1413
|
+
if (!isCurrent() || signal.aborted) return false;
|
|
1414
|
+
const result = await ui.showPlanModeSettings(ctx, {
|
|
1415
|
+
tools: selectableTools(),
|
|
1416
|
+
activeToolNames: safeGetActiveTools(),
|
|
1417
|
+
signal,
|
|
1418
|
+
isCurrent,
|
|
1419
|
+
settingsPath: dependencies.settingsPath,
|
|
1420
|
+
updateSettings: dependencies.updateSettings ?? updatePlanModeSettings,
|
|
1421
|
+
onSaved: (saved) => {
|
|
1422
|
+
if (!isCurrent()) return;
|
|
1423
|
+
settings = saved;
|
|
1424
|
+
applyPlanModeShortcut(configuredPlanModeToggleShortcut(saved));
|
|
1425
|
+
},
|
|
1426
|
+
...(dependencies.readSettings
|
|
1427
|
+
? { readSettings: async () => dependencies.readSettings?.() ?? { kind: "missing" } }
|
|
1428
|
+
: {}),
|
|
1429
|
+
});
|
|
1430
|
+
return result.kind === "closed" && "reason" in result && result.reason === "close";
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
function allowModeTransition(ctx: ExtensionContext, action: string) {
|
|
1434
|
+
if (ctx.isIdle()) return true;
|
|
1435
|
+
const message = `Cannot ${action} while an agent run is active. Wait for the run to settle, then retry.`;
|
|
1436
|
+
if (!ctx.hasUI) throw new Error(message);
|
|
1437
|
+
ctx.ui.notify(message, "warning");
|
|
1438
|
+
return false;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
function advanceWorkflowGeneration() {
|
|
1442
|
+
cancelDeferredFreshImplementation();
|
|
1443
|
+
workflowGeneration += 1;
|
|
1444
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1445
|
+
finalizationRequest.reset();
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
function finalizationRunOutcome(messages: unknown): FinalizationRunOutcome {
|
|
1449
|
+
const stopReason = latestAssistantStopReason(messages);
|
|
1450
|
+
if (stopReason === undefined || stopReason === "stop") return "normal";
|
|
1451
|
+
if (stopReason === "aborted") return "cancelled";
|
|
1452
|
+
return "error";
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
function captureMenuLifecycle() {
|
|
1456
|
+
const sessionGeneration = menuGeneration;
|
|
1457
|
+
const planWorkflowGeneration = workflowGeneration;
|
|
1458
|
+
const owner = workflowOwner;
|
|
1459
|
+
const controller = menuController;
|
|
1460
|
+
return {
|
|
1461
|
+
signal: controller.signal,
|
|
1462
|
+
isCurrent: () =>
|
|
1463
|
+
sessionGeneration === menuGeneration &&
|
|
1464
|
+
planWorkflowGeneration === workflowGeneration &&
|
|
1465
|
+
!controller.signal.aborted &&
|
|
1466
|
+
(!state.enabled || workflowMutex.isOwner(owner)),
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
function planModePolicyToolNames() {
|
|
1471
|
+
if (state.enabled) return workflowAllowedToolNames ?? [];
|
|
1472
|
+
return computePlanModePolicyToolNames();
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
function workflowDesiredToolNames() {
|
|
1476
|
+
const policy = state.workflowToolPolicy;
|
|
1477
|
+
if (!state.enabled || !policy) return new Set<string>();
|
|
1478
|
+
return new Set(policy.kind === "automatic" ? automaticPlanModeToolNames() : (policy.desiredNames ?? []));
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
function beginWorkflowToolPolicy() {
|
|
1482
|
+
const kind = toolPolicySelectionIsExplicit() ? "explicit" : "automatic";
|
|
1483
|
+
const desiredNames = desiredPlanModeToolNames();
|
|
1484
|
+
const allowedNames = resolvePlanModePolicyToolNames(desiredNames);
|
|
1485
|
+
const policy: PlanModeWorkflowToolPolicy = {
|
|
1486
|
+
kind,
|
|
1487
|
+
...(kind === "explicit" ? { desiredNames } : {}),
|
|
1488
|
+
allowedNames,
|
|
1489
|
+
resolved: false,
|
|
1490
|
+
};
|
|
1491
|
+
state = { ...state, workflowToolPolicy: policy };
|
|
1492
|
+
pendingWorkflowToolPolicy = { generation: workflowGeneration, mode: "resolve" };
|
|
1493
|
+
workflowAllowedToolNames = allowedNames;
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
function resolvePendingWorkflowToolPolicy(ctx: ExtensionContext) {
|
|
1497
|
+
const pending = pendingWorkflowToolPolicy;
|
|
1498
|
+
if (!pending) return;
|
|
1499
|
+
if (pending.generation !== workflowGeneration || !state.enabled || !workflowMutex.isOwner(workflowOwner)) {
|
|
1500
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1501
|
+
return;
|
|
1502
|
+
}
|
|
1503
|
+
const policy = state.workflowToolPolicy;
|
|
1504
|
+
const expectedResolved = pending.mode === "revalidate";
|
|
1505
|
+
if (!policy || policy.resolved !== expectedResolved) {
|
|
1506
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
const allowedNames =
|
|
1510
|
+
pending.mode === "resolve" ? resolveWorkflowToolPolicy(policy) : revalidateFrozenWorkflowToolPolicy(policy);
|
|
1511
|
+
const policyChanged = !policy.resolved || !arrayEquals(policy.allowedNames, allowedNames);
|
|
1512
|
+
workflowAllowedToolNames = allowedNames;
|
|
1513
|
+
state = {
|
|
1514
|
+
...state,
|
|
1515
|
+
workflowToolPolicy: { ...policy, allowedNames, resolved: true },
|
|
1516
|
+
};
|
|
1517
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1518
|
+
if (policyChanged) persistState();
|
|
1519
|
+
updateUi(ctx);
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
function toolPolicySelectionIsExplicit() {
|
|
1523
|
+
return (
|
|
1524
|
+
state.selectedToolNames !== undefined ||
|
|
1525
|
+
state.selectedToolKeys !== undefined ||
|
|
1526
|
+
settings.defaultPlanTools !== undefined
|
|
1527
|
+
);
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
function desiredPlanModeToolNames() {
|
|
1531
|
+
const tools = activePlanPolicyTools();
|
|
1532
|
+
return Array.from(snapshotPlanModeSelectedNames(tools, toolSelectionSnapshot()));
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
function automaticPlanModeToolNames() {
|
|
1536
|
+
return Array.from(snapshotPlanModeSelectedNames(activePlanPolicyTools(), {}));
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
function resolveWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy) {
|
|
1540
|
+
return resolvePlanModePolicyToolNames(
|
|
1541
|
+
policy.kind === "automatic" ? automaticPlanModeToolNames() : (policy.desiredNames ?? []),
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
function revalidateFrozenWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy) {
|
|
1546
|
+
const currentlyAllowed = new Set(
|
|
1547
|
+
policy.kind === "automatic"
|
|
1548
|
+
? resolvePlanModePolicyToolNames(automaticPlanModeToolNames())
|
|
1549
|
+
: resolvePlanModePolicyToolNames(policy.allowedNames),
|
|
1550
|
+
);
|
|
1551
|
+
return policy.allowedNames.filter((name) => currentlyAllowed.has(name));
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
function restoreWorkflowToolPolicy(policy: PlanModeWorkflowToolPolicy | undefined) {
|
|
1555
|
+
let nextPolicy: PlanModeWorkflowToolPolicy;
|
|
1556
|
+
if (!policy) {
|
|
1557
|
+
const kind = toolPolicySelectionIsExplicit() ? "explicit" : "automatic";
|
|
1558
|
+
const desiredNames = desiredPlanModeToolNames();
|
|
1559
|
+
nextPolicy = {
|
|
1560
|
+
kind,
|
|
1561
|
+
...(kind === "explicit" ? { desiredNames } : {}),
|
|
1562
|
+
allowedNames: resolvePlanModePolicyToolNames(desiredNames),
|
|
1563
|
+
resolved: true,
|
|
1564
|
+
};
|
|
1565
|
+
} else if (policy.resolved) {
|
|
1566
|
+
nextPolicy = policy;
|
|
1567
|
+
} else {
|
|
1568
|
+
nextPolicy = {
|
|
1569
|
+
...policy,
|
|
1570
|
+
allowedNames: resolveWorkflowToolPolicy(policy),
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
state = { ...state, workflowToolPolicy: nextPolicy };
|
|
1574
|
+
workflowAllowedToolNames = nextPolicy.resolved
|
|
1575
|
+
? revalidateFrozenWorkflowToolPolicy(nextPolicy)
|
|
1576
|
+
: nextPolicy.allowedNames;
|
|
1577
|
+
pendingWorkflowToolPolicy = {
|
|
1578
|
+
generation: workflowGeneration,
|
|
1579
|
+
mode: nextPolicy.resolved ? "revalidate" : "resolve",
|
|
1580
|
+
};
|
|
1581
|
+
return !workflowToolPoliciesEqual(policy, nextPolicy);
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
function workflowToolPoliciesEqual(left: PlanModeWorkflowToolPolicy | undefined, right: PlanModeWorkflowToolPolicy) {
|
|
1585
|
+
return (
|
|
1586
|
+
left?.kind === right.kind &&
|
|
1587
|
+
left.resolved === right.resolved &&
|
|
1588
|
+
arrayEquals(left.allowedNames, right.allowedNames) &&
|
|
1589
|
+
arrayEquals(left.desiredNames ?? [], right.desiredNames ?? [])
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
function arrayEquals(left: readonly string[], right: readonly string[]) {
|
|
1594
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
function computePlanModePolicyToolNames() {
|
|
1598
|
+
return resolvePlanModePolicyToolNames(desiredPlanModeToolNames());
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
function resolvePlanModePolicyToolNames(desiredNames: readonly string[]) {
|
|
1602
|
+
const selectedNames = new Set(desiredNames);
|
|
1603
|
+
return activePlanPolicyTools()
|
|
1604
|
+
.filter((tool) => selectedNames.has(tool.name) && canSelectToolInPlanMode(tool))
|
|
1605
|
+
.map((tool) => tool.name);
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
function toolSelectionSnapshot() {
|
|
1609
|
+
return {
|
|
1610
|
+
selectedToolNames: state.selectedToolNames,
|
|
1611
|
+
selectedToolKeys: state.selectedToolKeys,
|
|
1612
|
+
defaultPlanTools: settings.defaultPlanTools,
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
function selectableTools() {
|
|
1617
|
+
return safeGetAllTools()
|
|
1618
|
+
.filter((tool) => tool.name !== PLAN_MODE_QUESTION_TOOL_NAME && tool.name !== PLAN_MODE_COMPLETE_TOOL_NAME)
|
|
1619
|
+
.sort(compareTools);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
function activePlanPolicyTools() {
|
|
1623
|
+
const activeNames = new Set(safeGetActiveTools());
|
|
1624
|
+
return selectableTools().filter((tool) => activeNames.has(tool.name));
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
function safeGetAllTools() {
|
|
1628
|
+
try {
|
|
1629
|
+
return pi.getAllTools();
|
|
1630
|
+
} catch {
|
|
1631
|
+
return [];
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function runtimeAdmissionIsPending(sessionManager: ExtensionContext["sessionManager"]) {
|
|
1636
|
+
return (
|
|
1637
|
+
activeImplementationRuntimeApplication?.sessionManager === sessionManager ||
|
|
1638
|
+
pendingRuntimeAdmissionSession === sessionManager ||
|
|
1639
|
+
queuedRuntimeAdmissionInputs.some((queued) => queued.sessionManager === sessionManager)
|
|
1640
|
+
);
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
function takeQueuedRuntimeAdmissionInputs(sessionManager: ExtensionContext["sessionManager"]) {
|
|
1644
|
+
const matching: QueuedRuntimeAdmissionInput[] = [];
|
|
1645
|
+
const remaining: QueuedRuntimeAdmissionInput[] = [];
|
|
1646
|
+
for (const queued of queuedRuntimeAdmissionInputs) {
|
|
1647
|
+
if (queued.sessionManager === sessionManager) matching.push(queued);
|
|
1648
|
+
else remaining.push(queued);
|
|
1649
|
+
}
|
|
1650
|
+
queuedRuntimeAdmissionInputs = remaining;
|
|
1651
|
+
return matching;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
function removeQueuedRuntimeAdmissionInput(queuedInput: QueuedRuntimeAdmissionInput) {
|
|
1655
|
+
const index = queuedRuntimeAdmissionInputs.indexOf(queuedInput);
|
|
1656
|
+
if (index >= 0) queuedRuntimeAdmissionInputs.splice(index, 1);
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
function runtimeAdmissionInputContent(queued: QueuedRuntimeAdmissionInput) {
|
|
1660
|
+
return queued.images?.length ? [{ type: "text" as const, text: queued.text }, ...queued.images] : queued.text;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
function refreshStateForFirstPrompt(ctx: ExtensionContext) {
|
|
1664
|
+
if (!refreshStateBeforeFirstAgentStart) return;
|
|
1665
|
+
refreshStateBeforeFirstAgentStart = false;
|
|
1666
|
+
implementationRetention.reset();
|
|
1667
|
+
const branch = ctx.sessionManager.getBranch();
|
|
1668
|
+
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
1669
|
+
restoreModeContractTracking(branch, restoredState);
|
|
1670
|
+
if (!installRestoredState(restoredState, ctx)) return;
|
|
1671
|
+
implementationRetention.restore(state.activeImplementation);
|
|
1672
|
+
updateUi(ctx);
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
async function applyPendingImplementationRuntime(
|
|
1676
|
+
ctx: ExtensionContext,
|
|
1677
|
+
holdForAdmission = false,
|
|
1678
|
+
): Promise<ImplementationRuntimeApplicationResult> {
|
|
1679
|
+
const session = ctx.sessionManager;
|
|
1680
|
+
const activeApplication = activeImplementationRuntimeApplication;
|
|
1681
|
+
if (activeApplication?.sessionManager === session) {
|
|
1682
|
+
if (holdForAdmission) activeApplication.holdForAdmission = true;
|
|
1683
|
+
return activeApplication.completion;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
const intent = state.enabled ? undefined : state.pendingImplementationRuntime;
|
|
1687
|
+
if (!intent) return "ready";
|
|
1688
|
+
const generation = menuGeneration;
|
|
1689
|
+
const isCurrent = () =>
|
|
1690
|
+
currentSession === session && generation === menuGeneration && !menuController.signal.aborted;
|
|
1691
|
+
const notifyCurrent = (message: string) => {
|
|
1692
|
+
if (!isCurrent()) return;
|
|
1693
|
+
try {
|
|
1694
|
+
ctx.ui.notify(message, "warning");
|
|
1695
|
+
} catch {
|
|
1696
|
+
// The session can become stale while an asynchronous runtime application settles.
|
|
1697
|
+
}
|
|
1698
|
+
};
|
|
1699
|
+
let application!: ActiveImplementationRuntimeApplication;
|
|
1700
|
+
const completion = Promise.resolve()
|
|
1701
|
+
.then(async (): Promise<ImplementationRuntimeApplicationResult> => {
|
|
1702
|
+
if (!isCurrent()) return "stale";
|
|
1703
|
+
const pendingState = state;
|
|
1704
|
+
const previousThinkingLevel = pi.getThinkingLevel();
|
|
1705
|
+
|
|
1706
|
+
const applyThinking = () => {
|
|
1707
|
+
if (!intent.thinkingLevel) return;
|
|
1708
|
+
try {
|
|
1709
|
+
pi.setThinkingLevel(intent.thinkingLevel);
|
|
1710
|
+
const effectiveLevel = pi.getThinkingLevel();
|
|
1711
|
+
if (effectiveLevel !== intent.thinkingLevel) {
|
|
1712
|
+
notifyCurrent(
|
|
1713
|
+
`Implementation thinking ${intent.thinkingLevel} is unsupported by the destination model; Pi is using ${effectiveLevel}.`,
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
} catch (error: unknown) {
|
|
1717
|
+
notifyCurrent(
|
|
1718
|
+
`Implementation thinking ${intent.thinkingLevel} could not be applied: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default will continue.`,
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
|
|
1723
|
+
if (intent.model) {
|
|
1724
|
+
let model: ReturnType<ExtensionContext["modelRegistry"]["find"]>;
|
|
1725
|
+
let resolutionFailed = false;
|
|
1726
|
+
try {
|
|
1727
|
+
model = ctx.modelRegistry.find(intent.model.provider, intent.model.modelId);
|
|
1728
|
+
} catch (error: unknown) {
|
|
1729
|
+
notifyCurrent(
|
|
1730
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be resolved: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1731
|
+
);
|
|
1732
|
+
resolutionFailed = true;
|
|
1733
|
+
model = undefined;
|
|
1734
|
+
}
|
|
1735
|
+
if (!model && !resolutionFailed) {
|
|
1736
|
+
notifyCurrent(
|
|
1737
|
+
`Implementation model ${terminalModelReference(intent.model)} is no longer available. The destination default model will continue.`,
|
|
1738
|
+
);
|
|
1739
|
+
}
|
|
1740
|
+
if (model) {
|
|
1741
|
+
let authenticated = false;
|
|
1742
|
+
try {
|
|
1743
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
1744
|
+
if (!isCurrent()) return "stale";
|
|
1745
|
+
if (auth.ok) authenticated = true;
|
|
1746
|
+
else {
|
|
1747
|
+
notifyCurrent(
|
|
1748
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be authenticated: ${safeTerminalText(auth.error)}. The destination default model will continue.`,
|
|
1749
|
+
);
|
|
1750
|
+
}
|
|
1751
|
+
} catch (error: unknown) {
|
|
1752
|
+
notifyCurrent(
|
|
1753
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be authenticated: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1754
|
+
);
|
|
1755
|
+
}
|
|
1756
|
+
if (!isCurrent()) return "stale";
|
|
1757
|
+
if (authenticated) {
|
|
1758
|
+
let applied = false;
|
|
1759
|
+
let applicationFailed = false;
|
|
1760
|
+
application.drainOnShutdown = true;
|
|
1761
|
+
try {
|
|
1762
|
+
try {
|
|
1763
|
+
applied = await pi.setModel(model);
|
|
1764
|
+
} catch (error: unknown) {
|
|
1765
|
+
applicationFailed = true;
|
|
1766
|
+
notifyCurrent(
|
|
1767
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be applied: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1768
|
+
);
|
|
1769
|
+
}
|
|
1770
|
+
} finally {
|
|
1771
|
+
application.drainOnShutdown = false;
|
|
1772
|
+
}
|
|
1773
|
+
if (!isCurrent()) return "stale";
|
|
1774
|
+
if (!applied && !applicationFailed) {
|
|
1775
|
+
notifyCurrent(
|
|
1776
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be applied after authentication changed. The destination default model will continue.`,
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
if (!isCurrent()) return "stale";
|
|
1783
|
+
applyThinking();
|
|
1784
|
+
if (!isCurrent()) return "stale";
|
|
1785
|
+
|
|
1786
|
+
state = { ...state, pendingImplementationRuntime: undefined };
|
|
1787
|
+
try {
|
|
1788
|
+
persistState();
|
|
1789
|
+
} catch (error: unknown) {
|
|
1790
|
+
state = pendingState;
|
|
1791
|
+
try {
|
|
1792
|
+
if (pi.getThinkingLevel() !== previousThinkingLevel) {
|
|
1793
|
+
pi.setThinkingLevel(previousThinkingLevel);
|
|
1794
|
+
}
|
|
1795
|
+
} catch {
|
|
1796
|
+
// Keep the durable intent pending even if a runtime rollback is unavailable.
|
|
1797
|
+
}
|
|
1798
|
+
try {
|
|
1799
|
+
persistState();
|
|
1800
|
+
} catch {
|
|
1801
|
+
// SessionManager mutates its in-memory branch before a disk append can fail.
|
|
1802
|
+
// A best-effort rollback entry keeps that branch aligned with durable pending state.
|
|
1803
|
+
}
|
|
1804
|
+
notifyCurrent(
|
|
1805
|
+
`Unable to apply fresh implementation settings because their one-shot state could not be consumed: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The implementation request was not sent; retry after session persistence is available.`,
|
|
1806
|
+
);
|
|
1807
|
+
return "blocked";
|
|
1808
|
+
}
|
|
1809
|
+
if (application.holdForAdmission) pendingRuntimeAdmissionSession = session;
|
|
1810
|
+
return "ready";
|
|
1811
|
+
})
|
|
1812
|
+
.finally(() => {
|
|
1813
|
+
if (activeImplementationRuntimeApplication === application) {
|
|
1814
|
+
activeImplementationRuntimeApplication = undefined;
|
|
1815
|
+
}
|
|
1816
|
+
});
|
|
1817
|
+
application = {
|
|
1818
|
+
sessionManager: session,
|
|
1819
|
+
completion,
|
|
1820
|
+
drainOnShutdown: false,
|
|
1821
|
+
holdForAdmission,
|
|
1822
|
+
};
|
|
1823
|
+
activeImplementationRuntimeApplication = application;
|
|
1824
|
+
return completion;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
function applyPlanThinkingLevel() {
|
|
1828
|
+
if (state.manualThinkingLevel) {
|
|
1829
|
+
if (pi.getThinkingLevel() !== state.manualThinkingLevel) {
|
|
1830
|
+
pi.setThinkingLevel(state.manualThinkingLevel);
|
|
1831
|
+
}
|
|
1832
|
+
return;
|
|
1833
|
+
}
|
|
1834
|
+
const configured = configuredThinkingLevel(settings);
|
|
1835
|
+
if (!configured) {
|
|
1836
|
+
state = {
|
|
1837
|
+
...state,
|
|
1838
|
+
previousThinkingLevel: undefined,
|
|
1839
|
+
appliedThinkingLevel: undefined,
|
|
1840
|
+
};
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
const current = pi.getThinkingLevel();
|
|
1844
|
+
if (!state.appliedThinkingLevel) state.previousThinkingLevel = current;
|
|
1845
|
+
if (current !== configured) pi.setThinkingLevel(configured);
|
|
1846
|
+
state.appliedThinkingLevel = pi.getThinkingLevel();
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
function captureManualThinkingLevel() {
|
|
1850
|
+
if (!state.appliedThinkingLevel) return;
|
|
1851
|
+
const current = pi.getThinkingLevel();
|
|
1852
|
+
if (current === state.appliedThinkingLevel) return;
|
|
1853
|
+
state = {
|
|
1854
|
+
...state,
|
|
1855
|
+
manualThinkingLevel: current,
|
|
1856
|
+
previousThinkingLevel: undefined,
|
|
1857
|
+
appliedThinkingLevel: undefined,
|
|
1858
|
+
};
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
function restoreThinkingLevel() {
|
|
1862
|
+
captureManualThinkingLevel();
|
|
1863
|
+
const { appliedThinkingLevel, previousThinkingLevel } = state;
|
|
1864
|
+
if (appliedThinkingLevel && previousThinkingLevel && pi.getThinkingLevel() === appliedThinkingLevel) {
|
|
1865
|
+
pi.setThinkingLevel(previousThinkingLevel);
|
|
1866
|
+
}
|
|
1867
|
+
state = { ...state, appliedThinkingLevel: undefined, previousThinkingLevel: undefined };
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
function safeGetActiveTools() {
|
|
1871
|
+
try {
|
|
1872
|
+
return pi.getActiveTools();
|
|
1873
|
+
} catch {
|
|
1874
|
+
return DEFAULT_TOOLS;
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
function installRestoredState(candidate: PlanModeState, ctx: ExtensionContext) {
|
|
1879
|
+
const previousState = state;
|
|
1880
|
+
const previousWorkflowAllowedToolNames = workflowAllowedToolNames;
|
|
1881
|
+
const previousPendingWorkflowToolPolicy = pendingWorkflowToolPolicy;
|
|
1882
|
+
const previousOwner = workflowOwner;
|
|
1883
|
+
const wasEnabled = state.enabled;
|
|
1884
|
+
if (candidate.enabled && !workflowMutex.isOwner(workflowOwner)) {
|
|
1885
|
+
const owner = workflowMutex.acquire();
|
|
1886
|
+
if (!owner) {
|
|
1887
|
+
state = { enabled: false, awaitingAction: false };
|
|
1888
|
+
workflowAllowedToolNames = undefined;
|
|
1889
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1890
|
+
reportRestoredWorkflowBusy(ctx);
|
|
1891
|
+
return false;
|
|
1892
|
+
}
|
|
1893
|
+
workflowOwner = owner;
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
try {
|
|
1897
|
+
if (candidate.enabled) {
|
|
1898
|
+
try {
|
|
1899
|
+
assertPlanModeHelperToolsAvailable(safeGetActiveTools());
|
|
1900
|
+
} catch {
|
|
1901
|
+
state = { enabled: false, awaitingAction: false };
|
|
1902
|
+
workflowAllowedToolNames = undefined;
|
|
1903
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1904
|
+
if (workflowOwner !== previousOwner) {
|
|
1905
|
+
workflowMutex.release(workflowOwner);
|
|
1906
|
+
workflowOwner = previousOwner;
|
|
1907
|
+
}
|
|
1908
|
+
reportRestoredHelpersUnavailable(ctx);
|
|
1909
|
+
return false;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
if (wasEnabled && !candidate.enabled) {
|
|
1913
|
+
readyPresentationIntent = undefined;
|
|
1914
|
+
restoreThinkingLevel();
|
|
1915
|
+
}
|
|
1916
|
+
state = candidate;
|
|
1917
|
+
const policyChanged = state.enabled ? restoreWorkflowToolPolicy(state.workflowToolPolicy) : false;
|
|
1918
|
+
if (!state.enabled) {
|
|
1919
|
+
workflowAllowedToolNames = undefined;
|
|
1920
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1921
|
+
}
|
|
1922
|
+
if (policyChanged) persistState();
|
|
1923
|
+
if (state.enabled) applyPlanThinkingLevel();
|
|
1924
|
+
else if (wasEnabled) releaseWorkflowOwner();
|
|
1925
|
+
return true;
|
|
1926
|
+
} catch (error: unknown) {
|
|
1927
|
+
try {
|
|
1928
|
+
if (!wasEnabled && state.enabled) restoreThinkingLevel();
|
|
1929
|
+
} finally {
|
|
1930
|
+
state = previousState;
|
|
1931
|
+
workflowAllowedToolNames = previousWorkflowAllowedToolNames;
|
|
1932
|
+
pendingWorkflowToolPolicy = previousPendingWorkflowToolPolicy;
|
|
1933
|
+
if (workflowOwner !== previousOwner) {
|
|
1934
|
+
workflowMutex.release(workflowOwner);
|
|
1935
|
+
workflowOwner = previousOwner;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
throw error;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
function rollbackNewActivation(
|
|
1943
|
+
previousState: PlanModeState,
|
|
1944
|
+
ctx: ExtensionContext,
|
|
1945
|
+
previousOwner?: WorkflowMutexOwner,
|
|
1946
|
+
) {
|
|
1947
|
+
const activatedOwner = workflowOwner;
|
|
1948
|
+
readyPresentationIntent = undefined;
|
|
1949
|
+
try {
|
|
1950
|
+
if (state.enabled) {
|
|
1951
|
+
publishModeContract("normal", ctx);
|
|
1952
|
+
restoreThinkingLevel();
|
|
1953
|
+
}
|
|
1954
|
+
} finally {
|
|
1955
|
+
state = previousState;
|
|
1956
|
+
workflowAllowedToolNames = undefined;
|
|
1957
|
+
pendingWorkflowToolPolicy = undefined;
|
|
1958
|
+
try {
|
|
1959
|
+
persistState();
|
|
1960
|
+
updateUi(ctx);
|
|
1961
|
+
} finally {
|
|
1962
|
+
if (activatedOwner !== previousOwner) {
|
|
1963
|
+
workflowMutex.release(activatedOwner);
|
|
1964
|
+
workflowOwner = previousOwner;
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
function bindWorkflowSessionIfNeeded(ctx: ExtensionContext) {
|
|
1971
|
+
if (currentSession === ctx.sessionManager) return;
|
|
1972
|
+
currentSession = ctx.sessionManager;
|
|
1973
|
+
workflowOwner = undefined;
|
|
1974
|
+
workflowMutex.bindSession(ctx.sessionManager);
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
function releaseWorkflowOwner() {
|
|
1978
|
+
const owner = workflowOwner;
|
|
1979
|
+
workflowMutex.release(owner);
|
|
1980
|
+
if (!workflowMutex.isOwner(owner)) workflowOwner = undefined;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
function reportWorkflowBusy(ctx: ExtensionContext) {
|
|
1984
|
+
const message = "Another workflow is active in this session. End it before starting Plan mode.";
|
|
1985
|
+
if (!ctx.hasUI) throw new Error(message);
|
|
1986
|
+
ctx.ui.notify(message, "warning");
|
|
1987
|
+
return false;
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
function reportRestoredWorkflowBusy(ctx: ExtensionContext) {
|
|
1991
|
+
if (!ctx.hasUI) return;
|
|
1992
|
+
ctx.ui.notify(
|
|
1993
|
+
"Plan mode was not restored because another workflow is active in this session. Reload or start Plan mode after it ends.",
|
|
1994
|
+
"warning",
|
|
1995
|
+
);
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
function reportRestoredHelpersUnavailable(ctx: ExtensionContext) {
|
|
1999
|
+
if (!ctx.hasUI) return;
|
|
2000
|
+
ctx.ui.notify(
|
|
2001
|
+
"Plan mode was not restored because its helper tools are unavailable under the active tool policy.",
|
|
2002
|
+
"warning",
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
function reportHelperActivationFailure(ctx: ExtensionContext, error: unknown) {
|
|
2007
|
+
const detail = safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
2008
|
+
const message = `Cannot start Plan mode: ${detail}.`;
|
|
2009
|
+
if (!ctx.hasUI) throw new Error(message, { cause: error });
|
|
2010
|
+
ctx.ui.notify(message, "error");
|
|
2011
|
+
return false;
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
function updateUi(ctx: ExtensionContext) {
|
|
2015
|
+
updatePlanModeUi(ctx, state, formatToolSummary);
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
function clearUi(ctx: ExtensionContext) {
|
|
2019
|
+
clearPlanModeUi(ctx);
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
function planStatusText() {
|
|
2023
|
+
return formatPlanModeStatusText(state, formatToolSummary);
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
function implementationOutcome() {
|
|
2027
|
+
return implementationRetentionPreview(configuredImplementationPlanRetention(settings));
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
function formatToolSummary() {
|
|
2031
|
+
const names = planModePolicyToolNames();
|
|
2032
|
+
return `Plan policy allows: ${names.length > 0 ? names.join(", ") : "none"}. Model-visible tools stay unchanged.`;
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
function toolByName(toolName: string) {
|
|
2036
|
+
return safeGetAllTools().find((candidate) => candidate.name === toolName);
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
function terminalToolName(value: string) {
|
|
2040
|
+
const safe = safeTerminalText(value) || "(unnamed tool)";
|
|
2041
|
+
return safe.length > 120 ? `${safe.slice(0, 119)}…` : safe;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
function terminalModelReference(model: { provider: string; modelId: string }) {
|
|
2045
|
+
const safe = safeTerminalText(`${model.provider}/${model.modelId}`) || "(unnamed model)";
|
|
2046
|
+
return safe.length > 160 ? `${safe.slice(0, 159)}…` : safe;
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
function terminalErrorDetail(error: unknown) {
|
|
2050
|
+
const safe = safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
2051
|
+
if (!safe) return "unknown error";
|
|
2052
|
+
return safe.length > 500 ? `${safe.slice(0, 499)}…` : safe;
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
function safeTerminalText(value: string) {
|
|
2056
|
+
return [...stripVTControlCharacters(value)]
|
|
2057
|
+
.map((character) => {
|
|
2058
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
2059
|
+
return codePoint <= 0x1f ||
|
|
2060
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
2061
|
+
(codePoint >= 0x202a && codePoint <= 0x202e) ||
|
|
2062
|
+
(codePoint >= 0x2066 && codePoint <= 0x2069)
|
|
2063
|
+
? " "
|
|
2064
|
+
: character;
|
|
2065
|
+
})
|
|
2066
|
+
.join("")
|
|
2067
|
+
.trim();
|
|
2068
|
+
}
|
|
1717
2069
|
}
|
|
1718
2070
|
|
|
1719
2071
|
export { completePlanArguments } from "./command.js";
|
|
1720
2072
|
export {
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2073
|
+
extractProposedPlan,
|
|
2074
|
+
latestAssistantText,
|
|
2075
|
+
parseProposedPlan,
|
|
2076
|
+
stripProposedPlanBlocks,
|
|
2077
|
+
stripProposedPlanBlocksFromMessage,
|
|
1726
2078
|
} from "./message-transform.js";
|
|
1727
2079
|
export {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
2080
|
+
createModeContractMessage,
|
|
2081
|
+
modeContractContent,
|
|
2082
|
+
reconcileModeContract,
|
|
1731
2083
|
} from "./mode-contract.js";
|
|
1732
2084
|
export { buildPlanModePrompt } from "./prompt.js";
|
|
1733
2085
|
export { normalizePlanModeQuestionParams } from "./question-tool.js";
|