@indigoai-us/hq-cli 5.345.203 → 5.345.205

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 (95) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/command-catalog.generated.d.ts +51 -1
  3. package/dist/command-catalog.generated.js +63 -1
  4. package/dist/commands/bot.d.ts +67 -0
  5. package/dist/commands/bot.js +308 -41
  6. package/dist/commands/cloud-off-gate.d.ts +9 -2
  7. package/dist/commands/cloud-off-gate.js +49 -13
  8. package/dist/commands/cloud.d.ts +14 -2
  9. package/dist/commands/cloud.js +25 -16
  10. package/dist/commands/feedback.d.ts +26 -0
  11. package/dist/commands/feedback.js +91 -5
  12. package/dist/commands/install-global.d.ts +3 -1
  13. package/dist/commands/install-global.js +15 -4
  14. package/dist/commands/lanes.d.ts +2 -0
  15. package/dist/commands/lanes.js +9 -3
  16. package/dist/commands/mcp-serve.d.ts +5 -2
  17. package/dist/commands/mcp-serve.js +45 -3
  18. package/dist/commands/mcp-status.d.ts +3 -1
  19. package/dist/commands/mcp-status.js +24 -14
  20. package/dist/commands/secrets.js +11 -1
  21. package/dist/commands/sources.js +10 -15
  22. package/dist/commands/sync-core.js +2 -0
  23. package/dist/index.js +7 -2
  24. package/dist/lib/bot/claude-login.d.ts +7 -1
  25. package/dist/lib/bot/claude-login.js +56 -16
  26. package/dist/lib/bot/executive-settings.d.ts +13 -0
  27. package/dist/lib/bot/executive-settings.js +20 -17
  28. package/dist/lib/bot/executive-sync.d.ts +12 -0
  29. package/dist/lib/bot/executive-sync.js +36 -0
  30. package/dist/lib/bot/executive.d.ts +51 -0
  31. package/dist/lib/bot/executive.js +144 -0
  32. package/dist/lib/bot/fleet-cron.d.ts +7 -0
  33. package/dist/lib/bot/fleet-cron.js +2 -1
  34. package/dist/lib/bot/jobs/executive-handover.d.ts +6 -0
  35. package/dist/lib/bot/jobs/executive-handover.js +33 -0
  36. package/dist/lib/bot/jobs/hot-signal-sync.d.ts +22 -0
  37. package/dist/lib/bot/jobs/hot-signal-sync.js +47 -0
  38. package/dist/lib/bot/listed-running.d.ts +16 -0
  39. package/dist/lib/bot/listed-running.js +16 -0
  40. package/dist/lib/bot/pending-switch.d.ts +51 -0
  41. package/dist/lib/bot/pending-switch.js +103 -0
  42. package/dist/lib/bot/run.js +27 -3
  43. package/dist/lib/bot/runtime-failure.js +19 -3
  44. package/dist/lib/bot/status.d.ts +20 -0
  45. package/dist/lib/capacity/collectors/claude-oauth.d.ts +53 -5
  46. package/dist/lib/capacity/collectors/claude-oauth.js +251 -71
  47. package/dist/lib/company-skill-wrapper.js +29 -0
  48. package/dist/lib/daemon/catalog.js +2 -2
  49. package/dist/lib/daemon/hq-child-bootstrap.mjs +197 -2
  50. package/dist/lib/daemon/resolver.d.ts +14 -0
  51. package/dist/lib/daemon/resolver.js +96 -0
  52. package/dist/lib/daemon/run.d.ts +11 -0
  53. package/dist/lib/daemon/run.js +60 -8
  54. package/dist/lib/daemon/server.d.ts +2 -9
  55. package/dist/lib/daemon/server.js +3 -89
  56. package/dist/lib/doctor/checks/claude-wiring.js +41 -0
  57. package/dist/lib/doctor/checks/sync-health.d.ts +8 -0
  58. package/dist/lib/doctor/checks/sync-health.js +34 -2
  59. package/dist/lib/doctor/duplicate-hook-registrations.d.ts +34 -0
  60. package/dist/lib/doctor/duplicate-hook-registrations.js +130 -0
  61. package/dist/lib/doctor/fix/apply.js +79 -3
  62. package/dist/lib/doctor/fix/remediation.d.ts +1 -1
  63. package/dist/lib/doctor/fix/remediation.js +15 -0
  64. package/dist/lib/doctor/types.d.ts +1 -1
  65. package/dist/lib/doctor/types.js +1 -0
  66. package/dist/lib/lanes/config.js +13 -0
  67. package/dist/lib/lanes/throttle.d.ts +12 -1
  68. package/dist/lib/lanes/throttle.js +27 -8
  69. package/dist/lib/lanes/types.d.ts +5 -0
  70. package/dist/lib/mcp/local-native-tools.d.ts +40 -0
  71. package/dist/lib/mcp/local-native-tools.js +111 -0
  72. package/dist/lib/mcp/tools.d.ts +2 -0
  73. package/dist/lib/mcp/tools.js +2 -0
  74. package/dist/lib/signals/steps/extract.d.ts +111 -5
  75. package/dist/lib/signals/steps/extract.js +524 -55
  76. package/dist/lib/sync-core/engine-home.d.ts +2 -1
  77. package/dist/lib/sync-core/engine-home.js +25 -8
  78. package/dist/lib/sync-core/reconcile.d.ts +3 -1
  79. package/dist/lib/sync-core/reconcile.js +21 -8
  80. package/dist/lib/sync-core/release.d.ts +14 -5
  81. package/dist/lib/sync-core/release.js +58 -14
  82. package/dist/sentry-init.js +6 -0
  83. package/dist/utils/cli-network-lifecycle.d.ts +11 -1
  84. package/dist/utils/cli-network-lifecycle.js +36 -2
  85. package/dist/utils/daemon-helper-lifetime.d.ts +39 -0
  86. package/dist/utils/daemon-helper-lifetime.js +209 -0
  87. package/dist/utils/daemon-helper-markers.d.ts +24 -0
  88. package/dist/utils/daemon-helper-markers.js +53 -0
  89. package/dist/utils/mcp-launcher.d.ts +28 -0
  90. package/dist/utils/mcp-launcher.js +117 -0
  91. package/dist/utils/package-use-lease.d.ts +10 -0
  92. package/dist/utils/package-use-lease.js +31 -0
  93. package/dist/utils/resident-package-preload.js +4 -1
  94. package/docs/anywhere-runtime.md +7 -0
  95. package/package.json +7 -7
