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
@@ -6,11 +6,11 @@ Repository-controlled project `advisor.json` files are not applied. Models, prom
6
6
 
7
7
  ## Configure from chat
8
8
 
9
- When the user asks the Executor to configure pi-advisor, it can call `advisor_config` with `action: "read"` to inspect current effective settings and persisted global overrides, or `action: "update"` with a sparse `changes` object. Updates use the exact keys returned as `writableKeys`; `null` removes a saved override and restores the extension default. The tool validates the merged config, preserves unknown fields, displays the old-to-new changes, and requires interactive confirmation before writing. It remains available even when model-facing Advisor tools are disabled.
9
+ When the user asks the Executor to configure pi-advisor, it can call `advisor_config` with `action: "read"` to inspect current effective settings and persisted global overrides, or `action: "update"` with a sparse `changes` object. Updates use the exact keys returned as `writableKeys`; `null` removes a saved override and restores the extension default. The tool validates the merged config, preserves unknown fields, displays the old-to-new changes, and requires interactive confirmation before writing. It remains available when model-facing Advisor tools are disabled in persistent mode; session mode also disables `advisor_config` until `/advisor` is run.
10
10
 
11
11
  Model changes open the existing interactive picker rather than accepting a model-generated ref as approval. Saving an Executor model only pins it; run `/advisor` to activate that model in the current session. Jev classifier and credential provisioning stays in `/advisor-settings`. `advisorOutcomeLogging` is supported only as a separate update through its existing consent path. Always on changes apply to future sessions and do not start an Advisor call immediately.
12
12
 
13
- `/advisor` also accepts `executor=`, `advisor=`, and `contextMaxChars=` overrides for the current activation. For example, `/advisor contextMaxChars=30000` sets the reconstructed-history limit; use `0` for no history. The `ALL` option in settings represents the complete current branch and remains subject to the Advisor model's context limit. When `executor` is unset, the Executor is the current Pi session model and thinking level (unless `executorEffort` is set); `/advisor` does not switch models. On first use, or when the saved Advisor ref is missing from the configuration or Pi's available model list, `/advisor` opens the available-model picker instead of silently choosing an unconfigured model. An explicit `executor` that is no longer available is picked again; choose **Current session model** to clear it. Selected refs are persisted only after the models pass activation checks.
13
+ `/advisor` also accepts `executor=`, `advisor=`, and `contextMaxChars=` overrides for the current activation. For example, `/advisor contextMaxChars=30000` sets the reconstructed-history limit; use `0` for no history. The `ALL` option in settings represents the complete current branch and remains subject to the Advisor model's context limit. When `executor` is unset, the Executor is the current Pi session model and thinking level (unless `executorEffort` is set); `/advisor` does not switch models. On first use, or when the saved Advisor ref is missing from the configuration or Pi's available model list, `/advisor` opens the available-model picker instead of silently choosing an unconfigured model. An explicit `executor` that is no longer available is picked again; choose **Current session model** to clear it. In the default persistent mode, selected refs are saved only after the models pass activation checks. In session mode, `/advisor` keeps model selections and arguments in memory without writing `advisor.json`; use `/advisor-models` to save them explicitly.
14
14
 
15
15
  All fields are optional. The model refs below are explicit examples of models available through your configured providers; omit them to choose models interactively with `/advisor`. Disclosure and redaction fields are explained in [Privacy and data handling](privacy.md).
16
16
 
@@ -47,6 +47,7 @@ All fields are optional. The model refs below are explicit examples of models av
47
47
  "advisorGitContext": "summary",
48
48
  "advisorGitContextMaxChars": 20000,
49
49
  "simpleMode": false,
50
+ "advisorActivation": "persistent",
50
51
  "advisorToolsEnabled": true,
51
52
  "alwaysOn": false,
52
53
  "advisorHerdrIntegration": true,
@@ -89,15 +90,15 @@ Payloads are session-local and never persisted. They expire after five minutes,
89
90
 
90
91
  The `/advisor-settings` row opens a searchable multi-select menu containing the models configured in Pi. Type to fuzzy-filter the list, use Space to toggle models, and press Enter to apply. The setting is checked before budgets, repeated-call tracking, Jev screening, or any Advisor provider request, so a denied model neither spends Advisor budget nor triggers a gate. Matching is exact and case-sensitive.
91
92
 
92
- ## Simple mode and persistent activation
93
+ ## Simple mode and activation scope
93
94
 
94
95
  - `simpleMode` defaults to `false`. When enabled, `ask_advisor` remains available for voluntary second opinions when Advisor tools are enabled, and `/advisor-manual` remains available. Plan/failure/completion rules, loop gates, blocking, call budgets, and session summaries are disabled. Context limits, result caps, redaction, and tool disclosure policies still apply.
