@thermai/flightdeck-cli 0.1.0-pilot.6 → 0.1.0-pilot.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 (3) hide show
  1. package/README.md +74 -17
  2. package/dist/index.js +1886 -40
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/index.ts
4
4
  import { Command } from "commander";
5
- import { readFileSync as readFileSync4 } from "node:fs";
5
+ import { readFileSync as readFileSync5 } from "node:fs";
6
6
 
7
7
  // src/submit-core.ts
8
8
  import { existsSync as existsSync2, mkdtempSync, readFileSync as readFileSync2, rmSync } from "node:fs";
@@ -31,6 +31,12 @@ var CliError = class extends Error {
31
31
  this.name = "CliError";
32
32
  }
33
33
  };
34
+ var NetworkError = class extends CliError {
35
+ constructor(message, options) {
36
+ super(message, options);
37
+ this.name = "NetworkError";
38
+ }
39
+ };
34
40
  function suspensionMessage(status2, code) {
35
41
  if (status2 !== 403) return void 0;
36
42
  switch (code) {
@@ -60,7 +66,7 @@ function httpError(action, status2, code) {
60
66
  const guidance = status2 === 401 ? " Check the account with flightdeck whoami; sign in again if the credential expired or was revoked." : status2 === 403 ? " Check the account permissions and requested credential scopes." : status2 === 429 ? " Wait briefly before retrying." : "";
61
67
  return new CliError(`${action} (HTTP ${status2}).${guidance}`);
62
68
  }
63
- var ERROR_TYPES = /* @__PURE__ */ new Set(["Error", "TypeError", "RangeError", "SyntaxError", "CliError", "AbortError"]);
69
+ var ERROR_TYPES = /* @__PURE__ */ new Set(["Error", "TypeError", "RangeError", "SyntaxError", "CliError", "NetworkError", "AtcFatalError", "AbortError"]);
64
70
  var ERROR_CODES = /* @__PURE__ */ new Set([
65
71
  "EACCES",
66
72
  "EPERM",
@@ -98,6 +104,7 @@ Diagnostic: ${details.join("; ") || "unknown error type"}`;
98
104
  // src/client.ts
99
105
  var DEFAULT_API_URL = "https://api.flightdeck.thermai.uk";
100
106
  var CONFIG_PATH = join(homedir(), ".flightdeck", "config.json");
107
+ var ADMIN_SCOPE_MESSAGE = "Run `flightdeck login --admin` to authorise this workstation for administrator views.";
101
108
  var ACCOUNTING_HEADER = "X-Flightdeck-Accounting";
102
109
  var ACCOUNTING_CONTRACT = "credits-v1";
103
110
  var ARTIFACT_REDIRECTS = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
@@ -238,10 +245,10 @@ async function publicApiRequest(apiUrl, method, path, body) {
238
245
  body: requestBody(body, headers)
239
246
  });
240
247
  }
241
- async function apiRequest(method, path, body) {
242
- return authenticatedRequest(loadConfig(), method, path, body);
248
+ async function apiRequest(method, path, body, options = {}) {
249
+ return authenticatedRequest(options.config ?? loadConfig(), method, path, body, "follow", options.signal);
243
250
  }
244
- async function authenticatedRequest(config, method, path, body, redirect = "follow") {
251
+ async function authenticatedRequest(config, method, path, body, redirect = "follow", signal) {
245
252
  const headers = {
246
253
  Authorization: `Bearer ${config.api_key}`,
247
254
  [ACCOUNTING_HEADER]: ACCOUNTING_CONTRACT
@@ -255,9 +262,15 @@ async function authenticatedRequest(config, method, path, body, redirect = "foll
255
262
  method,
256
263
  headers,
257
264
  body: requestBody(body, headers),
258
- redirect
265
+ redirect,
266
+ signal
259
267
  });
260
268
  if (response.status === 403) {
269
+ const raw = await response.clone().json().catch(() => null);
270
+ const body2 = raw && typeof raw === "object" ? raw : {};
271
+ if (body2.error === "admin_scope_required" || body2.error === "credential_scope_required" && (body2.required_scope === "admin:jobs:read" || body2.required_scope === void 0 && /^\/(?:admin|platform)\/flights(?:\?|$)/.test(path))) {
272
+ throw new CliError(ADMIN_SCOPE_MESSAGE);
273
+ }
261
274
  throw await suspensionError(response) ?? httpError("Access denied", response.status);
262
275
  }
263
276
  if (path !== "/auth/credential/revoke" && (response.ok || response.status === 402)) assertAccountingContract(response);
@@ -301,7 +314,7 @@ async function request(url, options) {
301
314
  try {
302
315
  return await fetch(url, options);
303
316
  } catch (cause) {
304
- throw new CliError("Could not reach the Flightdeck API. Check your network connection and API URL before retrying.", { cause });
317
+ throw new NetworkError("Could not reach the Flightdeck API. Check your network connection and API URL before retrying.", { cause });
305
318
  }
306
319
  }
307
320
 
@@ -341,9 +354,9 @@ function environmentContractIssues(value, mode = "customer") {
341
354
  return ["environment must be a string record"];
342
355
  }
343
356
  const entries = Object.entries(value);
344
- const runtime = mode === "runtime";
345
- const maximumEntries = runtime ? MAX_RUNTIME_ENV_ENTRIES : MAX_CUSTOMER_ENV_ENTRIES;
346
- const maximumBytes = runtime ? MAX_RUNTIME_ENV_UTF8_BYTES : MAX_CUSTOMER_ENV_UTF8_BYTES;
357
+ const runtime2 = mode === "runtime";
358
+ const maximumEntries = runtime2 ? MAX_RUNTIME_ENV_ENTRIES : MAX_CUSTOMER_ENV_ENTRIES;
359
+ const maximumBytes = runtime2 ? MAX_RUNTIME_ENV_UTF8_BYTES : MAX_CUSTOMER_ENV_UTF8_BYTES;
347
360
  const issues = [];
348
361
  let aggregateBytes = 0;
349
362
  if (entries.length > maximumEntries) {
@@ -360,7 +373,7 @@ function environmentContractIssues(value, mode = "customer") {
360
373
  if (name.length > MAX_ENV_KEY_CHARACTERS) {
361
374
  issues.push(`${name} exceeds ${MAX_ENV_KEY_CHARACTERS} characters`);
362
375
  }
363
- if (!runtime && RESERVED.has(name)) {
376
+ if (!runtime2 && RESERVED.has(name)) {
364
377
  issues.push(`${name} is reserved by Flightdeck`);
365
378
  }
366
379
  const valueBytes = utf8Bytes(rawValue);
@@ -492,7 +505,7 @@ Track with: flightdeck status ${body.flight_id}`);
492
505
  }
493
506
 
494
507
  // src/commands/sbatch.ts
495
- import { existsSync as existsSync3, readFileSync as readFileSync3 } from "node:fs";
508
+ import { existsSync as existsSync4, readFileSync as readFileSync4, rmSync as rmSync2 } from "node:fs";
496
509
  import { dirname as dirname2, isAbsolute, normalize, relative, resolve as resolve2 } from "node:path";
497
510
 
498
511
  // src/sbatch.ts
@@ -738,24 +751,24 @@ function parseGpuRequest(value, fromGres) {
738
751
  if (tokens.length === 1) {
739
752
  const onlyToken = tokens[0];
740
753
  if (/^\d+$/.test(onlyToken)) {
741
- const count2 = Number(onlyToken);
742
- if (!Number.isSafeInteger(count2) || count2 < 1 || count2 > MAX_GPU_COUNT) {
754
+ const count3 = Number(onlyToken);
755
+ if (!Number.isSafeInteger(count3) || count3 < 1 || count3 > MAX_GPU_COUNT) {
743
756
  throw new Error(`GPU count must be between 1 and ${MAX_GPU_COUNT} in "${value}"`);
744
757
  }
745
- return { gpuCount: count2 };
758
+ return { gpuCount: count3 };
746
759
  }
747
760
  return { gpuType: onlyToken, gpuCount: 1 };
748
761
  }
749
- const [type, count] = tokens;
750
- if (!/^\d+$/.test(count) || !Number.isSafeInteger(Number(count)) || Number(count) < 1 || Number(count) > MAX_GPU_COUNT) {
762
+ const [type, count2] = tokens;
763
+ if (!/^\d+$/.test(count2) || !Number.isSafeInteger(Number(count2)) || Number(count2) < 1 || Number(count2) > MAX_GPU_COUNT) {
751
764
  throw new Error(`GPU count must be between 1 and ${MAX_GPU_COUNT} in "${value}"`);
752
765
  }
753
- return { gpuType: type, gpuCount: Number(count) };
766
+ return { gpuType: type, gpuCount: Number(count2) };
754
767
  }
755
- function parseSbatchScript(text) {
768
+ function parseSbatchScript(text2) {
756
769
  const result = { warnings: [], exportedEnv: {} };
757
770
  const ignored = /* @__PURE__ */ new Set();
758
- for (const rawLine of text.split("\n")) {
771
+ for (const rawLine of text2.split("\n")) {
759
772
  const line = rawLine.trim();
760
773
  if (line === "") continue;
761
774
  if (!line.startsWith("#")) break;
@@ -845,6 +858,59 @@ function parseSbatchScript(text) {
845
858
  return result;
846
859
  }
847
860
 
861
+ // src/batch-record.ts
862
+ import { chmodSync as chmodSync2, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, renameSync as renameSync2, unlinkSync as unlinkSync2, writeFileSync as writeFileSync2 } from "node:fs";
863
+ import { homedir as homedir2 } from "node:os";
864
+ import { join as join3 } from "node:path";
865
+ var BATCHES_DIRECTORY = join3(homedir2(), ".flightdeck", "batches");
866
+ function isBatchJobId(value) {
867
+ return /^\d{1,32}$/.test(value);
868
+ }
869
+ function batchRecordPath(jobId, directory = BATCHES_DIRECTORY) {
870
+ if (!isBatchJobId(jobId)) throw new CliError(`Not an sbatch job ID: ${jobId}`);
871
+ return join3(directory, `${jobId}.json`);
872
+ }
873
+ function saveBatchRecord(record2, directory = BATCHES_DIRECTORY) {
874
+ const path = batchRecordPath(record2.job_id, directory);
875
+ mkdirSync2(directory, { recursive: true, mode: 448 });
876
+ chmodSync2(directory, 448);
877
+ const temporaryPath = `${path}.tmp-${process.pid}`;
878
+ try {
879
+ writeFileSync2(temporaryPath, `${JSON.stringify(record2, null, 2)}
880
+ `, { encoding: "utf8", mode: 384, flag: "wx" });
881
+ renameSync2(temporaryPath, path);
882
+ } finally {
883
+ if (existsSync3(temporaryPath)) unlinkSync2(temporaryPath);
884
+ }
885
+ }
886
+ function validFlight(value) {
887
+ if (!value || typeof value !== "object") return false;
888
+ const flight = value;
889
+ return typeof flight.flight_id === "string" && flight.flight_id.length > 0 && typeof flight.name === "string" && (flight.task_id === null || typeof flight.task_id === "number" && Number.isInteger(flight.task_id));
890
+ }
891
+ function validApiUrl(value) {
892
+ try {
893
+ normalizeApiUrl(value);
894
+ return true;
895
+ } catch {
896
+ return false;
897
+ }
898
+ }
899
+ function loadBatchRecord(jobId, directory = BATCHES_DIRECTORY) {
900
+ const path = batchRecordPath(jobId, directory);
901
+ if (!existsSync3(path)) return null;
902
+ let record2;
903
+ try {
904
+ record2 = JSON.parse(readFileSync3(path, "utf8"));
905
+ } catch {
906
+ throw new CliError(`The local record for sbatch job ${jobId} is unreadable: ${path}`);
907
+ }
908
+ if (record2?.version !== 1 || record2.job_id !== jobId || typeof record2.job_name !== "string" || typeof record2.api_url !== "string" || typeof record2.submitted_at !== "string" || !Number.isFinite(Date.parse(record2.submitted_at)) || !Array.isArray(record2.flights) || !record2.flights.every(validFlight) || !validApiUrl(record2.api_url)) {
909
+ throw new CliError(`The local record for sbatch job ${jobId} is not valid: ${path}`);
910
+ }
911
+ return record2;
912
+ }
913
+
848
914
  // src/commands/sbatch.ts
849
915
  var MAX_DURATION_S = 86400;
850
916
  var WORKSPACE_ROOT = "/workspace";
@@ -975,12 +1041,56 @@ function buildSbatchPlans(args) {
975
1041
  }
976
1042
  }));
977
1043
  }
1044
+ function trackWithLine(submitted) {
1045
+ const hint = `Track with: flightdeck status ${submitted[0].flightId}`;
1046
+ return submitted.length > 1 ? `${hint} (or any flight ID above)` : hint;
1047
+ }
1048
+ function watchLiveLine(jobId) {
1049
+ return `Watch live: flightdeck atc ${jobId} (sbatch job ID, not a flight ID; works on this machine only)`;
1050
+ }
1051
+ function batchRecorder(jobId, jobName, target = { apiUrl: readConfig()?.api_url }) {
1052
+ const apiUrl = target.apiUrl;
1053
+ const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
1054
+ let warned = false;
1055
+ const recorder = {
1056
+ saved: false,
1057
+ record(submitted) {
1058
+ if (!apiUrl) return;
1059
+ try {
1060
+ saveBatchRecord({
1061
+ version: 1,
1062
+ job_id: jobId,
1063
+ job_name: jobName,
1064
+ api_url: apiUrl,
1065
+ submitted_at: submittedAt,
1066
+ flights: submitted.map((flight) => ({ flight_id: flight.flightId, task_id: flight.taskId, name: flight.name }))
1067
+ }, target.directory);
1068
+ recorder.saved = true;
1069
+ } catch (error) {
1070
+ recorder.saved = false;
1071
+ if (!warned) {
1072
+ warned = true;
1073
+ console.error(` warning: could not record this batch for flightdeck atc (${error.code ?? "write failed"})`);
1074
+ }
1075
+ }
1076
+ },
1077
+ /** A record missing the latest tasks would misrepresent the array; drop it. */
1078
+ finish() {
1079
+ if (recorder.saved || !apiUrl) return;
1080
+ try {
1081
+ rmSync2(batchRecordPath(jobId, target.directory), { force: true });
1082
+ } catch {
1083
+ }
1084
+ }
1085
+ };
1086
+ return recorder;
1087
+ }
978
1088
  async function sbatch(scriptPath, options) {
979
- if (!existsSync3(scriptPath)) {
1089
+ if (!existsSync4(scriptPath)) {
980
1090
  console.error(`Script not found: ${scriptPath}`);
981
1091
  process.exit(1);
982
1092
  }
983
- const scriptText = readFileSync3(scriptPath, "utf-8");
1093
+ const scriptText = readFileSync4(scriptPath, "utf-8");
984
1094
  let directives;
985
1095
  try {
986
1096
  directives = parseSbatchScript(scriptText);
@@ -1048,6 +1158,7 @@ Packaging files...`);
1048
1158
  `);
1049
1159
  const submitted = [];
1050
1160
  let failures = 0;
1161
+ const batch = batchRecorder(jobId, jobName);
1051
1162
  for (const task of tasks) {
1052
1163
  try {
1053
1164
  const result = await submitFlight(task.plan, bundle);
@@ -1060,12 +1171,16 @@ Packaging files...`);
1060
1171
  } else {
1061
1172
  console.error(` ${task.plan.name}: ${err.message}`);
1062
1173
  }
1174
+ continue;
1063
1175
  }
1176
+ batch.record(submitted);
1064
1177
  }
1178
+ batch.finish();
1065
1179
  console.log(`
1066
1180
  ${submitted.length} submitted, ${failures} failed.`);
1067
1181
  if (submitted.length > 0) {
1068
- console.log(`Track with: flightdeck status <flight_id>`);
1182
+ console.log(trackWithLine(submitted));
1183
+ if (batch.saved) console.log(watchLiveLine(jobId));
1069
1184
  }
1070
1185
  if (failures > 0) process.exit(1);
1071
1186
  return { submitted, failures };
@@ -1156,6 +1271,1582 @@ async function cancel(flightId) {
1156
1271
  console.log(`Cancelled ${flightId}`);
1157
1272
  }
1158
1273
 
1274
+ // src/atc/input.ts
1275
+ var ESCAPE_SEQUENCES = [
1276
+ ["\x1B[A", "up"],
1277
+ ["\x1BOA", "up"],
1278
+ ["\x1B[B", "down"],
1279
+ ["\x1BOB", "down"],
1280
+ ["\x1B[5~", "pageup"],
1281
+ ["\x1B[6~", "pagedown"],
1282
+ ["\x1B[H", "home"],
1283
+ ["\x1BOH", "home"],
1284
+ ["\x1B[1~", "home"],
1285
+ ["\x1B[F", "end"],
1286
+ ["\x1BOF", "end"],
1287
+ ["\x1B[4~", "end"]
1288
+ ];
1289
+ var SINGLE_CHARACTERS = {
1290
+ "\r": "enter",
1291
+ "\n": "enter",
1292
+ "": "interrupt",
1293
+ "\f": "redraw",
1294
+ r: "refresh",
1295
+ R: "refresh",
1296
+ q: "quit",
1297
+ Q: "quit",
1298
+ k: "up",
1299
+ j: "down",
1300
+ g: "home",
1301
+ G: "end",
1302
+ l: "logs",
1303
+ L: "logs",
1304
+ f: "follow",
1305
+ F: "follow",
1306
+ " ": "pagedown",
1307
+ b: "pageup"
1308
+ };
1309
+ var PASTE_START = "\x1B[200~";
1310
+ var PASTE_END = "\x1B[201~";
1311
+ var ESCAPE_TIMEOUT_MS = 50;
1312
+ var PASTE_TIMEOUT_MS = 1500;
1313
+ var COMPLETE_SEQUENCE = /^\x1b(\[[0-9;?]*[ -/]*[@-~]|O.)/;
1314
+ var INCOMPLETE_SEQUENCE = /^\x1b(\[[0-9;?]*[ -/]*|O)?$/;
1315
+ var KeyReader = class {
1316
+ pending = "";
1317
+ inPaste = false;
1318
+ /** How long to wait before `flushPending`, or null when nothing is held. */
1319
+ get pendingTimeoutMs() {
1320
+ if (this.inPaste) return PASTE_TIMEOUT_MS;
1321
+ return this.pending.length > 0 ? ESCAPE_TIMEOUT_MS : null;
1322
+ }
1323
+ feed(chunk) {
1324
+ let buffer = this.pending + chunk;
1325
+ this.pending = "";
1326
+ const keys = [];
1327
+ while (buffer.length > 0) {
1328
+ if (this.inPaste) {
1329
+ const end = buffer.indexOf(PASTE_END);
1330
+ const interrupt = buffer.indexOf("");
1331
+ if (interrupt >= 0 && (end < 0 || interrupt < end)) {
1332
+ this.inPaste = false;
1333
+ keys.push("interrupt");
1334
+ buffer = buffer.slice(interrupt + 1);
1335
+ continue;
1336
+ }
1337
+ if (end < 0) {
1338
+ let keep = Math.min(PASTE_END.length - 1, buffer.length);
1339
+ while (keep > 0 && !PASTE_END.startsWith(buffer.slice(-keep))) keep -= 1;
1340
+ this.pending = keep ? buffer.slice(-keep) : "";
1341
+ return keys;
1342
+ }
1343
+ this.inPaste = false;
1344
+ buffer = buffer.slice(end + PASTE_END.length);
1345
+ continue;
1346
+ }
1347
+ if (buffer[0] !== "\x1B") {
1348
+ keys.push(SINGLE_CHARACTERS[buffer[0]] ?? "unknown");
1349
+ buffer = buffer.slice(1);
1350
+ continue;
1351
+ }
1352
+ if (buffer.startsWith(PASTE_START)) {
1353
+ this.inPaste = true;
1354
+ buffer = buffer.slice(PASTE_START.length);
1355
+ continue;
1356
+ }
1357
+ const known = ESCAPE_SEQUENCES.find(([sequence]) => buffer.startsWith(sequence));
1358
+ if (known) {
1359
+ keys.push(known[1]);
1360
+ buffer = buffer.slice(known[0].length);
1361
+ continue;
1362
+ }
1363
+ const complete = COMPLETE_SEQUENCE.exec(buffer);
1364
+ if (complete) {
1365
+ keys.push("unknown");
1366
+ buffer = buffer.slice(complete[0].length);
1367
+ continue;
1368
+ }
1369
+ if (INCOMPLETE_SEQUENCE.test(buffer)) {
1370
+ this.pending = buffer;
1371
+ return keys;
1372
+ }
1373
+ keys.push("unknown");
1374
+ buffer = buffer.slice(2);
1375
+ }
1376
+ return keys;
1377
+ }
1378
+ /**
1379
+ * The timeout passed: an unfinished paste is abandoned, and a held escape
1380
+ * prefix was a bare Escape (plus any junk after it).
1381
+ */
1382
+ flushPending() {
1383
+ if (this.inPaste) {
1384
+ this.inPaste = false;
1385
+ this.pending = "";
1386
+ return [];
1387
+ }
1388
+ if (!this.pending) return [];
1389
+ const held = this.pending;
1390
+ this.pending = "";
1391
+ return held === "\x1B" ? ["escape"] : ["escape", ...this.feed(held.slice(1))];
1392
+ }
1393
+ };
1394
+
1395
+ // src/atc/model.ts
1396
+ var TERMINAL_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]);
1397
+ var AtcFatalError = class extends CliError {
1398
+ constructor(message, options) {
1399
+ super(message, options);
1400
+ this.name = "AtcFatalError";
1401
+ }
1402
+ };
1403
+ function isTerminal(status2) {
1404
+ return TERMINAL_STATUSES.has(status2);
1405
+ }
1406
+ function elapsedSeconds(flight, nowMs) {
1407
+ const startedIso = flight.started_at ?? flight.dispatched_at;
1408
+ if (!startedIso) return null;
1409
+ const endMs = flight.completed_at ? Date.parse(flight.completed_at) : nowMs;
1410
+ const seconds = Math.floor((endMs - Date.parse(startedIso)) / 1e3);
1411
+ return Number.isFinite(seconds) ? Math.max(0, seconds) : null;
1412
+ }
1413
+ function formatDuration(totalSeconds) {
1414
+ if (totalSeconds === null) return "\u2014";
1415
+ const hours = Math.floor(totalSeconds / 3600);
1416
+ const minutes = Math.floor(totalSeconds % 3600 / 60);
1417
+ const seconds = totalSeconds % 60;
1418
+ const pad = (value) => String(value).padStart(2, "0");
1419
+ return hours > 0 ? `${hours}:${pad(minutes)}:${pad(seconds)}` : `${pad(minutes)}:${pad(seconds)}`;
1420
+ }
1421
+
1422
+ // src/atc/text.ts
1423
+ var COLOR_CODES = {
1424
+ red: 31,
1425
+ green: 32,
1426
+ yellow: 33,
1427
+ blue: 34,
1428
+ magenta: 35,
1429
+ cyan: 36,
1430
+ white: 37,
1431
+ gray: 90
1432
+ };
1433
+ var ZERO_WIDTH = /^[\p{Mn}\p{Me}\p{Cf}]$/u;
1434
+ var EMOJI_PRESENTATION = new RegExp("^\\p{Emoji_Presentation}$", "u");
1435
+ var PICTOGRAPHIC = new RegExp("\\p{Extended_Pictographic}", "u");
1436
+ var FLAG = new RegExp("^\\p{Regional_Indicator}{2}$", "u");
1437
+ var WIDE_RANGES = [
1438
+ [4352, 4447],
1439
+ [11904, 12350],
1440
+ [12353, 13311],
1441
+ [13312, 19903],
1442
+ [19968, 40959],
1443
+ [40960, 42191],
1444
+ [43360, 43391],
1445
+ [44032, 55203],
1446
+ [63744, 64255],
1447
+ [65040, 65049],
1448
+ [65072, 65135],
1449
+ [65280, 65376],
1450
+ [65504, 65510],
1451
+ [127744, 128591],
1452
+ [129280, 129535],
1453
+ [131072, 196605],
1454
+ [196608, 262141]
1455
+ ];
1456
+ var graphemes = new Intl.Segmenter(void 0, { granularity: "grapheme" });
1457
+ function cellWidth(character) {
1458
+ if (ZERO_WIDTH.test(character)) return 0;
1459
+ const codePoint = character.codePointAt(0);
1460
+ if (EMOJI_PRESENTATION.test(character)) return 2;
1461
+ for (const [low, high] of WIDE_RANGES) {
1462
+ if (codePoint >= low && codePoint <= high) return 2;
1463
+ }
1464
+ return 1;
1465
+ }
1466
+ function clusterWidth(cluster) {
1467
+ const codePoints = [...cluster];
1468
+ if (codePoints.length > 1) {
1469
+ if (FLAG.test(cluster)) return 2;
1470
+ if (PICTOGRAPHIC.test(cluster) && (cluster.includes("\uFE0F") || cluster.includes("\u200D"))) return 2;
1471
+ }
1472
+ return cellWidth(codePoints[0]);
1473
+ }
1474
+ function sanitize(text2) {
1475
+ return text2.replace(/[‪-‮⁦-⁩]/g, "").replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
1476
+ }
1477
+ function* clustersOf(text2) {
1478
+ for (const { segment } of graphemes.segment(text2)) yield segment;
1479
+ }
1480
+ function textWidth(text2) {
1481
+ let total = 0;
1482
+ for (const cluster of clustersOf(sanitize(text2))) total += clusterWidth(cluster);
1483
+ return total;
1484
+ }
1485
+ function seg(text2, style) {
1486
+ return { text: text2, style };
1487
+ }
1488
+ function visibleLength(line) {
1489
+ return line.reduce((total, segment) => total + textWidth(segment.text), 0);
1490
+ }
1491
+ function sgr(style, colorEnabled) {
1492
+ if (!style) return "";
1493
+ const codes = [];
1494
+ if (style.bold) codes.push(1);
1495
+ if (style.dim) codes.push(2);
1496
+ if (style.inverse) codes.push(7);
1497
+ if (style.color && colorEnabled) codes.push(COLOR_CODES[style.color]);
1498
+ return codes.length ? `\x1B[${codes.join(";")}m` : "";
1499
+ }
1500
+ function takeCells(text2, width) {
1501
+ const scanLimit = width * 2 + 8;
1502
+ let scanned = text2.length > scanLimit ? text2.slice(0, scanLimit) : text2;
1503
+ if (scanned.length < text2.length && /[\ud800-\udbff]$/.test(scanned)) scanned = scanned.slice(0, -1);
1504
+ let visible = "";
1505
+ let used = 0;
1506
+ for (const cluster of clustersOf(sanitize(scanned))) {
1507
+ const clusterCells = clusterWidth(cluster);
1508
+ if (used + clusterCells > width) return { visible, used, consumedAll: false };
1509
+ visible += cluster;
1510
+ used += clusterCells;
1511
+ }
1512
+ return { visible, used, consumedAll: scanned.length === text2.length };
1513
+ }
1514
+ function encodeLine(line, width, colorEnabled, fill) {
1515
+ let remaining = width;
1516
+ let output = "";
1517
+ for (const segment of line) {
1518
+ if (remaining <= 0) break;
1519
+ const { visible, used, consumedAll } = takeCells(segment.text, remaining);
1520
+ remaining -= used;
1521
+ const open = sgr(segment.style, colorEnabled);
1522
+ output += open && visible ? `${open}${visible}\x1B[0m` : visible;
1523
+ if (!consumedAll) break;
1524
+ }
1525
+ if (remaining > 0) {
1526
+ const open = sgr(fill, colorEnabled);
1527
+ output += open ? `${open}${" ".repeat(remaining)}\x1B[0m` : " ".repeat(remaining);
1528
+ }
1529
+ return output;
1530
+ }
1531
+ function clipLine(line, width) {
1532
+ const clipped = [];
1533
+ let remaining = width;
1534
+ for (const segment of line) {
1535
+ if (remaining <= 0) break;
1536
+ const { visible, used, consumedAll } = takeCells(segment.text, remaining);
1537
+ clipped.push({ text: visible, style: segment.style });
1538
+ remaining -= used;
1539
+ if (!consumedAll) break;
1540
+ }
1541
+ return clipped;
1542
+ }
1543
+ function fixed(text2, width, align = "left") {
1544
+ const clean = sanitize(text2);
1545
+ const currentWidth = textWidth(clean);
1546
+ if (currentWidth > width) {
1547
+ const room = width <= 1 ? width : width - 1;
1548
+ const { visible, used } = takeCells(clean, room);
1549
+ return `${visible}${width <= 1 ? "" : "\u2026"}${" ".repeat(room - used)}`;
1550
+ }
1551
+ const padding = " ".repeat(width - currentWidth);
1552
+ return align === "left" ? clean + padding : padding + clean;
1553
+ }
1554
+ function highlight(line, style) {
1555
+ return line.map((segment) => ({ text: segment.text, style: { ...style, ...segment.style, inverse: style.inverse } }));
1556
+ }
1557
+
1558
+ // src/atc/screen.ts
1559
+ var ENTER_ALTERNATE_SCREEN = "\x1B[?1049h\x1B[?25l\x1B[?7l\x1B[?2004h\x1B[2J";
1560
+ var LEAVE_ALTERNATE_SCREEN = "\x1B[0m\x1B[?2004l\x1B[?7h\x1B[?25h\x1B[?1049l";
1561
+ var BEGIN_FRAME = "\x1B[?2026h";
1562
+ var END_FRAME = "\x1B[?2026l";
1563
+ var Screen = class {
1564
+ constructor(output, colorEnabled) {
1565
+ this.output = output;
1566
+ this.colorEnabled = colorEnabled;
1567
+ }
1568
+ output;
1569
+ colorEnabled;
1570
+ previousRows = [];
1571
+ previousSize = "";
1572
+ active = false;
1573
+ get width() {
1574
+ return this.output.columns ?? 80;
1575
+ }
1576
+ get height() {
1577
+ return this.output.rows ?? 24;
1578
+ }
1579
+ enter() {
1580
+ if (this.active) return;
1581
+ this.active = true;
1582
+ this.output.write(ENTER_ALTERNATE_SCREEN);
1583
+ }
1584
+ leave() {
1585
+ if (!this.active) return;
1586
+ this.active = false;
1587
+ this.output.write(LEAVE_ALTERNATE_SCREEN);
1588
+ }
1589
+ /** Force a full repaint on the next draw (after a resize). */
1590
+ invalidate() {
1591
+ this.previousRows = [];
1592
+ }
1593
+ draw(lines) {
1594
+ const width = this.width;
1595
+ const height = this.height;
1596
+ const size = `${width}x${height}`;
1597
+ let frame = "";
1598
+ if (size !== this.previousSize) {
1599
+ frame += "\x1B[2J";
1600
+ this.previousRows = [];
1601
+ this.previousSize = size;
1602
+ }
1603
+ for (let row = 0; row < height; row += 1) {
1604
+ const encoded = encodeLine(lines[row] ?? [], width, this.colorEnabled);
1605
+ if (this.previousRows[row] === encoded) continue;
1606
+ this.previousRows[row] = encoded;
1607
+ frame += `\x1B[${row + 1};1H${encoded}`;
1608
+ }
1609
+ if (frame) this.output.write(BEGIN_FRAME + frame + END_FRAME);
1610
+ }
1611
+ };
1612
+
1613
+ // src/atc/state.ts
1614
+ var MAX_LOG_LINES = 5e3;
1615
+ var MAX_LOG_LINE_LENGTH = 64 * 1024;
1616
+ function initialState() {
1617
+ return {
1618
+ view: "board",
1619
+ logReturnView: "board",
1620
+ flights: [],
1621
+ selectedFlightId: null,
1622
+ events: null,
1623
+ logs: null,
1624
+ following: true,
1625
+ logTopLine: 0,
1626
+ flipStartedAtMs: /* @__PURE__ */ new Map(),
1627
+ lastUpdatedMs: null,
1628
+ lastError: null,
1629
+ selectionError: null,
1630
+ notice: null,
1631
+ quit: false
1632
+ };
1633
+ }
1634
+ function selectedFlight(state) {
1635
+ return state.flights.find((flight) => flight.flight_id === state.selectedFlightId);
1636
+ }
1637
+ function selectedIndex(state) {
1638
+ const index = state.flights.findIndex((flight) => flight.flight_id === state.selectedFlightId);
1639
+ return index < 0 ? 0 : index;
1640
+ }
1641
+ function mergeFlightRecord(state, record2, nowMs) {
1642
+ const previous = state.flights.find((flight) => flight.flight_id === record2.flight_id);
1643
+ if (!previous) return state;
1644
+ const flipStartedAtMs = previous.status !== record2.status ? new Map(state.flipStartedAtMs).set(record2.flight_id, nowMs) : state.flipStartedAtMs;
1645
+ return {
1646
+ ...state,
1647
+ flipStartedAtMs,
1648
+ flights: state.flights.map((flight) => flight.flight_id === record2.flight_id ? record2 : flight)
1649
+ };
1650
+ }
1651
+ function applyFlights(state, incoming, nowMs) {
1652
+ const previousById = new Map(state.flights.map((flight) => [flight.flight_id, flight]));
1653
+ const previousStatusById = new Map(state.flights.map((flight) => [flight.flight_id, flight.status]));
1654
+ const flights = incoming.map((flight) => {
1655
+ const previous = previousById.get(flight.flight_id);
1656
+ const carry = flight.recovery === void 0 && previous?.recovery !== void 0 && previous.status === flight.status && !isTerminal(flight.status);
1657
+ return carry ? { ...flight, recovery: previous.recovery } : flight;
1658
+ });
1659
+ const flipStartedAtMs = new Map(state.flipStartedAtMs);
1660
+ for (const flight of flights) {
1661
+ const previousStatus = previousStatusById.get(flight.flight_id);
1662
+ if (previousStatus !== void 0 && previousStatus !== flight.status) flipStartedAtMs.set(flight.flight_id, nowMs);
1663
+ }
1664
+ const merged = { ...state, flights, flipStartedAtMs, lastUpdatedMs: nowMs, lastError: null };
1665
+ if (flights.some((flight) => flight.flight_id === state.selectedFlightId)) return merged;
1666
+ const vanished = state.view !== "board" ? state.flights.find((flight) => flight.flight_id === state.selectedFlightId) : void 0;
1667
+ return {
1668
+ ...merged,
1669
+ selectedFlightId: flights[0]?.flight_id ?? null,
1670
+ view: vanished ? "board" : state.view,
1671
+ notice: vanished ? `${vanished.name} is no longer listed` : state.notice
1672
+ };
1673
+ }
1674
+ function capLineLength(content) {
1675
+ if (content.length <= MAX_LOG_LINE_LENGTH) return content;
1676
+ const capped = content.slice(0, MAX_LOG_LINE_LENGTH);
1677
+ return /[\ud800-\udbff]$/.test(capped) ? capped.slice(0, -1) : capped;
1678
+ }
1679
+ function resolveCarriageReturns(text2) {
1680
+ const body = text2.replace(/\r+$/, "");
1681
+ const pendingReturn = body.length !== text2.length;
1682
+ return { content: body.slice(body.lastIndexOf("\r") + 1), pendingReturn };
1683
+ }
1684
+ function splitLogChunks(existing, tail, chunks) {
1685
+ const lines = [...existing];
1686
+ let currentTail = tail;
1687
+ for (const chunk of chunks) {
1688
+ if (chunk.content === "") continue;
1689
+ const pieces = chunk.content.split("\n");
1690
+ pieces.forEach((piece, index) => {
1691
+ const isLastPiece = index === pieces.length - 1;
1692
+ if (isLastPiece && piece === "") {
1693
+ currentTail = "closed";
1694
+ return;
1695
+ }
1696
+ const previous = lines[lines.length - 1];
1697
+ const continues = index === 0 && currentTail !== "closed" && previous?.stream === chunk.stream;
1698
+ const prefix = continues ? previous.content + (currentTail === "carriage-return" ? "\r" : "") : "";
1699
+ const { content, pendingReturn } = resolveCarriageReturns(prefix + piece);
1700
+ const line = { stream: chunk.stream, content: capLineLength(content) };
1701
+ if (continues) lines[lines.length - 1] = line;
1702
+ else lines.push(line);
1703
+ currentTail = !isLastPiece ? "closed" : pendingReturn ? "carriage-return" : "open";
1704
+ });
1705
+ }
1706
+ return { lines, tail: currentTail };
1707
+ }
1708
+ function appendLogs(state, flightId, chunks, cursor, complete, attemptSeq) {
1709
+ const existing = state.logs?.flightId === flightId ? state.logs : null;
1710
+ let priorLines = existing?.lines ?? [];
1711
+ let priorTail = existing?.tail ?? "closed";
1712
+ const hadAttempt = typeof existing?.attemptSeq === "number" || existing?.attemptSeq === null && existing.lines.length > 0;
1713
+ const newAttempt = hadAttempt && typeof attemptSeq === "number" && attemptSeq !== existing.attemptSeq;
1714
+ if (newAttempt) {
1715
+ priorLines = [...priorLines, { stream: "meta", content: `\u2500\u2500 dispatch attempt ${attemptSeq}: its output starts here \u2500\u2500` }];
1716
+ priorTail = "closed";
1717
+ }
1718
+ const split = splitLogChunks(priorLines, priorTail, chunks);
1719
+ const overflow = Math.max(0, split.lines.length - MAX_LOG_LINES);
1720
+ return {
1721
+ ...state,
1722
+ logs: {
1723
+ flightId,
1724
+ lines: overflow ? split.lines.slice(overflow) : split.lines,
1725
+ tail: split.tail,
1726
+ attemptSeq: attemptSeq !== void 0 ? attemptSeq : existing?.attemptSeq,
1727
+ cursor,
1728
+ complete,
1729
+ droppedLineCount: (existing?.droppedLineCount ?? 0) + overflow
1730
+ },
1731
+ // Keep a paused view anchored on the same text when old lines are trimmed.
1732
+ logTopLine: Math.max(0, state.logTopLine - overflow)
1733
+ };
1734
+ }
1735
+ function restartLogs(state, flightId) {
1736
+ const marker = { stream: "meta", content: "\u2500\u2500 log position reset; output below restarts from the beginning \u2500\u2500" };
1737
+ const withMarker = appendLogs(state, flightId, [], "", false);
1738
+ return {
1739
+ ...withMarker,
1740
+ logs: { ...withMarker.logs, lines: [...withMarker.logs.lines, marker], tail: "closed", cursor: void 0 }
1741
+ };
1742
+ }
1743
+ function moveSelection(state, delta) {
1744
+ if (state.flights.length === 0) return state;
1745
+ const index = Math.min(state.flights.length - 1, Math.max(0, selectedIndex(state) + delta));
1746
+ return { ...state, selectedFlightId: state.flights[index].flight_id };
1747
+ }
1748
+ function scrollLogs(state, delta, viewportRows) {
1749
+ const totalLines = state.logs?.lines.length ?? 0;
1750
+ if (totalLines <= viewportRows) return state;
1751
+ const lastTop = Math.max(0, totalLines - viewportRows);
1752
+ const currentTop = state.following ? lastTop : state.logTopLine;
1753
+ const nextTop = Math.min(lastTop, Math.max(0, currentTop + delta));
1754
+ if (delta > 0 && nextTop === lastTop) return { ...state, following: true, logTopLine: nextTop };
1755
+ return { ...state, following: false, logTopLine: nextTop };
1756
+ }
1757
+ function openLogs(state) {
1758
+ if (!state.selectedFlightId) return state;
1759
+ const sameFlight = state.logs?.flightId === state.selectedFlightId;
1760
+ return {
1761
+ ...state,
1762
+ view: "logs",
1763
+ logReturnView: state.view === "logs" ? state.logReturnView : state.view,
1764
+ logs: sameFlight ? state.logs : null,
1765
+ following: true,
1766
+ logTopLine: 0
1767
+ };
1768
+ }
1769
+ function reduceKey(current, key, context) {
1770
+ const state = current.notice ? { ...current, notice: null } : current;
1771
+ if (key === "quit" || key === "interrupt") return { ...state, quit: true };
1772
+ if (state.view === "board") {
1773
+ switch (key) {
1774
+ case "up":
1775
+ return moveSelection(state, -1);
1776
+ case "down":
1777
+ return moveSelection(state, 1);
1778
+ case "pageup":
1779
+ return moveSelection(state, -context.boardViewportRows);
1780
+ case "pagedown":
1781
+ return moveSelection(state, context.boardViewportRows);
1782
+ case "home":
1783
+ return moveSelection(state, -state.flights.length);
1784
+ case "end":
1785
+ return moveSelection(state, state.flights.length);
1786
+ case "enter":
1787
+ return state.selectedFlightId ? { ...state, view: "detail" } : state;
1788
+ case "logs":
1789
+ return openLogs(state);
1790
+ default:
1791
+ return state;
1792
+ }
1793
+ }
1794
+ if (state.view === "detail") {
1795
+ switch (key) {
1796
+ case "escape":
1797
+ return { ...state, view: "board" };
1798
+ case "logs":
1799
+ case "enter":
1800
+ return openLogs(state);
1801
+ // Walk through flights without leaving the detail view.
1802
+ case "up":
1803
+ return moveSelection(state, -1);
1804
+ case "down":
1805
+ return moveSelection(state, 1);
1806
+ default:
1807
+ return state;
1808
+ }
1809
+ }
1810
+ switch (key) {
1811
+ case "escape":
1812
+ return { ...state, view: state.logReturnView };
1813
+ case "up":
1814
+ return scrollLogs(state, -1, context.logViewportRows);
1815
+ case "down":
1816
+ return scrollLogs(state, 1, context.logViewportRows);
1817
+ case "pageup":
1818
+ return scrollLogs(state, -context.logViewportRows, context.logViewportRows);
1819
+ case "pagedown":
1820
+ return scrollLogs(state, context.logViewportRows, context.logViewportRows);
1821
+ case "home":
1822
+ return { ...state, following: false, logTopLine: 0 };
1823
+ case "end":
1824
+ return { ...state, following: true };
1825
+ case "follow": {
1826
+ if (!state.following) return { ...state, following: true };
1827
+ const totalLines = state.logs?.lines.length ?? 0;
1828
+ return { ...state, following: false, logTopLine: Math.max(0, totalLines - context.logViewportRows) };
1829
+ }
1830
+ default:
1831
+ return state;
1832
+ }
1833
+ }
1834
+
1835
+ // src/atc/view.ts
1836
+ var MIN_WIDTH = 60;
1837
+ var MIN_HEIGHT = 12;
1838
+ var FLIP_DURATION_MS = 700;
1839
+ var FRAME_MS = 100;
1840
+ var BOARD_CHROME_ROWS = 6;
1841
+ var LOG_CHROME_ROWS = 3;
1842
+ var SPINNERS = {
1843
+ dispatching: ["\u25D0", "\u25D3", "\u25D1", "\u25D2"],
1844
+ running: ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"],
1845
+ finalizing: ["\u2801", "\u2802", "\u2804", "\u2840", "\u2880", "\u2820", "\u2810", "\u2808"]
1846
+ };
1847
+ var STATUS_STYLE = {
1848
+ queued: { glyph: "\xB7", style: { color: "gray" } },
1849
+ dispatching: { glyph: "\u25D0", style: { color: "yellow" } },
1850
+ running: { glyph: "\u25CF", style: { color: "cyan", bold: true } },
1851
+ finalizing: { glyph: "\u25C6", style: { color: "blue" } },
1852
+ completed: { glyph: "\u2713", style: { color: "green" } },
1853
+ failed: { glyph: "\u2717", style: { color: "red", bold: true } },
1854
+ cancelled: { glyph: "\u2298", style: { color: "gray" } }
1855
+ };
1856
+ var UNKNOWN_STATUS = { glyph: "?", style: { color: "magenta" } };
1857
+ var SUMMARY_ORDER = ["completed", "failed", "cancelled", "finalizing", "running", "dispatching", "queued"];
1858
+ function statusStyle(status2) {
1859
+ return (STATUS_STYLE[status2] ?? UNKNOWN_STATUS).style;
1860
+ }
1861
+ function statusGlyph(status2, nowMs) {
1862
+ const frames = SPINNERS[status2];
1863
+ if (frames) return frames[Math.floor(nowMs / FRAME_MS) % frames.length];
1864
+ return (STATUS_STYLE[status2] ?? UNKNOWN_STATUS).glyph;
1865
+ }
1866
+ var FLAP_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
1867
+ function splitFlap(target, width, flipStartedAtMs, nowMs) {
1868
+ const padded = fixed(target, width);
1869
+ if (flipStartedAtMs === void 0) return padded;
1870
+ const progress = (nowMs - flipStartedAtMs) / FLIP_DURATION_MS;
1871
+ if (progress >= 1 || progress < 0) return padded;
1872
+ const characters = [...padded];
1873
+ const settledCount = Math.floor(progress * characters.length);
1874
+ const frame = Math.floor(nowMs / 50);
1875
+ return characters.map((character, position) => {
1876
+ if (position < settledCount) return character;
1877
+ const scramble = (frame * 7 + position * 13 + (frame >> 2) * position) % FLAP_ALPHABET.length;
1878
+ return FLAP_ALPHABET[scramble];
1879
+ }).join("");
1880
+ }
1881
+ function budgetGauge(flight, nowMs, width) {
1882
+ const elapsed = elapsedSeconds(flight, nowMs);
1883
+ if (elapsed === null || !flight.max_duration_s) return [seg(fixed("", width))];
1884
+ const fraction = Math.min(1, elapsed / flight.max_duration_s);
1885
+ const filled = Math.round(fraction * width);
1886
+ const color = fraction >= 0.9 ? "red" : fraction >= 0.7 ? "yellow" : "green";
1887
+ const style = isTerminal(flight.status) ? { color: "gray" } : { color };
1888
+ return [seg("\u2588".repeat(filled), style), seg("\u2591".repeat(width - filled), { color: "gray", dim: true })];
1889
+ }
1890
+ function clockTime(nowMs) {
1891
+ if (!Number.isFinite(nowMs)) return "\u2014";
1892
+ const date = new Date(nowMs);
1893
+ const pad = (value) => String(value).padStart(2, "0");
1894
+ return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
1895
+ }
1896
+ function timestamp(iso) {
1897
+ return iso ? clockTime(Date.parse(iso)) : "\u2014";
1898
+ }
1899
+ function titleBar(left, context) {
1900
+ const right = ` ${clockTime(context.nowMs)} `;
1901
+ const leftText = fixed(` ${left} `, Math.max(1, context.width - textWidth(right) - 1)).trimEnd() + " ";
1902
+ const gap = Math.max(1, context.width - textWidth(leftText) - textWidth(right));
1903
+ const barStyle = { color: "cyan", bold: true, inverse: true };
1904
+ return [seg(leftText, barStyle), seg(" ".repeat(gap), barStyle), seg(right, barStyle)];
1905
+ }
1906
+ function rule(width) {
1907
+ return [seg("\u2500".repeat(width), { color: "gray", dim: true })];
1908
+ }
1909
+ function keyHints(hints) {
1910
+ const segments = [seg(" ")];
1911
+ for (const [key, action] of hints) {
1912
+ segments.push(seg(key, { color: "cyan", bold: true }), seg(` ${action} `, { color: "gray" }));
1913
+ }
1914
+ return segments;
1915
+ }
1916
+ function footer(state, context, hints) {
1917
+ const error = state.lastError ?? (state.view === "board" ? null : state.selectionError);
1918
+ if (error) return [seg(fixed(` \u26A0 ${error}`, context.width), { color: "red", bold: true })];
1919
+ let rightText;
1920
+ let rightStyle;
1921
+ if (state.notice) {
1922
+ rightText = ` ${state.notice} `;
1923
+ rightStyle = { color: "yellow", bold: true };
1924
+ } else if (state.lastUpdatedMs === null) {
1925
+ rightText = " connecting\u2026 ";
1926
+ rightStyle = { color: "yellow" };
1927
+ } else {
1928
+ const ageSeconds = Math.max(0, Math.floor((context.nowMs - state.lastUpdatedMs) / 1e3));
1929
+ rightText = ` updated ${ageSeconds}s ago `;
1930
+ rightStyle = { color: "gray" };
1931
+ }
1932
+ rightText = fixed(rightText, Math.min(textWidth(rightText), context.width));
1933
+ const hintWidth = context.width - textWidth(rightText);
1934
+ const left = clipLine(keyHints(hints), hintWidth);
1935
+ return [...left, seg(" ".repeat(Math.max(0, hintWidth - visibleLength(left)))), seg(rightText, rightStyle)];
1936
+ }
1937
+ function boardColumns(width) {
1938
+ const showGpu = width >= 72;
1939
+ const showCredits = width >= 80;
1940
+ const showBudget = width >= 96;
1941
+ const fixedColumns = [
1942
+ { header: "", width: 2 },
1943
+ { header: "FLIGHT", width: 13 },
1944
+ { header: "NAME", width: 0 },
1945
+ ...showGpu ? [{ header: "GPU", width: 10 }] : [],
1946
+ { header: "STATUS", width: 15 },
1947
+ { header: "ELAPSED", width: 9, align: "right" },
1948
+ ...showBudget ? [{ header: " TIME BUDGET", width: 14 }] : [],
1949
+ ...showCredits ? [{ header: "CREDITS", width: 8, align: "right" }] : []
1950
+ ];
1951
+ const usedWidth = fixedColumns.reduce((total, column) => total + column.width + 1, 0);
1952
+ fixedColumns[2].width = Math.max(8, width - usedWidth - 1);
1953
+ return { columns: fixedColumns, showGpu, showBudget, showCredits };
1954
+ }
1955
+ function boardRow(flight, state, context, layout, isSelected) {
1956
+ const [cursorColumn, flightColumn, nameColumn] = layout.columns;
1957
+ const style = statusStyle(flight.status);
1958
+ const statusText = splitFlap(flight.status, 12, state.flipStartedAtMs.get(flight.flight_id), context.nowMs);
1959
+ const recovering = flight.recovery && flight.recovery.phase !== "finished";
1960
+ const line = [
1961
+ seg(fixed(isSelected ? "\u25B8" : "", cursorColumn.width + 1), { color: "cyan", bold: true }),
1962
+ seg(fixed(flight.flight_id, flightColumn.width) + " ", { color: "gray" }),
1963
+ seg(fixed(flight.name, nameColumn.width) + " ")
1964
+ ];
1965
+ if (layout.showGpu) line.push(seg(fixed(`${flight.gpu_type ?? "any"} x${flight.gpu_count}`, 10) + " ", { color: "white" }));
1966
+ line.push(
1967
+ seg(`${statusGlyph(flight.status, context.nowMs)} `, style),
1968
+ seg(statusText + (recovering ? "\u27F3" : " ") + " ", recovering ? { ...style, color: "magenta" } : style),
1969
+ seg(fixed(formatDuration(elapsedSeconds(flight, context.nowMs)), 9, "right") + " ")
1970
+ );
1971
+ if (layout.showBudget) line.push(seg(" "), ...budgetGauge(flight, context.nowMs, 12), seg(" "));
1972
+ if (layout.showCredits) {
1973
+ const credits = flight.charge_credits;
1974
+ line.push(seg(fixed(credits == null ? "\u2014" : String(credits), 8, "right"), { color: "yellow" }));
1975
+ }
1976
+ if (!isSelected) return line;
1977
+ return highlight([...line, seg(" ".repeat(context.width))], { inverse: true });
1978
+ }
1979
+ function statusCounts(flights) {
1980
+ const counts = /* @__PURE__ */ new Map();
1981
+ for (const flight of flights) counts.set(flight.status, (counts.get(flight.status) ?? 0) + 1);
1982
+ return counts;
1983
+ }
1984
+ function orderedStatuses(counts) {
1985
+ const unknown = [...counts.keys()].filter((status2) => !SUMMARY_ORDER.includes(status2)).sort();
1986
+ return [...SUMMARY_ORDER, ...unknown].filter((status2) => (counts.get(status2) ?? 0) > 0);
1987
+ }
1988
+ function progressBar(flights, width) {
1989
+ if (flights.length === 0 || width <= 0) return [seg("\u2591".repeat(Math.max(0, width)), { color: "gray", dim: true })];
1990
+ const counts = statusCounts(flights);
1991
+ const statuses = orderedStatuses(counts);
1992
+ const exact = statuses.map((status2) => (counts.get(status2) ?? 0) / flights.length * width);
1993
+ const widths = exact.map(Math.floor);
1994
+ let leftover = width - widths.reduce((total, value) => total + value, 0);
1995
+ const byRemainder = exact.map((value, index) => ({ index, remainder: value - Math.floor(value) })).sort((left, right) => right.remainder - left.remainder);
1996
+ for (const { index } of byRemainder) {
1997
+ if (leftover <= 0) break;
1998
+ widths[index] += 1;
1999
+ leftover -= 1;
2000
+ }
2001
+ const fillFor = (status2) => isTerminal(status2) ? "\u2588" : status2 === "queued" ? "\u2591" : "\u2593";
2002
+ return statuses.map((status2, index) => seg(fillFor(status2).repeat(widths[index]), statusStyle(status2)));
2003
+ }
2004
+ function summaryCounts(flights) {
2005
+ const counts = statusCounts(flights);
2006
+ const line = [seg(" ")];
2007
+ orderedStatuses(counts).forEach((status2, index) => {
2008
+ if (index > 0) line.push(seg(" \xB7 ", { color: "gray" }));
2009
+ line.push(seg(`${counts.get(status2)} ${status2}`, statusStyle(status2)));
2010
+ });
2011
+ if (line.length === 1) line.push(seg("no flights yet", { color: "gray" }));
2012
+ return line;
2013
+ }
2014
+ function boardViewportRows(height) {
2015
+ return Math.max(1, height - BOARD_CHROME_ROWS);
2016
+ }
2017
+ function renderBoard(state, context) {
2018
+ const layout = boardColumns(context.width);
2019
+ const bodyRows = boardViewportRows(context.height);
2020
+ const lines = [titleBar(`FLIGHTDECK ATC \u2500\u2500\u2500 ${context.title} \u2500\u2500\u2500 ${state.flights.length} flight${state.flights.length === 1 ? "" : "s"}`, context)];
2021
+ lines.push([seg(layout.columns.map((column) => fixed(column.header, column.width, column.align)).join(" "), { color: "gray", bold: true })]);
2022
+ const selected = selectedIndex(state);
2023
+ const top = Math.min(Math.max(0, selected - Math.floor(bodyRows / 2)), Math.max(0, state.flights.length - bodyRows));
2024
+ const visible = state.flights.slice(top, top + bodyRows);
2025
+ for (const [offset, flight] of visible.entries()) {
2026
+ lines.push(boardRow(flight, state, context, layout, top + offset === selected));
2027
+ }
2028
+ if (state.flights.length === 0) lines.push([seg(" waiting for flights\u2026", { color: "gray" })]);
2029
+ while (lines.length < bodyRows + 2) lines.push([]);
2030
+ const scrollNote = state.flights.length > bodyRows ? ` ${top + 1}\u2013${top + visible.length} of ${state.flights.length} ` : "";
2031
+ lines.push([...rule(context.width - textWidth(scrollNote)), seg(scrollNote, { color: "gray" })]);
2032
+ lines.push([seg(" "), ...progressBar(state.flights, context.width - 2)]);
2033
+ lines.push(summaryCounts(state.flights));
2034
+ lines.push(footer(state, context, [["\u2191\u2193", "select"], ["\u23CE", "detail"], ["l", "logs"], ["r", "refresh"], ["q", "quit"]]));
2035
+ return lines;
2036
+ }
2037
+ function field(label, value) {
2038
+ const valueSegments = typeof value === "string" ? [seg(value)] : value;
2039
+ return [seg(` ${fixed(label, 11)}`, { color: "gray" }), ...valueSegments];
2040
+ }
2041
+ function renderDetail(state, context) {
2042
+ const flight = selectedFlight(state);
2043
+ if (!flight) return renderBoard({ ...state, view: "board" }, context);
2044
+ const style = statusStyle(flight.status);
2045
+ const elapsed = elapsedSeconds(flight, context.nowMs);
2046
+ const lines = [titleBar(`FLIGHT ${selectedIndex(state) + 1}/${state.flights.length} \u2500\u2500\u2500 ${flight.name}`, context), []];
2047
+ const statusSegments = [
2048
+ seg(`${statusGlyph(flight.status, context.nowMs)} `, style),
2049
+ seg(splitFlap(flight.status, 12, state.flipStartedAtMs.get(flight.flight_id), context.nowMs).trimEnd(), { ...style, bold: true })
2050
+ ];
2051
+ if (flight.recovery && flight.recovery.phase !== "finished") {
2052
+ statusSegments.push(seg(` \u27F3 GPU recovery: ${flight.recovery.phase}`, { color: "magenta" }));
2053
+ }
2054
+ lines.push(field("Flight", [seg(flight.flight_id, { color: "white", bold: true })]));
2055
+ lines.push(field("Name", flight.name));
2056
+ lines.push(field("Status", statusSegments));
2057
+ lines.push(field("GPU", `${flight.gpu_type ?? "any"} x${flight.gpu_count}${flight.provider ? ` via ${flight.provider}` : ""}`));
2058
+ lines.push(field("Timeline", [
2059
+ seg(`queued ${timestamp(flight.queued_at)}`),
2060
+ seg(" \u2192 ", { color: "gray" }),
2061
+ seg(`dispatched ${timestamp(flight.dispatched_at)}`),
2062
+ seg(" \u2192 ", { color: "gray" }),
2063
+ seg(`started ${timestamp(flight.started_at)}`),
2064
+ seg(" \u2192 ", { color: "gray" }),
2065
+ seg(`finished ${timestamp(flight.completed_at)}`)
2066
+ ]));
2067
+ const gaugeWidth = Math.max(10, Math.min(40, context.width - 40));
2068
+ lines.push(field("Elapsed", [
2069
+ seg(fixed(formatDuration(elapsed), 10)),
2070
+ ...flight.max_duration_s ? [
2071
+ ...budgetGauge(flight, context.nowMs, gaugeWidth),
2072
+ seg(` of ${formatDuration(flight.max_duration_s)} max`, { color: "gray" })
2073
+ ] : []
2074
+ ]));
2075
+ if (flight.exit_code != null) lines.push(field("Exit code", [seg(String(flight.exit_code), { color: flight.exit_code === 0 ? "green" : "red", bold: true })]));
2076
+ if (flight.error_message) lines.push(field("Error", [seg(flight.error_message, { color: "red" })]));
2077
+ if (flight.charge_credits != null) lines.push(field("Credits", [seg(String(flight.charge_credits), { color: "yellow" })]));
2078
+ lines.push([]);
2079
+ lines.push([seg(" EVENTS ", { color: "cyan", bold: true }), ...rule(Math.max(0, context.width - 11))]);
2080
+ const eventRows = context.height - lines.length - 1;
2081
+ const events = state.events?.flightId === flight.flight_id ? state.events.items : null;
2082
+ if (events === null) {
2083
+ lines.push([seg(" loading\u2026", { color: "gray" })]);
2084
+ } else if (events.length === 0) {
2085
+ lines.push([seg(" no events recorded", { color: "gray" })]);
2086
+ } else {
2087
+ for (const event of events.slice(-Math.max(1, eventRows))) {
2088
+ lines.push([
2089
+ seg(` ${timestamp(event.created_at)} `, { color: "gray" }),
2090
+ seg(fixed(event.type, 26) + " ", { color: event.type.includes("fault") || event.type.includes("limit") ? "red" : "blue" }),
2091
+ seg(event.message ?? "")
2092
+ ]);
2093
+ }
2094
+ }
2095
+ while (lines.length < context.height - 1) lines.push([]);
2096
+ lines.length = context.height - 1;
2097
+ lines.push(footer(state, context, [["\u2191\u2193", "prev/next flight"], ["l", "logs"], ["r", "refresh"], ["esc", "board"], ["q", "quit"]]));
2098
+ return lines;
2099
+ }
2100
+ function logViewportRows(height) {
2101
+ return Math.max(1, height - LOG_CHROME_ROWS);
2102
+ }
2103
+ function renderLogs(state, context) {
2104
+ const flight = selectedFlight(state);
2105
+ const bodyRows = logViewportRows(context.height);
2106
+ const buffer = state.logs?.flightId === state.selectedFlightId ? state.logs : null;
2107
+ const allLines = buffer?.lines ?? [];
2108
+ const lastTop = Math.max(0, allLines.length - bodyRows);
2109
+ const top = state.following ? lastTop : Math.min(state.logTopLine, lastTop);
2110
+ const mode = [buffer?.complete ? "\u25A0 complete" : null, state.following ? buffer?.complete ? null : "\u25CF following" : "\u23F8 paused"].filter(Boolean).join(" \xB7 ");
2111
+ const lines = [titleBar(`LOGS \u2500\u2500\u2500 ${flight?.name ?? "?"} \u2500\u2500\u2500 ${flight?.flight_id ?? ""} \u2500\u2500\u2500 ${mode}`, context)];
2112
+ if (!buffer) {
2113
+ lines.push([seg(" loading\u2026", { color: "gray" })]);
2114
+ } else if (allLines.length === 0) {
2115
+ lines.push([seg(buffer.complete ? " (no output)" : " waiting for output\u2026", { color: "gray" })]);
2116
+ } else {
2117
+ for (const logLine of allLines.slice(top, top + bodyRows)) {
2118
+ const lineStyle = logLine.stream === "stderr" ? { color: "red" } : logLine.stream === "meta" ? { color: "cyan", dim: true } : void 0;
2119
+ lines.push([seg(logLine.content.replace(/\t/g, " "), lineStyle)]);
2120
+ }
2121
+ }
2122
+ while (lines.length < bodyRows + 1) lines.push([]);
2123
+ const dropped = buffer?.droppedLineCount ? ` (+${buffer.droppedLineCount} earlier lines dropped)` : "";
2124
+ const position = allLines.length ? ` lines ${top + 1}\u2013${Math.min(allLines.length, top + bodyRows)} of ${allLines.length}${dropped} ` : "";
2125
+ lines.push([...rule(context.width - textWidth(position)), seg(position, { color: "gray" })]);
2126
+ lines.push(footer(state, context, [["\u2191\u2193", "scroll"], ["PgUp/PgDn", "page"], ["f", "follow"], ["esc", "back"], ["q", "quit"]]));
2127
+ return lines;
2128
+ }
2129
+ function render(state, context) {
2130
+ if (context.width < MIN_WIDTH || context.height < MIN_HEIGHT) {
2131
+ const message = `Terminal too small: ${context.width}\xD7${context.height}, need ${MIN_WIDTH}\xD7${MIN_HEIGHT}`;
2132
+ return [[seg(message, { color: "yellow" })]];
2133
+ }
2134
+ switch (state.view) {
2135
+ case "detail":
2136
+ return renderDetail(state, context);
2137
+ case "logs":
2138
+ return renderLogs(state, context);
2139
+ default:
2140
+ return renderBoard(state, context);
2141
+ }
2142
+ }
2143
+
2144
+ // src/atc/app.ts
2145
+ var ANIMATION_INTERVAL_MS = 100;
2146
+ var MAX_LOG_PAGES_PER_REFRESH = 10;
2147
+ var MAX_BACKOFF_MS = 6e4;
2148
+ var SETTLED_REFRESH_MS = 15e3;
2149
+ var SIGNAL_EXIT_CODES = {
2150
+ SIGHUP: 129,
2151
+ SIGINT: 130,
2152
+ SIGQUIT: 131,
2153
+ SIGTERM: 143
2154
+ };
2155
+ var HANDLED_SIGNALS = Object.keys(SIGNAL_EXIT_CODES);
2156
+ function describeFailure(error) {
2157
+ return error instanceof CliError ? error.message : "update failed; retrying";
2158
+ }
2159
+ async function runAtc(source, terminal) {
2160
+ const now = terminal.now ?? Date.now;
2161
+ const screen = new Screen(terminal.output, terminal.colorEnabled);
2162
+ const keyReader = new KeyReader();
2163
+ let state = source.initialFlightId ? { ...initialState(), view: "detail", selectedFlightId: source.initialFlightId } : initialState();
2164
+ let stopped = false;
2165
+ let resolveRun = () => {
2166
+ };
2167
+ let rejectRun = () => {
2168
+ };
2169
+ const finished = new Promise((resolve4, reject) => {
2170
+ resolveRun = resolve4;
2171
+ rejectRun = reject;
2172
+ });
2173
+ const requests = new AbortController();
2174
+ let pollTimer;
2175
+ let selectionTimer;
2176
+ let animationTimer;
2177
+ let escapeTimer;
2178
+ let pollInFlight = false;
2179
+ let detailFetchInFlight = false;
2180
+ let consecutivePollFailures = 0;
2181
+ let consecutiveSelectionFailures = 0;
2182
+ const redraw = () => {
2183
+ if (stopped) return;
2184
+ try {
2185
+ screen.draw(render(state, { width: screen.width, height: screen.height, nowMs: now(), title: source.title }));
2186
+ } catch (error) {
2187
+ fail(error);
2188
+ }
2189
+ };
2190
+ const endIfFatal = (error) => {
2191
+ if (error instanceof AtcFatalError) fail(error);
2192
+ return stopped;
2193
+ };
2194
+ const refreshSelection = async () => {
2195
+ const flightId = state.selectedFlightId;
2196
+ const requestedView = state.view;
2197
+ if (stopped || detailFetchInFlight || !flightId || requestedView === "board") return;
2198
+ detailFetchInFlight = true;
2199
+ try {
2200
+ if (requestedView === "detail") {
2201
+ const [items, fullRecord] = await Promise.all([
2202
+ source.listEvents(flightId, requests.signal),
2203
+ source.readFlight?.(flightId, requests.signal)
2204
+ ]);
2205
+ if (stopped || state.selectedFlightId !== flightId) return;
2206
+ state = { ...state, events: { flightId, items } };
2207
+ if (fullRecord) state = mergeFlightRecord(state, fullRecord, now());
2208
+ } else {
2209
+ for (let pageCount = 0; pageCount < MAX_LOG_PAGES_PER_REFRESH && !stopped; pageCount += 1) {
2210
+ const buffer = state.logs?.flightId === flightId ? state.logs : null;
2211
+ const requestedCursor = buffer?.cursor;
2212
+ const page = await source.readLogs(flightId, requestedCursor, requests.signal);
2213
+ const current = state.logs?.flightId === flightId ? state.logs : null;
2214
+ if (stopped || state.selectedFlightId !== flightId || current?.cursor !== requestedCursor) return;
2215
+ if (page.restart) {
2216
+ state = restartLogs(state, flightId);
2217
+ continue;
2218
+ }
2219
+ state = appendLogs(state, flightId, page.lines, page.nextCursor, page.complete, page.attemptSeq);
2220
+ if (!page.hasMore) break;
2221
+ redraw();
2222
+ }
2223
+ }
2224
+ consecutiveSelectionFailures = 0;
2225
+ state = { ...state, selectionError: null };
2226
+ } catch (error) {
2227
+ if (!endIfFatal(error)) {
2228
+ consecutiveSelectionFailures += 1;
2229
+ state = { ...state, selectionError: describeFailure(error) };
2230
+ }
2231
+ } finally {
2232
+ detailFetchInFlight = false;
2233
+ redraw();
2234
+ }
2235
+ if (!stopped && (state.selectedFlightId !== flightId || state.view !== requestedView)) spawn(refreshSelection);
2236
+ };
2237
+ const poll = async () => {
2238
+ if (stopped || pollInFlight) return;
2239
+ pollInFlight = true;
2240
+ let delayMs = source.pollIntervalMs;
2241
+ try {
2242
+ const flights = await source.listFlights(requests.signal);
2243
+ if (stopped) return;
2244
+ consecutivePollFailures = 0;
2245
+ state = applyFlights(state, flights, now());
2246
+ } catch (error) {
2247
+ consecutivePollFailures += 1;
2248
+ delayMs = Math.min(MAX_BACKOFF_MS, source.pollIntervalMs * 2 ** consecutivePollFailures);
2249
+ if (!endIfFatal(error)) {
2250
+ const retrying = consecutivePollFailures > 1 ? ` (retrying in ${Math.round(delayMs / 1e3)}s)` : "";
2251
+ state = { ...state, lastError: describeFailure(error) + retrying };
2252
+ }
2253
+ } finally {
2254
+ pollInFlight = false;
2255
+ }
2256
+ redraw();
2257
+ if (!stopped) pollTimer = setTimeout(() => spawn(poll), delayMs);
2258
+ };
2259
+ const selectionDelayMs = () => {
2260
+ const base = source.selectionPollIntervalMs ?? source.pollIntervalMs;
2261
+ const flight = selectedFlight(state);
2262
+ const cadence = flight && isTerminal(flight.status) ? Math.max(base, SETTLED_REFRESH_MS) : base;
2263
+ return Math.min(MAX_BACKOFF_MS, cadence * 2 ** consecutiveSelectionFailures);
2264
+ };
2265
+ const runSelection = async () => {
2266
+ await refreshSelection();
2267
+ scheduleSelection();
2268
+ };
2269
+ function scheduleSelection(delayMs = selectionDelayMs()) {
2270
+ clearTimeout(selectionTimer);
2271
+ if (!stopped) selectionTimer = setTimeout(() => spawn(runSelection), delayMs);
2272
+ }
2273
+ function spawn(task) {
2274
+ task().catch(fail);
2275
+ }
2276
+ const applyKeys = (keys) => {
2277
+ const before = state;
2278
+ const context = { boardViewportRows: boardViewportRows(screen.height), logViewportRows: logViewportRows(screen.height) };
2279
+ let refreshRequested = false;
2280
+ for (const key of keys) {
2281
+ if (key === "redraw") screen.invalidate();
2282
+ if (key === "refresh") refreshRequested = true;
2283
+ state = reduceKey(state, key, context);
2284
+ }
2285
+ if (state.quit) {
2286
+ stop();
2287
+ return;
2288
+ }
2289
+ if (refreshRequested) {
2290
+ consecutivePollFailures = 0;
2291
+ consecutiveSelectionFailures = 0;
2292
+ clearTimeout(pollTimer);
2293
+ spawn(poll);
2294
+ spawn(runSelection);
2295
+ }
2296
+ if (state.view !== before.view || state.selectedFlightId !== before.selectedFlightId) {
2297
+ consecutiveSelectionFailures = 0;
2298
+ state = { ...state, selectionError: null };
2299
+ spawn(runSelection);
2300
+ }
2301
+ redraw();
2302
+ };
2303
+ const onKeys = (chunk) => {
2304
+ clearTimeout(escapeTimer);
2305
+ applyKeys(keyReader.feed(chunk));
2306
+ const timeoutMs = keyReader.pendingTimeoutMs;
2307
+ if (!stopped && timeoutMs !== null) {
2308
+ escapeTimer = setTimeout(() => applyKeys(keyReader.flushPending()), timeoutMs);
2309
+ }
2310
+ };
2311
+ const onResize = () => {
2312
+ screen.invalidate();
2313
+ redraw();
2314
+ };
2315
+ const restoreTerminal = () => {
2316
+ terminal.input.off("data", onKeys);
2317
+ terminal.output.off?.("resize", onResize);
2318
+ try {
2319
+ terminal.input.setRawMode?.(false);
2320
+ } catch {
2321
+ }
2322
+ terminal.input.pause();
2323
+ screen.leave();
2324
+ };
2325
+ const onProcessExit = () => restoreTerminal();
2326
+ const onSignal = (signal) => {
2327
+ process.exitCode = SIGNAL_EXIT_CODES[signal] ?? 1;
2328
+ stop();
2329
+ };
2330
+ function teardown() {
2331
+ if (stopped) return false;
2332
+ stopped = true;
2333
+ requests.abort();
2334
+ clearTimeout(pollTimer);
2335
+ clearTimeout(selectionTimer);
2336
+ clearTimeout(escapeTimer);
2337
+ clearInterval(animationTimer);
2338
+ process.off("exit", onProcessExit);
2339
+ for (const signal of HANDLED_SIGNALS) process.off(signal, onSignal);
2340
+ restoreTerminal();
2341
+ return true;
2342
+ }
2343
+ function stop() {
2344
+ if (teardown()) resolveRun();
2345
+ }
2346
+ function fail(error) {
2347
+ if (teardown()) rejectRun(error);
2348
+ }
2349
+ process.on("exit", onProcessExit);
2350
+ for (const signal of HANDLED_SIGNALS) process.on(signal, onSignal);
2351
+ terminal.input.setRawMode?.(true);
2352
+ terminal.input.setEncoding("utf8");
2353
+ terminal.input.on("data", onKeys);
2354
+ terminal.input.resume();
2355
+ terminal.output.on?.("resize", onResize);
2356
+ animationTimer = setInterval(redraw, ANIMATION_INTERVAL_MS);
2357
+ screen.enter();
2358
+ redraw();
2359
+ if (!stopped) {
2360
+ spawn(poll);
2361
+ spawn(runSelection);
2362
+ }
2363
+ await finished;
2364
+ }
2365
+
2366
+ // src/atc/demo-source.ts
2367
+ var MOLECULES = [
2368
+ "water",
2369
+ "methane",
2370
+ "ammonia",
2371
+ "ethanol",
2372
+ "benzene",
2373
+ "formaldehyde",
2374
+ "acetone",
2375
+ "caffeine",
2376
+ "glycine",
2377
+ "urea",
2378
+ "pyridine",
2379
+ "phenol",
2380
+ "toluene",
2381
+ "aspirin",
2382
+ "naphthalene",
2383
+ "acetic-acid",
2384
+ "imidazole",
2385
+ "furan",
2386
+ "thiophene",
2387
+ "indole",
2388
+ "serotonin"
2389
+ ];
2390
+ var CONCURRENT_GPUS = 8;
2391
+ var MAX_DURATION_S2 = 150;
2392
+ var FAILING_TASK = 13;
2393
+ var RECOVERING_TASK = 6;
2394
+ var CANCELLED_TASK = 20;
2395
+ var CANCELLED_AT_S = 35;
2396
+ function seededRandom(seed) {
2397
+ let state = seed >>> 0;
2398
+ return () => {
2399
+ state = state + 1831565813 >>> 0;
2400
+ let mixed = state;
2401
+ mixed = Math.imul(mixed ^ mixed >>> 15, mixed | 1);
2402
+ mixed ^= mixed + Math.imul(mixed ^ mixed >>> 7, mixed | 61);
2403
+ return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
2404
+ };
2405
+ }
2406
+ function between(random, low, high) {
2407
+ return low + random() * (high - low);
2408
+ }
2409
+ function buildLogs(plan, random) {
2410
+ const logs2 = [];
2411
+ const at = (offsetS, content, stream = "stdout") => logs2.push({ atS: plan.startAtS + offsetS, stream, content });
2412
+ at(0.2, `[molsim] task ${plan.taskIndex} of 21 \xB7 SLURM_ARRAY_TASK_ID=${plan.taskIndex}
2413
+ `);
2414
+ at(0.5, `[molsim] loading geometry inputs/${plan.molecule}.xyz
2415
+ `);
2416
+ at(0.9, `[molsim] method B3LYP/def2-TZVP \xB7 device cuda:0 (${plan.gpuType})
2417
+ `);
2418
+ at(1.2, "[molsim] building integrals ");
2419
+ for (let percent = 10; percent <= 100; percent += 30) at(1.2 + percent / 60, `\r[molsim] building integrals ${percent}%`);
2420
+ at(3.2, "\n");
2421
+ const runSeconds = plan.finalizeAtS - plan.startAtS;
2422
+ const iterations = Math.max(6, Math.floor((runSeconds - 4) / 1.6));
2423
+ let energy = -40 - random() * 400;
2424
+ let delta = 0.5 + random();
2425
+ for (let iteration = 1; iteration <= iterations; iteration += 1) {
2426
+ const offsetS = 3.6 + (iteration - 1) * ((runSeconds - 4.5) / iterations);
2427
+ if (plan.recoveryWindowS && plan.startAtS + offsetS >= plan.recoveryWindowS[0] && plan.startAtS + offsetS < plan.recoveryWindowS[1]) continue;
2428
+ energy -= delta;
2429
+ delta *= 0.35 + random() * 0.2;
2430
+ at(offsetS, `SCF iter ${String(iteration).padStart(3)} E = ${energy.toFixed(8)} Ha \u0394E = ${delta.toExponential(2)}
2431
+ `);
2432
+ if (iteration === 4 && random() < 0.4) at(offsetS + 0.1, "UserWarning: level shift applied to aid convergence\n", "stderr");
2433
+ }
2434
+ if (plan.recoveryWindowS) {
2435
+ const [faultS, resumeS] = plan.recoveryWindowS;
2436
+ logs2.push({ atS: faultS, stream: "stderr", content: "CUDA error: an illegal memory access was encountered (Xid 79)\n" });
2437
+ logs2.push({ atS: resumeS + 0.5, stream: "stdout", content: "[molsim] resumed on replacement GPU from checkpoint\n" });
2438
+ }
2439
+ if (plan.outcome === "failed") {
2440
+ logs2.push({ atS: plan.finalizeAtS - 0.6, stream: "stderr", content: 'Traceback (most recent call last):\n File "run.py", line 88, in <module>\n result = scf.kernel()\n' });
2441
+ logs2.push({ atS: plan.finalizeAtS - 0.4, stream: "stderr", content: "RuntimeError: SCF did not converge in 128 cycles\n" });
2442
+ } else if (plan.outcome === "completed") {
2443
+ logs2.push({ atS: plan.finalizeAtS - 0.4, stream: "stdout", content: `[molsim] converged: E(total) = ${energy.toFixed(8)} Ha
2444
+ ` });
2445
+ logs2.push({ atS: plan.finalizeAtS + 0.3, stream: "stdout", content: `[molsim] wrote outputs/${plan.molecule}.json
2446
+ ` });
2447
+ }
2448
+ return logs2.filter((entry) => entry.atS < plan.endAtS).sort((left, right) => left.atS - right.atS);
2449
+ }
2450
+ function buildEvents(plan) {
2451
+ const events = [{ atS: 0, type: "state_changed", message: "Flight queued" }];
2452
+ events.push({ atS: plan.dispatchAtS, type: "state_changed", message: `Dispatching to lambda (${plan.gpuType})` });
2453
+ events.push({ atS: plan.startAtS, type: "state_changed", message: "Instance booted; workload running" });
2454
+ if (plan.recoveryWindowS) {
2455
+ const [faultS, resumeS] = plan.recoveryWindowS;
2456
+ events.push({ atS: faultS, type: "gpu_recovery_started", message: "GPU fault detected (Xid 79); collecting partial outputs" });
2457
+ events.push({ atS: faultS + 2, type: "gpu_recovery_dispatch", message: "Replacement instance authorized (1 of 1)" });
2458
+ events.push({ atS: resumeS, type: "state_changed", message: "Replacement running" });
2459
+ }
2460
+ events.push({ atS: plan.finalizeAtS, type: "state_changed", message: "Collecting outputs" });
2461
+ if (plan.outcome === "cancelled") {
2462
+ const beforeCancel = events.filter((event) => event.atS < plan.endAtS);
2463
+ return [...beforeCancel, { atS: plan.endAtS, type: "state_changed", message: "Flight cancelled by user" }];
2464
+ }
2465
+ events.push({ atS: plan.endAtS, type: "state_changed", message: plan.outcome === "failed" ? "Workload exited with code 1" : "Flight completed" });
2466
+ return events;
2467
+ }
2468
+ function buildDemoPlan(seed = 21) {
2469
+ const random = seededRandom(seed);
2470
+ const gpuFreeAtS = Array.from({ length: CONCURRENT_GPUS }, () => 0);
2471
+ const plans = [];
2472
+ for (let taskIndex = 0; taskIndex < MOLECULES.length; taskIndex += 1) {
2473
+ const slot = gpuFreeAtS.indexOf(Math.min(...gpuFreeAtS));
2474
+ const dispatchAtS = Math.max(gpuFreeAtS[slot], 1 + taskIndex * 0.4);
2475
+ const startAtS = dispatchAtS + between(random, 3, 9);
2476
+ const recoveryWindowS = taskIndex === RECOVERING_TASK ? [startAtS + 14, startAtS + 24] : null;
2477
+ const finalizeAtS = startAtS + between(random, 18, 55) + (recoveryWindowS ? 10 : 0);
2478
+ const endAtS = finalizeAtS + between(random, 2, 4);
2479
+ let outcome = taskIndex === FAILING_TASK ? "failed" : "completed";
2480
+ let taskEndS = endAtS;
2481
+ if (taskIndex === CANCELLED_TASK) {
2482
+ outcome = "cancelled";
2483
+ taskEndS = CANCELLED_AT_S;
2484
+ }
2485
+ if (outcome !== "cancelled") gpuFreeAtS[slot] = endAtS;
2486
+ else if (taskEndS > dispatchAtS) gpuFreeAtS[slot] = taskEndS;
2487
+ const partial = {
2488
+ taskIndex,
2489
+ // Same shape as real IDs: "fl-" and 24 hex digits.
2490
+ flightId: `fl-${Math.floor(random() * 4294967295).toString(16).padStart(8, "0")}${String(taskIndex).padStart(16, "0")}`,
2491
+ name: `molsim[${taskIndex}]`,
2492
+ molecule: MOLECULES[taskIndex],
2493
+ gpuType: taskIndex % 5 === 4 ? "H100" : "A100",
2494
+ dispatchAtS,
2495
+ startAtS,
2496
+ finalizeAtS,
2497
+ endAtS: taskEndS,
2498
+ outcome,
2499
+ recoveryWindowS
2500
+ };
2501
+ const events = buildEvents(partial);
2502
+ plans.push({ ...partial, events, logs: buildLogs({ ...partial, events }, random) });
2503
+ }
2504
+ return plans;
2505
+ }
2506
+ function statusAt(plan, elapsedS) {
2507
+ if (elapsedS >= plan.endAtS) return plan.outcome;
2508
+ if (elapsedS < plan.dispatchAtS) return "queued";
2509
+ if (elapsedS < plan.startAtS) return "dispatching";
2510
+ if (elapsedS < plan.finalizeAtS) return "running";
2511
+ return "finalizing";
2512
+ }
2513
+ var DemoDataSource = class {
2514
+ constructor(now = Date.now, seed = 21) {
2515
+ this.now = now;
2516
+ this.plans = buildDemoPlan(seed);
2517
+ this.startedAtMs = now();
2518
+ }
2519
+ now;
2520
+ title = "DEMO \xB7 molsim array \xB7 21 tasks";
2521
+ pollIntervalMs = 500;
2522
+ plans;
2523
+ startedAtMs;
2524
+ elapsedS() {
2525
+ return (this.now() - this.startedAtMs) / 1e3;
2526
+ }
2527
+ iso(offsetS) {
2528
+ return new Date(this.startedAtMs + offsetS * 1e3).toISOString();
2529
+ }
2530
+ plan(flightId) {
2531
+ const plan = this.plans.find((candidate) => candidate.flightId === flightId);
2532
+ if (!plan) throw new Error(`Unknown demo flight ${flightId}`);
2533
+ return plan;
2534
+ }
2535
+ async listFlights() {
2536
+ const elapsedS = this.elapsedS();
2537
+ return this.plans.map((plan) => {
2538
+ const status2 = statusAt(plan, elapsedS);
2539
+ const reached = (offsetS) => elapsedS >= offsetS && offsetS < plan.endAtS ? this.iso(offsetS) : null;
2540
+ const finished = elapsedS >= plan.endAtS;
2541
+ const inRecovery = plan.recoveryWindowS !== null && elapsedS >= plan.recoveryWindowS[0] && elapsedS < plan.recoveryWindowS[1];
2542
+ return {
2543
+ flight_id: plan.flightId,
2544
+ name: plan.name,
2545
+ status: status2,
2546
+ gpu_type: plan.gpuType,
2547
+ gpu_count: 1,
2548
+ max_duration_s: MAX_DURATION_S2,
2549
+ queued_at: this.iso(0),
2550
+ dispatched_at: reached(plan.dispatchAtS),
2551
+ started_at: reached(plan.startAtS),
2552
+ completed_at: finished ? this.iso(plan.endAtS) : null,
2553
+ exit_code: finished && plan.outcome !== "cancelled" ? plan.outcome === "failed" ? 1 : 0 : null,
2554
+ error_message: finished && plan.outcome === "failed" ? "Workload exited with code 1" : null,
2555
+ charge_credits: finished && plan.startAtS < plan.endAtS ? Math.max(1, Math.ceil((plan.endAtS - plan.startAtS) / 20)) : null,
2556
+ provider: elapsedS >= plan.dispatchAtS && plan.dispatchAtS < plan.endAtS ? "lambda" : null,
2557
+ recovery: inRecovery ? { phase: elapsedS < plan.recoveryWindowS[0] + 2 ? "collecting" : "replacement" } : null
2558
+ };
2559
+ });
2560
+ }
2561
+ async listEvents(flightId) {
2562
+ const elapsedS = this.elapsedS();
2563
+ return this.plan(flightId).events.filter((event) => event.atS <= elapsedS).map((event, index) => ({ id: index + 1, type: event.type, message: event.message, created_at: this.iso(event.atS) }));
2564
+ }
2565
+ async readLogs(flightId, cursor) {
2566
+ const plan = this.plan(flightId);
2567
+ const elapsedS = this.elapsedS();
2568
+ const startIndex = cursor ? Number(cursor) : 0;
2569
+ const available = plan.logs.filter((entry) => entry.atS <= elapsedS);
2570
+ const lines = available.slice(startIndex).map(({ stream, content }) => ({ stream, content }));
2571
+ return {
2572
+ lines,
2573
+ nextCursor: String(available.length),
2574
+ complete: elapsedS >= plan.endAtS && available.length === plan.logs.length
2575
+ };
2576
+ }
2577
+ };
2578
+
2579
+ // src/atc/live-source.ts
2580
+ var RECENT_FLIGHT_LIMIT = 50;
2581
+ var SEARCH_FLIGHT_LIMIT = 200;
2582
+ var BATCH_WINDOW_MARGIN_MS = 5 * 6e4;
2583
+ var MAX_BATCH_PAGES = 10;
2584
+ var MAX_STRAGGLER_READS = 20;
2585
+ var MAX_CONSECUTIVE_STRAGGLER_FAILURES = 3;
2586
+ var REQUEST_TIMEOUT_MS = 15e3;
2587
+ function placeholder(member, status2) {
2588
+ return {
2589
+ flight_id: member.flight_id,
2590
+ name: member.name,
2591
+ status: status2,
2592
+ gpu_type: null,
2593
+ gpu_count: 1,
2594
+ max_duration_s: null,
2595
+ queued_at: null,
2596
+ dispatched_at: null,
2597
+ started_at: null,
2598
+ completed_at: null,
2599
+ exit_code: null,
2600
+ error_message: null
2601
+ };
2602
+ }
2603
+ function nameMatcher(pattern) {
2604
+ const source = [...pattern].map((character) => character === "*" ? ".*" : character === "?" ? "." : character.replace(/[.+^${}()|[\]\\]/g, "\\$&")).join("");
2605
+ const expression = new RegExp(`^${source}$`, "su");
2606
+ return (name) => expression.test(name);
2607
+ }
2608
+ var INVALID_RESPONSE = "Unexpected response from the Flightdeck API.";
2609
+ function record(value) {
2610
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw new CliError(INVALID_RESPONSE);
2611
+ return value;
2612
+ }
2613
+ var text = (value) => typeof value === "string" ? value : null;
2614
+ var count = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
2615
+ function toFlight(value) {
2616
+ const raw = record(value);
2617
+ const flightId = text(raw.flight_id);
2618
+ if (!flightId) throw new CliError(INVALID_RESPONSE);
2619
+ const recoveryPhase = raw.recovery && typeof raw.recovery === "object" ? text(raw.recovery.phase) : null;
2620
+ return {
2621
+ flight_id: flightId,
2622
+ name: text(raw.name) ?? flightId,
2623
+ status: text(raw.status) ?? "unknown",
2624
+ gpu_type: text(raw.gpu_type),
2625
+ gpu_count: count(raw.gpu_count) ?? 1,
2626
+ max_duration_s: count(raw.max_duration_s),
2627
+ queued_at: text(raw.queued_at),
2628
+ dispatched_at: text(raw.dispatched_at),
2629
+ started_at: text(raw.started_at),
2630
+ completed_at: text(raw.completed_at),
2631
+ exit_code: count(raw.exit_code),
2632
+ error_message: text(raw.error_message),
2633
+ // Customers receive credits; platform admins receive monetary fields instead, which the board does not show.
2634
+ charge_credits: count(raw.charge_credits),
2635
+ provider: text(raw.provider),
2636
+ recovery: !("recovery" in raw) ? void 0 : recoveryPhase ? { phase: recoveryPhase } : null
2637
+ };
2638
+ }
2639
+ var LiveDataSource = class {
2640
+ constructor(target, config, request2 = apiRequest, timeoutMs = REQUEST_TIMEOUT_MS) {
2641
+ this.target = target;
2642
+ this.config = config;
2643
+ this.request = request2;
2644
+ this.timeoutMs = timeoutMs;
2645
+ if (target.kind === "flight") this.initialFlightId = target.flightId;
2646
+ }
2647
+ target;
2648
+ config;
2649
+ request;
2650
+ timeoutMs;
2651
+ pollIntervalMs = 5e3;
2652
+ selectionPollIntervalMs = 2e3;
2653
+ initialFlightId;
2654
+ accountLabel = null;
2655
+ /** Settled batch tasks never change, so they are not looked up again. */
2656
+ settledBatchFlights = /* @__PURE__ */ new Map();
2657
+ /** Last known record of every batch task, shown when a poll could not refresh it. */
2658
+ lastKnownBatchFlights = /* @__PURE__ */ new Map();
2659
+ /** When each straggler was last read singly, so the per-poll cap rotates through them. */
2660
+ stragglerReadAt = /* @__PURE__ */ new Map();
2661
+ get title() {
2662
+ const scope = this.target.kind === "recent" ? `recent flights (last ${RECENT_FLIGHT_LIMIT})` : this.target.kind === "flight" ? `flight ${this.target.flightId}` : this.target.kind === "batch" ? `sbatch ${this.target.record.job_id} \u2500\u2500\u2500 ${this.target.record.job_name}` : `flights named ${this.target.pattern} (last ${SEARCH_FLIGHT_LIMIT})`;
2663
+ return this.accountLabel ? `${scope} \u2500\u2500\u2500 ${this.accountLabel}` : scope;
2664
+ }
2665
+ /**
2666
+ * Run before the screen opens, so sign-in problems and unknown flights are
2667
+ * reported as ordinary CLI errors. Also learns the account name for the title.
2668
+ */
2669
+ async preflight(signal) {
2670
+ try {
2671
+ const account = record(await (await this.get("/auth/me", "Unable to read account", signal)).json());
2672
+ this.accountLabel = text(account.name) ?? text(account.email);
2673
+ } catch {
2674
+ }
2675
+ await this.listFlights(signal);
2676
+ }
2677
+ async get(path, action, signal, allowed = []) {
2678
+ let response;
2679
+ try {
2680
+ response = await this.request("GET", path, void 0, {
2681
+ config: this.config,
2682
+ signal: AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)])
2683
+ });
2684
+ } catch (error) {
2685
+ if (error instanceof NetworkError) throw error;
2686
+ if (error instanceof CliError) throw new AtcFatalError(error.message, { cause: error });
2687
+ throw error;
2688
+ }
2689
+ if (response.ok || allowed.includes(response.status)) return response;
2690
+ await response.body?.cancel();
2691
+ if (response.status === 401) throw new AtcFatalError(httpError(action, 401).message);
2692
+ throw httpError(action, response.status);
2693
+ }
2694
+ async listPage(query, signal) {
2695
+ const response = await this.get(`/flights?${query}`, "Unable to list flights", signal);
2696
+ const flights = record(await response.json()).flights;
2697
+ if (!Array.isArray(flights)) throw new CliError(INVALID_RESPONSE);
2698
+ return flights.map(toFlight);
2699
+ }
2700
+ async listFlights(signal) {
2701
+ switch (this.target.kind) {
2702
+ case "flight":
2703
+ return [await this.readFlight(this.target.flightId, signal)];
2704
+ case "recent":
2705
+ return this.listPage(`limit=${RECENT_FLIGHT_LIMIT}`, signal);
2706
+ case "name": {
2707
+ const matches = nameMatcher(this.target.pattern);
2708
+ return (await this.listPage(`limit=${SEARCH_FLIGHT_LIMIT}`, signal)).filter((flight) => matches(flight.name));
2709
+ }
2710
+ case "batch":
2711
+ return this.listBatch(this.target.record, signal);
2712
+ }
2713
+ }
2714
+ /**
2715
+ * The list returns the newest matches first, so a large array's oldest tasks
2716
+ * fall past the first page. Walk back through the submission window with
2717
+ * `to=` until every unsettled task is found; settled tasks are remembered and
2718
+ * need no lookup. The first page is always read, even for a finished array,
2719
+ * so an expired login or lost connection still shows.
2720
+ */
2721
+ async listBatch(batch, signal) {
2722
+ const from = new Date(Date.parse(batch.submitted_at) - BATCH_WINDOW_MARGIN_MS).toISOString();
2723
+ const wanted = new Set(batch.flights.map((member) => member.flight_id).filter((id) => !this.settledBatchFlights.has(id)));
2724
+ const found = /* @__PURE__ */ new Map();
2725
+ let to;
2726
+ for (let page = 0; page < MAX_BATCH_PAGES && (page === 0 || found.size < wanted.size); page += 1) {
2727
+ const window = `from=${encodeURIComponent(from)}${to ? `&to=${encodeURIComponent(to)}` : ""}`;
2728
+ const rows = await this.listPage(`${window}&limit=${SEARCH_FLIGHT_LIMIT}`, signal);
2729
+ for (const flight of rows) if (wanted.has(flight.flight_id)) found.set(flight.flight_id, flight);
2730
+ const oldest = rows[rows.length - 1]?.queued_at ?? void 0;
2731
+ if (rows.length < SEARCH_FLIGHT_LIMIT || !oldest || oldest === to) break;
2732
+ to = oldest;
2733
+ }
2734
+ const stragglers = [...wanted].filter((id) => !found.has(id)).sort((left, right) => (this.stragglerReadAt.get(left) ?? 0) - (this.stragglerReadAt.get(right) ?? 0)).slice(0, MAX_STRAGGLER_READS);
2735
+ let consecutiveFailures = 0;
2736
+ for (const flightId of stragglers) {
2737
+ const member = batch.flights.find((candidate) => candidate.flight_id === flightId);
2738
+ this.stragglerReadAt.set(flightId, Date.now());
2739
+ try {
2740
+ found.set(flightId, await this.readBatchMember(member, signal));
2741
+ consecutiveFailures = 0;
2742
+ } catch (error) {
2743
+ if (error instanceof AtcFatalError || error instanceof NetworkError || signal.aborted) throw error;
2744
+ if (++consecutiveFailures >= MAX_CONSECUTIVE_STRAGGLER_FAILURES) break;
2745
+ }
2746
+ }
2747
+ return batch.flights.map((member) => {
2748
+ const flight = this.settledBatchFlights.get(member.flight_id) ?? found.get(member.flight_id) ?? this.lastKnownBatchFlights.get(member.flight_id) ?? placeholder(member, "unknown");
2749
+ if (isTerminal(flight.status) || flight.status === "missing") this.settledBatchFlights.set(member.flight_id, flight);
2750
+ this.lastKnownBatchFlights.set(member.flight_id, flight);
2751
+ return flight;
2752
+ });
2753
+ }
2754
+ async readBatchMember(member, signal) {
2755
+ const response = await this.get(`/flights/${encodeURIComponent(member.flight_id)}`, "Unable to read flight", signal, [404]);
2756
+ if (response.status !== 404) return toFlight(await response.json());
2757
+ await response.body?.cancel();
2758
+ return placeholder(member, "missing");
2759
+ }
2760
+ async readFlight(flightId, signal) {
2761
+ const response = await this.get(`/flights/${encodeURIComponent(flightId)}`, "Unable to read flight", signal, [404]);
2762
+ if (response.status === 404) {
2763
+ await response.body?.cancel();
2764
+ if (this.target.kind === "flight") throw new AtcFatalError(`Flight ${flightId} was not found for this account.`);
2765
+ throw httpError("Unable to read flight", 404);
2766
+ }
2767
+ return toFlight(await response.json());
2768
+ }
2769
+ async listEvents(flightId, signal) {
2770
+ const response = await this.get(`/flights/${encodeURIComponent(flightId)}/events`, "Unable to read flight events", signal);
2771
+ const events = record(await response.json()).events;
2772
+ if (!Array.isArray(events)) throw new CliError(INVALID_RESPONSE);
2773
+ return events.map((value) => {
2774
+ const raw = record(value);
2775
+ return { id: count(raw.id) ?? 0, type: text(raw.type) ?? "event", message: text(raw.message), created_at: text(raw.created_at) ?? "" };
2776
+ });
2777
+ }
2778
+ async readLogs(flightId, cursor, signal) {
2779
+ const query = cursor ? `?cursor=${encodeURIComponent(cursor)}` : "";
2780
+ const response = await this.get(`/flights/${encodeURIComponent(flightId)}/logs${query}`, "Unable to read logs", signal, [400]);
2781
+ if (response.status === 400) {
2782
+ const body2 = await response.json().catch(() => ({}));
2783
+ if (cursor && body2.error === "invalid_cursor") return { lines: [], nextCursor: "", complete: false, restart: true };
2784
+ throw httpError("Unable to read logs", 400);
2785
+ }
2786
+ const body = record(await response.json());
2787
+ if (typeof body.has_more !== "boolean") {
2788
+ throw new AtcFatalError("This backend does not support complete log pagination. Deploy the updated backend before using flightdeck atc.");
2789
+ }
2790
+ const nextCursor = text(body.next_cursor);
2791
+ if (!Array.isArray(body.logs) || typeof body.complete !== "boolean" || !nextCursor || body.has_more && body.complete) {
2792
+ throw new CliError(INVALID_RESPONSE);
2793
+ }
2794
+ if (body.has_more && nextCursor === cursor) throw new CliError("Log pagination cursor did not advance; retrying.");
2795
+ const lines = body.logs.map((value) => {
2796
+ const raw = record(value);
2797
+ const content = text(raw.content);
2798
+ if (content === null || typeof raw.stream !== "string") throw new CliError(INVALID_RESPONSE);
2799
+ return { stream: raw.stream, content };
2800
+ });
2801
+ const attemptSeq = body.attempt_seq === null ? null : count(body.attempt_seq) ?? void 0;
2802
+ return { lines, nextCursor, complete: body.complete, hasMore: body.has_more, attemptSeq };
2803
+ }
2804
+ };
2805
+
2806
+ // src/commands/atc.ts
2807
+ function batchTarget(jobId, config) {
2808
+ const record2 = loadBatchRecord(jobId);
2809
+ if (!record2) {
2810
+ throw new CliError(`No local record of sbatch job ${jobId}. \`flightdeck sbatch\` records arrays on the machine that submitted them (from this CLI version on). Try \`flightdeck atc --name '<job-name>*'\` instead.`);
2811
+ }
2812
+ if (normalizeApiUrl(record2.api_url) !== normalizeApiUrl(config.api_url)) {
2813
+ throw new CliError(`sbatch job ${jobId} was submitted to ${record2.api_url}, but this login uses ${config.api_url}.`);
2814
+ }
2815
+ return { kind: "batch", record: record2 };
2816
+ }
2817
+ function liveTarget(target, name, config) {
2818
+ if (name !== void 0) {
2819
+ if (target) throw new CliError("Use either a flight/job ID or --name, not both.");
2820
+ if (!name.trim()) throw new CliError("--name needs a pattern, such as 'molsim*'.");
2821
+ return { kind: "name", pattern: name };
2822
+ }
2823
+ if (!target) return { kind: "recent" };
2824
+ if (isBatchJobId(target)) return batchTarget(target, config);
2825
+ return { kind: "flight", flightId: target };
2826
+ }
2827
+ async function atc(target, options) {
2828
+ if (options.demo && (target || options.name !== void 0)) throw new CliError("--demo runs a simulated array; drop the flight/job ID or --name.");
2829
+ const config = options.demo ? null : loadConfig();
2830
+ const liveTargetChoice = config ? liveTarget(target, options.name, config) : null;
2831
+ if (!process.stdin.isTTY || !process.stdout.isTTY || process.env.TERM === "dumb") {
2832
+ throw new CliError("flightdeck atc needs an interactive terminal. Use `flightdeck status` or `flightdeck logs` in scripts.");
2833
+ }
2834
+ let source;
2835
+ if (!config || !liveTargetChoice) {
2836
+ source = new DemoDataSource();
2837
+ } else {
2838
+ const liveSource = new LiveDataSource(liveTargetChoice, config);
2839
+ await liveSource.preflight(new AbortController().signal);
2840
+ source = liveSource;
2841
+ }
2842
+ await runAtc(source, {
2843
+ input: process.stdin,
2844
+ output: process.stdout,
2845
+ // https://no-color.org: any non-empty value disables color.
2846
+ colorEnabled: !process.env.NO_COLOR
2847
+ });
2848
+ }
2849
+
1159
2850
  // src/commands/outputs.ts
1160
2851
  async function listOutputs(flightId) {
1161
2852
  const response = await apiRequest("GET", `/flights/${flightId}/outputs`);
@@ -1169,7 +2860,7 @@ async function listOutputs(flightId) {
1169
2860
 
1170
2861
  // src/commands/download.ts
1171
2862
  import { mkdir, writeFile } from "node:fs/promises";
1172
- import { basename, join as join3, resolve as resolve3 } from "node:path";
2863
+ import { basename, join as join4, resolve as resolve3 } from "node:path";
1173
2864
  async function download(flightId, options) {
1174
2865
  const manifestResponse = await apiRequest("GET", `/flights/${flightId}/outputs`);
1175
2866
  if (!manifestResponse.ok) throw httpError("Unable to list outputs for download", manifestResponse.status);
@@ -1179,15 +2870,15 @@ async function download(flightId, options) {
1179
2870
  for (const output of manifest.outputs ?? []) {
1180
2871
  const response = await artifactRequest(flightId, output.artifact_id);
1181
2872
  if (!response.ok) throw httpError("Unable to download output", response.status);
1182
- const target = join3(root, basename(output.filename));
2873
+ const target = join4(root, basename(output.filename));
1183
2874
  await writeFile(target, Buffer.from(await response.arrayBuffer()), { flag: "wx" });
1184
2875
  console.log(`Downloaded ${target}`);
1185
2876
  }
1186
2877
  }
1187
2878
 
1188
2879
  // src/commands/slurm-test.ts
1189
- import { mkdtempSync as mkdtempSync2, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "node:fs";
1190
- import { join as join4, relative as relative2 } from "node:path";
2880
+ import { mkdtempSync as mkdtempSync2, rmSync as rmSync3, writeFileSync as writeFileSync3 } from "node:fs";
2881
+ import { join as join5, relative as relative2 } from "node:path";
1191
2882
  var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]);
1192
2883
  var CONFORMANCE_SCRIPT = `#!/bin/bash
1193
2884
  #SBATCH --job-name=slurm-conformance
@@ -1246,9 +2937,9 @@ async function verifyResults(flightId, taskId) {
1246
2937
  async function slurmTest(options) {
1247
2938
  const timeoutMs = positiveSeconds(options.timeout, "--timeout") * 1e3;
1248
2939
  const pollIntervalMs = positiveSeconds(options.pollInterval, "--poll-interval") * 1e3;
1249
- const tempDir = mkdtempSync2(join4(process.cwd(), ".flightdeck-slurm-test-"));
1250
- const scriptPath = join4(tempDir, "conformance.sbatch");
1251
- writeFileSync2(scriptPath, CONFORMANCE_SCRIPT, { encoding: "utf8", mode: 448 });
2940
+ const tempDir = mkdtempSync2(join5(process.cwd(), ".flightdeck-slurm-test-"));
2941
+ const scriptPath = join5(tempDir, "conformance.sbatch");
2942
+ writeFileSync3(scriptPath, CONFORMANCE_SCRIPT, { encoding: "utf8", mode: 448 });
1252
2943
  let submitted;
1253
2944
  try {
1254
2945
  const result = await sbatch(relative2(process.cwd(), scriptPath), {
@@ -1256,7 +2947,7 @@ async function slurmTest(options) {
1256
2947
  });
1257
2948
  submitted = result.submitted;
1258
2949
  } finally {
1259
- rmSync2(tempDir, { recursive: true, force: true });
2950
+ rmSync3(tempDir, { recursive: true, force: true });
1260
2951
  }
1261
2952
  if (submitted.length !== 2) throw new CliError(`expected 2 conformance flights, submitted ${submitted.length}`);
1262
2953
  const deadline = Date.now() + timeoutMs;
@@ -1288,11 +2979,37 @@ Slurm provider conformance passed for ${submitted.length} array tasks.`);
1288
2979
  }
1289
2980
 
1290
2981
  // src/commands/login.ts
2982
+ import { hostname } from "node:os";
1291
2983
  var SCOPES = ["jobs:read", "jobs:submit", "jobs:cancel", "account:read"];
2984
+ function loginScopes(admin = false) {
2985
+ return admin ? [...SCOPES, "admin:jobs:read"] : [...SCOPES];
2986
+ }
2987
+ var HOST_LABEL_MAX_LENGTH = 60;
2988
+ function shortHostname(rawHostname) {
2989
+ const trimmed = rawHostname.trim();
2990
+ const isAddress = /^[\d.]+$/.test(trimmed) || trimmed.includes(":");
2991
+ const label = (isAddress ? trimmed : trimmed.split(".")[0]).replace(/[^\p{L}\p{M}\p{N}_-]+/gu, "-").replace(/^-+/, "");
2992
+ let capped = "";
2993
+ for (const character of label) {
2994
+ if (capped.length + character.length > HOST_LABEL_MAX_LENGTH) break;
2995
+ capped += character;
2996
+ }
2997
+ return capped.replace(/-+$/, "");
2998
+ }
2999
+ function defaultCredentialName(rawHostname = hostname()) {
3000
+ const host = shortHostname(rawHostname);
3001
+ return host ? `flightdeck-cli@${host}` : "flightdeck-cli";
3002
+ }
3003
+ function credentialNameInUseMessage(credentialName, rawHostname = hostname(), now = /* @__PURE__ */ new Date()) {
3004
+ const timestamp2 = now.toISOString().slice(0, 16).replace("T", "-").replace(":", "");
3005
+ let suggestedName = `${shortHostname(rawHostname) || "workstation"}-${timestamp2}`;
3006
+ if (suggestedName.toLowerCase() === credentialName.trim().toLowerCase()) suggestedName += "-2";
3007
+ return `Login was approved, but no credential was created: your account already has an active CLI credential named "${credentialName.trim()}". Each machine needs its own credential name. Run login again with a new name and approve it in the browser, e.g. flightdeck login --name ${suggestedName} (or revoke the old credential in the portal first).`;
3008
+ }
1292
3009
  function sleep(milliseconds) {
1293
3010
  return new Promise((resolve4) => setTimeout(resolve4, milliseconds));
1294
3011
  }
1295
- async function loginFailure(response) {
3012
+ async function loginFailure(response, credentialName) {
1296
3013
  const suspended = await suspensionError(response);
1297
3014
  if (suspended) return suspended;
1298
3015
  try {
@@ -1300,11 +3017,14 @@ async function loginFailure(response) {
1300
3017
  if (body.error === "device_authorization_denied") {
1301
3018
  return new CliError("Login was not approved. Run flightdeck login to try again.");
1302
3019
  }
3020
+ if (body.error === "admin_role_required") {
3021
+ return new CliError("This account is not an administrator; run flightdeck login without --admin.");
3022
+ }
1303
3023
  if (body.error === "expired_device_code") {
1304
3024
  return new CliError("Login expired before it was approved. Run flightdeck login to try again.");
1305
3025
  }
1306
3026
  if (body.error === "credential_name_in_use") {
1307
- return new CliError("That credential name is already in use. Choose another --name or revoke the old credential in the portal.");
3027
+ return new CliError(credentialNameInUseMessage(credentialName));
1308
3028
  }
1309
3029
  } catch {
1310
3030
  }
@@ -1315,9 +3035,10 @@ async function login(options) {
1315
3035
  throw new CliError("Already logged in. Run flightdeck whoami to check the account, or flightdeck logout before signing in again.");
1316
3036
  }
1317
3037
  const apiUrl = normalizeApiUrl(options.apiUrl ?? DEFAULT_API_URL);
3038
+ const credentialName = options.name ?? defaultCredentialName();
1318
3039
  const started = await publicApiRequest(apiUrl, "POST", "/auth/device/code", {
1319
- credential_name: options.name ?? "flightdeck-cli",
1320
- scopes: SCOPES
3040
+ credential_name: credentialName,
3041
+ scopes: loginScopes(options.admin)
1321
3042
  });
1322
3043
  if (!started.ok) throw httpError("Unable to start login", started.status);
1323
3044
  const authorization = await started.json();
@@ -1338,7 +3059,7 @@ Verification code: ${authorization.user_code}`);
1338
3059
  if (Number.isFinite(retryAfter) && retryAfter > 0) intervalSeconds = retryAfter;
1339
3060
  continue;
1340
3061
  }
1341
- if (!response.ok) throw await loginFailure(response);
3062
+ if (!response.ok) throw await loginFailure(response, credentialName);
1342
3063
  const token = await response.json();
1343
3064
  saveConfig({ api_url: apiUrl, api_key: token.access_token, credential_id: token.credential_id });
1344
3065
  console.log("Logged in. The credential was stored in ~/.flightdeck/config.json.");
@@ -1386,21 +3107,146 @@ async function balance() {
1386
3107
  console.log(JSON.stringify(await response.json(), null, 2));
1387
3108
  }
1388
3109
 
3110
+ // src/commands/admin-flights-format.ts
3111
+ function plain(value) {
3112
+ return value.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ");
3113
+ }
3114
+ function runtime(flight, now) {
3115
+ if (!flight.started_at) return "-";
3116
+ const seconds = Math.max(0, Math.floor(((flight.completed_at ? Date.parse(flight.completed_at) : now) - Date.parse(flight.started_at)) / 1e3));
3117
+ return `${Math.floor(seconds / 3600)}h ${Math.floor(seconds / 60) % 60}m ${seconds % 60}s`;
3118
+ }
3119
+ function formatAdminFlights(flights, platform, now = Date.now()) {
3120
+ if (!flights.length) return "No flights in this time window.";
3121
+ const organizations = /* @__PURE__ */ new Map();
3122
+ for (const flight of flights) {
3123
+ const key = platform ? flight.organization?.id ?? "" : "";
3124
+ if (!organizations.has(key)) organizations.set(key, {
3125
+ label: flight.organization ? `${plain(flight.organization.name)} (${plain(flight.organization.slug)})` : "(no organisation)",
3126
+ members: /* @__PURE__ */ new Map()
3127
+ });
3128
+ const group = organizations.get(key);
3129
+ const memberKey = flight.account?.id ?? "";
3130
+ if (!group.members.has(memberKey)) group.members.set(memberKey, {
3131
+ label: flight.account ? `${plain(flight.account.name || flight.account.email)} <${plain(flight.account.email)}>` : "(deleted account)",
3132
+ flights: []
3133
+ });
3134
+ group.members.get(memberKey).flights.push(flight);
3135
+ }
3136
+ const lines = [];
3137
+ for (const group of [...organizations.values()].sort((a, b) => a.label.localeCompare(b.label))) {
3138
+ const count2 = [...group.members.values()].reduce((sum, member) => sum + member.flights.length, 0);
3139
+ if (platform) lines.push(`${group.label} \u2014 ${count2} flight${count2 === 1 ? "" : "s"}`);
3140
+ for (const member of [...group.members.values()].sort((a, b) => a.label.localeCompare(b.label))) {
3141
+ lines.push(`${platform ? " " : ""}${member.label} \u2014 ${member.flights.length} flight${member.flights.length === 1 ? "" : "s"}`);
3142
+ const headers = [
3143
+ "Flight ID",
3144
+ "Name",
3145
+ "Status",
3146
+ "Shape",
3147
+ "Provider",
3148
+ "Queued (UTC)",
3149
+ "Runtime",
3150
+ ...platform ? ["Provider cost", "Charge", "Reserved"] : ["Credits", "Reserved credits"]
3151
+ ];
3152
+ const rows = member.flights.slice().sort((a, b) => b.queued_at.localeCompare(a.queued_at)).map((flight) => [
3153
+ flight.flight_id,
3154
+ flight.name,
3155
+ flight.status,
3156
+ flight.compute_type === "cpu" ? `CPU x${flight.cpu_count} / ${flight.memory_mib} MiB` : `${flight.gpu_type ?? "GPU"} x${flight.gpu_count}`,
3157
+ flight.provider ?? "-",
3158
+ flight.queued_at,
3159
+ runtime(flight, now),
3160
+ ...platform ? [flight.cost_pence == null ? "-" : `${flight.cost_pence}p`, `${flight.charge_pence ?? 0}p`, `${flight.reserved_pence ?? 0}p`] : [String(flight.charge_credits ?? 0), String(flight.reserved_credits ?? 0)]
3161
+ ].map(plain));
3162
+ const widths = headers.map((header, index) => rows.reduce((width, row) => Math.max(width, row[index].length), header.length));
3163
+ const indent = platform ? " " : " ";
3164
+ for (const row of [headers, ...rows]) lines.push(indent + row.map((cell, index) => cell.padEnd(widths[index])).join(" ").trimEnd());
3165
+ lines.push("");
3166
+ }
3167
+ }
3168
+ return lines.join("\n").trimEnd();
3169
+ }
3170
+
3171
+ // src/commands/admin-flights.ts
3172
+ function adminFlightEndpoint(identity) {
3173
+ const endpoint2 = identity.platform_admin ? "/platform/flights" : identity.organization?.role === "org_admin" ? "/admin/flights" : null;
3174
+ if (!endpoint2) throw new CliError("Your account is not an organisation or platform administrator.");
3175
+ if (!identity.credential_scopes?.includes("admin:jobs:read")) {
3176
+ throw new CliError(ADMIN_SCOPE_MESSAGE);
3177
+ }
3178
+ return endpoint2;
3179
+ }
3180
+ function adminFlightWindow(options, now = /* @__PURE__ */ new Date()) {
3181
+ if (options.since !== void 0 && (options.from !== void 0 || options.to !== void 0)) {
3182
+ throw new CliError("Use either --since or --from with --to.");
3183
+ }
3184
+ if (options.from !== void 0 || options.to !== void 0) {
3185
+ if (!options.from || !options.to) throw new CliError("Supply both --from and --to.");
3186
+ const iso = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
3187
+ const from2 = new Date(options.from), to = new Date(options.to);
3188
+ if (!iso.test(options.from) || !iso.test(options.to) || !Number.isFinite(from2.getTime()) || !Number.isFinite(to.getTime()) || from2 > to) throw new CliError("Use ISO timestamps with --from before --to.");
3189
+ return { from: from2.toISOString(), to: to.toISOString() };
3190
+ }
3191
+ const duration = /^(\d+)([smhdw])$/.exec(options.since ?? "7d");
3192
+ const units = { s: 1e3, m: 6e4, h: 36e5, d: 864e5, w: 6048e5 };
3193
+ const milliseconds = duration ? Number(duration[1]) * units[duration[2]] : NaN;
3194
+ const from = new Date(now.getTime() - milliseconds);
3195
+ if (!Number.isSafeInteger(milliseconds) || milliseconds <= 0 || !Number.isFinite(from.getTime())) {
3196
+ throw new CliError("Use a positive duration for --since, e.g. 7d or 24h.");
3197
+ }
3198
+ return { from: from.toISOString(), to: now.toISOString() };
3199
+ }
3200
+ var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3201
+ var STATUSES = ["queued", "dispatching", "running", "finalizing", "completed", "failed", "cancelled"];
3202
+ async function adminFlights(options) {
3203
+ const window = adminFlightWindow(options);
3204
+ const limitText = options.limit ?? "100";
3205
+ const limit = Number(limitText);
3206
+ if (!/^\d+$/.test(limitText) || !Number.isSafeInteger(limit) || limit < 1) throw new CliError("--limit must be a positive integer.");
3207
+ if (options.status && !STATUSES.includes(options.status)) throw new CliError(`--status must be one of: ${STATUSES.join(", ")}.`);
3208
+ const me = await apiRequest("GET", "/auth/me");
3209
+ if (!me.ok) throw httpError("Unable to read account", me.status);
3210
+ const identity = await me.json();
3211
+ const endpoint2 = adminFlightEndpoint(identity);
3212
+ if (options.org && endpoint2 !== "/platform/flights") throw new CliError("--org is available only to platform administrators.");
3213
+ const query = new URLSearchParams(window);
3214
+ if (options.org) query.set(UUID.test(options.org) ? "organization_id" : "organization_slug", options.org);
3215
+ if (options.member) query.set(UUID.test(options.member) ? "account_id" : "account_email", options.member);
3216
+ if (options.status) query.set("status", options.status);
3217
+ const flights = [];
3218
+ const cursors = /* @__PURE__ */ new Set();
3219
+ while (flights.length < limit) {
3220
+ query.set("limit", String(Math.min(500, limit - flights.length)));
3221
+ const response = await apiRequest("GET", `${endpoint2}?${query}`);
3222
+ if (!response.ok) throw httpError("Unable to list administrator flights", response.status);
3223
+ const page = await response.json();
3224
+ flights.push(...page.flights.slice(0, limit - flights.length));
3225
+ if (!page.next_cursor) break;
3226
+ if (cursors.has(page.next_cursor) || !page.flights.length) throw new CliError("The flight list returned an invalid pagination cursor.");
3227
+ cursors.add(page.next_cursor);
3228
+ query.set("cursor", page.next_cursor);
3229
+ }
3230
+ console.log(options.json ? JSON.stringify(flights, null, 2) : formatAdminFlights(flights, identity.platform_admin));
3231
+ }
3232
+
1389
3233
  // src/index.ts
1390
3234
  var program = new Command();
1391
- program.name("flightdeck").description("FlightDeck CLI \u2014 submit GPU compute jobs").option("--debug", "include safe error type/code and CLI frame diagnostics (no raw stack or credentials)").version(JSON.parse(readFileSync4(new URL("../package.json", import.meta.url), "utf8")).version);
1392
- program.command("login").description("Log in through the FlightDeck portal").option("--api-url <url>", "FlightDeck API URL", DEFAULT_API_URL).option("--name <name>", "name for this CLI credential", "flightdeck-cli").action(login);
3235
+ program.name("flightdeck").description("FlightDeck CLI \u2014 submit GPU compute jobs").option("--debug", "include safe error type/code and CLI frame diagnostics (no raw stack or credentials)").version(JSON.parse(readFileSync5(new URL("../package.json", import.meta.url), "utf8")).version);
3236
+ program.command("login").description("Log in through the FlightDeck portal").option("--api-url <url>", "FlightDeck API URL", DEFAULT_API_URL).option("--name <name>", "name for this CLI credential (default: flightdeck-cli@<hostname>)").option("--admin", "authorise this workstation to list flights as an administrator").action(login);
1393
3237
  program.command("logout").description("Remove the local login and attempt server credential revocation").option("--local", "remove the local login without revoking the credential").action(logout);
1394
3238
  program.command("whoami").description("Show the current FlightDeck account").action(whoami);
1395
3239
  program.command("balance").description("Show the current credit balance").action(balance);
1396
- program.command("submit").description("Submit a job from local files").argument("<entrypoint>", 'command to run (e.g. "python train.py --epochs 50")').option("-n, --name <name>", "job name", "cli-job").option("-f, --files <glob...>", "files to include (default: current directory)").option("-r, --requirements <file>", "pip requirements file", "requirements.txt").option("--gpu <type>", "GPU type (e.g. A100, H100)").option("--gpu-count <n>", "number of GPUs", "1").option("--time <duration>", "max duration (e.g. 1h, 30m, 3600s)", "1h").option("--base-image <image>", "override base Docker image").option("--provider <id>", "pin the flight to one compute provider (default: operator's order)").option("-e, --env <vars...>", "environment variables (KEY=VALUE)").action(submit);
1397
- program.command("sbatch").description("Submit a Slurm batch script (#SBATCH directives, incl. job arrays)").argument("<script>", "path to the .slurm/.sh batch script").option("-f, --files <glob...>", "files to include (default: current directory)").option("-r, --requirements <file>", "pip requirements file to install before the script runs").option("--base-image <image>", "override base Docker image (e.g. to pin a CUDA version)").option("--provider <id>", "pin every task to one compute provider (default: operator's order)").option("-n, --name <name>", "override job name (default: #SBATCH --job-name)").option("-e, --env <vars...>", "extra environment variables (KEY=VALUE)").option("--dry-run", "parse and show what would be submitted, without submitting").action(async (script, options) => {
3240
+ program.command("admin").description("Administrator views").command("flights").description("List member flights. Organisations retain flights submitted before a member leaves.").option("--org <slug|id>", "organisation filter (platform administrators only)").option("--member <email|id>", "member filter").option("--status <status>", "flight status").option("--since <duration>", "look back, e.g. 7d or 24h (default: 7d)").option("--from <iso>", "start of the UTC time window").option("--to <iso>", "end of the UTC time window").option("--limit <n>", "maximum flights, following cursors", "100").option("--json", "print raw, ungrouped rows").action(adminFlights);
3241
+ program.command("submit").description("Submit a job from local files").argument("<entrypoint>", 'command to run (e.g. "python train.py --epochs 50")').option("-n, --name <name>", "job name", "cli-job").option("-f, --files <glob...>", "files to include (default: current directory)").option("-r, --requirements <file>", "pip requirements file", "requirements.txt").option("--gpu <type>", "GPU type (e.g. A100, H100)").option("--gpu-count <n>", "number of GPUs", "1").option("--time <duration>", "max duration (e.g. 1h, 30m, 3600s)", "1h").option("--base-image <image>", "runner image built FROM flightdeck-runner (see /docs/images)").option("--provider <id>", "pin the flight to one compute provider (default: operator's order)").option("-e, --env <vars...>", "environment variables (KEY=VALUE)").action(submit);
3242
+ program.command("sbatch").description("Submit a Slurm batch script (#SBATCH directives, incl. job arrays)").argument("<script>", "path to the .slurm/.sh batch script").option("-f, --files <glob...>", "files to include (default: current directory)").option("-r, --requirements <file>", "pip requirements file to install before the script runs").option("--base-image <image>", "runner image built FROM flightdeck-runner (see /docs/images)").option("--provider <id>", "pin every task to one compute provider (default: operator's order)").option("-n, --name <name>", "override job name (default: #SBATCH --job-name)").option("-e, --env <vars...>", "extra environment variables (KEY=VALUE)").option("--dry-run", "parse and show what would be submitted, without submitting").action(async (script, options) => {
1398
3243
  await sbatch(script, options);
1399
3244
  });
1400
3245
  program.command("slurm-test").description("Run the provider-neutral Slurm conformance workload").option("--timeout <seconds>", "maximum time to wait for all tasks", "1800").option("--poll-interval <seconds>", "status polling interval", "5").action(slurmTest);
1401
3246
  program.command("status").description("Check job status").argument("<flight_id>", "flight ID to check").action(status);
1402
3247
  program.command("logs").description("Stream flight logs").argument("<flight_id>").option("-f, --follow", "follow until complete").action(logs);
1403
3248
  program.command("cancel").description("Cancel a flight").argument("<flight_id>").action(cancel);
3249
+ program.command("atc").alias("tui").description("Live full-screen view of your flights, one flight, or an sbatch array (read-only)").argument("[flight_id|job_id]", "a flight, or an sbatch job ID submitted from this machine (default: recent flights)").option("--name <pattern>", "flights whose name matches, e.g. 'molsim*' (searches your last 200)").option("--demo", "run against a simulated 21-task array (no account needed)").action(atc);
1404
3250
  program.command("outputs").description("List flight outputs").argument("<flight_id>").action(listOutputs);
1405
3251
  program.command("download").description("Download all flight outputs").argument("<flight_id>").option("-o, --output-dir <dir>", "destination directory", ".").action(download);
1406
3252
  try {