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.

Stopping the server stops its agents too (no orphaned Claude Code/Codex after a restart)

許元豪7ddfa5a4d1a7
diff --git a/app/restart.sh b/app/restart.sh
index 83c4d30..4b69d7c 100644
--- a/app/restart.sh
+++ b/app/restart.sh
@@ -7,10 +7,10 @@ if [ "$1" != "--force" ]; then
BUSY=$(curl -s -m 2 localhost:${PORT:-4318}/api/projects | node -e "let s='';process.stdin.on('data',d=>s+=d).on('end',()=>{try{const W=['analyzing','styling','planning','replanning','producing','revising','critiquing'];console.log(JSON.parse(s).filter(p=>W.includes(p.stage)).map(p=>p.id+'('+p.stage+')').join(' '))}catch{}})")
if [ -n "$BUSY" ]; then echo "not restarting — still working: $BUSY (use --force to restart anyway)"; exit 2; fi
fi
-# stop only the server on this port (other checkouts / ports keep running)
+# stop only the server on this port (other checkouts / ports keep running), together with the agents it started
P=${PORT:-4318}
if command -v powershell >/dev/null 2>&1; then
- powershell -NoProfile -Command "Get-NetTCPConnection -LocalPort $P -State Listen -ErrorAction SilentlyContinue | % { Stop-Process -Id \$_.OwningProcess -Force }" >/dev/null 2>&1 || true
+ powershell -NoProfile -Command "Get-NetTCPConnection -LocalPort $P -State Listen -ErrorAction SilentlyContinue | % { taskkill /PID \$_.OwningProcess /T /F }" >/dev/null 2>&1 || true
elif command -v lsof >/dev/null 2>&1; then
lsof -ti tcp:$P -sTCP:LISTEN | xargs kill 2>/dev/null || true
else
diff --git a/app/server/index.mjs b/app/server/index.mjs
index 7e7c57d..cbc4d82 100644
--- a/app/server/index.mjs
+++ b/app/server/index.mjs
@@ -7,6 +7,8 @@ import { existsSync, mkdirSync, renameSync } from 'node:fs';
import { join, extname, resolve, sep } from 'node:path';
import { agentStatus } from './agents/index.mjs';
import * as J from './jobs.mjs';
+// 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); });
const PORT = +(process.env.PORT || 4318);
const app = express();
diff --git a/app/server/jobs.mjs b/app/server/jobs.mjs
index 2b29c68..a2f7eba 100644
--- a/app/server/jobs.mjs
+++ b/app/server/jobs.mjs
@@ -40,6 +40,8 @@ export const CONFIG = {
export const bus = new EventEmitter(); bus.setMaxListeners(100);
const running = new Map(); // id → Set<AbortController>
+// Stop every agent this server started (on shutdown), so none keeps working unseen after a restart.
+export function stopAll() { for (const set of running.values()) for (const ac of set) ac.abort(); }
const now = () => new Date().toISOString();
export const dirOf = (id) => join(PROJECTS, id);