pi-advisor-flow 0.16.1-dev.1 → 0.17.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +16 -8
  3. package/dist/index.js +328 -96
  4. package/docs/configuration.md +10 -9
  5. package/locales/de.json +6 -1
  6. package/locales/en.json +6 -1
  7. package/locales/fr.json +6 -1
  8. package/package.json +1 -1
  9. package/src/commands/activation-preparation.ts +30 -3
  10. package/src/commands/activation-settings.ts +73 -0
  11. package/src/commands/activation.ts +75 -56
  12. package/src/commands/advisor-tools.ts +27 -6
  13. package/src/commands/configuration-tool-preview.ts +70 -13
  14. package/src/commands/configuration-tool-runtime.ts +3 -20
  15. package/src/commands/configuration-tool-update.ts +23 -1
  16. package/src/commands/lifecycle.ts +30 -1
  17. package/src/commands/manual-command.ts +4 -3
  18. package/src/commands/manual-consultation.ts +5 -5
  19. package/src/commands/model-commands.ts +17 -0
  20. package/src/commands/model-picker.ts +14 -6
  21. package/src/commands/settings-commands.ts +36 -20
  22. package/src/commands/settings-persistence.ts +33 -1
  23. package/src/config/defaults.ts +3 -0
  24. package/src/config/schema.ts +14 -1
  25. package/src/config/session-overrides.ts +109 -0
  26. package/src/config/state.ts +7 -0
  27. package/src/config/storage.ts +15 -1
  28. package/src/config/types.ts +3 -0
  29. package/src/session-state.ts +2 -0
  30. package/src/tools/budget-feedback.ts +2 -2
  31. package/src/tools/register-lifecycle.ts +12 -6
  32. package/src/tools/registration.ts +8 -1
  33. package/src/tools/session-availability.ts +10 -0
  34. package/src/ui/settings-items.ts +27 -6
  35. package/src/ui/settings-mutations.ts +7 -0
  36. package/src/ui/settings-selector.ts +1 -0
  37. package/src/ui/types.ts +6 -1
@@ -1,7 +1,7 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
- import { getAdvisorSettings } from "../config/state.ts";
4
- import { setAdvisorToolsActive } from "./advisor-tools.ts";
3
+ import type { getAdvisorSettings } from "../config/state.ts";
4
+ import { reconcileAdvisorToolsAfterSettings } from "./activation-settings.ts";
5
5
  import type { CommandRuntime } from "./types.ts";
6
6
 
