@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
@@ -37,6 +37,58 @@ export function isLongLivedHelperCommand(argv) {
37
37
  const command = commandArgv(argv);
38
38
  return LONG_LIVED_HELPER_COMMANDS.some((prefix) => prefix.length <= command.length && prefix.every((part, index) => command[index] === part));
39
39
  }
40
+ /**
41
+ * Short commands that a scheduler runs again and again: the daemon's scheduled
42
+ * units (src/lib/daemon/catalog.ts) and the outpost systemd timers. Each run is
43
+ * a fresh process. Matched on argv, not on an environment mark, so a run that
44
+ * a daemon started before this release still matches once the new CLI is on
45
+ * disk. Leading `--hq-root <path>` after `core` is skipped.
46
+ */
47
+ const SCHEDULED_SHORT_RUN_COMMANDS = [
48
+ ["daemon", "service", "self-update"],
49
+ ["daemon", "service", "fleet-bots-update"],
50
+ ["daemon", "service", "sync-core-reconcile"],
51
+ ["daemon", "service", "sync-core-selfcheck"],
52
+ ["daemon", "service", "sync-core-manifest"],
53
+ ["runners", "reconcile"],
54
+ ["lanes", "reconcile"],
55
+ ["lanes", "outpost-pruner", "run"],
56
+ ["index", "sync"],
57
+ ["core", "jobs", "reconcile"],
58
+ ];
59
+ function withoutCoreHqRoot(command) {
60
+ if (command[0] !== "core")
61
+ return command;
62
+ const rest = command.slice(1);
63
+ while (rest[0] === "--hq-root" && rest.length >= 2)
64
+ rest.splice(0, 2);
65
+ return ["core", ...rest];
66
+ }
67
+ /**
68
+ * True for a scheduled short run. Such a run still reports errors, but it does
69
+ * not start a release-health session, so a run with nothing to report opens no
70
+ * connection to Sentry. One session per run was one new ingest socket per run:
71
+ * a 15 s reconcile timer alone left thousands of sockets in TIME_WAIT a day.
72
+ */
73
+ export function isScheduledShortRunCommand(argv, options = {}) {
74
+ // A person running one of these by hand keeps the normal session. Units and
75
+ // timers run with no terminal on stdin or stdout.
76
+ if (options.interactive ?? isInteractiveTerminal())
77
+ return false;
78
+ const command = withoutCoreHqRoot(commandArgv(argv));
79
+ if (command.includes("--dry-run"))
80
+ return false;
81
+ return SCHEDULED_SHORT_RUN_COMMANDS.some((prefix) => {
82
+ if (prefix.length > command.length || !prefix.every((part, index) => command[index] === part))
83
+ return false;
84
+ // Exact subcommand: `lanes reconcile install` is a setup command, not the timer run.
85
+ const next = command[prefix.length];
86
+ return next === undefined || next.startsWith("-");
87
+ });
88
+ }
89
+ function isInteractiveTerminal() {
90
+ return process.stdin.isTTY === true || process.stdout.isTTY === true;
91
+ }
40
92
  /**
41
93
  * True when this process must not open the Sentry transport. `HQ_DAEMON_RESIDENT`
42
94
  * marks the long-running process the daemon started, including one whose
@@ -29,15 +29,17 @@
29
29
  * ../startup-registration.ts; version-gate.ts / self-update.ts / update-lock.ts
30
30
  * are only READ (their exported symbols), never modified.
31
31
  */
32
+ import { MISSING_REGISTRATION_PLAN_CODE } from "./registration-plan-error.js";
32
33
  /**
33
34
  * The loader-error `code`s recovery classifies. The first two mean "a module
34
35
  * could not be resolved"; `ENOENT` is the loader/resolver dialect where a module
35
36
  * was present at one step and gone at the next (HQ-CLI-1M esm-load open, HQ-CLI-20
36
37
  * cjs-load open, HQ-CLI-1S resolve-time lstat); `EVAL_THROW` is the synthetic code
37
38
  * for a module-EVALUATION throw that carries NO real `code` (HQ-CLI-1T) — see
38
- * {@link ModuleErrorDialect}.
39
+ * {@link ModuleErrorDialect}. `ERR_HQ_NO_REGISTRATION_PLAN` is hq-cli's own code
40
+ * for a catalog root the registration plan does not know (HQ-CLI-GM).
39
41
  */
