@indigoai-us/hq-cli 5.345.263 → 5.345.264

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 (62) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/contracts/host-canary-release.json +7 -7
  3. package/dist/command-catalog.generated.d.ts +37 -0
  4. package/dist/command-catalog.generated.js +46 -0
  5. package/dist/command-registration-plan.d.ts +6 -0
  6. package/dist/command-registration-plan.js +1 -0
  7. package/dist/commands/bot.d.ts +2 -0
  8. package/dist/commands/bot.js +4 -3
  9. package/dist/commands/sync-held.d.ts +11 -0
  10. package/dist/commands/sync-held.js +65 -1
  11. package/dist/commands/sync-telemetry-report.d.ts +5 -0
  12. package/dist/commands/sync-telemetry-report.js +239 -0
  13. package/dist/commands/sync-telemetry.d.ts +47 -0
  14. package/dist/commands/sync-telemetry.js +309 -0
  15. package/dist/index.js +107 -43
  16. package/dist/lib/bot/runnable.d.ts +17 -7
  17. package/dist/lib/bot/runnable.js +17 -10
  18. package/dist/lib/capacity/available.js +6 -2
  19. package/dist/lib/capacity/collectors/claude-oauth.d.ts +10 -256
  20. package/dist/lib/capacity/collectors/claude-oauth.js +19 -1078
  21. package/dist/lib/capacity/ledger.d.ts +12 -1
  22. package/dist/lib/capacity/ledger.js +15 -5
  23. package/dist/lib/capacity/route.d.ts +2 -0
  24. package/dist/lib/capacity/route.js +21 -4
  25. package/dist/lib/capacity/scheduler.js +2 -3
  26. package/dist/lib/capacity/sign-in.js +4 -4
  27. package/dist/lib/daemon/ops/skill-command.d.ts +2 -0
  28. package/dist/lib/daemon/ops/skill-command.js +7 -1
  29. package/dist/lib/doctor/checks/antigravity-wiring.d.ts +2 -1
  30. package/dist/lib/doctor/checks/antigravity-wiring.js +59 -3
  31. package/dist/lib/doctor/checks/hook-engine.d.ts +2 -0
  32. package/dist/lib/doctor/checks/hook-engine.js +100 -1
  33. package/dist/lib/doctor/checks/host-core.d.ts +1 -1
  34. package/dist/lib/doctor/checks/host-core.js +11 -4
  35. package/dist/lib/hook-engine/coverage.d.ts +19 -10
  36. package/dist/lib/hook-engine/coverage.js +103 -24
  37. package/dist/lib/hook-engine/native-precompact-compatibility.json +7 -1
  38. package/dist/lib/hook-engine/native-stop-compatibility.json +8 -4
  39. package/dist/lib/hook-engine/reconcile.js +49 -1
  40. package/dist/lib/install/antigravity-targets.d.ts +22 -0
  41. package/dist/lib/install/antigravity-targets.js +82 -0
  42. package/dist/main.js +8 -3
  43. package/dist/sentry-init.d.ts +7 -1
  44. package/dist/sentry-init.js +6 -2
  45. package/dist/sentry.d.ts +7 -0
  46. package/dist/sentry.js +16 -1
  47. package/dist/started-version.d.ts +13 -0
  48. package/dist/started-version.js +34 -0
  49. package/dist/startup-registration.d.ts +9 -9
  50. package/dist/startup-registration.js +42 -18
  51. package/dist/utils/client-health.d.ts +1 -0
  52. package/dist/utils/client-health.js +54 -11
  53. package/dist/utils/core-next-kill.d.ts +9 -0
  54. package/dist/utils/core-next-kill.js +22 -0
  55. package/dist/utils/host-registration.d.ts +4 -2
  56. package/dist/utils/host-registration.js +55 -7
  57. package/dist/utils/install-tree-recovery-markers.d.ts +45 -0
  58. package/dist/utils/install-tree-recovery-markers.js +70 -0
  59. package/dist/utils/install-tree-torn.d.ts +102 -5
  60. package/dist/utils/install-tree-torn.js +199 -2
  61. package/dist/utils/sentry-fingerprint.js +3 -0
  62. package/package.json +7 -7
