@aibexhq/aibex-link 0.0.7 → 0.0.8

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 +375 -58
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1781,6 +1781,48 @@ const SETUP_PROGRAMS = {
1781
1781
  function bracketedPaste(text) {
1782
1782
  return `\x1B[200~${text}\x1B[201~`;
1783
1783
  }
1784
+ function transitionSession(phase, change) {
1785
+ switch (change.type) {
1786
+ case "launched": {
1787
+ return phase === "preparing" ? "launching" : void 0;
1788
+ }
1789
+ case "prompted": {
1790
+ return phase === "launching" ? "running" : void 0;
1791
+ }
1792
+ case "host-lost": {
1793
+ return phase === "launching" || phase === "running" ? "detached" : void 0;
1794
+ }
1795
+ case "reattached": {
1796
+ if (phase !== "detached") return void 0;
1797
+ return change.prompted ? "running" : "launching";
1798
+ }
1799
+ case "ended": {
1800
+ return phase === "ended" || phase === "fenced" ? void 0 : "ended";
1801
+ }
1802
+ case "fenced": {
1803
+ return phase === "fenced" ? void 0 : "fenced";
1804
+ }
1805
+ }
1806
+ }
1807
+ function reportedState(phase, hints) {
1808
+ switch (phase) {
1809
+ case "preparing": {
1810
+ return { state: "starting" };
1811
+ }
1812
+ case "launching": {
1813
+ return hints.notReady ? { state: "waiting", waiting_on: "login" } : { state: "starting" };
1814
+ }
1815
+ case "running": {
1816
+ return { state: hints.activity };
1817
+ }
1818
+ case "detached": {
1819
+ return { state: "waiting", waiting_on: "host" };
1820
+ }
1821
+ case "ended": {
1822
+ return { state: "ended" };
1823
+ }
1824
+ }
1825
+ }
1784
1826
  const MAX_CHUNK_BYTES = 1024 * 1024;
1785
1827
  class Transcript {
1786
1828
  constructor(spoolFile, state) {
@@ -1958,7 +2000,9 @@ const DEFAULT_SESSION_TIMINGS = {
1958
2000
  statusIntervalMs: 5e3,
1959
2001
  uploadIntervalMs: 5e3,
1960
2002
  uploadThresholdBytes: 256 * 1024,
1961
- settleMs: 150
2003
+ settleMs: 150,
2004
+ reattachMs: 5e3,
2005
+ reattachMaxMs: 6e4
1962
2006
  };
1963
2007
  const STATUS_MAX = 200;
1964
2008
  function newEventId() {
@@ -1974,24 +2018,38 @@ class Session {
1974
2018
  const now = manager.now();
1975
2019
  this.lastProgressAt = now;
1976
2020
  this.quietSince = now;
1977
- this.readySince = now;
1978
2021
  this.transcript = new Transcript(
1979
2022
  manager.spoolFile(record.thread_id),
1980
2023
  record.transcript
1981
2024
  );
1982
- if (record.ended) this.state = "ended";
1983
- else if (record.prompted) this.state = "idle";
2025
+ if (record.ended) this.phase = "ended";
2026
+ else if (record.terminal === void 0) this.phase = "preparing";
2027
+ else if (record.prompted) {
2028
+ this.phase = "running";
2029
+ this.activity = "idle";
2030
+ } else {
2031
+ this.phase = "launching";
2032
+ this.readySince = now;
2033
+ }
1984
2034
  }
1985
2035
  client;
1986
- state = "starting";
1987
- waitingOn;
2036
+ phase;
2037
+ /** The host's last output hint, once the prompt is in. */
2038
+ activity = "working";
2039
+ /** The harness showed no ready marker within the timeout. */
2040
+ notReady = false;
1988
2041
  lastProgressAt;
1989
2042
  busy = false;
1990
2043
  lastScreenSeq = -1;
1991
2044
  quietSince;
1992
2045
  lastSendAt = 0;
1993
2046
  sending = false;
2047
+ /** When the session entered `launching`: the ready clock starts there. */
1994
2048
  readySince;
2049
+ /** A stop that came while detached, carried out on reattach. */
2050
+ stopPending;
2051
+ reattachDelayMs = 0;
2052
+ reattachTimer;
1995
2053
  timer;
1996
2054
  uploadTimer;
1997
2055
  uploading;
@@ -1999,10 +2057,20 @@ class Session {
1999
2057
  statusSent;
2000
2058
  statusPending;
2001
2059
  statusTimer;
2002
- /** Set when the server fenced the lease: nothing more is sent. */
2003
- fenced = false;
2004
2060
  disposed = false;
2005
2061
  transcript;
2062
+ /** Set when the server fenced the lease: nothing more is sent. */
2063
+ get fenced() {
2064
+ return this.phase === "fenced";
2065
+ }
2066
+ /** Moves through `transitionSession`; answers whether the phase changed. */
2067
+ move(change) {
2068
+ const next = transitionSession(this.phase, change);
2069
+ if (next === void 0) return false;
2070
+ this.phase = next;
2071
+ if (next === "launching") this.readySince ??= this.manager.now();
2072
+ return true;
2073
+ }
2006
2074
  get harness() {
2007
2075
  return this.manager.harnesses[this.record.harness];
2008
2076
  }
@@ -2012,23 +2080,26 @@ class Session {
2012
2080
  save() {
2013
2081
  this.manager.save(this);
2014
2082
  }
2015
- /** Attaches for the transcript and watches for state, then starts checking. */
2083
+ /**
2084
+ * Attaches for the transcript and watches for state, then starts checking.
2085
+ * Losing the connection, now or later, detaches the session rather than
2086
+ * ending it: the terminal may still run.
2087
+ */
2016
2088
  async drive(client) {
2017
2089
  this.client = client;
2018
2090
  const terminal = this.record.terminal;
2019
2091
  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
- }
2092
+ if (this.client === client)
2093
+ this.detach("the terminal host connection closed");
2025
2094
  });
2026
2095
  const watched = await client.subscribe(
2027
2096
  { type: "watch", terminal },
2028
2097
  (event) => this.onWatch(event)
2029
2098
  );
2030
2099
  if (!watched.ok) {
2031
- this.end("the terminal is gone");
2100
+ if (watched.error?.code === "closed") {
2101
+ this.detach("the terminal host connection closed");
2102
+ } else this.end("the terminal is gone");
2032
2103
  return;
2033
2104
  }
2034
2105
  const since = this.transcript.seq();
@@ -2054,11 +2125,76 @@ class Session {
2054
2125
  this.end("exited");
2055
2126
  return;
2056
2127
  }
2128
+ if (this.client !== client || this.phase === "detached") return;
2129
+ this.reattachDelayMs = 0;
2130
+ this.stopTimers();
2057
2131
  this.timer = setInterval(() => void this.check(), this.timings.checkMs);
2058
2132
  this.uploadTimer = setInterval(
2059
2133
  () => void this.upload(),
2060
2134
  this.timings.uploadIntervalMs
2061
2135
  );
2136
+ const pending = this.stopPending;
2137
+ if (pending !== void 0) {
2138
+ this.stopPending = void 0;
2139
+ await this.manager.stopSession(this, pending);
2140
+ }
2141
+ }
2142
+ /**
2143
+ * The host connection is gone, or a subscription was dropped: the session
2144
+ * is `detached`, listed as waiting on the host, and looks for its terminal
2145
+ * again with backoff.
2146
+ */
2147
+ detach(reason) {
2148
+ if (this.disposed || !this.move({ type: "host-lost" })) return;
2149
+ this.manager.log.line(
2150
+ `${this.record.thread_id}: detached from its terminal (${reason}); finding it again`
2151
+ );
2152
+ clearInterval(this.timer);
2153
+ const client = this.client;
2154
+ this.client = void 0;
2155
+ client?.close();
2156
+ this.scheduleReattach();
2157
+ }
2158
+ scheduleReattach() {
2159
+ if (this.disposed || this.phase !== "detached") return;
2160
+ this.reattachDelayMs = this.reattachDelayMs === 0 ? this.timings.reattachMs : Math.min(this.reattachDelayMs * 2, this.timings.reattachMaxMs);
2161
+ clearTimeout(this.reattachTimer);
2162
+ this.reattachTimer = setTimeout(
2163
+ () => void this.reattach(),
2164
+ this.reattachDelayMs
2165
+ );
2166
+ }
2167
+ /** Finds the terminal on any host and drives it again, or ends the session when it is gone. */
2168
+ async reattach() {
2169
+ if (this.disposed || this.phase !== "detached") return;
2170
+ const found = await this.manager.locate(this.record);
2171
+ if (this.disposed || this.phase !== "detached") return;
2172
+ if (found.kind === "unreachable") {
2173
+ this.scheduleReattach();
2174
+ return;
2175
+ }
2176
+ if (found.kind !== "running") {
2177
+ this.end(found.kind === "exited" ? "exited" : "its terminal is gone");
2178
+ return;
2179
+ }
2180
+ let client;
2181
+ try {
2182
+ client = await HostClient.connect(found.socket);
2183
+ } catch {
2184
+ this.scheduleReattach();
2185
+ return;
2186
+ }
2187
+ if (this.disposed || this.phase !== "detached") {
2188
+ client.close();
2189
+ return;
2190
+ }
2191
+ this.record.socket = found.socket;
2192
+ this.move({ type: "reattached", prompted: this.record.prompted });
2193
+ this.save();
2194
+ this.manager.log.line(
2195
+ `${this.record.thread_id}: found ${this.record.terminal} again on ${path.basename(found.socket)}`
2196
+ );
2197
+ await this.drive(client);
2062
2198
  }
2063
2199
  onOutput(event) {
2064
2200
  if (event.event === "output" && !this.fenced) {
@@ -2068,6 +2204,8 @@ class Session {
2068
2204
  }
2069
2205
  } else if (event.event === "exit") {
2070
2206
  this.end("exited");
2207
+ } else if (event.event === "dropped") {
2208
+ this.detach("the terminal host dropped the output subscription");
2071
2209
  }
2072
2210
  }
2073
2211
  onWatch(event) {
@@ -2080,12 +2218,12 @@ class Session {
2080
2218
  }
2081
2219
  case "active": {
2082
2220
  this.busy = true;
2083
- if (this.record.prompted && !this.record.ended) this.state = "working";
2221
+ this.activity = "working";
2084
2222
  break;
2085
2223
  }
2086
2224
  case "idle": {
2087
2225
  this.busy = false;
2088
- if (this.record.prompted && !this.record.ended) this.state = "idle";
2226
+ this.activity = "idle";
2089
2227
  break;
2090
2228
  }
2091
2229
  case "title": {
@@ -2097,6 +2235,10 @@ class Session {
2097
2235
  this.end("exited");
2098
2236
  break;
2099
2237
  }
2238
+ case "dropped": {
2239
+ this.detach("the terminal host dropped the watch subscription");
2240
+ break;
2241
+ }
2100
2242
  }
2101
2243
  }
2102
2244
  /** Whether output has been quiet long enough to type. */
@@ -2110,7 +2252,7 @@ class Session {
2110
2252
  }
2111
2253
  /** Types the prompt once the harness is ready, then one follow-up per idle. */
2112
2254
  async check() {
2113
- if (this.sending || this.record.ended || this.fenced || !this.client) {
2255
+ if (this.sending || this.phase !== "launching" && this.phase !== "running" || !this.client) {
2114
2256
  return;
2115
2257
  }
2116
2258
  const next = this.record.queue[0];
@@ -2128,9 +2270,8 @@ class Session {
2128
2270
  const quiet = this.quiet(screen.seq);
2129
2271
  const ready = this.record.prompted || this.harness.ready.test(String(screen.text));
2130
2272
  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";
2273
+ if (this.phase === "launching" && !this.notReady && this.readySince !== void 0 && this.manager.now() - this.readySince >= this.timings.readyTimeoutMs) {
2274
+ this.notReady = true;
2134
2275
  this.setStatus("harness not ready");
2135
2276
  this.manager.log.line(
2136
2277
  `${this.record.thread_id}: ${this.record.harness} showed no ready marker in ${Math.round(this.timings.readyTimeoutMs / 1e3)}s; nothing typed`
@@ -2143,6 +2284,15 @@ class Session {
2143
2284
  this.sending = false;
2144
2285
  }
2145
2286
  }
2287
+ /** Whether the session may still be typed into by this client. */
2288
+ live(client) {
2289
+ return this.client === client && (this.phase === "launching" || this.phase === "running");
2290
+ }
2291
+ /**
2292
+ * Types one queued text and presses Enter. The phase is read again after
2293
+ * every await, so a session that ended, detached or was fenced meanwhile
2294
+ * is never marked as working.
2295
+ */
2146
2296
  async send(text) {
2147
2297
  const client = this.client;
2148
2298
  if (!client) return;
@@ -2153,15 +2303,22 @@ class Session {
2153
2303
  terminal,
2154
2304
  data: bracketedPaste(text)
2155
2305
  }) : await client.request({ type: "type", terminal, text });
2156
- if (!sent.ok) return;
2306
+ if (!sent.ok || !this.live(client)) return;
2157
2307
  await new Promise((resolve) => setTimeout(resolve, this.timings.settleMs));
2158
- await client.request({ type: "press", terminal, keys: ["Enter"] });
2308
+ if (!this.live(client)) return;
2309
+ const pressed = await client.request({
2310
+ type: "press",
2311
+ terminal,
2312
+ keys: ["Enter"]
2313
+ });
2314
+ if (!pressed.ok || !this.live(client)) return;
2159
2315
  this.record.queue.shift();
2160
2316
  const first = !this.record.prompted;
2161
2317
  this.record.prompted = true;
2162
2318
  this.lastSendAt = this.manager.now();
2163
- this.state = "working";
2164
- this.waitingOn = void 0;
2319
+ this.move({ type: "prompted" });
2320
+ this.activity = "working";
2321
+ this.notReady = false;
2165
2322
  this.save();
2166
2323
  this.manager.log.line(
2167
2324
  `${this.record.thread_id}: ${first ? "typed the prompt" : "typed a follow-up"} into ${this.record.harness}`
@@ -2198,19 +2355,32 @@ class Session {
2198
2355
  }
2199
2356
  /** The program exited, or the session never started: report it once. */
2200
2357
  end(reason) {
2201
- if (this.record.ended || this.fenced) return;
2358
+ if (this.record.ended || !this.move({ type: "ended" })) return;
2202
2359
  this.record.ended = reason;
2203
- this.state = "ended";
2204
2360
  this.stopTimers();
2361
+ clearTimeout(this.reattachTimer);
2205
2362
  this.manager.event(this, { type: "session.ended", without_done: true });
2206
2363
  this.manager.log.line(
2207
2364
  `${this.record.thread_id}: session ended (${reason})`
2208
2365
  );
2209
2366
  void this.upload();
2210
2367
  }
2368
+ /** Kills the terminal; the reply says whether the host confirmed it. */
2211
2369
  async kill() {
2212
- if (!this.client || this.record.terminal === void 0) return;
2213
- await this.client.request({ type: "kill", terminal: this.record.terminal });
2370
+ if (!this.client || this.record.terminal === void 0) {
2371
+ return {
2372
+ ok: false,
2373
+ error: { code: "closed", message: "no terminal host connection" }
2374
+ };
2375
+ }
2376
+ return this.client.request({
2377
+ type: "kill",
2378
+ terminal: this.record.terminal
2379
+ });
2380
+ }
2381
+ /** Remembers a stop that came while detached, for the reattach. */
2382
+ stopLater(why) {
2383
+ this.stopPending = why;
2214
2384
  }
2215
2385
  /** Sends the spool from the server's offset. */
2216
2386
  upload() {
@@ -2268,21 +2438,27 @@ class Session {
2268
2438
  this.disposed = true;
2269
2439
  this.stopTimers();
2270
2440
  clearTimeout(this.statusTimer);
2441
+ clearTimeout(this.reattachTimer);
2271
2442
  this.client?.close();
2272
2443
  }
2273
2444
  heartbeat() {
2274
2445
  const { record } = this;
2446
+ const phase = this.phase === "fenced" ? "ended" : this.phase;
2447
+ const reported = reportedState(phase, {
2448
+ activity: this.activity,
2449
+ notReady: this.notReady
2450
+ });
2275
2451
  const session = {
2276
2452
  thread_id: record.thread_id,
2277
2453
  lease_id: record.lease_id,
2278
2454
  epoch: record.epoch,
2279
2455
  harness: record.harness,
2280
- state: this.state,
2456
+ state: reported.state,
2281
2457
  last_progress_at: new Date(this.lastProgressAt).toISOString(),
2282
2458
  transcript_offset: record.transcript.base + record.transcript.uploaded,
2283
2459
  started_at: record.started_at
2284
2460
  };
2285
- if (this.waitingOn) session.waiting_on = this.waitingOn;
2461
+ if (reported.waiting_on) session.waiting_on = reported.waiting_on;
2286
2462
  if (record.repo) session.repo = record.repo;
2287
2463
  if (record.worktree) session.worktree = record.worktree;
2288
2464
  return session;
@@ -2322,28 +2498,44 @@ class Sessions {
2322
2498
  list() {
2323
2499
  return [...this.sessions.values()].filter((session) => !session.fenced).map((session) => session.heartbeat());
2324
2500
  }
2325
- /** Picks up every recorded session from whichever terminal host runs it. */
2501
+ /**
2502
+ * Picks up every recorded session from whichever terminal host runs it.
2503
+ * A record that never got its terminal ends: the link restarted while it
2504
+ * was being prepared, and its `work.assign` will not come again.
2505
+ */
2326
2506
  async resume() {
2327
2507
  mkdirSync(this.directory, { recursive: true, mode: 448 });
2328
2508
  const files = readdirSync(this.directory).filter(
2329
2509
  (name) => name.endsWith(".json")
2330
2510
  );
2331
- const terminals = await listTerminals(
2332
- hostSockets(this.options.terminalsDirectory, this.options.terminalSocket)
2333
- );
2334
2511
  for (const file of files) {
2335
2512
  const record = readJson(path.join(this.directory, file));
2336
2513
  if (!record?.thread_id || this.sessions.has(record.thread_id)) continue;
2337
2514
  const session = new Session(record, this);
2338
2515
  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");
2516
+ if (session.phase === "ended") {
2517
+ this.settle(session);
2518
+ continue;
2519
+ }
2520
+ if (session.phase === "preparing") {
2521
+ const why = "the link restarted before the session started";
2522
+ this.event(session, { type: "status", text: why });
2523
+ session.end(why);
2344
2524
  this.settle(session);
2345
2525
  continue;
2346
2526
  }
2527
+ const found = await this.locate(record);
2528
+ if (found.kind === "exited" || found.kind === "gone") {
2529
+ session.end(
2530
+ found.kind === "exited" ? "exited" : "its terminal is gone"
2531
+ );
2532
+ this.settle(session);
2533
+ continue;
2534
+ }
2535
+ if (found.kind === "unreachable") {
2536
+ session.detach("its terminal host cannot be reached");
2537
+ continue;
2538
+ }
2347
2539
  record.socket = found.socket;
2348
2540
  try {
2349
2541
  await session.drive(await HostClient.connect(found.socket));
@@ -2351,10 +2543,36 @@ class Sessions {
2351
2543
  `${record.thread_id}: picked up ${record.terminal} on ${path.basename(found.socket)}`
2352
2544
  );
2353
2545
  } catch (error) {
2354
- session.end(`its terminal host cannot be reached: ${errorText(error)}`);
2546
+ session.detach(
2547
+ `its terminal host cannot be reached: ${errorText(error)}`
2548
+ );
2355
2549
  }
2356
2550
  }
2357
2551
  }
2552
+ /**
2553
+ * Where a recorded session's terminal is: running on some host, exited,
2554
+ * gone (the host that ran it answers without it, or nothing listens on
2555
+ * its socket any more), or unreachable for now.
2556
+ */
2557
+ async locate(record) {
2558
+ const terminals = await listTerminals(
2559
+ hostSockets(this.options.terminalsDirectory, this.options.terminalSocket)
2560
+ );
2561
+ const found = terminals.find((terminal) => terminal.id === record.terminal);
2562
+ if (found) {
2563
+ return found.exited ? { kind: "exited" } : { kind: "running", socket: found.socket };
2564
+ }
2565
+ if (record.socket === void 0) return { kind: "gone" };
2566
+ try {
2567
+ const client = await HostClient.connect(record.socket);
2568
+ const listed = await client.request({ type: "list" });
2569
+ client.close();
2570
+ return listed.ok ? { kind: "gone" } : { kind: "unreachable" };
2571
+ } catch (error) {
2572
+ const code = error.code;
2573
+ return code === "ECONNREFUSED" || code === "ENOENT" ? { kind: "gone" } : { kind: "unreachable" };
2574
+ }
2575
+ }
2358
2576
  /** Handles a command the poller recorded; anything else is ignored. */
2359
2577
  async handle(command) {
2360
2578
  switch (command.type) {
@@ -2366,12 +2584,12 @@ class Sessions {
2366
2584
  await this.message(command);
2367
2585
  return true;
2368
2586
  }
2369
- case "thread.stop":
2587
+ case "thread.stop": {
2588
+ await this.stop(command.thread_id, command.type, command.lease_id);
2589
+ return true;
2590
+ }
2370
2591
  case "control.drop_session": {
2371
- await this.stop(
2372
- command.thread_id,
2373
- command.type
2374
- );
2592
+ await this.stop(command.thread_id, command.type);
2375
2593
  return true;
2376
2594
  }
2377
2595
  default: {
@@ -2379,18 +2597,22 @@ class Sessions {
2379
2597
  }
2380
2598
  }
2381
2599
  }
2600
+ /**
2601
+ * Starts a session for `work.assign`. Its record is saved before the first
2602
+ * await, so a link that restarts while it is being prepared ends it
2603
+ * visibly instead of forgetting it. A stop or fence while it is being
2604
+ * prepared ends the start after the step in flight.
2605
+ */
2382
2606
  async assign(command) {
2383
2607
  const { lease } = command;
2384
2608
  const existing = this.sessions.get(lease.thread_id);
2385
2609
  if (existing && existing.record.lease_id === lease.lease_id) return;
2386
2610
  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
2611
  const record = {
2390
2612
  thread_id: lease.thread_id,
2391
2613
  lease_id: lease.lease_id,
2392
2614
  epoch: lease.epoch,
2393
- harness: harness ?? "claude",
2615
+ harness: "claude",
2394
2616
  started_at: new Date(this.now()).toISOString(),
2395
2617
  queue: [],
2396
2618
  prompted: false,
@@ -2400,12 +2622,18 @@ class Sessions {
2400
2622
  };
2401
2623
  const session = new Session(record, this);
2402
2624
  this.sessions.set(record.thread_id, session);
2625
+ this.save(session);
2626
+ const stale = () => session.phase !== "preparing" || session.disposed;
2403
2627
  const refuse = (reason) => {
2404
2628
  this.log.line(`work.assign for ${record.thread_id} refused: ${reason}`);
2405
2629
  this.event(session, { type: "status", text: `refused: ${reason}` });
2406
2630
  session.end(reason);
2407
2631
  this.settle(session);
2408
2632
  };
2633
+ const opened = await this.options.access.open(command.request);
2634
+ if (stale()) return;
2635
+ const harness = opened.ok ? opened.payload.needs?.harness : void 0;
2636
+ if (harness) record.harness = harness;
2409
2637
  if (!opened.ok) {
2410
2638
  refuse(`the request did not pass the accept check (${opened.reason})`);
2411
2639
  return;
@@ -2418,7 +2646,9 @@ class Sessions {
2418
2646
  }
2419
2647
  record.repo = request.repo;
2420
2648
  record.queue = [request.prompt];
2649
+ this.save(session);
2421
2650
  const checkout = await this.options.repos.find(request.repo);
2651
+ if (stale()) return;
2422
2652
  if (!checkout) {
2423
2653
  refuse(
2424
2654
  `no checkout of ${request.repo.owner}/${request.repo.name} under ${this.options.paths.repos}`
@@ -2432,6 +2662,7 @@ class Sessions {
2432
2662
  baseRef: request.base_ref,
2433
2663
  reposConfig: this.options.paths.reposConfig
2434
2664
  });
2665
+ if (stale()) return;
2435
2666
  if (!prepared.ok) {
2436
2667
  refuse(`the worktree could not be made: ${prepared.reason}`);
2437
2668
  return;
@@ -2443,7 +2674,13 @@ class Sessions {
2443
2674
  if (socket === void 0) throw new Error("no terminal host is set");
2444
2675
  client = await HostClient.connect(socket);
2445
2676
  } catch (error) {
2446
- refuse(`the terminal host cannot be reached: ${errorText(error)}`);
2677
+ if (!stale()) {
2678
+ refuse(`the terminal host cannot be reached: ${errorText(error)}`);
2679
+ }
2680
+ return;
2681
+ }
2682
+ if (stale()) {
2683
+ client.close();
2447
2684
  return;
2448
2685
  }
2449
2686
  const created = await client.request({
@@ -2455,11 +2692,19 @@ class Sessions {
2455
2692
  });
2456
2693
  if (!created.ok) {
2457
2694
  client.close();
2458
- refuse(`${entry.command} could not start: ${created.error?.message}`);
2695
+ if (!stale()) {
2696
+ refuse(`${entry.command} could not start: ${created.error?.message}`);
2697
+ }
2698
+ return;
2699
+ }
2700
+ if (stale()) {
2701
+ await client.request({ type: "kill", terminal: created.terminal });
2702
+ client.close();
2459
2703
  return;
2460
2704
  }
2461
2705
  record.socket = socket;
2462
2706
  record.terminal = created.terminal;
2707
+ session.move({ type: "launched" });
2463
2708
  this.save(session);
2464
2709
  this.log.line(
2465
2710
  `${record.thread_id}: started ${entry.command} in ${prepared.worktree.path} as ${record.terminal}`
@@ -2474,6 +2719,12 @@ class Sessions {
2474
2719
  );
2475
2720
  return;
2476
2721
  }
2722
+ if (command.lease_id !== void 0 && command.lease_id !== session.record.lease_id) {
2723
+ this.log.line(
2724
+ `thread.message for lease ${command.lease_id} of ${command.thread_id}, but this session holds ${session.record.lease_id}; acked`
2725
+ );
2726
+ return;
2727
+ }
2477
2728
  const opened = await this.options.access.open(command.message);
2478
2729
  if (!opened.ok || typeof opened.payload.text !== "string") {
2479
2730
  const reason = opened.ok ? "malformed" : opened.reason;
@@ -2491,7 +2742,7 @@ class Sessions {
2491
2742
  session.record.messages.push(messageId);
2492
2743
  session.queueText(text);
2493
2744
  }
2494
- async stop(threadId, why) {
2745
+ async stop(threadId, why, leaseId) {
2495
2746
  const session = this.sessions.get(threadId);
2496
2747
  if (!session || session.record.ended) {
2497
2748
  this.log.line(
@@ -2499,9 +2750,41 @@ class Sessions {
2499
2750
  );
2500
2751
  return;
2501
2752
  }
2753
+ if (leaseId !== void 0 && leaseId !== session.record.lease_id) {
2754
+ this.log.line(
2755
+ `${why} for lease ${leaseId} of ${threadId}, but this session holds ${session.record.lease_id}; acked`
2756
+ );
2757
+ return;
2758
+ }
2502
2759
  this.log.line(`${threadId}: ${why}; ending the session`);
2503
- await session.kill();
2504
- session.end(why);
2760
+ await this.stopSession(session, why);
2761
+ }
2762
+ /**
2763
+ * Kills a session's terminal and ends it once the host confirms the kill,
2764
+ * or says the terminal is gone. A session being prepared ends at once; a
2765
+ * detached one, or one whose kill met a closed connection, is killed when
2766
+ * it reattaches.
2767
+ */
2768
+ async stopSession(session, why) {
2769
+ if (session.phase === "preparing") {
2770
+ session.end(why);
2771
+ return;
2772
+ }
2773
+ if (session.phase === "detached") {
2774
+ session.stopLater(why);
2775
+ return;
2776
+ }
2777
+ const reply = await session.kill();
2778
+ if (reply.ok || reply.error?.code === "exited") {
2779
+ session.end(why);
2780
+ } else if (reply.error?.code === "closed") {
2781
+ session.stopLater(why);
2782
+ session.detach("the terminal host connection closed during a kill");
2783
+ } else {
2784
+ session.end(
2785
+ reply.error?.code === "unknown-terminal" ? "its terminal is gone" : why
2786
+ );
2787
+ }
2505
2788
  }
2506
2789
  /** After each poll: fences, then everything still owed to the herd. */
2507
2790
  async afterPoll(answer) {
@@ -2520,16 +2803,50 @@ class Sessions {
2520
2803
  }
2521
2804
  }
2522
2805
  }
2523
- /** Kills the terminal and forgets the session, sending nothing more for it. */
2806
+ /**
2807
+ * Kills the terminal and forgets the session, sending nothing more for it.
2808
+ * A session with no live host connection (detached, or one that closed
2809
+ * under the kill) has its terminal found on any host and killed there,
2810
+ * since nothing will look for it once the session is forgotten.
2811
+ */
2524
2812
  async fence(session) {
2525
- if (session.fenced) return;
2526
- session.fenced = true;
2813
+ const ended = session.record.ended !== void 0;
2814
+ if (!session.move({ type: "fenced" })) return;
2527
2815
  this.log.line(
2528
2816
  `${session.record.thread_id}: lease ${session.record.lease_id} is fenced; ending the session and sending nothing more`
2529
2817
  );
2530
- if (!session.record.ended) await session.kill();
2818
+ if (!ended) {
2819
+ const reply = await session.kill();
2820
+ if (!reply.ok && reply.error?.code === "closed") {
2821
+ await this.killAnywhere(session.record);
2822
+ }
2823
+ }
2531
2824
  this.forget(session);
2532
2825
  }
2826
+ /** Finds a recorded terminal on whichever host runs it and kills it there. */
2827
+ async killAnywhere(record) {
2828
+ if (record.terminal === void 0) return;
2829
+ const found = await this.locate(record);
2830
+ if (found.kind === "unreachable") {
2831
+ this.log.line(
2832
+ `${record.thread_id}: could not reach the terminal host to kill ${record.terminal}`
2833
+ );
2834
+ return;
2835
+ }
2836
+ if (found.kind !== "running") return;
2837
+ try {
2838
+ const client = await HostClient.connect(found.socket);
2839
+ try {
2840
+ await client.request({ type: "kill", terminal: record.terminal });
2841
+ } finally {
2842
+ client.close();
2843
+ }
2844
+ } catch (error) {
2845
+ this.log.line(
2846
+ `${record.thread_id}: could not kill ${record.terminal}: ${errorText(error)}`
2847
+ );
2848
+ }
2849
+ }
2533
2850
  forget(session) {
2534
2851
  session.dispose();
2535
2852
  if (this.sessions.get(session.record.thread_id) === session) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aibexhq/aibex-link",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "The process aibexd runs to link a machine to an Aibex herd",
5
5
  "homepage": "https://github.com/aibexhq",
6
6
  "bugs": {