@primitive.ai/prim 0.1.0-alpha.114 → 0.1.0-alpha.116
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 +62 -0
- package/SKILL.md +5 -3
- package/assets/primitive-gameplan/SKILL.md +134 -0
- package/assets/primitive-gameplan/claude-hooks.json +28 -0
- package/assets/primitive-gameplan/scripts/activate.mjs +37 -0
- package/dist/{chunk-IZXLH3R5.js → chunk-3TIKBQHV.js} +2584 -821
- package/dist/{chunk-NH3VC7YO.js → chunk-4OEJ2ONG.js} +1 -1
- package/dist/{chunk-DWAXFXDG.js → chunk-4SN5BCIG.js} +1 -1
- package/dist/{chunk-3KKEV7VI.js → chunk-6IRAGUYK.js} +1 -1
- package/dist/chunk-CXMLAWC2.js +96 -0
- package/dist/chunk-D3ZDZZYS.js +449 -0
- package/dist/{chunk-RUUSUF43.js → chunk-I3J344DF.js} +231 -94
- package/dist/{chunk-7DVEH7FO.js → chunk-JKJ22ONL.js} +1 -1
- package/dist/{chunk-PO5QAYD7.js → chunk-JKO6PZWQ.js} +119 -24
- package/dist/{chunk-BXLH4LDU.js → chunk-LEUZDTNT.js} +1 -1
- package/dist/{chunk-PJW6JZMH.js → chunk-LLKJAET2.js} +1 -1
- package/dist/{chunk-IMYELJIV.js → chunk-RLBASQJD.js} +3 -3
- package/dist/{chunk-RTULPLS7.js → chunk-S55JKCHQ.js} +1 -1
- package/dist/daemon/server.js +2583 -822
- package/dist/hook-runtime/daemon/server.js +2583 -822
- package/dist/hook-runtime/hooks/post-commit.js +2581 -820
- package/dist/hook-runtime/hooks/post-rewrite.js +2581 -820
- package/dist/hook-runtime/hooks/post-tool-use.js +2689 -921
- package/dist/hook-runtime/hooks/pre-tool-use.js +2922 -985
- package/dist/hook-runtime/hooks/prim-hook.js +2682 -914
- package/dist/hook-runtime/hooks/session-end.js +19 -12
- package/dist/hook-runtime/hooks/session-start.js +2828 -1060
- package/dist/hook-runtime/index.js +23395 -12348
- package/dist/hooks/post-commit.js +3 -3
- package/dist/hooks/post-rewrite.js +3 -3
- package/dist/hooks/post-tool-use.js +11 -11
- package/dist/hooks/pre-tool-use.js +78 -316
- package/dist/hooks/prim-hook.js +10 -10
- package/dist/hooks/session-end.js +2 -2
- package/dist/hooks/session-start.js +6 -6
- package/dist/index.js +564 -157
- package/package.json +8 -1
- package/dist/chunk-JDIAPBY7.js +0 -233
|
@@ -1,96 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
observeRun,
|
|
8
|
-
readRun,
|
|
9
|
-
runPath,
|
|
10
|
-
saveDemoJson,
|
|
11
|
-
saveRun,
|
|
12
|
-
sessionExcluded,
|
|
13
|
-
sessionKey
|
|
14
|
-
} from "./chunk-JDIAPBY7.js";
|
|
2
|
+
isDemoStatusResponse
|
|
3
|
+
} from "./chunk-3TIKBQHV.js";
|
|
4
|
+
import {
|
|
5
|
+
inspectHookRuntime
|
|
6
|
+
} from "./chunk-JKO6PZWQ.js";
|
|
15
7
|
import {
|
|
16
|
-
|
|
8
|
+
atomicWriteFile,
|
|
9
|
+
primConfigDirectory,
|
|
10
|
+
withFileLock,
|
|
11
|
+
withFileLockSync
|
|
17
12
|
} from "./chunk-XG7S3LIL.js";
|
|
18
13
|
|
|
19
|
-
// src/demo/isolation.ts
|
|
20
|
-
import { existsSync } from "fs";
|
|
21
|
-
function observeDemoInvocation(envelope, agent, checkExclusion = true) {
|
|
22
|
-
const session = typeof envelope.session_id === "string" && envelope.session_id ? envelope.session_id : agent === "hermes" ? envelope.extra?.session_key : void 0;
|
|
23
|
-
let invocation = false;
|
|
24
|
-
if (envelope.hook_event_name === "PreToolUse") {
|
|
25
|
-
const tool = agent === "cursor" ? "Shell" : agent === "hermes" ? "terminal" : "Bash";
|
|
26
|
-
const input = envelope.tool_input;
|
|
27
|
-
const command = input?.command ?? input?.cmd;
|
|
28
|
-
if (typeof command === "string" && command.length < 8192) {
|
|
29
|
-
const argv = demoArgv(command);
|
|
30
|
-
const id = argv?.[4] ?? command.match(/\x27([0-9a-f-]{36})\x27/u)?.[1];
|
|
31
|
-
if (id && RUN_ID.test(id) && existsSync(runPath(id))) {
|
|
32
|
-
const identity = typeof session === "string" && session ? sessionKey(agent, session) : void 0;
|
|
33
|
-
const observed = (result, errorCode) => observeRun(id, {
|
|
34
|
-
at: Date.now(),
|
|
35
|
-
agent,
|
|
36
|
-
runtimeVersion: packageVersion() ?? void 0,
|
|
37
|
-
session: identity,
|
|
38
|
-
result,
|
|
39
|
-
...errorCode ? { errorCode } : {}
|
|
40
|
-
});
|
|
41
|
-
const action = argv?.[3];
|
|
42
|
-
const choice = argv?.[6];
|
|
43
|
-
const reason = argv?.[8];
|
|
44
|
-
const recognized = ["start", "attempt", "resolve", "status", "skip"].includes(action);
|
|
45
|
-
const validChoice = action !== "resolve" || choice === "adhere" || choice === "supersede" && typeof reason === "string" && reason.trim().length > 0 && reason.length <= 500;
|
|
46
|
-
const matches = (run) => !run.terminal && recognized && validChoice && demoCommand(run, action, choice, reason) === command;
|
|
47
|
-
let candidate;
|
|
48
|
-
try {
|
|
49
|
-
candidate = readRun(id);
|
|
50
|
-
} catch {
|
|
51
|
-
}
|
|
52
|
-
if (envelope.tool_name !== tool || typeof input?.command !== "string") {
|
|
53
|
-
observed("tool-mismatch");
|
|
54
|
-
} else if (!candidate || !matches(candidate)) {
|
|
55
|
-
observed("command-mismatch");
|
|
56
|
-
} else
|
|
57
|
-
try {
|
|
58
|
-
invocation = lockRun(id, (run) => {
|
|
59
|
-
if (!matches(run)) {
|
|
60
|
-
observed("command-mismatch");
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
if (action === "skip") return true;
|
|
64
|
-
if (!identity) {
|
|
65
|
-
observed("session-missing");
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
const key = identity;
|
|
69
|
-
if (run.agent && run.agent !== agent || run.binding && run.binding !== key) {
|
|
70
|
-
observed("session-mismatch");
|
|
71
|
-
run.acknowledgment = void 0;
|
|
72
|
-
saveRun(run);
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
saveDemoJson(exclusionPath(key), { runId: run.id });
|
|
76
|
-
run.binding = key;
|
|
77
|
-
run.agent = agent;
|
|
78
|
-
run.acknowledgment = { command, at: Date.now() };
|
|
79
|
-
saveRun(run);
|
|
80
|
-
observed("acknowledged");
|
|
81
|
-
return true;
|
|
82
|
-
});
|
|
83
|
-
} catch (error) {
|
|
84
|
-
const code = error.code;
|
|
85
|
-
observed("write-failed", code && /^[A-Z0-9_]{1,32}$/u.test(code) ? code : void 0);
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return { invocation, excluded: checkExclusion && sessionExcluded(agent, session) };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
14
|
// src/hooks/pre-tool-use-scoring.ts
|
|
95
15
|
import { isAbsolute, relative, sep } from "path";
|
|
96
16
|
function anyUnverified(results) {
|
|
@@ -364,12 +284,12 @@ function children(node) {
|
|
|
364
284
|
return nodes;
|
|
365
285
|
}
|
|
366
286
|
function literal(text) {
|
|
367
|
-
const
|
|
368
|
-
if (
|
|
287
|
+
const quote2 = text[0];
|
|
288
|
+
if (quote2 !== '"' && quote2 !== "'" || text.at(-1) !== quote2) return;
|
|
369
289
|
let value = "";
|
|
370
290
|
for (let i = 1; i < text.length - 1; i++) {
|
|
371
291
|
const char = text[i];
|
|
372
|
-
if (char ===
|
|
292
|
+
if (char === quote2 || char === "\n" || char === "\r") return;
|
|
373
293
|
if (char !== "\\") {
|
|
374
294
|
value += char;
|
|
375
295
|
continue;
|
|
@@ -1190,8 +1110,202 @@ function analyzeShellTargets(source) {
|
|
|
1190
1110
|
}
|
|
1191
1111
|
}
|
|
1192
1112
|
|
|
1113
|
+
// src/demo/state.ts
|
|
1114
|
+
import { createHash, randomUUID } from "crypto";
|
|
1115
|
+
import { existsSync, lstatSync, readFileSync, readdirSync } from "fs";
|
|
1116
|
+
import { join, resolve } from "path";
|
|
1117
|
+
var RUN_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
|
|
1118
|
+
var demoDirectory = () => join(primConfigDirectory(), "demo");
|
|
1119
|
+
var runPath = (id) => {
|
|
1120
|
+
if (!RUN_ID.test(id)) throw new Error("invalid demo run ID");
|
|
1121
|
+
return join(demoDirectory(), "runs", `${id}.json`);
|
|
1122
|
+
};
|
|
1123
|
+
function readDemoJson(path) {
|
|
1124
|
+
const stat = lstatSync(path);
|
|
1125
|
+
if (!stat.isFile() || stat.size > 16384) throw new Error(`invalid demo state: ${path}`);
|
|
1126
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
1127
|
+
}
|
|
1128
|
+
function saveDemoJson(path, value) {
|
|
1129
|
+
atomicWriteFile(path, `${JSON.stringify(value)}
|
|
1130
|
+
`, { mode: 384, ensureParent: true });
|
|
1131
|
+
}
|
|
1132
|
+
function readRun(id) {
|
|
1133
|
+
const run = readDemoJson(runPath(id));
|
|
1134
|
+
if (![1, 2].includes(run.version) || run.id !== id || ![
|
|
1135
|
+
"prepared",
|
|
1136
|
+
"proposal",
|
|
1137
|
+
"conflict",
|
|
1138
|
+
"prompt",
|
|
1139
|
+
"approved",
|
|
1140
|
+
"applied",
|
|
1141
|
+
"cleanup",
|
|
1142
|
+
"completed",
|
|
1143
|
+
"skipped"
|
|
1144
|
+
].includes(run.stage) || run.needsStatus !== void 0 && typeof run.needsStatus !== "boolean" || run.server !== void 0 && !isDemoStatusResponse(run.server) || run.receipt !== void 0 && (!Number.isSafeInteger(run.receipt.revision) || run.receipt.revision < 0 || !/^[a-f0-9]{64}$/u.test(run.receipt.appliedDigest)) || typeof run.terminal !== "boolean" || !Array.isArray(run.launcher) || run.launcher.length !== 2 || run.launcher.some((value) => typeof value !== "string" || !value.startsWith("/")) || run.agent !== void 0 && !["claude_code", "codex", "cursor", "hermes"].includes(run.agent)) {
|
|
1145
|
+
throw new Error("invalid demo run state");
|
|
1146
|
+
}
|
|
1147
|
+
return run;
|
|
1148
|
+
}
|
|
1149
|
+
var saveRun = (run) => saveDemoJson(runPath(run.id), run);
|
|
1150
|
+
var lockRun = (id, action) => withFileLockSync(`${runPath(id)}.lock`, () => action(readRun(id)), { timeoutMs: 500 });
|
|
1151
|
+
function prepareRun(agent, terminal = false) {
|
|
1152
|
+
const run = {
|
|
1153
|
+
version: 2,
|
|
1154
|
+
id: randomUUID(),
|
|
1155
|
+
stage: "prepared",
|
|
1156
|
+
terminal,
|
|
1157
|
+
agent,
|
|
1158
|
+
preparationKey: preparationKey(agent),
|
|
1159
|
+
launcher: [process.execPath, resolve(process.argv[1])]
|
|
1160
|
+
};
|
|
1161
|
+
saveRun(run);
|
|
1162
|
+
return run;
|
|
1163
|
+
}
|
|
1164
|
+
function preparationKey(agent) {
|
|
1165
|
+
return agent === "codex" && process.env.CODEX_THREAD_ID ? sessionKey(agent, `${process.env.CODEX_THREAD_ID}\0${process.cwd()}`) : void 0;
|
|
1166
|
+
}
|
|
1167
|
+
var quote = (value) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
1168
|
+
function demoCommand(run, action, choice, reason) {
|
|
1169
|
+
return [
|
|
1170
|
+
...run.launcher,
|
|
1171
|
+
"demo",
|
|
1172
|
+
action,
|
|
1173
|
+
run.id,
|
|
1174
|
+
...choice === void 0 ? [] : ["--choice", choice],
|
|
1175
|
+
...reason === void 0 ? [] : ["--reason", reason]
|
|
1176
|
+
].map(quote).join(" ");
|
|
1177
|
+
}
|
|
1178
|
+
function demoWelcomeCommand(run) {
|
|
1179
|
+
return [
|
|
1180
|
+
process.execPath,
|
|
1181
|
+
resolve(process.argv[1]),
|
|
1182
|
+
"welcome",
|
|
1183
|
+
"--after-demo",
|
|
1184
|
+
...run.agent ? ["--agent", run.agent === "claude_code" ? "claude" : run.agent] : [],
|
|
1185
|
+
...run.binding ? ["--capture-excluded"] : []
|
|
1186
|
+
].map(quote).join(" ");
|
|
1187
|
+
}
|
|
1188
|
+
function demoDiagnosticCommand(run) {
|
|
1189
|
+
return [process.execPath, resolve(process.argv[1]), "demo", "status", run.id].map(quote).join(" ");
|
|
1190
|
+
}
|
|
1191
|
+
function demoResumeCommand(run) {
|
|
1192
|
+
return [process.execPath, resolve(process.argv[1]), "demo", "resume", run.id].map(quote).join(" ");
|
|
1193
|
+
}
|
|
1194
|
+
function onboardingFinished() {
|
|
1195
|
+
const path = join(demoDirectory(), "onboarding.json");
|
|
1196
|
+
if (!existsSync(path)) return false;
|
|
1197
|
+
const value = readDemoJson(path);
|
|
1198
|
+
if (value.outcome !== "completed" && value.outcome !== "skipped") {
|
|
1199
|
+
throw new Error("invalid local onboarding outcome");
|
|
1200
|
+
}
|
|
1201
|
+
return true;
|
|
1202
|
+
}
|
|
1203
|
+
function finishOnboarding(outcome) {
|
|
1204
|
+
saveDemoJson(join(demoDirectory(), "onboarding.json"), { outcome });
|
|
1205
|
+
}
|
|
1206
|
+
function pendingRuns() {
|
|
1207
|
+
const path = join(demoDirectory(), "runs");
|
|
1208
|
+
if (!existsSync(path)) return [];
|
|
1209
|
+
return readdirSync(path).filter((name) => name.endsWith(".json") && RUN_ID.test(name.slice(0, -5))).map((name) => name.slice(0, -5)).filter((id) => !["completed", "skipped"].includes(readRun(id).stage));
|
|
1210
|
+
}
|
|
1211
|
+
function sessionKey(agent, session) {
|
|
1212
|
+
return createHash("sha256").update(`${agent}\0${session}`).digest("hex");
|
|
1213
|
+
}
|
|
1214
|
+
function exclusionPath(key) {
|
|
1215
|
+
if (!/^[0-9a-f]{64}$/u.test(key)) throw new Error("invalid demo session identity");
|
|
1216
|
+
return join(demoDirectory(), "excluded-sessions", `${key}.json`);
|
|
1217
|
+
}
|
|
1218
|
+
function sessionExcluded(agent, session) {
|
|
1219
|
+
if (typeof session !== "string" || !session) return false;
|
|
1220
|
+
const path = exclusionPath(sessionKey(agent, session));
|
|
1221
|
+
if (!existsSync(path)) return false;
|
|
1222
|
+
const record = readDemoJson(path);
|
|
1223
|
+
if (typeof record.runId !== "string" || !RUN_ID.test(record.runId)) {
|
|
1224
|
+
throw new Error("invalid demo capture exclusion");
|
|
1225
|
+
}
|
|
1226
|
+
return true;
|
|
1227
|
+
}
|
|
1228
|
+
var lockRunAsync = (id, action) => withFileLock(`${runPath(id)}.lock`, () => action(readRun(id)), { timeoutMs: 500 });
|
|
1229
|
+
function demoArgv(command) {
|
|
1230
|
+
const result = [];
|
|
1231
|
+
let i = 0;
|
|
1232
|
+
while (i < command.length) {
|
|
1233
|
+
if (command[i++] !== "'") return;
|
|
1234
|
+
let value = "";
|
|
1235
|
+
let closed = false;
|
|
1236
|
+
while (i < command.length) {
|
|
1237
|
+
if (command.slice(i, i + 4) === "'\\''") {
|
|
1238
|
+
value += "'";
|
|
1239
|
+
i += 4;
|
|
1240
|
+
} else if (command[i] === "'") {
|
|
1241
|
+
i++;
|
|
1242
|
+
closed = true;
|
|
1243
|
+
break;
|
|
1244
|
+
} else {
|
|
1245
|
+
value += command[i++];
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
if (!closed) return;
|
|
1249
|
+
result.push(value);
|
|
1250
|
+
if (i === command.length) return result;
|
|
1251
|
+
if (command[i++] !== " " || i === command.length) return;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// src/demo/diagnostics.ts
|
|
1256
|
+
import { existsSync as existsSync2 } from "fs";
|
|
1257
|
+
import { join as join2 } from "path";
|
|
1258
|
+
var observationPath = (id) => join2(demoDirectory(), "observations", `${id}.json`);
|
|
1259
|
+
function observeRun(id, observation) {
|
|
1260
|
+
try {
|
|
1261
|
+
saveDemoJson(observationPath(id), observation);
|
|
1262
|
+
} catch {
|
|
1263
|
+
process.stderr.write(
|
|
1264
|
+
"[prim] demo isolation diagnostic could not be saved; check access to Primitive's configuration directory.\n"
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
function isolationDiagnostics(run) {
|
|
1269
|
+
let observation;
|
|
1270
|
+
let observationError;
|
|
1271
|
+
try {
|
|
1272
|
+
if (existsSync2(observationPath(run.id))) {
|
|
1273
|
+
const value = readDemoJson(observationPath(run.id));
|
|
1274
|
+
if (!value || !Number.isFinite(value.at) || !["claude_code", "codex", "cursor", "hermes"].includes(value.agent ?? "") || ![
|
|
1275
|
+
"acknowledged",
|
|
1276
|
+
"tool-mismatch",
|
|
1277
|
+
"command-mismatch",
|
|
1278
|
+
"session-mismatch",
|
|
1279
|
+
"session-missing",
|
|
1280
|
+
"write-failed"
|
|
1281
|
+
].includes(value.result ?? "") || value.runtimeVersion !== void 0 && !/^[a-zA-Z0-9.+-]{1,80}$/u.test(value.runtimeVersion) || value.session !== void 0 && !/^[a-f0-9]{64}$/u.test(value.session) || value.errorCode !== void 0 && !/^[A-Z0-9_]{1,32}$/u.test(value.errorCode))
|
|
1282
|
+
throw new Error("invalid demo observation");
|
|
1283
|
+
observation = value;
|
|
1284
|
+
}
|
|
1285
|
+
} catch {
|
|
1286
|
+
observationError = "unreadable";
|
|
1287
|
+
}
|
|
1288
|
+
const expected = run.agent === "codex" && process.env.CODEX_THREAD_ID ? sessionKey(run.agent, process.env.CODEX_THREAD_ID) : void 0;
|
|
1289
|
+
return {
|
|
1290
|
+
runtime: inspectHookRuntime(),
|
|
1291
|
+
expectedEvent: "PreToolUse",
|
|
1292
|
+
expectedTool: run.agent === "cursor" ? "Shell" : run.agent === "hermes" ? "terminal" : "Bash",
|
|
1293
|
+
hookObservation: observation ? {
|
|
1294
|
+
at: observation.at,
|
|
1295
|
+
agent: observation.agent,
|
|
1296
|
+
runtimeVersion: observation.runtimeVersion ?? "unknown",
|
|
1297
|
+
result: observation.result,
|
|
1298
|
+
ageMs: Math.max(0, Date.now() - observation.at),
|
|
1299
|
+
sessionMatches: expected ? observation.session === expected : "unknown",
|
|
1300
|
+
...observation.errorCode ? { errorCode: observation.errorCode } : {}
|
|
1301
|
+
} : null,
|
|
1302
|
+
...observationError ? { observationError } : {},
|
|
1303
|
+
trust: "unknown",
|
|
1304
|
+
capture: run.binding ? "excluded-for-bound-session" : "not-verified"
|
|
1305
|
+
};
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1193
1308
|
export {
|
|
1194
|
-
observeDemoInvocation,
|
|
1195
1309
|
buildHookOutput,
|
|
1196
1310
|
buildCodexOutput,
|
|
1197
1311
|
buildCursorOutput,
|
|
@@ -1203,5 +1317,28 @@ export {
|
|
|
1203
1317
|
extractFileTargets,
|
|
1204
1318
|
readHookMode,
|
|
1205
1319
|
demoteForMode,
|
|
1206
|
-
analyzeShellTargets
|
|
1320
|
+
analyzeShellTargets,
|
|
1321
|
+
RUN_ID,
|
|
1322
|
+
runPath,
|
|
1323
|
+
readDemoJson,
|
|
1324
|
+
saveDemoJson,
|
|
1325
|
+
readRun,
|
|
1326
|
+
saveRun,
|
|
1327
|
+
lockRun,
|
|
1328
|
+
prepareRun,
|
|
1329
|
+
preparationKey,
|
|
1330
|
+
demoCommand,
|
|
1331
|
+
demoWelcomeCommand,
|
|
1332
|
+
demoDiagnosticCommand,
|
|
1333
|
+
demoResumeCommand,
|
|
1334
|
+
onboardingFinished,
|
|
1335
|
+
finishOnboarding,
|
|
1336
|
+
pendingRuns,
|
|
1337
|
+
sessionKey,
|
|
1338
|
+
exclusionPath,
|
|
1339
|
+
sessionExcluded,
|
|
1340
|
+
lockRunAsync,
|
|
1341
|
+
demoArgv,
|
|
1342
|
+
observeRun,
|
|
1343
|
+
isolationDiagnostics
|
|
1207
1344
|
};
|
|
@@ -19,9 +19,9 @@ import {
|
|
|
19
19
|
} from "./chunk-XG7S3LIL.js";
|
|
20
20
|
|
|
21
21
|
// src/commands/claude-install.ts
|
|
22
|
-
import { existsSync as existsSync2, readFileSync as
|
|
22
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "fs";
|
|
23
23
|
import { homedir } from "os";
|
|
24
|
-
import { join as
|
|
24
|
+
import { join as join3 } from "path";
|
|
25
25
|
|
|
26
26
|
// src/lib/hook-runtime.ts
|
|
27
27
|
import { createHash, randomBytes } from "crypto";
|
|
@@ -734,6 +734,83 @@ function removeHookRuntime(options = {}) {
|
|
|
734
734
|
return { changed: true, paths };
|
|
735
735
|
}
|
|
736
736
|
|
|
737
|
+
// src/lib/preflight-host.ts
|
|
738
|
+
import { execFileSync } from "child_process";
|
|
739
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
740
|
+
import { join as join2 } from "path";
|
|
741
|
+
function inspectFailureProtection(host) {
|
|
742
|
+
if (host === "codex")
|
|
743
|
+
return {
|
|
744
|
+
host,
|
|
745
|
+
status: "limited",
|
|
746
|
+
detail: "Codex accepts Primitive's explicit deny before the 240-second deadline, but does not provide a documented fail-closed policy for a killed, crashed, or timed-out hook. The installed hook timeout is 300 seconds; this host boundary is not guaranteed."
|
|
747
|
+
};
|
|
748
|
+
let output;
|
|
749
|
+
try {
|
|
750
|
+
output = execFileSync(host, [host === "hermes" ? "version" : "--version"], {
|
|
751
|
+
encoding: "utf8",
|
|
752
|
+
timeout: 3e3,
|
|
753
|
+
maxBuffer: 64 * 1024,
|
|
754
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
755
|
+
});
|
|
756
|
+
} catch {
|
|
757
|
+
return {
|
|
758
|
+
host,
|
|
759
|
+
status: "unknown",
|
|
760
|
+
detail: `Cannot inspect ${host}; install or expose the host on PATH, then run prim doctor. Host failure protection is not verified.`
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
const version = /\b(\d+\.\d+\.\d+(?:-[\w.-]+)?)\b/u.exec(output)?.[1];
|
|
764
|
+
if (host === "hermes") {
|
|
765
|
+
const project = /^\s*Project:\s*(.+?)\s*$/mu.exec(output)?.[1];
|
|
766
|
+
try {
|
|
767
|
+
if (project) {
|
|
768
|
+
const source = readFileSync2(join2(project, "agent", "shell_hooks.py"), "utf8");
|
|
769
|
+
if (/fail_closed\s*:\s*bool/u.test(source) && /["']fail_closed["']/u.test(source)) {
|
|
770
|
+
return {
|
|
771
|
+
host,
|
|
772
|
+
version,
|
|
773
|
+
status: "supported",
|
|
774
|
+
detail: "Installed Hermes exposes fail_closed shell hooks; Primitive configures fail_closed: true and the 300-second maximum timeout."
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
return {
|
|
778
|
+
host,
|
|
779
|
+
version,
|
|
780
|
+
status: "unsupported",
|
|
781
|
+
detail: "Installed Hermes does not expose fail_closed shell hooks. Upgrade Hermes before relying on host timeout/crash protection; then run prim hermes install and prim doctor."
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
} catch {
|
|
785
|
+
}
|
|
786
|
+
return {
|
|
787
|
+
host,
|
|
788
|
+
version,
|
|
789
|
+
status: "unknown",
|
|
790
|
+
detail: "Cannot verify installed Hermes fail_closed support. Upgrade to a version supporting fail_closed, expose its Project path through hermes version, then run prim doctor."
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
const minimum = host === "claude" ? "2.1.295" : "3.24.9";
|
|
794
|
+
const comparison = compareSemver(version, minimum);
|
|
795
|
+
const status = comparison === void 0 ? "unknown" : comparison < 0 ? "unsupported" : "supported";
|
|
796
|
+
return {
|
|
797
|
+
host,
|
|
798
|
+
version,
|
|
799
|
+
status,
|
|
800
|
+
detail: status === "supported" ? `${host} ${version} supports the configured ${host === "claude" ? 'onFailure: "block"' : "failClosed: true"} policy and 300-second timeout.` : `${host} ${version ?? "version unknown"}: upgrade to ${minimum} or newer ${host === "cursor" ? "(Primitive's verified failure-protection baseline) " : ""}before relying on host timeout/crash protection; then run prim ${host} install and prim doctor.`
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
function reportFailureProtection(host) {
|
|
804
|
+
const protection = inspectFailureProtection(host);
|
|
805
|
+
console.error(`[prim] ${protection.detail}`);
|
|
806
|
+
if (protection.status === "unsupported" || protection.status === "unknown") {
|
|
807
|
+
console.error(
|
|
808
|
+
"[prim] Conflict Gate host failure protection is not ready; installed settings alone do not establish readiness."
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
return protection;
|
|
812
|
+
}
|
|
813
|
+
|
|
737
814
|
// src/commands/claude-install.ts
|
|
738
815
|
var CAPTURE_BIN = "prim-hook";
|
|
739
816
|
var GATE_BIN = "prim-pre-tool-use";
|
|
@@ -752,11 +829,11 @@ var PRIM_BINS = [
|
|
|
752
829
|
SESSION_END_BIN
|
|
753
830
|
];
|
|
754
831
|
var JSON_INDENT = 2;
|
|
755
|
-
var USER_SCOPE_PATH =
|
|
832
|
+
var USER_SCOPE_PATH = join3(homedir(), ".claude", "settings.json");
|
|
756
833
|
function projectRoot() {
|
|
757
834
|
return gitToplevel() ?? process.cwd();
|
|
758
835
|
}
|
|
759
|
-
var projectScopePath = () =>
|
|
836
|
+
var projectScopePath = () => join3(projectRoot(), ".claude", "settings.json");
|
|
760
837
|
var CAPTURE_EVENTS = [
|
|
761
838
|
"SessionStart",
|
|
762
839
|
"UserPromptSubmit",
|
|
@@ -772,7 +849,8 @@ function makeRegistration(event, matcher, bin, args = "") {
|
|
|
772
849
|
event,
|
|
773
850
|
matcher,
|
|
774
851
|
bin,
|
|
775
|
-
command: stableHookCommand(bin, args)
|
|
852
|
+
command: stableHookCommand(bin, args),
|
|
853
|
+
...bin === GATE_BIN ? { timeout: 300, ...args.includes("--agent codex") ? {} : { onFailure: "block" } } : {}
|
|
776
854
|
};
|
|
777
855
|
}
|
|
778
856
|
function makeDetachedRegistration(event, matcher, bin, args = "") {
|
|
@@ -810,7 +888,7 @@ function readSettings(path) {
|
|
|
810
888
|
if (!existsSync2(path)) {
|
|
811
889
|
return {};
|
|
812
890
|
}
|
|
813
|
-
const raw =
|
|
891
|
+
const raw = readFileSync3(path, "utf-8");
|
|
814
892
|
try {
|
|
815
893
|
return JSON.parse(raw);
|
|
816
894
|
} catch (err) {
|
|
@@ -830,7 +908,7 @@ function hasAnyHookRegistration(settings) {
|
|
|
830
908
|
return ownedRegistrationEntries(settings).length > 0;
|
|
831
909
|
}
|
|
832
910
|
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;
|
|
911
|
+
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 && owned.entry.hooks[0].timeout === registration.timeout && owned.entry.hooks[0].onFailure === registration.onFailure && (registration.bin !== GATE_BIN || owned.entry.hooks[0].async !== true);
|
|
834
912
|
}
|
|
835
913
|
function hasCompleteHookRegistration(settings) {
|
|
836
914
|
const owned = ownedRegistrationEntries(settings);
|
|
@@ -839,7 +917,17 @@ function hasCompleteHookRegistration(settings) {
|
|
|
839
917
|
);
|
|
840
918
|
}
|
|
841
919
|
function canonicalEntry(reg) {
|
|
842
|
-
return {
|
|
920
|
+
return {
|
|
921
|
+
matcher: reg.matcher,
|
|
922
|
+
hooks: [
|
|
923
|
+
{
|
|
924
|
+
type: "command",
|
|
925
|
+
command: reg.command,
|
|
926
|
+
...reg.timeout === void 0 ? {} : { timeout: reg.timeout },
|
|
927
|
+
...reg.onFailure === void 0 ? {} : { onFailure: reg.onFailure }
|
|
928
|
+
}
|
|
929
|
+
]
|
|
930
|
+
};
|
|
843
931
|
}
|
|
844
932
|
function stripCommand(list, bin) {
|
|
845
933
|
const out = [];
|
|
@@ -852,9 +940,9 @@ function stripCommand(list, bin) {
|
|
|
852
940
|
return out;
|
|
853
941
|
}
|
|
854
942
|
function ensureRegistration(list, reg, force) {
|
|
855
|
-
const isCanonical = (e) => e.matcher === reg.matcher && e.hooks?.length === 1 && e.hooks[0].command === reg.command;
|
|
943
|
+
const isCanonical = (e) => e.matcher === reg.matcher && e.hooks?.length === 1 && e.hooks[0].type === "command" && e.hooks[0].command === reg.command && e.hooks[0].timeout === reg.timeout && e.hooks[0].onFailure === reg.onFailure && (reg.bin !== GATE_BIN || e.hooks[0].async !== true);
|
|
856
944
|
const hasStray = (e) => !isCanonical(e) && (e.hooks ?? []).some((h) => commandMatchesBin(h.command, reg.bin));
|
|
857
|
-
if (list.some(isCanonical) && !list.some(hasStray) && !force) {
|
|
945
|
+
if (list.some(isCanonical) && !list.some(hasStray) && !force && (reg.bin !== GATE_BIN || list.filter(isCanonical).length === 1)) {
|
|
858
946
|
return list;
|
|
859
947
|
}
|
|
860
948
|
return [...stripCommand(list, reg.bin), canonicalEntry(reg)];
|
|
@@ -1120,6 +1208,7 @@ function registerClaudeCommands(program) {
|
|
|
1120
1208
|
).option("--force", "Replace any drifted prim hook entries").action((opts) => {
|
|
1121
1209
|
const scope = resolveScope(opts.scope);
|
|
1122
1210
|
const result = performInstall(scope, opts.force ?? false);
|
|
1211
|
+
const failureProtection = reportFailureProtection("claude");
|
|
1123
1212
|
if (result.changed) {
|
|
1124
1213
|
console.error(
|
|
1125
1214
|
`[prim] Claude Code integration installed (${scope} scope) at ${result.path}`
|
|
@@ -1137,7 +1226,7 @@ function registerClaudeCommands(program) {
|
|
|
1137
1226
|
`[prim] \u26A0 statusline skipped: an existing custom statusLine is preserved at ${owner}; run \`prim daemon status\` for Primitive health`
|
|
1138
1227
|
);
|
|
1139
1228
|
}
|
|
1140
|
-
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1229
|
+
console.log(JSON.stringify({ ...result, failureProtection }, null, JSON_INDENT));
|
|
1141
1230
|
});
|
|
1142
1231
|
claude.command("preauth").description(
|
|
1143
1232
|
"Pre-authorize prim's own calls (allow-rule + auto-mode trust, no hooks) so they never prompt"
|
|
@@ -1169,18 +1258,19 @@ function registerClaudeCommands(program) {
|
|
|
1169
1258
|
"Report whether each prim surface (gate, capture, feedback, statusline) is installed per scope"
|
|
1170
1259
|
).action(() => {
|
|
1171
1260
|
const result = performStatus();
|
|
1261
|
+
const failureProtection = result.user.gate || result.project.gate ? reportFailureProtection("claude") : void 0;
|
|
1172
1262
|
const mark = (b) => b ? "\u2713" : "\u2717";
|
|
1173
1263
|
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
1264
|
console.error(`${line("user", result.user)}
|
|
1175
1265
|
${line("project", result.project)}`);
|
|
1176
|
-
console.log(JSON.stringify(result, null, JSON_INDENT));
|
|
1266
|
+
console.log(JSON.stringify({ ...result, failureProtection }, null, JSON_INDENT));
|
|
1177
1267
|
});
|
|
1178
1268
|
}
|
|
1179
1269
|
|
|
1180
1270
|
// src/commands/cursor-install.ts
|
|
1181
|
-
import { existsSync as existsSync3, readFileSync as
|
|
1271
|
+
import { existsSync as existsSync3, readFileSync as readFileSync4 } from "fs";
|
|
1182
1272
|
import { homedir as homedir2 } from "os";
|
|
1183
|
-
import { isAbsolute as isAbsolute2, join as
|
|
1273
|
+
import { isAbsolute as isAbsolute2, join as join4 } from "path";
|
|
1184
1274
|
var CAPTURE_BIN2 = "prim-hook";
|
|
1185
1275
|
var GATE_BIN2 = "prim-pre-tool-use";
|
|
1186
1276
|
var POST_TOOL_BIN = "prim-post-tool-use";
|
|
@@ -1201,7 +1291,7 @@ var CURSOR_EVENTS = [
|
|
|
1201
1291
|
];
|
|
1202
1292
|
var REGISTRATIONS2 = [
|
|
1203
1293
|
...CURSOR_EVENTS.map((event) => ({ event, bin: CAPTURE_BIN2 })),
|
|
1204
|
-
{ event: "preToolUse", bin: GATE_BIN2, matcher: "Write|Delete|Shell", timeout:
|
|
1294
|
+
{ event: "preToolUse", bin: GATE_BIN2, matcher: "Write|Delete|Shell", timeout: 300 },
|
|
1205
1295
|
{ event: "postToolUse", bin: POST_TOOL_BIN },
|
|
1206
1296
|
{ event: "postToolUseFailure", bin: POST_TOOL_BIN },
|
|
1207
1297
|
{ event: "sessionStart", bin: SESSION_START_BIN2 },
|
|
@@ -1211,14 +1301,14 @@ function cursorConfigDirectory(env = process.env, home = homedir2(), platform =
|
|
|
1211
1301
|
const explicit = env.CURSOR_CONFIG_DIR?.trim();
|
|
1212
1302
|
if (explicit && isAbsolute2(explicit)) return explicit;
|
|
1213
1303
|
const xdg = env.XDG_CONFIG_HOME?.trim();
|
|
1214
|
-
if (platform !== "darwin" && xdg && isAbsolute2(xdg)) return
|
|
1215
|
-
return
|
|
1304
|
+
if (platform !== "darwin" && xdg && isAbsolute2(xdg)) return join4(xdg, "cursor");
|
|
1305
|
+
return join4(home, ".cursor");
|
|
1216
1306
|
}
|
|
1217
1307
|
function cursorHooksPath(scope, root = projectRoot()) {
|
|
1218
|
-
return scope === "user" ?
|
|
1308
|
+
return scope === "user" ? join4(cursorConfigDirectory(), "hooks.json") : join4(root, ".cursor", "hooks.json");
|
|
1219
1309
|
}
|
|
1220
1310
|
function cursorCliConfigPath() {
|
|
1221
|
-
return
|
|
1311
|
+
return join4(cursorConfigDirectory(), "cli-config.json");
|
|
1222
1312
|
}
|
|
1223
1313
|
function plainObject(value) {
|
|
1224
1314
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -1227,7 +1317,7 @@ function readCursorHooks(path) {
|
|
|
1227
1317
|
if (!existsSync3(path)) return {};
|
|
1228
1318
|
let parsed;
|
|
1229
1319
|
try {
|
|
1230
|
-
parsed = JSON.parse(
|
|
1320
|
+
parsed = JSON.parse(readFileSync4(path, "utf8"));
|
|
1231
1321
|
} catch {
|
|
1232
1322
|
throw new Error(`invalid Cursor hooks JSON at ${path}`);
|
|
1233
1323
|
}
|
|
@@ -1249,7 +1339,7 @@ function readObject(path) {
|
|
|
1249
1339
|
if (!existsSync3(path)) return {};
|
|
1250
1340
|
let parsed;
|
|
1251
1341
|
try {
|
|
1252
|
-
parsed = JSON.parse(
|
|
1342
|
+
parsed = JSON.parse(readFileSync4(path, "utf8"));
|
|
1253
1343
|
} catch {
|
|
1254
1344
|
throw new Error(`invalid Cursor CLI config JSON at ${path}`);
|
|
1255
1345
|
}
|
|
@@ -1282,7 +1372,8 @@ function hookFor(registration, scope) {
|
|
|
1282
1372
|
return {
|
|
1283
1373
|
command: commandFor(registration, scope),
|
|
1284
1374
|
...registration.matcher ? { matcher: registration.matcher } : {},
|
|
1285
|
-
...registration.timeout ? { timeout: registration.timeout } : {}
|
|
1375
|
+
...registration.timeout ? { timeout: registration.timeout } : {},
|
|
1376
|
+
...registration.bin === GATE_BIN2 ? { failClosed: true } : {}
|
|
1286
1377
|
};
|
|
1287
1378
|
}
|
|
1288
1379
|
function exactHook(entry, registration, scope) {
|
|
@@ -1440,11 +1531,12 @@ function registerCursorCommands(program) {
|
|
|
1440
1531
|
const cursor = program.command("cursor").description("Manage the native Cursor integration (capture, gate, ingest, presence)");
|
|
1441
1532
|
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
1533
|
const result = performInstall2(resolveScope2(opts.scope), opts.force);
|
|
1534
|
+
const failureProtection = reportFailureProtection("cursor");
|
|
1443
1535
|
console.error(
|
|
1444
1536
|
result.changed ? `[prim] Cursor integration installed (${result.scope} scope) at ${result.path}` : `[prim] Cursor integration already present at ${result.path}`
|
|
1445
1537
|
);
|
|
1446
1538
|
if (result.restartRequired) console.error("[prim] restart Cursor CLI to load the footer");
|
|
1447
|
-
console.log(JSON.stringify(result, null, 2));
|
|
1539
|
+
console.log(JSON.stringify({ ...result, failureProtection }, null, 2));
|
|
1448
1540
|
});
|
|
1449
1541
|
cursor.command("uninstall").description("Remove only Primitive-owned Cursor hooks and footer").option("--scope <scope>", "project (default) or user").action((opts) => {
|
|
1450
1542
|
const result = performUninstall2(resolveScope2(opts.scope));
|
|
@@ -1456,12 +1548,13 @@ function registerCursorCommands(program) {
|
|
|
1456
1548
|
});
|
|
1457
1549
|
cursor.command("status").description("Report native Cursor hook and footer status").action(() => {
|
|
1458
1550
|
const result = performStatus2();
|
|
1551
|
+
const failureProtection = result.user.gate || result.project.gate ? reportFailureProtection("cursor") : void 0;
|
|
1459
1552
|
const mark = (value) => value ? "\u2713" : "\u2717";
|
|
1460
1553
|
console.error(
|
|
1461
1554
|
`[prim] user: gate ${mark(result.user.gate)} \xB7 capture ${mark(result.user.capture)} \xB7 footer ${mark(result.user.footer)} (${result.user.path})
|
|
1462
1555
|
[prim] project: gate ${mark(result.project.gate)} \xB7 capture ${mark(result.project.capture)} (${result.project.path})`
|
|
1463
1556
|
);
|
|
1464
|
-
console.log(JSON.stringify(result, null, 2));
|
|
1557
|
+
console.log(JSON.stringify({ ...result, failureProtection }, null, 2));
|
|
1465
1558
|
});
|
|
1466
1559
|
}
|
|
1467
1560
|
|
|
@@ -1479,6 +1572,8 @@ export {
|
|
|
1479
1572
|
inspectGitHookEntrypoint,
|
|
1480
1573
|
assertOwnedHookRuntime,
|
|
1481
1574
|
removeHookRuntime,
|
|
1575
|
+
inspectFailureProtection,
|
|
1576
|
+
reportFailureProtection,
|
|
1482
1577
|
projectRoot,
|
|
1483
1578
|
makeRegistration,
|
|
1484
1579
|
readSettings,
|