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([render-slowness-on-long-sessions-per-scene-render]): cap Creative Director runs[] at 200 to stop O(N²) per-scene degradation

[._.]/ Adam Eivy289a02cc2daf
diff --git a/server/services/creativeDirector/local.js b/server/services/creativeDirector/local.js
index a41ad58af1..fc45cdb301 100644
--- a/server/services/creativeDirector/local.js
+++ b/server/services/creativeDirector/local.js
@@ -21,6 +21,27 @@ import { createCollection } from '../mediaCollections.js';
const PROJECTS_FILE = join(PATHS.data, 'creative-director-projects.json');
+// Without a cap, runs[] grows unbounded and every loadAll/saveAll (≈10 per
+// scene render) parses + serializes a file whose size scales with cumulative
+// renders — turning per-scene orchestration into O(N²) wall-clock. In-flight
+// runs are load-bearing for orphan/dedup detection in completionHook and the
+// boot recovery scan, so trim only preserves the most-recent terminal entries.
+const MAX_PERSISTED_RUNS = 200;
+const TERMINAL_RUN_STATUSES = new Set(['completed', 'failed']);
+
+export function trimRuns(runs) {
+ if (!Array.isArray(runs) || runs.length <= MAX_PERSISTED_RUNS) return runs || [];
+ const inflight = [];
+ const terminal = [];
+ for (const r of runs) {
+ if (r && TERMINAL_RUN_STATUSES.has(r.status)) terminal.push(r);
+ else inflight.push(r);
+ }
+ const keepTerminal = Math.max(0, MAX_PERSISTED_RUNS - inflight.length);
+ const trimmedTerminal = keepTerminal === 0 ? [] : terminal.slice(-keepTerminal);
+ return [...inflight, ...trimmedTerminal];
+}
+
async function loadAll() {
const raw = await readJSONFile(PROJECTS_FILE, []);
return Array.isArray(raw) ? raw : [];
@@ -160,7 +181,7 @@ export async function recordRun(id, runEntry) {
if (idx < 0) throw new ServerError('Project not found', { status: 404, code: 'NOT_FOUND' });
const project = all[idx];
const run = { startedAt: new Date().toISOString(), ...runEntry, runId: runEntry.runId || randomUUID() };
- project.runs = [...(project.runs || []), run];
+ project.runs = trimRuns([...(project.runs || []), run]);
project.updatedAt = new Date().toISOString();
all[idx] = project;
await saveAll(all);
diff --git a/server/services/creativeDirector/local.test.js b/server/services/creativeDirector/local.test.js
index 0ed733954d..4230c4d673 100644
--- a/server/services/creativeDirector/local.test.js
+++ b/server/services/creativeDirector/local.test.js
@@ -17,7 +17,7 @@ vi.mock('../mediaCollections.js', () => ({
createCollection: vi.fn(async () => ({ id: 'col-1' })),
}));
-const { setTreatment } = await import('./local.js');
+const { setTreatment, recordRun, trimRuns } = await import('./local.js');
const VALID_TREATMENT = {
logline: 'A cat finds a hat.',
@@ -70,3 +70,64 @@ describe('setTreatment — status preservation', () => {
expect(result.status).toBe('rendering');
});
});
+
+describe('trimRuns — bound runs[] growth', () => {
+ it('passes through arrays under the cap unchanged', () => {
+ const runs = Array.from({ length: 50 }, (_, i) => ({ runId: `r-${i}`, status: 'completed' }));
+ expect(trimRuns(runs)).toBe(runs);
+ });
+
+ it('keeps the most recent terminal runs when over the cap', () => {
+ const runs = Array.from({ length: 250 }, (_, i) => ({ runId: `r-${i}`, status: 'completed' }));
+ const trimmed = trimRuns(runs);
+ expect(trimmed).toHaveLength(200);
+ expect(trimmed[0].runId).toBe('r-50');
+ expect(trimmed[199].runId).toBe('r-249');
+ });
+
+ it('preserves every in-flight run even when total exceeds the cap', () => {
+ const terminal = Array.from({ length: 300 }, (_, i) => ({ runId: `done-${i}`, status: 'completed' }));
+ const inflight = [
+ { runId: 'live-1', status: 'running', kind: 'evaluate', sceneId: 'scene-1' },
+ { runId: 'live-2', status: 'queued', kind: 'treatment' },
+ ];
+ const trimmed = trimRuns([...terminal, ...inflight]);
+ expect(trimmed).toHaveLength(200);
+ expect(trimmed.filter((r) => r.runId.startsWith('live-'))).toHaveLength(2);
+ expect(trimmed.filter((r) => r.status === 'completed')).toHaveLength(198);
+ });
+
+ it('treats unknown non-terminal statuses as in-flight (orphan/wedge detection load-bearing)', () => {
+ const runs = [
+ { runId: 'mystery', status: 'evaluating' },
+ ...Array.from({ length: 300 }, (_, i) => ({ runId: `done-${i}`, status: 'failed' })),
+ ];
+ const trimmed = trimRuns(runs);
+ expect(trimmed.find((r) => r.runId === 'mystery')).toBeTruthy();
+ });
+
+ it('tolerates non-array / nullish input', () => {
+ expect(trimRuns(null)).toEqual([]);
+ expect(trimRuns(undefined)).toEqual([]);
+ });
+});
+
+describe('recordRun — applies trim on append', () => {
+ it('caps persisted runs[] when recordRun would otherwise grow it past the cap', async () => {
+ const existing = Array.from({ length: 200 }, (_, i) => ({ runId: `r-${i}`, status: 'completed', startedAt: new Date(2026, 0, 1, 0, i).toISOString() }));
+ mockReadJSONFile.mockResolvedValue([{ id: 'cd-1', status: 'rendering', name: 'Test', runs: existing }]);
+ await recordRun('cd-1', { agentId: 'agent-x', kind: 'evaluate', sceneId: 'scene-1', status: 'running' });
+ const saved = mockAtomicWrite.mock.calls[0][1];
+ expect(saved[0].runs).toHaveLength(200);
+ // The new in-flight run must survive trim, even though it pushed total past the cap.
+ expect(saved[0].runs.find((r) => r.kind === 'evaluate' && r.sceneId === 'scene-1' && r.status === 'running')).toBeTruthy();
+ });
+
+ it('does not cap when total is still under the limit', async () => {
+ const existing = Array.from({ length: 10 }, (_, i) => ({ runId: `r-${i}`, status: 'completed' }));
+ mockReadJSONFile.mockResolvedValue([{ id: 'cd-1', status: 'rendering', name: 'Test', runs: existing }]);
+ await recordRun('cd-1', { agentId: 'agent-x', kind: 'evaluate', status: 'running' });
+ const saved = mockAtomicWrite.mock.calls[0][1];
+ expect(saved[0].runs).toHaveLength(11);
+ });
+});