@@ -140,8 +140,10 @@ export type WriteClaudeCredentials = (request: WriteClaudeCredentialsRequest) =>
140
140
  * way). The gate treats this as proof the account is already signed out and
141
141
  * records revoked without hitting the token endpoint.
142
142
  * - `home` is the resolved per-host provider home for the account; the gate
143
- * compares it to the home being refreshed with `path.resolve` so symlinks
144
- * do not slip through. `null` is accepted only when the row is not live.
143
+ * compares it to the home being refreshed with `canonicalHomePath` (realpath,
144
+ * lower case on darwin) so symlinks and case differences do not slip through;
145
+ * a home that cannot be resolved blocks. `null` is accepted only when the row
146
+ * is not live.
145
147
  */
146
148
  export interface LiveLane {
147
149
  home: string | null;
@@ -164,8 +166,13 @@ export type ListLiveLanes = () => Promise<readonly LiveLane[]>;
164
166
  */
165
167
  export interface ClaudeProcess {
166
168
  pid: number;
169
+ /**
170
+ * `HOME` is set when the probe read it. Claude Code resolves its default config dir as
171
+ * HOME/.claude, so a process with HOME set and no CLAUDE_CONFIG_DIR holds that dir.
172
+ */
167
173
  env: {
168
174
  CLAUDE_CONFIG_DIR?: string;
175
+ HOME?: string;
169
176
  };
170
177
  probe: "env" | "command-only";
171
178
  }
@@ -245,7 +252,7 @@ export declare function mapClaudeOAuthUsage(body: unknown, options: MapOptions):
245
252
  readings: ReadingInput[];
246
253
  failures: ParserFailureEvent[];
247
254
  };
248
- export declare function collectClaudeOAuth(options?: CollectClaudeOAuthOptions): Promise<ClaudeOAuthCollectResult>;
255
+ export declare function collectClaudeOAuth(inputOptions?: CollectClaudeOAuthOptions): Promise<ClaudeOAuthCollectResult>;
249
256
  /**
250
257
  * Ledger account id → hashed profile account id. Rows with no captured
251
258
  * identity are omitted. The raw uuid is never returned.
@@ -349,11 +356,16 @@ export declare function resolveHqInvocation(argv?: readonly string[], execPath?:
349
356
  command: string;
350
357
  args: string[];
351
358
  };
352
- /** Exported for tests; `invocation` defaults to {@link resolveHqInvocation}. */
359
+ /**
360
+ * Run `hq ... --json` and parse stdout. Async on purpose: the collector runs inside the mesh
361
+ * daemon, and a synchronous read that takes 15-30 s on a loaded machine froze the daemon's
362
+ * event loop (MQTT keepalive, owner heartbeat) for the whole read.
363
+ * Exported for tests; `invocation` defaults to {@link resolveHqInvocation}.
364
+ */
353
365
  export declare function execHqJson(args: readonly string[], invocation?: {
354
366
  command: string;
355
367
  args: string[];
356
- }, env?: NodeJS.ProcessEnv): unknown;
368
+ }, env?: NodeJS.ProcessEnv): Promise<unknown>;
357
369
  /**
358
370
  * Parse `hq lanes list --json` payload into typed rows.
359
371
  * Exported for tests; fail-closed semantics live in defaultListLiveLanes.
@@ -369,12 +381,48 @@ export declare function parseAccountsListPayload(payload: unknown): readonly Raw
369
381
  * Exported for tests.
370
382
  */
371
383
  export declare function buildLiveLanesFromPayloads(lanes: readonly RawLaneRow[], accounts: readonly RawAccountRow[]): readonly LiveLane[];
