@indigoai-us/hq-cli 5.345.267 → 5.345.269

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 (52) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +12 -0
  3. package/contracts/host-canary-release.json +7 -7
  4. package/dist/commands/core-job-notify-native.d.ts +6 -0
  5. package/dist/commands/core-job-notify-native.js +64 -4
  6. package/dist/commands/core-jobs-native.js +3 -16
  7. package/dist/commands/install-global.js +54 -17
  8. package/dist/lib/daemon/machine-check-owner.js +1 -1
  9. package/dist/lib/daemon/machine-checks.d.ts +3 -11
  10. package/dist/lib/daemon/machine-checks.js +4 -15
  11. package/dist/lib/daemon/run.js +12 -0
  12. package/dist/lib/doctor/checks/codex-wiring.d.ts +4 -2
  13. package/dist/lib/doctor/checks/codex-wiring.js +70 -13
  14. package/dist/lib/doctor/checks/fleet-hook-core.d.ts +4 -4
  15. package/dist/lib/doctor/checks/fleet-hook-core.js +5 -10
  16. package/dist/lib/doctor/checks/grok-wiring.js +38 -0
  17. package/dist/lib/doctor/checks/hook-engine.js +0 -2
  18. package/dist/lib/doctor/checks/host-core.d.ts +1 -1
  19. package/dist/lib/doctor/checks/host-core.js +8 -38
  20. package/dist/lib/hook-engine/coverage.d.ts +10 -1
  21. package/dist/lib/hook-engine/coverage.js +63 -8
  22. package/dist/lib/hook-engine/fleet-registration.js +7 -4
  23. package/dist/lib/hook-engine/native-precompact-compatibility.json +7 -1
  24. package/dist/lib/hook-engine/native-stop-compatibility.json +30 -11
  25. package/dist/lib/hook-engine/reconcile.d.ts +10 -1
  26. package/dist/lib/hook-engine/reconcile.js +339 -42
  27. package/dist/lib/hook-engine/stable-gate.d.ts +43 -0
  28. package/dist/lib/hook-engine/stable-gate.js +276 -0
  29. package/dist/lib/hot-channels/fleet-complete.js +2 -2
  30. package/dist/lib/install/antigravity-targets.js +2 -1
  31. package/dist/lib/install/claude-targets.d.ts +6 -6
  32. package/dist/lib/install/claude-targets.js +13 -9
  33. package/dist/lib/install/codex-targets.d.ts +4 -4
  34. package/dist/lib/install/codex-targets.js +26 -16
  35. package/dist/lib/install/hook-target.d.ts +23 -2
  36. package/dist/lib/install/hook-target.js +89 -16
  37. package/dist/lib/outpost-identity-env.d.ts +18 -0
  38. package/dist/lib/outpost-identity-env.js +29 -0
  39. package/dist/lib/signals/steps/extract.js +46 -12
  40. package/dist/utils/core-next-kill.d.ts +2 -15
  41. package/dist/utils/core-next-kill.js +5 -36
  42. package/dist/utils/hook-engine-crash.d.ts +86 -0
  43. package/dist/utils/hook-engine-crash.js +325 -0
  44. package/dist/utils/hook-engine-rollup.d.ts +8 -3
  45. package/dist/utils/hook-engine-rollup.js +0 -1
  46. package/dist/utils/hook-trust.d.ts +2 -0
  47. package/dist/utils/hook-trust.js +156 -13
  48. package/dist/utils/host-registration.d.ts +158 -4
  49. package/dist/utils/host-registration.js +500 -17
  50. package/dist/utils/version-gate.d.ts +42 -4
  51. package/dist/utils/version-gate.js +84 -25
  52. package/package.json +7 -7
