@narumitw/pi-plan-mode 0.56.0 → 0.57.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/dist/index.ts CHANGED
@@ -17,10 +17,12 @@ import {
17
17
  configuredPlanExportPath,
18
18
  configuredPlanModeToggleShortcut,
19
19
  configuredThinkingLevel,
20
+ createImplementationPreferenceChange,
20
21
  createImplementationRetentionCoordinator,
21
22
  exportStoredPlan,
22
23
  findBlockedCommandSegment,
23
24
  findBlockedPowerShellCommandSegment,
25
+ hasImplementationPreferences,
24
26
  implementationRetentionPreview,
25
27
  invalidPlanMessage,
26
28
  latestAssistantStopReason,
@@ -28,19 +30,24 @@ import {
28
30
  messageTextContent,
29
31
  normalizePlanModeCompletion,
30
32
  normalizePlanModeQuestionParams,
33
+ normalizePlanModeSettings,
34
+ observeImplementationModelSelections,
31
35
  parseProposedPlan,
32
36
  planExportDestination,
33
37
  planFromCompletionDetails,
34
38
  planModeCompleted,
35
39
  planModeQuestionCancelled,
36
40
  planModeSettingsPath,
41
+ preferenceError,
42
+ preflightImplementationPreferences,
37
43
  readCommand,
38
44
  readPlanModeSettings,
39
45
  renderPlanModeCompletion,
46
+ sameModel,
40
47
  snapshotPlanModeSelectedNames,
41
48
  toolPolicyLabel,
42
49
  updatePlanModeSettings
43
- } from "./chunks/chunk-57OBPS7P.js";
50
+ } from "./chunks/chunk-YUFPCQVG.js";
44
51
 
45
52
  // src/plan-mode.ts
46
53
  import { randomUUID as randomUUID2 } from "node:crypto";
