@primitive.ai/prim 0.1.0-alpha.128 → 0.1.0-alpha.129
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/dist/{chunk-6QAXVFRJ.js → chunk-BVGASV3B.js} +942 -123
- package/dist/{chunk-M6KIDB5R.js → chunk-DYXCJ6WZ.js} +10 -10
- package/dist/{chunk-B3ICJZXV.js → chunk-FHDTJP2C.js} +2 -2
- package/dist/{chunk-XINFTNA2.js → chunk-HDL3UXYJ.js} +5 -5
- package/dist/{chunk-C357P37M.js → chunk-IJ3W2YBM.js} +4 -4
- package/dist/{chunk-N6PZE7HX.js → chunk-IZAEAALI.js} +2 -2
- package/dist/{chunk-PTZ6BWSU.js → chunk-KZNOT2YH.js} +2 -2
- package/dist/{chunk-P4O7L7JZ.js → chunk-L7U3G4XR.js} +2 -2
- package/dist/{chunk-EQWUCFXK.js → chunk-LBZJD2I4.js} +3 -5
- package/dist/{chunk-PRJ7P6J5.js → chunk-MMMY57UA.js} +22 -837
- package/dist/{chunk-WEGZGF3K.js → chunk-NWWTFQP2.js} +1 -1
- package/dist/{chunk-NYDM4HSS.js → chunk-QSDWWEHF.js} +2 -2
- package/dist/{chunk-3KPTVK4N.js → chunk-RAEOSDQ6.js} +1 -1
- package/dist/{chunk-E57UVUJL.js → chunk-RS5ADTDF.js} +4 -4
- package/dist/{chunk-ANMNGGTN.js → chunk-RWJ7WHTJ.js} +2 -2
- package/dist/{chunk-RDT2IV2Z.js → chunk-SJE7OWVS.js} +3 -3
- package/dist/{chunk-VLT2KGPE.js → chunk-USJ4JGDC.js} +3 -3
- package/dist/{chunk-ZCT23NBS.js → chunk-YPHDV5AS.js} +2 -2
- package/dist/{chunk-QQ3G5SXX.js → chunk-YSV43WLK.js} +1 -1
- package/dist/{chunk-4DYEBYKM.js → chunk-ZKIBD5MO.js} +2 -2
- package/dist/daemon/server.js +624 -289
- package/dist/hook-runtime/daemon/server.js +624 -289
- package/dist/hook-runtime/hooks/post-commit.js +307 -186
- package/dist/hook-runtime/hooks/post-rewrite.js +302 -181
- package/dist/hook-runtime/hooks/post-tool-use.js +490 -276
- package/dist/hook-runtime/hooks/pre-commit.js +516 -181
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +221 -7
- package/dist/hook-runtime/hooks/prim-hook.js +511 -297
- package/dist/hook-runtime/hooks/session-end.js +229 -229
- package/dist/hook-runtime/hooks/session-start.js +170 -14
- package/dist/hook-runtime/index.js +1623 -1607
- package/dist/hook-runtime/statusline-main.js +245 -124
- package/dist/hooks/post-commit.js +7 -7
- package/dist/hooks/post-rewrite.js +6 -6
- package/dist/hooks/post-tool-use.js +15 -15
- package/dist/hooks/pre-commit.js +4 -4
- package/dist/hooks/pre-tool-use-worker.js +9 -9
- package/dist/hooks/prim-hook.js +16 -16
- package/dist/hooks/session-end.js +4 -4
- package/dist/hooks/session-start.js +12 -13
- package/dist/index.js +24 -24
- package/dist/statusline-main.js +245 -124
- package/package.json +1 -1
|
@@ -58,30 +58,30 @@ import { dirname as dirname14, join as join27 } from "path";
|
|
|
58
58
|
import { fileURLToPath as fileURLToPath5 } from "url";
|
|
59
59
|
|
|
60
60
|
// src/binding.ts
|
|
61
|
-
import { existsSync as
|
|
62
|
-
import { dirname as
|
|
61
|
+
import { existsSync as existsSync6, readFileSync as readFileSync9 } from "fs";
|
|
62
|
+
import { dirname as dirname8, join as join12 } from "path";
|
|
63
63
|
|
|
64
64
|
// src/lib/credentials.ts
|
|
65
|
-
import { createHash as
|
|
66
|
-
import { readFileSync as
|
|
67
|
-
import { join as
|
|
65
|
+
import { createHash as createHash7 } from "crypto";
|
|
66
|
+
import { readFileSync as readFileSync8 } from "fs";
|
|
67
|
+
import { join as join11 } from "path";
|
|
68
68
|
|
|
69
69
|
// src/lib/machine-credentials.ts
|
|
70
70
|
import { execFileSync as execFileSync2 } from "child_process";
|
|
71
|
-
import { createHash as
|
|
71
|
+
import { createHash as createHash6, randomBytes as randomBytes4, randomUUID as randomUUID2 } from "crypto";
|
|
72
72
|
import {
|
|
73
|
-
chmodSync,
|
|
74
|
-
existsSync as
|
|
73
|
+
chmodSync as chmodSync2,
|
|
74
|
+
existsSync as existsSync5,
|
|
75
75
|
linkSync,
|
|
76
|
-
lstatSync as
|
|
77
|
-
mkdirSync as
|
|
78
|
-
readFileSync as
|
|
79
|
-
rmSync as
|
|
80
|
-
writeFileSync as
|
|
76
|
+
lstatSync as lstatSync5,
|
|
77
|
+
mkdirSync as mkdirSync4,
|
|
78
|
+
readFileSync as readFileSync7,
|
|
79
|
+
rmSync as rmSync7,
|
|
80
|
+
writeFileSync as writeFileSync4
|
|
81
81
|
} from "fs";
|
|
82
|
-
import { createRequire } from "module";
|
|
83
|
-
import { dirname as
|
|
84
|
-
import { fileURLToPath as
|
|
82
|
+
import { createRequire as createRequire2 } from "module";
|
|
83
|
+
import { dirname as dirname7, join as join10 } from "path";
|
|
84
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
85
85
|
|
|
86
86
|
// src/lib/atomic-file.ts
|
|
87
87
|
import { randomUUID } from "crypto";
|
|
@@ -332,18 +332,71 @@ async function withFileLock(lockDir, operation, options = {}) {
|
|
|
332
332
|
}
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
-
// src/lib/
|
|
336
|
-
import {
|
|
337
|
-
import {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
335
|
+
// src/lib/hook-runtime.ts
|
|
336
|
+
import { createHash, randomBytes as randomBytes2 } from "crypto";
|
|
337
|
+
import {
|
|
338
|
+
constants as constants2,
|
|
339
|
+
accessSync,
|
|
340
|
+
chmodSync,
|
|
341
|
+
copyFileSync,
|
|
342
|
+
existsSync as existsSync4,
|
|
343
|
+
lstatSync,
|
|
344
|
+
mkdirSync as mkdirSync3,
|
|
345
|
+
mkdtempSync,
|
|
346
|
+
readFileSync as readFileSync3,
|
|
347
|
+
readdirSync,
|
|
348
|
+
realpathSync,
|
|
349
|
+
renameSync as renameSync2,
|
|
350
|
+
rmSync as rmSync3,
|
|
351
|
+
statSync as statSync2,
|
|
352
|
+
utimesSync,
|
|
353
|
+
writeFileSync as writeFileSync3
|
|
354
|
+
} from "fs";
|
|
355
|
+
import { delimiter, dirname as dirname5, isAbsolute as isAbsolute3, join as join5, resolve } from "path";
|
|
342
356
|
|
|
343
357
|
// src/lib/bin-path.ts
|
|
344
358
|
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
345
359
|
import { dirname as dirname3, isAbsolute, join as join2 } from "path";
|
|
346
360
|
import { fileURLToPath } from "url";
|
|
361
|
+
|
|
362
|
+
// src/lib/semver.ts
|
|
363
|
+
function parseSemver(value) {
|
|
364
|
+
if (typeof value !== "string" || value.length > 256) return;
|
|
365
|
+
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(
|
|
366
|
+
value
|
|
367
|
+
);
|
|
368
|
+
if (!match) return;
|
|
369
|
+
const prerelease = match[4]?.split(".") ?? [];
|
|
370
|
+
if (prerelease.some((part) => /^\d+$/u.test(part) && part.length > 1 && part.startsWith("0"))) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
return { core: [BigInt(match[1]), BigInt(match[2]), BigInt(match[3])], prerelease };
|
|
374
|
+
}
|
|
375
|
+
function compareSemver(left, right) {
|
|
376
|
+
const a = parseSemver(left);
|
|
377
|
+
const b = parseSemver(right);
|
|
378
|
+
if (!a || !b) return;
|
|
379
|
+
for (let i = 0; i < a.core.length; i += 1) {
|
|
380
|
+
if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
|
|
381
|
+
}
|
|
382
|
+
if (a.prerelease.length === 0 || b.prerelease.length === 0) {
|
|
383
|
+
return a.prerelease.length === b.prerelease.length ? 0 : a.prerelease.length === 0 ? 1 : -1;
|
|
384
|
+
}
|
|
385
|
+
for (let i = 0; i < Math.max(a.prerelease.length, b.prerelease.length); i += 1) {
|
|
386
|
+
const x = a.prerelease[i];
|
|
387
|
+
const y = b.prerelease[i];
|
|
388
|
+
if (x === void 0 || y === void 0) return x === void 0 ? -1 : 1;
|
|
389
|
+
if (x === y) continue;
|
|
390
|
+
const xNumeric = /^\d+$/u.test(x);
|
|
391
|
+
const yNumeric = /^\d+$/u.test(y);
|
|
392
|
+
if (xNumeric && yNumeric) return BigInt(x) < BigInt(y) ? -1 : 1;
|
|
393
|
+
if (xNumeric !== yNumeric) return xNumeric ? -1 : 1;
|
|
394
|
+
return x < y ? -1 : 1;
|
|
395
|
+
}
|
|
396
|
+
return 0;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// src/lib/bin-path.ts
|
|
347
400
|
var PKG_NAME = "@primitive.ai/prim";
|
|
348
401
|
var ROOT_WALK_LIMIT = 6;
|
|
349
402
|
var STABLE_HOOK_LAUNCHER_NAME = "prim-hook-launcher-v1";
|
|
@@ -418,6 +471,309 @@ function detachedHookShimCommand(bin, args = "") {
|
|
|
418
471
|
return `payload=$(cat); { trap '' HUP; printf '%s' "$payload" | { ${stableHookCommand(bin, args)}; }; } </dev/null >/dev/null 2>&1 &`;
|
|
419
472
|
}
|
|
420
473
|
|
|
474
|
+
// src/lib/git-hook-contract.ts
|
|
475
|
+
var GIT_HOOK_ENTRYPOINT_NAME = "prim-git-hook-v1";
|
|
476
|
+
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
477
|
+
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
478
|
+
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
479
|
+
# where prim is active (git config prim.active true) and never fails the hook.
|
|
480
|
+
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
481
|
+
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
482
|
+
[ -x "$prim_launcher" ] || exit 0
|
|
483
|
+
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
484
|
+
prim_hook=\${1-}
|
|
485
|
+
if [ "$#" -gt 0 ]; then shift; fi
|
|
486
|
+
case "$prim_hook" in
|
|
487
|
+
pre-commit)
|
|
488
|
+
"$prim_launcher" prim-pre-commit "$@" || :
|
|
489
|
+
;;
|
|
490
|
+
post-commit)
|
|
491
|
+
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
492
|
+
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
493
|
+
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
494
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
495
|
+
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
496
|
+
(
|
|
497
|
+
trap '' HUP
|
|
498
|
+
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
499
|
+
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
500
|
+
) </dev/null >/dev/null 2>&1 &
|
|
501
|
+
;;
|
|
502
|
+
post-rewrite)
|
|
503
|
+
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
504
|
+
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
505
|
+
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
506
|
+
rm -f "$prim_pairs"
|
|
507
|
+
exit 0
|
|
508
|
+
fi
|
|
509
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
510
|
+
(
|
|
511
|
+
trap '' HUP
|
|
512
|
+
trap 'rm -f "$prim_pairs"' 0
|
|
513
|
+
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
514
|
+
) </dev/null >/dev/null 2>&1 &
|
|
515
|
+
;;
|
|
516
|
+
esac
|
|
517
|
+
exit 0
|
|
518
|
+
`;
|
|
519
|
+
|
|
520
|
+
// src/lib/native-keyring.ts
|
|
521
|
+
import { existsSync as existsSync3 } from "fs";
|
|
522
|
+
import { createRequire } from "module";
|
|
523
|
+
import { dirname as dirname4, join as join3 } from "path";
|
|
524
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
525
|
+
var require2 = createRequire(import.meta.url);
|
|
526
|
+
var KEYRING_NATIVE_FILES = ["native/keyring.cjs", "native/keyring.node"];
|
|
527
|
+
|
|
528
|
+
// src/lib/paths.ts
|
|
529
|
+
import { homedir } from "os";
|
|
530
|
+
import { isAbsolute as isAbsolute2, join as join4, normalize } from "path";
|
|
531
|
+
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
532
|
+
function absolutePath(value, variable) {
|
|
533
|
+
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
534
|
+
throw new Error(`${variable} contains unsafe characters`);
|
|
535
|
+
}
|
|
536
|
+
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
537
|
+
const normalized = normalize(value);
|
|
538
|
+
return normalized === value ? value : void 0;
|
|
539
|
+
}
|
|
540
|
+
function resolvePrimConfigDirectory(options = {}) {
|
|
541
|
+
const env = options.env ?? process.env;
|
|
542
|
+
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
543
|
+
if (explicit) return { path: explicit, source: "explicit" };
|
|
544
|
+
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
545
|
+
if (xdgConfigHome) return { path: join4(xdgConfigHome, "prim"), source: "xdg" };
|
|
546
|
+
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
547
|
+
if (!home) {
|
|
548
|
+
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
549
|
+
}
|
|
550
|
+
return { path: join4(home, ".config", "prim"), source: "default" };
|
|
551
|
+
}
|
|
552
|
+
function primConfigDirectory(options = {}) {
|
|
553
|
+
return resolvePrimConfigDirectory(options).path;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/lib/hook-runtime.ts
|
|
557
|
+
var HOOK_RUNTIME_SCHEMA_VERSION = 1;
|
|
558
|
+
var RELEASE_PREFIX = "release-";
|
|
559
|
+
var RELEASE_NAME_RE = /^release-[0-9a-f]{64}$/u;
|
|
560
|
+
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
561
|
+
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
562
|
+
var HOOK_RUNTIME_ENTRIES = {
|
|
563
|
+
// SessionStart self-healing resolves this exact CLI entry to run
|
|
564
|
+
// `daemon ensure`; it must travel with the immutable hook release.
|
|
565
|
+
prim: "dist/index.js",
|
|
566
|
+
"prim-hook": "dist/hooks/prim-hook.js",
|
|
567
|
+
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
568
|
+
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
569
|
+
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
570
|
+
"prim-pre-tool-use-worker": "dist/hooks/pre-tool-use-worker.js",
|
|
571
|
+
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
572
|
+
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
573
|
+
"prim-session-start": "dist/hooks/session-start.js",
|
|
574
|
+
"prim-session-end": "dist/hooks/session-end.js",
|
|
575
|
+
"prim-daemon-server": "dist/daemon/server.js",
|
|
576
|
+
"prim-statusline": "dist/statusline-main.js"
|
|
577
|
+
};
|
|
578
|
+
var SOURCE_ENTRIES = Object.fromEntries(
|
|
579
|
+
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
580
|
+
bin,
|
|
581
|
+
target.replace(/^dist\//u, "")
|
|
582
|
+
])
|
|
583
|
+
);
|
|
584
|
+
function hookRuntimePaths(options = {}) {
|
|
585
|
+
const env = options.env ?? process.env;
|
|
586
|
+
const configDir = primConfigDirectory({
|
|
587
|
+
...options,
|
|
588
|
+
env,
|
|
589
|
+
homeDir: options.homeDir ?? env.HOME ?? ""
|
|
590
|
+
});
|
|
591
|
+
const runtimeDir = join5(configDir, "hook-runtime");
|
|
592
|
+
return {
|
|
593
|
+
configDir,
|
|
594
|
+
launcher: join5(configDir, STABLE_HOOK_LAUNCHER_NAME),
|
|
595
|
+
gitHookEntrypoint: join5(configDir, GIT_HOOK_ENTRYPOINT_NAME),
|
|
596
|
+
runtimeDir,
|
|
597
|
+
releasesDir: join5(runtimeDir, "releases"),
|
|
598
|
+
current: join5(runtimeDir, "current"),
|
|
599
|
+
selectionLock: join5(runtimeDir, "selection.lock")
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
function sha256(path) {
|
|
603
|
+
return createHash("sha256").update(readFileSync3(path)).digest("hex");
|
|
604
|
+
}
|
|
605
|
+
function exactManifest(manifest) {
|
|
606
|
+
return `${JSON.stringify(manifest, null, 2)}
|
|
607
|
+
`;
|
|
608
|
+
}
|
|
609
|
+
function exactKeys(value, expected) {
|
|
610
|
+
const actual = Object.keys(value).sort();
|
|
611
|
+
const wanted = [...expected].sort();
|
|
612
|
+
return actual.length === wanted.length && actual.every((key, index) => key === wanted[index]);
|
|
613
|
+
}
|
|
614
|
+
function releaseId(manifest) {
|
|
615
|
+
return createHash("sha256").update(exactManifest(manifest)).digest("hex");
|
|
616
|
+
}
|
|
617
|
+
function runtimePackageJson(manifest) {
|
|
618
|
+
return `${JSON.stringify(
|
|
619
|
+
{
|
|
620
|
+
name: "@primitive.ai/prim",
|
|
621
|
+
version: manifest.version,
|
|
622
|
+
type: "module",
|
|
623
|
+
bin: HOOK_RUNTIME_ENTRIES
|
|
624
|
+
},
|
|
625
|
+
null,
|
|
626
|
+
2
|
|
627
|
+
)}
|
|
628
|
+
`;
|
|
629
|
+
}
|
|
630
|
+
function validateRelease(releaseDir, manifest) {
|
|
631
|
+
try {
|
|
632
|
+
if (!directReleaseLayout(releaseDir, manifest)) return false;
|
|
633
|
+
if (readFileSync3(join5(releaseDir, "manifest.json"), "utf8") !== exactManifest(manifest)) {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
if (readFileSync3(join5(releaseDir, "package.json"), "utf8") !== runtimePackageJson(manifest)) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
if (readFileSync3(join5(releaseDir, "node"), "utf8") !== `${manifest.nodePath}
|
|
640
|
+
`) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
for (const [bin, relativePath] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
644
|
+
const target = join5(releaseDir, relativePath);
|
|
645
|
+
if (sha256(target) !== manifest.files[bin]) return false;
|
|
646
|
+
}
|
|
647
|
+
for (const [path, digest] of Object.entries(manifest.native ?? {})) {
|
|
648
|
+
if (sha256(join5(releaseDir, path)) !== digest) return false;
|
|
649
|
+
}
|
|
650
|
+
return true;
|
|
651
|
+
} catch {
|
|
652
|
+
return false;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function directReleaseLayout(releaseDir, manifest) {
|
|
656
|
+
try {
|
|
657
|
+
const directories = /* @__PURE__ */ new Set([releaseDir]);
|
|
658
|
+
for (const relativePath of [
|
|
659
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
660
|
+
...Object.keys(manifest.native ?? {})
|
|
661
|
+
]) {
|
|
662
|
+
for (let directory = dirname5(relativePath); directory !== "."; directory = dirname5(directory)) {
|
|
663
|
+
directories.add(join5(releaseDir, directory));
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (![...directories].every((directory) => lstatSync(directory).isDirectory())) return false;
|
|
667
|
+
const files = [
|
|
668
|
+
"manifest.json",
|
|
669
|
+
"package.json",
|
|
670
|
+
"node",
|
|
671
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
672
|
+
...Object.keys(manifest.native ?? {})
|
|
673
|
+
];
|
|
674
|
+
const owned = /* @__PURE__ */ new Set([...directories, ...files.map((path) => join5(releaseDir, path))]);
|
|
675
|
+
if (![...directories].every(
|
|
676
|
+
(directory) => readdirSync(directory).every((name2) => owned.has(join5(directory, name2)))
|
|
677
|
+
))
|
|
678
|
+
return false;
|
|
679
|
+
return files.every((relativePath) => lstatSync(join5(releaseDir, relativePath)).isFile());
|
|
680
|
+
} catch {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
function readReleaseManifest(releaseDir, releaseName) {
|
|
685
|
+
try {
|
|
686
|
+
const manifestPath = join5(releaseDir, "manifest.json");
|
|
687
|
+
const manifestStat = statSync2(manifestPath);
|
|
688
|
+
if (!manifestStat.isFile() || manifestStat.size > MAX_MANIFEST_BYTES) return null;
|
|
689
|
+
const raw = readFileSync3(manifestPath, "utf8");
|
|
690
|
+
const parsed = JSON.parse(raw);
|
|
691
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
692
|
+
if (!exactKeys(parsed, [
|
|
693
|
+
"schemaVersion",
|
|
694
|
+
"version",
|
|
695
|
+
"nodePath",
|
|
696
|
+
"files",
|
|
697
|
+
..."native" in parsed ? ["native"] : []
|
|
698
|
+
]))
|
|
699
|
+
return null;
|
|
700
|
+
const candidate = parsed;
|
|
701
|
+
if (candidate.schemaVersion !== HOOK_RUNTIME_SCHEMA_VERSION || typeof candidate.version !== "string" || compareSemver(candidate.version, candidate.version) !== 0 || typeof candidate.nodePath !== "string" || !isAbsolute3(candidate.nodePath) || /[\r\n]/u.test(candidate.nodePath) || !candidate.files || typeof candidate.files !== "object" || Array.isArray(candidate.files) || !exactKeys(candidate.files, Object.keys(HOOK_RUNTIME_ENTRIES)) || Object.values(candidate.files).some(
|
|
702
|
+
(digest) => typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest)
|
|
703
|
+
)) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
if (candidate.native !== void 0 && (!candidate.native || typeof candidate.native !== "object" || Array.isArray(candidate.native) || !exactKeys(candidate.native, KEYRING_NATIVE_FILES) || Object.values(candidate.native).some(
|
|
707
|
+
(digest) => typeof digest !== "string" || !/^[a-f0-9]{64}$/u.test(digest)
|
|
708
|
+
)))
|
|
709
|
+
return null;
|
|
710
|
+
const manifest = candidate;
|
|
711
|
+
if (raw !== exactManifest(manifest) || releaseName !== `${RELEASE_PREFIX}${releaseId(manifest)}` || !validateRelease(releaseDir, manifest)) {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
return manifest;
|
|
715
|
+
} catch {
|
|
716
|
+
return null;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
function readSelectedRelease(paths) {
|
|
720
|
+
let raw;
|
|
721
|
+
try {
|
|
722
|
+
raw = readFileSync3(paths.current, "utf8");
|
|
723
|
+
} catch (error) {
|
|
724
|
+
if (error.code === "ENOENT") return null;
|
|
725
|
+
throw error;
|
|
726
|
+
}
|
|
727
|
+
const name2 = raw.endsWith("\n") ? raw.slice(0, -1) : "";
|
|
728
|
+
if (`${name2}
|
|
729
|
+
` !== raw || !RELEASE_NAME_RE.test(name2)) {
|
|
730
|
+
throw new Error("cannot stage hook runtime: selected release is malformed");
|
|
731
|
+
}
|
|
732
|
+
const dir = join5(paths.releasesDir, name2);
|
|
733
|
+
const manifest = readReleaseManifest(dir, name2);
|
|
734
|
+
if (!manifest) {
|
|
735
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
736
|
+
}
|
|
737
|
+
return { name: name2, dir, manifest };
|
|
738
|
+
}
|
|
739
|
+
function selectedHookRuntimeKeyringLoader(options = {}) {
|
|
740
|
+
try {
|
|
741
|
+
const selected = readSelectedRelease(hookRuntimePaths(options));
|
|
742
|
+
if (!selected?.manifest.native) return void 0;
|
|
743
|
+
const loader = join5(selected.dir, "native", "keyring.cjs");
|
|
744
|
+
return existsSync4(loader) ? loader : void 0;
|
|
745
|
+
} catch {
|
|
746
|
+
return void 0;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
750
|
+
set -eu
|
|
751
|
+
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
752
|
+
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
753
|
+
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
754
|
+
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
755
|
+
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
756
|
+
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
757
|
+
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
758
|
+
case "\${1-}" in
|
|
759
|
+
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
760
|
+
*) exit 64 ;;
|
|
761
|
+
esac
|
|
762
|
+
shift
|
|
763
|
+
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
764
|
+
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
765
|
+
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
766
|
+
exec "$prim_node" "$prim_entry" "$@"
|
|
767
|
+
`;
|
|
768
|
+
|
|
769
|
+
// src/lib/machine-broker.ts
|
|
770
|
+
import { spawn } from "child_process";
|
|
771
|
+
import { createHash as createHash2, createHmac, randomBytes as randomBytes3, timingSafeEqual } from "crypto";
|
|
772
|
+
import { lstatSync as lstatSync2, readFileSync as readFileSync4, rmSync as rmSync4 } from "fs";
|
|
773
|
+
import { createServer } from "http";
|
|
774
|
+
import { basename, join as join6 } from "path";
|
|
775
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
776
|
+
|
|
421
777
|
// src/lib/loopback-fetch.ts
|
|
422
778
|
import { request } from "http";
|
|
423
779
|
import { Readable } from "stream";
|
|
@@ -851,34 +1207,6 @@ async function verifyInjectedMachineIdentity(apiUrl, clientId, signal, fetchImpl
|
|
|
851
1207
|
return parseInjectedMachineIdentity(response.status, value, apiUrl, clientId);
|
|
852
1208
|
}
|
|
853
1209
|
|
|
854
|
-
// src/lib/paths.ts
|
|
855
|
-
import { homedir } from "os";
|
|
856
|
-
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
857
|
-
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
858
|
-
function absolutePath(value, variable) {
|
|
859
|
-
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
860
|
-
throw new Error(`${variable} contains unsafe characters`);
|
|
861
|
-
}
|
|
862
|
-
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
863
|
-
const normalized = normalize(value);
|
|
864
|
-
return normalized === value ? value : void 0;
|
|
865
|
-
}
|
|
866
|
-
function resolvePrimConfigDirectory(options = {}) {
|
|
867
|
-
const env = options.env ?? process.env;
|
|
868
|
-
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
869
|
-
if (explicit) return { path: explicit, source: "explicit" };
|
|
870
|
-
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
871
|
-
if (xdgConfigHome) return { path: join3(xdgConfigHome, "prim"), source: "xdg" };
|
|
872
|
-
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
873
|
-
if (!home) {
|
|
874
|
-
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
875
|
-
}
|
|
876
|
-
return { path: join3(home, ".config", "prim"), source: "default" };
|
|
877
|
-
}
|
|
878
|
-
function primConfigDirectory(options = {}) {
|
|
879
|
-
return resolvePrimConfigDirectory(options).path;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
1210
|
// src/lib/machine-broker.ts
|
|
883
1211
|
var MAX_RESPONSE_BYTES = 96 * 1024;
|
|
884
1212
|
var MAX_FORWARD_REQUEST_BYTES = 12 * 1024 * 1024;
|
|
@@ -888,14 +1216,14 @@ var FORWARD_REQUEST_PROOF = "forward-request";
|
|
|
888
1216
|
var FORWARD_ANSWER_PROOF = "forward-answer";
|
|
889
1217
|
var IDLE_MS = 5 * 6e4;
|
|
890
1218
|
function descriptorPath(directory, generation) {
|
|
891
|
-
return
|
|
1219
|
+
return join6(directory, `machine_broker_${generation}.json`);
|
|
892
1220
|
}
|
|
893
1221
|
function readDescriptor(path) {
|
|
894
1222
|
try {
|
|
895
|
-
const stat =
|
|
1223
|
+
const stat = lstatSync2(path);
|
|
896
1224
|
if (!stat.isFile() || stat.size > 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.()))
|
|
897
1225
|
return void 0;
|
|
898
|
-
const data = JSON.parse(
|
|
1226
|
+
const data = JSON.parse(readFileSync4(path, "utf8"));
|
|
899
1227
|
return Number.isInteger(data.port) && data.port > 0 && data.port <= 65535 && /^[a-f0-9]{64}$/u.test(data.nonce) && Number.isInteger(data.pid) ? data : void 0;
|
|
900
1228
|
} catch {
|
|
901
1229
|
return void 0;
|
|
@@ -908,7 +1236,7 @@ function matches(left, right) {
|
|
|
908
1236
|
return typeof right === "string" && /^[a-f0-9]{64}$/u.test(right) && timingSafeEqual(Buffer.from(left, "hex"), Buffer.from(right, "hex"));
|
|
909
1237
|
}
|
|
910
1238
|
function tokenHash(token) {
|
|
911
|
-
return token ?
|
|
1239
|
+
return token ? createHash2("sha256").update(token).digest("hex") : void 0;
|
|
912
1240
|
}
|
|
913
1241
|
async function brokerRequest(descriptor, environment, options) {
|
|
914
1242
|
const body = JSON.stringify({
|
|
@@ -985,7 +1313,7 @@ async function acquireBrokerCredential(options) {
|
|
|
985
1313
|
async function ensureBroker(options) {
|
|
986
1314
|
const path = descriptorPath(options.directory, options.environment.generation);
|
|
987
1315
|
return withFileLock(
|
|
988
|
-
|
|
1316
|
+
join6(options.directory, "machine-broker.lock"),
|
|
989
1317
|
async () => {
|
|
990
1318
|
const current = readDescriptor(path);
|
|
991
1319
|
if (current) {
|
|
@@ -998,7 +1326,7 @@ async function ensureBroker(options) {
|
|
|
998
1326
|
options.signal.throwIfAborted();
|
|
999
1327
|
}
|
|
1000
1328
|
}
|
|
1001
|
-
const bundled =
|
|
1329
|
+
const bundled = fileURLToPath3(import.meta.url);
|
|
1002
1330
|
const entry = basename(bundled) === "prim-daemon-server" ? bundled : binFile("prim-daemon-server");
|
|
1003
1331
|
if (!entry)
|
|
1004
1332
|
throw new MachineAuthError(
|
|
@@ -1136,23 +1464,23 @@ async function forwardBrokerRequest(options) {
|
|
|
1136
1464
|
}
|
|
1137
1465
|
|
|
1138
1466
|
// src/lib/machine-retry.ts
|
|
1139
|
-
import { createHash as
|
|
1140
|
-
import { lstatSync as
|
|
1141
|
-
import { join as
|
|
1467
|
+
import { createHash as createHash3 } from "crypto";
|
|
1468
|
+
import { lstatSync as lstatSync3, readFileSync as readFileSync5, readdirSync as readdirSync2, rmSync as rmSync5 } from "fs";
|
|
1469
|
+
import { join as join7 } from "path";
|
|
1142
1470
|
var SERVER_WAIT_PREFIX = "The sign-in service asked prim to wait";
|
|
1143
1471
|
var MAX_SERVER_WAIT_MS = 15 * 6e4;
|
|
1144
1472
|
async function withMachineCooldown(directory, apiUrl, clientId, signal, request3) {
|
|
1145
1473
|
signal?.throwIfAborted();
|
|
1146
|
-
const key =
|
|
1147
|
-
const path =
|
|
1474
|
+
const key = createHash3("sha256").update(`${apiUrl}\0${clientId}`).digest("hex");
|
|
1475
|
+
const path = join7(directory, `machine_retry_${key}.json`);
|
|
1148
1476
|
let state = {
|
|
1149
1477
|
failures: 0,
|
|
1150
1478
|
retryAt: 0
|
|
1151
1479
|
};
|
|
1152
1480
|
try {
|
|
1153
|
-
const stat =
|
|
1481
|
+
const stat = lstatSync3(path);
|
|
1154
1482
|
if (!stat.isFile() || stat.size > 1024) throw new Error("Invalid retry state");
|
|
1155
|
-
const data = JSON.parse(
|
|
1483
|
+
const data = JSON.parse(readFileSync5(path, "utf8"));
|
|
1156
1484
|
if (!Number.isSafeInteger(data.failures) || data.failures < 0 || data.failures > 16 || !Number.isSafeInteger(data.retryAt) || data.retryAt < 0)
|
|
1157
1485
|
throw new Error("Invalid retry state");
|
|
1158
1486
|
state = data;
|
|
@@ -1176,7 +1504,7 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request3
|
|
|
1176
1504
|
);
|
|
1177
1505
|
try {
|
|
1178
1506
|
const result = await request3();
|
|
1179
|
-
|
|
1507
|
+
rmSync5(path, { force: true });
|
|
1180
1508
|
return result;
|
|
1181
1509
|
} catch (error) {
|
|
1182
1510
|
if (!signal?.aborted && error instanceof MachineAuthError && [
|
|
@@ -1205,16 +1533,16 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request3
|
|
|
1205
1533
|
|
|
1206
1534
|
// src/lib/session-identity.ts
|
|
1207
1535
|
import { execFile } from "child_process";
|
|
1208
|
-
import { createHash as
|
|
1209
|
-
import { rmSync as
|
|
1210
|
-
import { join as
|
|
1536
|
+
import { createHash as createHash5 } from "crypto";
|
|
1537
|
+
import { rmSync as rmSync6 } from "fs";
|
|
1538
|
+
import { join as join9 } from "path";
|
|
1211
1539
|
import { promisify } from "util";
|
|
1212
1540
|
|
|
1213
1541
|
// src/lib/git.ts
|
|
1214
1542
|
import { execFileSync } from "child_process";
|
|
1215
|
-
import { createHash as
|
|
1216
|
-
import { lstatSync as
|
|
1217
|
-
import { basename as basename2, dirname as
|
|
1543
|
+
import { createHash as createHash4 } from "crypto";
|
|
1544
|
+
import { lstatSync as lstatSync4, readFileSync as readFileSync6, realpathSync as realpathSync2 } from "fs";
|
|
1545
|
+
import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute4, join as join8, relative, resolve as resolve2, sep } from "path";
|
|
1218
1546
|
var GIT_TIMEOUT_MS = 1e3;
|
|
1219
1547
|
var MAX_SETTINGS_BYTES = 1024 * 1024;
|
|
1220
1548
|
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
@@ -1271,12 +1599,12 @@ function githubRepositoryFromRemote(remote) {
|
|
|
1271
1599
|
return path ? parseGithubRepositoryName(path) : null;
|
|
1272
1600
|
}
|
|
1273
1601
|
function checkoutSettingsText(repoRoot) {
|
|
1274
|
-
const path =
|
|
1602
|
+
const path = join8(repoRoot, ".claude", "settings.json");
|
|
1275
1603
|
try {
|
|
1276
|
-
if (!
|
|
1277
|
-
const stat =
|
|
1604
|
+
if (!lstatSync4(join8(repoRoot, ".claude")).isDirectory()) return null;
|
|
1605
|
+
const stat = lstatSync4(path);
|
|
1278
1606
|
if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
|
|
1279
|
-
return
|
|
1607
|
+
return readFileSync6(path, "utf8");
|
|
1280
1608
|
} catch {
|
|
1281
1609
|
return null;
|
|
1282
1610
|
}
|
|
@@ -1310,19 +1638,19 @@ function githubRepositoryFullName(cwd) {
|
|
|
1310
1638
|
function canonicalGitRoot(cwd) {
|
|
1311
1639
|
try {
|
|
1312
1640
|
const top = gitToplevel(cwd);
|
|
1313
|
-
return top ?
|
|
1641
|
+
return top ? realpathSync2.native(top) : null;
|
|
1314
1642
|
} catch {
|
|
1315
1643
|
return null;
|
|
1316
1644
|
}
|
|
1317
1645
|
}
|
|
1318
1646
|
function relatedWorktreeTarget(path, cwd, origin) {
|
|
1319
|
-
let parent =
|
|
1647
|
+
let parent = dirname6(resolve2(cwd, path));
|
|
1320
1648
|
while (true) {
|
|
1321
1649
|
try {
|
|
1322
|
-
|
|
1650
|
+
realpathSync2.native(parent);
|
|
1323
1651
|
break;
|
|
1324
1652
|
} catch {
|
|
1325
|
-
const next =
|
|
1653
|
+
const next = dirname6(parent);
|
|
1326
1654
|
if (next === parent) return null;
|
|
1327
1655
|
parent = next;
|
|
1328
1656
|
}
|
|
@@ -1332,7 +1660,7 @@ function relatedWorktreeTarget(path, cwd, origin) {
|
|
|
1332
1660
|
const common = (root) => {
|
|
1333
1661
|
const value = gitValue(root, ["rev-parse", "--git-common-dir"]);
|
|
1334
1662
|
try {
|
|
1335
|
-
return value ?
|
|
1663
|
+
return value ? realpathSync2.native(resolve2(root, value)) : void 0;
|
|
1336
1664
|
} catch {
|
|
1337
1665
|
return void 0;
|
|
1338
1666
|
}
|
|
@@ -1362,16 +1690,16 @@ function normalizeOriginRemote(remote) {
|
|
|
1362
1690
|
}
|
|
1363
1691
|
}
|
|
1364
1692
|
function opaqueRepoKey(source, value) {
|
|
1365
|
-
return `repo_v1_${
|
|
1693
|
+
return `repo_v1_${createHash4("sha256").update(`${source}\0${value}`).digest("hex")}`;
|
|
1366
1694
|
}
|
|
1367
1695
|
function resolveRepositoryContext(cwd = process.cwd()) {
|
|
1368
1696
|
const top = gitToplevel(cwd);
|
|
1369
1697
|
if (!top) return null;
|
|
1370
1698
|
let repoRoot;
|
|
1371
1699
|
try {
|
|
1372
|
-
repoRoot =
|
|
1700
|
+
repoRoot = realpathSync2.native(top);
|
|
1373
1701
|
} catch {
|
|
1374
|
-
repoRoot =
|
|
1702
|
+
repoRoot = resolve2(top);
|
|
1375
1703
|
}
|
|
1376
1704
|
const origin = gitValue(repoRoot, ["config", "--get", "remote.origin.url"]);
|
|
1377
1705
|
const repoFullName = githubRepositoryFullName(repoRoot) ?? void 0;
|
|
@@ -1408,19 +1736,19 @@ function nearestExistingAncestor(path) {
|
|
|
1408
1736
|
let cursor = path;
|
|
1409
1737
|
while (true) {
|
|
1410
1738
|
try {
|
|
1411
|
-
|
|
1739
|
+
lstatSync4(cursor);
|
|
1412
1740
|
break;
|
|
1413
1741
|
} catch (error) {
|
|
1414
1742
|
const code = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
1415
1743
|
if (code !== "ENOENT" && code !== "ENOTDIR") return null;
|
|
1416
|
-
const parent =
|
|
1744
|
+
const parent = dirname6(cursor);
|
|
1417
1745
|
if (parent === cursor) return null;
|
|
1418
1746
|
suffix.unshift(cursor.slice(parent.length + (parent.endsWith(sep) ? 0 : 1)));
|
|
1419
1747
|
cursor = parent;
|
|
1420
1748
|
}
|
|
1421
1749
|
}
|
|
1422
1750
|
try {
|
|
1423
|
-
return { real:
|
|
1751
|
+
return { real: realpathSync2.native(cursor), suffix };
|
|
1424
1752
|
} catch {
|
|
1425
1753
|
return null;
|
|
1426
1754
|
}
|
|
@@ -1429,13 +1757,13 @@ function canonicalRepositoryPathScoped(filePath, context, baseDir) {
|
|
|
1429
1757
|
if (!filePath || filePath !== filePath.trim() || filePath.includes("\0")) {
|
|
1430
1758
|
return { ok: false, reason: "invalid_path" };
|
|
1431
1759
|
}
|
|
1432
|
-
const absolute =
|
|
1760
|
+
const absolute = resolve2(baseDir, filePath);
|
|
1433
1761
|
const resolved = nearestExistingAncestor(absolute);
|
|
1434
1762
|
if (!resolved) return { ok: false, reason: "invalid_path" };
|
|
1435
|
-
const target =
|
|
1763
|
+
const target = resolve2(resolved.real, ...resolved.suffix);
|
|
1436
1764
|
const rel = relative(context.repoRoot, target);
|
|
1437
1765
|
if (!rel || rel === ".") return { ok: false, reason: "invalid_path" };
|
|
1438
|
-
if (
|
|
1766
|
+
if (isAbsolute4(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
1439
1767
|
return { ok: false, reason: "outside_repository" };
|
|
1440
1768
|
}
|
|
1441
1769
|
return { ok: true, file: sep === "/" ? rel : rel.split(sep).join("/") };
|
|
@@ -1443,26 +1771,26 @@ function canonicalRepositoryPathScoped(filePath, context, baseDir) {
|
|
|
1443
1771
|
function canonicalRepositoryPathLegacy(filePath, cwd, repositoryRoot = canonicalGitRoot(cwd)) {
|
|
1444
1772
|
if (!repositoryRoot || !filePath || [...filePath].some((char) => char < " " || char === "\x7F" || "~*?[]{}$`\\".includes(char)))
|
|
1445
1773
|
return null;
|
|
1446
|
-
const absolute =
|
|
1774
|
+
const absolute = resolve2(cwd, filePath);
|
|
1447
1775
|
let target;
|
|
1448
1776
|
try {
|
|
1449
|
-
const stat =
|
|
1777
|
+
const stat = lstatSync4(absolute);
|
|
1450
1778
|
if (!stat.isFile() || stat.isSymbolicLink()) return null;
|
|
1451
|
-
target =
|
|
1779
|
+
target = realpathSync2.native(absolute);
|
|
1452
1780
|
} catch (error) {
|
|
1453
1781
|
if (!(error instanceof Error) || error.code !== "ENOENT") {
|
|
1454
1782
|
return null;
|
|
1455
1783
|
}
|
|
1456
1784
|
try {
|
|
1457
|
-
const parent =
|
|
1458
|
-
if (!
|
|
1459
|
-
target =
|
|
1785
|
+
const parent = realpathSync2.native(dirname6(absolute));
|
|
1786
|
+
if (!lstatSync4(parent).isDirectory()) return null;
|
|
1787
|
+
target = resolve2(parent, basename2(absolute));
|
|
1460
1788
|
} catch {
|
|
1461
1789
|
return null;
|
|
1462
1790
|
}
|
|
1463
1791
|
}
|
|
1464
1792
|
const rel = relative(repositoryRoot, target);
|
|
1465
|
-
if (!rel ||
|
|
1793
|
+
if (!rel || isAbsolute4(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
1466
1794
|
return null;
|
|
1467
1795
|
}
|
|
1468
1796
|
return sep === "/" ? rel : rel.split(sep).join("/");
|
|
@@ -1621,7 +1949,7 @@ function sessionIdentityMessage(outcome) {
|
|
|
1621
1949
|
return `Primitive could not identify who started this Claude Code cloud session: ${outcome.cause}. To fix it, ${outcome.remedy}.`;
|
|
1622
1950
|
}
|
|
1623
1951
|
function hash(value) {
|
|
1624
|
-
return
|
|
1952
|
+
return createHash5("sha256").update(value).digest("hex");
|
|
1625
1953
|
}
|
|
1626
1954
|
function sessionContext(deps) {
|
|
1627
1955
|
const env = deps.env ?? process.env;
|
|
@@ -1648,9 +1976,9 @@ function sessionContext(deps) {
|
|
|
1648
1976
|
clientId: environment.clientId,
|
|
1649
1977
|
generation: environment.generation,
|
|
1650
1978
|
sessionId,
|
|
1651
|
-
tokenPath:
|
|
1652
|
-
statePath:
|
|
1653
|
-
lockPath:
|
|
1979
|
+
tokenPath: join9(directory, `session_identity_${key}.json`),
|
|
1980
|
+
statePath: join9(directory, `session_identity_${key}.state.json`),
|
|
1981
|
+
lockPath: join9(directory, "session-identity.lock")
|
|
1654
1982
|
};
|
|
1655
1983
|
}
|
|
1656
1984
|
function readRecord(path) {
|
|
@@ -1682,7 +2010,7 @@ function readState(context) {
|
|
|
1682
2010
|
function writeToken(context, record) {
|
|
1683
2011
|
prepareMachineMetadataDirectory(context.directory);
|
|
1684
2012
|
writeMachineMetadata(context.tokenPath, record);
|
|
1685
|
-
|
|
2013
|
+
rmSync6(context.statePath, { force: true });
|
|
1686
2014
|
}
|
|
1687
2015
|
function writeState(context, state) {
|
|
1688
2016
|
try {
|
|
@@ -2292,7 +2620,7 @@ async function ensureSessionIdentity(options = {}, deps = {}) {
|
|
|
2292
2620
|
return identified(refreshed.record, true);
|
|
2293
2621
|
}
|
|
2294
2622
|
if (refreshed.status === "refused") {
|
|
2295
|
-
|
|
2623
|
+
rmSync6(ctx.tokenPath, { force: true });
|
|
2296
2624
|
writeState(ctx, {
|
|
2297
2625
|
sessionId: ctx.sessionId,
|
|
2298
2626
|
status: "failed",
|
|
@@ -2436,7 +2764,7 @@ async function acquireSessionCredential(options = {}, deps = {}) {
|
|
|
2436
2764
|
function sessionScoped(deps) {
|
|
2437
2765
|
return !deps.localEnvironment && !deps.environmentOnly && sessionIdentityMode(deps);
|
|
2438
2766
|
}
|
|
2439
|
-
var
|
|
2767
|
+
var require3 = createRequire2(import.meta.url);
|
|
2440
2768
|
var SERVICE = "ai.primitive.prim.machine";
|
|
2441
2769
|
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
2442
2770
|
var PROFILE_FILE = "machine_profile.json";
|
|
@@ -2449,16 +2777,16 @@ function apiUrlFor(deps) {
|
|
|
2449
2777
|
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL2);
|
|
2450
2778
|
}
|
|
2451
2779
|
function hash2(value) {
|
|
2452
|
-
return
|
|
2780
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
2453
2781
|
}
|
|
2454
2782
|
function recordPath(directory, generation) {
|
|
2455
|
-
return
|
|
2783
|
+
return join10(directory, `machine_${generation}.json`);
|
|
2456
2784
|
}
|
|
2457
2785
|
function storageKey(directory, generation) {
|
|
2458
2786
|
return `${hash2(directory)}:${generation}`;
|
|
2459
2787
|
}
|
|
2460
2788
|
function environmentContextPath(deps, clientId) {
|
|
2461
|
-
return
|
|
2789
|
+
return join10(
|
|
2462
2790
|
configurationDirectory(deps),
|
|
2463
2791
|
`machine_context_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
2464
2792
|
);
|
|
@@ -2518,22 +2846,28 @@ function readEnvironmentContext(environment, deps) {
|
|
|
2518
2846
|
return void 0;
|
|
2519
2847
|
}
|
|
2520
2848
|
}
|
|
2521
|
-
function
|
|
2522
|
-
let directory =
|
|
2849
|
+
function keyringLoaderPath(start, options = {}) {
|
|
2850
|
+
let directory = start;
|
|
2523
2851
|
for (let depth = 0; depth < 5; depth++) {
|
|
2524
|
-
const loader =
|
|
2525
|
-
if (
|
|
2526
|
-
|
|
2527
|
-
return new module2.AsyncEntry(SERVICE, key, { linux: { store: "secret-service" } });
|
|
2528
|
-
}
|
|
2529
|
-
directory = dirname5(directory);
|
|
2852
|
+
const loader = join10(directory, "native", "keyring.cjs");
|
|
2853
|
+
if (existsSync5(loader)) return loader;
|
|
2854
|
+
directory = dirname7(directory);
|
|
2530
2855
|
}
|
|
2531
|
-
|
|
2856
|
+
try {
|
|
2857
|
+
createRequire2(join10(start, "noop.js")).resolve("@napi-rs/keyring");
|
|
2858
|
+
return void 0;
|
|
2859
|
+
} catch {
|
|
2860
|
+
return selectedHookRuntimeKeyringLoader(options);
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
function keyringEntry(key) {
|
|
2864
|
+
const loader = keyringLoaderPath(dirname7(fileURLToPath4(import.meta.url)));
|
|
2865
|
+
const module = require3(loader ?? "@napi-rs/keyring");
|
|
2532
2866
|
return new module.AsyncEntry(SERVICE, key, { linux: { store: "secret-service" } });
|
|
2533
2867
|
}
|
|
2534
2868
|
function protectPath(path, directory) {
|
|
2535
2869
|
if (process.platform !== "win32") {
|
|
2536
|
-
|
|
2870
|
+
chmodSync2(path, directory ? 448 : 384);
|
|
2537
2871
|
return;
|
|
2538
2872
|
}
|
|
2539
2873
|
const encodedPath = Buffer.from(path).toString("base64");
|
|
@@ -2550,8 +2884,8 @@ function protectPath(path, directory) {
|
|
|
2550
2884
|
);
|
|
2551
2885
|
}
|
|
2552
2886
|
function ensureDirectory(directory) {
|
|
2553
|
-
|
|
2554
|
-
if (
|
|
2887
|
+
mkdirSync4(directory, { recursive: true, mode: 448 });
|
|
2888
|
+
if (lstatSync5(directory).isSymbolicLink())
|
|
2555
2889
|
throw new MachineAuthError(
|
|
2556
2890
|
"unsafe_storage",
|
|
2557
2891
|
"Credential directory must not be a symbolic link."
|
|
@@ -2569,14 +2903,14 @@ function readMachineMetadata(path) {
|
|
|
2569
2903
|
function readPrivate(path) {
|
|
2570
2904
|
let raw;
|
|
2571
2905
|
try {
|
|
2572
|
-
const stat =
|
|
2906
|
+
const stat = lstatSync5(path);
|
|
2573
2907
|
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
2574
2908
|
throw new MachineAuthError(
|
|
2575
2909
|
"unsafe_storage",
|
|
2576
2910
|
"Machine credential files must be owner-only regular files."
|
|
2577
2911
|
);
|
|
2578
2912
|
}
|
|
2579
|
-
raw =
|
|
2913
|
+
raw = readFileSync7(path, "utf8");
|
|
2580
2914
|
} catch (error) {
|
|
2581
2915
|
if (error.code === "ENOENT") return void 0;
|
|
2582
2916
|
throw error;
|
|
@@ -2621,7 +2955,7 @@ function parseProfile(value) {
|
|
|
2621
2955
|
};
|
|
2622
2956
|
}
|
|
2623
2957
|
function readMachineProfile(deps = {}) {
|
|
2624
|
-
const value = readPrivate(
|
|
2958
|
+
const value = readPrivate(join10(configurationDirectory(deps), PROFILE_FILE));
|
|
2625
2959
|
return value === void 0 ? void 0 : parseProfile(value);
|
|
2626
2960
|
}
|
|
2627
2961
|
var NETWORK_SECRET_AUTH = "network-secret";
|
|
@@ -2630,20 +2964,20 @@ var INJECTED_IDENTITY_TTL_MS = 60 * 6e4;
|
|
|
2630
2964
|
var brokerKeys = /* @__PURE__ */ new Map();
|
|
2631
2965
|
function networkSecretBrokerKey(deps, clientId) {
|
|
2632
2966
|
const directory = configurationDirectory(deps);
|
|
2633
|
-
const path =
|
|
2967
|
+
const path = join10(directory, `machine_network_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.key`);
|
|
2634
2968
|
const known = brokerKeys.get(path);
|
|
2635
2969
|
if (known) return known;
|
|
2636
2970
|
ensureDirectory(directory);
|
|
2637
2971
|
const read = () => {
|
|
2638
2972
|
try {
|
|
2639
|
-
const stat =
|
|
2973
|
+
const stat = lstatSync5(path);
|
|
2640
2974
|
if (!stat.isFile() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()))
|
|
2641
2975
|
throw new MachineAuthError(
|
|
2642
2976
|
"unsafe_storage",
|
|
2643
2977
|
`The machine broker key at ${path} is not an owner-only file. Delete it and retry.`,
|
|
2644
2978
|
true
|
|
2645
2979
|
);
|
|
2646
|
-
const value =
|
|
2980
|
+
const value = readFileSync7(path, "utf8").trim();
|
|
2647
2981
|
return { key: /^[a-f0-9]{64}$/u.test(value) ? value : void 0 };
|
|
2648
2982
|
} catch (error) {
|
|
2649
2983
|
if (error.code === "ENOENT") return void 0;
|
|
@@ -2662,16 +2996,16 @@ function networkSecretBrokerKey(deps, clientId) {
|
|
|
2662
2996
|
`The machine broker key at ${path} is damaged. Delete it and retry.`,
|
|
2663
2997
|
true
|
|
2664
2998
|
);
|
|
2665
|
-
const staged = `${path}.${process.pid}.${
|
|
2999
|
+
const staged = `${path}.${process.pid}.${randomBytes4(6).toString("hex")}`;
|
|
2666
3000
|
try {
|
|
2667
|
-
|
|
3001
|
+
writeFileSync4(staged, `${randomBytes4(32).toString("hex")}
|
|
2668
3002
|
`, { mode: 384, flag: "wx" });
|
|
2669
3003
|
protectPath(staged, false);
|
|
2670
3004
|
linkSync(staged, path);
|
|
2671
3005
|
} catch (error) {
|
|
2672
3006
|
if (error.code !== "EEXIST") throw error;
|
|
2673
3007
|
} finally {
|
|
2674
|
-
|
|
3008
|
+
rmSync7(staged, { force: true });
|
|
2675
3009
|
}
|
|
2676
3010
|
}
|
|
2677
3011
|
const key = read()?.key;
|
|
@@ -2730,7 +3064,7 @@ function environmentCredentials(deps) {
|
|
|
2730
3064
|
}
|
|
2731
3065
|
function hasMachineConfiguration(deps = {}) {
|
|
2732
3066
|
const env = deps.env ?? process.env;
|
|
2733
|
-
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 ||
|
|
3067
|
+
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 || existsSync5(join10(configurationDirectory(deps), PROFILE_FILE));
|
|
2734
3068
|
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
2735
3069
|
throw new MachineAuthError(
|
|
2736
3070
|
"conflicting_credentials",
|
|
@@ -2766,7 +3100,7 @@ function machineSessionEnded(deps = {}) {
|
|
|
2766
3100
|
const generation = environmentCredentials(deps)?.generation ?? readMachineProfile(deps)?.generation;
|
|
2767
3101
|
if (!generation) return false;
|
|
2768
3102
|
if (environmentCredentials(deps)) return environmentTerminal === generation;
|
|
2769
|
-
return readPrivate(
|
|
3103
|
+
return readPrivate(join10(configurationDirectory(deps), "machine_terminal.json")) === generation;
|
|
2770
3104
|
}
|
|
2771
3105
|
var environmentTerminal;
|
|
2772
3106
|
var NETWORK_REJECTION_PAUSE_MS = 3e4;
|
|
@@ -2859,7 +3193,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2859
3193
|
}
|
|
2860
3194
|
}
|
|
2861
3195
|
return withFileLock(
|
|
2862
|
-
|
|
3196
|
+
join10(directory2, "credentials.lock"),
|
|
2863
3197
|
async () => {
|
|
2864
3198
|
if (machineSessionEnded(deps))
|
|
2865
3199
|
throw new MachineAuthError(
|
|
@@ -2941,7 +3275,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2941
3275
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2942
3276
|
try {
|
|
2943
3277
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2944
|
-
|
|
3278
|
+
rmSync7(contextPath, { force: true });
|
|
2945
3279
|
} catch {
|
|
2946
3280
|
}
|
|
2947
3281
|
throw error;
|
|
@@ -2951,7 +3285,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2951
3285
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2952
3286
|
try {
|
|
2953
3287
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2954
|
-
|
|
3288
|
+
rmSync7(contextPath, { force: true });
|
|
2955
3289
|
} catch {
|
|
2956
3290
|
}
|
|
2957
3291
|
}
|
|
@@ -2965,7 +3299,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2965
3299
|
const expected = machineContext(deps);
|
|
2966
3300
|
const directory = configurationDirectory(deps);
|
|
2967
3301
|
return withFileLock(
|
|
2968
|
-
|
|
3302
|
+
join10(directory, "credentials.lock"),
|
|
2969
3303
|
async () => {
|
|
2970
3304
|
const profile = readMachineProfile(deps);
|
|
2971
3305
|
if (!profile || profile.generation !== expected?.generation || profile.apiUrl !== apiUrl)
|
|
@@ -3015,7 +3349,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
3015
3349
|
await persistRecord(record, deps, signal);
|
|
3016
3350
|
} catch (error) {
|
|
3017
3351
|
if (error instanceof MachineAuthError && error.terminal)
|
|
3018
|
-
writePrivate(
|
|
3352
|
+
writePrivate(join10(directory, "machine_terminal.json"), profile.generation);
|
|
3019
3353
|
throw error;
|
|
3020
3354
|
}
|
|
3021
3355
|
}
|
|
@@ -3078,20 +3412,20 @@ function writeMachineMetadata(path, value) {
|
|
|
3078
3412
|
|
|
3079
3413
|
// src/lib/credentials.ts
|
|
3080
3414
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
3081
|
-
var TOKEN_FILE_PATH =
|
|
3082
|
-
var REFRESH_TOKEN_PATH =
|
|
3083
|
-
var TOKEN_EXPIRES_PATH =
|
|
3084
|
-
var TERMINAL_REFRESH_PATH =
|
|
3085
|
-
var CREDENTIAL_METADATA_PATH =
|
|
3086
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
3087
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
3088
|
-
var CREDENTIAL_LOCK_PATH =
|
|
3415
|
+
var TOKEN_FILE_PATH = join11(CONFIG_DIRECTORY, "token");
|
|
3416
|
+
var REFRESH_TOKEN_PATH = join11(CONFIG_DIRECTORY, "refresh_token");
|
|
3417
|
+
var TOKEN_EXPIRES_PATH = join11(CONFIG_DIRECTORY, "token_expires_at");
|
|
3418
|
+
var TERMINAL_REFRESH_PATH = join11(CONFIG_DIRECTORY, "refresh_terminal");
|
|
3419
|
+
var CREDENTIAL_METADATA_PATH = join11(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
3420
|
+
var CREDENTIAL_FAMILY_PATH = join11(CONFIG_DIRECTORY, "credential_family.json");
|
|
3421
|
+
var CREDENTIAL_MIGRATION_PATH = join11(CONFIG_DIRECTORY, "credential_migration.json");
|
|
3422
|
+
var CREDENTIAL_LOCK_PATH = join11(CONFIG_DIRECTORY, "credentials.lock");
|
|
3089
3423
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
3090
3424
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
3091
3425
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
3092
3426
|
function readTrimmed(path) {
|
|
3093
3427
|
try {
|
|
3094
|
-
const value =
|
|
3428
|
+
const value = readFileSync8(path, "utf8").trim();
|
|
3095
3429
|
return value || void 0;
|
|
3096
3430
|
} catch {
|
|
3097
3431
|
return void 0;
|
|
@@ -3128,7 +3462,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
3128
3462
|
return { token: stored, source: "token_file" };
|
|
3129
3463
|
}
|
|
3130
3464
|
function credentialFingerprint(value) {
|
|
3131
|
-
return
|
|
3465
|
+
return createHash7("sha256").update(value).digest("hex");
|
|
3132
3466
|
}
|
|
3133
3467
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
3134
3468
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -3148,7 +3482,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
3148
3482
|
function readBoundedCredentialJson(path) {
|
|
3149
3483
|
let raw;
|
|
3150
3484
|
try {
|
|
3151
|
-
raw =
|
|
3485
|
+
raw = readFileSync8(path, "utf8");
|
|
3152
3486
|
} catch (error) {
|
|
3153
3487
|
return error.code === "ENOENT" ? void 0 : null;
|
|
3154
3488
|
}
|
|
@@ -3225,14 +3559,14 @@ function jwtOrganizationId(token) {
|
|
|
3225
3559
|
|
|
3226
3560
|
// src/binding.ts
|
|
3227
3561
|
var PRIM_CONFIG_DIR = primConfigDirectory();
|
|
3228
|
-
var SESSIONS_DIR =
|
|
3562
|
+
var SESSIONS_DIR = join12(PRIM_CONFIG_DIR, "sessions");
|
|
3229
3563
|
var WORKSPACE_FILE = ".prim/workspace.json";
|
|
3230
3564
|
function readJsonSafe(path) {
|
|
3231
|
-
if (!
|
|
3565
|
+
if (!existsSync6(path)) {
|
|
3232
3566
|
return;
|
|
3233
3567
|
}
|
|
3234
3568
|
try {
|
|
3235
|
-
return JSON.parse(
|
|
3569
|
+
return JSON.parse(readFileSync9(path, "utf-8"));
|
|
3236
3570
|
} catch {
|
|
3237
3571
|
return;
|
|
3238
3572
|
}
|
|
@@ -3241,17 +3575,17 @@ function fromSessionMarker(sessionId) {
|
|
|
3241
3575
|
if (!sessionId) {
|
|
3242
3576
|
return;
|
|
3243
3577
|
}
|
|
3244
|
-
const marker = readJsonSafe(
|
|
3578
|
+
const marker = readJsonSafe(join12(SESSIONS_DIR, `${sessionId}.json`));
|
|
3245
3579
|
return marker?.orgId;
|
|
3246
3580
|
}
|
|
3247
3581
|
function fromWorkspaceFile(startDir) {
|
|
3248
3582
|
let dir = startDir;
|
|
3249
3583
|
while (true) {
|
|
3250
|
-
const config = readJsonSafe(
|
|
3584
|
+
const config = readJsonSafe(join12(dir, WORKSPACE_FILE));
|
|
3251
3585
|
if (config?.orgId) {
|
|
3252
3586
|
return config.orgId;
|
|
3253
3587
|
}
|
|
3254
|
-
const parent =
|
|
3588
|
+
const parent = dirname8(dir);
|
|
3255
3589
|
if (parent === dir) {
|
|
3256
3590
|
return;
|
|
3257
3591
|
}
|
|
@@ -3290,126 +3624,6 @@ import { existsSync as existsSync9 } from "fs";
|
|
|
3290
3624
|
import { existsSync as existsSync8 } from "fs";
|
|
3291
3625
|
import { join as join14 } from "path";
|
|
3292
3626
|
|
|
3293
|
-
// src/lib/hook-runtime.ts
|
|
3294
|
-
import { createHash as createHash7, randomBytes as randomBytes4 } from "crypto";
|
|
3295
|
-
import {
|
|
3296
|
-
constants as constants2,
|
|
3297
|
-
accessSync,
|
|
3298
|
-
chmodSync as chmodSync2,
|
|
3299
|
-
copyFileSync,
|
|
3300
|
-
existsSync as existsSync6,
|
|
3301
|
-
lstatSync as lstatSync5,
|
|
3302
|
-
mkdirSync as mkdirSync4,
|
|
3303
|
-
mkdtempSync,
|
|
3304
|
-
readFileSync as readFileSync9,
|
|
3305
|
-
readdirSync as readdirSync2,
|
|
3306
|
-
realpathSync as realpathSync2,
|
|
3307
|
-
renameSync as renameSync2,
|
|
3308
|
-
rmSync as rmSync7,
|
|
3309
|
-
statSync as statSync2,
|
|
3310
|
-
utimesSync,
|
|
3311
|
-
writeFileSync as writeFileSync4
|
|
3312
|
-
} from "fs";
|
|
3313
|
-
import { delimiter, dirname as dirname8, isAbsolute as isAbsolute4, join as join12, resolve as resolve2 } from "path";
|
|
3314
|
-
|
|
3315
|
-
// src/lib/git-hook-contract.ts
|
|
3316
|
-
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
3317
|
-
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
3318
|
-
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
3319
|
-
# where prim is active (git config prim.active true) and never fails the hook.
|
|
3320
|
-
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
3321
|
-
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
3322
|
-
[ -x "$prim_launcher" ] || exit 0
|
|
3323
|
-
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
3324
|
-
prim_hook=\${1-}
|
|
3325
|
-
if [ "$#" -gt 0 ]; then shift; fi
|
|
3326
|
-
case "$prim_hook" in
|
|
3327
|
-
pre-commit)
|
|
3328
|
-
"$prim_launcher" prim-pre-commit "$@" || :
|
|
3329
|
-
;;
|
|
3330
|
-
post-commit)
|
|
3331
|
-
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
3332
|
-
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
3333
|
-
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
3334
|
-
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
3335
|
-
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
3336
|
-
(
|
|
3337
|
-
trap '' HUP
|
|
3338
|
-
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
3339
|
-
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
3340
|
-
) </dev/null >/dev/null 2>&1 &
|
|
3341
|
-
;;
|
|
3342
|
-
post-rewrite)
|
|
3343
|
-
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
3344
|
-
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
3345
|
-
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
3346
|
-
rm -f "$prim_pairs"
|
|
3347
|
-
exit 0
|
|
3348
|
-
fi
|
|
3349
|
-
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
3350
|
-
(
|
|
3351
|
-
trap '' HUP
|
|
3352
|
-
trap 'rm -f "$prim_pairs"' 0
|
|
3353
|
-
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
3354
|
-
) </dev/null >/dev/null 2>&1 &
|
|
3355
|
-
;;
|
|
3356
|
-
esac
|
|
3357
|
-
exit 0
|
|
3358
|
-
`;
|
|
3359
|
-
|
|
3360
|
-
// src/lib/native-keyring.ts
|
|
3361
|
-
import { existsSync as existsSync5 } from "fs";
|
|
3362
|
-
import { createRequire as createRequire2 } from "module";
|
|
3363
|
-
import { dirname as dirname7, join as join11 } from "path";
|
|
3364
|
-
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
3365
|
-
var require3 = createRequire2(import.meta.url);
|
|
3366
|
-
|
|
3367
|
-
// src/lib/hook-runtime.ts
|
|
3368
|
-
var RELEASE_PREFIX = "release-";
|
|
3369
|
-
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
3370
|
-
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
3371
|
-
var HOOK_RUNTIME_ENTRIES = {
|
|
3372
|
-
// SessionStart self-healing resolves this exact CLI entry to run
|
|
3373
|
-
// `daemon ensure`; it must travel with the immutable hook release.
|
|
3374
|
-
prim: "dist/index.js",
|
|
3375
|
-
"prim-hook": "dist/hooks/prim-hook.js",
|
|
3376
|
-
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
3377
|
-
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
3378
|
-
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
3379
|
-
"prim-pre-tool-use-worker": "dist/hooks/pre-tool-use-worker.js",
|
|
3380
|
-
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
3381
|
-
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
3382
|
-
"prim-session-start": "dist/hooks/session-start.js",
|
|
3383
|
-
"prim-session-end": "dist/hooks/session-end.js",
|
|
3384
|
-
"prim-daemon-server": "dist/daemon/server.js",
|
|
3385
|
-
"prim-statusline": "dist/statusline-main.js"
|
|
3386
|
-
};
|
|
3387
|
-
var SOURCE_ENTRIES = Object.fromEntries(
|
|
3388
|
-
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
3389
|
-
bin,
|
|
3390
|
-
target.replace(/^dist\//u, "")
|
|
3391
|
-
])
|
|
3392
|
-
);
|
|
3393
|
-
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
3394
|
-
set -eu
|
|
3395
|
-
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
3396
|
-
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
3397
|
-
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
3398
|
-
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
3399
|
-
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
3400
|
-
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
3401
|
-
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
3402
|
-
case "\${1-}" in
|
|
3403
|
-
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
3404
|
-
*) exit 64 ;;
|
|
3405
|
-
esac
|
|
3406
|
-
shift
|
|
3407
|
-
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
3408
|
-
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
3409
|
-
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
3410
|
-
exec "$prim_node" "$prim_entry" "$@"
|
|
3411
|
-
`;
|
|
3412
|
-
|
|
3413
3627
|
// src/demo/state.ts
|
|
3414
3628
|
import { createHash as createHash8, randomUUID as randomUUID3 } from "crypto";
|
|
3415
3629
|
import { existsSync as existsSync7, lstatSync as lstatSync6, readFileSync as readFileSync10, readdirSync as readdirSync3 } from "fs";
|
|
@@ -31812,18 +32026,18 @@ var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
|
31812
32026
|
function boundedClaim(value) {
|
|
31813
32027
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
31814
32028
|
}
|
|
31815
|
-
function
|
|
32029
|
+
function sha2562(value) {
|
|
31816
32030
|
return createHash10("sha256").update(value).digest("hex");
|
|
31817
32031
|
}
|
|
31818
32032
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
31819
32033
|
if (hasMachineConfiguration()) {
|
|
31820
32034
|
const machine = machineContext();
|
|
31821
32035
|
return machine ? {
|
|
31822
|
-
principalId:
|
|
32036
|
+
principalId: sha2562(
|
|
31823
32037
|
`${machine.issuer}\0${machine.clientId}${machine.authorizedUserId ? `\0${machine.authorizedUserId}` : ""}`
|
|
31824
32038
|
),
|
|
31825
32039
|
organizationId: machine.workosOrganizationId,
|
|
31826
|
-
credentialFingerprint:
|
|
32040
|
+
credentialFingerprint: sha2562(machine.generation)
|
|
31827
32041
|
} : void 0;
|
|
31828
32042
|
}
|
|
31829
32043
|
if (!token) return void 0;
|
|
@@ -31833,9 +32047,9 @@ function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
|
31833
32047
|
if (!(subject && organizationId)) return void 0;
|
|
31834
32048
|
const issuer = boundedClaim(payload?.iss) ?? "unknown-issuer";
|
|
31835
32049
|
return {
|
|
31836
|
-
principalId:
|
|
32050
|
+
principalId: sha2562(`${issuer}\0${subject}`),
|
|
31837
32051
|
organizationId,
|
|
31838
|
-
credentialFingerprint:
|
|
32052
|
+
credentialFingerprint: sha2562(token)
|
|
31839
32053
|
};
|
|
31840
32054
|
}
|
|
31841
32055
|
function daemonPrincipalsMatch(caller, daemon) {
|