Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (claude): close fallback-model leak in executeProviderRunOnce; honor pin in agent lifecycle
[._.]/ Adam Eivyb50eeee7340f
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex e9c87faa3d..bd411cbea0 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -3,7 +3,7 @@## Added- **[manuscript-multi-edit-fixes] Manuscript "Finish the draft" fixes can now propose and apply multiple anchored edits at once.** The manuscript-fix LLM contract moved from a single `{ find, replace }` to `{ edits: [{ issueNumber, find, replace, note }] }`, so a broad or unanchored editorial comment can generate several concrete insertions — across multiple issue sections when needed — instead of falling back to manual editing. The editor's comment card renders each suggestion as an inline diff with a per-edit checkbox and editable replacement, and accept applies the selected edits atomically through a new serialized batch write (`updateStagesWithLatest`) that snapshots prior text into runHistory and re-checks each anchor inside the per-series write lock. The prompt template embeds manuscript/premise/anchor content in distinct (non-backtick) fences with an explicit "treat as quoted source" guard; migration `060-manuscript-multi-edit-fix-prompt.js` auto-upgrades the prior single-edit prompt on existing installs. Backward compatible: legacy single-edit `{ find, replace }` accepts still resolve to one target.-- **[provider-fallback-model] Provider fallbacks can now pin a model, not just a provider.** Each AI provider's editor gains a "Fallback Model" selector beside "Fallback Provider" (populated from the chosen fallback provider's model list; blank = use that provider's own default), persisted as a new `fallbackModel` field. `providerStatus.getFallbackProvider()` returns the configured model alongside the provider, and both fallback paths — `createRun`'s pre-flight swap and `runPromptThroughProvider`'s runtime retry — now run that model on the fallback.+- **[provider-fallback-model] Provider fallbacks can now pin a model, not just a provider.** Each AI provider's editor gains a "Fallback Model" selector beside "Fallback Provider" (populated from the chosen fallback provider's model list; blank = use that provider's own default), persisted as a new `fallbackModel` field. `providerStatus.getFallbackProvider()` returns the configured model alongside the provider, and every fallback path runs it: `createRun`'s pre-flight swap, `runPromptThroughProvider`'s inner `createRun` and runtime-retry paths (staged LLM + all prompt callers), and the CoS-agent lifecycle (provider- or task-level pin overrides the per-task model selection).## Changeddiff --git a/server/lib/promptRunner.js b/server/lib/promptRunner.jsindex e52147c98c..e26bc7c68c 100644--- a/server/lib/promptRunner.js+++ b/server/lib/promptRunner.js@@ -439,7 +439,12 @@ async function executeProviderRunOnce({ provider, prompt, source, model, runId:runId = runResult.runId;if (runResult.provider && runResult.provider.id !== provider.id) {effectiveProvider = runResult.provider;- effectiveModel = resolveEffectiveModel(effectiveProvider, model);+ // Re-resolve against the FALLBACK provider using the configured+ // `fallbackModel` createRun surfaced — NOT the caller's `model`, which+ // was resolved against the (now-benched) primary and almost never+ // exists on the fallback. Forwarding it is the leak that sent+ // `codex-configured-default` to LM Studio (mirrors stageRunner.js).+ effectiveModel = resolveEffectiveModel(effectiveProvider, runResult.fallbackModel ?? null);// createRun persisted `metadata.model = effectiveModel || provider.defaultModel`// using the ORIGINAL provider's resolved value — so /runs would// attribute a model that doesn't belong to the fallback (e.g. andiff --git a/server/lib/promptRunner.test.js b/server/lib/promptRunner.test.jsindex 1dac2df067..15a0b4c2ae 100644--- a/server/lib/promptRunner.test.js+++ b/server/lib/promptRunner.test.js@@ -124,6 +124,38 @@ describe('promptRunner — happy paths', () => {expect(runner.executeCliRun).not.toHaveBeenCalled();});+ it('runs the fallbackModel (not the primary model) when createRun proactively swaps to a fallback', async () => {+ // Primary is benched at call time, so the toolkit createRun swaps to an+ // API fallback and surfaces the configured fallbackModel. The run must+ // execute that model on the fallback — NOT the primary's resolved model+ // (the leak that sent `codex-configured-default` to LM Studio). This is+ // the common caller path (no pre-created runId), distinct from the+ // runtime-retry path covered below.+ const fallback = apiProvider({ id: 'fb-api', defaultModel: 'fb-default' });+ runner.createRun.mockResolvedValue({+ runId: 'run-fb',+ provider: fallback,+ fallbackModel: 'pinned-fb',+ });+ let ranModel;+ runner.executeApiRun.mockImplementation(async (id, _p, model, _pr, _cwd, _ctx, onData, onComplete) => {+ ranModel = model;+ onData('ok');+ onComplete({ success: true });+ });++ const out = await runPromptThroughProvider({+ provider: cliProvider({ defaultModel: 'codex-configured-default' }),+ prompt: 'p',+ source: 'test',+ });++ expect(runner.executeApiRun).toHaveBeenCalledTimes(1);+ expect(runner.executeCliRun).not.toHaveBeenCalled();+ expect(ranModel).toBe('pinned-fb');+ expect(out.model).toBe('pinned-fb');+ });+it('forwards the provider id + model + source to createRun', async () => {runner.executeApiRun.mockImplementation(async (id, _p, _m, _pr, _cwd, _ctx, onData, onComplete) => {onData('ok');diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.jsindex 09754ee589..8458e96269 100644--- a/server/services/agentLifecycle.js+++ b/server/services/agentLifecycle.js@@ -187,6 +187,10 @@ export async function spawnAgentForTask(task) {}// Check provider availability (usage limits, rate limits, etc.)+ // Set when we fall back below to a provider with a configured "Fallback+ // Model" pin — it overrides the usual per-task model selection so the+ // user's chosen fallback provider+model pair is honored on agent runs.+ let fallbackModelPin = null;const providerAvailable = isProviderAvailable(provider.id);if (!providerAvailable) {const status = getProviderStatus(provider.id);@@ -202,7 +206,8 @@ export async function spawnAgentForTask(task) {const { providers: providerList = [] } = await getAllProviders();const providersMap = Object.fromEntries(providerList.map((p) => [p.id, p]));const taskFallbackId = task.metadata?.fallbackProvider;- const fallbackResult = await getFallbackProvider(provider.id, providersMap, taskFallbackId);+ const taskFallbackModel = task.metadata?.fallbackModel;+ const fallbackResult = await getFallbackProvider(provider.id, providersMap, taskFallbackId, taskFallbackModel);if (fallbackResult) {emitLog('info', `Using fallback provider: ${fallbackResult.provider.id} (source: ${fallbackResult.source})`, {@@ -212,6 +217,7 @@ export async function spawnAgentForTask(task) {fallbackSource: fallbackResult.source});provider = fallbackResult.provider;+ fallbackModelPin = fallbackResult.model || null;} else {const errorMsg = `Provider ${provider.id} unavailable (${status.message}) and no fallback available`;cleanupOnError(errorMsg);@@ -241,6 +247,12 @@ export async function spawnAgentForTask(task) {const modelSelection = await selectModelForTask(task, provider);let selectedModel = modelSelection.model;+ // A configured "Fallback Model" pin (from the provider- or task-level+ // fallback we took above) wins over the usual selection — the user+ // explicitly chose this model to run on the fallback. The compatibility+ // check below still guards it against the fallback provider's model list.+ if (fallbackModelPin) selectedModel = fallbackModelPin;+// Validate model is compatible with providerif (selectedModel && provider.models && provider.models.length > 0) {const modelIsValid = provider.models.includes(selectedModel);diff --git a/server/services/providerStatus.js b/server/services/providerStatus.jsindex 7afe182796..34240a63f7 100644--- a/server/services/providerStatus.js+++ b/server/services/providerStatus.js@@ -78,15 +78,18 @@ export async function markProviderAvailable(providerId) {/*** Get the best available fallback provider- * Returns null if no fallback is available+ * Returns `{ provider, source, model }` (or null if no fallback is available).+ * `model` is the configured fallback model to run on the chosen provider (the+ * task-level pin, the primary's `fallbackModel`, or null = use the fallback's+ * own default) — never the primary's model.** Priority order:- * 1. Task-level fallback (task.metadata.fallbackProvider)- * 2. Provider-level fallback (provider.fallbackProvider)+ * 1. Task-level fallback (task.metadata.fallbackProvider / fallbackModel)+ * 2. Provider-level fallback (provider.fallbackProvider / fallbackModel)* 3. System default priority list*/-export function getFallbackProvider(primaryProviderId, providers, taskFallbackId = null) {- return providerStatusService.getFallbackProvider(primaryProviderId, providers, taskFallbackId);+export function getFallbackProvider(primaryProviderId, providers, taskFallbackId = null, taskFallbackModelId = null) {+ return providerStatusService.getFallbackProvider(primaryProviderId, providers, taskFallbackId, taskFallbackModelId);}/**