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

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 (40) hide show
  1. package/README.md +11 -0
  2. package/dist/{chunk-7IPGD6CD.js → chunk-25DDRMWS.js} +2 -2
  3. package/dist/{chunk-B6EPQ4YH.js → chunk-2IRLFOEL.js} +123 -19
  4. package/dist/{chunk-WKLYYT63.js → chunk-2Q3OGSTV.js} +3 -3
  5. package/dist/{chunk-2DPLT2CQ.js → chunk-5LRKJTLA.js} +1 -1
  6. package/dist/{chunk-AN6XTFIN.js → chunk-6C3CCVDI.js} +2 -2
  7. package/dist/{chunk-NIQAVFP6.js → chunk-7VIMU2HR.js} +1 -1
  8. package/dist/{chunk-2DA2B4EC.js → chunk-EJPKZT5M.js} +2 -2
  9. package/dist/{chunk-HUZXBJJ5.js → chunk-FK7FJPWS.js} +1 -1
  10. package/dist/{chunk-QPMLAD2C.js → chunk-JJI7FU24.js} +2 -2
  11. package/dist/{chunk-PPZBEOWK.js → chunk-JJYS7IPD.js} +3 -3
  12. package/dist/{chunk-RT7DXMTN.js → chunk-K4JWJ6OM.js} +79 -266
  13. package/dist/{chunk-3X77XQGW.js → chunk-KGBMU76T.js} +1 -1
  14. package/dist/{chunk-JOR67B5T.js → chunk-O2WV5SQZ.js} +8 -8
  15. package/dist/{chunk-IL443QTF.js → chunk-X5BN67D2.js} +2 -2
  16. package/dist/{chunk-6QK75B7G.js → chunk-XG7S3LIL.js} +214 -11
  17. package/dist/{chunk-MN4X6RR6.js → chunk-XPS6LG6I.js} +5 -7
  18. package/dist/{chunk-BIAWZMCK.js → chunk-ZNLIGT3J.js} +2 -2
  19. package/dist/daemon/server.js +247 -180
  20. package/dist/hook-runtime/daemon/server.js +247 -180
  21. package/dist/hook-runtime/hooks/post-commit.js +114 -59
  22. package/dist/hook-runtime/hooks/post-rewrite.js +109 -54
  23. package/dist/hook-runtime/hooks/post-tool-use.js +256 -156
  24. package/dist/hook-runtime/hooks/pre-commit.js +177 -33
  25. package/dist/hook-runtime/hooks/pre-tool-use.js +237 -137
  26. package/dist/hook-runtime/hooks/prim-hook.js +281 -181
  27. package/dist/hook-runtime/hooks/session-start.js +209 -109
  28. package/dist/hook-runtime/index.js +615 -515
  29. package/dist/hook-runtime/statusline-main.js +71 -16
  30. package/dist/hooks/post-commit.js +7 -7
  31. package/dist/hooks/post-rewrite.js +6 -6
  32. package/dist/hooks/post-tool-use.js +13 -13
  33. package/dist/hooks/pre-commit.js +4 -4
  34. package/dist/hooks/pre-tool-use.js +10 -11
  35. package/dist/hooks/prim-hook.js +14 -14
  36. package/dist/hooks/session-end.js +4 -4
  37. package/dist/hooks/session-start.js +11 -11
  38. package/dist/index.js +20 -20
  39. package/dist/statusline-main.js +71 -16
  40. package/package.json +1 -1
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  boundedHealthError
3
- } from "./chunk-7IPGD6CD.js";
3
+ } from "./chunk-25DDRMWS.js";
4
4
  import {
5
5
  stripControlChars
6
- } from "./chunk-B6EPQ4YH.js";
6
+ } from "./chunk-2IRLFOEL.js";
7
7
 
8
8
  // src/lib/delivery-backlog.ts
9
9
  var MS_PER_SECOND = 1e3;
@@ -857,6 +857,194 @@ function decisionIngestionStatus(cwd) {
857
857
  return isRepoActiveForCapture(cwd) ? "enabled" : "disabled";
858
858
  }
859
859
 
