@primitive.ai/prim 0.1.0-alpha.56 → 0.1.0-alpha.58

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.
@@ -1,226 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
- warmBinCache
4
- } from "../chunk-HSZPTVKU.js";
5
- import {
6
- isRepoActiveForCapture
7
- } from "../chunk-R5ZJRSLV.js";
3
+ PREFLIGHT_PROTOCOL_VERSION,
4
+ buildCodexOutput,
5
+ buildHermesOutput,
6
+ buildHookOutput,
7
+ demoteForMode,
8
+ failOpenCodex,
9
+ failOpenHermes,
10
+ failOpenOutput,
11
+ proposalFor,
12
+ readHookMode,
13
+ requestPreflight,
14
+ resolvePreflightTargets,
15
+ resultForPreflight,
16
+ unverifiedResult
17
+ } from "../chunk-YWJHWGXH.js";
18
+ import "../chunk-GUXBUBQ7.js";
8
19
  import {
9
- getClient,
10
- getSiteUrl
11
- } from "../chunk-DWDEQRWN.js";
20
+ warmBinCache
21
+ } from "../chunk-YIMUIPMD.js";
12
22
  import {
13
- daemonRequest
14
- } from "../chunk-UTKQTZHL.js";
23
+ isRepoActive,
24
+ repoSyncId
25
+ } from "../chunk-LDQZASIF.js";
26
+ import "../chunk-I3HIPAIR.js";
15
27
  import {
16
28
  normalizeEnvelope,
17
29
  parseAgent
18
30
  } from "../chunk-TCDUH7AN.js";
19
31
 
