Skip to content

Show it a video you love. Get a new video in the same style. An AI crew (Claude Code or Codex) plans, builds and reviews it with you.

fix(agents): re-detect the installed CLIs instead of caching them forever

Reiz190037e7faf149e6
diff --git a/app/server/agents-cache.test.ts b/app/server/agents-cache.test.ts
new file mode 100644
index 0000000..8823164
--- /dev/null
+++ b/app/server/agents-cache.test.ts
@@ -0,0 +1,40 @@
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+import { createAgentCache } from './agents-cache.ts';
+import type { AgentStatus } from '../shared/types.ts';
+
+function clock(start = 1_000) {
+ let t = start;
+ return { now: () => t, advance: (ms: number) => { t += ms; } };
+}
+
+test('reuses the detection inside the ttl', () => {
+ const c = clock();
+ let calls = 0;
+ const detect = (): AgentStatus => { calls += 1; return { claude: `v${calls}`, codex: null }; };
+ const status = createAgentCache(detect, 1000, c.now);
+ assert.equal(status().claude, 'v1');
+ c.advance(999);
+ assert.equal(status().claude, 'v1');
+ assert.equal(calls, 1);
+});
+
+test('re-detects once the ttl has passed, so a new CLI shows up without a restart', () => {
+ const c = clock();
+ let calls = 0;
+ const detect = (): AgentStatus => { calls += 1; return { claude: null, codex: calls > 1 ? 'codex 1.0' : null }; };
+ const status = createAgentCache(detect, 1000, c.now);
+ assert.equal(status().codex, null);
+ c.advance(1000);
+ assert.equal(status().codex, 'codex 1.0');
+ assert.equal(calls, 2);
+});
+
+test('a ttl of zero disables the cache', () => {
+ let calls = 0;
+ const detect = (): AgentStatus => { calls += 1; return { claude: null, codex: null }; };
+ const status = createAgentCache(detect, 0, () => 0);
+ status();
+ status();
+ assert.equal(calls, 2);
+});
diff --git a/app/server/agents-cache.ts b/app/server/agents-cache.ts
new file mode 100644
index 0000000..bae6cf7
--- /dev/null
+++ b/app/server/agents-cache.ts
@@ -0,0 +1,27 @@
+// Cache for the installed-agent probe.
+//
+// agentStatus() spawns each CLI with --version, which is far too expensive to run on every
+// request and cheap enough to re-run occasionally. It used to be cached for the lifetime of the
+// process, so a Claude Code or Codex installed while the server was running never appeared in the
+// picker until the next restart.
+//
+// AGENTS_TTL_MS overrides the window; 0 disables the cache (useful when testing the picker).
+import { agentStatus } from './agents/index.ts';
+import type { AgentStatus } from '../shared/types.ts';
+
+const DEFAULT_TTL_MS = 5 * 60 * 1000;
+
+export function createAgentCache(
+ detect: () => AgentStatus = agentStatus,
+ ttlMs: number = Number(process.env.AGENTS_TTL_MS ?? DEFAULT_TTL_MS),
+ now: () => number = Date.now,
+) {
+ let cached: AgentStatus | null = null;
+ let at = 0;
+ return function status(): AgentStatus {
+ if (cached && ttlMs > 0 && now() - at < ttlMs) return cached;
+ cached = detect();
+ at = now();
+ return cached;
+ };
+}
diff --git a/app/server/index.ts b/app/server/index.ts
index 2629df9..40e49ea 100644
--- a/app/server/index.ts
+++ b/app/server/index.ts
@@ -5,8 +5,7 @@ import express, { type Request, type Response } from 'express';
import multer from 'multer';
import { existsSync, mkdirSync, renameSync, unlinkSync } from 'node:fs';
import { join, extname, resolve, sep } from 'node:path';
-import type { AgentStatus } from '../shared/types.ts';
-import { agentStatus } from './agents/index.ts';
+import { createAgentCache } from './agents-cache.ts';
import * as J from './jobs.ts';
// Ctrl+C / kill: stop the agents first (the next start marks their turns interrupted, and Retry resumes them)
for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { J.stopAll(); setTimeout(() => process.exit(0), 800); });
@@ -27,8 +26,8 @@ const guard = (res: Response, id: string) => { if (!/^[\w-]+$/.test(id) || !exis
// Answers as soon as the server is listening: start.bat / start.sh poll it before opening the browser
app.get('/api/health', (req, res) => res.json({ ok: true }));
-let AGENTS: AgentStatus | null = null;
-app.get('/api/agents', (req, res) => { AGENTS = AGENTS || agentStatus(); res.json(AGENTS); });
+const agents = createAgentCache();
+app.get('/api/agents', (req, res) => res.json(agents()));
app.get('/api/projects', (req, res) => res.json(J.listJobs()));
// New project: a reference (file upload or URL) + brief + agent, optional music / assets / lyrics files.