@primitive.ai/prim 0.1.0-alpha.12 → 0.1.0-alpha.120

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.
Files changed (56) hide show
  1. package/README.md +540 -38
  2. package/SKILL.md +315 -75
  3. package/assets/primitive-gameplan/SKILL.md +134 -0
  4. package/assets/primitive-gameplan/claude-hooks.json +28 -0
  5. package/assets/primitive-gameplan/scripts/activate.mjs +37 -0
  6. package/dist/chunk-2AKDX4RU.js +209 -0
  7. package/dist/chunk-4GLVJLEQ.js +55 -0
  8. package/dist/chunk-5BQ2NEMP.js +22695 -0
  9. package/dist/chunk-A6M2M5A2.js +36 -0
  10. package/dist/chunk-CCGFRNAA.js +1871 -0
  11. package/dist/chunk-D235RJNO.js +164 -0
  12. package/dist/chunk-EYBE724K.js +135 -0
  13. package/dist/chunk-EZXEWFDG.js +210 -0
  14. package/dist/chunk-GYQOZGGF.js +168 -0
  15. package/dist/chunk-HXICZG5J.js +503 -0
  16. package/dist/chunk-JSBRDJBE.js +30 -0
  17. package/dist/chunk-KZB4ABIB.js +13 -0
  18. package/dist/chunk-L52OD62C.js +1444 -0
  19. package/dist/chunk-LBCPRHNG.js +175 -0
  20. package/dist/chunk-LTTF4MBE.js +439 -0
  21. package/dist/chunk-MWNRXAEB.js +1572 -0
  22. package/dist/chunk-NQFRQ56V.js +262 -0
  23. package/dist/chunk-OYHYCK4K.js +13 -0
  24. package/dist/chunk-P2O7NLVN.js +1341 -0
  25. package/dist/chunk-R2NUH4B7.js +96 -0
  26. package/dist/chunk-SLDZJD6Q.js +157 -0
  27. package/dist/chunk-TC4FTRYO.js +253 -0
  28. package/dist/chunk-U67RBNIZ.js +844 -0
  29. package/dist/chunk-VOC4KOOP.js +258 -0
  30. package/dist/chunk-YTF6OCF3.js +459 -0
  31. package/dist/daemon/server.js +26444 -0
  32. package/dist/hook-runtime/daemon/server.js +26445 -0
  33. package/dist/hook-runtime/hooks/post-commit.js +23836 -0
  34. package/dist/hook-runtime/hooks/post-rewrite.js +23735 -0
  35. package/dist/hook-runtime/hooks/post-tool-use.js +36151 -0
  36. package/dist/hook-runtime/hooks/pre-commit.js +1737 -0
  37. package/dist/hook-runtime/hooks/pre-tool-use-worker.js +35067 -0
  38. package/dist/hook-runtime/hooks/pre-tool-use.js +96 -0
  39. package/dist/hook-runtime/hooks/prim-hook.js +36211 -0
  40. package/dist/hook-runtime/hooks/session-end.js +1188 -0
  41. package/dist/hook-runtime/hooks/session-start.js +33950 -0
  42. package/dist/hook-runtime/index.js +71245 -0
  43. package/dist/hook-runtime/statusline-main.js +892 -0
  44. package/dist/hooks/post-commit.js +197 -0
  45. package/dist/hooks/post-rewrite.js +206 -0
  46. package/dist/hooks/post-tool-use.js +295 -0
  47. package/dist/hooks/pre-commit.js +16 -108
  48. package/dist/hooks/pre-tool-use-worker.js +238 -0
  49. package/dist/hooks/pre-tool-use.js +87 -0
  50. package/dist/hooks/prim-hook.js +245 -0
  51. package/dist/hooks/session-end.js +67 -0
  52. package/dist/hooks/session-start.js +312 -0
  53. package/dist/index.js +10998 -572
  54. package/dist/statusline-main.js +891 -0
  55. package/package.json +49 -16
  56. package/dist/chunk-3APLWTLB.js +0 -165
