@nice-code/commander 0.102.0 → 0.103.0

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.
@@ -24,7 +24,7 @@
24
24
  }
25
25
  * { box-sizing: border-box; }
26
26
  </style>
27
- <script type="module" crossorigin src="/assets/index-XAXsvgf4.js"></script>
27
+ <script type="module" crossorigin src="/assets/index-CP-c9YFT.js"></script>
28
28
  <link rel="stylesheet" crossorigin href="/assets/index-BEawHnvQ.css">
29
29
  </head>
30
30
  <body>
@@ -91,6 +91,13 @@ interface ICommanderProcessDefinition {
91
91
  */
92
92
  dependsOn?: string[];
93
93
  endpoints?: ICommanderEndpoint[];
94
+ /**
95
+ * A start that finds an `exclusive` endpoint held by a STRAY — this same command (argv + cwd)
96
+ * running outside the daemon, typically started by hand in a terminal that has since gone —
97
+ * stops that tree and takes the port, instead of refusing. A stranger on the port still
98
+ * refuses. Default false; `up --replace-same` does the same for one invocation.
99
+ */
100
+ replaceSame?: boolean;
94
101
  /** Primary "open" link when no endpoint owns it. */
95
102
  url?: string;
96
103
  ready?: TCommanderReady;
@@ -131,6 +138,8 @@ interface ICommanderSuiteDefinition {
131
138
  }
132
139
  interface ICommanderDefaults {
133
140
  kind?: TNiceProcessKind;
141
+ /** See `ICommanderProcessDefinition.replaceSame`. */
142
+ replaceSame?: boolean;
134
143
  stop?: INiceProcessStopOptions;
135
144
  restart?: INiceProcessRestartOptions;
136
145
  log?: {
@@ -91,6 +91,13 @@ interface ICommanderProcessDefinition {
91
91
  */
92
92
  dependsOn?: string[];
93
93
  endpoints?: ICommanderEndpoint[];
94
+ /**
95
+ * A start that finds an `exclusive` endpoint held by a STRAY — this same command (argv + cwd)
96
+ * running outside the daemon, typically started by hand in a terminal that has since gone —
97
+ * stops that tree and takes the port, instead of refusing. A stranger on the port still
98
+ * refuses. Default false; `up --replace-same` does the same for one invocation.
99
+ */
100
+ replaceSame?: boolean;
94
101
  /** Primary "open" link when no endpoint owns it. */
95
102
  url?: string;
96
103
  ready?: TCommanderReady;
@@ -131,6 +138,8 @@ interface ICommanderSuiteDefinition {
131
138
  }
132
139
  interface ICommanderDefaults {
133
140
  kind?: TNiceProcessKind;
141
+ /** See `ICommanderProcessDefinition.replaceSame`. */
142
+ replaceSame?: boolean;
134
143
  stop?: INiceProcessStopOptions;
135
144
  restart?: INiceProcessRestartOptions;
136
145
  log?: {
@@ -530,6 +530,7 @@ function projectProcess(status) {
530
530
  removed: status.removed,
531
531
  envDrift: status.envDrift,
532
532
  ...status.blockedBy != null ? { blockedBy: status.blockedBy } : {},
533
+ ...status.startError != null ? { startError: status.startError } : {},
533
534
  degradedBy: status.degradedBy,
534
535
  ...status.unreadyReason != null ? { unreadyReason: status.unreadyReason } : {},
535
536
  ...status.capability != null ? { capability: {
@@ -543,6 +544,7 @@ function projectProcess(status) {
543
544
  command: status.detail.command,
544
545
  cwd: status.detail.cwd,
545
546
  dependsOn: status.detail.dependsOn,
547
+ ...status.detail.url != null ? { url: status.detail.url } : {},
546
548
  ...status.detail.ready != null ? { ready: status.detail.ready } : {},
547
549
  ...status.detail.timeoutMs != null ? { timeoutMs: status.detail.timeoutMs } : {},
548
550
  endpoints: status.detail.endpoints.map((endpoint) => ({
@@ -640,12 +642,13 @@ function createControlPlane(options) {
640
642
  } }), require_cli.httpAcceptorCarrier()],
641
643
  handlers: [require_cli.commanderControlDomain.wrapAsLocalHandler({
642
644
  ports: (input) => ports.run(input),
643
- start: async ({ ids, wait, staggerMs }) => {
645
+ start: async ({ ids, wait, staggerMs, replaceSame }) => {
644
646
  const config = supervisor.config;
645
647
  const ordered = require_cli.orderByDependency(config.processes, require_cli.withDependencies(config.processes, ids));
646
648
  return supervisor.start(ordered, {
647
649
  wait: wait ?? true,
648
- staggerMs: staggerMs ?? 0
650
+ staggerMs: staggerMs ?? 0,
651
+ replaceSame: replaceSame === true
649
652
  });
650
653
  },
651
654
  stop: async ({ ids, withDependencies: alsoDependencies }) => {
@@ -658,13 +661,14 @@ function createControlPlane(options) {
658
661
  warnings: [...report.warnings, `left running — still needed by other live processes: ${kept.join(", ")}`]
659
662
  };
660
663
  },
661
- restart: async ({ ids, wait, staggerMs }) => {
664
+ restart: async ({ ids, wait, staggerMs, replaceSame }) => {
662
665
  const config = supervisor.config;
663
666
  const ordered = require_cli.orderByDependency(config.processes, require_cli.withDependencies(config.processes, ids));
664
667
  return supervisor.restart(ordered, {
665
668
  wait: wait ?? true,
666
669
  staggerMs: staggerMs ?? 0,
667
- stopIds: ids
670
+ stopIds: ids,
671
+ replaceSame: replaceSame === true
668
672
  });
669
673
  },
670
674
  runSuite: async (input) => {
@@ -1299,6 +1303,8 @@ var CommanderSupervisor = class {
1299
1303
  _mergedListeners = /* @__PURE__ */ new Set();
1300
1304
  _envOverrides;
1301
1305
  _stopping = false;
1306
+ /** The daemon and its ancestors — never a stray, never signalled. Read once. */
1307
+ _protectedPids = null;
1302
1308
  constructor(options) {
1303
1309
  this._config = options.config;
1304
1310
  this._ledger = options.ledger;
@@ -1372,6 +1378,8 @@ var CommanderSupervisor = class {
1372
1378
  stale: false,
1373
1379
  removed: false,
1374
1380
  blockedBy: null,
1381
+ blockedOn: null,
1382
+ startError: null,
1375
1383
  degradedBy: /* @__PURE__ */ new Set(),
1376
1384
  taskTimer: null,
1377
1385
  taskRun: null,
@@ -1434,6 +1442,7 @@ var CommanderSupervisor = class {
1434
1442
  }
1435
1443
  if (definition.kind === "service" && (next === "stopped" || next === "succeeded" || next === "failed" || next === "crashed")) this._runEnded(holder, next === "stopped" ? "stopped" : next === "succeeded" ? "succeeded" : "crashed");
1436
1444
  this._recomputeDegraded();
1445
+ this._recomputeBlocked();
1437
1446
  this._notify(definition.id);
1438
1447
  });
1439
1448
  holder.proc = proc;
@@ -1455,26 +1464,75 @@ var CommanderSupervisor = class {
1455
1464
  }
1456
1465
  }
1457
1466
  /**
1467
+ * A blocked note names ONE dependency; once that dependency is ready (or, for a task, has
1468
+ * succeeded) the note is stale and is dropped. Before this, `status` could show a dependent
1469
+ * "blocked: dependency X is not running" two rows under an X that was plainly ready — the
1470
+ * reason was from an earlier generation and nothing ever re-evaluated it.
1471
+ */
1472
+ _recomputeBlocked() {
1473
+ for (const holder of this._holders.values()) {
1474
+ if (holder.blockedBy == null || holder.blockedOn == null) continue;
1475
+ const dep = this._holders.get(holder.blockedOn);
1476
+ if (dep == null || dep.removed || dep.proc == null) continue;
1477
+ if (!(dep.definition.kind === "task" ? dep.proc.state === "succeeded" : dep.proc.state === "ready")) continue;
1478
+ holder.blockedBy = null;
1479
+ holder.blockedOn = null;
1480
+ this._notify(holder.definition.id);
1481
+ }
1482
+ }
1483
+ _protected() {
1484
+ this._protectedPids ??= require_cli.ancestors().then((pids) => new Set(pids));
1485
+ return this._protectedPids;
1486
+ }
1487
+ /**
1458
1488
  * Edge 13 — a declared EXCLUSIVE endpoint that is already bound belongs to somebody else, and
1459
1489
  * spawning on top of it produces exactly the confusion the endpoint model exists to prevent:
1460
1490
  * the child either fails to bind with a message nobody reads, or (with a fallback binder)
1461
1491
  * quietly drifts to another port while readiness latches on the squatter (edge 48).
1462
1492
  *
1463
- * ⚠ Reported, never cleared. Attributing a listening socket to a pid is not portable, so an
1464
- * occupied port is surfaced and the start refused — killing an unidentified holder is the one
1465
- * thing this must not do. Our own orphans are already gone by here (the boot scavenger reaps
1466
- * them before anything starts) and a live foreign lease was refused earlier (C1).
1493
+ * The owner is identified where the platform allows (argv + cwd, up the listener's ancestry).
1494
+ * A STRAY — this very definition's command, started outside the daemon — is the one owner a
1495
+ * start may replace, and only when asked (`replaceSame`); it is stopped as the tree that was
1496
+ * matched. Anything else is reported with what it is and refused: killing an unidentified or
1497
+ * foreign holder is the one thing this must not do. Our own orphans are already gone by here
1498
+ * (the boot scavenger reaps them before anything starts) and a live foreign lease was refused
1499
+ * earlier (C1).
1467
1500
  */
1468
- async _exclusiveEndpointConflict(holder) {
1501
+ async _exclusiveEndpointConflict(holder, replaceSame) {
1502
+ const id = holder.definition.id;
1469
1503
  for (const endpoint of holder.definition.endpoints) {
1470
1504
  if (endpoint.ownership !== "exclusive") continue;
1471
1505
  if (!await tcpBound(endpoint.host, endpoint.port)) continue;
1472
1506
  await wait(ENDPOINT_RECHECK_MS);
1473
1507
  if (!await tcpBound(endpoint.host, endpoint.port)) continue;
1474
- return `exclusive endpoint "${endpoint.name}" (${endpoint.host}:${endpoint.port}) is already in use — use inspect ports in the UI, or run \`nice-commander ports ${holder.definition.id}\` to identify and stop its owner`;
1508
+ const where = `exclusive endpoint "${endpoint.name}" (${endpoint.host}:${endpoint.port})`;
1509
+ const owner = await require_cli.identifyEndpointOwner(holder.definition, endpoint, await this._protected()).catch(() => null);
1510
+ if (owner?.stray != null) {
1511
+ const root = owner.stray.root.pid;
1512
+ if (!replaceSame) return `${where} is held by a stray copy of this definition (pid ${root}, the same command in the same cwd, started outside the daemon) — \`up ${id} --replace-same\` replaces it, \`ports ${id} --kill-owner\` stops it, or set replaceSame: true on the definition`;
1513
+ if (await this._replaceStray(endpoint, owner.stray, holder.definition.stop.graceMs)) continue;
1514
+ return `${where} is held by a stray copy of this definition (pid ${root}) that did not release the port after SIGTERM and SIGKILL — \`nice-commander ports ${id}\` shows what remains`;
1515
+ }
1516
+ if (owner != null) return `${where} is already in use by pid ${owner.pid} (${owner.command}${owner.cwd != null ? `, cwd ${owner.cwd}` : ""}) — not this definition's command, so it is never stopped automatically; \`nice-commander ports ${id}\` inspects and stops it`;
1517
+ return `${where} is already in use — use inspect ports in the UI, or run \`nice-commander ports ${id}\` to identify and stop its owner`;
1475
1518
  }
1476
1519
  return null;
1477
1520
  }
1521
+ /** Take a stray's port: the same ladder a stop uses — SIGTERM, grace, SIGKILL — then verify. */
1522
+ async _replaceStray(endpoint, stray, graceMs = 4e3) {
1523
+ const released = async (bound) => {
1524
+ const deadline = Date.now() + bound;
1525
+ while (Date.now() < deadline) {
1526
+ if (!await tcpBound(endpoint.host, endpoint.port)) return true;
1527
+ await wait(100);
1528
+ }
1529
+ return !await tcpBound(endpoint.host, endpoint.port);
1530
+ };
1531
+ if (await require_cli.signalStray(stray, "SIGTERM") === 0) return released(500);
1532
+ if (await released(graceMs)) return true;
1533
+ await require_cli.signalStray(stray, "SIGKILL");
1534
+ return released(2e3);
1535
+ }
1478
1536
  /** Wait until `proc.state` satisfies `want`, a terminal state intervenes, or the bound lapses. */
1479
1537
  _waitForState(proc, want, timeoutMs) {
1480
1538
  if (want(proc.state)) return Promise.resolve(true);
@@ -1501,6 +1559,7 @@ var CommanderSupervisor = class {
1501
1559
  async start(orderedIds, options = {}) {
1502
1560
  const waitForReady = options.wait !== false;
1503
1561
  const staggerMs = options.staggerMs ?? 0;
1562
+ const replaceSame = options.replaceSame === true;
1504
1563
  const results = [];
1505
1564
  const taskRuns = /* @__PURE__ */ new Map();
1506
1565
  let spawned = 0;
@@ -1530,15 +1589,19 @@ var CommanderSupervisor = class {
1530
1589
  }
1531
1590
  const leaseKind = leased.get(id);
1532
1591
  if (leaseKind != null) {
1592
+ holder.startError = `held by a live \`${leaseKind}\` process — stop it there first`;
1593
+ this._notify(id);
1533
1594
  results.push({
1534
1595
  id,
1535
1596
  outcome: "failed",
1536
- reason: `held by a live \`${leaseKind}\` process — stop it there first`
1597
+ reason: holder.startError
1537
1598
  });
1538
1599
  continue;
1539
1600
  }
1540
1601
  let blocked = null;
1602
+ let blockedOn = null;
1541
1603
  for (const depId of holder.definition.dependsOn) {
1604
+ blockedOn = depId;
1542
1605
  const dep = this._holders.get(depId);
1543
1606
  if (dep == null || dep.removed) {
1544
1607
  blocked = `dependency "${depId}" is unavailable`;
@@ -1567,6 +1630,7 @@ var CommanderSupervisor = class {
1567
1630
  }
1568
1631
  if (blocked != null) {
1569
1632
  holder.blockedBy = blocked;
1633
+ holder.blockedOn = blockedOn;
1570
1634
  this._notify(id);
1571
1635
  results.push({
1572
1636
  id,
@@ -1576,9 +1640,10 @@ var CommanderSupervisor = class {
1576
1640
  continue;
1577
1641
  }
1578
1642
  holder.blockedBy = null;
1643
+ holder.blockedOn = null;
1579
1644
  let result;
1580
1645
  if (holder.definition.kind === "task") {
1581
- const run = this._runTaskOnce(holder, taskRuns);
1646
+ const run = this._runTaskOnce(holder, taskRuns, replaceSame);
1582
1647
  if (waitForReady) result = await run;
1583
1648
  else {
1584
1649
  run.catch(() => {});
@@ -1598,26 +1663,33 @@ var CommanderSupervisor = class {
1598
1663
  outcome: "failed",
1599
1664
  reason: "supervisor is stopping"
1600
1665
  };
1601
- const conflict = await this._exclusiveEndpointConflict(holder);
1602
- if (conflict != null) return {
1603
- id,
1604
- outcome: "failed",
1605
- reason: conflict
1606
- };
1666
+ const conflict = await this._exclusiveEndpointConflict(holder, replaceSame || holder.definition.replaceSame);
1667
+ if (conflict != null) {
1668
+ holder.startError = conflict;
1669
+ this._notify(id);
1670
+ return {
1671
+ id,
1672
+ outcome: "failed",
1673
+ reason: conflict
1674
+ };
1675
+ }
1607
1676
  if (spawned > 0 && staggerMs > 0) await wait(staggerMs);
1608
1677
  spawned += 1;
1609
1678
  try {
1610
1679
  await proc.start();
1611
1680
  holder.startedAt = Date.now();
1612
1681
  holder.stale = false;
1682
+ holder.startError = null;
1613
1683
  this._recomputeDegraded();
1614
1684
  this._notify(id);
1615
1685
  } catch (error) {
1616
1686
  this._runEnded(holder, "failed");
1687
+ holder.startError = error.message;
1688
+ this._notify(id);
1617
1689
  return {
1618
1690
  id,
1619
1691
  outcome: "failed",
1620
- reason: error.message
1692
+ reason: holder.startError
1621
1693
  };
1622
1694
  }
1623
1695
  if (!waitForReady) return {
@@ -1645,10 +1717,10 @@ var CommanderSupervisor = class {
1645
1717
  * A task within ONE start transaction: task dependencies run once per transaction, so a
1646
1718
  * repeat request inside the same action joins that run rather than starting a second one.
1647
1719
  */
1648
- _runTaskOnce(holder, taskRuns) {
1720
+ _runTaskOnce(holder, taskRuns, replaceSame = false) {
1649
1721
  const existing = taskRuns.get(holder.definition.id);
1650
1722
  if (existing != null) return existing;
1651
- const run = this._taskRun(holder);
1723
+ const run = this._taskRun(holder, replaceSame);
1652
1724
  taskRuns.set(holder.definition.id, run);
1653
1725
  return run;
1654
1726
  }
@@ -1661,7 +1733,7 @@ var CommanderSupervisor = class {
1661
1733
  * the task's own exit — an unbounded deadlock for a task with no `timeoutMs`, and the reason a
1662
1734
  * wedged check could hang daemon shutdown forever (C3, edges 14/36/39).
1663
1735
  */
1664
- _taskRun(holder) {
1736
+ _taskRun(holder, replaceSame = false) {
1665
1737
  if (holder.taskRun != null) return holder.taskRun;
1666
1738
  const id = holder.definition.id;
1667
1739
  const run = (async () => {
@@ -1673,23 +1745,30 @@ var CommanderSupervisor = class {
1673
1745
  outcome: "failed",
1674
1746
  reason: "supervisor is stopping"
1675
1747
  };
1676
- const conflict = await this._exclusiveEndpointConflict(holder);
1677
- if (conflict != null) return {
1678
- id,
1679
- outcome: "failed",
1680
- reason: conflict
1681
- };
1748
+ const conflict = await this._exclusiveEndpointConflict(holder, replaceSame || holder.definition.replaceSame);
1749
+ if (conflict != null) {
1750
+ holder.startError = conflict;
1751
+ this._notify(id);
1752
+ return {
1753
+ id,
1754
+ outcome: "failed",
1755
+ reason: conflict
1756
+ };
1757
+ }
1682
1758
  try {
1683
1759
  await proc.start();
1684
1760
  holder.startedAt = Date.now();
1685
1761
  holder.stale = false;
1762
+ holder.startError = null;
1686
1763
  this._notify(id);
1687
1764
  } catch (error) {
1688
1765
  this._runEnded(holder, "failed");
1766
+ holder.startError = error.message;
1767
+ this._notify(id);
1689
1768
  return {
1690
1769
  id,
1691
1770
  outcome: "failed",
1692
- reason: error.message
1771
+ reason: holder.startError
1693
1772
  };
1694
1773
  }
1695
1774
  return null;
@@ -1798,7 +1877,11 @@ var CommanderSupervisor = class {
1798
1877
  }
1799
1878
  results.push(await this._enqueue(holder, async () => {
1800
1879
  if (holder.proc == null || !ACTIVE_STATES.has(holder.proc.state)) {
1880
+ const noted = holder.blockedBy != null || holder.startError != null;
1801
1881
  holder.blockedBy = null;
1882
+ holder.blockedOn = null;
1883
+ holder.startError = null;
1884
+ if (noted) this._notify(id);
1802
1885
  return {
1803
1886
  id,
1804
1887
  outcome: "already-stopped"
@@ -1883,6 +1966,7 @@ var CommanderSupervisor = class {
1883
1966
  holder.removed = true;
1884
1967
  this._notify(holder.definition.id);
1885
1968
  }
1969
+ this._recomputeBlocked();
1886
1970
  }
1887
1971
  get envOverrides() {
1888
1972
  return this._envOverrides;
@@ -1899,9 +1983,9 @@ var CommanderSupervisor = class {
1899
1983
  status() {
1900
1984
  return [...this._holders.values()].map((holder) => {
1901
1985
  const proc = holder.proc;
1902
- const rawState = proc != null ? proc.state : holder.blockedBy != null ? "blocked" : "not-started";
1903
- const state = rawState === "stopped" && holder.lastTask?.timedOut === true ? "failed" : rawState;
1904
1986
  const active = proc != null && ACTIVE_STATES.has(proc.state);
1987
+ const rawState = active ? proc.state : holder.blockedBy != null ? "blocked" : proc != null && proc.state !== "idle" ? proc.state : "not-started";
1988
+ const state = rawState === "stopped" && holder.lastTask?.timedOut === true ? "failed" : rawState;
1905
1989
  const envVars = require_cli.envVarViews(holder.definition, this._envOverrides, process.env);
1906
1990
  return {
1907
1991
  id: holder.definition.id,
@@ -1918,6 +2002,7 @@ var CommanderSupervisor = class {
1918
2002
  removed: holder.removed,
1919
2003
  envDrift: active && holder.envFingerprint !== require_cli.overrideFingerprint(holder.definition, this._envOverrides),
1920
2004
  blockedBy: holder.blockedBy,
2005
+ startError: active ? null : holder.startError,
1921
2006
  degradedBy: [...holder.degradedBy],
1922
2007
  unreadyReason: proc?.unreadyReason ?? null,
1923
2008
  capability: proc?.capability ?? null,
@@ -1979,6 +2064,7 @@ function describe(definition, envVars) {
1979
2064
  command: typeof run === "string" ? run : run.join(" "),
1980
2065
  cwd: definition.cwd,
1981
2066
  dependsOn: [...definition.dependsOn],
2067
+ ...definition.url == null ? {} : { url: definition.url },
1982
2068
  envVars,
1983
2069
  ...definition.timeoutMs == null ? {} : { timeoutMs: definition.timeoutMs },
1984
2070
  ...definition.ready == null ? {} : { ready: describeReady(definition.ready) },
@@ -2270,7 +2356,7 @@ async function startCommanderDaemon(argv) {
2270
2356
  configKey: resolved.configKey,
2271
2357
  configRevisionHash: resolved.configRevisionHash,
2272
2358
  configPath: resolved.configPath,
2273
- protocolVersion: 6,
2359
+ protocolVersion: 7,
2274
2360
  packageVersion: await require_cli.commanderPackageVersion(),
2275
2361
  realmSchemaHash: require_cli.commanderStatusRealm.schemaHash
2276
2362
  });
@@ -2355,4 +2441,4 @@ async function runCommanderDaemon(argv) {
2355
2441
  //#endregion
2356
2442
  exports.runCommanderDaemon = runCommanderDaemon;
2357
2443
 
2358
- //# sourceMappingURL=main-D-StoxUH.cjs.map
2444
+ //# sourceMappingURL=main-DkrkRGT6.cjs.map