384
+ /**
385
+ * Share one lane read across every home in a collect. Without this each expired home ran its
386
+ * own pair of `hq lanes ... --json` reads, so one slow machine turned into N timeouts per
387
+ * collect. A failure is shared too: the next home fails closed without waiting again.
388
+ * Exported for tests.
389
+ */
390
+ export declare function memoizeListLiveLanes(list: ListLiveLanes): ListLiveLanes;
372
391
  /**
373
392
  * Parse `ps -axo pid=,command=` into rows that mention the `claude` executable.
374
393
  * Exported for tests. Returned rows carry `probe: "command-only"` so the gate
375
394
  * knows it cannot read per-process CLAUDE_CONFIG_DIR on darwin.
376
395
  */
377
396
  export declare function parsePsClaudeOutput(stdout: string): readonly ClaudeProcess[];
397
+ /** Pid to full command line, from `ps -ww -o pid=,command= -p <pids>`. Exported for tests. */
398
+ export declare function parsePsCommandLines(stdout: string): ReadonlyMap<number, string>;
399
+ export interface PsEnvProbeOptions {
400
+ isDirectory?: (dir: string) => boolean;
401
+ /** False only when the pid is known to be gone (ESRCH). Unknown counts as alive. */
402
+ isAlive?: (pid: number) => boolean;
403
+ }
404
+ /**
405
+ * Upgrade darwin command-only rows with each process's environment. `ps -wwE` prints a
406
+ * same-user process's command line followed by its environment, unquoted and space separated.
407
+ * Only the text after the exact command line (read separately with `ps -ww`) is treated as the
408
+ * environment, so a prompt argument that mentions CLAUDE_CONFIG_DIR= or HOME= is never read as
409
+ * env. A row becomes `probe: "env"` only when:
410
+ * - the `-E` line starts with the command line plus a space, and the rest starts with NAME=;
411
+ * - CLAUDE_CONFIG_DIR= appears at most once in that rest, and its value is absolute and
412
+ * names an existing directory (the value runs to the next ` NAME=` or the end of line).
413
+ * Anything else stays command-only, which blocks every home. A pid missing from the output
414
+ * is dropped only when it is known to have exited; otherwise it stays command-only.
415
+ * Exported for tests.
416
+ */
417
+ export declare function parsePsEnvOutput(envStdout: string, commandLines: ReadonlyMap<number, string>, rows: readonly ClaudeProcess[], options?: PsEnvProbeOptions): readonly ClaudeProcess[];
418
+ /** Default config dir of an env-probed process with CLAUDE_CONFIG_DIR unset. Exported for tests. */
419
+ export declare function processDefaultConfigDir(proc: ClaudeProcess): string;
420
+ /**
421
+ * Canonical form of a home directory for comparison: symlinks resolved by the OS, and lower
422
+ * case on darwin, where APFS is case-insensitive by default. Null when the path cannot be
423
+ * resolved. Exported for tests.
424
+ */
425
+ export declare function canonicalHomePath(dir: string, platform?: NodeJS.Platform): string | null;
378
426
  export interface ClaudeTokenRefreshResult {
379
427
  outcome: "refreshed" | "revoked" | "error" | "skipped-race" | "skipped-live" | "skipped-live-check-failed" | "skipped-process" | "skipped-process-check-failed" | "skipped-backoff" | "skipped-revoked";
380
428
  newAccessToken?: string;
@@ -36,6 +36,7 @@ import * as os from "node:os";
36
36
  import * as path from "node:path";
37
37
  import { DIR_MODE, FILE_MODE, appendReadings, capacityRoot, EXPIRED_REASON, SIGN_IN_REVOKED_REASON, readCollectorMark, redactProviderTokens, } from "../ledger.js";
38
38
  import { hashClaudeAccountId } from "./claude-statusline.js";
39
+ import { REMOTE_LANE_READ_ENV } from "../../lanes/outpost-dispatch.js";
39
40
  export const CLAUDE_OAUTH_PARSER_VERSION = "claude-oauth-usage.1";
40
41
  export const CLAUDE_OAUTH_PROVIDER = "claude";
41
42
  export const CLAUDE_OAUTH_SOURCE = "oauth-usage";
@@ -136,7 +137,11 @@ export function mapClaudeOAuthUsage(body, options) {
136
137
  }
137
138
  return { readings, failures };
138
139
  }
