agent-shim 14.0.2 → 14.0.4

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.cjs CHANGED
@@ -32306,6 +32306,9 @@ function reportableAllowlistFileProblem(allowlistFile, ports, served) {
32306
32306
  return problem?.repairable === false || served ? problem : void 0;
32307
32307
  }
32308
32308
  function verifyAllowlistFile(allowlistFile, ports) {
32309
+ if (platformRefusal(ports.platform) !== void 0) {
32310
+ return void 0;
32311
+ }
32309
32312
  const uid = ports.currentUid();
32310
32313
  const directories = pathDirectories(allowlistFile, ports);
32311
32314
  if (typeof directories === "string") {
@@ -33595,7 +33598,10 @@ var init_realPorts = __esm({
33595
33598
  };
33596
33599
  SOCKET_PERMISSION_BITS = 4095;
33597
33600
  realHeadroomSocketTrust = {
33598
- platform: process.platform,
33601
+ // Read at each use, not fixed when the module loads, so what the checks do for a platform is testable through the real wiring.
33602
+ get platform() {
33603
+ return process.platform;
33604
+ },
33599
33605
  currentUid: () => {
33600
33606
  if (process.getuid === void 0) {
33601
33607
  throw new Error(`no POSIX user id on ${process.platform}`);
@@ -45026,6 +45032,20 @@ function createDoorLookup(io) {
45026
45032
  });
45027
45033
  };
45028
45034
  }
45035
+ function sourcePortClassifier(platform) {
45036
+ const classify = (error63) => {
45037
+ const attempts = Reflect.get(error63, "errors");
45038
+ if (Array.isArray(attempts) && attempts.length > 0) {
45039
+ const kinds = attempts.map((attempt) => attempt instanceof Error ? classify(attempt) : void 0);
45040
+ return kinds.includes("excluded") ? "excluded" : kinds.includes("held") ? "held" : void 0;
45041
+ }
45042
+ if (error63.code === "EADDRINUSE" || error63.code === "EADDRNOTAVAIL") {
45043
+ return "held";
45044
+ }
45045
+ return platform === "win32" && error63.code === "EACCES" && error63.syscall === "bind" ? "excluded" : void 0;
45046
+ };
45047
+ return classify;
45048
+ }
45029
45049
  function doorDial(io, target, ports) {
45030
45050
  const dialOnce = (options) => new Promise((resolve3) => {
45031
45051
  const socket = io.connect(options);
@@ -45038,12 +45058,15 @@ function doorDial(io, target, ports) {
45038
45058
  if (error63.message === "" && Array.isArray(attempts) && attempts.length > 0) {
45039
45059
  const flattened = new Error(attempts.map((attempt) => withoutLocalEnd(attempt instanceof Error ? attempt.message : String(attempt))).join("; "));
45040
45060
  flattened.code = error63.code;
45061
+ flattened.syscall = error63.syscall;
45062
+ Object.defineProperty(flattened, "errors", { value: attempts });
45041
45063
  resolve3({ error: flattened, socket });
45042
45064
  return;
45043
45065
  }
45044
45066
  if (withoutLocalEnd(error63.message) !== error63.message) {
45045
45067
  const trimmed = new Error(withoutLocalEnd(error63.message));
45046
45068
  trimmed.code = error63.code;
45069
+ trimmed.syscall = error63.syscall;
45047
45070
  resolve3({ error: trimmed, socket });
45048
45071
  return;
45049
45072
  }
@@ -45053,17 +45076,35 @@ function doorDial(io, target, ports) {
45053
45076
  if (io.isIP(target.host) !== 0) {
45054
45077
  return dialOnce({ host: target.host, port: target.port });
45055
45078
  }
45056
- const attemptFrom = (attempt) => dialOnce({ host: target.host, port: target.port, localPort: ports.take(), lookup: io.lookup }).then((outcome) => {
45057
- if (outcome.error === null) {
45058
- return outcome;
45059
- }
45060
- const held = outcome.error.code === "EADDRINUSE" || outcome.error.code === "EADDRNOTAVAIL";
45061
- if (!held || attempt === ports.count - 1) {
45062
- return outcome;
45063
- }
45064
- return attemptFrom(attempt + 1);
45065
- });
45066
- return attemptFrom(0);
45079
+ const attemptFrom = (attempt, refused2) => {
45080
+ const port = ports.take();
45081
+ return dialOnce({ host: target.host, port: target.port, localPort: port, lookup: io.lookup }).then((outcome) => {
45082
+ if (outcome.error === null) {
45083
+ return outcome;
45084
+ }
45085
+ const failure2 = io.sourcePortFailure(outcome.error);
45086
+ if (failure2 === void 0) {
45087
+ return outcome;
45088
+ }
45089
+ if (failure2 === "excluded") {
45090
+ ports.exclude(port);
45091
+ }
45092
+ const refusedSoFar = failure2 === "excluded" ? refused2 + 1 : refused2;
45093
+ if (attempt < ports.count - 1) {
45094
+ return attemptFrom(attempt + 1, refusedSoFar);
45095
+ }
45096
+ if (refusedSoFar === 0) {
45097
+ return outcome;
45098
+ }
45099
+ const scope = refusedSoFar === ports.count ? "every source port" : `${String(refusedSoFar)} of its ${String(ports.count)} source ports (the rest were held by other sockets)`;
45100
+ const refusal2 = new Error(
45101
+ `${scope} of the door's reserved range (${String(ports.start)} to ${String(ports.end)}) could not be bound: Windows refuses a port inside an excluded range with EACCES (Hyper-V, WSL2 and Docker Desktop reserve blocks of ports). List the excluded ranges with \`netsh int ipv4 show excludedportrange protocol=tcp\``
45102
+ );
45103
+ refusal2.code = refusedSoFar === ports.count ? "EACCES" : outcome.error.code;
45104
+ return { error: refusal2, socket: outcome.socket };
45105
+ });
45106
+ };
45107
+ return attemptFrom(0, 0);
45067
45108
  }
45068
45109
  function probeAccept(dial, timeoutMs, startTimer) {
45069
45110
  return new Promise((resolve3) => {
@@ -45098,7 +45139,7 @@ function runAcceptProbe(deps, data, startTimer) {
45098
45139
  const lookup2 = deps.createDoorLookup({ isIP: deps.net.isIP, resolve4: (name, callback) => {
45099
45140
  deps.dns.resolve4(name, callback);
45100
45141
  } });
45101
- const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP };
45142
+ const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP, sourcePortFailure: deps.sourcePortClassifier(data.platform) };
45102
45143
  return deps.probeAccept(() => deps.doorDial(io, { host: data.host, port: data.port }, new deps.SourcePortRange(data.portStart, data.portEnd, data.portFirst)), data.timeoutMs, startTimer);
45103
45144
  }
45104
45145
  var init_doorDial = __esm({
@@ -45111,14 +45152,17 @@ var init_doorDial = __esm({
45111
45152
  function isSuccessful2(status) {
45112
45153
  return status >= HTTP_STATUS.ok && status < SUCCESS_STATUS_MAX_EXCLUSIVE2;
45113
45154
  }
45155
+ function realDoorDialIo(platform = process.platform, connect3 = (options) => net3.connect(options)) {
45156
+ return { connect: connect3, lookup: realAddressLookup, isIP: net3.isIP, sourcePortFailure: sourcePortClassifier(platform) };
45157
+ }
45114
45158
  function dialTarget(options, defaultPort) {
45115
45159
  return { host: typeof options.host === "string" ? options.host : "", port: typeof options.port === "number" ? options.port : defaultPort };
45116
45160
  }
45117
45161
  function literalDial(target) {
45118
45162
  return net3.connect({ host: target.host, port: target.port });
45119
45163
  }
45120
- async function reservedDial(target, ports) {
45121
- return await doorDial(realDialIo, target, ports);
45164
+ async function reservedDial(target, ports, io) {
45165
+ return await doorDial(io, target, ports);
45122
45166
  }
45123
45167
  function requestServername(options, fallback) {
45124
45168
  return "servername" in options && typeof options.servername === "string" && options.servername !== "" ? options.servername : fallback;
@@ -45258,9 +45302,9 @@ function realRcStreamDial(target = { host: CONNECT_INTERCEPT_HOST, port: HTTPS_P
45258
45302
  }
45259
45303
  };
45260
45304
  }
45261
- function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
45262
- const plainAgent = new ExemptHttpAgent(ports);
45263
- const tlsAgent = new ExemptTlsAgent(ports);
45305
+ function realConnectEffects(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
45306
+ const plainAgent = new ExemptHttpAgent(ports, io);
45307
+ const tlsAgent = new ExemptTlsAgent(ports, io);
45264
45308
  const listenOnce = async (target, onSocket) => await new Promise((resolve3, reject) => {
45265
45309
  const accepted = /* @__PURE__ */ new Set();
45266
45310
  const server = net3.createServer((socket) => {
@@ -45392,7 +45436,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
45392
45436
  };
45393
45437
  },
45394
45438
  connectTlsUpstream: async (host, port, clientAlpn) => {
45395
- const dialled = await reservedDial({ host, port }, ports);
45439
+ const dialled = await reservedDial({ host, port }, ports, io);
45396
45440
  if (dialled.error !== null) {
45397
45441
  throw dialled.error;
45398
45442
  }
@@ -45473,7 +45517,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
45473
45517
  }
45474
45518
  };
45475
45519
  }
45476
- var import_node_dns, http2, https3, net3, tls, SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, realDialIo, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
45520
+ var import_node_dns, http2, https3, net3, tls, SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
45477
45521
  var init_connectEffects = __esm({
45478
45522
  "src/frontdoor/connectEffects.ts"() {
45479
45523
  "use strict";
@@ -45495,13 +45539,28 @@ var init_connectEffects = __esm({
45495
45539
  this.end = end;
45496
45540
  this.count = end - start + 1;
45497
45541
  this.next = first;
45542
+ this.excluded = /* @__PURE__ */ new Set();
45498
45543
  }
45499
- /** The next port in rotation, wrapping from the end back to the start. */
45544
+ /**
45545
+ * The next port in rotation, wrapping from the end back to the start, skipping every port the system has refused to bind (`exclude`) so a dial does not walk a refused block again. A cycle that finds nothing else forgets the refusals and starts over: the exclusions of a host can change while the door runs, and a range refused throughout is then tried again, one pass, not skipped for ever. The memory is a set of ports of the range, so it holds at most `count` entries.
45546
+ */
45500
45547
  take() {
45548
+ for (let skipped = 0; skipped < this.count; skipped += 1) {
45549
+ const port2 = this.next;
45550
+ this.next = this.next === this.end ? this.start : this.next + 1;
45551
+ if (!this.excluded.has(port2)) {
45552
+ return port2;
45553
+ }
45554
+ }
45555
+ this.excluded.clear();
45501
45556
  const port = this.next;
45502
45557
  this.next = this.next === this.end ? this.start : this.next + 1;
45503
45558
  return port;
45504
45559
  }
45560
+ /** Remembers that the system refuses to bind `port`, so `take` skips it. Only an exclusion belongs here: a port a sibling holds is free again when the sibling closes. */
45561
+ exclude(port) {
45562
+ this.excluded.add(port);
45563
+ }
45505
45564
  };
45506
45565
  DOOR_SOURCE_PORT_START = 47900;
45507
45566
  DOOR_SOURCE_PORT_COUNT = 1e3;
@@ -45513,39 +45572,42 @@ var init_connectEffects = __esm({
45513
45572
  realAddressLookup = createDoorLookup({ isIP: net3.isIP, resolve4: (host, callback) => {
45514
45573
  import_node_dns.default.resolve4(host, callback);
45515
45574
  } });
45516
- realDialIo = { connect: (options) => net3.connect(options), lookup: realAddressLookup, isIP: net3.isIP };
45517
45575
  HTTP_PORT = 80;
45518
45576
  ExemptHttpAgent = class extends http2.Agent {
45519
- constructor(ports = DOOR_SOURCE_PORTS) {
45577
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
45520
45578
  super(POOLING);
45521
45579
  this.ports = ports;
45580
+ this.io = io;
45522
45581
  }
45523
45582
  ports;
45583
+ io;
45524
45584
  // A reserved-port dial that has to retry cannot produce its socket synchronously, so it settles first and arrives through the callback (node's documented asynchronous createConnection form); a literal dial has nothing to retry and keeps node's own synchronous shape.
45525
45585
  createConnection(options, oncreate) {
45526
45586
  const target = dialTarget(options, HTTP_PORT);
45527
45587
  if (net3.isIP(target.host) !== 0) {
45528
45588
  return literalDial(target);
45529
45589
  }
45530
- void reservedDial(target, this.ports).then((outcome) => {
45590
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
45531
45591
  oncreate?.(outcome.error, outcome.socket);
45532
45592
  });
45533
45593
  return void 0;
45534
45594
  }
45535
45595
  };
45536
45596
  ExemptTlsAgent = class extends https3.Agent {
45537
- constructor(ports = DOOR_SOURCE_PORTS) {
45597
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
45538
45598
  super(POOLING);
45539
45599
  this.ports = ports;
45600
+ this.io = io;
45540
45601
  }
45541
45602
  ports;
45603
+ io;
45542
45604
  // The same split as the plain sibling: a literal dial wraps TLS around the socket node's own agent would have returned, a reserved dial settles (retries included) first and arrives through the callback, which is exactly where node's own agent wraps its own dials.
45543
45605
  createConnection(options, oncreate) {
45544
45606
  const target = dialTarget(options, HTTPS_PORT);
45545
45607
  if (net3.isIP(target.host) !== 0) {
45546
45608
  return tls.connect({ socket: literalDial(target), servername: requestServername(options, target.host), ...tlsFacts(options) });
45547
45609
  }
45548
- void reservedDial(target, this.ports).then((outcome) => {
45610
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
45549
45611
  if (outcome.error !== null) {
45550
45612
  oncreate?.(outcome.error, outcome.socket);
45551
45613
  return;
@@ -45579,7 +45641,7 @@ function probeFrontDoorSync(port, caPem, timeoutMs = PROBE_TIMEOUT_MS, workers =
45579
45641
  }
45580
45642
  function probeListenerAcceptSync(port, timeoutMs = PROBE_TIMEOUT_MS, workers = realProbeWorkers) {
45581
45643
  refuseToBlockInRole("a synchronous probe of a front door listener");
45582
- return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END }, timeoutMs).verdict();
45644
+ return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, platform: process.platform }, timeoutMs).verdict();
45583
45645
  }
45584
45646
  function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
45585
45647
  refuseToBlockInRole("a synchronous probe of a provider's address");
@@ -45604,7 +45666,7 @@ function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
45604
45666
  }
45605
45667
  const portFirst = DOOR_SOURCE_PORT_START + (base + index * stride) % DOOR_SOURCE_PORT_COUNT;
45606
45668
  try {
45607
- const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst }, target.timeoutMs);
45669
+ const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst, platform: process.platform }, target.timeoutMs);
45608
45670
  running.push({ key: targetKey(target), verdict: started.verdict, cancel: started.cancel });
45609
45671
  } catch (error63) {
45610
45672
  const reason = `${PROBE_WORKER_FAILURE_PREFIX}the worker could not be started: ${error63 instanceof Error ? error63.message : String(error63)}`;
@@ -45779,15 +45841,16 @@ request.end();
45779
45841
  ACCEPT_PROBE_SOURCE = probeWorkerSource(`
45780
45842
  const net = require("node:net");
45781
45843
  const dns = require("node:dns");
45782
- const { host, port, portStart, portEnd, portFirst, timeoutMs, ok, failed } = workerData;
45844
+ const { host, port, portStart, portEnd, portFirst, platform, timeoutMs, ok, failed } = workerData;
45783
45845
  const finish = finishProbe;
45784
45846
  const SourcePortRange = ${SourcePortRange.toString()};
45785
45847
  const createDoorLookup = ${createDoorLookup.toString()};
45848
+ const sourcePortClassifier = ${sourcePortClassifier.toString()};
45786
45849
  const doorDial = ${doorDial.toString()};
45787
45850
  const probeAccept = ${probeAccept.toString()};
45788
45851
  const runAcceptProbe = ${runAcceptProbe.toString()};
45789
45852
  const startTimer = (callback, ms) => { const timer = setTimeout(callback, ms); return () => { clearTimeout(timer); }; };
45790
- runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange }, { host, port, portStart, portEnd, portFirst, timeoutMs }, startTimer).then((verdict) => {
45853
+ runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange, sourcePortClassifier }, { host, port, portStart, portEnd, portFirst, platform, timeoutMs }, startTimer).then((verdict) => {
45791
45854
  finish(verdict.ok ? ok : failed, verdict.ok ? "" : verdict.reason);
45792
45855
  });
45793
45856
  `);
@@ -74381,7 +74444,7 @@ var init_package = __esm({
74381
74444
  "package.json"() {
74382
74445
  package_default = {
74383
74446
  name: "agent-shim",
74384
- version: "14.0.2",
74447
+ version: "14.0.4",
74385
74448
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
74386
74449
  license: "MIT",
74387
74450
  author: "Joseph Mearman <joseph@mearman.co.uk>",
package/dist/index.cjs CHANGED
@@ -8230,6 +8230,20 @@ function createDoorLookup(io) {
8230
8230
  });
8231
8231
  };
8232
8232
  }
8233
+ function sourcePortClassifier(platform) {
8234
+ const classify = (error) => {
8235
+ const attempts = Reflect.get(error, "errors");
8236
+ if (Array.isArray(attempts) && attempts.length > 0) {
8237
+ const kinds = attempts.map((attempt) => attempt instanceof Error ? classify(attempt) : void 0);
8238
+ return kinds.includes("excluded") ? "excluded" : kinds.includes("held") ? "held" : void 0;
8239
+ }
8240
+ if (error.code === "EADDRINUSE" || error.code === "EADDRNOTAVAIL") {
8241
+ return "held";
8242
+ }
8243
+ return platform === "win32" && error.code === "EACCES" && error.syscall === "bind" ? "excluded" : void 0;
8244
+ };
8245
+ return classify;
8246
+ }
8233
8247
  function doorDial(io, target, ports) {
8234
8248
  const dialOnce = (options) => new Promise((resolve) => {
8235
8249
  const socket = io.connect(options);
@@ -8242,12 +8256,15 @@ function doorDial(io, target, ports) {
8242
8256
  if (error.message === "" && Array.isArray(attempts) && attempts.length > 0) {
8243
8257
  const flattened = new Error(attempts.map((attempt) => withoutLocalEnd(attempt instanceof Error ? attempt.message : String(attempt))).join("; "));
8244
8258
  flattened.code = error.code;
8259
+ flattened.syscall = error.syscall;
8260
+ Object.defineProperty(flattened, "errors", { value: attempts });
8245
8261
  resolve({ error: flattened, socket });
8246
8262
  return;
8247
8263
  }
8248
8264
  if (withoutLocalEnd(error.message) !== error.message) {
8249
8265
  const trimmed = new Error(withoutLocalEnd(error.message));
8250
8266
  trimmed.code = error.code;
8267
+ trimmed.syscall = error.syscall;
8251
8268
  resolve({ error: trimmed, socket });
8252
8269
  return;
8253
8270
  }
@@ -8257,17 +8274,35 @@ function doorDial(io, target, ports) {
8257
8274
  if (io.isIP(target.host) !== 0) {
8258
8275
  return dialOnce({ host: target.host, port: target.port });
8259
8276
  }
8260
- const attemptFrom = (attempt) => dialOnce({ host: target.host, port: target.port, localPort: ports.take(), lookup: io.lookup }).then((outcome) => {
8261
- if (outcome.error === null) {
8262
- return outcome;
8263
- }
8264
- const held = outcome.error.code === "EADDRINUSE" || outcome.error.code === "EADDRNOTAVAIL";
8265
- if (!held || attempt === ports.count - 1) {
8266
- return outcome;
8267
- }
8268
- return attemptFrom(attempt + 1);
8269
- });
8270
- return attemptFrom(0);
8277
+ const attemptFrom = (attempt, refused2) => {
8278
+ const port = ports.take();
8279
+ return dialOnce({ host: target.host, port: target.port, localPort: port, lookup: io.lookup }).then((outcome) => {
8280
+ if (outcome.error === null) {
8281
+ return outcome;
8282
+ }
8283
+ const failure = io.sourcePortFailure(outcome.error);
8284
+ if (failure === void 0) {
8285
+ return outcome;
8286
+ }
8287
+ if (failure === "excluded") {
8288
+ ports.exclude(port);
8289
+ }
8290
+ const refusedSoFar = failure === "excluded" ? refused2 + 1 : refused2;
8291
+ if (attempt < ports.count - 1) {
8292
+ return attemptFrom(attempt + 1, refusedSoFar);
8293
+ }
8294
+ if (refusedSoFar === 0) {
8295
+ return outcome;
8296
+ }
8297
+ const scope = refusedSoFar === ports.count ? "every source port" : `${String(refusedSoFar)} of its ${String(ports.count)} source ports (the rest were held by other sockets)`;
8298
+ const refusal2 = new Error(
8299
+ `${scope} of the door's reserved range (${String(ports.start)} to ${String(ports.end)}) could not be bound: Windows refuses a port inside an excluded range with EACCES (Hyper-V, WSL2 and Docker Desktop reserve blocks of ports). List the excluded ranges with \`netsh int ipv4 show excludedportrange protocol=tcp\``
8300
+ );
8301
+ refusal2.code = refusedSoFar === ports.count ? "EACCES" : outcome.error.code;
8302
+ return { error: refusal2, socket: outcome.socket };
8303
+ });
8304
+ };
8305
+ return attemptFrom(0, 0);
8271
8306
  }
8272
8307
  function probeAccept(dial, timeoutMs, startTimer) {
8273
8308
  return new Promise((resolve) => {
@@ -8302,7 +8337,7 @@ function runAcceptProbe(deps, data, startTimer) {
8302
8337
  const lookup2 = deps.createDoorLookup({ isIP: deps.net.isIP, resolve4: (name, callback) => {
8303
8338
  deps.dns.resolve4(name, callback);
8304
8339
  } });
8305
- const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP };
8340
+ const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP, sourcePortFailure: deps.sourcePortClassifier(data.platform) };
8306
8341
  return deps.probeAccept(() => deps.doorDial(io, { host: data.host, port: data.port }, new deps.SourcePortRange(data.portStart, data.portEnd, data.portFirst)), data.timeoutMs, startTimer);
8307
8342
  }
8308
8343
  var init_doorDial = __esm({
@@ -8315,14 +8350,17 @@ var init_doorDial = __esm({
8315
8350
  function isSuccessful2(status) {
8316
8351
  return status >= HTTP_STATUS.ok && status < SUCCESS_STATUS_MAX_EXCLUSIVE2;
8317
8352
  }
8353
+ function realDoorDialIo(platform = process.platform, connect3 = (options) => net2.connect(options)) {
8354
+ return { connect: connect3, lookup: realAddressLookup, isIP: net2.isIP, sourcePortFailure: sourcePortClassifier(platform) };
8355
+ }
8318
8356
  function dialTarget(options, defaultPort) {
8319
8357
  return { host: typeof options.host === "string" ? options.host : "", port: typeof options.port === "number" ? options.port : defaultPort };
8320
8358
  }
8321
8359
  function literalDial(target) {
8322
8360
  return net2.connect({ host: target.host, port: target.port });
8323
8361
  }
8324
- async function reservedDial(target, ports) {
8325
- return await doorDial(realDialIo, target, ports);
8362
+ async function reservedDial(target, ports, io) {
8363
+ return await doorDial(io, target, ports);
8326
8364
  }
8327
8365
  function requestServername(options, fallback) {
8328
8366
  return "servername" in options && typeof options.servername === "string" && options.servername !== "" ? options.servername : fallback;
@@ -8462,9 +8500,9 @@ function realRcStreamDial(target = { host: CONNECT_INTERCEPT_HOST, port: HTTPS_P
8462
8500
  }
8463
8501
  };
8464
8502
  }
8465
- function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8466
- const plainAgent = new ExemptHttpAgent(ports);
8467
- const tlsAgent = new ExemptTlsAgent(ports);
8503
+ function realConnectEffects(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8504
+ const plainAgent = new ExemptHttpAgent(ports, io);
8505
+ const tlsAgent = new ExemptTlsAgent(ports, io);
8468
8506
  const listenOnce = async (target, onSocket) => await new Promise((resolve, reject) => {
8469
8507
  const accepted = /* @__PURE__ */ new Set();
8470
8508
  const server = net2.createServer((socket) => {
@@ -8596,7 +8634,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8596
8634
  };
8597
8635
  },
8598
8636
  connectTlsUpstream: async (host, port, clientAlpn) => {
8599
- const dialled = await reservedDial({ host, port }, ports);
8637
+ const dialled = await reservedDial({ host, port }, ports, io);
8600
8638
  if (dialled.error !== null) {
8601
8639
  throw dialled.error;
8602
8640
  }
@@ -8677,7 +8715,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8677
8715
  }
8678
8716
  };
8679
8717
  }
8680
- var import_node_dns, http, https2, net2, tls, SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, realDialIo, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
8718
+ var import_node_dns, http, https2, net2, tls, SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
8681
8719
  var init_connectEffects = __esm({
8682
8720
  "src/frontdoor/connectEffects.ts"() {
8683
8721
  "use strict";
@@ -8699,13 +8737,28 @@ var init_connectEffects = __esm({
8699
8737
  this.end = end;
8700
8738
  this.count = end - start + 1;
8701
8739
  this.next = first;
8740
+ this.excluded = /* @__PURE__ */ new Set();
8702
8741
  }
8703
- /** The next port in rotation, wrapping from the end back to the start. */
8742
+ /**
8743
+ * The next port in rotation, wrapping from the end back to the start, skipping every port the system has refused to bind (`exclude`) so a dial does not walk a refused block again. A cycle that finds nothing else forgets the refusals and starts over: the exclusions of a host can change while the door runs, and a range refused throughout is then tried again, one pass, not skipped for ever. The memory is a set of ports of the range, so it holds at most `count` entries.
8744
+ */
8704
8745
  take() {
8746
+ for (let skipped = 0; skipped < this.count; skipped += 1) {
8747
+ const port2 = this.next;
8748
+ this.next = this.next === this.end ? this.start : this.next + 1;
8749
+ if (!this.excluded.has(port2)) {
8750
+ return port2;
8751
+ }
8752
+ }
8753
+ this.excluded.clear();
8705
8754
  const port = this.next;
8706
8755
  this.next = this.next === this.end ? this.start : this.next + 1;
8707
8756
  return port;
8708
8757
  }
8758
+ /** Remembers that the system refuses to bind `port`, so `take` skips it. Only an exclusion belongs here: a port a sibling holds is free again when the sibling closes. */
8759
+ exclude(port) {
8760
+ this.excluded.add(port);
8761
+ }
8709
8762
  };
8710
8763
  DOOR_SOURCE_PORT_START = 47900;
8711
8764
  DOOR_SOURCE_PORT_COUNT = 1e3;
@@ -8717,39 +8770,42 @@ var init_connectEffects = __esm({
8717
8770
  realAddressLookup = createDoorLookup({ isIP: net2.isIP, resolve4: (host, callback) => {
8718
8771
  import_node_dns.default.resolve4(host, callback);
8719
8772
  } });
8720
- realDialIo = { connect: (options) => net2.connect(options), lookup: realAddressLookup, isIP: net2.isIP };
8721
8773
  HTTP_PORT = 80;
8722
8774
  ExemptHttpAgent = class extends http.Agent {
8723
- constructor(ports = DOOR_SOURCE_PORTS) {
8775
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8724
8776
  super(POOLING);
8725
8777
  this.ports = ports;
8778
+ this.io = io;
8726
8779
  }
8727
8780
  ports;
8781
+ io;
8728
8782
  // A reserved-port dial that has to retry cannot produce its socket synchronously, so it settles first and arrives through the callback (node's documented asynchronous createConnection form); a literal dial has nothing to retry and keeps node's own synchronous shape.
8729
8783
  createConnection(options, oncreate) {
8730
8784
  const target = dialTarget(options, HTTP_PORT);
8731
8785
  if (net2.isIP(target.host) !== 0) {
8732
8786
  return literalDial(target);
8733
8787
  }
8734
- void reservedDial(target, this.ports).then((outcome) => {
8788
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
8735
8789
  oncreate?.(outcome.error, outcome.socket);
8736
8790
  });
8737
8791
  return void 0;
8738
8792
  }
8739
8793
  };
8740
8794
  ExemptTlsAgent = class extends https2.Agent {
8741
- constructor(ports = DOOR_SOURCE_PORTS) {
8795
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8742
8796
  super(POOLING);
8743
8797
  this.ports = ports;
8798
+ this.io = io;
8744
8799
  }
8745
8800
  ports;
8801
+ io;
8746
8802
  // The same split as the plain sibling: a literal dial wraps TLS around the socket node's own agent would have returned, a reserved dial settles (retries included) first and arrives through the callback, which is exactly where node's own agent wraps its own dials.
8747
8803
  createConnection(options, oncreate) {
8748
8804
  const target = dialTarget(options, HTTPS_PORT);
8749
8805
  if (net2.isIP(target.host) !== 0) {
8750
8806
  return tls.connect({ socket: literalDial(target), servername: requestServername(options, target.host), ...tlsFacts(options) });
8751
8807
  }
8752
- void reservedDial(target, this.ports).then((outcome) => {
8808
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
8753
8809
  if (outcome.error !== null) {
8754
8810
  oncreate?.(outcome.error, outcome.socket);
8755
8811
  return;
@@ -10800,6 +10856,9 @@ function reportableAllowlistFileProblem(allowlistFile, ports, served) {
10800
10856
  return problem?.repairable === false || served ? problem : void 0;
10801
10857
  }
10802
10858
  function verifyAllowlistFile(allowlistFile, ports) {
10859
+ if (platformRefusal(ports.platform) !== void 0) {
10860
+ return void 0;
10861
+ }
10803
10862
  const uid = ports.currentUid();
10804
10863
  const directories = pathDirectories(allowlistFile, ports);
10805
10864
  if (typeof directories === "string") {
@@ -11871,7 +11930,10 @@ var init_realPorts = __esm({
11871
11930
  };
11872
11931
  SOCKET_PERMISSION_BITS = 4095;
11873
11932
  realHeadroomSocketTrust = {
11874
- platform: process.platform,
11933
+ // Read at each use, not fixed when the module loads, so what the checks do for a platform is testable through the real wiring.
11934
+ get platform() {
11935
+ return process.platform;
11936
+ },
11875
11937
  currentUid: () => {
11876
11938
  if (process.getuid === void 0) {
11877
11939
  throw new Error(`no POSIX user id on ${process.platform}`);
@@ -13814,7 +13876,7 @@ var init_package = __esm({
13814
13876
  "package.json"() {
13815
13877
  package_default = {
13816
13878
  name: "agent-shim",
13817
- version: "14.0.2",
13879
+ version: "14.0.4",
13818
13880
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
13819
13881
  license: "MIT",
13820
13882
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -15094,7 +15156,7 @@ function probeFrontDoorSync(port, caPem, timeoutMs = PROBE_TIMEOUT_MS, workers =
15094
15156
  }
15095
15157
  function probeListenerAcceptSync(port, timeoutMs = PROBE_TIMEOUT_MS, workers = realProbeWorkers) {
15096
15158
  refuseToBlockInRole("a synchronous probe of a front door listener");
15097
- return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END }, timeoutMs).verdict();
15159
+ return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, platform: process.platform }, timeoutMs).verdict();
15098
15160
  }
15099
15161
  function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
15100
15162
  refuseToBlockInRole("a synchronous probe of a provider's address");
@@ -15119,7 +15181,7 @@ function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
15119
15181
  }
15120
15182
  const portFirst = DOOR_SOURCE_PORT_START + (base + index * stride) % DOOR_SOURCE_PORT_COUNT;
15121
15183
  try {
15122
- const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst }, target.timeoutMs);
15184
+ const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst, platform: process.platform }, target.timeoutMs);
15123
15185
  running.push({ key: targetKey(target), verdict: started.verdict, cancel: started.cancel });
15124
15186
  } catch (error) {
15125
15187
  const reason = `${PROBE_WORKER_FAILURE_PREFIX}the worker could not be started: ${error instanceof Error ? error.message : String(error)}`;
@@ -15294,15 +15356,16 @@ request.end();
15294
15356
  ACCEPT_PROBE_SOURCE = probeWorkerSource(`
15295
15357
  const net = require("node:net");
15296
15358
  const dns = require("node:dns");
15297
- const { host, port, portStart, portEnd, portFirst, timeoutMs, ok, failed } = workerData;
15359
+ const { host, port, portStart, portEnd, portFirst, platform, timeoutMs, ok, failed } = workerData;
15298
15360
  const finish = finishProbe;
15299
15361
  const SourcePortRange = ${SourcePortRange.toString()};
15300
15362
  const createDoorLookup = ${createDoorLookup.toString()};
15363
+ const sourcePortClassifier = ${sourcePortClassifier.toString()};
15301
15364
  const doorDial = ${doorDial.toString()};
15302
15365
  const probeAccept = ${probeAccept.toString()};
15303
15366
  const runAcceptProbe = ${runAcceptProbe.toString()};
15304
15367
  const startTimer = (callback, ms) => { const timer = setTimeout(callback, ms); return () => { clearTimeout(timer); }; };
15305
- runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange }, { host, port, portStart, portEnd, portFirst, timeoutMs }, startTimer).then((verdict) => {
15368
+ runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange, sourcePortClassifier }, { host, port, portStart, portEnd, portFirst, platform, timeoutMs }, startTimer).then((verdict) => {
15306
15369
  finish(verdict.ok ? ok : failed, verdict.ok ? "" : verdict.reason);
15307
15370
  });
15308
15371
  `);
package/dist/index.mjs CHANGED
@@ -8205,6 +8205,20 @@ function createDoorLookup(io) {
8205
8205
  });
8206
8206
  };
8207
8207
  }
8208
+ function sourcePortClassifier(platform) {
8209
+ const classify = (error) => {
8210
+ const attempts = Reflect.get(error, "errors");
8211
+ if (Array.isArray(attempts) && attempts.length > 0) {
8212
+ const kinds = attempts.map((attempt) => attempt instanceof Error ? classify(attempt) : void 0);
8213
+ return kinds.includes("excluded") ? "excluded" : kinds.includes("held") ? "held" : void 0;
8214
+ }
8215
+ if (error.code === "EADDRINUSE" || error.code === "EADDRNOTAVAIL") {
8216
+ return "held";
8217
+ }
8218
+ return platform === "win32" && error.code === "EACCES" && error.syscall === "bind" ? "excluded" : void 0;
8219
+ };
8220
+ return classify;
8221
+ }
8208
8222
  function doorDial(io, target, ports) {
8209
8223
  const dialOnce = (options) => new Promise((resolve) => {
8210
8224
  const socket = io.connect(options);
@@ -8217,12 +8231,15 @@ function doorDial(io, target, ports) {
8217
8231
  if (error.message === "" && Array.isArray(attempts) && attempts.length > 0) {
8218
8232
  const flattened = new Error(attempts.map((attempt) => withoutLocalEnd(attempt instanceof Error ? attempt.message : String(attempt))).join("; "));
8219
8233
  flattened.code = error.code;
8234
+ flattened.syscall = error.syscall;
8235
+ Object.defineProperty(flattened, "errors", { value: attempts });
8220
8236
  resolve({ error: flattened, socket });
8221
8237
  return;
8222
8238
  }
8223
8239
  if (withoutLocalEnd(error.message) !== error.message) {
8224
8240
  const trimmed = new Error(withoutLocalEnd(error.message));
8225
8241
  trimmed.code = error.code;
8242
+ trimmed.syscall = error.syscall;
8226
8243
  resolve({ error: trimmed, socket });
8227
8244
  return;
8228
8245
  }
@@ -8232,17 +8249,35 @@ function doorDial(io, target, ports) {
8232
8249
  if (io.isIP(target.host) !== 0) {
8233
8250
  return dialOnce({ host: target.host, port: target.port });
8234
8251
  }
8235
- const attemptFrom = (attempt) => dialOnce({ host: target.host, port: target.port, localPort: ports.take(), lookup: io.lookup }).then((outcome) => {
8236
- if (outcome.error === null) {
8237
- return outcome;
8238
- }
8239
- const held = outcome.error.code === "EADDRINUSE" || outcome.error.code === "EADDRNOTAVAIL";
8240
- if (!held || attempt === ports.count - 1) {
8241
- return outcome;
8242
- }
8243
- return attemptFrom(attempt + 1);
8244
- });
8245
- return attemptFrom(0);
8252
+ const attemptFrom = (attempt, refused2) => {
8253
+ const port = ports.take();
8254
+ return dialOnce({ host: target.host, port: target.port, localPort: port, lookup: io.lookup }).then((outcome) => {
8255
+ if (outcome.error === null) {
8256
+ return outcome;
8257
+ }
8258
+ const failure = io.sourcePortFailure(outcome.error);
8259
+ if (failure === void 0) {
8260
+ return outcome;
8261
+ }
8262
+ if (failure === "excluded") {
8263
+ ports.exclude(port);
8264
+ }
8265
+ const refusedSoFar = failure === "excluded" ? refused2 + 1 : refused2;
8266
+ if (attempt < ports.count - 1) {
8267
+ return attemptFrom(attempt + 1, refusedSoFar);
8268
+ }
8269
+ if (refusedSoFar === 0) {
8270
+ return outcome;
8271
+ }
8272
+ const scope = refusedSoFar === ports.count ? "every source port" : `${String(refusedSoFar)} of its ${String(ports.count)} source ports (the rest were held by other sockets)`;
8273
+ const refusal2 = new Error(
8274
+ `${scope} of the door's reserved range (${String(ports.start)} to ${String(ports.end)}) could not be bound: Windows refuses a port inside an excluded range with EACCES (Hyper-V, WSL2 and Docker Desktop reserve blocks of ports). List the excluded ranges with \`netsh int ipv4 show excludedportrange protocol=tcp\``
8275
+ );
8276
+ refusal2.code = refusedSoFar === ports.count ? "EACCES" : outcome.error.code;
8277
+ return { error: refusal2, socket: outcome.socket };
8278
+ });
8279
+ };
8280
+ return attemptFrom(0, 0);
8246
8281
  }
8247
8282
  function probeAccept(dial, timeoutMs, startTimer) {
8248
8283
  return new Promise((resolve) => {
@@ -8277,7 +8312,7 @@ function runAcceptProbe(deps, data, startTimer) {
8277
8312
  const lookup2 = deps.createDoorLookup({ isIP: deps.net.isIP, resolve4: (name, callback) => {
8278
8313
  deps.dns.resolve4(name, callback);
8279
8314
  } });
8280
- const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP };
8315
+ const io = { connect: (options) => deps.net.connect(options), lookup: lookup2, isIP: deps.net.isIP, sourcePortFailure: deps.sourcePortClassifier(data.platform) };
8281
8316
  return deps.probeAccept(() => deps.doorDial(io, { host: data.host, port: data.port }, new deps.SourcePortRange(data.portStart, data.portEnd, data.portFirst)), data.timeoutMs, startTimer);
8282
8317
  }
8283
8318
  var init_doorDial = __esm({
@@ -8295,14 +8330,17 @@ import * as tls from "node:tls";
8295
8330
  function isSuccessful2(status) {
8296
8331
  return status >= HTTP_STATUS.ok && status < SUCCESS_STATUS_MAX_EXCLUSIVE2;
8297
8332
  }
8333
+ function realDoorDialIo(platform = process.platform, connect3 = (options) => net2.connect(options)) {
8334
+ return { connect: connect3, lookup: realAddressLookup, isIP: net2.isIP, sourcePortFailure: sourcePortClassifier(platform) };
8335
+ }
8298
8336
  function dialTarget(options, defaultPort) {
8299
8337
  return { host: typeof options.host === "string" ? options.host : "", port: typeof options.port === "number" ? options.port : defaultPort };
8300
8338
  }
8301
8339
  function literalDial(target) {
8302
8340
  return net2.connect({ host: target.host, port: target.port });
8303
8341
  }
8304
- async function reservedDial(target, ports) {
8305
- return await doorDial(realDialIo, target, ports);
8342
+ async function reservedDial(target, ports, io) {
8343
+ return await doorDial(io, target, ports);
8306
8344
  }
8307
8345
  function requestServername(options, fallback) {
8308
8346
  return "servername" in options && typeof options.servername === "string" && options.servername !== "" ? options.servername : fallback;
@@ -8442,9 +8480,9 @@ function realRcStreamDial(target = { host: CONNECT_INTERCEPT_HOST, port: HTTPS_P
8442
8480
  }
8443
8481
  };
8444
8482
  }
8445
- function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8446
- const plainAgent = new ExemptHttpAgent(ports);
8447
- const tlsAgent = new ExemptTlsAgent(ports);
8483
+ function realConnectEffects(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8484
+ const plainAgent = new ExemptHttpAgent(ports, io);
8485
+ const tlsAgent = new ExemptTlsAgent(ports, io);
8448
8486
  const listenOnce = async (target, onSocket) => await new Promise((resolve, reject) => {
8449
8487
  const accepted = /* @__PURE__ */ new Set();
8450
8488
  const server = net2.createServer((socket) => {
@@ -8576,7 +8614,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8576
8614
  };
8577
8615
  },
8578
8616
  connectTlsUpstream: async (host, port, clientAlpn) => {
8579
- const dialled = await reservedDial({ host, port }, ports);
8617
+ const dialled = await reservedDial({ host, port }, ports, io);
8580
8618
  if (dialled.error !== null) {
8581
8619
  throw dialled.error;
8582
8620
  }
@@ -8657,7 +8695,7 @@ function realConnectEffects(ports = DOOR_SOURCE_PORTS) {
8657
8695
  }
8658
8696
  };
8659
8697
  }
8660
- var SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, realDialIo, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
8698
+ var SUCCESS_STATUS_MAX_EXCLUSIVE2, SourcePortRange, DOOR_SOURCE_PORT_START, DOOR_SOURCE_PORT_COUNT, DOOR_SOURCE_PORT_END, DOOR_SOURCE_PORTS, POOLED_SOCKET_IDLE_MS, POOLED_SOCKETS_KEPT_IDLE, POOLING, realAddressLookup, HTTP_PORT, ExemptHttpAgent, ExemptTlsAgent, NO_CHUNKS;
8661
8699
  var init_connectEffects = __esm({
8662
8700
  "src/frontdoor/connectEffects.ts"() {
8663
8701
  "use strict";
@@ -8674,13 +8712,28 @@ var init_connectEffects = __esm({
8674
8712
  this.end = end;
8675
8713
  this.count = end - start + 1;
8676
8714
  this.next = first;
8715
+ this.excluded = /* @__PURE__ */ new Set();
8677
8716
  }
8678
- /** The next port in rotation, wrapping from the end back to the start. */
8717
+ /**
8718
+ * The next port in rotation, wrapping from the end back to the start, skipping every port the system has refused to bind (`exclude`) so a dial does not walk a refused block again. A cycle that finds nothing else forgets the refusals and starts over: the exclusions of a host can change while the door runs, and a range refused throughout is then tried again, one pass, not skipped for ever. The memory is a set of ports of the range, so it holds at most `count` entries.
8719
+ */
8679
8720
  take() {
8721
+ for (let skipped = 0; skipped < this.count; skipped += 1) {
8722
+ const port2 = this.next;
8723
+ this.next = this.next === this.end ? this.start : this.next + 1;
8724
+ if (!this.excluded.has(port2)) {
8725
+ return port2;
8726
+ }
8727
+ }
8728
+ this.excluded.clear();
8680
8729
  const port = this.next;
8681
8730
  this.next = this.next === this.end ? this.start : this.next + 1;
8682
8731
  return port;
8683
8732
  }
8733
+ /** Remembers that the system refuses to bind `port`, so `take` skips it. Only an exclusion belongs here: a port a sibling holds is free again when the sibling closes. */
8734
+ exclude(port) {
8735
+ this.excluded.add(port);
8736
+ }
8684
8737
  };
8685
8738
  DOOR_SOURCE_PORT_START = 47900;
8686
8739
  DOOR_SOURCE_PORT_COUNT = 1e3;
@@ -8692,39 +8745,42 @@ var init_connectEffects = __esm({
8692
8745
  realAddressLookup = createDoorLookup({ isIP: net2.isIP, resolve4: (host, callback) => {
8693
8746
  dns.resolve4(host, callback);
8694
8747
  } });
8695
- realDialIo = { connect: (options) => net2.connect(options), lookup: realAddressLookup, isIP: net2.isIP };
8696
8748
  HTTP_PORT = 80;
8697
8749
  ExemptHttpAgent = class extends http.Agent {
8698
- constructor(ports = DOOR_SOURCE_PORTS) {
8750
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8699
8751
  super(POOLING);
8700
8752
  this.ports = ports;
8753
+ this.io = io;
8701
8754
  }
8702
8755
  ports;
8756
+ io;
8703
8757
  // A reserved-port dial that has to retry cannot produce its socket synchronously, so it settles first and arrives through the callback (node's documented asynchronous createConnection form); a literal dial has nothing to retry and keeps node's own synchronous shape.
8704
8758
  createConnection(options, oncreate) {
8705
8759
  const target = dialTarget(options, HTTP_PORT);
8706
8760
  if (net2.isIP(target.host) !== 0) {
8707
8761
  return literalDial(target);
8708
8762
  }
8709
- void reservedDial(target, this.ports).then((outcome) => {
8763
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
8710
8764
  oncreate?.(outcome.error, outcome.socket);
8711
8765
  });
8712
8766
  return void 0;
8713
8767
  }
8714
8768
  };
8715
8769
  ExemptTlsAgent = class extends https2.Agent {
8716
- constructor(ports = DOOR_SOURCE_PORTS) {
8770
+ constructor(ports = DOOR_SOURCE_PORTS, io = realDoorDialIo()) {
8717
8771
  super(POOLING);
8718
8772
  this.ports = ports;
8773
+ this.io = io;
8719
8774
  }
8720
8775
  ports;
8776
+ io;
8721
8777
  // The same split as the plain sibling: a literal dial wraps TLS around the socket node's own agent would have returned, a reserved dial settles (retries included) first and arrives through the callback, which is exactly where node's own agent wraps its own dials.
8722
8778
  createConnection(options, oncreate) {
8723
8779
  const target = dialTarget(options, HTTPS_PORT);
8724
8780
  if (net2.isIP(target.host) !== 0) {
8725
8781
  return tls.connect({ socket: literalDial(target), servername: requestServername(options, target.host), ...tlsFacts(options) });
8726
8782
  }
8727
- void reservedDial(target, this.ports).then((outcome) => {
8783
+ void reservedDial(target, this.ports, this.io).then((outcome) => {
8728
8784
  if (outcome.error !== null) {
8729
8785
  oncreate?.(outcome.error, outcome.socket);
8730
8786
  return;
@@ -10774,6 +10830,9 @@ function reportableAllowlistFileProblem(allowlistFile, ports, served) {
10774
10830
  return problem?.repairable === false || served ? problem : void 0;
10775
10831
  }
10776
10832
  function verifyAllowlistFile(allowlistFile, ports) {
10833
+ if (platformRefusal(ports.platform) !== void 0) {
10834
+ return void 0;
10835
+ }
10777
10836
  const uid = ports.currentUid();
10778
10837
  const directories = pathDirectories(allowlistFile, ports);
10779
10838
  if (typeof directories === "string") {
@@ -11844,7 +11903,10 @@ var init_realPorts = __esm({
11844
11903
  };
11845
11904
  SOCKET_PERMISSION_BITS = 4095;
11846
11905
  realHeadroomSocketTrust = {
11847
- platform: process.platform,
11906
+ // Read at each use, not fixed when the module loads, so what the checks do for a platform is testable through the real wiring.
11907
+ get platform() {
11908
+ return process.platform;
11909
+ },
11848
11910
  currentUid: () => {
11849
11911
  if (process.getuid === void 0) {
11850
11912
  throw new Error(`no POSIX user id on ${process.platform}`);
@@ -13787,7 +13849,7 @@ var init_package = __esm({
13787
13849
  "package.json"() {
13788
13850
  package_default = {
13789
13851
  name: "agent-shim",
13790
- version: "14.0.2",
13852
+ version: "14.0.4",
13791
13853
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
13792
13854
  license: "MIT",
13793
13855
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -15066,7 +15128,7 @@ function probeFrontDoorSync(port, caPem, timeoutMs = PROBE_TIMEOUT_MS, workers =
15066
15128
  }
15067
15129
  function probeListenerAcceptSync(port, timeoutMs = PROBE_TIMEOUT_MS, workers = realProbeWorkers) {
15068
15130
  refuseToBlockInRole("a synchronous probe of a front door listener");
15069
- return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END }, timeoutMs).verdict();
15131
+ return workers.start(ACCEPT_PROBE_SOURCE, { host: "127.0.0.1", port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, platform: process.platform }, timeoutMs).verdict();
15070
15132
  }
15071
15133
  function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
15072
15134
  refuseToBlockInRole("a synchronous probe of a provider's address");
@@ -15091,7 +15153,7 @@ function startAddressProbes(targets, workers = realProbeWorkers, portBase) {
15091
15153
  }
15092
15154
  const portFirst = DOOR_SOURCE_PORT_START + (base + index * stride) % DOOR_SOURCE_PORT_COUNT;
15093
15155
  try {
15094
- const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst }, target.timeoutMs);
15156
+ const started = workers.start(ACCEPT_PROBE_SOURCE, { host: target.host, port: target.port, portStart: DOOR_SOURCE_PORT_START, portEnd: DOOR_SOURCE_PORT_END, portFirst, platform: process.platform }, target.timeoutMs);
15095
15157
  running.push({ key: targetKey(target), verdict: started.verdict, cancel: started.cancel });
15096
15158
  } catch (error) {
15097
15159
  const reason = `${PROBE_WORKER_FAILURE_PREFIX}the worker could not be started: ${error instanceof Error ? error.message : String(error)}`;
@@ -15265,15 +15327,16 @@ request.end();
15265
15327
  ACCEPT_PROBE_SOURCE = probeWorkerSource(`
15266
15328
  const net = require("node:net");
15267
15329
  const dns = require("node:dns");
15268
- const { host, port, portStart, portEnd, portFirst, timeoutMs, ok, failed } = workerData;
15330
+ const { host, port, portStart, portEnd, portFirst, platform, timeoutMs, ok, failed } = workerData;
15269
15331
  const finish = finishProbe;
15270
15332
  const SourcePortRange = ${SourcePortRange.toString()};
15271
15333
  const createDoorLookup = ${createDoorLookup.toString()};
15334
+ const sourcePortClassifier = ${sourcePortClassifier.toString()};
15272
15335
  const doorDial = ${doorDial.toString()};
15273
15336
  const probeAccept = ${probeAccept.toString()};
15274
15337
  const runAcceptProbe = ${runAcceptProbe.toString()};
15275
15338
  const startTimer = (callback, ms) => { const timer = setTimeout(callback, ms); return () => { clearTimeout(timer); }; };
15276
- runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange }, { host, port, portStart, portEnd, portFirst, timeoutMs }, startTimer).then((verdict) => {
15339
+ runAcceptProbe({ net, dns, createDoorLookup, doorDial, probeAccept, SourcePortRange, sourcePortClassifier }, { host, port, portStart, portEnd, portFirst, platform, timeoutMs }, startTimer).then((verdict) => {
15277
15340
  finish(verdict.ok ? ok : failed, verdict.ok ? "" : verdict.reason);
15278
15341
  });
15279
15342
  `);
@@ -4,7 +4,7 @@ import * as zod from 'zod';
4
4
  import { z } from 'zod';
5
5
  import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
6
6
  import * as net from 'node:net';
7
- import net__default from 'node:net';
7
+ import net__default, { LookupFunction } from 'node:net';
8
8
  import { Duplex, Readable } from 'node:stream';
9
9
  import * as _orpc_client from '@orpc/client';
10
10
  import * as zod_v4_core from 'zod/v4/core';
@@ -7859,6 +7859,27 @@ interface RcStreamHub {
7859
7859
  /** Creates the attachment hub. One per door process; everything it holds dies with it. */
7860
7860
  declare function createRcStreamHub(deps: RcStreamHubDeps): RcStreamHub;
7861
7861
 
7862
+ /** What a dialled socket must offer the dial: its lifecycle events, and closing it. */
7863
+ interface DialSocket {
7864
+ readonly once: ((event: "connect", listener: () => void) => unknown) & ((event: "error", listener: (error: NodeJS.ErrnoException) => void) => unknown);
7865
+ readonly destroy: () => unknown;
7866
+ }
7867
+ /** What the dial needs: a connect, the door's lookup, and how to tell an address literal from a name. */
7868
+ interface DoorDialIo<S extends DialSocket> {
7869
+ readonly connect: (options: {
7870
+ readonly host: string;
7871
+ readonly port: number;
7872
+ readonly localPort?: number;
7873
+ readonly lookup?: LookupFunction;
7874
+ }) => S;
7875
+ readonly lookup: LookupFunction;
7876
+ readonly isIP: (host: string) => number;
7877
+ /** Why a failed dial from a reserved source port failed because of that port, so the next port of the rotation is tried, or undefined when it failed for any other reason: see `sourcePortClassifier`. Injected so the dial reads no platform of its own. */
7878
+ readonly sourcePortFailure: (error: NodeJS.ErrnoException) => SourcePortFailure | undefined;
7879
+ }
7880
+ /** Why a source port could not be used: another socket holds it (`held`, which passes as that socket closes), or the operating system refuses to bind it at all (`excluded`, which holds until the system's configuration changes). */
7881
+ type SourcePortFailure = "held" | "excluded";
7882
+
7862
7883
  /** The real `ConnectEffects` over node's own net, tls, and http. `connectTcp` opens real connections to the CONNECTed host, so tests that want a blind tunnel redirect it. */
7863
7884
  /**
7864
7885
  * A range of local source ports the door's own upstream sockets bind, handed out in rotation because node binds one port per socket with no range option. The width is also the dial's retry bound: one attempt per port, so a fully-held range surfaces its last bind error instead of spinning. Injectable so the exhaustion tests hold a private range instead of racing every other worker for the door's real one.
@@ -7868,10 +7889,15 @@ declare class SourcePortRange {
7868
7889
  readonly end: number;
7869
7890
  readonly count: number;
7870
7891
  private next;
7892
+ private readonly excluded;
7871
7893
  /** `first` is the port the rotation hands out first, `start` unless a caller gives its own: several rotations that serve one purpose at once (the probes of a batch) start apart, so they do not all bind the first port of the range and walk up it together. */
7872
7894
  constructor(start: number, end: number, first?: number);
7873
- /** The next port in rotation, wrapping from the end back to the start. */
7895
+ /**
7896
+ * The next port in rotation, wrapping from the end back to the start, skipping every port the system has refused to bind (`exclude`) so a dial does not walk a refused block again. A cycle that finds nothing else forgets the refusals and starts over: the exclusions of a host can change while the door runs, and a range refused throughout is then tried again, one pass, not skipped for ever. The memory is a set of ports of the range, so it holds at most `count` entries.
7897
+ */
7874
7898
  take(): number;
7899
+ /** Remembers that the system refuses to bind `port`, so `take` skips it. Only an exclusion belongs here: a port a sibling holds is free again when the sibling closes. */
7900
+ exclude(port: number): void;
7875
7901
  }
7876
7902
  /** Where the Remote Control inject path dials: the API host's own address by default, over TLS with the door's interception-proof agent (real-name DNS resolution and the pf-exempt source-port range), so an injected event cannot be looped back into the door's own transparent surface. A test redirects the dial at a local stand-in presenting the host's name, exactly as the forwarding targets are redirected. */
7877
7903
  interface RcDialTarget {
@@ -7898,7 +7924,7 @@ declare function lateRcEventDial(resolve: () => RcDialTarget | undefined): RcEve
7898
7924
  declare function lateRcStreamDial(resolve: () => RcDialTarget | undefined): RcStreamDial;
7899
7925
  declare function realRcStreamDial(target?: RcDialTarget): RcStreamDial;
7900
7926
  /** The real effects, dialling upstream from `ports` (the door's own range unless a test injects a private one). */
7901
- declare function realConnectEffects(ports?: SourcePortRange): ConnectEffects;
7927
+ declare function realConnectEffects(ports?: SourcePortRange, io?: DoorDialIo<net.Socket>): ConnectEffects;
7902
7928
 
7903
7929
  /**
7904
7930
  * The agent tree reader: pure over an injected filesystem, so the CLI, the library surface and tests read the same way, and nothing here writes. The layout it reads is documented in `docs/agent-transcripts.md`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-shim",
3
- "version": "14.0.2",
3
+ "version": "14.0.4",
4
4
  "description": "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
5
5
  "license": "MIT",
6
6
  "author": "Joseph Mearman <joseph@mearman.co.uk>",