20
- // src/hooks/pre-tool-use-scoring.ts
21
- import { isAbsolute, relative, sep } from "path";
22
- var VERDICT_SEVERITY = {
23
- allow: 0,
24
- warn: 1,
25
- ask: 2,
26
- deny: 3
27
- };
28
- function toRepoRelative(filePath, cwd) {
29
- const rel = isAbsolute(filePath) ? relative(cwd, filePath) : filePath;
30
- return sep === "/" ? rel : rel.split(sep).join("/");
31
- }
32
- function aggregateCheckResults(results) {
33
- let worst = "allow";
34
- for (const r of results) {
35
- if (r.verdict !== "unavailable" && VERDICT_SEVERITY[r.verdict] > VERDICT_SEVERITY[worst]) {
36
- worst = r.verdict;
37
- }
38
- }
39
- return worst;
40
- }
41
- function anyUnverified(results) {
42
- return results.some((r) => r.verdict === "unavailable" || r.truncated);
43
- }
44
- function unverifiedNote(results) {
45
- const causes = [];
46
- const unavailable = results.find((r) => r.verdict === "unavailable");
47
- if (unavailable) {
48
- causes.push(
49
- unavailable.unavailable ? `decision check skipped \u2014 ${unavailable.unavailable}` : "decision check skipped \u2014 not verified"
50
- );
51
- }
52
- if (results.some((r) => r.truncated)) {
53
- causes.push("decision check partial \u2014 conflict set truncated (per-file cap hit)");
54
- }
55
- return causes.map((c) => `[primitive] ${c}`).join("\n");
56
- }
57
- function buildHookOutput(aggregate, results, agent2 = "claude_code") {
58
- if (aggregate === "deny") {
59
- const reason = results.filter((r) => r.verdict === "deny").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n") || "[primitive] conflict detected (no detail available)";
60
- return {
61
- hookSpecificOutput: {
62
- hookEventName: "PreToolUse",
63
- permissionDecision: "deny",
64
- permissionDecisionReason: reason
65
- }
66
- };
67
- }
68
- if (agent2 === "codex" && aggregate === "ask") {
69
- const reason = results.filter((r) => r.verdict === "ask" || r.verdict === "deny").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n");
70
- const context = results.map((r) => r.additionalContext).filter((s) => s.length > 0).join("\n");
71
- const merged = [reason, context].filter((s) => s.length > 0).join("\n\n");
72
- const out = {
73
- hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow" }
74
- };
75
- if (merged.length > 0) {
76
- out.hookSpecificOutput.additionalContext = merged;
77
- }
78
- return out;
79
- }
80
- if (aggregate === "ask") {
81
- const reason = results.filter((r) => r.verdict === "ask" || r.verdict === "deny").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n") || "[primitive] please confirm this edit";
82
- const additionalContext = results.map((r) => r.additionalContext).filter((s) => s.length > 0).join("\n");
83
- const out = {
84
- hookSpecificOutput: {
85
- hookEventName: "PreToolUse",
86
- permissionDecision: "ask",
87
- permissionDecisionReason: reason
88
- }
89
- };
90
- if (additionalContext.length > 0) {
91
- out.hookSpecificOutput.additionalContext = additionalContext;
92
- }
93
- return out;
94
- }
95
- const notes = [
96
- ...results.map((r) => r.additionalContext).filter((s) => s.length > 0),
97
- anyUnverified(results) ? unverifiedNote(results) : ""
98
- ].filter((s) => s.length > 0).join("\n");
99
- if (notes.length > 0) {
100
- return {
101
- hookSpecificOutput: {
102
- hookEventName: "PreToolUse",
103
- permissionDecision: "allow",
104
- additionalContext: notes
105
- }
106
- };
107
- }
108
- return {
109
- hookSpecificOutput: {
110
- hookEventName: "PreToolUse",
111
- permissionDecision: "allow"
112
- }
113
- };
114
- }
115
- function failOpenOutput() {
116
- return {
117
- hookSpecificOutput: {
118
- hookEventName: "PreToolUse",
119
- permissionDecision: "allow"
120
- }
121
- };
122
- }
123
- function buildHermesOutput(aggregate, results) {
124
- if (aggregate !== "deny" && aggregate !== "ask") {
125
- return {};
126
- }
127
- const reason = results.filter((r) => r.verdict === "deny" || r.verdict === "ask").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n");
128
- const directive = results.map((r) => r.additionalContext).filter((s) => s.length > 0).join("\n");
129
- const message = [reason, directive].filter((s) => s.length > 0).join("\n\n") || "[primitive] conflict detected (no detail available)";
130
- return { action: "block", message };
131
- }
132
- function failOpenHermes() {
133
- return {};
134
- }
135
- var SUPPORTED_TOOLS = /* @__PURE__ */ new Set(["Edit", "Write", "MultiEdit"]);
136
- var APPLY_PATCH_FILE_RE = /^\*\*\* (?:Update|Add|Delete) File: (?<path>.+)$/;
137
- var MOVE_FILE_RE = /^\*\*\* Move File:\s*(?<src>.+?)\s*->\s*(?<dst>.+?)\s*$/;
138
- var LINE_SPLIT_RE = /\r?\n/;
139
- function parseApplyPatchPaths(command) {
140
- const paths = /* @__PURE__ */ new Set();
141
- for (const line of command.split(LINE_SPLIT_RE)) {
142
- const path = APPLY_PATCH_FILE_RE.exec(line)?.groups?.path?.trim();
143
- if (path) {
144
- paths.add(path);
145
- continue;
146
- }
147
- const move = MOVE_FILE_RE.exec(line)?.groups;
148
- if (move?.src) {
149
- paths.add(move.src.trim());
150
- if (move.dst) {
151
- paths.add(move.dst.trim());
152
- }
153
- }
154
- }
155
- return Array.from(paths);
156
- }
157
- function extractCodexFilePaths(toolName, toolInput) {
158
- if (toolName !== "apply_patch") {
159
- return [];
160
- }
161
- if (!toolInput || typeof toolInput !== "object") {
162
- return [];
163
- }
164
- const command = toolInput.command;
165
- return typeof command === "string" ? parseApplyPatchPaths(command) : [];
166
- }
167
- var HERMES_EDITING_TOOLS = /* @__PURE__ */ new Set(["write_file", "patch"]);
168
- function extractHermesFilePaths(toolName, toolInput) {
169
- if (!HERMES_EDITING_TOOLS.has(toolName)) {
170
- return [];
171
- }
172
- if (!toolInput || typeof toolInput !== "object") {
173
- return [];
174
- }
175
- const input = toolInput;
176
- if (toolName === "patch" && input.mode === "patch") {
177
- return typeof input.patch === "string" ? parseApplyPatchPaths(input.patch) : [];
178
- }
179
- return typeof input.path === "string" && input.path.length > 0 ? [input.path] : [];
180
- }
181
- function extractFilePaths(toolName, toolInput, agent2 = "claude_code") {
182
- if (agent2 === "codex") {
183
- return extractCodexFilePaths(toolName, toolInput);
184
- }
185
- if (agent2 === "hermes") {
186
- return extractHermesFilePaths(toolName, toolInput);
187
- }
188
- if (!SUPPORTED_TOOLS.has(toolName)) {
189
- return [];
190
- }
191
- if (!toolInput || typeof toolInput !== "object") {
192
- return [];
193
- }
194
- const input = toolInput;
195
- if (typeof input.file_path === "string" && input.file_path.length > 0) {
196
- return [input.file_path];
197
- }
198
- return [];
199
- }
200
- function readHookMode(env) {
201
- if (env.PRIM_BYPASS === "1" || env.PRIM_BYPASS === "true") {
202
- return "off";
203
- }
204
- const mode = env.PRIM_HOOK_MODE;
205
- if (mode === "off" || mode === "warn") {
206
- return mode;
207
- }
208
- return "block";
209
- }
210
- function demoteForMode(verdict, mode) {
211
- if (mode === "off") {
212
- return "allow";
213
- }
214
- if (mode === "warn" && (verdict === "ask" || verdict === "deny")) {
215
- return "warn";
216
- }
217
- return verdict;
218
- }
219
-
220
32
  // src/hooks/pre-tool-use.ts
