agent-theatre 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1315,7 +1315,26 @@ var Theatre = class {
1315
1315
  this.port = typeof addr === "object" && addr ? addr.port : port;
1316
1316
  const home = this.opts.home ?? join2(homedir2(), ".agent-theatre");
1317
1317
  mkdirSync(home, { recursive: true });
1318
+ try {
1319
+ const old = parseInt(readFileSync2(join2(home, "port"), "utf8").trim(), 10);
1320
+ if (old > 0 && old !== this.port && portAlive(old) && !existsSync(join2(home, "ports", String(old)))) {
1321
+ mkdirSync(join2(home, "ports"), { recursive: true });
1322
+ writeFileSync(join2(home, "ports", String(old)), "(started by an older build)");
1323
+ }
1324
+ } catch {
1325
+ }
1318
1326
  writeFileSync(join2(home, "port"), String(this.port));
1327
+ mkdirSync(join2(home, "ports"), { recursive: true });
1328
+ for (const f of readdirSync2(join2(home, "ports"))) {
1329
+ const n = parseInt(f, 10);
1330
+ if (n > 0 && n !== this.port && !portAlive(n)) {
1331
+ try {
1332
+ rmSync(join2(home, "ports", f));
1333
+ } catch {
1334
+ }
1335
+ }
1336
+ }
1337
+ writeFileSync(join2(home, "ports", String(this.port)), this.opts.cwd);
1319
1338
  const lostAfter = this.opts.lostAfterMs ?? 9e4;
1320
1339
  this.timer = setInterval(() => {
1321
1340
  const l = this.live;
@@ -1334,6 +1353,10 @@ var Theatre = class {
1334
1353
  if (readFileSync2(join2(home, "port"), "utf8").trim() === String(this.port)) rmSync(join2(home, "port"));
1335
1354
  } catch {
1336
1355
  }
1356
+ try {
1357
+ rmSync(join2(home, "ports", String(this.port)));
1358
+ } catch {
1359
+ }
1337
1360
  for (const ws of this.wss?.clients ?? []) ws.close();
1338
1361
  this.wss?.close();
1339
1362
  await new Promise((res) => this.server ? this.server.close(() => res()) : res());
@@ -1377,6 +1400,12 @@ function claudeBinary() {
1377
1400
  }
1378
1401
  return null;
1379
1402
  }
1403
+ function portAlive(port) {
1404
+ const r = spawnSync("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-t"], { encoding: "utf8", timeout: 2e3 });
1405
+ if (r.status === 0 && r.stdout.trim()) return true;
1406
+ if (r.error) return true;
1407
+ return false;
1408
+ }
1380
1409
 
1381
1410
  // src/scan.ts
1382
1411
  import { readdirSync as readdirSync3, readFileSync as readFileSync3, statSync as statSync2 } from "fs";
@@ -1511,20 +1540,22 @@ function hasOurHooks(existing) {
1511
1540
  // src/hooks/script.ts
1512
1541
  var HOOK_SCRIPT = `#!/usr/bin/env node
1513
1542
  // Agent Theatre hook \u2014 forwards Claude Code events to the local theatre. Never blocks the agent.
1514
- import { readFileSync } from "node:fs";
1543
+ import { readFileSync, readdirSync } from "node:fs";
1515
1544
  import { homedir } from "node:os";
1516
1545
  import { join } from "node:path";
1517
- const portFile = join(process.env.AGENT_THEATRE_HOME || join(homedir(), ".agent-theatre"), "port");
1518
- let port = 0;
1519
- try { port = parseInt(readFileSync(portFile, "utf8").trim(), 10); } catch { process.exit(0); }
1520
- if (!port) process.exit(0);
1546
+ // Every running stage registers itself in ports/<port>; the event goes to all of them (browser, VS Code, a second repo\u2026).
1547
+ const home = process.env.AGENT_THEATRE_HOME || join(homedir(), ".agent-theatre");
1548
+ const ports = new Set();
1549
+ try { for (const f of readdirSync(join(home, "ports"))) { const n = parseInt(f, 10); if (n > 0) ports.add(n); } } catch { /* no registry yet */ }
1550
+ try { const n = parseInt(readFileSync(join(home, "port"), "utf8").trim(), 10); if (n > 0) ports.add(n); } catch { /* no port file */ }
1551
+ if (!ports.size) process.exit(0);
1521
1552
  let body = "";
1522
1553
  process.stdin.setEncoding("utf8");
1523
1554
  process.stdin.on("data", (c) => { body += c; if (body.length > 2_000_000) body = body.slice(0, 2_000_000); });
1524
1555
  process.stdin.on("end", async () => {
1525
1556
  try {
1526
- await fetch(\`http://127.0.0.1:\${port}/hook\`, { method: "POST", headers: { "content-type": "application/json" }, body, signal: AbortSignal.timeout(300) });
1527
- } catch { /* theatre not running or slow: that is fine */ }
1557
+ await Promise.allSettled([...ports].map((port) => fetch(\`http://127.0.0.1:\${port}/hook\`, { method: "POST", headers: { "content-type": "application/json" }, body, signal: AbortSignal.timeout(300) })));
1558
+ } catch { /* a theatre not running or slow: that is fine */ }
1528
1559
  process.exit(0);
1529
1560
  });
1530
1561
  setTimeout(() => process.exit(0), 600).unref();