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