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
fix: detach legacy shot HTTP waiters without canceling durable work (#13204)
[._.]/ Adam Eivy5ee83cef471c
diff --git a/server/routes/fableLoom.js b/server/routes/fableLoom.jsindex beead41f60..b577237d9c 100644--- a/server/routes/fableLoom.js+++ b/server/routes/fableLoom.js@@ -336,7 +336,9 @@ router.post('/:id/episodes/:episodeId/shots/cancel', asyncHandler(async (req, reres.json(await cancelEpisodeShotRun(req.params.id, req.params.episodeId));}));router.post('/:id/episodes/:episodeId/shots/plan', asyncHandler(async (req, res) => {- res.json(await planEpisodeShotRun(req.params.id, req.params.episodeId, validateRequest(shotRunStartSchema, req.body ?? {})));+ const controller = new AbortController();+ res.once('close', () => controller.abort());+ res.json(await planEpisodeShotRun(req.params.id, req.params.episodeId, validateRequest(shotRunStartSchema, req.body ?? {}), { signal: controller.signal }));}));router.post('/:id/episodes/:episodeId/shots/apply', asyncHandler(async (req, res) => {res.json(await applyEpisodeShots(req.params.id, req.params.episodeId, validateRequest(shotApplySchema, req.body)));diff --git a/server/services/fableLoom/shotRuns.js b/server/services/fableLoom/shotRuns.jsindex d27d1deb6f..ab2dd27567 100644--- a/server/services/fableLoom/shotRuns.js+++ b/server/services/fableLoom/shotRuns.js@@ -119,23 +119,30 @@ export async function cancelEpisodeShotRun(loomId, episodeId) {}/** Compatibility callers still await a plan, but do not own the worker's lifetime. */-export async function planEpisodeShotRun(loomId, episodeId, options = {}) {- let runId;- let resolveResult, rejectResult;- const earlyTerminals = new Map();- const result = new Promise((resolve, reject) => { resolveResult = resolve; rejectResult = reject; });- const accept = run => {- if (run.loomId !== loomId || run.episodeId !== episodeId || !['completed', 'failed', 'canceled'].includes(run.status)) return;- if (!runId) { earlyTerminals.set(run.id, run); return; }- if (run.id !== runId) return;- if (run.status === 'completed') resolveResult(run.result);- else rejectResult(new ServerError(run.message, { status: 422, code: 'SHOT_RUN_FAILED' }));- };- // Subscribe before start, including an instant completion or an already-active run.- fableLoomRunEvents.on('shots', accept);- return startEpisodeShotRun(loomId, episodeId, options).then(run => {- runId = run.id;- accept(earlyTerminals.get(runId) || run);- return result;- }).finally(() => fableLoomRunEvents.off('shots', accept));+export async function planEpisodeShotRun(loomId, episodeId, options = {}, { signal } = {}) {+ let accept, onAbort;+ const result = new Promise((resolve, reject) => {+ let runId;+ const earlyTerminals = new Map();+ accept = run => {+ if (run.loomId !== loomId || run.episodeId !== episodeId || !['completed', 'failed', 'canceled'].includes(run.status)) return;+ if (!runId) { earlyTerminals.set(run.id, run); return; }+ if (run.id !== runId) return;+ if (run.status === 'completed') resolve(run.result);+ else reject(new ServerError(run.message, { status: 422, code: 'SHOT_RUN_FAILED' }));+ };+ onAbort = () => reject(new ServerError('The HTTP waiter closed; shot planning continues in the background.', { status: 499, code: 'SHOT_WAITER_CLOSED' }));+ // Subscribe before start, including instant completion or an already-active run.+ fableLoomRunEvents.on('shots', accept);+ signal?.addEventListener('abort', onAbort, { once: true });+ if (signal?.aborted) onAbort();+ void startEpisodeShotRun(loomId, episodeId, options).then(run => {+ runId = run.id;+ accept(earlyTerminals.get(runId) || run);+ }).catch(reject);+ });+ return result.finally(() => {+ fableLoomRunEvents.off('shots', accept);+ signal?.removeEventListener('abort', onAbort);+ });}diff --git a/server/services/fableLoom/shots.test.js b/server/services/fableLoom/shots.test.jsindex 5a30222d02..c82c5a62da 100644--- a/server/services/fableLoom/shots.test.js+++ b/server/services/fableLoom/shots.test.js@@ -236,3 +236,26 @@ it('keeps the synchronous compatibility response backed by a durable backgroundexpect(result.validation.stats.ready).toBe(true);expect((await getEpisodeShotRun(loom.id, ep))).toMatchObject({ status: 'completed', result: { sourceFingerprint: result.sourceFingerprint } });});++it('detaches a disconnected compatibility waiter while its durable worker completes', async () => {+ const { planEpisodeShotRun, getEpisodeShotRun } = await import('./shotRuns.js');+ const { fableLoomRunEvents } = await import('./runEvents.js');+ const { loom, ep, groups } = await setup();+ mockPlan(groups);+ const implementation = runStagedLLM.getMockImplementation();+ let finishCall, startedCall;+ const ready = new Promise(resolve => { startedCall = resolve; });+ runStagedLLM.mockImplementationOnce(() => new Promise(resolve => { finishCall = resolve; startedCall(); }));+ const controller = new AbortController();+ const waiting = planEpisodeShotRun(loom.id, ep, {}, { signal: controller.signal });+ await ready;+ controller.abort();+ await expect(waiting).rejects.toMatchObject({ code: 'SHOT_WAITER_CLOSED' });+ expect((await getEpisodeShotRun(loom.id, ep)).status).toBe('running');+ const completed = new Promise(resolve => {+ const handler = run => { if (run.loomId === loom.id && run.status === 'completed') { fableLoomRunEvents.off('shots', handler); resolve(run); } };+ fableLoomRunEvents.on('shots', handler);+ });+ finishCall(await implementation('fableloom-plan-shots', { source: JSON.stringify({ scenes: [{ id: groups[0].sceneId }] }) }));+ expect((await completed).result.validation.stats.ready).toBe(true);+});