95
- - `advisorToolsEnabled` defaults to `true` to preserve on-demand tool access. When `false`, Pi removes `ask_advisor` and `record_advisor_outcome` from the active tool set for new, resumed, forked, and reloaded sessions. This explicit opt-out takes precedence over `alwaysOn`. Re-enable tools in `/advisor-settings`, or run `/advisor`; the latter clears the opt-out only after successful activation.
96
- - `alwaysOn` defaults to `false` and controls automatic flow activation separately from tool availability. When enabled and Advisor tools are enabled, Pi restores the configured Executor, if any, and activates the flow for new, resumed, forked, and reloaded sessions. If model refs are missing or unavailable, startup reports the problem without activating; run `/advisor` to choose available models interactively. A native subagent launcher that sets `PI_SUBAGENT_CHILD=1` keeps its host-selected model and thinking level; Advisor tools still activate, but the child is not switched to the parent Executor.
97
- - With a pinned `executor`, an explicit `/model` selection made before `/advisor` is held for that session and adopted as the Executor on the next successful activation, and while the Advisor flow is active it is persisted as the Executor immediately. Without a pinned `executor`, `/model` only changes the session model, which the Executor follows. A model restored with a session or selected by cycling does not change the saved Executor.
98
- - `/advisor-off` persists `advisorToolsEnabled: false` and turns `alwaysOn` off, keeping model-facing Advisor tools disabled in later sessions. `/advisor-manual` remains available.
99
- - In Simple mode, settings keeps the Context window/history, Advisor tools, Simple mode, and Always on controls. Advanced values remain saved and take effect when Simple mode is disabled.
100
- - Settings changes are applied and persisted as they happen; Escape closes the screen without a separate Save action.
96
+ - `advisorActivation` defaults to `"persistent"`, preserving the existing behavior. Set it to `"session"` in `/advisor-settings`, through `advisor_config`, or in the global `advisor.json` to start with `ask_advisor`, `record_advisor_outcome`, and `advisor_config` inactive. `/advisor` and `/advisor-off` then change only the current session and never write `advisor.json`, including when `/advisor` opens the model picker or receives arguments. Model choices and arguments from `/advisor` remain in memory until the next session start; `/advisor-models` and `/advisor-settings` still save explicit configuration changes. `/advisor-manual` and `/advisor-settings` remain available while the tools are inactive.
97
+ - In persistent mode, `advisorToolsEnabled` defaults to `true` to preserve on-demand tool access. When `false`, Pi removes `ask_advisor` and `record_advisor_outcome` from the active tool set for new, resumed, forked, and reloaded sessions. This explicit opt-out takes precedence over `alwaysOn`. Re-enable tools in `/advisor-settings`, or run `/advisor`; the latter clears the opt-out only after successful activation. In session mode, the saved preference is left unchanged by `/advisor` and `/advisor-off` and does not prevent explicit activation.
98
+ - `alwaysOn` defaults to `false` and controls automatic flow activation separately from tool availability. In persistent mode, when enabled and Advisor tools are enabled, Pi restores the configured Executor, if any, and activates the flow for new, resumed, forked, and reloaded sessions. If model refs are missing or unavailable, startup reports the problem without activating; run `/advisor` to choose available models interactively. A native subagent launcher that sets `PI_SUBAGENT_CHILD=1` keeps its host-selected model and thinking level; Advisor tools still activate, but the child is not switched to the parent Executor. Session mode takes precedence at every `session_start`, including `/reload`, and leaves the saved `alwaysOn` value untouched.
99
+ - In persistent mode, `/advisor-off` persists `advisorToolsEnabled: false` and turns `alwaysOn` off, keeping model-facing Advisor tools disabled in later sessions. In session mode it disables all three Advisor tools without changing either saved value.
100
+ - In Simple mode, settings keeps the Context window/history, Advisor activation, Advisor tools, Simple mode, and Always on controls. Advanced values remain saved and take effect when Simple mode is disabled.
101
+ - Settings changes are applied and persisted as they happen; Escape closes the screen without a separate Save action. Switching to session mode keeps the current Advisor on/off state while making `advisor_config` follow it; the next `session_start` starts inactive. Switching back to persistent mode immediately applies the saved `advisorToolsEnabled` preference and makes `advisor_config` available.
101
102
 
102
103
  ## Experimental Advisor Scout
103
104
 
package/locales/de.json CHANGED
@@ -211,8 +211,12 @@
211
211
  "sessionSummary.title": "[Sitzungs-Advisor-Zusammenfassung]",
212
212
  "sessionSummary.triggers.label": "Auslöser:",
213
213
  "sessionSummary.usage.label": "Nutzung:",
