@aibexhq/aibex-link 0.0.5 → 0.0.7

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/index.js CHANGED
@@ -1,24 +1,10 @@
1
- import { randomBytes } from "node:crypto";
2
- import { readFileSync, mkdirSync, writeFileSync, chmodSync, renameSync, rmSync, existsSync } from "node:fs";
3
1
  import path from "node:path";
4
- import { platform, arch, release } from "node:os";
2
+ import { readFileSync, mkdirSync, writeFileSync, chmodSync, renameSync, rmSync, existsSync, readdirSync, statSync, appendFileSync, openSync, readSync, closeSync, cpSync, accessSync, constants } from "node:fs";
3
+ import { randomBytes, createHash } from "node:crypto";
4
+ import { platform, arch, release, homedir } from "node:os";
5
5
  import { fileURLToPath } from "node:url";
6
- const DEFAULT_ALIVE_INTERVAL_MS = 15e3;
7
- const DEFAULT_POLL_BACKOFF_INITIAL_MS = 5e3;
8
- const DEFAULT_POLL_BACKOFF_MAX_MS = 5 * 6e4;
9
- function timing(key, fallback, env = process.env) {
10
- const raw = env.AIBEX_TIMINGS;
11
- if (!raw) return fallback;
12
- try {
13
- const value = JSON.parse(raw)[key];
14
- return typeof value === "number" && value > 0 ? value : fallback;
15
- } catch {
16
- return fallback;
17
- }
18
- }
19
- function aliveIntervalMs(env = process.env) {
20
- return timing("aliveIntervalMs", DEFAULT_ALIVE_INTERVAL_MS, env);
21
- }
6
+ import { createConnection } from "node:net";
7
+ import { execFile } from "node:child_process";
22
8
  const encoder = new TextEncoder();
23
9
  function utf8(text) {
24
10
  return new Uint8Array(encoder.encode(text));
@@ -438,6 +424,24 @@ function responseComponents(requestSigned) {
438
424
  }
439
425
  return components;
440
426
  }
427
+ const PROBLEMS = {
428
+ "invalid-request": { status: 400, title: "The request does not match the protocol" },
429
+ "join-key-invalid": { status: 401, title: "The join key is unknown, spent or expired" },
430
+ "machine-present": { status: 409, title: "That machine is still present; it can only be resumed once it is offline" },
431
+ "signature-invalid": { status: 401, title: "The request signature does not verify" },
432
+ "signature-expired": { status: 401, title: "The request signature was created too long ago or in the future" },
433
+ "nonce-reused": { status: 401, title: "The request nonce was already used" },
434
+ "lease-fenced": { status: 409, title: "The lease is from an older epoch; stop the session and write nothing more for it" },
435
+ "transcript-offset": { status: 409, title: "The append does not start where the transcript ends; next_offset says where it does" }
436
+ };
437
+ const PROBLEM_BASE = "https://aibex.com/problems/";
438
+ function problemName(value) {
439
+ if (typeof value.type !== "string" || !value.type.startsWith(PROBLEM_BASE)) {
440
+ return void 0;
441
+ }
442
+ const name = value.type.slice(PROBLEM_BASE.length);
443
+ return name in PROBLEMS ? name : void 0;
444
+ }
441
445
  function randomNonce() {
442
446
  return toBase64Url(crypto.getRandomValues(new Uint8Array(16)));
443
447
  }
@@ -492,6 +496,49 @@ async function verifyResponse(options) {
492
496
  return false;
493
497
  }
494
498
  }
499
+ function isRecord$1(value) {
500
+ return typeof value === "object" && value !== null && !Array.isArray(value);
501
+ }
502
+ async function openEnvelope(envelope, trust) {
503
+ if (!isRecord$1(envelope) || typeof envelope.signer !== "string" || typeof envelope.key_id !== "string" || typeof envelope.payload !== "string" || typeof envelope.signature !== "string") {
504
+ return { ok: false, reason: "malformed" };
505
+ }
506
+ const { signer, key_id: keyId } = envelope;
507
+ const key = trust.keys.find(
508
+ (candidate) => candidate.principal === signer && candidate.keyId === keyId
509
+ );
510
+ if (!key) return { ok: false, reason: "unknown-key" };
511
+ let bytes;
512
+ let verified = false;
513
+ try {
514
+ bytes = fromBase64Url(envelope.payload);
515
+ verified = await verifyBytes(
516
+ await importSignPublicKey(key.publicKey),
517
+ fromBase64Url(envelope.signature),
518
+ bytes
519
+ );
520
+ } catch {
521
+ return { ok: false, reason: "bad-signature" };
522
+ }
523
+ if (!verified) return { ok: false, reason: "bad-signature" };
524
+ let payload;
525
+ try {
526
+ payload = JSON.parse(
527
+ new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }).decode(bytes)
528
+ );
529
+ } catch {
530
+ return { ok: false, reason: "malformed" };
531
+ }
532
+ if (!isRecord$1(payload)) return { ok: false, reason: "malformed" };
533
+ if (trust.accept.includes(signer)) {
534
+ return { ok: true, payload, signer, requester: signer };
535
+ }
536
+ const requester = typeof payload.requester === "string" ? payload.requester : payload.from;
537
+ if (trust.integrations.includes(signer) && typeof requester === "string" && trust.accept.includes(requester)) {
538
+ return { ok: true, payload, signer, requester };
539
+ }
540
+ return { ok: false, reason: "not-accepted" };
541
+ }
495
542
  function isRecord(value) {
496
543
  return typeof value === "object" && value !== null;
497
544
  }
@@ -500,7 +547,7 @@ function isKeyPair(value) {
500
547
  }
