@feigi/fleet-ctl 3.24.4 → 3.24.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.24.4",
3
+ "version": "3.24.6",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
package/scripts/board.mjs CHANGED
@@ -52,6 +52,7 @@ import { fileURLToPath } from "node:url";
52
52
  import { isCLI } from "./is-cli.mjs";
53
53
  import { dirname, join, basename } from "node:path";
54
54
  import { createServer, request as httpRequest } from "node:http";
55
+ import { connect } from "node:net";
55
56
  import { inspect } from "node:util";
56
57
 
57
58
  const NAME = "board";
@@ -1582,7 +1583,34 @@ function bindFailure(server, port) {
1582
1583
  const onListening = () => { server.removeListener("error", onError); resolve(null); };
1583
1584
  server.once("error", onError);
1584
1585
  server.once("listening", onListening);
1585
- server.listen(port);
1586
+ server.listen(port, COCKPIT_HOST);
1587
+ });
1588
+ }
1589
+
1590
+ // Whether something already answers on COCKPIT_HOST:port, asked BEFORE the
1591
+ // bind rather than read off it: a 127.0.0.1 bind does not reliably fail when
1592
+ // the port is taken. On macOS/BSD, SO_REUSEADDR lets it succeed beside a
1593
+ // listener that holds every interface — a dev server, or a cockpit started
1594
+ // before the bind was narrowed to loopback — and the launch would then sit
1595
+ // on that holder's 127.0.0.1 traffic instead of stepping over it or reusing
1596
+ // it. Linux refuses that bind outright; this check gives both the same answer.
1597
+ // Resolves in bindFailure()'s vocabulary so the loop reads one shape: null
1598
+ // when the connection is refused (nothing holds the port), an EADDRINUSE
1599
+ // error when it connects or times out, and any other error as itself — a
1600
+ // fault of its own, like EACCES on a bind. A timeout counts as held because
1601
+ // a live holder whose accept backlog is full drops the SYN rather than
1602
+ // refusing it. The window between this check and the bind is a race the
1603
+ // loop accepts, like its others.
1604
+ function heldFailure(port) {
1605
+ return new Promise((resolve) => {
1606
+ const socket = connect({ host: COCKPIT_HOST, port });
1607
+ const held = () => {
1608
+ socket.destroy();
1609
+ resolve(Object.assign(new Error(`port ${port} answers on ${COCKPIT_HOST}`), { code: "EADDRINUSE" }));
1610
+ };
1611
+ socket.setTimeout(PROBE_TIMEOUT_MS, held);
1612
+ socket.once("connect", held);
1613
+ socket.once("error", (e) => resolve(e.code === "ECONNREFUSED" ? null : e));
1586
1614
  });
1587
1615
  }
1588
1616
 