214
- "settings.advisorToolsEnabled.desc": "Erlaubt dem Modell, die Advisor-Tools aufzurufen; /advisor-off deaktiviert sie sitzungsübergreifend.",
214
+ "settings.advisorActivation.desc": "Dauerhafte Aktivierung wählen oder bei jedem Sitzungsstart, einschließlich /reload, inaktiv beginnen.",
215
+ "settings.advisorActivation.label": "Advisor-Aktivierung",
216
+ "settings.advisorToolsEnabled.desc": "Erlaubt dem Modell, die Advisor-Tools aufzurufen; der Aktivierungsmodus bestimmt die Dauer.",
215
217
  "settings.advisorToolsEnabled.label": "Advisor-Tools",
218
+ "settings.advisorToolsEnabled.savedLabel": "Advisor-Tools (gespeichert)",
219
+ "settings.advisorToolsEnabled.sessionDesc": "Gespeicherte Einstellung für den dauerhaften Modus. Änderungen schalten auch die Advisor-Tools dieser Sitzung um.",
216
220
  "settings.agentsMdContext.desc": "Vertrauenswürdige globale und Projekt-AGENTS.md-Regeln in Advisor-Aufrufe einbeziehen.",
217
221
  "settings.agentsMdContext.label": "AGENTS.md-Kontext",
218
222
  "settings.alwaysOn.desc": "Den Advisor-Flow automatisch für unterstützte Agent-Turns ausführen.",
@@ -314,6 +318,7 @@
314
318
  "settings.untrackedContent.label": "Ungetrackter Dateiinhalt",
315
319
  "settingsCmd.off.alwaysOnTurnedOff": "Immer aktiv ausgeschaltet.",
316
320
  "settingsCmd.off.message": "Advisor-Tools deaktiviert. Aktuelles Modell unverändert.",
321
+ "settingsCmd.off.session": "Advisor-Tools für diese Sitzung deaktiviert. Aktuelles Modell unverändert.",
317
322
  "settingsCmd.offReloadFailed.prefix": "Advisor-Tools für diese Sitzung deaktiviert, aber die Konfiguration konnte nicht neu geladen werden:",
318
323
  "settingsCmd.offSaveFailed.prefix": "Advisor-Tools für diese Sitzung deaktiviert, aber die Einstellung konnte nicht gespeichert werden:",
319
324
  "settingsCmd.refreshFailed.prefix": "Advisor-Einstellungen gespeichert, aber die Statusaktualisierung schlug fehl:",
package/locales/en.json CHANGED
@@ -212,8 +212,12 @@
212
212
  "sessionSummary.title": "[Session Advisor Summary]",
213
213
  "sessionSummary.triggers.label": "Triggers:",
214
214
  "sessionSummary.usage.label": "Usage:",
215
- "settings.advisorToolsEnabled.desc": "Allow the model to call the Advisor tools; /advisor-off disables them across sessions.",
215
+ "settings.advisorActivation.desc": "Choose persistent activation or start inactive at each session start, including /reload.",
216
+ "settings.advisorActivation.label": "Advisor activation",
217
+ "settings.advisorToolsEnabled.desc": "Allow the model to call the Advisor tools; activation scope determines how long they stay enabled.",
216
218
  "settings.advisorToolsEnabled.label": "Advisor tools",
219
+ "settings.advisorToolsEnabled.savedLabel": "Advisor tools (saved)",
220
+ "settings.advisorToolsEnabled.sessionDesc": "Saved preference for persistent mode. Changing it also toggles this session’s Advisor tools.",
217
221
  "settings.agentsMdContext.desc": "Include trusted project and global AGENTS.md rules in Advisor calls.",
218
222
  "settings.agentsMdContext.label": "AGENTS.md context",
219
223
  "settings.alwaysOn.desc": "Run the Advisor flow automatically for supported agent turns.",
@@ -315,6 +319,7 @@
315
319
  "settings.untrackedContent.label": "Untracked file content",
316
320
  "settingsCmd.off.alwaysOnTurnedOff": "Always on turned off.",
317
321
  "settingsCmd.off.message": "Advisor tools disabled. Current model unchanged.",
322
+ "settingsCmd.off.session": "Advisor tools disabled for this session. Current model unchanged.",
318
323
  "settingsCmd.offReloadFailed.prefix": "Advisor tools disabled for this session, but configuration could not be reloaded:",
319
324
  "settingsCmd.offSaveFailed.prefix": "Advisor tools disabled for this session, but the setting could not be saved:",
320
325
  "settingsCmd.refreshFailed.prefix": "Advisor settings were saved, but the status refresh failed:",
package/locales/fr.json CHANGED
@@ -211,8 +211,12 @@
211
211
  "sessionSummary.title": "[Résumé Advisor de session]",
212
212
  "sessionSummary.triggers.label": "Déclencheurs :",
