@indigoai-us/hq-cli 5.345.218 → 5.345.220

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 (138) hide show
  1. package/CHANGELOG.md +130 -0
  2. package/dist/command-catalog.generated.d.ts +94 -1
  3. package/dist/command-catalog.generated.js +114 -1
  4. package/dist/commands/agents.d.ts +22 -0
  5. package/dist/commands/agents.js +92 -1
  6. package/dist/commands/bot.d.ts +48 -0
  7. package/dist/commands/bot.js +108 -8
  8. package/dist/commands/cloud.d.ts +16 -0
  9. package/dist/commands/cloud.js +112 -102
  10. package/dist/commands/core-checkpoint-native.d.ts +13 -0
  11. package/dist/commands/core-checkpoint-native.js +167 -52
  12. package/dist/commands/core-hook-registration-plan.d.ts +8 -0
  13. package/dist/commands/core-hook-registration-plan.js +22 -0
  14. package/dist/commands/core.js +9 -0
  15. package/dist/commands/daemon.js +21 -2
  16. package/dist/commands/doctor.js +4 -0
  17. package/dist/commands/integrations.js +84 -3
  18. package/dist/commands/lanes-shared.d.ts +2 -0
  19. package/dist/commands/lanes-watch.js +28 -1
  20. package/dist/commands/lanes.d.ts +2 -0
  21. package/dist/commands/lanes.js +99 -9
  22. package/dist/commands/packs.js +23 -0
  23. package/dist/commands/pkg-update.d.ts +6 -0
  24. package/dist/commands/pkg-update.js +15 -7
  25. package/dist/commands/reindex.js +6 -0
  26. package/dist/commands/run.js +55 -45
  27. package/dist/commands/sources.js +3 -2
  28. package/dist/index.js +8 -0
  29. package/dist/lib/anywhere-person-setting.d.ts +2 -2
  30. package/dist/lib/anywhere-person-setting.js +2 -2
  31. package/dist/lib/bot/fleet-cron.js +5 -2
  32. package/dist/lib/bot/inflight.d.ts +9 -0
  33. package/dist/lib/bot/jobs/hot-signal-sync.d.ts +51 -0
  34. package/dist/lib/bot/jobs/hot-signal-sync.js +90 -9
  35. package/dist/lib/bot/run.d.ts +29 -5
  36. package/dist/lib/bot/run.js +203 -1
  37. package/dist/lib/bot/signals-try-now.d.ts +76 -0
  38. package/dist/lib/bot/signals-try-now.js +111 -0
  39. package/dist/lib/capacity/collectors/claude-oauth.d.ts +22 -3
  40. package/dist/lib/capacity/collectors/claude-oauth.js +78 -22
  41. package/dist/lib/capacity/scheduler.d.ts +8 -0
  42. package/dist/lib/capacity/scheduler.js +5 -2
  43. package/dist/lib/daemon/anywhere-listener.d.ts +33 -1
  44. package/dist/lib/daemon/anywhere-listener.js +86 -4
  45. package/dist/lib/daemon/machine-check-owner.d.ts +21 -0
  46. package/dist/lib/daemon/machine-check-owner.js +95 -0
  47. package/dist/lib/daemon/machine-check-registration.d.ts +4 -0
  48. package/dist/lib/daemon/machine-check-registration.js +64 -0
  49. package/dist/lib/daemon/machine-checks.d.ts +144 -0
  50. package/dist/lib/daemon/machine-checks.js +412 -0
  51. package/dist/lib/daemon/run.js +35 -4
  52. package/dist/lib/daemon/runtime-notifications.d.ts +7 -0
  53. package/dist/lib/daemon/runtime-notifications.js +50 -0
  54. package/dist/lib/daemon/server.d.ts +2 -0
  55. package/dist/lib/daemon/server.js +6 -0
  56. package/dist/lib/daemon/state-file.d.ts +12 -1
  57. package/dist/lib/daemon/state-file.js +23 -1
  58. package/dist/lib/doctor/checks/fleet-hook-core.d.ts +12 -0
  59. package/dist/lib/doctor/checks/fleet-hook-core.js +127 -0
  60. package/dist/lib/doctor/checks/host-core.js +3 -1
  61. package/dist/lib/doctor/checks/machine-checks.d.ts +16 -0
  62. package/dist/lib/doctor/checks/machine-checks.js +103 -0
  63. package/dist/lib/doctor/registry.js +4 -0
  64. package/dist/lib/doctor/types.d.ts +4 -0
  65. package/dist/lib/hook-engine/canary-gate.d.ts +64 -0
  66. package/dist/lib/hook-engine/canary-gate.js +147 -0
  67. package/dist/lib/hook-engine/coverage.d.ts +3 -1
  68. package/dist/lib/hook-engine/coverage.js +8 -2
  69. package/dist/lib/hook-engine/fleet-registration.d.ts +42 -0
  70. package/dist/lib/hook-engine/fleet-registration.js +168 -0
  71. package/dist/lib/hook-engine/reconcile.js +3 -2
  72. package/dist/lib/hook-engine/registration-identity.d.ts +33 -0
  73. package/dist/lib/hook-engine/registration-identity.js +97 -0
  74. package/dist/lib/hook-engine/registration.d.ts +1 -30
  75. package/dist/lib/hook-engine/registration.js +1 -88
  76. package/dist/lib/hook-engine/resolve.d.ts +2 -0
  77. package/dist/lib/hook-engine/resolve.js +7 -3
  78. package/dist/lib/hot-channels/alert.d.ts +10 -1
  79. package/dist/lib/hot-channels/alert.js +34 -7
  80. package/dist/lib/hot-channels/signals-claude-login.d.ts +53 -0
  81. package/dist/lib/hot-channels/signals-claude-login.js +96 -0
  82. package/dist/lib/hot-channels/signals-pass.d.ts +17 -0
  83. package/dist/lib/hot-channels/signals-pass.js +31 -3
  84. package/dist/lib/lanes/canonical-host.d.ts +8 -1
  85. package/dist/lib/lanes/canonical-host.js +9 -3
  86. package/dist/lib/lanes/machine-identity.d.ts +15 -0
  87. package/dist/lib/lanes/machine-identity.js +26 -0
  88. package/dist/lib/lanes/spawn.js +17 -3
  89. package/dist/lib/search-index/index.js +11 -1
  90. package/dist/lib/signals/engine.d.ts +1 -0
  91. package/dist/lib/signals/engine.js +7 -0
  92. package/dist/lib/signals/reply.d.ts +48 -0
  93. package/dist/lib/signals/reply.js +72 -11
  94. package/dist/lib/signals/steps/adapters.js +6 -0
  95. package/dist/lib/signals/steps/extract.d.ts +50 -3
  96. package/dist/lib/signals/steps/extract.js +100 -17
  97. package/dist/lib/signals/steps/types.d.ts +19 -0
  98. package/dist/lib/sync-core/engine-home.d.ts +6 -1
  99. package/dist/lib/sync-core/engine-home.js +13 -3
  100. package/dist/lib/sync-core/reconcile.js +1 -1
  101. package/dist/lib/sync-core/release-compare.d.ts +7 -0
  102. package/dist/lib/sync-core/release-compare.js +16 -0
  103. package/dist/lib/sync-core/release.d.ts +2 -1
  104. package/dist/lib/sync-core/release.js +2 -10
  105. package/dist/run/schema-env-overrides.d.ts +24 -0
  106. package/dist/run/schema-env-overrides.js +72 -0
  107. package/dist/utils/core-next-flag.d.ts +4 -0
  108. package/dist/utils/core-next-flag.js +22 -0
  109. package/dist/utils/core-next-kill.d.ts +16 -0
  110. package/dist/utils/core-next-kill.js +35 -0
  111. package/dist/utils/host-registration.d.ts +33 -23
  112. package/dist/utils/host-registration.js +90 -66
  113. package/package.json +14 -10
  114. package/scripts/check-function-manifest.mjs +5 -4
  115. package/dist/commands/__fixtures__/access-vault.d.ts +0 -95
  116. package/dist/commands/__fixtures__/access-vault.js +0 -166
  117. package/dist/commands/__fixtures__/make-tar.d.ts +0 -38
  118. package/dist/commands/__fixtures__/make-tar.js +0 -102
  119. package/dist/commands/secrets-public-types.test-d.d.ts +0 -2
  120. package/dist/commands/secrets-public-types.test-d.js +0 -18
  121. package/dist/lib/decisions/fixtures/us001.d.ts +0 -137
  122. package/dist/lib/decisions/fixtures/us001.js +0 -21
  123. package/dist/lib/doctor/__testing__/fake-hq-tree.d.ts +0 -226
  124. package/dist/lib/doctor/__testing__/fake-hq-tree.js +0 -408
  125. package/dist/lib/doctor/__testing__/run-doctor.d.ts +0 -8
  126. package/dist/lib/doctor/__testing__/run-doctor.js +0 -69
  127. package/dist/lib/fingerprint/fixtures.d.ts +0 -19
  128. package/dist/lib/fingerprint/fixtures.js +0 -61
  129. package/dist/lib/hot-channels/adapters/__fixtures__/dummy.d.ts +0 -33
  130. package/dist/lib/hot-channels/adapters/__fixtures__/dummy.js +0 -53
  131. package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.d.ts +0 -38
  132. package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.js +0 -64
  133. package/dist/lib/mesh/live/daemon/mqtt-events.test-d.d.ts +0 -2
  134. package/dist/lib/mesh/live/daemon/mqtt-events.test-d.js +0 -10
  135. package/dist/lib/test-utils/isolated-flag-company-context.d.ts +0 -3
  136. package/dist/lib/test-utils/isolated-flag-company-context.js +0 -18
  137. package/scripts/hook-engine-manifest.mjs +0 -22
  138. package/scripts/mesh-daemon-if-stale.mjs +0 -20