@@ -0,0 +1,96 @@
1
+ import {
2
+ RUN_ID,
3
+ demoArgv,
4
+ demoCommand,
5
+ exclusionPath,
6
+ lockRun,
7
+ observeRun,
8
+ readRun,
9
+ runPath,
10
+ saveDemoJson,
11
+ saveRun,
12
+ sessionExcluded,
13
+ sessionKey
14
+ } from "./chunk-L52OD62C.js";
15
+ import {
16
+ packageVersion
17
+ } from "./chunk-P2O7NLVN.js";
18
+
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
+ export {
95
+ observeDemoInvocation
96
+ };
@@ -0,0 +1,157 @@
1
+ import {
2
+ decodeJwtPayload,
3
+ primConfigDirectory,
4
+ resolveAuthCredential
5
+ } from "./chunk-P2O7NLVN.js";
6
+
7
+ // src/daemon/principal.ts
8
+ import { createHash } from "crypto";
9
+ var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
10
+ function boundedClaim(value) {
11
+ return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
12
+ }
13
+ function sha256(value) {
14
+ return createHash("sha256").update(value).digest("hex");
15
+ }
16
+ function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
17
+ if (!token) return void 0;
18
+ const payload = decodeJwtPayload(token);
19
+ const subject = boundedClaim(payload?.sub);
20
+ const organizationId = boundedClaim(payload?.org_id);
21
+ if (!(subject && organizationId)) return void 0;
22
+ const issuer = boundedClaim(payload?.iss) ?? "unknown-issuer";
23
+ return {
24
+ principalId: sha256(`${issuer}\0${subject}`),
25
+ organizationId,
26
+ credentialFingerprint: sha256(token)
27
+ };
28
+ }
29
+ function daemonPrincipalsMatch(caller, daemon) {
30
+ return caller !== void 0 && daemon !== void 0 && caller.principalId === daemon.principalId && caller.organizationId === daemon.organizationId && caller.credentialFingerprint === daemon.credentialFingerprint;
31
+ }
32
+
33
+ // src/daemon/protocol.ts
34
+ var DAEMON_JSON_REQUEST_MAX_BYTES = 64 * 1024;
35
+ var DAEMON_JSON_RESPONSE_MAX_BYTES = 8 * 1024 * 1024;
36
+ var DECISION_IDENTITY_VERSION = 7;
37
+ function isRecord(value) {
38
+ return typeof value === "object" && value !== null && !Array.isArray(value);
39
+ }
40
+ function parseDaemonResponseEnvelope(value) {
41
+ if (!isRecord(value)) return void 0;
42
+ if (!(typeof value.id === "number" && Number.isSafeInteger(value.id) && value.id >= 0)) {
43
+ return void 0;
44
+ }
45
+ if (typeof value.ok !== "boolean") return void 0;
46
+ if (value.error !== void 0 && typeof value.error !== "string") return void 0;
47
+ return {
48
+ id: value.id,
49
+ ...typeof value.decisionIdentityVersion === "number" ? { decisionIdentityVersion: value.decisionIdentityVersion } : {},
50
+ ok: value.ok,
51
+ ...value.result === void 0 ? {} : { result: value.result },
52
+ ...value.error === void 0 ? {} : { error: value.error }
53
+ };
54
+ }
55
+
56
+ // src/daemon/client.ts
57
+ import { createConnection } from "net";
58
+ import { join } from "path";
59
+ var SOCK_PATH = join(primConfigDirectory(), "sock");
60
+ var DEFAULT_TIMEOUT_MS = 250;
61
+ var nextRequestId = 1;
62
+ function daemonRequest(method, params = {}, opts = {}) {
63
+ const id = nextRequestId++;
64
+ const caller = resolveDaemonPrincipal();
65
+ const request = {
66
+ id,
67
+ method,
68
+ params,
69
+ ...caller ? { caller } : {}
70
+ };
71
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
72
+ return new Promise((resolve) => {
73
+ let settled = false;
74
+ let socket;
75
+ let buffer = Buffer.alloc(0);
76
+ const settle = (value) => {
77
+ if (settled) {
78
+ return;
79
+ }
80
+ settled = true;
81
+ if (socket) {
82
+ try {
83
+ socket.destroy();
84
+ } catch {
85
+ }
86
+ }
87
+ resolve(value);
88
+ };
89
+ const timer = setTimeout(() => settle(null), timeoutMs);
90
+ timer.unref();
91
+ try {
92
+ socket = createConnection(SOCK_PATH);
93
+ } catch {
94
+ clearTimeout(timer);
95
+ settle(null);
96
+ return;
97
+ }
98
+ socket.on("error", () => {
99
+ clearTimeout(timer);
100
+ settle(null);
101
+ });
102
+ socket.on("connect", () => {
103
+ try {
104
+ socket?.write(`${JSON.stringify(request)}
105
+ `);
106
+ } catch {
107
+ clearTimeout(timer);
108
+ settle(null);
109
+ }
110
+ });
111
+ socket.on("data", (chunk) => {
112
+ buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
113
+ if (buffer.length > DAEMON_JSON_RESPONSE_MAX_BYTES) {
114
+ clearTimeout(timer);
115
+ settle(null);
116
+ return;
117
+ }
118
+ const newlineIdx = buffer.indexOf(10);
119
+ if (newlineIdx === -1) {
120
+ return;
121
+ }
122
+ const line = buffer.subarray(0, newlineIdx).toString("utf8");
123
+ try {
124
+ const res = parseDaemonResponseEnvelope(JSON.parse(line));
125
+ if (!res || res.id !== id) {
126
+ clearTimeout(timer);
127
+ settle(null);
128
+ return;
129
+ }
130
+ clearTimeout(timer);
131
+ settle(
132
+ (!(method.startsWith("decisions_") || method.startsWith("decision_")) || res.decisionIdentityVersion === DECISION_IDENTITY_VERSION) && res.ok && res.result !== void 0 ? res.result : null
133
+ );
134
+ } catch {
135
+ clearTimeout(timer);
136
+ settle(null);
137
+ }
138
+ });
139
+ socket.on("end", () => {
140
+ if (!settled) {
141
+ clearTimeout(timer);
142
+ settle(null);
143
+ }
144
+ });
145
+ });
146
+ }
147
+ async function daemonIsLive(timeoutMs = DEFAULT_TIMEOUT_MS) {
148
+ const result = await daemonRequest("ping", {}, { timeoutMs });
149
+ return result?.pong === true;
150
+ }
151
+
152
+ export {
153
+ resolveDaemonPrincipal,
154
+ daemonPrincipalsMatch,
155
+ daemonRequest,
156
+ daemonIsLive
157
+ };
@@ -0,0 +1,253 @@
1
+ import {
2
+ cursorHooksPath,
3
+ hasExactCursorHandler,
4
+ readCursorHooks
5
+ } from "./chunk-MWNRXAEB.js";
6
+ import {
7
+ resolveRepositoryContext
8
+ } from "./chunk-P2O7NLVN.js";
9
+
10
+ // src/hooks/cursor-coexistence.ts
11
+ var CURSOR_EVENTS = /* @__PURE__ */ new Set([
12
+ "sessionStart",
13
+ "beforeSubmitPrompt",
14
+ "preToolUse",
15
+ "postToolUse",
16
+ "postToolUseFailure",
17
+ "afterAgentResponse",
18
+ "subagentStop",
19
+ "stop",
20
+ "sessionEnd"
21
+ ]);
22
+ function cursorEvidence(parsed) {
23
+ return typeof parsed.cursor_version === "string" && parsed.cursor_version.length > 0 && typeof parsed.conversation_id === "string" && parsed.conversation_id.length > 0 && typeof parsed.generation_id === "string" && parsed.generation_id.length > 0 && Array.isArray(parsed.workspace_roots) && typeof parsed.hook_event_name === "string" && CURSOR_EVENTS.has(parsed.hook_event_name);
24
+ }
25
+ function uniqueProjectRoot(parsed) {
26
+ if (!Array.isArray(parsed.workspace_roots)) return;
27
+ const roots = new Set(
28
+ parsed.workspace_roots.filter((value) => typeof value === "string" && value.length > 0).map((value) => resolveRepositoryContext(value)?.repoRoot).filter((value) => !!value)
29
+ );
30
+ return roots.size === 1 ? [...roots][0] : void 0;
31
+ }
32
+ function shouldSuppressImportedCursorHandler(parsed, bin) {
33
+ if (!cursorEvidence(parsed)) return false;
34
+ const project = uniqueProjectRoot(parsed);
35
+ if (project) {
36
+ try {
37
+ if (hasExactCursorHandler(
38
+ readCursorHooks(cursorHooksPath("project", project)),
39
+ parsed.hook_event_name,
40
+ bin,
41
+ "project"
42
+ )) {
43
+ return true;
44
+ }
45
+ } catch {
46
+ }
47
+ }
48
+ try {
49
+ return hasExactCursorHandler(
50
+ readCursorHooks(cursorHooksPath("user")),
51
+ parsed.hook_event_name,
52
+ bin,
53
+ "user"
54
+ );
55
+ } catch {
56
+ return false;
57
+ }
58
+ }
59
+
60
+ // src/hooks/hook-stdin.ts
61
+ var DEFAULT_MAX_BYTES = 1024 * 1024;
62
+ function readHookStdin(timeoutMs = 1e3, maxBytes = DEFAULT_MAX_BYTES) {
63
+ return new Promise((resolve2, reject) => {
64
+ const chunks = [];
65
+ let bytes = 0;
66
+ let settled = false;
67
+ const finish = (error) => {
68
+ if (settled) return;
69
+ settled = true;
70
+ clearTimeout(timer);
71
+ process.stdin.removeAllListeners("data");
72
+ process.stdin.removeAllListeners("end");
73
+ process.stdin.removeAllListeners("error");
74
+ if (error) reject(error);
75
+ else resolve2(Buffer.concat(chunks, bytes).toString("utf-8"));
76
+ };
77
+ const timer = setTimeout(() => finish(new Error("stdin read timeout")), timeoutMs);
78
+ process.stdin.on("data", (chunk) => {
79
+ bytes += chunk.length;
80
+ if (bytes > maxBytes) {
81
+ finish(new Error("stdin envelope exceeds size limit"));
82
+ return;
83
+ }
84
+ chunks.push(chunk);
85
+ });
86
+ process.stdin.on("end", () => finish());
87
+ process.stdin.on("error", (error) => finish(error));
88
+ });
89
+ }
90
+
91
+ // src/hooks/normalize.ts
92
+ import { createHash } from "crypto";
93
+ import { realpathSync } from "fs";
94
+ import { isAbsolute, resolve } from "path";
95
+ var HERMES_EVENT_MAP = {
96
+ on_session_start: "SessionStart",
97
+ on_session_end: "SessionEnd",
98
+ pre_llm_call: "UserPromptSubmit",
99
+ post_llm_call: "Stop",
100
+ pre_tool_call: "PreToolUse",
101
+ post_tool_call: "PostToolUse",
102
+ subagent_stop: "SubagentStop"
103
+ };
104
+ var CURSOR_EVENT_MAP = {
105
+ sessionStart: "SessionStart",
106
+ beforeSubmitPrompt: "UserPromptSubmit",
107
+ preToolUse: "PreToolUse",
108
+ postToolUse: "PostToolUse",
109
+ postToolUseFailure: "PostToolUseFailure",
110
+ afterAgentResponse: "AssistantResponse",
111
+ subagentStop: "SubagentStop",
112
+ stop: "Stop",
113
+ sessionEnd: "SessionEnd"
114
+ };
115
+ var CURSOR_TOOL_EVENTS = /* @__PURE__ */ new Set(["PreToolUse", "PostToolUse", "PostToolUseFailure"]);
116
+ var SAFE_CORRELATION_ID_RE = /^[A-Za-z0-9._:-]{1,128}$/u;
117
+ var CURSOR_LOCAL_ONLY_FIELDS = /* @__PURE__ */ new Set([
118
+ "conversation_id",
119
+ "generation_id",
120
+ "user_email",
121
+ "account_email",
122
+ "transcript_path",
123
+ "transcript",
124
+ "thought",
125
+ "thoughts",
126
+ "thinking",
127
+ "tool_output"
128
+ ]);
129
+ function requiredId(value, field) {
130
+ if (typeof value !== "string" || value.length === 0) {
131
+ throw new TypeError(`Cursor hook envelope is missing ${field}`);
132
+ }
133
+ return value;
134
+ }
135
+ function cursorToolUseId(value) {
136
+ if (SAFE_CORRELATION_ID_RE.test(value)) return value;
137
+ return `cursor:tool:v1:${createHash("sha256").update(value).digest("hex")}`;
138
+ }
139
+ function workspaceCandidates(value) {
140
+ if (!Array.isArray(value)) return [];
141
+ const roots = value.filter((item) => typeof item === "string" && item.length > 0);
142
+ if (roots.some((root) => !isAbsolute(root))) {
143
+ throw new TypeError("Cursor workspace roots must be absolute");
144
+ }
145
+ return roots;
146
+ }
147
+ function pathIdentity(path) {
148
+ try {
149
+ return realpathSync.native(path);
150
+ } catch {
151
+ return resolve(path);
152
+ }
153
+ }
154
+ function resolveCursorCwd(parsed) {
155
+ const roots = workspaceCandidates(parsed.workspace_roots).map(pathIdentity);
156
+ const rootRepositories = roots.map((root) => resolveRepositoryContext(root)?.repoRoot);
157
+ const repositoryRoots = new Set(rootRepositories.filter((root) => !!root));
158
+ if (repositoryRoots.size > 1) {
159
+ throw new TypeError("Cursor hook envelope spans multiple repositories");
160
+ }
161
+ if (roots.length > 1 && rootRepositories.some((root) => root === void 0)) {
162
+ throw new TypeError("Cursor hook envelope has ambiguous workspace roots");
163
+ }
164
+ const expectedRepo = [...repositoryRoots][0];
165
+ const rawCwd = typeof parsed.cwd === "string" && parsed.cwd.length > 0 ? parsed.cwd : void 0;
166
+ if (rawCwd && !isAbsolute(rawCwd)) {
167
+ throw new TypeError("Cursor hook cwd must be absolute");
168
+ }
169
+ const envelopeCwd = rawCwd ? pathIdentity(rawCwd) : void 0;
170
+ const input = typeof parsed.tool_input === "object" && parsed.tool_input !== null && !Array.isArray(parsed.tool_input) ? parsed.tool_input : void 0;
171
+ const rawToolCwd = parsed.tool_name === "Shell" && typeof input?.working_directory === "string" ? input.working_directory : void 0;
172
+ const base = envelopeCwd ?? (roots.length === 1 ? roots[0] : expectedRepo);
173
+ if (rawToolCwd && !isAbsolute(rawToolCwd) && !base) {
174
+ throw new TypeError("relative Cursor tool cwd has no workspace base");
175
+ }
176
+ const toolCwd = rawToolCwd ? pathIdentity(isAbsolute(rawToolCwd) ? rawToolCwd : resolve(base, rawToolCwd)) : void 0;
177
+ const selected = toolCwd ?? envelopeCwd ?? (roots.length === 1 ? roots[0] : expectedRepo);
178
+ if (!selected) {
179
+ throw new TypeError("Cursor hook envelope has no unambiguous workspace");
180
+ }
181
+ const selectedRepo = resolveRepositoryContext(selected)?.repoRoot;
182
+ if (expectedRepo && selectedRepo !== expectedRepo) {
183
+ throw new TypeError("Cursor hook cwd conflicts with workspace repository");
184
+ }
185
+ return selected;
186
+ }
187
+ function normalizeCursorEnvelope(parsed) {
188
+ const rawEvent = requiredId(parsed.hook_event_name, "hook_event_name");
189
+ const event = CURSOR_EVENT_MAP[rawEvent];
190
+ if (!event) throw new TypeError(`unsupported Cursor hook event: ${rawEvent}`);
191
+ const sessionId = requiredId(parsed.conversation_id, "conversation_id");
192
+ const turnId = requiredId(parsed.generation_id, "generation_id");
193
+ if (parsed.session_id !== void 0 && parsed.session_id !== sessionId) {
194
+ throw new TypeError("Cursor conversation_id conflicts with session_id");
195
+ }
196
+ if (parsed.turn_id !== void 0 && parsed.turn_id !== turnId) {
197
+ throw new TypeError("Cursor generation_id conflicts with turn_id");
198
+ }
199
+ const rawToolUseId = CURSOR_TOOL_EVENTS.has(event) ? requiredId(parsed.tool_use_id, "tool_use_id") : void 0;
200
+ const normalized = {
201
+ ...Object.fromEntries(
202
+ Object.entries(parsed).filter(([key]) => !CURSOR_LOCAL_ONLY_FIELDS.has(key))
203
+ ),
204
+ hook_event_name: event,
205
+ session_id: sessionId,
206
+ turn_id: turnId,
207
+ cwd: resolveCursorCwd(parsed),
208
+ ...rawToolUseId === void 0 ? {} : { tool_use_id: cursorToolUseId(rawToolUseId) }
209
+ };
210
+ if (typeof parsed.tool_output === "string") {
211
+ try {
212
+ normalized.tool_response = JSON.parse(parsed.tool_output);
213
+ } catch {
214
+ normalized.tool_response = parsed.tool_output;
215
+ }
216
+ }
217
+ return normalized;
218
+ }
219
+ function normalizeEnvelope(parsed, agent) {
220
+ if (agent === "cursor") {
221
+ return normalizeCursorEnvelope(parsed);
222
+ }
223
+ if (agent === "codex") {
224
+ const event = parsed.hook_event_name;
225
+ if ((event === "PreToolUse" || event === "PostToolUse") && (typeof parsed.tool_use_id !== "string" || parsed.tool_use_id.length === 0)) {
226
+ const stableToolIdentity = JSON.stringify([
227
+ parsed.session_id ?? null,
228
+ parsed.turn_id ?? null,
229
+ parsed.tool_name ?? null,
230
+ parsed.tool_input ?? null
231
+ ]);
232
+ return {
233
+ ...parsed,
234
+ tool_use_id: `codex:fallback:v1:${createHash("sha256").update(stableToolIdentity).digest("hex")}`
235
+ };
236
+ }
237
+ return parsed;
238
+ }
239
+ if (agent !== "hermes") {
240
+ return parsed;
241
+ }
242
+ const raw = parsed.hook_event_name;
243
+ if (typeof raw === "string" && raw in HERMES_EVENT_MAP) {
244
+ return { ...parsed, hook_event_name: HERMES_EVENT_MAP[raw] };
245
+ }
246
+ return parsed;
247
+ }
248
+
249
+ export {
250
+ shouldSuppressImportedCursorHandler,
251
+ readHookStdin,
252
+ normalizeEnvelope
253
+ };