213
213
  "sessionSummary.usage.label": "Utilisation :",
214
- "settings.advisorToolsEnabled.desc": "Autorise le modèle à appeler les outils Advisor ; /advisor-off les désactive entre sessions.",
214
+ "settings.advisorActivation.desc": "Choisir une activation persistante ou démarrer inactif à chaque début de session, y compris /reload.",
215
+ "settings.advisorActivation.label": "Activation Advisor",
216
+ "settings.advisorToolsEnabled.desc": "Autorise le modèle à appeler les outils Advisor ; le mode d’activation détermine leur durée.",
215
217
  "settings.advisorToolsEnabled.label": "Outils Advisor",
218
+ "settings.advisorToolsEnabled.savedLabel": "Outils Advisor (enregistrés)",
219
+ "settings.advisorToolsEnabled.sessionDesc": "Préférence enregistrée pour le mode persistant. La modifier active ou désactive aussi les outils Advisor de cette session.",
216
220
  "settings.agentsMdContext.desc": "Inclure les règles AGENTS.md globales et du projet de confiance dans les appels Advisor.",
217
221
  "settings.agentsMdContext.label": "Contexte AGENTS.md",
218
222
  "settings.alwaysOn.desc": "Exécuter le flux Advisor automatiquement pour les tours d'agent pris en charge.",
@@ -314,6 +318,7 @@
314
318
  "settings.untrackedContent.label": "Contenu des fichiers non suivis",
315
319
  "settingsCmd.off.alwaysOnTurnedOff": "Toujours actif désactivé.",
316
320
  "settingsCmd.off.message": "Outils Advisor désactivés. Modèle actuel inchangé.",
321
+ "settingsCmd.off.session": "Outils Advisor désactivés pour cette session. Modèle actuel inchangé.",
317
322
  "settingsCmd.offReloadFailed.prefix": "Outils Advisor désactivés pour cette session, mais la configuration n'a pas pu être rechargée :",
318
323
  "settingsCmd.offSaveFailed.prefix": "Outils Advisor désactivés pour cette session, mais le réglage n'a pas pu être enregistré :",
319
324
  "settingsCmd.refreshFailed.prefix": "Réglages Advisor enregistrés, mais l'actualisation du statut a échoué :",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-advisor-flow",
3
- "version": "0.16.1-dev.1",
3
+ "version": "0.17.0",
4
4
  "description": "Advanced Executor/Advisor flow for Pi, fully configurable and extendable.",
