@narumitw/pi-plan-mode 0.57.1 → 0.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -4
- package/dist/chunks/{chunk-57OBPS7P.js → chunk-QHHHBTDO.js} +170 -83
- package/dist/chunks/chunk-QHHHBTDO.js.map +7 -0
- package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UNJGC2RQ.js} +322 -14
- package/dist/chunks/interactive-ui-UNJGC2RQ.js.map +7 -0
- package/dist/index.ts +667 -152
- package/dist/index.ts.map +4 -4
- package/docs/settings.md +26 -2
- package/package.json +1 -1
- package/src/command.ts +1 -0
- package/src/fresh-handoff-coordinator.ts +55 -0
- package/src/fresh-implementation.ts +153 -29
- package/src/implementation-models.ts +48 -0
- package/src/plan-action-controller.ts +78 -7
- package/src/plan-action-menus.ts +272 -17
- package/src/plan-mode.ts +454 -21
- package/src/settings-menu.ts +157 -2
- package/src/settings.ts +81 -1
- package/src/state.ts +62 -0
- 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,10 +1,13 @@
|
|
|
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
|
ExtensionAPI,
|
|
6
7
|
ExtensionCommandContext,
|
|
7
8
|
ExtensionContext,
|
|
9
|
+
InputEvent,
|
|
10
|
+
InputSource,
|
|
8
11
|
} from "@earendil-works/pi-coding-agent";
|
|
9
12
|
import { completePlanArguments } from "./command.js";
|
|
10
13
|
import {
|
|
@@ -21,6 +24,7 @@ import {
|
|
|
21
24
|
type FinalizationRunOutcome,
|
|
22
25
|
RETRY_FINALIZE_PLAN_PROMPT,
|
|
23
26
|
} from "./finalization-request.js";
|
|
27
|
+
import { createDeferredFreshHandoffCoordinator } from "./fresh-handoff-coordinator.js";
|
|
24
28
|
import {
|
|
25
29
|
formatHistoryImplementationPrompt,
|
|
26
30
|
formatImplementationHandoff,
|
|
@@ -46,7 +50,10 @@ import {
|
|
|
46
50
|
type PlanModeContract,
|
|
47
51
|
reconcileModeContract,
|
|
48
52
|
} from "./mode-contract.js";
|
|
49
|
-
import {
|
|
53
|
+
import {
|
|
54
|
+
createPlanActionController,
|
|
55
|
+
type FreshImplementationTiming,
|
|
56
|
+
} from "./plan-action-controller.js";
|
|
50
57
|
import { createPlanExportController } from "./plan-export-controller.js";
|
|
51
58
|
import {
|
|
52
59
|
clearPlanModeUi,
|
|
@@ -74,6 +81,7 @@ import {
|
|
|
74
81
|
configuredImplementationPlanRetention,
|
|
75
82
|
configuredPlanModeToggleShortcut,
|
|
76
83
|
configuredThinkingLevel,
|
|
84
|
+
type ImplementationPlanRetention,
|
|
77
85
|
type PlanModeSettings,
|
|
78
86
|
type PlanModeSettingsPatch,
|
|
79
87
|
planModeSettingsPath,
|
|
@@ -82,6 +90,7 @@ import {
|
|
|
82
90
|
updatePlanModeSettings,
|
|
83
91
|
} from "./settings.js";
|
|
84
92
|
import {
|
|
93
|
+
type ImplementationRuntimeSelection,
|
|
85
94
|
type PlanCompletionSource,
|
|
86
95
|
type PlanModeState,
|
|
87
96
|
type PlanModeWorkflowToolPolicy,
|
|
@@ -99,6 +108,7 @@ import { WorkflowMutex, type WorkflowMutexOwner } from "./workflow-mutex.js";
|
|
|
99
108
|
|
|
100
109
|
const STATE_ENTRY_TYPE = "plan-mode-state";
|
|
101
110
|
const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
|
|
111
|
+
const RECOVERED_RUNTIME_ADMISSION_INPUT_MESSAGE_TYPE = "plan-mode-recovered-input";
|
|
102
112
|
const BLOCKED_MUTATING_TOOLS = new Set(["edit", "write", "update_plan"]);
|
|
103
113
|
const DEFAULT_TOOLS = ["read", "bash", "edit", "write"];
|
|
104
114
|
interface ReadyPresentationIntent {
|
|
@@ -106,10 +116,37 @@ interface ReadyPresentationIntent {
|
|
|
106
116
|
plan: string;
|
|
107
117
|
source: PlanCompletionSource;
|
|
108
118
|
}
|
|
119
|
+
interface DeferredFreshImplementation {
|
|
120
|
+
ctx: ExtensionContext;
|
|
121
|
+
sourceSession: object;
|
|
122
|
+
menuGeneration: number;
|
|
123
|
+
workflowGeneration: number;
|
|
124
|
+
workflowOwner: WorkflowMutexOwner | undefined;
|
|
125
|
+
enabled: boolean;
|
|
126
|
+
plan: string;
|
|
127
|
+
source: PlanCompletionSource;
|
|
128
|
+
savedPlan: PlanModeState["savedPlan"];
|
|
129
|
+
retention: ImplementationPlanRetention;
|
|
130
|
+
runtime: ImplementationRuntimeSelection | undefined;
|
|
131
|
+
menuIsCurrent(): boolean;
|
|
132
|
+
}
|
|
109
133
|
interface PendingWorkflowToolPolicy {
|
|
110
134
|
generation: number;
|
|
111
135
|
mode: "resolve" | "revalidate";
|
|
112
136
|
}
|
|
137
|
+
type ImplementationRuntimeApplicationResult = "ready" | "blocked" | "stale";
|
|
138
|
+
interface ActiveImplementationRuntimeApplication {
|
|
139
|
+
sessionManager: ExtensionContext["sessionManager"];
|
|
140
|
+
completion: Promise<ImplementationRuntimeApplicationResult>;
|
|
141
|
+
drainOnShutdown: boolean;
|
|
142
|
+
holdForAdmission: boolean;
|
|
143
|
+
}
|
|
144
|
+
interface QueuedRuntimeAdmissionInput {
|
|
145
|
+
sessionManager: ExtensionContext["sessionManager"];
|
|
146
|
+
text: string;
|
|
147
|
+
images?: NonNullable<InputEvent["images"]>;
|
|
148
|
+
source: InputSource;
|
|
149
|
+
}
|
|
113
150
|
type InteractiveUi = typeof import("./interactive-ui.js");
|
|
114
151
|
|
|
115
152
|
interface PlanModeDependencies {
|
|
@@ -151,10 +188,15 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
151
188
|
let modeContractsRelevant = false;
|
|
152
189
|
let readyPresentationIntent: ReadyPresentationIntent | undefined;
|
|
153
190
|
let latestCommandContext: ExtensionCommandContext | undefined;
|
|
191
|
+
let stagedFreshImplementation: DeferredFreshImplementation | undefined;
|
|
192
|
+
const deferredFreshHandoff = createDeferredFreshHandoffCoordinator();
|
|
154
193
|
let nextReadyPresentationNonce = 0;
|
|
155
194
|
let menuGeneration = 0;
|
|
156
195
|
let workflowGeneration = 0;
|
|
157
196
|
let refreshStateBeforeFirstAgentStart = false;
|
|
197
|
+
let activeImplementationRuntimeApplication: ActiveImplementationRuntimeApplication | undefined;
|
|
198
|
+
let pendingRuntimeAdmissionSession: object | undefined;
|
|
199
|
+
let queuedRuntimeAdmissionInputs: QueuedRuntimeAdmissionInput[] = [];
|
|
158
200
|
let menuController = new AbortController();
|
|
159
201
|
let settingsWatch: ReturnType<typeof watch> | undefined;
|
|
160
202
|
let settingsReloadTimer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -173,6 +215,10 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
173
215
|
getState: () => state,
|
|
174
216
|
captureLifecycle: captureMenuLifecycle,
|
|
175
217
|
statusText: planStatusText,
|
|
218
|
+
// ExtensionContext.thinkingLevel was added after the supported Pi 0.80.6 floor.
|
|
219
|
+
// ExtensionAPI has exposed the same runtime value throughout that compatibility range.
|
|
220
|
+
getThinkingLevel: () => pi.getThinkingLevel(),
|
|
221
|
+
getSettings: () => settings,
|
|
176
222
|
implementationOutcome,
|
|
177
223
|
getExportDestination: (ctx) => planExports.getDestination(ctx),
|
|
178
224
|
show: (ctx) => showStoredPlan(pi, ctx, state),
|
|
@@ -296,6 +342,14 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
296
342
|
savePlanForLater(ctx);
|
|
297
343
|
return;
|
|
298
344
|
}
|
|
345
|
+
if (command === "settings") {
|
|
346
|
+
if (!ctx.hasUI) {
|
|
347
|
+
throw new Error("/plan settings requires TUI or RPC mode and is unavailable here.");
|
|
348
|
+
}
|
|
349
|
+
const lifecycle = captureMenuLifecycle();
|
|
350
|
+
await showSettings(ctx, lifecycle.signal, lifecycle.isCurrent);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
299
353
|
const exportMatch = /^export(?:\s+([\s\S]+))?$/iu.exec(prompt);
|
|
300
354
|
if (exportMatch) {
|
|
301
355
|
const lifecycle = captureMenuLifecycle();
|
|
@@ -442,6 +496,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
442
496
|
};
|
|
443
497
|
|
|
444
498
|
pi.on("session_start", async (event, ctx) => {
|
|
499
|
+
cancelDeferredFreshImplementation();
|
|
445
500
|
const generation = ++menuGeneration;
|
|
446
501
|
finalizationRequest.reset();
|
|
447
502
|
currentSession = ctx.sessionManager;
|
|
@@ -449,6 +504,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
449
504
|
workflowOwner = undefined;
|
|
450
505
|
workflowMutex.bindSession(ctx.sessionManager);
|
|
451
506
|
refreshStateBeforeFirstAgentStart = event.reason === "new";
|
|
507
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
508
|
+
queuedRuntimeAdmissionInputs = [];
|
|
452
509
|
menuController.abort(new DOMException("Plan-mode session replaced", "AbortError"));
|
|
453
510
|
menuController = new AbortController();
|
|
454
511
|
readyPresentationIntent = undefined;
|
|
@@ -467,9 +524,22 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
467
524
|
if (!installRestoredState(restoredState, ctx)) return;
|
|
468
525
|
implementationRetention.restore(state.activeImplementation);
|
|
469
526
|
updateUi(ctx);
|
|
527
|
+
// A new session receives its setup entries after session_start, so its input gate refreshes
|
|
528
|
+
// them. Resumed and forked sessions already have the intent and must apply it here because
|
|
529
|
+
// extension-triggered custom-message turns bypass both input and before_agent_start.
|
|
530
|
+
if (event.reason !== "new") await applyPendingImplementationRuntime(ctx);
|
|
470
531
|
});
|
|
471
532
|
|
|
472
533
|
pi.on("session_before_tree", (event, ctx) => {
|
|
534
|
+
if (runtimeAdmissionIsPending(ctx.sessionManager)) {
|
|
535
|
+
if (ctx.hasUI) {
|
|
536
|
+
ctx.ui.notify(
|
|
537
|
+
"Wait for fresh implementation startup to admit the pending prompts before changing branches.",
|
|
538
|
+
"warning",
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
return { cancel: true };
|
|
542
|
+
}
|
|
473
543
|
const target = ctx.sessionManager.getEntry(event.preparation.targetId);
|
|
474
544
|
if (target?.type !== "custom_message" || target.customType !== MODE_CONTRACT_MESSAGE_TYPE) {
|
|
475
545
|
return;
|
|
@@ -483,13 +553,16 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
483
553
|
return { cancel: true };
|
|
484
554
|
});
|
|
485
555
|
|
|
486
|
-
pi.on("session_tree", (_event, ctx) => {
|
|
556
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
557
|
+
cancelDeferredFreshImplementation();
|
|
487
558
|
advanceWorkflowGeneration();
|
|
488
559
|
menuGeneration += 1;
|
|
489
560
|
menuController.abort(new DOMException("Plan-mode tree branch changed", "AbortError"));
|
|
490
561
|
menuController = new AbortController();
|
|
491
562
|
readyPresentationIntent = undefined;
|
|
492
563
|
latestCommandContext = undefined;
|
|
564
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
565
|
+
queuedRuntimeAdmissionInputs = [];
|
|
493
566
|
implementationRetention.reset();
|
|
494
567
|
const branch = ctx.sessionManager.getBranch();
|
|
495
568
|
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
@@ -498,6 +571,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
498
571
|
implementationRetention.restore(state.activeImplementation);
|
|
499
572
|
startPlanModeSettingsWatch(menuGeneration);
|
|
500
573
|
updateUi(ctx);
|
|
574
|
+
await applyPendingImplementationRuntime(ctx);
|
|
501
575
|
});
|
|
502
576
|
|
|
503
577
|
pi.on("thinking_level_select", (event) => {
|
|
@@ -514,16 +588,41 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
514
588
|
});
|
|
515
589
|
|
|
516
590
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
591
|
+
cancelDeferredFreshImplementation();
|
|
517
592
|
const shutdownSession = ctx.sessionManager;
|
|
593
|
+
const runtimeApplication =
|
|
594
|
+
activeImplementationRuntimeApplication?.sessionManager === shutdownSession &&
|
|
595
|
+
activeImplementationRuntimeApplication.drainOnShutdown
|
|
596
|
+
? activeImplementationRuntimeApplication
|
|
597
|
+
: undefined;
|
|
598
|
+
const queuedInputs = takeQueuedRuntimeAdmissionInputs(shutdownSession);
|
|
599
|
+
for (const queued of queuedInputs) {
|
|
600
|
+
pi.sendMessage(
|
|
601
|
+
{
|
|
602
|
+
customType: RECOVERED_RUNTIME_ADMISSION_INPUT_MESSAGE_TYPE,
|
|
603
|
+
content: runtimeAdmissionInputContent(queued),
|
|
604
|
+
display: true,
|
|
605
|
+
details: { source: queued.source },
|
|
606
|
+
},
|
|
607
|
+
{ triggerTurn: false },
|
|
608
|
+
);
|
|
609
|
+
}
|
|
518
610
|
finalizationRequest.reset();
|
|
519
611
|
menuGeneration += 1;
|
|
520
612
|
menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
|
|
521
613
|
readyPresentationIntent = undefined;
|
|
522
614
|
latestCommandContext = undefined;
|
|
523
615
|
refreshStateBeforeFirstAgentStart = false;
|
|
616
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
617
|
+
queuedRuntimeAdmissionInputs = [];
|
|
524
618
|
workflowAllowedToolNames = undefined;
|
|
525
619
|
pendingWorkflowToolPolicy = undefined;
|
|
526
620
|
implementationRetention.reset();
|
|
621
|
+
if (runtimeApplication) await runtimeApplication.completion;
|
|
622
|
+
if (currentSession !== undefined && currentSession !== shutdownSession) {
|
|
623
|
+
workflowMutex.unbindSession(shutdownSession);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
527
626
|
await awaitPlanModeSettingsWrites(dependencies.settingsPath);
|
|
528
627
|
if (currentSession !== undefined && currentSession !== shutdownSession) {
|
|
529
628
|
workflowMutex.unbindSession(shutdownSession);
|
|
@@ -654,19 +753,47 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
654
753
|
return { messages: messages as typeof event.messages };
|
|
655
754
|
});
|
|
656
755
|
|
|
657
|
-
pi.on("
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
756
|
+
pi.on("input", async (event, ctx) => {
|
|
757
|
+
refreshStateForFirstPrompt(ctx);
|
|
758
|
+
const waitsForRuntimeAdmission =
|
|
759
|
+
activeImplementationRuntimeApplication?.sessionManager === ctx.sessionManager ||
|
|
760
|
+
pendingRuntimeAdmissionSession === ctx.sessionManager;
|
|
761
|
+
const queuedInput = waitsForRuntimeAdmission
|
|
762
|
+
? {
|
|
763
|
+
sessionManager: ctx.sessionManager,
|
|
764
|
+
text: event.text,
|
|
765
|
+
...(event.images ? { images: [...event.images] } : {}),
|
|
766
|
+
source: event.source,
|
|
767
|
+
}
|
|
768
|
+
: undefined;
|
|
769
|
+
if (queuedInput) queuedRuntimeAdmissionInputs.push(queuedInput);
|
|
770
|
+
const result = await applyPendingImplementationRuntime(ctx, true);
|
|
771
|
+
if (result !== "ready") {
|
|
772
|
+
if (result === "stale" && queuedInput) removeQueuedRuntimeAdmissionInput(queuedInput);
|
|
773
|
+
return { action: "handled" };
|
|
774
|
+
}
|
|
775
|
+
if (queuedInput) return { action: "handled" };
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
779
|
+
if (currentSession !== ctx.sessionManager) return;
|
|
780
|
+
if (pendingRuntimeAdmissionSession === ctx.sessionManager) {
|
|
781
|
+
pendingRuntimeAdmissionSession = undefined;
|
|
667
782
|
}
|
|
783
|
+
const queuedInputs = takeQueuedRuntimeAdmissionInputs(ctx.sessionManager);
|
|
784
|
+
for (const queued of queuedInputs) {
|
|
785
|
+
pi.sendUserMessage(runtimeAdmissionInputContent(queued), {
|
|
786
|
+
deliverAs: "followUp",
|
|
787
|
+
expandPromptTemplates: queued.source !== "extension",
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
pi.on("before_agent_start", (_event, ctx) => {
|
|
793
|
+
refreshStateForFirstPrompt(ctx);
|
|
668
794
|
if (!state.enabled || !workflowMutex.isOwner(workflowOwner)) return;
|
|
669
795
|
if (state.latestPlan || state.awaitingAction) {
|
|
796
|
+
cancelDeferredFreshImplementation();
|
|
670
797
|
readyPresentationIntent = undefined;
|
|
671
798
|
state = {
|
|
672
799
|
...state,
|
|
@@ -726,6 +853,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
726
853
|
if (!ctx.isIdle() || ctx.hasPendingMessages()) return;
|
|
727
854
|
|
|
728
855
|
readyPresentationIntent = undefined;
|
|
856
|
+
stagedFreshImplementation = undefined;
|
|
729
857
|
try {
|
|
730
858
|
if (intent.source === "legacy_proposed_plan") {
|
|
731
859
|
pi.sendMessage(
|
|
@@ -740,7 +868,11 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
740
868
|
if (ctx.hasUI && completedPlanIsCurrent(intent)) {
|
|
741
869
|
await planActions.showReady(latestCommandContext ?? ctx);
|
|
742
870
|
}
|
|
871
|
+
const request = stagedFreshImplementation;
|
|
872
|
+
stagedFreshImplementation = undefined;
|
|
873
|
+
if (request) armDeferredFreshImplementation(request);
|
|
743
874
|
} catch (error: unknown) {
|
|
875
|
+
stagedFreshImplementation = undefined;
|
|
744
876
|
if (!isStaleExtensionContextError(error)) throw error;
|
|
745
877
|
}
|
|
746
878
|
});
|
|
@@ -776,6 +908,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
776
908
|
awaitingAction: false,
|
|
777
909
|
savedPlan: undefined,
|
|
778
910
|
activeImplementation: undefined,
|
|
911
|
+
pendingImplementationRuntime: undefined,
|
|
779
912
|
selectedToolNames: candidate.selectedToolNames,
|
|
780
913
|
selectedToolKeys: candidate.selectedToolKeys,
|
|
781
914
|
};
|
|
@@ -820,6 +953,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
820
953
|
awaitingAction: false,
|
|
821
954
|
savedPlan: undefined,
|
|
822
955
|
activeImplementation: undefined,
|
|
956
|
+
pendingImplementationRuntime: undefined,
|
|
823
957
|
workflowToolPolicy: undefined,
|
|
824
958
|
manualThinkingLevel: undefined,
|
|
825
959
|
};
|
|
@@ -839,7 +973,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
839
973
|
hasModeContractArtifact(branch) ||
|
|
840
974
|
restoredState.enabled ||
|
|
841
975
|
restoredState.savedPlan !== undefined ||
|
|
842
|
-
restoredState.activeImplementation !== undefined
|
|
976
|
+
restoredState.activeImplementation !== undefined ||
|
|
977
|
+
restoredState.pendingImplementationRuntime !== undefined;
|
|
843
978
|
}
|
|
844
979
|
|
|
845
980
|
function publishModeContract(mode: PlanModeContract, ctx: ExtensionContext) {
|
|
@@ -971,6 +1106,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
971
1106
|
awaitingAction: false,
|
|
972
1107
|
savedPlan: { plan, source },
|
|
973
1108
|
activeImplementation: undefined,
|
|
1109
|
+
pendingImplementationRuntime: undefined,
|
|
974
1110
|
workflowToolPolicy: undefined,
|
|
975
1111
|
manualThinkingLevel: undefined,
|
|
976
1112
|
};
|
|
@@ -982,13 +1118,98 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
982
1118
|
ctx.ui.notify("Plan saved for later. Plan mode disabled.", "info");
|
|
983
1119
|
}
|
|
984
1120
|
|
|
985
|
-
async function startFreshImplementation(
|
|
986
|
-
|
|
987
|
-
|
|
1121
|
+
async function startFreshImplementation(
|
|
1122
|
+
ctx: ExtensionContext,
|
|
1123
|
+
menuIsCurrent: () => boolean,
|
|
1124
|
+
runtime: ImplementationRuntimeSelection | undefined,
|
|
1125
|
+
timing: FreshImplementationTiming,
|
|
1126
|
+
) {
|
|
1127
|
+
const retention = configuredImplementationPlanRetention(settings);
|
|
1128
|
+
if (timing === "immediate") {
|
|
1129
|
+
await startFreshImplementationFromState(ctx, {
|
|
1130
|
+
getState: () => state,
|
|
1131
|
+
menuIsCurrent,
|
|
1132
|
+
retention,
|
|
1133
|
+
stateEntryType: STATE_ENTRY_TYPE,
|
|
1134
|
+
runtime,
|
|
1135
|
+
});
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
const initialState = state;
|
|
1140
|
+
const savedPlan = initialState.enabled ? undefined : initialState.savedPlan;
|
|
1141
|
+
const plan = (initialState.enabled ? initialState.latestPlan : savedPlan?.plan)?.trim();
|
|
1142
|
+
const source = initialState.enabled ? initialState.latestPlanSource : savedPlan?.source;
|
|
1143
|
+
if (!plan || !source || !menuIsCurrent()) return;
|
|
1144
|
+
stagedFreshImplementation = {
|
|
1145
|
+
ctx,
|
|
1146
|
+
sourceSession: ctx.sessionManager,
|
|
1147
|
+
menuGeneration,
|
|
1148
|
+
workflowGeneration,
|
|
1149
|
+
workflowOwner,
|
|
1150
|
+
enabled: initialState.enabled,
|
|
1151
|
+
plan,
|
|
1152
|
+
source,
|
|
1153
|
+
savedPlan,
|
|
1154
|
+
retention,
|
|
1155
|
+
runtime: runtime
|
|
1156
|
+
? {
|
|
1157
|
+
...(runtime.model ? { model: { ...runtime.model } } : {}),
|
|
1158
|
+
...(runtime.thinkingLevel ? { thinkingLevel: runtime.thinkingLevel } : {}),
|
|
1159
|
+
}
|
|
1160
|
+
: undefined,
|
|
988
1161
|
menuIsCurrent,
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
function armDeferredFreshImplementation(request: DeferredFreshImplementation) {
|
|
1166
|
+
deferredFreshHandoff.schedule(
|
|
1167
|
+
async (taskIsCurrent) => {
|
|
1168
|
+
const isCurrent = () => taskIsCurrent() && deferredFreshImplementationIsCurrent(request);
|
|
1169
|
+
if (!isCurrent()) return;
|
|
1170
|
+
await startFreshImplementationFromState(request.ctx, {
|
|
1171
|
+
getState: () => state,
|
|
1172
|
+
menuIsCurrent: isCurrent,
|
|
1173
|
+
retention: request.retention,
|
|
1174
|
+
stateEntryType: STATE_ENTRY_TYPE,
|
|
1175
|
+
runtime: request.runtime,
|
|
1176
|
+
});
|
|
1177
|
+
},
|
|
1178
|
+
(error) => {
|
|
1179
|
+
if (!deferredFreshImplementationIsCurrent(request)) return;
|
|
1180
|
+
try {
|
|
1181
|
+
request.ctx.ui.notify(
|
|
1182
|
+
`Unable to start the deferred fresh implementation: ${terminalErrorDetail(error)}`,
|
|
1183
|
+
"error",
|
|
1184
|
+
);
|
|
1185
|
+
} catch {
|
|
1186
|
+
// The source context can become stale while a detached failure is reported.
|
|
1187
|
+
}
|
|
1188
|
+
},
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
function deferredFreshImplementationIsCurrent(request: DeferredFreshImplementation) {
|
|
1193
|
+
if (
|
|
1194
|
+
currentSession !== request.sourceSession ||
|
|
1195
|
+
menuGeneration !== request.menuGeneration ||
|
|
1196
|
+
workflowGeneration !== request.workflowGeneration ||
|
|
1197
|
+
workflowOwner !== request.workflowOwner ||
|
|
1198
|
+
!request.menuIsCurrent() ||
|
|
1199
|
+
state.enabled !== request.enabled
|
|
1200
|
+
) {
|
|
1201
|
+
return false;
|
|
1202
|
+
}
|
|
1203
|
+
return request.enabled
|
|
1204
|
+
? workflowMutex.isOwner(request.workflowOwner) &&
|
|
1205
|
+
state.latestPlan === request.plan &&
|
|
1206
|
+
state.latestPlanSource === request.source
|
|
1207
|
+
: state.savedPlan === request.savedPlan;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function cancelDeferredFreshImplementation() {
|
|
1211
|
+
stagedFreshImplementation = undefined;
|
|
1212
|
+
deferredFreshHandoff.cancel();
|
|
992
1213
|
}
|
|
993
1214
|
|
|
994
1215
|
async function startImplementation(ctx: ExtensionContext) {
|
|
@@ -1032,6 +1253,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1032
1253
|
latestPlanSource: undefined,
|
|
1033
1254
|
awaitingAction: false,
|
|
1034
1255
|
savedPlan: undefined,
|
|
1256
|
+
pendingImplementationRuntime: undefined,
|
|
1035
1257
|
activeImplementation: usesConversationHistory
|
|
1036
1258
|
? undefined
|
|
1037
1259
|
: {
|
|
@@ -1276,6 +1498,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1276
1498
|
}
|
|
1277
1499
|
|
|
1278
1500
|
function advanceWorkflowGeneration() {
|
|
1501
|
+
cancelDeferredFreshImplementation();
|
|
1279
1502
|
workflowGeneration += 1;
|
|
1280
1503
|
pendingWorkflowToolPolicy = undefined;
|
|
1281
1504
|
finalizationRequest.reset();
|
|
@@ -1482,6 +1705,200 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1482
1705
|
}
|
|
1483
1706
|
}
|
|
1484
1707
|
|
|
1708
|
+
function runtimeAdmissionIsPending(sessionManager: ExtensionContext["sessionManager"]) {
|
|
1709
|
+
return (
|
|
1710
|
+
activeImplementationRuntimeApplication?.sessionManager === sessionManager ||
|
|
1711
|
+
pendingRuntimeAdmissionSession === sessionManager ||
|
|
1712
|
+
queuedRuntimeAdmissionInputs.some((queued) => queued.sessionManager === sessionManager)
|
|
1713
|
+
);
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
function takeQueuedRuntimeAdmissionInputs(sessionManager: ExtensionContext["sessionManager"]) {
|
|
1717
|
+
const matching: QueuedRuntimeAdmissionInput[] = [];
|
|
1718
|
+
const remaining: QueuedRuntimeAdmissionInput[] = [];
|
|
1719
|
+
for (const queued of queuedRuntimeAdmissionInputs) {
|
|
1720
|
+
if (queued.sessionManager === sessionManager) matching.push(queued);
|
|
1721
|
+
else remaining.push(queued);
|
|
1722
|
+
}
|
|
1723
|
+
queuedRuntimeAdmissionInputs = remaining;
|
|
1724
|
+
return matching;
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
function removeQueuedRuntimeAdmissionInput(queuedInput: QueuedRuntimeAdmissionInput) {
|
|
1728
|
+
const index = queuedRuntimeAdmissionInputs.indexOf(queuedInput);
|
|
1729
|
+
if (index >= 0) queuedRuntimeAdmissionInputs.splice(index, 1);
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
function runtimeAdmissionInputContent(queued: QueuedRuntimeAdmissionInput) {
|
|
1733
|
+
return queued.images?.length
|
|
1734
|
+
? [{ type: "text" as const, text: queued.text }, ...queued.images]
|
|
1735
|
+
: queued.text;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
function refreshStateForFirstPrompt(ctx: ExtensionContext) {
|
|
1739
|
+
if (!refreshStateBeforeFirstAgentStart) return;
|
|
1740
|
+
refreshStateBeforeFirstAgentStart = false;
|
|
1741
|
+
implementationRetention.reset();
|
|
1742
|
+
const branch = ctx.sessionManager.getBranch();
|
|
1743
|
+
const restoredState = restorePlanModeState(branch, STATE_ENTRY_TYPE);
|
|
1744
|
+
restoreModeContractTracking(branch, restoredState);
|
|
1745
|
+
if (!installRestoredState(restoredState, ctx)) return;
|
|
1746
|
+
implementationRetention.restore(state.activeImplementation);
|
|
1747
|
+
updateUi(ctx);
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
async function applyPendingImplementationRuntime(
|
|
1751
|
+
ctx: ExtensionContext,
|
|
1752
|
+
holdForAdmission = false,
|
|
1753
|
+
): Promise<ImplementationRuntimeApplicationResult> {
|
|
1754
|
+
const session = ctx.sessionManager;
|
|
1755
|
+
const activeApplication = activeImplementationRuntimeApplication;
|
|
1756
|
+
if (activeApplication?.sessionManager === session) {
|
|
1757
|
+
if (holdForAdmission) activeApplication.holdForAdmission = true;
|
|
1758
|
+
return activeApplication.completion;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
const intent = state.enabled ? undefined : state.pendingImplementationRuntime;
|
|
1762
|
+
if (!intent) return "ready";
|
|
1763
|
+
const generation = menuGeneration;
|
|
1764
|
+
const isCurrent = () =>
|
|
1765
|
+
currentSession === session && generation === menuGeneration && !menuController.signal.aborted;
|
|
1766
|
+
const notifyCurrent = (message: string) => {
|
|
1767
|
+
if (!isCurrent()) return;
|
|
1768
|
+
try {
|
|
1769
|
+
ctx.ui.notify(message, "warning");
|
|
1770
|
+
} catch {
|
|
1771
|
+
// The session can become stale while an asynchronous runtime application settles.
|
|
1772
|
+
}
|
|
1773
|
+
};
|
|
1774
|
+
let application!: ActiveImplementationRuntimeApplication;
|
|
1775
|
+
const completion = Promise.resolve()
|
|
1776
|
+
.then(async (): Promise<ImplementationRuntimeApplicationResult> => {
|
|
1777
|
+
if (!isCurrent()) return "stale";
|
|
1778
|
+
const pendingState = state;
|
|
1779
|
+
const previousThinkingLevel = pi.getThinkingLevel();
|
|
1780
|
+
|
|
1781
|
+
const applyThinking = () => {
|
|
1782
|
+
if (!intent.thinkingLevel) return;
|
|
1783
|
+
try {
|
|
1784
|
+
pi.setThinkingLevel(intent.thinkingLevel);
|
|
1785
|
+
const effectiveLevel = pi.getThinkingLevel();
|
|
1786
|
+
if (effectiveLevel !== intent.thinkingLevel) {
|
|
1787
|
+
notifyCurrent(
|
|
1788
|
+
`Implementation thinking ${intent.thinkingLevel} is unsupported by the destination model; Pi is using ${effectiveLevel}.`,
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1791
|
+
} catch (error: unknown) {
|
|
1792
|
+
notifyCurrent(
|
|
1793
|
+
`Implementation thinking ${intent.thinkingLevel} could not be applied: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default will continue.`,
|
|
1794
|
+
);
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
|
|
1798
|
+
if (intent.model) {
|
|
1799
|
+
let model: ReturnType<ExtensionContext["modelRegistry"]["find"]>;
|
|
1800
|
+
let resolutionFailed = false;
|
|
1801
|
+
try {
|
|
1802
|
+
model = ctx.modelRegistry.find(intent.model.provider, intent.model.modelId);
|
|
1803
|
+
} catch (error: unknown) {
|
|
1804
|
+
notifyCurrent(
|
|
1805
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be resolved: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1806
|
+
);
|
|
1807
|
+
resolutionFailed = true;
|
|
1808
|
+
model = undefined;
|
|
1809
|
+
}
|
|
1810
|
+
if (!model && !resolutionFailed) {
|
|
1811
|
+
notifyCurrent(
|
|
1812
|
+
`Implementation model ${terminalModelReference(intent.model)} is no longer available. The destination default model will continue.`,
|
|
1813
|
+
);
|
|
1814
|
+
}
|
|
1815
|
+
if (model) {
|
|
1816
|
+
let authenticated = false;
|
|
1817
|
+
try {
|
|
1818
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
1819
|
+
if (!isCurrent()) return "stale";
|
|
1820
|
+
if (auth.ok) authenticated = true;
|
|
1821
|
+
else {
|
|
1822
|
+
notifyCurrent(
|
|
1823
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be authenticated: ${safeTerminalText(auth.error)}. The destination default model will continue.`,
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
} catch (error: unknown) {
|
|
1827
|
+
notifyCurrent(
|
|
1828
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be authenticated: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1829
|
+
);
|
|
1830
|
+
}
|
|
1831
|
+
if (!isCurrent()) return "stale";
|
|
1832
|
+
if (authenticated) {
|
|
1833
|
+
let applied = false;
|
|
1834
|
+
let applicationFailed = false;
|
|
1835
|
+
application.drainOnShutdown = true;
|
|
1836
|
+
try {
|
|
1837
|
+
try {
|
|
1838
|
+
applied = await pi.setModel(model);
|
|
1839
|
+
} catch (error: unknown) {
|
|
1840
|
+
applicationFailed = true;
|
|
1841
|
+
notifyCurrent(
|
|
1842
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be applied: ${safeTerminalText(error instanceof Error ? error.message : String(error))}. The destination default model will continue.`,
|
|
1843
|
+
);
|
|
1844
|
+
}
|
|
1845
|
+
} finally {
|
|
1846
|
+
application.drainOnShutdown = false;
|
|
1847
|
+
}
|
|
1848
|
+
if (!isCurrent()) return "stale";
|
|
1849
|
+
if (!applied && !applicationFailed) {
|
|
1850
|
+
notifyCurrent(
|
|
1851
|
+
`Implementation model ${terminalModelReference(intent.model)} could not be applied after authentication changed. The destination default model will continue.`,
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
if (!isCurrent()) return "stale";
|
|
1858
|
+
applyThinking();
|
|
1859
|
+
if (!isCurrent()) return "stale";
|
|
1860
|
+
|
|
1861
|
+
state = { ...state, pendingImplementationRuntime: undefined };
|
|
1862
|
+
try {
|
|
1863
|
+
persistState();
|
|
1864
|
+
} catch (error: unknown) {
|
|
1865
|
+
state = pendingState;
|
|
1866
|
+
try {
|
|
1867
|
+
if (pi.getThinkingLevel() !== previousThinkingLevel) {
|
|
1868
|
+
pi.setThinkingLevel(previousThinkingLevel);
|
|
1869
|
+
}
|
|
1870
|
+
} catch {
|
|
1871
|
+
// Keep the durable intent pending even if a runtime rollback is unavailable.
|
|
1872
|
+
}
|
|
1873
|
+
try {
|
|
1874
|
+
persistState();
|
|
1875
|
+
} catch {
|
|
1876
|
+
// SessionManager mutates its in-memory branch before a disk append can fail.
|
|
1877
|
+
// A best-effort rollback entry keeps that branch aligned with durable pending state.
|
|
1878
|
+
}
|
|
1879
|
+
notifyCurrent(
|
|
1880
|
+
`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.`,
|
|
1881
|
+
);
|
|
1882
|
+
return "blocked";
|
|
1883
|
+
}
|
|
1884
|
+
if (application.holdForAdmission) pendingRuntimeAdmissionSession = session;
|
|
1885
|
+
return "ready";
|
|
1886
|
+
})
|
|
1887
|
+
.finally(() => {
|
|
1888
|
+
if (activeImplementationRuntimeApplication === application) {
|
|
1889
|
+
activeImplementationRuntimeApplication = undefined;
|
|
1890
|
+
}
|
|
1891
|
+
});
|
|
1892
|
+
application = {
|
|
1893
|
+
sessionManager: session,
|
|
1894
|
+
completion,
|
|
1895
|
+
drainOnShutdown: false,
|
|
1896
|
+
holdForAdmission,
|
|
1897
|
+
};
|
|
1898
|
+
activeImplementationRuntimeApplication = application;
|
|
1899
|
+
return completion;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1485
1902
|
function applyPlanThinkingLevel() {
|
|
1486
1903
|
if (state.manualThinkingLevel) {
|
|
1487
1904
|
if (pi.getThinkingLevel() !== state.manualThinkingLevel) {
|
|
@@ -1705,11 +2122,27 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
1705
2122
|
return safe.length > 120 ? `${safe.slice(0, 119)}…` : safe;
|
|
1706
2123
|
}
|
|
1707
2124
|
|
|
2125
|
+
function terminalModelReference(model: { provider: string; modelId: string }) {
|
|
2126
|
+
const safe = safeTerminalText(`${model.provider}/${model.modelId}`) || "(unnamed model)";
|
|
2127
|
+
return safe.length > 160 ? `${safe.slice(0, 159)}…` : safe;
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
function terminalErrorDetail(error: unknown) {
|
|
2131
|
+
const safe = safeTerminalText(error instanceof Error ? error.message : String(error));
|
|
2132
|
+
if (!safe) return "unknown error";
|
|
2133
|
+
return safe.length > 500 ? `${safe.slice(0, 499)}…` : safe;
|
|
2134
|
+
}
|
|
2135
|
+
|
|
1708
2136
|
function safeTerminalText(value: string) {
|
|
1709
|
-
return [...value]
|
|
2137
|
+
return [...stripVTControlCharacters(value)]
|
|
1710
2138
|
.map((character) => {
|
|
1711
2139
|
const codePoint = character.codePointAt(0) ?? 0;
|
|
1712
|
-
return codePoint <= 0x1f ||
|
|
2140
|
+
return codePoint <= 0x1f ||
|
|
2141
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
2142
|
+
(codePoint >= 0x202a && codePoint <= 0x202e) ||
|
|
2143
|
+
(codePoint >= 0x2066 && codePoint <= 0x2069)
|
|
2144
|
+
? " "
|
|
2145
|
+
: character;
|
|
1713
2146
|
})
|
|
1714
2147
|
.join("")
|
|
1715
2148
|
.trim();
|