@aibexhq/aibex-link 0.0.7 → 0.0.9

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.
Files changed (2) hide show
  1. package/dist/index.js +1057 -235
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import path from "node:path";
2
- import { readFileSync, mkdirSync, writeFileSync, chmodSync, renameSync, rmSync, existsSync, readdirSync, statSync, appendFileSync, openSync, readSync, closeSync, cpSync, accessSync, constants } from "node:fs";
2
+ import { readFileSync, mkdirSync, writeFileSync, chmodSync, renameSync, rmSync, existsSync, readdirSync, cpSync, statSync, mkdtempSync, appendFileSync, openSync, readSync, closeSync, accessSync, constants } from "node:fs";
3
3
  import { randomBytes, createHash } from "node:crypto";
4
- import { platform, arch, release, homedir } from "node:os";
4
+ import { platform, arch, release, tmpdir, homedir } from "node:os";
5
+ import { execFile } from "node:child_process";
5
6
  import { fileURLToPath } from "node:url";
6
7
  import { createConnection } from "node:net";
7
- import { execFile } from "node:child_process";
8
8
  const encoder = new TextEncoder();
9
9
  function utf8(text) {
10
10
  return new Uint8Array(encoder.encode(text));
@@ -888,6 +888,10 @@ async function collectReport(options) {
888
888
  if (options.activations && options.activations.length > 0) {
889
889
  report.activations = options.activations;
890
890
  }
891
+ if (options.worktrees) report.worktrees = options.worktrees;
892
+ if (options.releases && options.releases.length > 0) {
893
+ report.releases = options.releases;
894
+ }
891
895
  return report;
892
896
  }
893
897
  function buildHeartbeat(input) {
@@ -907,15 +911,625 @@ function buildHeartbeat(input) {
907
911
  sessions: input.report.sessions,
908
912
  acks: input.acks
909
913
  };
910
- const { repos, access, features, setup_terminals, activations } = input.report;
914
+ const {
915
+ repos,
916
+ access,
917
+ features,
918
+ setup_terminals,
919
+ activations,
920
+ worktrees,
921
+ releases
922
+ } = input.report;
911
923
  if (repos) heartbeat.repos = repos;
912
924
  if (access) heartbeat.access = access;
913
925
  if (features) heartbeat.features = features;
914
926
  if (setup_terminals) heartbeat.setup_terminals = setup_terminals;
915
927
  if (activations) heartbeat.activations = activations;
928
+ if (worktrees) heartbeat.worktrees = worktrees;
929
+ if (releases) heartbeat.releases = releases;
916
930
  if (input.diagnostics) heartbeat.diagnostics = input.diagnostics;
917
931
  return heartbeat;
918
932
  }
933
+ const GITHUB_HOST = "github.com";
934
+ const REPO_SCAN_MS = 6e4;
935
+ const GITHUB_REMOTE = [
936
+ /^https:\/\/(?:[^@/]+@)?github\.com\/([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/u,
937
+ /^(?:ssh:\/\/)?git@github\.com[:/]([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/u
938
+ ];
939
+ function parseGitHubRemote(url) {
940
+ for (const pattern of GITHUB_REMOTE) {
941
+ const match = pattern.exec(url.trim());
942
+ if (match?.[1] && match[2]) return { owner: match[1], name: match[2] };
943
+ }
944
+ return void 0;
945
+ }
946
+ function git(cwd, args, environment) {
947
+ return new Promise((resolve) => {
948
+ execFile(
949
+ "git",
950
+ ["-C", cwd, ...args],
951
+ environment ? { env: { ...process.env, ...environment } } : {},
952
+ (failure, stdout, stderr) => resolve({
953
+ ok: failure === null,
954
+ stdout: stdout.trim(),
955
+ stderr: stderr.trim()
956
+ })
957
+ );
958
+ });
959
+ }
960
+ function directories(parent) {
961
+ try {
962
+ return readdirSync(parent, { withFileTypes: true }).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")).map((entry) => entry.name).toSorted();
963
+ } catch {
964
+ return [];
965
+ }
966
+ }
967
+ async function scanRepos(reposDirectory) {
968
+ const github = path.join(reposDirectory, GITHUB_HOST);
969
+ const candidates = directories(github).flatMap(
970
+ (owner) => directories(path.join(github, owner)).map((name) => path.join(github, owner, name)).filter((directory) => existsSync(path.join(directory, ".git")))
971
+ );
972
+ const found = await Promise.all(
973
+ candidates.map(async (directory) => {
974
+ const origin = await git(directory, [
975
+ "config",
976
+ "--get",
977
+ "remote.origin.url"
978
+ ]);
979
+ const repo = origin.ok ? parseGitHubRemote(origin.stdout) : void 0;
980
+ return repo ? { repo, path: directory } : void 0;
981
+ })
982
+ );
983
+ return found.filter((checkout) => checkout !== void 0);
984
+ }
985
+ class Repos {
986
+ constructor(reposDirectory, now = Date.now) {
987
+ this.reposDirectory = reposDirectory;
988
+ this.now = now;
989
+ }
990
+ scanned = [];
991
+ scannedAt = Number.NEGATIVE_INFINITY;
992
+ async list(options = {}) {
993
+ if (options.fresh || this.now() - this.scannedAt >= REPO_SCAN_MS) {
994
+ this.scanned = await scanRepos(this.reposDirectory);
995
+ this.scannedAt = this.now();
996
+ }
997
+ return this.scanned;
998
+ }
999
+ /** The checkout for a repository, rescanning once when it is not known. */
1000
+ async find(repo) {
1001
+ const match = (checkouts) => checkouts.find(
1002
+ (checkout) => checkout.repo.owner.toLowerCase() === repo.owner.toLowerCase() && checkout.repo.name.toLowerCase() === repo.name.toLowerCase()
1003
+ );
1004
+ return match(await this.list()) ?? match(await this.list({ fresh: true }));
1005
+ }
1006
+ }
1007
+ const DEFAULT_CARRY = [".mcp.json", ".claude/settings.local.json"];
1008
+ function carryList(reposConfig) {
1009
+ const value = readJson(reposConfig);
1010
+ const carry = value?.carry;
1011
+ if (!Array.isArray(carry)) return DEFAULT_CARRY;
1012
+ return carry.filter(
1013
+ (entry) => typeof entry === "string" && entry !== "" && !path.isAbsolute(entry) && !entry.split(/[/\\]/u).includes("..")
1014
+ );
1015
+ }
1016
+ function salvageReference(threadId) {
1017
+ return `refs/aibex/salvage/${threadId}`;
1018
+ }
1019
+ async function defaultBranch(checkout) {
1020
+ const head = await git(checkout, [
1021
+ "symbolic-ref",
1022
+ "--short",
1023
+ "refs/remotes/origin/HEAD"
1024
+ ]);
1025
+ return head.ok && head.stdout.startsWith("origin/") ? head.stdout.slice("origin/".length) : "main";
1026
+ }
1027
+ async function restoreWorktree(options) {
1028
+ const { checkout, worktreePath, branch, salvage } = options;
1029
+ const added = await git(checkout, [
1030
+ "worktree",
1031
+ "add",
1032
+ "--quiet",
1033
+ "-B",
1034
+ branch,
1035
+ worktreePath,
1036
+ `${salvage}^1`
1037
+ ]);
1038
+ if (!added.ok) {
1039
+ return { ok: false, reason: added.stderr || "git worktree add failed" };
1040
+ }
1041
+ for (const step of [
1042
+ ["read-tree", "-u", "--reset", salvage],
1043
+ ["reset", "--quiet"]
1044
+ ]) {
1045
+ const result = await git(worktreePath, step);
1046
+ if (!result.ok) {
1047
+ return {
1048
+ ok: false,
1049
+ reason: `git ${step[0]} failed while restoring ${salvage}: ${result.stderr}`
1050
+ };
1051
+ }
1052
+ }
1053
+ return { ok: true };
1054
+ }
1055
+ async function prepareWorktree(options) {
1056
+ const { checkout, threadId } = options;
1057
+ const worktreePath = path.join(options.workDirectory, threadId);
1058
+ const branch = `aibex/${threadId}`;
1059
+ const fetched = await git(checkout, ["fetch", "--quiet", "origin"]);
1060
+ const base = options.baseRef ?? await defaultBranch(checkout);
1061
+ let restored;
1062
+ const known = await git(checkout, [
1063
+ "rev-parse",
1064
+ "--verify",
1065
+ "--quiet",
1066
+ `refs/heads/${branch}`
1067
+ ]);
1068
+ if (!existsSync(path.join(worktreePath, ".git"))) {
1069
+ mkdirSync(options.workDirectory, { recursive: true });
1070
+ const salvage = await git(checkout, [
1071
+ "rev-parse",
1072
+ "--verify",
1073
+ "--quiet",
1074
+ salvageReference(threadId)
1075
+ ]);
1076
+ const salvagedHead = salvage.ok ? await git(checkout, ["rev-parse", `${salvage.stdout}^1`]) : void 0;
1077
+ if (salvage.ok && (!known.ok || salvagedHead?.ok && salvagedHead.stdout === known.stdout)) {
1078
+ const rebuilt = await restoreWorktree({
1079
+ checkout,
1080
+ worktreePath,
1081
+ branch,
1082
+ salvage: salvage.stdout
1083
+ });
1084
+ if (!rebuilt.ok) return rebuilt;
1085
+ restored = salvage.stdout;
1086
+ }
1087
+ }
1088
+ if (restored === void 0 && !existsSync(path.join(worktreePath, ".git"))) {
1089
+ const added = await git(
1090
+ checkout,
1091
+ known.ok ? ["worktree", "add", "--quiet", worktreePath, branch] : [
1092
+ "worktree",
1093
+ "add",
1094
+ "--quiet",
1095
+ "-b",
1096
+ branch,
1097
+ worktreePath,
1098
+ `origin/${base}`
1099
+ ]
1100
+ );
1101
+ if (!added.ok) {
1102
+ const why = added.stderr || `git worktree add failed`;
1103
+ return {
1104
+ ok: false,
1105
+ reason: fetched.ok ? why : `${why} (git fetch failed too: ${fetched.stderr})`
1106
+ };
1107
+ }
1108
+ }
1109
+ const carried = [];
1110
+ for (const file of carryList(options.reposConfig)) {
1111
+ const from = path.join(checkout, file);
1112
+ const to = path.join(worktreePath, file);
1113
+ if (!existsSync(from) || existsSync(to)) continue;
1114
+ const tracked = await git(checkout, [
1115
+ "ls-files",
1116
+ "--error-unmatch",
1117
+ "--",
1118
+ file
1119
+ ]);
1120
+ if (tracked.ok) continue;
1121
+ mkdirSync(path.dirname(to), { recursive: true });
1122
+ cpSync(from, to, { recursive: true });
1123
+ carried.push(file);
1124
+ }
1125
+ return {
1126
+ ok: true,
1127
+ worktree: { path: worktreePath, branch, base },
1128
+ carried,
1129
+ ...restored === void 0 ? {} : { restored }
1130
+ };
1131
+ }
1132
+ const WORKTREE_SCAN_MS = 6e4;
1133
+ const HALF_MADE_AGE_MS = 10 * 6e4;
1134
+ const THREAD_ID = /^t_[A-Za-z0-9]{8,64}$/u;
1135
+ const SALVAGE_IDENTITY = {
1136
+ GIT_AUTHOR_NAME: "aibex",
1137
+ GIT_AUTHOR_EMAIL: "aibex@localhost",
1138
+ GIT_COMMITTER_NAME: "aibex",
1139
+ GIT_COMMITTER_EMAIL: "aibex@localhost"
1140
+ };
1141
+ function threadDirectories(work) {
1142
+ try {
1143
+ return readdirSync(work, { withFileTypes: true }).filter((entry) => entry.isDirectory() && THREAD_ID.test(entry.name)).map((entry) => entry.name).toSorted();
1144
+ } catch {
1145
+ return [];
1146
+ }
1147
+ }
1148
+ function isWorktree(directory) {
1149
+ try {
1150
+ statSync(path.join(directory, ".git"));
1151
+ return true;
1152
+ } catch {
1153
+ return false;
1154
+ }
1155
+ }
1156
+ async function checkoutOf(worktree) {
1157
+ const common = await git(worktree, [
1158
+ "rev-parse",
1159
+ "--path-format=absolute",
1160
+ "--git-common-dir"
1161
+ ]);
1162
+ return common.ok && common.stdout ? path.dirname(common.stdout) : void 0;
1163
+ }
1164
+ async function hasUnpushed(worktree) {
1165
+ const count = await git(worktree, [
1166
+ "rev-list",
1167
+ "--count",
1168
+ "HEAD",
1169
+ "--not",
1170
+ "--remotes"
1171
+ ]);
1172
+ return count.ok && Number(count.stdout) > 0;
1173
+ }
1174
+ async function isDirty(worktree, carry) {
1175
+ const status = await git(worktree, [
1176
+ "status",
1177
+ "--porcelain",
1178
+ "-z",
1179
+ "--untracked-files=all"
1180
+ ]);
1181
+ if (!status.ok) return false;
1182
+ const entries = status.stdout.split("\0");
1183
+ for (let index = 0; index < entries.length; index++) {
1184
+ const entry = entries[index] ?? "";
1185
+ if (entry === "") continue;
1186
+ const code = entry.slice(0, 2);
1187
+ const file = entry.slice(3);
1188
+ if (code[0] === "R" || code[0] === "C") index++;
1189
+ const carried = code === "??" && carry.some((item) => file === item || file.startsWith(`${item}/`));
1190
+ if (!carried) return true;
1191
+ }
1192
+ return false;
1193
+ }
1194
+ async function scanWorktrees(work, reposConfig) {
1195
+ const carry = carryList(reposConfig);
1196
+ const listings = [];
1197
+ for (const threadId of threadDirectories(work)) {
1198
+ const directory = path.join(work, threadId);
1199
+ let createdAt;
1200
+ try {
1201
+ createdAt = statSync(directory).mtime.toISOString();
1202
+ } catch {
1203
+ continue;
1204
+ }
1205
+ const listing = {
1206
+ thread_id: threadId,
1207
+ dirty: false,
1208
+ unpushed: false,
1209
+ created_at: createdAt
1210
+ };
1211
+ if (isWorktree(directory)) {
1212
+ const head = await git(directory, ["rev-parse", "--verify", "HEAD"]);
1213
+ if (head.ok && /^[0-9a-f]{40}$/u.test(head.stdout)) {
1214
+ listing.head = head.stdout;
1215
+ listing.dirty = await isDirty(directory, carry);
1216
+ listing.unpushed = await hasUnpushed(directory);
1217
+ }
1218
+ }
1219
+ listings.push(listing);
1220
+ }
1221
+ return listings;
1222
+ }
1223
+ async function salvageCommit(options) {
1224
+ const { worktree } = options;
1225
+ const scratch = mkdtempSync(path.join(tmpdir(), "aibex-salvage-"));
1226
+ const environment = {
1227
+ ...SALVAGE_IDENTITY,
1228
+ GIT_INDEX_FILE: path.join(scratch, "index")
1229
+ };
1230
+ try {
1231
+ const steps = [
1232
+ ["read-tree", options.head],
1233
+ ["add", "--all", "--", "."]
1234
+ ];
1235
+ for (const file of options.carry) {
1236
+ const tracked = await git(worktree, [
1237
+ "cat-file",
1238
+ "-e",
1239
+ `${options.head}:${file}`
1240
+ ]);
1241
+ if (!tracked.ok) {
1242
+ steps.push([
1243
+ "rm",
1244
+ "-r",
1245
+ "--cached",
1246
+ "--quiet",
1247
+ "--ignore-unmatch",
1248
+ "--",
1249
+ file
1250
+ ]);
1251
+ }
1252
+ }
1253
+ for (const step of steps) {
1254
+ const result = await git(worktree, step, environment);
1255
+ if (!result.ok) {
1256
+ return {
1257
+ ok: false,
1258
+ reason: `git ${step[0]} failed: ${result.stderr}`
1259
+ };
1260
+ }
1261
+ }
1262
+ const tree = await git(worktree, ["write-tree"], environment);
1263
+ if (!tree.ok) {
1264
+ return { ok: false, reason: `git write-tree failed: ${tree.stderr}` };
1265
+ }
1266
+ const commit = await git(
1267
+ worktree,
1268
+ [
1269
+ "commit-tree",
1270
+ tree.stdout,
1271
+ "-p",
1272
+ options.head,
1273
+ ...options.previous ? ["-p", options.previous] : [],
1274
+ "-m",
1275
+ `aibex salvage ${options.threadId} epoch ${options.epoch}`
1276
+ ],
1277
+ environment
1278
+ );
1279
+ if (!commit.ok) {
1280
+ return { ok: false, reason: `git commit-tree failed: ${commit.stderr}` };
1281
+ }
1282
+ return { ok: true, commit: commit.stdout };
1283
+ } finally {
1284
+ rmSync(scratch, { recursive: true, force: true });
1285
+ }
1286
+ }
1287
+ async function releaseWorktree(options) {
1288
+ const { threadId } = options;
1289
+ if (options.sessionListed) {
1290
+ return {
1291
+ outcome: "refused",
1292
+ reason: "a session for this thread is listed"
1293
+ };
1294
+ }
1295
+ if (!THREAD_ID.test(threadId)) {
1296
+ return { outcome: "refused", reason: "not a thread id" };
1297
+ }
1298
+ const worktree = path.join(options.work, threadId);
1299
+ let exists = true;
1300
+ try {
1301
+ statSync(worktree);
1302
+ } catch {
1303
+ exists = false;
1304
+ }
1305
+ if (!exists) {
1306
+ let saved;
1307
+ for (const checkout2 of options.checkouts) {
1308
+ await git(checkout2, ["worktree", "prune"]);
1309
+ const dropped2 = await dropBranch(checkout2, threadId, options.epoch);
1310
+ if (!dropped2.ok) return { outcome: "refused", reason: dropped2.reason };
1311
+ saved = dropped2.salvage ?? saved;
1312
+ }
1313
+ return saved === void 0 ? { outcome: "released" } : { outcome: "released", salvage: saved };
1314
+ }
1315
+ if (!isWorktree(worktree)) {
1316
+ rmSync(worktree, { recursive: true, force: true });
1317
+ return { outcome: "released" };
1318
+ }
1319
+ const checkout = await checkoutOf(worktree);
1320
+ const head = await git(worktree, ["rev-parse", "--verify", "HEAD"]);
1321
+ if (!checkout || !head.ok) {
1322
+ return {
1323
+ outcome: "refused",
1324
+ reason: `not a usable worktree: ${head.stderr || "no checkout"}`
1325
+ };
1326
+ }
1327
+ const reference = salvageReference(threadId);
1328
+ const carry = carryList(options.reposConfig);
1329
+ const dirty = await isDirty(worktree, carry);
1330
+ const unpushed = await hasUnpushed(worktree);
1331
+ let salvage;
1332
+ if (dirty || unpushed) {
1333
+ const previous = await git(checkout, [
1334
+ "rev-parse",
1335
+ "--verify",
1336
+ "--quiet",
1337
+ reference
1338
+ ]);
1339
+ const made = await salvageCommit({
1340
+ worktree,
1341
+ threadId,
1342
+ epoch: options.epoch,
1343
+ head: head.stdout,
1344
+ carry,
1345
+ previous: previous.ok ? previous.stdout : void 0
1346
+ });
1347
+ if (!made.ok) return { outcome: "refused", reason: made.reason };
1348
+ const updated = await git(checkout, [
1349
+ "update-ref",
1350
+ "-m",
1351
+ `aibex salvage ${threadId}`,
1352
+ reference,
1353
+ made.commit
1354
+ ]);
1355
+ if (!updated.ok) {
1356
+ return {
1357
+ outcome: "refused",
1358
+ reason: `could not write ${reference}: ${updated.stderr}`
1359
+ };
1360
+ }
1361
+ salvage = made.commit;
1362
+ }
1363
+ const removed = await git(checkout, [
1364
+ "worktree",
1365
+ "remove",
1366
+ "--force",
1367
+ worktree
1368
+ ]);
1369
+ if (!removed.ok) {
1370
+ return {
1371
+ outcome: "refused",
1372
+ reason: `git worktree remove failed: ${removed.stderr}`
1373
+ };
1374
+ }
1375
+ const dropped = await dropBranch(checkout, threadId, options.epoch);
1376
+ if (!dropped.ok) return { outcome: "refused", reason: dropped.reason };
1377
+ salvage = dropped.salvage ?? salvage;
1378
+ return salvage === void 0 ? { outcome: "released" } : { outcome: "released", salvage };
1379
+ }
1380
+ async function dropBranch(checkout, threadId, epoch) {
1381
+ const branch = `aibex/${threadId}`;
1382
+ const known = await git(checkout, [
1383
+ "rev-parse",
1384
+ "--verify",
1385
+ "--quiet",
1386
+ `refs/heads/${branch}`
1387
+ ]);
1388
+ if (!known.ok) return { ok: true };
1389
+ const reference = salvageReference(threadId);
1390
+ const previous = await git(checkout, [
1391
+ "rev-parse",
1392
+ "--verify",
1393
+ "--quiet",
1394
+ reference
1395
+ ]);
1396
+ const unsaved = await git(checkout, [
1397
+ "rev-list",
1398
+ "--count",
1399
+ known.stdout,
1400
+ "--not",
1401
+ "--remotes",
1402
+ ...previous.ok ? [previous.stdout] : []
1403
+ ]);
1404
+ if (!unsaved.ok) {
1405
+ return { ok: false, reason: `git rev-list failed: ${unsaved.stderr}` };
1406
+ }
1407
+ let salvage;
1408
+ if (Number(unsaved.stdout) > 0) {
1409
+ const commit = await git(
1410
+ checkout,
1411
+ [
1412
+ "commit-tree",
1413
+ `${known.stdout}^{tree}`,
1414
+ "-p",
1415
+ known.stdout,
1416
+ ...previous.ok ? ["-p", previous.stdout] : [],
1417
+ "-m",
1418
+ `aibex salvage ${threadId} epoch ${epoch}`
1419
+ ],
1420
+ SALVAGE_IDENTITY
1421
+ );
1422
+ if (!commit.ok) {
1423
+ return { ok: false, reason: `git commit-tree failed: ${commit.stderr}` };
1424
+ }
1425
+ const updated = await git(checkout, [
1426
+ "update-ref",
1427
+ "-m",
1428
+ `aibex salvage ${threadId}`,
1429
+ reference,
1430
+ commit.stdout
1431
+ ]);
1432
+ if (!updated.ok) {
1433
+ return {
1434
+ ok: false,
1435
+ reason: `could not write ${reference}: ${updated.stderr}`
1436
+ };
1437
+ }
1438
+ salvage = commit.stdout;
1439
+ }
1440
+ const deleted = await git(checkout, ["branch", "-D", "--quiet", branch]);
1441
+ if (!deleted.ok) {
1442
+ return { ok: false, reason: `git branch -D failed: ${deleted.stderr}` };
1443
+ }
1444
+ return salvage === void 0 ? { ok: true } : { ok: true, salvage };
1445
+ }
1446
+ async function sweep(options) {
1447
+ const removed = [];
1448
+ for (const checkout of options.checkouts) {
1449
+ await git(checkout, ["worktree", "prune"]);
1450
+ }
1451
+ let names = [];
1452
+ try {
1453
+ names = readdirSync(options.sessionsDirectory);
1454
+ } catch {
1455
+ }
1456
+ const records = new Set(
1457
+ names.filter((name) => name.endsWith(".json")).map((name) => name.slice(0, -5))
1458
+ );
1459
+ for (const name of names) {
1460
+ if (!name.endsWith(".spool") || records.has(name.slice(0, -6))) continue;
1461
+ rmSync(path.join(options.sessionsDirectory, name), { force: true });
1462
+ removed.push(path.join(options.sessionsDirectory, name));
1463
+ }
1464
+ for (const threadId of threadDirectories(options.work)) {
1465
+ const directory = path.join(options.work, threadId);
1466
+ if (isWorktree(directory)) continue;
1467
+ try {
1468
+ if (options.now - statSync(directory).mtimeMs < HALF_MADE_AGE_MS)
1469
+ continue;
1470
+ } catch {
1471
+ continue;
1472
+ }
1473
+ rmSync(directory, { recursive: true, force: true });
1474
+ removed.push(directory);
1475
+ }
1476
+ return removed;
1477
+ }
1478
+ class Reaper {
1479
+ constructor(options) {
1480
+ this.options = options;
1481
+ }
1482
+ listing = [];
1483
+ scannedAt = Number.NEGATIVE_INFINITY;
1484
+ pending = [];
1485
+ inFlight = [];
1486
+ now() {
1487
+ return (this.options.now ?? Date.now)();
1488
+ }
1489
+ /** The listing and the outcomes to report in the next heartbeat. */
1490
+ async report() {
1491
+ const now = this.now();
1492
+ if (now - this.scannedAt >= (this.options.scanMs ?? WORKTREE_SCAN_MS)) {
1493
+ this.listing = await scanWorktrees(
1494
+ this.options.work,
1495
+ this.options.reposConfig
1496
+ );
1497
+ this.scannedAt = now;
1498
+ }
1499
+ this.inFlight = [...this.pending];
1500
+ return { worktrees: this.listing, releases: this.inFlight };
1501
+ }
1502
+ /** The poll that carried the last report succeeded. */
1503
+ delivered() {
1504
+ this.pending = this.pending.filter(
1505
+ (result) => !this.inFlight.includes(result)
1506
+ );
1507
+ this.inFlight = [];
1508
+ }
1509
+ /** Handles one `work.release`. */
1510
+ async release(command, sessionListed) {
1511
+ const outcome = await releaseWorktree({
1512
+ work: this.options.work,
1513
+ threadId: command.thread_id,
1514
+ epoch: command.epoch,
1515
+ reposConfig: this.options.reposConfig,
1516
+ checkouts: await this.options.checkouts(),
1517
+ sessionListed
1518
+ });
1519
+ const result = {
1520
+ command_id: command.command_id,
1521
+ thread_id: command.thread_id,
1522
+ epoch: command.epoch,
1523
+ ...outcome
1524
+ };
1525
+ this.options.log(
1526
+ outcome.outcome === "released" ? `${command.thread_id}: worktree released${outcome.salvage ? `; its work is saved at ${salvageReference(command.thread_id)} (${outcome.salvage})` : ""}` : `${command.thread_id}: release refused: ${outcome.reason}`
1527
+ );
1528
+ this.pending.push(result);
1529
+ this.scannedAt = Number.NEGATIVE_INFINITY;
1530
+ return result;
1531
+ }
1532
+ }
919
1533
  const LOG_TAIL_LINES = 50;
920
1534
  class LinkLog {
921
1535
  constructor(write = (line) => process.stdout.write(`${line}
@@ -1643,106 +2257,33 @@ class Relay {
1643
2257
  const viewerId = request.id;
1644
2258
  let client;
1645
2259
  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()
2260
+ client = await this.connection(socket);
2261
+ } catch {
2262
+ this.refuse(conn, viewerId, "the terminal host cannot be reached");
2263
+ return;
2264
+ }
2265
+ const { id: _id, ...body } = request;
2266
+ let held = [];
2267
+ const event = (message) => this.send({
2268
+ type: "event",
2269
+ conn,
2270
+ message: { ...message, id: viewerId }
2271
+ });
2272
+ const reply = await client.subscribe(
2273
+ body,
2274
+ request.type === "attach" ? (message) => {
2275
+ if (held) held.push(message);
2276
+ else event(message);
2277
+ } : void 0
1744
2278
  );
1745
- return match(await this.list()) ?? match(await this.list({ fresh: true }));
2279
+ this.send({
2280
+ type: "reply",
2281
+ conn,
2282
+ message: { ...reply, id: viewerId }
2283
+ });
2284
+ const queued = held;
2285
+ held = void 0;
2286
+ for (const message of queued) event(message);
1746
2287
  }
1747
2288
  }
1748
2289
  const HARNESSES = {
@@ -1781,6 +2322,48 @@ const SETUP_PROGRAMS = {
1781
2322
  function bracketedPaste(text) {
1782
2323
  return `\x1B[200~${text}\x1B[201~`;
1783
2324
  }
2325
+ function transitionSession(phase, change) {
2326
+ switch (change.type) {
2327
+ case "launched": {
2328
+ return phase === "preparing" ? "launching" : void 0;
2329
+ }
2330
+ case "prompted": {
2331
+ return phase === "launching" ? "running" : void 0;
2332
+ }
2333
+ case "host-lost": {
2334
+ return phase === "launching" || phase === "running" ? "detached" : void 0;
2335
+ }
2336
+ case "reattached": {
2337
+ if (phase !== "detached") return void 0;
2338
+ return change.prompted ? "running" : "launching";
2339
+ }
2340
+ case "ended": {
2341
+ return phase === "ended" || phase === "fenced" ? void 0 : "ended";
2342
+ }
2343
+ case "fenced": {
2344
+ return phase === "fenced" ? void 0 : "fenced";
2345
+ }
2346
+ }
2347
+ }
2348
+ function reportedState(phase, hints) {
2349
+ switch (phase) {
2350
+ case "preparing": {
2351
+ return { state: "starting" };
2352
+ }
2353
+ case "launching": {
2354
+ return hints.notReady ? { state: "waiting", waiting_on: "login" } : { state: "starting" };
2355
+ }
2356
+ case "running": {
2357
+ return { state: hints.activity };
2358
+ }
2359
+ case "detached": {
2360
+ return { state: "waiting", waiting_on: "host" };
2361
+ }
2362
+ case "ended": {
2363
+ return { state: "ended" };
2364
+ }
2365
+ }
2366
+ }
1784
2367
  const MAX_CHUNK_BYTES = 1024 * 1024;
1785
2368
  class Transcript {
1786
2369
  constructor(spoolFile, state) {
@@ -1878,79 +2461,6 @@ class Transcript {
1878
2461
  rmSync(this.spoolFile, { force: true });
1879
2462
  }
1880
2463
  }
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
2464
  const DEFAULT_SESSION_TIMINGS = {
1955
2465
  readyTimeoutMs: 6e4,
1956
2466
  checkMs: 250,
@@ -1958,7 +2468,9 @@ const DEFAULT_SESSION_TIMINGS = {
1958
2468
  statusIntervalMs: 5e3,
1959
2469
  uploadIntervalMs: 5e3,
1960
2470
  uploadThresholdBytes: 256 * 1024,
1961
- settleMs: 150
2471
+ settleMs: 150,
2472
+ reattachMs: 5e3,
2473
+ reattachMaxMs: 6e4
1962
2474
  };
1963
2475
  const STATUS_MAX = 200;
1964
2476
  function newEventId() {
@@ -1974,24 +2486,38 @@ class Session {
1974
2486
  const now = manager.now();
1975
2487
  this.lastProgressAt = now;
1976
2488
  this.quietSince = now;
1977
- this.readySince = now;
1978
2489
  this.transcript = new Transcript(
1979
2490
  manager.spoolFile(record.thread_id),
1980
2491
  record.transcript
1981
2492
  );
1982
- if (record.ended) this.state = "ended";
1983
- else if (record.prompted) this.state = "idle";
2493
+ if (record.ended) this.phase = "ended";
2494
+ else if (record.terminal === void 0) this.phase = "preparing";
2495
+ else if (record.prompted) {
2496
+ this.phase = "running";
2497
+ this.activity = "idle";
2498
+ } else {
2499
+ this.phase = "launching";
2500
+ this.readySince = now;
2501
+ }
1984
2502
  }
1985
2503
  client;
1986
- state = "starting";
1987
- waitingOn;
2504
+ phase;
2505
+ /** The host's last output hint, once the prompt is in. */
2506
+ activity = "working";
2507
+ /** The harness showed no ready marker within the timeout. */
2508
+ notReady = false;
1988
2509
  lastProgressAt;
1989
2510
  busy = false;
1990
2511
  lastScreenSeq = -1;
1991
2512
  quietSince;
1992
2513
  lastSendAt = 0;
1993
2514
  sending = false;
2515
+ /** When the session entered `launching`: the ready clock starts there. */
1994
2516
  readySince;
2517
+ /** A stop that came while detached, carried out on reattach. */
2518
+ stopPending;
2519
+ reattachDelayMs = 0;
2520
+ reattachTimer;
1995
2521
  timer;
1996
2522
  uploadTimer;
1997
2523
  uploading;
@@ -1999,10 +2525,20 @@ class Session {
1999
2525
  statusSent;
2000
2526
  statusPending;
2001
2527
  statusTimer;
2002
- /** Set when the server fenced the lease: nothing more is sent. */
2003
- fenced = false;
2004
2528
  disposed = false;
2005
2529
  transcript;
2530
+ /** Set when the server fenced the lease: nothing more is sent. */
2531
+ get fenced() {
2532
+ return this.phase === "fenced";
2533
+ }
2534
+ /** Moves through `transitionSession`; answers whether the phase changed. */
2535
+ move(change) {
2536
+ const next = transitionSession(this.phase, change);
2537
+ if (next === void 0) return false;
2538
+ this.phase = next;
2539
+ if (next === "launching") this.readySince ??= this.manager.now();
2540
+ return true;
2541
+ }
2006
2542
  get harness() {
2007
2543
  return this.manager.harnesses[this.record.harness];
2008
2544
  }
@@ -2012,23 +2548,26 @@ class Session {
2012
2548
  save() {
2013
2549
  this.manager.save(this);
2014
2550
  }
2015
- /** Attaches for the transcript and watches for state, then starts checking. */
2551
+ /**
2552
+ * Attaches for the transcript and watches for state, then starts checking.
2553
+ * Losing the connection, now or later, detaches the session rather than
2554
+ * ending it: the terminal may still run.
2555
+ */
2016
2556
  async drive(client) {
2017
2557
  this.client = client;
2018
2558
  const terminal = this.record.terminal;
2019
2559
  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
- }
2560
+ if (this.client === client)
2561
+ this.detach("the terminal host connection closed");
2025
2562
  });
2026
2563
  const watched = await client.subscribe(
2027
2564
  { type: "watch", terminal },
2028
2565
  (event) => this.onWatch(event)
2029
2566
  );
2030
2567
  if (!watched.ok) {
2031
- this.end("the terminal is gone");
2568
+ if (watched.error?.code === "closed") {
2569
+ this.detach("the terminal host connection closed");
2570
+ } else this.end("the terminal is gone");
2032
2571
  return;
2033
2572
  }
2034
2573
  const since = this.transcript.seq();
@@ -2054,11 +2593,76 @@ class Session {
2054
2593
  this.end("exited");
2055
2594
  return;
2056
2595
  }
2596
+ if (this.client !== client || this.phase === "detached") return;
2597
+ this.reattachDelayMs = 0;
2598
+ this.stopTimers();
2057
2599
  this.timer = setInterval(() => void this.check(), this.timings.checkMs);
2058
2600
  this.uploadTimer = setInterval(
2059
2601
  () => void this.upload(),
2060
2602
  this.timings.uploadIntervalMs
2061
2603
  );
2604
+ const pending = this.stopPending;
2605
+ if (pending !== void 0) {
2606
+ this.stopPending = void 0;
2607
+ await this.manager.stopSession(this, pending);
2608
+ }
2609
+ }
2610
+ /**
2611
+ * The host connection is gone, or a subscription was dropped: the session
2612
+ * is `detached`, listed as waiting on the host, and looks for its terminal
2613
+ * again with backoff.
2614
+ */
2615
+ detach(reason) {
2616
+ if (this.disposed || !this.move({ type: "host-lost" })) return;
2617
+ this.manager.log.line(
2618
+ `${this.record.thread_id}: detached from its terminal (${reason}); finding it again`
2619
+ );
2620
+ clearInterval(this.timer);
2621
+ const client = this.client;
2622
+ this.client = void 0;
2623
+ client?.close();
2624
+ this.scheduleReattach();
2625
+ }
2626
+ scheduleReattach() {
2627
+ if (this.disposed || this.phase !== "detached") return;
2628
+ this.reattachDelayMs = this.reattachDelayMs === 0 ? this.timings.reattachMs : Math.min(this.reattachDelayMs * 2, this.timings.reattachMaxMs);
2629
+ clearTimeout(this.reattachTimer);
2630
+ this.reattachTimer = setTimeout(
2631
+ () => void this.reattach(),
2632
+ this.reattachDelayMs
2633
+ );
2634
+ }
2635
+ /** Finds the terminal on any host and drives it again, or ends the session when it is gone. */
2636
+ async reattach() {
2637
+ if (this.disposed || this.phase !== "detached") return;
2638
+ const found = await this.manager.locate(this.record);
2639
+ if (this.disposed || this.phase !== "detached") return;
2640
+ if (found.kind === "unreachable") {
2641
+ this.scheduleReattach();
2642
+ return;
2643
+ }
2644
+ if (found.kind !== "running") {
2645
+ this.end(found.kind === "exited" ? "exited" : "its terminal is gone");
2646
+ return;
2647
+ }
2648
+ let client;
2649
+ try {
2650
+ client = await HostClient.connect(found.socket);
2651
+ } catch {
2652
+ this.scheduleReattach();
2653
+ return;
2654
+ }
2655
+ if (this.disposed || this.phase !== "detached") {
2656
+ client.close();
2657
+ return;
2658
+ }
2659
+ this.record.socket = found.socket;
2660
+ this.move({ type: "reattached", prompted: this.record.prompted });
2661
+ this.save();
2662
+ this.manager.log.line(
2663
+ `${this.record.thread_id}: found ${this.record.terminal} again on ${path.basename(found.socket)}`
2664
+ );
2665
+ await this.drive(client);
2062
2666
  }
2063
2667
  onOutput(event) {
2064
2668
  if (event.event === "output" && !this.fenced) {
@@ -2068,6 +2672,8 @@ class Session {
2068
2672
  }
2069
2673
  } else if (event.event === "exit") {
2070
2674
  this.end("exited");
2675
+ } else if (event.event === "dropped") {
2676
+ this.detach("the terminal host dropped the output subscription");
2071
2677
  }
2072
2678
  }
2073
2679
  onWatch(event) {
@@ -2080,12 +2686,12 @@ class Session {
2080
2686
  }
2081
2687
  case "active": {
2082
2688
  this.busy = true;
2083
- if (this.record.prompted && !this.record.ended) this.state = "working";
2689
+ this.activity = "working";
2084
2690
  break;
2085
2691
  }
2086
2692
  case "idle": {
2087
2693
  this.busy = false;
2088
- if (this.record.prompted && !this.record.ended) this.state = "idle";
2694
+ this.activity = "idle";
2089
2695
  break;
2090
2696
  }
2091
2697
  case "title": {
@@ -2097,6 +2703,10 @@ class Session {
2097
2703
  this.end("exited");
2098
2704
  break;
2099
2705
  }
2706
+ case "dropped": {
2707
+ this.detach("the terminal host dropped the watch subscription");
2708
+ break;
2709
+ }
2100
2710
  }
2101
2711
  }
2102
2712
  /** Whether output has been quiet long enough to type. */
@@ -2110,7 +2720,7 @@ class Session {
2110
2720
  }
2111
2721
  /** Types the prompt once the harness is ready, then one follow-up per idle. */
2112
2722
  async check() {
2113
- if (this.sending || this.record.ended || this.fenced || !this.client) {
2723
+ if (this.sending || this.phase !== "launching" && this.phase !== "running" || !this.client) {
2114
2724
  return;
2115
2725
  }
2116
2726
  const next = this.record.queue[0];
@@ -2128,9 +2738,8 @@ class Session {
2128
2738
  const quiet = this.quiet(screen.seq);
2129
2739
  const ready = this.record.prompted || this.harness.ready.test(String(screen.text));
2130
2740
  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";
2741
+ if (this.phase === "launching" && !this.notReady && this.readySince !== void 0 && this.manager.now() - this.readySince >= this.timings.readyTimeoutMs) {
2742
+ this.notReady = true;
2134
2743
  this.setStatus("harness not ready");
2135
2744
  this.manager.log.line(
2136
2745
  `${this.record.thread_id}: ${this.record.harness} showed no ready marker in ${Math.round(this.timings.readyTimeoutMs / 1e3)}s; nothing typed`
@@ -2143,6 +2752,15 @@ class Session {
2143
2752
  this.sending = false;
2144
2753
  }
2145
2754
  }
2755
+ /** Whether the session may still be typed into by this client. */
2756
+ live(client) {
2757
+ return this.client === client && (this.phase === "launching" || this.phase === "running");
2758
+ }
2759
+ /**
2760
+ * Types one queued text and presses Enter. The phase is read again after
2761
+ * every await, so a session that ended, detached or was fenced meanwhile
2762
+ * is never marked as working.
2763
+ */
2146
2764
  async send(text) {
2147
2765
  const client = this.client;
2148
2766
  if (!client) return;
@@ -2153,15 +2771,22 @@ class Session {
2153
2771
  terminal,
2154
2772
  data: bracketedPaste(text)
2155
2773
  }) : await client.request({ type: "type", terminal, text });
2156
- if (!sent.ok) return;
2774
+ if (!sent.ok || !this.live(client)) return;
2157
2775
  await new Promise((resolve) => setTimeout(resolve, this.timings.settleMs));
2158
- await client.request({ type: "press", terminal, keys: ["Enter"] });
2776
+ if (!this.live(client)) return;
2777
+ const pressed = await client.request({
2778
+ type: "press",
2779
+ terminal,
2780
+ keys: ["Enter"]
2781
+ });
2782
+ if (!pressed.ok || !this.live(client)) return;
2159
2783
  this.record.queue.shift();
2160
2784
  const first = !this.record.prompted;
2161
2785
  this.record.prompted = true;
2162
2786
  this.lastSendAt = this.manager.now();
2163
- this.state = "working";
2164
- this.waitingOn = void 0;
2787
+ this.move({ type: "prompted" });
2788
+ this.activity = "working";
2789
+ this.notReady = false;
2165
2790
  this.save();
2166
2791
  this.manager.log.line(
2167
2792
  `${this.record.thread_id}: ${first ? "typed the prompt" : "typed a follow-up"} into ${this.record.harness}`
@@ -2198,19 +2823,32 @@ class Session {
2198
2823
  }
2199
2824
  /** The program exited, or the session never started: report it once. */
2200
2825
  end(reason) {
2201
- if (this.record.ended || this.fenced) return;
2826
+ if (this.record.ended || !this.move({ type: "ended" })) return;
2202
2827
  this.record.ended = reason;
2203
- this.state = "ended";
2204
2828
  this.stopTimers();
2829
+ clearTimeout(this.reattachTimer);
2205
2830
  this.manager.event(this, { type: "session.ended", without_done: true });
2206
2831
  this.manager.log.line(
2207
2832
  `${this.record.thread_id}: session ended (${reason})`
2208
2833
  );
2209
2834
  void this.upload();
2210
2835
  }
2836
+ /** Kills the terminal; the reply says whether the host confirmed it. */
2211
2837
  async kill() {
2212
- if (!this.client || this.record.terminal === void 0) return;
2213
- await this.client.request({ type: "kill", terminal: this.record.terminal });
2838
+ if (!this.client || this.record.terminal === void 0) {
2839
+ return {
2840
+ ok: false,
2841
+ error: { code: "closed", message: "no terminal host connection" }
2842
+ };
2843
+ }
2844
+ return this.client.request({
2845
+ type: "kill",
2846
+ terminal: this.record.terminal
2847
+ });
2848
+ }
2849
+ /** Remembers a stop that came while detached, for the reattach. */
2850
+ stopLater(why) {
2851
+ this.stopPending = why;
2214
2852
  }
2215
2853
  /** Sends the spool from the server's offset. */
2216
2854
  upload() {
@@ -2268,21 +2906,27 @@ class Session {
2268
2906
  this.disposed = true;
2269
2907
  this.stopTimers();
2270
2908
  clearTimeout(this.statusTimer);
2909
+ clearTimeout(this.reattachTimer);
2271
2910
  this.client?.close();
2272
2911
  }
2273
2912
  heartbeat() {
2274
2913
  const { record } = this;
2914
+ const phase = this.phase === "fenced" ? "ended" : this.phase;
2915
+ const reported = reportedState(phase, {
2916
+ activity: this.activity,
2917
+ notReady: this.notReady
2918
+ });
2275
2919
  const session = {
2276
2920
  thread_id: record.thread_id,
2277
2921
  lease_id: record.lease_id,
2278
2922
  epoch: record.epoch,
2279
2923
  harness: record.harness,
2280
- state: this.state,
2924
+ state: reported.state,
2281
2925
  last_progress_at: new Date(this.lastProgressAt).toISOString(),
2282
2926
  transcript_offset: record.transcript.base + record.transcript.uploaded,
2283
2927
  started_at: record.started_at
2284
2928
  };
2285
- if (this.waitingOn) session.waiting_on = this.waitingOn;
2929
+ if (reported.waiting_on) session.waiting_on = reported.waiting_on;
2286
2930
  if (record.repo) session.repo = record.repo;
2287
2931
  if (record.worktree) session.worktree = record.worktree;
2288
2932
  return session;
@@ -2322,28 +2966,44 @@ class Sessions {
2322
2966
  list() {
2323
2967
  return [...this.sessions.values()].filter((session) => !session.fenced).map((session) => session.heartbeat());
2324
2968
  }
2325
- /** Picks up every recorded session from whichever terminal host runs it. */
2969
+ /**
2970
+ * Picks up every recorded session from whichever terminal host runs it.
2971
+ * A record that never got its terminal ends: the link restarted while it
2972
+ * was being prepared, and its `work.assign` will not come again.
2973
+ */
2326
2974
  async resume() {
2327
2975
  mkdirSync(this.directory, { recursive: true, mode: 448 });
2328
2976
  const files = readdirSync(this.directory).filter(
2329
2977
  (name) => name.endsWith(".json")
2330
2978
  );
2331
- const terminals = await listTerminals(
2332
- hostSockets(this.options.terminalsDirectory, this.options.terminalSocket)
2333
- );
2334
2979
  for (const file of files) {
2335
2980
  const record = readJson(path.join(this.directory, file));
2336
2981
  if (!record?.thread_id || this.sessions.has(record.thread_id)) continue;
2337
2982
  const session = new Session(record, this);
2338
2983
  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");
2984
+ if (session.phase === "ended") {
2344
2985
  this.settle(session);
2345
2986
  continue;
2346
2987
  }
2988
+ if (session.phase === "preparing") {
2989
+ const why = "the link restarted before the session started";
2990
+ this.event(session, { type: "status", text: why });
2991
+ session.end(why);
2992
+ this.settle(session);
2993
+ continue;
2994
+ }
2995
+ const found = await this.locate(record);
2996
+ if (found.kind === "exited" || found.kind === "gone") {
2997
+ session.end(
2998
+ found.kind === "exited" ? "exited" : "its terminal is gone"
2999
+ );
3000
+ this.settle(session);
3001
+ continue;
3002
+ }
3003
+ if (found.kind === "unreachable") {
3004
+ session.detach("its terminal host cannot be reached");
3005
+ continue;
3006
+ }
2347
3007
  record.socket = found.socket;
2348
3008
  try {
2349
3009
  await session.drive(await HostClient.connect(found.socket));
@@ -2351,10 +3011,36 @@ class Sessions {
2351
3011
  `${record.thread_id}: picked up ${record.terminal} on ${path.basename(found.socket)}`
2352
3012
  );
2353
3013
  } catch (error) {
2354
- session.end(`its terminal host cannot be reached: ${errorText(error)}`);
3014
+ session.detach(
3015
+ `its terminal host cannot be reached: ${errorText(error)}`
3016
+ );
2355
3017
  }
2356
3018
  }
2357
3019
  }
3020
+ /**
3021
+ * Where a recorded session's terminal is: running on some host, exited,
3022
+ * gone (the host that ran it answers without it, or nothing listens on
3023
+ * its socket any more), or unreachable for now.
3024
+ */
3025
+ async locate(record) {
3026
+ const terminals = await listTerminals(
3027
+ hostSockets(this.options.terminalsDirectory, this.options.terminalSocket)
3028
+ );
3029
+ const found = terminals.find((terminal) => terminal.id === record.terminal);
3030
+ if (found) {
3031
+ return found.exited ? { kind: "exited" } : { kind: "running", socket: found.socket };
3032
+ }
3033
+ if (record.socket === void 0) return { kind: "gone" };
3034
+ try {
3035
+ const client = await HostClient.connect(record.socket);
3036
+ const listed = await client.request({ type: "list" });
3037
+ client.close();
3038
+ return listed.ok ? { kind: "gone" } : { kind: "unreachable" };
3039
+ } catch (error) {
3040
+ const code = error.code;
3041
+ return code === "ECONNREFUSED" || code === "ENOENT" ? { kind: "gone" } : { kind: "unreachable" };
3042
+ }
3043
+ }
2358
3044
  /** Handles a command the poller recorded; anything else is ignored. */
2359
3045
  async handle(command) {
2360
3046
  switch (command.type) {
@@ -2366,12 +3052,12 @@ class Sessions {
2366
3052
  await this.message(command);
2367
3053
  return true;
2368
3054
  }
2369
- case "thread.stop":
3055
+ case "thread.stop": {
3056
+ await this.stop(command.thread_id, command.type, command.lease_id);
3057
+ return true;
3058
+ }
2370
3059
  case "control.drop_session": {
2371
- await this.stop(
2372
- command.thread_id,
2373
- command.type
2374
- );
3060
+ await this.stop(command.thread_id, command.type);
2375
3061
  return true;
2376
3062
  }
2377
3063
  default: {
@@ -2379,18 +3065,22 @@ class Sessions {
2379
3065
  }
2380
3066
  }
2381
3067
  }
3068
+ /**
3069
+ * Starts a session for `work.assign`. Its record is saved before the first
3070
+ * await, so a link that restarts while it is being prepared ends it
3071
+ * visibly instead of forgetting it. A stop or fence while it is being
3072
+ * prepared ends the start after the step in flight.
3073
+ */
2382
3074
  async assign(command) {
2383
3075
  const { lease } = command;
2384
3076
  const existing = this.sessions.get(lease.thread_id);
2385
3077
  if (existing && existing.record.lease_id === lease.lease_id) return;
2386
3078
  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
3079
  const record = {
2390
3080
  thread_id: lease.thread_id,
2391
3081
  lease_id: lease.lease_id,
2392
3082
  epoch: lease.epoch,
2393
- harness: harness ?? "claude",
3083
+ harness: "claude",
2394
3084
  started_at: new Date(this.now()).toISOString(),
2395
3085
  queue: [],
2396
3086
  prompted: false,
@@ -2400,12 +3090,18 @@ class Sessions {
2400
3090
  };
2401
3091
  const session = new Session(record, this);
2402
3092
  this.sessions.set(record.thread_id, session);
3093
+ this.save(session);
3094
+ const stale = () => session.phase !== "preparing" || session.disposed;
2403
3095
  const refuse = (reason) => {
2404
3096
  this.log.line(`work.assign for ${record.thread_id} refused: ${reason}`);
2405
3097
  this.event(session, { type: "status", text: `refused: ${reason}` });
2406
3098
  session.end(reason);
2407
3099
  this.settle(session);
2408
3100
  };
3101
+ const opened = await this.options.access.open(command.request);
3102
+ if (stale()) return;
3103
+ const harness = opened.ok ? opened.payload.needs?.harness : void 0;
3104
+ if (harness) record.harness = harness;
2409
3105
  if (!opened.ok) {
2410
3106
  refuse(`the request did not pass the accept check (${opened.reason})`);
2411
3107
  return;
@@ -2418,7 +3114,9 @@ class Sessions {
2418
3114
  }
2419
3115
  record.repo = request.repo;
2420
3116
  record.queue = [request.prompt];
3117
+ this.save(session);
2421
3118
  const checkout = await this.options.repos.find(request.repo);
3119
+ if (stale()) return;
2422
3120
  if (!checkout) {
2423
3121
  refuse(
2424
3122
  `no checkout of ${request.repo.owner}/${request.repo.name} under ${this.options.paths.repos}`
@@ -2432,18 +3130,30 @@ class Sessions {
2432
3130
  baseRef: request.base_ref,
2433
3131
  reposConfig: this.options.paths.reposConfig
2434
3132
  });
3133
+ if (stale()) return;
2435
3134
  if (!prepared.ok) {
2436
3135
  refuse(`the worktree could not be made: ${prepared.reason}`);
2437
3136
  return;
2438
3137
  }
2439
3138
  record.worktree = prepared.worktree;
3139
+ if (prepared.restored !== void 0) {
3140
+ this.options.log.line(
3141
+ `${record.thread_id}: rebuilt its worktree from salvaged work (${prepared.restored})`
3142
+ );
3143
+ }
2440
3144
  const socket = this.options.terminalSocket;
2441
3145
  let client;
2442
3146
  try {
2443
3147
  if (socket === void 0) throw new Error("no terminal host is set");
2444
3148
  client = await HostClient.connect(socket);
2445
3149
  } catch (error) {
2446
- refuse(`the terminal host cannot be reached: ${errorText(error)}`);
3150
+ if (!stale()) {
3151
+ refuse(`the terminal host cannot be reached: ${errorText(error)}`);
3152
+ }
3153
+ return;
3154
+ }
3155
+ if (stale()) {
3156
+ client.close();
2447
3157
  return;
2448
3158
  }
2449
3159
  const created = await client.request({
@@ -2455,11 +3165,19 @@ class Sessions {
2455
3165
  });
2456
3166
  if (!created.ok) {
2457
3167
  client.close();
2458
- refuse(`${entry.command} could not start: ${created.error?.message}`);
3168
+ if (!stale()) {
3169
+ refuse(`${entry.command} could not start: ${created.error?.message}`);
3170
+ }
3171
+ return;
3172
+ }
3173
+ if (stale()) {
3174
+ await client.request({ type: "kill", terminal: created.terminal });
3175
+ client.close();
2459
3176
  return;
2460
3177
  }
2461
3178
  record.socket = socket;
2462
3179
  record.terminal = created.terminal;
3180
+ session.move({ type: "launched" });
2463
3181
  this.save(session);
2464
3182
  this.log.line(
2465
3183
  `${record.thread_id}: started ${entry.command} in ${prepared.worktree.path} as ${record.terminal}`
@@ -2474,6 +3192,12 @@ class Sessions {
2474
3192
  );
2475
3193
  return;
2476
3194
  }
3195
+ if (command.lease_id !== void 0 && command.lease_id !== session.record.lease_id) {
3196
+ this.log.line(
3197
+ `thread.message for lease ${command.lease_id} of ${command.thread_id}, but this session holds ${session.record.lease_id}; acked`
3198
+ );
3199
+ return;
3200
+ }
2477
3201
  const opened = await this.options.access.open(command.message);
2478
3202
  if (!opened.ok || typeof opened.payload.text !== "string") {
2479
3203
  const reason = opened.ok ? "malformed" : opened.reason;
@@ -2491,7 +3215,7 @@ class Sessions {
2491
3215
  session.record.messages.push(messageId);
2492
3216
  session.queueText(text);
2493
3217
  }
2494
- async stop(threadId, why) {
3218
+ async stop(threadId, why, leaseId) {
2495
3219
  const session = this.sessions.get(threadId);
2496
3220
  if (!session || session.record.ended) {
2497
3221
  this.log.line(
@@ -2499,9 +3223,41 @@ class Sessions {
2499
3223
  );
2500
3224
  return;
2501
3225
  }
3226
+ if (leaseId !== void 0 && leaseId !== session.record.lease_id) {
3227
+ this.log.line(
3228
+ `${why} for lease ${leaseId} of ${threadId}, but this session holds ${session.record.lease_id}; acked`
3229
+ );
3230
+ return;
3231
+ }
2502
3232
  this.log.line(`${threadId}: ${why}; ending the session`);
2503
- await session.kill();
2504
- session.end(why);
3233
+ await this.stopSession(session, why);
3234
+ }
3235
+ /**
3236
+ * Kills a session's terminal and ends it once the host confirms the kill,
3237
+ * or says the terminal is gone. A session being prepared ends at once; a
3238
+ * detached one, or one whose kill met a closed connection, is killed when
3239
+ * it reattaches.
3240
+ */
3241
+ async stopSession(session, why) {
3242
+ if (session.phase === "preparing") {
3243
+ session.end(why);
3244
+ return;
3245
+ }
3246
+ if (session.phase === "detached") {
3247
+ session.stopLater(why);
3248
+ return;
3249
+ }
3250
+ const reply = await session.kill();
3251
+ if (reply.ok || reply.error?.code === "exited") {
3252
+ session.end(why);
3253
+ } else if (reply.error?.code === "closed") {
3254
+ session.stopLater(why);
3255
+ session.detach("the terminal host connection closed during a kill");
3256
+ } else {
3257
+ session.end(
3258
+ reply.error?.code === "unknown-terminal" ? "its terminal is gone" : why
3259
+ );
3260
+ }
2505
3261
  }
2506
3262
  /** After each poll: fences, then everything still owed to the herd. */
2507
3263
  async afterPoll(answer) {
@@ -2520,16 +3276,50 @@ class Sessions {
2520
3276
  }
2521
3277
  }
2522
3278
  }
2523
- /** Kills the terminal and forgets the session, sending nothing more for it. */
3279
+ /**
3280
+ * Kills the terminal and forgets the session, sending nothing more for it.
3281
+ * A session with no live host connection (detached, or one that closed
3282
+ * under the kill) has its terminal found on any host and killed there,
3283
+ * since nothing will look for it once the session is forgotten.
3284
+ */
2524
3285
  async fence(session) {
2525
- if (session.fenced) return;
2526
- session.fenced = true;
3286
+ const ended = session.record.ended !== void 0;
3287
+ if (!session.move({ type: "fenced" })) return;
2527
3288
  this.log.line(
2528
3289
  `${session.record.thread_id}: lease ${session.record.lease_id} is fenced; ending the session and sending nothing more`
2529
3290
  );
2530
- if (!session.record.ended) await session.kill();
3291
+ if (!ended) {
3292
+ const reply = await session.kill();
3293
+ if (!reply.ok && reply.error?.code === "closed") {
3294
+ await this.killAnywhere(session.record);
3295
+ }
3296
+ }
2531
3297
  this.forget(session);
2532
3298
  }
3299
+ /** Finds a recorded terminal on whichever host runs it and kills it there. */
3300
+ async killAnywhere(record) {
3301
+ if (record.terminal === void 0) return;
3302
+ const found = await this.locate(record);
3303
+ if (found.kind === "unreachable") {
3304
+ this.log.line(
3305
+ `${record.thread_id}: could not reach the terminal host to kill ${record.terminal}`
3306
+ );
3307
+ return;
3308
+ }
3309
+ if (found.kind !== "running") return;
3310
+ try {
3311
+ const client = await HostClient.connect(found.socket);
3312
+ try {
3313
+ await client.request({ type: "kill", terminal: record.terminal });
3314
+ } finally {
3315
+ client.close();
3316
+ }
3317
+ } catch (error) {
3318
+ this.log.line(
3319
+ `${record.thread_id}: could not kill ${record.terminal}: ${errorText(error)}`
3320
+ );
3321
+ }
3322
+ }
2533
3323
  forget(session) {
2534
3324
  session.dispose();
2535
3325
  if (this.sessions.get(session.record.thread_id) === session) {
@@ -2845,6 +3635,7 @@ function main() {
2845
3635
  const terminalsDirectory = process.env.AIBEX_TERMINALS_DIR;
2846
3636
  let poller;
2847
3637
  let sessions;
3638
+ let reaper;
2848
3639
  if (identity) {
2849
3640
  sessions = new Sessions({
2850
3641
  paths,
@@ -2870,6 +3661,16 @@ function main() {
2870
3661
  log
2871
3662
  });
2872
3663
  const running = sessions;
3664
+ reaper = new Reaper({
3665
+ work: paths.work,
3666
+ reposConfig: paths.reposConfig,
3667
+ checkouts: async () => {
3668
+ const checkouts = await repos.list();
3669
+ return checkouts.map((checkout) => checkout.path);
3670
+ },
3671
+ log: (line) => log.line(line)
3672
+ });
3673
+ const reaping = reaper;
2873
3674
  const relay = new Relay({
2874
3675
  host: identity.host,
2875
3676
  herd: new HerdClient(identity),
@@ -2903,18 +3704,27 @@ function main() {
2903
3704
  features: ["sessions", "relay", "setup"],
2904
3705
  sessions: running.list(),
2905
3706
  setupTerminals: await setup.list(),
2906
- activations: activations.list()
3707
+ activations: activations.list(),
3708
+ ...await reaping.report()
2907
3709
  }),
2908
3710
  handleCommand: async (command) => {
2909
3711
  if (command.type === "relay.open") {
2910
3712
  await relay.open();
2911
3713
  return true;
2912
3714
  }
3715
+ if (command.type === "work.release") {
3716
+ const listed = running.list().some(
3717
+ (session) => session.thread_id === command.thread_id && session.state !== "ended"
3718
+ );
3719
+ await reaping.release(command, listed);
3720
+ return true;
3721
+ }
2913
3722
  if (command.type !== "terminal.open") return running.handle(command);
2914
3723
  await setup.open(command);
2915
3724
  return true;
2916
3725
  },
2917
3726
  afterPoll: async (answer) => {
3727
+ reaping.delivered();
2918
3728
  activations.confirm(answer.activations_confirmed);
2919
3729
  await running.afterPoll(answer);
2920
3730
  },
@@ -2981,6 +3791,18 @@ function main() {
2981
3791
  if (poller && sessions) {
2982
3792
  const [started, recorded] = [poller, sessions];
2983
3793
  void (async () => {
3794
+ try {
3795
+ const checkouts = await repos.list();
3796
+ const swept = await sweep({
3797
+ checkouts: checkouts.map((checkout) => checkout.path),
3798
+ work: paths.work,
3799
+ sessionsDirectory: path.join(hostDirectory, "sessions"),
3800
+ now: Date.now()
3801
+ });
3802
+ for (const removed of swept) log.line(`removed litter ${removed}`);
3803
+ } catch (error) {
3804
+ log.line(`the start-up sweep failed: ${String(error)}`);
3805
+ }
2984
3806
  try {
2985
3807
  await recorded.resume();
2986
3808
  } catch (error) {