@primitive.ai/prim 0.1.0-alpha.1 → 0.1.0-alpha.100
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 +413 -37
- package/SKILL.md +272 -0
- package/dist/chunk-2IRLFOEL.js +766 -0
- package/dist/chunk-2Q3OGSTV.js +164 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5HFTJEA6.js +168 -0
- package/dist/chunk-5LRKJTLA.js +155 -0
- package/dist/chunk-6C3CCVDI.js +264 -0
- package/dist/chunk-7VIMU2HR.js +209 -0
- package/dist/chunk-C4D2LMTS.js +511 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GSBHBU7N.js +18982 -0
- package/dist/chunk-H6JAQVW4.js +258 -0
- package/dist/chunk-IDMLJ4TM.js +36 -0
- package/dist/chunk-JJI7FU24.js +439 -0
- package/dist/chunk-K4JWJ6OM.js +1496 -0
- package/dist/chunk-KGBMU76T.js +175 -0
- package/dist/chunk-MXVCP33E.js +847 -0
- package/dist/chunk-MZEVEPL3.js +262 -0
- package/dist/chunk-O2WV5SQZ.js +1876 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-QIHXTYRD.js +135 -0
- package/dist/chunk-XG7S3LIL.js +1372 -0
- package/dist/daemon/server.js +22644 -0
- package/dist/hook-runtime/daemon/server.js +22644 -0
- package/dist/hook-runtime/hooks/post-commit.js +20133 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +20032 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +27307 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1657 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +26714 -0
- package/dist/hook-runtime/hooks/prim-hook.js +27367 -0
- package/dist/hook-runtime/hooks/session-end.js +1176 -0
- package/dist/hook-runtime/hooks/session-start.js +30167 -0
- package/dist/hook-runtime/index.js +53486 -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 +285 -0
- package/dist/hooks/pre-commit.js +21 -74
- package/dist/hooks/pre-tool-use.js +387 -0
- package/dist/hooks/prim-hook.js +235 -0
- package/dist/hooks/session-end.js +64 -0
- package/dist/hooks/session-start.js +309 -0
- package/dist/index.js +9721 -423
- package/dist/statusline-main.js +886 -0
- package/package.json +46 -23
- package/dist/chunk-3APLWTLB.js +0 -165
package/dist/hooks/pre-commit.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
checkAffectedDecisions,
|
|
4
|
+
formatDecisionsWarning
|
|
5
|
+
} from "../chunk-2Q3OGSTV.js";
|
|
6
|
+
import "../chunk-2IRLFOEL.js";
|
|
7
|
+
import "../chunk-5LRKJTLA.js";
|
|
8
|
+
import "../chunk-XG7S3LIL.js";
|
|
5
9
|
|
|
6
10
|
// src/hooks/pre-commit.ts
|
|
7
11
|
import { execSync } from "child_process";
|
|
@@ -11,81 +15,24 @@ function getStagedFiles() {
|
|
|
11
15
|
});
|
|
12
16
|
return output.trim().split("\n").filter((f) => f.length > 0);
|
|
13
17
|
}
|
|
14
|
-
function
|
|
15
|
-
const regexStr = pattern.replaceAll("**", "\xA7GLOBSTAR\xA7").replaceAll("*", "[^/]*").replaceAll("\xA7GLOBSTAR\xA7", ".*");
|
|
16
|
-
const regex = new RegExp(`^${regexStr}$`);
|
|
17
|
-
return regex.test(filePath);
|
|
18
|
-
}
|
|
19
|
-
function findAffectedContexts(stagedFiles, specs) {
|
|
20
|
-
const affected = /* @__PURE__ */ new Map();
|
|
21
|
-
for (const file of stagedFiles) {
|
|
22
|
-
for (const spec of specs) {
|
|
23
|
-
for (const pattern of spec.filePatterns) {
|
|
24
|
-
if (matchPattern(file, pattern)) {
|
|
25
|
-
const existing = affected.get(spec._id);
|
|
26
|
-
if (existing) {
|
|
27
|
-
existing.matchedFiles.push(file);
|
|
28
|
-
} else {
|
|
29
|
-
affected.set(spec._id, {
|
|
30
|
-
contextId: spec._id,
|
|
31
|
-
matchedFiles: [file]
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return affected;
|
|
40
|
-
}
|
|
41
|
-
var HOOK_TIMEOUT_MS = 1e4;
|
|
42
|
-
async function main() {
|
|
18
|
+
async function runDecisionsCheck() {
|
|
43
19
|
const stagedFiles = getStagedFiles();
|
|
44
20
|
if (stagedFiles.length === 0) {
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
const client = getClient();
|
|
48
|
-
let mappings = [];
|
|
49
|
-
try {
|
|
50
|
-
mappings = await client.get("/api/cli/specs/mappings", {
|
|
51
|
-
signal: AbortSignal.timeout(HOOK_TIMEOUT_MS)
|
|
52
|
-
});
|
|
53
|
-
} catch {
|
|
54
|
-
process.exit(0);
|
|
55
|
-
}
|
|
56
|
-
if (mappings.length === 0) {
|
|
57
|
-
process.exit(0);
|
|
21
|
+
return { decisions: [], truncated: false };
|
|
58
22
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const ctx = await client.get(`/api/cli/contexts/${contextId}`, {
|
|
67
|
-
signal: AbortSignal.timeout(HOOK_TIMEOUT_MS)
|
|
68
|
-
});
|
|
69
|
-
if (!ctx._id) {
|
|
70
|
-
console.log(` [skip] ${contextId} \u2014 not found`);
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
if (!ctx.isSpecDocument) {
|
|
74
|
-
console.log(` [skip] ${contextId} \u2014 not a spec document`);
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
await client.post(`/api/cli/contexts/${contextId}/sync`, void 0, {
|
|
78
|
-
signal: AbortSignal.timeout(HOOK_TIMEOUT_MS)
|
|
79
|
-
});
|
|
80
|
-
console.log(` [synced] ${contextId} \u2014 ${ctx.name ?? "(unnamed)"}`);
|
|
81
|
-
} catch (error) {
|
|
82
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
83
|
-
console.error(` [error] ${contextId} \u2014 ${message}`);
|
|
84
|
-
}
|
|
23
|
+
return checkAffectedDecisions(stagedFiles);
|
|
24
|
+
}
|
|
25
|
+
async function main() {
|
|
26
|
+
const decisionsResult = await runDecisionsCheck();
|
|
27
|
+
const warning = formatDecisionsWarning(decisionsResult);
|
|
28
|
+
if (warning) {
|
|
29
|
+
console.error(warning);
|
|
85
30
|
}
|
|
86
31
|
process.exit(0);
|
|
87
32
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
33
|
+
if (!process.env.VITEST) {
|
|
34
|
+
main().catch((error) => {
|
|
35
|
+
console.error("[prim] Pre-commit hook error:", error);
|
|
36
|
+
process.exit(0);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
analyzeShellTargets,
|
|
4
|
+
buildCodexOutput,
|
|
5
|
+
buildCursorOutput,
|
|
6
|
+
buildHermesOutput,
|
|
7
|
+
buildHookOutput,
|
|
8
|
+
demoteForMode,
|
|
9
|
+
extractFileTargets,
|
|
10
|
+
failOpenCodex,
|
|
11
|
+
failOpenCursor,
|
|
12
|
+
failOpenHermes,
|
|
13
|
+
failOpenOutput,
|
|
14
|
+
readHookMode
|
|
15
|
+
} from "../chunk-MXVCP33E.js";
|
|
16
|
+
import {
|
|
17
|
+
appendCodexContext,
|
|
18
|
+
hasVisibleCodexMessage,
|
|
19
|
+
prepareCodexContext
|
|
20
|
+
} from "../chunk-C4D2LMTS.js";
|
|
21
|
+
import "../chunk-5HFTJEA6.js";
|
|
22
|
+
import {
|
|
23
|
+
DEFAULT_RULES,
|
|
24
|
+
scrub
|
|
25
|
+
} from "../chunk-MZEVEPL3.js";
|
|
26
|
+
import {
|
|
27
|
+
boundedHealthError,
|
|
28
|
+
cachedCollectScopeAdmits
|
|
29
|
+
} from "../chunk-GSBHBU7N.js";
|
|
30
|
+
import {
|
|
31
|
+
HttpError,
|
|
32
|
+
getClient
|
|
33
|
+
} from "../chunk-2IRLFOEL.js";
|
|
34
|
+
import {
|
|
35
|
+
normalizeEnvelope,
|
|
36
|
+
parseAgent,
|
|
37
|
+
readHookStdin,
|
|
38
|
+
shouldSuppressImportedCursorHandler
|
|
39
|
+
} from "../chunk-6C3CCVDI.js";
|
|
40
|
+
import "../chunk-K4JWJ6OM.js";
|
|
41
|
+
import "../chunk-5LRKJTLA.js";
|
|
42
|
+
import {
|
|
43
|
+
canonicalGitRoot,
|
|
44
|
+
canonicalRepositoryPath,
|
|
45
|
+
currentBranch,
|
|
46
|
+
githubRepositoryFullName,
|
|
47
|
+
isRepoActive,
|
|
48
|
+
packageVersion,
|
|
49
|
+
repoSyncId,
|
|
50
|
+
resolveAuthCredential
|
|
51
|
+
} from "../chunk-XG7S3LIL.js";
|
|
52
|
+
|
|
53
|
+
// src/hooks/preflight-v3.ts
|
|
54
|
+
import { Buffer } from "buffer";
|
|
55
|
+
var PREFLIGHT_PROTOCOL_VERSION = 3;
|
|
56
|
+
var PREFLIGHT_TIMEOUT_MS = 6500;
|
|
57
|
+
var MAX_PREFLIGHT_PATHS = 32;
|
|
58
|
+
var MAX_PROPOSAL_BYTES = 6144;
|
|
59
|
+
var MAX_CLIENT_VERSION_CHARS = 32;
|
|
60
|
+
var CLIENT_VERSION_RE = /^[A-Za-z0-9][A-Za-z0-9._+-]*$/;
|
|
61
|
+
var MAX_DECISION_DISCLOSURES = 16;
|
|
62
|
+
var DISCLOSED_DECISION_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;
|
|
63
|
+
var DISCLOSED_DECISION_SHORT_ID_RE = /^[0-9a-f]{8}$/u;
|
|
64
|
+
function boundedClientVersion(raw) {
|
|
65
|
+
return typeof raw === "string" && raw.length <= MAX_CLIENT_VERSION_CHARS && CLIENT_VERSION_RE.test(raw) ? raw : "unknown";
|
|
66
|
+
}
|
|
67
|
+
function resolvePreflightTargets(args) {
|
|
68
|
+
let rawPaths;
|
|
69
|
+
let coverage = "complete";
|
|
70
|
+
let mutation = "present";
|
|
71
|
+
let definite = false;
|
|
72
|
+
const shellTool = (args.agent === "claude_code" || args.agent === "codex") && args.toolName === "Bash" || args.agent === "cursor" && args.toolName === "Shell";
|
|
73
|
+
if (shellTool) {
|
|
74
|
+
const command = typeof args.toolInput === "string" ? args.toolInput : args.toolInput && typeof args.toolInput === "object" ? args.toolInput.command : void 0;
|
|
75
|
+
if (typeof command !== "string") {
|
|
76
|
+
return { paths: [], coverage: "unverified", mutation: "present" };
|
|
77
|
+
}
|
|
78
|
+
const shell = analyzeShellTargets(command);
|
|
79
|
+
rawPaths = shell.paths;
|
|
80
|
+
coverage = shell.coverage;
|
|
81
|
+
mutation = shell.mutation;
|
|
82
|
+
definite = shell.definiteEdit === true;
|
|
83
|
+
} else {
|
|
84
|
+
const extracted = extractFileTargets(args.toolName, args.toolInput, args.agent);
|
|
85
|
+
if (!extracted) return { paths: [], coverage: "complete", mutation: "none" };
|
|
86
|
+
rawPaths = extracted.paths;
|
|
87
|
+
if (!extracted.complete) coverage = "unverified";
|
|
88
|
+
definite = true;
|
|
89
|
+
}
|
|
90
|
+
const root = canonicalGitRoot(args.cwd);
|
|
91
|
+
const paths = /* @__PURE__ */ new Set();
|
|
92
|
+
for (const rawPath of rawPaths) {
|
|
93
|
+
const canonical = canonicalRepositoryPath(rawPath, args.cwd, root);
|
|
94
|
+
if (canonical) paths.add(canonical);
|
|
95
|
+
else coverage = "unverified";
|
|
96
|
+
}
|
|
97
|
+
const bounded = [...paths];
|
|
98
|
+
if (bounded.length > MAX_PREFLIGHT_PATHS) coverage = "unverified";
|
|
99
|
+
const resolved = bounded.slice(0, MAX_PREFLIGHT_PATHS);
|
|
100
|
+
if (definite && resolved.length === 0 && mutation === "present") {
|
|
101
|
+
return { paths: resolved, coverage: "unverified", mutation, definite: true };
|
|
102
|
+
}
|
|
103
|
+
return { paths: resolved, coverage, mutation };
|
|
104
|
+
}
|
|
105
|
+
function truncateUtf8(value, maxBytes) {
|
|
106
|
+
const encoded = Buffer.from(value);
|
|
107
|
+
if (encoded.byteLength <= maxBytes) return value;
|
|
108
|
+
return encoded.subarray(0, maxBytes).toString("utf8").replace(/\uFFFD$/u, "");
|
|
109
|
+
}
|
|
110
|
+
function proposalFor(toolInput) {
|
|
111
|
+
const redacted = scrub(toolInput, DEFAULT_RULES);
|
|
112
|
+
return truncateUtf8(JSON.stringify(redacted) ?? "", MAX_PROPOSAL_BYTES);
|
|
113
|
+
}
|
|
114
|
+
function parsePreflightResponse(value) {
|
|
115
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
116
|
+
const response = value;
|
|
117
|
+
const verdicts = /* @__PURE__ */ new Set(["allow", "warn", "ask", "block", "unavailable"]);
|
|
118
|
+
if (response.protocolVersion !== PREFLIGHT_PROTOCOL_VERSION || typeof response.verdict !== "string" || !verdicts.has(response.verdict) || typeof response.reasonCode !== "string" || typeof response.message !== "string" || !Array.isArray(response.conflicts) || !Array.isArray(response.bypassed)) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (response.checkId !== void 0 && (typeof response.checkId !== "string" || !DISCLOSED_DECISION_ID_RE.test(response.checkId)))
|
|
122
|
+
return null;
|
|
123
|
+
const disclosures = response.decisionDisclosures;
|
|
124
|
+
if (disclosures !== void 0) {
|
|
125
|
+
if (!Array.isArray(disclosures) || disclosures.length === 0 || disclosures.length > MAX_DECISION_DISCLOSURES) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const seen = /* @__PURE__ */ new Set();
|
|
129
|
+
for (const value2 of disclosures) {
|
|
130
|
+
if (!value2 || typeof value2 !== "object" || Array.isArray(value2)) return null;
|
|
131
|
+
const disclosure = value2;
|
|
132
|
+
if (Object.keys(disclosure).length !== 3 || !Object.hasOwn(disclosure, "decisionId") || !Object.hasOwn(disclosure, "shortId") || !Object.hasOwn(disclosure, "participation") || typeof disclosure.decisionId !== "string" || !DISCLOSED_DECISION_ID_RE.test(disclosure.decisionId) || typeof disclosure.shortId !== "string" || !DISCLOSED_DECISION_SHORT_ID_RE.test(disclosure.shortId) || disclosure.participation !== "candidate" && disclosure.participation !== "reconcile_bypass" || seen.has(disclosure.decisionId)) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
seen.add(disclosure.decisionId);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return response;
|
|
139
|
+
}
|
|
140
|
+
function decisionDisclosureContext(response) {
|
|
141
|
+
return response.decisionDisclosures?.map(({ decisionId }) => `[primitive] hidden Decision: prim decisions show ${decisionId}`).join("\n") ?? "";
|
|
142
|
+
}
|
|
143
|
+
async function requestPreflight(request, signal = AbortSignal.timeout(PREFLIGHT_TIMEOUT_MS)) {
|
|
144
|
+
const response = await getClient().post("/api/cli/decisions/conflict-check", request, {
|
|
145
|
+
signal,
|
|
146
|
+
quietRefresh: true
|
|
147
|
+
});
|
|
148
|
+
return parsePreflightResponse(response);
|
|
149
|
+
}
|
|
150
|
+
function resultForPreflight(response) {
|
|
151
|
+
const verdict = response.verdict === "block" ? "deny" : response.verdict;
|
|
152
|
+
const message = boundedHealthError(response.message) ?? "";
|
|
153
|
+
const unavailable = message || boundedHealthError(response.reasonCode);
|
|
154
|
+
const safe = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
|
155
|
+
const directives = response.conflicts.flatMap((value) => {
|
|
156
|
+
if (!value || typeof value !== "object") return [];
|
|
157
|
+
const { decisionId, shortId } = value;
|
|
158
|
+
const id = typeof shortId === "string" && safe.test(shortId) ? `dec_${shortId}` : typeof decisionId === "string" && safe.test(decisionId) ? decisionId : void 0;
|
|
159
|
+
if (!id) return [];
|
|
160
|
+
return [
|
|
161
|
+
`To reconcile, run: prim reconcile ${id}`,
|
|
162
|
+
...response.checkId && safe.test(response.checkId) ? [
|
|
163
|
+
`Check: ${response.checkId}. After resolving, record the confirmed outcome: prim reconcile ${id} --check ${response.checkId} --outcome followed (retained Decision), or --outcome superseded (after replacing it). A bypass alone is not a resolution.`
|
|
164
|
+
] : []
|
|
165
|
+
];
|
|
166
|
+
});
|
|
167
|
+
const reason = [message, ...new Set(directives)].filter(Boolean).join("\n");
|
|
168
|
+
const disclosureContext = decisionDisclosureContext(response);
|
|
169
|
+
return {
|
|
170
|
+
verdict,
|
|
171
|
+
conflicts: response.conflicts,
|
|
172
|
+
reason,
|
|
173
|
+
additionalContext: [
|
|
174
|
+
disclosureContext,
|
|
175
|
+
["warn", "ask", "block"].includes(response.verdict) ? reason : ""
|
|
176
|
+
].filter(Boolean).join("\n"),
|
|
177
|
+
truncated: false,
|
|
178
|
+
unavailable: response.verdict === "unavailable" ? unavailable : void 0
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function unverifiedResult(message) {
|
|
182
|
+
return {
|
|
183
|
+
verdict: "unavailable",
|
|
184
|
+
conflicts: [],
|
|
185
|
+
reason: "",
|
|
186
|
+
additionalContext: "",
|
|
187
|
+
truncated: false,
|
|
188
|
+
unavailable: message
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/hooks/pre-tool-use.ts
|
|
193
|
+
var STDIN_TIMEOUT_MS = 1e3;
|
|
194
|
+
var agent = parseAgent(process.argv);
|
|
195
|
+
async function emit(output) {
|
|
196
|
+
return await new Promise((resolve) => {
|
|
197
|
+
try {
|
|
198
|
+
process.stdout.write(`${JSON.stringify(output)}
|
|
199
|
+
`, (error) => resolve(!error));
|
|
200
|
+
} catch {
|
|
201
|
+
resolve(false);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async function emitWithAcknowledgment(output, acknowledge) {
|
|
206
|
+
const handedOff = await emit(output);
|
|
207
|
+
await acknowledge?.(handedOff);
|
|
208
|
+
}
|
|
209
|
+
function failOpen() {
|
|
210
|
+
if (agent === "hermes") return failOpenHermes();
|
|
211
|
+
if (agent === "cursor") return failOpenCursor();
|
|
212
|
+
return agent === "codex" ? failOpenCodex() : failOpenOutput();
|
|
213
|
+
}
|
|
214
|
+
function invocationId(envelope) {
|
|
215
|
+
const value = agent === "hermes" ? envelope.extra?.tool_call_id : envelope.tool_use_id;
|
|
216
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
217
|
+
}
|
|
218
|
+
function hermesAdvisory(result, aggregate) {
|
|
219
|
+
const parts = [];
|
|
220
|
+
if (aggregate === "warn") parts.push(result.reason);
|
|
221
|
+
if (result.verdict === "unavailable") {
|
|
222
|
+
parts.push(result.unavailable ?? result.reason);
|
|
223
|
+
}
|
|
224
|
+
if (result.additionalContext && result.additionalContext !== result.reason) {
|
|
225
|
+
parts.push(result.additionalContext);
|
|
226
|
+
}
|
|
227
|
+
const message = [...new Set(parts.filter(Boolean))].join("\n");
|
|
228
|
+
return message.startsWith("[primitive]") ? message : message ? `[primitive] ${message}` : "";
|
|
229
|
+
}
|
|
230
|
+
function preflightFailure(error) {
|
|
231
|
+
if (!(error instanceof HttpError && error.status === 401)) {
|
|
232
|
+
return "enforcement service unavailable; change was not verified";
|
|
233
|
+
}
|
|
234
|
+
if (resolveAuthCredential()?.source === "environment") {
|
|
235
|
+
return "PRIM_TOKEN was rejected; change was not verified";
|
|
236
|
+
}
|
|
237
|
+
return "authentication required; run `prim auth login`; change was not verified";
|
|
238
|
+
}
|
|
239
|
+
async function emitUnverified(message, envelope) {
|
|
240
|
+
const result = unverifiedResult(message);
|
|
241
|
+
if (agent === "hermes") {
|
|
242
|
+
process.stderr.write(`[primitive] ${message}
|
|
243
|
+
`);
|
|
244
|
+
await emit(failOpenHermes());
|
|
245
|
+
} else if (agent === "cursor") {
|
|
246
|
+
process.stderr.write(`[primitive] ${message}
|
|
247
|
+
`);
|
|
248
|
+
await emit(buildCursorOutput("allow", [result]));
|
|
249
|
+
} else {
|
|
250
|
+
let output = agent === "codex" ? buildCodexOutput("allow", [result]) : buildHookOutput("allow", [result]);
|
|
251
|
+
if (agent === "codex" && typeof envelope?.session_id === "string" && envelope.session_id) {
|
|
252
|
+
try {
|
|
253
|
+
const context = await prepareCodexContext({
|
|
254
|
+
cwd: envelope.cwd ?? process.cwd(),
|
|
255
|
+
sessionId: envelope.session_id,
|
|
256
|
+
includeDigest: false
|
|
257
|
+
});
|
|
258
|
+
output = appendCodexContext(output, context.context);
|
|
259
|
+
await emitWithAcknowledgment(output, context.acknowledge);
|
|
260
|
+
return;
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
await emit(output);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async function main() {
|
|
268
|
+
let raw;
|
|
269
|
+
try {
|
|
270
|
+
raw = await readHookStdin(STDIN_TIMEOUT_MS);
|
|
271
|
+
} catch {
|
|
272
|
+
await emit(failOpen());
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
let envelope;
|
|
276
|
+
try {
|
|
277
|
+
const incoming = JSON.parse(raw);
|
|
278
|
+
if (agent !== "cursor" && shouldSuppressImportedCursorHandler(incoming, "prim-pre-tool-use")) {
|
|
279
|
+
await emit(failOpenCursor());
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
envelope = normalizeEnvelope(incoming, agent);
|
|
283
|
+
} catch {
|
|
284
|
+
await emit(failOpen());
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (envelope.hook_event_name !== "PreToolUse") {
|
|
288
|
+
await emit(failOpen());
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const env = process.env;
|
|
292
|
+
const mode = readHookMode(env);
|
|
293
|
+
if (mode === "off") {
|
|
294
|
+
await emit(failOpen());
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const toolName = typeof envelope.tool_name === "string" ? envelope.tool_name : "";
|
|
298
|
+
const cwd = typeof envelope.cwd === "string" && envelope.cwd.length > 0 ? envelope.cwd : process.cwd();
|
|
299
|
+
const targets = resolvePreflightTargets({
|
|
300
|
+
toolName,
|
|
301
|
+
toolInput: envelope.tool_input,
|
|
302
|
+
agent,
|
|
303
|
+
cwd
|
|
304
|
+
});
|
|
305
|
+
if (targets.mutation === "none") {
|
|
306
|
+
await emit(failOpen());
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (!isRepoActive(cwd)) {
|
|
310
|
+
await emit(failOpen());
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (targets.paths.length === 0 && !targets.definite) {
|
|
314
|
+
await emit(failOpen());
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (agent === "cursor" && targets.coverage === "unverified") {
|
|
318
|
+
await emitUnverified("tool targets were ambiguous or outside the active repository", envelope);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const binding = repoSyncId(cwd);
|
|
322
|
+
const branch = currentBranch(cwd);
|
|
323
|
+
const sessionId = envelope.session_id;
|
|
324
|
+
const callId = invocationId(envelope);
|
|
325
|
+
if (!binding || typeof sessionId !== "string" || !sessionId || !callId) {
|
|
326
|
+
await emitUnverified(
|
|
327
|
+
"GitHub repo connection is required for repository-specific file attribution, Conflict Gate verification, and commit correlation; or tool invocation identity is unavailable",
|
|
328
|
+
envelope
|
|
329
|
+
);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const collectScopeAdmits = cachedCollectScopeAdmits(cwd, {
|
|
333
|
+
repository: githubRepositoryFullName(cwd) ?? void 0,
|
|
334
|
+
branch,
|
|
335
|
+
agent,
|
|
336
|
+
paths: targets.paths,
|
|
337
|
+
pathsComplete: targets.coverage === "complete"
|
|
338
|
+
});
|
|
339
|
+
const request = {
|
|
340
|
+
protocolVersion: PREFLIGHT_PROTOCOL_VERSION,
|
|
341
|
+
agent,
|
|
342
|
+
clientMode: mode,
|
|
343
|
+
clientVersion: boundedClientVersion(packageVersion()),
|
|
344
|
+
sessionId,
|
|
345
|
+
invocationId: callId,
|
|
346
|
+
repoSyncId: binding,
|
|
347
|
+
paths: targets.paths,
|
|
348
|
+
coverage: targets.coverage,
|
|
349
|
+
...branch === void 0 ? {} : { branch },
|
|
350
|
+
proposal: collectScopeAdmits ? proposalFor(envelope.tool_input) : ""
|
|
351
|
+
};
|
|
352
|
+
let result;
|
|
353
|
+
try {
|
|
354
|
+
const response = await requestPreflight(request);
|
|
355
|
+
if (!response) {
|
|
356
|
+
await emitUnverified("enforcement service returned an incompatible response", envelope);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
result = resultForPreflight(response);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
await emitUnverified(preflightFailure(error), envelope);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const aggregate = demoteForMode(
|
|
365
|
+
result.verdict === "unavailable" ? "allow" : result.verdict,
|
|
366
|
+
mode
|
|
367
|
+
);
|
|
368
|
+
if (agent === "hermes" && aggregate !== "deny" && aggregate !== "ask") {
|
|
369
|
+
const advisory = hermesAdvisory(result, aggregate);
|
|
370
|
+
if (advisory) process.stderr.write(`${advisory}
|
|
371
|
+
`);
|
|
372
|
+
}
|
|
373
|
+
let output = agent === "hermes" ? buildHermesOutput(aggregate, [result]) : agent === "cursor" ? buildCursorOutput(aggregate, [result]) : agent === "codex" ? buildCodexOutput(aggregate, [result]) : buildHookOutput(aggregate, [result]);
|
|
374
|
+
if (agent === "codex" && hasVisibleCodexMessage(output) && typeof sessionId === "string" && sessionId.length > 0) {
|
|
375
|
+
try {
|
|
376
|
+
const context = await prepareCodexContext({ cwd, sessionId, includeDigest: false });
|
|
377
|
+
output = appendCodexContext(output, context.context);
|
|
378
|
+
await emitWithAcknowledgment(output, context.acknowledge);
|
|
379
|
+
return;
|
|
380
|
+
} catch {
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
await emit(output);
|
|
384
|
+
}
|
|
385
|
+
main().catch(async () => {
|
|
386
|
+
await emit(failOpen());
|
|
387
|
+
});
|