@indigoai-us/hq-cli 5.345.230 → 5.345.232

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 (106) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +3 -2
  3. package/assets/lanes-reconcile/hq-lanes-reconcile.service +1 -1
  4. package/assets/scaffold/core/scripts/pipeline-conductor.sh +14 -14
  5. package/assets/scaffold/core/scripts/pipeline-driver.sh +7 -7
  6. package/assets/scaffold/core/scripts/session-project.sh +19 -4
  7. package/dist/command-catalog.generated.d.ts +213 -177
  8. package/dist/command-catalog.generated.js +263 -212
  9. package/dist/command-registration-plan.d.ts +6 -24
  10. package/dist/command-registration-plan.js +4 -8
  11. package/dist/commands/auth.js +3 -2
  12. package/dist/commands/core-pipeline-conductor-command.js +1 -1
  13. package/dist/commands/core-pipeline-conductor-native.js +5 -5
  14. package/dist/commands/core-pipeline-driver-native.js +5 -5
  15. package/dist/commands/crm.js +2 -1
  16. package/dist/commands/daemon.js +7 -36
  17. package/dist/commands/deploy-access.js +6 -13
  18. package/dist/commands/deploy-comments.js +5 -13
  19. package/dist/commands/deploy-domains.js +6 -13
  20. package/dist/commands/deploy-list.js +25 -11
  21. package/dist/commands/deploy-mutations.d.ts +3 -0
  22. package/dist/commands/deploy-mutations.js +147 -0
  23. package/dist/commands/link.js +29 -0
  24. package/dist/commands/mesh.js +3 -1
  25. package/dist/commands/packs.js +3 -2
  26. package/dist/commands/pkg-remove.js +2 -1
  27. package/dist/commands/report.js +12 -9
  28. package/dist/data/command-states.json +10 -7
  29. package/dist/lib/bot/fleet-bots-update-pass.d.ts +8 -0
  30. package/dist/lib/bot/fleet-bots-update-pass.js +40 -0
  31. package/dist/lib/bot/fleet-bots-update-task.d.ts +12 -0
  32. package/dist/lib/bot/fleet-bots-update-task.js +52 -0
  33. package/dist/lib/bot/fleet-bots-update-worker-v1.d.ts +2 -0
  34. package/dist/lib/bot/fleet-bots-update-worker-v1.js +5 -0
  35. package/dist/lib/daemon/catalog.d.ts +8 -5
  36. package/dist/lib/daemon/catalog.js +15 -8
  37. package/dist/lib/daemon/hook-requests-codex-settings.d.ts +26 -0
  38. package/dist/lib/daemon/hook-requests-codex-settings.js +185 -0
  39. package/dist/lib/daemon/hook-requests-conduct-link.d.ts +1 -0
  40. package/dist/lib/daemon/hook-requests-conduct-link.js +21 -4
  41. package/dist/lib/daemon/hook-requests.d.ts +3 -2
  42. package/dist/lib/daemon/hook-requests.js +16 -3
  43. package/dist/lib/daemon/lock.js +33 -37
  44. package/dist/lib/daemon/ops/skill-command.d.ts +99 -0
  45. package/dist/lib/daemon/ops/skill-command.js +260 -0
  46. package/dist/lib/daemon/protocol.d.ts +1 -1
  47. package/dist/lib/daemon/protocol.js +1 -0
  48. package/dist/lib/daemon/run.js +10 -18
  49. package/dist/lib/daemon/server.js +7 -0
  50. package/dist/lib/daemon/supervisor.d.ts +5 -0
  51. package/dist/lib/daemon/supervisor.js +17 -2
  52. package/dist/lib/db/app-ref.js +10 -15
  53. package/dist/lib/deploy-apps.d.ts +29 -0
  54. package/dist/lib/deploy-apps.js +37 -0
  55. package/dist/lib/doctor/checks/search-index.d.ts +16 -0
  56. package/dist/lib/doctor/checks/search-index.js +61 -0
  57. package/dist/lib/doctor/checks/stranded-lanes.js +1 -1
  58. package/dist/lib/doctor/registry.js +4 -0
  59. package/dist/lib/flag-registry.d.ts +7 -0
  60. package/dist/lib/flag-registry.js +20 -0
  61. package/dist/lib/hook-engine/coverage.d.ts +18 -0
  62. package/dist/lib/hook-engine/coverage.js +42 -1
  63. package/dist/lib/lanes/accounts.js +3 -3
  64. package/dist/lib/lanes/admission-host.js +1 -1
  65. package/dist/lib/lanes/admission.js +4 -4
  66. package/dist/lib/lanes/autopilot.js +2 -2
  67. package/dist/lib/lanes/blocker-freshness.js +1 -1
  68. package/dist/lib/lanes/compaction.js +5 -5
  69. package/dist/lib/lanes/delivery.js +1 -1
  70. package/dist/lib/lanes/depth-cap.js +1 -1
  71. package/dist/lib/lanes/disk-guard.js +1 -1
  72. package/dist/lib/lanes/dropbox.js +1 -1
  73. package/dist/lib/lanes/hooks-install.js +1 -1
  74. package/dist/lib/lanes/imported-question.js +1 -1
  75. package/dist/lib/lanes/keep-alive.d.ts +2 -2
  76. package/dist/lib/lanes/keep-alive.js +3 -3
  77. package/dist/lib/lanes/kr-freshness.js +1 -1
  78. package/dist/lib/lanes/links.js +92 -11
  79. package/dist/lib/lanes/loop.js +2 -2
  80. package/dist/lib/lanes/machine-identity.js +3 -3
  81. package/dist/lib/lanes/models.js +6 -6
  82. package/dist/lib/lanes/quota.js +1 -1
  83. package/dist/lib/lanes/reconcile-install.js +3 -3
  84. package/dist/lib/lanes/reconcile.js +16 -16
  85. package/dist/lib/lanes/remote-control-lane.js +6 -6
  86. package/dist/lib/lanes/reviews.js +2 -2
  87. package/dist/lib/lanes/spawn.js +24 -24
  88. package/dist/lib/lanes/status-fields.js +4 -4
  89. package/dist/lib/lanes/wait.js +4 -4
  90. package/dist/lib/lanes/watch.js +2 -2
  91. package/dist/lib/lanes/watcher-main.js +1 -1
  92. package/dist/lib/lanes/watcher.js +38 -38
  93. package/dist/lib/lanes/worker-process.js +5 -5
  94. package/dist/lib/mesh/live/daemon/pid-lock.d.ts +2 -4
  95. package/dist/lib/mesh/live/daemon/pid-lock.js +38 -55
  96. package/dist/utils/exclusive-lock.d.ts +60 -0
  97. package/dist/utils/exclusive-lock.js +65 -0
  98. package/dist/utils/exit-after-worker.d.ts +7 -0
  99. package/dist/utils/exit-after-worker.js +15 -0
  100. package/package.json +8 -7
  101. package/dist/commands/staff/listings.d.ts +0 -41
  102. package/dist/commands/staff/listings.js +0 -160
  103. package/dist/commands/staff/reset-test-account.d.ts +0 -121
  104. package/dist/commands/staff/reset-test-account.js +0 -285
  105. package/dist/commands/staff/test-account.d.ts +0 -51
  106. package/dist/commands/staff/test-account.js +0 -196