139
- export async function collectClaudeOAuth(options = {}) {
140
+ export async function collectClaudeOAuth(inputOptions = {}) {
141
+ const options = {
142
+ ...inputOptions,
143
+ listLiveLanes: memoizeListLiveLanes(inputOptions.listLiveLanes ?? defaultListLiveLanes),
144
+ };
140
145
  const ledger = ledgerOptions(options);
141
146
  const root = ledger.root ?? capacityRoot(ledger.env, ledger.home);
142
147
  const scoped = { ...ledger, root };
@@ -1766,43 +1771,62 @@ export function resolveHqInvocation(argv = process.argv, execPath = process.exec
1766
1771
  }
1767
1772
  /** Output cap for the `hq ... --json` reads behind the refresh gates. */
1768
1773
  const HQ_JSON_MAX_BUFFER_BYTES = 64 * 1024 * 1024;
1769
- const HQ_JSON_TIMEOUT_MS = 15_000;
1770
- /** Exported for tests; `invocation` defaults to {@link resolveHqInvocation}. */
1774
+ const HQ_JSON_TIMEOUT_MS = 30_000;
1775
+ /**
1776
+ * Run `hq ... --json` and parse stdout. Async on purpose: the collector runs inside the mesh
1777
+ * daemon, and a synchronous read that takes 15-30 s on a loaded machine froze the daemon's
1778
+ * event loop (MQTT keepalive, owner heartbeat) for the whole read.
1779
+ * Exported for tests; `invocation` defaults to {@link resolveHqInvocation}.
1780
+ */
1771
1781
  export function execHqJson(args, invocation = resolveHqInvocation(), env = process.env) {
1772
- const result = spawnSync(invocation.command, [...invocation.args, ...args], {
1773
- env,
1774
- encoding: "utf8",
1775
- // The lane list takes ~2.5 s on a busy machine and grows with lane history; 5 s left too
1776
- // little headroom under load, and a timeout fails the gate closed.
1777
- timeout: HQ_JSON_TIMEOUT_MS,
1778
- // `hq lanes list --json` grows with lane history (1.7 MB on a busy machine). The spawnSync
1779
- // default of 1 MiB raised ENOBUFS, which made the live-lane gate fail closed on every collect.
1780
- maxBuffer: HQ_JSON_MAX_BUFFER_BYTES,
1781
- windowsHide: true,
1782
- stdio: ["ignore", "pipe", "pipe"],
1782
+ const label = `hq ${args.join(" ")}`;
1783
+ return new Promise((resolve, reject) => {
1784
+ execFile(invocation.command, [...invocation.args, ...args], {
1785
+ env,
1786
+ encoding: "utf8",
1787
+ // The lane list takes ~2.5 s on an idle machine and grows with lane history. Under heavy
1788
+ // load (load average ~95 on 18 cores) the same read took 16-18 s, so 15 s still failed the
1789
+ // gate closed on every collect and every Claude home stayed expired.
1790
+ timeout: HQ_JSON_TIMEOUT_MS,
1791
+ // `hq lanes list --json` grows with lane history (6.8 MB on a busy machine). The 1 MiB
1792
+ // default made the live-lane gate fail closed on every collect.
1793
+ maxBuffer: HQ_JSON_MAX_BUFFER_BYTES,
1794
+ // SIGTERM can be trapped, and execFile waits for the child to exit before calling back.
1795
+ killSignal: "SIGKILL",
1796
+ windowsHide: true,
1797
+ }, (error, stdout) => {
1798
+ if (error) {
1799
+ const failure = error;
1800
+ const code = failure.code;
1801
+ if (code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") {
1802
+ reject(new Error(`${label} output exceeded ${HQ_JSON_MAX_BUFFER_BYTES} bytes`));
1803
+ }
1804
+ else if (failure.killed) {
1805
+ reject(new Error(`${label} timed out`));
1806
+ }
1807
+ else if (typeof code === "number") {
1808
+ reject(new Error(`${label} exit ${code}`));
1809
+ }
1810
+ else if (failure.signal) {
1811
+ reject(new Error(`${label} killed by ${failure.signal}`));
1812
+ }
1813
+ else {
1814
+ reject(new Error(`${label} failed to spawn: ${failure.message}`));
1815
+ }
1816
+ return;
1817
+ }
1818
+ if (!stdout) {
1819
+ reject(new Error(`${label} empty stdout`));
1820
+ return;
1821
+ }
1822
+ try {
1823
+ resolve(JSON.parse(stdout));
1824
+ }
1825
+ catch (parseError) {
1826
+ reject(new Error(`${label} invalid JSON: ${parseError.message}`));
1827
+ }
1828
+ });
1783
1829
  });
1784
- if (result.error) {
1785
- const code = result.error.code;
1786
- if (code === "ETIMEDOUT")
1787
- throw new Error(`hq ${args.join(" ")} timed out`);
1788
- if (code === "ENOBUFS")
1789
- throw new Error(`hq ${args.join(" ")} output exceeded ${HQ_JSON_MAX_BUFFER_BYTES} bytes`);
1790
- throw new Error(`hq ${args.join(" ")} failed to spawn: ${result.error.message}`);
1791
- }
1792
- if (result.signal)
1793
- throw new Error(`hq ${args.join(" ")} killed by ${result.signal}`);
1794
- if (typeof result.status !== "number")
1795
- throw new Error(`hq ${args.join(" ")} no exit status`);
1796
- if (result.status !== 0)
1797
- throw new Error(`hq ${args.join(" ")} exit ${result.status}`);
1798
- if (!result.stdout)
1799
- throw new Error(`hq ${args.join(" ")} empty stdout`);
1800
- try {
1801
- return JSON.parse(result.stdout);
1802
- }
1803
- catch (error) {
1804
- throw new Error(`hq ${args.join(" ")} invalid JSON: ${error.message}`);
1805
- }
1806
1830
  }
1807
1831
  /**
1808
1832
  * Parse `hq lanes list --json` payload into typed rows.
@@ -1889,12 +1913,31 @@ export function buildLiveLanesFromPayloads(lanes, accounts) {
1889
1913
  * Throws on hq call failure or unexpected JSON shape; callers skip the refresh.
1890
1914
  */
1891
1915
  const defaultListLiveLanes = async () => {
1892
- const lanesPayload = execHqJson(["lanes", "list", "--json"]);
1893
- const accountsPayload = execHqJson(["lanes", "accounts", "list", "--json"]);
1916
+ // The gate only needs lanes that use this host's Claude homes. Skip the Outpost mirror
1917
+ // refresh (a network round trip per Outpost); cached mirror rows are still listed.
1918
+ const env = { ...process.env, [REMOTE_LANE_READ_ENV]: "1" };
1919
+ const invocation = resolveHqInvocation();
1920
+ const [lanesPayload, accountsPayload] = await Promise.all([
1921
+ execHqJson(["lanes", "list", "--json"], invocation, env),
1922
+ execHqJson(["lanes", "accounts", "list", "--json"], invocation, env),
1923
+ ]);
1894
1924
  const lanes = parseLanesListPayload(lanesPayload);
1895
1925
  const accounts = parseAccountsListPayload(accountsPayload);
1896
1926
  return buildLiveLanesFromPayloads(lanes, accounts);
1897
1927
  };
1928
+ /**
1929
+ * Share one lane read across every home in a collect. Without this each expired home ran its
1930
+ * own pair of `hq lanes ... --json` reads, so one slow machine turned into N timeouts per
1931
+ * collect. A failure is shared too: the next home fails closed without waiting again.
1932
+ * Exported for tests.
1933
+ */
1934
+ export function memoizeListLiveLanes(list) {
1935
+ let pending = null;
1936
+ return () => {
1937
+ pending ??= list();
1938
+ return pending;
1939
+ };
1940
+ }
1898
1941
  /**
1899
1942
  * Parse `ps -axo pid=,command=` into rows that mention the `claude` executable.
1900
1943
  * Exported for tests. Returned rows carry `probe: "command-only"` so the gate
@@ -1926,14 +1969,145 @@ export function parsePsClaudeOutput(stdout) {
1926
1969
  }
1927
1970
  return out;
1928
1971
  }
1972
+ /** Pid to full command line, from `ps -ww -o pid=,command= -p <pids>`. Exported for tests. */
1973
+ export function parsePsCommandLines(stdout) {
1974
+ const lines = new Map();
1975
+ for (const rawLine of stdout.split("\n")) {
1976
+ const match = /^\s*(\d+) (.*)$/.exec(rawLine.replace(/\s+$/, ""));
1977
+ if (match)
1978
+ lines.set(Number(match[1]), match[2]);
1979
+ }
1980
+ return lines;
1981
+ }
1982
+ /**
1983
+ * Upgrade darwin command-only rows with each process's environment. `ps -wwE` prints a
1984
+ * same-user process's command line followed by its environment, unquoted and space separated.
1985
+ * Only the text after the exact command line (read separately with `ps -ww`) is treated as the
1986
+ * environment, so a prompt argument that mentions CLAUDE_CONFIG_DIR= or HOME= is never read as
1987
+ * env. A row becomes `probe: "env"` only when:
1988
+ * - the `-E` line starts with the command line plus a space, and the rest starts with NAME=;
1989
+ * - CLAUDE_CONFIG_DIR= appears at most once in that rest, and its value is absolute and
1990
+ * names an existing directory (the value runs to the next ` NAME=` or the end of line).
1991
+ * Anything else stays command-only, which blocks every home. A pid missing from the output
1992
+ * is dropped only when it is known to have exited; otherwise it stays command-only.
1993
+ * Exported for tests.
1994
+ */
1995
+ export function parsePsEnvOutput(envStdout, commandLines, rows, options = {}) {
1996
+ const isDirectory = options.isDirectory ?? defaultIsDirectory;
1997
+ const isAlive = options.isAlive ?? defaultIsAlive;
1998
+ const envLines = parsePsCommandLines(envStdout);
1999
+ const blocked = (pid) => ({ pid, env: {}, probe: "command-only" });
2000
+ const out = [];
2001
+ for (const row of rows) {
2002
+ const command = commandLines.get(row.pid);
2003
+ const line = envLines.get(row.pid);
2004
+ if (command === undefined || line === undefined) {
2005
+ if (isAlive(row.pid))
2006
+ out.push(blocked(row.pid));
2007
+ continue;
2008
+ }
2009
+ if (!line.startsWith(`${command} `)) {
2010
+ // No visible env (another user's or a protected process), or the pid was reused.
2011
+ out.push(blocked(row.pid));
2012
+ continue;
2013
+ }
2014
+ const env = line.slice(command.length + 1);
2015
+ if (!/^[A-Za-z_][A-Za-z0-9_]*=/.test(env)) {
2016
+ out.push(blocked(row.pid));
2017
+ continue;
2018
+ }
2019
+ const mentions = env.split("CLAUDE_CONFIG_DIR=").length - 1;
2020
+ const match = /(?:^| )CLAUDE_CONFIG_DIR=(.*?)(?= [A-Za-z_][A-Za-z0-9_]*=|$)/.exec(env);
2021
+ if (mentions === 0) {
2022
+ // The default config dir is HOME/.claude for this process. HOME must be one absolute
2023
+ // value; an unset HOME falls back to the collector's own home.
2024
+ const homes = [...env.matchAll(/(?:^| )HOME=(.*?)(?= [A-Za-z_][A-Za-z0-9_]*=|$)/g)].map((m) => m[1]);
2025
+ if (homes.length === 0) {
2026
+ out.push({ pid: row.pid, env: {}, probe: "env" });
2027
+ }
2028
+ else if (homes.length === 1 && path.isAbsolute(homes[0])) {
2029
+ out.push({ pid: row.pid, env: { HOME: homes[0] }, probe: "env" });
2030
+ }
2031
+ else {
2032
+ out.push(blocked(row.pid));
2033
+ }
2034
+ }
2035
+ else if (mentions === 1 && match && path.isAbsolute(match[1]) && isDirectory(match[1])) {
2036
+ out.push({ pid: row.pid, env: { CLAUDE_CONFIG_DIR: match[1] }, probe: "env" });
2037
+ }
2038
+ else {
2039
+ out.push(blocked(row.pid));
2040
+ }
2041
+ }
2042
+ return out;
2043
+ }
2044
+ /** Default config dir of an env-probed process with CLAUDE_CONFIG_DIR unset. Exported for tests. */
2045
+ export function processDefaultConfigDir(proc) {
2046
+ const home = typeof proc.env.HOME === "string" && path.isAbsolute(proc.env.HOME) ? proc.env.HOME : os.homedir();
2047
+ return path.join(home, ".claude");
2048
+ }
2049
+ function defaultIsAlive(pid) {
2050
+ try {
2051
+ process.kill(pid, 0);
2052
+ return true;
2053
+ }
2054
+ catch (error) {
2055
+ return error.code !== "ESRCH";
2056
+ }
2057
+ }
2058
+ /**
2059
+ * Canonical form of a home directory for comparison: symlinks resolved by the OS, and lower
2060
+ * case on darwin, where APFS is case-insensitive by default. Null when the path cannot be
2061
+ * resolved. Exported for tests.
2062
+ */
2063
+ export function canonicalHomePath(dir, platform = process.platform) {
2064
+ try {
2065
+ const real = fs.realpathSync.native(path.resolve(dir));
2066
+ return platform === "darwin" ? real.toLowerCase() : real;
2067
+ }
2068
+ catch {
2069
+ return null;
2070
+ }
2071
+ }
2072
+ function defaultIsDirectory(dir) {
2073
+ try {
2074
+ return fs.statSync(dir).isDirectory();
2075
+ }
2076
+ catch {
2077
+ return false;
2078
+ }
2079
+ }
2080
+ function runPs(args) {
2081
+ const result = spawnSync("ps", [...args], {
2082
+ encoding: "utf8",
2083
+ timeout: 5_000,
2084
+ // Full command lines for every process can pass the 1 MiB default on a busy machine.
2085
+ maxBuffer: HQ_JSON_MAX_BUFFER_BYTES,
2086
+ windowsHide: true,
2087
+ stdio: ["ignore", "pipe", "pipe"],
2088
+ });
2089
+ if (result.error)
2090
+ throw new Error(`ps failed to spawn: ${result.error.message}`);
2091
+ if (result.signal)
2092
+ throw new Error(`ps killed by ${result.signal}`);
2093
+ if (typeof result.status !== "number")
2094
+ throw new Error("ps no exit status");
2095
+ // `ps -p` exits 1 both when no pid still exists and on a real error. Accept exit 1 only with
2096
+ // empty stderr; parsePsEnvOutput then keeps any pid that is not known to be gone as blocking.
2097
+ const quietNoMatch = args.includes("-p") && result.status === 1 && !(result.stderr ?? "").trim();
2098
+ if (result.status !== 0 && !quietNoMatch)
2099
+ throw new Error(`ps exit ${result.status}`);
2100
+ return result.stdout ?? "";
2101
+ }
1929
2102
  /**
1930
2103
  * Fail-closed enumeration of running Claude Code processes.
1931
2104
  *
1932
2105
  * linux: /proc/<pid>/environ gives us per-process CLAUDE_CONFIG_DIR; absent
1933
2106
  * value means the process holds the default home.
1934
- * darwin: ps cannot read a child process environment, so we return
1935
- * `probe: "command-only"` rows. The gate treats such a row as holding the
1936
- * default home AND blocking every non-default home.
2107
+ * darwin: `ps -axo` finds claude processes, then `ps -wwE` reads their
2108
+ * environment (same user only). Rows whose environment cannot be read stay
2109
+ * `probe: "command-only"`; the gate treats such a row as holding the default
2110
+ * home AND blocking every non-default home.
1937
2111
  * other platforms: return []. The lane gate is authoritative there.
1938
2112
  *
1939
2113
  * Throws on ps/readdir failure so the caller skips refresh.
@@ -1975,32 +2149,23 @@ const defaultListProcesses = async () => {
1975
2149
  throw new Error(`read /proc/${entry}/environ failed`);
1976
2150
  }
1977
2151
  const match = /(?:^|\0)CLAUDE_CONFIG_DIR=([^\0]+)/.exec(envText);
1978
- out.push({
1979
- pid,
1980
- env: match && match[1] ? { CLAUDE_CONFIG_DIR: match[1] } : {},
1981
- probe: "env",
1982
- });
2152
+ const homeMatch = /(?:^|\0)HOME=([^\0]+)/.exec(envText);
2153
+ const procEnv = {};
2154
+ if (match && match[1])
2155
+ procEnv.CLAUDE_CONFIG_DIR = match[1];
2156
+ if (homeMatch && homeMatch[1])
2157
+ procEnv.HOME = homeMatch[1];
2158
+ out.push({ pid, env: procEnv, probe: "env" });
1983
2159
  }
1984
2160
  return out;
1985
2161
  }
1986
2162
  if (process.platform === "darwin") {
1987
- const result = spawnSync("ps", ["-axo", "pid=,command="], {
1988
- encoding: "utf8",
1989
- timeout: 5_000,
1990
- // Full command lines for every process can pass the 1 MiB default on a busy machine.
1991
- maxBuffer: HQ_JSON_MAX_BUFFER_BYTES,
1992
- windowsHide: true,
1993
- stdio: ["ignore", "pipe", "pipe"],
1994
- });
1995
- if (result.error)
1996
- throw new Error(`ps failed to spawn: ${result.error.message}`);
1997
- if (result.signal)
1998
- throw new Error(`ps killed by ${result.signal}`);
1999
- if (typeof result.status !== "number")
2000
- throw new Error("ps no exit status");
2001
- if (result.status !== 0)
2002
- throw new Error(`ps exit ${result.status}`);
2003
- return parsePsClaudeOutput(result.stdout ?? "");
2163
+ const rows = parsePsClaudeOutput(runPs(["-axo", "pid=,command="]));
2164
+ if (rows.length === 0)
2165
+ return rows;
2166
+ const pids = rows.map((row) => String(row.pid)).join(",");
2167
+ const commandLines = parsePsCommandLines(runPs(["-ww", "-o", "pid=,command=", "-p", pids]));
2168
+ return parsePsEnvOutput(runPs(["-wwE", "-o", "pid=,command=", "-p", pids]), commandLines, rows);
2004
2169
  }
2005
2170
  return [];
2006
2171
  };
@@ -2027,7 +2192,16 @@ async function attemptClaudeTokenRefresh(ctx) {
2027
2192
  return { outcome: "skipped-live-check-failed", errorMessage: message };
2028
2193
  }
2029
2194
  const resolvedHome = path.resolve(ctx.home);
2030
- const sameHome = (candidate) => typeof candidate === "string" && candidate.length > 0 && path.resolve(candidate) === resolvedHome;
2195
+ const targetHome = canonicalHomePath(ctx.home) ?? resolvedHome;
2196
+ // Exact match, for the auth-failed short circuit, which records the home as revoked.
2197
+ const sameHome = (candidate) => typeof candidate === "string" && candidate.length > 0 && canonicalHomePath(candidate) === targetHome;
2198
+ // Blocking match: a path that cannot be resolved might be this home, so it blocks.
2199
+ const mayHoldHome = (candidate) => {
2200
+ if (typeof candidate !== "string" || candidate.length === 0)
2201
+ return false;
2202
+ const canonical = canonicalHomePath(candidate);
2203
+ return canonical === null || canonical === targetHome;
2204
+ };
2031
2205
  // Addendum 1: if the lane watcher classified a 401/403 on this home as
2032
2206
  // provider_auth_failed, treat the account as already signed out and record
2033
2207
  // revoked immediately. No token endpoint call, no response body stored.
@@ -2046,7 +2220,7 @@ async function attemptClaudeTokenRefresh(ctx) {
2046
2220
  });
2047
2221
  return { outcome: "revoked", errorClass: "revoked" };
2048
2222
  }
2049
- if (liveLanes.some((lane) => lane.isLive && sameHome(lane.home))) {
2223
+ if (liveLanes.some((lane) => lane.isLive && mayHoldHome(lane.home))) {
2050
2224
  recordRefresh(ctx.root, key, { attemptedAt: new Date(ctx.nowMs).toISOString(), outcome: "skipped-live" });
2051
2225
  return { outcome: "skipped-live" };
2052
2226
  }
@@ -2065,15 +2239,21 @@ async function attemptClaudeTokenRefresh(ctx) {
2065
2239
  return { outcome: "skipped-process-check-failed", errorMessage: message };
2066
2240
  }
2067
2241
  const defaultHome = path.join(os.homedir(), ".claude");
2068
- const isDefaultHome = resolvedHome === path.resolve(defaultHome);
2242
+ const isDefaultHome = targetHome === (canonicalHomePath(defaultHome) ?? path.resolve(defaultHome));
2069
2243
  const procBlocks = procs.some((proc) => {
2070
2244
  if (proc.probe === "env") {
2071
2245
  const dir = typeof proc.env.CLAUDE_CONFIG_DIR === "string" ? proc.env.CLAUDE_CONFIG_DIR : "";
2072
- // On linux we have a real env read. Default home is held when
2073
- // CLAUDE_CONFIG_DIR is empty; non-default home only when it points here.
2074
- if (!dir)
2075
- return isDefaultHome;
2076
- return path.resolve(dir) === resolvedHome;
2246
+ // A real env read. With CLAUDE_CONFIG_DIR unset the process holds HOME/.claude for its
2247
+ // own HOME (the collector's home when HOME was not read). Claude Code adds no keychain
2248
+ // suffix without CLAUDE_CONFIG_DIR, so such a process may also use the default keychain
2249
+ // item; it keeps blocking the collector's default home too.
2250
+ // Compare the process's default dir the same way as the collector's: a dir that does not
2251
+ // exist yet is compared by its resolved path, so a missing ~/.claude does not block every home.
2252
+ if (!dir) {
2253
+ const procDefault = processDefaultConfigDir(proc);
2254
+ return isDefaultHome || targetHome === (canonicalHomePath(procDefault) ?? path.resolve(procDefault));
2255
+ }
2256
+ return mayHoldHome(dir);
2077
2257
  }
2078
2258
  // command-only (darwin): we cannot read the per-process env. Conservatively
2079
2259
  // treat the row as holding the default home AND blocking non-default homes.
@@ -28,6 +28,12 @@ function encodeLocalSegment(segment, win32) {
28
28
  }
29
29
  return encoded;
30
30
  }
31
+ function assertSkillBody(skillFile, body, which) {
32
+ if (body.trim().length === 0) {
33
+ throw new Error(`The ${which} SKILL.md at '${skillFile}' is empty, so it was not bridged as a skill. ` +
34
+ "Restore the skill body (run 'hq sync') and retry.");
35
+ }
36
+ }
31
37
  function lstat(pathname) {
32
38
  try {
33
39
  return fs.lstatSync(pathname);
@@ -265,6 +271,10 @@ export function surfaceCompanySkill(input) {
265
271
  if (!fs.existsSync(skillFile) || !fs.statSync(skillFile).isFile()) {
266
272
  throw new Error(`The canonical SKILL.md does not exist at '${skillFile}'.`);
267
273
  }
274
+ // An empty body (a zero-byte file left by an interrupted write or a sync that
275
+ // has not hydrated yet) would be bridged as a placeholder skill with no
276
+ // instructions. Fail loudly instead of surfacing it.
277
+ assertSkillBody(skillFile, fs.readFileSync(skillFile, "utf8"), "canonical");
268
278
  const wrapperName = encodeLocalSegment(`${input.companySlug}:${input.skillSlug}`, input.win32 ?? process.platform === "win32");
269
279
  const win32 = input.win32 ?? process.platform === "win32";
270
280
  const wrapperPath = path.join(input.hqRoot, ".claude", "skills", wrapperName);
@@ -349,6 +359,25 @@ export function surfaceCompanySkill(input) {
349
359
  // safer than rolling back a successful replacement.
350
360
  }
351
361
  }
362
+ // Read the body back through the committed wrapper (junction on Windows)
363
+ // and require it to match the canonical file, so a wrapper that exposes an
364
+ // empty or different SKILL.md is reported instead of silently shipped.
365
+ const canonicalBody = fs.readFileSync(skillFile, "utf8");
366
+ let bridgedBody = "";
367
+ try {
368
+ bridgedBody = fs.readFileSync(path.join(wrapperPath, "SKILL.md"), "utf8");
369
+ }
370
+ catch (readError) {
371
+ removeEntryNoFollow(wrapperPath, { force: true });
372
+ removeGeneratedMarker(wrapperPath);
373
+ throw new Error(`The bridged skill '${wrapperPath}' could not be read back: ` +
374
+ `${readError instanceof Error ? readError.message : String(readError)}.`);
375
+ }
376
+ if (bridgedBody !== canonicalBody) {
377
+ removeEntryNoFollow(wrapperPath, { force: true });
378
+ removeGeneratedMarker(wrapperPath);
379
+ throw new Error(`The bridged skill '${wrapperPath}' does not match the canonical SKILL.md at '${skillFile}'.`);
380
+ }
352
381
  writeGeneratedMarker(wrapperPath, input.companySlug, input.skillSlug);
353
382
  pruneStaleNamespacedWrappers(runtimeRoot, wrapperName, input.hqRoot, input.companySlug, win32);
354
383
  if (input.legacyPrefix) {
@@ -211,8 +211,8 @@ function planService(service, ctx) {
211
211
  return [unit];
212
212
  }
213
213
  case "sync-core": {
214
- if (ctx.platform !== "darwin" && ctx.platform !== "linux") {
215
- return [unavailable(service, "the new sync engine runs on macOS and Linux only")];
214
+ if (ctx.platform !== "darwin" && ctx.platform !== "linux" && ctx.platform !== "win32") {
215
+ return [unavailable(service, "the new sync engine runs on macOS, Linux and Windows only")];
216
216
  }
217
217
  const reconcile = base(service, {
218
218
  id: SYNC_CORE_RECONCILE_UNIT,