@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
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import {
|
|
2
|
+
atomicWriteFile,
|
|
3
|
+
repoActiveFlag
|
|
4
|
+
} from "./chunk-XG7S3LIL.js";
|
|
5
|
+
|
|
6
|
+
// src/lib/commit-heartbeat.ts
|
|
7
|
+
import { execFileSync } from "child_process";
|
|
8
|
+
import { mkdirSync, readFileSync, readdirSync, unlinkSync } from "fs";
|
|
9
|
+
import { dirname, join, resolve } from "path";
|
|
10
|
+
var FIRED_DIR = "prim/post-commit-fired";
|
|
11
|
+
var WIRED_PATH = "prim/git-hooks-wired";
|
|
12
|
+
var GIT_TIMEOUT_MS = 1e3;
|
|
13
|
+
var MAX_STAMP_BYTES = 1024;
|
|
14
|
+
var REFLOG_SCAN_LIMIT = 50;
|
|
15
|
+
var MAX_FIRED_STAMPS = 256;
|
|
16
|
+
var PRUNE_SLACK = 64;
|
|
17
|
+
var FULL_SHA_RE = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u;
|
|
18
|
+
var REFLOG_LINE_RE = /^([0-9a-f]{40}|[0-9a-f]{64})\tHEAD@\{([0-9]+)\}\t(.*)$/u;
|
|
19
|
+
var COMMIT_SUBJECT_RE = /^commit(?: \([^)]*\))?:/u;
|
|
20
|
+
var REFLOG_RESOLUTION_MS = 1e3;
|
|
21
|
+
var POST_COMMIT_GRACE_MS = 6e4;
|
|
22
|
+
function commonPath(cwd, relative) {
|
|
23
|
+
const value = execFileSync("git", ["rev-parse", "--git-common-dir"], {
|
|
24
|
+
cwd,
|
|
25
|
+
encoding: "utf8",
|
|
26
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
27
|
+
timeout: GIT_TIMEOUT_MS
|
|
28
|
+
}).replace(/\r?\n$/u, "");
|
|
29
|
+
if (value === "" || /[\0\r\n]/u.test(value)) throw new Error("Git returned an unsafe path");
|
|
30
|
+
return join(resolve(cwd, value), relative);
|
|
31
|
+
}
|
|
32
|
+
function gitPath(cwd, relative) {
|
|
33
|
+
const value = execFileSync("git", ["rev-parse", "--git-path", relative], {
|
|
34
|
+
cwd,
|
|
35
|
+
encoding: "utf8",
|
|
36
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
37
|
+
timeout: GIT_TIMEOUT_MS
|
|
38
|
+
}).replace(/\r?\n$/u, "");
|
|
39
|
+
if (value === "" || /[\0\r\n]/u.test(value)) throw new Error("Git returned an unsafe path");
|
|
40
|
+
return resolve(cwd, value);
|
|
41
|
+
}
|
|
42
|
+
function ensurePrivateParent(path) {
|
|
43
|
+
mkdirSync(dirname(dirname(path)), { recursive: true, mode: 448 });
|
|
44
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
45
|
+
}
|
|
46
|
+
function writeStamp(path, at) {
|
|
47
|
+
ensurePrivateParent(path);
|
|
48
|
+
atomicWriteFile(path, `${JSON.stringify({ at })}
|
|
49
|
+
`, { mode: 384 });
|
|
50
|
+
}
|
|
51
|
+
function readStampAt(path) {
|
|
52
|
+
try {
|
|
53
|
+
const raw = readFileSync(path, "utf8");
|
|
54
|
+
if (raw.length > MAX_STAMP_BYTES) return void 0;
|
|
55
|
+
const parsed = JSON.parse(raw);
|
|
56
|
+
return typeof parsed.at === "number" && Number.isSafeInteger(parsed.at) && parsed.at > 0 ? parsed.at : void 0;
|
|
57
|
+
} catch {
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function recordConversationCaptured(cwd, agent) {
|
|
62
|
+
try {
|
|
63
|
+
writeStamp(gitPath(cwd, `prim/conversation-captured/${agent}`), Date.now());
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function lastConversationCaptured(cwd, agent) {
|
|
68
|
+
try {
|
|
69
|
+
return readStampAt(gitPath(cwd, `prim/conversation-captured/${agent}`));
|
|
70
|
+
} catch {
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function readFiredStamps(cwd) {
|
|
75
|
+
try {
|
|
76
|
+
const dir = gitPath(cwd, FIRED_DIR);
|
|
77
|
+
return readdirSync(dir).flatMap((sha) => {
|
|
78
|
+
if (!FULL_SHA_RE.test(sha)) return [];
|
|
79
|
+
const at = readStampAt(join(dir, sha));
|
|
80
|
+
return at === void 0 ? [] : [{ sha, at }];
|
|
81
|
+
});
|
|
82
|
+
} catch {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function recordPostCommitFired(cwd, sha, now = Date.now()) {
|
|
87
|
+
if (!sha || !FULL_SHA_RE.test(sha)) return;
|
|
88
|
+
try {
|
|
89
|
+
const dir = gitPath(cwd, FIRED_DIR);
|
|
90
|
+
writeStamp(join(dir, sha), now);
|
|
91
|
+
const names = readdirSync(dir).filter((name) => FULL_SHA_RE.test(name));
|
|
92
|
+
if (names.length <= MAX_FIRED_STAMPS + PRUNE_SLACK) return;
|
|
93
|
+
const stale = names.map((name) => ({ name, at: readStampAt(join(dir, name)) ?? 0 })).sort((left, right) => right.at - left.at || left.name.localeCompare(right.name)).slice(MAX_FIRED_STAMPS);
|
|
94
|
+
for (const { name } of stale) unlinkSync(join(dir, name));
|
|
95
|
+
} catch {
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function recordHooksWired(cwd, options = {}) {
|
|
99
|
+
try {
|
|
100
|
+
const path = commonPath(cwd, WIRED_PATH);
|
|
101
|
+
if (options.onlyIfAbsent && readStampAt(path) !== void 0) return;
|
|
102
|
+
writeStamp(path, options.now ?? Date.now());
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function clearHooksWired(cwd) {
|
|
107
|
+
try {
|
|
108
|
+
unlinkSync(commonPath(cwd, WIRED_PATH));
|
|
109
|
+
} catch {
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function latestLocalCommit(cwd) {
|
|
113
|
+
let output;
|
|
114
|
+
try {
|
|
115
|
+
output = execFileSync(
|
|
116
|
+
"git",
|
|
117
|
+
[
|
|
118
|
+
"log",
|
|
119
|
+
"-g",
|
|
120
|
+
"--no-show-signature",
|
|
121
|
+
"-n",
|
|
122
|
+
String(REFLOG_SCAN_LIMIT),
|
|
123
|
+
"--date=unix",
|
|
124
|
+
"--format=%H%x09%gd%x09%gs",
|
|
125
|
+
"HEAD"
|
|
126
|
+
],
|
|
127
|
+
{ cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: GIT_TIMEOUT_MS }
|
|
128
|
+
);
|
|
129
|
+
} catch {
|
|
130
|
+
return void 0;
|
|
131
|
+
}
|
|
132
|
+
for (const line of output.split("\n")) {
|
|
133
|
+
const match = REFLOG_LINE_RE.exec(line);
|
|
134
|
+
if (match?.[1] && match[2] && COMMIT_SUBJECT_RE.test(match[3] ?? "")) {
|
|
135
|
+
return { sha: match[1], at: Number(match[2]) * 1e3 };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return void 0;
|
|
139
|
+
}
|
|
140
|
+
function inspectPostCommitFiring(cwd, now = Date.now()) {
|
|
141
|
+
if (repoActiveFlag(cwd) !== "true") return { state: "inactive" };
|
|
142
|
+
const commit = latestLocalCommit(cwd);
|
|
143
|
+
if (!commit) return { state: "unverified" };
|
|
144
|
+
const stamps = readFiredStamps(cwd);
|
|
145
|
+
const own = stamps.find((stamp) => stamp.sha === commit.sha);
|
|
146
|
+
if (own) return { state: "fired", commitAt: commit.at, firedAt: own.at };
|
|
147
|
+
if (now - commit.at < POST_COMMIT_GRACE_MS) return { state: "pending", commitAt: commit.at };
|
|
148
|
+
const lastFiredAt = stamps.reduce(
|
|
149
|
+
(latest, stamp) => latest === void 0 || stamp.at > latest ? stamp.at : latest,
|
|
150
|
+
void 0
|
|
151
|
+
);
|
|
152
|
+
let wiredAt;
|
|
153
|
+
try {
|
|
154
|
+
wiredAt = readStampAt(commonPath(cwd, WIRED_PATH));
|
|
155
|
+
} catch {
|
|
156
|
+
wiredAt = void 0;
|
|
157
|
+
}
|
|
158
|
+
if (wiredAt === void 0) return { state: "unverified" };
|
|
159
|
+
const firedSinceWired = lastFiredAt !== void 0 && lastFiredAt >= wiredAt;
|
|
160
|
+
const judged = commit.at > wiredAt || firedSinceWired && commit.at + REFLOG_RESOLUTION_MS > lastFiredAt;
|
|
161
|
+
if (judged) {
|
|
162
|
+
return lastFiredAt === void 0 ? { state: "not_firing", commitAt: commit.at } : { state: "not_firing", commitAt: commit.at, firedAt: lastFiredAt };
|
|
163
|
+
}
|
|
164
|
+
return { state: "unverified" };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export {
|
|
168
|
+
POST_COMMIT_GRACE_MS,
|
|
169
|
+
recordConversationCaptured,
|
|
170
|
+
lastConversationCaptured,
|
|
171
|
+
recordPostCommitFired,
|
|
172
|
+
recordHooksWired,
|
|
173
|
+
clearHooksWired,
|
|
174
|
+
inspectPostCommitFiring
|
|
175
|
+
};
|