221
- var HOOK_TIMEOUT_MS = 4500;
222
33
  var STDIN_TIMEOUT_MS = 1e3;
223
- var DAEMON_TIMEOUT_MS = 250;
224
34
  async function readStdin() {
225
35
  return new Promise((resolve, reject) => {
226
36
  const chunks = [];
@@ -244,25 +54,24 @@ function emit(output) {
244
54
  `);
245
55
  }
246
56
  function failOpen() {
247
- return agent === "hermes" ? failOpenHermes() : failOpenOutput();
57
+ if (agent === "hermes") return failOpenHermes();
58
+ return agent === "codex" ? failOpenCodex() : failOpenOutput();
248
59
  }
249
- async function checkOneFile(file) {
250
- const fromDaemon = await daemonRequest(
251
- "conflict_check",
252
- // callerEnv lets a staging-bound daemon refuse a prod-context gate check
253
- // (and vice versa) so we fall through to a direct call against our own env.
254
- { file, callerEnv: getSiteUrl() },
255
- { timeoutMs: DAEMON_TIMEOUT_MS }
256
- );
257
- if (fromDaemon) {
258
- return fromDaemon;
60
+ function invocationId(envelope) {
61
+ const value = agent === "hermes" ? envelope.extra?.tool_call_id : envelope.tool_use_id;
62
+ return typeof value === "string" && value.length > 0 ? value : void 0;
63
+ }
64
+ function emitUnverified(message) {
65
+ const result = unverifiedResult(message);
66
+ if (agent === "hermes") {
67
+ process.stderr.write(`[primitive] ${message}
68
+ `);
69
+ emit(failOpenHermes());
70
+ } else {
71
+ emit(
72
+ agent === "codex" ? buildCodexOutput("allow", [result]) : buildHookOutput("allow", [result])
73
+ );
259
74
  }
260
- const client = getClient();
261
- return await client.post(
262
- "/api/cli/decisions/conflict-check",
263
- { file },
264
- { signal: AbortSignal.timeout(HOOK_TIMEOUT_MS) }
265
- );
266
75
  }
267
76
  async function main() {
268
77
  warmBinCache();
@@ -295,28 +104,63 @@ async function main() {
295
104
  }
296
105
  const toolName = typeof envelope.tool_name === "string" ? envelope.tool_name : "";
297
106
  const cwd = typeof envelope.cwd === "string" && envelope.cwd.length > 0 ? envelope.cwd : process.cwd();
298
- const files = extractFilePaths(toolName, envelope.tool_input, agent).map(
299
- (f) => toRepoRelative(f, cwd)
300
- );
301
- if (files.length === 0) {
107
+ const targets = resolvePreflightTargets({
108
+ toolName,
109
+ toolInput: envelope.tool_input,
110
+ agent,
111
+ cwd
112
+ });
113
+ if (targets.mutation === "none") {
302
114
  emit(failOpen());
303
115
  return;
304
116
  }
305
- if (!isRepoActiveForCapture(cwd)) {
117
+ if (!isRepoActive(cwd)) {
306
118
  emit(failOpen());
307
119
  return;
308
120
  }
309
- let results;
121
+ if (targets.paths.length === 0) {
122
+ emitUnverified("mutation targets could not be determined; enforcement not verified");
123
+ return;
124
+ }
125
+ const binding = repoSyncId(cwd);
126
+ const sessionId = envelope.session_id;
127
+ const callId = invocationId(envelope);
128
+ if (!binding || typeof sessionId !== "string" || !sessionId || !callId) {
129
+ emitUnverified("repository binding or tool invocation identity is unavailable");
130
+ return;
131
+ }
132
+ const request = {
133
+ protocolVersion: PREFLIGHT_PROTOCOL_VERSION,
134
+ agent,
135
+ sessionId,
136
+ invocationId: callId,
137
+ repoSyncId: binding,
138
+ paths: targets.paths,
139
+ coverage: targets.coverage,
140
+ proposal: proposalFor(envelope.tool_input)
141
+ };
142
+ let result;
310
143
  try {
311
- results = await Promise.all(files.map((f) => checkOneFile(f)));
144
+ const response = await requestPreflight(request);
145
+ if (!response) {
146
+ emitUnverified("enforcement service returned an incompatible response");
147
+ return;
148
+ }
149
+ result = resultForPreflight(response);
312
150
  } catch {
313
- emit(failOpen());
151
+ emitUnverified("enforcement service unavailable; change was not verified");
314
152
  return;
315
153
  }
316
- const rawAggregate = aggregateCheckResults(results);
317
- const aggregate = demoteForMode(rawAggregate, mode);
154
+ const aggregate = demoteForMode(
155
+ result.verdict === "unavailable" ? "allow" : result.verdict,
156
+ mode
157
+ );
158
+ if (agent === "hermes" && (aggregate === "warn" || result.verdict === "unavailable")) {
159
+ process.stderr.write(`[primitive] ${result.reason || result.unavailable}
160
+ `);
161
+ }
318
162
  emit(
319
- agent === "hermes" ? buildHermesOutput(aggregate, results) : buildHookOutput(aggregate, results, agent)
163
+ agent === "hermes" ? buildHermesOutput(aggregate, [result]) : agent === "codex" ? buildCodexOutput(aggregate, [result]) : buildHookOutput(aggregate, [result])
320
164
  );
321
165
  }
322
166
  main().catch(() => {
@@ -1,15 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
- scrubFromCwd
4
- } from "../chunk-6LAQVM26.js";
5
- import {
3
+ postToolInvocationId,
6
4
  shouldFlushAfter,
7
5
  toMove
8
- } from "../chunk-F5QCFBJ6.js";
6
+ } from "../chunk-YDBZTFW5.js";
9
7
  import {
10
8
  appendMove,
11
9
  resolveOrg
12
- } from "../chunk-BOTKPLTI.js";
10
+ } from "../chunk-S4LFRVGD.js";
11
+ import {
12
+ scrubFromCwd
13
+ } from "../chunk-GUXBUBQ7.js";
13
14
  import {
14
15
  buildHookOutput,
15
16
  handoffHookOutput
@@ -19,17 +20,17 @@ import {
19
20
  acknowledgeDecisionFeedback,
20
21
  leaseDecisionFeedback,
21
22
  renderFeedback
22
- } from "../chunk-WELBNODJ.js";
23
+ } from "../chunk-R4CUX6FY.js";
23
24
  import {
24
25
  getOrCreateWorkspaceId
25
26
  } from "../chunk-IMAIBPUC.js";
26
27
  import {
27
28
  warmBinCache
28
- } from "../chunk-HSZPTVKU.js";
29
+ } from "../chunk-YIMUIPMD.js";
29
30
  import {
30
31
  isRepoActiveForCapture
31
- } from "../chunk-R5ZJRSLV.js";
32
- import "../chunk-DWDEQRWN.js";
32
+ } from "../chunk-LDQZASIF.js";
33
+ import "../chunk-I3HIPAIR.js";
33
34
  import {
34
35
  normalizeEnvelope,
35
36
  parseAgent
@@ -90,7 +91,8 @@ async function main() {
90
91
  const identity = getOrCreateWorkspaceId(cwd);
91
92
  const workspaceId = identity.status === "ready" ? identity.workspaceId : void 0;
92
93
  try {
93
- const base = toMove(parsed, resolveCliVersion(), agent, workspaceId);
94
+ const invocationId = postToolInvocationId(parsed, agent);
95
+ const base = toMove(parsed, resolveCliVersion(), agent, workspaceId, invocationId);
94
96
  const move = { ...base, payload: scrubFromCwd(parsed, cwd) };
95
97
  const { orgId } = resolveOrg({ sessionId: move.sessionId, cwd: move.env.cwd });
96
98
  appendMove(move, orgId);
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  hasUsableCodexGuidance,
4
4
  refreshClaudePlugins
5
- } from "../chunk-ROL7SDXX.js";
5
+ } from "../chunk-UBEBEBLM.js";
6
6
  import {
7
7
  buildHookOutput,
8
8
  handoffHookOutput
@@ -12,22 +12,22 @@ import {
12
12
  acknowledgeDecisionFeedback,
13
13
  leaseDecisionFeedback,
14
14
  renderFeedback
15
- } from "../chunk-WELBNODJ.js";
15
+ } from "../chunk-R4CUX6FY.js";
16
16
  import {
17
17
  getOrCreateWorkspaceId
18
18
  } from "../chunk-IMAIBPUC.js";
19
19
  import {
20
20
  binFile,
21
21
  warmBinCache
22
- } from "../chunk-HSZPTVKU.js";
22
+ } from "../chunk-YIMUIPMD.js";
23
23
  import {
24
24
  gitToplevel,
25
25
  isRepoActiveForCapture
26
- } from "../chunk-R5ZJRSLV.js";
26
+ } from "../chunk-LDQZASIF.js";
27
27
  import {
28
28
  getSiteUrl,
29
29
  isSessionEnded
30
- } from "../chunk-DWDEQRWN.js";
30
+ } from "../chunk-I3HIPAIR.js";
31
31
  import {
32
32
  daemonRequest
33
33
  } from "../chunk-UTKQTZHL.js";