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

fix: finalize shot AI operations once across every terminal path (#13204)

[._.]/ Adam Eivy594e921bc84b
diff --git a/server/services/fableLoom/shots.js b/server/services/fableLoom/shots.js
index 10c6a7846d..a80036e9e5 100644
--- a/server/services/fableLoom/shots.js
+++ b/server/services/fableLoom/shots.js
@@ -82,10 +82,21 @@ export async function applyEpisodeShots(id, episodeId, { sourceFingerprint, grou
}
/** Checkpoints are supplied only by the local run service, never by HTTP callers. */
-export async function runEpisodeShotAutopilot(id, episodeId, {
- providerId, model, effort, operationId, guidance = '', maxRounds = 3, apply = false,
+export async function runEpisodeShotAutopilot(id, episodeId, options = {}) {
+ const status = options.operationId ? startAIOp({ op: 'fableloom-shots', label: 'Planning timed shots', operationId: options.operationId, localOnly: true, silent: true }) : null;
+ return planEpisodeShotAttempt(id, episodeId, options, status).then(result => {
+ status?.complete(options.apply ? 'Timed shots applied' : 'Shot plan ready to preview', { shellReady: false });
+ return result;
+ }).catch(error => {
+ status?.error(error.message);
+ throw error;
+ });
+}
+
+async function planEpisodeShotAttempt(id, episodeId, {
+ providerId, model, effort, guidance = '', maxRounds = 3, apply = false,
checkpoint = {}, saveCheckpoint = async () => {}, isCanceled = () => false,
-} = {}) {
+} = {}, status = null) {
const { loom, episode } = await requireSource(id, episodeId);
const sourceFingerprint = fingerprint(loom, episode);
if (checkpoint.sourceFingerprint && checkpoint.sourceFingerprint !== sourceFingerprint) {
@@ -94,7 +105,6 @@ export async function runEpisodeShotAutopilot(id, episodeId, {
checkpoint.sourceFingerprint = sourceFingerprint;
checkpoint.scenes ||= {};
checkpoint.consistency ||= [];
- const status = operationId ? startAIOp({ op: 'fableloom-shots', label: 'Planning timed shots', operationId, localOnly: true, silent: true }) : null;
const universe = loom.universeId ? await getUniverse(loom.universeId) : null;
const cast = (universe?.characters || []).map(({ id, name, physicalDescription }) => ({ id, name, physicalDescription }));
// The rest of the episode supplies graph context, never another scene's teleplay.
@@ -121,7 +131,6 @@ export async function runEpisodeShotAutopilot(id, episodeId, {
onRunReady: (meta) => status?.update('ready', 'Shot autopilot is running', meta),
}).catch((error) => {
const message = `${stage} round ${round}${sceneId ? ` scene ${sceneId}` : ''} on provider ${selectedProvider.id}: ${error.message}`;
- status?.error(message);
throw new ServerError(message, { status: error.status || 502, code: error.code || 'SHOT_STAGE_FAILED' });
});
};
@@ -203,13 +212,12 @@ export async function runEpisodeShotAutopilot(id, episodeId, {
delete scene.group;
}
await persist();
- return runEpisodeShotAutopilot(id, episodeId, { providerId, model, effort, operationId, guidance, maxRounds, apply, checkpoint, saveCheckpoint, isCanceled });
+ return planEpisodeShotAttempt(id, episodeId, { providerId, model, effort, guidance, maxRounds, apply, checkpoint, saveCheckpoint, isCanceled }, status);
}
throw new ServerError(`Shot consistency review needs revision. Completed steps preserved.\n${feedback}`, { status: 422, code: 'SHOT_PLAN_INVALID' });
}
const result = { sourceFingerprint, groups: compiled.groups, validation: compiled.validation, review, attempt: Math.max(...Object.values(checkpoint.scenes).map((scene) => scene.rounds.length)) };
checkCanceled();
const applied = apply ? await applyEpisodeShots(id, episodeId, result, { isCanceled }) : {};
- status?.complete(apply ? 'Timed shots applied' : 'Shot plan ready to preview', { shellReady: false });
return { ...result, ...applied };
}
diff --git a/server/services/fableLoom/shots.test.js b/server/services/fableLoom/shots.test.js
index 36f7c77b19..dd6921c574 100644
--- a/server/services/fableLoom/shots.test.js
+++ b/server/services/fableLoom/shots.test.js
@@ -8,13 +8,17 @@ vi.mock('../../lib/fileUtils.js', async (original) => {
return { ...actual, PATHS: { ...actual.PATHS, data: TEST_DATA_ROOT } };
});
const runStagedLLM = vi.hoisted(() => vi.fn());
-vi.mock('../stageRunner.js', () => ({ runStagedLLM, resolveStageRoute: async () => ({ provider: { id: 'test-provider', timeout: 300000 } }) }));
+const aiStatus = vi.hoisted(() => ({ update: vi.fn(), complete: vi.fn(), error: vi.fn() }));
+const startAIOp = vi.hoisted(() => vi.fn());
+const resolveStageRoute = vi.hoisted(() => vi.fn());
+vi.mock('../aiStatusEvents.js', () => ({ startAIOp }));
+vi.mock('../stageRunner.js', () => ({ runStagedLLM, resolveStageRoute }));
vi.mock('../universeBuilder.js', () => ({ getUniverse: async () => ({ id: 'example-universe' }) }));
vi.mock('../pipeline/series.js', () => ({ getSeries: async () => null }));
const { createLoom, addEpisode, addNode, updateNode, getLoom } = await import('./records.js');
const { _resetFableLoomBackend } = await import('./store.js');
const { runEpisodeShotAutopilot, applyEpisodeShots } = await import('./shots.js');
-beforeEach(() => { rmSync(join(TEST_DATA_ROOT, 'fableloom'), { recursive: true, force: true }); _resetFableLoomBackend(); runStagedLLM.mockReset(); });
+beforeEach(() => { rmSync(join(TEST_DATA_ROOT, 'fableloom'), { recursive: true, force: true }); _resetFableLoomBackend(); runStagedLLM.mockReset(); startAIOp.mockReset().mockReturnValue(aiStatus); Object.values(aiStatus).forEach(mock => mock.mockClear()); resolveStageRoute.mockReset().mockResolvedValue({ provider: { id: 'test-provider', timeout: 300000 } }); });
afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));
const shot = (title, dialogue = []) => ({ title, durationSeconds: 8, action: 'A courier watches the gate.', framing: 'Medium static shot', dialogue, imagePrompt: 'Courier at the gate', videoPrompt: 'Courier waits.' });
async function setup() {
@@ -226,7 +230,10 @@ it('repairs cross-scene consistency findings within the remaining rounds and kee
}
return { content: { summary: 'Scene checked.', risks: [] } };
});
- await expect(runEpisodeShotAutopilot(loom.id, ep, { checkpoint })).resolves.toMatchObject({ attempt: 2 });
+ await expect(runEpisodeShotAutopilot(loom.id, ep, { checkpoint, operationId: 'example-operation' })).resolves.toMatchObject({ attempt: 2 });
+ expect(startAIOp).toHaveBeenCalledTimes(1);
+ expect(aiStatus.complete).toHaveBeenCalledTimes(1);
+ expect(aiStatus.error).not.toHaveBeenCalled();
expect(checkpoint.consistency).toHaveLength(2);
expect(checkpoint.consistency[0].review.risks).toEqual(['Preserve the lamp placement between scenes.']);
expect(checkpoint.scenes[groups[0].sceneId].rounds[0].feedback).toContain('lamp');
@@ -263,3 +270,17 @@ it('detaches a disconnected compatibility waiter while its durable worker comple
finishCall(await implementation('fableloom-plan-shots', { source: JSON.stringify({ scenes: [{ id: groups[0].sceneId }] }) }));
expect((await completed).result.validation.stats.ready).toBe(true);
});
+
+it('finalizes operation status once for failures before provider execution and round exhaustion', async () => {
+ const { loom, ep, groups } = await setup();
+ resolveStageRoute.mockRejectedValueOnce(new Error('Example route unavailable'));
+ await expect(runEpisodeShotAutopilot(loom.id, ep, { operationId: 'route-operation' })).rejects.toThrow('Example route unavailable');
+ expect(aiStatus.error).toHaveBeenCalledExactlyOnceWith('Example route unavailable');
+ expect(aiStatus.complete).not.toHaveBeenCalled();
+ aiStatus.error.mockClear();
+ groups[0].shots[0].dialogue = [{ speaker: 'Courier', text: 'too many words '.repeat(30) }];
+ mockPlan(groups);
+ await expect(runEpisodeShotAutopilot(loom.id, ep, { operationId: 'invalid-operation', maxRounds: 1 })).rejects.toThrow('exhausted 1 rounds');
+ expect(aiStatus.error).toHaveBeenCalledTimes(1);
+ expect(aiStatus.complete).not.toHaveBeenCalled();
+});