40
- type ModuleNotFoundCode = "ERR_MODULE_NOT_FOUND" | "MODULE_NOT_FOUND" | "ENOENT" | "EVAL_THROW";
42
+ type ModuleNotFoundCode = "ERR_MODULE_NOT_FOUND" | "MODULE_NOT_FOUND" | "ENOENT" | "EVAL_THROW" | typeof MISSING_REGISTRATION_PLAN_CODE;
41
43
  /**
42
44
  * The closed set of resolution-failure dialects, verified on Node v22.23.1 and
43
45
  * the observed Node 24 synchronous ESM loader shape (the @sentry/node
@@ -75,10 +77,16 @@ type ModuleNotFoundCode = "ERR_MODULE_NOT_FOUND" | "MODULE_NOT_FOUND" | "ENOENT"
75
77
  * module and its top-level code threw a code-LESS TypeError /
76
78
  * ReferenceError / SyntaxError under Module._compile /
77
79
  * esm/module_job. The specifier is the bounded throw-site path.
78
- * - `unknown`: a module-not-found whose message did not parse; recovery
80
+ * - `missing-plan`: the startup command catalog named a root that the lazily
81
+ * imported registration plan has no entry for (HQ-CLI-GM): an
82
+ * update replaced the install between the two imports, so the
83
+ * catalog and the plan come from different releases. No file is
84
+ * missing, so the target is `unknown` and readiness rides the
85
+ * lock / retired-dir / quiet signals, like the loader races.
86
+ * - `unknown`: a module-not-found whose message did not parse; recovery
79
87
  * still waits on the lock / retired-dir / quiet signals.
80
88
  */
81
- type ModuleErrorDialect = "esm-path" | "esm-package" | "cjs-path" | "cjs-package" | "cjs-relative" | "esm-enoent" | "cjs-enoent" | "resolve-enoent" | "eval-throw" | "unknown";
89
+ type ModuleErrorDialect = "esm-path" | "esm-package" | "cjs-path" | "cjs-package" | "cjs-relative" | "esm-enoent" | "cjs-enoent" | "resolve-enoent" | "eval-throw" | "missing-plan" | "unknown";
82
90
  /**
83
91
  * The missing thing, re-resolvable by the readiness probe:
84
92
  * - `path`: an absolute filesystem path (a `.js` file, or a package dir).
@@ -142,6 +150,16 @@ export declare function packageNameOf(specifier: string): string;
142
150
  * `null` so it is rethrown to the existing boundary unchanged.
143
151
  */
144
152
  export declare function classifyModuleNotFound(err: unknown): ClassifiedModuleError | null;
153
+ /**
154
+ * Classify a lazy root's "No registration plan" failure (HQ-CLI-GM) as a torn
155
+ * install. Matched by hq-cli's own error `code` plus a string `root`, never by
156
+ * message text, so it holds even when the error class came from a different
157
+ * install than this module. Like the loader-race dialects it needs no writer
158
+ * gate: the catalog and the plan disagreeing is itself the sign that the tree
159
+ * changed under this process. The parity test guarantees a single release has a
160
+ * plan for every catalog root, so a settled re-exec resolves it.
161
+ */
162
+ export declare function classifyMissingRegistrationPlan(err: unknown): ClassifiedModuleError | null;
145
163
  /** The structural fingerprint of a module-EVALUATION throw (HQ-CLI-1T). */