860
+ // src/lib/bin-path.ts
861
+ import { existsSync as existsSync2, readFileSync as readFileSync3 } from "fs";
862
+ import { dirname as dirname2, isAbsolute as isAbsolute3, join as join4 } from "path";
863
+ import { fileURLToPath } from "url";
864
+
865
+ // src/lib/semver.ts
866
+ function parseSemver(value) {
867
+ if (typeof value !== "string" || value.length > 256) return;
868
+ const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u.exec(
869
+ value
870
+ );
871
+ if (!match) return;
872
+ const prerelease = match[4]?.split(".") ?? [];
873
+ if (prerelease.some((part) => /^\d+$/u.test(part) && part.length > 1 && part.startsWith("0"))) {
874
+ return;
875
+ }
876
+ return { core: [BigInt(match[1]), BigInt(match[2]), BigInt(match[3])], prerelease };
877
+ }
878
+ function compareSemver(left, right) {
879
+ const a = parseSemver(left);
880
+ const b = parseSemver(right);
881
+ if (!a || !b) return;
882
+ for (let i = 0; i < a.core.length; i += 1) {
883
+ if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
884
+ }
885
+ if (a.prerelease.length === 0 || b.prerelease.length === 0) {
886
+ return a.prerelease.length === b.prerelease.length ? 0 : a.prerelease.length === 0 ? 1 : -1;
887
+ }
888
+ for (let i = 0; i < Math.max(a.prerelease.length, b.prerelease.length); i += 1) {
889
+ const x = a.prerelease[i];
890
+ const y = b.prerelease[i];
891
+ if (x === void 0 || y === void 0) return x === void 0 ? -1 : 1;
892
+ if (x === y) continue;
893
+ const xNumeric = /^\d+$/u.test(x);
894
+ const yNumeric = /^\d+$/u.test(y);
895
+ if (xNumeric && yNumeric) return BigInt(x) < BigInt(y) ? -1 : 1;
896
+ if (xNumeric !== yNumeric) return xNumeric ? -1 : 1;
897
+ return x < y ? -1 : 1;
898
+ }
899
+ return 0;
900
+ }
901
+
902
+ // src/lib/bin-path.ts
903
+ var PKG_NAME = "@primitive.ai/prim";
904
+ var ROOT_WALK_LIMIT = 6;
905
+ var STABLE_HOOK_LAUNCHER_NAME = "prim-hook-launcher-v1";
906
+ var resolvedRoot;
907
+ function locateRoot() {
908
+ if (resolvedRoot !== void 0) {
909
+ return resolvedRoot;
910
+ }
911
+ let dir = dirname2(fileURLToPath(import.meta.url));
912
+ for (let depth = 0; depth < ROOT_WALK_LIMIT; depth++) {
913
+ const manifestPath = join4(dir, "package.json");
914
+ if (existsSync2(manifestPath)) {
915
+ try {
916
+ const manifest = JSON.parse(readFileSync3(manifestPath, "utf-8"));
917
+ if (manifest.name === PKG_NAME && manifest.bin) {
918
+ resolvedRoot = { dir, version: manifest.version, bin: manifest.bin };
919
+ return resolvedRoot;
920
+ }
921
+ } catch {
922
+ }
923
+ }
924
+ const parent = dirname2(dir);
925
+ if (parent === dir) {
926
+ break;
927
+ }
928
+ dir = parent;
929
+ }
930
+ resolvedRoot = null;
931
+ return resolvedRoot;
932
+ }
933
+ function binFile(bin) {
934
+ const root = locateRoot();
935
+ const rel = root?.bin[bin];
936
+ if (!root || !rel) {
937
+ return null;
938
+ }
939
+ return isAbsolute3(rel) ? rel : join4(root.dir, rel);
940
+ }
941
+ function packageVersion() {
942
+ return locateRoot()?.version ?? null;
943
+ }
944
+ function packageRoot() {
945
+ return locateRoot()?.dir ?? null;
946
+ }
947
+ function shellQuote(value) {
948
+ return `'${value.replaceAll("'", `'"'"'`)}'`;
949
+ }
950
+ function shellWord(value) {
951
+ return /^[A-Za-z0-9_@%+=:,./-]+$/u.test(value) ? value : shellQuote(value);
952
+ }
953
+ function pinnedNpxArgs(bin, args = [], options = {}) {
954
+ const version = packageVersion();
955
+ if (!version) throw new Error("cannot determine Primitive package version");
956
+ return [
957
+ "--yes",
958
+ "--ignore-scripts",
959
+ ...options.preferOnline ? ["--prefer-online"] : [],
960
+ "-p",
961
+ `${PKG_NAME}@${version}`,
962
+ bin,
963
+ ...args
964
+ ];
965
+ }
966
+ function pinnedNpxCommand(bin, args = [], options = {}) {
967
+ return ["npx", ...pinnedNpxArgs(bin, args, options)].map(shellWord).join(" ");
968
+ }
969
+ function pinnedHookCommand(bin, args = "") {
970
+ const suffix = args ? ` ${args}` : "";
971
+ const fallback = `${pinnedNpxCommand(bin)}${suffix}`;
972
+ const file = binFile(bin);
973
+ if (!file) return fallback;
974
+ return `if [ -x ${shellQuote(process.execPath)} ] && [ -f ${shellQuote(file)} ]; then ${shellQuote(process.execPath)} ${shellQuote(file)}${suffix}; else ${fallback}; fi`;
975
+ }
976
+ var STABLE_HOOK_ARGUMENTS_RE = /^[-A-Za-z0-9_ ]*$/u;
977
+ var STABLE_HOOK_BIN_RE = /^[A-Za-z0-9][A-Za-z0-9-]*$/u;
978
+ var PRIM_CONFIG_SHELL_RESOLVER = [
979
+ 'prim_absolute() { case "$1" in [/]* ) ;; *) return 1 ;; esac; case "$1" in [[:space:]]*|*[[:space:]]|*[/][/]*|*[/].[/]*|*[/]..[/]*|*[/].|*[/]..|?*[/]) return 1 ;; esac; }; ',
980
+ 'prim_config=${PRIM_CONFIG_DIR:-}; if ! prim_absolute "$prim_config"; then prim_config=${XDG_CONFIG_HOME:-}; if prim_absolute "$prim_config"; then ',
981
+ 'case "$prim_config" in /) prim_config=/prim ;; *) prim_config="$prim_config/prim" ;; esac; ',
982
+ 'else prim_home=${HOME:-}; prim_absolute "$prim_home" || exit 78; case "$prim_home" in /) prim_config=/.config/prim ;; *) prim_config="$prim_home/.config/prim" ;; esac; ',
983
+ "fi; fi; "
984
+ ].join("");
985
+ function stableHookCommand(bin, args = "") {
986
+ if (!STABLE_HOOK_BIN_RE.test(bin) || !STABLE_HOOK_ARGUMENTS_RE.test(args)) {
987
+ throw new Error("invalid stable hook command");
988
+ }
989
+ const resolver = [
990
+ // #242 and earlier identify managed commands by this package marker. Keep
991
+ // it as inert data during the rolling window so an older uninstall or
992
+ // reinstall can remove the stable entry without invoking npm or PATH.
993
+ `prim_legacy_reader='-p ${PKG_NAME}@stable prim-shim.sh ${bin} '; `,
994
+ PRIM_CONFIG_SHELL_RESOLVER,
995
+ `case "$prim_config" in /) prim_launcher=/${STABLE_HOOK_LAUNCHER_NAME} ;; *) prim_launcher="$prim_config/${STABLE_HOOK_LAUNCHER_NAME}" ;; esac; `,
996
+ 'exec "$prim_launcher" "$@"'
997
+ ].join("");
998
+ const suffix = args ? ` ${args}` : "";
999
+ return `/bin/sh -c ${shellQuote(resolver)} ${STABLE_HOOK_LAUNCHER_NAME} ${bin}${suffix}`;
1000
+ }
1001
+ function detachedHookShimCommand(bin, args = "") {
1002
+ return `payload=$(cat); { trap '' HUP; printf '%s' "$payload" | { ${stableHookCommand(bin, args)}; }; } </dev/null >/dev/null 2>&1 &`;
1003
+ }
1004
+ function commandMatchesBin(command, bin) {
1005
+ if (!command) {
1006
+ return false;
1007
+ }
1008
+ const c = command.trim();
1009
+ if (c === bin || c.startsWith(`${bin} `)) {
1010
+ return true;
1011
+ }
1012
+ const exactBin = new RegExp(`(?:^|\\s)${bin.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s|;|$)`);
1013
+ return c.includes(`command -v ${bin} `) || c.includes(`-p ${PKG_NAME}@`) && exactBin.test(c) || c.includes(STABLE_HOOK_LAUNCHER_NAME) && exactBin.test(c);
1014
+ }
1015
+ function exactPinnedNpxVersion(command, bin) {
1016
+ const escapedBin = bin.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1017
+ const shellQuoted = `'(?:[^']|'"'"')*'`;
1018
+ const safeArgs = "(?: [A-Za-z0-9_@%+=:,./-]+)*";
1019
+ const direct = new RegExp(
1020
+ `^npx --yes --ignore-scripts -p @primitive\\.ai/prim@(?<version>[0-9A-Za-z.+-]+) ${escapedBin}${safeArgs}$`,
1021
+ "u"
1022
+ ).exec(command);
1023
+ const wrapped = new RegExp(
1024
+ `^if \\[ -x (?<node>${shellQuoted}) \\] && \\[ -f (?<entry>${shellQuoted}) \\]; then \\k<node> \\k<entry>(?<args>${safeArgs}); else npx --yes --ignore-scripts -p @primitive\\.ai/prim@(?<version>[0-9A-Za-z.+-]+) ${escapedBin}\\k<args>; fi$`,
1025
+ "u"
1026
+ ).exec(command);
1027
+ const version = direct?.groups?.version ?? wrapped?.groups?.version;
1028
+ return version && compareSemver(version, version) !== void 0 ? version : void 0;
1029
+ }
1030
+ function hookCommandResolution(command, bin) {
1031
+ const trimmed = command?.trim();
1032
+ if (!trimmed || !commandMatchesBin(trimmed, bin)) return void 0;
1033
+ if (trimmed.includes(STABLE_HOOK_LAUNCHER_NAME)) return { kind: "stable_launcher" };
1034
+ const version = exactPinnedNpxVersion(trimmed, bin);
1035
+ return version ? { kind: "exact_npx_fallback", version } : { kind: "legacy_path" };
1036
+ }
1037
+ function hookCommandResolutions(commands, bins) {
1038
+ const resolutions = [];
1039
+ for (const command of commands) {
1040
+ for (const bin of bins) {
1041
+ const resolution = hookCommandResolution(command, bin);
1042
+ if (resolution) resolutions.push(resolution);
1043
+ }
1044
+ }
1045
+ return resolutions;
1046
+ }
1047
+
860
1048
  // src/lib/atomic-file.ts