@@ -1,226 +0,0 @@
1
- /**
2
- * buildFakeHqTree — a reusable test harness that materialises a fake HQ tree in
3
- * the OS temp directory from a declarative spec.
4
- *
5
- * Every `hq doctor` check is fundamentally a function of the on-disk shape of an
6
- * HQ tree: which hooks exist, whether they are executable, whether they are
7
- * registered in settings, which `hook-gate.sh` profiles list them, and whether
8
- * the Codex mirror still matches its Claude original. This helper lets a test
9
- * declare that shape — including its deliberately-broken variants — and get back
10
- * a throwaway tree plus a manifest describing exactly what was written, without
11
- * ever touching the real HQ tree.
12
- *
13
- * Design notes:
14
- * - Everything is written under a `mkdtempSync` root inside `os.tmpdir()`. The
15
- * real HQ tree is never read or written.
16
- * - The root path is canonicalised with `realpathSync` so callers can assert
17
- * containment under the temp dir on platforms (macOS) where the temp dir is
18
- * a symlink (`/var` -> `/private/var`).
19
- * - Cleanup is automatic: the first build registers a single `process` exit
20
- * handler that removes every tracked root, so nothing survives a test run,
21
- * including on test failure. `tree.cleanup()` is also exposed for eager
22
- * removal, and is idempotent.
23
- *
24
- * The reference for this pattern is core/scripts/test-codex-hook-adapter.sh,
25
- * which stands up a throwaway HQ tree with `mktemp -d` and stubbed hook scripts.
26
- */
27
- /** Claude/Codex lifecycle events a hook can be registered against. */
28
- type HookEventName = "PreToolUse" | "PostToolUse" | "UserPromptSubmit" | "SessionStart" | "Stop" | "SubagentStop" | "PreCompact" | "Notification";
29
- /** The three `hook-gate.sh` allowlist profiles. */
30
- export type GateProfile = "minimal" | "standard" | "strict";
31
- /** Canonical ordering of the gate profiles. */
32
- export declare const GATE_PROFILES: readonly GateProfile[];
33
- /**
34
- * Declares a Codex mirror of a Claude hook. Claude and Grok both execute the
35
- * canonical `.claude/hooks/` scripts; only Codex runs duplicated copies, so the
36
- * Codex mirror is the entire drift surface the doctor has to watch.
37
- */
38
- interface FakeCodexMirrorSpec {
39
- /** Whether the mirror file is written to `.codex/hooks/`. Default: true. */
40
- present?: boolean;
41
- /**
42
- * Script body for the mirror. Defaults to the Claude original's body (an
43
- * identical, healthy mirror). Supply a different string to model content
44
- * drift between the Codex copy and its Claude original.
45
- */
46
- body?: string;
47
- /** Whether the mirror file carries the executable bit. Default: true. */
48
- executable?: boolean;
49
- /** Explicit file mode override (masked to 0o777). Overrides `executable`. */
50
- mode?: number;
51
- /** Whether the mirror is registered in `.codex/hooks.json`. Default: true. */
52
- registered?: boolean;
53
- /** Optional settings matcher (e.g. "Bash", "Glob"). */
54
- matcher?: string;
55
- }
56
- /** Declares a single hook and its cross-platform state. */
57
- export interface FakeHookSpec {
58
- /** Hook id, e.g. "detect-secrets". Also names the `<id>.sh` file. */
59
- id: string;
60
- /** Script body. Default: a trivial pass-through that exits 0. */
61
- body?: string;
62
- /**
63
- * Whether the `.claude/hooks/<id>.sh` file is written to disk. Default: true.
64
- * Set false to model a missing hook file (combine with `registered: true` for
65
- * a hook that is registered in settings but absent from disk).
66
- */
67
- present?: boolean;
68
- /**
69
- * Whether the hook file carries the executable bit. Default: true. Set false
70
- * to model a present-but-not-executable hook (mode 0o644).
71
- */
72
- executable?: boolean;
73
- /** Explicit file mode override (masked to 0o777). Overrides `executable`. */
74
- mode?: number;
75
- /** Whether the hook is registered in `.claude/settings.json`. Default: true. */
76
- registered?: boolean;
77
- /** Events the hook registers against. Default: SessionStart and PreToolUse. */
78
- events?: HookEventName[];
79
- /** Optional settings matcher (e.g. "Bash", "Glob"). */
80
- matcher?: string;
81
- /**
82
- * Which `hook-gate.sh` profiles list this id. Default: all three. Pass a
83
- * subset (e.g. ["minimal"]) to model a hook id present in only some of the
84
- * three profiles — the exact defect the three-profile check exists to catch.
85
- */
86
- profiles?: GateProfile[];
87
- /**
88
- * Codex mirror configuration. When omitted, a healthy identical mirror is
89
- * created. Pass `false` to model a Claude hook with no Codex counterpart, or
90
- * an object to override the mirror (e.g. `{ body }` to model content drift).
91
- */
92
- codex?: FakeCodexMirrorSpec | false;
93
- }
94
- /**
95
- * Declares the adapter-architecture Codex wiring: `.codex/config.toml` routing
96
- * lifecycle events to `hq-codex-hook-adapter.sh`, which dispatches the
97
- * `.claude/settings.json` hooks live. When this spec is present the tree models
98
- * a current-release root: no `.codex/hooks.json` and no per-hook mirrors are
99
- * written by default (declare a hook's `codex` spec explicitly, or set
100
- * `legacyHooksJson`, to model pre-adapter leftovers).
101
- */
102
- interface FakeCodexAdapterSpec {
103
- /** Events wired to the adapter in config.toml. Default: the shipped eight. */
104
- events?: string[];
105
- /** Whether the adapter script is written. Default: true. */
106
- adapterPresent?: boolean;
107
- /** Whether the adapter script carries the executable bit. Default: true. */
108
- adapterExecutable?: boolean;
109
- /** Whether `core/scripts/lib/hook-adapter-core.sh` is written. Default: true. */
110
- dispatchLibPresent?: boolean;
111
- /** Whether a leftover `.codex/hooks.json` is still written. Default: false. */
112
- legacyHooksJson?: boolean;
113
- }
114
- /** Declares the `.grok/hooks/` state. */
115
- interface FakeGrokSpec {
116
- /** Whether the `.grok/hooks/` scaffold is created at all. Default: true. */
117
- present?: boolean;
118
- /** Whether `hq-grok-hook-adapter.sh` carries the executable bit. Default: true. */
119
- adapterExecutable?: boolean;
120
- /**
121
- * Whether the user-global bridge under `~/.grok/hooks/` is recorded as
122
- * installed. The real bridge lives in the home dir and cannot be simulated in
123
- * a project-local temp tree, so this is a manifest flag only. Default: true.
124
- */
125
- bridgeInstalled?: boolean;
126
- }
127
- /** The declarative spec passed to {@link buildFakeHqTree}. */
128
- export interface FakeHqTreeSpec {
129
- /** Hooks to materialise. Default: none. */
130
- hooks?: FakeHookSpec[];
131
- /** Extra top-level keys merged into `.claude/settings.json`. */
132
- claudeSettings?: Record<string, unknown>;
133
- /**
134
- * Adapter-architecture Codex wiring. When set (object or `true` for a fully
135
- * healthy default), the tree models a current-release root: config.toml +
136
- * adapter + dispatch library, and no hooks.json or per-hook mirrors unless
137
- * explicitly declared. When omitted, the tree keeps the legacy mirror shape.
138
- */
139
- codexAdapter?: FakeCodexAdapterSpec | true;
140
- /** Grok scaffold config, or `false` to omit `.grok/`. Default: healthy. */
141
- grok?: FakeGrokSpec | false;
142
- /** Initialise a git repo at the tree root. Default: false. */
143
- git?: boolean;
144
- /** temp-dir name prefix. Default: "hq-doctor-fake-". */
145
- prefix?: string;
146
- }
147
- /** Manifest entry describing a materialised Codex mirror. */
148
- interface FakeCodexManifestEntry {
149
- /** Absolute path where the mirror lives (whether or not it was written). */
150
- scriptPath: string;
151
- /** Whether the mirror file exists on disk. */
152
- present: boolean;
153
- /** On-disk mode bits (0o777-masked), or null when absent. */
154
- mode: number | null;
155
- /** Whether the mirror carries the executable bit. */
156
- executable: boolean;
157
- /** Whether the mirror is registered in `.codex/hooks.json`. */
158
- registered: boolean;
159
- /** Whether the mirror's content differs from its Claude original. */
160
- drifted: boolean;
161
- }
162
- /** Manifest entry describing a materialised hook across all platforms. */
163
- interface FakeHookManifestEntry {
164
- id: string;
165
- /** Absolute path of the Claude script (whether or not it was written). */
166
- scriptPath: string;
167
- /** Whether the Claude hook file exists on disk. */
168
- present: boolean;
169
- /** On-disk mode bits (0o777-masked), or null when absent. */
170
- mode: number | null;
171
- /** Whether the Claude hook carries the executable bit. */
172
- executable: boolean;
173
- /** Whether the hook is registered in `.claude/settings.json`. */
174
- registered: boolean;
175
- /** Events the hook is registered against. */
176
- events: HookEventName[];
177
- /** Optional settings matcher (e.g. "Bash", "Glob"), or null when unset. */
178
- matcher: string | null;
179
- /** Which `hook-gate.sh` profiles list this id. */
180
- profiles: GateProfile[];
181
- /** The Codex mirror, or null when the hook has no Codex counterpart. */
182
- codex: FakeCodexManifestEntry | null;
183
- }
184
- /** A structured description of everything {@link buildFakeHqTree} wrote. */
185
- interface FakeHqTreeManifest {
186
- root: string;
187
- claudeSettingsPath: string;
188
- claudeHooksDir: string;
189
- claudeHookGatePath: string;
190
- codexHooksJsonPath: string;
191
- codexHooksDir: string;
192
- codexHookGatePath: string;
193
- /** Path of `.codex/config.toml`, or null when no adapter spec was given. */
194
- codexConfigTomlPath: string | null;
195
- /** Path of the adapter script, or null when no adapter spec was given. */
196
- codexAdapterPath: string | null;
197
- grokDir: string | null;
198
- grokAdapterPath: string | null;
199
- grokRegistrationPath: string | null;
200
- grokBridgeInstalled: boolean;
201
- gitInitialised: boolean;
202
- hooks: FakeHookManifestEntry[];
203
- }
204
- /** The handle returned by {@link buildFakeHqTree}. */
205
- export interface FakeHqTree {
206
- /** Absolute, canonicalised tree root under the OS temp dir. */
207
- root: string;
208
- /** Structured description of everything that was written. */
209
- manifest: FakeHqTreeManifest;
210
- /** Join a path relative to the tree root. */
211
- path: (...segments: string[]) => string;
212
- /** Remove this tree from disk. Idempotent. */
213
- cleanup: () => void;
214
- }
215
- /** Remove every fake HQ tree still on disk. Safe to call repeatedly. */
216
- export declare function cleanupAllFakeHqTrees(): void;
217
- /** The set of tree roots this process is still tracking for cleanup. */
218
- export declare function trackedFakeHqTreeRoots(): string[];
219
- /**
220
- * Materialise a fake HQ tree from `spec` and return its root, manifest, and a
221
- * cleanup handle. The tree lives under `os.tmpdir()` and is swept automatically
222
- * when the process exits.
223
- */
224
- export declare function buildFakeHqTree(spec?: FakeHqTreeSpec): FakeHqTree;
225
- export {};
226
- //# sourceMappingURL=fake-hq-tree.d.ts.map
@@ -1,408 +0,0 @@
1
- /**
2
- * buildFakeHqTree — a reusable test harness that materialises a fake HQ tree in
3
- * the OS temp directory from a declarative spec.
4
- *
5
- * Every `hq doctor` check is fundamentally a function of the on-disk shape of an
6
- * HQ tree: which hooks exist, whether they are executable, whether they are
7
- * registered in settings, which `hook-gate.sh` profiles list them, and whether
8
- * the Codex mirror still matches its Claude original. This helper lets a test
9
- * declare that shape — including its deliberately-broken variants — and get back
10
- * a throwaway tree plus a manifest describing exactly what was written, without
11
- * ever touching the real HQ tree.
12
- *
13
- * Design notes:
14
- * - Everything is written under a `mkdtempSync` root inside `os.tmpdir()`. The
15
- * real HQ tree is never read or written.
16
- * - The root path is canonicalised with `realpathSync` so callers can assert
17
- * containment under the temp dir on platforms (macOS) where the temp dir is
18
- * a symlink (`/var` -> `/private/var`).
19
- * - Cleanup is automatic: the first build registers a single `process` exit
20
- * handler that removes every tracked root, so nothing survives a test run,
21
- * including on test failure. `tree.cleanup()` is also exposed for eager
22
- * removal, and is idempotent.
23
- *
24
- * The reference for this pattern is core/scripts/test-codex-hook-adapter.sh,
25
- * which stands up a throwaway HQ tree with `mktemp -d` and stubbed hook scripts.
26
- */
27
- import { spawnSync } from "node:child_process";
28
- import * as fs from "node:fs";
29
- import * as os from "node:os";
30
- import * as path from "node:path";
31
- /** Canonical ordering of the gate profiles. */
32
- export const GATE_PROFILES = [
33
- "minimal",
34
- "standard",
35
- "strict",
36
- ];
37
- const DEFAULT_HOOK_BODY = "#!/bin/bash\ncat >/dev/null\nexit 0\n";
38
- const EXECUTABLE_MODE = 0o755;
39
- const NON_EXECUTABLE_MODE = 0o644;
40
- const DEFAULT_PREFIX = "hq-doctor-fake-";
41
- /** The lifecycle events the shipped `.codex/config.toml` wires. */
42
- const ADAPTER_DEFAULT_EVENTS = [
43
- "SessionStart",
44
- "UserPromptSubmit",
45
- "PreToolUse",
46
- "PostToolUse",
47
- "Stop",
48
- "SubagentStop",
49
- "PreCompact",
50
- "SessionEnd",
51
- ];
52
- // --- Automatic cleanup registry ------------------------------------------------
53
- const trackedRoots = new Set();
54
- let exitHandlerRegistered = false;
55
- /** Remove every fake HQ tree still on disk. Safe to call repeatedly. */
56
- export function cleanupAllFakeHqTrees() {
57
- for (const root of trackedRoots) {
58
- removeTree(root);
59
- }
60
- trackedRoots.clear();
61
- }
62
- /** The set of tree roots this process is still tracking for cleanup. */
63
- export function trackedFakeHqTreeRoots() {
64
- return [...trackedRoots];
65
- }
66
- function removeTree(root) {
67
- try {
68
- fs.rmSync(root, { recursive: true, force: true });
69
- }
70
- catch {
71
- // Best-effort: never let cleanup throw and mask a test's real failure.
72
- }
73
- }
74
- /**
75
- * Register a single process-exit handler that sweeps every tracked root. Bound
76
- * once (guarded by a module flag) so repeated builds do not pile up listeners.
77
- * `exit` fires on normal completion and after a failed test run alike, which is
78
- * what guarantees no temp dir survives.
79
- */
80
- function ensureExitHandler() {
81
- if (exitHandlerRegistered)
82
- return;
83
- exitHandlerRegistered = true;
84
- process.on("exit", cleanupAllFakeHqTrees);
85
- }
86
- // --- Builder -------------------------------------------------------------------
87
- /**
88
- * Materialise a fake HQ tree from `spec` and return its root, manifest, and a
89
- * cleanup handle. The tree lives under `os.tmpdir()` and is swept automatically
90
- * when the process exits.
91
- */
92
- export function buildFakeHqTree(spec = {}) {
93
- ensureExitHandler();
94
- const prefix = spec.prefix ?? DEFAULT_PREFIX;
95
- const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), prefix)));
96
- trackedRoots.add(root);
97
- const claudeHooksDir = path.join(root, ".claude", "hooks");
98
- const codexHooksDir = path.join(root, ".codex", "hooks");
99
- fs.mkdirSync(claudeHooksDir, { recursive: true });
100
- fs.mkdirSync(codexHooksDir, { recursive: true });
101
- // A `core/` directory is what marks this as an HQ tree for later root
102
- // discovery; cheap to add and keeps the fixture realistic.
103
- fs.mkdirSync(path.join(root, "core", "scripts"), { recursive: true });
104
- const adapterSpec = spec.codexAdapter === true ? {} : (spec.codexAdapter ?? null);
105
- const hookSpecs = spec.hooks ?? [];
106
- const hookEntries = hookSpecs.map((hook) =>
107
- // Adapter-architecture trees have no per-hook mirrors unless a hook
108
- // explicitly declares one (to model a pre-adapter leftover).
109
- writeHook(claudeHooksDir, codexHooksDir, hook, adapterSpec === null));
110
- // hook-gate.sh carries the three profile lists, aggregated across every
111
- // hook's `profiles`. Legacy trees mirror it to `.codex/hooks/` too; adapter
112
- // trees run the Claude gate directly, so no mirror is written there.
113
- const gateSource = renderHookGate(hookEntries);
114
- const claudeHookGatePath = path.join(claudeHooksDir, "hook-gate.sh");
115
- const codexHookGatePath = path.join(codexHooksDir, "hook-gate.sh");
116
- writeScript(claudeHookGatePath, gateSource, EXECUTABLE_MODE);
117
- if (adapterSpec === null) {
118
- writeScript(codexHookGatePath, gateSource, EXECUTABLE_MODE);
119
- }
120
- const claudeSettingsPath = path.join(root, ".claude", "settings.json");
121
- fs.writeFileSync(claudeSettingsPath, JSON.stringify(renderClaudeSettings(hookEntries, spec.claudeSettings), null, 2) + "\n");
122
- const codexHooksJsonPath = path.join(root, ".codex", "hooks.json");
123
- if (adapterSpec === null || adapterSpec.legacyHooksJson) {
124
- fs.writeFileSync(codexHooksJsonPath, JSON.stringify(renderCodexHooksJson(root, hookEntries), null, 2) + "\n");
125
- }
126
- const adapterPaths = writeCodexAdapter(root, codexHooksDir, adapterSpec);
127
- const grok = writeGrok(root, spec.grok);
128
- let gitInitialised = false;
129
- if (spec.git) {
130
- gitInitialised = initGitRepo(root);
131
- }
132
- const manifest = {
133
- root,
134
- claudeSettingsPath,
135
- claudeHooksDir,
136
- claudeHookGatePath,
137
- codexHooksJsonPath,
138
- codexHooksDir,
139
- codexHookGatePath,
140
- codexConfigTomlPath: adapterPaths.configTomlPath,
141
- codexAdapterPath: adapterPaths.adapterPath,
142
- grokDir: grok.dir,
143
- grokAdapterPath: grok.adapterPath,
144
- grokRegistrationPath: grok.registrationPath,
145
- grokBridgeInstalled: grok.bridgeInstalled,
146
- gitInitialised,
147
- hooks: hookEntries,
148
- };
149
- let cleaned = false;
150
- const cleanup = () => {
151
- if (cleaned)
152
- return;
153
- cleaned = true;
154
- removeTree(root);
155
- trackedRoots.delete(root);
156
- };
157
- return {
158
- root,
159
- manifest,
160
- path: (...segments) => path.join(root, ...segments),
161
- cleanup,
162
- };
163
- }
164
- // --- Hook materialisation ------------------------------------------------------
165
- function writeHook(claudeHooksDir, codexHooksDir, hook, defaultMirror) {
166
- const claudeBody = hook.body ?? DEFAULT_HOOK_BODY;
167
- const present = hook.present !== false;
168
- const registered = hook.registered !== false;
169
- const events = hook.events ?? ["SessionStart", "PreToolUse"];
170
- const profiles = hook.profiles ?? [...GATE_PROFILES];
171
- const scriptPath = path.join(claudeHooksDir, `${hook.id}.sh`);
172
- let mode = null;
173
- let executable = false;
174
- if (present) {
175
- const targetMode = resolveMode(hook.mode, hook.executable);
176
- writeScript(scriptPath, claudeBody, targetMode);
177
- mode = statMode(scriptPath);
178
- executable = isExecutable(mode);
179
- }
180
- const codex = writeCodexMirror(codexHooksDir, hook, claudeBody, defaultMirror);
181
- return {
182
- id: hook.id,
183
- scriptPath,
184
- present,
185
- mode,
186
- executable,
187
- registered,
188
- events,
189
- matcher: hook.matcher ?? null,
190
- profiles,
191
- codex,
192
- };
193
- }
194
- function writeCodexMirror(codexHooksDir, hook, claudeBody, defaultMirror) {
195
- // `codex: false` => a Claude hook with no Codex counterpart. An undeclared
196
- // mirror defaults to healthy on legacy trees and to absent on adapter trees.
197
- if (hook.codex === false)
198
- return null;
199
- if (hook.codex === undefined && !defaultMirror)
200
- return null;
201
- const codexSpec = hook.codex ?? {};
202
- const present = codexSpec.present !== false;
203
- const registered = codexSpec.registered !== false;
204
- const body = codexSpec.body ?? claudeBody;
205
- const drifted = body !== claudeBody;
206
- const scriptPath = path.join(codexHooksDir, `${hook.id}.sh`);
207
- let mode = null;
208
- let executable = false;
209
- if (present) {
210
- const targetMode = resolveMode(codexSpec.mode, codexSpec.executable);
211
- writeScript(scriptPath, body, targetMode);
212
- mode = statMode(scriptPath);
213
- executable = isExecutable(mode);
214
- }
215
- return { scriptPath, present, mode, executable, registered, drifted };
216
- }
217
- function renderClaudeSettings(hooks, extra) {
218
- const events = {};
219
- for (const hook of hooks) {
220
- if (!hook.registered)
221
- continue;
222
- const command = `bash "$CLAUDE_PROJECT_DIR/.claude/hooks/hook-gate.sh" ${hook.id} ` +
223
- `"$CLAUDE_PROJECT_DIR/.claude/hooks/${hook.id}.sh"`;
224
- for (const event of hook.events) {
225
- (events[event] ??= []).push(makeSettingsEntry(command, hook.matcher ?? undefined));
226
- }
227
- }
228
- return { ...extra, hooks: events };
229
- }
230
- function renderCodexHooksJson(root, hooks) {
231
- const events = {};
232
- for (const hook of hooks) {
233
- if (!hook.codex || !hook.codex.registered)
234
- continue;
235
- const gate = `${root}/.codex/hooks/hook-gate.sh`;
236
- const script = `${root}/.codex/hooks/${hook.id}.sh`;
237
- const command = `'${gate}' ${hook.id} '${script}'`;
238
- for (const event of hook.events) {
239
- (events[event] ??= []).push(makeSettingsEntry(command, hook.matcher ?? undefined));
240
- }
241
- }
242
- return { hooks: events };
243
- }
244
- function makeSettingsEntry(command, matcher) {
245
- const entry = {
246
- hooks: [{ type: "command", command, timeout: 5 }],
247
- };
248
- if (matcher)
249
- entry.matcher = matcher;
250
- return entry;
251
- }
252
- /**
253
- * Render a self-contained `hook-gate.sh` whose three profile functions list the
254
- * hook ids assigned to each profile, modelled on the real gate in
255
- * `.claude/hooks/hook-gate.sh`. Self-contained so the fixture never has to
256
- * source the real HQ tree.
257
- */
258
- function renderHookGate(hooks) {
259
- const byProfile = {
260
- minimal: [],
261
- standard: [],
262
- strict: [],
263
- };
264
- for (const hook of hooks) {
265
- for (const profile of hook.profiles) {
266
- byProfile[profile].push(hook.id);
267
- }
268
- }
269
- const fns = GATE_PROFILES.map((profile) => renderProfileFunction(profile, byProfile[profile])).join("\n\n");
270
- return `#!/bin/bash
271
- # Fake hook-gate.sh generated by buildFakeHqTree (test fixture — not the real gate).
272
- set -euo pipefail
273
-
274
- if [ $# -lt 2 ]; then
275
- echo "USAGE: hook-gate.sh <hook-id> <actual-hook-script> [args...]" >&2
276
- exit 1
277
- fi
278
-
279
- HOOK_ID="$1"
280
- HOOK_SCRIPT="$2"
281
- shift 2
282
-
283
- PROFILE="\${HQ_HOOK_PROFILE:-standard}"
284
-
285
- ${fns}
286
-
287
- should_run=0
288
- case "$PROFILE" in
289
- minimal)
290
- if is_in_minimal_profile "$HOOK_ID"; then should_run=1; fi
291
- ;;
292
- standard)
293
- if is_in_standard_profile "$HOOK_ID"; then should_run=1; fi
294
- ;;
295
- strict)
296
- if is_in_strict_profile "$HOOK_ID"; then should_run=1; fi
297
- ;;
298
- *)
299
- echo "ERROR: Unknown profile '$PROFILE'. Use minimal|standard|strict" >&2
300
- exit 1
301
- ;;
302
- esac
303
-
304
- if [ "$should_run" -eq 0 ]; then
305
- exit 0
306
- fi
307
-
308
- exec "$HOOK_SCRIPT" "$@"
309
- `;
310
- }
311
- function renderProfileFunction(profile, ids) {
312
- const caseBlock = ids.length > 0 ? ` ${ids.join("|")})\n return 0\n ;;\n` : "";
313
- return `is_in_${profile}_profile() {
314
- case "$1" in
315
- ${caseBlock} *)
316
- return 1
317
- ;;
318
- esac
319
- }`;
320
- }
321
- /**
322
- * Materialise the adapter-architecture wiring: `.codex/config.toml` routing the
323
- * spec'd events to `hq-codex-hook-adapter.sh`, the adapter script itself, and
324
- * the `core/scripts/lib/hook-adapter-core.sh` dispatch library it sources —
325
- * mirroring the shape the hq-core release ships.
326
- */
327
- function writeCodexAdapter(root, codexHooksDir, spec) {
328
- if (spec === null)
329
- return { configTomlPath: null, adapterPath: null };
330
- const events = spec.events ?? [...ADAPTER_DEFAULT_EVENTS];
331
- const adapterPath = path.join(codexHooksDir, "hq-codex-hook-adapter.sh");
332
- const command = `exec /bin/bash "$PWD/.codex/hooks/hq-codex-hook-adapter.sh"`;
333
- const blocks = events.map((event) => `[[hooks.${event}]]\n\n[[hooks.${event}.hooks]]\ntype = "command"\ncommand = '${command}'\ntimeout = 30\n`);
334
- const configTomlPath = path.join(root, ".codex", "config.toml");
335
- fs.writeFileSync(configTomlPath, `[features]\nhooks = true\n\n${blocks.join("\n")}`);
336
- if (spec.adapterPresent !== false) {
337
- writeScript(adapterPath, "#!/bin/bash\ncat >/dev/null\nexit 0\n", spec.adapterExecutable === false ? NON_EXECUTABLE_MODE : EXECUTABLE_MODE);
338
- }
339
- if (spec.dispatchLibPresent !== false) {
340
- const libPath = path.join(root, "core", "scripts", "lib");
341
- fs.mkdirSync(libPath, { recursive: true });
342
- fs.writeFileSync(path.join(libPath, "hook-adapter-core.sh"), "# fake hook-adapter-core.sh (test fixture)\n");
343
- }
344
- return { configTomlPath, adapterPath };
345
- }
346
- function writeGrok(root, spec) {
347
- const absent = {
348
- dir: null,
349
- adapterPath: null,
350
- registrationPath: null,
351
- bridgeInstalled: false,
352
- };
353
- if (spec === false)
354
- return absent;
355
- const grokSpec = spec ?? {};
356
- if (grokSpec.present === false)
357
- return absent;
358
- const dir = path.join(root, ".grok", "hooks");
359
- fs.mkdirSync(dir, { recursive: true });
360
- const adapterPath = path.join(dir, "hq-grok-hook-adapter.sh");
361
- writeScript(adapterPath, "#!/bin/bash\ncat >/dev/null\nexit 0\n", grokSpec.adapterExecutable === false ? NON_EXECUTABLE_MODE : EXECUTABLE_MODE);
362
- const registrationPath = path.join(dir, "hq-grok.json");
363
- const registration = {
364
- hooks: {
365
- PreToolUse: [
366
- {
367
- hooks: [
368
- { type: "command", command: "./hq-grok-hook-adapter.sh", timeout: 30 },
369
- ],
370
- },
371
- ],
372
- },
373
- };
374
- fs.writeFileSync(registrationPath, JSON.stringify(registration, null, 2) + "\n");
375
- return {
376
- dir,
377
- adapterPath,
378
- registrationPath,
379
- bridgeInstalled: grokSpec.bridgeInstalled !== false,
380
- };
381
- }
382
- // --- git -----------------------------------------------------------------------
383
- function initGitRepo(root) {
384
- // Seed a git repo the way the reference harness does, without shelling out
385
- // for identity config: write it straight into .git/config.
386
- const res = spawnSync("git", ["init", "-q", "-b", "main"], { cwd: root });
387
- if (res.status !== 0)
388
- return false;
389
- fs.appendFileSync(path.join(root, ".git", "config"), "[user]\n\temail = fake-hq-tree@example.com\n\tname = fake-hq-tree\n[commit]\n\tgpgsign = false\n");
390
- return true;
391
- }
392
- // --- Low-level helpers ---------------------------------------------------------
393
- function resolveMode(explicit, executable) {
394
- if (explicit !== undefined)
395
- return explicit & 0o777;
396
- return executable === false ? NON_EXECUTABLE_MODE : EXECUTABLE_MODE;
397
- }
398
- function writeScript(target, body, mode) {
399
- fs.writeFileSync(target, body);
400
- fs.chmodSync(target, mode);
401
- }
402
- function statMode(target) {
403
- return fs.statSync(target).mode & 0o777;
404
- }
405
- function isExecutable(mode) {
406
- return mode !== null && (mode & 0o111) !== 0;
407
- }
408
- //# sourceMappingURL=fake-hq-tree.js.map
@@ -1,8 +0,0 @@
1
- import { type RunDoctorOptions, type RunDoctorResult } from "../../../commands/doctor.js";
2
- /**
3
- * Run doctor tests against synthetic state. The production registry normally
4
- * reads local HQ work-context, sync, daemon, and session state; tests must not
5
- * inspect the developer's HOME or request the live integrations inventory.
6
- */
7
- export declare function runDoctorForTest(options?: RunDoctorOptions): Promise<RunDoctorResult>;
8
- //# sourceMappingURL=run-doctor.d.ts.map