@@ -0,0 +1,43 @@
1
+ import { type CoreNextTarget } from '../../utils/host-registration.js';
2
+ export declare const MACHINE_HOOK_HOSTS: readonly ["claude", "codex", "grok", "antigravity"];
3
+ export type MachineHookHost = (typeof MACHINE_HOOK_HOSTS)[number];
4
+ /**
5
+ * Every host (`claude`, `codex`, `grok`, `antigravity`) that currently has HQ
6
+ * hook entries on disk on this machine (project or user scope).
7
+ */
8
+ export declare function detectMachineHookHosts(root: string, home: string, env?: NodeJS.ProcessEnv): MachineHookHost[];
9
+ export interface StableGateDecision {
10
+ allowed: boolean;
11
+ /** Every host required to accept before `stable` may move. */
12
+ checkedHosts: MachineHookHost[];
13
+ /** Every host (required or optional) that accepted the candidate. */
14
+ acceptedHosts: MachineHookHost[];
15
+ /** Required hosts that refused the candidate. */
16
+ refusedHosts: MachineHookHost[];
17
+ /** Per-host narrowed CoreNextTarget results. */
18
+ targets: Partial<Record<MachineHookHost, CoreNextTarget | null>>;
19
+ claudeProject: CoreNextTarget | null;
20
+ /** Operator-facing warning naming the refusing host(s) when `!allowed`. */
21
+ refusalSummary?: string;
22
+ }
23
+ /**
24
+ * Decide whether the shared `<home>/.hq/bin/hq-hook/stable` link may move to
25
+ * `rawCoreNext` (`engineVersion`) on this machine.
26
+ *
27
+ * `requiredHosts` (typically the host(s) being installed/reconciled) is unioned
28
+ * with every host that already has HQ hook entries on disk
29
+ * ([`detectMachineHookHosts`]). `stable` may move only when EVERY host in that
30
+ * union accepts the candidate. `optionalHosts` (such as Codex project config
31
+ * when newly seeding on a fresh root) are evaluated and recorded if accepted,
32
+ * without blocking if they have no entries on disk yet.
33
+ */
34
+ export declare function evaluateStableCommit(options: {
35
+ rawCoreNext: CoreNextTarget;
36
+ root: string;
37
+ home: string;
38
+ env: NodeJS.ProcessEnv;
39
+ engineVersion?: string;
40
+ requiredHosts: readonly MachineHookHost[];
41
+ optionalHosts?: readonly MachineHookHost[];
42
+ }): StableGateDecision;
43
+ //# sourceMappingURL=stable-gate.d.ts.map
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Shared `<home>/.hq/bin/hq-hook/stable` link commit gate (US-027 M-1).
3
+ *
4
+ * The host canary gate decides per host (`claude`, `codex`, `grok`, plus
5
+ * capability/registration checks for `antigravity`), but
6
+ * `<home>/.hq/bin/hq-hook/stable` is one link shared by every host on the
7
+ * machine. On a native-only HQ root where the legacy bash dispatchers were
8
+ * removed, moving `stable` when one host accepts a new engine while another
9
+ * host with HQ hook entries on this machine refuses it would silently run the
10
+ * refused engine on that other host.
11
+ *
12
+ * This module is the single source of truth used by `hq install --global`,
13
+ * `hq reindex`, and Grok trust to:
14
+ * 1. Detect every host (`claude`, `codex`, `grok`, `antigravity`) that has HQ
15
+ * hook entries on this machine.
16
+ * 2. Check whether every such host (plus the host(s) being installed) accepts
17
+ * the candidate engine.
18
+ * 3. Refuse moving `stable` — and name the refusing host(s) — whenever any
19
+ * active host refuses the candidate.
20
+ */
21
+ import * as fs from 'node:fs';
22
+ import * as path from 'node:path';
23
+ import { ANTIGRAVITY_HOOK_EVENTS, ANTIGRAVITY_HOOK_NAME, antigravityEventEntries, antigravityTargets, } from '../install/antigravity-targets.js';
24
+ import { isHqHookEntry } from '../install/claude-targets.js';
25
+ import { isAdapterEntry } from '../install/codex-targets.js';
26
+ import { isCoreNextEntry, isNativeEntry } from './registration.js';
27
+ import { userHookSettingsFile } from './registration-identity.js';
28
+ import { claudeProjectTarget, forHost } from './coverage.js';
29
+ import { isPortableNativeEntry, isShippedPortableClaudeCommand, STABLE_LINK_NAME, } from '../../utils/host-registration.js';
30
+ export const MACHINE_HOOK_HOSTS = ['claude', 'codex', 'grok', 'antigravity'];
31
+ const CODEX_NATIVE_BEGIN = '# BEGIN HQ NATIVE HOOKS (managed by `hq reindex`)';
32
+ function referencesStableEngine(entry) {
33
+ const fields = [entry.command, entry.commandWindows];
34
+ return fields.some((value) => {
35
+ if (typeof value !== 'string')
36
+ return false;
37
+ return value.includes('/.hq/bin/hq-hook/stable')
38
+ || value.includes('\\.hq\\bin\\hq-hook\\stable');
39
+ });
40
+ }
41
+ function jsonHookEntries(file) {
42
+ if (!fs.existsSync(file))
43
+ return null;
44
+ let raw;
45
+ try {
46
+ raw = fs.readFileSync(file, 'utf8');
47
+ }
48
+ catch {
49
+ return 'unreadable';
50
+ }
51
+ let doc;
52
+ try {
53
+ doc = JSON.parse(raw);
54
+ }
55
+ catch {
56
+ return raw.includes('hq-hook') || raw.includes('master-hook.sh') || raw.includes('hq-codex-hook-adapter.sh') || raw.includes('hq-hq-bridge.sh')
57
+ ? 'unreadable'
58
+ : null;
59
+ }
60
+ const hooks = doc && typeof doc === 'object' && !Array.isArray(doc)
61
+ ? doc.hooks
62
+ : undefined;
63
+ if (!hooks || typeof hooks !== 'object' || Array.isArray(hooks))
64
+ return [];
65
+ const out = [];
66
+ for (const [event, groups] of Object.entries(hooks)) {
67
+ if (!Array.isArray(groups))
68
+ continue;
69
+ for (const group of groups) {
70
+ if (!group || typeof group !== 'object' || !Array.isArray(group.hooks))
71
+ continue;
72
+ for (const h of group.hooks) {
73
+ if (h && typeof h === 'object' && !Array.isArray(h)) {
74
+ out.push([event, h]);
75
+ }
76
+ }
77
+ }
78
+ }
79
+ return out;
80
+ }
81
+ function hasClaudeProjectEntries(root) {
82
+ for (const file of [path.join(root, '.claude', 'settings.json'), path.join(root, '.claude', 'settings.local.json')]) {
83
+ const entries = jsonHookEntries(file);
84
+ if (entries === 'unreadable')
85
+ return true;
86
+ if (!entries)
87
+ continue;
88
+ for (const [event, entry] of entries) {
89
+ if (isCoreNextEntry(entry, root)
90
+ || isNativeEntry(entry, root)
91
+ || referencesStableEngine(entry)
92
+ || (typeof entry.command === 'string' && (isShippedPortableClaudeCommand(entry.command, event)
93
+ || entry.command.includes('master-hook.sh')))) {
94
+ return true;
95
+ }
96
+ }
97
+ }
98
+ return false;
99
+ }
100
+ function hasClaudeUserEntries(root, home, env) {
101
+ const entries = jsonHookEntries(userHookSettingsFile('claude', home, env));
102
+ if (entries === 'unreadable')
103
+ return true;
104
+ if (!entries)
105
+ return false;
106
+ return entries.some(([, entry]) => isHqHookEntry(entry, root) || referencesStableEngine(entry));
107
+ }
108
+ function hasCodexEntries(root, home, env) {
109
+ for (const file of [userHookSettingsFile('codex', home, env), path.join(root, '.codex', 'hooks.json')]) {
110
+ const entries = jsonHookEntries(file);
111
+ if (entries === 'unreadable')
112
+ return true;
113
+ if (entries && entries.some(([, entry]) => isAdapterEntry(entry, root) || referencesStableEngine(entry))) {
114
+ return true;
115
+ }
116
+ }
117
+ const configToml = path.join(root, '.codex', 'config.toml');
118
+ if (fs.existsSync(configToml)) {
119
+ try {
120
+ const text = fs.readFileSync(configToml, 'utf8');
121
+ if (text.includes(CODEX_NATIVE_BEGIN)
122
+ || text.includes('/.hq/bin/hq-hook/stable')
123
+ || text.includes('\\.hq\\bin\\hq-hook\\stable')
124
+ || text.includes('hq-codex-hook-adapter.sh')) {
125
+ return true;
126
+ }
127
+ }
128
+ catch {
129
+ return true;
130
+ }
131
+ }
132
+ return false;
133
+ }
134
+ function hasGrokEntries(root, home) {
135
+ const bridge = path.join(home, '.grok', 'hooks', 'hq-hq-bridge.json');
136
+ const entries = jsonHookEntries(bridge);
137
+ if (entries === 'unreadable')
138
+ return true;
139
+ if (!entries)
140
+ return false;
141
+ return entries.some(([, entry]) => (isCoreNextEntry(entry, root)
142
+ || isPortableNativeEntry(entry, 'grok', root, 'in-tree')
143
+ || referencesStableEngine(entry)
144
+ || (typeof entry.command === 'string' && (entry.command.includes('hq-hq-bridge.sh')
145
+ || entry.command.includes('hq-grok-hook-adapter.sh')))));
146
+ }
147
+ function antigravityRegisteredEvents(root, home) {
148
+ const file = antigravityTargets(home).hooksFile;
149
+ if (!fs.existsSync(file))
150
+ return null;
151
+ let raw;
152
+ try {
153
+ raw = fs.readFileSync(file, 'utf8');
154
+ }
155
+ catch {
156
+ return 'unreadable';
157
+ }
158
+ let doc;
159
+ try {
160
+ doc = JSON.parse(raw);
161
+ }
162
+ catch {
163
+ return raw.includes('hq-hook') ? 'unreadable' : null;
164
+ }
165
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc))
166
+ return null;
167
+ const hqSpec = doc[ANTIGRAVITY_HOOK_NAME];
168
+ if (!hqSpec || typeof hqSpec !== 'object' || Array.isArray(hqSpec))
169
+ return null;
170
+ const byEvent = antigravityEventEntries({ [ANTIGRAVITY_HOOK_NAME]: hqSpec });
171
+ const events = [];
172
+ for (const [event, list] of byEvent) {
173
+ if (list.some((entry) => (isCoreNextEntry(entry, root)
174
+ || isPortableNativeEntry(entry, 'antigravity', root, 'project')
175
+ || referencesStableEngine(entry)))) {
176
+ events.push(event);
177
+ }
178
+ }
179
+ return events.length > 0 ? events : null;
180
+ }
181
+ /**
182
+ * Every host (`claude`, `codex`, `grok`, `antigravity`) that currently has HQ
183
+ * hook entries on disk on this machine (project or user scope).
184
+ */
185
+ export function detectMachineHookHosts(root, home, env = process.env) {
186
+ const hosts = [];
187
+ if (hasClaudeProjectEntries(root) || hasClaudeUserEntries(root, home, env))
188
+ hosts.push('claude');
189
+ if (hasCodexEntries(root, home, env))
190
+ hosts.push('codex');
191
+ if (hasGrokEntries(root, home))
192
+ hosts.push('grok');
193
+ if (antigravityRegisteredEvents(root, home) !== null)
194
+ hosts.push('antigravity');
195
+ return hosts;
196
+ }
197
+ function evaluateAntigravity(rawCoreNext, root, home) {
198
+ if (!rawCoreNext.registrationHosts?.includes('antigravity'))
199
+ return null;
200
+ if (rawCoreNext.canary && !rawCoreNext.canary(root, 'antigravity'))
201
+ return null;
202
+ const registered = antigravityRegisteredEvents(root, home);
203
+ const neededEvents = Array.isArray(registered) && registered.length > 0
204
+ ? registered
205
+ : [...ANTIGRAVITY_HOOK_EVENTS];
206
+ const covered = neededEvents.every((event) => rawCoreNext.events.includes(event));
207
+ if (!covered)
208
+ return null;
209
+ const events = ANTIGRAVITY_HOOK_EVENTS.filter((event) => rawCoreNext.events.includes(event));
210
+ return events.length > 0 ? { ...rawCoreNext, events } : null;
211
+ }
212
+ /**
213
+ * Decide whether the shared `<home>/.hq/bin/hq-hook/stable` link may move to
214
+ * `rawCoreNext` (`engineVersion`) on this machine.
215
+ *
216
+ * `requiredHosts` (typically the host(s) being installed/reconciled) is unioned
217
+ * with every host that already has HQ hook entries on disk
218
+ * ([`detectMachineHookHosts`]). `stable` may move only when EVERY host in that
219
+ * union accepts the candidate. `optionalHosts` (such as Codex project config
220
+ * when newly seeding on a fresh root) are evaluated and recorded if accepted,
221
+ * without blocking if they have no entries on disk yet.
222
+ */
223
+ export function evaluateStableCommit(options) {
224
+ const { rawCoreNext, root, home, env, engineVersion } = options;
225
+ const existingHosts = detectMachineHookHosts(root, home, env);
226
+ const requiredSet = new Set([...options.requiredHosts, ...existingHosts]);
227
+ const optionalSet = new Set(options.optionalHosts ?? []);
228
+ const checkedHosts = MACHINE_HOOK_HOSTS.filter((h) => requiredSet.has(h));
229
+ const evalHosts = MACHINE_HOOK_HOSTS.filter((h) => requiredSet.has(h) || optionalSet.has(h));
230
+ const targets = {};
231
+ let claudeProject = null;
232
+ const acceptedHosts = [];
233
+ const refusedHosts = [];
234
+ for (const host of evalHosts) {
235
+ let hostTarget;
236
+ if (host === 'claude') {
237
+ const claudeNext = forHost(rawCoreNext, root, 'claude', env);
238
+ claudeProject = claudeProjectTarget(rawCoreNext, claudeNext, root, env);
239
+ const accepted = Boolean(claudeNext || claudeProject);
240
+ targets.claude = claudeNext;
241
+ if (accepted)
242
+ acceptedHosts.push('claude');
243
+ else if (requiredSet.has('claude'))
244
+ refusedHosts.push('claude');
245
+ continue;
246
+ }
247
+ if (host === 'antigravity') {
248
+ hostTarget = evaluateAntigravity(rawCoreNext, root, home);
249
+ }
250
+ else {
251
+ hostTarget = forHost(rawCoreNext, root, host, env);
252
+ }
253
+ targets[host] = hostTarget;
254
+ if (hostTarget)
255
+ acceptedHosts.push(host);
256
+ else if (requiredSet.has(host))
257
+ refusedHosts.push(host);
258
+ }
259
+ const allowed = checkedHosts.length > 0 && refusedHosts.length === 0;
260
+ let refusalSummary;
261
+ if (!allowed && refusedHosts.length > 0) {
262
+ const stableLink = path.join(home, '.hq', 'bin', 'hq-hook', STABLE_LINK_NAME);
263
+ const ver = engineVersion ? `hq-hook ${engineVersion}` : 'the new hook engine';
264
+ refusalSummary = `hq: warning: keeping ${stableLink} on its current engine instead of ${ver} because ${refusedHosts.join(', ')} refused it on ${root}`;
265
+ }
266
+ return {
267
+ allowed,
268
+ checkedHosts,
269
+ acceptedHosts,
270
+ refusedHosts,
271
+ targets,
272
+ claudeProject,
273
+ ...(refusalSummary ? { refusalSummary } : {}),
274
+ };
275
+ }
276
+ //# sourceMappingURL=stable-gate.js.map
@@ -172,10 +172,10 @@ export function fleetFailureClass(code) {
172
172
  /** A log-safe message for a helper error: the code and the fix, never provider text. */
173
173
  export function fleetFailureMessage(target, code, detail) {
174
174
  if (code === "signed_out") {
175
- return `${target.botName}'s own model login is not usable (hq-fleet-local complete: signed_out). Signals run on the login ${target.botName} answers chat with; sign ${target.botName} in again (hq bot auth ${target.botName}) and the queued documents run on the next pass`;
175
+ return `${target.botName}'s own model login is not usable (hq-fleet-local complete: signed_out). Signals run on the login ${target.botName} answers chat with; sign ${target.botName} in again (hq bot auth ${target.botName} --provider ${target.runtime}) and the queued documents run on the next pass`;
176
176
  }
177
177
  if (code === "api_key_login") {
178
- return `${target.botName} signs in with an API key, and signals run only on a subscription sign-in (hq-fleet-local complete: api_key_login). Sign ${target.botName} in with a Claude or ChatGPT subscription (hq bot auth ${target.botName})`;
178
+ return `${target.botName} signs in with an API key, and signals run only on a subscription sign-in (hq-fleet-local complete: api_key_login). Sign ${target.botName} in with a Claude or ChatGPT subscription (hq bot auth ${target.botName} --provider ${target.runtime})`;
179
179
  }
180
180
  return `hq-fleet-local complete failed (${code})${detail}`;
181
181
  }
@@ -18,6 +18,7 @@ import * as path from 'node:path';
18
18
  import { charterPath } from './claude-targets.js';
19
19
  import { matchFrontmatter } from '../../utils/frontmatter.js';
20
20
  import { isCoreNextEntry } from '../hook-engine/registration-identity.js';
21
+ import { isPortableNativeEntry } from '../../utils/host-registration.js';
21
22
  export const ANTIGRAVITY_BUNDLE_ID = 'com.google.antigravity';
22
23
  export const ANTIGRAVITY_CLI_BINARY = 'agy';
23
24
  export const ANTIGRAVITY_RULE_MARKER = '<!-- hq-anywhere:antigravity (managed by `hq install --global`; edits are overwritten) -->';
@@ -195,7 +196,7 @@ export function removeAntigravityHqHooks(doc, root) {
195
196
  if (!existing || typeof existing !== 'object' || Array.isArray(existing))
196
197
  return doc;
197
198
  const entries = antigravityEventEntries({ [ANTIGRAVITY_HOOK_NAME]: existing }).flatMap(([, list]) => list);
198
- if (entries.length === 0 || !entries.every((entry) => isCoreNextEntry(entry, root)))
199
+ if (entries.length === 0 || !entries.every((entry) => isCoreNextEntry(entry, root) || isPortableNativeEntry(entry, 'antigravity', root, 'project')))
199
200
  return doc;
200
201
  const next = { ...doc };
201
202
  delete next[ANTIGRAVITY_HOOK_NAME];
@@ -1,5 +1,4 @@
1
- import { type NativeTarget } from '../hook-engine/registration.js';
2
- import { type CoreNextTarget } from '../../utils/host-registration.js';
1
+ import { type CoreNextTarget, type FallbackTarget } from '../../utils/host-registration.js';
3
2
  /** Name of the HQ MCP server entry in ~/.claude.json. */
4
3
  export declare const HQ_MCP_SERVER_NAME = "hq";
5
4
  /** `_hqPack` provenance stamped on the HQ MCP server entry. */
@@ -23,7 +22,7 @@ export declare function masterHookPath(hqRoot: string): string;
23
22
  /** hqd hook shim (US-010): registered instead of master-hook.sh once hqd is installed. */
24
23
  export declare function shimPath(hqRoot: string): string;
25
24
  /** Which HQ hook script the user-scope install registers. */
26
- export type HookTarget = 'master' | 'shim' | NativeTarget | CoreNextTarget;
25
+ export type HookTarget = FallbackTarget | CoreNextTarget;
27
26
  export declare function charterPath(hqRoot: string): string;
28
27
  /**
29
28
  * Claude Code @imports do not load when the path contains backslashes.
@@ -33,7 +32,7 @@ export declare function charterPath(hqRoot: string): string;
33
32
  export declare function claudeMdImportPath(hqRoot: string): string;
34
33
  export declare function hqSkillsSource(hqRoot: string): string;
35
34
  /** The exact hook command registered for one event (absolute path, quoted). */
36
- export declare function hookCommand(hqRoot: string, event: string, target?: HookTarget): string;
35
+ export declare function hookCommand(hqRoot: string, event: string, target?: HookTarget, platform?: NodeJS.Platform): string;
37
36
  /** Append the block (replacing any prior HQ block). `null` input = file absent. */
38
37
  export declare function addClaudeMdBlock(current: string | null, hqRoot: string): string;
39
38
  /** Strip the HQ block, plus the separator `addClaudeMdBlock` put before it. */
@@ -43,6 +42,7 @@ type HookGroup = {
43
42
  matcher?: string;
44
43
  hooks?: Array<Record<string, unknown>>;
45
44
  };
45
+ export declare function isHqHookEntry(entry: unknown, hqRoot: string): boolean;
46
46
  /**
47
47
  * `groups` with `want` in the place of the first entry `ours` matches, every
48
48
  * other matching entry removed (a group it leaves empty is dropped), and
@@ -53,10 +53,10 @@ export declare function replaceInPlace(groups: HookGroup[], want: Record<string,
53
53
  * Add one HQ hook group per event; idempotent. An HQ entry for the other
54
54
  * target (master-hook.sh vs the hqd shim) is swapped out. Foreign hooks are untouched.
55
55
  */
56
- export declare function addHqHooks(doc: Doc, hqRoot: string, target?: HookTarget): Doc;
56
+ export declare function addHqHooks(doc: Doc, hqRoot: string, target?: HookTarget, platform?: NodeJS.Platform): Doc;
57
57
  /** Remove only HQ master-hook entries; drop containers this left empty. */
58
58
  export declare function removeHqHooks(doc: Doc, hqRoot: string): Doc;
59
- export declare function hasAllHqHooks(doc: Doc, hqRoot: string, target?: HookTarget): boolean;
59
+ export declare function hasAllHqHooks(doc: Doc, hqRoot: string, target?: HookTarget, platform?: NodeJS.Platform): boolean;
60
60
  /**
61
61
  * Minimal line diff (LCS) with -/+ prefixes. Only changed lines and `context`
62
62
  * unchanged lines around them are shown, so a dry run never echoes a whole
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import * as path from 'path';
8
8
  import { engineArgs, isCoreNextEntry, isNativeEntry } from '../hook-engine/registration.js';
9
- import { coreNextArgs, placeEvent, targetForEvent } from '../../utils/host-registration.js';
9
+ import { coreNextArgs, isPortableNativeEntry, placeEvent, portableNativeCommandForPlatform, targetForEvent } from '../../utils/host-registration.js';
10
10
  /** Events master-hook.sh is registered for at user scope. */
11
11
  const GLOBAL_HOOK_EVENTS = [
12
12
  'SessionStart',
@@ -58,7 +58,9 @@ export function hqSkillsSource(hqRoot) {
58
58
  return path.join(hqRoot, '.claude', 'skills');
59
59
  }
60
60
  /** The exact hook command registered for one event (absolute path, quoted). */
61
- export function hookCommand(hqRoot, event, target = 'master') {
61
+ export function hookCommand(hqRoot, event, target = 'master', platform = process.platform) {
62
+ if (target === 'portable')
63
+ return portableNativeCommandForPlatform('claude', event, hqRoot, 'global', platform);
62
64
  const skipInsideRoot = `if [ -d "\${CLAUDE_PROJECT_DIR:-}" ] && [ "\${CLAUDE_PROJECT_DIR}" -ef ${shellQuote(hqRoot)} ]; then exit 0; fi; cwd=$(pwd -P 2>/dev/null || pwd); while [ -n "$cwd" ]; do if [ -f "$cwd/core/core.yaml" ] && [ -f "$cwd/companies/manifest.yaml" ]; then exit 0; fi; [ "$cwd" = / ] && break; cwd=\${cwd%/*}; [ -n "$cwd" ] || cwd=/; done; `;
63
65
  // Many HQ hooks take their root from CLAUDE_PROJECT_DIR (some before HQ_ROOT). In a
64
66
  // foreign project that is the user's folder, so they load no policies and write
@@ -105,10 +107,12 @@ export function removeClaudeMdBlock(current) {
105
107
  start -= 1;
106
108
  return current.slice(0, start) + current.slice(end);
107
109
  }
108
- function isHqHookEntry(entry, hqRoot) {
110
+ export function isHqHookEntry(entry, hqRoot) {
109
111
  if (!entry || typeof entry !== 'object')
110
112
  return false;
111
- if (isNativeEntry(entry, hqRoot) || isCoreNextEntry(entry, hqRoot))
113
+ if (isNativeEntry(entry, hqRoot)
114
+ || isCoreNextEntry(entry, hqRoot)
115
+ || isPortableNativeEntry(entry, 'claude', hqRoot, 'global'))
112
116
  return true;
113
117
  const cmd = entry.command;
114
118
  if (typeof cmd !== 'string')
@@ -155,7 +159,7 @@ export function replaceInPlace(groups, want, ours) {
155
159
  * Add one HQ hook group per event; idempotent. An HQ entry for the other
156
160
  * target (master-hook.sh vs the hqd shim) is swapped out. Foreign hooks are untouched.
157
161
  */
158
- export function addHqHooks(doc, hqRoot, target = 'master') {
162
+ export function addHqHooks(doc, hqRoot, target = 'master', platform = process.platform) {
159
163
  const hooks = { ...(doc.hooks ?? {}) };
160
164
  let changed = false;
161
165
  for (const event of GLOBAL_HOOK_EVENTS) {
@@ -166,7 +170,7 @@ export function addHqHooks(doc, hqRoot, target = 'master') {
166
170
  const t = targetForEvent(target, event);
167
171
  const shellTarget = isCoreNext(t) ? (t.fallback ?? 'master') : t;
168
172
  const shellWant = typeof shellTarget !== 'object'
169
- ? { type: 'command', command: hookCommand(hqRoot, event, shellTarget), timeout: 300 }
173
+ ? { type: 'command', command: hookCommand(hqRoot, event, shellTarget, platform), timeout: 300 }
170
174
  : { type: 'command', command: shellTarget.engine, args: engineArgs(hqRoot, event, 'claude', true), timeout: 300 };
171
175
  const want = isCoreNext(t)
172
176
  ? { type: 'command', command: t.coreNext, args: coreNextArgs('claude', event, hqRoot, 'global'), timeout: 300 }
@@ -180,7 +184,7 @@ export function addHqHooks(doc, hqRoot, target = 'master') {
180
184
  const next = placeEvent(groups, {
181
185
  isShell: (h) => isHqHookEntry(h, hqRoot) && !isCoreNextEntry(h, hqRoot),
182
186
  isNative: (h) => isCoreNextEntry(h, hqRoot),
183
- toShell: () => shellWant,
187
+ toShell: (h) => (shellTarget === 'portable' && isCoreNextEntry(h, hqRoot) ? h : shellWant),
184
188
  toNative: isCoreNext(t) ? () => want : null,
185
189
  restore: shellWant,
186
190
  single: true,
@@ -239,8 +243,8 @@ export function removeHqHooks(doc, hqRoot) {
239
243
  next.hooks = hooks;
240
244
  return next;
241
245
  }
242
- export function hasAllHqHooks(doc, hqRoot, target = 'master') {
243
- return addHqHooks(doc, hqRoot, target) === doc;
246
+ export function hasAllHqHooks(doc, hqRoot, target = 'master', platform = process.platform) {
247
+ return addHqHooks(doc, hqRoot, target, platform) === doc;
244
248
  }
245
249
  // ---------------------------------------------------------------------------
246
250
  // Dry-run diff.
@@ -73,15 +73,15 @@ export declare function codexUserHookClaimed(hqRoot: string, target: HookTarget,
73
73
  * The user-scope hooks.json entry: {@link codexHookEntry} behind
74
74
  * {@link codexUserHookClaimed}. `commandWindows` is unchanged.
75
75
  */
76
- export declare function codexUserHookEntry(hqRoot: string, event: string, eventTarget?: HookTarget): Record<string, unknown>;
76
+ export declare function codexUserHookEntry(hqRoot: string, event: string, eventTarget?: HookTarget, platform?: NodeJS.Platform): Record<string, unknown>;
77
77
  /** The HQ hooks.json entry for one event and target. */
78
- export declare function codexHookEntry(hqRoot: string, event: string, eventTarget?: HookTarget): Record<string, unknown>;
78
+ export declare function codexHookEntry(hqRoot: string, event: string, eventTarget?: HookTarget, platform?: NodeJS.Platform): Record<string, unknown>;
79
79
  /**
80
80
  * Add one HQ hook group per event; idempotent. An HQ entry for the other
81
81
  * target (adapter vs the hqd shim) is swapped out, and so is an HQ entry
82
82
  * written before the user-scope guard existed. Foreign hooks are untouched.
83
83
  */
84
- export declare function addCodexHooks(doc: Doc, hqRoot: string, target?: HookTarget): Doc;
84
+ export declare function addCodexHooks(doc: Doc, hqRoot: string, target?: HookTarget, platform?: NodeJS.Platform): Doc;
85
85
  /** Remove only HQ adapter entries; drop containers this left empty. */
86
86
  export declare function removeCodexHooks(doc: Doc, hqRoot: string): Doc;
87
87
  /**
@@ -90,7 +90,7 @@ export declare function removeCodexHooks(doc: Doc, hqRoot: string): Doc;
90
90
  * never trusts a hook the user added themselves.
91
91
  */
92
92
  export declare function codexHookTrustKeys(doc: Doc, hooksJsonPath: string, hqRoot: string): string[];
93
- export declare function hasAllCodexHooks(doc: Doc, hqRoot: string, target?: HookTarget): boolean;
93
+ export declare function hasAllCodexHooks(doc: Doc, hqRoot: string, target?: HookTarget, platform?: NodeJS.Platform): boolean;
94
94
  export type LegacySkillsMirror = {
95
95
  kind: 'absent';
96
96
  }
@@ -13,7 +13,7 @@ import * as fs from 'fs';
13
13
  import { engineArgs, isCoreNextEntry, isNativeEntry, quotePosix, quoteWindows } from '../hook-engine/registration.js';
14
14
  import * as path from 'path';
15
15
  import { shimPath } from './claude-targets.js';
16
- import { coreNextCommand, coreNextCommandWindows, placeEvent, targetForEvent } from '../../utils/host-registration.js';
16
+ import { coreNextCommand, coreNextCommandWindows, isPortableNativeEntry, placeEvent, portableNativeCommand, portableNativeCommandWindows, targetForEvent } from '../../utils/host-registration.js';
17
17
  /** Events the Codex hook adapter is registered for at user scope. */
18
18
  const CODEX_HOOK_EVENTS = [
19
19
  'SessionStart',
@@ -93,7 +93,9 @@ export function removeAgentsMdBlock(current) {
93
93
  export function isAdapterEntry(entry, hqRoot) {
94
94
  if (!entry || typeof entry !== 'object')
95
95
  return false;
96
- if (isNativeEntry(entry, hqRoot) || isCoreNextEntry(entry, hqRoot))
96
+ if (isNativeEntry(entry, hqRoot)
97
+ || isCoreNextEntry(entry, hqRoot)
98
+ || isPortableNativeEntry(entry, 'codex', hqRoot, 'project'))
97
99
  return true;
98
100
  const cmd = entry.command;
99
101
  return (typeof cmd === 'string' &&
@@ -171,22 +173,29 @@ export function codexUserHookClaimed(hqRoot, target, command) {
171
173
  * The user-scope hooks.json entry: {@link codexHookEntry} behind
172
174
  * {@link codexUserHookClaimed}. `commandWindows` is unchanged.
173
175
  */
174
- export function codexUserHookEntry(hqRoot, event, eventTarget = 'master') {
176
+ export function codexUserHookEntry(hqRoot, event, eventTarget = 'master', platform = process.platform) {
175
177
  const target = targetForEvent(eventTarget, event);
176
- const entry = codexHookEntry(hqRoot, event, target);
177
- // The new core (hq-core-rust US-017) is called directly, with no shell
178
- // wrapper and no scope option: like the claimed entry above, it answers
179
- // inside an HQ tree too, because Codex may not load or trust the root's own
180
- // project hooks. Where both run, each answers, as both do today.
181
- if (typeof target === 'object' && 'coreNext' in target)
178
+ const entry = codexHookEntry(hqRoot, event, target, platform);
179
+ // The new core (hq-core-rust US-017) and its fail-closed portable native
180
+ // wrapper are called directly without the legacy adapter claim wrapper:
181
+ // they answer inside an HQ tree too, because Codex may not load or trust
182
+ // the root's own project hooks.
183
+ if (target === 'portable' || (typeof target === 'object' && 'coreNext' in target))
182
184
  return entry;
183
185
  return { ...entry, command: codexUserHookClaimed(hqRoot, target, entry.command) };
184
186
  }
185
187
  /** The HQ hooks.json entry for one event and target. */
186
- export function codexHookEntry(hqRoot, event, eventTarget = 'master') {
188
+ export function codexHookEntry(hqRoot, event, eventTarget = 'master', platform = process.platform) {
187
189
  const timeout = event === 'SessionEnd' ? 3 : 300;
188
190
  // With the new core (US-017), an event it cannot take yet keeps the flag-off target.
189
191
  const target = targetForEvent(eventTarget, event);
192
+ if (target === 'portable') {
193
+ if (platform === 'win32') {
194
+ const winCmd = portableNativeCommandWindows('codex', event, hqRoot, 'project');
195
+ return { type: 'command', command: winCmd, commandWindows: winCmd, timeout };
196
+ }
197
+ return { type: 'command', command: portableNativeCommand('codex', event, hqRoot, 'project'), timeout };
198
+ }
190
199
  if (typeof target !== 'object')
191
200
  return { type: 'command', command: codexHookCommand(hqRoot, target), timeout };
192
201
  if ('coreNext' in target) {
@@ -206,11 +215,11 @@ export function codexHookEntry(hqRoot, event, eventTarget = 'master') {
206
215
  * target (adapter vs the hqd shim) is swapped out, and so is an HQ entry
207
216
  * written before the user-scope guard existed. Foreign hooks are untouched.
208
217
  */
209
- export function addCodexHooks(doc, hqRoot, target = 'master') {
218
+ export function addCodexHooks(doc, hqRoot, target = 'master', platform = process.platform) {
210
219
  const hooks = { ...(doc.hooks ?? {}) };
211
220
  let changed = false;
212
221
  for (const event of CODEX_HOOK_EVENTS) {
213
- const want = codexUserHookEntry(hqRoot, event, target);
222
+ const want = codexUserHookEntry(hqRoot, event, target, platform);
214
223
  let groups = Array.isArray(hooks[event]) ? [...hooks[event]] : [];
215
224
  // To or from the new core, the HQ entry keeps its place: Codex keys a
216
225
  // user's trust of each hook by its position, so moving HQ's entry behind
@@ -221,11 +230,12 @@ export function addCodexHooks(doc, hqRoot, target = 'master') {
221
230
  if (inPlace) {
222
231
  const t = targetForEvent(target, event);
223
232
  const switched = typeof t === 'object' && 'coreNext' in t;
224
- const shellWant = switched ? codexUserHookEntry(hqRoot, event, t.fallback ?? 'master') : want;
233
+ const fallbackTarget = switched ? (t.fallback ?? 'master') : t;
234
+ const shellWant = switched ? codexUserHookEntry(hqRoot, event, fallbackTarget, platform) : want;
225
235
  const next = placeEvent(groups, {
226
236
  isShell: (h) => isAdapterEntry(h, hqRoot) && !isCoreNextEntry(h, hqRoot),
227
237
  isNative: (h) => isCoreNextEntry(h, hqRoot),
228
- toShell: () => shellWant,
238
+ toShell: (h) => (fallbackTarget === 'portable' && isCoreNextEntry(h, hqRoot) ? h : shellWant),
229
239
  toNative: switched ? () => want : null,
230
240
  restore: shellWant,
231
241
  single: true,
@@ -309,8 +319,8 @@ export function codexHookTrustKeys(doc, hooksJsonPath, hqRoot) {
309
319
  }
310
320
  return keys;
311
321
  }
312
- export function hasAllCodexHooks(doc, hqRoot, target = 'master') {
313
- return addCodexHooks(doc, hqRoot, target) === doc;
322
+ export function hasAllCodexHooks(doc, hqRoot, target = 'master', platform = process.platform) {
323
+ return addCodexHooks(doc, hqRoot, target, platform) === doc;
314
324
  }
315
325
  function within(child, parent) {
316
326
  const rel = path.relative(parent, child);
@@ -6,11 +6,32 @@ import type { HookTarget } from './claude-targets.js';
6
6
  * install registered counts only when it runs as this user.
7
7
  */
8
8
  export declare function isDaemonInstalled(home: string, platform?: NodeJS.Platform, queryTask?: (task: string) => boolean, taskName?: () => string, legacyTaskIsMine?: () => boolean): boolean;
9
+ export interface ResolvedInstallHookTarget {
10
+ target: HookTarget;
11
+ /**
12
+ * Set on a native-only root when `core.next` is on and the host canary or
13
+ * coverage gate refused the candidate engine (M-2 / M-1). Callers must never
14
+ * link `pinnedEngine` as `stable` in the portable fallback.
15
+ */
16
+ refusedCandidate?: {
17
+ pinnedEngine?: string;
18
+ engineVersion?: string;
19
+ refusedHosts?: CoreNextHost[];
20
+ checkedHosts?: CoreNextHost[];
21
+ refusalSummary?: string;
22
+ };
23
+ }
24
+ /**
25
+ * US-010 / US-017 / US-027: resolve the hook target and, on native-only roots,
26
+ * defer linking `stable` until EVERY host with HQ hook entries on this machine
27
+ * (plus `host`) accepts the candidate engine.
28
+ */
29
+ export declare function resolveInstallHookTarget(hqRoot: string, home: string, daemonInstalled?: boolean, env?: NodeJS.ProcessEnv, host?: CoreNextHost, platform?: NodeJS.Platform): ResolvedInstallHookTarget;
9
30
  /**
10
31
  * US-010: with hqd installed (and the checkout shipping the shim), hooks go to
11
32
  * the hqd shim; otherwise to master-hook.sh / the Codex adapter.
12
33
  */
13
- export declare function hookTargetFor(hqRoot: string, home: string, daemonInstalled?: boolean, env?: NodeJS.ProcessEnv, host?: CoreNextHost): HookTarget;
34
+ export declare function hookTargetFor(hqRoot: string, home: string, daemonInstalled?: boolean, env?: NodeJS.ProcessEnv, host?: CoreNextHost, platform?: NodeJS.Platform): HookTarget;
14
35
  /**
15
36
  * The target with `core.next` off: the pinned launcher, or master-hook.sh
16
37
  * (the Codex adapter on Codex).
@@ -22,5 +43,5 @@ export declare function hookTargetFor(hqRoot: string, home: string, daemonInstal
22
43
  * the daemon is up. The root, daemon and host arguments stay for callers; they
23
44
  * no longer change the answer.
24
45
  */
25
- export declare function legacyHookTarget(_hqRoot: string, home: string, _daemonInstalled?: boolean, env?: NodeJS.ProcessEnv, _host?: CoreNextHost): FallbackTarget;
46
+ export declare function legacyHookTarget(hqRoot: string, _home: string, _daemonInstalled?: boolean, _env?: NodeJS.ProcessEnv, _host?: CoreNextHost): FallbackTarget;
26
47
  //# sourceMappingURL=hook-target.d.ts.map