861
1049
  import { randomUUID } from "crypto";
862
1050
  import {
@@ -870,7 +1058,7 @@ import {
870
1058
  rmSync,
871
1059
  writeFileSync
872
1060
  } from "fs";
873
- import { dirname as dirname2 } from "path";
1061
+ import { dirname as dirname3 } from "path";
874
1062
  var DEFAULT_FILE_MODE = 438;
875
1063
  function syncDirectory(path) {
876
1064
  const fd = openSync(path, constants.O_RDONLY);
@@ -893,14 +1081,14 @@ function syncCreatedParentChain(firstCreatedParent, parent) {
893
1081
  let current = parent;
894
1082
  while (current !== firstCreatedParent) {
895
1083
  createdDirectories.push(current);
896
- const next = dirname2(current);
1084
+ const next = dirname3(current);
897
1085
  if (next === current) {
898
1086
  throw new Error("recursive mkdir returned a path outside the target parent chain");
899
1087
  }
900
1088
  current = next;
901
1089
  }
902
1090
  createdDirectories.push(firstCreatedParent);
903
- syncDirectory(dirname2(firstCreatedParent));
1091
+ syncDirectory(dirname3(firstCreatedParent));
904
1092
  for (const directory of createdDirectories.reverse().slice(0, -1)) {
905
1093
  syncDirectory(directory);
906
1094
  }
@@ -912,7 +1100,7 @@ function ensureDurableDirectory(path, mode) {
912
1100
  }
913
1101
  }
914
1102
  function atomicWriteFile(target, content, options = {}) {
915
- const parent = dirname2(target);
1103
+ const parent = dirname3(target);
916
1104
  if (options.ensureParent) {
917
1105
  ensureDurableDirectory(parent);
918
1106
  }
@@ -956,19 +1144,19 @@ function processIsAlive(pid, probe = process.kill) {
956
1144
 
957
1145
  // src/lib/file-lock.ts
958
1146
  import { randomBytes } from "crypto";
959
- import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync3, rmSync as rmSync2, statSync, writeFileSync as writeFileSync2 } from "fs";
960
- import { dirname as dirname3, join as join4 } from "path";
1147
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync4, rmSync as rmSync2, statSync, writeFileSync as writeFileSync2 } from "fs";
1148
+ import { dirname as dirname4, join as join5 } from "path";
961
1149
  var DEFAULT_TIMEOUT_MS = 3e4;
962
1150
  var DEFAULT_POLL_MS = 50;
963
1151
  var DEFAULT_INIT_GRACE_MS = 2e3;
964
1152
  var DIRECTORY_MODE = 448;
965
1153
  var FILE_MODE = 384;
966
1154
  function ownerPath(lockDir) {
967
- return join4(lockDir, "owner.json");
1155
+ return join5(lockDir, "owner.json");
968
1156
  }
969
1157
  function readOwner(lockDir) {
970
1158
  try {
971
- const owner = JSON.parse(readFileSync3(ownerPath(lockDir), "utf8"));
1159
+ const owner = JSON.parse(readFileSync4(ownerPath(lockDir), "utf8"));
972
1160
  if (!Number.isInteger(owner.pid) || owner.pid <= 0 || typeof owner.nonce !== "string" || typeof owner.createdAt !== "number") {
973
1161
  return null;
974
1162
  }
@@ -1056,7 +1244,7 @@ function defaultSleepSync(ms) {
1056
1244
  Atomics.wait(syncSleeper, 0, 0, ms);
1057
1245
  }
1058
1246
  function withFileLockSync(lockDir, operation, options = {}) {
1059
- mkdirSync2(dirname3(lockDir), { recursive: true, mode: DIRECTORY_MODE });
1247
+ mkdirSync2(dirname4(lockDir), { recursive: true, mode: DIRECTORY_MODE });
1060
1248
  const nowMs = options.nowMs ?? Date.now;
1061
1249
  const alive = options.processAlive ?? processIsAlive;
1062
1250
  const deadline = nowMs() + (options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
@@ -1080,7 +1268,7 @@ function withFileLockSync(lockDir, operation, options = {}) {
1080
1268
  }
1081
1269
  }
1082
1270
  async function withFileLock(lockDir, operation, options = {}) {
1083
- mkdirSync2(dirname3(lockDir), { recursive: true, mode: DIRECTORY_MODE });
1271
+ mkdirSync2(dirname4(lockDir), { recursive: true, mode: DIRECTORY_MODE });
1084
1272
  const nowMs = options.nowMs ?? Date.now;
1085
1273
  const alive = options.processAlive ?? processIsAlive;
1086
1274
  const deadline = nowMs() + (options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
@@ -1165,5 +1353,20 @@ export {
1165
1353
  atomicWriteFile,
1166
1354
  processIsAlive,
1167
1355
  withFileLockSync,
1168
- withFileLock
1356
+ withFileLock,
1357
+ compareSemver,
1358
+ STABLE_HOOK_LAUNCHER_NAME,
1359
+ binFile,
1360
+ packageVersion,
1361
+ packageRoot,
1362
+ shellQuote,
1363
+ pinnedNpxArgs,
1364
+ pinnedNpxCommand,
1365
+ pinnedHookCommand,
1366
+ PRIM_CONFIG_SHELL_RESOLVER,
1367
+ stableHookCommand,
1368
+ detachedHookShimCommand,
1369
+ commandMatchesBin,
1370
+ hookCommandResolution,
1371
+ hookCommandResolutions
1169
1372
  };
@@ -1,26 +1,24 @@
1
1
  import {
2
2
  formatStatusline
3
- } from "./chunk-IL443QTF.js";
3
+ } from "./chunk-X5BN67D2.js";
4
4
  import {
5
5
  getSiteUrl,
6
6
  isSessionEnded,
7
7
  terminalSafeText
8
- } from "./chunk-B6EPQ4YH.js";
9
- import {
10
- packageVersion
11
- } from "./chunk-RT7DXMTN.js";
8
+ } from "./chunk-2IRLFOEL.js";
12
9
  import {
13
10
  daemonPrincipalsMatch,
14
11
  daemonRequest,
15
12
  resolveDaemonPrincipal
16
- } from "./chunk-2DPLT2CQ.js";
13
+ } from "./chunk-5LRKJTLA.js";
17
14
  import {
18
15
  decisionIngestionStatus,
19
16
  gitToplevel,
17
+ packageVersion,
20
18
  primConfigDirectory,
21
19
  repositoryBindingState,
22
20
  withFileLock
23
- } from "./chunk-6QK75B7G.js";
21
+ } from "./chunk-XG7S3LIL.js";
24
22
 
25
23
  // src/hooks/codex-context.ts
26
24
  import { createHash, randomBytes as randomBytes2 } from "crypto";
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  boundedHealthError
3
- } from "./chunk-7IPGD6CD.js";
3
+ } from "./chunk-25DDRMWS.js";
4
4
  import {
5
5
  terminalSafeLine
6
- } from "./chunk-B6EPQ4YH.js";
6
+ } from "./chunk-2IRLFOEL.js";
7
7
 
8
8
  // src/hooks/redact.ts
9
9
  import { existsSync, readFileSync, statSync } from "fs";