146
164
  export interface EvalThrowShape {
147
165
  /** The thrown error's `name`, in {@link EVAL_THROW_NAMES}. */
@@ -37,6 +37,7 @@ import { CLI_NAME } from "../cli-version.js";
37
37
  import { boundedDiagnosticValue } from "./package-root-diagnostics.js";
38
38
  import { isLockStale, UPDATE_LOCK_STALE_MS } from "./update-lock.js";
39
39
  import { HQ_CLI_NPM_UPDATE_COMMAND } from "./npm-install-flags.js";
40
+ import { MISSING_REGISTRATION_PLAN_CODE } from "./registration-plan-error.js";
40
41
  /** Byte caps for the (hq-derived, path-shaped) diagnostic strings. */
41
42
  const SPECIFIER_BYTES = 256;
42
43
  const IMPORTER_BYTES = 256;
@@ -410,6 +411,29 @@ export function classifyModuleNotFound(err) {
410
411
  // the lock / retired-dir / quiet signals only.
411
412
  return { code, dialect: "unknown", specifier: "", importer: "", target: { kind: "unknown" } };
412
413
  }
414
+ /**
415
+ * Classify a lazy root's "No registration plan" failure (HQ-CLI-GM) as a torn
416
+ * install. Matched by hq-cli's own error `code` plus a string `root`, never by
417
+ * message text, so it holds even when the error class came from a different
418
+ * install than this module. Like the loader-race dialects it needs no writer
419
+ * gate: the catalog and the plan disagreeing is itself the sign that the tree
420
+ * changed under this process. The parity test guarantees a single release has a
421
+ * plan for every catalog root, so a settled re-exec resolves it.
422
+ */
423
+ export function classifyMissingRegistrationPlan(err) {
424
+ if (err === null || typeof err !== "object")
425
+ return null;
426
+ const record = err;
427
+ if (record.code !== MISSING_REGISTRATION_PLAN_CODE || typeof record.root !== "string")
428
+ return null;
429
+ return {
430
+ code: MISSING_REGISTRATION_PLAN_CODE,
431
+ dialect: "missing-plan",
432
+ specifier: `registration plan for '${record.root}'`,
433
+ importer: "",
434
+ target: { kind: "unknown" },
435
+ };
436
+ }
413
437
  /**
414
438
  * Detect a module-EVALUATION throw STRUCTURALLY, WITHOUT the node_modules
415
439
  * boundary gate: an error with NO `code`, a `name` in the closed set
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The error a lazy root throws when the registration plan has no entry for it
3
+ * (Sentry HQ-CLI-GM, feedback_05f34e58).
4
+ *
5
+ * `lazy-commands.ts` reads the command catalog at startup and imports the
6
+ * registration plan (`register-all.js`) later, when it registers the requested
7
+ * root. When hq-cli updates itself between those two steps, the old catalog can
8
+ * name a root that the new plan does not have. That is a torn install, so the
9
+ * torn-install recovery in startup-registration.ts classifies this error by its
10
+ * `code` (see classifyMissingRegistrationPlan) and re-execs the settled install.
11
+ *
12
+ * Kept in its own dependency-free module so the startup path that throws it does
13
+ * not import the torn-install classifier.
14
+ */
15
+ export declare const MISSING_REGISTRATION_PLAN_CODE = "ERR_HQ_NO_REGISTRATION_PLAN";
16
+ export declare class MissingRegistrationPlanError extends Error {
17
+ readonly code = "ERR_HQ_NO_REGISTRATION_PLAN";
18
+ /** The catalog root name that had no plan (hq-derived, never argv). */
19
+ readonly root: string;
20
+ constructor(root: string);
21
+ }
22
+ //# sourceMappingURL=registration-plan-error.d.ts.map
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The error a lazy root throws when the registration plan has no entry for it
3
+ * (Sentry HQ-CLI-GM, feedback_05f34e58).
4
+ *
5
+ * `lazy-commands.ts` reads the command catalog at startup and imports the
6
+ * registration plan (`register-all.js`) later, when it registers the requested
7
+ * root. When hq-cli updates itself between those two steps, the old catalog can
8
+ * name a root that the new plan does not have. That is a torn install, so the
9
+ * torn-install recovery in startup-registration.ts classifies this error by its
10
+ * `code` (see classifyMissingRegistrationPlan) and re-execs the settled install.
11
+ *
12
+ * Kept in its own dependency-free module so the startup path that throws it does
13
+ * not import the torn-install classifier.
14
+ */
15
+ export const MISSING_REGISTRATION_PLAN_CODE = "ERR_HQ_NO_REGISTRATION_PLAN";
16
+ export class MissingRegistrationPlanError extends Error {
17
+ code = MISSING_REGISTRATION_PLAN_CODE;
18
+ /** The catalog root name that had no plan (hq-derived, never argv). */
19
+ root;
20
+ constructor(root) {
21
+ super(`No registration plan for '${root}'`);
22
+ this.name = "MissingRegistrationPlanError";
23
+ this.root = root;
24
+ Object.setPrototypeOf(this, new.target.prototype);
25
+ }
26
+ }
27
+ //# sourceMappingURL=registration-plan-error.js.map
@@ -47,4 +47,67 @@ export declare function removeEntryNoFollow(p: string, opts?: {
47
47
  * Returns false when nothing exists at `linkPath`.
48
48
  */
49
49
  export declare function removeLinkOnly(linkPath: string): boolean;
50
+ /** Marker file written into a folder HQ copied in place of a release link. */
51
+ export declare const LINK_COPY_MARKER = ".hq-link-copy";
52
+ /** What a link copy's marker records. */
53
+ export interface LinkCopyMarker {
54
+ /** The absolute folder the copy was made from (the link's target). */
55
+ source: string;
56
+ /** The entry name HQ gave the copy. */
57
+ name: string;
58
+ /** {@link linkCopyTreeHash} of the copy when HQ wrote it. */
59
+ hash: string;
60
+ /** Why HQ copied instead of linking. */
61
+ reason?: string;
62
+ }
63
+ export interface LinkCopyResult {
64
+ /** Hash recorded in the marker. */
65
+ hash: string;
66
+ /** Paths (relative to the source) of links that resolve outside `within`; not copied. */
67
+ skippedOutside: string[];
68
+ }
69
+ export interface LinkCopyOptions {
70
+ /** Real path of the HQ root. A link inside the source is followed only when it resolves under it. */
71
+ within: string;
72
+ /** Why HQ copied instead of linking; stored in the marker. */
73
+ reason?: string;
74
+ }
75
+ /**
76
+ * A sha256 over the names and contents of what a link copy of `dir` holds,
77
+ * read with the same rules as the copy (see {@link walkLinkCopyTree}). Hashing
78
+ * the copy and hashing its source give the same value while they match.
79
+ */
80
+ export declare function linkCopyTreeHash(dir: string, within: string): string;
81
+ /**
82
+ * The hash of an HQ link copy as it sits on disk. HQ copies never contain
83
+ * links (links in the source are copied as real files and folders, or left
84
+ * out), so any link inside the copy is something the user added: it is hashed
85
+ * by its text and never followed. For a copy without links this equals
86
+ * {@link linkCopyTreeHash} of the copy.
87
+ */
88
+ export declare function linkCopyContentHash(dir: string, within: string): string;
89
+ /**
90
+ * The marker of a folder HQ copied in place of a release link, or null when
91
+ * `p` is not a real folder carrying a valid marker.
92
+ */
93
+ export declare function readLinkCopyMarker(p: string): LinkCopyMarker | null;
94
+ /**
95
+ * Copy the folder `source` to the new path `dest` and mark it as a link copy.
96
+ * `source` must resolve under `opts.within` (the HQ root). A link inside the
97
+ * source is followed only when it resolves under `opts.within`; one that
98
+ * resolves outside is left out and listed in `skippedOutside`. A partial copy
99
+ * is removed when the copy fails.
100
+ */
101
+ export declare function copyFolderForLink(source: string, dest: string, opts: LinkCopyOptions): LinkCopyResult;
102
+ /**
103
+ * Replace the marked link copy at `dest` with a fresh copy of `source`. The
104
+ * new copy is made beside it first, so a failed copy leaves the old copy in
105
+ * place. The old copy is then renamed aside, the new one renamed into place,
106
+ * and only then is the old one deleted; if the second rename fails the old
107
+ * copy is renamed back. A failure at any step leaves a complete, marked copy
108
+ * at `dest`. Refuses a folder without a link-copy marker. The caller decides
109
+ * whether the old copy holds user changes (compare
110
+ * {@link linkCopyContentHash} with the marker's hash) before calling this.
111
+ */
112
+ export declare function refreshLinkCopy(source: string, dest: string, opts: LinkCopyOptions): LinkCopyResult;
50
113
  //# sourceMappingURL=safe-link.d.ts.map
@@ -306,4 +306,250 @@ function assertStillLink(linkPath) {
306
306
  throw new Error(`Refusing to remove ${linkPath}: it is no longer a link.`);
307
307
  }
308
308
  }
309
+ // ---------------------------------------------------------------------------
310
+ // Copies made in place of a release-shipped directory link
311
+ //
312
+ // On some Windows machines a junction a standard user creates is written with
313
+ // the right target but cannot be listed through by anyone (Beaker, Windows 11
314
+ // build 26300, hq-core-staging PR 1883). HQ then removes the junction and
315
+ // copies the target folder in its place. The copy carries a `.hq-link-copy`
316
+ // marker with the same fields as a skill copy's marker (source, entry name,
317
+ // hash of the copied files), so `hq doctor` can tell an HQ copy from a folder
318
+ // the user made, tell whether the user changed it, and tell whether its source
319
+ // moved on since the copy was made.
320
+ // ---------------------------------------------------------------------------
321
+ /** Marker file written into a folder HQ copied in place of a release link. */
322
+ export const LINK_COPY_MARKER = '.hq-link-copy';
323
+ function isUnderReal(rootReal, candidate) {
324
+ const base = rootReal.endsWith(path.sep) ? rootReal.slice(0, -1) : rootReal;
325
+ return candidate === base || candidate.startsWith(base + path.sep);
326
+ }
327
+ /**
328
+ * Walk `root` in sorted order the way a link copy reads it: links are followed
329
+ * only when their real path is under `within`, a link back to a folder on the
330
+ * current path (a loop) is skipped, and the top-level marker is ignored.
331
+ * Entries that are neither files nor folders are skipped.
332
+ */
333
+ function walkLinkCopyTree(root, within, visit, linksAsEntries = false) {
334
+ const ancestors = new Set([fs.realpathSync.native(root)]);
335
+ const walk = (rel) => {
336
+ const abs = rel === '' ? root : path.join(root, rel);
337
+ for (const name of fs.readdirSync(abs).sort()) {
338
+ if (rel === '' && name === LINK_COPY_MARKER)
339
+ continue;
340
+ const childRel = rel === '' ? name : `${rel}/${name}`;
341
+ const child = path.join(abs, name);
342
+ const lst = fs.lstatSync(child);
343
+ let st = lst;
344
+ if (lst.isSymbolicLink() && linksAsEntries) {
345
+ visit({ kind: 'link', rel: childRel, abs: child });
346
+ continue;
347
+ }
348
+ if (lst.isSymbolicLink()) {
349
+ let real;
350
+ try {
351
+ real = fs.realpathSync.native(child);
352
+ }
353
+ catch {
354
+ continue; // A dangling link has nothing to copy.
355
+ }
356
+ if (!isUnderReal(within, real)) {
357
+ visit({ kind: 'outside', rel: childRel, abs: child });
358
+ continue;
359
+ }
360
+ st = fs.statSync(child);
361
+ }
362
+ if (st.isDirectory()) {
363
+ const real = fs.realpathSync.native(child);
364
+ if (ancestors.has(real))
365
+ continue;
366
+ visit({ kind: 'dir', rel: childRel, abs: child });
367
+ ancestors.add(real);
368
+ try {
369
+ walk(childRel);
370
+ }
371
+ finally {
372
+ ancestors.delete(real);
373
+ }
374
+ }
375
+ else if (st.isFile()) {
376
+ visit({ kind: 'file', rel: childRel, abs: child });
377
+ }
378
+ }
379
+ };
380
+ walk('');
381
+ }
382
+ /**
383
+ * A sha256 over the names and contents of what a link copy of `dir` holds,
384
+ * read with the same rules as the copy (see {@link walkLinkCopyTree}). Hashing
385
+ * the copy and hashing its source give the same value while they match.
386
+ */
387
+ export function linkCopyTreeHash(dir, within) {
388
+ const hash = crypto.createHash('sha256');
389
+ walkLinkCopyTree(dir, within, (entry) => {
390
+ if (entry.kind === 'dir')
391
+ hash.update(`d:${entry.rel}\0`);
392
+ else if (entry.kind === 'file') {
393
+ const content = crypto.createHash('sha256').update(fs.readFileSync(entry.abs)).digest('hex');
394
+ hash.update(`f:${entry.rel}\0${content}\0`);
395
+ }
396
+ });
397
+ return hash.digest('hex');
398
+ }
399
+ /**
400
+ * The hash of an HQ link copy as it sits on disk. HQ copies never contain
401
+ * links (links in the source are copied as real files and folders, or left
402
+ * out), so any link inside the copy is something the user added: it is hashed
403
+ * by its text and never followed. For a copy without links this equals
404
+ * {@link linkCopyTreeHash} of the copy.
405
+ */
406
+ export function linkCopyContentHash(dir, within) {
407
+ const hash = crypto.createHash('sha256');
408
+ walkLinkCopyTree(dir, within, (entry) => {
409
+ if (entry.kind === 'dir')
410
+ hash.update(`d:${entry.rel}\0`);
411
+ else if (entry.kind === 'file') {
412
+ const content = crypto.createHash('sha256').update(fs.readFileSync(entry.abs)).digest('hex');
413
+ hash.update(`f:${entry.rel}\0${content}\0`);
414
+ }
415
+ else if (entry.kind === 'link') {
416
+ hash.update(`l:${entry.rel}\0${fs.readlinkSync(entry.abs)}\0`);
417
+ }
418
+ }, true);
419
+ return hash.digest('hex');
420
+ }
421
+ /**
422
+ * The marker of a folder HQ copied in place of a release link, or null when
423
+ * `p` is not a real folder carrying a valid marker.
424
+ */
425
+ export function readLinkCopyMarker(p) {
426
+ try {
427
+ if (!fs.lstatSync(p).isDirectory())
428
+ return null;
429
+ const parsed = JSON.parse(fs.readFileSync(path.join(p, LINK_COPY_MARKER), 'utf-8'));
430
+ if (typeof parsed.source !== 'string' || parsed.source.length === 0)
431
+ return null;
432
+ if (typeof parsed.name !== 'string' || typeof parsed.hash !== 'string')
433
+ return null;
434
+ return {
435
+ source: parsed.source,
436
+ name: parsed.name,
437
+ hash: parsed.hash,
438
+ reason: typeof parsed.reason === 'string' ? parsed.reason : undefined,
439
+ };
440
+ }
441
+ catch {
442
+ return null;
443
+ }
444
+ }
445
+ function copyIntoNewFolder(source, dest, opts, name) {
446
+ const sourceReal = fs.realpathSync.native(source);
447
+ if (!isUnderReal(opts.within, sourceReal)) {
448
+ throw new Error(`Refusing to copy ${source}: it resolves outside ${opts.within}.`);
449
+ }
450
+ const skippedOutside = [];
451
+ let created = false;
452
+ try {
453
+ fs.mkdirSync(dest);
454
+ created = true;
455
+ walkLinkCopyTree(source, opts.within, (entry) => {
456
+ const to = path.join(dest, ...entry.rel.split('/'));
457
+ if (entry.kind === 'outside')
458
+ skippedOutside.push(entry.rel);
459
+ else if (entry.kind === 'dir')
460
+ fs.mkdirSync(to);
461
+ else if (entry.kind === 'file')
462
+ fs.copyFileSync(entry.abs, to, fs.constants.COPYFILE_EXCL);
463
+ });
464
+ const hash = linkCopyContentHash(dest, opts.within);
465
+ const marker = { source, name, hash, ...(opts.reason ? { reason: opts.reason } : {}) };
466
+ fs.writeFileSync(path.join(dest, LINK_COPY_MARKER), `${JSON.stringify(marker)}\n`);
467
+ return { hash, skippedOutside };
468
+ }
469
+ catch (err) {
470
+ // Remove the partial copy, but only when this call created the folder.
471
+ if (created) {
472
+ try {
473
+ removeTreeNoFollow(dest);
474
+ }
475
+ catch { /* report the copy error */ }
476
+ }
477
+ throw err;
478
+ }
479
+ }
480
+ /**
481
+ * Copy the folder `source` to the new path `dest` and mark it as a link copy.
482
+ * `source` must resolve under `opts.within` (the HQ root). A link inside the
483
+ * source is followed only when it resolves under `opts.within`; one that
484
+ * resolves outside is left out and listed in `skippedOutside`. A partial copy
485
+ * is removed when the copy fails.
486
+ */
487
+ export function copyFolderForLink(source, dest, opts) {
488
+ return copyIntoNewFolder(source, dest, opts, path.basename(dest));
489
+ }
490
+ /**
491
+ * Replace the marked link copy at `dest` with a fresh copy of `source`. The
492
+ * new copy is made beside it first, so a failed copy leaves the old copy in
493
+ * place. The old copy is then renamed aside, the new one renamed into place,
494
+ * and only then is the old one deleted; if the second rename fails the old
495
+ * copy is renamed back. A failure at any step leaves a complete, marked copy
496
+ * at `dest`. Refuses a folder without a link-copy marker. The caller decides
497
+ * whether the old copy holds user changes (compare
498
+ * {@link linkCopyContentHash} with the marker's hash) before calling this.
499
+ */
500
+ export function refreshLinkCopy(source, dest, opts) {
501
+ if (readLinkCopyMarker(dest) === null) {
502
+ throw new Error(`Refusing to refresh ${dest}: it is not a copy made by HQ.`);
503
+ }
504
+ // Fixed names reserved for HQ, so a leftover from an interrupted run is
505
+ // found and cleared by the next one.
506
+ const staging = `${dest}.hq-link-refresh`;
507
+ const old = `${dest}.hq-link-old`;
508
+ for (const leftover of [staging, old]) {
509
+ if (fs.existsSync(leftover) || isLinkPath(leftover))
510
+ removeEntryNoFollow(leftover);
511
+ }
512
+ const result = copyIntoNewFolder(source, staging, opts, path.basename(dest));
513
+ try {
514
+ fs.renameSync(dest, old);
515
+ }
516
+ catch (err) {
517
+ try {
518
+ removeTreeNoFollow(staging);
519
+ }
520
+ catch { /* report the swap error */ }
521
+ throw err;
522
+ }
523
+ try {
524
+ fs.renameSync(staging, dest);
525
+ }
526
+ catch (err) {
527
+ try {
528
+ fs.renameSync(old, dest);
529
+ }
530
+ catch {
531
+ /* the old copy stays at `old`; report the swap error */
532
+ }
533
+ try {
534
+ removeTreeNoFollow(staging);
535
+ }
536
+ catch { /* report the swap error */ }
537
+ throw err;
538
+ }
539
+ try {
540
+ removeTreeNoFollow(old);
541
+ }
542
+ catch {
543
+ // The fresh copy is in place; the next refresh clears the leftover.
544
+ }
545
+ return result;
546
+ }
547
+ function isLinkPath(p) {
548
+ try {
549
+ return fs.lstatSync(p).isSymbolicLink();
550
+ }
551
+ catch {
552
+ return false;
553
+ }
554
+ }
309
555
  //# sourceMappingURL=safe-link.js.map
@@ -174,6 +174,7 @@ const INSTALL_TREE_PHASES = new Set([
174
174
  "esm-package",
175
175
  "esm-path",
176
176
  "eval-throw",
177
+ "missing-plan",
177
178
  "resolve-enoent",
178
179
  "unknown",
179
180
  ]);
@@ -207,6 +208,8 @@ function installTreePhaseTag(value) {
207
208
  return "loader_read";
208
209
  if (value === "eval-throw")
209
210
  return "module_evaluation";
211
+ if (value === "missing-plan")
212
+ return "registration_plan";
210
213
  if (value === "esm-package" || value === "cjs-package")
211
214
  return "package_resolution";
212
215
  if (value === "unknown")
@@ -0,0 +1,28 @@
1
+ /**
2
+ * One shared keep-alive agent for the Sentry transport.
3
+ *
4
+ * `@sentry/node-core`'s default transport builds an agent with
5
+ * `keepAlive: false` and `maxSockets: 30`, so every envelope is a new TCP
6
+ * connection that this process closes, leaving a TIME_WAIT behind. This
7
+ * module reuses one connection per process and caps it at a few sockets.
8
+ *
9
+ * No static network import: fast native entries reach the Sentry setup, and
10
+ * their closure guard (node-network-compat-closure.test.ts) rejects one. The
11
+ * built-in modules are loaded on the first request.
12
+ */
13
+ import type { Agent, ClientRequest, IncomingMessage, RequestOptions } from "node:http";
14
+ type Protocol = "http:" | "https:";
15
+ /** At most this many sockets to Sentry ingest at once, per process. */
16
+ export declare const SENTRY_MAX_SOCKETS = 2;
17
+ /** An idle or stalled Sentry socket is closed after this long. */
18
+ export declare const SENTRY_SOCKET_TIMEOUT_MS = 2000;
19
+ /** The shared agent for one protocol, created on first use. */
20
+ export declare function sentryTransportAgent(protocol?: Protocol): Agent;
21
+ /** `transportOptions.httpModule` for the Sentry Node transport. */
22
+ export declare const sentryHttpModule: {
23
+ request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
24
+ };
25
+ /** Close the shared sockets at the end of a run. A later send opens a new agent. */
26
+ export declare function closeSentryTransportAgents(): void;
27
+ export {};
28
+ //# sourceMappingURL=sentry-keepalive-transport.d.ts.map
@@ -0,0 +1,70 @@
1
+ /**
2
+ * One shared keep-alive agent for the Sentry transport.
3
+ *
4
+ * `@sentry/node-core`'s default transport builds an agent with
5
+ * `keepAlive: false` and `maxSockets: 30`, so every envelope is a new TCP
6
+ * connection that this process closes, leaving a TIME_WAIT behind. This
7
+ * module reuses one connection per process and caps it at a few sockets.
8
+ *
9
+ * No static network import: fast native entries reach the Sentry setup, and
10
+ * their closure guard (node-network-compat-closure.test.ts) rejects one. The
11
+ * built-in modules are loaded on the first request.
12
+ */
13
+ import { createRequire } from "node:module";
14
+ const requireBuiltin = createRequire(import.meta.url);
15
+ /** At most this many sockets to Sentry ingest at once, per process. */
16
+ export const SENTRY_MAX_SOCKETS = 2;
17
+ /** An idle or stalled Sentry socket is closed after this long. */
18
+ export const SENTRY_SOCKET_TIMEOUT_MS = 2_000;
19
+ const agents = new Map();
20
+ function builtin(protocol) {
21
+ return requireBuiltin(protocol === "http:" ? "node:http" : "node:https");
22
+ }
23
+ /** The shared agent for one protocol, created on first use. */
24
+ export function sentryTransportAgent(protocol = "https:") {
25
+ let agent = agents.get(protocol);
26
+ if (!agent) {
27
+ agent = new (builtin(protocol).Agent)({
28
+ keepAlive: true,
29
+ keepAliveMsecs: 1_000,
30
+ maxSockets: SENTRY_MAX_SOCKETS,
31
+ maxFreeSockets: 1,
32
+ timeout: SENTRY_SOCKET_TIMEOUT_MS,
33
+ });
34
+ agents.set(protocol, agent);
35
+ }
36
+ return agent;
37
+ }
38
+ /**
39
+ * The SDK passes its own plain agent with every request. A proxy agent is a
40
+ * subclass; keep it so proxied installs still reach Sentry.
41
+ */
42
+ function isSdkPlainAgent(agent) {
43
+ if (agent === undefined || agent === null)
44
+ return true;
45
+ if (typeof agent !== "object")
46
+ return false;
47
+ const ctor = agent.constructor;
48
+ return ctor === builtin("http:").Agent || ctor === builtin("https:").Agent;
49
+ }
50
+ /** `transportOptions.httpModule` for the Sentry Node transport. */
51
+ export const sentryHttpModule = {
52
+ request(options, callback) {
53
+ if (typeof options === "string" || options instanceof URL) {
54
+ const protocol = new URL(String(options)).protocol === "http:" ? "http:" : "https:";
55
+ return builtin(protocol).request(options, { agent: sentryTransportAgent(protocol) }, callback);
56
+ }
57
+ const protocol = options.protocol === "http:" ? "http:" : "https:";
58
+ const request = isSdkPlainAgent(options.agent)
59
+ ? { ...options, agent: sentryTransportAgent(protocol) }
60
+ : options;
61
+ return builtin(protocol).request(request, callback);
62
+ },
63
+ };
64
+ /** Close the shared sockets at the end of a run. A later send opens a new agent. */
65
+ export function closeSentryTransportAgents() {
66
+ for (const agent of agents.values())
67
+ agent.destroy();
68
+ agents.clear();
69
+ }
70
+ //# sourceMappingURL=sentry-keepalive-transport.js.map