@indigoai-us/hq-cli 5.345.212-beta.0 → 5.345.214

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 (119) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/dist/bin/hq-auth-refresh.js +2 -1
  3. package/dist/command-catalog.generated.d.ts +308 -43
  4. package/dist/command-catalog.generated.js +373 -43
  5. package/dist/command-registration-plan.d.ts +15 -0
  6. package/dist/command-registration-plan.js +20 -1
  7. package/dist/commands/agent-inbox.d.ts +11 -1
  8. package/dist/commands/agent-inbox.js +128 -38
  9. package/dist/commands/agent-kit.js +2 -2
  10. package/dist/commands/agents.js +6 -1
  11. package/dist/commands/billing.js +17 -4
  12. package/dist/commands/core-handoff.js +1 -0
  13. package/dist/commands/core-hq-session-native.js +2 -2
  14. package/dist/commands/core-pin-native.js +1 -1
  15. package/dist/commands/core-policy.js +1 -1
  16. package/dist/commands/core-resolve-company-native.js +1 -1
  17. package/dist/commands/core-sentry-report-fast.d.ts +2 -0
  18. package/dist/commands/core-sentry-report-fast.js +3 -1
  19. package/dist/commands/daemon.js +68 -6
  20. package/dist/commands/files.js +2 -2
  21. package/dist/commands/integrations-api.d.ts +11 -0
  22. package/dist/commands/integrations-connect.d.ts +9 -1
  23. package/dist/commands/integrations-connect.js +154 -18
  24. package/dist/commands/integrations-core.d.ts +1 -1
  25. package/dist/commands/integrations-core.js +18 -10
  26. package/dist/commands/integrations.js +1 -1
  27. package/dist/commands/lanes-link.js +10 -5
  28. package/dist/commands/lanes.js +34 -16
  29. package/dist/commands/rescue.js +13 -0
  30. package/dist/commands/secrets.d.ts +11 -0
  31. package/dist/commands/secrets.js +41 -7
  32. package/dist/index.js +2 -0
  33. package/dist/lazy-commands.js +6 -1
  34. package/dist/lib/agent-kit/inbox-fetch.d.ts +122 -1
  35. package/dist/lib/agent-kit/inbox-fetch.js +215 -3
  36. package/dist/lib/agent-kit/inbox-redact.js +1 -1
  37. package/dist/lib/agent-kit/inbox-state.d.ts +98 -1
  38. package/dist/lib/agent-kit/inbox-state.js +261 -4
  39. package/dist/lib/agent-kit/mcp/tools.d.ts +3 -3
  40. package/dist/lib/agent-kit/mcp/tools.js +68 -25
  41. package/dist/lib/agent-kit/run/inbox.js +4 -2
  42. package/dist/lib/agent-kit/skills.js +4 -1
  43. package/dist/lib/agent-kit/wake.d.ts +9 -1
  44. package/dist/lib/agent-kit/wake.js +10 -3
  45. package/dist/lib/billing/usage-only.d.ts +4 -1
  46. package/dist/lib/billing/usage-only.js +7 -2
  47. package/dist/lib/bot/fleet-cron.js +193 -14
  48. package/dist/lib/bot/fleet-download.d.ts +2 -0
  49. package/dist/lib/bot/fleet-download.js +23 -0
  50. package/dist/lib/bot/fleet-local.d.ts +1 -0
  51. package/dist/lib/bot/fleet-local.js +41 -8
  52. package/dist/lib/capacity/ledger.d.ts +7 -0
  53. package/dist/lib/capacity/ledger.js +31 -1
  54. package/dist/lib/capacity/project.d.ts +1 -1
  55. package/dist/lib/capacity/project.js +13 -4
  56. package/dist/lib/capacity/route.d.ts +10 -0
  57. package/dist/lib/capacity/route.js +16 -0
  58. package/dist/lib/core-utils/session-id.d.ts +1 -1
  59. package/dist/lib/core-utils/session-id.js +4 -0
  60. package/dist/lib/daemon/install.d.ts +15 -0
  61. package/dist/lib/daemon/install.js +31 -0
  62. package/dist/lib/daemon/migrate.js +29 -4
  63. package/dist/lib/daemon/run.js +9 -1
  64. package/dist/lib/daemon/updater.d.ts +34 -2
  65. package/dist/lib/daemon/updater.js +51 -2
  66. package/dist/lib/doctor/__testing__/run-doctor.js +4 -0
  67. package/dist/lib/doctor/checks/jq.d.ts +27 -0
  68. package/dist/lib/doctor/checks/jq.js +82 -0
  69. package/dist/lib/doctor/checks/release-links.d.ts +50 -8
  70. package/dist/lib/doctor/checks/release-links.js +260 -24
  71. package/dist/lib/doctor/fix/apply.js +24 -8
  72. package/dist/lib/doctor/fix/remediation.js +15 -5
  73. package/dist/lib/doctor/registry.js +4 -0
  74. package/dist/lib/flag-snapshot-file.js +36 -8
  75. package/dist/lib/install/codex-targets.d.ts +52 -1
  76. package/dist/lib/install/codex-targets.js +77 -2
  77. package/dist/lib/lanes/autopilot-registry.d.ts +14 -0
  78. package/dist/lib/lanes/autopilot-registry.js +59 -7
  79. package/dist/lib/lanes/links.d.ts +14 -0
  80. package/dist/lib/lanes/links.js +320 -25
  81. package/dist/lib/lanes/monitor.d.ts +4 -1
  82. package/dist/lib/lanes/monitor.js +47 -10
  83. package/dist/lib/lanes/outpost-pruner-install.d.ts +10 -0
  84. package/dist/lib/lanes/outpost-pruner-install.js +4 -3
  85. package/dist/lib/lanes/spawn.js +8 -1
  86. package/dist/lib/lanes/watch.js +31 -0
  87. package/dist/lib/onboarding/checkpoint.d.ts +6 -0
  88. package/dist/lib/onboarding/checkpoint.js +6 -0
  89. package/dist/lib/onboarding/orchestrator.d.ts +3 -1
  90. package/dist/lib/onboarding/orchestrator.js +62 -9
  91. package/dist/lib/onboarding/types.d.ts +8 -0
  92. package/dist/lib/work-context/organize.d.ts +23 -1
  93. package/dist/lib/work-context/organize.js +25 -3
  94. package/dist/lib/work-context/reconcile.js +3 -2
  95. package/dist/main.js +3 -1
  96. package/dist/secrets-env-action.js +7 -5
  97. package/dist/sentry-init.d.ts +2 -0
  98. package/dist/sentry-init.js +13 -2
  99. package/dist/sentry.d.ts +2 -0
  100. package/dist/sentry.js +5 -1
  101. package/dist/startup-registration.js +7 -2
  102. package/dist/utils/billing-gate.d.ts +12 -16
  103. package/dist/utils/billing-gate.js +17 -18
  104. package/dist/utils/cli-network-lifecycle.d.ts +9 -0
  105. package/dist/utils/cli-network-lifecycle.js +52 -0
  106. package/dist/utils/install-tree-torn.d.ts +22 -4
  107. package/dist/utils/install-tree-torn.js +24 -0
  108. package/dist/utils/registration-plan-error.d.ts +22 -0
  109. package/dist/utils/registration-plan-error.js +27 -0
  110. package/dist/utils/safe-link.d.ts +63 -0
  111. package/dist/utils/safe-link.js +246 -0
  112. package/dist/utils/sentry-fingerprint.js +3 -0
  113. package/dist/utils/sentry-keepalive-transport.d.ts +28 -0
  114. package/dist/utils/sentry-keepalive-transport.js +70 -0
  115. package/dist/utils/unauthorized-error.d.ts +36 -0
  116. package/dist/utils/unauthorized-error.js +105 -0
  117. package/dist/utils/vault-api.js +7 -5
  118. package/package.json +7 -7
  119. package/scripts/install-hook-engine.mjs +17 -0
