@primitive.ai/prim 0.1.0-alpha.11 → 0.1.0-alpha.110
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 +450 -38
- package/SKILL.md +366 -0
- package/dist/chunk-2IRLFOEL.js +766 -0
- package/dist/chunk-2Q3OGSTV.js +164 -0
- package/dist/chunk-43ZKL27W.js +258 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5BLGBSDM.js +233 -0
- package/dist/chunk-5LRKJTLA.js +155 -0
- package/dist/chunk-7VIMU2HR.js +209 -0
- package/dist/chunk-AOJ254C2.js +511 -0
- package/dist/chunk-CTRAJCPN.js +964 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GA2RXPJL.js +168 -0
- package/dist/chunk-HTRFPXMD.js +135 -0
- package/dist/chunk-IQZA65NV.js +1871 -0
- package/dist/chunk-JJI7FU24.js +439 -0
- package/dist/chunk-KGBMU76T.js +175 -0
- package/dist/chunk-KQ4WBK5R.js +262 -0
- package/dist/chunk-NH3VC7YO.js +264 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-PO5QAYD7.js +1499 -0
- package/dist/chunk-UDBBJWQH.js +20383 -0
- package/dist/chunk-W4ZEPZLK.js +36 -0
- package/dist/chunk-XG7S3LIL.js +1372 -0
- package/dist/daemon/server.js +24051 -0
- package/dist/hook-runtime/daemon/server.js +24051 -0
- package/dist/hook-runtime/hooks/post-commit.js +21524 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +21423 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +28917 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1657 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +28425 -0
- package/dist/hook-runtime/hooks/prim-hook.js +28977 -0
- package/dist/hook-runtime/hooks/session-end.js +1176 -0
- package/dist/hook-runtime/hooks/session-start.js +31553 -0
- package/dist/hook-runtime/index.js +59630 -0
- package/dist/hook-runtime/statusline-main.js +886 -0
- package/dist/hooks/post-commit.js +196 -0
- package/dist/hooks/post-rewrite.js +205 -0
- package/dist/hooks/post-tool-use.js +292 -0
- package/dist/hooks/pre-commit.js +15 -108
- package/dist/hooks/pre-tool-use.js +470 -0
- package/dist/hooks/prim-hook.js +242 -0
- package/dist/hooks/session-end.js +64 -0
- package/dist/hooks/session-start.js +309 -0
- package/dist/index.js +10651 -427
- package/dist/statusline-main.js +886 -0
- package/package.json +41 -16
- package/dist/chunk-3APLWTLB.js +0 -165
|
@@ -0,0 +1,1499 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PRIM_CONFIG_SHELL_RESOLVER,
|
|
3
|
+
STABLE_HOOK_LAUNCHER_NAME,
|
|
4
|
+
atomicWriteFile,
|
|
5
|
+
commandMatchesBin,
|
|
6
|
+
compareSemver,
|
|
7
|
+
detachedHookShimCommand,
|
|
8
|
+
gitToplevel,
|
|
9
|
+
hookCommandResolution,
|
|
10
|
+
hookCommandResolutions,
|
|
11
|
+
packageRoot,
|
|
12
|
+
packageVersion,
|
|
13
|
+
primConfigDirectory,
|
|
14
|
+
shellQuote,
|
|
15
|
+
stableHookCommand,
|
|
16
|
+
syncDirectory,
|
|
17
|
+
syncFile,
|
|
18
|
+
withFileLockSync
|
|
19
|
+
} from "./chunk-XG7S3LIL.js";
|
|
20
|
+
|
|
21
|
+
// src/commands/claude-install.ts
|
|
22
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
23
|
+
import { homedir } from "os";
|
|
24
|
+
import { join as join2 } from "path";
|
|
25
|
+
|
|
26
|
+
// src/lib/hook-runtime.ts
|
|
27
|
+
import { createHash, randomBytes } from "crypto";
|
|
28
|
+
import {
|
|
29
|
+
constants,
|
|
30
|
+
accessSync,
|
|
31
|
+
chmodSync,
|
|
32
|
+
copyFileSync,
|
|
33
|
+
existsSync,
|
|
34
|
+
lstatSync,
|
|
35
|
+
mkdirSync,
|
|
36
|
+
mkdtempSync,
|
|
37
|
+
readFileSync,
|
|
38
|
+
readdirSync,
|
|
39
|
+
realpathSync,
|
|
40
|
+
renameSync,
|
|
41
|
+
rmSync,
|
|
42
|
+
statSync,
|
|
43
|
+
utimesSync,
|
|
44
|
+
writeFileSync
|
|
45
|
+
} from "fs";
|
|
46
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "path";
|
|
47
|
+
|
|
48
|
+
// src/lib/git-hook-contract.ts
|
|
49
|
+
var MANAGED_GIT_HOOK_NAMES = ["pre-commit", "post-commit", "post-rewrite"];
|
|
50
|
+
var GIT_HOOK_ENTRYPOINT_NAME = "prim-git-hook-v1";
|
|
51
|
+
var GIT_HOOK_CONTRACT_VERSION = 1;
|
|
52
|
+
var CONTRACT_LINE_PREFIX = "# prim git hook v";
|
|
53
|
+
var CONTRACT_LINE_RE = /^# prim git hook v([0-9]+)\b/mu;
|
|
54
|
+
function blockMarkers(hook) {
|
|
55
|
+
return { start: `# >>> prim ${hook} hook >>>`, end: `# <<< prim ${hook} hook <<<` };
|
|
56
|
+
}
|
|
57
|
+
function entrypointCommand(hook) {
|
|
58
|
+
const script = [
|
|
59
|
+
PRIM_CONFIG_SHELL_RESOLVER,
|
|
60
|
+
`case "$prim_config" in /) prim_entry=/${GIT_HOOK_ENTRYPOINT_NAME} ;; `,
|
|
61
|
+
`*) prim_entry="$prim_config/${GIT_HOOK_ENTRYPOINT_NAME}" ;; esac; `,
|
|
62
|
+
'[ -x "$prim_entry" ] || exit 0; exec "$prim_entry" "$@"'
|
|
63
|
+
].join("");
|
|
64
|
+
return `/bin/sh -c ${shellQuote(script)} ${GIT_HOOK_ENTRYPOINT_NAME} ${hook} "$@"`;
|
|
65
|
+
}
|
|
66
|
+
function managedHookBlock(hook) {
|
|
67
|
+
const { start, end } = blockMarkers(hook);
|
|
68
|
+
const header = `${CONTRACT_LINE_PREFIX}${GIT_HOOK_CONTRACT_VERSION}: a no-op unless prim is installed and enabled here (see \`prim hooks snippet ${hook}\`).`;
|
|
69
|
+
if (hook !== "post-rewrite") {
|
|
70
|
+
return `${start}
|
|
71
|
+
${header}
|
|
72
|
+
# shellcheck disable=SC2016
|
|
73
|
+
${entrypointCommand(hook)} || :
|
|
74
|
+
${end}`;
|
|
75
|
+
}
|
|
76
|
+
return `${start}
|
|
77
|
+
${header}
|
|
78
|
+
prim_rewrite_stdin=$(mktemp 2>/dev/null) || prim_rewrite_stdin=
|
|
79
|
+
if test -n "\${prim_rewrite_stdin}" && cat >"\${prim_rewrite_stdin}"; then
|
|
80
|
+
# shellcheck disable=SC2016
|
|
81
|
+
${entrypointCommand(hook)} <"\${prim_rewrite_stdin}" || :
|
|
82
|
+
exec <"\${prim_rewrite_stdin}"
|
|
83
|
+
fi
|
|
84
|
+
if test -n "\${prim_rewrite_stdin}"; then rm -f "\${prim_rewrite_stdin}"; fi
|
|
85
|
+
unset prim_rewrite_stdin
|
|
86
|
+
${end}`;
|
|
87
|
+
}
|
|
88
|
+
var OPEN_WORDS = /* @__PURE__ */ new Set(["then", "do", "else", "{", "(", "&&", "||", "|"]);
|
|
89
|
+
function canonicalHookBlock(text) {
|
|
90
|
+
const tokens = [];
|
|
91
|
+
let word = "";
|
|
92
|
+
let quote;
|
|
93
|
+
const separate = () => {
|
|
94
|
+
const last = tokens.at(-1);
|
|
95
|
+
if (last !== void 0 && last !== ";" && !OPEN_WORDS.has(last)) tokens.push(";");
|
|
96
|
+
};
|
|
97
|
+
const flush = () => {
|
|
98
|
+
if (word === "") return;
|
|
99
|
+
const last = tokens.at(-1);
|
|
100
|
+
if (last !== void 0 && /^[0-9]*(?:<|>|>>|<&|>&)$/u.test(last))
|
|
101
|
+
tokens[tokens.length - 1] += word;
|
|
102
|
+
else tokens.push(word);
|
|
103
|
+
word = "";
|
|
104
|
+
};
|
|
105
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
106
|
+
const char = text[index];
|
|
107
|
+
if (quote) {
|
|
108
|
+
word += char;
|
|
109
|
+
if (char === quote) quote = void 0;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (char === "'" || char === '"') {
|
|
113
|
+
quote = char;
|
|
114
|
+
word += char;
|
|
115
|
+
} else if (char === "#" && word === "") {
|
|
116
|
+
const end = text.indexOf("\n", index);
|
|
117
|
+
const comment = text.slice(index, end === -1 ? text.length : end);
|
|
118
|
+
tokens.push(comment.replace(/\s+/gu, " ").trim());
|
|
119
|
+
index = (end === -1 ? text.length : end) - 1;
|
|
120
|
+
} else if (char === "\n" || char === ";") {
|
|
121
|
+
flush();
|
|
122
|
+
if (char === ";" && text[index + 1] === ";") {
|
|
123
|
+
tokens.push(";;");
|
|
124
|
+
index += 1;
|
|
125
|
+
} else {
|
|
126
|
+
separate();
|
|
127
|
+
}
|
|
128
|
+
} else if (/\s/u.test(char)) {
|
|
129
|
+
flush();
|
|
130
|
+
} else {
|
|
131
|
+
word += char;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
flush();
|
|
135
|
+
while (tokens.at(-1) === ";") tokens.pop();
|
|
136
|
+
return tokens.join("\0");
|
|
137
|
+
}
|
|
138
|
+
function hookBlockContractVersion(block) {
|
|
139
|
+
const match = CONTRACT_LINE_RE.exec(block);
|
|
140
|
+
return match?.[1] ? Number(match[1]) : void 0;
|
|
141
|
+
}
|
|
142
|
+
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
143
|
+
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
144
|
+
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
145
|
+
# where prim is active (git config prim.active true) and never fails the hook.
|
|
146
|
+
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
147
|
+
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
148
|
+
[ -x "$prim_launcher" ] || exit 0
|
|
149
|
+
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
150
|
+
prim_hook=\${1-}
|
|
151
|
+
if [ "$#" -gt 0 ]; then shift; fi
|
|
152
|
+
case "$prim_hook" in
|
|
153
|
+
pre-commit)
|
|
154
|
+
"$prim_launcher" prim-pre-commit "$@" || :
|
|
155
|
+
;;
|
|
156
|
+
post-commit)
|
|
157
|
+
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
158
|
+
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
159
|
+
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
160
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
161
|
+
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
162
|
+
(
|
|
163
|
+
trap '' HUP
|
|
164
|
+
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
165
|
+
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
166
|
+
) </dev/null >/dev/null 2>&1 &
|
|
167
|
+
;;
|
|
168
|
+
post-rewrite)
|
|
169
|
+
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
170
|
+
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
171
|
+
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
172
|
+
rm -f "$prim_pairs"
|
|
173
|
+
exit 0
|
|
174
|
+
fi
|
|
175
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
176
|
+
(
|
|
177
|
+
trap '' HUP
|
|
178
|
+
trap 'rm -f "$prim_pairs"' 0
|
|
179
|
+
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
180
|
+
) </dev/null >/dev/null 2>&1 &
|
|
181
|
+
;;
|
|
182
|
+
esac
|
|
183
|
+
exit 0
|
|
184
|
+
`;
|
|
185
|
+
|
|
186
|
+
// src/lib/hook-runtime.ts
|
|
187
|
+
var HOOK_RUNTIME_SCHEMA_VERSION = 1;
|
|
188
|
+
var DIRECTORY_MODE = 448;
|
|
189
|
+
var DATA_FILE_MODE = 384;
|
|
190
|
+
var LAUNCHER_MODE = 448;
|
|
191
|
+
var RELEASE_PREFIX = "release-";
|
|
192
|
+
var RELEASE_NAME_RE = /^release-[0-9a-f]{64}$/u;
|
|
193
|
+
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
194
|
+
var MAX_RELEASES = 8;
|
|
195
|
+
var MAX_RELEASE_DELETIONS_PER_STAGE = 8;
|
|
196
|
+
var RETAIN_PREDECESSORS = 2;
|
|
197
|
+
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
198
|
+
var HOOK_RUNTIME_ENTRIES = {
|
|
199
|
+
// SessionStart self-healing resolves this exact CLI entry to run
|
|
200
|
+
// `daemon ensure`; it must travel with the immutable hook release.
|
|
201
|
+
prim: "dist/index.js",
|
|
202
|
+
"prim-hook": "dist/hooks/prim-hook.js",
|
|
203
|
+
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
204
|
+
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
205
|
+
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
206
|
+
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
207
|
+
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
208
|
+
"prim-session-start": "dist/hooks/session-start.js",
|
|
209
|
+
"prim-session-end": "dist/hooks/session-end.js",
|
|
210
|
+
"prim-daemon-server": "dist/daemon/server.js",
|
|
211
|
+
"prim-statusline": "dist/statusline-main.js"
|
|
212
|
+
};
|
|
213
|
+
var SOURCE_ENTRIES = Object.fromEntries(
|
|
214
|
+
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
215
|
+
bin,
|
|
216
|
+
target.replace(/^dist\//u, "")
|
|
217
|
+
])
|
|
218
|
+
);
|
|
219
|
+
function hookRuntimePaths(options = {}) {
|
|
220
|
+
const env = options.env ?? process.env;
|
|
221
|
+
const configDir = primConfigDirectory({
|
|
222
|
+
...options,
|
|
223
|
+
env,
|
|
224
|
+
homeDir: options.homeDir ?? env.HOME ?? ""
|
|
225
|
+
});
|
|
226
|
+
const runtimeDir = join(configDir, "hook-runtime");
|
|
227
|
+
return {
|
|
228
|
+
configDir,
|
|
229
|
+
launcher: join(configDir, STABLE_HOOK_LAUNCHER_NAME),
|
|
230
|
+
gitHookEntrypoint: join(configDir, GIT_HOOK_ENTRYPOINT_NAME),
|
|
231
|
+
runtimeDir,
|
|
232
|
+
releasesDir: join(runtimeDir, "releases"),
|
|
233
|
+
current: join(runtimeDir, "current"),
|
|
234
|
+
selectionLock: join(runtimeDir, "selection.lock")
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function sha256(path) {
|
|
238
|
+
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
239
|
+
}
|
|
240
|
+
function exactManifest(manifest) {
|
|
241
|
+
return `${JSON.stringify(manifest, null, 2)}
|
|
242
|
+
`;
|
|
243
|
+
}
|
|
244
|
+
function exactKeys(value, expected) {
|
|
245
|
+
const actual = Object.keys(value).sort();
|
|
246
|
+
const wanted = [...expected].sort();
|
|
247
|
+
return actual.length === wanted.length && actual.every((key, index) => key === wanted[index]);
|
|
248
|
+
}
|
|
249
|
+
function releaseId(manifest) {
|
|
250
|
+
return createHash("sha256").update(exactManifest(manifest)).digest("hex");
|
|
251
|
+
}
|
|
252
|
+
function runtimePackageJson(manifest) {
|
|
253
|
+
return `${JSON.stringify(
|
|
254
|
+
{
|
|
255
|
+
name: "@primitive.ai/prim",
|
|
256
|
+
version: manifest.version,
|
|
257
|
+
type: "module",
|
|
258
|
+
bin: HOOK_RUNTIME_ENTRIES
|
|
259
|
+
},
|
|
260
|
+
null,
|
|
261
|
+
2
|
|
262
|
+
)}
|
|
263
|
+
`;
|
|
264
|
+
}
|
|
265
|
+
function validateRelease(releaseDir, manifest) {
|
|
266
|
+
try {
|
|
267
|
+
if (!directReleaseLayout(releaseDir)) return false;
|
|
268
|
+
if (readFileSync(join(releaseDir, "manifest.json"), "utf8") !== exactManifest(manifest)) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
if (readFileSync(join(releaseDir, "package.json"), "utf8") !== runtimePackageJson(manifest)) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
if (readFileSync(join(releaseDir, "node"), "utf8") !== `${manifest.nodePath}
|
|
275
|
+
`) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
for (const [bin, relativePath] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
279
|
+
const target = join(releaseDir, relativePath);
|
|
280
|
+
if (sha256(target) !== manifest.files[bin]) return false;
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
} catch {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function directReleaseLayout(releaseDir) {
|
|
288
|
+
try {
|
|
289
|
+
const directories = /* @__PURE__ */ new Set([releaseDir]);
|
|
290
|
+
for (const relativePath of Object.values(HOOK_RUNTIME_ENTRIES)) {
|
|
291
|
+
for (let directory = dirname(relativePath); directory !== "."; directory = dirname(directory)) {
|
|
292
|
+
directories.add(join(releaseDir, directory));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (![...directories].every((directory) => lstatSync(directory).isDirectory())) return false;
|
|
296
|
+
const files = ["manifest.json", "package.json", "node", ...Object.values(HOOK_RUNTIME_ENTRIES)];
|
|
297
|
+
return files.every((relativePath) => lstatSync(join(releaseDir, relativePath)).isFile());
|
|
298
|
+
} catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function readReleaseManifest(releaseDir, releaseName) {
|
|
303
|
+
try {
|
|
304
|
+
const manifestPath = join(releaseDir, "manifest.json");
|
|
305
|
+
const manifestStat = statSync(manifestPath);
|
|
306
|
+
if (!manifestStat.isFile() || manifestStat.size > MAX_MANIFEST_BYTES) return null;
|
|
307
|
+
const raw = readFileSync(manifestPath, "utf8");
|
|
308
|
+
const parsed = JSON.parse(raw);
|
|
309
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
310
|
+
if (!exactKeys(parsed, ["schemaVersion", "version", "nodePath", "files"])) return null;
|
|
311
|
+
const candidate = parsed;
|
|
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(
|
|
313
|
+
(digest) => typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest)
|
|
314
|
+
)) {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
const manifest = candidate;
|
|
318
|
+
if (raw !== exactManifest(manifest) || releaseName !== `${RELEASE_PREFIX}${releaseId(manifest)}` || !validateRelease(releaseDir, manifest)) {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
return manifest;
|
|
322
|
+
} catch {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function readSelectedRelease(paths) {
|
|
327
|
+
let raw;
|
|
328
|
+
try {
|
|
329
|
+
raw = readFileSync(paths.current, "utf8");
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (error.code === "ENOENT") return null;
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
const name = raw.endsWith("\n") ? raw.slice(0, -1) : "";
|
|
335
|
+
if (`${name}
|
|
336
|
+
` !== raw || !RELEASE_NAME_RE.test(name)) {
|
|
337
|
+
throw new Error("cannot stage hook runtime: selected release is malformed");
|
|
338
|
+
}
|
|
339
|
+
const dir = join(paths.releasesDir, name);
|
|
340
|
+
const manifest = readReleaseManifest(dir, name);
|
|
341
|
+
if (!manifest) {
|
|
342
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
343
|
+
}
|
|
344
|
+
return { name, dir, manifest };
|
|
345
|
+
}
|
|
346
|
+
function releaseEntries(paths) {
|
|
347
|
+
return readdirSync(paths.releasesDir, { withFileTypes: true }).flatMap((entry) => {
|
|
348
|
+
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name)) return [];
|
|
349
|
+
const dir = join(paths.releasesDir, entry.name);
|
|
350
|
+
try {
|
|
351
|
+
return [{ name: entry.name, dir, mtimeMs: statSync(dir).mtimeMs }];
|
|
352
|
+
} catch {
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
function pruneReleases(paths, protectedNames, reserveSlots, now) {
|
|
358
|
+
const entries = releaseEntries(paths).sort(
|
|
359
|
+
(left, right) => right.mtimeMs - left.mtimeMs || left.name.localeCompare(right.name)
|
|
360
|
+
);
|
|
361
|
+
const protectedWithPredecessors = new Set(protectedNames);
|
|
362
|
+
for (const entry of entries) {
|
|
363
|
+
if (protectedWithPredecessors.size >= protectedNames.size + RETAIN_PREDECESSORS) break;
|
|
364
|
+
if (!protectedWithPredecessors.has(entry.name)) protectedWithPredecessors.add(entry.name);
|
|
365
|
+
}
|
|
366
|
+
const candidates = entries.filter(
|
|
367
|
+
(entry) => !protectedWithPredecessors.has(entry.name) && now - entry.mtimeMs >= RELEASE_GRACE_MS
|
|
368
|
+
).sort((left, right) => left.mtimeMs - right.mtimeMs || left.name.localeCompare(right.name));
|
|
369
|
+
let remaining = entries.length;
|
|
370
|
+
let deleted = 0;
|
|
371
|
+
for (const entry of candidates) {
|
|
372
|
+
if (remaining + reserveSlots <= MAX_RELEASES || deleted >= MAX_RELEASE_DELETIONS_PER_STAGE) {
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
rmSync(entry.dir, { recursive: true, force: true });
|
|
376
|
+
remaining -= 1;
|
|
377
|
+
deleted += 1;
|
|
378
|
+
}
|
|
379
|
+
if (remaining + reserveSlots > MAX_RELEASES) {
|
|
380
|
+
throw new Error(
|
|
381
|
+
"cannot stage hook runtime: release retention is full while recent runtimes are protected"
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
function sourceManifest(sourceDir, version, nodePath) {
|
|
386
|
+
const files = {};
|
|
387
|
+
for (const [bin, relativePath] of Object.entries(SOURCE_ENTRIES)) {
|
|
388
|
+
const source = join(sourceDir, relativePath);
|
|
389
|
+
if (!existsSync(source) || !statSync(source).isFile()) {
|
|
390
|
+
throw new Error(`cannot stage hook runtime: missing self-contained ${bin} bundle`);
|
|
391
|
+
}
|
|
392
|
+
files[bin] = sha256(source);
|
|
393
|
+
}
|
|
394
|
+
return { schemaVersion: HOOK_RUNTIME_SCHEMA_VERSION, version, nodePath, files };
|
|
395
|
+
}
|
|
396
|
+
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
397
|
+
set -eu
|
|
398
|
+
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
399
|
+
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
400
|
+
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
401
|
+
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
402
|
+
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
403
|
+
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
404
|
+
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
405
|
+
case "\${1-}" in
|
|
406
|
+
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
407
|
+
*) exit 64 ;;
|
|
408
|
+
esac
|
|
409
|
+
shift
|
|
410
|
+
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
411
|
+
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
412
|
+
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
413
|
+
exec "$prim_node" "$prim_entry" "$@"
|
|
414
|
+
`;
|
|
415
|
+
function privateDirectory(path) {
|
|
416
|
+
const stat = lstatIfPresent(path);
|
|
417
|
+
if (!stat?.isDirectory()) return false;
|
|
418
|
+
if (process.platform === "win32") return true;
|
|
419
|
+
const uid = process.getuid?.();
|
|
420
|
+
return (Number(stat.mode) & 511) === DIRECTORY_MODE && (uid === void 0 || stat.uid === uid);
|
|
421
|
+
}
|
|
422
|
+
function privateFile(path, mode) {
|
|
423
|
+
const stat = lstatIfPresent(path);
|
|
424
|
+
if (!stat?.isFile()) return false;
|
|
425
|
+
if (process.platform === "win32") return true;
|
|
426
|
+
const uid = process.getuid?.();
|
|
427
|
+
return (Number(stat.mode) & 511) === mode && (uid === void 0 || stat.uid === uid);
|
|
428
|
+
}
|
|
429
|
+
function hasSafeRuntimeLayout(paths, selected) {
|
|
430
|
+
const releaseDirectories = /* @__PURE__ */ new Set([selected.dir]);
|
|
431
|
+
for (const relativePath of Object.values(HOOK_RUNTIME_ENTRIES)) {
|
|
432
|
+
for (let directory = dirname(relativePath); directory !== "."; directory = dirname(directory)) {
|
|
433
|
+
releaseDirectories.add(join(selected.dir, directory));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
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)) {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
return Object.values(HOOK_RUNTIME_ENTRIES).every(
|
|
440
|
+
(relativePath) => privateFile(join(selected.dir, relativePath), DATA_FILE_MODE)
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
function sameNodeExecutable(recorded, desired) {
|
|
444
|
+
try {
|
|
445
|
+
if (!statSync(recorded).isFile()) return false;
|
|
446
|
+
accessSync(recorded, constants.X_OK);
|
|
447
|
+
return realpathSync(recorded) === realpathSync(desired);
|
|
448
|
+
} catch {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function recordedNodeMatchesCurrentProcess(nodePath) {
|
|
453
|
+
return sameNodeExecutable(nodePath, process.execPath);
|
|
454
|
+
}
|
|
455
|
+
function inspectHookRuntime(options = {}) {
|
|
456
|
+
let paths;
|
|
457
|
+
try {
|
|
458
|
+
paths = hookRuntimePaths(options);
|
|
459
|
+
} catch {
|
|
460
|
+
return { state: "invalid" };
|
|
461
|
+
}
|
|
462
|
+
const launcher = lstatIfPresent(paths.launcher);
|
|
463
|
+
const selector = lstatIfPresent(paths.current);
|
|
464
|
+
if (!(launcher || selector)) return { state: "missing" };
|
|
465
|
+
if (!(launcher && selector)) return { state: "invalid" };
|
|
466
|
+
try {
|
|
467
|
+
if (readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
468
|
+
return { state: "invalid" };
|
|
469
|
+
}
|
|
470
|
+
const selected = readSelectedRelease(paths);
|
|
471
|
+
if (!selected) return { state: "invalid" };
|
|
472
|
+
assertOwnedHookRuntime(paths);
|
|
473
|
+
if (!hasSafeRuntimeLayout(paths, selected)) return { state: "invalid" };
|
|
474
|
+
return recordedNodeMatchesCurrentProcess(selected.manifest.nodePath) ? { state: "ready", version: selected.manifest.version } : { state: "invalid" };
|
|
475
|
+
} catch {
|
|
476
|
+
return { state: "invalid" };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
function writeRelease(sourceDir, releaseDir, manifest, releasesDir) {
|
|
480
|
+
const stagingDir = mkdtempSync(join(releasesDir, ".stage-"));
|
|
481
|
+
chmodSync(stagingDir, DIRECTORY_MODE);
|
|
482
|
+
try {
|
|
483
|
+
const directories = /* @__PURE__ */ new Set([stagingDir]);
|
|
484
|
+
const syncStagedFile = (path) => {
|
|
485
|
+
syncFile(path);
|
|
486
|
+
for (let directory = dirname(path); ; directory = dirname(directory)) {
|
|
487
|
+
directories.add(directory);
|
|
488
|
+
if (directory === stagingDir) break;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
for (const [bin, targetRelative] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
492
|
+
const source = join(sourceDir, SOURCE_ENTRIES[bin]);
|
|
493
|
+
const target = join(stagingDir, targetRelative);
|
|
494
|
+
mkdirSync(dirname(target), { recursive: true, mode: DIRECTORY_MODE });
|
|
495
|
+
chmodSync(dirname(target), DIRECTORY_MODE);
|
|
496
|
+
copyFileSync(source, target);
|
|
497
|
+
chmodSync(target, DATA_FILE_MODE);
|
|
498
|
+
if (sha256(target) !== manifest.files[bin]) {
|
|
499
|
+
throw new Error(`cannot stage hook runtime: ${bin} changed while it was copied`);
|
|
500
|
+
}
|
|
501
|
+
syncStagedFile(target);
|
|
502
|
+
}
|
|
503
|
+
const nodePath = join(stagingDir, "node");
|
|
504
|
+
writeFileSync(nodePath, `${manifest.nodePath}
|
|
505
|
+
`, {
|
|
506
|
+
encoding: "utf8",
|
|
507
|
+
flag: "wx",
|
|
508
|
+
mode: DATA_FILE_MODE
|
|
509
|
+
});
|
|
510
|
+
syncStagedFile(nodePath);
|
|
511
|
+
const packageJsonPath = join(stagingDir, "package.json");
|
|
512
|
+
writeFileSync(packageJsonPath, runtimePackageJson(manifest), {
|
|
513
|
+
encoding: "utf8",
|
|
514
|
+
flag: "wx",
|
|
515
|
+
mode: DATA_FILE_MODE
|
|
516
|
+
});
|
|
517
|
+
syncStagedFile(packageJsonPath);
|
|
518
|
+
const manifestPath = join(stagingDir, "manifest.json");
|
|
519
|
+
writeFileSync(manifestPath, exactManifest(manifest), {
|
|
520
|
+
encoding: "utf8",
|
|
521
|
+
flag: "wx",
|
|
522
|
+
mode: DATA_FILE_MODE
|
|
523
|
+
});
|
|
524
|
+
syncStagedFile(manifestPath);
|
|
525
|
+
for (const directory of [...directories].sort((left, right) => right.length - left.length)) {
|
|
526
|
+
syncDirectory(directory);
|
|
527
|
+
}
|
|
528
|
+
try {
|
|
529
|
+
renameSync(stagingDir, releaseDir);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
const code = error.code;
|
|
532
|
+
if (code !== "EEXIST" && code !== "ENOTEMPTY" || !validateRelease(releaseDir, manifest)) {
|
|
533
|
+
throw error;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
} finally {
|
|
537
|
+
rmSync(stagingDir, { recursive: true, force: true });
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function stableNodePath(execPath = process.execPath, searchPath = process.env.PATH ?? "") {
|
|
541
|
+
let target;
|
|
542
|
+
try {
|
|
543
|
+
target = realpathSync(execPath);
|
|
544
|
+
} catch {
|
|
545
|
+
return execPath;
|
|
546
|
+
}
|
|
547
|
+
for (const directory of searchPath.split(delimiter)) {
|
|
548
|
+
if (!isAbsolute(directory)) continue;
|
|
549
|
+
const candidate = join(directory, "node");
|
|
550
|
+
try {
|
|
551
|
+
if (realpathSync(candidate) === target) return candidate;
|
|
552
|
+
} catch {
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return execPath;
|
|
556
|
+
}
|
|
557
|
+
function stageHookRuntime(options = {}) {
|
|
558
|
+
const root = packageRoot();
|
|
559
|
+
const sourceDir = options.sourceDir ?? (root ? join(root, "dist", "hook-runtime") : null);
|
|
560
|
+
const version = options.version ?? packageVersion();
|
|
561
|
+
const nodePath = resolve(options.nodePath ?? stableNodePath());
|
|
562
|
+
if (!sourceDir || !version || compareSemver(version, version) !== 0 || !isAbsolute(nodePath) || /[\r\n]/u.test(nodePath)) {
|
|
563
|
+
throw new Error("cannot stage hook runtime: package identity is unavailable");
|
|
564
|
+
}
|
|
565
|
+
const manifest = sourceManifest(sourceDir, version, nodePath);
|
|
566
|
+
const paths = hookRuntimePaths(options);
|
|
567
|
+
const releaseName = `${RELEASE_PREFIX}${releaseId(manifest)}`;
|
|
568
|
+
const releaseDir = join(paths.releasesDir, releaseName);
|
|
569
|
+
mkdirSync(paths.releasesDir, { recursive: true, mode: DIRECTORY_MODE });
|
|
570
|
+
chmodSync(paths.configDir, DIRECTORY_MODE);
|
|
571
|
+
chmodSync(paths.runtimeDir, DIRECTORY_MODE);
|
|
572
|
+
chmodSync(paths.releasesDir, DIRECTORY_MODE);
|
|
573
|
+
return withFileLockSync(paths.selectionLock, () => {
|
|
574
|
+
const now = Date.now();
|
|
575
|
+
const selected = readSelectedRelease(paths);
|
|
576
|
+
if (selected) utimesSync(selected.dir, now / 1e3, now / 1e3);
|
|
577
|
+
const precedence = selected ? compareSemver(selected.manifest.version, manifest.version) : -1;
|
|
578
|
+
if (precedence === void 0) {
|
|
579
|
+
throw new Error("cannot stage hook runtime: selected release version is malformed");
|
|
580
|
+
}
|
|
581
|
+
const selectRequested = !selected || precedence < 0 || precedence === 0 && !sameNodeExecutable(selected.manifest.nodePath, nodePath);
|
|
582
|
+
const requestedExists = existsSync(releaseDir);
|
|
583
|
+
const protectedBefore = new Set(selected ? [selected.name] : []);
|
|
584
|
+
if (selectRequested && requestedExists) protectedBefore.add(releaseName);
|
|
585
|
+
pruneReleases(paths, protectedBefore, selectRequested && !requestedExists ? 1 : 0, now);
|
|
586
|
+
if (selectRequested && !validateRelease(releaseDir, manifest)) {
|
|
587
|
+
if (requestedExists) {
|
|
588
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
589
|
+
}
|
|
590
|
+
writeRelease(sourceDir, releaseDir, manifest, paths.releasesDir);
|
|
591
|
+
}
|
|
592
|
+
const finalRelease = selectRequested ? { name: releaseName, dir: releaseDir, manifest } : selected;
|
|
593
|
+
const launcherCurrent = (() => {
|
|
594
|
+
try {
|
|
595
|
+
return readFileSync(paths.launcher, "utf8") === STABLE_HOOK_LAUNCHER_CONTENT;
|
|
596
|
+
} catch {
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
})();
|
|
600
|
+
const selectorCurrent = selected?.name === finalRelease.name;
|
|
601
|
+
if (!launcherCurrent) {
|
|
602
|
+
atomicWriteFile(paths.launcher, STABLE_HOOK_LAUNCHER_CONTENT, {
|
|
603
|
+
ensureParent: true,
|
|
604
|
+
mode: LAUNCHER_MODE
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
const entrypointCurrent = gitHookEntrypointCurrent(paths);
|
|
608
|
+
if (!entrypointCurrent) {
|
|
609
|
+
atomicWriteFile(paths.gitHookEntrypoint, GIT_HOOK_ENTRYPOINT_CONTENT, {
|
|
610
|
+
ensureParent: true,
|
|
611
|
+
mode: LAUNCHER_MODE
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
if (!selectorCurrent) {
|
|
615
|
+
syncDirectory(paths.releasesDir);
|
|
616
|
+
atomicWriteFile(paths.current, `${finalRelease.name}
|
|
617
|
+
`, {
|
|
618
|
+
ensureParent: true,
|
|
619
|
+
mode: DATA_FILE_MODE
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
utimesSync(finalRelease.dir, now / 1e3, now / 1e3);
|
|
623
|
+
pruneReleases(
|
|
624
|
+
paths,
|
|
625
|
+
/* @__PURE__ */ new Set([finalRelease.name, ...selected ? [selected.name] : []]),
|
|
626
|
+
0,
|
|
627
|
+
now
|
|
628
|
+
);
|
|
629
|
+
return {
|
|
630
|
+
changed: !launcherCurrent || !entrypointCurrent || !selectorCurrent,
|
|
631
|
+
releaseDir: finalRelease.dir,
|
|
632
|
+
manifest: finalRelease.manifest,
|
|
633
|
+
paths
|
|
634
|
+
};
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
function gitHookEntrypointCurrent(paths) {
|
|
638
|
+
try {
|
|
639
|
+
return readFileSync(paths.gitHookEntrypoint, "utf8") === GIT_HOOK_ENTRYPOINT_CONTENT;
|
|
640
|
+
} catch {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
function inspectGitHookEntrypoint(options = {}) {
|
|
645
|
+
let paths;
|
|
646
|
+
try {
|
|
647
|
+
paths = hookRuntimePaths(options);
|
|
648
|
+
} catch {
|
|
649
|
+
return "invalid";
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
const entrypoint = lstatIfPresent(paths.gitHookEntrypoint);
|
|
653
|
+
if (!entrypoint) return "missing";
|
|
654
|
+
if (!entrypoint.isFile() || (Number(entrypoint.mode) & 64) === 0 || !gitHookEntrypointCurrent(paths) || readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT || !lstatIfPresent(paths.current)?.isFile()) {
|
|
655
|
+
return "invalid";
|
|
656
|
+
}
|
|
657
|
+
return "ready";
|
|
658
|
+
} catch {
|
|
659
|
+
return "invalid";
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
function lstatIfPresent(path) {
|
|
663
|
+
try {
|
|
664
|
+
return lstatSync(path);
|
|
665
|
+
} catch (error) {
|
|
666
|
+
const code = error.code;
|
|
667
|
+
if (code === "ENOENT" || code === "ENOTDIR") return void 0;
|
|
668
|
+
throw error;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function assertOwnedHookRuntime(paths) {
|
|
672
|
+
const entrypoint = lstatIfPresent(paths.gitHookEntrypoint);
|
|
673
|
+
if (entrypoint && (!entrypoint.isFile() || !gitHookEntrypointCurrent(paths))) {
|
|
674
|
+
throw new Error(
|
|
675
|
+
`refusing to remove unrecognized Git hook entrypoint at ${paths.gitHookEntrypoint}`
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
const launcher = lstatIfPresent(paths.launcher);
|
|
679
|
+
if (launcher) {
|
|
680
|
+
if (!launcher.isFile() || readFileSync(paths.launcher, "utf8") !== STABLE_HOOK_LAUNCHER_CONTENT) {
|
|
681
|
+
throw new Error(`refusing to remove unrecognized hook launcher at ${paths.launcher}`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const runtime = lstatIfPresent(paths.runtimeDir);
|
|
685
|
+
if (!runtime) return;
|
|
686
|
+
if (!runtime.isDirectory()) {
|
|
687
|
+
throw new Error(`refusing to remove non-directory hook runtime at ${paths.runtimeDir}`);
|
|
688
|
+
}
|
|
689
|
+
const allowed = /* @__PURE__ */ new Set(["current", "releases", "selection.lock"]);
|
|
690
|
+
const unexpected = readdirSync(paths.runtimeDir).filter((entry) => !allowed.has(entry));
|
|
691
|
+
if (unexpected.length > 0) {
|
|
692
|
+
throw new Error(
|
|
693
|
+
`refusing to remove hook runtime with unrecognized entries: ${unexpected.sort().join(", ")}`
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
const current = lstatIfPresent(paths.current);
|
|
697
|
+
const releases = lstatIfPresent(paths.releasesDir);
|
|
698
|
+
if (current) {
|
|
699
|
+
if (!current.isFile() || !releases || !readSelectedRelease(paths)) {
|
|
700
|
+
throw new Error(`refusing to remove malformed hook runtime selection at ${paths.current}`);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (!releases) return;
|
|
704
|
+
if (!releases.isDirectory()) {
|
|
705
|
+
throw new Error(`refusing to remove non-directory hook releases at ${paths.releasesDir}`);
|
|
706
|
+
}
|
|
707
|
+
for (const entry of readdirSync(paths.releasesDir, { withFileTypes: true })) {
|
|
708
|
+
const releaseDir = join(paths.releasesDir, entry.name);
|
|
709
|
+
if (!entry.isDirectory() || !RELEASE_NAME_RE.test(entry.name) || !readReleaseManifest(releaseDir, entry.name)) {
|
|
710
|
+
throw new Error(`refusing to remove unrecognized hook release ${releaseDir}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
function removeHookRuntime(options = {}) {
|
|
715
|
+
const paths = hookRuntimePaths(options);
|
|
716
|
+
const hadLauncher = lstatIfPresent(paths.launcher) !== void 0;
|
|
717
|
+
const hadEntrypoint = lstatIfPresent(paths.gitHookEntrypoint) !== void 0;
|
|
718
|
+
const hadRuntime = lstatIfPresent(paths.runtimeDir) !== void 0;
|
|
719
|
+
if (!hadLauncher && !hadEntrypoint && !hadRuntime) return { changed: false, paths };
|
|
720
|
+
assertOwnedHookRuntime(paths);
|
|
721
|
+
if (!hadRuntime) {
|
|
722
|
+
rmSync(paths.gitHookEntrypoint, { force: true });
|
|
723
|
+
rmSync(paths.launcher, { force: true });
|
|
724
|
+
return { changed: true, paths };
|
|
725
|
+
}
|
|
726
|
+
withFileLockSync(paths.selectionLock, () => {
|
|
727
|
+
assertOwnedHookRuntime(paths);
|
|
728
|
+
if (lstatIfPresent(paths.gitHookEntrypoint)) rmSync(paths.gitHookEntrypoint, { force: true });
|
|
729
|
+
if (lstatIfPresent(paths.launcher)) rmSync(paths.launcher, { force: true });
|
|
730
|
+
const quarantined = `${paths.runtimeDir}.uninstall-${String(process.pid)}-${randomBytes(8).toString("hex")}`;
|
|
731
|
+
renameSync(paths.runtimeDir, quarantined);
|
|
732
|
+
rmSync(quarantined, { recursive: true, force: true });
|
|
733
|
+
});
|
|
734
|
+
return { changed: true, paths };
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// src/commands/claude-install.ts
|
|
738
|
+
var CAPTURE_BIN = "prim-hook";
|
|
739
|
+
var GATE_BIN = "prim-pre-tool-use";
|
|
740
|
+
var POST_TOOL_USE_BIN = "prim-post-tool-use";
|
|
741
|
+
var SESSION_START_BIN = "prim-session-start";
|
|
742
|
+
var SESSION_END_BIN = "prim-session-end";
|
|
743
|
+
var STATUSLINE_BIN = "prim-statusline";
|
|
744
|
+
var STATUSLINE_ARGS = "";
|
|
745
|
+
var STATUSLINE_COMMAND = stableHookCommand(STATUSLINE_BIN, STATUSLINE_ARGS);
|
|
746
|
+
var STATUSLINE_REFRESH_SECONDS = 5;
|
|
747
|
+
var PRIM_BINS = [
|
|
748
|
+
CAPTURE_BIN,
|
|
749
|
+
GATE_BIN,
|
|
750
|
+
POST_TOOL_USE_BIN,
|
|
751
|
+
SESSION_START_BIN,
|
|
752
|
+
SESSION_END_BIN
|
|
753
|
+
];
|
|
754
|
+
var JSON_INDENT = 2;
|
|
755
|
+
var USER_SCOPE_PATH = join2(homedir(), ".claude", "settings.json");
|
|
756
|
+
function projectRoot() {
|
|
757
|
+
return gitToplevel() ?? process.cwd();
|
|
758
|
+
}
|
|
759
|
+
var projectScopePath = () => join2(projectRoot(), ".claude", "settings.json");
|
|
760
|
+
var CAPTURE_EVENTS = [
|
|
761
|
+
"SessionStart",
|
|
762
|
+
"UserPromptSubmit",
|
|
763
|
+
"PreToolUse",
|
|
764
|
+
"PostToolUse",
|
|
765
|
+
"PostToolUseFailure",
|
|
766
|
+
"Stop",
|
|
767
|
+
"SessionEnd",
|
|
768
|
+
"SubagentStop"
|
|
769
|
+
];
|
|
770
|
+
function makeRegistration(event, matcher, bin, args = "") {
|
|
771
|
+
return {
|
|
772
|
+
event,
|
|
773
|
+
matcher,
|
|
774
|
+
bin,
|
|
775
|
+
command: stableHookCommand(bin, args)
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
function makeDetachedRegistration(event, matcher, bin, args = "") {
|
|
779
|
+
return { event, matcher, bin, command: detachedHookShimCommand(bin, args) };
|
|
780
|
+
}
|
|
781
|
+
var REGISTRATIONS = [
|
|
782
|
+
...CAPTURE_EVENTS.map(
|
|
783
|
+
(event) => event === "SessionEnd" ? makeDetachedRegistration(event, "*", CAPTURE_BIN) : makeRegistration(event, "*", CAPTURE_BIN)
|
|
784
|
+
),
|
|
785
|
+
makeRegistration("PreToolUse", "Edit|Write|MultiEdit|NotebookEdit|Bash", GATE_BIN),
|
|
786
|
+
makeRegistration("PostToolUse", "Edit|Write|MultiEdit|NotebookEdit|Bash", POST_TOOL_USE_BIN),
|
|
787
|
+
makeRegistration(
|
|
788
|
+
"PostToolUseFailure",
|
|
789
|
+
"Edit|Write|MultiEdit|NotebookEdit|Bash",
|
|
790
|
+
POST_TOOL_USE_BIN
|
|
791
|
+
),
|
|
792
|
+
makeRegistration("SessionStart", "*", SESSION_START_BIN),
|
|
793
|
+
makeDetachedRegistration("SessionEnd", "*", SESSION_END_BIN)
|
|
794
|
+
];
|
|
795
|
+
var PRIM_PERMISSION_RULE = "Bash(npx --yes @primitive.ai/prim*)";
|
|
796
|
+
var LEGACY_PRIM_PERMISSION_RULES = [
|
|
797
|
+
"Bash(npx --yes @primitive.ai/prim@latest:*)",
|
|
798
|
+
"Bash(npx --yes @primitive.ai/prim:*)"
|
|
799
|
+
];
|
|
800
|
+
var ALL_PRIM_PERMISSION_RULES = /* @__PURE__ */ new Set([
|
|
801
|
+
PRIM_PERMISSION_RULE,
|
|
802
|
+
...LEGACY_PRIM_PERMISSION_RULES
|
|
803
|
+
]);
|
|
804
|
+
var PRIM_AUTOMODE_TRUST = "Primitive CLI (prim): the @primitive.ai/prim npm package \u2014 run via `npx --yes @primitive.ai/prim \u2026` \u2014 and its API at api.getprimitive.ai are trusted developer tooling this user installed for passive decision capture, conflict checks, and team presence. Running prim and its routine API/daemon calls is expected, not untrusted code execution or data exfiltration.";
|
|
805
|
+
var AUTOMODE_DEFAULTS = "$defaults";
|
|
806
|
+
function settingsPathFor(scope) {
|
|
807
|
+
return scope === "user" ? USER_SCOPE_PATH : projectScopePath();
|
|
808
|
+
}
|
|
809
|
+
function readSettings(path) {
|
|
810
|
+
if (!existsSync2(path)) {
|
|
811
|
+
return {};
|
|
812
|
+
}
|
|
813
|
+
const raw = readFileSync2(path, "utf-8");
|
|
814
|
+
try {
|
|
815
|
+
return JSON.parse(raw);
|
|
816
|
+
} catch (err) {
|
|
817
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
818
|
+
throw new Error(`${path} is not valid JSON: ${detail}`);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function entryHasCommand(entry, bin) {
|
|
822
|
+
return entry.hooks?.some((h) => commandMatchesBin(h.command, bin)) ?? false;
|
|
823
|
+
}
|
|
824
|
+
function ownedRegistrationEntries(settings) {
|
|
825
|
+
return Object.entries(settings.hooks ?? {}).flatMap(
|
|
826
|
+
([event, entries]) => (entries ?? []).filter((entry) => PRIM_BINS.some((bin) => entryHasCommand(entry, bin))).map((entry) => ({ event, entry }))
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
function hasAnyHookRegistration(settings) {
|
|
830
|
+
return ownedRegistrationEntries(settings).length > 0;
|
|
831
|
+
}
|
|
832
|
+
function entryMatchesRegistration(owned, registration) {
|
|
833
|
+
return owned.event === registration.event && owned.entry.matcher === registration.matcher && owned.entry.hooks?.length === 1 && owned.entry.hooks[0].type === "command" && owned.entry.hooks[0].command === registration.command;
|
|
834
|
+
}
|
|
835
|
+
function hasCompleteHookRegistration(settings) {
|
|
836
|
+
const owned = ownedRegistrationEntries(settings);
|
|
837
|
+
return owned.length === REGISTRATIONS.length && REGISTRATIONS.every(
|
|
838
|
+
(registration) => owned.filter((entry) => entryMatchesRegistration(entry, registration)).length === 1
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
function canonicalEntry(reg) {
|
|
842
|
+
return { matcher: reg.matcher, hooks: [{ type: "command", command: reg.command }] };
|
|
843
|
+
}
|
|
844
|
+
function stripCommand(list, bin) {
|
|
845
|
+
const out = [];
|
|
846
|
+
for (const e of list) {
|
|
847
|
+
const hooks = (e.hooks ?? []).filter((h) => !commandMatchesBin(h.command, bin));
|
|
848
|
+
if (hooks.length > 0) {
|
|
849
|
+
out.push({ ...e, hooks });
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return out;
|
|
853
|
+
}
|
|
854
|
+
function ensureRegistration(list, reg, force) {
|
|
855
|
+
const isCanonical = (e) => e.matcher === reg.matcher && e.hooks?.length === 1 && e.hooks[0].command === reg.command;
|
|
856
|
+
const hasStray = (e) => !isCanonical(e) && (e.hooks ?? []).some((h) => commandMatchesBin(h.command, reg.bin));
|
|
857
|
+
if (list.some(isCanonical) && !list.some(hasStray) && !force) {
|
|
858
|
+
return list;
|
|
859
|
+
}
|
|
860
|
+
return [...stripCommand(list, reg.bin), canonicalEntry(reg)];
|
|
861
|
+
}
|
|
862
|
+
var LEGACY_STATUSLINE_COMMAND = "prim statusline";
|
|
863
|
+
var RUNTIME_STATUSLINE_SUFFIXES = [
|
|
864
|
+
"/prim/runtime/prim-statusline",
|
|
865
|
+
// One pre-launcher build persisted the JS entry directly. Keep recognizing
|
|
866
|
+
// it so reinstall migrates and uninstall removes that older staged form.
|
|
867
|
+
"/prim/runtime/current/prim-statusline"
|
|
868
|
+
];
|
|
869
|
+
function isPrimStatusLine(settings) {
|
|
870
|
+
const s = settings.statusLine;
|
|
871
|
+
if (s?.type !== "command") {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
const c = (s.command ?? "").trim();
|
|
875
|
+
return c === LEGACY_STATUSLINE_COMMAND || c.includes("@primitive.ai/prim") && c.includes("statusline") || c.includes("prim-hook-launcher-v1") && commandMatchesBin(c, STATUSLINE_BIN) || RUNTIME_STATUSLINE_SUFFIXES.some((suffix) => c.includes(suffix));
|
|
876
|
+
}
|
|
877
|
+
function hookRuntimeResolutions(settings) {
|
|
878
|
+
const resolutions = hookCommandResolutions(
|
|
879
|
+
Object.values(settings.hooks ?? {}).flatMap(
|
|
880
|
+
(entries) => (entries ?? []).flatMap((entry) => (entry.hooks ?? []).map((hook) => hook.command))
|
|
881
|
+
),
|
|
882
|
+
PRIM_BINS
|
|
883
|
+
);
|
|
884
|
+
if (!isPrimStatusLine(settings)) return resolutions;
|
|
885
|
+
const statusline = hookCommandResolution(settings.statusLine?.command, STATUSLINE_BIN);
|
|
886
|
+
resolutions.push(statusline ?? { kind: "legacy_path" });
|
|
887
|
+
return resolutions;
|
|
888
|
+
}
|
|
889
|
+
function inspectHookRuntimeResolutions() {
|
|
890
|
+
return [
|
|
891
|
+
...hookRuntimeResolutions(readSettings(USER_SCOPE_PATH)),
|
|
892
|
+
...hookRuntimeResolutions(readSettings(projectScopePath()))
|
|
893
|
+
];
|
|
894
|
+
}
|
|
895
|
+
function applyStatusLine(settings, command = STATUSLINE_COMMAND) {
|
|
896
|
+
if (command === false) {
|
|
897
|
+
return isPrimStatusLine(settings) ? { ...settings, statusLine: void 0 } : settings;
|
|
898
|
+
}
|
|
899
|
+
if (settings.statusLine && !isPrimStatusLine(settings)) {
|
|
900
|
+
return settings;
|
|
901
|
+
}
|
|
902
|
+
return {
|
|
903
|
+
...settings,
|
|
904
|
+
statusLine: {
|
|
905
|
+
type: "command",
|
|
906
|
+
command,
|
|
907
|
+
refreshInterval: STATUSLINE_REFRESH_SECONDS
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
function applyPermissions(settings) {
|
|
912
|
+
const permissions = settings.permissions ?? {};
|
|
913
|
+
const allow = permissions.allow ?? [];
|
|
914
|
+
const hasCanonical = allow.includes(PRIM_PERMISSION_RULE);
|
|
915
|
+
const hasLegacy = allow.some((rule) => LEGACY_PRIM_PERMISSION_RULES.includes(rule));
|
|
916
|
+
if (hasCanonical && !hasLegacy) {
|
|
917
|
+
return settings;
|
|
918
|
+
}
|
|
919
|
+
const withoutPrim = allow.filter((rule) => !ALL_PRIM_PERMISSION_RULES.has(rule));
|
|
920
|
+
return {
|
|
921
|
+
...settings,
|
|
922
|
+
permissions: { ...permissions, allow: [...withoutPrim, PRIM_PERMISSION_RULE] }
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
function removePrimPermission(settings) {
|
|
926
|
+
const permissions = settings.permissions;
|
|
927
|
+
if (!permissions?.allow?.some((rule) => ALL_PRIM_PERMISSION_RULES.has(rule))) {
|
|
928
|
+
return settings;
|
|
929
|
+
}
|
|
930
|
+
const allow = permissions.allow.filter((rule) => !ALL_PRIM_PERMISSION_RULES.has(rule));
|
|
931
|
+
const nextPermissions = {
|
|
932
|
+
...permissions,
|
|
933
|
+
allow: allow.length > 0 ? allow : void 0
|
|
934
|
+
};
|
|
935
|
+
const hasOtherPerms = Object.values(nextPermissions).some((v) => v !== void 0);
|
|
936
|
+
return { ...settings, permissions: hasOtherPerms ? nextPermissions : void 0 };
|
|
937
|
+
}
|
|
938
|
+
function applyAutoModeTrust(settings) {
|
|
939
|
+
const autoMode = settings.autoMode ?? {};
|
|
940
|
+
const env = autoMode.environment;
|
|
941
|
+
if (env?.includes(PRIM_AUTOMODE_TRUST)) {
|
|
942
|
+
return settings;
|
|
943
|
+
}
|
|
944
|
+
const nextEnv = env ? [...env, PRIM_AUTOMODE_TRUST] : [AUTOMODE_DEFAULTS, PRIM_AUTOMODE_TRUST];
|
|
945
|
+
return { ...settings, autoMode: { ...autoMode, environment: nextEnv } };
|
|
946
|
+
}
|
|
947
|
+
function removeAutoModeTrust(settings) {
|
|
948
|
+
const autoMode = settings.autoMode;
|
|
949
|
+
if (!autoMode?.environment?.includes(PRIM_AUTOMODE_TRUST)) {
|
|
950
|
+
return settings;
|
|
951
|
+
}
|
|
952
|
+
const env = autoMode.environment.filter((e) => e !== PRIM_AUTOMODE_TRUST);
|
|
953
|
+
const onlyDefaultsLeft = env.length === 0 || env.length === 1 && env[0] === AUTOMODE_DEFAULTS;
|
|
954
|
+
const nextAutoMode = {
|
|
955
|
+
...autoMode,
|
|
956
|
+
environment: onlyDefaultsLeft ? void 0 : env
|
|
957
|
+
};
|
|
958
|
+
const hasOther = Object.values(nextAutoMode).some((v) => v !== void 0);
|
|
959
|
+
return { ...settings, autoMode: hasOther ? nextAutoMode : void 0 };
|
|
960
|
+
}
|
|
961
|
+
function applyInstall(settings, options = {}) {
|
|
962
|
+
const hooks = { ...settings.hooks ?? {} };
|
|
963
|
+
for (const reg of REGISTRATIONS) {
|
|
964
|
+
hooks[reg.event] = ensureRegistration(hooks[reg.event] ?? [], reg, options.force ?? false);
|
|
965
|
+
}
|
|
966
|
+
return applyPermissions(
|
|
967
|
+
applyStatusLine(
|
|
968
|
+
{ ...settings, hooks },
|
|
969
|
+
options.installStatusline === false ? false : options.statuslineCommand ?? STATUSLINE_COMMAND
|
|
970
|
+
)
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
function applyUninstall(settings) {
|
|
974
|
+
const source = settings.hooks ?? {};
|
|
975
|
+
const hooks = {};
|
|
976
|
+
for (const event of Object.keys(source)) {
|
|
977
|
+
let list = source[event] ?? [];
|
|
978
|
+
for (const bin of PRIM_BINS) {
|
|
979
|
+
list = stripCommand(list, bin);
|
|
980
|
+
}
|
|
981
|
+
if (list.length > 0) {
|
|
982
|
+
hooks[event] = list;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
const next = { ...settings, hooks };
|
|
986
|
+
if (isPrimStatusLine(next)) {
|
|
987
|
+
next.statusLine = void 0;
|
|
988
|
+
}
|
|
989
|
+
return removeAutoModeTrust(removePrimPermission(next));
|
|
990
|
+
}
|
|
991
|
+
function captureInstalled(settings) {
|
|
992
|
+
return CAPTURE_EVENTS.some(
|
|
993
|
+
(event) => (settings.hooks?.[event] ?? []).some((e) => entryHasCommand(e, CAPTURE_BIN))
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
function feedbackInstalled(settings) {
|
|
997
|
+
const stopCapture = (settings.hooks?.Stop ?? []).some(
|
|
998
|
+
(entry) => entryHasCommand(entry, CAPTURE_BIN)
|
|
999
|
+
);
|
|
1000
|
+
const sessionStart = (settings.hooks?.SessionStart ?? []).some(
|
|
1001
|
+
(entry) => entryHasCommand(entry, SESSION_START_BIN)
|
|
1002
|
+
);
|
|
1003
|
+
return stopCapture && sessionStart;
|
|
1004
|
+
}
|
|
1005
|
+
function statuslineInstalled(settings) {
|
|
1006
|
+
return isPrimStatusLine(settings);
|
|
1007
|
+
}
|
|
1008
|
+
function statuslineState(settings) {
|
|
1009
|
+
if (isPrimStatusLine(settings)) {
|
|
1010
|
+
return "primitive";
|
|
1011
|
+
}
|
|
1012
|
+
return settings.statusLine ? "custom" : "empty";
|
|
1013
|
+
}
|
|
1014
|
+
function isGateInstalled(settings) {
|
|
1015
|
+
return (settings.hooks?.PreToolUse ?? []).some((e) => entryHasCommand(e, GATE_BIN));
|
|
1016
|
+
}
|
|
1017
|
+
function atomicWrite(path, content) {
|
|
1018
|
+
atomicWriteFile(path, `${JSON.stringify(content, null, JSON_INDENT)}
|
|
1019
|
+
`, {
|
|
1020
|
+
ensureParent: true
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
function userStatuslineBlocksProjectInstall(scope, userSettings) {
|
|
1024
|
+
return scope === "project" && statuslineState(userSettings) === "custom";
|
|
1025
|
+
}
|
|
1026
|
+
function performInstall(scope, force) {
|
|
1027
|
+
stageHookRuntime();
|
|
1028
|
+
const path = settingsPathFor(scope);
|
|
1029
|
+
const before = readSettings(path);
|
|
1030
|
+
const blockedByUserStatusline = userStatuslineBlocksProjectInstall(
|
|
1031
|
+
scope,
|
|
1032
|
+
scope === "project" ? readSettings(USER_SCOPE_PATH) : {}
|
|
1033
|
+
);
|
|
1034
|
+
const after = applyInstall(before, {
|
|
1035
|
+
force,
|
|
1036
|
+
statuslineCommand: STATUSLINE_COMMAND,
|
|
1037
|
+
installStatusline: !blockedByUserStatusline
|
|
1038
|
+
});
|
|
1039
|
+
const changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
1040
|
+
if (changed) {
|
|
1041
|
+
atomicWrite(path, after);
|
|
1042
|
+
}
|
|
1043
|
+
return {
|
|
1044
|
+
scope,
|
|
1045
|
+
path,
|
|
1046
|
+
gate: isGateInstalled(after),
|
|
1047
|
+
capture: captureInstalled(after),
|
|
1048
|
+
feedback: feedbackInstalled(after),
|
|
1049
|
+
statusline: statuslineInstalled(after),
|
|
1050
|
+
statuslineState: statuslineState(after),
|
|
1051
|
+
changed
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
function performUninstall(scope) {
|
|
1055
|
+
const path = settingsPathFor(scope);
|
|
1056
|
+
const before = readSettings(path);
|
|
1057
|
+
const after = applyUninstall(before);
|
|
1058
|
+
const changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
1059
|
+
if (changed) {
|
|
1060
|
+
atomicWrite(path, after);
|
|
1061
|
+
}
|
|
1062
|
+
return {
|
|
1063
|
+
scope,
|
|
1064
|
+
path,
|
|
1065
|
+
gate: isGateInstalled(after),
|
|
1066
|
+
capture: captureInstalled(after),
|
|
1067
|
+
feedback: feedbackInstalled(after),
|
|
1068
|
+
statusline: statuslineInstalled(after),
|
|
1069
|
+
statuslineState: statuslineState(after),
|
|
1070
|
+
changed
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
function performPermissionInstall(scope) {
|
|
1074
|
+
const path = settingsPathFor(scope);
|
|
1075
|
+
const before = readSettings(path);
|
|
1076
|
+
const after = applyAutoModeTrust(applyPermissions(before));
|
|
1077
|
+
const changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
1078
|
+
if (changed) {
|
|
1079
|
+
atomicWrite(path, after);
|
|
1080
|
+
}
|
|
1081
|
+
return {
|
|
1082
|
+
scope,
|
|
1083
|
+
path,
|
|
1084
|
+
allowed: (after.permissions?.allow ?? []).includes(PRIM_PERMISSION_RULE),
|
|
1085
|
+
autoModeTrusted: (after.autoMode?.environment ?? []).includes(PRIM_AUTOMODE_TRUST),
|
|
1086
|
+
changed
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
function performStatus() {
|
|
1090
|
+
const statusFor2 = (path) => {
|
|
1091
|
+
const settings = readSettings(path);
|
|
1092
|
+
return {
|
|
1093
|
+
path,
|
|
1094
|
+
present: hasAnyHookRegistration(settings) || statuslineInstalled(settings),
|
|
1095
|
+
gate: isGateInstalled(settings),
|
|
1096
|
+
capture: captureInstalled(settings),
|
|
1097
|
+
feedback: feedbackInstalled(settings),
|
|
1098
|
+
complete: hasCompleteHookRegistration(settings),
|
|
1099
|
+
statusline: statuslineInstalled(settings),
|
|
1100
|
+
statuslineState: statuslineState(settings)
|
|
1101
|
+
};
|
|
1102
|
+
};
|
|
1103
|
+
return { user: statusFor2(USER_SCOPE_PATH), project: statusFor2(projectScopePath()) };
|
|
1104
|
+
}
|
|
1105
|
+
function resolveScope(input) {
|
|
1106
|
+
if (input === void 0 || input === "project") {
|
|
1107
|
+
return "project";
|
|
1108
|
+
}
|
|
1109
|
+
if (input === "user") {
|
|
1110
|
+
return "user";
|
|
1111
|
+
}
|
|
1112
|
+
console.error(`[prim] unknown --scope "${input}" (expected: user or project)`);
|
|
1113
|
+
process.exit(1);
|
|
1114
|
+
}
|
|
1115
|
+
function registerClaudeCommands(program) {
|
|
1116
|
+
const claude = program.command("claude").description("Manage the prim Claude Code integration (capture, gate, ingest, presence)");
|
|
1117
|
+
claude.command("install").description("Register the prim hooks + statusline in Claude Code's settings.json").option(
|
|
1118
|
+
"--scope <scope>",
|
|
1119
|
+
"project (default, the repo's .claude/settings.json) or user (~/.claude/settings.json)"
|
|
1120
|
+
).option("--force", "Replace any drifted prim hook entries").action((opts) => {
|
|
1121
|
+
const scope = resolveScope(opts.scope);
|
|
1122
|
+
const result = performInstall(scope, opts.force ?? false);
|
|
1123
|
+
if (result.changed) {
|
|
1124
|
+
console.error(
|
|
1125
|
+
`[prim] Claude Code integration installed (${scope} scope) at ${result.path}`
|
|
1126
|
+
);
|
|
1127
|
+
} else {
|
|
1128
|
+
console.error(
|
|
1129
|
+
`[prim] Claude Code integration already present at ${result.path} (no changes)`
|
|
1130
|
+
);
|
|
1131
|
+
}
|
|
1132
|
+
const projectState = scope === "user" ? performStatus().project.statuslineState : void 0;
|
|
1133
|
+
const userState = scope === "project" ? performStatus().user.statuslineState : void 0;
|
|
1134
|
+
if (result.statuslineState === "custom" || projectState === "custom" || userState === "custom") {
|
|
1135
|
+
const owner = result.statuslineState === "custom" ? result.path : projectState === "custom" ? projectScopePath() : USER_SCOPE_PATH;
|
|
1136
|
+
console.error(
|
|
1137
|
+
`[prim] \u26A0 statusline skipped: an existing custom statusLine is preserved at ${owner}; run \`prim daemon status\` for Primitive health`
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1141
|
+
});
|
|
1142
|
+
claude.command("preauth").description(
|
|
1143
|
+
"Pre-authorize prim's own calls (allow-rule + auto-mode trust, no hooks) so they never prompt"
|
|
1144
|
+
).option(
|
|
1145
|
+
"--scope <scope>",
|
|
1146
|
+
"user (default for preauth \u2014 covers every repo) or project (this repo's .claude/settings.json)"
|
|
1147
|
+
).action((opts) => {
|
|
1148
|
+
const scope = resolveScope(opts.scope ?? "user");
|
|
1149
|
+
const result = performPermissionInstall(scope);
|
|
1150
|
+
console.error(
|
|
1151
|
+
result.changed ? `[prim] prim pre-authorization written (${scope} scope) at ${result.path} (allow-rule + auto-mode trust)` : `[prim] prim pre-authorization already present at ${result.path} (no changes)`
|
|
1152
|
+
);
|
|
1153
|
+
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1154
|
+
});
|
|
1155
|
+
claude.command("uninstall").description("Remove all prim hooks + the prim statusline from settings.json").option(
|
|
1156
|
+
"--scope <scope>",
|
|
1157
|
+
"project (default, the repo's .claude/settings.json) or user (~/.claude/settings.json)"
|
|
1158
|
+
).action((opts) => {
|
|
1159
|
+
const scope = resolveScope(opts.scope);
|
|
1160
|
+
const result = performUninstall(scope);
|
|
1161
|
+
if (result.changed) {
|
|
1162
|
+
console.error(`[prim] prim hooks removed from ${result.path}`);
|
|
1163
|
+
} else {
|
|
1164
|
+
console.error(`[prim] no prim hooks to remove at ${result.path} (nothing changed)`);
|
|
1165
|
+
}
|
|
1166
|
+
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1167
|
+
});
|
|
1168
|
+
claude.command("status").description(
|
|
1169
|
+
"Report whether each prim surface (gate, capture, feedback, statusline) is installed per scope"
|
|
1170
|
+
).action(() => {
|
|
1171
|
+
const result = performStatus();
|
|
1172
|
+
const mark = (b) => b ? "\u2713" : "\u2717";
|
|
1173
|
+
const line = (label, s) => `[prim] ${label}: gate ${mark(s.gate)} \xB7 capture ${mark(s.capture)} \xB7 feedback ${mark(s.feedback)} \xB7 statusline ${s.statuslineState} (${s.path})`;
|
|
1174
|
+
console.error(`${line("user", result.user)}
|
|
1175
|
+
${line("project", result.project)}`);
|
|
1176
|
+
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
// src/commands/cursor-install.ts
|
|
1181
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
1182
|
+
import { homedir as homedir2 } from "os";
|
|
1183
|
+
import { isAbsolute as isAbsolute2, join as join3 } from "path";
|
|
1184
|
+
var CAPTURE_BIN2 = "prim-hook";
|
|
1185
|
+
var GATE_BIN2 = "prim-pre-tool-use";
|
|
1186
|
+
var POST_TOOL_BIN = "prim-post-tool-use";
|
|
1187
|
+
var SESSION_START_BIN2 = "prim-session-start";
|
|
1188
|
+
var SESSION_END_BIN2 = "prim-session-end";
|
|
1189
|
+
var STATUSLINE_BIN2 = "prim-statusline";
|
|
1190
|
+
var PRIM_BINS2 = [CAPTURE_BIN2, GATE_BIN2, POST_TOOL_BIN, SESSION_START_BIN2, SESSION_END_BIN2];
|
|
1191
|
+
var CURSOR_EVENTS = [
|
|
1192
|
+
"sessionStart",
|
|
1193
|
+
"beforeSubmitPrompt",
|
|
1194
|
+
"preToolUse",
|
|
1195
|
+
"postToolUse",
|
|
1196
|
+
"postToolUseFailure",
|
|
1197
|
+
"afterAgentResponse",
|
|
1198
|
+
"subagentStop",
|
|
1199
|
+
"stop",
|
|
1200
|
+
"sessionEnd"
|
|
1201
|
+
];
|
|
1202
|
+
var REGISTRATIONS2 = [
|
|
1203
|
+
...CURSOR_EVENTS.map((event) => ({ event, bin: CAPTURE_BIN2 })),
|
|
1204
|
+
{ event: "preToolUse", bin: GATE_BIN2, matcher: "Write|Delete|Shell", timeout: 10 },
|
|
1205
|
+
{ event: "postToolUse", bin: POST_TOOL_BIN },
|
|
1206
|
+
{ event: "postToolUseFailure", bin: POST_TOOL_BIN },
|
|
1207
|
+
{ event: "sessionStart", bin: SESSION_START_BIN2 },
|
|
1208
|
+
{ event: "sessionEnd", bin: SESSION_END_BIN2 }
|
|
1209
|
+
];
|
|
1210
|
+
function cursorConfigDirectory(env = process.env, home = homedir2(), platform = process.platform) {
|
|
1211
|
+
const explicit = env.CURSOR_CONFIG_DIR?.trim();
|
|
1212
|
+
if (explicit && isAbsolute2(explicit)) return explicit;
|
|
1213
|
+
const xdg = env.XDG_CONFIG_HOME?.trim();
|
|
1214
|
+
if (platform !== "darwin" && xdg && isAbsolute2(xdg)) return join3(xdg, "cursor");
|
|
1215
|
+
return join3(home, ".cursor");
|
|
1216
|
+
}
|
|
1217
|
+
function cursorHooksPath(scope, root = projectRoot()) {
|
|
1218
|
+
return scope === "user" ? join3(cursorConfigDirectory(), "hooks.json") : join3(root, ".cursor", "hooks.json");
|
|
1219
|
+
}
|
|
1220
|
+
function cursorCliConfigPath() {
|
|
1221
|
+
return join3(cursorConfigDirectory(), "cli-config.json");
|
|
1222
|
+
}
|
|
1223
|
+
function plainObject(value) {
|
|
1224
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1225
|
+
}
|
|
1226
|
+
function readCursorHooks(path) {
|
|
1227
|
+
if (!existsSync3(path)) return {};
|
|
1228
|
+
let parsed;
|
|
1229
|
+
try {
|
|
1230
|
+
parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
1231
|
+
} catch {
|
|
1232
|
+
throw new Error(`invalid Cursor hooks JSON at ${path}`);
|
|
1233
|
+
}
|
|
1234
|
+
if (!plainObject(parsed)) throw new Error(`Cursor hooks config must be an object: ${path}`);
|
|
1235
|
+
if (parsed.version !== void 0 && parsed.version !== 1) {
|
|
1236
|
+
throw new Error(`unsupported Cursor hooks version at ${path} (expected version 1)`);
|
|
1237
|
+
}
|
|
1238
|
+
if (parsed.hooks !== void 0) {
|
|
1239
|
+
if (!plainObject(parsed.hooks)) throw new Error(`Cursor hooks must be an object: ${path}`);
|
|
1240
|
+
for (const [event, entries] of Object.entries(parsed.hooks)) {
|
|
1241
|
+
if (!Array.isArray(entries) || !entries.every(plainObject)) {
|
|
1242
|
+
throw new Error(`Cursor hook list must contain objects: ${event} in ${path}`);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return parsed;
|
|
1247
|
+
}
|
|
1248
|
+
function readObject(path) {
|
|
1249
|
+
if (!existsSync3(path)) return {};
|
|
1250
|
+
let parsed;
|
|
1251
|
+
try {
|
|
1252
|
+
parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
1253
|
+
} catch {
|
|
1254
|
+
throw new Error(`invalid Cursor CLI config JSON at ${path}`);
|
|
1255
|
+
}
|
|
1256
|
+
if (!plainObject(parsed)) throw new Error(`Cursor CLI config must be an object: ${path}`);
|
|
1257
|
+
return parsed;
|
|
1258
|
+
}
|
|
1259
|
+
function writeJson(path, value) {
|
|
1260
|
+
atomicWriteFile(path, `${JSON.stringify(value, null, 2)}
|
|
1261
|
+
`, { ensureParent: true });
|
|
1262
|
+
}
|
|
1263
|
+
function commandFor(registration, scope) {
|
|
1264
|
+
return stableHookCommand(
|
|
1265
|
+
registration.bin,
|
|
1266
|
+
`--agent cursor --event ${registration.event} --scope ${scope}`
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
var OWNED_COMMANDS = new Set(
|
|
1270
|
+
["project", "user"].flatMap(
|
|
1271
|
+
(scope) => REGISTRATIONS2.map((registration) => commandFor(registration, scope))
|
|
1272
|
+
)
|
|
1273
|
+
);
|
|
1274
|
+
function jsoncUnsafeCommand(command) {
|
|
1275
|
+
return command.replace('case "$1" in [/]* ', 'case "$1" in /*').replace("*[/][/]*", "*//*").replace("*[/].[/]*", "*/./*").replace("*[/]..[/]*", "*/../*").replace("*[/].", "*/.").replace("*[/]..", "*/..").replace("?*[/]", "?*/");
|
|
1276
|
+
}
|
|
1277
|
+
var JSONC_UNSAFE_OWNED_COMMANDS = new Set([...OWNED_COMMANDS].map(jsoncUnsafeCommand));
|
|
1278
|
+
function isOwnedCursorHookCommand(command) {
|
|
1279
|
+
return typeof command === "string" && (OWNED_COMMANDS.has(command) || JSONC_UNSAFE_OWNED_COMMANDS.has(command));
|
|
1280
|
+
}
|
|
1281
|
+
function hookFor(registration, scope) {
|
|
1282
|
+
return {
|
|
1283
|
+
command: commandFor(registration, scope),
|
|
1284
|
+
...registration.matcher ? { matcher: registration.matcher } : {},
|
|
1285
|
+
...registration.timeout ? { timeout: registration.timeout } : {}
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
function exactHook(entry, registration, scope) {
|
|
1289
|
+
return JSON.stringify(entry) === JSON.stringify(hookFor(registration, scope));
|
|
1290
|
+
}
|
|
1291
|
+
function applyCursorInstall(settings, scope) {
|
|
1292
|
+
const hooks = {};
|
|
1293
|
+
for (const [event, entries] of Object.entries(settings.hooks ?? {})) {
|
|
1294
|
+
hooks[event] = entries.filter((entry) => !isOwnedCursorHookCommand(entry.command));
|
|
1295
|
+
}
|
|
1296
|
+
for (const registration of REGISTRATIONS2) {
|
|
1297
|
+
const entries = hooks[registration.event] ?? [];
|
|
1298
|
+
entries.push(hookFor(registration, scope));
|
|
1299
|
+
hooks[registration.event] = entries;
|
|
1300
|
+
}
|
|
1301
|
+
return { ...settings, version: 1, hooks };
|
|
1302
|
+
}
|
|
1303
|
+
function applyCursorUninstall(settings) {
|
|
1304
|
+
const hooks = {};
|
|
1305
|
+
for (const [event, entries] of Object.entries(settings.hooks ?? {})) {
|
|
1306
|
+
const retained = entries.filter((entry) => !isOwnedCursorHookCommand(entry.command));
|
|
1307
|
+
if (retained.length > 0) hooks[event] = retained;
|
|
1308
|
+
}
|
|
1309
|
+
return { ...settings, hooks };
|
|
1310
|
+
}
|
|
1311
|
+
function ownedEntries(settings) {
|
|
1312
|
+
return Object.entries(settings.hooks ?? {}).flatMap(
|
|
1313
|
+
([event, entries]) => entries.filter((entry) => isOwnedCursorHookCommand(entry.command)).map((entry) => ({ event, entry }))
|
|
1314
|
+
);
|
|
1315
|
+
}
|
|
1316
|
+
function hasExactCursorHandler(settings, event, bin, scope) {
|
|
1317
|
+
const registration = REGISTRATIONS2.find(
|
|
1318
|
+
(candidate) => candidate.event === event && candidate.bin === bin
|
|
1319
|
+
);
|
|
1320
|
+
return registration ? (settings.hooks?.[event] ?? []).some((entry) => exactHook(entry, registration, scope)) : false;
|
|
1321
|
+
}
|
|
1322
|
+
function complete(settings, scope) {
|
|
1323
|
+
const owned = ownedEntries(settings);
|
|
1324
|
+
return owned.length === REGISTRATIONS2.length && REGISTRATIONS2.every(
|
|
1325
|
+
(registration) => owned.filter(
|
|
1326
|
+
({ event, entry }) => event === registration.event && exactHook(entry, registration, scope)
|
|
1327
|
+
).length === 1
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
function captureInstalled2(settings, scope) {
|
|
1331
|
+
return CURSOR_EVENTS.every((event) => hasExactCursorHandler(settings, event, CAPTURE_BIN2, scope));
|
|
1332
|
+
}
|
|
1333
|
+
function gateInstalled(settings, scope) {
|
|
1334
|
+
return hasExactCursorHandler(settings, "preToolUse", GATE_BIN2, scope);
|
|
1335
|
+
}
|
|
1336
|
+
var FOOTER = {
|
|
1337
|
+
type: "command",
|
|
1338
|
+
command: stableHookCommand(STATUSLINE_BIN2),
|
|
1339
|
+
updateIntervalMs: 1e3,
|
|
1340
|
+
timeoutMs: 2e3
|
|
1341
|
+
};
|
|
1342
|
+
var JSONC_UNSAFE_FOOTER = {
|
|
1343
|
+
...FOOTER,
|
|
1344
|
+
command: jsoncUnsafeCommand(FOOTER.command)
|
|
1345
|
+
};
|
|
1346
|
+
function exactFooter(value) {
|
|
1347
|
+
return plainObject(value) && JSON.stringify(value) === JSON.stringify(FOOTER);
|
|
1348
|
+
}
|
|
1349
|
+
function jsoncUnsafeFooter(value) {
|
|
1350
|
+
return plainObject(value) && JSON.stringify(value) === JSON.stringify(JSONC_UNSAFE_FOOTER);
|
|
1351
|
+
}
|
|
1352
|
+
function applyCursorFooterInstall(config) {
|
|
1353
|
+
if (config.statusLine === void 0)
|
|
1354
|
+
return { config: { ...config, statusLine: FOOTER }, installed: true, preservedCustom: false };
|
|
1355
|
+
if (exactFooter(config.statusLine)) return { config, installed: true, preservedCustom: false };
|
|
1356
|
+
if (jsoncUnsafeFooter(config.statusLine)) {
|
|
1357
|
+
return { config: { ...config, statusLine: FOOTER }, installed: true, preservedCustom: false };
|
|
1358
|
+
}
|
|
1359
|
+
return { config, installed: false, preservedCustom: true };
|
|
1360
|
+
}
|
|
1361
|
+
function applyCursorFooterUninstall(config) {
|
|
1362
|
+
if (!exactFooter(config.statusLine) && !jsoncUnsafeFooter(config.statusLine)) {
|
|
1363
|
+
return { config, removed: false };
|
|
1364
|
+
}
|
|
1365
|
+
return {
|
|
1366
|
+
config: Object.fromEntries(Object.entries(config).filter(([key]) => key !== "statusLine")),
|
|
1367
|
+
removed: true
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
function statusFor(scope, path = cursorHooksPath(scope)) {
|
|
1371
|
+
const settings = readCursorHooks(path);
|
|
1372
|
+
const base = {
|
|
1373
|
+
path,
|
|
1374
|
+
present: ownedEntries(settings).length > 0,
|
|
1375
|
+
gate: gateInstalled(settings, scope),
|
|
1376
|
+
capture: captureInstalled2(settings, scope),
|
|
1377
|
+
complete: complete(settings, scope)
|
|
1378
|
+
};
|
|
1379
|
+
if (scope === "user") {
|
|
1380
|
+
const cliConfig = readObject(cursorCliConfigPath());
|
|
1381
|
+
base.footer = exactFooter(cliConfig.statusLine);
|
|
1382
|
+
base.footerPreservedCustom = cliConfig.statusLine !== void 0 && !base.footer;
|
|
1383
|
+
}
|
|
1384
|
+
return base;
|
|
1385
|
+
}
|
|
1386
|
+
function performInstall2(scope, _force = false) {
|
|
1387
|
+
stageHookRuntime();
|
|
1388
|
+
const path = cursorHooksPath(scope);
|
|
1389
|
+
const before = readCursorHooks(path);
|
|
1390
|
+
const after = applyCursorInstall(before, scope);
|
|
1391
|
+
let changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
1392
|
+
if (changed) writeJson(path, after);
|
|
1393
|
+
let restartRequired = false;
|
|
1394
|
+
if (scope === "user") {
|
|
1395
|
+
const cliPath = cursorCliConfigPath();
|
|
1396
|
+
const cliBefore = readObject(cliPath);
|
|
1397
|
+
const footer = applyCursorFooterInstall(cliBefore);
|
|
1398
|
+
if (JSON.stringify(cliBefore) !== JSON.stringify(footer.config)) {
|
|
1399
|
+
writeJson(cliPath, footer.config);
|
|
1400
|
+
changed = true;
|
|
1401
|
+
restartRequired = true;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
return { scope, ...statusFor(scope, path), changed, restartRequired };
|
|
1405
|
+
}
|
|
1406
|
+
function performUninstall2(scope) {
|
|
1407
|
+
const path = cursorHooksPath(scope);
|
|
1408
|
+
const before = readCursorHooks(path);
|
|
1409
|
+
const after = applyCursorUninstall(before);
|
|
1410
|
+
let changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
1411
|
+
if (changed) writeJson(path, after);
|
|
1412
|
+
let restartRequired = false;
|
|
1413
|
+
if (scope === "user") {
|
|
1414
|
+
const cliPath = cursorCliConfigPath();
|
|
1415
|
+
const cliBefore = readObject(cliPath);
|
|
1416
|
+
const footer = applyCursorFooterUninstall(cliBefore);
|
|
1417
|
+
if (footer.removed) {
|
|
1418
|
+
writeJson(cliPath, footer.config);
|
|
1419
|
+
changed = true;
|
|
1420
|
+
restartRequired = true;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
return { scope, ...statusFor(scope, path), changed, restartRequired };
|
|
1424
|
+
}
|
|
1425
|
+
function performStatus2() {
|
|
1426
|
+
return { user: statusFor("user"), project: statusFor("project") };
|
|
1427
|
+
}
|
|
1428
|
+
function inspectHookRuntimeResolutions2() {
|
|
1429
|
+
const commands = ["user", "project"].flatMap(
|
|
1430
|
+
(scope) => ownedEntries(readCursorHooks(cursorHooksPath(scope))).map(({ entry }) => entry.command)
|
|
1431
|
+
);
|
|
1432
|
+
return hookCommandResolutions(commands, PRIM_BINS2);
|
|
1433
|
+
}
|
|
1434
|
+
function resolveScope2(input) {
|
|
1435
|
+
if (input === void 0 || input === "project") return "project";
|
|
1436
|
+
if (input === "user") return "user";
|
|
1437
|
+
throw new Error(`unknown --scope "${input}" (expected: user or project)`);
|
|
1438
|
+
}
|
|
1439
|
+
function registerCursorCommands(program) {
|
|
1440
|
+
const cursor = program.command("cursor").description("Manage the native Cursor integration (capture, gate, ingest, presence)");
|
|
1441
|
+
cursor.command("install").description("Register Primitive in Cursor's native hooks.json").option("--scope <scope>", "project (default) or user").option("--force", "Repair drifted Primitive entries; custom Cursor fields remain untouched").action((opts) => {
|
|
1442
|
+
const result = performInstall2(resolveScope2(opts.scope), opts.force);
|
|
1443
|
+
console.error(
|
|
1444
|
+
result.changed ? `[prim] Cursor integration installed (${result.scope} scope) at ${result.path}` : `[prim] Cursor integration already present at ${result.path}`
|
|
1445
|
+
);
|
|
1446
|
+
if (result.restartRequired) console.error("[prim] restart Cursor CLI to load the footer");
|
|
1447
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1448
|
+
});
|
|
1449
|
+
cursor.command("uninstall").description("Remove only Primitive-owned Cursor hooks and footer").option("--scope <scope>", "project (default) or user").action((opts) => {
|
|
1450
|
+
const result = performUninstall2(resolveScope2(opts.scope));
|
|
1451
|
+
console.error(
|
|
1452
|
+
result.changed ? `[prim] Cursor integration removed (${result.scope} scope)` : `[prim] no Primitive-owned Cursor integration found (${result.scope} scope)`
|
|
1453
|
+
);
|
|
1454
|
+
if (result.restartRequired) console.error("[prim] restart Cursor CLI to remove the footer");
|
|
1455
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1456
|
+
});
|
|
1457
|
+
cursor.command("status").description("Report native Cursor hook and footer status").action(() => {
|
|
1458
|
+
const result = performStatus2();
|
|
1459
|
+
const mark = (value) => value ? "\u2713" : "\u2717";
|
|
1460
|
+
console.error(
|
|
1461
|
+
`[prim] user: gate ${mark(result.user.gate)} \xB7 capture ${mark(result.user.capture)} \xB7 footer ${mark(result.user.footer)} (${result.user.path})
|
|
1462
|
+
[prim] project: gate ${mark(result.project.gate)} \xB7 capture ${mark(result.project.capture)} (${result.project.path})`
|
|
1463
|
+
);
|
|
1464
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
export {
|
|
1469
|
+
MANAGED_GIT_HOOK_NAMES,
|
|
1470
|
+
GIT_HOOK_CONTRACT_VERSION,
|
|
1471
|
+
blockMarkers,
|
|
1472
|
+
managedHookBlock,
|
|
1473
|
+
canonicalHookBlock,
|
|
1474
|
+
hookBlockContractVersion,
|
|
1475
|
+
hookRuntimePaths,
|
|
1476
|
+
inspectHookRuntime,
|
|
1477
|
+
stableNodePath,
|
|
1478
|
+
stageHookRuntime,
|
|
1479
|
+
inspectGitHookEntrypoint,
|
|
1480
|
+
assertOwnedHookRuntime,
|
|
1481
|
+
removeHookRuntime,
|
|
1482
|
+
projectRoot,
|
|
1483
|
+
makeRegistration,
|
|
1484
|
+
readSettings,
|
|
1485
|
+
entryHasCommand,
|
|
1486
|
+
stripCommand,
|
|
1487
|
+
ensureRegistration,
|
|
1488
|
+
inspectHookRuntimeResolutions,
|
|
1489
|
+
atomicWrite,
|
|
1490
|
+
performStatus,
|
|
1491
|
+
registerClaudeCommands,
|
|
1492
|
+
cursorConfigDirectory,
|
|
1493
|
+
cursorHooksPath,
|
|
1494
|
+
readCursorHooks,
|
|
1495
|
+
hasExactCursorHandler,
|
|
1496
|
+
performStatus2,
|
|
1497
|
+
inspectHookRuntimeResolutions2,
|
|
1498
|
+
registerCursorCommands
|
|
1499
|
+
};
|