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: register a Code Animation production run before its insert so concurrent starts cannot interrupt it (#11863)

Adam Eivy83530357e447
diff --git a/server/services/codeAnimation/stages.db.test.js b/server/services/codeAnimation/stages.db.test.js
index 71fac2a9be..fad00b6735 100644
--- a/server/services/codeAnimation/stages.db.test.js
+++ b/server/services/codeAnimation/stages.db.test.js
@@ -7,7 +7,7 @@ import { lazyTempDataRoot, makePathsProxy, cleanupTempDataRoots } from '../../li
vi.mock('../../lib/paths.js', async importOriginal =>
makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('code-animation-stages-') }));
// Inject faults into the selected owned staging tree; all other storage stays real.
-const stagingFaults = vi.hoisted(() => ({ write: false, cleanup: false, afterReserve: null, target: '/render/', directory: null, publication: false }));
+const stagingFaults = vi.hoisted(() => ({ write: false, cleanup: false, afterReserve: null, target: '/render/', directory: null, publication: false, beforeStart: null }));
vi.mock('fs/promises', async importOriginal => {
const real = await importOriginal();
return {
@@ -41,6 +41,10 @@ vi.mock('./projectStore.js', async importOriginal => {
const real = await importOriginal();
return {
...real,
+ startStageRun: async (...args) => {
+ await stagingFaults.beforeStart?.();
+ return real.startStageRun(...args);
+ },
commitRepairRevision: async (...args) => {
if (stagingFaults.publication) {
throw Object.assign(new Error('Synthetic repair publication failure'), { code: 'PUBLICATION_FAILED' });
@@ -445,6 +449,48 @@ describe.skipIf(!ready)('Production stage runs', () => {
expect(saved.id).toBe(run.id);
});
+ it('lets exactly one of two concurrent starts run and never marks the winner interrupted', async () => {
+ const { id } = await project();
+ let release;
+ const gate = new Promise(resolve => { release = resolve; });
+ const blocking = vi.fn(async (directory, options) => {
+ if (!options.times.length) return sample(directory, options);
+ await gate;
+ return sample(directory, options);
+ });
+ // Hold each insert until both requests have captured their live-run list, so neither sees the other registered by chance.
+ let entered = 0;
+ let bothEntered;
+ const barrier = new Promise(resolve => { bothEntered = resolve; });
+ stagingFaults.beforeStart = async () => {
+ if (++entered === 2) bothEntered();
+ await barrier;
+ };
+ const results = await Promise.allSettled([
+ startProductionStageRun(id, {}, { sample: blocking, repair: unfreeze, render }),
+ startProductionStageRun(id, {}, { sample: blocking, repair: unfreeze, render }),
+ ]).finally(() => { stagingFaults.beforeStart = null; });
+ const won = results.filter(result => result.status === 'fulfilled');
+ const lost = results.filter(result => result.status === 'rejected');
+ expect(won).toHaveLength(1);
+ expect(lost).toHaveLength(1);
+ expect(lost[0].reason).toMatchObject({ code: 'CODE_ANIMATION_RUN_ACTIVE' });
+ expect(activeStageRunIds()).toEqual([won[0].value.run.id]);
+ const { items } = await getProductionHistory(id, { limit: 10, offset: 0 });
+ expect(items.map(item => item.status)).not.toContain('interrupted');
+ release();
+ await won[0].value.done;
+ });
+
+ it('leaves no live run registered when the start insert fails', async () => {
+ const { id } = await project();
+ stagingFaults.beforeStart = async () => { throw new Error('Synthetic insert failure'); };
+ await expect(startProductionStageRun(id, {}, { sample, repair: unfreeze, render }))
+ .rejects.toThrow('Synthetic insert failure')
+ .finally(() => { stagingFaults.beforeStart = null; });
+ expect(activeStageRunIds()).toEqual([]);
+ });
+
it('keeps one live run per project, cancels owned work, retains accepted artifacts, and resumes without re-measuring the style frame', async () => {
const { id } = await project();
let pilotStarted;
diff --git a/server/services/codeAnimation/stages.js b/server/services/codeAnimation/stages.js
index 6d0688a4ee..3d2fd6efa2 100644
--- a/server/services/codeAnimation/stages.js
+++ b/server/services/codeAnimation/stages.js
@@ -530,8 +530,14 @@ export async function startProductionStageRun(projectId, input, deps = {}) {
runId, projectId, project, state, deps: { ...defaultDeps, ...deps }, controller: new AbortController(),
startedAt: Date.now(), prior: prior?.data.stages ?? [], resumedFrom: prior?.id ?? null,
};
- await store.startStageRun(runId, projectId, snapshot(ctx), activeStageRunIds());
+ // Registered before the insert so a concurrent start sees this run as live and cannot mark its fresh row interrupted.
activeRuns.set(runId, { controller: ctx.controller, projectId });
+ try {
+ await store.startStageRun(runId, projectId, snapshot(ctx), activeStageRunIds());
+ } catch (error) {
+ activeRuns.delete(runId);
+ throw error;
+ }
emitCodeAnimationChanged(projectId);
console.log(`🎞️ Code Animation production run ${runId.slice(0, 8)} started on revision ${revision.id.slice(0, 8)}`);
// Process boundary: nothing may reject unhandled out of the background run.