@@ -8,13 +8,34 @@
8
8
  import * as fs from "node:fs";
9
9
  import * as os from "node:os";
10
10
  import * as path from "node:path";
11
- import { removeLinkOnly } from "../../../utils/safe-link.js";
11
+ import { copyFolderForLink, linkCopyContentHash, linkCopyTreeHash, readLinkCopyMarker, refreshLinkCopy, removeLinkOnly, } from "../../../utils/safe-link.js";
12
12
  export const RELEASE_LINKS_FAMILY_ID = "release-links";
13
13
  export const RELEASE_LINKS_FAMILY_TITLE = "Release links";
14
14
  export const RELEASE_LINKS_CHECK_ID = "release-links.resolve";
15
- export const RELEASE_LINK_REMEDIATION = "Run `hq doctor --fix` (or `hq rescue -y`) to restore the shipped link. A dangling link that points outside the HQ root and the temp folder is treated as your own and left alone. On Windows, a directory link is written as a junction and a file link as a copy when symlinks are not allowed.";
15
+ export const RELEASE_LINK_REMEDIATION = "Run `hq doctor --fix` (or `hq rescue -y`) to restore the shipped link. A dangling link that points outside the HQ root and the temp folder is treated as your own and left alone. On Windows, a directory link is written as a junction and a file link as a copy when symlinks are not allowed. A junction Windows will not list through is replaced by a copy of its target.";
16
16
  /** Message prefix for a link whose target is missing; `--fix` keys off it. */
17
17
  export const DANGLING_RELEASE_LINK_PROBLEM = "does not resolve.";
18
+ /**
19
+ * Message prefix for a directory link that resolves to its shipped target but
20
+ * cannot be listed through (a junction a standard Windows user created on
21
+ * some machines); `--fix` keys off it.
22
+ */
23
+ export const UNLISTABLE_RELEASE_LINK_PROBLEM = "is a link Windows will not list through.";
24
+ /**
25
+ * Message prefix for an HQ fallback copy whose target changed since the copy
26
+ * was made and that the user has not edited; `--fix` keys off it.
27
+ */
28
+ export const STALE_RELEASE_LINK_COPY_PROBLEM = "is an out-of-date copy of its target.";
29
+ /** Remediation for a fallback copy that is current or out of date. */
30
+ export const RELEASE_LINK_COPY_REMEDIATION = "Copies do not follow updates. `hq doctor --fix` or `hq rescue -y` refreshes the copy from its target when the target changes. Make changes in the target folder, not in the copy: a copy with changes made inside it is not refreshed, and a refresh would replace them.";
31
+ /** Every message prefix the `repair-release-link` fix acts on. */
32
+ export const REPAIRABLE_RELEASE_LINK_PROBLEMS = [
33
+ DANGLING_RELEASE_LINK_PROBLEM,
34
+ UNLISTABLE_RELEASE_LINK_PROBLEM,
35
+ STALE_RELEASE_LINK_COPY_PROBLEM,
36
+ ];
37
+ /** Why HQ copies a directory instead of keeping the junction. */
38
+ const UNLISTABLE_JUNCTION_REASON = "Windows created the junction but would not list its contents, so HQ copied the target instead.";
18
39
  const CYGWIN_SYMLINK_COOKIE = Buffer.from("!<symlink>", "latin1");