@@ -1605,12 +1633,16 @@ function bindFailure(server, port) {
1605
1633
  // ruled out as the fix for it — it would have to clear a gather(), which
1606
1634
  // charges every launch past a stranger-held port that much per candidate.
1607
1635
  const PROBE_TIMEOUT_MS = 1000;
1608
- // 127.0.0.1 rather than `localhost`: no resolver in the path of a launch.
1609
- // This is not the cockpit's bind address — bindFailure's listen() names no
1610
- // host, so a cockpit binds every interface, not loopback alone — but
1611
- // 127.0.0.1 is one of them, so every cockpit answers here. The announced URL
1612
- // stays `localhost`, which is the operator's spelling, not this probe's.
1613
- const PROBE_HOST = "127.0.0.1";
1636
+ // The one address a cockpit binds, and so the one address the probe and the
1637
+ // pre-bind check dial: whatever holds a candidate where this launch would
1638
+ // bind answers here. 127.0.0.1 rather than `localhost` keeps a resolver out
1639
+ // of the launch path, and loopback alone keeps the board — a read-only
1640
+ // mirror of the ledger — off every other machine on the network; remote
1641
+ // viewing goes through an `ssh -L` tunnel. IPv4 only: `::1` is left unbound,
1642
+ // which is why every URL printed below names 127.0.0.1 too. `localhost`
1643
+ // resolves `::1` first, and on `::1` a browser can reach some other process
1644
+ // that holds the same port.
1645
+ const COCKPIT_HOST = "127.0.0.1";
1614
1646
  // A holder is not necessarily a cockpit, and a second of localhost writes is
1615
1647
  // a lot of memory to accept from one. The board payload for a real run is
1616
1648
  // kilobytes; anything past this is not one, so it is refused as foreign
@@ -1663,7 +1695,7 @@ export function probeCockpitWorkspace(port, timeoutMs = PROBE_TIMEOUT_MS) {
1663
1695
  req.destroy();
1664
1696
  resolve(workspace);
1665
1697
  }
1666
- const req = httpRequest({ host: PROBE_HOST, port, path: "/board.json", method: "GET" }, (res) => {
1698
+ const req = httpRequest({ host: COCKPIT_HOST, port, path: "/board.json", method: "GET" }, (res) => {
1667
1699
  if (res.statusCode !== 200) { finish(null); return; }
1668
1700
  let body = "";
1669
1701
  res.setEncoding("utf8");
@@ -1810,9 +1842,10 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
1810
1842
  // port with nothing a handshake could ever match, reopening the
1811
1843
  // dual-cockpit hazard.
1812
1844
  //
1813
- // Bind stays the FIRST thing tried per candidate: that is what lets two
1814
- // launches racing at start settle on one winner quickly, the winner's
1815
- // identity published (below) before it can block inside its own gather().
1845
+ // Bind stays ahead of any handshake per candidate — only heldFailure()'s
1846
+ // bare connect precedes it: that is what lets two launches racing at start
1847
+ // settle on one winner quickly, the winner's identity published (below)
1848
+ // before it can block inside its own gather().
1816
1849
  // But a successful bind is not the end of the story — once this process
1817
1850
  // holds a candidate, the REST of the window still gets checked for a
1818
1851
  // live cockpit that landed further along, exactly the shape a departed
@@ -1829,14 +1862,15 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
1829
1862
  for (let i = 0; i < candidates.length; i++) {
1830
1863
  const candidate = candidates[i];
1831
1864
  const attempt = createBoardServer(stateDir);
1832
- const failure = await bindFailure(attempt, candidate);
1865
+ // --port 0 asks the kernel for a port nobody holds; there is nothing to dial.
1866
+ const failure = (candidate === 0 ? null : await heldFailure(candidate)) ?? await bindFailure(attempt, candidate);
1833
1867
  if (!failure) {
1834
1868
  const rest = scannable ? candidates.slice(i + 1) : [];
1835
1869
  const holders = await Promise.all(rest.map((p) => probeCockpitWorkspace(p)));
1836
1870
  const matchAt = holders.indexOf(instance.workspace);
1837
1871
  if (matchAt === -1) { server = attempt; break; }
1838
1872
  attempt.close();
1839
- const url = `http://localhost:${rest[matchAt]}/`;
1873
+ const url = `http://${COCKPIT_HOST}:${rest[matchAt]}/`;
1840
1874
  console.error(`${NAME}: cockpit already running for this workspace on ${url}`);
1841
1875
  // Nothing is opened here, --open or not. This cockpit was
1842
1876
  // already running when this launch arrived, so its tab was the business
@@ -1852,14 +1886,15 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
1852
1886
  process.exit(0);
1853
1887
  }
1854
1888
  // Only a port that is TAKEN is a candidate for any of the below. EACCES
1855
- // on a privileged port, EADDRNOTAVAIL on an unusable address: those are
1856
- // faults of their own, and scanning past them would bury each one under
1857
- // an exhausted-range message at the end that names the wrong problem.
1889
+ // on a privileged port, EADDRNOTAVAIL on an unusable address, a pre-bind
1890
+ // connect that fails with anything but ECONNREFUSED: those are faults of
1891
+ // their own, and scanning past them would bury each one under an
1892
+ // exhausted-range message at the end that names the wrong problem.
1858
1893
  if (failure.code !== "EADDRINUSE") die(failure.message);
1859
1894
  if (!scannable) die(`port ${candidate} in use — pass --port <n>`);
1860
1895
  const holder = await probeCockpitWorkspace(candidate);
1861
1896
  if (holder === instance.workspace) {
1862
- const url = `http://localhost:${candidate}/`;
1897
+ const url = `http://${COCKPIT_HOST}:${candidate}/`;
1863
1898
  console.error(`${NAME}: cockpit already running for this workspace on ${url}`);
1864
1899
  // Already running, so nothing opens — the same rule as the arm above.
1865
1900
  process.exit(0);
@@ -1894,11 +1929,11 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
1894
1929
  //
1895
1930
  // Announce the port we GOT, not the one we asked for. They differ for the
1896
1931
  // one value `--port 0` newly permits: listen(0) binds an ephemeral port, so
1897
- // echoing the request prints — and --opens — http://localhost:0, which
1932
+ // echoing the request prints — and --opens — http://127.0.0.1:0, which
1898
1933
  // reaches nothing while the board sits on a port nobody was told.
1899
1934
  // address() is only populated once listening, hence only here.
1900
1935
  const bound = server.address().port;
1901
- console.error(`${NAME}: cockpit on http://localhost:${bound} (interval ${interval}s)`);
1936
+ console.error(`${NAME}: cockpit on http://${COCKPIT_HOST}:${bound} (interval ${interval}s)`);
1902
1937
 
1903
1938
  // Publish identity the instant this port is ours — before the
1904
1939
  // first tick, which is the one that can be slow (gather() shells out to
@@ -1948,7 +1983,7 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
1948
1983
  // return only when that browser exits, so a launcher awaited any earlier
1949
1984
  // could hold the first tick back for the whole session. openBrowser()
1950
1985
  // never throws; a launcher that is missing or fails is one warning line.
1951
- if (open) await openBrowser(`http://localhost:${bound}/`);
1986
+ if (open) await openBrowser(`http://${COCKPIT_HOST}:${bound}/`);
1952
1987
  await new Promise(() => {}); // run until signalled
1953
1988
  }
1954
1989
 
@@ -55,7 +55,10 @@
55
55
  // counted ruling holding anything but `yes` or `no` in either column, one
56
56
  // blank included, is refused rather than read as a pass. So is a non-blank
57
57
  // ruling row of such a ticket whose `run_date` is not `YYYY-MM-DD`: it cannot
58
- // be placed against the Pull, and dropping it would uncount the ticket.
58
+ // be placed against the Pull, and dropping it would uncount the ticket. So is
59
+ // an admissible Pull at X whose `run_date` is not `YYYY-MM-DD`: it cannot be
60
+ // placed against the day X's definition was added, and dropping it would
61
+ // uncount its ruling.
59
62
  // X is to be withdrawn once it has at least STOP.verdicts verdicts and
60
63
  // floor failures ÷ verdicts is at least STOP.failRate.
61
64
  //
@@ -68,7 +71,7 @@ import { isCLI } from "./is-cli.mjs";
68
71
  import { CELL, POLICY_CELL } from "./ledger-grammar.mjs";
69
72
  import { formatTsv as formatMemberTsv, parseTsv as parseMemberTsv } from "./member-outcomes.mjs";
70
73
  import { parseFeatures } from "./pr-cost.mjs";
71
- import { rulingFor, rulingsByTicket } from "./tier-outcomes.mjs";
74
+ import { DATE, rulingFor, rulingsByTicket } from "./tier-outcomes.mjs";
72
75
 
73
76
  const NAME = "cell-readout";
74
77
  export const GATE = Object.freeze({ comparisons: 10, runDates: 5 });
@@ -168,8 +171,9 @@ export function readout({ features, members }) {
168
171
  * rule withdraws the cell. `verdicts` are parsed tier-outcomes.tsv rows.
169
172
  * Throws on a counted ruling whose `closed_own_ticket` or `minted_false_claim`
170
173
  * is not `yes` or `no`, on a non-blank ruling row of a Pulled ticket whose
171
- * `run_date` is not `YYYY-MM-DD`, and on a member-outcomes session+agent
172
- * repeated with different fields.
174
+ * `run_date` is not `YYYY-MM-DD`, on an admissible Pull at a cell named in
175
+ * `added` whose `run_date` is not `YYYY-MM-DD`, and on a member-outcomes
176
+ * session+agent repeated with different fields.
173
177
  */
174
178
  export function stoppingRule({ features, members, verdicts, added }) {
175
179
  const byKey = indexMembers(members);
@@ -178,7 +182,9 @@ export function stoppingRule({ features, members, verdicts, added }) {
178
182
  const charged = new Map(Object.keys(added).filter((c) => c !== POLICY_CELL).map((c) => [c, new Map()]));
179
183
  for (const p of features) {
180
184
  const pulls = charged.get(p.chosen_cell);
181
- if (!pulls || p.run_date < added[p.chosen_cell] || !admissibleMember(p, byKey)) continue;
185
+ if (!pulls || !admissibleMember(p, byKey)) continue;
186
+ if (!DATE.test(p.run_date)) throw new Error(`ticket #${p.ticket} (Pull ${p.agent} at ${p.chosen_cell}): run_date is '${p.run_date}', expected YYYY-MM-DD`);
187
+ if (p.run_date < added[p.chosen_cell]) continue;
182
188
  pulls.set(p.ticket, p);
183
189
  }
184
190
  return [...charged].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([cell, pulls]) => {