@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,210 @@
|
|
|
1
|
+
// src/lib/workspace-id.ts
|
|
2
|
+
import { execFileSync } from "child_process";
|
|
3
|
+
import { randomUUID } from "crypto";
|
|
4
|
+
import {
|
|
5
|
+
constants,
|
|
6
|
+
chmodSync,
|
|
7
|
+
closeSync,
|
|
8
|
+
fchmodSync,
|
|
9
|
+
fsyncSync,
|
|
10
|
+
linkSync,
|
|
11
|
+
lstatSync,
|
|
12
|
+
mkdirSync,
|
|
13
|
+
openSync,
|
|
14
|
+
readFileSync,
|
|
15
|
+
statSync,
|
|
16
|
+
unlinkSync,
|
|
17
|
+
writeFileSync
|
|
18
|
+
} from "fs";
|
|
19
|
+
import { dirname, isAbsolute, join, resolve } from "path";
|
|
20
|
+
var WORKSPACE_ID_RELATIVE_PATH = join("prim", "workspace-id");
|
|
21
|
+
var MAX_ANCESTORS = 64;
|
|
22
|
+
var MAX_GIT_FILE_BYTES = 4096;
|
|
23
|
+
var MAX_ID_FILE_BYTES = 128;
|
|
24
|
+
var GIT_FALLBACK_TIMEOUT_MS = 250;
|
|
25
|
+
var RACE_READ_ATTEMPTS = 5;
|
|
26
|
+
var RACE_READ_DELAY_MS = 5;
|
|
27
|
+
var CANONICAL_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
28
|
+
var raceDelay = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
|
|
29
|
+
function isCanonicalWorkspaceId(value) {
|
|
30
|
+
return CANONICAL_UUID.test(value);
|
|
31
|
+
}
|
|
32
|
+
function inspectWorkspaceId(cwd = process.cwd()) {
|
|
33
|
+
const resolved = resolveWorkspaceIdPath(cwd);
|
|
34
|
+
if (resolved.status !== "resolved") return resolved;
|
|
35
|
+
return inspectPath(resolved.path);
|
|
36
|
+
}
|
|
37
|
+
function getOrCreateWorkspaceId(cwd = process.cwd()) {
|
|
38
|
+
const inspection = inspectWorkspaceId(cwd);
|
|
39
|
+
if (inspection.status !== "missing") return inspection;
|
|
40
|
+
return createWorkspaceId(inspection.path);
|
|
41
|
+
}
|
|
42
|
+
function resolveWorkspaceIdPath(cwd) {
|
|
43
|
+
if (!process.env.GIT_DIR && !process.env.GIT_WORK_TREE) {
|
|
44
|
+
const fromFilesystem = resolveFromFilesystem(cwd);
|
|
45
|
+
if (fromFilesystem) return { status: "resolved", path: fromFilesystem };
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const output = execFileSync(
|
|
49
|
+
"git",
|
|
50
|
+
["rev-parse", "--is-inside-work-tree", "--git-path", WORKSPACE_ID_RELATIVE_PATH],
|
|
51
|
+
{
|
|
52
|
+
cwd,
|
|
53
|
+
encoding: "utf8",
|
|
54
|
+
maxBuffer: 4096,
|
|
55
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
56
|
+
timeout: GIT_FALLBACK_TIMEOUT_MS,
|
|
57
|
+
windowsHide: true
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
const [insideWorktree, gitPath, ...extra] = output.trim().split(/\r?\n/);
|
|
61
|
+
if (insideWorktree !== "true" || !gitPath || extra.length > 0 || gitPath.includes("\0")) {
|
|
62
|
+
return { status: "not_git" };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
status: "resolved",
|
|
66
|
+
path: isAbsolute(gitPath) ? resolve(gitPath) : resolve(cwd, gitPath)
|
|
67
|
+
};
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const code = errorCode(error);
|
|
70
|
+
if (code === "ETIMEDOUT" || code === "EACCES" || code === "EPERM") {
|
|
71
|
+
return { status: "unavailable", operation: "resolve", errorCode: code };
|
|
72
|
+
}
|
|
73
|
+
return { status: "not_git" };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function resolveFromFilesystem(cwd) {
|
|
77
|
+
let current = resolve(cwd);
|
|
78
|
+
for (let depth = 0; depth < MAX_ANCESTORS; depth += 1) {
|
|
79
|
+
const dotGit = join(current, ".git");
|
|
80
|
+
try {
|
|
81
|
+
const stat = lstatSync(dotGit);
|
|
82
|
+
if (stat.isDirectory() && isGitDirectory(dotGit)) {
|
|
83
|
+
return join(dotGit, WORKSPACE_ID_RELATIVE_PATH);
|
|
84
|
+
}
|
|
85
|
+
if (stat.isFile() && stat.size <= MAX_GIT_FILE_BYTES) {
|
|
86
|
+
const contents = readFileSync(dotGit, "utf8");
|
|
87
|
+
const match = /^gitdir: (.+?)(?:\r?\n)?$/.exec(contents);
|
|
88
|
+
if (match?.[1]) {
|
|
89
|
+
const gitDirectory = isAbsolute(match[1]) ? resolve(match[1]) : resolve(current, match[1]);
|
|
90
|
+
if (isGitDirectory(gitDirectory)) {
|
|
91
|
+
return join(gitDirectory, WORKSPACE_ID_RELATIVE_PATH);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
const code = errorCode(error);
|
|
98
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const parent = dirname(current);
|
|
103
|
+
if (parent === current) break;
|
|
104
|
+
current = parent;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
function isGitDirectory(path) {
|
|
109
|
+
try {
|
|
110
|
+
return statSync(join(path, "HEAD")).isFile();
|
|
111
|
+
} catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function inspectPath(path) {
|
|
116
|
+
let stat;
|
|
117
|
+
try {
|
|
118
|
+
stat = lstatSync(path);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
const code = errorCode(error);
|
|
121
|
+
if (code === "ENOENT" || code === "ENOTDIR") return { status: "missing", path };
|
|
122
|
+
return { status: "unavailable", path, operation: "read", errorCode: code };
|
|
123
|
+
}
|
|
124
|
+
if (!stat.isFile()) return { status: "corrupt", path, reason: "not_file" };
|
|
125
|
+
if (stat.size > MAX_ID_FILE_BYTES) {
|
|
126
|
+
return { status: "corrupt", path, reason: "invalid_content" };
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const contents = readFileSync(path, "utf8");
|
|
130
|
+
const workspaceId = contents.endsWith("\n") ? contents.slice(0, -1) : contents;
|
|
131
|
+
if ((contents === workspaceId || contents === `${workspaceId}
|
|
132
|
+
`) && isCanonicalWorkspaceId(workspaceId)) {
|
|
133
|
+
return { status: "ready", workspaceId, path, created: false };
|
|
134
|
+
}
|
|
135
|
+
return { status: "corrupt", path, reason: "invalid_content" };
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return {
|
|
138
|
+
status: "unavailable",
|
|
139
|
+
path,
|
|
140
|
+
operation: "read",
|
|
141
|
+
errorCode: errorCode(error)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function createWorkspaceId(path) {
|
|
146
|
+
const parent = dirname(path);
|
|
147
|
+
const workspaceId = randomUUID();
|
|
148
|
+
const temporaryPath = join(parent, `.workspace-id-${process.pid}-${randomUUID()}.tmp`);
|
|
149
|
+
let temporaryCreated = false;
|
|
150
|
+
let publishing = false;
|
|
151
|
+
try {
|
|
152
|
+
mkdirSync(parent, { recursive: true, mode: 448 });
|
|
153
|
+
chmodSync(parent, 448);
|
|
154
|
+
const fd = openSync(
|
|
155
|
+
temporaryPath,
|
|
156
|
+
constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY,
|
|
157
|
+
384
|
|
158
|
+
);
|
|
159
|
+
temporaryCreated = true;
|
|
160
|
+
try {
|
|
161
|
+
writeFileSync(fd, `${workspaceId}
|
|
162
|
+
`, "utf8");
|
|
163
|
+
fchmodSync(fd, 384);
|
|
164
|
+
fsyncSync(fd);
|
|
165
|
+
} finally {
|
|
166
|
+
closeSync(fd);
|
|
167
|
+
}
|
|
168
|
+
publishing = true;
|
|
169
|
+
linkSync(temporaryPath, path);
|
|
170
|
+
return { status: "ready", workspaceId, path, created: true };
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (publishing && errorCode(error) === "EEXIST") return inspectAfterRace(path);
|
|
173
|
+
return {
|
|
174
|
+
status: "unavailable",
|
|
175
|
+
path,
|
|
176
|
+
operation: "create",
|
|
177
|
+
errorCode: errorCode(error)
|
|
178
|
+
};
|
|
179
|
+
} finally {
|
|
180
|
+
if (temporaryCreated) {
|
|
181
|
+
try {
|
|
182
|
+
unlinkSync(temporaryPath);
|
|
183
|
+
} catch {
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function inspectAfterRace(path) {
|
|
189
|
+
let result = { status: "missing", path };
|
|
190
|
+
for (let attempt = 0; attempt < RACE_READ_ATTEMPTS; attempt += 1) {
|
|
191
|
+
result = inspectPath(path);
|
|
192
|
+
if (result.status === "ready") return result;
|
|
193
|
+
if (attempt + 1 < RACE_READ_ATTEMPTS) {
|
|
194
|
+
Atomics.wait(raceDelay, 0, 0, RACE_READ_DELAY_MS);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (result.status === "missing") {
|
|
198
|
+
return { status: "unavailable", path, operation: "create", errorCode: "EEXIST" };
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
function errorCode(error) {
|
|
203
|
+
if (typeof error !== "object" || error === null || !("code" in error)) return void 0;
|
|
204
|
+
return typeof error.code === "string" ? error.code : void 0;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export {
|
|
208
|
+
isCanonicalWorkspaceId,
|
|
209
|
+
getOrCreateWorkspaceId
|
|
210
|
+
};
|