package/dist/index.js CHANGED
@@ -3,6 +3,9 @@
3
3
  // Node 20+ API (e.g. util.styleText) or a newer native ABI is evaluated.
4
4
  import "./node-preflight.js";
5
5
  import "./node-network-compat-fast.js";
6
+ // Capture this process's starting CLI version synchronously at entry, before
7
+ // any lazy or fast-path import can load after a concurrent folder swap.
8
+ import { STARTED_CLI_VERSION } from "./started-version.js";
6
9
  import { isVersionRequest, wantsLabelledVersionReport } from "./version-request.js";
7
10
  // Dependency-light: a pure parser + table, no command graph. Safe to load on
8
11
  // every path (including --version) without reintroducing the heavy startup.
@@ -12,6 +15,7 @@ import { CLI_COMMAND_HELP_CATALOG_FLAG } from "./lib/cli-flag-keys.js";
12
15
  // heavy import (main.js) is lazy and reached only when a rejection fires.
13
16
  import { installProcessRejectionBoundary } from "./unhandled-rejection-boundary.js";
14
17
  import { daemonDirectReplacementEnv, installDaemonHelperLifetime } from "./utils/daemon-helper-lifetime.js";
18
+ import { consumeInstallTreeRecoveryMarkers, installTreeRecoveryEnv, } from "./utils/install-tree-recovery-markers.js";
15
19
  import { installFastPathStreamErrorHandlers } from "./utils/fast-stream-errors.js";
16
20
  import { installWindowsHideDefault } from "./utils/windows-hide-default.js";
17
21
  import { fileURLToPath } from "node:url";
@@ -19,7 +23,7 @@ import { parseMonitorCheckFastRequest } from "./lanes-monitor-check-argv.js";
19
23
  import { isLaneSessionStartFastRequest } from "./lanes-session-start-argv.js";
20
24
  import { writeLanesAliasDeprecationForArgv } from "./runners-alias.js";
21
25
  import { createRequire } from "node:module";
22
- import { lstatSync } from "node:fs";
26
+ import { lstatSync, realpathSync } from "node:fs";
23
27
  import { homedir } from "node:os";
24
28
  import { join } from "node:path";
25
29
  import { hookEnginePathArgs, runHookEnginePath } from "./hook-engine-path.js";
@@ -65,44 +69,72 @@ installFastPathStreamErrorHandlers();
65
69
  // markers before any command can start another hq process. Builtins only, so
66
70
  // the fast path still does not load the command graph.
67
71
  installDaemonHelperLifetime();