@@ -333,7 +333,7 @@ function maybeHeartbeatLane(hqRoot, laneId, nowMs) {
333
333
  });
334
334
  }
335
335
  catch (err) {
336
- process.stderr.write(`hq lanes watcher: heartbeat failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
336
+ process.stderr.write(`hq runners watcher: heartbeat failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
337
337
  }
338
338
  }
339
339
  export function stalledReason(silenceMs) {
@@ -387,7 +387,7 @@ function recordLaneRefusal(hqRoot, lane, classification, quota) {
387
387
  });
388
388
  }
389
389
  catch (err) {
390
- process.stderr.write(`hq lanes watcher: refusal not recorded for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
390
+ process.stderr.write(`hq runners watcher: refusal not recorded for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
391
391
  }
392
392
  }
393
393
  export function applyTerminalToLane(hqRoot, laneId, classification, opts = {}) {
@@ -410,7 +410,7 @@ function applyTerminalToLaneWithResult(hqRoot, laneId, classification, opts = {}
410
410
  questionRefusalCode = questionRecordRefusalCode(err);
411
411
  if (questionRefusalCode === null)
412
412
  throw err;
413
- process.stderr.write(`hq lanes watcher: terminal ask refused for ${laneId}: ${questionRefusalCode}\n`);
413
+ process.stderr.write(`hq runners watcher: terminal ask refused for ${laneId}: ${questionRefusalCode}\n`);
414
414
  }
415
415
  }
416
416
  const terminalClassification = questionRefusalCode === null
@@ -608,7 +608,7 @@ function applyTerminalToLaneWithResult(hqRoot, laneId, classification, opts = {}
608
608
  emitExecutiveLaneFinished(hqRoot, updated);
609
609
  }
610
610
  catch (error) {
611
- process.stderr.write(`hq lanes watcher: source report event failed: ${error instanceof Error ? error.message : String(error)}\n`);
611
+ process.stderr.write(`hq runners watcher: source report event failed: ${error instanceof Error ? error.message : String(error)}\n`);
612
612
  }