19
40
  /** Paths git records as symlinks in the hq-core release. */
20
41
  export const RELEASE_LINKS = [
@@ -49,21 +70,25 @@ export function checkReleaseLinks(context) {
49
70
  },
50
71
  ];
51
72
  }
52
- const failures = [];
73
+ const findings = [];
53
74
  for (const entry of present) {
54
75
  const problem = releaseLinkProblem(context.hqRoot, entry);
55
- if (!problem)
76
+ if (problem) {
77
+ findings.push({
78
+ status: "FAIL",
79
+ checkId: RELEASE_LINKS_CHECK_ID,
80
+ target: entry.rel,
81
+ message: `${entry.rel} ${problem}`,
82
+ remediation: RELEASE_LINK_REMEDIATION,
83
+ });
56
84
  continue;
57
- failures.push({
58
- status: "FAIL",
59
- checkId: RELEASE_LINKS_CHECK_ID,
60
- target: entry.rel,
61
- message: `${entry.rel} ${problem}`,
62
- remediation: RELEASE_LINK_REMEDIATION,
63
- });
85
+ }
86
+ const copy = releaseLinkCopyFinding(context.hqRoot, entry);
87
+ if (copy)
88
+ findings.push(copy);
64
89
  }
65
- if (failures.length > 0)
66
- return failures;
90
+ if (findings.length > 0)
91
+ return findings;
67
92
  return [
68
93
  {
69
94
  status: "PASS",
@@ -93,6 +118,11 @@ function releaseLinkProblem(hqRoot, entry) {
93
118
  if (!pathIsInside(hqRoot, resolved)) {
94
119
  return "points outside this HQ root, so it does not resolve to the shipped target.";
95
120
  }
121
+ if (entry.kind === "dir" &&
122
+ resolvesToShippedTarget(hqRoot, entry, resolved) &&
123
+ !linkListsLikeTarget(destination, resolved)) {
124
+ return `${UNLISTABLE_RELEASE_LINK_PROBLEM} It points at ${resolved}, but listing through it fails or shows a different number of entries than the folder itself.`;
125
+ }
96
126
  return null;
97
127
  }
98
128
  if (lst.isFile()) {
@@ -124,6 +154,104 @@ function readlinkAbsOrNull(destination) {
124
154
  return null;
125
155
  }
126
156
  }
157
+ /**
158
+ * Whether listing a directory through `link` works and shows as many entries
159
+ * as listing `target` directly. False when the link listing throws (EPERM,
160
+ * EACCES, ENOENT, ...) or the counts differ. When `target` itself cannot be
161
+ * listed the link is not blamed, so this returns true.
162
+ */
163
+ export function linkListsLikeTarget(link, target) {
164
+ let expected;
165
+ try {
166
+ expected = fs.readdirSync(target).length;
167
+ }
168
+ catch {
169
+ return true;
170
+ }
171
+ try {
172
+ return fs.readdirSync(link).length === expected;
173
+ }
174
+ catch {
175
+ return false;
176
+ }
177
+ }
178
+ function resolvesToShippedTarget(hqRoot, entry, resolved) {
179
+ const shipped = containedTarget(hqRoot, entry);
180
+ if (!shipped)
181
+ return false;
182
+ const shippedReal = resolvesOrNull(shipped);
183
+ return shippedReal !== null && shippedReal === resolved;
184
+ }
185
+ /**
186
+ * Classify the HQ fallback copy at a directory release link: `edited` when
187
+ * its files no longer match the hash HQ recorded, else `stale` when its
188
+ * target changed since, else `current`. Null when the path is not an HQ copy
189
+ * of the shipped target (a folder the user made, or a copy of something else).
190
+ */
191
+ function linkCopyState(hqRoot, entry) {
192
+ if (entry.kind !== "dir")
193
+ return null;
194
+ const destination = path.join(hqRoot, entry.rel);
195
+ const marker = readLinkCopyMarker(destination);
196
+ if (!marker)
197
+ return null;
198
+ const shipped = containedTarget(hqRoot, entry);
199
+ if (!shipped)
200
+ return null;
201
+ const rootReal = resolvesOrNull(path.resolve(hqRoot));
202
+ if (rootReal === null)
203
+ return null;
204
+ const shippedAbs = path.join(rootReal, path.relative(path.resolve(hqRoot), shipped));
205
+ if (path.resolve(marker.source) !== shippedAbs && path.resolve(marker.source) !== path.resolve(shipped)) {
206
+ return null;
207
+ }
208
+ try {
209
+ if (linkCopyContentHash(destination, rootReal) !== marker.hash) {
210
+ return { state: "edited", source: marker.source };
211
+ }
212
+ if (!statOrNull(marker.source)?.isDirectory()) {
213
+ return { state: "unreadable", source: marker.source, reason: "its target folder is missing" };
214
+ }
215
+ const fresh = linkCopyTreeHash(marker.source, rootReal) === marker.hash;
216
+ return { state: fresh ? "current" : "stale", source: marker.source };
217
+ }
218
+ catch (err) {
219
+ return { state: "unreadable", source: marker.source, reason: errorMessage(err) };
220
+ }
221
+ }
222
+ /** A WARN for an HQ fallback copy at a directory release link, or null. */
223
+ function releaseLinkCopyFinding(hqRoot, entry) {
224
+ const copy = linkCopyState(hqRoot, entry);
225
+ if (!copy)
226
+ return null;
227
+ const base = { status: "WARN", checkId: RELEASE_LINKS_CHECK_ID, target: entry.rel };
228
+ switch (copy.state) {
229
+ case "current":
230
+ return {
231
+ ...base,
232
+ message: `${entry.rel} is a copy of ${copy.source}, made because Windows would not list through a junction there. It matches its target now.`,
233
+ remediation: RELEASE_LINK_COPY_REMEDIATION,
234
+ };
235
+ case "stale":
236
+ return {
237
+ ...base,
238
+ message: `${entry.rel} ${STALE_RELEASE_LINK_COPY_PROBLEM} It was copied from ${copy.source} because Windows would not list through a junction there, and that folder has changed since.`,
239
+ remediation: RELEASE_LINK_COPY_REMEDIATION,
240
+ };
241
+ case "edited":
242
+ return {
243
+ ...base,
244
+ message: `${entry.rel} is a copy of ${copy.source}, and files inside it changed after HQ made it. HQ will not refresh it, because a refresh would replace those changes.`,
245
+ remediation: `Move the changes you want to keep into ${copy.source}, then delete ${entry.rel} and run \`hq rescue -y\` to lay it again.`,
246
+ };
247
+ case "unreadable":
248
+ return {
249
+ ...base,
250
+ message: `${entry.rel} is a copy of ${copy.source} that HQ could not compare with its target: ${copy.reason}.`,
251
+ remediation: RELEASE_LINK_COPY_REMEDIATION,
252
+ };
253
+ }
254
+ }
127
255
  /** Prefix of the folder hq-cloud's rescue creates with mkdtemp. */
128
256
  const RESCUE_STAGING_SEGMENT_PREFIX = "hq-replace-rescue-";
129
257
  /**
@@ -217,6 +345,19 @@ function readFileOrNull(target) {
217
345
  * is created. The old entry is removed with {@link removeLinkOnly}, which never
218
346
  * follows it. A failure on one link is reported as `failed` and the remaining
219
347
  * links are still repaired.
348
+ *
349
+ * Windows junctions that cannot be listed through: on some Windows machines a
350
+ * junction a standard user creates has the right target but nobody can list
351
+ * through it (hq-core-staging PR 1883, Beaker). After writing a directory
352
+ * junction this lists through it and compares the entry count with the
353
+ * target; on a mismatch the junction is removed and the target copied in its
354
+ * place, marked with `.hq-link-copy` (`copied`). A link that already resolves
355
+ * to the shipped target but cannot be listed through is rewritten the same
356
+ * way. A link that lists correctly is never replaced.
357
+ *
358
+ * HQ copies: an unedited copy whose target changed is refreshed (`refreshed`).
359
+ * A copy whose files changed after HQ made it is left alone (`kept`), because
360
+ * a refresh would replace those changes. A current copy is not touched.
220
361
  */
221
362
  export function repairDanglingReleaseLinks(hqRoot, deps = {}) {
222
363
  const platform = deps.platform ?? process.platform;
@@ -233,9 +374,7 @@ export function repairDanglingReleaseLinks(hqRoot, deps = {}) {
233
374
  continue;
234
375
  const destination = path.join(rootReal, entry.rel);
235
376
  const lst = lstatOrNull(destination);
236
- if (!lst || !lst.isSymbolicLink())
237
- continue;
238
- if (resolvesOrNull(destination) !== null)
377
+ if (!lst)
239
378
  continue;
240
379
  const parentReal = resolvesOrNull(path.dirname(destination));
241
380
  if (parentReal === null || !isUnder(rootReal, parentReal))
@@ -243,6 +382,30 @@ export function repairDanglingReleaseLinks(hqRoot, deps = {}) {
243
382
  const target = path.resolve(path.dirname(destination), entry.linkText);
244
383
  if (!isUnder(rootReal, target))
245
384
  continue;
385
+ if (!lst.isSymbolicLink()) {
386
+ const copyRepair = refreshStaleLinkCopy(rootReal, entry, destination);
387
+ if (copyRepair)
388
+ repairs.push(copyRepair);
389
+ continue;
390
+ }
391
+ const resolved = resolvesOrNull(destination);
392
+ if (resolved !== null) {
393
+ // A resolving link is replaced only when it is a directory link to the
394
+ // shipped target that cannot be listed through.
395
+ if (entry.kind !== "dir" ||
396
+ resolved !== resolvesOrNull(target) ||
397
+ linkListsLikeTarget(destination, resolved)) {
398
+ continue;
399
+ }
400
+ try {
401
+ removeLinkOnly(destination);
402
+ repairs.push(writeReleaseLink(entry, target, destination, platform, rootReal));
403
+ }
404
+ catch (err) {
405
+ repairs.push({ rel: entry.rel, action: "failed", reason: errorMessage(err) });
406
+ }
407
+ continue;
408
+ }
246
409
  let current;
247
410
  try {
248
411
  current = path.resolve(path.dirname(destination), fs.readlinkSync(destination));
@@ -269,8 +432,7 @@ export function repairDanglingReleaseLinks(hqRoot, deps = {}) {
269
432
  repairs.push({ rel: entry.rel, action: "removed" });
270
433
  continue;
271
434
  }
272
- const linkText = writeReleaseLink(entry, target, destination, platform);
273
- repairs.push({ rel: entry.rel, action: "repointed", target, linkText });
435
+ repairs.push(writeReleaseLink(entry, target, destination, platform, rootReal));
274
436
  }
275
437
  catch (err) {
276
438
  repairs.push({ rel: entry.rel, action: "failed", reason: errorMessage(err) });
@@ -278,19 +440,93 @@ export function repairDanglingReleaseLinks(hqRoot, deps = {}) {
278
440
  }
279
441
  return repairs;
280
442
  }
281
- /** Write the link and return the text it carries. */
282
- function writeReleaseLink(entry, target, destination, platform) {
443
+ /**
444
+ * Refresh the HQ copy at a directory release link when its target changed and
445
+ * its own files did not. Returns null when there is nothing to do: the path is
446
+ * not an HQ copy of the shipped target, or the copy is current.
447
+ */
448
+ function refreshStaleLinkCopy(rootReal, entry, destination) {
449
+ const copy = linkCopyState(rootReal, entry);
450
+ if (!copy || copy.state === "current")
451
+ return null;
452
+ if (copy.state === "edited") {
453
+ return {
454
+ rel: entry.rel,
455
+ action: "kept",
456
+ target: copy.source,
457
+ reason: `files inside the copy changed after HQ made it, so it was not refreshed. Move the changes you want to keep into ${copy.source}, then delete ${entry.rel} and run \`hq rescue -y\``,
458
+ };
459
+ }
460
+ if (copy.state === "unreadable") {
461
+ return { rel: entry.rel, action: "failed", target: copy.source, reason: copy.reason };
462
+ }
463
+ try {
464
+ const result = refreshLinkCopy(copy.source, destination, {
465
+ within: rootReal,
466
+ reason: UNLISTABLE_JUNCTION_REASON,
467
+ });
468
+ return {
469
+ rel: entry.rel,
470
+ action: "refreshed",
471
+ target: copy.source,
472
+ ...(result.skippedOutside.length > 0 ? { skippedOutside: result.skippedOutside } : {}),
473
+ };
474
+ }
475
+ catch (err) {
476
+ return { rel: entry.rel, action: "failed", target: copy.source, reason: errorMessage(err) };
477
+ }
478
+ }
479
+ /**
480
+ * Write the link and report what was written. On Windows a directory link is
481
+ * a junction that must list like its target; when it does not, the junction is
482
+ * removed and the target copied in its place.
483
+ */
484
+ function writeReleaseLink(entry, target, destination, platform, rootReal) {
485
+ const repointed = (linkText) => ({
486
+ rel: entry.rel,
487
+ action: "repointed",
488
+ target,
489
+ linkText,
490
+ });
283
491
  if (platform !== "win32") {
284
492
  fs.symlinkSync(entry.linkText, destination, entry.kind);
285
- return entry.linkText;
493
+ return repointed(entry.linkText);
286
494
  }
287
495
  if (entry.kind === "dir") {
288
496
  fs.symlinkSync(target, destination, "junction");
289
- return target;
497
+ if (linkListsLikeTarget(destination, target))
498
+ return repointed(target);
499
+ removeLinkOnly(destination);
500
+ let result;
501
+ try {
502
+ result = copyFolderForLink(target, destination, {
503
+ within: rootReal,
504
+ reason: UNLISTABLE_JUNCTION_REASON,
505
+ });
506
+ }
507
+ catch (err) {
508
+ // Put the junction back so the path is not left empty: an empty path is
509
+ // invisible to the check and to later repairs, while the junction keeps
510
+ // the FAIL (and its --fix) in front of the user.
511
+ try {
512
+ fs.symlinkSync(target, destination, "junction");
513
+ }
514
+ catch {
515
+ /* report the copy error */
516
+ }
517
+ throw err;
518
+ }
519
+ return {
520
+ rel: entry.rel,
521
+ action: "copied",
522
+ target,
523
+ reason: UNLISTABLE_JUNCTION_REASON,
524
+ ...(result.skippedOutside.length > 0 ? { skippedOutside: result.skippedOutside } : {}),
525
+ };
290
526
  }
291
527
  try {
292
528
  fs.symlinkSync(target, destination, "file");
293
- return target;
529
+ return repointed(target);
294
530
  }
295
531
  catch (err) {
296
532
  const code = err.code;
@@ -299,7 +535,7 @@ function writeReleaseLink(entry, target, destination, platform) {
299
535
  // Windows without the symlink privilege: the check accepts a copy whose
300
536
  // bytes equal the target.
301
537
  fs.copyFileSync(target, destination);
302
- return target;
538
+ return repointed(target);
303
539
  }
304
540
  }
305
541
  function tempRoots(tmpDir) {
@@ -44,7 +44,7 @@ import { getStateDir } from "@indigoai-us/hq-cloud";
44
44
  import { isPersonalJournalSlug } from "../checks/sync-health.js";
45
45
  import { createDefaultRegistry } from "../registry.js";
46
46
  import { flattenFamilies } from "../report.js";
47
- import { repairDanglingReleaseLinks } from "../checks/release-links.js";
47
+ import { STALE_RELEASE_LINK_COPY_PROBLEM, UNLISTABLE_RELEASE_LINK_PROBLEM, repairDanglingReleaseLinks, } from "../checks/release-links.js";
48
48
  import { parseHookGateProfiles, requiredMissingProfiles, } from "../hook-gate-profiles.js";
49
49
  import { createBackup } from "./backup.js";
50
50
  import { removeStrayGateEntries } from "../stray-gate-entries.js";
@@ -1096,10 +1096,8 @@ function planRepairReleaseLink(hqRoot, result, rem) {
1096
1096
  fixClass: "repair-release-link",
1097
1097
  target: rel,
1098
1098
  relpath: "",
1099
- preview: ` ${rel}\n` +
1100
- ` - dangling link\n` +
1101
- ` + the shipped link to its target under the HQ root (removed if that target is missing)`,
1102
- summary: `repair dangling link ${rel}`,
1099
+ preview: releaseLinkPreview(rel, result.message),
1100
+ summary: `repair release link ${rel}`,
1103
1101
  apply: () => {
1104
1102
  // Each plan repairs only its own link, and a throw is recorded as this
1105
1103
  // plan's failure so the remaining plans still run.
@@ -1109,7 +1107,7 @@ function planRepairReleaseLink(hqRoot, result, rem) {
1109
1107
  if (!entry) {
1110
1108
  failure = `${rel} was not repaired`;
1111
1109
  }
1112
- else if (entry.action === "skipped" || entry.action === "failed") {
1110
+ else if (entry.action === "skipped" || entry.action === "failed" || entry.action === "kept") {
1113
1111
  failure = `${rel} was not repaired: ${entry.reason ?? entry.action}`;
1114
1112
  }
1115
1113
  else {
@@ -1122,8 +1120,12 @@ function planRepairReleaseLink(hqRoot, result, rem) {
1122
1120
  },
1123
1121
  postStatus: (results) => {
1124
1122
  const own = results.filter((entry) => entry.checkId === result.checkId);
1125
- if (own.some((entry) => entry.target === rel))
1126
- return "FAIL";
1123
+ // A link that became a fallback copy is a WARN by design (copies do not
1124
+ // follow updates); anything else still reported for this path is its
1125
+ // own status.
1126
+ const mine = own.find((entry) => entry.target === rel);
1127
+ if (mine)
1128
+ return mine.status;
1127
1129
  // Another link's failure is that plan's status, not this one's.
1128
1130
  if (own.some((entry) => entry.status === "FAIL"))
1129
1131
  return "PASS";
@@ -1132,6 +1134,20 @@ function planRepairReleaseLink(hqRoot, result, rem) {
1132
1134
  failureReason: () => failure,
1133
1135
  };
1134
1136
  }
1137
+ function releaseLinkPreview(rel, message) {
1138
+ const problem = message.startsWith(rel + " ") ? message.slice(rel.length + 1) : message;
1139
+ if (problem.startsWith(UNLISTABLE_RELEASE_LINK_PROBLEM)) {
1140
+ return (` ${rel}\n` +
1141
+ ` - a link Windows will not list through\n` +
1142
+ ` + the shipped link, or a marked copy of its target if Windows still will not list through it`);
1143
+ }
1144
+ if (problem.startsWith(STALE_RELEASE_LINK_COPY_PROBLEM)) {
1145
+ return ` ${rel}\n` + ` - an out-of-date copy\n` + ` + a fresh copy of its target`;
1146
+ }
1147
+ return (` ${rel}\n` +
1148
+ ` - dangling link\n` +
1149
+ ` + the shipped link to its target under the HQ root (removed if that target is missing)`);
1150
+ }
1135
1151
  /** Restore the execute bit on a hook script. */
1136
1152
  function planExecutableBit(hqRoot, result, rem) {
1137
1153
  const abs = rem.fixTarget;
@@ -60,7 +60,7 @@
60
60
  * deciding which of two diverged copies is correct needs human judgement.
61
61
  */
62
62
  import * as path from "node:path";
63
- import { DANGLING_RELEASE_LINK_PROBLEM, RELEASE_LINKS, RELEASE_LINKS_CHECK_ID, } from "../checks/release-links.js";
63
+ import { REPAIRABLE_RELEASE_LINK_PROBLEMS, STALE_RELEASE_LINK_COPY_PROBLEM, UNLISTABLE_RELEASE_LINK_PROBLEM, RELEASE_LINKS, RELEASE_LINKS_CHECK_ID, } from "../checks/release-links.js";
64
64
  import { isCompanyJournalSlug, isSafeCompanySlug, isPersonalJournalSlug, journalSlugFromCheckId, } from "../checks/sync-health.js";
65
65
  /** Matches an exec-bit remediation command, quoted or not: `chmod +x <path>`. */
66
66
  const CHMOD_EXEC = /^chmod\s+\+x\s+(.+)$/;
@@ -189,16 +189,26 @@ export function deriveRemediation(result) {
189
189
  };
190
190
  }
191
191
  // repair-release-link — a shipped link whose target is missing, such as one
192
- // a rescue pointed at its deleted temp folder (feedback_2ab63e50).
192
+ // a rescue pointed at its deleted temp folder (feedback_2ab63e50); a
193
+ // Windows junction that cannot be listed through (FAIL); or an unedited HQ
194
+ // fallback copy whose target changed (WARN).
193
195
  if (checkId === RELEASE_LINKS_CHECK_ID &&
194
- result.status === "FAIL" &&
196
+ (result.status === "FAIL" || result.status === "WARN") &&
195
197
  RELEASE_LINKS.some((entry) => entry.rel === target) &&
196
- result.message.startsWith(target + " " + DANGLING_RELEASE_LINK_PROBLEM)) {
198
+ REPAIRABLE_RELEASE_LINK_PROBLEMS.some((problem) => result.message.startsWith(target + " " + problem))) {
199
+ const problem = result.message.slice(target.length + 1);
200
+ let action = `Repoint the dangling ${target} link at its target under the HQ root (or remove the link when that target is missing).`;
201
+ if (problem.startsWith(UNLISTABLE_RELEASE_LINK_PROBLEM)) {
202
+ action = `Rewrite the ${target} link; if Windows still will not list through the junction, replace it with a marked copy of its target.`;
203
+ }
204
+ else if (problem.startsWith(STALE_RELEASE_LINK_COPY_PROBLEM)) {
205
+ action = `Refresh the ${target} copy from its target. Copies whose files were changed are left alone.`;
206
+ }
197
207
  return {
198
208
  autoFixable: true,
199
209
  fixClass: "repair-release-link",
200
210
  fixTarget: target,
201
- action: `Repoint the dangling ${target} link at its target under the HQ root (or remove the link when that target is missing).`,
211
+ action,
202
212
  command: "hq doctor --fix",
203
213
  };
204
214
  }
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import * as fs from "node:fs";
17
17
  import { hookEngineFamily } from './checks/hook-engine.js';
18
+ import { jqFamily } from './checks/jq.js';
18
19
  import * as path from "node:path";
19
20
  import { checkCodexWiring } from "./checks/codex-wiring.js";
20
21
  import { checkGrokWiring } from "./checks/grok-wiring.js";
@@ -245,6 +246,9 @@ export function createDefaultRegistry() {
245
246
  const registry = new DoctorRegistry();
246
247
  registry.register(hooksFamily);
247
248
  registry.register(hookEngineFamily);
249
+ // master-hook parses every payload with jq and blocks tool calls without it
250
+ // (hq-core-staging PR 1882). PATH scan plus `jq --version`; never installs.
251
+ registry.register(jqFamily);
248
252
  registry.register(scaffoldCliFloorFamily);
249
253
  // Fixture coverage (US-007): reports UNTESTED for every registered hook with
250
254
  // no fixture and prints the tested/total coverage line. A separate family so
@@ -146,8 +146,38 @@ export function personFlagSnapshotPath(env = process.env) {
146
146
  const cacheHome = env.XDG_CACHE_HOME?.trim() || path.join(os.homedir(), ".cache");
147
147
  return path.join(cacheHome, "hq", "flags", "snapshot-person.json");
148
148
  }
149
+ /**
150
+ * Per-hook engine bridge flags (`hook-engine-port-<hook-id>`) carry a mode,
151
+ * not only a boolean. The hq-hook engine (crates/hq-hook/src/flags.rs
152
+ * `mode_of`) maps `true` and `"on"` to on, `"shadow"` to shadow, and every
153
+ * other value to off, so these exact strings must survive the snapshot.
154
+ */
155
+ const HOOK_ENGINE_PORT_FLAG_PREFIX = "hook-engine-port-";
156
+ const HOOK_ENGINE_PORT_MODES = new Set(["off", "shadow", "on"]);
157
+ function isHookEnginePortMode(key, value) {
158
+ return key.startsWith(HOOK_ENGINE_PORT_FLAG_PREFIX)
159
+ && key.length > HOOK_ENGINE_PORT_FLAG_PREFIX.length
160
+ && HOOK_ENGINE_PORT_MODES.has(value);
161
+ }
162
+ function isPersistedFlagValue(key, value) {
163
+ return typeof value === "boolean" || isHookEnginePortMode(key, value);
164
+ }
149
165
  function normalizedFlagValues(flags) {
150
- return Object.fromEntries(Object.entries(flags).filter((entry) => Boolean(entry[0].trim()) && typeof entry[1] === "boolean"));
166
+ return Object.fromEntries(Object.entries(flags).filter((entry) => Boolean(entry[0].trim()) && isPersistedFlagValue(entry[0], entry[1])));
167
+ }
168
+ /**
169
+ * Validate every stored value, then return only the boolean decisions. CLI
170
+ * gates are boolean; the engine reads port mode strings from the file itself.
171
+ */
172
+ function booleanFlagsOf(flags) {
173
+ const booleans = {};
174
+ for (const [key, value] of Object.entries(flags)) {
175
+ if (!isPersistedFlagValue(key, value))
176
+ return null;
177
+ if (typeof value === "boolean")
178
+ booleans[key] = value;
179
+ }
180
+ return booleans;
151
181
  }
152
182
  function writeSnapshotAt(destination, snapshot, cacheRootKind) {
153
183
  const directory = path.dirname(destination);
@@ -259,8 +289,8 @@ export function readPersonFlagSnapshotFile(options = {}) {
259
289
  const age = (options.now ?? Date.now()) - fetchedAt;
260
290
  if (!Number.isFinite(fetchedAt) || age < 0 || age > (options.maxAgeMs ?? FLAG_SNAPSHOT_MAX_AGE_MS))
261
291
  continue;
262
- const flags = value.flags;
263
- if (!Object.values(flags).every((enabled) => typeof enabled === "boolean"))
292
+ const flags = booleanFlagsOf(value.flags);
293
+ if (!flags)
264
294
  continue;
265
295
  if (file === fallback && fallback)
266
296
  fallbackCacheUsed = true;
@@ -341,11 +371,9 @@ function parseSnapshotFile(companyUid, file, options) {
341
371
  if (!Number.isFinite(fetchedAt) || age < 0 || age > (options.maxAgeMs ?? FLAG_SNAPSHOT_MAX_AGE_MS)) {
342
372
  return null;
343
373
  }
344
- const flags = value.flags;
345
- for (const enabled of Object.values(flags)) {
346
- if (typeof enabled !== "boolean")
347
- return null;
348
- }
374
+ const flags = booleanFlagsOf(value.flags);
375
+ if (!flags)
376
+ return null;
349
377
  return {
350
378
  company_uid: companyUid,
351
379
  fetched_at: value.fetched_at,
@@ -24,11 +24,62 @@ export declare function addAgentsMdBlock(current: string | null, charter: string
24
24
  export declare function removeAgentsMdBlock(current: string): string;
25
25
  type Doc = Record<string, unknown>;
26
26
  export declare function isAdapterEntry(entry: unknown, hqRoot: string): boolean;
27
+ /**
28
+ * Where Codex hook runs claim an event, under `$HOME` (feedback_bf4df70e,
29
+ * feedback_d69d01c0). Shared with the hq-core Codex hook adapter
30
+ * (`.codex/hooks/hq-codex-hook-adapter.sh`), which claims the same way, so the
31
+ * two must agree on this directory, the key and the window.
32
+ */
33
+ export declare const CODEX_HOOK_CLAIM_DIR = ".hq/state/codex-hook-claims";
34
+ /** A claim younger than this many seconds makes a second run of the same event a no-op. */
35
+ export declare const CODEX_HOOK_CLAIM_WINDOW_SECONDS = 10;
36
+ /** Claim markers older than this many minutes are pruned on every successful claim. */
37
+ export declare const CODEX_HOOK_CLAIM_PRUNE_MINUTES = 2;
38
+ /** Set on the hook a wrapper claimed for, so the adapter it runs does not claim again. */
39
+ export declare const CODEX_HOOK_CLAIM_ENV = "HQ_CODEX_HOOK_CLAIM";
40
+ /**
41
+ * Wrap a user-scope (~/.codex/hooks.json) command so one Codex event runs the
42
+ * HQ hooks once (feedback_bf4df70e, feedback_d69d01c0).
43
+ *
44
+ * Inside an HQ root, Codex runs this entry and the root's own
45
+ * `.codex/config.toml` hook for the same event, at the same time, with no
46
+ * dedupe. Predicting from a shell whether Codex will load and trust the project
47
+ * hook proved unreliable (project-root rules, per-hook trust by hash, path
48
+ * spellings), so both registrations stay and the second run of an event is a
49
+ * no-op at runtime instead. Each run claims the event once:
50
+ *
51
+ * - The key is `cksum` of the real HQ root, the Codex process (`$PPID`) and
52
+ * the exact stdin payload. Codex sends both hooks of one event the same
53
+ * payload from the same process; different events differ in their payload
54
+ * (event name, tool call, turn), and other Codex sessions differ in `$PPID`.
55
+ * - The claim is an atomic `mkdir` of `$HOME/.hq/state/codex-hook-claims/<key>`
56
+ * holding the claim time. The first run to create it does the full work. A
57
+ * run that finds a claim younger than {@link CODEX_HOOK_CLAIM_WINDOW_SECONDS}
58
+ * exits 0 with no output; an older claim is taken over, so two identical
59
+ * events further apart both run. Markers older than
60
+ * {@link CODEX_HOOK_CLAIM_PRUNE_MINUTES} minutes are pruned.
61
+ * - The claiming run passes {@link CODEX_HOOK_CLAIM_ENV} to the command it
62
+ * runs, so the HQ adapter does not claim a second time.
63
+ * - When anything needed is missing (`HOME`, the HQ root, the target file, a
64
+ * writable claim directory, `cksum`), the entry runs without claiming. Any
65
+ * failure therefore leads to the hook running twice, never to no hook.
66
+ *
67
+ * Until the hq-core adapter claims too, the project hook never claims, this
68
+ * entry always wins, and both still run, as before. `commandWindows` is not
69
+ * wrapped, so Windows keeps running both.
70
+ */
71
+ export declare function codexUserHookClaimed(hqRoot: string, target: HookTarget, command: string): string;
72
+ /**
73
+ * The user-scope hooks.json entry: {@link codexHookEntry} behind
74
+ * {@link codexUserHookClaimed}. `commandWindows` is unchanged.
75
+ */
76
+ export declare function codexUserHookEntry(hqRoot: string, event: string, target?: HookTarget): Record<string, unknown>;
27
77
  /** The HQ hooks.json entry for one event and target. */
28
78
  export declare function codexHookEntry(hqRoot: string, event: string, target?: HookTarget): Record<string, unknown>;
29
79
  /**
30
80
  * Add one HQ hook group per event; idempotent. An HQ entry for the other
31
- * target (adapter vs the hqd shim) is swapped out. Foreign hooks are untouched.
81
+ * target (adapter vs the hqd shim) is swapped out, and so is an HQ entry
82
+ * written before the user-scope guard existed. Foreign hooks are untouched.
32
83
  */
33
84
  export declare function addCodexHooks(doc: Doc, hqRoot: string, target?: HookTarget): Doc;
34
85
  /** Remove only HQ adapter entries; drop containers this left empty. */