@primitive.ai/prim 0.1.0-alpha.96 → 0.1.0-alpha.98

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 (39) hide show
  1. package/README.md +9 -0
  2. package/dist/{chunk-YYE2NLWX.js → chunk-2DA2B4EC.js} +2 -2
  3. package/dist/{chunk-35FMRWJH.js → chunk-2DPLT2CQ.js} +1 -1
  4. package/dist/{chunk-XEDJ5N3M.js → chunk-3X77XQGW.js} +2 -4
  5. package/dist/{chunk-DWRSEC34.js → chunk-6QK75B7G.js} +392 -15
  6. package/dist/{chunk-Z6NPNLOT.js → chunk-7IPGD6CD.js} +1708 -1578
  7. package/dist/{chunk-RK4ZS3HG.js → chunk-AN6XTFIN.js} +2 -2
  8. package/dist/{chunk-LY6NJ7KL.js → chunk-B6EPQ4YH.js} +1 -1
  9. package/dist/{chunk-URLLSALP.js → chunk-BIAWZMCK.js} +2 -2
  10. package/dist/{chunk-QCBB5HFX.js → chunk-HUZXBJJ5.js} +1 -1
  11. package/dist/{chunk-ROLYLXJS.js → chunk-IL443QTF.js} +2 -2
  12. package/dist/{chunk-7KABYYWG.js → chunk-JOR67B5T.js} +6 -8
  13. package/dist/{chunk-FVDBULGZ.js → chunk-MN4X6RR6.js} +8 -10
  14. package/dist/{chunk-UO2U6IKF.js → chunk-NIQAVFP6.js} +1 -1
  15. package/dist/{chunk-CU7RW7XU.js → chunk-PPZBEOWK.js} +3 -3
  16. package/dist/{chunk-4GHANPWQ.js → chunk-QPMLAD2C.js} +2 -2
  17. package/dist/{chunk-Y5W4TZM7.js → chunk-RT7DXMTN.js} +2 -4
  18. package/dist/{chunk-27ADT6FQ.js → chunk-WKLYYT63.js} +44 -6
  19. package/dist/daemon/server.js +1704 -1572
  20. package/dist/hook-runtime/daemon/server.js +1704 -1572
  21. package/dist/hook-runtime/hooks/post-commit.js +1704 -1572
  22. package/dist/hook-runtime/hooks/post-rewrite.js +1704 -1572
  23. package/dist/hook-runtime/hooks/post-tool-use.js +1704 -1572
  24. package/dist/hook-runtime/hooks/pre-commit.js +208 -9
  25. package/dist/hook-runtime/hooks/pre-tool-use.js +1704 -1572
  26. package/dist/hook-runtime/hooks/prim-hook.js +1704 -1572
  27. package/dist/hook-runtime/hooks/session-start.js +1704 -1572
  28. package/dist/hook-runtime/index.js +1745 -1578
  29. package/dist/hooks/post-commit.js +7 -8
  30. package/dist/hooks/post-rewrite.js +6 -7
  31. package/dist/hooks/post-tool-use.js +13 -14
  32. package/dist/hooks/pre-commit.js +4 -4
  33. package/dist/hooks/pre-tool-use.js +10 -12
  34. package/dist/hooks/prim-hook.js +14 -15
  35. package/dist/hooks/session-end.js +6 -7
  36. package/dist/hooks/session-start.js +13 -14
  37. package/dist/index.js +25 -27
  38. package/package.json +1 -1
  39. package/dist/chunk-DHOQGHKQ.js +0 -379
@@ -496,14 +496,14 @@ function throwIfAborted(signal) {
496
496
  }