@@ -120,6 +127,65 @@ function createFinalizationRequestCoordinator() {
120
127
  // src/fresh-implementation.ts
121
128
  import { randomUUID } from "node:crypto";
122
129
 
130
+ // src/fresh-implementation-preferences.ts
131
+ var FRESH_PREFERENCES_ENTRY = "plan-mode:fresh-preferences:v1";
132
+ async function applyFreshImplementationPreferences(pi, ctx, isCurrent, selectionSnapshot) {
133
+ if (!isCurrent() || !ctx.isIdle()) return;
134
+ const entry = latestPreferenceEntry(ctx);
135
+ if (entry?.type !== "custom" || !entry.data || typeof entry.data !== "object") return;
136
+ const request = entry.data;
137
+ if (request.status !== "pending" || typeof request.id !== "string") return;
138
+ const preferences = normalizePlanModeSettings(request.preferences);
139
+ pi.appendEntry(FRESH_PREFERENCES_ENTRY, { id: request.id, status: "failed" });
140
+ if (!preferences || !hasImplementationPreferences(preferences)) return;
141
+ const change = createImplementationPreferenceChange(
142
+ pi,
143
+ ctx,
144
+ isCurrent,
145
+ void 0,
146
+ selectionSnapshot
147
+ );
148
+ try {
149
+ const originalModel = ctx.model;
150
+ const originalThinking = pi.getThinkingLevel();
151
+ const current = () => isCurrent() && ctx.isIdle();
152
+ const model = await preflightImplementationPreferences(
153
+ ctx,
154
+ preferences,
155
+ () => current() && sameModel(ctx.model, originalModel) && pi.getThinkingLevel() === originalThinking
156
+ );
157
+ if (!model || !current()) return;
158
+ if (!await change.apply(model, preferences, current) || !current()) {
159
+ await change.rollback();
160
+ return;
161
+ }
162
+ pi.appendEntry(FRESH_PREFERENCES_ENTRY, {
163
+ id: request.id,
164
+ status: "applied",
165
+ model: { provider: ctx.model?.provider, id: ctx.model?.id },
166
+ thinking: pi.getThinkingLevel()
167
+ });
168
+ } catch (error) {
169
+ if (!isCurrent()) return;
170
+ await change.rollback().catch(() => void 0);
171
+ if (!isCurrent()) return;
172
+ pi.appendEntry(FRESH_PREFERENCES_ENTRY, {
173
+ id: request.id,
174
+ status: "failed",
175
+ error: preferenceError(error)
176
+ });
177
+ }
178
+ }
179
+ function latestPreferenceEntry(ctx) {
180
+ return ctx.sessionManager.getBranch().slice().reverse().find((entry) => entry.type === "custom" && entry.customType === FRESH_PREFERENCES_ENTRY);
181
+ }
182
+ function freshPreferencesApplied(ctx, id) {
183
+ const entry = latestPreferenceEntry(ctx);
184
+ if (entry?.type !== "custom" || !entry.data || typeof entry.data !== "object") return false;
185
+ const result = entry.data;
186
+ return result.id === id && result.status === "applied" && sameModel(ctx.model, result.model) && ctx.thinkingLevel === result.thinking;
187
+ }
188
+
123
189
  // src/prompt.ts
124
190
  var PLAN_CONTEXT_MARKER = "[CODEX-LIKE PLAN MODE ACTIVE]";
125
191
  function buildPlanModePrompt() {
@@ -295,6 +361,7 @@ async function startFreshImplementationFromState(ctx, options) {
295
361
  return startFreshImplementationSession(ctx, {
296
362
  plan,
297
363
  source,
364
+ preferences: options.preferences,
298
365
  retention: options.retention,
299
366
  stateEntryType: options.stateEntryType,
300
367
  isCurrent
@@ -306,8 +373,22 @@ async function startFreshImplementationSession(ctx, request) {
306
373
  }
307
374
  await ctx.waitForIdle();
308
375
  if (!request.isCurrent()) return { kind: "stale" };
309
- if (!await preflightModel(ctx, request.isCurrent)) return { kind: "rejected" };
310
- if (!request.isCurrent()) return { kind: "stale" };
376
+ const preferences = request.preferences ?? {};
377
+ const originalModel = ctx.model;
378
+ const originalThinking = ctx.thinkingLevel;
379
+ const preflightCurrent = () => request.isCurrent() && sameModel(ctx.model, originalModel) && ctx.thinkingLevel === originalThinking;
380
+ const preferencesId = hasImplementationPreferences(preferences) ? randomUUID() : void 0;
381
+ if (preferencesId) {
382
+ try {
383
+ if (!await preflightImplementationPreferences(ctx, preferences, preflightCurrent))
384
+ return { kind: "stale" };
385
+ } catch (error) {
386
+ if (preflightCurrent())
387
+ safeNotify(ctx, `Unable to implement the plan: ${safeErrorDetail(error)}`, "error");
388
+ return { kind: "rejected" };
389
+ }
390
+ } else if (!await preflightModel(ctx, preflightCurrent)) return { kind: "rejected" };
391
+ if (!preflightCurrent() || !ctx.isIdle()) return { kind: "stale" };
311
392
  const usesConversationHistory = request.retention === "clear-on-start";
312
393
  const destinationState = usesConversationHistory ? void 0 : {
313
394
  enabled: false,
@@ -331,6 +412,12 @@ async function startFreshImplementationSession(ctx, request) {
331
412
  ...parentSession ? { parentSession } : {},
332
413
  setup: async (sessionManager) => {
333
414
  try {
415
+ if (preferencesId)
416
+ sessionManager.appendCustomEntry(FRESH_PREFERENCES_ENTRY, {
417
+ id: preferencesId,
418
+ status: "pending",
419
+ preferences
420
+ });
334
421
  const contract = createModeContractMessage("normal");
335
422
  sessionManager.appendCustomMessageEntry(
336
423
  contract.customType,
@@ -346,6 +433,9 @@ async function startFreshImplementationSession(ctx, request) {
346
433
  }
347
434
  },
348
435
  withSession: async (replacementCtx) => {
436
+ if (!setupError && preferencesId && !freshPreferencesApplied(replacementCtx, preferencesId)) {
437
+ setupError = "Implementation model/thinking could not be applied or changed before kickoff";
438
+ }
349
439
  if (setupError) {
350
440
  recoverSetupFailure(replacementCtx, handoff, setupError);
351
441
  return;
@@ -442,7 +532,15 @@ function safeErrorDetail(error) {
442
532
 
443
533
  // src/plan-action-controller.ts
444
534
  function createPlanActionController(options) {
445
- const freshAction = (ctx, lifecycle, signal) => options.implementFresh(ctx, () => lifecycle.isCurrent() && !signal.aborted);
535
+ const implementationActions = (ctx, lifecycle) => ({
536
+ implementationPreferences: options.getImplementationPreferences?.(),
537
+ implementHere: (preferences, signal) => options.implementHere(
538
+ ctx,
539
+ preferences,
540
+ () => lifecycle.isCurrent() && !lifecycle.signal.aborted && !signal?.aborted
541
+ ),
542
+ implementFresh: (signal, preferences) => options.implementFresh(ctx, () => lifecycle.isCurrent() && !signal.aborted, preferences)
543
+ });
446
544
  return {
447
545
  async showSaved(ctx) {
448
546
  const lifecycle = options.captureLifecycle();
@@ -456,8 +554,7 @@ function createPlanActionController(options) {
456
554
  signal: lifecycle.signal,
457
555
  isCurrent: lifecycle.isCurrent,
458
556
  show: () => options.show(ctx),
459
- implementHere: () => options.implementHere(ctx),
460
- implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
557
+ ...implementationActions(ctx, lifecycle),
461
558
  exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
462
559
  settings: (signal) => options.settings(ctx, signal, lifecycle.isCurrent),
463
560
  clear: () => options.clearSaved(ctx)
@@ -480,8 +577,7 @@ function createPlanActionController(options) {
480
577
  ...lifecycle,
481
578
  show: () => options.show(ctx),
482
579
  finalize: () => options.finalize(ctx),
483
- implementHere: () => options.implementHere(ctx),
484
- implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
580
+ ...implementationActions(ctx, lifecycle),
485
581
  exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
486
582
  save: () => options.save(ctx),
487
583
  stay: () => options.stay(ctx),
@@ -497,8 +593,7 @@ function createPlanActionController(options) {
497
593
  ...lifecycle,
498
594
  implementationOutcome: options.implementationOutcome,
499
595
  getExportDestination: () => options.getExportDestination(ctx),
500
- implementHere: () => options.implementHere(ctx),
501
- implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
596
+ ...implementationActions(ctx, lifecycle),
502
597
  exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
503
598
  save: () => options.save(ctx),
504
599
  stay: () => void 0,
@@ -866,6 +961,7 @@ var PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
866
961
  var BLOCKED_MUTATING_TOOLS = /* @__PURE__ */ new Set(["edit", "write", "update_plan"]);
867
962
  var DEFAULT_TOOLS = ["read", "bash", "edit", "write"];
868
963
  function planMode(pi, dependencies = {}) {
964
+ const selectionSnapshot = observeImplementationModelSelections(pi);
869
965
  const workflowMutex = new WorkflowMutex(pi);
870
966
  let workflowOwner;
871
967
  let currentSession;
@@ -874,7 +970,7 @@ function planMode(pi, dependencies = {}) {
874
970
  const loadInteractiveUi = () => {
875
971
  if (dependencies.loadInteractiveUi) return dependencies.loadInteractiveUi();
876
972
  if (!interactiveUiPromise) {
877
- interactiveUiPromise = import("./chunks/interactive-ui-N2ZKBVNE.js").catch((error) => {
973
+ interactiveUiPromise = import("./chunks/interactive-ui-M3GEZN4M.js").catch((error) => {
878
974
  interactiveUiPromise = void 0;
879
975
  throw error;
880
976
  });
@@ -895,6 +991,8 @@ function planMode(pi, dependencies = {}) {
895
991
  let latestCommandContext;
896
992
  let nextReadyPresentationNonce = 0;
897
993
  let menuGeneration = 0;
994
+ let implementationInFlight = false;
995
+ let applyingImplementationPreferences = false;
898
996
  let workflowGeneration = 0;
899
997
  let refreshStateBeforeFirstAgentStart = false;
900
998
  let menuController = new AbortController();
@@ -919,6 +1017,10 @@ function planMode(pi, dependencies = {}) {
919
1017
  getExportDestination: (ctx) => planExports.getDestination(ctx),
920
1018
  show: (ctx) => showStoredPlan(pi, ctx, state),
921
1019
  finalize: requestFinalPlan,
1020
+ getImplementationPreferences: () => ({
1021
+ implementationModel: settings.implementationModel,
1022
+ implementationThinkingLevel: settings.implementationThinkingLevel
1023
+ }),
922
1024
  implementHere: startImplementation,
923
1025
  implementFresh: startFreshImplementation,
924
1026
  exportPlan,
@@ -1180,6 +1282,13 @@ function planMode(pi, dependencies = {}) {
1180
1282
  if (!installRestoredState(restoredState, ctx)) return;
1181
1283
  implementationRetention.restore(state.activeImplementation);
1182
1284
  updateUi(ctx);
1285
+ if (event.reason === "new")
1286
+ await applyFreshImplementationPreferences(
1287
+ pi,
1288
+ ctx,
1289
+ () => generation === menuGeneration && !menuController.signal.aborted,
1290
+ selectionSnapshot
1291
+ );
1183
1292
  });
1184
1293
  pi.on("session_before_tree", (event, ctx) => {
1185
1294
  const target = ctx.sessionManager.getEntry(event.preparation.targetId);
@@ -1211,7 +1320,7 @@ function planMode(pi, dependencies = {}) {
1211
1320
  updateUi(ctx);
1212
1321
  });
1213
1322
  pi.on("thinking_level_select", (event) => {
1214
- if (!state.enabled || !state.appliedThinkingLevel) return;
1323
+ if (applyingImplementationPreferences || !state.enabled || !state.appliedThinkingLevel) return;
1215
1324
  if (event.level !== state.appliedThinkingLevel) {
1216
1325
  state = {
1217
1326
  ...state,
@@ -1622,15 +1731,73 @@ ${intent.plan}`,
1622
1731
  releaseWorkflowOwner();
1623
1732
  ctx.ui.notify("Plan saved for later. Plan mode disabled.", "info");
1624
1733
  }
1625
- async function startFreshImplementation(ctx, menuIsCurrent) {
1734
+ async function startFreshImplementation(ctx, menuIsCurrent, preferences = settings) {
1626
1735
  await startFreshImplementationFromState(ctx, {
1736
+ preferences,
1627
1737
  getState: () => state,
1628
1738
  menuIsCurrent,
1629
1739
  retention: configuredImplementationPlanRetention(settings),
1630
1740
  stateEntryType: STATE_ENTRY_TYPE
1631
1741
  });
1632
1742
  }
1633
- async function startImplementation(ctx) {
1743
+ async function startImplementation(ctx, preferences = settings, menuIsCurrent = () => true) {
1744
+ if (implementationInFlight || !menuIsCurrent()) return;
1745
+ if (!hasImplementationPreferences(preferences)) {
1746
+ await startImplementationCore(ctx);
1747
+ return;
1748
+ }
1749
+ if (!allowModeTransition(ctx, "start plan implementation")) return;
1750
+ if (ctx.mode === "print" || ctx.mode === "json")
1751
+ throw new Error("Implementation model options require TUI or RPC mode.");
1752
+ const previousState = state;
1753
+ if (!(state.enabled ? state.latestPlan : state.savedPlan?.plan)) return;
1754
+ const generation = menuGeneration;
1755
+ const workflow = workflowGeneration;
1756
+ const original = { model: ctx.model, thinking: pi.getThinkingLevel() };
1757
+ const sessionCurrent = () => generation === menuGeneration && !menuController.signal.aborted;
1758
+ let expectedState = state;
1759
+ const current = () => sessionCurrent() && workflow === workflowGeneration && state === expectedState && menuIsCurrent() && ctx.isIdle();
1760
+ const change = createImplementationPreferenceChange(
1761
+ pi,
1762
+ ctx,
1763
+ sessionCurrent,
1764
+ original,
1765
+ selectionSnapshot
1766
+ );
1767
+ implementationInFlight = true;
1768
+ let started = false;
1769
+ try {
1770
+ const model = await preflightImplementationPreferences(
1771
+ ctx,
1772
+ preferences,
1773
+ () => current() && sameModel(ctx.model, original.model) && pi.getThinkingLevel() === original.thinking
1774
+ );
1775
+ if (!model || !current()) return;
1776
+ applyingImplementationPreferences = true;
1777
+ if (state.enabled) restoreThinkingLevel();
1778
+ expectedState = state;
1779
+ if (!await change.apply(model, preferences, current) || !current()) return;
1780
+ started = await startImplementationCore(ctx, true) === true;
1781
+ } catch (error) {
1782
+ if (sessionCurrent())
1783
+ ctx.ui.notify(`Unable to start implementation: ${preferenceError(error)}`, "error");
1784
+ } finally {
1785
+ if (!started && sessionCurrent()) {
1786
+ await change.rollback().catch((error) => {
1787
+ if (sessionCurrent()) ctx.ui.notify(preferenceError(error), "error");
1788
+ });
1789
+ if (sessionCurrent() && state === expectedState) {
1790
+ state = previousState;
1791
+ captureManualThinkingLevel();
1792
+ persistState();
1793
+ updateUi(ctx);
1794
+ }
1795
+ }
1796
+ applyingImplementationPreferences = false;
1797
+ implementationInFlight = false;
1798
+ }
1799
+ }
1800
+ async function startImplementationCore(ctx, preferencesApplied = false) {
1634
1801
  const savedPlan = state.enabled ? void 0 : state.savedPlan;
1635
1802
  const initialPlan = (state.enabled ? state.latestPlan : savedPlan?.plan)?.trim();
1636
1803
  if (!initialPlan) {
@@ -1638,7 +1805,7 @@ ${intent.plan}`,
1638
1805
  return;
1639
1806
  }
1640
1807
  if (!allowModeTransition(ctx, "start plan implementation")) return;
1641
- if (savedPlan) {
1808
+ if (savedPlan && !preferencesApplied) {
1642
1809
  const sessionGeneration = menuGeneration;
1643
1810
  const planWorkflowGeneration = workflowGeneration;
1644
1811
  const isCurrent = () => sessionGeneration === menuGeneration && planWorkflowGeneration === workflowGeneration && !menuController.signal.aborted && !state.enabled && state.savedPlan === savedPlan;
@@ -1674,7 +1841,7 @@ ${intent.plan}`,
1674
1841
  workflowToolPolicy: void 0,
1675
1842
  manualThinkingLevel: void 0
1676
1843
  };
1677
- if (wasEnabled) {
1844
+ if (wasEnabled && !preferencesApplied) {
1678
1845
  restoreThinkingLevel();
1679
1846
  state = { ...state, manualThinkingLevel: void 0 };
1680
1847
  }
@@ -1688,13 +1855,14 @@ ${intent.plan}`,
1688
1855
  if (wasEnabled) {
1689
1856
  restoreWorkflowToolPolicy(state.workflowToolPolicy);
1690
1857
  publishModeContract("plan", ctx);
1691
- applyPlanThinkingLevel();
1858
+ if (!preferencesApplied) applyPlanThinkingLevel();
1692
1859
  }
1693
1860
  persistState();
1694
1861
  updateUi(ctx);
1695
1862
  return;
1696
1863
  }
1697
1864
  if (wasEnabled) releaseWorkflowOwner();
1865
+ return true;
1698
1866
  }
1699
1867
  function clearActiveImplementation(id, ctx) {
1700
1868
  if (state.activeImplementation?.id !== id) return false;