5
5
  "keywords": [
6
6
  "advisor",
@@ -1,6 +1,8 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
+ import { getSessionActivationOverrides } from "../config/session-overrides.ts";
3
4
  import {
5
+ advisorActivationRef,
4
6
  advisorEffortRef,
5
7
  advisorFallbackModelRef,
6
8
  advisorRef,
@@ -53,9 +55,27 @@ export const prepareActivationModels = async (
53
55
  ctx: ExtensionContext,
54
56
  announce: boolean,
55
57
  executorOverride: boolean,
56
- advisorOverride: boolean
58
+ advisorOverride: boolean,
59
+ canContinue: () => boolean
57
60
  ): Promise<PreparedActivationModels | undefined> => {
58
- const persisted = getPersistedModelRefs();
61
+ const saved = getPersistedModelRefs();
62
+
63
+ const overrides =
64
+ advisorActivationRef === "session"
65
+ ? getSessionActivationOverrides(ctx)
66
+ : undefined;
67
+
68
+ const persisted = {
69
+ advisor:
70
+ overrides && Object.hasOwn(overrides, "advisor")
71
+ ? advisorRef || undefined
72
+ : saved.advisor,
73
+ executor:
74
+ overrides && Object.hasOwn(overrides, "executor")
75
+ ? executorRef || undefined
76
+ : saved.executor,
77
+ };
78
+
59
79
  const availableRefs = getAvailableModelRefs(ctx);
60
80
  const availableRefSet = availableRefs ? new Set(availableRefs) : undefined;
61
81
 
@@ -111,6 +131,7 @@ export const prepareActivationModels = async (
111
131
  }
112
132
 
113
133
  const selection = await selectAdvisorModels(ctx, {
134
+ canContinue,
114
135
  advisor: advisorOverride || persisted.advisor ? advisorRef : "",
115
136
  advisorEffort: advisorEffortRef,
116
137
  advisorFallbackModel: advisorFallbackModelRef,
@@ -118,9 +139,15 @@ export const prepareActivationModels = async (
118
139
  executorEffort: executorEffortRef,
119
140
  selectAdvisor: plan.selectAdvisor,
120
141
  selectExecutor: plan.selectExecutor,
142
+ }).catch((error) => {
143
+ if (!canContinue()) {
144
+ return undefined;
145
+ }
146
+
147
+ throw error;
121
148
  });
122
149
 
123
- if (!selection) {
150
+ if (!selection || !canContinue()) {
124
151
  return;
125
152
  }
126
153
 
@@ -0,0 +1,73 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+
3
+ import { getAdvisorSettings } from "../config/state.ts";
4
+ import {
5
+ enableAdvisorConfigurationTool,
6
+ setAdvisorToolsActive,
7
+ } from "./advisor-tools.ts";
8
+ import type { CommandRuntime } from "./types.ts";
9
+
10
+ export const reconcileAdvisorToolsAfterSettings = (
11
+ runtime: CommandRuntime,
12
+ ctx: ExtensionContext,
13
+ previous: ReturnType<typeof getAdvisorSettings>,
14
+ refreshPersistentTools = true
15
+ ) => {
16
+ const next = getAdvisorSettings();
17
+ const modeChanged = previous.advisorActivation !== next.advisorActivation;
18
+
19
+ const preferenceChanged =
20
+ previous.advisorToolsEnabled !== next.advisorToolsEnabled;
21
+
22
+ const sessionScoped = next.advisorActivation === "session";
23
+
24
+ const enabledByAlwaysOn =
25
+ !previous.alwaysOn &&
26
+ next.alwaysOn &&
27
+ !previous.advisorToolsEnabled &&
28
+ next.advisorToolsEnabled;
29
+
30
+ const persistentOutOfSync =
31
+ !sessionScoped && runtime.flowEnabled() !== next.advisorToolsEnabled;
32
+
33
+ if (
34
+ modeChanged ||
35
+ preferenceChanged ||
36
+ (!refreshPersistentTools && persistentOutOfSync)
37
+ ) {
38
+ runtime.noteAdvisorToolsStateChange();
39
+ }
40
+
41
+ let enabled = next.advisorToolsEnabled;
42
+
43
+ if (sessionScoped) {
44
+ if (modeChanged) {
45
+ runtime.advisorSessionState.sessionToolsEnabled = runtime.flowEnabled();
46
+ } else if (preferenceChanged && !enabledByAlwaysOn) {
47
+ runtime.advisorSessionState.sessionToolsEnabled = enabled;
48
+ }
49
+
50
+ enabled = runtime.advisorSessionState.sessionToolsEnabled;
51
+ setAdvisorToolsActive(runtime.pi, enabled, true);
52
+ } else {
53
+ if (modeChanged) {
54
+ enableAdvisorConfigurationTool(runtime.pi);
55
+ }
56
+
57
+ if (
58
+ refreshPersistentTools ||
59
+ modeChanged ||
60
+ preferenceChanged ||
61
+ persistentOutOfSync
62
+ ) {
63
+ setAdvisorToolsActive(runtime.pi, enabled);
64
+ }
65
+ }
66
+
67
+ if (!enabled) {
68
+ runtime.resetSameModelNotice();
69
+ }
70
+
71
+ runtime.updateSameModelNotice(ctx);
72
+ runtime.updateAdvisorUsageStatus(ctx);
73
+ };
@@ -7,21 +7,25 @@ import {
7
7
  isMarkedSubagent,
8
8
  } from "../child-session.ts";
9
9
  import { parseArgs } from "../config/args.ts";
10
+ import { rememberSessionActivationOverrides } from "../config/session-overrides.ts";
10
11
  import {
12
+ advisorActivationRef,
11
13
  advisorEffortRef,
14
+ advisorFallbackModelRef,
12
15
  advisorRef,
13
16
  advisorToolsEnabledRef,
14
17
  contextMaxCharsRef,
15
18
  executorEffortRef,
16
19
  executorRef,
17
20
  setAdvisorEffortRef,
21
+ setAdvisorFallbackModelRef,
18
22
  setAdvisorRef,
19
23
  setAdvisorToolsEnabledRef,
20
24
  setContextMaxCharsRef,
21
25
  setExecutorEffortRef,
22
26
  setExecutorRef,
23
27
  } from "../config/state.ts";
24
- import { saveConfig } from "../config/storage.ts";
28
+ import { rebaseConfigBaseline, saveConfig } from "../config/storage.ts";
25
29
  import { t } from "../i18n.ts";
26
30
  import { hasUsableModelAuth } from "../model-auth.ts";
27
31
  import { sameModelAdvisorDisabled } from "../tools/model-access.ts";
@@ -106,11 +110,41 @@ const restoreActivationRefsIfCurrent = (
106
110
  const liveRefSnapshot = () => ({
107
111
  advisor: advisorRef,
108
112
  advisorEffort: advisorEffortRef,
113
+ advisorFallbackModel: advisorFallbackModelRef,
109
114
  contextMaxChars: contextMaxCharsRef,
110
115
  executor: executorRef,
111
116
  executorEffort: executorEffortRef,
112
117
  });
113
118
 
119
+ const restoreActivationRefs = (
120
+ previous: ReturnType<typeof liveRefSnapshot>,
121
+ attempted: ReturnType<typeof liveRefSnapshot>
122
+ ) => {
123
+ if (advisorRef === attempted.advisor) {
124
+ setAdvisorRef(previous.advisor);
125
+ }
126
+
127
+ if (advisorEffortRef === attempted.advisorEffort) {
128
+ setAdvisorEffortRef(previous.advisorEffort);
129
+ }
130
+
131
+ if (advisorFallbackModelRef === attempted.advisorFallbackModel) {
132
+ setAdvisorFallbackModelRef(previous.advisorFallbackModel);
133
+ }
134
+
135
+ if (contextMaxCharsRef === attempted.contextMaxChars) {
136
+ setContextMaxCharsRef(previous.contextMaxChars);
137
+ }
138
+
139
+ if (executorRef === attempted.executor) {
140
+ setExecutorRef(previous.executor);
141
+ }
142
+
143
+ if (executorEffortRef === attempted.executorEffort) {
144
+ setExecutorEffortRef(previous.executorEffort);
145
+ }
146
+ };
147
+
114
148
  const persistSuccessfulActivation = (
115
149
  ctx: ExtensionContext,
116
150
  shouldSave: boolean,
@@ -149,6 +183,27 @@ const persistSuccessfulActivation = (
149
183
  return true;
150
184
  };
151
185
 
186
+ const announceActivation = (
187
+ ctx: ExtensionContext,
188
+ activeModel: { id: string; provider: string } | undefined
189
+ ) => {
190
+ const activeExecutorRef = effectiveExecutorRef(ctx);
191
+ const activeExecutorEffort = effectiveExecutorEffort(ctx);
192
+
193
+ notify(
194
+ ctx,
195
+ `${t(
196
+ "activation.explanation",
197
+ "The Advisor is a second-opinion model that reviews the Executor's context and returns risks, alternatives, and verification steps without changing files or running tools."
198
+ )}\n\n${t("activation.flow", "Advisor flow")} ${
199
+ sameModelAdvisorDisabled(ctx, activeModel)
200
+ ? t("activation.flowConfigured", "configured")
201
+ : t("activation.flowReady", "ready")
202
+ } — Executor: ${activeExecutorRef} (thinking: ${activeExecutorEffort || "default"}) · Advisor: ${advisorRef} (thinking: ${advisorEffortRef || "default"})`,
203
+ "info"
204
+ );
205
+ };
206
+
152
207
  export const activateAdvisor = async (
153
208
  runtime: CommandRuntime,
154
209
  args: string,
@@ -164,39 +219,10 @@ export const activateAdvisor = async (
164
219
  const canContinue = () =>
165
220
  runtime.toolAvailabilityRevision === toolAvailabilityRevision;
166
221
 
167
- const previous = {
168
- advisor: advisorRef,
169
- advisorEffort: advisorEffortRef,
170
- contextMaxChars: contextMaxCharsRef,
171
- executor: executorRef,
172
- executorEffort: executorEffortRef,
173
- };
174
-
175
- const liveSnapshot = liveRefSnapshot;
176
-
177
- // Restores only refs this activation still owns. A concurrent settings
178
- // save that replaced a ref keeps its newer value.
179
- const restoreRefs = (attempted: ReturnType<typeof liveSnapshot>) => {
180
- if (advisorRef === attempted.advisor) {
181
- setAdvisorRef(previous.advisor);
182
- }
183
-
184
- if (advisorEffortRef === attempted.advisorEffort) {
185
- setAdvisorEffortRef(previous.advisorEffort);
186
- }
187
-
188
- if (contextMaxCharsRef === attempted.contextMaxChars) {
189
- setContextMaxCharsRef(previous.contextMaxChars);
190
- }
191
-
192
- if (executorRef === attempted.executor) {
193
- setExecutorRef(previous.executor);
194
- }
222
+ const previous = liveRefSnapshot();
195
223
 
196
- if (executorEffortRef === attempted.executorEffort) {
197
- setExecutorEffortRef(previous.executorEffort);
198
- }
199
- };
224
+ const restoreRefs = (attempted: ReturnType<typeof liveRefSnapshot>) =>
225
+ restoreActivationRefs(previous, attempted);
200
226
 
201
227
  let attempted = liveRefSnapshot();
202
228
  const executorOverride = hasExecutorOverride(args);
@@ -216,7 +242,8 @@ export const activateAdvisor = async (
216
242
  ctx,
217
243
  announce,
218
244
  executorOverride,
219
- advisorOverride
245
+ advisorOverride,
246
+ canContinue
220
247
  );
221
248
 
222
249
  if (!prepared) {
@@ -243,18 +270,17 @@ export const activateAdvisor = async (
243
270
  return;
244
271
  }
245
272
 
246
- // parseArgs, model picking, and an inactive `/model` selection only mutate
247
- // in-memory refs. Persist them once both models are known and authenticated,
248
- // so an unusable model reference is never written to the configuration.
249
- const reenableTools = !advisorToolsEnabledRef;
273
+ // Persist model changes only after both models are known and authenticated.
274
+ const sessionScoped = advisorActivationRef === "session";
275
+ const reenableTools = !sessionScoped && !advisorToolsEnabledRef;
250
276
 
251
277
  if (reenableTools) {
252
278
  setAdvisorToolsEnabledRef(true);
253
279
  }
254
280
 
255
- const shouldSave = Boolean(
256
- args.trim() || prepared.pickedModels || reenableTools
257
- );
281
+ const shouldSave =
282
+ !sessionScoped &&
283
+ Boolean(args.trim() || prepared.pickedModels || reenableTools);
258
284
 
259
285
  if (
260
286
  !persistSuccessfulActivation(ctx, shouldSave, reenableTools, () =>
@@ -269,7 +295,13 @@ export const activateAdvisor = async (
269
295
  runtime.pi.setThinkingLevel(executorEffortRef as ThinkingLevel);
270
296
  }
271
297
 
272
- setAdvisorToolsActive(runtime.pi, true);
298
+ if (sessionScoped) {
299
+ rememberSessionActivationOverrides(ctx, previous, liveRefSnapshot());
300
+ rebaseConfigBaseline();
301
+ runtime.advisorSessionState.sessionToolsEnabled = true;
302
+ }
303
+
304
+ setAdvisorToolsActive(runtime.pi, true, sessionScoped);
273
305
 
274
306
  const activeModel = followsSessionExecutor()
275
307
  ? ctx.model
@@ -278,19 +310,6 @@ export const activateAdvisor = async (
278
310
  runtime.updateSameModelNotice(ctx, activeModel);
279
311
 
280
312
  if (announce) {
281
- const activeExecutorRef = effectiveExecutorRef(ctx);
282
- const activeExecutorEffort = effectiveExecutorEffort(ctx);
283
- notify(
284
- ctx,
285
- `${t(
286
- "activation.explanation",
287
- "The Advisor is a second-opinion model that reviews the Executor's context and returns risks, alternatives, and verification steps without changing files or running tools."
288
- )}\n\n${t("activation.flow", "Advisor flow")} ${
289
- sameModelAdvisorDisabled(ctx, activeModel)
290
- ? t("activation.flowConfigured", "configured")
291
- : t("activation.flowReady", "ready")
292
- } — Executor: ${activeExecutorRef} (thinking: ${activeExecutorEffort || "default"}) · Advisor: ${advisorRef} (thinking: ${advisorEffortRef || "default"})`,
293
- "info"
294
- );
313
+ announceActivation(ctx, activeModel);
295
314
  }
296
315
  };
@@ -2,18 +2,24 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
 
3
3
  const ADVISOR_TOOL_NAMES = new Set(["ask_advisor", "record_advisor_outcome"]);
4
4
 
5
- export const setAdvisorToolsActive = (
5
+ const ALL_ADVISOR_TOOL_NAMES = new Set([
6
+ ...ADVISOR_TOOL_NAMES,
7
+ "advisor_config",
8
+ ]);
9
+
10
+ const CONFIGURATION_TOOL_NAME = new Set(["advisor_config"]);
11
+
12
+ const setToolsActive = (
6
13
  pi: Pick<ExtensionAPI, "getActiveTools" | "setActiveTools">,
7
- enabled: boolean
14
+ enabled: boolean,
15
+ names: Set<string>
8
16
  ) => {
9
17
  const current = pi.getActiveTools();
10
18
 
11
- const next = current.filter(
12
- (name) => enabled || !ADVISOR_TOOL_NAMES.has(name)
13
- );
19
+ const next = current.filter((name) => enabled || !names.has(name));
14
20
 
15
21
  if (enabled) {
16
- for (const name of ADVISOR_TOOL_NAMES) {
22
+ for (const name of names) {
17
23
  if (!next.includes(name)) {
18
24
  next.push(name);
19
25
  }
@@ -27,3 +33,18 @@ export const setAdvisorToolsActive = (
27
33
  pi.setActiveTools(next);
28
34
  }
29
35
  };
36
+
37
+ export const setAdvisorToolsActive = (
38
+ pi: Pick<ExtensionAPI, "getActiveTools" | "setActiveTools">,
39
+ enabled: boolean,
40
+ includeConfiguration = false
41
+ ) =>
42
+ setToolsActive(
43
+ pi,
44
+ enabled,
45
+ includeConfiguration ? ALL_ADVISOR_TOOL_NAMES : ADVISOR_TOOL_NAMES
46
+ );
47
+
48
+ export const enableAdvisorConfigurationTool = (
49
+ pi: Pick<ExtensionAPI, "getActiveTools" | "setActiveTools">
50
+ ) => setToolsActive(pi, true, CONFIGURATION_TOOL_NAME);
@@ -92,26 +92,91 @@ export const summarizeAdvisorConfigPatch = (
92
92
  })
93
93
  .join("\n");
94
94
 
95
- const automationRiskNotes = (
95
+ const enabledToolsRiskNote = (
96
+ sessionScoped: boolean,
97
+ alwaysOn: boolean,
98
+ enabledByAlwaysOn: boolean
99
+ ) => {
100
+ if (sessionScoped && enabledByAlwaysOn) {
101
+ return "The saved tool preference changes; enabling Always on does not activate this session.";
102
+ }
103
+
104
+ if (sessionScoped) {
105
+ return "Advisor tools become available in this session; later session starts remain inactive.";
106
+ }
107
+
108
+ if (alwaysOn) {
109
+ return "Advisor tools become available, and saved Always on will activate the flow in future sessions.";
110
+ }
111
+
112
+ return "Advisor tools become available; this does not activate Always on.";
113
+ };
114
+
115
+ const activationRiskNotes = (
96
116
  changes: AdvisorConfigPatch,
97
117
  before: AdvisorConfigSnapshot
98
118
  ) => {
99
119
  const notes: string[] = [];
100
120
 
101
- if (changes.simpleMode === true) {
121
+ const sessionScoped =
122
+ changes.advisorActivation === "session" ||
123
+ (changes.advisorActivation === undefined &&
124
+ before.advisorActivation === "session");
125
+
126
+ if (changes.advisorActivation === "session") {
102
127
  notes.push(
103
- "Simple mode disables automatic gates, gate blocking, and configured call budgets; voluntary Advisor calls remain available."
128
+ "The current Advisor on/off state is kept; advisor_config follows it. Every session start, including /reload, starts inactive even when Always on is saved."
129
+ );
130
+ }
131
+
132
+ if (
133
+ changes.advisorActivation === "persistent" ||
134
+ changes.advisorActivation === null
135
+ ) {
136
+ notes.push(
137
+ "Persistent mode applies the saved Advisor tools preference immediately and makes advisor_config available."
104
138
  );
105
139
  }
106
140
 
107
141
  if (changes.advisorToolsEnabled === false) {
108
142
  notes.push(
109
- "Advisor tools will be disabled immediately. advisor_config remains available to re-enable them."
143
+ sessionScoped
144
+ ? "Advisor tools, including advisor_config, will be disabled immediately; use /advisor to enable them for this session."
145
+ : "Advisor tools will be disabled immediately. advisor_config remains available to re-enable them."
110
146
  );
111
147
  }
112
148
 
113
149
  if (changes.alwaysOn === true) {
114
- notes.push("Always on enables automatic activation for future sessions.");
150
+ notes.push(
151
+ sessionScoped
152
+ ? "Always on is saved, but session activation starts inactive."
153
+ : "Always on enables automatic activation for future sessions."
154
+ );
155
+ }
156
+
157
+ if (changes.advisorToolsEnabled === true) {
158
+ notes.push(
159
+ enabledToolsRiskNote(
160
+ sessionScoped,
161
+ before.alwaysOn === true || changes.alwaysOn === true,
162
+ changes.alwaysOn === true && before.advisorToolsEnabled === false
163
+ )
164
+ );
165
+ }
166
+
167
+ return notes;
168
+ };
169
+
170
+ const automationRiskNotes = (
171
+ changes: AdvisorConfigPatch,
172
+ before: AdvisorConfigSnapshot
173
+ ) => {
174
+ const notes = activationRiskNotes(changes, before);
175
+
176
+ if (changes.simpleMode === true) {
177
+ notes.push(
178
+ "Simple mode disables automatic gates, gate blocking, and configured call budgets; voluntary Advisor calls remain available."
179
+ );
115
180
  }
116
181
 
117
182
  if (
@@ -132,14 +197,6 @@ const automationRiskNotes = (
132
197
  notes.push("A blocked gate may stop the current session.");
133
198
  }
134
199
 
135
- if (changes.advisorToolsEnabled === true) {
136
- notes.push(
137
- before.alwaysOn === true || changes.alwaysOn === true
138
- ? "Advisor tools become available, and saved Always on will activate the flow in future sessions."
139
- : "Advisor tools become available; this does not activate Always on."
140
- );
141
- }
142
-
143
200
  return notes;
144
201
  };
145
202