Skip to content

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

address review: stage fallback attribution points at the fresh run, not the failed primary

[._.]/ Adam Eivybcccf0e4d09b
diff --git a/server/lib/promptRunner.js b/server/lib/promptRunner.js
index fb4f64d52e..c5f26742ff 100644
--- a/server/lib/promptRunner.js
+++ b/server/lib/promptRunner.js
@@ -196,13 +196,15 @@ export function assertProvider(provider, { message, code, status = 503 } = {}) {
* must pass this — without it, the CLI/TUI spawn lands in PortOS's own
* cwd and the analysis runs against the wrong files. No-op for API
* providers (no spawn).
- * @returns {Promise<{ text: string, runId: string, model: string|null, usedFallback?: boolean, fallbackFrom?: { id: string, name: string } }>}
+ * @returns {Promise<{ text: string, runId: string, model: string|null, usedFallback?: boolean, fallbackFrom?: { id: string, name: string }, fallbackProvider?: object }>}
* — `model` is the resolved model that actually executed (null when
- * neither override nor provider.defaultModel applies). `usedFallback`
- * and `fallbackFrom` are present only when the primary failed and the
- * retry path recovered via a configured fallback; callers that care
- * about provider attribution should branch on `usedFallback` to know
- * whether `model` belongs to the requested primary or the fallback.
+ * neither override nor provider.defaultModel applies). `runId` always
+ * points to the run record that *actually* ran — on fallback recovery
+ * this is the fresh fallback runId, NOT the failed primary's. When
+ * `usedFallback` is true, `fallbackFrom` identifies the failed primary
+ * and `fallbackProvider` is the full provider object that actually ran
+ * (so callers persisting run attribution can write the correct
+ * providerId without re-picking the fallback themselves).
*/
export async function runPromptThroughProvider(args) {
// Validate inputs up front so an accidentally-null `provider` (or one
@@ -303,6 +305,7 @@ export async function runPromptThroughProvider(args) {
...fallbackResult,
usedFallback: true,
fallbackFrom: { id: failed.id, name: failed.name },
+ fallbackProvider: fallback,
};
}
}
diff --git a/server/lib/promptRunner.test.js b/server/lib/promptRunner.test.js
index fd5344b8c5..60bc293479 100644
--- a/server/lib/promptRunner.test.js
+++ b/server/lib/promptRunner.test.js
@@ -635,6 +635,10 @@ describe('promptRunner — retry-with-fallback', () => {
expect(out.text).toBe('fallback content');
expect(out.usedFallback).toBe(true);
expect(out.fallbackFrom).toEqual({ id: 'primary-cli', name: 'Primary CLI' });
+ // fallbackProvider exposes the full provider object that actually ran
+ // so attribution callers (stageRunner persisting runId for history /
+ // restore) can record providerId without re-picking the fallback.
+ expect(out.fallbackProvider).toMatchObject({ id: 'fallback-api', name: 'Fallback API' });
// Primary was marked unavailable before retry; fallback was looked up
// and used; the deferred autoFixer task was cancelled.
diff --git a/server/lib/stageRunner.js b/server/lib/stageRunner.js
index 8ff83fa659..560d440dc9 100644
--- a/server/lib/stageRunner.js
+++ b/server/lib/stageRunner.js
@@ -279,15 +279,33 @@ export async function runStagedLLM(stageName, variables, options = {}) {
// Stage runs pre-create the run record (so the runId can be logged BEFORE
// the LLM call starts), then thread that id through the shared runner.
- const { text } = await runPromptThroughProvider({
+ // On runtime fallback (primary attempted + failed, fallback retried + won)
+ // `runPromptThroughProvider` ignores our pre-created `runId` and creates
+ // a fresh one for the fallback's record — so the successful output lives
+ // at `result.runId`, NOT the `runId` we passed in. The pre-created record
+ // stays as the failed-primary entry. Capture the post-fallback attribution
+ // (runId / model / providerId) here so the persisted stage result points
+ // at the run that actually produced the text — otherwise pipeline history
+ // / restore links land on a failed record.
+ const runResult2 = await runPromptThroughProvider({
provider: effectiveProvider, model: effectiveModel, prompt, source: options.source || 'staged-llm', runId,
timeout: effectiveTimeout,
});
+ const { text } = runResult2;
+ let finalRunId = runId;
+ let finalProvider = effectiveProvider;
+ let finalModel = effectiveModel;
+ if (runResult2.usedFallback && runResult2.fallbackProvider) {
+ finalRunId = runResult2.runId;
+ finalProvider = runResult2.fallbackProvider;
+ finalModel = runResult2.model ?? finalModel;
+ console.log(`⚡ stage fallback succeeded: ${finalProvider.id} / ${finalModel || '(default)'} / ${stageName} → ${finalRunId.slice(0, 8)}`);
+ }
// Codex CLI dumps the full transcript (banner + metadata + echoed prompt +
// `codex\n<reply>` + token-stats footer). Carve out the assistant reply
// before either parsing JSON or returning text. Idempotent for non-Codex
// providers — returns input unchanged when the banner isn't present.
const cleaned = extractCodexAssistant(text);
const content = options.returnsJson ? extractJson(cleaned, { promptToStrip: prompt }) : cleaned;
- return { content, model: effectiveModel || null, providerId: effectiveProvider.id, runId };
+ return { content, model: finalModel || null, providerId: finalProvider.id, runId: finalRunId };
}