@indigoai-us/hq-cli 5.345.204 → 5.345.206
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.
- package/CHANGELOG.md +40 -0
- package/dist/command-catalog.generated.d.ts +51 -1
- package/dist/command-catalog.generated.js +63 -1
- package/dist/commands/bot.d.ts +67 -0
- package/dist/commands/bot.js +301 -39
- package/dist/commands/cloud-off-gate.d.ts +9 -2
- package/dist/commands/cloud-off-gate.js +49 -13
- package/dist/commands/feedback.d.ts +26 -0
- package/dist/commands/feedback.js +91 -5
- package/dist/commands/install-global.d.ts +3 -1
- package/dist/commands/install-global.js +15 -4
- package/dist/commands/lanes.d.ts +2 -0
- package/dist/commands/lanes.js +9 -3
- package/dist/commands/mcp-serve.d.ts +5 -2
- package/dist/commands/mcp-serve.js +45 -3
- package/dist/commands/mcp-status.d.ts +3 -1
- package/dist/commands/mcp-status.js +24 -14
- package/dist/commands/secrets.js +11 -1
- package/dist/commands/sources.js +10 -15
- package/dist/index.js +7 -2
- package/dist/lib/bot/executive-settings.d.ts +13 -0
- package/dist/lib/bot/executive-settings.js +20 -17
- package/dist/lib/bot/executive-sync.d.ts +12 -0
- package/dist/lib/bot/executive-sync.js +36 -0
- package/dist/lib/bot/executive.d.ts +51 -0
- package/dist/lib/bot/executive.js +144 -0
- package/dist/lib/bot/fleet-cron.d.ts +7 -0
- package/dist/lib/bot/fleet-cron.js +2 -1
- package/dist/lib/bot/fleet-local.js +9 -9
- package/dist/lib/bot/jobs/executive-handover.d.ts +6 -0
- package/dist/lib/bot/jobs/executive-handover.js +33 -0
- package/dist/lib/bot/jobs/hot-signal-sync.d.ts +22 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +47 -0
- package/dist/lib/bot/listed-running.d.ts +16 -0
- package/dist/lib/bot/listed-running.js +16 -0
- package/dist/lib/bot/pending-switch.d.ts +51 -0
- package/dist/lib/bot/pending-switch.js +103 -0
- package/dist/lib/bot/run.js +27 -3
- package/dist/lib/bot/runtime-failure.js +19 -3
- package/dist/lib/bot/status.d.ts +20 -0
- package/dist/lib/company-skill-wrapper.js +29 -0
- package/dist/lib/daemon/hq-child-bootstrap.mjs +197 -2
- package/dist/lib/daemon/resolver.d.ts +14 -0
- package/dist/lib/daemon/resolver.js +96 -0
- package/dist/lib/daemon/run.d.ts +11 -0
- package/dist/lib/daemon/run.js +58 -6
- package/dist/lib/daemon/server.d.ts +2 -9
- package/dist/lib/daemon/server.js +3 -89
- package/dist/lib/doctor/checks/claude-wiring.js +41 -0
- package/dist/lib/doctor/checks/sync-health.d.ts +8 -0
- package/dist/lib/doctor/checks/sync-health.js +34 -2
- package/dist/lib/doctor/duplicate-hook-registrations.d.ts +34 -0
- package/dist/lib/doctor/duplicate-hook-registrations.js +130 -0
- package/dist/lib/doctor/fix/apply.js +79 -3
- package/dist/lib/doctor/fix/remediation.d.ts +1 -1
- package/dist/lib/doctor/fix/remediation.js +15 -0
- package/dist/lib/doctor/types.d.ts +1 -1
- package/dist/lib/doctor/types.js +1 -0
- package/dist/lib/lanes/links.d.ts +2 -0
- package/dist/lib/lanes/links.js +12 -1
- package/dist/lib/mcp/local-native-tools.d.ts +40 -0
- package/dist/lib/mcp/local-native-tools.js +111 -0
- package/dist/lib/mcp/tools.d.ts +2 -0
- package/dist/lib/mcp/tools.js +2 -0
- package/dist/lib/signals/steps/extract.d.ts +1 -1
- package/dist/lib/signals/steps/extract.js +4 -16
- package/dist/sentry-init.js +6 -0
- package/dist/utils/cli-network-lifecycle.d.ts +11 -1
- package/dist/utils/cli-network-lifecycle.js +36 -2
- package/dist/utils/daemon-helper-lifetime.d.ts +39 -0
- package/dist/utils/daemon-helper-lifetime.js +209 -0
- package/dist/utils/daemon-helper-markers.d.ts +24 -0
- package/dist/utils/daemon-helper-markers.js +53 -0
- package/dist/utils/mcp-launcher.d.ts +28 -0
- package/dist/utils/mcp-launcher.js +117 -0
- package/dist/utils/package-use-lease.d.ts +10 -0
- package/dist/utils/package-use-lease.js +31 -0
- package/dist/utils/resident-package-preload.js +4 -1
- package/docs/anywhere-runtime.md +7 -0
- package/package.json +7 -7
|
@@ -111,7 +111,8 @@ import yaml from "js-yaml";
|
|
|
111
111
|
import { findPriorSignals, normalizeSignalTitle, priorSignalPath } from "../prior-signals.js";
|
|
112
112
|
import { splitFrontmatter } from "../../hot-channels/signals-pass.js";
|
|
113
113
|
import { audienceKey } from "../../hot-channels/audience.js";
|
|
114
|
-
import {
|
|
114
|
+
import { isBotRuntimeId } from "../../bot/config.js";
|
|
115
|
+
import { resolveExecutiveBot } from "../../bot/executive.js";
|
|
115
116
|
import { botsRoot } from "../../bot/paths.js";
|
|
116
117
|
import { parseClaudeAuthStatus } from "../../bot/runtime/claude.js";
|
|
117
118
|
import { parseCodexLoginStatus } from "../../bot/runtime/codex.js";
|
|
@@ -182,22 +183,9 @@ export function claudeAllowedTools(channelDir, wrapperPath) {
|
|
|
182
183
|
// A rule path that starts with `//` is absolute.
|
|
183
184
|
return [`Read(/${channelDir}/**)`, `Bash(bash ${wrapperPath} *)`];
|
|
184
185
|
}
|
|
185
|
-
/**
|
|
186
|
+
/** The Executive bot's runtime (lib/bot/executive.ts), or null when there is no personal bot. */
|
|
186
187
|
export function defaultResolveBotRuntime(root = botsRoot()) {
|
|
187
|
-
|
|
188
|
-
return null;
|
|
189
|
-
const names = fs.readdirSync(root).sort();
|
|
190
|
-
const configs = names
|
|
191
|
-
.map((n) => {
|
|
192
|
-
try {
|
|
193
|
-
return readBotConfig(path.join(root, n));
|
|
194
|
-
}
|
|
195
|
-
catch {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
.filter((c) => c !== null);
|
|
200
|
-
const main = configs.find((c) => effectiveBotKind(c) === "personal") ?? null;
|
|
188
|
+
const main = resolveExecutiveBot({ root })?.config ?? null;
|
|
201
189
|
return main && isBotRuntimeId(main.runtime) ? main.runtime : null;
|
|
202
190
|
}
|
|
203
191
|
function yamlField(text, key) {
|
package/dist/sentry-init.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { BUNDLED_DSN } from "./sentry-dsn.generated.js";
|
|
3
3
|
import { CLI_VERSION } from "./cli-version.js";
|
|
4
4
|
import { beforeBreadcrumb } from "./utils/breadcrumb-buffer.js";
|
|
5
|
+
import { residentProcessSkipsSentry } from "./utils/cli-network-lifecycle.js";
|
|
5
6
|
import { getCachedSentryUser } from "./utils/sentry-identity.js";
|
|
6
7
|
const requireRuntime = createRequire(import.meta.url);
|
|
7
8
|
let sentryModule;
|
|
@@ -114,6 +115,11 @@ async function resolveManualOnlyReportSkip() {
|
|
|
114
115
|
return flagModule.clientHealthSkipManualOnlyReportsEnabled();
|
|
115
116
|
}
|
|
116
117
|
export function initSentryRuntime(beforeSend, overrides = {}, pendingBreadcrumbs = []) {
|
|
118
|
+
// Every startup path (main, fast-route errors, auth refresh, explicit
|
|
119
|
+
// reports) comes through here. A resident process must return before the
|
|
120
|
+
// SDK opens its transport.
|
|
121
|
+
if (residentProcessSkipsSentry(process.argv, process.env))
|
|
122
|
+
return undefined;
|
|
117
123
|
const dsn = BUNDLED_DSN || process.env.SENTRY_DSN;
|
|
118
124
|
if (!dsn)
|
|
119
125
|
return undefined;
|
|
@@ -5,8 +5,18 @@ export declare function isHqDaemonRunCommand(argv: readonly string[]): boolean;
|
|
|
5
5
|
* network changes, while the SDK is designed around one short CLI invocation.
|
|
6
6
|
*/
|
|
7
7
|
export declare function isLongLivedDaemonCommand(argv: readonly string[]): boolean;
|
|
8
|
+
export declare function isLongLivedHelperCommand(argv: readonly string[]): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* True when this process must not open the Sentry transport. `HQ_DAEMON_RESIDENT`
|
|
11
|
+
* marks the long-running process the daemon started, including one whose
|
|
12
|
+
* arguments are not in the list above. That process records the skip and
|
|
13
|
+
* removes the variable before it starts another command, so a short command
|
|
14
|
+
* inside the session still reports. Scheduled units do not get the variable.
|
|
15
|
+
* The recorded skip applies only to this process's own environment.
|
|
16
|
+
*/
|
|
17
|
+
export declare function residentProcessSkipsSentry(argv: readonly string[], env?: NodeJS.ProcessEnv): boolean;
|
|
8
18
|
/** Suppress only Sentry startup; every other global lifecycle operation still runs. */
|
|
9
|
-
export declare function runSentryInitialization<T>(argv: readonly string[], operation: () => T): T | undefined;
|
|
19
|
+
export declare function runSentryInitialization<T>(argv: readonly string[], operation: () => T, env?: NodeJS.ProcessEnv): T | undefined;
|
|
10
20
|
export declare function isMonitorFastPathCommand(argv: readonly string[]): boolean;
|
|
11
21
|
/** The commit-msg hook runs `hq fingerprint self --offline`; it must stay fast and offline. */
|
|
12
22
|
export declare function isOfflineFingerprintCommand(argv: readonly string[]): boolean;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { daemonDirectResidentSkipsSentry } from "./daemon-helper-markers.js";
|
|
1
2
|
const MONITOR_HIDDEN_NETWORK_BYPASS = new Set(["drain", "wait", "enabled", "_run"]);
|
|
2
3
|
function commandArgv(argv) {
|
|
3
4
|
return argv.slice(2).filter((arg) => arg !== "--verbose");
|
|
@@ -16,9 +17,42 @@ export function isLongLivedDaemonCommand(argv) {
|
|
|
16
17
|
return (isHqDaemonRunCommand(argv) ||
|
|
17
18
|
(command[0] === "mesh" && command[1] === "daemon" && command[2] === "run"));
|
|
18
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Long-lived daemon helpers. A short supervised job (reconcile, index, update)
|
|
22
|
+
* is not in this list: it still reports, then exits. Match whole argv tokens
|
|
23
|
+
* so `remote-control` does not swallow `remote-control-pool`.
|
|
24
|
+
*/
|
|
25
|
+
const LONG_LIVED_HELPER_COMMANDS = [
|
|
26
|
+
["agent", "kit", "run", "heartbeat"],
|
|
27
|
+
["agent", "kit", "run", "inbox"],
|
|
28
|
+
["outposts", "heartbeat"],
|
|
29
|
+
["outposts", "session-host"],
|
|
30
|
+
["bot", "run"],
|
|
31
|
+
["daemon", "service", "codex-remote-control"],
|
|
32
|
+
["daemon", "service", "remote-control-pool-forwarder-host"],
|
|
33
|
+
["daemon", "service", "remote-control-pool"],
|
|
34
|
+
["daemon", "service", "remote-control"],
|
|
35
|
+
];
|
|
36
|
+
export function isLongLivedHelperCommand(argv) {
|
|
37
|
+
const command = commandArgv(argv);
|
|
38
|
+
return LONG_LIVED_HELPER_COMMANDS.some((prefix) => prefix.length <= command.length && prefix.every((part, index) => command[index] === part));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* True when this process must not open the Sentry transport. `HQ_DAEMON_RESIDENT`
|
|
42
|
+
* marks the long-running process the daemon started, including one whose
|
|
43
|
+
* arguments are not in the list above. That process records the skip and
|
|
44
|
+
* removes the variable before it starts another command, so a short command
|
|
45
|
+
* inside the session still reports. Scheduled units do not get the variable.
|
|
46
|
+
* The recorded skip applies only to this process's own environment.
|
|
47
|
+
*/
|
|
48
|
+
export function residentProcessSkipsSentry(argv, env = process.env) {
|
|
49
|
+
const markedResident = env.HQ_DAEMON_RESIDENT === "1"
|
|
50
|
+
|| (env === process.env && daemonDirectResidentSkipsSentry());
|
|
51
|
+
return markedResident || isLongLivedDaemonCommand(argv) || isLongLivedHelperCommand(argv);
|
|
52
|
+
}
|
|
19
53
|
/** Suppress only Sentry startup; every other global lifecycle operation still runs. */
|
|
20
|
-
export function runSentryInitialization(argv, operation) {
|
|
21
|
-
if (
|
|
54
|
+
export function runSentryInitialization(argv, operation, env = process.env) {
|
|
55
|
+
if (residentProcessSkipsSentry(argv, env))
|
|
22
56
|
return undefined;
|
|
23
57
|
return operation();
|
|
24
58
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stop a daemon helper when the process that owns it is gone.
|
|
3
|
+
*
|
|
4
|
+
* The child bootstrap has its own copy of the same rules (it is staged as one
|
|
5
|
+
* file and cannot import this module). Keep the handoff age and the pipe
|
|
6
|
+
* check in step with src/lib/daemon/hq-child-bootstrap.mts.
|
|
7
|
+
*
|
|
8
|
+
* stdin is not an owner channel unless it is a FIFO or a Node pipe socket.
|
|
9
|
+
* The daemon spawns helpers with stdin "ignore", which is /dev/null, and
|
|
10
|
+
* reading that returns EOF at once. Node implements stdio "pipe" as a socket.
|
|
11
|
+
*/
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
export { daemonDirectReplacementEnv, daemonDirectResidentSkipsSentry } from "./daemon-helper-markers.js";
|
|
14
|
+
/** Same window as HANDOFF_MAX_AGE_MS in succession.ts. */
|
|
15
|
+
export declare const HANDOFF_MAX_AGE_MS: number;
|
|
16
|
+
export declare const OWNER_POLL_MS = 500;
|
|
17
|
+
export declare const OWNER_HANDOFF_RECHECK_MS = 1000;
|
|
18
|
+
export declare const CHILD_STOP_GRACE_MS = 2000;
|
|
19
|
+
export declare function descriptorIsOwnerPipe(fd: number, fstat?: (fd: number) => fs.Stats): boolean;
|
|
20
|
+
export declare function readProcStart(pid: number): number | null;
|
|
21
|
+
export declare function parentStillOwns(pid: number, start: number | null, kill?: (pid: number, signal: 0) => void, readStart?: (pid: number) => number | null): boolean;
|
|
22
|
+
export declare function handoffKeepsPid(record: unknown, pid: number, now: number, selfStart: number | null, maxAgeMs?: number): boolean;
|
|
23
|
+
export declare function handleOwnerLoss(options: {
|
|
24
|
+
handoffPath: string | undefined;
|
|
25
|
+
readHandoff: () => unknown;
|
|
26
|
+
pid: number;
|
|
27
|
+
selfStart: number | null;
|
|
28
|
+
now: () => number;
|
|
29
|
+
wait: (ms: number) => Promise<void>;
|
|
30
|
+
recheckMs?: number;
|
|
31
|
+
}): Promise<"detach" | "stop">;
|
|
32
|
+
/**
|
|
33
|
+
* Arm the watch only for the process the daemon started directly. A later hq
|
|
34
|
+
* command inherits `HQ_DAEMON_UNIT` and must not exit with its launcher or
|
|
35
|
+
* lose Sentry. The one-shot markers are removed from the environment first,
|
|
36
|
+
* before any command can spawn a child.
|
|
37
|
+
*/
|
|
38
|
+
export declare function installDaemonHelperLifetime(env?: NodeJS.ProcessEnv): void;
|
|
39
|
+
//# sourceMappingURL=daemon-helper-lifetime.d.ts.map
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stop a daemon helper when the process that owns it is gone.
|
|
3
|
+
*
|
|
4
|
+
* The child bootstrap has its own copy of the same rules (it is staged as one
|
|
5
|
+
* file and cannot import this module). Keep the handoff age and the pipe
|
|
6
|
+
* check in step with src/lib/daemon/hq-child-bootstrap.mts.
|
|
7
|
+
*
|
|
8
|
+
* stdin is not an owner channel unless it is a FIFO or a Node pipe socket.
|
|
9
|
+
* The daemon spawns helpers with stdin "ignore", which is /dev/null, and
|
|
10
|
+
* reading that returns EOF at once. Node implements stdio "pipe" as a socket.
|
|
11
|
+
*/
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
import * as net from "node:net";
|
|
14
|
+
import { consumeOneShotOwnerMarkers } from "./daemon-helper-markers.js";
|
|
15
|
+
export { daemonDirectReplacementEnv, daemonDirectResidentSkipsSentry } from "./daemon-helper-markers.js";
|
|
16
|
+
/** Same window as HANDOFF_MAX_AGE_MS in succession.ts. */
|
|
17
|
+
export const HANDOFF_MAX_AGE_MS = 5 * 60_000;
|
|
18
|
+
export const OWNER_POLL_MS = 500;
|
|
19
|
+
export const OWNER_HANDOFF_RECHECK_MS = 1_000;
|
|
20
|
+
export const CHILD_STOP_GRACE_MS = 2_000;
|
|
21
|
+
export function descriptorIsOwnerPipe(fd, fstat = fs.fstatSync) {
|
|
22
|
+
try {
|
|
23
|
+
const stat = fstat(fd);
|
|
24
|
+
// Node's "pipe" stdio is a socketpair on macOS and Linux, not a FIFO.
|
|
25
|
+
// /dev/null is a character device, so ignored stdin still does not match.
|
|
26
|
+
return stat.isFIFO() || stat.isSocket();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function readProcStart(pid) {
|
|
33
|
+
try {
|
|
34
|
+
const text = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
35
|
+
const close = text.lastIndexOf(")");
|
|
36
|
+
if (close < 0)
|
|
37
|
+
return null;
|
|
38
|
+
const rest = text.slice(close + 1).trim().split(/\s+/);
|
|
39
|
+
if (rest[0] === "Z" || rest[0] === "X")
|
|
40
|
+
return null;
|
|
41
|
+
const start = Number(rest[19]);
|
|
42
|
+
return Number.isInteger(start) ? start : null;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function parentStillOwns(pid, start, kill = (target, signal) => process.kill(target, signal), readStart = readProcStart) {
|
|
49
|
+
if (!Number.isInteger(pid) || pid <= 1)
|
|
50
|
+
return false;
|
|
51
|
+
try {
|
|
52
|
+
kill(pid, 0);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error.code !== "EPERM")
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (start === null)
|
|
59
|
+
return true;
|
|
60
|
+
const current = readStart(pid);
|
|
61
|
+
if (current === null)
|
|
62
|
+
return false;
|
|
63
|
+
return current === start;
|
|
64
|
+
}
|
|
65
|
+
export function handoffKeepsPid(record, pid, now, selfStart, maxAgeMs = HANDOFF_MAX_AGE_MS) {
|
|
66
|
+
if (!record || typeof record !== "object")
|
|
67
|
+
return false;
|
|
68
|
+
const releasedAt = record.releasedAt;
|
|
69
|
+
if (typeof releasedAt !== "number" || !Number.isFinite(releasedAt))
|
|
70
|
+
return false;
|
|
71
|
+
if (now - releasedAt > maxAgeMs)
|
|
72
|
+
return false;
|
|
73
|
+
const units = record.units;
|
|
74
|
+
if (!Array.isArray(units))
|
|
75
|
+
return false;
|
|
76
|
+
return units.some((unit) => {
|
|
77
|
+
if (!unit || typeof unit !== "object")
|
|
78
|
+
return false;
|
|
79
|
+
if (unit.pid !== pid)
|
|
80
|
+
return false;
|
|
81
|
+
const start = unit.start;
|
|
82
|
+
if (typeof start === "number" && selfStart !== null && start !== selfStart)
|
|
83
|
+
return false;
|
|
84
|
+
return true;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export async function handleOwnerLoss(options) {
|
|
88
|
+
const keeps = () => handoffKeepsPid(options.readHandoff(), options.pid, options.now(), options.selfStart);
|
|
89
|
+
if (options.handoffPath) {
|
|
90
|
+
const deadline = options.now() + (options.recheckMs ?? OWNER_HANDOFF_RECHECK_MS);
|
|
91
|
+
while (options.now() < deadline) {
|
|
92
|
+
if (keeps())
|
|
93
|
+
return "detach";
|
|
94
|
+
await options.wait(50);
|
|
95
|
+
if (keeps())
|
|
96
|
+
return "detach";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return keeps() ? "detach" : "stop";
|
|
100
|
+
}
|
|
101
|
+
let installed = false;
|
|
102
|
+
const ownerSockets = [];
|
|
103
|
+
let ownerPoll;
|
|
104
|
+
function watchPipeEof(fd, onEof) {
|
|
105
|
+
if (!descriptorIsOwnerPipe(fd))
|
|
106
|
+
return;
|
|
107
|
+
// fs.ReadStream has no unref. A referenced read would keep a finished
|
|
108
|
+
// command alive. /dev/null never reaches here: it is not a pipe.
|
|
109
|
+
let socket;
|
|
110
|
+
try {
|
|
111
|
+
socket = new net.Socket({ fd, readable: true, writable: false });
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
ownerSockets.push(socket);
|
|
117
|
+
socket.on("end", onEof);
|
|
118
|
+
socket.on("error", () => { });
|
|
119
|
+
socket.resume();
|
|
120
|
+
socket.unref();
|
|
121
|
+
}
|
|
122
|
+
function stopOwnerWatch() {
|
|
123
|
+
if (ownerPoll)
|
|
124
|
+
clearInterval(ownerPoll);
|
|
125
|
+
ownerPoll = undefined;
|
|
126
|
+
for (const socket of ownerSockets.splice(0)) {
|
|
127
|
+
socket.removeAllListeners("end");
|
|
128
|
+
socket.removeAllListeners("close");
|
|
129
|
+
socket.on("error", () => { });
|
|
130
|
+
socket.destroy();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function stopSelf() {
|
|
134
|
+
try {
|
|
135
|
+
process.kill(process.pid, "SIGTERM");
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Already on the way out.
|
|
139
|
+
}
|
|
140
|
+
const timer = setTimeout(() => {
|
|
141
|
+
try {
|
|
142
|
+
process.kill(process.pid, "SIGKILL");
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// The process is already gone.
|
|
146
|
+
}
|
|
147
|
+
}, CHILD_STOP_GRACE_MS);
|
|
148
|
+
timer.unref();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Arm the watch only for the process the daemon started directly. A later hq
|
|
152
|
+
* command inherits `HQ_DAEMON_UNIT` and must not exit with its launcher or
|
|
153
|
+
* lose Sentry. The one-shot markers are removed from the environment first,
|
|
154
|
+
* before any command can spawn a child.
|
|
155
|
+
*/
|
|
156
|
+
export function installDaemonHelperLifetime(env = process.env) {
|
|
157
|
+
const direct = env.HQ_DAEMON_BOOTSTRAP_CHILD === "1" || env.HQ_DAEMON_RESIDENT === "1";
|
|
158
|
+
if (!direct || installed)
|
|
159
|
+
return;
|
|
160
|
+
installed = true;
|
|
161
|
+
const { handoffPath, controlFd } = consumeOneShotOwnerMarkers(env);
|
|
162
|
+
const parentPid = process.ppid;
|
|
163
|
+
const parentStart = readProcStart(parentPid);
|
|
164
|
+
let state = "bound";
|
|
165
|
+
const loseOwner = () => {
|
|
166
|
+
if (state !== "bound")
|
|
167
|
+
return;
|
|
168
|
+
state = "stopping";
|
|
169
|
+
void handleOwnerLoss({
|
|
170
|
+
handoffPath,
|
|
171
|
+
readHandoff: () => {
|
|
172
|
+
if (!handoffPath)
|
|
173
|
+
return null;
|
|
174
|
+
try {
|
|
175
|
+
return JSON.parse(fs.readFileSync(handoffPath, "utf8"));
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
pid: process.pid,
|
|
182
|
+
selfStart: readProcStart(process.pid),
|
|
183
|
+
now: () => Date.now(),
|
|
184
|
+
wait: (ms) => new Promise((resolve) => { setTimeout(resolve, ms); }),
|
|
185
|
+
}).then((decision) => {
|
|
186
|
+
if (decision === "detach") {
|
|
187
|
+
state = "detached";
|
|
188
|
+
stopOwnerWatch();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
stopOwnerWatch();
|
|
192
|
+
stopSelf();
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
if (Number.isInteger(controlFd) && controlFd >= 0)
|
|
196
|
+
watchPipeEof(controlFd, loseOwner);
|
|
197
|
+
// A parent that passes a real stdin pipe, and does not use the control fd,
|
|
198
|
+
// is also an owner. /dev/null is not a pipe and is ignored.
|
|
199
|
+
if (!Number.isInteger(controlFd))
|
|
200
|
+
watchPipeEof(0, loseOwner);
|
|
201
|
+
if (parentPid > 1) {
|
|
202
|
+
ownerPoll = setInterval(() => {
|
|
203
|
+
if (!parentStillOwns(parentPid, parentStart))
|
|
204
|
+
loseOwner();
|
|
205
|
+
}, OWNER_POLL_MS);
|
|
206
|
+
ownerPoll.unref();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=daemon-helper-lifetime.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-shot markers for the process the daemon started.
|
|
3
|
+
*
|
|
4
|
+
* This module does not import node:net. Fast native commands read the Sentry
|
|
5
|
+
* skip through it, and a network import here would fail their closure guard.
|
|
6
|
+
* The owner watch lives in daemon-helper-lifetime.ts and is loaded only by
|
|
7
|
+
* the full CLI entry.
|
|
8
|
+
*
|
|
9
|
+
* `HQ_DAEMON_UNIT` is not a marker. Bot and agent sessions inherit it on
|
|
10
|
+
* purpose. A descendant that treated it as an owner signal would exit when
|
|
11
|
+
* its own launcher exited.
|
|
12
|
+
*/
|
|
13
|
+
/** True after this process consumed `HQ_DAEMON_RESIDENT=1` and cleared it. */
|
|
14
|
+
export declare function daemonDirectResidentSkipsSentry(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Environment for a torn-install re-exec of this same helper. Descendants must
|
|
17
|
+
* not see the one-shot markers; a replacement of this process must.
|
|
18
|
+
*/
|
|
19
|
+
export declare function daemonDirectReplacementEnv(base: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
20
|
+
export declare function consumeOneShotOwnerMarkers(env: NodeJS.ProcessEnv): {
|
|
21
|
+
handoffPath: string | undefined;
|
|
22
|
+
controlFd: number;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=daemon-helper-markers.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-shot markers for the process the daemon started.
|
|
3
|
+
*
|
|
4
|
+
* This module does not import node:net. Fast native commands read the Sentry
|
|
5
|
+
* skip through it, and a network import here would fail their closure guard.
|
|
6
|
+
* The owner watch lives in daemon-helper-lifetime.ts and is loaded only by
|
|
7
|
+
* the full CLI entry.
|
|
8
|
+
*
|
|
9
|
+
* `HQ_DAEMON_UNIT` is not a marker. Bot and agent sessions inherit it on
|
|
10
|
+
* purpose. A descendant that treated it as an owner signal would exit when
|
|
11
|
+
* its own launcher exited.
|
|
12
|
+
*/
|
|
13
|
+
const ONE_SHOT_OWNER_KEYS = [
|
|
14
|
+
"HQ_DAEMON_BOOTSTRAP_CHILD",
|
|
15
|
+
"HQ_DAEMON_RESIDENT",
|
|
16
|
+
"HQ_DAEMON_OWNER_CONTROL_FD",
|
|
17
|
+
"HQ_DAEMON_HANDOFF_PATH",
|
|
18
|
+
];
|
|
19
|
+
let directResidentSkip = false;
|
|
20
|
+
let directMarkers;
|
|
21
|
+
/** True after this process consumed `HQ_DAEMON_RESIDENT=1` and cleared it. */
|
|
22
|
+
export function daemonDirectResidentSkipsSentry() {
|
|
23
|
+
return directResidentSkip;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Environment for a torn-install re-exec of this same helper. Descendants must
|
|
27
|
+
* not see the one-shot markers; a replacement of this process must.
|
|
28
|
+
*/
|
|
29
|
+
export function daemonDirectReplacementEnv(base) {
|
|
30
|
+
if (!directMarkers)
|
|
31
|
+
return base;
|
|
32
|
+
return { ...base, ...directMarkers };
|
|
33
|
+
}
|
|
34
|
+
export function consumeOneShotOwnerMarkers(env) {
|
|
35
|
+
const handoffPath = typeof env.HQ_DAEMON_HANDOFF_PATH === "string" && env.HQ_DAEMON_HANDOFF_PATH !== ""
|
|
36
|
+
? env.HQ_DAEMON_HANDOFF_PATH
|
|
37
|
+
: undefined;
|
|
38
|
+
const controlFd = Number(env.HQ_DAEMON_OWNER_CONTROL_FD);
|
|
39
|
+
const markers = {};
|
|
40
|
+
for (const key of ONE_SHOT_OWNER_KEYS) {
|
|
41
|
+
const value = env[key];
|
|
42
|
+
if (typeof value === "string" && value !== "")
|
|
43
|
+
markers[key] = value;
|
|
44
|
+
delete env[key];
|
|
45
|
+
}
|
|
46
|
+
if (env === process.env) {
|
|
47
|
+
directMarkers = markers;
|
|
48
|
+
if (markers.HQ_DAEMON_RESIDENT === "1")
|
|
49
|
+
directResidentSkip = true;
|
|
50
|
+
}
|
|
51
|
+
return { handoffPath, controlFd };
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=daemon-helper-markers.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** True when `p` lives under a version-pinned package-manager or runtime directory. */
|
|
2
|
+
export declare function isVersionedStorePath(p: string): boolean;
|
|
3
|
+
/** True when `p` is a versioned store path that is not the running CLI entry. */
|
|
4
|
+
export declare function isStaleVersionedStorePath(p: string, runningEntry: string | undefined): boolean;
|
|
5
|
+
export interface McpLaunch {
|
|
6
|
+
/** Absolute node binary. */
|
|
7
|
+
command: string;
|
|
8
|
+
/** Absolute CLI entry script, unversioned where one can be proven to be this CLI. */
|
|
9
|
+
entry: string;
|
|
10
|
+
/** PATH for the MCP subprocess. */
|
|
11
|
+
path: string;
|
|
12
|
+
}
|
|
13
|
+
export interface McpLaunchInput {
|
|
14
|
+
execPath: string;
|
|
15
|
+
argv1: string;
|
|
16
|
+
env: NodeJS.ProcessEnv;
|
|
17
|
+
platform: NodeJS.Platform;
|
|
18
|
+
home?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decide what the MCP registration should run: an absolute node binary and an
|
|
22
|
+
* unversioned entry path for THIS CLI. No PATH lookup of `hq` and no shell shim
|
|
23
|
+
* is ever registered; a `hq` found on PATH is only used when it is a file or
|
|
24
|
+
* symlink whose realpath is the running entry (never on win32, where PATH hits
|
|
25
|
+
* are .cmd/.ps1 shims that hosts cannot spawn without a shell).
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveMcpLaunch(o: McpLaunchInput): McpLaunch;
|
|
28
|
+
//# sourceMappingURL=mcp-launcher.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { MCP_SUBPROCESS_PATH } from '../lib/install/claude-targets.js';
|
|
4
|
+
import { findExecutableOnPath } from './resolve-executable.js';
|
|
5
|
+
const PKG_NAME = '@indigoai-us/hq-cli';
|
|
6
|
+
const SEP = '[\\\\/]';
|
|
7
|
+
/**
|
|
8
|
+
* Path layouts that embed a package or runtime version, so a path recorded
|
|
9
|
+
* inside them goes stale on the next update. Kept small and tested.
|
|
10
|
+
*/
|
|
11
|
+
const VERSIONED_PATH_RES = [
|
|
12
|
+
// pnpm content-addressable store links and virtual store
|
|
13
|
+
new RegExp(`${SEP}store${SEP}v\\d+${SEP}links${SEP}`),
|
|
14
|
+
new RegExp(`${SEP}\\.pnpm${SEP}`),
|
|
15
|
+
// Homebrew: Cellar/<formula>/<version>/...
|
|
16
|
+
new RegExp(`${SEP}Cellar${SEP}[^\\\\/]+${SEP}[^\\\\/]+${SEP}`),
|
|
17
|
+
// nvm (versions/node/vX), fnm (node-versions/vX)
|
|
18
|
+
new RegExp(`${SEP}versions${SEP}node${SEP}v?\\d`),
|
|
19
|
+
new RegExp(`${SEP}node-versions${SEP}v?\\d`),
|
|
20
|
+
// volta: tools/image/node/<version>, tools/image/packages/<name>
|
|
21
|
+
new RegExp(`${SEP}tools${SEP}image${SEP}(?:node|packages)${SEP}`),
|
|
22
|
+
// yarn berry caches (zip per version)
|
|
23
|
+
new RegExp(`${SEP}\\.yarn${SEP}(?:berry${SEP})?cache${SEP}`),
|
|
24
|
+
];
|
|
25
|
+
/** True when `p` lives under a version-pinned package-manager or runtime directory. */
|
|
26
|
+
export function isVersionedStorePath(p) {
|
|
27
|
+
return VERSIONED_PATH_RES.some((re) => re.test(p));
|
|
28
|
+
}
|
|
29
|
+
function realOrSelf(p) {
|
|
30
|
+
try {
|
|
31
|
+
return fs.realpathSync(p);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return p;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** True when `p` is a versioned store path that is not the running CLI entry. */
|
|
38
|
+
export function isStaleVersionedStorePath(p, runningEntry) {
|
|
39
|
+
if (!isVersionedStorePath(p))
|
|
40
|
+
return false;
|
|
41
|
+
if (!runningEntry)
|
|
42
|
+
return true;
|
|
43
|
+
return realOrSelf(p) !== realOrSelf(runningEntry);
|
|
44
|
+
}
|
|
45
|
+
/** Walk up from `file` to the directory whose package.json is this package. */
|
|
46
|
+
function packageRootOf(file) {
|
|
47
|
+
let dir = path.dirname(file);
|
|
48
|
+
for (;;) {
|
|
49
|
+
try {
|
|
50
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
51
|
+
if (pkg.name === PKG_NAME)
|
|
52
|
+
return dir;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// keep walking
|
|
56
|
+
}
|
|
57
|
+
const parent = path.dirname(dir);
|
|
58
|
+
if (parent === dir)
|
|
59
|
+
return undefined;
|
|
60
|
+
dir = parent;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Stable `global/<n>/node_modules/@indigoai-us/hq-cli/<entry>` links pnpm maintains across updates. */
|
|
64
|
+
function pnpmGlobalAliases(realEntry, o) {
|
|
65
|
+
const root = packageRootOf(realEntry);
|
|
66
|
+
if (!root)
|
|
67
|
+
return [];
|
|
68
|
+
const rel = path.relative(root, realEntry);
|
|
69
|
+
const homes = [
|
|
70
|
+
o.env.PNPM_HOME,
|
|
71
|
+
o.env.LOCALAPPDATA ? path.join(o.env.LOCALAPPDATA, 'pnpm') : undefined,
|
|
72
|
+
o.home ? path.join(o.home, 'Library', 'pnpm') : undefined,
|
|
73
|
+
o.home ? path.join(o.home, '.local', 'share', 'pnpm') : undefined,
|
|
74
|
+
].filter((h) => !!h);
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const h of homes) {
|
|
77
|
+
let versions;
|
|
78
|
+
try {
|
|
79
|
+
versions = fs.readdirSync(path.join(h, 'global'));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
for (const v of versions)
|
|
85
|
+
out.push(path.join(h, 'global', v, 'node_modules', ...PKG_NAME.split('/'), rel));
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Decide what the MCP registration should run: an absolute node binary and an
|
|
91
|
+
* unversioned entry path for THIS CLI. No PATH lookup of `hq` and no shell shim
|
|
92
|
+
* is ever registered; a `hq` found on PATH is only used when it is a file or
|
|
93
|
+
* symlink whose realpath is the running entry (never on win32, where PATH hits
|
|
94
|
+
* are .cmd/.ps1 shims that hosts cannot spawn without a shell).
|
|
95
|
+
*/
|
|
96
|
+
export function resolveMcpLaunch(o) {
|
|
97
|
+
const argv1 = path.resolve(o.argv1);
|
|
98
|
+
const realEntry = realOrSelf(argv1);
|
|
99
|
+
const pathValue = o.env.PATH ?? '';
|
|
100
|
+
const candidates = [argv1, ...pnpmGlobalAliases(realEntry, o)];
|
|
101
|
+
if (o.platform !== 'win32') {
|
|
102
|
+
const onPath = findExecutableOnPath('hq', pathValue, o.platform);
|
|
103
|
+
if (onPath)
|
|
104
|
+
candidates.push(onPath);
|
|
105
|
+
}
|
|
106
|
+
const entry = candidates.find((c) => path.isAbsolute(c) && !isVersionedStorePath(c) && fs.existsSync(c) && realOrSelf(c) === realEntry) ?? realEntry;
|
|
107
|
+
let command = o.execPath;
|
|
108
|
+
if (isVersionedStorePath(command)) {
|
|
109
|
+
const nodeOnPath = findExecutableOnPath('node', pathValue, o.platform);
|
|
110
|
+
if (nodeOnPath && !isVersionedStorePath(nodeOnPath) && realOrSelf(nodeOnPath) === realOrSelf(command))
|
|
111
|
+
command = nodeOnPath;
|
|
112
|
+
}
|
|
113
|
+
const delimiter = o.platform === 'win32' ? ';' : ':';
|
|
114
|
+
const dirs = [path.dirname(command), path.dirname(o.execPath), ...MCP_SUBPROCESS_PATH.split(':')];
|
|
115
|
+
return { command, entry, path: [...new Set(dirs)].join(delimiter) };
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=mcp-launcher.js.map
|
|
@@ -7,6 +7,16 @@ export declare const PACKAGE_USE_LEASE_HANDOFF_ENV = "HQ_CLI_PACKAGE_USE_LEASE_H
|
|
|
7
7
|
*/
|
|
8
8
|
export declare const DEFAULT_UPDATE_WAIT_MS = 240000;
|
|
9
9
|
export declare function updateWaitTimeoutMs(env?: NodeJS.ProcessEnv): number;
|
|
10
|
+
/**
|
|
11
|
+
* The longest one process may hold a package-use lease. The heartbeat keeps the
|
|
12
|
+
* lease fresh for as long as the event loop runs, so without a ceiling a
|
|
13
|
+
* process stuck at a terminal prompt held it for four days and blocked every
|
|
14
|
+
* desktop CLI update (feedback_77165502). Ordinary commands finish in minutes,
|
|
15
|
+
* and long-running commands release after their preload. Override with
|
|
16
|
+
* HQ_CLI_PACKAGE_USE_MAX_HOLD_MS.
|
|
17
|
+
*/
|
|
18
|
+
export declare const DEFAULT_PACKAGE_USE_LEASE_MAX_HOLD_MS: number;
|
|
19
|
+
export declare function packageUseLeaseMaxHoldMs(env?: NodeJS.ProcessEnv): number;
|
|
10
20
|
export declare const PACKAGE_USE_LEASE_PURPOSES: readonly ["command", "resident-preload", "mcp-serve", "hook", "daemon"];
|
|
11
21
|
export type PackageUseLeasePurpose = (typeof PACKAGE_USE_LEASE_PURPOSES)[number];
|
|
12
22
|
export declare class PackageUseUpdatePendingError extends Error {
|
|
@@ -24,6 +24,28 @@ export function updateWaitTimeoutMs(env = process.env) {
|
|
|
24
24
|
}
|
|
25
25
|
const PACKAGE_USE_LEASE_HEARTBEAT_INTERVAL_MS = 5 * 60 * 1000;
|
|
26
26
|
let packageUseLeaseHeartbeatIntervalMs = PACKAGE_USE_LEASE_HEARTBEAT_INTERVAL_MS;
|
|
27
|
+
/**
|
|
28
|
+
* The longest one process may hold a package-use lease. The heartbeat keeps the
|
|
29
|
+
* lease fresh for as long as the event loop runs, so without a ceiling a
|
|
30
|
+
* process stuck at a terminal prompt held it for four days and blocked every
|
|
31
|
+
* desktop CLI update (feedback_77165502). Ordinary commands finish in minutes,
|
|
32
|
+
* and long-running commands release after their preload. Override with
|
|
33
|
+
* HQ_CLI_PACKAGE_USE_MAX_HOLD_MS.
|
|
34
|
+
*/
|
|
35
|
+
export const DEFAULT_PACKAGE_USE_LEASE_MAX_HOLD_MS = 3 * 60 * 60 * 1000;
|
|
36
|
+
export function packageUseLeaseMaxHoldMs(env = process.env) {
|
|
37
|
+
const raw = env.HQ_CLI_PACKAGE_USE_MAX_HOLD_MS?.trim();
|
|
38
|
+
if (!raw || !/^\d+$/.test(raw) || Number(raw) <= 0)
|
|
39
|
+
return DEFAULT_PACKAGE_USE_LEASE_MAX_HOLD_MS;
|
|
40
|
+
return Number(raw);
|
|
41
|
+
}
|
|
42
|
+
function formatHoldDuration(ms) {
|
|
43
|
+
if (ms >= 60 * 60 * 1000)
|
|
44
|
+
return `${Math.round(ms / (60 * 60 * 1000))}h`;
|
|
45
|
+
if (ms >= 60 * 1000)
|
|
46
|
+
return `${Math.round(ms / (60 * 1000))}m`;
|
|
47
|
+
return `${Math.max(1, Math.round(ms / 1000))}s`;
|
|
48
|
+
}
|
|
27
49
|
export const PACKAGE_USE_LEASE_PURPOSES = [
|
|
28
50
|
"command",
|
|
29
51
|
"resident-preload",
|
|
@@ -224,10 +246,19 @@ export function acquirePackageUseLease(prefix, hqVersion, stateDirectory, purpos
|
|
|
224
246
|
let released = false;
|
|
225
247
|
let heartbeatInFlight = false;
|
|
226
248
|
let heartbeat = null;
|
|
249
|
+
const acquiredAtMs = Date.now();
|
|
250
|
+
const maxHoldMs = packageUseLeaseMaxHoldMs();
|
|
227
251
|
const startHeartbeat = () => {
|
|
228
252
|
heartbeat = setInterval(() => {
|
|
229
253
|
if (released || heartbeatInFlight)
|
|
230
254
|
return;
|
|
255
|
+
const heldMs = Date.now() - acquiredAtMs;
|
|
256
|
+
if (heldMs >= maxHoldMs) {
|
|
257
|
+
process.stderr.write(`hq-cli: released the HQ CLI package-use lease held by pid ${record.pid} (${purpose}) ` +
|
|
258
|
+
`after ${formatHoldDuration(heldMs)} so HQ CLI updates are not blocked\n`);
|
|
259
|
+
release();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
231
262
|
heartbeatInFlight = true;
|
|
232
263
|
const now = new Date();
|
|
233
264
|
void utimes(leasePath, now, now)
|