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

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 (57) hide show
  1. package/README.md +550 -38
  2. package/SKILL.md +320 -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-2DNC2MUN.js +503 -0
  7. package/dist/chunk-2PFJJO2M.js +262 -0
  8. package/dist/chunk-4GLVJLEQ.js +55 -0
  9. package/dist/chunk-4GYJHN26.js +1440 -0
  10. package/dist/chunk-6DEROXLM.js +22695 -0
  11. package/dist/chunk-7LXRFXZG.js +1992 -0
  12. package/dist/chunk-AKGRY7OU.js +1572 -0
  13. package/dist/chunk-BHT6X3T5.js +1450 -0
  14. package/dist/chunk-DQ4I3FLF.js +164 -0
  15. package/dist/chunk-DVHD6LBQ.js +258 -0
  16. package/dist/chunk-EZXEWFDG.js +210 -0
  17. package/dist/chunk-HG7FJK7R.js +253 -0
  18. package/dist/chunk-JSBRDJBE.js +30 -0
  19. package/dist/chunk-JXRJLHE4.js +168 -0
  20. package/dist/chunk-JZC2LHS6.js +209 -0
  21. package/dist/chunk-KZB4ABIB.js +13 -0
  22. package/dist/chunk-LQLP26RL.js +459 -0
  23. package/dist/chunk-M4NZJCPQ.js +135 -0
  24. package/dist/chunk-MRDY7VQP.js +96 -0
  25. package/dist/chunk-NMZSCSMY.js +157 -0
  26. package/dist/chunk-NQZLXYTW.js +36 -0
  27. package/dist/chunk-OXW6PSPT.js +439 -0
  28. package/dist/chunk-OYHYCK4K.js +13 -0
  29. package/dist/chunk-S3ZIMGXV.js +844 -0
  30. package/dist/chunk-WN7OURQ3.js +175 -0
  31. package/dist/daemon/server.js +26506 -0
  32. package/dist/hook-runtime/daemon/server.js +26507 -0
  33. package/dist/hook-runtime/hooks/post-commit.js +23898 -0
  34. package/dist/hook-runtime/hooks/post-rewrite.js +23797 -0
  35. package/dist/hook-runtime/hooks/post-tool-use.js +36213 -0
  36. package/dist/hook-runtime/hooks/pre-commit.js +1741 -0
  37. package/dist/hook-runtime/hooks/pre-tool-use-worker.js +35071 -0
  38. package/dist/hook-runtime/hooks/pre-tool-use.js +96 -0
  39. package/dist/hook-runtime/hooks/prim-hook.js +36273 -0
  40. package/dist/hook-runtime/hooks/session-end.js +1250 -0
  41. package/dist/hook-runtime/hooks/session-start.js +34021 -0
  42. package/dist/hook-runtime/index.js +71700 -0
  43. package/dist/hook-runtime/statusline-main.js +954 -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 +11249 -572
  54. package/dist/statusline-main.js +953 -0
  55. package/package.json +51 -17
  56. package/skills/prim-welcome/SKILL.md +197 -0
  57. 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-BHT6X3T5.js";
15
+ import {
16
+ packageVersion
17
+ } from "./chunk-4GYJHN26.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-4GYJHN26.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,36 @@
1
+ import {
2
+ scheduleCollectScopeRefresh
3
+ } from "./chunk-6DEROXLM.js";
4
+
5
+ // src/ingest-response.ts
6
+ var IngestAcknowledgementError = class extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = "IngestAcknowledgementError";
10
+ }
11
+ };
12
+ function requireDurableIngestAcknowledgement(value, expected, cwd = process.cwd()) {
13
+ if (typeof value !== "object" || value === null) {
14
+ throw new IngestAcknowledgementError("ingest response was not an object");
15
+ }
16
+ const response = value;
17
+ if (response.disposition !== "persisted") {
18
+ throw new IngestAcknowledgementError("ingest response did not confirm persistence");
19
+ }
20
+ if (response.acknowledged !== expected) {
21
+ throw new IngestAcknowledgementError(
22
+ `ingest acknowledged ${String(response.acknowledged)} of ${String(expected)} moves`
23
+ );
24
+ }
25
+ if (typeof response.collectScopeVersion === "number" && Number.isSafeInteger(response.collectScopeVersion) && response.collectScopeVersion >= 0) {
26
+ const roots = typeof cwd === "string" ? [cwd] : cwd;
27
+ for (const root of new Set(roots)) {
28
+ scheduleCollectScopeRefresh(root, response.collectScopeVersion);
29
+ }
30
+ }
31
+ return value;
32
+ }
33
+
34
+ export {
35
+ requireDurableIngestAcknowledgement
36
+ };