72
+ // Consume any one-shot torn-install recovery guard markers into process memory
73
+ // and clear them from process.env before any command or hook can spawn a child.
74
+ // Every recovery seam in this process still sees the guard via
75
+ // installTreeRecoveryEnv, while spawned descendants can recover on their own.
76
+ consumeInstallTreeRecoveryMarkers(process.env);
77
+ async function resolveEntryInstall() {
78
+ try {
79
+ const { packageRoot } = await import("./utils/hq-roots.js");
80
+ return { packageRoot: packageRoot() };
81
+ }
82
+ catch {
83
+ return { packageRoot: null };
84
+ }
85
+ }
68
86
  async function runMainWithInstallTreeRecovery() {
69
87
  let runCli;
70
- const { registerCommandsWithRecovery } = await import("./startup-registration.js");
71
88
  try {
72
- const result = await registerCommandsWithRecovery({
73
- register: async () => {
74
- runCli = (await import("./main.js")).runCli;
75
- },
76
- argv: process.argv,
77
- env: daemonDirectReplacementEnv(process.env),
78
- entryPath: fileURLToPath(import.meta.url),
79
- stderr: process.stderr,
80
- });
81
- if (result.reexecStatus !== undefined) {
82
- process.exitCode = result.reexecStatus;
83
- return;
89
+ runCli = (await import("./main.js")).runCli;
90
+ }
91
+ catch (initialError) {
92
+ // Keep the clean main startup path free of an extra packageRoot() walk;
93
+ // only a failed import of main.js pays for the recovery seam.
94
+ const [{ registerCommandsWithRecovery }, entryInstall] = await Promise.all([
95
+ import("./startup-registration.js"),
96
+ resolveEntryInstall(),
97
+ ]);
98
+ try {
99
+ const result = await registerCommandsWithRecovery({
100
+ register: async () => {
101
+ throw initialError;
102
+ },
103
+ argv: process.argv,
104
+ env: installTreeRecoveryEnv(daemonDirectReplacementEnv(process.env)),
105
+ entryPath: fileURLToPath(import.meta.url),
106
+ stderr: process.stderr,
107
+ deps: {
108
+ resolveInstall: () => entryInstall,
109
+ startedVersion: STARTED_CLI_VERSION,
110
+ },
111
+ });
112
+ if (result.reexecStatus !== undefined) {
113
+ process.exitCode = result.reexecStatus;
114
+ return;
115
+ }
84
116
  }
85
- if (!runCli)
86
- throw new Error("HQ CLI startup did not load its command runner");
87
- await runCli();
88
- }
89
- catch (error) {
90
- // main.js may be the module that is missing, so the global rejection
91
- // reporter cannot import its usual handler to print the recovery remedy.
92
- const installTree = await import("./utils/install-tree-torn.js");
93
- if (installTree.isInstallTreeTornError(error)) {
94
- process.stderr.write(`${installTree.installTreeTornStderrLine(error)}\n`);
95
- const sentry = await import("./sentry.js");
96
- sentry.initSentry();
97
- sentry.Sentry.captureException(error, installTree.installTreeTornCaptureContext(error));
98
- sentry.finishSentrySession();
99
- await sentry.Sentry.flush(2_000);
100
- sentry.closeSentryTransport();
101
- process.exitCode = 1;
102
- return;
117
+ catch (error) {
118
+ // main.js may be the module that is missing, so the global rejection
119
+ // reporter cannot import its usual handler to print the recovery remedy.
120
+ const installTree = await import("./utils/install-tree-torn.js");
121
+ if (installTree.isInstallTreeTornError(error)) {
122
+ process.stderr.write(`${installTree.installTreeTornStderrLine(error)}\n`);
123
+ const sentry = await import("./sentry.js");
124
+ sentry.initSentry();
125
+ sentry.Sentry.captureException(error, installTree.installTreeTornCaptureContext(error));
126
+ sentry.finishSentrySession();
127
+ await sentry.Sentry.flush(2_000);
128
+ sentry.closeSentryTransport();
129
+ process.exitCode = 1;
130
+ return;
131
+ }
132
+ throw error;
103
133
  }
104
- throw error;
105
134
  }
135
+ if (!runCli)
136
+ throw new Error("HQ CLI startup did not load its command runner");
137
+ await runCli();
106
138
  }
107
139
  /**
108
140
  * Fast-route imports happen before main.ts command registration, so the
@@ -116,24 +148,30 @@ const recoverFastImport = async (load, options = {}) => {
116
148
  try {
117
149
  return { module: await load() };
118
150
  }
119
- catch {
151
+ catch (initialError) {
120
152
  // Keep the clean hook path free of startup-registration's update-lock and
121
153
  // package-root imports. Only a failed import pays for the recovery seam.
122
- const { registerCommandsWithRecovery } = await import("./startup-registration.js");
123
- let module;
154
+ const [{ registerCommandsWithRecovery }, entryInstall] = await Promise.all([
155
+ import("./startup-registration.js"),
156
+ resolveEntryInstall(),
157
+ ]);
124
158
  try {
125
159
  const result = await registerCommandsWithRecovery({
126
160
  register: async () => {
127
- module = await load();
161
+ throw initialError;
128
162
  },
129
163
  argv: process.argv,
130
- env: daemonDirectReplacementEnv(process.env),
164
+ env: installTreeRecoveryEnv(daemonDirectReplacementEnv(process.env)),
131
165
  entryPath: fileURLToPath(import.meta.url),
132
166
  stderr: process.stderr,
133
167
  reexecOnSettled: options.reexecOnSettled,
168
+ deps: {
169
+ resolveInstall: () => entryInstall,
170
+ startedVersion: STARTED_CLI_VERSION,
171
+ },
134
172
  });
135
173
  return result.reexecStatus === undefined
136
- ? { module }
174
+ ? {}
137
175
  : { reexecStatus: result.reexecStatus };
138
176
  }
139
177
  catch (error) {
@@ -189,6 +227,21 @@ async function runMonitorCheckFastWithRecovery(request) {
189
227
  return;
190
228
  await runMainWithInstallTreeRecovery();
191
229
  }
230
+ async function runMcpServeWithRecovery() {
231
+ const gate = await recoverFastImport(() => import("./commands/mcp-serve-gate.js"));
232
+ if (gate.reexecStatus !== undefined) {
233
+ process.exitCode = gate.reexecStatus;
234
+ return;
235
+ }
236
+ if (!gate.module || gate.module.denyMcpServeWithoutTokenCache())
237
+ return;
238
+ const loaded = await recoverFastImport(() => import("./commands/mcp-serve.js"));
239
+ if (loaded.reexecStatus !== undefined) {
240
+ process.exitCode = loaded.reexecStatus;
241
+ return;
242
+ }
243
+ await loaded.module?.runMcpServe();
244
+ }
192
245
  /**
193
246
  * The SessionStart hook runs `hq runners session-start` on every new session
194
247
  * under a 2 s cap. Load only that command's module, not the command graph.
@@ -329,7 +382,7 @@ export function stableHealWouldRun(argv, env, platform, home = homedir()) {
329
382
  return false;
330
383
  if (args[0] === "daemon")
331
384
  return false;
332
- if (!/node_modules[/\\]@indigoai-us[/\\]hq-cli[/\\]/i.test(argv[1] ?? ""))
385
+ if (!isInstalledPackagePath(argv[1] ?? "") && !isInstalledPackagePath(resolvedEntryPath(argv[1])))
333
386
  return false;
334
387
  try {
335
388
  return lstatSync(join(home, ".hq", "bin", "hq-hook", "stable")).isSymbolicLink();
@@ -338,6 +391,19 @@ export function stableHealWouldRun(argv, env, platform, home = homedir()) {
338
391
  return false;
339
392
  }
340
393
  }
394
+ function isInstalledPackagePath(entry) {
395
+ return /node_modules[/\\]@indigoai-us[/\\]hq-cli[/\\]/i.test(entry);
396
+ }
397
+ function resolvedEntryPath(entry) {
398
+ if (!entry)
399
+ return "";
400
+ try {
401
+ return realpathSync(entry);
402
+ }
403
+ catch {
404
+ return entry;
405
+ }
406
+ }
341
407
  function dispatchInstallFollowupThenCommand(versionRequest, helpRequest, parsedFastCore, monitorRequest, secretsRequest, mcpRequest) {
342
408
  if (!versionRequest && !monitorRequest && !parsedFastCore && !secretsRequest && !mcpRequest
343
409
  && installFollowupWouldDoWork(process.argv, process.env, process.platform)) {
@@ -447,9 +513,7 @@ function dispatchCliCommand(versionRequest, helpRequest, parsedFastCore, monitor
447
513
  // Without a person token cache the server is denied before mcp-serve.js
448
514
  // and its hqd protocol, lease and preload imports load; same message and
449
515
  // exit code as runMcpServe gives.
450
- void import("./commands/mcp-serve-gate.js").then(({ denyMcpServeWithoutTokenCache }) => denyMcpServeWithoutTokenCache()
451
- ? undefined
452
- : import("./commands/mcp-serve.js").then(({ runMcpServe }) => runMcpServe()));
516
+ void runMcpServeWithRecovery();
453
517
  }
454
518
  else if (parsedFastCore) {
455
519
  // Hot relocated plumbing (`hq core hq-session`, `hq core checkpoint-stop-gate`)
@@ -16,11 +16,15 @@
16
16
  *
17
17
  * The rule here is the runtime's rule, asked before anything is written: the
18
18
  * bot must run on a computer (`computeMode: "local"`), and the kind this
19
- * installation would run it as — from the settings the cloud kept for it — must
20
- * be the kind the cloud record says it is. A company bot whose saved settings
21
- * do not say "company" (the case above: no saved settings at all, so the adopt
22
- * defaults to personal) is exactly the bot the runtime refuses, and it is now
23
- * refused here instead, before it costs anything.
19
+ * installation would run it as must be the kind the cloud record says it is.
20
+ *
21
+ * When the cloud kept no settings, adopt/restore take the kind from the
22
+ * record (`botKind`), not the personal default. That is what `hq bot run`
23
+ * then checks. The old personal default is what produced the dead LaunchAgent
24
+ * above; refusing every company bot with no saved settings then made
25
+ * `hq bot status` recommend `hq bot adopt` for a command that always refused.
26
+ * A saved `kind: "personal"` on a company record is still a real mismatch and
27
+ * is still refused.
24
28
  */
25
29
  import type { MyLocalBot } from "./api.js";
26
30
  /**
@@ -28,8 +32,9 @@ import type { MyLocalBot } from "./api.js";
28
32
  * the desktop app and the `--json` consumers switch on these values.
29
33
  *
30
34
  * cloud-compute the record runs in HQ Cloud, not on any computer
31
- * company-bot it acts as a company member, and nothing here can run it
32
- * as one — the runtime would refuse it at every start
35
+ * company-bot the record is a company bot, but the settings HQ kept
36
+ * would run it as a personal bot — the runtime would refuse
37
+ * it at every start
33
38
  * kind-mismatch anything else the runtime's identity preflight would refuse
34
39
  */
35
40
  export type BotNotRunnableReason = "cloud-compute" | "company-bot" | "kind-mismatch";
@@ -41,6 +46,11 @@ export interface RemoteBotRunnability {
41
46
  }
42
47
  /** The record fields the decision reads — nothing else is consulted. */
43
48
  export type RemoteBotRunnabilityInput = Pick<MyLocalBot, "botKind" | "computeMode" | "localConfig">;
49
+ /**
50
+ * Kind this installation would write into bot.json, then run. Missing saved
51
+ * settings follow the cloud record, not the personal restore default.
52
+ */
53
+ export declare function restoreKindForRecord(bot: RemoteBotRunnabilityInput): "personal" | "company";
44
54
  /**
45
55
  * The one answer restore, adopt and `list --remote` all use. `name` only
46
56
  * shapes the sentence; the decision itself reads the record.
@@ -16,20 +16,27 @@
16
16
  *
17
17
  * The rule here is the runtime's rule, asked before anything is written: the
18
18
  * bot must run on a computer (`computeMode: "local"`), and the kind this
19
- * installation would run it as — from the settings the cloud kept for it — must
20
- * be the kind the cloud record says it is. A company bot whose saved settings
21
- * do not say "company" (the case above: no saved settings at all, so the adopt
22
- * defaults to personal) is exactly the bot the runtime refuses, and it is now
23
- * refused here instead, before it costs anything.
19
+ * installation would run it as must be the kind the cloud record says it is.
20
+ *
21
+ * When the cloud kept no settings, adopt/restore take the kind from the
22
+ * record (`botKind`), not the personal default. That is what `hq bot run`
23
+ * then checks. The old personal default is what produced the dead LaunchAgent
24
+ * above; refusing every company bot with no saved settings then made
25
+ * `hq bot status` recommend `hq bot adopt` for a command that always refused.
26
+ * A saved `kind: "personal"` on a company record is still a real mismatch and
27
+ * is still refused.
24
28
  */
25
29
  import { effectiveBotKind } from "./config.js";
26
30
  import { parseBotLocalConfig } from "./local-config.js";
27
31
  import { BOT_RESTORE_DEFAULTS } from "./local-config.js";
28
- /** The kind this installation would run the bot as, from the settings the cloud kept. */
29
- function kindHere(bot) {
32
+ /**
33
+ * Kind this installation would write into bot.json, then run. Missing saved
34
+ * settings follow the cloud record, not the personal restore default.
35
+ */
36
+ export function restoreKindForRecord(bot) {
30
37
  const local = parseBotLocalConfig(bot.localConfig);
31
38
  return effectiveBotKind({
32
- kind: local?.kind ?? BOT_RESTORE_DEFAULTS.kind,
39
+ kind: local?.kind ?? (bot.botKind === "company" ? "company" : BOT_RESTORE_DEFAULTS.kind),
33
40
  ...(local?.workerId ? { workerId: local.workerId } : {}),
34
41
  });
35
42
  }
@@ -38,7 +45,7 @@ function sentence(reason, name) {
38
45
  if (reason === "cloud-compute")
39
46
  return `${subject} runs in the cloud, not on a computer, so there is nothing to bring back here.`;
40
47
  if (reason === "company-bot") {
41
- return `${subject} is a company bot: it runs in HQ Cloud for its company, not on this Mac, so there is nothing to bring back here.`;
48
+ return `${subject} is a company bot, but the settings HQ kept for it would run it as a personal bot, so this computer cannot start it.`;
42
49
  }
43
50
  return `${subject} is not set up as the kind of bot this computer can run, so there is nothing to bring back here.`;
44
51
  }
@@ -51,7 +58,7 @@ export function remoteBotRunnability(bot, name) {
51
58
  return { runnable: false, reason: "cloud-compute", message: sentence("cloud-compute", name) };
52
59
  }
53
60
  const record = bot.botKind === "company" ? "company" : "personal";
54
- const here = kindHere(bot);
61
+ const here = restoreKindForRecord(bot);
55
62
  if (record !== here) {
56
63
  const reason = record === "company" ? "company-bot" : "kind-mismatch";
57
64
  return { runnable: false, reason, message: sentence(reason, name) };
@@ -30,6 +30,8 @@ export const AVAILABLE_FILE_NAME = "available.json";
30
30
  export const FRESH_WAIT_MS = 3_000;
31
31
  export const STATUS_PROBE_TIMEOUT_MS = 2_500;
32
32
  export const COLLECT_MARK = ".available-collect.lock";
33
+ /** Reason for a signed-in account whose access token expired and has no reading since. */
34
+ const EXPIRED_UNREAD_REASON = "Sign-in expired, and it renews on next use. Usage has not been read since.";
33
35
  export const COLLECT_MARK_TTL_MS = 60_000;
34
36
  const FRESH_POLL_MS = 100;
35
37
  /**
@@ -168,7 +170,9 @@ function planRow(context, result) {
168
170
  leftPercent: null,
169
171
  backAt: null,
170
172
  reading: { ageSeconds: reading.ageSeconds, confidence: "unknown" },
171
- reason: "Signed in. Reading usage.",
173
+ reason: ranked.token_expired
174
+ ? EXPIRED_UNREAD_REASON
175
+ : "Signed in. Reading usage.",
172
176
  nextStep: { kind: "use", label: `Use ${name}` },
173
177
  };
174
178
  }
@@ -253,7 +257,7 @@ function planRow(context, result) {
253
257
  reason: reasonText === SKIP_STALE_READING
254
258
  ? "No usage reading in the last 24 hours."
255
259
  : account.authState === "expired"
256
- ? "Sign-in expired, and it renews on next use. Usage has not been read since."
260
+ ? EXPIRED_UNREAD_REASON
257
261
  : "Signed in. Reading usage.",
258
262
  nextStep: { kind: "use", label: `Use ${name}` },
259
263
  };