497
497
  async function defaultSleep(ms, signal) {
498
498
  if (!signal) {
499
- await new Promise((resolve2) => setTimeout(resolve2, ms));
499
+ await new Promise((resolve3) => setTimeout(resolve3, ms));
500
500
  return;
501
501
  }
502
502
  throwIfAborted(signal);
503
- await new Promise((resolve2, reject) => {
503
+ await new Promise((resolve3, reject) => {
504
504
  const timer = setTimeout(() => {
505
505
  signal.removeEventListener("abort", onAbort);
506
- resolve2();
506
+ resolve3();
507
507
  }, ms);
508
508
  const onAbort = () => {
509
509
  clearTimeout(timer);
@@ -1098,7 +1098,7 @@ function daemonRequest(method, params = {}, opts = {}) {
1098
1098
  ...caller ? { caller } : {}
1099
1099
  };
1100
1100
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
1101
- return new Promise((resolve2) => {
1101
+ return new Promise((resolve3) => {
1102
1102
  let settled = false;
1103
1103
  let socket;
1104
1104
  let buffer = Buffer.alloc(0);
@@ -1113,7 +1113,7 @@ function daemonRequest(method, params = {}, opts = {}) {
1113
1113
  } catch {
1114
1114
  }
1115
1115
  }
1116
- resolve2(value);
1116
+ resolve3(value);
1117
1117
  };
1118
1118
  const timer = setTimeout(() => settle(null), timeoutMs);
1119
1119
  timer.unref();
@@ -1196,6 +1196,173 @@ async function daemonOrDirectGet(method, path, client, timeoutMs = DAEMON_HTTP_T
1196
1196
  );
1197
1197
  }
1198
1198
 
1199
+ // src/lib/activation.ts
1200
+ import { execFileSync as execFileSync2 } from "child_process";
1201
+ import { existsSync as existsSync3, readFileSync as readFileSync4 } from "fs";
1202
+ import { homedir as homedir2 } from "os";
1203
+ import { join as join6 } from "path";
1204
+
1205
+ // src/lib/git.ts
1206
+ import { execFileSync } from "child_process";
1207
+ import { createHash as createHash4 } from "crypto";
1208
+ import { lstatSync, realpathSync } from "fs";
1209
+ import { basename, dirname as dirname4, isAbsolute as isAbsolute2, relative, resolve as resolve2, sep } from "path";
1210
+ var GIT_TIMEOUT_MS = 1e3;
1211
+ var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
1212
+ var GITHUB_REPOSITORY_PATTERN = /^[A-Za-z0-9._-]{1,100}$/u;
1213
+ function gitToplevel(cwd) {
1214
+ try {
1215
+ return execFileSync("git", ["rev-parse", "--show-toplevel"], {
1216
+ cwd,
1217
+ encoding: "utf-8",
1218
+ stdio: ["ignore", "pipe", "ignore"],
1219
+ timeout: GIT_TIMEOUT_MS
1220
+ }).trim();
1221
+ } catch {
1222
+ return null;
1223
+ }
1224
+ }
1225
+ function gitValue(cwd, args) {
1226
+ try {
1227
+ const value = execFileSync("git", args, {
1228
+ cwd,
1229
+ encoding: "utf-8",
1230
+ stdio: ["ignore", "pipe", "ignore"],
1231
+ timeout: GIT_TIMEOUT_MS
1232
+ }).trim();
1233
+ return value || void 0;
1234
+ } catch {
1235
+ return void 0;
1236
+ }
1237
+ }
1238
+ function currentBranch(cwd) {
1239
+ return gitValue(cwd, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
1240
+ }
1241
+ function githubRepositoryFullName(cwd) {
1242
+ const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
1243
+ if (!remote || [...remote].some((char) => char < " " || char === "\x7F")) return null;
1244
+ const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
1245
+ remote
1246
+ );
1247
+ const path = match?.[1] ?? match?.[2];
1248
+ if (!path) return null;
1249
+ const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
1250
+ if (parts.length !== 2 || !GITHUB_OWNER_PATTERN.test(parts[0] ?? "") || !GITHUB_REPOSITORY_PATTERN.test(parts[1] ?? "")) {
1251
+ return null;
1252
+ }
1253
+ return `${parts[0]}/${parts[1]}`;
1254
+ }
1255
+ function canonicalGitRoot(cwd) {
1256
+ try {
1257
+ const top = gitToplevel(cwd);
1258
+ return top ? realpathSync.native(top) : null;
1259
+ } catch {
1260
+ return null;
1261
+ }
1262
+ }
1263
+ function nearestExistingAncestor(path) {
1264
+ const suffix = [];
1265
+ let cursor = path;
1266
+ while (true) {
1267
+ try {
1268
+ lstatSync(cursor);
1269
+ break;
1270
+ } catch (error) {
1271
+ const code = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
1272
+ if (code !== "ENOENT" && code !== "ENOTDIR") return null;
1273
+ const parent = dirname4(cursor);
1274
+ if (parent === cursor) return null;
1275
+ suffix.unshift(cursor.slice(parent.length + (parent.endsWith(sep) ? 0 : 1)));
1276
+ cursor = parent;
1277
+ }
1278
+ }
1279
+ try {
1280
+ return { real: realpathSync.native(cursor), suffix };
1281
+ } catch {
1282
+ return null;
1283
+ }
1284
+ }
1285
+ function canonicalRepositoryPathScoped(filePath, context, baseDir) {
1286
+ if (!filePath || filePath !== filePath.trim() || filePath.includes("\0")) {
1287
+ return { ok: false, reason: "invalid_path" };
1288
+ }
1289
+ const absolute = resolve2(baseDir, filePath);
1290
+ const resolved = nearestExistingAncestor(absolute);
1291
+ if (!resolved) return { ok: false, reason: "invalid_path" };
1292
+ const target = resolve2(resolved.real, ...resolved.suffix);
1293
+ const rel = relative(context.repoRoot, target);
1294
+ if (!rel || rel === ".") return { ok: false, reason: "invalid_path" };
1295
+ if (isAbsolute2(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
1296
+ return { ok: false, reason: "outside_repository" };
1297
+ }
1298
+ return { ok: true, file: sep === "/" ? rel : rel.split(sep).join("/") };
1299
+ }
1300
+ function canonicalRepositoryPathLegacy(filePath, cwd, repositoryRoot = canonicalGitRoot(cwd)) {
1301
+ if (!repositoryRoot || !filePath || [...filePath].some((char) => char < " " || char === "\x7F" || "~*?[]{}$`\\".includes(char)))
1302
+ return null;
1303
+ const absolute = resolve2(cwd, filePath);
1304
+ let target;
1305
+ try {
1306
+ const stat = lstatSync(absolute);
1307
+ if (!stat.isFile() || stat.isSymbolicLink()) return null;
1308
+ target = realpathSync.native(absolute);
1309
+ } catch (error) {
1310
+ if (!(error instanceof Error) || error.code !== "ENOENT") {
1311
+ return null;
1312
+ }
1313
+ try {
1314
+ const parent = realpathSync.native(dirname4(absolute));
1315
+ if (!lstatSync(parent).isDirectory()) return null;
1316
+ target = resolve2(parent, basename(absolute));
1317
+ } catch {
1318
+ return null;
1319
+ }
1320
+ }
1321
+ const rel = relative(repositoryRoot, target);
1322
+ if (!rel || isAbsolute2(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
1323
+ return null;
1324
+ }
1325
+ return sep === "/" ? rel : rel.split(sep).join("/");
1326
+ }
1327
+ function canonicalRepositoryPath(filePath, contextOrCwd, baseDirOrRoot) {
1328
+ if (typeof contextOrCwd === "string") {
1329
+ return canonicalRepositoryPathLegacy(filePath, contextOrCwd, baseDirOrRoot);
1330
+ }
1331
+ return canonicalRepositoryPathScoped(
1332
+ filePath,
1333
+ contextOrCwd,
1334
+ baseDirOrRoot ?? contextOrCwd.repoRoot
1335
+ );
1336
+ }
1337
+
1338
+ // src/lib/activation.ts
1339
+ var PRIM_REPO_SYNC_ID_KEY = "prim.repoSyncId";
1340
+ var PRIM_REPO_SYNC_REPOSITORY_KEY = "prim.repoSyncRepository";
1341
+ var GIT_TIMEOUT_MS2 = 1e3;
1342
+ var REPO_SYNC_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
1343
+ function isValidRepoSyncId(value) {
1344
+ return typeof value === "string" && REPO_SYNC_ID_RE.test(value);
1345
+ }
1346
+ function localGitConfigValue(cwd, key) {
1347
+ try {
1348
+ const value = execFileSync2("git", ["config", "--local", "--get", key], {
1349
+ cwd,
1350
+ encoding: "utf-8",
1351
+ stdio: ["ignore", "pipe", "ignore"],
1352
+ timeout: GIT_TIMEOUT_MS2
1353
+ }).trim();
1354
+ return value || void 0;
1355
+ } catch {
1356
+ return void 0;
1357
+ }
1358
+ }
1359
+ function repoSyncId(cwd) {
1360
+ const value = localGitConfigValue(cwd, PRIM_REPO_SYNC_ID_KEY);
1361
+ if (!isValidRepoSyncId(value)) return void 0;
1362
+ const repositoryFullName = localGitConfigValue(cwd, PRIM_REPO_SYNC_REPOSITORY_KEY);
1363
+ return repositoryFullName === githubRepositoryFullName(cwd) ? value : void 0;
1364
+ }
1365
+
1199
1366
  // src/hooks/decisions-check.ts
1200
1367
  var DECISIONS_CHECK_TIMEOUT_MS = 1e4;
1201
1368
  var MAX_FILES_PER_REQUEST = 25;
@@ -1207,18 +1374,28 @@ function chunk(items, size) {
1207
1374
  }
1208
1375
  return out;
1209
1376
  }
1210
- async function fetchAffecting(client, batch) {
1377
+ async function fetchAffecting(client, batch, repository) {
1211
1378
  const params = new URLSearchParams();
1379
+ params.set("repoSyncId", repository.repoSyncId);
1380
+ if (repository.branch) params.set("branch", repository.branch);
1212
1381
  for (const file of batch) {
1213
1382
  params.append("files", file);
1214
1383
  }
1215
1384
  try {
1216
- return await daemonOrDirectGet(
1385
+ const response = await daemonOrDirectGet(
1217
1386
  "decisions_affecting",
1218
1387
  `/api/cli/decisions/affecting?${params.toString()}`,
1219
1388
  client,
1220
1389
  DECISIONS_CHECK_TIMEOUT_MS
1221
1390
  );
1391
+ if (response.repoSyncId !== repository.repoSyncId) {
1392
+ return {
1393
+ decisions: [],
1394
+ truncated: response.truncated === true,
1395
+ unavailable: response.unavailable ?? "service did not verify repository-scoped discovery"
1396
+ };
1397
+ }
1398
+ return response;
1222
1399
  } catch (err) {
1223
1400
  const detail = err instanceof Error ? err.message : String(err);
1224
1401
  return { decisions: [], truncated: false, unavailable: `decision check failed: ${detail}` };
@@ -1228,9 +1405,31 @@ async function checkAffectedDecisions(filePaths, deps = defaultDeps) {
1228
1405
  if (filePaths.length === 0) {
1229
1406
  return { decisions: [], truncated: false };
1230
1407
  }
1408
+ const cwd = process.cwd();
1409
+ const root = canonicalGitRoot(cwd);
1410
+ const binding = repoSyncId(cwd);
1411
+ if (!root || !binding) {
1412
+ return {
1413
+ decisions: [],
1414
+ truncated: false,
1415
+ unavailable: "this checkout has no current Primitive repository binding; run prim enable"
1416
+ };
1417
+ }
1418
+ const canonical = filePaths.map(
1419
+ (file) => canonicalRepositoryPath(file, { repoRoot: root }, root)
1420
+ );
1421
+ if (canonical.some((path) => !path.ok)) {
1422
+ return {
1423
+ decisions: [],
1424
+ truncated: false,
1425
+ unavailable: "files must resolve inside this repository; use repository-relative paths"
1426
+ };
1427
+ }
1428
+ const paths = [...new Set(canonical.flatMap((path) => path.ok ? [path.file] : []))];
1429
+ const repository = { repoSyncId: binding, branch: currentBranch(cwd) };
1231
1430
  const client = deps.getClient();
1232
1431
  const responses = await Promise.all(
1233
- chunk(filePaths, MAX_FILES_PER_REQUEST).map((batch) => fetchAffecting(client, batch))
1432
+ chunk(paths, MAX_FILES_PER_REQUEST).map((batch) => fetchAffecting(client, batch, repository))
1234
1433
  );
1235
1434
  const byId = /* @__PURE__ */ new Map();
1236
1435
  let truncated = false;
@@ -1278,7 +1477,7 @@ function formatDecisionsWarning(result) {
1278
1477
  }
1279
1478
  if (result.truncated) {
1280
1479
  lines.push(
1281
- "[prim] result truncated \u2014 more files than the server checks per request; not all decisions shown"
1480
+ "[prim] result truncated \u2014 a discovery scan or result limit was reached; not all decisions shown"
1282
1481
  );
1283
1482
  }
1284
1483
  return lines.join("\n");