@primitive.ai/prim 0.1.0-alpha.97 → 0.1.0-alpha.99
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/README.md +20 -0
- package/dist/{chunk-ZYK3TBEW.js → chunk-25DDRMWS.js} +4 -6
- package/dist/{chunk-LY6NJ7KL.js → chunk-2IRLFOEL.js} +123 -19
- package/dist/{chunk-27ADT6FQ.js → chunk-2Q3OGSTV.js} +44 -6
- package/dist/{chunk-35FMRWJH.js → chunk-5LRKJTLA.js} +1 -1
- package/dist/{chunk-RK4ZS3HG.js → chunk-6C3CCVDI.js} +2 -2
- package/dist/{chunk-UO2U6IKF.js → chunk-7VIMU2HR.js} +1 -1
- package/dist/{chunk-OW4GJBLW.js → chunk-EJPKZT5M.js} +2 -2
- package/dist/{chunk-2MBV7ZMV.js → chunk-FK7FJPWS.js} +1 -1
- package/dist/{chunk-4GHANPWQ.js → chunk-JJI7FU24.js} +2 -2
- package/dist/{chunk-AJYZB5PF.js → chunk-JJYS7IPD.js} +3 -3
- package/dist/{chunk-Y5W4TZM7.js → chunk-K4JWJ6OM.js} +80 -269
- package/dist/{chunk-XEDJ5N3M.js → chunk-KGBMU76T.js} +2 -4
- package/dist/{chunk-7KABYYWG.js → chunk-O2WV5SQZ.js} +11 -13
- package/dist/{chunk-FBPZ4YUD.js → chunk-X5BN67D2.js} +2 -2
- package/dist/{chunk-DWRSEC34.js → chunk-XG7S3LIL.js} +596 -16
- package/dist/{chunk-3I4GEWXQ.js → chunk-XPS6LG6I.js} +8 -12
- package/dist/{chunk-QNPTWTBG.js → chunk-ZNLIGT3J.js} +2 -2
- package/dist/daemon/server.js +247 -180
- package/dist/hook-runtime/daemon/server.js +247 -180
- package/dist/hook-runtime/hooks/post-commit.js +114 -59
- package/dist/hook-runtime/hooks/post-rewrite.js +109 -54
- package/dist/hook-runtime/hooks/post-tool-use.js +256 -156
- package/dist/hook-runtime/hooks/pre-commit.js +374 -31
- package/dist/hook-runtime/hooks/pre-tool-use.js +237 -137
- package/dist/hook-runtime/hooks/prim-hook.js +281 -181
- package/dist/hook-runtime/hooks/session-start.js +209 -109
- package/dist/hook-runtime/index.js +656 -521
- package/dist/hook-runtime/statusline-main.js +71 -16
- package/dist/hooks/post-commit.js +7 -8
- package/dist/hooks/post-rewrite.js +6 -7
- package/dist/hooks/post-tool-use.js +13 -14
- package/dist/hooks/pre-commit.js +4 -4
- package/dist/hooks/pre-tool-use.js +11 -14
- package/dist/hooks/prim-hook.js +14 -15
- package/dist/hooks/session-end.js +6 -7
- package/dist/hooks/session-start.js +13 -14
- package/dist/index.js +33 -35
- package/dist/statusline-main.js +71 -16
- package/package.json +1 -1
- package/dist/chunk-DHOQGHKQ.js +0 -379
|
@@ -1,206 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
PRIM_CONFIG_SHELL_RESOLVER,
|
|
3
|
+
STABLE_HOOK_LAUNCHER_NAME,
|
|
5
4
|
atomicWriteFile,
|
|
5
|
+
commandMatchesBin,
|
|
6
|
+
compareSemver,
|
|
7
|
+
detachedHookShimCommand,
|
|
8
|
+
gitToplevel,
|
|
9
|
+
hookCommandResolution,
|
|
10
|
+
hookCommandResolutions,
|
|
11
|
+
packageRoot,
|
|
12
|
+
packageVersion,
|
|
6
13
|
primConfigDirectory,
|
|
14
|
+
shellQuote,
|
|
15
|
+
stableHookCommand,
|
|
7
16
|
syncDirectory,
|
|
8
17
|
syncFile,
|
|
9
18
|
withFileLockSync
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
|
|
12
|
-
// src/lib/bin-path.ts
|
|
13
|
-
import { existsSync, readFileSync } from "fs";
|
|
14
|
-
import { dirname, isAbsolute, join } from "path";
|
|
15
|
-
import { fileURLToPath } from "url";
|
|
16
|
-
|
|
17
|
-
// src/lib/semver.ts
|
|
18
|
-
function parseSemver(value) {
|
|
19
|
-
if (typeof value !== "string" || value.length > 256) return;
|
|
20
|
-
const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u.exec(
|
|
21
|
-
value
|
|
22
|
-
);
|
|
23
|
-
if (!match) return;
|
|
24
|
-
const prerelease = match[4]?.split(".") ?? [];
|
|
25
|
-
if (prerelease.some((part) => /^\d+$/u.test(part) && part.length > 1 && part.startsWith("0"))) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
return { core: [BigInt(match[1]), BigInt(match[2]), BigInt(match[3])], prerelease };
|
|
29
|
-
}
|
|
30
|
-
function compareSemver(left, right) {
|
|
31
|
-
const a = parseSemver(left);
|
|
32
|
-
const b = parseSemver(right);
|
|
33
|
-
if (!a || !b) return;
|
|
34
|
-
for (let i = 0; i < a.core.length; i += 1) {
|
|
35
|
-
if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
|
|
36
|
-
}
|
|
37
|
-
if (a.prerelease.length === 0 || b.prerelease.length === 0) {
|
|
38
|
-
return a.prerelease.length === b.prerelease.length ? 0 : a.prerelease.length === 0 ? 1 : -1;
|
|
39
|
-
}
|
|
40
|
-
for (let i = 0; i < Math.max(a.prerelease.length, b.prerelease.length); i += 1) {
|
|
41
|
-
const x = a.prerelease[i];
|
|
42
|
-
const y = b.prerelease[i];
|
|
43
|
-
if (x === void 0 || y === void 0) return x === void 0 ? -1 : 1;
|
|
44
|
-
if (x === y) continue;
|
|
45
|
-
const xNumeric = /^\d+$/u.test(x);
|
|
46
|
-
const yNumeric = /^\d+$/u.test(y);
|
|
47
|
-
if (xNumeric && yNumeric) return BigInt(x) < BigInt(y) ? -1 : 1;
|
|
48
|
-
if (xNumeric !== yNumeric) return xNumeric ? -1 : 1;
|
|
49
|
-
return x < y ? -1 : 1;
|
|
50
|
-
}
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/lib/bin-path.ts
|
|
55
|
-
var PKG_NAME = "@primitive.ai/prim";
|
|
56
|
-
var ROOT_WALK_LIMIT = 6;
|
|
57
|
-
var STABLE_HOOK_LAUNCHER_NAME = "prim-hook-launcher-v1";
|
|
58
|
-
var resolvedRoot;
|
|
59
|
-
function locateRoot() {
|
|
60
|
-
if (resolvedRoot !== void 0) {
|
|
61
|
-
return resolvedRoot;
|
|
62
|
-
}
|
|
63
|
-
let dir = dirname(fileURLToPath(import.meta.url));
|
|
64
|
-
for (let depth = 0; depth < ROOT_WALK_LIMIT; depth++) {
|
|
65
|
-
const manifestPath = join(dir, "package.json");
|
|
66
|
-
if (existsSync(manifestPath)) {
|
|
67
|
-
try {
|
|
68
|
-
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
69
|
-
if (manifest.name === PKG_NAME && manifest.bin) {
|
|
70
|
-
resolvedRoot = { dir, version: manifest.version, bin: manifest.bin };
|
|
71
|
-
return resolvedRoot;
|
|
72
|
-
}
|
|
73
|
-
} catch {
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
const parent = dirname(dir);
|
|
77
|
-
if (parent === dir) {
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
dir = parent;
|
|
81
|
-
}
|
|
82
|
-
resolvedRoot = null;
|
|
83
|
-
return resolvedRoot;
|
|
84
|
-
}
|
|
85
|
-
function binFile(bin) {
|
|
86
|
-
const root = locateRoot();
|
|
87
|
-
const rel = root?.bin[bin];
|
|
88
|
-
if (!root || !rel) {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
return isAbsolute(rel) ? rel : join(root.dir, rel);
|
|
92
|
-
}
|
|
93
|
-
function packageVersion() {
|
|
94
|
-
return locateRoot()?.version ?? null;
|
|
95
|
-
}
|
|
96
|
-
function packageRoot() {
|
|
97
|
-
return locateRoot()?.dir ?? null;
|
|
98
|
-
}
|
|
99
|
-
function shellQuote(value) {
|
|
100
|
-
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
101
|
-
}
|
|
102
|
-
function shellWord(value) {
|
|
103
|
-
return /^[A-Za-z0-9_@%+=:,./-]+$/u.test(value) ? value : shellQuote(value);
|
|
104
|
-
}
|
|
105
|
-
function pinnedNpxArgs(bin, args = [], options = {}) {
|
|
106
|
-
const version = packageVersion();
|
|
107
|
-
if (!version) throw new Error("cannot determine Primitive package version");
|
|
108
|
-
return [
|
|
109
|
-
"--yes",
|
|
110
|
-
"--ignore-scripts",
|
|
111
|
-
...options.preferOnline ? ["--prefer-online"] : [],
|
|
112
|
-
"-p",
|
|
113
|
-
`${PKG_NAME}@${version}`,
|
|
114
|
-
bin,
|
|
115
|
-
...args
|
|
116
|
-
];
|
|
117
|
-
}
|
|
118
|
-
function pinnedNpxCommand(bin, args = [], options = {}) {
|
|
119
|
-
return ["npx", ...pinnedNpxArgs(bin, args, options)].map(shellWord).join(" ");
|
|
120
|
-
}
|
|
121
|
-
function pinnedHookCommand(bin, args = "") {
|
|
122
|
-
const suffix = args ? ` ${args}` : "";
|
|
123
|
-
const fallback = `${pinnedNpxCommand(bin)}${suffix}`;
|
|
124
|
-
const file = binFile(bin);
|
|
125
|
-
if (!file) return fallback;
|
|
126
|
-
return `if [ -x ${shellQuote(process.execPath)} ] && [ -f ${shellQuote(file)} ]; then ${shellQuote(process.execPath)} ${shellQuote(file)}${suffix}; else ${fallback}; fi`;
|
|
127
|
-
}
|
|
128
|
-
var STABLE_HOOK_ARGUMENTS_RE = /^[-A-Za-z0-9_ ]*$/u;
|
|
129
|
-
var STABLE_HOOK_BIN_RE = /^[A-Za-z0-9][A-Za-z0-9-]*$/u;
|
|
130
|
-
var PRIM_CONFIG_SHELL_RESOLVER = [
|
|
131
|
-
'prim_absolute() { case "$1" in [/]* ) ;; *) return 1 ;; esac; case "$1" in [[:space:]]*|*[[:space:]]|*[/][/]*|*[/].[/]*|*[/]..[/]*|*[/].|*[/]..|?*[/]) return 1 ;; esac; }; ',
|
|
132
|
-
'prim_config=${PRIM_CONFIG_DIR:-}; if ! prim_absolute "$prim_config"; then prim_config=${XDG_CONFIG_HOME:-}; if prim_absolute "$prim_config"; then ',
|
|
133
|
-
'case "$prim_config" in /) prim_config=/prim ;; *) prim_config="$prim_config/prim" ;; esac; ',
|
|
134
|
-
'else prim_home=${HOME:-}; prim_absolute "$prim_home" || exit 78; case "$prim_home" in /) prim_config=/.config/prim ;; *) prim_config="$prim_home/.config/prim" ;; esac; ',
|
|
135
|
-
"fi; fi; "
|
|
136
|
-
].join("");
|
|
137
|
-
function stableHookCommand(bin, args = "") {
|
|
138
|
-
if (!STABLE_HOOK_BIN_RE.test(bin) || !STABLE_HOOK_ARGUMENTS_RE.test(args)) {
|
|
139
|
-
throw new Error("invalid stable hook command");
|
|
140
|
-
}
|
|
141
|
-
const resolver = [
|
|
142
|
-
// #242 and earlier identify managed commands by this package marker. Keep
|
|
143
|
-
// it as inert data during the rolling window so an older uninstall or
|
|
144
|
-
// reinstall can remove the stable entry without invoking npm or PATH.
|
|
145
|
-
`prim_legacy_reader='-p ${PKG_NAME}@stable prim-shim.sh ${bin} '; `,
|
|
146
|
-
PRIM_CONFIG_SHELL_RESOLVER,
|
|
147
|
-
`case "$prim_config" in /) prim_launcher=/${STABLE_HOOK_LAUNCHER_NAME} ;; *) prim_launcher="$prim_config/${STABLE_HOOK_LAUNCHER_NAME}" ;; esac; `,
|
|
148
|
-
'exec "$prim_launcher" "$@"'
|
|
149
|
-
].join("");
|
|
150
|
-
const suffix = args ? ` ${args}` : "";
|
|
151
|
-
return `/bin/sh -c ${shellQuote(resolver)} ${STABLE_HOOK_LAUNCHER_NAME} ${bin}${suffix}`;
|
|
152
|
-
}
|
|
153
|
-
function detachedHookShimCommand(bin, args = "") {
|
|
154
|
-
return `payload=$(cat); { trap '' HUP; printf '%s' "$payload" | { ${stableHookCommand(bin, args)}; }; } </dev/null >/dev/null 2>&1 &`;
|
|
155
|
-
}
|
|
156
|
-
function commandMatchesBin(command, bin) {
|
|
157
|
-
if (!command) {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
const c = command.trim();
|
|
161
|
-
if (c === bin || c.startsWith(`${bin} `)) {
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
const exactBin = new RegExp(`(?:^|\\s)${bin.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s|;|$)`);
|
|
165
|
-
return c.includes(`command -v ${bin} `) || c.includes(`-p ${PKG_NAME}@`) && exactBin.test(c) || c.includes(STABLE_HOOK_LAUNCHER_NAME) && exactBin.test(c);
|
|
166
|
-
}
|
|
167
|
-
function exactPinnedNpxVersion(command, bin) {
|
|
168
|
-
const escapedBin = bin.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
169
|
-
const shellQuoted = `'(?:[^']|'"'"')*'`;
|
|
170
|
-
const safeArgs = "(?: [A-Za-z0-9_@%+=:,./-]+)*";
|
|
171
|
-
const direct = new RegExp(
|
|
172
|
-
`^npx --yes --ignore-scripts -p @primitive\\.ai/prim@(?<version>[0-9A-Za-z.+-]+) ${escapedBin}${safeArgs}$`,
|
|
173
|
-
"u"
|
|
174
|
-
).exec(command);
|
|
175
|
-
const wrapped = new RegExp(
|
|
176
|
-
`^if \\[ -x (?<node>${shellQuoted}) \\] && \\[ -f (?<entry>${shellQuoted}) \\]; then \\k<node> \\k<entry>(?<args>${safeArgs}); else npx --yes --ignore-scripts -p @primitive\\.ai/prim@(?<version>[0-9A-Za-z.+-]+) ${escapedBin}\\k<args>; fi$`,
|
|
177
|
-
"u"
|
|
178
|
-
).exec(command);
|
|
179
|
-
const version = direct?.groups?.version ?? wrapped?.groups?.version;
|
|
180
|
-
return version && compareSemver(version, version) !== void 0 ? version : void 0;
|
|
181
|
-
}
|
|
182
|
-
function hookCommandResolution(command, bin) {
|
|
183
|
-
const trimmed = command?.trim();
|
|
184
|
-
if (!trimmed || !commandMatchesBin(trimmed, bin)) return void 0;
|
|
185
|
-
if (trimmed.includes(STABLE_HOOK_LAUNCHER_NAME)) return { kind: "stable_launcher" };
|
|
186
|
-
const version = exactPinnedNpxVersion(trimmed, bin);
|
|
187
|
-
return version ? { kind: "exact_npx_fallback", version } : { kind: "legacy_path" };
|
|
188
|
-
}
|
|
189
|
-
function hookCommandResolutions(commands, bins) {
|
|
190
|
-
const resolutions = [];
|
|
191
|
-
for (const command of commands) {
|
|
192
|
-
for (const bin of bins) {
|
|
193
|
-
const resolution = hookCommandResolution(command, bin);
|
|
194
|
-
if (resolution) resolutions.push(resolution);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return resolutions;
|
|
198
|
-
}
|
|
19
|
+
} from "./chunk-XG7S3LIL.js";
|
|
199
20
|
|
|
200
21
|
// src/commands/claude-install.ts
|
|
201
|
-
import { existsSync as
|
|
22
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
202
23
|
import { homedir } from "os";
|
|
203
|
-
import { join as
|
|
24
|
+
import { join as join2 } from "path";
|
|
204
25
|
|
|
205
26
|
// src/lib/hook-runtime.ts
|
|
206
27
|
import { createHash, randomBytes } from "crypto";
|
|
@@ -209,11 +30,11 @@ import {
|
|
|
209
30
|
accessSync,
|
|
210
31
|
chmodSync,
|
|
211
32
|
copyFileSync,
|
|
212
|
-
existsSync
|
|
33
|
+
existsSync,
|
|
213
34
|
lstatSync,
|
|
214
35
|
mkdirSync,
|
|
215
36
|
mkdtempSync,
|
|
216
|
-
readFileSync
|
|
37
|
+
readFileSync,
|
|
217
38
|
readdirSync,
|
|
218
39
|
realpathSync,
|
|
219
40
|
renameSync,
|
|
@@ -222,7 +43,7 @@ import {
|
|
|
222
43
|
utimesSync,
|
|
223
44
|
writeFileSync
|
|
224
45
|
} from "fs";
|
|
225
|
-
import { delimiter, dirname
|
|
46
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "path";
|
|
226
47
|
|
|
227
48
|
// src/lib/git-hook-contract.ts
|
|
228
49
|
var MANAGED_GIT_HOOK_NAMES = ["pre-commit", "post-commit", "post-rewrite"];
|
|
@@ -402,19 +223,19 @@ function hookRuntimePaths(options = {}) {
|
|
|
402
223
|
env,
|
|
403
224
|
homeDir: options.homeDir ?? env.HOME ?? ""
|
|
404
225
|
});
|
|
405
|
-
const runtimeDir =
|
|
226
|
+
const runtimeDir = join(configDir, "hook-runtime");
|
|
406
227
|
return {
|
|
407
228
|
configDir,
|
|
408
|
-
launcher:
|
|
409
|
-
gitHookEntrypoint:
|
|
229
|
+
launcher: join(configDir, STABLE_HOOK_LAUNCHER_NAME),
|
|
230
|
+
gitHookEntrypoint: join(configDir, GIT_HOOK_ENTRYPOINT_NAME),
|
|
410
231
|
runtimeDir,
|
|
411
|
-
releasesDir:
|
|
412
|
-
current:
|
|
413
|
-
selectionLock:
|
|
232
|
+
releasesDir: join(runtimeDir, "releases"),
|
|
233
|
+
current: join(runtimeDir, "current"),
|
|
234
|
+
selectionLock: join(runtimeDir, "selection.lock")
|
|
414
235
|
};
|
|
415
236
|
}
|
|
416
237
|
function sha256(path) {
|
|
417
|
-
return createHash("sha256").update(
|
|
238
|
+
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
418
239
|
}
|
|
419
240
|
function exactManifest(manifest) {
|
|
420
241
|
return `${JSON.stringify(manifest, null, 2)}
|
|
@@ -444,18 +265,18 @@ function runtimePackageJson(manifest) {
|
|
|
444
265
|
function validateRelease(releaseDir, manifest) {
|
|
445
266
|
try {
|
|
446
267
|
if (!directReleaseLayout(releaseDir)) return false;
|
|
447
|
-
if (
|
|
268
|
+
if (readFileSync(join(releaseDir, "manifest.json"), "utf8") !== exactManifest(manifest)) {
|
|
448
269
|
return false;
|
|
449
270
|
}
|
|
450
|
-
if (
|
|
271
|
+
if (readFileSync(join(releaseDir, "package.json"), "utf8") !== runtimePackageJson(manifest)) {
|
|
451
272
|
return false;
|
|
452
273
|
}
|
|
453
|
-
if (
|
|
274
|
+
if (readFileSync(join(releaseDir, "node"), "utf8") !== `${manifest.nodePath}
|
|
454
275
|
`) {
|
|
455
276
|
return false;
|
|
456
277
|
}
|
|
457
278
|
for (const [bin, relativePath] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
458
|
-
const target =
|
|
279
|
+
const target = join(releaseDir, relativePath);
|
|
459
280
|
if (sha256(target) !== manifest.files[bin]) return false;
|
|
460
281
|
}
|
|
461
282
|
return true;
|
|
@@ -467,28 +288,28 @@ function directReleaseLayout(releaseDir) {
|
|
|
467
288
|
try {
|
|
468
289
|
const directories = /* @__PURE__ */ new Set([releaseDir]);
|
|
469
290
|
for (const relativePath of Object.values(HOOK_RUNTIME_ENTRIES)) {
|
|
470
|
-
for (let directory =
|
|
471
|
-
directories.add(
|
|
291
|
+
for (let directory = dirname(relativePath); directory !== "."; directory = dirname(directory)) {
|
|
292
|
+
directories.add(join(releaseDir, directory));
|
|
472
293
|
}
|
|
473
294
|
}
|
|
474
295
|
if (![...directories].every((directory) => lstatSync(directory).isDirectory())) return false;
|
|
475
296
|
const files = ["manifest.json", "package.json", "node", ...Object.values(HOOK_RUNTIME_ENTRIES)];
|
|
476
|
-
return files.every((relativePath) => lstatSync(
|
|
297
|
+
return files.every((relativePath) => lstatSync(join(releaseDir, relativePath)).isFile());
|
|
477
298
|
} catch {
|
|
478
299
|
return false;
|
|
479
300
|
}
|
|
480
301
|
}
|
|
481
302
|
function readReleaseManifest(releaseDir, releaseName) {
|
|
482
303
|
try {
|
|
483
|
-
const manifestPath =
|
|
304
|
+
const manifestPath = join(releaseDir, "manifest.json");
|
|
484
305
|
const manifestStat = statSync(manifestPath);
|
|
485
306
|
if (!manifestStat.isFile() || manifestStat.size > MAX_MANIFEST_BYTES) return null;
|
|
486
|
-
const raw =
|
|
307
|
+
const raw = readFileSync(manifestPath, "utf8");
|
|
487
308
|
const parsed = JSON.parse(raw);
|
|
488
309
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
489
310
|
if (!exactKeys(parsed, ["schemaVersion", "version", "nodePath", "files"])) return null;
|
|
490
311
|
const candidate = parsed;
|
|
491
|
-
if (candidate.schemaVersion !== HOOK_RUNTIME_SCHEMA_VERSION || typeof candidate.version !== "string" || compareSemver(candidate.version, candidate.version) !== 0 || typeof candidate.nodePath !== "string" || !
|
|
312
|
+
if (candidate.schemaVersion !== HOOK_RUNTIME_SCHEMA_VERSION || typeof candidate.version !== "string" || compareSemver(candidate.version, candidate.version) !== 0 || typeof candidate.nodePath !== "string" || !isAbsolute(candidate.nodePath) || /[\r\n]/u.test(candidate.nodePath) || !candidate.files || typeof candidate.files !== "object" || Array.isArray(candidate.files) || !exactKeys(candidate.files, Object.keys(HOOK_RUNTIME_ENTRIES)) || Object.values(candidate.files).some(
|
|
492
313
|
(digest) => typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest)
|
|
493
314
|
)) {
|
|
494
315
|
return null;
|
|
@@ -505,7 +326,7 @@ function readReleaseManifest(releaseDir, releaseName) {
|
|
|
505
326
|
function readSelectedRelease(paths) {
|
|
506
327
|
let raw;
|
|
507
328
|
try {
|
|
508
|
-
raw =
|
|
329
|
+
raw = readFileSync(paths.current, "utf8");
|
|
509
330
|
} catch (error) {
|
|
510
331
|
if (error.code === "ENOENT") return null;
|
|
511
332
|
throw error;
|
|
@@ -515,7 +336,7 @@ function readSelectedRelease(paths) {
|
|
|
515
336
|
` !== raw || !RELEASE_NAME_RE.test(name)) {
|
|
516
337
|
throw new Error("cannot stage hook runtime: selected release is malformed");
|
|
517
338
|
}
|
|
518
|
-
const dir =
|
|
339
|
+
const dir = join(paths.releasesDir, name);
|
|
519
340
|
const manifest = readReleaseManifest(dir, name);
|
|
520
341
|
if (!manifest) {
|
|
521
342
|
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
@@ -525,7 +346,7 @@ function readSelectedRelease(paths) {
|
|
|
525
346
|
function releaseEntries(paths) {
|
|
526
347
|
return readdirSync(paths.releasesDir, { withFileTypes: true }).flatMap((entry) => {
|
|
527
348
|
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name)) return [];
|
|
528
|
-
const dir =
|
|
349
|
+
const dir = join(paths.releasesDir, entry.name);
|
|
529
350
|
try {
|
|
530
351
|
return [{ name: entry.name, dir, mtimeMs: statSync(dir).mtimeMs }];
|
|
531
352
|
} catch {
|
|
@@ -564,8 +385,8 @@ function pruneReleases(paths, protectedNames, reserveSlots, now) {
|
|
|
564
385
|
function sourceManifest(sourceDir, version, nodePath) {
|
|
565
386
|
const files = {};
|
|
566
387
|
for (const [bin, relativePath] of Object.entries(SOURCE_ENTRIES)) {
|
|
567
|
-
const source =
|
|
568
|
-
if (!
|
|
388
|
+
const source = join(sourceDir, relativePath);
|
|
389
|
+
if (!existsSync(source) || !statSync(source).isFile()) {
|
|
569
390
|
throw new Error(`cannot stage hook runtime: missing self-contained ${bin} bundle`);
|
|
570
391
|
}
|
|
571
392
|
files[bin] = sha256(source);
|
|
@@ -608,15 +429,15 @@ function privateFile(path, mode) {
|
|
|
608
429
|
function hasSafeRuntimeLayout(paths, selected) {
|
|
609
430
|
const releaseDirectories = /* @__PURE__ */ new Set([selected.dir]);
|
|
610
431
|
for (const relativePath of Object.values(HOOK_RUNTIME_ENTRIES)) {
|
|
611
|
-
for (let directory =
|
|
612
|
-
releaseDirectories.add(
|
|
432
|
+
for (let directory = dirname(relativePath); directory !== "."; directory = dirname(directory)) {
|
|
433
|
+
releaseDirectories.add(join(selected.dir, directory));
|
|
613
434
|
}
|
|
614
435
|
}
|
|
615
|
-
if (!privateDirectory(paths.configDir) || !privateDirectory(paths.runtimeDir) || !privateDirectory(paths.releasesDir) || ![...releaseDirectories].every(privateDirectory) || !privateFile(paths.launcher, LAUNCHER_MODE) || !privateFile(paths.current, DATA_FILE_MODE) || !privateFile(
|
|
436
|
+
if (!privateDirectory(paths.configDir) || !privateDirectory(paths.runtimeDir) || !privateDirectory(paths.releasesDir) || ![...releaseDirectories].every(privateDirectory) || !privateFile(paths.launcher, LAUNCHER_MODE) || !privateFile(paths.current, DATA_FILE_MODE) || !privateFile(join(selected.dir, "manifest.json"), DATA_FILE_MODE) || !privateFile(join(selected.dir, "package.json"), DATA_FILE_MODE) || !privateFile(join(selected.dir, "node"), DATA_FILE_MODE)) {
|
|
616
437
|
return false;
|
|
617
438
|
}
|
|
618
439
|
return Object.values(HOOK_RUNTIME_ENTRIES).every(
|
|
619
|
-
(relativePath) => privateFile(
|
|
440
|
+
(relativePath) => privateFile(join(selected.dir, relativePath), DATA_FILE_MODE)
|
|
620
441
|
);
|
|
621
442
|
}
|
|
622
443
|
function recordedNodeMatchesCurrentProcess(nodePath) {
|
|
@@ -640,7 +461,7 @@ function inspectHookRuntime(options = {}) {
|
|
|
640
461
|
if (!(launcher || selector)) return { state: "missing" };
|
|
641
462
|
if (!(launcher && selector)) return { state: "invalid" };
|
|
642
463
|
try {
|
|
643
|
-
if (
|
|
464
|
+
if (readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
644
465
|
return { state: "invalid" };
|
|
645
466
|
}
|
|
646
467
|
const selected = readSelectedRelease(paths);
|
|
@@ -653,22 +474,22 @@ function inspectHookRuntime(options = {}) {
|
|
|
653
474
|
}
|
|
654
475
|
}
|
|
655
476
|
function writeRelease(sourceDir, releaseDir, manifest, releasesDir) {
|
|
656
|
-
const stagingDir = mkdtempSync(
|
|
477
|
+
const stagingDir = mkdtempSync(join(releasesDir, ".stage-"));
|
|
657
478
|
chmodSync(stagingDir, DIRECTORY_MODE);
|
|
658
479
|
try {
|
|
659
480
|
const directories = /* @__PURE__ */ new Set([stagingDir]);
|
|
660
481
|
const syncStagedFile = (path) => {
|
|
661
482
|
syncFile(path);
|
|
662
|
-
for (let directory =
|
|
483
|
+
for (let directory = dirname(path); ; directory = dirname(directory)) {
|
|
663
484
|
directories.add(directory);
|
|
664
485
|
if (directory === stagingDir) break;
|
|
665
486
|
}
|
|
666
487
|
};
|
|
667
488
|
for (const [bin, targetRelative] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
668
|
-
const source =
|
|
669
|
-
const target =
|
|
670
|
-
mkdirSync(
|
|
671
|
-
chmodSync(
|
|
489
|
+
const source = join(sourceDir, SOURCE_ENTRIES[bin]);
|
|
490
|
+
const target = join(stagingDir, targetRelative);
|
|
491
|
+
mkdirSync(dirname(target), { recursive: true, mode: DIRECTORY_MODE });
|
|
492
|
+
chmodSync(dirname(target), DIRECTORY_MODE);
|
|
672
493
|
copyFileSync(source, target);
|
|
673
494
|
chmodSync(target, DATA_FILE_MODE);
|
|
674
495
|
if (sha256(target) !== manifest.files[bin]) {
|
|
@@ -676,7 +497,7 @@ function writeRelease(sourceDir, releaseDir, manifest, releasesDir) {
|
|
|
676
497
|
}
|
|
677
498
|
syncStagedFile(target);
|
|
678
499
|
}
|
|
679
|
-
const nodePath =
|
|
500
|
+
const nodePath = join(stagingDir, "node");
|
|
680
501
|
writeFileSync(nodePath, `${manifest.nodePath}
|
|
681
502
|
`, {
|
|
682
503
|
encoding: "utf8",
|
|
@@ -684,14 +505,14 @@ function writeRelease(sourceDir, releaseDir, manifest, releasesDir) {
|
|
|
684
505
|
mode: DATA_FILE_MODE
|
|
685
506
|
});
|
|
686
507
|
syncStagedFile(nodePath);
|
|
687
|
-
const packageJsonPath =
|
|
508
|
+
const packageJsonPath = join(stagingDir, "package.json");
|
|
688
509
|
writeFileSync(packageJsonPath, runtimePackageJson(manifest), {
|
|
689
510
|
encoding: "utf8",
|
|
690
511
|
flag: "wx",
|
|
691
512
|
mode: DATA_FILE_MODE
|
|
692
513
|
});
|
|
693
514
|
syncStagedFile(packageJsonPath);
|
|
694
|
-
const manifestPath =
|
|
515
|
+
const manifestPath = join(stagingDir, "manifest.json");
|
|
695
516
|
writeFileSync(manifestPath, exactManifest(manifest), {
|
|
696
517
|
encoding: "utf8",
|
|
697
518
|
flag: "wx",
|
|
@@ -721,8 +542,8 @@ function stableNodePath(execPath = process.execPath, searchPath = process.env.PA
|
|
|
721
542
|
return execPath;
|
|
722
543
|
}
|
|
723
544
|
for (const directory of searchPath.split(delimiter)) {
|
|
724
|
-
if (!
|
|
725
|
-
const candidate =
|
|
545
|
+
if (!isAbsolute(directory)) continue;
|
|
546
|
+
const candidate = join(directory, "node");
|
|
726
547
|
try {
|
|
727
548
|
if (realpathSync(candidate) === target) return candidate;
|
|
728
549
|
} catch {
|
|
@@ -732,16 +553,16 @@ function stableNodePath(execPath = process.execPath, searchPath = process.env.PA
|
|
|
732
553
|
}
|
|
733
554
|
function stageHookRuntime(options = {}) {
|
|
734
555
|
const root = packageRoot();
|
|
735
|
-
const sourceDir = options.sourceDir ?? (root ?
|
|
556
|
+
const sourceDir = options.sourceDir ?? (root ? join(root, "dist", "hook-runtime") : null);
|
|
736
557
|
const version = options.version ?? packageVersion();
|
|
737
558
|
const nodePath = resolve(options.nodePath ?? stableNodePath());
|
|
738
|
-
if (!sourceDir || !version || compareSemver(version, version) !== 0 || !
|
|
559
|
+
if (!sourceDir || !version || compareSemver(version, version) !== 0 || !isAbsolute(nodePath) || /[\r\n]/u.test(nodePath)) {
|
|
739
560
|
throw new Error("cannot stage hook runtime: package identity is unavailable");
|
|
740
561
|
}
|
|
741
562
|
const manifest = sourceManifest(sourceDir, version, nodePath);
|
|
742
563
|
const paths = hookRuntimePaths(options);
|
|
743
564
|
const releaseName = `${RELEASE_PREFIX}${releaseId(manifest)}`;
|
|
744
|
-
const releaseDir =
|
|
565
|
+
const releaseDir = join(paths.releasesDir, releaseName);
|
|
745
566
|
mkdirSync(paths.releasesDir, { recursive: true, mode: DIRECTORY_MODE });
|
|
746
567
|
chmodSync(paths.configDir, DIRECTORY_MODE);
|
|
747
568
|
chmodSync(paths.runtimeDir, DIRECTORY_MODE);
|
|
@@ -755,7 +576,7 @@ function stageHookRuntime(options = {}) {
|
|
|
755
576
|
throw new Error("cannot stage hook runtime: selected release version is malformed");
|
|
756
577
|
}
|
|
757
578
|
const selectRequested = !selected || precedence < 0;
|
|
758
|
-
const requestedExists =
|
|
579
|
+
const requestedExists = existsSync(releaseDir);
|
|
759
580
|
const protectedBefore = new Set(selected ? [selected.name] : []);
|
|
760
581
|
if (selectRequested && requestedExists) protectedBefore.add(releaseName);
|
|
761
582
|
pruneReleases(paths, protectedBefore, selectRequested && !requestedExists ? 1 : 0, now);
|
|
@@ -768,7 +589,7 @@ function stageHookRuntime(options = {}) {
|
|
|
768
589
|
const finalRelease = selectRequested ? { name: releaseName, dir: releaseDir, manifest } : selected;
|
|
769
590
|
const launcherCurrent = (() => {
|
|
770
591
|
try {
|
|
771
|
-
return
|
|
592
|
+
return readFileSync(paths.launcher, "utf8") === STABLE_HOOK_LAUNCHER_CONTENT;
|
|
772
593
|
} catch {
|
|
773
594
|
return false;
|
|
774
595
|
}
|
|
@@ -812,7 +633,7 @@ function stageHookRuntime(options = {}) {
|
|
|
812
633
|
}
|
|
813
634
|
function gitHookEntrypointCurrent(paths) {
|
|
814
635
|
try {
|
|
815
|
-
return
|
|
636
|
+
return readFileSync(paths.gitHookEntrypoint, "utf8") === GIT_HOOK_ENTRYPOINT_CONTENT;
|
|
816
637
|
} catch {
|
|
817
638
|
return false;
|
|
818
639
|
}
|
|
@@ -827,7 +648,7 @@ function inspectGitHookEntrypoint(options = {}) {
|
|
|
827
648
|
try {
|
|
828
649
|
const entrypoint = lstatIfPresent(paths.gitHookEntrypoint);
|
|
829
650
|
if (!entrypoint) return "missing";
|
|
830
|
-
if (!entrypoint.isFile() || (Number(entrypoint.mode) & 64) === 0 || !gitHookEntrypointCurrent(paths) ||
|
|
651
|
+
if (!entrypoint.isFile() || (Number(entrypoint.mode) & 64) === 0 || !gitHookEntrypointCurrent(paths) || readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT || !lstatIfPresent(paths.current)?.isFile()) {
|
|
831
652
|
return "invalid";
|
|
832
653
|
}
|
|
833
654
|
return "ready";
|
|
@@ -853,7 +674,7 @@ function assertOwnedHookRuntime(paths) {
|
|
|
853
674
|
}
|
|
854
675
|
const launcher = lstatIfPresent(paths.launcher);
|
|
855
676
|
if (launcher) {
|
|
856
|
-
if (!launcher.isFile() ||
|
|
677
|
+
if (!launcher.isFile() || readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
857
678
|
throw new Error(`refusing to remove unrecognized hook launcher at ${paths.launcher}`);
|
|
858
679
|
}
|
|
859
680
|
}
|
|
@@ -881,7 +702,7 @@ function assertOwnedHookRuntime(paths) {
|
|
|
881
702
|
throw new Error(`refusing to remove non-directory hook releases at ${paths.releasesDir}`);
|
|
882
703
|
}
|
|
883
704
|
for (const entry of readdirSync(paths.releasesDir, { withFileTypes: true })) {
|
|
884
|
-
const releaseDir =
|
|
705
|
+
const releaseDir = join(paths.releasesDir, entry.name);
|
|
885
706
|
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name) || !readReleaseManifest(releaseDir, entry.name)) {
|
|
886
707
|
throw new Error(`refusing to remove unrecognized hook release ${releaseDir}`);
|
|
887
708
|
}
|
|
@@ -928,11 +749,11 @@ var PRIM_BINS = [
|
|
|
928
749
|
SESSION_END_BIN
|
|
929
750
|
];
|
|
930
751
|
var JSON_INDENT = 2;
|
|
931
|
-
var USER_SCOPE_PATH =
|
|
752
|
+
var USER_SCOPE_PATH = join2(homedir(), ".claude", "settings.json");
|
|
932
753
|
function projectRoot() {
|
|
933
754
|
return gitToplevel() ?? process.cwd();
|
|
934
755
|
}
|
|
935
|
-
var projectScopePath = () =>
|
|
756
|
+
var projectScopePath = () => join2(projectRoot(), ".claude", "settings.json");
|
|
936
757
|
var CAPTURE_EVENTS = [
|
|
937
758
|
"SessionStart",
|
|
938
759
|
"UserPromptSubmit",
|
|
@@ -983,10 +804,10 @@ function settingsPathFor(scope) {
|
|
|
983
804
|
return scope === "user" ? USER_SCOPE_PATH : projectScopePath();
|
|
984
805
|
}
|
|
985
806
|
function readSettings(path) {
|
|
986
|
-
if (!
|
|
807
|
+
if (!existsSync2(path)) {
|
|
987
808
|
return {};
|
|
988
809
|
}
|
|
989
|
-
const raw =
|
|
810
|
+
const raw = readFileSync2(path, "utf-8");
|
|
990
811
|
try {
|
|
991
812
|
return JSON.parse(raw);
|
|
992
813
|
} catch (err) {
|
|
@@ -1354,9 +1175,9 @@ ${line("project", result.project)}`);
|
|
|
1354
1175
|
}
|
|
1355
1176
|
|
|
1356
1177
|
// src/commands/cursor-install.ts
|
|
1357
|
-
import { existsSync as
|
|
1178
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
1358
1179
|
import { homedir as homedir2 } from "os";
|
|
1359
|
-
import { isAbsolute as
|
|
1180
|
+
import { isAbsolute as isAbsolute2, join as join3 } from "path";
|
|
1360
1181
|
var CAPTURE_BIN2 = "prim-hook";
|
|
1361
1182
|
var GATE_BIN2 = "prim-pre-tool-use";
|
|
1362
1183
|
var POST_TOOL_BIN = "prim-post-tool-use";
|
|
@@ -1385,25 +1206,25 @@ var REGISTRATIONS2 = [
|
|
|
1385
1206
|
];
|
|
1386
1207
|
function cursorConfigDirectory(env = process.env, home = homedir2(), platform = process.platform) {
|
|
1387
1208
|
const explicit = env.CURSOR_CONFIG_DIR?.trim();
|
|
1388
|
-
if (explicit &&
|
|
1209
|
+
if (explicit && isAbsolute2(explicit)) return explicit;
|
|
1389
1210
|
const xdg = env.XDG_CONFIG_HOME?.trim();
|
|
1390
|
-
if (platform !== "darwin" && xdg &&
|
|
1391
|
-
return
|
|
1211
|
+
if (platform !== "darwin" && xdg && isAbsolute2(xdg)) return join3(xdg, "cursor");
|
|
1212
|
+
return join3(home, ".cursor");
|
|
1392
1213
|
}
|
|
1393
1214
|
function cursorHooksPath(scope, root = projectRoot()) {
|
|
1394
|
-
return scope === "user" ?
|
|
1215
|
+
return scope === "user" ? join3(cursorConfigDirectory(), "hooks.json") : join3(root, ".cursor", "hooks.json");
|
|
1395
1216
|
}
|
|
1396
1217
|
function cursorCliConfigPath() {
|
|
1397
|
-
return
|
|
1218
|
+
return join3(cursorConfigDirectory(), "cli-config.json");
|
|
1398
1219
|
}
|
|
1399
1220
|
function plainObject(value) {
|
|
1400
1221
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1401
1222
|
}
|
|
1402
1223
|
function readCursorHooks(path) {
|
|
1403
|
-
if (!
|
|
1224
|
+
if (!existsSync3(path)) return {};
|
|
1404
1225
|
let parsed;
|
|
1405
1226
|
try {
|
|
1406
|
-
parsed = JSON.parse(
|
|
1227
|
+
parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
1407
1228
|
} catch {
|
|
1408
1229
|
throw new Error(`invalid Cursor hooks JSON at ${path}`);
|
|
1409
1230
|
}
|
|
@@ -1422,10 +1243,10 @@ function readCursorHooks(path) {
|
|
|
1422
1243
|
return parsed;
|
|
1423
1244
|
}
|
|
1424
1245
|
function readObject(path) {
|
|
1425
|
-
if (!
|
|
1246
|
+
if (!existsSync3(path)) return {};
|
|
1426
1247
|
let parsed;
|
|
1427
1248
|
try {
|
|
1428
|
-
parsed = JSON.parse(
|
|
1249
|
+
parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
1429
1250
|
} catch {
|
|
1430
1251
|
throw new Error(`invalid Cursor CLI config JSON at ${path}`);
|
|
1431
1252
|
}
|
|
@@ -1642,16 +1463,6 @@ function registerCursorCommands(program) {
|
|
|
1642
1463
|
}
|
|
1643
1464
|
|
|
1644
1465
|
export {
|
|
1645
|
-
compareSemver,
|
|
1646
|
-
binFile,
|
|
1647
|
-
packageVersion,
|
|
1648
|
-
packageRoot,
|
|
1649
|
-
pinnedNpxArgs,
|
|
1650
|
-
pinnedNpxCommand,
|
|
1651
|
-
pinnedHookCommand,
|
|
1652
|
-
stableHookCommand,
|
|
1653
|
-
commandMatchesBin,
|
|
1654
|
-
hookCommandResolutions,
|
|
1655
1466
|
MANAGED_GIT_HOOK_NAMES,
|
|
1656
1467
|
GIT_HOOK_CONTRACT_VERSION,
|
|
1657
1468
|
blockMarkers,
|