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

Preserve live production runs across concurrent admission and history reads

[._.]/ Adam Eivyfd315efa0a92
diff --git a/server/services/codeAnimation/projectStore.js b/server/services/codeAnimation/projectStore.js
index 17e366f83a..8a6b9a1785 100644
--- a/server/services/codeAnimation/projectStore.js
+++ b/server/services/codeAnimation/projectStore.js
@@ -118,17 +118,18 @@ const RUN_ACTIVE = 'running';
/**
* Insert a running stage run. Rows left `running` by a previous process (not in
- * `activeIds`) are marked interrupted first, so a restart never blocks the
- * project and never silently resumes. Another live run is a conflict.
+ * `getActiveIds()`) are marked interrupted first, so a restart never blocks the
+ * project and never silently resumes. Another live run is a conflict. Resolve
+ * live IDs under the project lock; a queued snapshot can omit a newer run.
*/
-export async function startStageRun(id, projectId, data, activeIds) {
+export async function startStageRun(id, projectId, data, getActiveIds) {
return withTransaction(async client => {
const project = await client.query('SELECT data FROM code_animation_projects WHERE id = $1 FOR UPDATE', [projectId]);
if (!project.rows[0]) throw missing();
await client.query(`UPDATE code_animation_project_runs SET status = 'interrupted',
data = data || '{"resumable":true}'::jsonb, completed_at = NOW()
WHERE project_id = $1 AND status = $2 AND data->>'kind' = 'production-stages' AND NOT (id = ANY($3::text[]))`,
- [projectId, RUN_ACTIVE, activeIds]);
+ [projectId, RUN_ACTIVE, getActiveIds()]);
const live = await client.query(`SELECT id FROM code_animation_project_runs
WHERE project_id = $1 AND status = $2 AND data->>'kind' = 'production-stages' LIMIT 1`, [projectId, RUN_ACTIVE]);
if (live.rows[0]) {
@@ -178,10 +179,13 @@ export async function commitRepairRevision(projectId, revision) {
});
}
-export async function interruptStageRuns(projectId, activeIds) {
- await query(`UPDATE code_animation_project_runs SET status = 'interrupted',
- data = data || '{"resumable":true}'::jsonb, completed_at = NOW()
- WHERE project_id = $1 AND status = 'running' AND data->>'kind' = 'production-stages' AND NOT (id = ANY($2::text[]))`, [projectId, activeIds]);
+export async function interruptStageRuns(projectId, getActiveIds) {
+ await withTransaction(async client => {
+ await client.query('SELECT id FROM code_animation_projects WHERE id = $1 FOR UPDATE', [projectId]);
+ await client.query(`UPDATE code_animation_project_runs SET status = 'interrupted',
+ data = data || '{"resumable":true}'::jsonb, completed_at = NOW()
+ WHERE project_id = $1 AND status = 'running' AND data->>'kind' = 'production-stages' AND NOT (id = ANY($2::text[]))`, [projectId, getActiveIds()]);
+ });
}
// ---- Accepted output (#9392): a frozen record beside, never inside, the run history ----
diff --git a/server/services/codeAnimation/projects.js b/server/services/codeAnimation/projects.js
index 0418d91074..80099860c8 100644
--- a/server/services/codeAnimation/projects.js
+++ b/server/services/codeAnimation/projects.js
@@ -34,7 +34,7 @@ export const listProductionProjects = page => store.pageProjectRecords(page);
export async function getProductionHistory(id, page) {
await getProductionProject(id);
await store.interruptImports(id, [...activeImports]);
- await store.interruptStageRuns(id, activeStageRunIds());
+ await store.interruptStageRuns(id, activeStageRunIds);
return store.pageProjectHistory(id, page);
}
diff --git a/server/services/codeAnimation/stages.db.test.js b/server/services/codeAnimation/stages.db.test.js
index fad00b6735..af79749226 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, beforeStart: null }));
+const stagingFaults = vi.hoisted(() => ({ write: false, cleanup: false, afterReserve: null, target: '/render/', directory: null, publication: false, beforeStart: null, beforeHistory: null }));
vi.mock('fs/promises', async importOriginal => {
const real = await importOriginal();
return {
@@ -42,8 +42,16 @@ vi.mock('./projectStore.js', async importOriginal => {
return {
...real,
startStageRun: async (...args) => {
- await stagingFaults.beforeStart?.();
- return real.startStageRun(...args);
+ const afterStart = await stagingFaults.beforeStart?.();
+ try {
+ return await real.startStageRun(...args);
+ } finally {
+ afterStart?.();
+ }
+ },
+ interruptStageRuns: async (...args) => {
+ await stagingFaults.beforeHistory?.();
+ return real.interruptStageRuns(...args);
},
commitRepairRevision: async (...args) => {
if (stagingFaults.publication) {
@@ -86,7 +94,7 @@ if (ready) await ensureSchema();
const ids = [];
afterEach(() => {
vi.useRealTimers(); stopRun.mockReset(); runPromptThroughProvider.mockReset();
- Object.assign(stagingFaults, { write: false, cleanup: false, afterReserve: null, target: '/render/', directory: null, publication: false });
+ Object.assign(stagingFaults, { write: false, cleanup: false, afterReserve: null, target: '/render/', directory: null, publication: false, beforeStart: null, beforeHistory: null });
});
afterAll(async () => {
if (ready && ids.length) await query('DELETE FROM code_animation_projects WHERE id = ANY($1::text[])', [ids]);
@@ -458,13 +466,16 @@ describe.skipIf(!ready)('Production stage runs', () => {
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.
+ // Force the later registration to commit first: the earlier caller must
+ // not use its pre-lock active-ID snapshot to interrupt that live winner.
let entered = 0;
- let bothEntered;
- const barrier = new Promise(resolve => { bothEntered = resolve; });
+ const laterInserted = Promise.withResolvers();
stagingFaults.beforeStart = async () => {
- if (++entered === 2) bothEntered();
- await barrier;
+ if (++entered === 1) {
+ await laterInserted.promise;
+ return;
+ }
+ return laterInserted.resolve;
};
const results = await Promise.allSettled([
startProductionStageRun(id, {}, { sample: blocking, repair: unfreeze, render }),
@@ -472,14 +483,48 @@ describe.skipIf(!ready)('Production stage runs', () => {
]).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;
+ try {
+ 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');
+ } finally {
+ release();
+ await Promise.all(won.map(result => result.value.done));
+ }
+ });
+
+ it('does not interrupt a newly admitted run when an older history request resumes', async () => {
+ const { id } = await project();
+ const historyEntered = Promise.withResolvers();
+ const historyGate = Promise.withResolvers();
+ const executionGate = Promise.withResolvers();
+ stagingFaults.beforeHistory = async () => {
+ historyEntered.resolve();
+ await historyGate.promise;
+ };
+ const history = getProductionHistory(id, { limit: 10, offset: 0 });
+ await historyEntered.promise;
+ let first;
+ try {
+ first = await startProductionStageRun(id, {}, {
+ sample: async (directory, options) => {
+ if (options.times.length) await executionGate.promise;
+ return sample(directory, options);
+ },
+ repair: unfreeze, render,
+ });
+ historyGate.resolve();
+ expect((await history).items.find(item => item.id === first.run.id)).toMatchObject({ status: 'running' });
+ expect(activeStageRunIds()).toEqual([first.run.id]);
+ } finally {
+ stagingFaults.beforeHistory = null;
+ historyGate.resolve();
+ executionGate.resolve();
+ await Promise.allSettled([history, first?.done]);
+ }
});
it('leaves no live run registered when the start insert fails', async () => {
diff --git a/server/services/codeAnimation/stages.js b/server/services/codeAnimation/stages.js
index 3d2fd6efa2..fd88b9a32f 100644
--- a/server/services/codeAnimation/stages.js
+++ b/server/services/codeAnimation/stages.js
@@ -533,7 +533,7 @@ export async function startProductionStageRun(projectId, input, deps = {}) {
// 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());
+ await store.startStageRun(runId, projectId, snapshot(ctx), activeStageRunIds);
} catch (error) {
activeRuns.delete(runId);
throw error;