7
7
  export const applyAdvisorConfigRuntimeSettings = (
@@ -9,24 +9,7 @@ export const applyAdvisorConfigRuntimeSettings = (
9
9
  ctx: ExtensionContext,
10
10
  previous: ReturnType<typeof getAdvisorSettings>
11
11
  ) => {
12
- const next = getAdvisorSettings();
13
- const shouldEnable = next.advisorToolsEnabled ?? true;
14
- const toolsAreActive = runtime.pi.getActiveTools().includes("ask_advisor");
15
-
16
- if (
17
- previous.advisorToolsEnabled !== next.advisorToolsEnabled ||
18
- toolsAreActive !== shouldEnable
19
- ) {
20
- runtime.noteAdvisorToolsStateChange();
21
- setAdvisorToolsActive(runtime.pi, shouldEnable);
22
-
23
- if (!shouldEnable) {
24
- runtime.resetSameModelNotice();
25
- }
26
- }
27
-
28
- runtime.updateSameModelNotice(ctx);
29
- runtime.updateAdvisorUsageStatus(ctx);
12
+ reconcileAdvisorToolsAfterSettings(runtime, ctx, previous, false);
30
13
  };
31
14
 
32
15
  export const cancelledAdvisorConfigResult = (message: string) => ({
@@ -1,5 +1,9 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
+ import {
4
+ clearSessionActivationOverrides,
5
+ SESSION_ACTIVATION_KEYS,
6
+ } from "../config/session-overrides.ts";
3
7
  import { getAdvisorSettings, setContextMaxCharsRef } from "../config/state.ts";
4
8
  import {
5
9
  configPaths,
@@ -95,12 +99,19 @@ const appliedAdvisorConfigResult = (
95
99
  ) => {
96
100
  const notices: string[] = [];
97
101
 
102
+ const sessionScoped =
103
+ prepared.proposed.advisorActivation === "session" ||
104
+ (prepared.proposed.advisorActivation === undefined &&
105
+ prepared.beforeSettings.advisorActivation === "session");
106
+
98
107
  if (
99
108
  Object.hasOwn(prepared.proposed, "alwaysOn") &&
100
109
  prepared.proposed.alwaysOn === true
101
110
  ) {
102
111
  notices.push(
103
- "Always on applies to future sessions; this did not start an Advisor call."
112
+ sessionScoped
113
+ ? "Always on is saved but session activation starts inactive; this did not start an Advisor call."
114
+ : "Always on applies to future sessions; this did not start an Advisor call."
104
115
  );
105
116
  }
106
117
 
@@ -162,6 +173,17 @@ const applyAdvisorConfigUpdate = (
162
173
  savedPath: string
163
174
  ) => {
164
175
  try {
176
+ if (Object.hasOwn(prepared.proposed, "advisorActivation")) {
177
+ clearSessionActivationOverrides(ctx);
178
+ } else {
179
+ clearSessionActivationOverrides(
180
+ ctx,
181
+ SESSION_ACTIVATION_KEYS.filter((key) =>
182
+ Object.hasOwn(prepared.proposed, key)
183
+ )
184
+ );
185
+ }
186
+
165
187
  loadConfig(ctx);
166
188
 
167
189
  if (
@@ -1,6 +1,11 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
- import { advisorToolsEnabledRef, alwaysOnRef } from "../config/state.ts";
3
+ import { clearSessionActivationOverrides } from "../config/session-overrides.ts";
4
+ import {
5
+ advisorActivationRef,
6
+ advisorToolsEnabledRef,
7
+ alwaysOnRef,
8
+ } from "../config/state.ts";
4
9
  import { loadConfig } from "../config/storage.ts";
5
10
  import { t } from "../i18n.ts";
6
11
  import { setAdvisorToolsActive } from "./advisor-tools.ts";
@@ -18,6 +23,9 @@ export const registerCommandLifecycle = (
18
23
  activateAdvisor: ActivateAdvisor
19
24
  ) => {
20
25
  runtime.pi.on("session_start", async (_event, ctx) => {
26
+ runtime.noteAdvisorToolsStateChange();
27
+ runtime.advisorSessionState.sessionToolsEnabled = false;
28
+ clearSessionActivationOverrides(ctx);
21
29
  runtime.resetSameModelNotice();
22
30
 
23
31
  // A malformed advisor.json or a provider auth failure must not reject a
@@ -25,6 +33,12 @@ export const registerCommandLifecycle = (
25
33
  try {
26
34
  loadConfig(ctx);
27
35
 
36
+ if (advisorActivationRef === "session") {
37
+ setAdvisorToolsActive(runtime.pi, false, true);
38
+
39
+ return;
40
+ }
41
+
28
42
  if (!advisorToolsEnabledRef) {
29
43
  setAdvisorToolsActive(runtime.pi, false);
30
44
  runtime.updateSameModelNotice(ctx);
@@ -49,6 +63,18 @@ export const registerCommandLifecycle = (
49
63
  }
50
64
  });
51
65
 
66
+ runtime.pi.on("session_tree", (_event, ctx) => {
67
+ if (advisorActivationRef === "session") {
68
+ runtime.noteAdvisorToolsStateChange();
69
+ setAdvisorToolsActive(
70
+ runtime.pi,
71
+ runtime.advisorSessionState.sessionToolsEnabled,
72
+ true
73
+ );
74
+ runtime.updateSameModelNotice(ctx);
75
+ }
76
+ });
77
+
52
78
  runtime.pi.on("model_select", (event, ctx) => {
53
79
  if (!runtime.suppressModelSelectionSync) {
54
80
  runtime.updateSameModelNotice(ctx, event.model);
@@ -59,6 +85,9 @@ export const registerCommandLifecycle = (
59
85
  });
60
86
 
61
87
  runtime.pi.on("session_shutdown", (_event, ctx) => {
88
+ runtime.noteAdvisorToolsStateChange();
89
+ clearSessionActivationOverrides(ctx);
90
+
62
91
  if (ctx.hasUI) {
63
92
  ctx.ui.setStatus("advisor-usage", undefined);
64
93
  }
@@ -123,9 +123,10 @@ export const registerManualCommand = (runtime: CommandRuntime) => {
123
123
  runtime.manualConsultations.set(controller, scoutStatusToken);
124
124
  runtime.pi.appendEntry?.("advisor-manual-call", { progressId, question });
125
125
 
126
- // Print mode tears the session down when the handler returns, so the
127
- // consultation must complete inside the handler to keep its ctx valid.
128
- if (ctx.mode === "print") {
126
+ // One-shot runs (`pi -p`, `pi --mode json -p`) tear the session down
127
+ // when the handler returns, so the consultation must complete inside
128
+ // the handler to keep its ctx valid.
129
+ if (ctx.mode === "print" || ctx.mode === "json") {
129
130
  await startManualConsultation(
130
131
  runtime,
131
132
  ctx,
@@ -34,7 +34,9 @@ export const startManualConsultation = async (
34
34
  progress: ManualAdvisorProgressState,
35
35
  gitContext?: GitContextLevel
36
36
  ) => {
37
- const isPrintMode = ctx.mode === "print";
37
+ // One-shot runs (`pi -p`, `pi --mode json -p`) have no follow-up turn;
38
+ // triggering one trips pi's turn_end entry resolution during teardown.
39
+ const isOneShot = ctx.mode === "print" || ctx.mode === "json";
38
40
  progress.phase = "preparing";
39
41
  runtime.requestManualRender(ctx);
40
42
 
@@ -153,10 +155,8 @@ export const startManualConsultation = async (
153
155
  {
154
156
  // Steer lets the current turn finish its active work; the Executor sees
155
157
  // the result before its next model call rather than being interrupted.
156
- // Print mode has no follow-up turn; triggering one trips pi's turn_end
157
- // entry resolution during teardown.
158
158
  deliverAs: "steer",
159
- triggerTurn: !isPrintMode,
159
+ triggerTurn: !isOneShot,
160
160
  }
161
161
  );
162
162
  } catch (error) {
@@ -185,7 +185,7 @@ export const startManualConsultation = async (
185
185
  },
186
186
  display: true,
187
187
  },
188
- { deliverAs: "steer", triggerTurn: !isPrintMode }
188
+ { deliverAs: "steer", triggerTurn: !isOneShot }
189
189
  );
190
190
  notify(
191
191
  ctx,
@@ -1,4 +1,6 @@
1
+ import { clearSessionActivationOverrides } from "../config/session-overrides.ts";
1
2
  import {
3
+ advisorActivationRef,
2
4
  advisorEffortRef,
3
5
  advisorFallbackModelRef,
4
6
  advisorRef,
@@ -18,6 +20,14 @@ import { activateAdvisor } from "./activation.ts";
18
20
  import { selectAdvisorModels } from "./model-picker.ts";
19
21
  import type { CommandRuntime } from "./types.ts";
20
22
 
23
+ const MODEL_SETTINGS = [
24
+ "advisor",
25
+ "advisorEffort",
26
+ "advisorFallbackModel",
27
+ "executor",
28
+ "executorEffort",
29
+ ] as const;
30
+
21
31
  export const registerModelCommands = (runtime: CommandRuntime) => {
22
32
  runtime.pi.registerCommand("advisor", {
23
33
  description:
@@ -58,11 +68,18 @@ export const registerModelCommands = (runtime: CommandRuntime) => {
58
68
  setExecutorEffortRef(selection.executorEffort);
59
69
  setAdvisorEffortRef(selection.advisorEffort);
60
70
 
71
+ const sessionScoped = advisorActivationRef === "session";
72
+
61
73
  const path = saveConfig(ctx, {
74
+ forceKeys: sessionScoped ? MODEL_SETTINGS : undefined,
62
75
  persistAdvisor: true,
63
76
  persistExecutor: true,
64
77
  });
65
78
 
79
+ if (sessionScoped) {
80
+ clearSessionActivationOverrides(ctx, MODEL_SETTINGS);
81
+ }
82
+
66
83
  runtime.updateSameModelNotice(ctx);
67
84
  ctx.ui.notify(
68
85
  `${t("notify.modelsSaved.prefix", "Saved Executor + Advisor configurations to")} ${path}`,
@@ -21,6 +21,7 @@ export interface AdvisorModelSelection {
21
21
  }
22
22
 
23
23
  export interface AdvisorModelPickerOptions {
24
+ canContinue?: () => boolean;
24
25
  advisor: string | undefined;
25
26
  advisorEffort: string | undefined;
26
27
  advisorFallbackModel: string | undefined;
@@ -30,11 +31,18 @@ export interface AdvisorModelPickerOptions {
30
31
  selectExecutor: boolean;
31
32
  }
32
33
 
34
+ const isCurrentChoice = (
35
+ choice: string | undefined,
36
+ canContinue: () => boolean
37
+ ): choice is string => Boolean(choice) && canContinue();
38
+
33
39
  export const selectAdvisorModels = async (
34
40
  ctx: ExtensionContext,
35
41
  options: AdvisorModelPickerOptions
36
42
  ): Promise<AdvisorModelSelection | undefined> => {
37
- if (!ctx.hasUI) {
43
+ const canContinue = () => options.canContinue?.() ?? true;
44
+
45
+ if (!canContinue() || !ctx.hasUI) {
38
46
  return undefined;
39
47
  }
40
48
 
@@ -84,7 +92,7 @@ export const selectAdvisorModels = async (
84
92
  })
85
93
  );
86
94
 
87
- if (!selectedExecutor) {
95
+ if (!isCurrentChoice(selectedExecutor, canContinue)) {
88
96
  return undefined;
89
97
  }
90
98
 
@@ -96,7 +104,7 @@ export const selectAdvisorModels = async (
96
104
  effortChoices(executorEffort)
97
105
  );
98
106
 
99
- if (!selectedExecutorEffort) {
107
+ if (!isCurrentChoice(selectedExecutorEffort, canContinue)) {
100
108
  return undefined;
101
109
  }
102
110
 
@@ -122,7 +130,7 @@ export const selectAdvisorModels = async (
122
130
  })
123
131
  );
124
132
 
125
- if (!selectedAdvisor) {
133
+ if (!isCurrentChoice(selectedAdvisor, canContinue)) {
126
134
  return undefined;
127
135
  }
128
136
 
@@ -131,7 +139,7 @@ export const selectAdvisorModels = async (
131
139
  effortChoices(advisorEffort)
132
140
  );
133
141
 
134
- if (!selectedAdvisorEffort) {
142
+ if (!isCurrentChoice(selectedAdvisorEffort, canContinue)) {
135
143
  return undefined;
136
144
  }
137
145
 
@@ -162,7 +170,7 @@ export const selectAdvisorModels = async (
162
170
  })
163
171
  );
164
172
 
165
- if (selectedFallback === undefined) {
173
+ if (selectedFallback === undefined || !canContinue()) {
166
174
  return undefined;
167
175
  }
168
176
 
@@ -1,6 +1,7 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
3
  import {
4
+ advisorActivationRef,
4
5
  alwaysOnRef,
5
6
  getAdvisorSettings,
6
7
  getPersistedModelRefs,
@@ -17,6 +18,7 @@ import {
17
18
  import { AdvisorSettingsSelector } from "../ui/settings-selector.ts";
18
19
  import type { AdvisorSettings } from "../ui/types.ts";
19
20
  import { loadCommandConfig } from "./activation-preparation.ts";
21
+ import { reconcileAdvisorToolsAfterSettings } from "./activation-settings.ts";
20
22
  import { setAdvisorToolsActive } from "./advisor-tools.ts";
21
23
  import {
22
24
  CONTEXT_PRESETS,
@@ -32,6 +34,8 @@ const persistJevSettings = (
32
34
  settings: AdvisorSettings,
33
35
  runtime: CommandRuntime
34
36
  ): boolean => {
37
+ const previous = getAdvisorSettings();
38
+
35
39
  try {
36
40
  saveAdvisorSettings(ctx, settings, { skipOutcomeLogging: true });
37
41
  } catch (error) {
@@ -45,9 +49,7 @@ const persistJevSettings = (
45
49
  }
46
50
 
47
51
  try {
48
- setAdvisorToolsActive(runtime.pi, settings.advisorToolsEnabled ?? true);
49
- runtime.updateSameModelNotice(ctx);
50
- runtime.updateAdvisorUsageStatus(ctx);
52
+ reconcileAdvisorToolsAfterSettings(runtime, ctx, previous);
51
53
  } catch (error) {
52
54
  const message = error instanceof Error ? error.message : String(error);
53
55
  ctx.ui.notify(
@@ -86,30 +88,26 @@ export const registerSettingsCommands = (runtime: CommandRuntime) => {
86
88
  modelRefs: getConfiguredModelRefs(ctx),
87
89
  onCancel: () => done(undefined),
88
90
  onChange: (settings) => {
89
- const toolsWereEnabled =
90
- getAdvisorSettings().advisorToolsEnabled ?? true;
91
-
92
- const toolsEnabled = settings.advisorToolsEnabled ?? true;
91
+ const previous = getAdvisorSettings();
93
92
 
94
93
  try {
95
94
  saveAdvisorSettings(ctx, settings);
96
-
97
- if (toolsWereEnabled !== toolsEnabled) {
98
- runtime.noteAdvisorToolsStateChange();
99
- }
100
-
101
- setAdvisorToolsActive(runtime.pi, toolsEnabled);
102
- runtime.updateSameModelNotice(ctx);
103
- runtime.updateAdvisorUsageStatus(ctx);
95
+ reconcileAdvisorToolsAfterSettings(runtime, ctx, previous);
104
96
  } catch (error) {
105
97
  const toolsDisabled =
106
- toolsWereEnabled && settings.advisorToolsEnabled === false;
98
+ previous.advisorToolsEnabled &&
99
+ settings.advisorToolsEnabled === false;
107
100
 
108
101
  if (toolsDisabled) {
109
102
  runtime.noteAdvisorToolsStateChange();
110
103
  setAdvisorToolsEnabledRef(false);
111
104
  setAlwaysOnRef(false);
112
- setAdvisorToolsActive(runtime.pi, false);
105
+ runtime.advisorSessionState.sessionToolsEnabled = false;
106
+ setAdvisorToolsActive(
107
+ runtime.pi,
108
+ false,
109
+ previous.advisorActivation === "session"
110
+ );
113
111
  runtime.resetSameModelNotice();
114
112
  }
115
113
 
@@ -140,11 +138,15 @@ export const registerSettingsCommands = (runtime: CommandRuntime) => {
140
138
  });
141
139
 
142
140
  runtime.pi.registerCommand("advisor-off", {
143
- description:
144
- "Disable model-facing Advisor tools across sessions; keep the current model",
141
+ description: "Disable Advisor tools; keep the current model",
145
142
  handler: (_args, ctx) => {
146
143
  runtime.noteAdvisorToolsStateChange();
147
- setAdvisorToolsActive(runtime.pi, false);
144
+ runtime.advisorSessionState.sessionToolsEnabled = false;
145
+ setAdvisorToolsActive(
146
+ runtime.pi,
147
+ false,
148
+ advisorActivationRef === "session"
149
+ );
148
150
  runtime.resetSameModelNotice();
149
151
 
150
152
  try {
@@ -165,6 +167,20 @@ export const registerSettingsCommands = (runtime: CommandRuntime) => {
165
167
  return Promise.resolve();
166
168
  }
167
169
 
170
+ if (advisorActivationRef === "session") {
171
+ setAdvisorToolsActive(runtime.pi, false, true);
172
+ notify(
173
+ ctx,
174
+ t(
175
+ "settingsCmd.off.session",
176
+ "Advisor tools disabled for this session. Current model unchanged."
177
+ ),
178
+ "info"
179
+ );
180
+
181
+ return Promise.resolve();
182
+ }
183
+
168
184
  const wasAlwaysOn = alwaysOnRef;
169
185
  const persisted = getPersistedModelRefs();
170
186
  setAdvisorToolsEnabledRef(false);
@@ -1,8 +1,11 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
+ import { clearSessionActivationOverrides } from "../config/session-overrides.ts";
4
+ import type { SessionActivationValues } from "../config/session-overrides.ts";
3
5
  import {
4
6
  getAdvisorSettings,
5
7
  getPersistedModelRefs,
8
+ setAdvisorActivationRef,
6
9
  setAdvisorAgentsMdContextRef,
7
10
  setAdvisorAutoLoopGateRef,
8
11
  setAdvisorBlockOnBlockedRef,
@@ -48,7 +51,11 @@ import {
48
51
  setShowUsageFooterRef,
49
52
  setSimpleModeRef,
50
53
  } from "../config/state.ts";
51
- import { saveConfig, saveGlobalOutcomeLogging } from "../config/storage.ts";
54
+ import {
55
+ loadConfig,
56
+ saveConfig,
57
+ saveGlobalOutcomeLogging,
58
+ } from "../config/storage.ts";
52
59
  import {
53
60
  DEFAULT_JEV_DIGEST_MAX_CHARS,
54
61
  DEFAULT_JEV_FILTER_NOUL_MARGIN,
@@ -87,6 +94,7 @@ const applySessionSettings = (settings: AdvisorSettings) => {
87
94
  setShowUsageDetailsRef(settings.showUsageDetails ?? true);
88
95
  setShowUsageFooterRef(settings.showUsageFooter ?? false);
89
96
  setSimpleModeRef(settings.simpleMode ?? false);
97
+ setAdvisorActivationRef(settings.advisorActivation ?? "persistent");
90
98
  setAdvisorToolsEnabledRef(settings.advisorToolsEnabled ?? true);
91
99
  setAlwaysOnRef(settings.alwaysOn ?? false);
92
100
  setAdvisorFailureModeRef(settings.failureMode ?? "block-session");
@@ -158,6 +166,30 @@ export const saveAdvisorSettings = (
158
166
  if (!options.skipOutcomeLogging) {
159
167
  saveGlobalOutcomeLogging(settings.outcomeLogging ?? false);
160
168
  }
169
+
170
+ if (previous.advisorActivation === "session") {
171
+ const next = getAdvisorSettings();
172
+ const changed: (keyof SessionActivationValues)[] = [];
173
+
174
+ if (previous.contextMaxChars !== next.contextMaxChars) {
175
+ changed.push("contextMaxChars");
176
+ }
177
+
178
+ if (previous.effort !== next.effort) {
179
+ changed.push("advisorEffort");
180
+ }
181
+
182
+ if (previous.fallbackModel !== next.fallbackModel) {
183
+ changed.push("advisorFallbackModel");
184
+ }
185
+
186
+ if (next.advisorActivation === "persistent") {
187
+ clearSessionActivationOverrides(ctx);
188
+ loadConfig(ctx);
189
+ } else {
190
+ clearSessionActivationOverrides(ctx, changed);
191
+ }
192
+ }
161
193
  } catch (error) {
162
194
  applyAdvisorSettings(previous);
163
195
  throw error;
@@ -1,4 +1,5 @@
1
1
  import {
2
+ setAdvisorActivationRef,
2
3
  setAdvisorAgentsMdContextRef,
3
4
  setAdvisorAutoLoopGateRef,
4
5
  setAdvisorBlockOnBlockedRef,
@@ -95,6 +96,7 @@ export const resetDefaults = () => {
95
96
  setAdvisorSessionSummaryRef(false);
96
97
  setAdvisorLogFeedbackRef(false);
97
98
  setSimpleModeRef(false);
99
+ setAdvisorActivationRef("persistent");
98
100
  setAdvisorToolsEnabledRef(true);
99
101
  setAlwaysOnRef(false);
100
102
  setAdvisorFailureModeRef("block-session");
@@ -222,6 +224,7 @@ export const applyConfig = (config: AdvisorConfig) => {
222
224
  applyOptionalConfig(config, "showUsageDetails", setShowUsageDetailsRef);
223
225
  applyOptionalConfig(config, "showUsageFooter", setShowUsageFooterRef);
224
226
  applyOptionalConfig(config, "simpleMode", setSimpleModeRef);
227
+ applyOptionalConfig(config, "advisorActivation", setAdvisorActivationRef);
225
228
  applyOptionalConfig(config, "advisorToolsEnabled", setAdvisorToolsEnabledRef);
226
229
  applyOptionalConfig(config, "alwaysOn", setAlwaysOnRef);
227
230
  applyOptionalConfig(config, "gateFailureMode", setAdvisorFailureModeRef);
@@ -22,6 +22,7 @@ import {
22
22
  isValidToolResultMaxLines,
23
23
  } from "./schema-validators.ts";
24
24
  import {
25
+ advisorActivationRef,
25
26
  advisorAgentsMdContextRef,
26
27
  advisorAutoLoopGateRef,
27
28
  advisorBlockOnBlockedRef,
@@ -75,7 +76,11 @@ import {
75
76
  showUsageFooterRef,
76
77
  simpleModeRef,
77
78
  } from "./state.ts";
78
- import type { AdvisorConfig, AdvisorConfigValue } from "./types.ts";
79
+ import type {
80
+ AdvisorActivation,
81
+ AdvisorConfig,
82
+ AdvisorConfigValue,
83
+ } from "./types.ts";
79
84
  import {
80
85
  GATE_FAILURE_MODES,
81
86
  JEV_TRANSPORTS,
@@ -121,6 +126,14 @@ export const CONFIG_SCHEMA = {
121
126
  persisted: true,
122
127
  type: "string",
123
128
  },
129
+ advisorActivation: {
130
+ accepted: "persistent or session",
131
+ current: () => advisorActivationRef,
132
+ persisted: true,
133
+ type: "enum",
134
+ validate: (value: unknown): value is AdvisorActivation =>
135
+ value === "persistent" || value === "session",
136
+ },
124
137
  advisorAgentsMdContext: {
125
138
  accepted: "true or false",
126
139
  current: () => advisorAgentsMdContextRef,
@@ -0,0 +1,109 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+
3
+ import {
4
+ setAdvisorEffortRef,
5
+ setAdvisorFallbackModelRef,
6
+ setAdvisorRef,
7
+ setContextMaxCharsRef,
8
+ setExecutorEffortRef,
9
+ setExecutorRef,
10
+ } from "./state.ts";
11
+
12
+ export interface SessionActivationValues {
13
+ advisor: string;
14
+ advisorEffort: string | undefined;
15
+ advisorFallbackModel: string | undefined;
16
+ contextMaxChars: number;
17
+ executor: string;
18
+ executorEffort: string | undefined;
19
+ }
20
+
21
+ export const SESSION_ACTIVATION_KEYS = [
22
+ "advisor",
23
+ "advisorEffort",
24
+ "advisorFallbackModel",
25
+ "contextMaxChars",
26
+ "executor",
27
+ "executorEffort",
28
+ ] as const satisfies readonly (keyof SessionActivationValues)[];
29
+
30
+ const overrides = new WeakMap<object, Partial<SessionActivationValues>>();
31
+
32
+ const sessionKey = (ctx: ExtensionContext): object => ctx.sessionManager ?? ctx;
33
+
34
+ export const getSessionActivationOverrides = (ctx: ExtensionContext) =>
35
+ overrides.get(sessionKey(ctx));
36
+
37
+ export const applySessionActivationOverrides = (ctx: ExtensionContext) => {
38
+ const values = getSessionActivationOverrides(ctx);
39
+
40
+ if (!values) {
41
+ return;
42
+ }
43
+
44
+ if (Object.hasOwn(values, "advisor")) {
45
+ setAdvisorRef(values.advisor ?? "");
46
+ }
47
+
48
+ if (Object.hasOwn(values, "advisorEffort")) {
49
+ setAdvisorEffortRef(values.advisorEffort);
50
+ }
51
+
52
+ if (Object.hasOwn(values, "advisorFallbackModel")) {
53
+ setAdvisorFallbackModelRef(values.advisorFallbackModel);
54
+ }
55
+
56
+ if (values.contextMaxChars !== undefined) {
57
+ setContextMaxCharsRef(values.contextMaxChars);
58
+ }
59
+
60
+ if (Object.hasOwn(values, "executor")) {
61
+ setExecutorRef(values.executor ?? "");
62
+ }
63
+
64
+ if (Object.hasOwn(values, "executorEffort")) {
65
+ setExecutorEffortRef(values.executorEffort);
66
+ }
67
+ };
68
+
69
+ export const rememberSessionActivationOverrides = (
70
+ ctx: ExtensionContext,
71
+ previous: SessionActivationValues,
72
+ current: SessionActivationValues
73
+ ) => {
74
+ const next = { ...getSessionActivationOverrides(ctx) };
75
+
76
+ for (const key of SESSION_ACTIVATION_KEYS) {
77
+ if (previous[key] !== current[key]) {
78
+ Object.assign(next, { [key]: current[key] });
79
+ }
80
+ }
81
+
82
+ if (Object.keys(next).length > 0) {
83
+ overrides.set(sessionKey(ctx), next);
84
+ }
85
+ };
86
+
87
+ export const clearSessionActivationOverrides = (
88
+ ctx: ExtensionContext,
89
+ keys: readonly (keyof SessionActivationValues)[] = SESSION_ACTIVATION_KEYS
90
+ ) => {
91
+ const key = sessionKey(ctx);
92
+ const existing = overrides.get(key);
93
+
94
+ if (!existing) {
95
+ return;
96
+ }
97
+
98
+ const remaining = { ...existing };
99
+
100
+ for (const name of keys) {
101
+ Reflect.deleteProperty(remaining, name);
102
+ }
103
+
104
+ if (Object.keys(remaining).length === 0) {
105
+ overrides.delete(key);
106
+ } else {
107
+ overrides.set(key, remaining);
108
+ }
109
+ };
@@ -17,6 +17,7 @@ import {
17
17
  DEFAULT_SCOUT_TIMEOUT_MS,
18
18
  } from "./types.ts";
19
19
  import type {
20
+ AdvisorActivation,
20
21
  AdvisorToolPolicies,
21
22
  GateFailureMode,
22
23
  JevTransport,
@@ -47,6 +48,7 @@ export let advisorSessionSummaryRef = false;
47
48
  export let advisorLogFeedbackRef = false;
48
49
  export let simpleModeRef = false;
49
50
  export let advisorToolsEnabledRef = true;
51
+ export let advisorActivationRef: AdvisorActivation = "persistent";
50
52
  export let alwaysOnRef = false;
51
53
  export let advisorFailureModeRef: GateFailureMode = "block-session";
52
54
  export let advisorHerdrIntegrationRef = true;
@@ -199,6 +201,10 @@ export const setAdvisorToolsEnabledRef = (enabled: boolean) => {
199
201
  advisorToolsEnabledRef = enabled;
200
202
  };
201
203
 
204
+ export const setAdvisorActivationRef = (activation: AdvisorActivation) => {
205
+ advisorActivationRef = activation;
206
+ };
207
+
202
208
  export const setAlwaysOnRef = (enabled: boolean) => {
203
209
  alwaysOnRef = enabled;
204
210
  };
@@ -329,6 +335,7 @@ export const setShowUsageFooterRef = (enabled: boolean) => {
329
335
  * imported mutable bindings, which can be snapshotted by extension loaders.
330
336
  */
331
337
  export const getAdvisorSettings = () => ({
338
+ advisorActivation: advisorActivationRef,
332
339
  advisorToolsEnabled: advisorToolsEnabledRef,
333
340
  agentsMdContext: advisorAgentsMdContextRef,
334
341
  alwaysOn: alwaysOnRef,