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 (codex): preflight screenshot existence + catch proactive non-API fallback

[._.]/ Adam Eivybd69bb202503
diff --git a/server/lib/promptRunner.js b/server/lib/promptRunner.js
index e50d9e5059..59fbd1647f 100644
--- a/server/lib/promptRunner.js
+++ b/server/lib/promptRunner.js
@@ -234,9 +234,12 @@ 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 }, fallbackProvider?: object }>}
+ * @returns {Promise<{ text: string, runId: string, model: string|null, provider: object, 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). `runId` always
+ * neither override nor provider.defaultModel applies). `provider` is the
+ * provider object that actually ran, reflecting createRun's proactive swap
+ * (read this to detect a proactive fallback; `usedFallback`/`fallbackProvider`
+ * only cover the retry-fallback path). `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
@@ -600,7 +603,14 @@ async function executeProviderRunOnce({ provider, prompt, source, model, runId:
const finalText = effectiveProvider.type === PROVIDER_TYPES.TUI
? (typeof result?.text === 'string' ? result.text : '')
: text;
- safeResolve({ text: finalText, runId, model: effectiveModel });
+ // Report the provider that ACTUALLY ran. `effectiveProvider` reflects
+ // createRun's proactive swap (when the requested provider was already
+ // benched) — distinct from the retry-fallback path, which the outer
+ // runPromptThroughProvider annotates separately. Callers that care
+ // about the effective provider's type (e.g. vision, which only works
+ // on API providers) must read this, since a proactive swap leaves
+ // `usedFallback`/`fallbackProvider` unset.
+ safeResolve({ text: finalText, runId, model: effectiveModel, provider: effectiveProvider });
}
};
diff --git a/server/lib/promptRunner.test.js b/server/lib/promptRunner.test.js
index cf60f4cc1c..fa46d63cf6 100644
--- a/server/lib/promptRunner.test.js
+++ b/server/lib/promptRunner.test.js
@@ -102,7 +102,9 @@ describe('promptRunner — happy paths', () => {
source: 'test',
});
- expect(out).toEqual({ text: 'hello world', runId: 'run-xyz', model: 'm-default' });
+ // `provider` is the effective provider that actually ran (reflects a
+ // proactive createRun swap); here no swap, so it echoes the input.
+ expect(out).toEqual({ text: 'hello world', runId: 'run-xyz', model: 'm-default', provider: cliProvider() });
expect(runner.executeCliRun).toHaveBeenCalledTimes(1);
expect(runner.executeApiRun).not.toHaveBeenCalled();
});
@@ -121,7 +123,7 @@ describe('promptRunner — happy paths', () => {
model: 'gpt-test',
});
- expect(out).toEqual({ text: 'foobar', runId: 'run-xyz', model: 'gpt-test' });
+ expect(out).toEqual({ text: 'foobar', runId: 'run-xyz', model: 'gpt-test', provider: apiProvider() });
expect(runner.executeApiRun).toHaveBeenCalledTimes(1);
expect(runner.executeCliRun).not.toHaveBeenCalled();
});
@@ -182,6 +184,10 @@ describe('promptRunner — happy paths', () => {
expect(runner.executeCliRun).not.toHaveBeenCalled();
expect(ranModel).toBe('pinned-fb');
expect(out.model).toBe('pinned-fb');
+ // The resolved result reports the proactively-swapped provider (not the
+ // requested one), so callers can detect a swap even though usedFallback
+ // stays unset on this path.
+ expect(out.provider).toEqual(fallback);
});
it('forwards the provider id + model + source to createRun', async () => {
@@ -512,7 +518,7 @@ describe('promptRunner — TUI provider routing', () => {
source: 'pipeline-text-stage',
});
- expect(out).toEqual({ text: 'once upon a time', runId: 'run-xyz', model: 'm-default' });
+ expect(out).toEqual({ text: 'once upon a time', runId: 'run-xyz', model: 'm-default', provider: tuiProvider() });
expect(tuiRunner.executeTuiRun).toHaveBeenCalledTimes(1);
expect(runner.executeCliRun).not.toHaveBeenCalled();
expect(runner.executeApiRun).not.toHaveBeenCalled();
diff --git a/server/routes/universeBuilder.js b/server/routes/universeBuilder.js
index a3007f7437..70eb573e02 100644
--- a/server/routes/universeBuilder.js
+++ b/server/routes/universeBuilder.js
@@ -28,7 +28,9 @@ import { BIBLE_KINDS, BIBLE_LIMITS, pruneStaleReferenceSheets } from '../lib/sto
import { getUniverseCanonUsage, listLinkedSeriesNames } from '../services/canonUsage.js';
import { expandWorldTemplate, generateCategoryVariations } from '../services/universeBuilderExpand.js';
import { describeEntityFromImages, VISION_KINDS, VISION_MAX_IMAGES } from '../services/universeVisionDescribe.js';
-import { sanitizeFilename } from '../lib/fileUtils.js';
+import { sanitizeFilename, PATHS } from '../lib/fileUtils.js';
+import { existsSync } from 'fs';
+import { join } from 'path';
import { refineWorldPrompts } from '../services/universeBuilderRefine.js';
import { promoteVariationToCanon, VALID_TARGET_KINDS } from '../services/universeBuilderPromote.js';
import { autoSortOtherBuckets } from '../services/universeBuilderAutoSort.js';
@@ -424,6 +426,14 @@ router.post('/describe-from-images', asyncHandler(async (req, res) => {
if (safe !== f) {
throw new ServerError(`Invalid screenshot filename: ${f}`, { status: 400, code: 'VALIDATION_ERROR' });
}
+ // Preflight existence: the runner's loadImageAsBase64 silently DROPS a
+ // missing image and still sends the text prompt, so a stale/never-uploaded
+ // filename would otherwise let the model describe with fewer references —
+ // or, if all are missing, hallucinate from the prompt alone. Fail loudly
+ // here instead. data/screenshots is where POST /api/screenshots writes.
+ if (!existsSync(join(PATHS.screenshots, safe))) {
+ throw new ServerError(`Screenshot not found: ${safe} — re-upload the image and retry.`, { status: 400, code: 'SCREENSHOT_NOT_FOUND' });
+ }
return safe;
});
const result = await describeEntityFromImages({ ...body, screenshots });
diff --git a/server/services/universeVisionDescribe.js b/server/services/universeVisionDescribe.js
index 9aaee3b024..800be471fe 100644
--- a/server/services/universeVisionDescribe.js
+++ b/server/services/universeVisionDescribe.js
@@ -123,13 +123,16 @@ export async function describeEntityFromImages({ kind, name, context, screenshot
screenshots: images,
});
- // runPromptThroughProvider's fallback path can swap a failed API provider for
- // a CLI/TUI one, which has no vision path and silently drops the images — so
- // a fallback completion would be prose hallucinated from the text prompt
- // alone, with no signal the references were ignored. Reject that outright
- // (the whole point of resolving an API provider up front was to never
- // describe images from nothing).
- if (result.usedFallback && result.fallbackProvider && result.fallbackProvider.type !== 'api') {
+ // The runner can swap providers two ways: a proactive swap inside createRun
+ // (when the chosen API provider was already benched — leaves
+ // usedFallback/fallbackProvider UNSET) and a retry fallback after a failure
+ // (sets them). Both surface the provider that actually ran as `result.provider`.
+ // A CLI/TUI provider has no vision path and silently drops the images, so the
+ // completion would be prose hallucinated from the text prompt alone — reject
+ // it outright (resolving an API provider up front was meant to guarantee the
+ // model actually sees the references).
+ const ranProvider = result.provider || result.fallbackProvider || provider;
+ if (ranProvider.type && ranProvider.type !== 'api') {
throw new ServerError(
'The vision request fell back to a non-vision provider that cannot read images. Configure a reliable vision-capable API provider and retry.',
{ status: 502, code: 'VISION_FALLBACK_DROPPED_IMAGES' },
@@ -146,10 +149,11 @@ export async function describeEntityFromImages({ kind, name, context, screenshot
return {
description,
- // Report the provider/model that ACTUALLY ran (a fallback may have swapped
- // it), so the UI's picker can reflect reality instead of the request.
+ // Report the provider/model that ACTUALLY ran (a proactive or retry swap
+ // may have changed it), so the UI's picker can reflect reality instead of
+ // the request.
llm: {
- provider: result.fallbackProvider?.id || provider.id,
+ provider: ranProvider.id || provider.id,
model: result.model || null,
},
};
diff --git a/server/services/universeVisionDescribe.test.js b/server/services/universeVisionDescribe.test.js
index 6907527048..980d4e3cd7 100644
--- a/server/services/universeVisionDescribe.test.js
+++ b/server/services/universeVisionDescribe.test.js
@@ -65,7 +65,15 @@ describe('describeEntityFromImages', () => {
expect(out.llm).toEqual({ provider: 'openai', model: 'gpt-vision' });
});
- it('rejects (VISION_FALLBACK_DROPPED_IMAGES) when the runner fell back to a non-API provider that drops images', async () => {
+ it('reports the effective provider on a proactive swap to another API provider', async () => {
+ promptRunner.runPromptThroughProvider.mockResolvedValue({
+ text: 'x', model: 'gpt-vision', provider: { id: 'openai', type: 'api' },
+ });
+ const out = await describeEntityFromImages({ kind: 'place', screenshots: ['a.png'] });
+ expect(out.llm).toEqual({ provider: 'openai', model: 'gpt-vision' });
+ });
+
+ it('rejects (VISION_FALLBACK_DROPPED_IMAGES) on a retry fallback to a non-API provider', async () => {
promptRunner.runPromptThroughProvider.mockResolvedValue({
text: 'hallucinated from text alone', model: 'sonnet', usedFallback: true, fallbackProvider: { id: 'claude-code', type: 'cli' },
});
@@ -73,6 +81,14 @@ describe('describeEntityFromImages', () => {
.rejects.toMatchObject({ code: 'VISION_FALLBACK_DROPPED_IMAGES', status: 502 });
});
+ it('rejects (VISION_FALLBACK_DROPPED_IMAGES) on a proactive swap to a non-API provider (no usedFallback set)', async () => {
+ promptRunner.runPromptThroughProvider.mockResolvedValue({
+ text: 'hallucinated from text alone', model: 'sonnet', provider: { id: 'claude-code', type: 'cli' },
+ });
+ await expect(describeEntityFromImages({ kind: 'character', screenshots: ['a.png'] }))
+ .rejects.toMatchObject({ code: 'VISION_FALLBACK_DROPPED_IMAGES', status: 502 });
+ });
+
it('forwards optional context into the vision prompt', async () => {
await describeEntityFromImages({ kind: 'object', context: 'a relic blade', screenshots: ['a.png'] });
const { prompt } = promptRunner.runPromptThroughProvider.mock.calls[0][0];