@lesscap/baton-cli 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/baton.mjs +44 -91
  2. package/package.json +2 -2
package/dist/baton.mjs CHANGED
@@ -799,12 +799,6 @@ var parseWorkerHandle = (handle) => {
799
799
  const m = handle.trim().match(/^(?:[wW]-)?(\d+)$/);
800
800
  return m ? Number(m[1]) : null;
801
801
  };
802
- var parseIssueUrl = (url) => {
803
- const m = url.match(/^https:\/\/github\.com\/[^/]+\/[^/]+\/issues\/(\d+)/);
804
- if (!m)
805
- throw new Error(`expected a GitHub issue url (https://github.com/o/r/issues/N), got "${url}"`);
806
- return { source: "github", number: Number(m[1]), url: url.split(/[?#]/)[0] };
807
- };
808
802
  var splitCsv = (s) => s ? s.split(",").map((x) => x.trim()).filter(Boolean) : void 0;
809
803
 
810
804
  // src/commands/init.ts
@@ -980,7 +974,6 @@ var requirement = defineCommand({
980
974
  project: { type: "string", description: "project id (overrides .baton.json)" },
981
975
  desc: { type: "string", description: "description" },
982
976
  body: { type: "string", description: "requirement body (markdown)" },
983
- github: { type: "string", description: "link a GitHub issue url (light association)" },
984
977
  ...common
985
978
  },
986
979
  run: async ({ args }) => {
@@ -991,8 +984,7 @@ var requirement = defineCommand({
991
984
  projectId: resolveProjectId(args),
992
985
  title: args.title,
993
986
  description: args.desc,
994
- body: args.body,
995
- external: args.github ? parseIssueUrl(args.github) : void 0
987
+ body: args.body
996
988
  },
997
989
  Boolean(args.json)
998
990
  )
@@ -1019,41 +1011,6 @@ var requirement = defineCommand({
1019
1011
  console.log(await updateRequirement(c, id, patch2, Boolean(args.json)));
1020
1012
  }
1021
1013
  }),
1022
- link: defineCommand({
1023
- meta: { name: "link", description: "link a requirement to a GitHub issue url" },
1024
- args: {
1025
- code: { type: "positional", required: true, description: "requirement code (R-N)" },
1026
- // Named `issue` (not `url`) — `url` would collide with the common --url flag.
1027
- issue: { type: "positional", required: true, description: "GitHub issue url" },
1028
- project: { type: "string", description: "project id (overrides .baton.json)" },
1029
- ...common
1030
- },
1031
- run: async ({ args }) => {
1032
- const c = clientFor(args);
1033
- const id = await resolveByCode(c, resolveProjectId(args), args.code);
1034
- console.log(
1035
- await updateRequirement(
1036
- c,
1037
- id,
1038
- { external: parseIssueUrl(args.issue) },
1039
- Boolean(args.json)
1040
- )
1041
- );
1042
- }
1043
- }),
1044
- unlink: defineCommand({
1045
- meta: { name: "unlink", description: "clear a requirement\u2019s GitHub issue association" },
1046
- args: {
1047
- code: { type: "positional", required: true, description: "requirement code (R-N)" },
1048
- project: { type: "string", description: "project id (overrides .baton.json)" },
1049
- ...common
1050
- },
1051
- run: async ({ args }) => {
1052
- const c = clientFor(args);
1053
- const id = await resolveByCode(c, resolveProjectId(args), args.code);
1054
- console.log(await updateRequirement(c, id, { external: null }, Boolean(args.json)));
1055
- }
1056
- }),
1057
1014
  ls: defineCommand({
1058
1015
  meta: { name: "ls", description: "list requirements in a project" },
1059
1016
  args: {
@@ -2338,7 +2295,6 @@ var task = defineCommand({
2338
2295
  project: { type: "string", description: "project id (overrides .baton.json)" },
2339
2296
  body: { type: "string", description: "task body (markdown)" },
2340
2297
  deps: { type: "string", description: "comma-separated dependency task codes (T-N,T-N)" },
2341
- github: { type: "string", description: "link a GitHub issue url (light association)" },
2342
2298
  ...common
2343
2299
  },
2344
2300
  run: async ({ args }) => {
@@ -2356,8 +2312,7 @@ var task = defineCommand({
2356
2312
  requirementId,
2357
2313
  title: args.title,
2358
2314
  body: args.body,
2359
- dependsOn: dependsOn.length ? dependsOn : void 0,
2360
- external: args.github ? parseIssueUrl(args.github) : void 0
2315
+ dependsOn: dependsOn.length ? dependsOn : void 0
2361
2316
  },
2362
2317
  Boolean(args.json)
2363
2318
  )
@@ -2384,36 +2339,6 @@ var task = defineCommand({
2384
2339
  console.log(await updateTask(c, id, patch2, Boolean(args.json)));
2385
2340
  }
2386
2341
  }),
2387
- link: defineCommand({
2388
- meta: { name: "link", description: "link a task to a GitHub issue url" },
2389
- args: {
2390
- code: { type: "positional", required: true, description: "task code (T-N)" },
2391
- // Named `issue` (not `url`) — `url` would collide with the common --url flag.
2392
- issue: { type: "positional", required: true, description: "GitHub issue url" },
2393
- project: { type: "string", description: "project id (overrides .baton.json)" },
2394
- ...common
2395
- },
2396
- run: async ({ args }) => {
2397
- const c = clientFor(args);
2398
- const id = await resolveTaskByCode(c, resolveProjectId(args), args.code);
2399
- console.log(
2400
- await updateTask(c, id, { external: parseIssueUrl(args.issue) }, Boolean(args.json))
2401
- );
2402
- }
2403
- }),
2404
- unlink: defineCommand({
2405
- meta: { name: "unlink", description: "clear a task\u2019s GitHub issue association" },
2406
- args: {
2407
- code: { type: "positional", required: true, description: "task code (T-N)" },
2408
- project: { type: "string", description: "project id (overrides .baton.json)" },
2409
- ...common
2410
- },
2411
- run: async ({ args }) => {
2412
- const c = clientFor(args);
2413
- const id = await resolveTaskByCode(c, resolveProjectId(args), args.code);
2414
- console.log(await updateTask(c, id, { external: null }, Boolean(args.json)));
2415
- }
2416
- }),
2417
2342
  ls: defineCommand({
2418
2343
  meta: { name: "ls", description: "list tasks in a requirement" },
2419
2344
  args: {
@@ -2595,6 +2520,14 @@ import { dirname, isAbsolute as isAbsolute2, resolve } from "node:path";
2595
2520
  var createWorktree = (input) => {
2596
2521
  if (existsSync4(input.worktreePath))
2597
2522
  throw new Error(`worktree path already exists: ${input.worktreePath}`);
2523
+ const head = spawnSync2("git", ["-C", input.repo, "rev-parse", "--verify", "-q", "HEAD"], {
2524
+ stdio: "pipe",
2525
+ encoding: "utf8"
2526
+ });
2527
+ if (head.status !== 0)
2528
+ throw new Error(
2529
+ `repo has no commits (unborn HEAD) \u2014 create an initial commit before starting sessions`
2530
+ );
2598
2531
  mkdirSync(dirname(input.worktreePath), { recursive: true });
2599
2532
  const branch = `baton/${input.sessionCode}`;
2600
2533
  const r = spawnSync2(
@@ -2642,6 +2575,10 @@ var removeWorktree = (repo, worktreePath) => {
2642
2575
  }
2643
2576
  };
2644
2577
 
2578
+ // src/worker/watchdog.ts
2579
+ var stale = (okAt, now, thresholdMs) => now - okAt > thresholdMs;
2580
+ var streamWedged = (isOpen, okAt, now, thresholdMs) => !isOpen && stale(okAt, now, thresholdMs);
2581
+
2645
2582
  // src/worker/daemon.ts
2646
2583
  var binPath = () => {
2647
2584
  const here = dirname2(fileURLToPath(import.meta.url));
@@ -2655,11 +2592,20 @@ var runWorkerDaemon = async (cfg, client, signal) => {
2655
2592
  const log = (m) => console.log(`[worker #${cfg.workerId} ${cfg.name}] ${m}`);
2656
2593
  const HEARTBEAT_MS = 3e4;
2657
2594
  const HEARTBEAT_TIMEOUT_MS = 15e3;
2658
- const MAX_HEARTBEAT_FAILURES = 5;
2659
- let heartbeatFailures = 0;
2595
+ const HEARTBEAT_DEAD_MS = 15e4;
2596
+ const STREAM_STALE_MS = 9e4;
2597
+ const ES_OPEN = 1;
2598
+ let heartbeatOkAt = Date.now();
2599
+ let streamOkAt = Date.now();
2660
2600
  let fatal = false;
2661
2601
  let stop = () => {
2662
2602
  };
2603
+ const trip = (reason) => {
2604
+ if (fatal) return;
2605
+ log(`${reason} \u2014 exiting so the supervisor restarts a clean worker`);
2606
+ fatal = true;
2607
+ stop();
2608
+ };
2663
2609
  const ping = async () => {
2664
2610
  try {
2665
2611
  await Promise.race([
@@ -2668,20 +2614,11 @@ var runWorkerDaemon = async (cfg, client, signal) => {
2668
2614
  (_, reject) => setTimeout(() => reject(new Error("heartbeat timeout")), HEARTBEAT_TIMEOUT_MS)
2669
2615
  )
2670
2616
  ]);
2671
- heartbeatFailures = 0;
2617
+ heartbeatOkAt = Date.now();
2672
2618
  } catch (e) {
2673
- heartbeatFailures += 1;
2674
- log(`heartbeat failed (${heartbeatFailures}/${MAX_HEARTBEAT_FAILURES}): ${String(e)}`);
2675
- if (heartbeatFailures >= MAX_HEARTBEAT_FAILURES) {
2676
- const downSec = MAX_HEARTBEAT_FAILURES * HEARTBEAT_MS / 1e3;
2677
- log(`heartbeat down ~${downSec}s \u2014 exiting so the supervisor restarts a clean worker`);
2678
- fatal = true;
2679
- stop();
2680
- }
2619
+ log(`heartbeat failed: ${String(e)}`);
2681
2620
  }
2682
2621
  };
2683
- void ping();
2684
- const hb = setInterval(() => void ping(), HEARTBEAT_MS);
2685
2622
  const killChild = (child) => {
2686
2623
  if (child.pid === void 0) return;
2687
2624
  try {
@@ -2783,8 +2720,12 @@ var runWorkerDaemon = async (cfg, client, signal) => {
2783
2720
  }
2784
2721
  })
2785
2722
  });
2786
- es.onopen = () => void reconcile().catch((err) => log(`reconcile failed: ${String(err)}`));
2723
+ es.onopen = () => {
2724
+ streamOkAt = Date.now();
2725
+ void reconcile().catch((err) => log(`reconcile failed: ${String(err)}`));
2726
+ };
2787
2727
  es.onmessage = (e) => {
2728
+ streamOkAt = Date.now();
2788
2729
  try {
2789
2730
  const cmd = JSON.parse(e.data);
2790
2731
  if (cmd.cmd === "session.start")
@@ -2800,6 +2741,18 @@ var runWorkerDaemon = async (cfg, client, signal) => {
2800
2741
  };
2801
2742
  es.onerror = () => log("command stream error (eventsource will retry)");
2802
2743
  log(`listening for commands (server ${cfg.server}, repo ${repo})`);
2744
+ const watchdog = () => {
2745
+ const now = Date.now();
2746
+ if (stale(heartbeatOkAt, now, HEARTBEAT_DEAD_MS))
2747
+ trip(`heartbeat down ~${HEARTBEAT_DEAD_MS / 1e3}s`);
2748
+ else if (streamWedged(es.readyState === ES_OPEN, streamOkAt, now, STREAM_STALE_MS))
2749
+ trip(`command stream wedged ~${STREAM_STALE_MS / 1e3}s`);
2750
+ };
2751
+ void ping();
2752
+ const hb = setInterval(() => {
2753
+ void ping();
2754
+ watchdog();
2755
+ }, HEARTBEAT_MS);
2803
2756
  await new Promise((resolve2) => {
2804
2757
  stop = resolve2;
2805
2758
  if (signal.aborted) return resolve2();
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@lesscap/baton-cli",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "baton CLI — register/run a worker, manage projects/requirements/tasks/sessions",
7
7
  "bin": {
8
- "baton": "./dist/baton.mjs"
8
+ "baton": "dist/baton.mjs"
9
9
  },
10
10
  "files": [
11
11
  "dist"