613
613
  notifySlotFreed({
614
614
  hqRoot,
@@ -635,7 +635,7 @@ export function isPidAlive(pid) {
635
635
  return false;
636
636
  if (code === "EPERM")
637
637
  return true;
638
- process.stderr.write(`hq lanes watcher: liveness probe for pid ${pid} failed: ${err instanceof Error ? err.message : String(err)}\n`);
638
+ process.stderr.write(`hq runners watcher: liveness probe for pid ${pid} failed: ${err instanceof Error ? err.message : String(err)}\n`);
639
639
  throw err;
640
640
  }
641
641
  }
@@ -647,7 +647,7 @@ function fileMtimeMs(filePath) {
647
647
  const code = err.code;
648
648
  if (code === "ENOENT")
649
649
  return null;
650
- process.stderr.write(`hq lanes watcher: failed to stat ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
650
+ process.stderr.write(`hq runners watcher: failed to stat ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
651
651
  throw err;
652
652
  }
653
653
  }
@@ -659,7 +659,7 @@ function fileNonEmpty(filePath) {
659
659
  const code = err.code;
660
660
  if (code === "ENOENT")
661
661
  return false;
662
- process.stderr.write(`hq lanes watcher: failed to stat ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
662
+ process.stderr.write(`hq runners watcher: failed to stat ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
663
663
  throw err;
664
664
  }
665
665
  }
@@ -747,7 +747,7 @@ export function readLatestEnvelopeRecord(hqRoot, laneId, minName) {
747
747
  if (latest === null)
748
748
  return null;
749
749
  if (latest.envelope === null) {
750
- process.stderr.write(`hq lanes watcher: envelope ${latest.path} is not a valid hq-lane-envelope.v1: ${latest.error}\n`);
750
+ process.stderr.write(`hq runners watcher: envelope ${latest.path} is not a valid hq-lane-envelope.v1: ${latest.error}\n`);
751
751
  return null;
752
752
  }
753
753
  return { envelope: latest.envelope, ref: latest.ref };
@@ -772,7 +772,7 @@ export function readLatestEnvelopeFile(hqRoot, laneId, minName) {
772
772
  const code = err.code;
773
773
  if (code === "ENOENT")
774
774
  return null;
775
- process.stderr.write(`hq lanes watcher: failed to list envelopes for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
775
+ process.stderr.write(`hq runners watcher: failed to list envelopes for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
776
776
  throw err;
777
777
  }
778
778
  if (names.length === 0)
@@ -784,7 +784,7 @@ export function readLatestEnvelopeFile(hqRoot, laneId, minName) {
784
784
  text = fs.readFileSync(filePath, "utf8");
785
785
  }
786
786
  catch (err) {
787
- process.stderr.write(`hq lanes watcher: failed to read envelope ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
787
+ process.stderr.write(`hq runners watcher: failed to read envelope ${filePath}: ${err instanceof Error ? err.message : String(err)}\n`);
788
788
  throw err;
789
789
  }
790
790
  const ref = path.posix.join("envelopes", latest);
@@ -858,7 +858,7 @@ async function settleManagerStatusGate(hqRoot, lane, manifestTurnId, at, deps, l
858
858
  target = resolveManagerStatusTarget(hqRoot, lane);
859
859
  }
860
860
  catch (err) {
861
- process.stderr.write(`hq lanes watcher: manager status mapping failed for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
861
+ process.stderr.write(`hq runners watcher: manager status mapping failed for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
862
862
  logDecision({ event: "status_gate_mapping_failed", at, error: err instanceof Error ? err.message : String(err) });
863
863
  return lane;
864
864
  }
@@ -875,7 +875,7 @@ async function settleManagerStatusGate(hqRoot, lane, manifestTurnId, at, deps, l
875
875
  written = managerStatusWasWritten(gate, readStatusSnapshot(gate.status_path));
876
876
  }
877
877
  catch (err) {
878
- process.stderr.write(`hq lanes watcher: manager status read failed for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
878
+ process.stderr.write(`hq runners watcher: manager status read failed for ${lane.lane_id}: ${err instanceof Error ? err.message : String(err)}\n`);
879
879
  logDecision({ event: "status_gate_status_read_failed", at, error: err instanceof Error ? err.message : String(err) });
880
880
  written = false;
881
881
  }
@@ -918,7 +918,7 @@ async function settleBlockerFreshness(hqRoot, lane, manifestTurnId, at, deps, me
918
918
  return;
919
919
  errorLogged = true;
920
920
  const code = error instanceof LanesError ? error.error : "blocker_freshness_unavailable";
921
- process.stderr.write(`hq lanes watcher: blocker freshness disabled for ${lane.lane_id}: ${code}\n`);
921
+ process.stderr.write(`hq runners watcher: blocker freshness disabled for ${lane.lane_id}: ${code}\n`);
922
922
  logDecision({ event: "blocker_freshness_disabled", at, error: code });
923
923
  },
924
924
  });
@@ -1090,7 +1090,7 @@ async function settleBlockerFreshness(hqRoot, lane, manifestTurnId, at, deps, me
1090
1090
  }
1091
1091
  catch (error) {
1092
1092
  const message = error instanceof Error ? error.message : String(error);
1093
- process.stderr.write(`hq lanes watcher: blocker freshness fallback delivery failed for ${lane.lane_id}: ${message}\n`);
1093
+ process.stderr.write(`hq runners watcher: blocker freshness fallback delivery failed for ${lane.lane_id}: ${message}\n`);
1094
1094
  logDecision({ event: "blocker_stale_escalation_fallback_failed", at, turn_id: turnId, fingerprint, error: message });
1095
1095
  updated = updateLaneFollowup(hqRoot, lane.lane_id, deps, (current) => {
1096
1096
  const state = current.blocker_freshness_state;
@@ -1157,7 +1157,7 @@ async function settleBlockerFreshness(hqRoot, lane, manifestTurnId, at, deps, me
1157
1157
  }
1158
1158
  catch (error) {
1159
1159
  const message = error instanceof Error ? error.message : String(error);
1160
- process.stderr.write(`hq lanes watcher: blocker freshness escalation delivery failed for ${lane.lane_id}: ${message}\n`);
1160
+ process.stderr.write(`hq runners watcher: blocker freshness escalation delivery failed for ${lane.lane_id}: ${message}\n`);
1161
1161
  logDecision({ event: "blocker_stale_escalation_delivery_failed", at, turn_id: turnId, error: message });
1162
1162
  updated = updateLaneFollowup(hqRoot, lane.lane_id, deps, (current) => {
1163
1163
  const state = current.blocker_freshness_state;
@@ -1196,7 +1196,7 @@ async function settleKrFreshness(hqRoot, lane, manifestTurnId, at, deps, logDeci
1196
1196
  return;
1197
1197
  errorLogged = true;
1198
1198
  const code = error instanceof LanesError ? error.error : "kr_freshness_unavailable";
1199
- process.stderr.write(`hq lanes watcher: KR freshness disabled for ${lane.lane_id}: ${code}\n`);
1199
+ process.stderr.write(`hq runners watcher: KR freshness disabled for ${lane.lane_id}: ${code}\n`);
1200
1200
  logDecision({ event: "kr_freshness_disabled", at, error: code });
1201
1201
  },
1202
1202
  });
@@ -1266,7 +1266,7 @@ async function deliverQuotaNotice(hqRoot, lane, quota, at, deps, logDecision) {
1266
1266
  }
1267
1267
  catch (err) {
1268
1268
  const message = err instanceof Error ? err.message : String(err);
1269
- process.stderr.write(`hq lanes watcher: quota notice failed for ${lane.lane_id}: ${message}\n`);
1269
+ process.stderr.write(`hq runners watcher: quota notice failed for ${lane.lane_id}: ${message}\n`);
1270
1270
  logDecision({ event: "quota_notice_failed", at, error: message });
1271
1271
  }
1272
1272
  }
@@ -1314,7 +1314,7 @@ export async function emitMeshSoft(event, logDecision, at, fn, timeoutMs = 10_00
1314
1314
  catch (err) {
1315
1315
  const message = err instanceof Error ? err.message : String(err);
1316
1316
  logDecision({ event: "mesh_event_failed", at, mesh_event: event, error: message });
1317
- process.stderr.write(`hq lanes watcher: mesh_event_failed ${event}: ${message}\n`);
1317
+ process.stderr.write(`hq runners watcher: mesh_event_failed ${event}: ${message}\n`);
1318
1318
  return false;
1319
1319
  }
1320
1320
  finally {
@@ -1329,7 +1329,7 @@ function readProgressIntervalMs(hqRoot, lane) {
1329
1329
  return providerProgressEventMinIntervalMs(config.providers[lane.provider]);
1330
1330
  }
1331
1331
  catch (err) {
1332
- process.stderr.write(`hq lanes watcher: lanes config unreadable, using default progress interval: ${err instanceof Error ? err.message : String(err)}\n`);
1332
+ process.stderr.write(`hq runners watcher: lanes config unreadable, using default progress interval: ${err instanceof Error ? err.message : String(err)}\n`);
1333
1333
  }
1334
1334
  return providerProgressEventMinIntervalMs({ max_running: 1, max_queued: 1, accounts: {} });
1335
1335
  }
@@ -1338,7 +1338,7 @@ function readLaneSoft(hqRoot, laneId) {
1338
1338
  return readLane(hqRoot, laneId);
1339
1339
  }
1340
1340
  catch (err) {
1341
- process.stderr.write(`hq lanes watcher: lane read failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1341
+ process.stderr.write(`hq runners watcher: lane read failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1342
1342
  return null;
1343
1343
  }
1344
1344
  }
@@ -1554,7 +1554,7 @@ export async function runWatcherLoop(hqRoot, laneId, deps = {}) {
1554
1554
  catch {
1555
1555
  // Logging is best effort; stderr below still records the loss.
1556
1556
  }
1557
- process.stderr.write(`hq lanes watcher: claim lost for ${laneId} to pid ${claimantPid ?? "unknown"}\n`);
1557
+ process.stderr.write(`hq runners watcher: claim lost for ${laneId} to pid ${claimantPid ?? "unknown"}\n`);
1558
1558
  return;
1559
1559
  }
1560
1560
  }
@@ -1624,7 +1624,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1624
1624
  err instanceof LanesError && err.error === "story_not_on_board") {
1625
1625
  storyStatusSuppressions.add(laneStoryStatusSuppressionKey(company, initialLane.project_id, initialLane.story_id));
1626
1626
  }
1627
- process.stderr.write(`hq lanes watcher: mesh thread ensure failed for ${laneId}: ${message}\n`);
1627
+ process.stderr.write(`hq runners watcher: mesh thread ensure failed for ${laneId}: ${message}\n`);
1628
1628
  writeWatcherDecision(outPath, {
1629
1629
  event: "mesh_event_failed",
1630
1630
  at: new Date(now()).toISOString(),
@@ -1745,7 +1745,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1745
1745
  providerRejection = routed.rejection;
1746
1746
  }
1747
1747
  catch (err) {
1748
- process.stderr.write(`hq lanes watcher: quota read failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1748
+ process.stderr.write(`hq runners watcher: quota read failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1749
1749
  }
1750
1750
  }
1751
1751
  // Spec §4: the process exited and its envelope file does not parse, or
@@ -1795,7 +1795,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1795
1795
  }
1796
1796
  catch (err) {
1797
1797
  const message = err instanceof Error ? err.message : String(err);
1798
- process.stderr.write(`hq lanes watcher: invalid ask fallback could not parse ${repairCandidate.path} for ${laneId}: ${message}\n`);
1798
+ process.stderr.write(`hq runners watcher: invalid ask fallback could not parse ${repairCandidate.path} for ${laneId}: ${message}\n`);
1799
1799
  logDecision({
1800
1800
  event: "envelope_repair",
1801
1801
  at: new Date(now()).toISOString(),
@@ -1812,7 +1812,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1812
1812
  latest = { envelope: recovery.envelope, ref: repairCandidate.ref };
1813
1813
  envelope = recovery.envelope;
1814
1814
  recoveredAsk = true;
1815
- const message = `hq lanes watcher: routing invalid ask envelope for ${laneId} to awaiting_input ` +
1815
+ const message = `hq runners watcher: routing invalid ask envelope for ${laneId} to awaiting_input ` +
1816
1816
  `with ${recovery.envelope.questions?.length ?? 0} validated question(s); errors: ${errors.join("; ")}`;
1817
1817
  process.stderr.write(`${message}\n`);
1818
1818
  logDecision({
@@ -1830,7 +1830,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1830
1830
  : [...new Set([...repair.errors, ...recovery.questionErrors])];
1831
1831
  invalidEnvelope = { errors };
1832
1832
  if (recovery !== null) {
1833
- const message = `hq lanes watcher: invalid ask envelope for ${laneId} remains failed; ` +
1833
+ const message = `hq runners watcher: invalid ask envelope for ${laneId} remains failed; ` +
1834
1834
  `0 of ${recovery.sourceQuestionCount} questions passed validation`;
1835
1835
  process.stderr.write(`${message}\n`);
1836
1836
  logDecision({
@@ -1893,7 +1893,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1893
1893
  markLaneStalled(hqRoot, laneId, tick.emit.at, reason);
1894
1894
  }
1895
1895
  catch (err) {
1896
- process.stderr.write(`hq lanes watcher: stalled write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1896
+ process.stderr.write(`hq runners watcher: stalled write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1897
1897
  }
1898
1898
  }
1899
1899
  if (tick.emit.kind === "progress") {
@@ -1902,7 +1902,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1902
1902
  clearLaneStalled(hqRoot, laneId);
1903
1903
  }
1904
1904
  catch (err) {
1905
- process.stderr.write(`hq lanes watcher: stalled clear failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1905
+ process.stderr.write(`hq runners watcher: stalled clear failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1906
1906
  }
1907
1907
  // Spec §8 progress row. Only a tick that saw new tool activity or
1908
1908
  // new output gets here (tickWatcher emits progress on `progressed`
@@ -1931,7 +1931,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1931
1931
  }));
1932
1932
  }
1933
1933
  catch (err) {
1934
- process.stderr.write(`hq lanes watcher: progress timestamp write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1934
+ process.stderr.write(`hq runners watcher: progress timestamp write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1935
1935
  }
1936
1936
  }
1937
1937
  }
@@ -1963,7 +1963,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1963
1963
  recordAccountQuota(hqRoot, quotaLane.provider, quotaLane.account, quota, new Date(now()));
1964
1964
  }
1965
1965
  catch (err) {
1966
- process.stderr.write(`hq lanes watcher: quota record failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1966
+ process.stderr.write(`hq runners watcher: quota record failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
1967
1967
  }
1968
1968
  }
1969
1969
  // A usage limit fails only this lane: no automatic move to another
@@ -1986,7 +1986,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
1986
1986
  }
1987
1987
  catch (err) {
1988
1988
  const message = err instanceof Error ? err.message : String(err);
1989
- process.stderr.write(`hq lanes watcher: account failover failed for ${laneId}: ${message}\n`);
1989
+ process.stderr.write(`hq runners watcher: account failover failed for ${laneId}: ${message}\n`);
1990
1990
  logDecision({ event: "account_failover_failed", at: tick.emit.at, reason: accountRetryReason, error: message });
1991
1991
  if (readLane(hqRoot, laneId).state !== "running")
1992
1992
  return;
@@ -2083,7 +2083,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
2083
2083
  }
2084
2084
  catch (err) {
2085
2085
  const message = err instanceof Error ? err.message : String(err);
2086
- process.stderr.write(`hq lanes watcher: queued message recovery failed for ${laneId}: ${message}\n`);
2086
+ process.stderr.write(`hq runners watcher: queued message recovery failed for ${laneId}: ${message}\n`);
2087
2087
  logDecision({
2088
2088
  event: "message_recovery_failed",
2089
2089
  at: tick.emit.at,
@@ -2134,7 +2134,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
2134
2134
  }
2135
2135
  catch (err) {
2136
2136
  const message = err instanceof Error ? err.message : String(err);
2137
- process.stderr.write(`hq lanes watcher: terminal keep-alive trigger failed for ${laneId}: ${message}\n`);
2137
+ process.stderr.write(`hq runners watcher: terminal keep-alive trigger failed for ${laneId}: ${message}\n`);
2138
2138
  logDecision({ event: "keep_alive_terminal_trigger_failed", at: tick.emit.at, error: message });
2139
2139
  }
2140
2140
  }
@@ -2152,7 +2152,7 @@ async function runWatcherLoopOwned(hqRoot, laneId, deps, releaseClaim = () => {
2152
2152
  }
2153
2153
  catch (err) {
2154
2154
  const message = err instanceof Error ? err.message : String(err);
2155
- process.stderr.write(`hq lanes watcher: queue admission failed after ${laneId}: ${message}\n`);
2155
+ process.stderr.write(`hq runners watcher: queue admission failed after ${laneId}: ${message}\n`);
2156
2156
  logDecision({ event: "queue_admission_failed", at: tick.emit.at, error: message });
2157
2157
  }
2158
2158
  }
@@ -2206,7 +2206,7 @@ function recoverRawEnvelopeArtifacts(rawText) {
2206
2206
  return Array.isArray(parsed) ? parsed : undefined;
2207
2207
  }
2208
2208
  catch {
2209
- process.stderr.write("hq lanes watcher: could not recover artifacts from the invalid envelope\n");
2209
+ process.stderr.write("hq runners watcher: could not recover artifacts from the invalid envelope\n");
2210
2210
  return undefined;
2211
2211
  }
2212
2212
  }
@@ -2247,7 +2247,7 @@ export function decideEnvelopeRepair(hqRoot, laneId, latest, nowMs, spawnRepair,
2247
2247
  }));
2248
2248
  }
2249
2249
  catch (err) {
2250
- process.stderr.write(`hq lanes watcher: envelope_repair write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
2250
+ process.stderr.write(`hq runners watcher: envelope_repair write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
2251
2251
  }
2252
2252
  logDecision({ event: "envelope_repair", at, outcome: "second_failure", errors });
2253
2253
  return { action: "give_up", errors };
@@ -2300,7 +2300,7 @@ export function decideEnvelopeRepair(hqRoot, laneId, latest, nowMs, spawnRepair,
2300
2300
  }
2301
2301
  catch (err) {
2302
2302
  const message = err instanceof Error ? err.message : String(err);
2303
- process.stderr.write(`hq lanes watcher: envelope repair spawn failed for ${laneId}: ${message}\n`);
2303
+ process.stderr.write(`hq runners watcher: envelope repair spawn failed for ${laneId}: ${message}\n`);
2304
2304
  writeRepairRecord(hqRoot, laneId, {
2305
2305
  ...base,
2306
2306
  mode,
@@ -2327,7 +2327,7 @@ function markRepairSucceeded(hqRoot, laneId, _envelopeRef) {
2327
2327
  });
2328
2328
  }
2329
2329
  catch (err) {
2330
- process.stderr.write(`hq lanes watcher: envelope_repair success write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
2330
+ process.stderr.write(`hq runners watcher: envelope_repair success write failed for ${laneId}: ${err instanceof Error ? err.message : String(err)}\n`);
2331
2331
  }
2332
2332
  }
2333
2333
  /** Exported so tests can prove the watcher reads the live record, not spawn env. */
@@ -35,7 +35,7 @@ async function boundedUsage(sampler, endedAt, waitMs) {
35
35
  sampler.stop(endedAt),
36
36
  new Promise((resolve) => {
37
37
  timer = setTimeout(() => {
38
- process.stderr.write("hq lanes: worker resource use was not ready in time; the exit record has none\n");
38
+ process.stderr.write("hq runners: worker resource use was not ready in time; the exit record has none\n");
39
39
  resolve(null);
40
40
  }, waitMs);
41
41
  // Not unref'd: this timer is what keeps the wrapper alive until the
@@ -44,7 +44,7 @@ async function boundedUsage(sampler, endedAt, waitMs) {
44
44
  ]);
45
45
  }
46
46
  catch (error) {
47
- process.stderr.write(`hq lanes: could not measure worker resource use: ${error instanceof Error ? error.message : String(error)}\n`);
47
+ process.stderr.write(`hq runners: could not measure worker resource use: ${error instanceof Error ? error.message : String(error)}\n`);
48
48
  return null;
49
49
  }
50
50
  finally {
@@ -79,7 +79,7 @@ export function runLaneWorkerProcess(command, args, env = process.env, cwd = pro
79
79
  recordExit(exitPath, code, signal, endedAt, usage);
80
80
  }
81
81
  catch (error) {
82
- process.stderr.write(`hq lanes: could not persist worker exit status: ${error instanceof Error ? error.message : String(error)}\n`);
82
+ process.stderr.write(`hq runners: could not persist worker exit status: ${error instanceof Error ? error.message : String(error)}\n`);
83
83
  }
84
84
  }
85
85
  return exitCodeFor(code, signal);
@@ -110,7 +110,7 @@ export function runLaneWorkerProcess(command, args, env = process.env, cwd = pro
110
110
  });
111
111
  };
112
112
  child.once("error", (error) => {
113
- process.stderr.write(`hq lanes: worker process could not start: ${error.message}\n`);
113
+ process.stderr.write(`hq runners: worker process could not start: ${error.message}\n`);
114
114
  end(127, null);
115
115
  });
116
116
  child.once("exit", (code, signal) => end(code, signal));
@@ -119,7 +119,7 @@ export function runLaneWorkerProcess(command, args, env = process.env, cwd = pro
119
119
  if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
120
120
  const [command, ...args] = process.argv.slice(2);
121
121
  if (!command) {
122
- process.stderr.write("hq lanes: worker process command is missing\n");
122
+ process.stderr.write("hq runners: worker process command is missing\n");
123
123
  process.exitCode = 2;
124
124
  }
125
125
  else {
@@ -9,10 +9,8 @@
9
9
  * directory with a temp file inside (rmdir can never clear that class).
10
10
  */
11
11
  import * as fs from "node:fs";
12
- export interface PidLockRecord {
13
- pid: number;
14
- ts: number;
15
- }
12
+ import { type PidLockRecord } from "../../../../utils/exclusive-lock.js";
13
+ export type { PidLockRecord };
16
14
  export type PidLockResult = {
17
15
  ok: true;
18
16
  path: string;
@@ -9,6 +9,7 @@
9
9
  * directory with a temp file inside (rmdir can never clear that class).
10
10
  */
11
11
  import * as fs from "node:fs";
12
+ import { createLockFile, lockIsStale, parsePidLockRecord, readLockFile, releaseLockFile, } from "../../../../utils/exclusive-lock.js";
12
13
  import { daemonDir, daemonPidPath } from "./paths.js";
13
14
  function defaultAlive(pid) {
14
15
  try {
@@ -20,29 +21,10 @@ function defaultAlive(pid) {
20
21
  }
21
22
  }
22
23
  export function parsePidLock(content) {
23
- const fields = {};
24
- for (const line of content.split("\n")) {
25
- const i = line.indexOf("=");
26
- if (i === -1)
27
- continue;
28
- fields[line.slice(0, i)] = line.slice(i + 1);
29
- }
30
- if (!/^\d+$/.test(fields.pid ?? "") || !/^\d+$/.test(fields.ts ?? "")) {
31
- return null;
32
- }
33
- return { pid: Number(fields.pid), ts: Number(fields.ts) };
24
+ return parsePidLockRecord(content);
34
25
  }
35
26
  export function readPidLock(lockPath, deps = {}) {
36
- const existsSync = deps.existsSync ?? fs.existsSync;
37
- const readFileSync = deps.readFileSync ?? fs.readFileSync;
38
- if (!existsSync(lockPath))
39
- return null;
40
- try {
41
- return parsePidLock(readFileSync(lockPath, "utf8"));
42
- }
43
- catch {
44
- return null;
45
- }
27
+ return readLockFile(lockPath, parsePidLockRecord, deps);
46
28
  }
47
29
  export function pidLockStatus(lockPath, deps) {
48
30
  const record = readPidLock(lockPath, deps);
@@ -50,7 +32,11 @@ export function pidLockStatus(lockPath, deps) {
50
32
  return "free";
51
33
  return deps.isProcessAlive(record.pid) ? "held" : "stale";
52
34
  }
53
- /** Bounded stale-takeover retries (exclusive create after unlink). */
35
+ /**
36
+ * Bounded stale-takeover retries (exclusive create after unlink).
37
+ * Stale policy: pid only. A lock whose owner is unreadable or not alive is
38
+ * reclaimed at once; a live owner keeps it at any age.
39
+ */
54
40
  const STALE_TAKEOVER_ATTEMPTS = 3;
55
41
  /**
56
42
  * Acquire the single-file pid lock via exclusive create (`wx`).
@@ -67,9 +53,9 @@ export function acquirePidLock(dir, deps) {
67
53
  mkdirSync(dir, { recursive: true, mode: 0o700 });
68
54
  const lockPath = daemonPidPath(dir);
69
55
  const body = `pid=${deps.pid}\nts=${deps.now()}\n`;
56
+ const stale = { kind: "pid", alive: (pid) => deps.isProcessAlive(pid) };
70
57
  for (let attempt = 0; attempt < STALE_TAKEOVER_ATTEMPTS; attempt += 1) {
71
- try {
72
- writeFileSync(lockPath, body, { flag: "wx", mode: 0o600 });
58
+ if (createLockFile(lockPath, body, writeFileSync)) {
73
59
  try {
74
60
  chmodSync(lockPath, 0o600);
75
61
  }
@@ -78,47 +64,44 @@ export function acquirePidLock(dir, deps) {
78
64
  }
79
65
  return { ok: true, path: lockPath };
80
66
  }
81
- catch (err) {
82
- const code = err?.code;
83
- if (code !== "EEXIST")
84
- throw err;
85
- const owner = readPidLock(lockPath, { readFileSync, existsSync });
86
- if (owner && deps.isProcessAlive(owner.pid)) {
87
- return { ok: false, reason: "held", owner, path: lockPath };
88
- }
89
- // Stale or unreadable: unlink and retry exclusive create.
90
- try {
91
- unlinkSync(lockPath);
92
- }
93
- catch (unlinkErr) {
94
- const ucode = unlinkErr?.code;
95
- if (ucode !== "ENOENT") {
96
- const raced = readPidLock(lockPath, { readFileSync, existsSync });
97
- if (raced && deps.isProcessAlive(raced.pid)) {
98
- return { ok: false, reason: "held", owner: raced, path: lockPath };
99
- }
100
- throw unlinkErr;
67
+ const owner = readPidLock(lockPath, { readFileSync, existsSync });
68
+ if (owner && !lockIsStale(stale, { owner })) {
69
+ return { ok: false, reason: "held", owner, path: lockPath };
70
+ }
71
+ // Stale or unreadable: unlink and retry exclusive create.
72
+ try {
73
+ unlinkSync(lockPath);
74
+ }
75
+ catch (unlinkErr) {
76
+ const ucode = unlinkErr?.code;
77
+ if (ucode !== "ENOENT") {
78
+ const raced = readPidLock(lockPath, { readFileSync, existsSync });
79
+ if (raced && !lockIsStale(stale, { owner: raced })) {
80
+ return { ok: false, reason: "held", owner: raced, path: lockPath };
101
81
  }
82
+ throw unlinkErr;
102
83
  }
103
84
  }
104
85
  }
105
86
  const finalOwner = readPidLock(lockPath, { readFileSync, existsSync });
106
- if (finalOwner && deps.isProcessAlive(finalOwner.pid)) {
87
+ if (finalOwner && !lockIsStale(stale, { owner: finalOwner })) {
107
88
  return { ok: false, reason: "held", owner: finalOwner, path: lockPath };
108
89
  }
109
90
  throw new Error(`pid lock acquire failed after ${STALE_TAKEOVER_ATTEMPTS} attempts`);
110
91
  }
111
92
  export function releasePidLock(dir, deps) {
112
- const lockPath = daemonPidPath(dir);
113
- const record = readPidLock(lockPath, deps);
114
- if (!record || record.pid !== deps.pid)
115
- return;
116
- try {
117
- (deps.unlinkSync ?? fs.unlinkSync)(lockPath);
118
- }
119
- catch {
120
- /* ignore */
121
- }
93
+ releaseLockFile(daemonPidPath(dir), {
94
+ read: (lockPath) => readPidLock(lockPath, deps),
95
+ owns: (record) => record.pid === deps.pid,
96
+ remove: (lockPath) => {
97
+ try {
98
+ (deps.unlinkSync ?? fs.unlinkSync)(lockPath);
99
+ }
100
+ catch {
101
+ /* ignore */
102
+ }
103
+ },
104
+ });
122
105
  }
123
106
  export function defaultPidLockDeps(overrides = {}) {
124
107
  return {
@@ -19,6 +19,7 @@
19
19
  * Both paths fail with EEXIST when the lock is held, so contention, stale-
20
20
  * owner reclamation and release are unchanged for every caller.
21
21
  */
22
+ import * as fs from "fs";
22
23
  /** Test seam: forget what we learned about which filesystems do links. */
23
24
  export declare function resetLinkSupportCache(): void;
24
25
  /**
@@ -29,4 +30,63 @@ export declare function resetLinkSupportCache(): void;
29
30
  * are written directly. Either way the lock ends up holding the same bytes.
30
31
  */
31
32
  export declare function acquireLockFileExclusively(candidate: string, lockFile: string, dirKey: string): void;
33
+ /** A `pid=<n>\nts=<unix seconds>` lock file (the daemon and mesh daemon pid locks). */
34
+ export interface PidLockRecord {
35
+ pid: number;
36
+ ts: number;
37
+ }
38
+ /** Parse a `pid=<n>\nts=<n>` body; null unless both fields are plain digits. */
39
+ export declare function parsePidLockRecord(content: string): PidLockRecord | null;
40
+ export interface LockFileReadDeps {
41
+ existsSync?: typeof fs.existsSync;
42
+ readFileSync?: typeof fs.readFileSync;
43
+ }
44
+ /** Read and parse a lock file. A missing file, a read error or a parse error is null. */
45
+ export declare function readLockFile<T>(lockPath: string, parse: (content: string) => T | null, deps?: LockFileReadDeps): T | null;
46
+ /**
47
+ * Create `lockPath` with `body` exclusively (`wx`, mode 0600). True when this
48
+ * call created it, false when it already exists (EEXIST); any other error is
49
+ * thrown.
50
+ */
51
+ export declare function createLockFile(lockPath: string, body: string, writeFileSync?: typeof fs.writeFileSync): boolean;
52
+ /**
53
+ * When a held lock may be reclaimed. Each caller passes the rule it has
54
+ * always used; the module does not choose one.
55
+ *
56
+ * - `pid`: stale unless the recorded owner is readable, is not `self` (when
57
+ * given) and `alive(owner.pid)` is true. No age limit: a live owner keeps
58
+ * the lock however old it is. `alive` is not called for `self`.
59
+ * - `age`: stale unless the lock's age is at most `maxAgeMs` (an age that
60
+ * is not a number is stale).
61
+ * - `none`: never stale.
62
+ */
63
+ export type StalePolicy = {
64
+ kind: "pid";
65
+ alive: (pid: number) => boolean;
66
+ self?: number;
67
+ } | {
68
+ kind: "age";
69
+ maxAgeMs: number;
70
+ } | {
71
+ kind: "none";
72
+ };
73
+ /** What the caller observed about the held lock: its owner (pid policy) or its age (age policy). */
74
+ export type LockObservation = {
75
+ owner: {
76
+ pid: number;
77
+ } | null;
78
+ } | {
79
+ ageMs: number;
80
+ };
81
+ export declare function lockIsStale(policy: StalePolicy, observed: LockObservation): boolean;
82
+ export interface LockFileRelease<T> {
83
+ /** Read the current owner; null when there is none or it is unreadable. */
84
+ read: (lockPath: string) => T | null;
85
+ /** The owner is this holder. */
86
+ owns: (owner: T) => boolean;
87
+ /** Remove the lock file (the caller's own error handling). */
88
+ remove: (lockPath: string) => void;
89
+ }
90
+ /** Remove the lock only when this holder owns it. True when `remove` ran. */
91
+ export declare function releaseLockFile<T>(lockPath: string, release: LockFileRelease<T>): boolean;
32
92
  //# sourceMappingURL=exclusive-lock.d.ts.map