501
548
  function parseIdentity(value) {
502
549
  if (!isRecord(value) || value.version !== 1) return void 0;
503
- const strings = [
550
+ const strings2 = [
504
551
  "host",
505
552
  "machine_id",
506
553
  "herd_id",
@@ -510,7 +557,7 @@ function parseIdentity(value) {
510
557
  "next_join_key",
511
558
  "joined_at"
512
559
  ];
513
- if (strings.some((key) => typeof value[key] !== "string")) return void 0;
560
+ if (strings2.some((key) => typeof value[key] !== "string")) return void 0;
514
561
  if (!isRecord(value.certificate) || !isRecord(value.keys)) return void 0;
515
562
  if (!isKeyPair(value.keys.sign) || !isKeyPair(value.keys.encrypt)) {
516
563
  return void 0;
@@ -562,6 +609,158 @@ function writeJsonAtomic(file, value, mode = 384) {
562
609
  function readIdentity(hostDirectory) {
563
610
  return parseIdentity(readJson(path.join(hostDirectory, IDENTITY_FILE)));
564
611
  }
612
+ const DASHBOARD = "app:dashboard";
613
+ const strings = (value) => Array.isArray(value) && value.every((item) => typeof item === "string") ? value : void 0;
614
+ function parseAccess(value) {
615
+ if (typeof value !== "object" || value === null) return void 0;
616
+ const record = value;
617
+ const accept = strings(record.accept);
618
+ const integrations = strings(record.integrations ?? []);
619
+ const keys = Array.isArray(record.keys) ? record.keys : void 0;
620
+ if (!accept || !integrations || !keys) return void 0;
621
+ const parsed = [];
622
+ for (const key of keys) {
623
+ const entry = key;
624
+ if (typeof entry?.principal !== "string" || typeof entry.key_id !== "string" || typeof entry.public_key !== "string") {
625
+ return void 0;
626
+ }
627
+ parsed.push({
628
+ principal: entry.principal,
629
+ key_id: entry.key_id,
630
+ public_key: entry.public_key
631
+ });
632
+ }
633
+ return { accept, integrations, keys: parsed };
634
+ }
635
+ function compare(file, trust) {
636
+ const problems = [];
637
+ if (!file.accept.includes(trust.owner)) {
638
+ problems.push(`the herd owner ${trust.owner} is not accepted`);
639
+ }
640
+ const pinned = file.keys.some(
641
+ (key) => key.principal === DASHBOARD && key.key_id === trust.dashboard.key_id && key.public_key === trust.dashboard.public_key
642
+ );
643
+ if (!pinned) {
644
+ problems.push(
645
+ `the dashboard key ${trust.dashboard.key_id} is not the pinned one`
646
+ );
647
+ }
648
+ return problems.length > 0 ? problems.join(", and ") : void 0;
649
+ }
650
+ class Access {
651
+ constructor(file, now = Date.now) {
652
+ this.file = file;
653
+ this.now = now;
654
+ }
655
+ /** Why the herd's latest `trust` is not what the file trusts, if it is not. */
656
+ mismatch;
657
+ read() {
658
+ return parseAccess(readJson(this.file));
659
+ }
660
+ /** Pins `trust` when there is no file yet, else compares it with the file. */
661
+ observe(trust) {
662
+ if (trust === void 0) return;
663
+ if (!existsSync(this.file)) {
664
+ writeJsonAtomic(this.file, {
665
+ accept: [trust.owner],
666
+ integrations: [DASHBOARD],
667
+ keys: [
668
+ {
669
+ principal: DASHBOARD,
670
+ key_id: trust.dashboard.key_id,
671
+ public_key: trust.dashboard.public_key
672
+ }
673
+ ],
674
+ pinned_at: new Date(this.now()).toISOString()
675
+ });
676
+ this.mismatch = void 0;
677
+ return;
678
+ }
679
+ const file = this.read();
680
+ this.mismatch = file ? compare(file, trust) : "access.json cannot be read, so no work is accepted";
681
+ }
682
+ /** Verifies an envelope against the file; nothing opens without one. */
683
+ open(envelope) {
684
+ const file = this.read();
685
+ const trust = file ? {
686
+ accept: file.accept,
687
+ integrations: file.integrations,
688
+ keys: file.keys.map((key) => ({
689
+ principal: key.principal,
690
+ keyId: key.key_id,
691
+ publicKey: key.public_key
692
+ }))
693
+ } : { accept: [], integrations: [], keys: [] };
694
+ return openEnvelope(envelope, trust);
695
+ }
696
+ /** The heartbeat's FYI copy of the file. */
697
+ heartbeat() {
698
+ const file = this.read();
699
+ if (!file) return void 0;
700
+ return { accept: file.accept, integrations: file.integrations };
701
+ }
702
+ }
703
+ const ACTIVATIONS_FILE = "activations.json";
704
+ const ACTIVATION_TTL_MS = 10 * 60 * 1e3;
705
+ const ACTIVATION_CODE = /^[0-9A-HJKMNP-TV-Z]{8}$/u;
706
+ function isActivation(value) {
707
+ if (typeof value !== "object" || value === null) return false;
708
+ const { code, at } = value;
709
+ return typeof code === "string" && ACTIVATION_CODE.test(code) && typeof at === "string" && !Number.isNaN(Date.parse(at));
710
+ }
711
+ class Activations {
712
+ constructor(hostDirectory, now = Date.now) {
713
+ this.now = now;
714
+ this.file = path.join(hostDirectory, ACTIVATIONS_FILE);
715
+ }
716
+ file;
717
+ /** Keeps a code to report; false when it is not one. */
718
+ add(code) {
719
+ if (!ACTIVATION_CODE.test(code)) return false;
720
+ const kept = this.read().filter((entry) => entry.code !== code);
721
+ this.write([...kept, { code, at: new Date(this.now()).toISOString() }]);
722
+ return true;
723
+ }
724
+ /** The codes to report now, forgetting those 10 minutes old. */
725
+ list() {
726
+ const all = this.read();
727
+ const fresh = all.filter(
728
+ (entry) => this.now() - Date.parse(entry.at) < ACTIVATION_TTL_MS
729
+ );
730
+ if (fresh.length !== all.length) this.write(fresh);
731
+ return fresh;
732
+ }
733
+ /** Forgets the codes a poll answer confirmed. */
734
+ confirm(codes) {
735
+ if (!codes || codes.length === 0) return;
736
+ const all = this.read();
737
+ const left = all.filter((entry) => !codes.includes(entry.code));
738
+ if (left.length !== all.length) this.write(left);
739
+ }
740
+ read() {
741
+ const value = readJson(this.file);
742
+ return Array.isArray(value) ? value.filter((entry) => isActivation(entry)) : [];
743
+ }
744
+ write(entries) {
745
+ writeJsonAtomic(this.file, entries);
746
+ }
747
+ }
748
+ const DEFAULT_ALIVE_INTERVAL_MS = 15e3;
749
+ const DEFAULT_POLL_BACKOFF_INITIAL_MS = 5e3;
750
+ const DEFAULT_POLL_BACKOFF_MAX_MS = 5 * 6e4;
751
+ function timing(key, fallback, env = process.env) {
752
+ const raw = env.AIBEX_TIMINGS;
753
+ if (!raw) return fallback;
754
+ try {
755
+ const value = JSON.parse(raw)[key];
756
+ return typeof value === "number" && value > 0 ? value : fallback;
757
+ } catch {
758
+ return fallback;
759
+ }
760
+ }
761
+ function aliveIntervalMs(env = process.env) {
762
+ return timing("aliveIntervalMs", DEFAULT_ALIVE_INTERVAL_MS, env);
763
+ }
565
764
  const REMEMBER_MS = 60 * 6e4;
566
765
  class HandledCommands {
567
766
  constructor(hostDirectory, now = Date.now) {
@@ -616,6 +815,24 @@ class HandledCommands {
616
815
  writeJsonAtomic(this.file, this.stored);
617
816
  }
618
817
  }
818
+ const DEFAULT_SLOTS = 2;
819
+ function linkPaths(hostDirectory) {
820
+ const state = path.resolve(hostDirectory, "..", "..");
821
+ return {
822
+ hostDirectory,
823
+ state,
824
+ repos: path.join(state, "repos"),
825
+ work: path.join(state, "work"),
826
+ reposConfig: path.join(state, "repos.json"),
827
+ config: path.join(state, "config.json"),
828
+ access: path.join(hostDirectory, "access.json")
829
+ };
830
+ }
831
+ function configuredSlots(paths) {
832
+ const value = readJson(paths.config);
833
+ const slots = value?.slots;
834
+ return typeof slots === "number" && Number.isInteger(slots) && slots >= 0 && slots <= 64 ? slots : DEFAULT_SLOTS;
835
+ }
619
836
  const HARNESS_COMMANDS = {
620
837
  claude: "claude",
621
838
  codex: "codex",
@@ -647,6 +864,32 @@ function describeMachine() {
647
864
  }
648
865
  };
649
866
  }
867
+ const EMPTY_REPORT = {
868
+ capacity: { slots_total: 0, slots_free: 0 },
869
+ sessions: []
870
+ };
871
+ async function collectReport(options) {
872
+ const sessions = options.sessions ?? [];
873
+ const slots = configuredSlots(options.paths);
874
+ const running = sessions.filter((session) => session.state !== "ended");
875
+ const checkouts = await options.repos.list();
876
+ const report = {
877
+ repos: checkouts.map((checkout) => checkout.repo),
878
+ capacity: {
879
+ slots_total: slots,
880
+ slots_free: Math.max(0, slots - running.length)
881
+ },
882
+ sessions,
883
+ features: options.features
884
+ };
885
+ const access = options.access.heartbeat();
886
+ if (access) report.access = access;
887
+ if (options.setupTerminals) report.setup_terminals = options.setupTerminals;
888
+ if (options.activations && options.activations.length > 0) {
889
+ report.activations = options.activations;
890
+ }
891
+ return report;
892
+ }
650
893
  function buildHeartbeat(input) {
651
894
  const machine = describeMachine();
652
895
  const heartbeat = {
@@ -660,10 +903,16 @@ function buildHeartbeat(input) {
660
903
  runner: machine.runner,
661
904
  os: machine.os,
662
905
  harnesses: input.harnesses,
663
- capacity: { slots_total: 0, slots_free: 0 },
664
- sessions: [],
906
+ capacity: input.report.capacity,
907
+ sessions: input.report.sessions,
665
908
  acks: input.acks
666
909
  };
910
+ const { repos, access, features, setup_terminals, activations } = input.report;
911
+ if (repos) heartbeat.repos = repos;
912
+ if (access) heartbeat.access = access;
913
+ if (features) heartbeat.features = features;
914
+ if (setup_terminals) heartbeat.setup_terminals = setup_terminals;
915
+ if (activations) heartbeat.activations = activations;
667
916
  if (input.diagnostics) heartbeat.diagnostics = input.diagnostics;
668
917
  return heartbeat;
669
918
  }
@@ -696,6 +945,62 @@ function readPackageInfo() {
696
945
  }
697
946
  return { name: manifest.name, version: manifest.version };
698
947
  }
948
+ class HerdClient {
949
+ constructor(identity, fetchImpl = fetch) {
950
+ this.identity = identity;
951
+ this.fetchImpl = fetchImpl;
952
+ }
953
+ /** The headers that sign a request, as a poll is signed. */
954
+ async sign(method, url, body) {
955
+ return signRequest({
956
+ method,
957
+ url,
958
+ body,
959
+ herdId: this.identity.herd_id,
960
+ machineId: this.identity.machine_id,
961
+ privateKey: await importSignPrivateKey(this.identity.keys.sign.private)
962
+ });
963
+ }
964
+ async post(pathname, value) {
965
+ const url = new URL(pathname, this.identity.host).href;
966
+ const body = JSON.stringify(value);
967
+ let headers;
968
+ let response;
969
+ let text;
970
+ try {
971
+ headers = await this.sign("POST", url, body);
972
+ response = await this.fetchImpl(url, {
973
+ method: "POST",
974
+ headers,
975
+ body,
976
+ signal: AbortSignal.timeout(3e4)
977
+ });
978
+ text = await response.text();
979
+ } catch (error) {
980
+ return {
981
+ ok: false,
982
+ reason: error instanceof Error ? error.message : String(error)
983
+ };
984
+ }
985
+ const verified = await verifyResponse({
986
+ response: { status: response.status, headers: response.headers },
987
+ body: text,
988
+ request: { method: "POST", url, headers },
989
+ publicKey: this.identity.server_public_key
990
+ });
991
+ if (!verified) {
992
+ return {
993
+ ok: false,
994
+ reason: `HTTP ${response.status}, not signed by the herd's server key`
995
+ };
996
+ }
997
+ try {
998
+ return { ok: true, status: response.status, body: JSON.parse(text) };
999
+ } catch {
1000
+ return { ok: false, reason: "the answer is not JSON" };
1001
+ }
1002
+ }
1003
+ }
699
1004
  const DEFAULT_INTERVAL_MS = 1e4;
700
1005
  const MIN_INTERVAL_MS = 1e3;
701
1006
  const EXACT_VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
@@ -763,6 +1068,14 @@ class Poller {
763
1068
  const acks = handled.unacked();
764
1069
  const diagnostics = this.diagnostics;
765
1070
  const status = this.options.status();
1071
+ let report = EMPTY_REPORT;
1072
+ try {
1073
+ report = await this.options.report?.() ?? EMPTY_REPORT;
1074
+ } catch (error) {
1075
+ this.options.log.line(
1076
+ `could not collect the heartbeat report: ${error instanceof Error ? error.message : String(error)}`
1077
+ );
1078
+ }
766
1079
  const body = JSON.stringify(
767
1080
  buildHeartbeat({
768
1081
  machineId: identity.machine_id,
@@ -772,6 +1085,7 @@ class Poller {
772
1085
  harnesses: this.options.harnesses,
773
1086
  acks,
774
1087
  diagnostics,
1088
+ report,
775
1089
  now: this.now()
776
1090
  })
777
1091
  );
@@ -834,12 +1148,34 @@ class Poller {
834
1148
  MIN_INTERVAL_MS,
835
1149
  answer.next_poll_after_ms || DEFAULT_INTERVAL_MS
836
1150
  );
1151
+ const access = this.options.access;
1152
+ if (access) {
1153
+ const before = access.mismatch;
1154
+ try {
1155
+ access.observe(answer.trust);
1156
+ } catch (error) {
1157
+ access.mismatch = `access.json could not be written: ${error instanceof Error ? error.message : String(error)}`;
1158
+ }
1159
+ if (access.mismatch !== void 0 && access.mismatch !== before) {
1160
+ this.options.log.line(
1161
+ `the herd's trust does not match access.json: ${access.mismatch}; work it signs is refused`
1162
+ );
1163
+ }
1164
+ }
837
1165
  this.recordPoll({
838
1166
  lastPollAt: new Date(this.now()).toISOString(),
839
1167
  machineState: answer.machine_state,
840
- lastError: null
1168
+ lastError: null,
1169
+ trustMismatch: access?.mismatch ?? null
841
1170
  });
842
1171
  for (const command of answer.commands) await this.handle(command);
1172
+ try {
1173
+ await this.options.afterPoll?.(answer);
1174
+ } catch (error) {
1175
+ this.options.log.line(
1176
+ `after the poll: ${error instanceof Error ? error.message : String(error)}`
1177
+ );
1178
+ }
843
1179
  return { kind: "ok", waitMs: this.intervalMs, response: answer };
844
1180
  }
845
1181
  async handle(command) {
@@ -850,6 +1186,21 @@ class Poller {
850
1186
  return;
851
1187
  }
852
1188
  const request = controlRequest(command);
1189
+ if (request === void 0 && this.options.handleCommand) {
1190
+ handled.record(id);
1191
+ try {
1192
+ if (await this.options.handleCommand(command)) return;
1193
+ } catch (error) {
1194
+ log.line(
1195
+ `${command.type} (${id}) failed: ${error instanceof Error ? error.message : String(error)}`
1196
+ );
1197
+ return;
1198
+ }
1199
+ log.line(
1200
+ `unknown command type ${command.type} (${id}); acked and ignored`
1201
+ );
1202
+ return;
1203
+ }
853
1204
  if (request === void 0) {
854
1205
  handled.record(id);
855
1206
  log.line(
@@ -874,93 +1225,1770 @@ class Poller {
874
1225
  }
875
1226
  }
876
1227
  }
877
- function send(message) {
878
- return new Promise((resolve) => {
879
- if (!process.send) {
880
- resolve(false);
881
- return;
1228
+ const MAX_SOCKET_PATH_BYTES = 100;
1229
+ function socketAddress(socketFile) {
1230
+ if (Buffer.byteLength(socketFile) <= MAX_SOCKET_PATH_BYTES) return socketFile;
1231
+ process.chdir(path.dirname(socketFile));
1232
+ return path.basename(socketFile);
1233
+ }
1234
+ const ENDING = /* @__PURE__ */ new Set(["exit", "removed", "dropped"]);
1235
+ class HostClient {
1236
+ constructor(socketFile, socket) {
1237
+ this.socketFile = socketFile;
1238
+ this.socket = socket;
1239
+ socket.setEncoding("utf8");
1240
+ socket.on("data", (chunk) => this.read(chunk));
1241
+ socket.on("close", () => this.end());
1242
+ socket.on("error", () => this.end());
1243
+ }
1244
+ buffer = "";
1245
+ nextId = 1;
1246
+ replies = /* @__PURE__ */ new Map();
1247
+ listeners = /* @__PURE__ */ new Map();
1248
+ closeListeners = [];
1249
+ closed = false;
1250
+ static connect(socketFile, timeoutMs = 2e3) {
1251
+ return new Promise((resolve, reject) => {
1252
+ let socket;
1253
+ try {
1254
+ socket = createConnection(socketAddress(socketFile));
1255
+ } catch (error) {
1256
+ reject(error instanceof Error ? error : new Error(String(error)));
1257
+ return;
1258
+ }
1259
+ const timer = setTimeout(() => {
1260
+ socket.destroy();
1261
+ reject(new Error(`${socketFile} did not answer`));
1262
+ }, timeoutMs);
1263
+ socket.once("connect", () => {
1264
+ clearTimeout(timer);
1265
+ resolve(new HostClient(socketFile, socket));
1266
+ });
1267
+ socket.once("error", (error) => {
1268
+ clearTimeout(timer);
1269
+ reject(error);
1270
+ });
1271
+ });
1272
+ }
1273
+ read(chunk) {
1274
+ this.buffer += chunk;
1275
+ let newline;
1276
+ while ((newline = this.buffer.indexOf("\n")) !== -1) {
1277
+ const line = this.buffer.slice(0, newline);
1278
+ this.buffer = this.buffer.slice(newline + 1);
1279
+ let message;
1280
+ try {
1281
+ message = JSON.parse(line);
1282
+ } catch {
1283
+ continue;
1284
+ }
1285
+ const id = message.id;
1286
+ if ("ok" in message) {
1287
+ const resolve = this.replies.get(id);
1288
+ this.replies.delete(id);
1289
+ if (message.ok !== true) this.listeners.delete(id);
1290
+ resolve?.(message);
1291
+ } else if (typeof message.event === "string") {
1292
+ this.listeners.get(id)?.(message);
1293
+ if (ENDING.has(message.event) && message.event !== "exit") {
1294
+ this.listeners.delete(id);
1295
+ }
1296
+ }
882
1297
  }
883
- process.send(message, void 0, {}, (error) => resolve(error === null));
884
- });
1298
+ }
1299
+ end() {
1300
+ if (this.closed) return;
1301
+ this.closed = true;
1302
+ const failed = {
1303
+ ok: false,
1304
+ error: { code: "closed", message: "the terminal host connection closed" }
1305
+ };
1306
+ for (const resolve of this.replies.values()) resolve(failed);
1307
+ this.replies.clear();
1308
+ this.listeners.clear();
1309
+ for (const listener of this.closeListeners.splice(0)) listener();
1310
+ }
1311
+ /** Sends one request (without an id) and resolves with its reply. */
1312
+ request(body) {
1313
+ return this.subscribe(body);
1314
+ }
1315
+ /** Sends an `attach` or `watch` and hands its events to `onEvent`. */
1316
+ subscribe(body, onEvent) {
1317
+ if (this.closed) {
1318
+ return Promise.resolve({
1319
+ ok: false,
1320
+ error: {
1321
+ code: "closed",
1322
+ message: "the terminal host connection closed"
1323
+ }
1324
+ });
1325
+ }
1326
+ const id = this.nextId++;
1327
+ if (onEvent) this.listeners.set(id, onEvent);
1328
+ return new Promise((resolve) => {
1329
+ this.replies.set(id, resolve);
1330
+ this.socket.write(`${JSON.stringify({ ...body, id })}
1331
+ `);
1332
+ });
1333
+ }
1334
+ onClose(listener) {
1335
+ if (this.closed) listener();
1336
+ else this.closeListeners.push(listener);
1337
+ }
1338
+ close() {
1339
+ this.socket.destroy();
1340
+ this.end();
1341
+ }
885
1342
  }
886
- function initialSelfUpdate() {
1343
+ async function requestOnce(socketFile, body) {
1344
+ let client;
887
1345
  try {
888
- const value = JSON.parse(process.env.AIBEX_SELF_UPDATE ?? "");
889
- if (typeof value.can === "boolean") {
890
- return {
891
- can: value.can,
892
- reason: typeof value.reason === "string" ? value.reason : null
893
- };
1346
+ client = await HostClient.connect(socketFile);
1347
+ } catch (error) {
1348
+ return {
1349
+ ok: false,
1350
+ error: { code: "closed", message: error.message }
1351
+ };
1352
+ }
1353
+ try {
1354
+ return await client.request(body);
1355
+ } finally {
1356
+ client.close();
1357
+ }
1358
+ }
1359
+ function hostSockets(terminalsDirectory, first) {
1360
+ let names = [];
1361
+ if (terminalsDirectory !== void 0) {
1362
+ try {
1363
+ names = readdirSync(terminalsDirectory).filter(
1364
+ (name) => name.endsWith(".sock")
1365
+ );
1366
+ } catch {
1367
+ names = [];
894
1368
  }
895
- } catch {
896
1369
  }
897
- return { can: true, reason: null };
1370
+ const sockets = names.toSorted().map((name) => path.join(terminalsDirectory ?? "", name));
1371
+ return first === void 0 ? sockets : [first, ...sockets.filter((socket) => socket !== first)];
898
1372
  }
899
- function main() {
900
- const info = readPackageInfo();
901
- if (process.argv.includes("--self-test")) {
902
- process.stdout.write(
903
- `${JSON.stringify({ name: info.name, version: info.version })}
904
- `
905
- );
906
- return;
1373
+ async function listTerminals(sockets) {
1374
+ const replies = await Promise.all(
1375
+ sockets.map(async (socket) => ({
1376
+ socket,
1377
+ reply: await requestOnce(socket, { type: "list" })
1378
+ }))
1379
+ );
1380
+ return replies.flatMap(
1381
+ ({ socket, reply }) => reply.ok && Array.isArray(reply.terminals) ? reply.terminals.map(
1382
+ (terminal) => ({ ...terminal, socket })
1383
+ ) : []
1384
+ );
1385
+ }
1386
+ const RELAYED = /* @__PURE__ */ new Set([
1387
+ "list",
1388
+ "screen",
1389
+ "snapshot",
1390
+ "attach",
1391
+ "write",
1392
+ "type",
1393
+ "press",
1394
+ "resize",
1395
+ "kill"
1396
+ ]);
1397
+ const LABELS = ["thread:", "setup:"];
1398
+ const DEFAULT_RELAY_TIMINGS = {
1399
+ idleCloseMs: 6e4,
1400
+ freshnessMs: 6e4
1401
+ };
1402
+ function isAibexTerminal(terminal) {
1403
+ return LABELS.some((label) => terminal?.label?.startsWith(label) ?? false);
1404
+ }
1405
+ class Relay {
1406
+ constructor(options) {
1407
+ this.options = options;
1408
+ this.timings = { ...DEFAULT_RELAY_TIMINGS, ...options.timings };
907
1409
  }
908
- const host = process.env.AIBEX_HOST;
909
- const hostDirectory = process.env.AIBEX_HOST_DIR;
910
- if (!host || !hostDirectory || !process.send) {
911
- process.stderr.write(
912
- "aibex-link is started by aibexd: it needs AIBEX_HOST, AIBEX_HOST_DIR and an IPC channel\n"
913
- );
914
- process.exitCode = 1;
915
- return;
1410
+ socket;
1411
+ idleTimer;
1412
+ /** Connections to terminal hosts made for this relay, by socket path. */
1413
+ hosts = /* @__PURE__ */ new Map();
1414
+ /**
1415
+ * Each viewer's requests still being handled, by `conn`: the tail of a
1416
+ * chain that runs them one at a time in the order they arrived.
1417
+ */
1418
+ queues = /* @__PURE__ */ new Map();
1419
+ /** Nonces seen, with when they may be forgotten. */
1420
+ nonces = /* @__PURE__ */ new Map();
1421
+ timings;
1422
+ now() {
1423
+ return (this.options.now ?? Date.now)();
916
1424
  }
917
- const log = new LinkLog();
918
- let status = {
919
- type: "status",
920
- aibexdVersion: process.env.AIBEX_AIBEXD_VERSION ?? "unknown",
921
- selfUpdate: initialSelfUpdate()
922
- };
923
- const identity = readIdentity(hostDirectory);
924
- let poller;
925
- if (identity) {
926
- log.line(
927
- `aibex-link ${info.version} linked to ${host} as ${identity.machine_id} (${identity.machine_name}) in herd ${identity.herd_id}`
928
- );
929
- poller = new Poller({
930
- identity,
931
- hostDirectory,
932
- linkVersion: info.version,
933
- log,
934
- handled: new HandledCommands(hostDirectory),
935
- status: () => status,
936
- harnesses: detectHarnesses(),
937
- forward: (commandId, request) => send({ type: "control", commandId, request }),
938
- backoffInitialMs: timing(
939
- "pollBackoffInitialMs",
940
- DEFAULT_POLL_BACKOFF_INITIAL_MS
941
- ),
942
- backoffMaxMs: timing("pollBackoffMaxMs", DEFAULT_POLL_BACKOFF_MAX_MS)
943
- });
944
- } else {
945
- log.line(
946
- `aibex-link ${info.version} linked to ${host}, not joined: run \`aibexd join <key>\` to join this machine to a herd`
947
- );
1425
+ isOpen() {
1426
+ return this.socket?.readyState === WebSocket.OPEN;
948
1427
  }
949
- process.on("message", (message) => {
950
- if (message.type === "status") {
951
- status = message;
952
- } else if (message.type === "diagnostics") {
953
- poller?.attachDiagnostics({
954
- ...message.diagnostics,
955
- collected_at: (/* @__PURE__ */ new Date()).toISOString(),
956
- command_id: message.commandId,
957
- log_tail: log.tail()
958
- });
959
- }
960
- });
961
- void send({ type: "ready" });
1428
+ /** How many terminal host connections the relay holds; each carries its subscriptions. */
1429
+ hostConnections() {
1430
+ return this.hosts.size;
1431
+ }
1432
+ /**
1433
+ * Opens a signed WebSocket to `/v1/relay`, replacing any open relay. The
1434
+ * upgrade is signed like a poll, over the URL's `https` (or local `http`)
1435
+ * form, and sent to its `wss` (`ws`) form.
1436
+ */
1437
+ async open() {
1438
+ this.close();
1439
+ const url = new URL("/v1/relay", this.options.host);
1440
+ const headers = await this.options.herd.sign("GET", url.href, "");
1441
+ const target = url.href.replace(/^http/u, "ws");
1442
+ const socket = new WebSocket(target, {
1443
+ headers: Object.fromEntries(headers)
1444
+ });
1445
+ this.socket = socket;
1446
+ const opened = await new Promise((resolve) => {
1447
+ const timer = setTimeout(() => {
1448
+ socket.close();
1449
+ resolve(false);
1450
+ }, 3e4);
1451
+ socket.addEventListener(
1452
+ "open",
1453
+ () => {
1454
+ clearTimeout(timer);
1455
+ resolve(true);
1456
+ },
1457
+ { once: true }
1458
+ );
1459
+ socket.addEventListener("error", () => resolve(false), { once: true });
1460
+ socket.addEventListener("close", () => resolve(false), { once: true });
1461
+ });
1462
+ if (!opened || this.socket !== socket) {
1463
+ if (this.socket === socket) this.socket = void 0;
1464
+ this.options.log.line("the herd refused the relay upgrade");
1465
+ return false;
1466
+ }
1467
+ this.options.log.line("relay open");
1468
+ socket.addEventListener("message", (message) => {
1469
+ void this.receive(message.data);
1470
+ });
1471
+ socket.addEventListener("close", () => {
1472
+ if (this.socket !== socket) return;
1473
+ this.socket = void 0;
1474
+ this.end();
1475
+ this.options.log.line("relay closed");
1476
+ });
1477
+ return true;
1478
+ }
1479
+ /** Closes the relay and ends every relayed subscription. */
1480
+ close() {
1481
+ const socket = this.socket;
1482
+ this.socket = void 0;
1483
+ socket?.close(1e3);
1484
+ this.end();
1485
+ }
1486
+ end() {
1487
+ clearTimeout(this.idleTimer);
1488
+ this.idleTimer = void 0;
1489
+ this.queues.clear();
1490
+ for (const pending of this.hosts.values()) {
1491
+ void (async () => {
1492
+ try {
1493
+ const client = await pending;
1494
+ client.close();
1495
+ } catch {
1496
+ }
1497
+ })();
1498
+ }
1499
+ this.hosts.clear();
1500
+ }
1501
+ send(frame) {
1502
+ if (this.socket?.readyState === WebSocket.OPEN) {
1503
+ this.socket.send(JSON.stringify(frame));
1504
+ }
1505
+ }
1506
+ refuse(conn, id, message) {
1507
+ this.send({
1508
+ type: "reply",
1509
+ conn,
1510
+ message: {
1511
+ id: typeof id === "string" || typeof id === "number" ? id : null,
1512
+ ok: false,
1513
+ error: { code: "bad-request", message }
1514
+ }
1515
+ });
1516
+ }
1517
+ async receive(data) {
1518
+ let frame;
1519
+ try {
1520
+ frame = JSON.parse(String(data));
1521
+ } catch {
1522
+ return;
1523
+ }
1524
+ if (frame.type === "viewers") {
1525
+ this.viewers(frame.count);
1526
+ } else if (frame.type === "request" && typeof frame.conn === "string") {
1527
+ await this.enqueue(frame.conn, frame.envelope);
1528
+ }
1529
+ }
1530
+ /**
1531
+ * Runs a viewer's request after every earlier request from the same
1532
+ * `conn` has reached its terminal host and been answered, so two
1533
+ * keystrokes are never swapped. Different viewers stay concurrent.
1534
+ */
1535
+ enqueue(conn, envelope) {
1536
+ const socket = this.socket;
1537
+ const previous = this.queues.get(conn);
1538
+ const run = (async () => {
1539
+ await previous;
1540
+ if (this.socket === socket) {
1541
+ try {
1542
+ await this.request(conn, envelope);
1543
+ } catch (error) {
1544
+ this.options.log.line(
1545
+ `a relayed request failed: ${error instanceof Error ? error.message : String(error)}`
1546
+ );
1547
+ }
1548
+ }
1549
+ })();
1550
+ this.queues.set(conn, run);
1551
+ void (async () => {
1552
+ await run;
1553
+ if (this.queues.get(conn) === run) this.queues.delete(conn);
1554
+ })();
1555
+ return run;
1556
+ }
1557
+ viewers(count) {
1558
+ clearTimeout(this.idleTimer);
1559
+ this.idleTimer = void 0;
1560
+ if (count > 0) return;
1561
+ this.idleTimer = setTimeout(() => {
1562
+ this.options.log.line("no viewers for a while; closing the relay");
1563
+ this.close();
1564
+ }, this.timings.idleCloseMs);
1565
+ }
1566
+ /** Checks a viewer's request and forwards it to the host that holds the terminal. */
1567
+ async request(conn, envelope) {
1568
+ const opened = await this.options.access.open(envelope);
1569
+ if (!opened.ok) {
1570
+ this.refuse(conn, null, `refused: ${opened.reason}`);
1571
+ return;
1572
+ }
1573
+ const { created_at: createdAt, nonce, request } = opened.payload;
1574
+ const id = request?.id;
1575
+ const age = this.now() - Date.parse(createdAt);
1576
+ if (!(Math.abs(age) <= this.timings.freshnessMs)) {
1577
+ this.refuse(conn, id, "refused: the request is not fresh");
1578
+ return;
1579
+ }
1580
+ const now = this.now();
1581
+ for (const [seen, until] of this.nonces) {
1582
+ if (until < now) this.nonces.delete(seen);
1583
+ }
1584
+ if (typeof nonce !== "string" || this.nonces.has(nonce)) {
1585
+ this.refuse(conn, id, "refused: the nonce was used before");
1586
+ return;
1587
+ }
1588
+ this.nonces.set(nonce, now + 2 * this.timings.freshnessMs);
1589
+ const type = request?.type;
1590
+ if (typeof type !== "string" || !RELAYED.has(type)) {
1591
+ this.refuse(conn, id, `refused: ${String(type)} is never relayed`);
1592
+ return;
1593
+ }
1594
+ const sockets = hostSockets(
1595
+ this.options.terminalsDirectory,
1596
+ this.options.terminalSocket
1597
+ );
1598
+ const terminals = await listTerminals(sockets);
1599
+ if (type === "list") {
1600
+ this.send({
1601
+ type: "reply",
1602
+ conn,
1603
+ message: {
1604
+ id,
1605
+ ok: true,
1606
+ terminals: terminals.filter((terminal2) => isAibexTerminal(terminal2)).map(({ socket: _socket, ...terminal2 }) => terminal2)
1607
+ }
1608
+ });
1609
+ return;
1610
+ }
1611
+ const terminalId = request.terminal;
1612
+ const terminal = terminals.find((found) => found.id === terminalId);
1613
+ if (!terminal || !isAibexTerminal(terminal)) {
1614
+ this.refuse(conn, id, "refused: not a terminal Aibex started");
1615
+ return;
1616
+ }
1617
+ if (type === "kill" && !terminal.label?.startsWith("setup:")) {
1618
+ this.refuse(conn, id, "refused: only a setup terminal can be killed");
1619
+ return;
1620
+ }
1621
+ await this.forward(conn, terminal.socket, request);
1622
+ }
1623
+ connection(socket) {
1624
+ let pending = this.hosts.get(socket);
1625
+ if (!pending) {
1626
+ pending = HostClient.connect(socket);
1627
+ this.hosts.set(socket, pending);
1628
+ void (async () => {
1629
+ try {
1630
+ const client = await pending;
1631
+ client.onClose(() => {
1632
+ if (this.hosts.get(socket) === pending) this.hosts.delete(socket);
1633
+ });
1634
+ } catch {
1635
+ if (this.hosts.get(socket) === pending) this.hosts.delete(socket);
1636
+ }
1637
+ })();
1638
+ }
1639
+ return pending;
1640
+ }
1641
+ /** Sends the request under the link's own id, and everything back under the viewer's. */
1642
+ async forward(conn, socket, request) {
1643
+ const viewerId = request.id;
1644
+ let client;
1645
+ try {
1646
+ client = await this.connection(socket);
1647
+ } catch {
1648
+ this.refuse(conn, viewerId, "the terminal host cannot be reached");
1649
+ return;
1650
+ }
1651
+ const { id: _id, ...body } = request;
1652
+ let held = [];
1653
+ const event = (message) => this.send({
1654
+ type: "event",
1655
+ conn,
1656
+ message: { ...message, id: viewerId }
1657
+ });
1658
+ const reply = await client.subscribe(
1659
+ body,
1660
+ request.type === "attach" ? (message) => {
1661
+ if (held) held.push(message);
1662
+ else event(message);
1663
+ } : void 0
1664
+ );
1665
+ this.send({
1666
+ type: "reply",
1667
+ conn,
1668
+ message: { ...reply, id: viewerId }
1669
+ });
1670
+ const queued = held;
1671
+ held = void 0;
1672
+ for (const message of queued) event(message);
1673
+ }
1674
+ }
1675
+ const GITHUB_HOST = "github.com";
1676
+ const REPO_SCAN_MS = 6e4;
1677
+ const GITHUB_REMOTE = [
1678
+ /^https:\/\/(?:[^@/]+@)?github\.com\/([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/u,
1679
+ /^(?:ssh:\/\/)?git@github\.com[:/]([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/u
1680
+ ];
1681
+ function parseGitHubRemote(url) {
1682
+ for (const pattern of GITHUB_REMOTE) {
1683
+ const match = pattern.exec(url.trim());
1684
+ if (match?.[1] && match[2]) return { owner: match[1], name: match[2] };
1685
+ }
1686
+ return void 0;
1687
+ }
1688
+ function git(cwd, args) {
1689
+ return new Promise((resolve) => {
1690
+ execFile(
1691
+ "git",
1692
+ ["-C", cwd, ...args],
1693
+ (failure, stdout, stderr) => resolve({
1694
+ ok: failure === null,
1695
+ stdout: stdout.trim(),
1696
+ stderr: stderr.trim()
1697
+ })
1698
+ );
1699
+ });
1700
+ }
1701
+ function directories(parent) {
1702
+ try {
1703
+ return readdirSync(parent, { withFileTypes: true }).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")).map((entry) => entry.name).toSorted();
1704
+ } catch {
1705
+ return [];
1706
+ }
1707
+ }
1708
+ async function scanRepos(reposDirectory) {
1709
+ const github = path.join(reposDirectory, GITHUB_HOST);
1710
+ const candidates = directories(github).flatMap(
1711
+ (owner) => directories(path.join(github, owner)).map((name) => path.join(github, owner, name)).filter((directory) => existsSync(path.join(directory, ".git")))
1712
+ );
1713
+ const found = await Promise.all(
1714
+ candidates.map(async (directory) => {
1715
+ const origin = await git(directory, [
1716
+ "config",
1717
+ "--get",
1718
+ "remote.origin.url"
1719
+ ]);
1720
+ const repo = origin.ok ? parseGitHubRemote(origin.stdout) : void 0;
1721
+ return repo ? { repo, path: directory } : void 0;
1722
+ })
1723
+ );
1724
+ return found.filter((checkout) => checkout !== void 0);
1725
+ }
1726
+ class Repos {
1727
+ constructor(reposDirectory, now = Date.now) {
1728
+ this.reposDirectory = reposDirectory;
1729
+ this.now = now;
1730
+ }
1731
+ scanned = [];
1732
+ scannedAt = Number.NEGATIVE_INFINITY;
1733
+ async list(options = {}) {
1734
+ if (options.fresh || this.now() - this.scannedAt >= REPO_SCAN_MS) {
1735
+ this.scanned = await scanRepos(this.reposDirectory);
1736
+ this.scannedAt = this.now();
1737
+ }
1738
+ return this.scanned;
1739
+ }
1740
+ /** The checkout for a repository, rescanning once when it is not known. */
1741
+ async find(repo) {
1742
+ const match = (checkouts) => checkouts.find(
1743
+ (checkout) => checkout.repo.owner.toLowerCase() === repo.owner.toLowerCase() && checkout.repo.name.toLowerCase() === repo.name.toLowerCase()
1744
+ );
1745
+ return match(await this.list()) ?? match(await this.list({ fresh: true }));
1746
+ }
1747
+ }
1748
+ const HARNESSES = {
1749
+ // The input box: a full-width rule, then `❯` and the placeholder. The
1750
+ // folder trust dialog also starts a line with `❯`, but never under a rule.
1751
+ claude: {
1752
+ command: "claude",
1753
+ args: [],
1754
+ ready: /─{20,}\n❯(?: |$)/mu,
1755
+ multiline: "paste"
1756
+ },
1757
+ // The composer line `› Ask Codex to do anything`. The update notice, the
1758
+ // folder trust dialog and sign-in all list numbered choices after `›`.
1759
+ codex: {
1760
+ command: "codex",
1761
+ // The update notice waits for a key, so a driven session skips it.
1762
+ args: ["-c", "check_for_update_on_startup=false"],
1763
+ ready: /^›(?: (?!\d+\. )|$)/mu,
1764
+ multiline: "paste"
1765
+ },
1766
+ // The input box under a row of `▄`, with `→` and the placeholder.
1767
+ cursor: {
1768
+ command: "cursor-agent",
1769
+ args: [],
1770
+ ready: /▄{20,}\n\s*→/u,
1771
+ multiline: "paste"
1772
+ }
1773
+ };
1774
+ const SETUP_PROGRAMS = {
1775
+ shell: void 0,
1776
+ claude: "claude",
1777
+ codex: "codex",
1778
+ cursor: "cursor-agent",
1779
+ "repo-add": "aibexd"
1780
+ };
1781
+ function bracketedPaste(text) {
1782
+ return `\x1B[200~${text}\x1B[201~`;
1783
+ }
1784
+ const MAX_CHUNK_BYTES = 1024 * 1024;
1785
+ class Transcript {
1786
+ constructor(spoolFile, state) {
1787
+ this.spoolFile = spoolFile;
1788
+ this.state = state;
1789
+ }
1790
+ size() {
1791
+ try {
1792
+ return statSync(this.spoolFile).size;
1793
+ } catch {
1794
+ return 0;
1795
+ }
1796
+ }
1797
+ /** The terminal `seq` the spool ends at. */
1798
+ seq() {
1799
+ return this.size() + this.state.seqOffset;
1800
+ }
1801
+ /** The bytes not yet stored on the server. */
1802
+ pending() {
1803
+ return this.size() - this.state.uploaded;
1804
+ }
1805
+ /** Appends output that ends at terminal `seq`, dropping what the spool already holds. */
1806
+ appendOutput(data, seq) {
1807
+ let bytes = Buffer.from(data, "utf8");
1808
+ const have = this.seq();
1809
+ if (seq <= have) return;
1810
+ const start = seq - bytes.length;
1811
+ if (start < have) bytes = bytes.subarray(have - start);
1812
+ appendFileSync(this.spoolFile, bytes, { mode: 384 });
1813
+ if (start > have) this.state.seqOffset += start - have;
1814
+ }
1815
+ /** Appends a screen redraw taken at terminal `seq`. */
1816
+ appendRedraw(serialized, seq) {
1817
+ appendFileSync(this.spoolFile, serialized, { mode: 384 });
1818
+ this.state.seqOffset = seq - this.size();
1819
+ }
1820
+ /** Empties the spool so it starts at server offset `base` with a redraw. */
1821
+ newSegment(base, serialized, seq) {
1822
+ writeFileSync(this.spoolFile, "", { mode: 384 });
1823
+ this.state.base = base;
1824
+ this.state.uploaded = 0;
1825
+ this.appendRedraw(serialized, seq);
1826
+ }
1827
+ read(from, length) {
1828
+ const buffer = Buffer.alloc(length);
1829
+ const fd = openSync(this.spoolFile, "r");
1830
+ try {
1831
+ const read = readSync(fd, buffer, 0, length, from);
1832
+ return buffer.subarray(0, read);
1833
+ } finally {
1834
+ closeSync(fd);
1835
+ }
1836
+ }
1837
+ /** Sends everything not yet stored, one chunk at a time. */
1838
+ async upload(options) {
1839
+ while (this.pending() > 0) {
1840
+ const chunk = this.read(
1841
+ this.state.uploaded,
1842
+ Math.min(this.pending(), MAX_CHUNK_BYTES)
1843
+ );
1844
+ const answer = await options.herd.post(
1845
+ `/v1/threads/${options.threadId}/transcript`,
1846
+ {
1847
+ lease_id: options.leaseId,
1848
+ epoch: options.epoch,
1849
+ harness: options.harness,
1850
+ format: "terminal",
1851
+ offset: this.state.base + this.state.uploaded,
1852
+ chunk: chunk.toString("base64url"),
1853
+ sha256: createHash("sha256").update(chunk).digest("hex")
1854
+ }
1855
+ );
1856
+ if (!answer.ok) return "failed";
1857
+ const body = answer.body ?? {};
1858
+ if (answer.status === 200 && typeof body.next_offset === "number") {
1859
+ this.state.uploaded = body.next_offset - this.state.base;
1860
+ continue;
1861
+ }
1862
+ const problem = problemName(body);
1863
+ if (problem === "lease-fenced") return "fenced";
1864
+ if (problem === "transcript-offset" && typeof body.next_offset === "number") {
1865
+ const next = body.next_offset - this.state.base;
1866
+ if (next >= 0 && next <= this.size()) {
1867
+ this.state.uploaded = next;
1868
+ continue;
1869
+ }
1870
+ return { newSegment: body.next_offset };
1871
+ }
1872
+ if (answer.status >= 500 || answer.status === 429) return "failed";
1873
+ return "refused";
1874
+ }
1875
+ return "done";
1876
+ }
1877
+ remove() {
1878
+ rmSync(this.spoolFile, { force: true });
1879
+ }
1880
+ }
1881
+ const DEFAULT_CARRY = [".mcp.json", ".claude/settings.local.json"];
1882
+ function carryList(reposConfig) {
1883
+ const value = readJson(reposConfig);
1884
+ const carry = value?.carry;
1885
+ if (!Array.isArray(carry)) return DEFAULT_CARRY;
1886
+ return carry.filter(
1887
+ (entry) => typeof entry === "string" && entry !== "" && !path.isAbsolute(entry) && !entry.split(/[/\\]/u).includes("..")
1888
+ );
1889
+ }
1890
+ async function defaultBranch(checkout) {
1891
+ const head = await git(checkout, [
1892
+ "symbolic-ref",
1893
+ "--short",
1894
+ "refs/remotes/origin/HEAD"
1895
+ ]);
1896
+ return head.ok && head.stdout.startsWith("origin/") ? head.stdout.slice("origin/".length) : "main";
1897
+ }
1898
+ async function prepareWorktree(options) {
1899
+ const { checkout, threadId } = options;
1900
+ const worktreePath = path.join(options.workDirectory, threadId);
1901
+ const branch = `aibex/${threadId}`;
1902
+ const fetched = await git(checkout, ["fetch", "--quiet", "origin"]);
1903
+ const base = options.baseRef ?? await defaultBranch(checkout);
1904
+ if (!existsSync(path.join(worktreePath, ".git"))) {
1905
+ mkdirSync(options.workDirectory, { recursive: true });
1906
+ const known = await git(checkout, [
1907
+ "rev-parse",
1908
+ "--verify",
1909
+ "--quiet",
1910
+ `refs/heads/${branch}`
1911
+ ]);
1912
+ const added = await git(
1913
+ checkout,
1914
+ known.ok ? ["worktree", "add", "--quiet", worktreePath, branch] : [
1915
+ "worktree",
1916
+ "add",
1917
+ "--quiet",
1918
+ "-b",
1919
+ branch,
1920
+ worktreePath,
1921
+ `origin/${base}`
1922
+ ]
1923
+ );
1924
+ if (!added.ok) {
1925
+ const why = added.stderr || `git worktree add failed`;
1926
+ return {
1927
+ ok: false,
1928
+ reason: fetched.ok ? why : `${why} (git fetch failed too: ${fetched.stderr})`
1929
+ };
1930
+ }
1931
+ }
1932
+ const carried = [];
1933
+ for (const file of carryList(options.reposConfig)) {
1934
+ const from = path.join(checkout, file);
1935
+ const to = path.join(worktreePath, file);
1936
+ if (!existsSync(from) || existsSync(to)) continue;
1937
+ const tracked = await git(checkout, [
1938
+ "ls-files",
1939
+ "--error-unmatch",
1940
+ "--",
1941
+ file
1942
+ ]);
1943
+ if (tracked.ok) continue;
1944
+ mkdirSync(path.dirname(to), { recursive: true });
1945
+ cpSync(from, to, { recursive: true });
1946
+ carried.push(file);
1947
+ }
1948
+ return {
1949
+ ok: true,
1950
+ worktree: { path: worktreePath, branch, base },
1951
+ carried
1952
+ };
1953
+ }
1954
+ const DEFAULT_SESSION_TIMINGS = {
1955
+ readyTimeoutMs: 6e4,
1956
+ checkMs: 250,
1957
+ quietMs: 750,
1958
+ statusIntervalMs: 5e3,
1959
+ uploadIntervalMs: 5e3,
1960
+ uploadThresholdBytes: 256 * 1024,
1961
+ settleMs: 150
1962
+ };
1963
+ const STATUS_MAX = 200;
1964
+ function newEventId() {
1965
+ return `e_${randomBytes(12).toString("base64url")}`;
1966
+ }
1967
+ function errorText(error) {
1968
+ return error instanceof Error ? error.message : String(error);
1969
+ }
1970
+ class Session {
1971
+ constructor(record, manager) {
1972
+ this.record = record;
1973
+ this.manager = manager;
1974
+ const now = manager.now();
1975
+ this.lastProgressAt = now;
1976
+ this.quietSince = now;
1977
+ this.readySince = now;
1978
+ this.transcript = new Transcript(
1979
+ manager.spoolFile(record.thread_id),
1980
+ record.transcript
1981
+ );
1982
+ if (record.ended) this.state = "ended";
1983
+ else if (record.prompted) this.state = "idle";
1984
+ }
1985
+ client;
1986
+ state = "starting";
1987
+ waitingOn;
1988
+ lastProgressAt;
1989
+ busy = false;
1990
+ lastScreenSeq = -1;
1991
+ quietSince;
1992
+ lastSendAt = 0;
1993
+ sending = false;
1994
+ readySince;
1995
+ timer;
1996
+ uploadTimer;
1997
+ uploading;
1998
+ statusAt = Number.NEGATIVE_INFINITY;
1999
+ statusSent;
2000
+ statusPending;
2001
+ statusTimer;
2002
+ /** Set when the server fenced the lease: nothing more is sent. */
2003
+ fenced = false;
2004
+ disposed = false;
2005
+ transcript;
2006
+ get harness() {
2007
+ return this.manager.harnesses[this.record.harness];
2008
+ }
2009
+ get timings() {
2010
+ return this.manager.timings;
2011
+ }
2012
+ save() {
2013
+ this.manager.save(this);
2014
+ }
2015
+ /** Attaches for the transcript and watches for state, then starts checking. */
2016
+ async drive(client) {
2017
+ this.client = client;
2018
+ const terminal = this.record.terminal;
2019
+ client.onClose(() => {
2020
+ if (!this.disposed && !this.record.ended) {
2021
+ this.manager.log.line(
2022
+ `lost the terminal host for ${this.record.thread_id}; it is picked up again when the link restarts`
2023
+ );
2024
+ }
2025
+ });
2026
+ const watched = await client.subscribe(
2027
+ { type: "watch", terminal },
2028
+ (event) => this.onWatch(event)
2029
+ );
2030
+ if (!watched.ok) {
2031
+ this.end("the terminal is gone");
2032
+ return;
2033
+ }
2034
+ const since = this.transcript.seq();
2035
+ let attached = await client.subscribe(
2036
+ { type: "attach", terminal, since },
2037
+ (event) => this.onOutput(event)
2038
+ );
2039
+ if (!attached.ok && attached.error?.code === "gap") {
2040
+ attached = await client.subscribe(
2041
+ { type: "attach", terminal },
2042
+ (event) => this.onOutput(event)
2043
+ );
2044
+ if (attached.ok && typeof attached.serialized === "string") {
2045
+ this.transcript.appendRedraw(
2046
+ attached.serialized,
2047
+ attached.seq
2048
+ );
2049
+ this.save();
2050
+ }
2051
+ }
2052
+ const screen = await client.request({ type: "screen", terminal });
2053
+ if (screen.ok && screen.exited) {
2054
+ this.end("exited");
2055
+ return;
2056
+ }
2057
+ this.timer = setInterval(() => void this.check(), this.timings.checkMs);
2058
+ this.uploadTimer = setInterval(
2059
+ () => void this.upload(),
2060
+ this.timings.uploadIntervalMs
2061
+ );
2062
+ }
2063
+ onOutput(event) {
2064
+ if (event.event === "output" && !this.fenced) {
2065
+ this.transcript.appendOutput(event.data, event.seq);
2066
+ if (this.transcript.pending() >= this.timings.uploadThresholdBytes) {
2067
+ void this.upload();
2068
+ }
2069
+ } else if (event.event === "exit") {
2070
+ this.end("exited");
2071
+ }
2072
+ }
2073
+ onWatch(event) {
2074
+ const now = this.manager.now();
2075
+ switch (event.event) {
2076
+ case "screen": {
2077
+ this.lastProgressAt = now;
2078
+ this.quietSince = now;
2079
+ break;
2080
+ }
2081
+ case "active": {
2082
+ this.busy = true;
2083
+ if (this.record.prompted && !this.record.ended) this.state = "working";
2084
+ break;
2085
+ }
2086
+ case "idle": {
2087
+ this.busy = false;
2088
+ if (this.record.prompted && !this.record.ended) this.state = "idle";
2089
+ break;
2090
+ }
2091
+ case "title": {
2092
+ this.setStatus(typeof event.title === "string" ? event.title : "");
2093
+ break;
2094
+ }
2095
+ case "exit":
2096
+ case "removed": {
2097
+ this.end("exited");
2098
+ break;
2099
+ }
2100
+ }
2101
+ }
2102
+ /** Whether output has been quiet long enough to type. */
2103
+ quiet(screenSeq) {
2104
+ const now = this.manager.now();
2105
+ if (screenSeq !== this.lastScreenSeq) {
2106
+ this.lastScreenSeq = screenSeq;
2107
+ this.quietSince = now;
2108
+ }
2109
+ return !this.busy && now - this.quietSince >= this.timings.quietMs;
2110
+ }
2111
+ /** Types the prompt once the harness is ready, then one follow-up per idle. */
2112
+ async check() {
2113
+ if (this.sending || this.record.ended || this.fenced || !this.client) {
2114
+ return;
2115
+ }
2116
+ const next = this.record.queue[0];
2117
+ if (next === void 0) return;
2118
+ if (this.record.prompted && this.manager.now() - this.lastSendAt < this.timings.quietMs * 2) {
2119
+ return;
2120
+ }
2121
+ this.sending = true;
2122
+ try {
2123
+ const screen = await this.client.request({
2124
+ type: "screen",
2125
+ terminal: this.record.terminal
2126
+ });
2127
+ if (!screen.ok) return;
2128
+ const quiet = this.quiet(screen.seq);
2129
+ const ready = this.record.prompted || this.harness.ready.test(String(screen.text));
2130
+ if (!quiet || !ready) {
2131
+ if (!this.record.prompted && this.state !== "waiting" && this.manager.now() - this.readySince >= this.timings.readyTimeoutMs) {
2132
+ this.state = "waiting";
2133
+ this.waitingOn = "login";
2134
+ this.setStatus("harness not ready");
2135
+ this.manager.log.line(
2136
+ `${this.record.thread_id}: ${this.record.harness} showed no ready marker in ${Math.round(this.timings.readyTimeoutMs / 1e3)}s; nothing typed`
2137
+ );
2138
+ }
2139
+ return;
2140
+ }
2141
+ await this.send(next);
2142
+ } finally {
2143
+ this.sending = false;
2144
+ }
2145
+ }
2146
+ async send(text) {
2147
+ const client = this.client;
2148
+ if (!client) return;
2149
+ const terminal = this.record.terminal;
2150
+ const multiline = text.includes("\n");
2151
+ const sent = multiline && this.harness.multiline === "paste" ? await client.request({
2152
+ type: "write",
2153
+ terminal,
2154
+ data: bracketedPaste(text)
2155
+ }) : await client.request({ type: "type", terminal, text });
2156
+ if (!sent.ok) return;
2157
+ await new Promise((resolve) => setTimeout(resolve, this.timings.settleMs));
2158
+ await client.request({ type: "press", terminal, keys: ["Enter"] });
2159
+ this.record.queue.shift();
2160
+ const first = !this.record.prompted;
2161
+ this.record.prompted = true;
2162
+ this.lastSendAt = this.manager.now();
2163
+ this.state = "working";
2164
+ this.waitingOn = void 0;
2165
+ this.save();
2166
+ this.manager.log.line(
2167
+ `${this.record.thread_id}: ${first ? "typed the prompt" : "typed a follow-up"} into ${this.record.harness}`
2168
+ );
2169
+ }
2170
+ /** A title becomes a `status` event, at most one per interval, the latest winning. */
2171
+ setStatus(text) {
2172
+ const status = text.trim().slice(0, STATUS_MAX);
2173
+ if (status === "") return;
2174
+ this.statusPending = status;
2175
+ const wait = this.statusAt + this.timings.statusIntervalMs - this.manager.now();
2176
+ if (wait <= 0) {
2177
+ this.flushStatus();
2178
+ } else {
2179
+ this.statusTimer ??= setTimeout(() => {
2180
+ this.statusTimer = void 0;
2181
+ this.flushStatus();
2182
+ }, wait);
2183
+ }
2184
+ }
2185
+ flushStatus() {
2186
+ const status = this.statusPending;
2187
+ this.statusPending = void 0;
2188
+ if (status === void 0 || status === this.statusSent || this.fenced) {
2189
+ return;
2190
+ }
2191
+ this.statusAt = this.manager.now();
2192
+ this.statusSent = status;
2193
+ this.manager.event(this, { type: "status", text: status });
2194
+ }
2195
+ queueText(text) {
2196
+ this.record.queue.push(text);
2197
+ this.save();
2198
+ }
2199
+ /** The program exited, or the session never started: report it once. */
2200
+ end(reason) {
2201
+ if (this.record.ended || this.fenced) return;
2202
+ this.record.ended = reason;
2203
+ this.state = "ended";
2204
+ this.stopTimers();
2205
+ this.manager.event(this, { type: "session.ended", without_done: true });
2206
+ this.manager.log.line(
2207
+ `${this.record.thread_id}: session ended (${reason})`
2208
+ );
2209
+ void this.upload();
2210
+ }
2211
+ async kill() {
2212
+ if (!this.client || this.record.terminal === void 0) return;
2213
+ await this.client.request({ type: "kill", terminal: this.record.terminal });
2214
+ }
2215
+ /** Sends the spool from the server's offset. */
2216
+ upload() {
2217
+ this.uploading ??= (async () => {
2218
+ try {
2219
+ await this.uploadOnce();
2220
+ } finally {
2221
+ this.uploading = void 0;
2222
+ this.manager.settle(this);
2223
+ }
2224
+ })();
2225
+ return this.uploading;
2226
+ }
2227
+ async uploadOnce() {
2228
+ if (this.fenced || this.transcript.pending() <= 0) return;
2229
+ const outcome = await this.transcript.upload({
2230
+ herd: this.manager.herd,
2231
+ threadId: this.record.thread_id,
2232
+ leaseId: this.record.lease_id,
2233
+ epoch: this.record.epoch,
2234
+ harness: this.record.harness
2235
+ });
2236
+ if (outcome === "fenced") {
2237
+ await this.manager.fence(this);
2238
+ return;
2239
+ }
2240
+ if (outcome === "refused") {
2241
+ this.manager.log.line(
2242
+ `${this.record.thread_id}: the herd refused the transcript; it stays in the spool`
2243
+ );
2244
+ this.transcript.state.uploaded = this.transcript.size();
2245
+ } else if (typeof outcome === "object") {
2246
+ const snapshot = this.client ? await this.client.request({
2247
+ type: "snapshot",
2248
+ terminal: this.record.terminal,
2249
+ lines: 0
2250
+ }) : void 0;
2251
+ this.transcript.newSegment(
2252
+ outcome.newSegment,
2253
+ snapshot?.ok && typeof snapshot.serialized === "string" ? snapshot.serialized : "",
2254
+ snapshot?.ok ? snapshot.seq : this.transcript.seq()
2255
+ );
2256
+ }
2257
+ this.save();
2258
+ }
2259
+ /** Whether everything about it reached the herd, so its record can go. */
2260
+ settled() {
2261
+ return this.record.ended !== void 0 && this.record.outbox.length === 0 && this.transcript.pending() <= 0 && this.uploading === void 0;
2262
+ }
2263
+ stopTimers() {
2264
+ clearInterval(this.timer);
2265
+ clearInterval(this.uploadTimer);
2266
+ }
2267
+ dispose() {
2268
+ this.disposed = true;
2269
+ this.stopTimers();
2270
+ clearTimeout(this.statusTimer);
2271
+ this.client?.close();
2272
+ }
2273
+ heartbeat() {
2274
+ const { record } = this;
2275
+ const session = {
2276
+ thread_id: record.thread_id,
2277
+ lease_id: record.lease_id,
2278
+ epoch: record.epoch,
2279
+ harness: record.harness,
2280
+ state: this.state,
2281
+ last_progress_at: new Date(this.lastProgressAt).toISOString(),
2282
+ transcript_offset: record.transcript.base + record.transcript.uploaded,
2283
+ started_at: record.started_at
2284
+ };
2285
+ if (this.waitingOn) session.waiting_on = this.waitingOn;
2286
+ if (record.repo) session.repo = record.repo;
2287
+ if (record.worktree) session.worktree = record.worktree;
2288
+ return session;
2289
+ }
2290
+ }
2291
+ class Sessions {
2292
+ constructor(options) {
2293
+ this.options = options;
2294
+ this.harnesses = options.harnesses ?? HARNESSES;
2295
+ this.timings = { ...DEFAULT_SESSION_TIMINGS, ...options.timings };
2296
+ this.herd = options.herd;
2297
+ this.log = options.log;
2298
+ this.directory = path.join(options.paths.hostDirectory, "sessions");
2299
+ }
2300
+ sessions = /* @__PURE__ */ new Map();
2301
+ harnesses;
2302
+ timings;
2303
+ herd;
2304
+ log;
2305
+ directory;
2306
+ flushing;
2307
+ flushTimer;
2308
+ now() {
2309
+ return (this.options.now ?? Date.now)();
2310
+ }
2311
+ spoolFile(threadId) {
2312
+ return path.join(this.directory, `${threadId}.spool`);
2313
+ }
2314
+ recordFile(threadId) {
2315
+ return path.join(this.directory, `${threadId}.json`);
2316
+ }
2317
+ save(session) {
2318
+ if (session.fenced || session.disposed) return;
2319
+ writeJsonAtomic(this.recordFile(session.record.thread_id), session.record);
2320
+ }
2321
+ /** The heartbeat's sessions. */
2322
+ list() {
2323
+ return [...this.sessions.values()].filter((session) => !session.fenced).map((session) => session.heartbeat());
2324
+ }
2325
+ /** Picks up every recorded session from whichever terminal host runs it. */
2326
+ async resume() {
2327
+ mkdirSync(this.directory, { recursive: true, mode: 448 });
2328
+ const files = readdirSync(this.directory).filter(
2329
+ (name) => name.endsWith(".json")
2330
+ );
2331
+ const terminals = await listTerminals(
2332
+ hostSockets(this.options.terminalsDirectory, this.options.terminalSocket)
2333
+ );
2334
+ for (const file of files) {
2335
+ const record = readJson(path.join(this.directory, file));
2336
+ if (!record?.thread_id || this.sessions.has(record.thread_id)) continue;
2337
+ const session = new Session(record, this);
2338
+ this.sessions.set(record.thread_id, session);
2339
+ const found = terminals.find(
2340
+ (terminal) => terminal.id === record.terminal
2341
+ );
2342
+ if (record.ended || !found || found.exited) {
2343
+ session.end(record.ended ?? "its terminal is gone");
2344
+ this.settle(session);
2345
+ continue;
2346
+ }
2347
+ record.socket = found.socket;
2348
+ try {
2349
+ await session.drive(await HostClient.connect(found.socket));
2350
+ this.log.line(
2351
+ `${record.thread_id}: picked up ${record.terminal} on ${path.basename(found.socket)}`
2352
+ );
2353
+ } catch (error) {
2354
+ session.end(`its terminal host cannot be reached: ${errorText(error)}`);
2355
+ }
2356
+ }
2357
+ }
2358
+ /** Handles a command the poller recorded; anything else is ignored. */
2359
+ async handle(command) {
2360
+ switch (command.type) {
2361
+ case "work.assign": {
2362
+ await this.assign(command);
2363
+ return true;
2364
+ }
2365
+ case "thread.message": {
2366
+ await this.message(command);
2367
+ return true;
2368
+ }
2369
+ case "thread.stop":
2370
+ case "control.drop_session": {
2371
+ await this.stop(
2372
+ command.thread_id,
2373
+ command.type
2374
+ );
2375
+ return true;
2376
+ }
2377
+ default: {
2378
+ return false;
2379
+ }
2380
+ }
2381
+ }
2382
+ async assign(command) {
2383
+ const { lease } = command;
2384
+ const existing = this.sessions.get(lease.thread_id);
2385
+ if (existing && existing.record.lease_id === lease.lease_id) return;
2386
+ if (existing) await this.fence(existing);
2387
+ const opened = await this.options.access.open(command.request);
2388
+ const harness = opened.ok ? opened.payload.needs?.harness : void 0;
2389
+ const record = {
2390
+ thread_id: lease.thread_id,
2391
+ lease_id: lease.lease_id,
2392
+ epoch: lease.epoch,
2393
+ harness: harness ?? "claude",
2394
+ started_at: new Date(this.now()).toISOString(),
2395
+ queue: [],
2396
+ prompted: false,
2397
+ messages: [],
2398
+ transcript: { base: 0, uploaded: 0, seqOffset: 0 },
2399
+ outbox: []
2400
+ };
2401
+ const session = new Session(record, this);
2402
+ this.sessions.set(record.thread_id, session);
2403
+ const refuse = (reason) => {
2404
+ this.log.line(`work.assign for ${record.thread_id} refused: ${reason}`);
2405
+ this.event(session, { type: "status", text: `refused: ${reason}` });
2406
+ session.end(reason);
2407
+ this.settle(session);
2408
+ };
2409
+ if (!opened.ok) {
2410
+ refuse(`the request did not pass the accept check (${opened.reason})`);
2411
+ return;
2412
+ }
2413
+ const request = opened.payload;
2414
+ const entry = harness ? this.harnesses[harness] : void 0;
2415
+ if (!entry || typeof request.prompt !== "string" || !request.repo) {
2416
+ refuse("the request is not one this link can run");
2417
+ return;
2418
+ }
2419
+ record.repo = request.repo;
2420
+ record.queue = [request.prompt];
2421
+ const checkout = await this.options.repos.find(request.repo);
2422
+ if (!checkout) {
2423
+ refuse(
2424
+ `no checkout of ${request.repo.owner}/${request.repo.name} under ${this.options.paths.repos}`
2425
+ );
2426
+ return;
2427
+ }
2428
+ const prepared = await prepareWorktree({
2429
+ checkout: checkout.path,
2430
+ workDirectory: this.options.paths.work,
2431
+ threadId: record.thread_id,
2432
+ baseRef: request.base_ref,
2433
+ reposConfig: this.options.paths.reposConfig
2434
+ });
2435
+ if (!prepared.ok) {
2436
+ refuse(`the worktree could not be made: ${prepared.reason}`);
2437
+ return;
2438
+ }
2439
+ record.worktree = prepared.worktree;
2440
+ const socket = this.options.terminalSocket;
2441
+ let client;
2442
+ try {
2443
+ if (socket === void 0) throw new Error("no terminal host is set");
2444
+ client = await HostClient.connect(socket);
2445
+ } catch (error) {
2446
+ refuse(`the terminal host cannot be reached: ${errorText(error)}`);
2447
+ return;
2448
+ }
2449
+ const created = await client.request({
2450
+ type: "create",
2451
+ file: entry.command,
2452
+ args: entry.args,
2453
+ cwd: prepared.worktree.path,
2454
+ label: `thread:${record.thread_id}`
2455
+ });
2456
+ if (!created.ok) {
2457
+ client.close();
2458
+ refuse(`${entry.command} could not start: ${created.error?.message}`);
2459
+ return;
2460
+ }
2461
+ record.socket = socket;
2462
+ record.terminal = created.terminal;
2463
+ this.save(session);
2464
+ this.log.line(
2465
+ `${record.thread_id}: started ${entry.command} in ${prepared.worktree.path} as ${record.terminal}`
2466
+ );
2467
+ await session.drive(client);
2468
+ }
2469
+ async message(command) {
2470
+ const session = this.sessions.get(command.thread_id);
2471
+ if (!session || session.record.ended) {
2472
+ this.log.line(
2473
+ `thread.message for ${command.thread_id}, which runs no session here; acked`
2474
+ );
2475
+ return;
2476
+ }
2477
+ const opened = await this.options.access.open(command.message);
2478
+ if (!opened.ok || typeof opened.payload.text !== "string") {
2479
+ const reason = opened.ok ? "malformed" : opened.reason;
2480
+ this.log.line(
2481
+ `thread.message for ${command.thread_id} refused: ${reason}`
2482
+ );
2483
+ this.event(session, {
2484
+ type: "status",
2485
+ text: `refused a follow-up: ${reason}`
2486
+ });
2487
+ return;
2488
+ }
2489
+ const { message_id: messageId, text } = opened.payload;
2490
+ if (session.record.messages.includes(messageId)) return;
2491
+ session.record.messages.push(messageId);
2492
+ session.queueText(text);
2493
+ }
2494
+ async stop(threadId, why) {
2495
+ const session = this.sessions.get(threadId);
2496
+ if (!session || session.record.ended) {
2497
+ this.log.line(
2498
+ `${why} for ${threadId}, which runs no session here; acked`
2499
+ );
2500
+ return;
2501
+ }
2502
+ this.log.line(`${threadId}: ${why}; ending the session`);
2503
+ await session.kill();
2504
+ session.end(why);
2505
+ }
2506
+ /** After each poll: fences, then everything still owed to the herd. */
2507
+ async afterPoll(answer) {
2508
+ await this.leases(answer.leases);
2509
+ await this.flush();
2510
+ await Promise.all(
2511
+ [...this.sessions.values()].filter((session) => session.record.ended !== void 0).map((session) => session.upload())
2512
+ );
2513
+ }
2514
+ /** Applies the lease statuses of a poll answer: a fenced lease ends its session silently. */
2515
+ async leases(leases) {
2516
+ for (const lease of leases) {
2517
+ const session = this.sessions.get(lease.thread_id);
2518
+ if (lease.status === "fenced" && session && session.record.lease_id === lease.lease_id) {
2519
+ await this.fence(session);
2520
+ }
2521
+ }
2522
+ }
2523
+ /** Kills the terminal and forgets the session, sending nothing more for it. */
2524
+ async fence(session) {
2525
+ if (session.fenced) return;
2526
+ session.fenced = true;
2527
+ this.log.line(
2528
+ `${session.record.thread_id}: lease ${session.record.lease_id} is fenced; ending the session and sending nothing more`
2529
+ );
2530
+ if (!session.record.ended) await session.kill();
2531
+ this.forget(session);
2532
+ }
2533
+ forget(session) {
2534
+ session.dispose();
2535
+ if (this.sessions.get(session.record.thread_id) === session) {
2536
+ this.sessions.delete(session.record.thread_id);
2537
+ }
2538
+ rmSync(this.recordFile(session.record.thread_id), { force: true });
2539
+ session.transcript.remove();
2540
+ }
2541
+ /** Forgets a session once everything about it reached the herd. */
2542
+ settle(session) {
2543
+ if (session.fenced) return;
2544
+ if (session.settled()) this.forget(session);
2545
+ }
2546
+ /** Queues an event for the herd and sends it soon. */
2547
+ event(session, event) {
2548
+ if (session.fenced) return;
2549
+ session.record.outbox.push({
2550
+ event_id: newEventId(),
2551
+ thread_id: session.record.thread_id,
2552
+ lease_id: session.record.lease_id,
2553
+ epoch: session.record.epoch,
2554
+ at: new Date(this.now()).toISOString(),
2555
+ ...event
2556
+ });
2557
+ this.save(session);
2558
+ clearTimeout(this.flushTimer);
2559
+ this.flushTimer = setTimeout(() => void this.flush(), 50);
2560
+ }
2561
+ /** Sends every queued event, at most 100 per request. */
2562
+ flush() {
2563
+ this.flushing ??= (async () => {
2564
+ try {
2565
+ await this.flushOnce();
2566
+ } finally {
2567
+ this.flushing = void 0;
2568
+ }
2569
+ })();
2570
+ return this.flushing;
2571
+ }
2572
+ async flushOnce() {
2573
+ const owners = /* @__PURE__ */ new Map();
2574
+ const events = [];
2575
+ for (const session of this.sessions.values()) {
2576
+ for (const event of session.record.outbox) {
2577
+ if (events.length >= 100) break;
2578
+ owners.set(event.event_id, session);
2579
+ events.push(event);
2580
+ }
2581
+ }
2582
+ if (events.length === 0) return;
2583
+ const answer = await this.herd.post("/v1/events", { events });
2584
+ const results = answer.ok ? answer.body.results : void 0;
2585
+ if (!Array.isArray(results)) {
2586
+ this.log.line(
2587
+ `could not send ${events.length} thread events: ${answer.ok ? `HTTP ${answer.status}` : answer.reason}; they go again`
2588
+ );
2589
+ return;
2590
+ }
2591
+ const fenced = /* @__PURE__ */ new Set();
2592
+ for (const result of results) {
2593
+ const session = owners.get(result.event_id);
2594
+ if (!session) continue;
2595
+ session.record.outbox = session.record.outbox.filter(
2596
+ (event) => event.event_id !== result.event_id
2597
+ );
2598
+ if (result.status === "fenced") fenced.add(session);
2599
+ if (result.status === "invalid") {
2600
+ this.log.line(
2601
+ `the herd called event ${result.event_id} of ${session.record.thread_id} invalid`
2602
+ );
2603
+ }
2604
+ }
2605
+ for (const session of new Set(owners.values())) {
2606
+ if (fenced.has(session)) {
2607
+ await this.fence(session);
2608
+ } else {
2609
+ this.save(session);
2610
+ this.settle(session);
2611
+ }
2612
+ }
2613
+ }
2614
+ /** Ends every timer and connection, leaving the terminals running. */
2615
+ dispose() {
2616
+ clearTimeout(this.flushTimer);
2617
+ for (const session of this.sessions.values()) session.dispose();
2618
+ }
2619
+ }
2620
+ const SETUP_LABEL = "setup:";
2621
+ function isProgram(value) {
2622
+ return typeof value === "string" && Object.hasOwn(SETUP_PROGRAMS, value);
2623
+ }
2624
+ const REPO_PART = /^[A-Za-z0-9_][A-Za-z0-9_.-]*$/u;
2625
+ function isAddRepo(value) {
2626
+ if (typeof value !== "object" || value === null) return false;
2627
+ const { owner, name } = value;
2628
+ return Object.keys(value).length === 2 && typeof owner === "string" && typeof name === "string" && REPO_PART.test(owner) && REPO_PART.test(name) && owner.length <= 100 && name.length <= 100;
2629
+ }
2630
+ class SetupTerminals {
2631
+ constructor(options) {
2632
+ this.options = options;
2633
+ this.home = options.home ?? homedir();
2634
+ }
2635
+ home;
2636
+ sockets() {
2637
+ return hostSockets(
2638
+ this.options.terminalsDirectory,
2639
+ this.options.terminalSocket
2640
+ );
2641
+ }
2642
+ /** `terminal.open`: checks the envelope and the program, then starts it. */
2643
+ async open(command) {
2644
+ const { log } = this.options;
2645
+ const opened = await this.options.access.open(
2646
+ command.request
2647
+ );
2648
+ if (!opened.ok) {
2649
+ log.line(`terminal.open refused: ${opened.reason}`);
2650
+ return;
2651
+ }
2652
+ const { open_id: openId, program, cwd, repo } = opened.payload;
2653
+ if (!isProgram(program)) {
2654
+ log.line(
2655
+ `terminal.open ${String(openId)} refused: ${JSON.stringify(program)} is not a setup program`
2656
+ );
2657
+ return;
2658
+ }
2659
+ if (program === "repo-add" ? !isAddRepo(repo) : repo !== void 0) {
2660
+ log.line(
2661
+ program === "repo-add" ? `terminal.open ${openId} refused: repo-add needs a repository whose owner and name are letters, digits, _, . and -, starting with neither - nor .` : `terminal.open ${openId} refused: only repo-add takes a repository`
2662
+ );
2663
+ return;
2664
+ }
2665
+ if (cwd?.kind !== "home" && cwd?.kind !== "repo") {
2666
+ log.line(`terminal.open ${openId} refused: unknown working directory`);
2667
+ return;
2668
+ }
2669
+ let directory = this.home;
2670
+ if (cwd.kind === "repo" && program !== "repo-add") {
2671
+ const checkout = await this.options.repos.find(cwd.repo);
2672
+ if (!checkout) {
2673
+ log.line(
2674
+ `terminal.open ${openId} refused: no checkout of ${cwd.repo.owner}/${cwd.repo.name}`
2675
+ );
2676
+ return;
2677
+ }
2678
+ directory = checkout.path;
2679
+ }
2680
+ const label = `${SETUP_LABEL}${openId}`;
2681
+ const existing = await listTerminals(this.sockets());
2682
+ if (existing.some((terminal) => terminal.label === label)) return;
2683
+ const searchPath = this.searchPath();
2684
+ const run = this.command(program, repo);
2685
+ const args = run.args;
2686
+ const file = resolveProgram(run.file, searchPath);
2687
+ const socket = this.options.terminalSocket;
2688
+ if (socket === void 0) {
2689
+ log.line(`terminal.open ${openId}: no terminal host is set`);
2690
+ return;
2691
+ }
2692
+ let client;
2693
+ try {
2694
+ client = await HostClient.connect(socket);
2695
+ } catch (error) {
2696
+ log.line(
2697
+ `terminal.open ${openId}: the terminal host cannot be reached: ${error.message}`
2698
+ );
2699
+ return;
2700
+ }
2701
+ try {
2702
+ const created = await client.request({
2703
+ type: "create",
2704
+ file,
2705
+ args,
2706
+ cwd: directory,
2707
+ env: { PATH: searchPath },
2708
+ label
2709
+ });
2710
+ log.line(
2711
+ created.ok ? `terminal.open ${openId}: ${file} in ${directory} as ${String(created.terminal)}` : `terminal.open ${openId}: ${file} could not start: ${created.error?.message}`
2712
+ );
2713
+ } finally {
2714
+ client.close();
2715
+ }
2716
+ }
2717
+ /** The file and arguments a program runs; `repo` is checked already. */
2718
+ command(program, repo) {
2719
+ if (program === "repo-add" && repo) {
2720
+ return {
2721
+ file: path.join(this.options.binDirectory, "aibexd"),
2722
+ args: ["repo", "add", `${repo.owner}/${repo.name}`]
2723
+ };
2724
+ }
2725
+ if (program === "shell") {
2726
+ return {
2727
+ file: this.options.shell ?? process.env.SHELL ?? "/bin/sh",
2728
+ args: ["-l"]
2729
+ };
2730
+ }
2731
+ return { file: SETUP_PROGRAMS[program] ?? "/bin/sh", args: [] };
2732
+ }
2733
+ /** `<state>/bin` first, so `aibexd activate` resolves in a harness started here. */
2734
+ searchPath() {
2735
+ const rest = (this.options.path ?? process.env.PATH ?? "").split(path.delimiter).filter(
2736
+ (directory) => directory !== "" && directory !== this.options.binDirectory
2737
+ );
2738
+ return [this.options.binDirectory, ...rest].join(path.delimiter);
2739
+ }
2740
+ /** Every setup terminal a host still keeps, running or exited. */
2741
+ async list() {
2742
+ const terminals = await listTerminals(this.sockets());
2743
+ return terminals.filter((terminal) => terminal.label?.startsWith(SETUP_LABEL)).map((terminal) => {
2744
+ const program = this.program(terminal.file);
2745
+ const entry = {
2746
+ open_id: (terminal.label ?? "").slice(SETUP_LABEL.length),
2747
+ terminal: terminal.id,
2748
+ program,
2749
+ cwd: program === "repo-add" ? { kind: "home" } : this.cwd(terminal.cwd)
2750
+ };
2751
+ if (program === "repo-add") {
2752
+ const [owner, name] = (terminal.args?.[2] ?? "").split("/");
2753
+ if (owner && name) entry.repo = { owner, name };
2754
+ }
2755
+ if (terminal.exited) entry.exit = terminal.exited;
2756
+ return entry;
2757
+ });
2758
+ }
2759
+ program(file) {
2760
+ const name = path.basename(file);
2761
+ const found = Object.entries(SETUP_PROGRAMS).find(
2762
+ ([, command]) => command === name
2763
+ );
2764
+ return found?.[0] ?? "shell";
2765
+ }
2766
+ cwd(directory) {
2767
+ const relative = path.relative(
2768
+ path.join(this.options.reposDirectory, GITHUB_HOST),
2769
+ directory
2770
+ );
2771
+ const [owner, name, ...rest] = relative.split(path.sep);
2772
+ if (!relative.startsWith("..") && !path.isAbsolute(relative) && owner && name && rest.length === 0) {
2773
+ const repo = { owner, name };
2774
+ return { kind: "repo", repo };
2775
+ }
2776
+ return { kind: "home" };
2777
+ }
2778
+ }
2779
+ function resolveProgram(file, searchPath) {
2780
+ if (file.includes("/")) return file;
2781
+ for (const directory of searchPath.split(path.delimiter)) {
2782
+ if (directory === "") continue;
2783
+ const candidate = path.join(directory, file);
2784
+ try {
2785
+ if (!statSync(candidate).isFile()) continue;
2786
+ accessSync(candidate, constants.X_OK);
2787
+ return candidate;
2788
+ } catch {
2789
+ }
2790
+ }
2791
+ return file;
2792
+ }
2793
+ function send(message) {
2794
+ return new Promise((resolve) => {
2795
+ if (!process.send) {
2796
+ resolve(false);
2797
+ return;
2798
+ }
2799
+ process.send(message, void 0, {}, (error) => resolve(error === null));
2800
+ });
2801
+ }
2802
+ function initialSelfUpdate() {
2803
+ try {
2804
+ const value = JSON.parse(process.env.AIBEX_SELF_UPDATE ?? "");
2805
+ if (typeof value.can === "boolean") {
2806
+ return {
2807
+ can: value.can,
2808
+ reason: typeof value.reason === "string" ? value.reason : null
2809
+ };
2810
+ }
2811
+ } catch {
2812
+ }
2813
+ return { can: true, reason: null };
2814
+ }
2815
+ function main() {
2816
+ const info = readPackageInfo();
2817
+ if (process.argv.includes("--self-test")) {
2818
+ process.stdout.write(
2819
+ `${JSON.stringify({ name: info.name, version: info.version })}
2820
+ `
2821
+ );
2822
+ return;
2823
+ }
2824
+ const host = process.env.AIBEX_HOST;
2825
+ const hostDirectory = process.env.AIBEX_HOST_DIR;
2826
+ if (!host || !hostDirectory || !process.send) {
2827
+ process.stderr.write(
2828
+ "aibex-link is started by aibexd: it needs AIBEX_HOST, AIBEX_HOST_DIR and an IPC channel\n"
2829
+ );
2830
+ process.exitCode = 1;
2831
+ return;
2832
+ }
2833
+ const log = new LinkLog();
2834
+ let status = {
2835
+ type: "status",
2836
+ aibexdVersion: process.env.AIBEX_AIBEXD_VERSION ?? "unknown",
2837
+ selfUpdate: initialSelfUpdate()
2838
+ };
2839
+ const identity = readIdentity(hostDirectory);
2840
+ const paths = linkPaths(hostDirectory);
2841
+ const access = new Access(paths.access);
2842
+ const repos = new Repos(paths.repos);
2843
+ const activations = new Activations(hostDirectory);
2844
+ const terminalSocket = process.env.AIBEX_TERMINAL_SOCKET;
2845
+ const terminalsDirectory = process.env.AIBEX_TERMINALS_DIR;
2846
+ let poller;
2847
+ let sessions;
2848
+ if (identity) {
2849
+ sessions = new Sessions({
2850
+ paths,
2851
+ access,
2852
+ repos,
2853
+ herd: new HerdClient(identity),
2854
+ log,
2855
+ terminalSocket,
2856
+ terminalsDirectory,
2857
+ timings: Object.fromEntries(
2858
+ Object.entries(DEFAULT_SESSION_TIMINGS).map(
2859
+ ([key, fallback]) => [key, timing(key, fallback)]
2860
+ )
2861
+ )
2862
+ });
2863
+ const setup = new SetupTerminals({
2864
+ access,
2865
+ repos,
2866
+ reposDirectory: paths.repos,
2867
+ binDirectory: path.join(paths.state, "bin"),
2868
+ terminalSocket,
2869
+ terminalsDirectory,
2870
+ log
2871
+ });
2872
+ const running = sessions;
2873
+ const relay = new Relay({
2874
+ host: identity.host,
2875
+ herd: new HerdClient(identity),
2876
+ access,
2877
+ log,
2878
+ terminalSocket,
2879
+ terminalsDirectory,
2880
+ timings: {
2881
+ idleCloseMs: timing(
2882
+ "relayIdleCloseMs",
2883
+ DEFAULT_RELAY_TIMINGS.idleCloseMs
2884
+ )
2885
+ }
2886
+ });
2887
+ log.line(
2888
+ `aibex-link ${info.version} linked to ${host} as ${identity.machine_id} (${identity.machine_name}) in herd ${identity.herd_id}`
2889
+ );
2890
+ poller = new Poller({
2891
+ identity,
2892
+ hostDirectory,
2893
+ linkVersion: info.version,
2894
+ log,
2895
+ handled: new HandledCommands(hostDirectory),
2896
+ status: () => status,
2897
+ harnesses: detectHarnesses(),
2898
+ access,
2899
+ report: async () => collectReport({
2900
+ paths,
2901
+ repos,
2902
+ access,
2903
+ features: ["sessions", "relay", "setup"],
2904
+ sessions: running.list(),
2905
+ setupTerminals: await setup.list(),
2906
+ activations: activations.list()
2907
+ }),
2908
+ handleCommand: async (command) => {
2909
+ if (command.type === "relay.open") {
2910
+ await relay.open();
2911
+ return true;
2912
+ }
2913
+ if (command.type !== "terminal.open") return running.handle(command);
2914
+ await setup.open(command);
2915
+ return true;
2916
+ },
2917
+ afterPoll: async (answer) => {
2918
+ activations.confirm(answer.activations_confirmed);
2919
+ await running.afterPoll(answer);
2920
+ },
2921
+ forward: (commandId, request) => send({ type: "control", commandId, request }),
2922
+ backoffInitialMs: timing(
2923
+ "pollBackoffInitialMs",
2924
+ DEFAULT_POLL_BACKOFF_INITIAL_MS
2925
+ ),
2926
+ backoffMaxMs: timing("pollBackoffMaxMs", DEFAULT_POLL_BACKOFF_MAX_MS)
2927
+ });
2928
+ } else {
2929
+ log.line(
2930
+ `aibex-link ${info.version} linked to ${host}, not joined: run \`aibexd join <key>\` to join this machine to a herd`
2931
+ );
2932
+ }
2933
+ const activate = (code) => {
2934
+ if (!poller) {
2935
+ return {
2936
+ type: "activated",
2937
+ code,
2938
+ ok: false,
2939
+ reason: "this machine has not joined a herd: run `aibexd join <key>` first"
2940
+ };
2941
+ }
2942
+ if (!activations.add(code)) {
2943
+ return {
2944
+ type: "activated",
2945
+ code,
2946
+ ok: false,
2947
+ reason: "not an activation code"
2948
+ };
2949
+ }
2950
+ log.line(`activation ${code} taken; reporting it until the herd confirms`);
2951
+ return { type: "activated", code, ok: true };
2952
+ };
2953
+ process.on("message", (message) => {
2954
+ switch (message.type) {
2955
+ case "status": {
2956
+ status = message;
2957
+ break;
2958
+ }
2959
+ case "activate": {
2960
+ void send(activate(message.code));
2961
+ break;
2962
+ }
2963
+ case "diagnostics": {
2964
+ const mismatch = access.mismatch;
2965
+ poller?.attachDiagnostics({
2966
+ ...message.diagnostics,
2967
+ warnings: [
2968
+ ...message.diagnostics.warnings,
2969
+ ...mismatch === void 0 ? [] : [`access.json does not match the herd's trust: ${mismatch}`]
2970
+ ],
2971
+ collected_at: (/* @__PURE__ */ new Date()).toISOString(),
2972
+ command_id: message.commandId,
2973
+ log_tail: log.tail()
2974
+ });
2975
+ break;
2976
+ }
2977
+ }
2978
+ });
2979
+ void send({ type: "ready" });
962
2980
  setInterval(() => void send({ type: "alive" }), aliveIntervalMs());
963
- poller?.start();
2981
+ if (poller && sessions) {
2982
+ const [started, recorded] = [poller, sessions];
2983
+ void (async () => {
2984
+ try {
2985
+ await recorded.resume();
2986
+ } catch (error) {
2987
+ log.line(`could not pick up recorded sessions: ${String(error)}`);
2988
+ }
2989
+ started.start();
2990
+ })();
2991
+ }
964
2992
  process.on("disconnect", () => process.exit(0));
965
2993
  process.on("SIGTERM", () => process.exit(0));
966
2994
  process.on("SIGINT", () => process.exit(0));