@indigoai-us/hq-cli 5.345.220 → 5.345.222
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 +164 -0
- package/dist/command-catalog.generated.d.ts +175 -2
- package/dist/command-catalog.generated.js +213 -2
- package/dist/commands/auth.js +6 -2
- package/dist/commands/bot-runtime.d.ts +18 -5
- package/dist/commands/bot-runtime.js +29 -12
- package/dist/commands/bot.d.ts +39 -2
- package/dist/commands/bot.js +140 -11
- package/dist/commands/core-delegate-w1b.js +9 -3
- package/dist/commands/core-stop-autosave.d.ts +21 -0
- package/dist/commands/core-stop-autosave.js +300 -0
- package/dist/commands/core.js +2 -0
- package/dist/commands/daemon.d.ts +6 -0
- package/dist/commands/daemon.js +46 -8
- package/dist/commands/doctor-usage.d.ts +5 -0
- package/dist/commands/doctor-usage.js +25 -0
- package/dist/commands/env.d.ts +49 -4
- package/dist/commands/env.js +355 -5
- package/dist/commands/lanes-shared.d.ts +3 -0
- package/dist/commands/lanes.d.ts +3 -0
- package/dist/commands/lanes.js +87 -3
- package/dist/commands/link.d.ts +2 -0
- package/dist/commands/link.js +14 -2
- package/dist/commands/mesh.js +23 -3
- package/dist/commands/sources.js +3 -3
- package/dist/commands/sync-core.d.ts +7 -0
- package/dist/commands/sync-core.js +132 -3
- package/dist/commands/whoami.js +96 -80
- package/dist/data/command-states.json +3 -0
- package/dist/lib/bot/config.d.ts +17 -0
- package/dist/lib/bot/config.js +13 -0
- package/dist/lib/bot/fleet-cron.js +24 -2
- package/dist/lib/bot/fleet-local.d.ts +30 -2
- package/dist/lib/bot/fleet-local.js +60 -1
- package/dist/lib/bot/grok-login-scope.d.ts +36 -0
- package/dist/lib/bot/grok-login-scope.js +93 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +32 -9
- package/dist/lib/bot/legacy-provider-login.js +28 -22
- package/dist/lib/bot/runtime-sign-in-check.js +6 -1
- package/dist/lib/bot/runtime-sign-in.d.ts +2 -0
- package/dist/lib/core-utils/sentry-report.js +2 -0
- package/dist/lib/daemon/catalog.js +2 -0
- package/dist/lib/daemon/install.d.ts +2 -1
- package/dist/lib/daemon/install.js +9 -0
- package/dist/lib/daemon/legacy.d.ts +1 -0
- package/dist/lib/daemon/legacy.js +5 -2
- package/dist/lib/daemon/machine-check-owner.d.ts +2 -0
- package/dist/lib/daemon/machine-check-owner.js +6 -1
- package/dist/lib/daemon/machine-checks.d.ts +29 -2
- package/dist/lib/daemon/machine-checks.js +64 -13
- package/dist/lib/daemon/mesh-owner.d.ts +45 -0
- package/dist/lib/daemon/mesh-owner.js +166 -0
- package/dist/lib/daemon/native-notice-ledger.d.ts +27 -0
- package/dist/lib/daemon/native-notice-ledger.js +113 -0
- package/dist/lib/daemon/run.d.ts +15 -0
- package/dist/lib/daemon/run.js +137 -11
- package/dist/lib/daemon/selfcheck.d.ts +35 -2
- package/dist/lib/daemon/selfcheck.js +118 -9
- package/dist/lib/daemon/stop-autosave-lock.d.ts +19 -0
- package/dist/lib/daemon/stop-autosave-lock.js +166 -0
- package/dist/lib/daemon/stop-autosave.d.ts +28 -0
- package/dist/lib/daemon/stop-autosave.js +111 -0
- package/dist/lib/daemon/stop-diagnostics.d.ts +11 -0
- package/dist/lib/daemon/stop-diagnostics.js +136 -0
- package/dist/lib/daemon/stop-job-executor.d.ts +19 -0
- package/dist/lib/daemon/stop-job-executor.js +126 -0
- package/dist/lib/daemon/stop-jobs.d.ts +93 -0
- package/dist/lib/daemon/stop-jobs.js +599 -0
- package/dist/lib/doctor/checks/fleet-hook-core.js +1 -1
- package/dist/lib/doctor/checks/host-core.js +1 -1
- package/dist/lib/environment/access-probes.d.ts +234 -0
- package/dist/lib/environment/access-probes.js +918 -0
- package/dist/lib/environment/profile.d.ts +15 -2
- package/dist/lib/environment/profile.js +55 -20
- package/dist/lib/environment/rfc3339.d.ts +8 -0
- package/dist/lib/environment/rfc3339.js +26 -0
- package/dist/lib/environment/work-requirements.d.ts +169 -0
- package/dist/lib/environment/work-requirements.js +389 -0
- package/dist/lib/hook-engine/coverage.d.ts +146 -7
- package/dist/lib/hook-engine/coverage.js +1480 -4
- package/dist/lib/hook-engine/fleet-registration.js +2 -2
- package/dist/lib/hook-engine/git-stop-config.json +513 -0
- package/dist/lib/hook-engine/native-stop-compatibility.json +362 -0
- package/dist/lib/hook-engine/reconcile.js +2 -2
- package/dist/lib/hot-channels/alert.d.ts +2 -2
- package/dist/lib/hot-channels/alert.js +8 -5
- package/dist/lib/hot-channels/signals-pass.d.ts +126 -17
- package/dist/lib/hot-channels/signals-pass.js +296 -17
- package/dist/lib/hot-channels/signals-runtime.d.ts +30 -0
- package/dist/lib/hot-channels/signals-runtime.js +82 -0
- package/dist/lib/install/hook-target.js +1 -1
- package/dist/lib/lanes/host-capacity.d.ts +26 -2
- package/dist/lib/lanes/host-capacity.js +17 -2
- package/dist/lib/lanes/host-compute.d.ts +90 -0
- package/dist/lib/lanes/host-compute.js +185 -0
- package/dist/lib/lanes/index.d.ts +2 -0
- package/dist/lib/lanes/index.js +1 -0
- package/dist/lib/lanes/reconcile.js +22 -1
- package/dist/lib/lanes/resource-usage.d.ts +155 -0
- package/dist/lib/lanes/resource-usage.js +589 -0
- package/dist/lib/lanes/run-size.d.ts +46 -0
- package/dist/lib/lanes/run-size.js +197 -0
- package/dist/lib/lanes/store.js +27 -0
- package/dist/lib/lanes/throttle.d.ts +36 -1
- package/dist/lib/lanes/throttle.js +47 -10
- package/dist/lib/lanes/types.d.ts +40 -0
- package/dist/lib/lanes/types.js +2 -2
- package/dist/lib/lanes/watcher.js +13 -4
- package/dist/lib/lanes/worker-process.d.ts +25 -2
- package/dist/lib/lanes/worker-process.js +71 -19
- package/dist/lib/mesh/live/daemon/config.d.ts +8 -1
- package/dist/lib/mesh/live/daemon/config.js +16 -0
- package/dist/lib/mesh/live/daemon/install.d.ts +31 -2
- package/dist/lib/mesh/live/daemon/install.js +142 -1
- package/dist/lib/mesh/live/daemon/login-home.d.ts +5 -0
- package/dist/lib/mesh/live/daemon/login-home.js +27 -0
- package/dist/lib/mesh/live/daemon/unit-home.d.ts +49 -0
- package/dist/lib/mesh/live/daemon/unit-home.js +134 -0
- package/dist/lib/placement/match.d.ts +192 -0
- package/dist/lib/placement/match.js +667 -0
- package/dist/lib/registry/registry.d.ts +35 -7
- package/dist/lib/registry/registry.js +88 -13
- package/dist/lib/safe-store.d.ts +14 -0
- package/dist/lib/safe-store.js +77 -4
- package/dist/lib/scan-packages/index.js +24 -2
- package/dist/lib/search-index/index.d.ts +88 -4
- package/dist/lib/search-index/index.js +288 -24
- package/dist/lib/search-index/max-doc-bytes.d.ts +17 -6
- package/dist/lib/search-index/max-doc-bytes.js +49 -23
- package/dist/lib/service-manager/startup-migration.d.ts +1 -0
- package/dist/lib/service-manager/startup-migration.js +3 -0
- package/dist/lib/signals/codex-lockdown.d.ts +79 -0
- package/dist/lib/signals/codex-lockdown.js +271 -0
- package/dist/lib/signals/codex-login.d.ts +37 -0
- package/dist/lib/signals/codex-login.js +83 -0
- package/dist/lib/signals/engine.d.ts +27 -0
- package/dist/lib/signals/engine.js +70 -0
- package/dist/lib/signals/reply.js +22 -1
- package/dist/lib/signals/run-record.js +6 -1
- package/dist/lib/signals/settings.d.ts +5 -0
- package/dist/lib/signals/settings.js +2 -0
- package/dist/lib/signals/steps/extract.d.ts +85 -9
- package/dist/lib/signals/steps/extract.js +317 -68
- package/dist/lib/signals/steps/types.d.ts +2 -0
- package/dist/lib/sync-core/engine-holds.d.ts +14 -0
- package/dist/lib/sync-core/engine-holds.js +60 -0
- package/dist/lib/sync-core/engine-home.d.ts +37 -1
- package/dist/lib/sync-core/engine-home.js +119 -16
- package/dist/lib/sync-core/reconcile.js +8 -0
- package/dist/lib/sync-core/rollout-state.d.ts +2 -1
- package/dist/lib/sync-core/rollout-state.js +5 -2
- package/dist/lib/sync-core/vault-settings.d.ts +21 -0
- package/dist/lib/sync-core/vault-settings.js +131 -0
- package/dist/lib/sync-core/vault-uid.d.ts +2 -0
- package/dist/lib/sync-core/vault-uid.js +4 -0
- package/dist/lib/windows/install-followup.d.ts +2 -0
- package/dist/lib/windows/install-followup.js +4 -1
- package/dist/schemas/hq-work-requirements.v1.schema.json +145 -0
- package/dist/utils/auth-session-check.d.ts +16 -0
- package/dist/utils/auth-session-check.js +16 -0
- package/dist/utils/client-health-contract.d.ts +6 -0
- package/dist/utils/client-health-contract.js +13 -0
- package/dist/utils/client-health.d.ts +7 -0
- package/dist/utils/client-health.js +22 -1
- package/dist/utils/command-usage.d.ts +40 -0
- package/dist/utils/command-usage.js +61 -0
- package/dist/utils/duplicate-command-usage.d.ts +72 -0
- package/dist/utils/duplicate-command-usage.js +125 -0
- package/dist/utils/host-registration.js +5 -2
- package/dist/utils/pack-contributions.d.ts +3 -0
- package/dist/utils/pack-contributions.js +110 -23
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/self-update.d.ts +3 -1
- package/dist/utils/self-update.js +56 -11
- package/dist/utils/sentry-fingerprint.js +9 -0
- package/dist/utils/update-lock.d.ts +151 -14
- package/dist/utils/update-lock.js +782 -46
- package/dist/utils/version-gate.js +12 -2
- package/dist/utils/windows-bash.d.ts +2 -0
- package/dist/utils/windows-bash.js +2 -1
- package/package.json +9 -8
- package/scripts/bench-core-compare.mjs +313 -0
- package/scripts/bench-core.mjs +399 -12
- package/scripts/check-function-manifest.mjs +116 -3
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { openStoreDir, parentStatRefusal } from "../safe-store.js";
|
|
6
|
+
function birthToken(pid) {
|
|
7
|
+
if (!Number.isSafeInteger(pid) || pid <= 0 || !fs.existsSync("/bin/ps"))
|
|
8
|
+
return undefined;
|
|
9
|
+
const result = spawnSync("/bin/ps", ["-p", String(pid), "-o", "lstart="], {
|
|
10
|
+
env: { PATH: "/usr/bin:/bin", LC_ALL: "C" }, encoding: "utf8", timeout: 1000, maxBuffer: 4096,
|
|
11
|
+
});
|
|
12
|
+
if (result.error || result.status !== 0)
|
|
13
|
+
return undefined;
|
|
14
|
+
return result.stdout.replace(/\s/g, "") || undefined;
|
|
15
|
+
}
|
|
16
|
+
/** Why the folder that holds lock `directory` cannot hold it, or undefined.
|
|
17
|
+
* The folder is resolved first (macOS /tmp is a link to /private/tmp) and must
|
|
18
|
+
* be owned by this user or root and closed to group and other writes, unless
|
|
19
|
+
* it is a root-owned sticky folder such as /tmp: there another user cannot
|
|
20
|
+
* remove or rename this user's lock, and the lock folder itself is checked as
|
|
21
|
+
* a private store before it is used. A store check of the parent itself would
|
|
22
|
+
* refuse /tmp (owned by root, world-writable), which made autosave skip every
|
|
23
|
+
* file on Linux without TMPDIR.
|
|
24
|
+
*/
|
|
25
|
+
export function lockParentRefusal(directory) {
|
|
26
|
+
let real;
|
|
27
|
+
try {
|
|
28
|
+
real = fs.realpathSync(path.dirname(directory));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
return `lock folder unavailable (${error.code ?? "error"})`;
|
|
32
|
+
}
|
|
33
|
+
let stat;
|
|
34
|
+
try {
|
|
35
|
+
stat = fs.lstatSync(real);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return "lock folder unavailable";
|
|
39
|
+
}
|
|
40
|
+
if (!stat.isDirectory())
|
|
41
|
+
return "lock folder is not a folder";
|
|
42
|
+
if (process.platform === "win32" || typeof process.getuid !== "function")
|
|
43
|
+
return undefined;
|
|
44
|
+
const refused = parentStatRefusal(stat, process.getuid());
|
|
45
|
+
return refused ? `lock folder refused (${refused.message})` : undefined;
|
|
46
|
+
}
|
|
47
|
+
/** Recover only an old, private autosave lock whose owner is definitely gone.
|
|
48
|
+
* Live or unreadable process identity stays held. The renamed directory is
|
|
49
|
+
* checked against the original inode before anything inside it is removed.
|
|
50
|
+
*/
|
|
51
|
+
export function acquireAutosaveLock(directory, now, staleMinutes, alive = pid => {
|
|
52
|
+
try {
|
|
53
|
+
process.kill(pid, 0);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return error.code === "ESRCH" ? false : undefined;
|
|
58
|
+
}
|
|
59
|
+
}, birth = birthToken) {
|
|
60
|
+
let recovered = false;
|
|
61
|
+
if (lockParentRefusal(directory) !== undefined)
|
|
62
|
+
return undefined;
|
|
63
|
+
try {
|
|
64
|
+
fs.mkdirSync(directory, { mode: 0o700 });
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error.code !== "EEXIST")
|
|
68
|
+
return undefined;
|
|
69
|
+
let before;
|
|
70
|
+
try {
|
|
71
|
+
before = fs.lstatSync(directory);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
if (!before.isDirectory() || before.isSymbolicLink() || now - before.mtimeMs <= staleMinutes * 60_000)
|
|
77
|
+
return undefined;
|
|
78
|
+
const store = openStoreDir(directory);
|
|
79
|
+
if (!store.ok)
|
|
80
|
+
return undefined;
|
|
81
|
+
try {
|
|
82
|
+
const entries = store.value.list();
|
|
83
|
+
if (!entries.ok || entries.value.some(name => name !== "owner"))
|
|
84
|
+
return undefined;
|
|
85
|
+
const owner = store.value.read("owner", { maxBytes: 512 });
|
|
86
|
+
if (owner.ok) {
|
|
87
|
+
const match = /^pid=(\d+)$/m.exec(owner.value.toString());
|
|
88
|
+
if (!match)
|
|
89
|
+
return undefined;
|
|
90
|
+
const pid = Number(match[1]);
|
|
91
|
+
const state = alive(pid);
|
|
92
|
+
if (state === undefined)
|
|
93
|
+
return undefined;
|
|
94
|
+
if (state) {
|
|
95
|
+
const recorded = /^birth=(.*)$/m.exec(owner.value.toString())?.[1];
|
|
96
|
+
const current = birth(pid);
|
|
97
|
+
if (!recorded || !current || recorded === current)
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (owner.code !== "missing")
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
store.value.close();
|
|
106
|
+
}
|
|
107
|
+
const retired = `${directory}.stale-${randomUUID()}`;
|
|
108
|
+
try {
|
|
109
|
+
fs.renameSync(directory, retired);
|
|
110
|
+
const captured = fs.lstatSync(retired);
|
|
111
|
+
if (captured.dev !== before.dev || captured.ino !== before.ino) {
|
|
112
|
+
if (!fs.existsSync(directory))
|
|
113
|
+
fs.renameSync(retired, directory);
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
const held = openStoreDir(retired);
|
|
117
|
+
if (!held.ok)
|
|
118
|
+
return undefined;
|
|
119
|
+
try {
|
|
120
|
+
held.value.remove("owner");
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
held.value.close();
|
|
124
|
+
}
|
|
125
|
+
fs.rmdirSync(retired);
|
|
126
|
+
fs.mkdirSync(directory, { mode: 0o700 });
|
|
127
|
+
recovered = true;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const held = openStoreDir(directory);
|
|
134
|
+
if (!held.ok)
|
|
135
|
+
return undefined;
|
|
136
|
+
let identity;
|
|
137
|
+
try {
|
|
138
|
+
identity = fs.lstatSync(directory);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
held.value.close();
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const owner = `pid=${process.pid}\nbirth=${birth(process.pid) ?? ""}\n`;
|
|
145
|
+
if (!held.value.writeAtomic("owner", owner).ok) {
|
|
146
|
+
held.value.close();
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
return { recovered, release() {
|
|
150
|
+
try {
|
|
151
|
+
const current = held.value.read("owner", { maxBytes: 512 });
|
|
152
|
+
if (current.ok && current.value.toString() === owner)
|
|
153
|
+
held.value.remove("owner");
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
held.value.close();
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const current = fs.lstatSync(directory);
|
|
160
|
+
if (current.dev === identity.dev && current.ino === identity.ino)
|
|
161
|
+
fs.rmdirSync(directory);
|
|
162
|
+
}
|
|
163
|
+
catch { /* Changed or nonempty lock remains held. */ }
|
|
164
|
+
} };
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=stop-autosave-lock.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface SweepDependencies {
|
|
2
|
+
git: (args: string[]) => {
|
|
3
|
+
code: number;
|
|
4
|
+
stdout: string;
|
|
5
|
+
stderr: string;
|
|
6
|
+
};
|
|
7
|
+
enabled: () => Promise<boolean>;
|
|
8
|
+
/** `top` is git's own `--show-toplevel` for the HQ root, to compare with git's top for the file. */
|
|
9
|
+
save: (relative: string, top: string) => Promise<number>;
|
|
10
|
+
log: (message: string) => void;
|
|
11
|
+
exists: (file: string) => boolean;
|
|
12
|
+
size: (file: string) => number | undefined;
|
|
13
|
+
}
|
|
14
|
+
/** Parse porcelain v1 -z without splitting filenames on whitespace. */
|
|
15
|
+
export declare function sweepCandidates(output: string): {
|
|
16
|
+
count: number;
|
|
17
|
+
paths: string[];
|
|
18
|
+
};
|
|
19
|
+
export declare function autosavePathAllowed(relative: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether git's `--show-toplevel` output names the checkout at `root` (a real
|
|
22
|
+
* path). Git for Windows prints `C:/Users/...` while Node's real path is
|
|
23
|
+
* `C:\Users\...`, and drive letters and names compare without case there.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sameCheckout(gitTop: string, root: string, platform?: NodeJS.Platform): boolean;
|
|
26
|
+
export declare function runStopSweep(root: string, minimum: number, deps: SweepDependencies): Promise<number>;
|
|
27
|
+
export declare function sweepFileSize(file: string): number | undefined;
|
|
28
|
+
//# sourceMappingURL=stop-autosave.d.ts.map
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** Stop sweep selection, separated from Git mutation and flag I/O.
|
|
2
|
+
* Only the local HQ checkout is eligible. Its nested repositories and company
|
|
3
|
+
* knowledge remain owned by their existing workflows.
|
|
4
|
+
*/
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
/** Parse porcelain v1 -z without splitting filenames on whitespace. */
|
|
8
|
+
export function sweepCandidates(output) {
|
|
9
|
+
const entries = output.split("\0");
|
|
10
|
+
let count = 0;
|
|
11
|
+
const paths = [];
|
|
12
|
+
for (let index = 0; index < entries.length; index++) {
|
|
13
|
+
const entry = entries[index];
|
|
14
|
+
if (!entry)
|
|
15
|
+
continue;
|
|
16
|
+
count++;
|
|
17
|
+
const xy = entry.slice(0, 2);
|
|
18
|
+
if (/[RC]/.test(xy)) {
|
|
19
|
+
index++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (xy === "??" || (xy[0] === " " && xy[1] !== " " && xy[1] !== "D"))
|
|
23
|
+
paths.push(entry.slice(3));
|
|
24
|
+
}
|
|
25
|
+
return { count, paths };
|
|
26
|
+
}
|
|
27
|
+
export function autosavePathAllowed(relative) {
|
|
28
|
+
if (!relative || path.isAbsolute(relative)
|
|
29
|
+
|| Array.from(relative).some(character => character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127))
|
|
30
|
+
return false;
|
|
31
|
+
if (relative.split(/[\\/]/).some(part => part === ".." || part === ".git"))
|
|
32
|
+
return false;
|
|
33
|
+
if (/^(?:repos|node_modules|\.next|\.vercel)\//.test(relative))
|
|
34
|
+
return false;
|
|
35
|
+
if (/^(?:workspace\/worktrees|\.claude\/worktrees)\//.test(relative))
|
|
36
|
+
return false;
|
|
37
|
+
if (/^companies\/[^/]+\/knowledge(?:\/|$)/.test(relative))
|
|
38
|
+
return false;
|
|
39
|
+
return !/\.(?:tmp|log)$/.test(relative);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Whether git's `--show-toplevel` output names the checkout at `root` (a real
|
|
43
|
+
* path). Git for Windows prints `C:/Users/...` while Node's real path is
|
|
44
|
+
* `C:\Users\...`, and drive letters and names compare without case there.
|
|
45
|
+
*/
|
|
46
|
+
export function sameCheckout(gitTop, root, platform = process.platform) {
|
|
47
|
+
if (platform !== "win32")
|
|
48
|
+
return gitTop === root;
|
|
49
|
+
const plain = (value) => path.win32.normalize(value).replace(/[\\]+$/, "").toLowerCase();
|
|
50
|
+
return gitTop !== "" && plain(gitTop) === plain(root);
|
|
51
|
+
}
|
|
52
|
+
export async function runStopSweep(root, minimum, deps) {
|
|
53
|
+
if (!deps.exists(path.join(root, "core/core.yaml")) || !deps.exists(path.join(root, ".git")))
|
|
54
|
+
return 0;
|
|
55
|
+
const top = deps.git(["rev-parse", "--show-toplevel"]);
|
|
56
|
+
const hqTop = top.stdout.trim();
|
|
57
|
+
if (top.code !== 0 || !sameCheckout(hqTop, root) || !await deps.enabled())
|
|
58
|
+
return 0;
|
|
59
|
+
const status = deps.git(["status", "--porcelain=v1", "-z", "--untracked-files=all"]);
|
|
60
|
+
if (status.code !== 0) {
|
|
61
|
+
deps.log(`FAIL stage=bash-status exit=${status.code}`);
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
const candidates = sweepCandidates(status.stdout);
|
|
65
|
+
if (candidates.count > 20) {
|
|
66
|
+
deps.log(`SKIP stage=bash-sweep reason=path-cap changed=${candidates.count} cap=20`);
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
for (const relative of candidates.paths) {
|
|
70
|
+
if (relative.split("/").includes("..") || path.isAbsolute(relative)) {
|
|
71
|
+
deps.log("SKIP stage=bash-sweep reason=unsafe-path");
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const ignored = deps.git(["check-ignore", "-q", "--no-index", "--", relative]);
|
|
75
|
+
if (ignored.code === 0) {
|
|
76
|
+
deps.log("SKIP stage=bash-sweep reason=ignored-path");
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (ignored.code !== 1) {
|
|
80
|
+
deps.log(`SKIP stage=bash-sweep reason=ignore-check-failed exit=${ignored.code}`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (!deps.exists(path.join(root, relative)))
|
|
84
|
+
continue;
|
|
85
|
+
const size = deps.size(path.join(root, relative));
|
|
86
|
+
if (size === undefined) {
|
|
87
|
+
deps.log("SKIP stage=bash-sweep reason=not-regular-file");
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (!relative.endsWith(".hqcloud") && size !== undefined && size > minimum) {
|
|
91
|
+
deps.log(`SKIP stage=bash-sweep reason=oversize bytes=${size} min=${minimum}`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!autosavePathAllowed(relative))
|
|
95
|
+
continue;
|
|
96
|
+
// The shell uses `set -u`, so a strict per-file failure does not prevent
|
|
97
|
+
// the remaining candidates from being processed.
|
|
98
|
+
await deps.save(relative, hqTop);
|
|
99
|
+
}
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
export function sweepFileSize(file) {
|
|
103
|
+
try {
|
|
104
|
+
const stat = fs.lstatSync(file);
|
|
105
|
+
return stat.isFile() && !stat.isSymbolicLink() ? stat.size : undefined;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=stop-autosave.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface StopDiagnosticOptions {
|
|
2
|
+
root: string;
|
|
3
|
+
ownsQueue: () => boolean;
|
|
4
|
+
report?: (body: string) => Promise<{
|
|
5
|
+
code: number;
|
|
6
|
+
}>;
|
|
7
|
+
log: (message: string) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function closeStopDiagnosticCursor(root: string): void;
|
|
10
|
+
export declare function drainStopDiagnostics(options: StopDiagnosticOptions): Promise<number>;
|
|
11
|
+
//# sourceMappingURL=stop-diagnostics.d.ts.map
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/** Delivers native Stop timeout diagnostics through the existing closed
|
|
2
|
+
* Sentry transport. It never reads hook payloads or executable text. */
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { openStoreDir } from "../safe-store.js";
|
|
5
|
+
import { reportSentryEvent } from "../core-utils/sentry-report.js";
|
|
6
|
+
const FIELDS = new Set(["hook_name", "hook_event", "hook_path", "session_id", "declared_timeout_ms", "elapsed_ms", "remaining_ms", "exit_code", "watchdog_timeout_ms", "tool_name", "hq_version", "platform", "load_average", "bash_env_set", "shell", "cwd_kind", "root_storage_class", "timing_precision", "nproc", "hook_script", "slow_phase", "slow_child", "slow_child_ms"]);
|
|
7
|
+
const activeRoots = new Set();
|
|
8
|
+
const cursors = new Map();
|
|
9
|
+
export function closeStopDiagnosticCursor(root) {
|
|
10
|
+
const key = path.resolve(root);
|
|
11
|
+
const held = cursors.get(key);
|
|
12
|
+
if (!held)
|
|
13
|
+
return;
|
|
14
|
+
held.cursor.close();
|
|
15
|
+
held.store.close();
|
|
16
|
+
cursors.delete(key);
|
|
17
|
+
}
|
|
18
|
+
export async function drainStopDiagnostics(options) {
|
|
19
|
+
if (!options.ownsQueue()) {
|
|
20
|
+
closeStopDiagnosticCursor(options.root);
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
const key = path.resolve(options.root);
|
|
24
|
+
if (activeRoots.has(key))
|
|
25
|
+
return 0;
|
|
26
|
+
activeRoots.add(key);
|
|
27
|
+
let done = 0;
|
|
28
|
+
try {
|
|
29
|
+
let held = cursors.get(key);
|
|
30
|
+
if (!held) {
|
|
31
|
+
const opened = openStoreDir(path.join(options.root, "workspace/.hook-timeout-native"));
|
|
32
|
+
if (!opened.ok)
|
|
33
|
+
return 0;
|
|
34
|
+
const cursor = opened.value.nameCursor();
|
|
35
|
+
if (!cursor.ok) {
|
|
36
|
+
opened.value.close();
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
held = { store: opened.value, cursor: cursor.value };
|
|
40
|
+
cursors.set(key, held);
|
|
41
|
+
}
|
|
42
|
+
const { store } = held;
|
|
43
|
+
const names = held.cursor.read(64);
|
|
44
|
+
if (!names.ok) {
|
|
45
|
+
closeStopDiagnosticCursor(key);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
for (const name of names.value.names) {
|
|
49
|
+
if (!options.ownsQueue()) {
|
|
50
|
+
closeStopDiagnosticCursor(key);
|
|
51
|
+
return done;
|
|
52
|
+
}
|
|
53
|
+
if (!/^[a-f0-9]{64}\.json$/.test(name))
|
|
54
|
+
continue;
|
|
55
|
+
const data = store.read(name, { maxBytes: 16384 });
|
|
56
|
+
if (!data.ok)
|
|
57
|
+
continue;
|
|
58
|
+
const receipt = store.read(`${name}.reported`, { maxBytes: 16 });
|
|
59
|
+
if (receipt.ok && receipt.value.toString() === "done\n")
|
|
60
|
+
continue;
|
|
61
|
+
const attempts = store.read(`${name}.attempts`, { maxBytes: 16 });
|
|
62
|
+
const count = attempts.ok ? Number(attempts.value.toString()) : 0;
|
|
63
|
+
if (!Number.isSafeInteger(count) || count < 0 || count >= 3)
|
|
64
|
+
continue;
|
|
65
|
+
let value;
|
|
66
|
+
try {
|
|
67
|
+
value = JSON.parse(data.value.toString());
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (value.type !== "hook_timeout_warning" || value.level !== "warning"
|
|
73
|
+
|| value.message !== "HQ hook is approaching configured timeout" || typeof value.fingerprint !== "string" || !/^hook-timeout:Stop:[a-f0-9]{64}$/.test(value.fingerprint)
|
|
74
|
+
|| value.metadata === null || typeof value.metadata !== "object" || Array.isArray(value.metadata))
|
|
75
|
+
continue;
|
|
76
|
+
if (Object.keys(value).some(key => !["type", "level", "message", "fingerprint", "metadata", "breadcrumb"].includes(key)))
|
|
77
|
+
continue;
|
|
78
|
+
if (value.breadcrumb !== undefined && (typeof value.breadcrumb !== "string"
|
|
79
|
+
|| !/^[a-f0-9]{64}\/(absolute|relative)-[a-f0-9]{64}\.json$/.test(value.breadcrumb)))
|
|
80
|
+
continue;
|
|
81
|
+
const metadata = value.metadata;
|
|
82
|
+
const { hook_sequence: sequence, ...scalars } = metadata;
|
|
83
|
+
if (sequence !== undefined && (!Array.isArray(sequence) || sequence.length > 20 || sequence.some(row => {
|
|
84
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
85
|
+
return true;
|
|
86
|
+
const value = row;
|
|
87
|
+
return Object.keys(value).sort().join() !== "event,ms,script" || typeof value.script !== "string" || !/^[A-Za-z0-9._-]{1,48}$/.test(value.script)
|
|
88
|
+
|| typeof value.event !== "string" || !/^[A-Za-z0-9._-]{1,48}$/.test(value.event)
|
|
89
|
+
|| !Number.isSafeInteger(value.ms) || Number(value.ms) < 0 || Number(value.ms) > 86400000;
|
|
90
|
+
})))
|
|
91
|
+
continue;
|
|
92
|
+
if (Object.entries(scalars).some(([key, val]) => !FIELDS.has(key)
|
|
93
|
+
|| (typeof val !== "string" && typeof val !== "number"))
|
|
94
|
+
|| metadata.hook_name !== "master-hook.sh" || metadata.hook_event !== "Stop")
|
|
95
|
+
continue;
|
|
96
|
+
if (Object.values(scalars).some(val => typeof val === "string" && (val.length > 256 || /[\r\n\0]/.test(val)))
|
|
97
|
+
|| Object.values(scalars).some(val => typeof val === "number" && (!Number.isFinite(val) || val < 0)))
|
|
98
|
+
continue;
|
|
99
|
+
if (!store.writeAtomic(`${name}.attempts`, String(count + 1)).ok)
|
|
100
|
+
continue;
|
|
101
|
+
const report = options.report ?? (body => reportSentryEvent(body, { hqRoot: options.root, timeoutMs: 750 }));
|
|
102
|
+
let timer;
|
|
103
|
+
try {
|
|
104
|
+
const timeout = new Promise(resolve => { timer = setTimeout(() => resolve({ code: 1 }), 1000); });
|
|
105
|
+
const { breadcrumb, ...event } = value;
|
|
106
|
+
const result = await Promise.race([report(JSON.stringify(event)), timeout]);
|
|
107
|
+
let receiptReady = true;
|
|
108
|
+
if (result.code === 0 && typeof breadcrumb === "string") {
|
|
109
|
+
const [session, file] = breadcrumb.split("/");
|
|
110
|
+
const warnings = openStoreDir(path.join(options.root, "workspace/.hook-timeout-breadcrumbs", session));
|
|
111
|
+
receiptReady = false;
|
|
112
|
+
if (warnings.ok) {
|
|
113
|
+
receiptReady = warnings.value.writeAtomic(`${file}.reported`, "").ok;
|
|
114
|
+
warnings.value.close();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (result.code === 0 && receiptReady && store.writeAtomic(`${name}.reported`, "done\n").ok)
|
|
118
|
+
done++;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
options.log("Stop timeout diagnostic delivery failed; retained for retry");
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
if (timer)
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (names.value.done)
|
|
129
|
+
closeStopDiagnosticCursor(key);
|
|
130
|
+
return done;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
activeRoots.delete(key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=stop-diagnostics.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type StopJob } from "./stop-jobs.js";
|
|
2
|
+
/** Completion receipts are read only from this job's private root. */
|
|
3
|
+
export declare function stopJobCompleted(job: StopJob, allowedRoot?: string): boolean;
|
|
4
|
+
/** Remove completion proofs written before `olderThan` (ms since the epoch)
|
|
5
|
+
* from `root`'s private results folder. The drainer calls this only when its
|
|
6
|
+
* queue is fully consumed, so every proof removed is for a job behind its
|
|
7
|
+
* cursor; a proof is read only to settle an interrupted receipt. Bounded per call.
|
|
8
|
+
*/
|
|
9
|
+
export declare function pruneStopJobResults(root: string, olderThan: number, limit?: number): void;
|
|
10
|
+
export declare function executeStopJob(job: StopJob, options: {
|
|
11
|
+
nodeBinary: string;
|
|
12
|
+
hqBinary: string;
|
|
13
|
+
allowedRoot: string | undefined;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
code: number;
|
|
18
|
+
}>;
|
|
19
|
+
//# sourceMappingURL=stop-job-executor.d.ts.map
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** Execute typed Stop jobs through the running CLI's own entry point. */
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { STOP_JOB_KILL_MS, stopJobArguments } from "./stop-jobs.js";
|
|
6
|
+
import { openStoreDir } from "../safe-store.js";
|
|
7
|
+
/** Completion receipts are read only from this job's private root. */
|
|
8
|
+
export function stopJobCompleted(job, allowedRoot = job.root) {
|
|
9
|
+
try {
|
|
10
|
+
if (fs.realpathSync(job.root) !== fs.realpathSync(allowedRoot))
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const opened = openStoreDir(path.join(job.root, "workspace/orchestrator/stop-job-results"));
|
|
17
|
+
if (!opened.ok)
|
|
18
|
+
return false;
|
|
19
|
+
try {
|
|
20
|
+
const read = opened.value.read(`${job.digest}.json`, { maxBytes: 4096 });
|
|
21
|
+
if (!read.ok)
|
|
22
|
+
return false;
|
|
23
|
+
const value = JSON.parse(read.value.toString());
|
|
24
|
+
return value !== null && typeof value === "object"
|
|
25
|
+
&& value.id === job.id
|
|
26
|
+
&& value.digest === job.digest
|
|
27
|
+
&& value.status === "completed";
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
opened.value.close();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Remove completion proofs written before `olderThan` (ms since the epoch)
|
|
37
|
+
* from `root`'s private results folder. The drainer calls this only when its
|
|
38
|
+
* queue is fully consumed, so every proof removed is for a job behind its
|
|
39
|
+
* cursor; a proof is read only to settle an interrupted receipt. Bounded per call.
|
|
40
|
+
*/
|
|
41
|
+
export function pruneStopJobResults(root, olderThan, limit = 4096) {
|
|
42
|
+
const opened = openStoreDir(path.join(root, "workspace/orchestrator/stop-job-results"));
|
|
43
|
+
if (!opened.ok)
|
|
44
|
+
return;
|
|
45
|
+
const store = opened.value;
|
|
46
|
+
try {
|
|
47
|
+
const names = store.nameCursor();
|
|
48
|
+
if (!names.ok)
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
let scanned = 0;
|
|
52
|
+
while (scanned < limit) {
|
|
53
|
+
const batch = names.value.read(256);
|
|
54
|
+
if (!batch.ok)
|
|
55
|
+
return;
|
|
56
|
+
for (const name of batch.value.names) {
|
|
57
|
+
scanned += 1;
|
|
58
|
+
if (!/^[0-9a-f]{64}\.json$/.test(name))
|
|
59
|
+
continue;
|
|
60
|
+
const facts = store.stat(name);
|
|
61
|
+
if (facts.ok && facts.value.mtimeMs < olderThan)
|
|
62
|
+
store.remove(name);
|
|
63
|
+
}
|
|
64
|
+
if (batch.value.done)
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
names.value.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
store.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export function executeStopJob(job, options) {
|
|
77
|
+
// A disappeared working directory never falls back to the daemon's cwd.
|
|
78
|
+
try {
|
|
79
|
+
if (!options.allowedRoot || fs.realpathSync(job.root) !== fs.realpathSync(options.allowedRoot))
|
|
80
|
+
return Promise.resolve({ code: 1 });
|
|
81
|
+
if (!fs.statSync(job.cwd).isDirectory())
|
|
82
|
+
return Promise.resolve({ code: 1 });
|
|
83
|
+
if (!fs.statSync(path.join(job.root, "core/core.yaml")).isFile())
|
|
84
|
+
return Promise.resolve({ code: 1 });
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return Promise.resolve({ code: 1 });
|
|
88
|
+
}
|
|
89
|
+
const args = stopJobArguments(job);
|
|
90
|
+
const env = { ...job.env, HQ_ROOT: job.root, CLAUDE_PROJECT_DIR: job.root, HQ_NO_UPDATE_CHECK: "1", HQ_STOP_JOB_ID: job.id };
|
|
91
|
+
return new Promise(resolve => {
|
|
92
|
+
if (options.signal?.aborted) {
|
|
93
|
+
resolve({ code: 1 });
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const child = spawn(options.nodeBinary, [options.hqBinary, ...args], {
|
|
97
|
+
cwd: job.cwd, env, shell: false, windowsHide: true, stdio: "ignore",
|
|
98
|
+
});
|
|
99
|
+
const stop = () => { child.kill("SIGTERM"); };
|
|
100
|
+
options.signal?.addEventListener("abort", stop, { once: true });
|
|
101
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), options.timeoutMs ?? STOP_JOB_KILL_MS);
|
|
102
|
+
let settled = false;
|
|
103
|
+
const finish = (code) => {
|
|
104
|
+
if (settled)
|
|
105
|
+
return;
|
|
106
|
+
settled = true;
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
options.signal?.removeEventListener("abort", stop);
|
|
109
|
+
if (code === 0) {
|
|
110
|
+
const opened = openStoreDir(path.join(job.root, "workspace/orchestrator/stop-job-results"), { create: true });
|
|
111
|
+
if (opened.ok) {
|
|
112
|
+
try {
|
|
113
|
+
opened.value.writeAtomic(`${job.digest}.json`, JSON.stringify({ id: job.id, digest: job.digest, status: "completed" }));
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
opened.value.close();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
resolve({ code });
|
|
121
|
+
};
|
|
122
|
+
child.once("error", () => finish(1));
|
|
123
|
+
child.once("close", code => finish(code ?? 1));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=stop-job-executor.js.map
|