@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
|
@@ -59,30 +59,30 @@ import { dirname as dirname15, join as join28 } from "path";
|
|
|
59
59
|
import { fileURLToPath as fileURLToPath5 } from "url";
|
|
60
60
|
|
|
61
61
|
// src/binding.ts
|
|
62
|
-
import { existsSync as
|
|
63
|
-
import { dirname as
|
|
62
|
+
import { existsSync as existsSync6, readFileSync as readFileSync9 } from "fs";
|
|
63
|
+
import { dirname as dirname8, join as join12 } from "path";
|
|
64
64
|
|
|
65
65
|
// src/lib/credentials.ts
|
|
66
|
-
import { createHash as
|
|
67
|
-
import { readFileSync as
|
|
68
|
-
import { join as
|
|
66
|
+
import { createHash as createHash7 } from "crypto";
|
|
67
|
+
import { readFileSync as readFileSync8 } from "fs";
|
|
68
|
+
import { join as join11 } from "path";
|
|
69
69
|
|
|
70
70
|
// src/lib/machine-credentials.ts
|
|
71
71
|
import { execFileSync as execFileSync2 } from "child_process";
|
|
72
|
-
import { createHash as
|
|
72
|
+
import { createHash as createHash6, randomBytes as randomBytes4, randomUUID as randomUUID2 } from "crypto";
|
|
73
73
|
import {
|
|
74
|
-
chmodSync,
|
|
75
|
-
existsSync as
|
|
74
|
+
chmodSync as chmodSync2,
|
|
75
|
+
existsSync as existsSync5,
|
|
76
76
|
linkSync,
|
|
77
|
-
lstatSync as
|
|
78
|
-
mkdirSync as
|
|
79
|
-
readFileSync as
|
|
80
|
-
rmSync as
|
|
81
|
-
writeFileSync as
|
|
77
|
+
lstatSync as lstatSync5,
|
|
78
|
+
mkdirSync as mkdirSync4,
|
|
79
|
+
readFileSync as readFileSync7,
|
|
80
|
+
rmSync as rmSync7,
|
|
81
|
+
writeFileSync as writeFileSync4
|
|
82
82
|
} from "fs";
|
|
83
|
-
import { createRequire } from "module";
|
|
84
|
-
import { dirname as
|
|
85
|
-
import { fileURLToPath as
|
|
83
|
+
import { createRequire as createRequire2 } from "module";
|
|
84
|
+
import { dirname as dirname7, join as join10 } from "path";
|
|
85
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
86
86
|
|
|
87
87
|
// src/lib/atomic-file.ts
|
|
88
88
|
import { randomUUID } from "crypto";
|
|
@@ -333,18 +333,71 @@ async function withFileLock(lockDir, operation, options = {}) {
|
|
|
333
333
|
}
|
|
334
334
|
}
|
|
335
335
|
|
|
336
|
-
// src/lib/
|
|
337
|
-
import {
|
|
338
|
-
import {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
336
|
+
// src/lib/hook-runtime.ts
|
|
337
|
+
import { createHash, randomBytes as randomBytes2 } from "crypto";
|
|
338
|
+
import {
|
|
339
|
+
constants as constants2,
|
|
340
|
+
accessSync,
|
|
341
|
+
chmodSync,
|
|
342
|
+
copyFileSync,
|
|
343
|
+
existsSync as existsSync4,
|
|
344
|
+
lstatSync,
|
|
345
|
+
mkdirSync as mkdirSync3,
|
|
346
|
+
mkdtempSync,
|
|
347
|
+
readFileSync as readFileSync3,
|
|
348
|
+
readdirSync,
|
|
349
|
+
realpathSync,
|
|
350
|
+
renameSync as renameSync2,
|
|
351
|
+
rmSync as rmSync3,
|
|
352
|
+
statSync as statSync2,
|
|
353
|
+
utimesSync,
|
|
354
|
+
writeFileSync as writeFileSync3
|
|
355
|
+
} from "fs";
|
|
356
|
+
import { delimiter, dirname as dirname5, isAbsolute as isAbsolute3, join as join5, resolve } from "path";
|
|
343
357
|
|
|
344
358
|
// src/lib/bin-path.ts
|
|
345
359
|
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
346
360
|
import { dirname as dirname3, isAbsolute, join as join2 } from "path";
|
|
347
361
|
import { fileURLToPath } from "url";
|
|
362
|
+
|
|
363
|
+
// src/lib/semver.ts
|
|
364
|
+
function parseSemver(value) {
|
|
365
|
+
if (typeof value !== "string" || value.length > 256) return;
|
|
366
|
+
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(
|
|
367
|
+
value
|
|
368
|
+
);
|
|
369
|
+
if (!match) return;
|
|
370
|
+
const prerelease = match[4]?.split(".") ?? [];
|
|
371
|
+
if (prerelease.some((part) => /^\d+$/u.test(part) && part.length > 1 && part.startsWith("0"))) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
return { core: [BigInt(match[1]), BigInt(match[2]), BigInt(match[3])], prerelease };
|
|
375
|
+
}
|
|
376
|
+
function compareSemver(left, right) {
|
|
377
|
+
const a = parseSemver(left);
|
|
378
|
+
const b = parseSemver(right);
|
|
379
|
+
if (!a || !b) return;
|
|
380
|
+
for (let i = 0; i < a.core.length; i += 1) {
|
|
381
|
+
if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
|
|
382
|
+
}
|
|
383
|
+
if (a.prerelease.length === 0 || b.prerelease.length === 0) {
|
|
384
|
+
return a.prerelease.length === b.prerelease.length ? 0 : a.prerelease.length === 0 ? 1 : -1;
|
|
385
|
+
}
|
|
386
|
+
for (let i = 0; i < Math.max(a.prerelease.length, b.prerelease.length); i += 1) {
|
|
387
|
+
const x = a.prerelease[i];
|
|
388
|
+
const y = b.prerelease[i];
|
|
389
|
+
if (x === void 0 || y === void 0) return x === void 0 ? -1 : 1;
|
|
390
|
+
if (x === y) continue;
|
|
391
|
+
const xNumeric = /^\d+$/u.test(x);
|
|
392
|
+
const yNumeric = /^\d+$/u.test(y);
|
|
393
|
+
if (xNumeric && yNumeric) return BigInt(x) < BigInt(y) ? -1 : 1;
|
|
394
|
+
if (xNumeric !== yNumeric) return xNumeric ? -1 : 1;
|
|
395
|
+
return x < y ? -1 : 1;
|
|
396
|
+
}
|
|
397
|
+
return 0;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/lib/bin-path.ts
|
|
348
401
|
var PKG_NAME = "@primitive.ai/prim";
|
|
349
402
|
var ROOT_WALK_LIMIT = 6;
|
|
350
403
|
var STABLE_HOOK_LAUNCHER_NAME = "prim-hook-launcher-v1";
|
|
@@ -419,6 +472,309 @@ function detachedHookShimCommand(bin, args = "") {
|
|
|
419
472
|
return `payload=$(cat); { trap '' HUP; printf '%s' "$payload" | { ${stableHookCommand(bin, args)}; }; } </dev/null >/dev/null 2>&1 &`;
|
|
420
473
|
}
|
|
421
474
|
|
|
475
|
+
// src/lib/git-hook-contract.ts
|
|
476
|
+
var GIT_HOOK_ENTRYPOINT_NAME = "prim-git-hook-v1";
|
|
477
|
+
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
478
|
+
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
479
|
+
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
480
|
+
# where prim is active (git config prim.active true) and never fails the hook.
|
|
481
|
+
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
482
|
+
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
483
|
+
[ -x "$prim_launcher" ] || exit 0
|
|
484
|
+
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
485
|
+
prim_hook=\${1-}
|
|
486
|
+
if [ "$#" -gt 0 ]; then shift; fi
|
|
487
|
+
case "$prim_hook" in
|
|
488
|
+
pre-commit)
|
|
489
|
+
"$prim_launcher" prim-pre-commit "$@" || :
|
|
490
|
+
;;
|
|
491
|
+
post-commit)
|
|
492
|
+
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
493
|
+
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
494
|
+
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
495
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
496
|
+
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
497
|
+
(
|
|
498
|
+
trap '' HUP
|
|
499
|
+
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
500
|
+
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
501
|
+
) </dev/null >/dev/null 2>&1 &
|
|
502
|
+
;;
|
|
503
|
+
post-rewrite)
|
|
504
|
+
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
505
|
+
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
506
|
+
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
507
|
+
rm -f "$prim_pairs"
|
|
508
|
+
exit 0
|
|
509
|
+
fi
|
|
510
|
+
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
511
|
+
(
|
|
512
|
+
trap '' HUP
|
|
513
|
+
trap 'rm -f "$prim_pairs"' 0
|
|
514
|
+
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
515
|
+
) </dev/null >/dev/null 2>&1 &
|
|
516
|
+
;;
|
|
517
|
+
esac
|
|
518
|
+
exit 0
|
|
519
|
+
`;
|
|
520
|
+
|
|
521
|
+
// src/lib/native-keyring.ts
|
|
522
|
+
import { existsSync as existsSync3 } from "fs";
|
|
523
|
+
import { createRequire } from "module";
|
|
524
|
+
import { dirname as dirname4, join as join3 } from "path";
|
|
525
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
526
|
+
var require2 = createRequire(import.meta.url);
|
|
527
|
+
var KEYRING_NATIVE_FILES = ["native/keyring.cjs", "native/keyring.node"];
|
|
528
|
+
|
|
529
|
+
// src/lib/paths.ts
|
|
530
|
+
import { homedir } from "os";
|
|
531
|
+
import { isAbsolute as isAbsolute2, join as join4, normalize } from "path";
|
|
532
|
+
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
533
|
+
function absolutePath(value, variable) {
|
|
534
|
+
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
535
|
+
throw new Error(`${variable} contains unsafe characters`);
|
|
536
|
+
}
|
|
537
|
+
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
538
|
+
const normalized = normalize(value);
|
|
539
|
+
return normalized === value ? value : void 0;
|
|
540
|
+
}
|
|
541
|
+
function resolvePrimConfigDirectory(options = {}) {
|
|
542
|
+
const env = options.env ?? process.env;
|
|
543
|
+
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
544
|
+
if (explicit) return { path: explicit, source: "explicit" };
|
|
545
|
+
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
546
|
+
if (xdgConfigHome) return { path: join4(xdgConfigHome, "prim"), source: "xdg" };
|
|
547
|
+
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
548
|
+
if (!home) {
|
|
549
|
+
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
550
|
+
}
|
|
551
|
+
return { path: join4(home, ".config", "prim"), source: "default" };
|
|
552
|
+
}
|
|
553
|
+
function primConfigDirectory(options = {}) {
|
|
554
|
+
return resolvePrimConfigDirectory(options).path;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/lib/hook-runtime.ts
|
|
558
|
+
var HOOK_RUNTIME_SCHEMA_VERSION = 1;
|
|
559
|
+
var RELEASE_PREFIX = "release-";
|
|
560
|
+
var RELEASE_NAME_RE = /^release-[0-9a-f]{64}$/u;
|
|
561
|
+
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
562
|
+
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
563
|
+
var HOOK_RUNTIME_ENTRIES = {
|
|
564
|
+
// SessionStart self-healing resolves this exact CLI entry to run
|
|
565
|
+
// `daemon ensure`; it must travel with the immutable hook release.
|
|
566
|
+
prim: "dist/index.js",
|
|
567
|
+
"prim-hook": "dist/hooks/prim-hook.js",
|
|
568
|
+
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
569
|
+
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
570
|
+
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
571
|
+
"prim-pre-tool-use-worker": "dist/hooks/pre-tool-use-worker.js",
|
|
572
|
+
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
573
|
+
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
574
|
+
"prim-session-start": "dist/hooks/session-start.js",
|
|
575
|
+
"prim-session-end": "dist/hooks/session-end.js",
|
|
576
|
+
"prim-daemon-server": "dist/daemon/server.js",
|
|
577
|
+
"prim-statusline": "dist/statusline-main.js"
|
|
578
|
+
};
|
|
579
|
+
var SOURCE_ENTRIES = Object.fromEntries(
|
|
580
|
+
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
581
|
+
bin,
|
|
582
|
+
target.replace(/^dist\//u, "")
|
|
583
|
+
])
|
|
584
|
+
);
|
|
585
|
+
function hookRuntimePaths(options = {}) {
|
|
586
|
+
const env = options.env ?? process.env;
|
|
587
|
+
const configDir = primConfigDirectory({
|
|
588
|
+
...options,
|
|
589
|
+
env,
|
|
590
|
+
homeDir: options.homeDir ?? env.HOME ?? ""
|
|
591
|
+
});
|
|
592
|
+
const runtimeDir = join5(configDir, "hook-runtime");
|
|
593
|
+
return {
|
|
594
|
+
configDir,
|
|
595
|
+
launcher: join5(configDir, STABLE_HOOK_LAUNCHER_NAME),
|
|
596
|
+
gitHookEntrypoint: join5(configDir, GIT_HOOK_ENTRYPOINT_NAME),
|
|
597
|
+
runtimeDir,
|
|
598
|
+
releasesDir: join5(runtimeDir, "releases"),
|
|
599
|
+
current: join5(runtimeDir, "current"),
|
|
600
|
+
selectionLock: join5(runtimeDir, "selection.lock")
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function sha256(path) {
|
|
604
|
+
return createHash("sha256").update(readFileSync3(path)).digest("hex");
|
|
605
|
+
}
|
|
606
|
+
function exactManifest(manifest) {
|
|
607
|
+
return `${JSON.stringify(manifest, null, 2)}
|
|
608
|
+
`;
|
|
609
|
+
}
|
|
610
|
+
function exactKeys(value, expected) {
|
|
611
|
+
const actual = Object.keys(value).sort();
|
|
612
|
+
const wanted = [...expected].sort();
|
|
613
|
+
return actual.length === wanted.length && actual.every((key, index) => key === wanted[index]);
|
|
614
|
+
}
|
|
615
|
+
function releaseId(manifest) {
|
|
616
|
+
return createHash("sha256").update(exactManifest(manifest)).digest("hex");
|
|
617
|
+
}
|
|
618
|
+
function runtimePackageJson(manifest) {
|
|
619
|
+
return `${JSON.stringify(
|
|
620
|
+
{
|
|
621
|
+
name: "@primitive.ai/prim",
|
|
622
|
+
version: manifest.version,
|
|
623
|
+
type: "module",
|
|
624
|
+
bin: HOOK_RUNTIME_ENTRIES
|
|
625
|
+
},
|
|
626
|
+
null,
|
|
627
|
+
2
|
|
628
|
+
)}
|
|
629
|
+
`;
|
|
630
|
+
}
|
|
631
|
+
function validateRelease(releaseDir, manifest) {
|
|
632
|
+
try {
|
|
633
|
+
if (!directReleaseLayout(releaseDir, manifest)) return false;
|
|
634
|
+
if (readFileSync3(join5(releaseDir, "manifest.json"), "utf8") !== exactManifest(manifest)) {
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
if (readFileSync3(join5(releaseDir, "package.json"), "utf8") !== runtimePackageJson(manifest)) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
if (readFileSync3(join5(releaseDir, "node"), "utf8") !== `${manifest.nodePath}
|
|
641
|
+
`) {
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
for (const [bin, relativePath] of Object.entries(HOOK_RUNTIME_ENTRIES)) {
|
|
645
|
+
const target = join5(releaseDir, relativePath);
|
|
646
|
+
if (sha256(target) !== manifest.files[bin]) return false;
|
|
647
|
+
}
|
|
648
|
+
for (const [path, digest] of Object.entries(manifest.native ?? {})) {
|
|
649
|
+
if (sha256(join5(releaseDir, path)) !== digest) return false;
|
|
650
|
+
}
|
|
651
|
+
return true;
|
|
652
|
+
} catch {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function directReleaseLayout(releaseDir, manifest) {
|
|
657
|
+
try {
|
|
658
|
+
const directories = /* @__PURE__ */ new Set([releaseDir]);
|
|
659
|
+
for (const relativePath of [
|
|
660
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
661
|
+
...Object.keys(manifest.native ?? {})
|
|
662
|
+
]) {
|
|
663
|
+
for (let directory = dirname5(relativePath); directory !== "."; directory = dirname5(directory)) {
|
|
664
|
+
directories.add(join5(releaseDir, directory));
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (![...directories].every((directory) => lstatSync(directory).isDirectory())) return false;
|
|
668
|
+
const files = [
|
|
669
|
+
"manifest.json",
|
|
670
|
+
"package.json",
|
|
671
|
+
"node",
|
|
672
|
+
...Object.values(HOOK_RUNTIME_ENTRIES),
|
|
673
|
+
...Object.keys(manifest.native ?? {})
|
|
674
|
+
];
|
|
675
|
+
const owned = /* @__PURE__ */ new Set([...directories, ...files.map((path) => join5(releaseDir, path))]);
|
|
676
|
+
if (![...directories].every(
|
|
677
|
+
(directory) => readdirSync(directory).every((name2) => owned.has(join5(directory, name2)))
|
|
678
|
+
))
|
|
679
|
+
return false;
|
|
680
|
+
return files.every((relativePath) => lstatSync(join5(releaseDir, relativePath)).isFile());
|
|
681
|
+
} catch {
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
function readReleaseManifest(releaseDir, releaseName) {
|
|
686
|
+
try {
|
|
687
|
+
const manifestPath = join5(releaseDir, "manifest.json");
|
|
688
|
+
const manifestStat = statSync2(manifestPath);
|
|
689
|
+
if (!manifestStat.isFile() || manifestStat.size > MAX_MANIFEST_BYTES) return null;
|
|
690
|
+
const raw = readFileSync3(manifestPath, "utf8");
|
|
691
|
+
const parsed = JSON.parse(raw);
|
|
692
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
693
|
+
if (!exactKeys(parsed, [
|
|
694
|
+
"schemaVersion",
|
|
695
|
+
"version",
|
|
696
|
+
"nodePath",
|
|
697
|
+
"files",
|
|
698
|
+
..."native" in parsed ? ["native"] : []
|
|
699
|
+
]))
|
|
700
|
+
return null;
|
|
701
|
+
const candidate = parsed;
|
|
702
|
+
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(
|
|
703
|
+
(digest) => typeof digest !== "string" || !/^[0-9a-f]{64}$/u.test(digest)
|
|
704
|
+
)) {
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
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(
|
|
708
|
+
(digest) => typeof digest !== "string" || !/^[a-f0-9]{64}$/u.test(digest)
|
|
709
|
+
)))
|
|
710
|
+
return null;
|
|
711
|
+
const manifest = candidate;
|
|
712
|
+
if (raw !== exactManifest(manifest) || releaseName !== `${RELEASE_PREFIX}${releaseId(manifest)}` || !validateRelease(releaseDir, manifest)) {
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
return manifest;
|
|
716
|
+
} catch {
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
function readSelectedRelease(paths) {
|
|
721
|
+
let raw;
|
|
722
|
+
try {
|
|
723
|
+
raw = readFileSync3(paths.current, "utf8");
|
|
724
|
+
} catch (error) {
|
|
725
|
+
if (error.code === "ENOENT") return null;
|
|
726
|
+
throw error;
|
|
727
|
+
}
|
|
728
|
+
const name2 = raw.endsWith("\n") ? raw.slice(0, -1) : "";
|
|
729
|
+
if (`${name2}
|
|
730
|
+
` !== raw || !RELEASE_NAME_RE.test(name2)) {
|
|
731
|
+
throw new Error("cannot stage hook runtime: selected release is malformed");
|
|
732
|
+
}
|
|
733
|
+
const dir = join5(paths.releasesDir, name2);
|
|
734
|
+
const manifest = readReleaseManifest(dir, name2);
|
|
735
|
+
if (!manifest) {
|
|
736
|
+
throw new Error("cannot stage hook runtime: immutable release is corrupt");
|
|
737
|
+
}
|
|
738
|
+
return { name: name2, dir, manifest };
|
|
739
|
+
}
|
|
740
|
+
function selectedHookRuntimeKeyringLoader(options = {}) {
|
|
741
|
+
try {
|
|
742
|
+
const selected = readSelectedRelease(hookRuntimePaths(options));
|
|
743
|
+
if (!selected?.manifest.native) return void 0;
|
|
744
|
+
const loader = join5(selected.dir, "native", "keyring.cjs");
|
|
745
|
+
return existsSync4(loader) ? loader : void 0;
|
|
746
|
+
} catch {
|
|
747
|
+
return void 0;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
751
|
+
set -eu
|
|
752
|
+
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
753
|
+
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
754
|
+
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
755
|
+
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
756
|
+
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
757
|
+
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
758
|
+
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
759
|
+
case "\${1-}" in
|
|
760
|
+
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
761
|
+
*) exit 64 ;;
|
|
762
|
+
esac
|
|
763
|
+
shift
|
|
764
|
+
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
765
|
+
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
766
|
+
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
767
|
+
exec "$prim_node" "$prim_entry" "$@"
|
|
768
|
+
`;
|
|
769
|
+
|
|
770
|
+
// src/lib/machine-broker.ts
|
|
771
|
+
import { spawn } from "child_process";
|
|
772
|
+
import { createHash as createHash2, createHmac, randomBytes as randomBytes3, timingSafeEqual } from "crypto";
|
|
773
|
+
import { lstatSync as lstatSync2, readFileSync as readFileSync4, rmSync as rmSync4 } from "fs";
|
|
774
|
+
import { createServer } from "http";
|
|
775
|
+
import { basename, join as join6 } from "path";
|
|
776
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
777
|
+
|
|
422
778
|
// src/lib/loopback-fetch.ts
|
|
423
779
|
import { request } from "http";
|
|
424
780
|
import { Readable } from "stream";
|
|
@@ -852,34 +1208,6 @@ async function verifyInjectedMachineIdentity(apiUrl, clientId, signal, fetchImpl
|
|
|
852
1208
|
return parseInjectedMachineIdentity(response.status, value, apiUrl, clientId);
|
|
853
1209
|
}
|
|
854
1210
|
|
|
855
|
-
// src/lib/paths.ts
|
|
856
|
-
import { homedir } from "os";
|
|
857
|
-
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
858
|
-
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
859
|
-
function absolutePath(value, variable) {
|
|
860
|
-
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
861
|
-
throw new Error(`${variable} contains unsafe characters`);
|
|
862
|
-
}
|
|
863
|
-
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
864
|
-
const normalized = normalize(value);
|
|
865
|
-
return normalized === value ? value : void 0;
|
|
866
|
-
}
|
|
867
|
-
function resolvePrimConfigDirectory(options = {}) {
|
|
868
|
-
const env = options.env ?? process.env;
|
|
869
|
-
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
870
|
-
if (explicit) return { path: explicit, source: "explicit" };
|
|
871
|
-
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
872
|
-
if (xdgConfigHome) return { path: join3(xdgConfigHome, "prim"), source: "xdg" };
|
|
873
|
-
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
874
|
-
if (!home) {
|
|
875
|
-
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
876
|
-
}
|
|
877
|
-
return { path: join3(home, ".config", "prim"), source: "default" };
|
|
878
|
-
}
|
|
879
|
-
function primConfigDirectory(options = {}) {
|
|
880
|
-
return resolvePrimConfigDirectory(options).path;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
1211
|
// src/lib/machine-broker.ts
|
|
884
1212
|
var MAX_RESPONSE_BYTES = 96 * 1024;
|
|
885
1213
|
var MAX_FORWARD_REQUEST_BYTES = 12 * 1024 * 1024;
|
|
@@ -889,14 +1217,14 @@ var FORWARD_REQUEST_PROOF = "forward-request";
|
|
|
889
1217
|
var FORWARD_ANSWER_PROOF = "forward-answer";
|
|
890
1218
|
var IDLE_MS = 5 * 6e4;
|
|
891
1219
|
function descriptorPath(directory, generation) {
|
|
892
|
-
return
|
|
1220
|
+
return join6(directory, `machine_broker_${generation}.json`);
|
|
893
1221
|
}
|
|
894
1222
|
function readDescriptor(path) {
|
|
895
1223
|
try {
|
|
896
|
-
const stat =
|
|
1224
|
+
const stat = lstatSync2(path);
|
|
897
1225
|
if (!stat.isFile() || stat.size > 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.()))
|
|
898
1226
|
return void 0;
|
|
899
|
-
const data = JSON.parse(
|
|
1227
|
+
const data = JSON.parse(readFileSync4(path, "utf8"));
|
|
900
1228
|
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;
|
|
901
1229
|
} catch {
|
|
902
1230
|
return void 0;
|
|
@@ -909,7 +1237,7 @@ function matches(left, right) {
|
|
|
909
1237
|
return typeof right === "string" && /^[a-f0-9]{64}$/u.test(right) && timingSafeEqual(Buffer.from(left, "hex"), Buffer.from(right, "hex"));
|
|
910
1238
|
}
|
|
911
1239
|
function tokenHash(token) {
|
|
912
|
-
return token ?
|
|
1240
|
+
return token ? createHash2("sha256").update(token).digest("hex") : void 0;
|
|
913
1241
|
}
|
|
914
1242
|
async function brokerRequest(descriptor, environment, options) {
|
|
915
1243
|
const body = JSON.stringify({
|
|
@@ -986,7 +1314,7 @@ async function acquireBrokerCredential(options) {
|
|
|
986
1314
|
async function ensureBroker(options) {
|
|
987
1315
|
const path = descriptorPath(options.directory, options.environment.generation);
|
|
988
1316
|
return withFileLock(
|
|
989
|
-
|
|
1317
|
+
join6(options.directory, "machine-broker.lock"),
|
|
990
1318
|
async () => {
|
|
991
1319
|
const current = readDescriptor(path);
|
|
992
1320
|
if (current) {
|
|
@@ -999,7 +1327,7 @@ async function ensureBroker(options) {
|
|
|
999
1327
|
options.signal.throwIfAborted();
|
|
1000
1328
|
}
|
|
1001
1329
|
}
|
|
1002
|
-
const bundled =
|
|
1330
|
+
const bundled = fileURLToPath3(import.meta.url);
|
|
1003
1331
|
const entry = basename(bundled) === "prim-daemon-server" ? bundled : binFile("prim-daemon-server");
|
|
1004
1332
|
if (!entry)
|
|
1005
1333
|
throw new MachineAuthError(
|
|
@@ -1137,23 +1465,23 @@ async function forwardBrokerRequest(options) {
|
|
|
1137
1465
|
}
|
|
1138
1466
|
|
|
1139
1467
|
// src/lib/machine-retry.ts
|
|
1140
|
-
import { createHash as
|
|
1141
|
-
import { lstatSync as
|
|
1142
|
-
import { join as
|
|
1468
|
+
import { createHash as createHash3 } from "crypto";
|
|
1469
|
+
import { lstatSync as lstatSync3, readFileSync as readFileSync5, readdirSync as readdirSync2, rmSync as rmSync5 } from "fs";
|
|
1470
|
+
import { join as join7 } from "path";
|
|
1143
1471
|
var SERVER_WAIT_PREFIX = "The sign-in service asked prim to wait";
|
|
1144
1472
|
var MAX_SERVER_WAIT_MS = 15 * 6e4;
|
|
1145
1473
|
async function withMachineCooldown(directory, apiUrl, clientId, signal, request3) {
|
|
1146
1474
|
signal?.throwIfAborted();
|
|
1147
|
-
const key =
|
|
1148
|
-
const path =
|
|
1475
|
+
const key = createHash3("sha256").update(`${apiUrl}\0${clientId}`).digest("hex");
|
|
1476
|
+
const path = join7(directory, `machine_retry_${key}.json`);
|
|
1149
1477
|
let state = {
|
|
1150
1478
|
failures: 0,
|
|
1151
1479
|
retryAt: 0
|
|
1152
1480
|
};
|
|
1153
1481
|
try {
|
|
1154
|
-
const stat =
|
|
1482
|
+
const stat = lstatSync3(path);
|
|
1155
1483
|
if (!stat.isFile() || stat.size > 1024) throw new Error("Invalid retry state");
|
|
1156
|
-
const data = JSON.parse(
|
|
1484
|
+
const data = JSON.parse(readFileSync5(path, "utf8"));
|
|
1157
1485
|
if (!Number.isSafeInteger(data.failures) || data.failures < 0 || data.failures > 16 || !Number.isSafeInteger(data.retryAt) || data.retryAt < 0)
|
|
1158
1486
|
throw new Error("Invalid retry state");
|
|
1159
1487
|
state = data;
|
|
@@ -1177,7 +1505,7 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request3
|
|
|
1177
1505
|
);
|
|
1178
1506
|
try {
|
|
1179
1507
|
const result = await request3();
|
|
1180
|
-
|
|
1508
|
+
rmSync5(path, { force: true });
|
|
1181
1509
|
return result;
|
|
1182
1510
|
} catch (error) {
|
|
1183
1511
|
if (!signal?.aborted && error instanceof MachineAuthError && [
|
|
@@ -1206,16 +1534,16 @@ async function withMachineCooldown(directory, apiUrl, clientId, signal, request3
|
|
|
1206
1534
|
|
|
1207
1535
|
// src/lib/session-identity.ts
|
|
1208
1536
|
import { execFile } from "child_process";
|
|
1209
|
-
import { createHash as
|
|
1210
|
-
import { rmSync as
|
|
1211
|
-
import { join as
|
|
1537
|
+
import { createHash as createHash5 } from "crypto";
|
|
1538
|
+
import { rmSync as rmSync6 } from "fs";
|
|
1539
|
+
import { join as join9 } from "path";
|
|
1212
1540
|
import { promisify } from "util";
|
|
1213
1541
|
|
|
1214
1542
|
// src/lib/git.ts
|
|
1215
1543
|
import { execFileSync } from "child_process";
|
|
1216
|
-
import { createHash as
|
|
1217
|
-
import { lstatSync as
|
|
1218
|
-
import { basename as basename2, dirname as
|
|
1544
|
+
import { createHash as createHash4 } from "crypto";
|
|
1545
|
+
import { lstatSync as lstatSync4, readFileSync as readFileSync6, realpathSync as realpathSync2 } from "fs";
|
|
1546
|
+
import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute4, join as join8, relative, resolve as resolve2, sep } from "path";
|
|
1219
1547
|
var GIT_TIMEOUT_MS = 1e3;
|
|
1220
1548
|
var MAX_SETTINGS_BYTES = 1024 * 1024;
|
|
1221
1549
|
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
@@ -1272,12 +1600,12 @@ function githubRepositoryFromRemote(remote) {
|
|
|
1272
1600
|
return path ? parseGithubRepositoryName(path) : null;
|
|
1273
1601
|
}
|
|
1274
1602
|
function checkoutSettingsText(repoRoot) {
|
|
1275
|
-
const path =
|
|
1603
|
+
const path = join8(repoRoot, ".claude", "settings.json");
|
|
1276
1604
|
try {
|
|
1277
|
-
if (!
|
|
1278
|
-
const stat =
|
|
1605
|
+
if (!lstatSync4(join8(repoRoot, ".claude")).isDirectory()) return null;
|
|
1606
|
+
const stat = lstatSync4(path);
|
|
1279
1607
|
if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
|
|
1280
|
-
return
|
|
1608
|
+
return readFileSync6(path, "utf8");
|
|
1281
1609
|
} catch {
|
|
1282
1610
|
return null;
|
|
1283
1611
|
}
|
|
@@ -1311,19 +1639,19 @@ function githubRepositoryFullName(cwd) {
|
|
|
1311
1639
|
function canonicalGitRoot(cwd) {
|
|
1312
1640
|
try {
|
|
1313
1641
|
const top = gitToplevel(cwd);
|
|
1314
|
-
return top ?
|
|
1642
|
+
return top ? realpathSync2.native(top) : null;
|
|
1315
1643
|
} catch {
|
|
1316
1644
|
return null;
|
|
1317
1645
|
}
|
|
1318
1646
|
}
|
|
1319
1647
|
function relatedWorktreeTarget(path, cwd, origin) {
|
|
1320
|
-
let parent =
|
|
1648
|
+
let parent = dirname6(resolve2(cwd, path));
|
|
1321
1649
|
while (true) {
|
|
1322
1650
|
try {
|
|
1323
|
-
|
|
1651
|
+
realpathSync2.native(parent);
|
|
1324
1652
|
break;
|
|
1325
1653
|
} catch {
|
|
1326
|
-
const next =
|
|
1654
|
+
const next = dirname6(parent);
|
|
1327
1655
|
if (next === parent) return null;
|
|
1328
1656
|
parent = next;
|
|
1329
1657
|
}
|
|
@@ -1333,7 +1661,7 @@ function relatedWorktreeTarget(path, cwd, origin) {
|
|
|
1333
1661
|
const common = (root) => {
|
|
1334
1662
|
const value = gitValue(root, ["rev-parse", "--git-common-dir"]);
|
|
1335
1663
|
try {
|
|
1336
|
-
return value ?
|
|
1664
|
+
return value ? realpathSync2.native(resolve2(root, value)) : void 0;
|
|
1337
1665
|
} catch {
|
|
1338
1666
|
return void 0;
|
|
1339
1667
|
}
|
|
@@ -1363,16 +1691,16 @@ function normalizeOriginRemote(remote) {
|
|
|
1363
1691
|
}
|
|
1364
1692
|
}
|
|
1365
1693
|
function opaqueRepoKey(source, value) {
|
|
1366
|
-
return `repo_v1_${
|
|
1694
|
+
return `repo_v1_${createHash4("sha256").update(`${source}\0${value}`).digest("hex")}`;
|
|
1367
1695
|
}
|
|
1368
1696
|
function resolveRepositoryContext(cwd = process.cwd()) {
|
|
1369
1697
|
const top = gitToplevel(cwd);
|
|
1370
1698
|
if (!top) return null;
|
|
1371
1699
|
let repoRoot;
|
|
1372
1700
|
try {
|
|
1373
|
-
repoRoot =
|
|
1701
|
+
repoRoot = realpathSync2.native(top);
|
|
1374
1702
|
} catch {
|
|
1375
|
-
repoRoot =
|
|
1703
|
+
repoRoot = resolve2(top);
|
|
1376
1704
|
}
|
|
1377
1705
|
const origin = gitValue(repoRoot, ["config", "--get", "remote.origin.url"]);
|
|
1378
1706
|
const repoFullName = githubRepositoryFullName(repoRoot) ?? void 0;
|
|
@@ -1409,19 +1737,19 @@ function nearestExistingAncestor(path) {
|
|
|
1409
1737
|
let cursor = path;
|
|
1410
1738
|
while (true) {
|
|
1411
1739
|
try {
|
|
1412
|
-
|
|
1740
|
+
lstatSync4(cursor);
|
|
1413
1741
|
break;
|
|
1414
1742
|
} catch (error) {
|
|
1415
1743
|
const code = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
1416
1744
|
if (code !== "ENOENT" && code !== "ENOTDIR") return null;
|
|
1417
|
-
const parent =
|
|
1745
|
+
const parent = dirname6(cursor);
|
|
1418
1746
|
if (parent === cursor) return null;
|
|
1419
1747
|
suffix.unshift(cursor.slice(parent.length + (parent.endsWith(sep) ? 0 : 1)));
|
|
1420
1748
|
cursor = parent;
|
|
1421
1749
|
}
|
|
1422
1750
|
}
|
|
1423
1751
|
try {
|
|
1424
|
-
return { real:
|
|
1752
|
+
return { real: realpathSync2.native(cursor), suffix };
|
|
1425
1753
|
} catch {
|
|
1426
1754
|
return null;
|
|
1427
1755
|
}
|
|
@@ -1430,13 +1758,13 @@ function canonicalRepositoryPathScoped(filePath, context, baseDir) {
|
|
|
1430
1758
|
if (!filePath || filePath !== filePath.trim() || filePath.includes("\0")) {
|
|
1431
1759
|
return { ok: false, reason: "invalid_path" };
|
|
1432
1760
|
}
|
|
1433
|
-
const absolute =
|
|
1761
|
+
const absolute = resolve2(baseDir, filePath);
|
|
1434
1762
|
const resolved = nearestExistingAncestor(absolute);
|
|
1435
1763
|
if (!resolved) return { ok: false, reason: "invalid_path" };
|
|
1436
|
-
const target =
|
|
1764
|
+
const target = resolve2(resolved.real, ...resolved.suffix);
|
|
1437
1765
|
const rel = relative(context.repoRoot, target);
|
|
1438
1766
|
if (!rel || rel === ".") return { ok: false, reason: "invalid_path" };
|
|
1439
|
-
if (
|
|
1767
|
+
if (isAbsolute4(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
1440
1768
|
return { ok: false, reason: "outside_repository" };
|
|
1441
1769
|
}
|
|
1442
1770
|
return { ok: true, file: sep === "/" ? rel : rel.split(sep).join("/") };
|
|
@@ -1444,26 +1772,26 @@ function canonicalRepositoryPathScoped(filePath, context, baseDir) {
|
|
|
1444
1772
|
function canonicalRepositoryPathLegacy(filePath, cwd, repositoryRoot = canonicalGitRoot(cwd)) {
|
|
1445
1773
|
if (!repositoryRoot || !filePath || [...filePath].some((char) => char < " " || char === "\x7F" || "~*?[]{}$`\\".includes(char)))
|
|
1446
1774
|
return null;
|
|
1447
|
-
const absolute =
|
|
1775
|
+
const absolute = resolve2(cwd, filePath);
|
|
1448
1776
|
let target;
|
|
1449
1777
|
try {
|
|
1450
|
-
const stat =
|
|
1778
|
+
const stat = lstatSync4(absolute);
|
|
1451
1779
|
if (!stat.isFile() || stat.isSymbolicLink()) return null;
|
|
1452
|
-
target =
|
|
1780
|
+
target = realpathSync2.native(absolute);
|
|
1453
1781
|
} catch (error) {
|
|
1454
1782
|
if (!(error instanceof Error) || error.code !== "ENOENT") {
|
|
1455
1783
|
return null;
|
|
1456
1784
|
}
|
|
1457
1785
|
try {
|
|
1458
|
-
const parent =
|
|
1459
|
-
if (!
|
|
1460
|
-
target =
|
|
1786
|
+
const parent = realpathSync2.native(dirname6(absolute));
|
|
1787
|
+
if (!lstatSync4(parent).isDirectory()) return null;
|
|
1788
|
+
target = resolve2(parent, basename2(absolute));
|
|
1461
1789
|
} catch {
|
|
1462
1790
|
return null;
|
|
1463
1791
|
}
|
|
1464
1792
|
}
|
|
1465
1793
|
const rel = relative(repositoryRoot, target);
|
|
1466
|
-
if (!rel ||
|
|
1794
|
+
if (!rel || isAbsolute4(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
1467
1795
|
return null;
|
|
1468
1796
|
}
|
|
1469
1797
|
return sep === "/" ? rel : rel.split(sep).join("/");
|
|
@@ -1625,7 +1953,7 @@ function sessionIdentityMessage(outcome) {
|
|
|
1625
1953
|
return `Primitive could not identify who started this Claude Code cloud session: ${outcome.cause}. To fix it, ${outcome.remedy}.`;
|
|
1626
1954
|
}
|
|
1627
1955
|
function hash(value) {
|
|
1628
|
-
return
|
|
1956
|
+
return createHash5("sha256").update(value).digest("hex");
|
|
1629
1957
|
}
|
|
1630
1958
|
function sessionContext(deps) {
|
|
1631
1959
|
const env = deps.env ?? process.env;
|
|
@@ -1652,9 +1980,9 @@ function sessionContext(deps) {
|
|
|
1652
1980
|
clientId: environment.clientId,
|
|
1653
1981
|
generation: environment.generation,
|
|
1654
1982
|
sessionId,
|
|
1655
|
-
tokenPath:
|
|
1656
|
-
statePath:
|
|
1657
|
-
lockPath:
|
|
1983
|
+
tokenPath: join9(directory, `session_identity_${key}.json`),
|
|
1984
|
+
statePath: join9(directory, `session_identity_${key}.state.json`),
|
|
1985
|
+
lockPath: join9(directory, "session-identity.lock")
|
|
1658
1986
|
};
|
|
1659
1987
|
}
|
|
1660
1988
|
function readRecord(path) {
|
|
@@ -1686,7 +2014,7 @@ function readState(context) {
|
|
|
1686
2014
|
function writeToken(context, record) {
|
|
1687
2015
|
prepareMachineMetadataDirectory(context.directory);
|
|
1688
2016
|
writeMachineMetadata(context.tokenPath, record);
|
|
1689
|
-
|
|
2017
|
+
rmSync6(context.statePath, { force: true });
|
|
1690
2018
|
}
|
|
1691
2019
|
function writeState(context, state) {
|
|
1692
2020
|
try {
|
|
@@ -2296,7 +2624,7 @@ async function ensureSessionIdentity(options = {}, deps = {}) {
|
|
|
2296
2624
|
return identified(refreshed.record, true);
|
|
2297
2625
|
}
|
|
2298
2626
|
if (refreshed.status === "refused") {
|
|
2299
|
-
|
|
2627
|
+
rmSync6(ctx.tokenPath, { force: true });
|
|
2300
2628
|
writeState(ctx, {
|
|
2301
2629
|
sessionId: ctx.sessionId,
|
|
2302
2630
|
status: "failed",
|
|
@@ -2440,7 +2768,7 @@ async function acquireSessionCredential(options = {}, deps = {}) {
|
|
|
2440
2768
|
function sessionScoped(deps) {
|
|
2441
2769
|
return !deps.localEnvironment && !deps.environmentOnly && sessionIdentityMode(deps);
|
|
2442
2770
|
}
|
|
2443
|
-
var
|
|
2771
|
+
var require3 = createRequire2(import.meta.url);
|
|
2444
2772
|
var SERVICE = "ai.primitive.prim.machine";
|
|
2445
2773
|
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
2446
2774
|
var PROFILE_FILE = "machine_profile.json";
|
|
@@ -2453,16 +2781,16 @@ function apiUrlFor(deps) {
|
|
|
2453
2781
|
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL2);
|
|
2454
2782
|
}
|
|
2455
2783
|
function hash2(value) {
|
|
2456
|
-
return
|
|
2784
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
2457
2785
|
}
|
|
2458
2786
|
function recordPath(directory, generation) {
|
|
2459
|
-
return
|
|
2787
|
+
return join10(directory, `machine_${generation}.json`);
|
|
2460
2788
|
}
|
|
2461
2789
|
function storageKey(directory, generation) {
|
|
2462
2790
|
return `${hash2(directory)}:${generation}`;
|
|
2463
2791
|
}
|
|
2464
2792
|
function environmentContextPath(deps, clientId) {
|
|
2465
|
-
return
|
|
2793
|
+
return join10(
|
|
2466
2794
|
configurationDirectory(deps),
|
|
2467
2795
|
`machine_context_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
2468
2796
|
);
|
|
@@ -2522,22 +2850,28 @@ function readEnvironmentContext(environment, deps) {
|
|
|
2522
2850
|
return void 0;
|
|
2523
2851
|
}
|
|
2524
2852
|
}
|
|
2525
|
-
function
|
|
2526
|
-
let directory =
|
|
2853
|
+
function keyringLoaderPath(start, options = {}) {
|
|
2854
|
+
let directory = start;
|
|
2527
2855
|
for (let depth = 0; depth < 5; depth++) {
|
|
2528
|
-
const loader =
|
|
2529
|
-
if (
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2856
|
+
const loader = join10(directory, "native", "keyring.cjs");
|
|
2857
|
+
if (existsSync5(loader)) return loader;
|
|
2858
|
+
directory = dirname7(directory);
|
|
2859
|
+
}
|
|
2860
|
+
try {
|
|
2861
|
+
createRequire2(join10(start, "noop.js")).resolve("@napi-rs/keyring");
|
|
2862
|
+
return void 0;
|
|
2863
|
+
} catch {
|
|
2864
|
+
return selectedHookRuntimeKeyringLoader(options);
|
|
2534
2865
|
}
|
|
2535
|
-
|
|
2866
|
+
}
|
|
2867
|
+
function keyringEntry(key) {
|
|
2868
|
+
const loader = keyringLoaderPath(dirname7(fileURLToPath4(import.meta.url)));
|
|
2869
|
+
const module = require3(loader ?? "@napi-rs/keyring");
|
|
2536
2870
|
return new module.AsyncEntry(SERVICE, key, { linux: { store: "secret-service" } });
|
|
2537
2871
|
}
|
|
2538
2872
|
function protectPath(path, directory) {
|
|
2539
2873
|
if (process.platform !== "win32") {
|
|
2540
|
-
|
|
2874
|
+
chmodSync2(path, directory ? 448 : 384);
|
|
2541
2875
|
return;
|
|
2542
2876
|
}
|
|
2543
2877
|
const encodedPath = Buffer.from(path).toString("base64");
|
|
@@ -2554,8 +2888,8 @@ function protectPath(path, directory) {
|
|
|
2554
2888
|
);
|
|
2555
2889
|
}
|
|
2556
2890
|
function ensureDirectory(directory) {
|
|
2557
|
-
|
|
2558
|
-
if (
|
|
2891
|
+
mkdirSync4(directory, { recursive: true, mode: 448 });
|
|
2892
|
+
if (lstatSync5(directory).isSymbolicLink())
|
|
2559
2893
|
throw new MachineAuthError(
|
|
2560
2894
|
"unsafe_storage",
|
|
2561
2895
|
"Credential directory must not be a symbolic link."
|
|
@@ -2573,14 +2907,14 @@ function readMachineMetadata(path) {
|
|
|
2573
2907
|
function readPrivate(path) {
|
|
2574
2908
|
let raw;
|
|
2575
2909
|
try {
|
|
2576
|
-
const stat =
|
|
2910
|
+
const stat = lstatSync5(path);
|
|
2577
2911
|
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
2578
2912
|
throw new MachineAuthError(
|
|
2579
2913
|
"unsafe_storage",
|
|
2580
2914
|
"Machine credential files must be owner-only regular files."
|
|
2581
2915
|
);
|
|
2582
2916
|
}
|
|
2583
|
-
raw =
|
|
2917
|
+
raw = readFileSync7(path, "utf8");
|
|
2584
2918
|
} catch (error) {
|
|
2585
2919
|
if (error.code === "ENOENT") return void 0;
|
|
2586
2920
|
throw error;
|
|
@@ -2625,7 +2959,7 @@ function parseProfile(value) {
|
|
|
2625
2959
|
};
|
|
2626
2960
|
}
|
|
2627
2961
|
function readMachineProfile(deps = {}) {
|
|
2628
|
-
const value = readPrivate(
|
|
2962
|
+
const value = readPrivate(join10(configurationDirectory(deps), PROFILE_FILE));
|
|
2629
2963
|
return value === void 0 ? void 0 : parseProfile(value);
|
|
2630
2964
|
}
|
|
2631
2965
|
var NETWORK_SECRET_AUTH = "network-secret";
|
|
@@ -2634,20 +2968,20 @@ var INJECTED_IDENTITY_TTL_MS = 60 * 6e4;
|
|
|
2634
2968
|
var brokerKeys = /* @__PURE__ */ new Map();
|
|
2635
2969
|
function networkSecretBrokerKey(deps, clientId) {
|
|
2636
2970
|
const directory = configurationDirectory(deps);
|
|
2637
|
-
const path =
|
|
2971
|
+
const path = join10(directory, `machine_network_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.key`);
|
|
2638
2972
|
const known = brokerKeys.get(path);
|
|
2639
2973
|
if (known) return known;
|
|
2640
2974
|
ensureDirectory(directory);
|
|
2641
2975
|
const read = () => {
|
|
2642
2976
|
try {
|
|
2643
|
-
const stat =
|
|
2977
|
+
const stat = lstatSync5(path);
|
|
2644
2978
|
if (!stat.isFile() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()))
|
|
2645
2979
|
throw new MachineAuthError(
|
|
2646
2980
|
"unsafe_storage",
|
|
2647
2981
|
`The machine broker key at ${path} is not an owner-only file. Delete it and retry.`,
|
|
2648
2982
|
true
|
|
2649
2983
|
);
|
|
2650
|
-
const value =
|
|
2984
|
+
const value = readFileSync7(path, "utf8").trim();
|
|
2651
2985
|
return { key: /^[a-f0-9]{64}$/u.test(value) ? value : void 0 };
|
|
2652
2986
|
} catch (error) {
|
|
2653
2987
|
if (error.code === "ENOENT") return void 0;
|
|
@@ -2666,16 +3000,16 @@ function networkSecretBrokerKey(deps, clientId) {
|
|
|
2666
3000
|
`The machine broker key at ${path} is damaged. Delete it and retry.`,
|
|
2667
3001
|
true
|
|
2668
3002
|
);
|
|
2669
|
-
const staged = `${path}.${process.pid}.${
|
|
3003
|
+
const staged = `${path}.${process.pid}.${randomBytes4(6).toString("hex")}`;
|
|
2670
3004
|
try {
|
|
2671
|
-
|
|
3005
|
+
writeFileSync4(staged, `${randomBytes4(32).toString("hex")}
|
|
2672
3006
|
`, { mode: 384, flag: "wx" });
|
|
2673
3007
|
protectPath(staged, false);
|
|
2674
3008
|
linkSync(staged, path);
|
|
2675
3009
|
} catch (error) {
|
|
2676
3010
|
if (error.code !== "EEXIST") throw error;
|
|
2677
3011
|
} finally {
|
|
2678
|
-
|
|
3012
|
+
rmSync7(staged, { force: true });
|
|
2679
3013
|
}
|
|
2680
3014
|
}
|
|
2681
3015
|
const key = read()?.key;
|
|
@@ -2734,7 +3068,7 @@ function environmentCredentials(deps) {
|
|
|
2734
3068
|
}
|
|
2735
3069
|
function hasMachineConfiguration(deps = {}) {
|
|
2736
3070
|
const env = deps.env ?? process.env;
|
|
2737
|
-
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 ||
|
|
3071
|
+
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));
|
|
2738
3072
|
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
2739
3073
|
throw new MachineAuthError(
|
|
2740
3074
|
"conflicting_credentials",
|
|
@@ -2770,7 +3104,7 @@ function machineSessionEnded(deps = {}) {
|
|
|
2770
3104
|
const generation = environmentCredentials(deps)?.generation ?? readMachineProfile(deps)?.generation;
|
|
2771
3105
|
if (!generation) return false;
|
|
2772
3106
|
if (environmentCredentials(deps)) return environmentTerminal === generation;
|
|
2773
|
-
return readPrivate(
|
|
3107
|
+
return readPrivate(join10(configurationDirectory(deps), "machine_terminal.json")) === generation;
|
|
2774
3108
|
}
|
|
2775
3109
|
var environmentTerminal;
|
|
2776
3110
|
var NETWORK_REJECTION_PAUSE_MS = 3e4;
|
|
@@ -2863,7 +3197,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2863
3197
|
}
|
|
2864
3198
|
}
|
|
2865
3199
|
return withFileLock(
|
|
2866
|
-
|
|
3200
|
+
join10(directory2, "credentials.lock"),
|
|
2867
3201
|
async () => {
|
|
2868
3202
|
if (machineSessionEnded(deps))
|
|
2869
3203
|
throw new MachineAuthError(
|
|
@@ -2945,7 +3279,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2945
3279
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2946
3280
|
try {
|
|
2947
3281
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2948
|
-
|
|
3282
|
+
rmSync7(contextPath, { force: true });
|
|
2949
3283
|
} catch {
|
|
2950
3284
|
}
|
|
2951
3285
|
throw error;
|
|
@@ -2955,7 +3289,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2955
3289
|
const contextPath = environmentContextPath(deps, environment.clientId);
|
|
2956
3290
|
try {
|
|
2957
3291
|
if (machineRecord(readPrivate(contextPath)).generation === environment.generation)
|
|
2958
|
-
|
|
3292
|
+
rmSync7(contextPath, { force: true });
|
|
2959
3293
|
} catch {
|
|
2960
3294
|
}
|
|
2961
3295
|
}
|
|
@@ -2969,7 +3303,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
2969
3303
|
const expected = machineContext(deps);
|
|
2970
3304
|
const directory = configurationDirectory(deps);
|
|
2971
3305
|
return withFileLock(
|
|
2972
|
-
|
|
3306
|
+
join10(directory, "credentials.lock"),
|
|
2973
3307
|
async () => {
|
|
2974
3308
|
const profile = readMachineProfile(deps);
|
|
2975
3309
|
if (!profile || profile.generation !== expected?.generation || profile.apiUrl !== apiUrl)
|
|
@@ -3019,7 +3353,7 @@ async function acquireMachineCredential(options = {}, deps = {}) {
|
|
|
3019
3353
|
await persistRecord(record, deps, signal);
|
|
3020
3354
|
} catch (error) {
|
|
3021
3355
|
if (error instanceof MachineAuthError && error.terminal)
|
|
3022
|
-
writePrivate(
|
|
3356
|
+
writePrivate(join10(directory, "machine_terminal.json"), profile.generation);
|
|
3023
3357
|
throw error;
|
|
3024
3358
|
}
|
|
3025
3359
|
}
|
|
@@ -3082,20 +3416,20 @@ function writeMachineMetadata(path, value) {
|
|
|
3082
3416
|
|
|
3083
3417
|
// src/lib/credentials.ts
|
|
3084
3418
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
3085
|
-
var TOKEN_FILE_PATH =
|
|
3086
|
-
var REFRESH_TOKEN_PATH =
|
|
3087
|
-
var TOKEN_EXPIRES_PATH =
|
|
3088
|
-
var TERMINAL_REFRESH_PATH =
|
|
3089
|
-
var CREDENTIAL_METADATA_PATH =
|
|
3090
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
3091
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
3092
|
-
var CREDENTIAL_LOCK_PATH =
|
|
3419
|
+
var TOKEN_FILE_PATH = join11(CONFIG_DIRECTORY, "token");
|
|
3420
|
+
var REFRESH_TOKEN_PATH = join11(CONFIG_DIRECTORY, "refresh_token");
|
|
3421
|
+
var TOKEN_EXPIRES_PATH = join11(CONFIG_DIRECTORY, "token_expires_at");
|
|
3422
|
+
var TERMINAL_REFRESH_PATH = join11(CONFIG_DIRECTORY, "refresh_terminal");
|
|
3423
|
+
var CREDENTIAL_METADATA_PATH = join11(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
3424
|
+
var CREDENTIAL_FAMILY_PATH = join11(CONFIG_DIRECTORY, "credential_family.json");
|
|
3425
|
+
var CREDENTIAL_MIGRATION_PATH = join11(CONFIG_DIRECTORY, "credential_migration.json");
|
|
3426
|
+
var CREDENTIAL_LOCK_PATH = join11(CONFIG_DIRECTORY, "credentials.lock");
|
|
3093
3427
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
3094
3428
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
3095
3429
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
3096
3430
|
function readTrimmed(path) {
|
|
3097
3431
|
try {
|
|
3098
|
-
const value =
|
|
3432
|
+
const value = readFileSync8(path, "utf8").trim();
|
|
3099
3433
|
return value || void 0;
|
|
3100
3434
|
} catch {
|
|
3101
3435
|
return void 0;
|
|
@@ -3132,7 +3466,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
3132
3466
|
return { token: stored, source: "token_file" };
|
|
3133
3467
|
}
|
|
3134
3468
|
function credentialFingerprint(value) {
|
|
3135
|
-
return
|
|
3469
|
+
return createHash7("sha256").update(value).digest("hex");
|
|
3136
3470
|
}
|
|
3137
3471
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
3138
3472
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -3152,7 +3486,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
3152
3486
|
function readBoundedCredentialJson(path) {
|
|
3153
3487
|
let raw;
|
|
3154
3488
|
try {
|
|
3155
|
-
raw =
|
|
3489
|
+
raw = readFileSync8(path, "utf8");
|
|
3156
3490
|
} catch (error) {
|
|
3157
3491
|
return error.code === "ENOENT" ? void 0 : null;
|
|
3158
3492
|
}
|
|
@@ -3229,14 +3563,14 @@ function jwtOrganizationId(token) {
|
|
|
3229
3563
|
|
|
3230
3564
|
// src/binding.ts
|
|
3231
3565
|
var PRIM_CONFIG_DIR = primConfigDirectory();
|
|
3232
|
-
var SESSIONS_DIR =
|
|
3566
|
+
var SESSIONS_DIR = join12(PRIM_CONFIG_DIR, "sessions");
|
|
3233
3567
|
var WORKSPACE_FILE = ".prim/workspace.json";
|
|
3234
3568
|
function readJsonSafe(path) {
|
|
3235
|
-
if (!
|
|
3569
|
+
if (!existsSync6(path)) {
|
|
3236
3570
|
return;
|
|
3237
3571
|
}
|
|
3238
3572
|
try {
|
|
3239
|
-
return JSON.parse(
|
|
3573
|
+
return JSON.parse(readFileSync9(path, "utf-8"));
|
|
3240
3574
|
} catch {
|
|
3241
3575
|
return;
|
|
3242
3576
|
}
|
|
@@ -3245,17 +3579,17 @@ function fromSessionMarker(sessionId) {
|
|
|
3245
3579
|
if (!sessionId) {
|
|
3246
3580
|
return;
|
|
3247
3581
|
}
|
|
3248
|
-
const marker = readJsonSafe(
|
|
3582
|
+
const marker = readJsonSafe(join12(SESSIONS_DIR, `${sessionId}.json`));
|
|
3249
3583
|
return marker?.orgId;
|
|
3250
3584
|
}
|
|
3251
3585
|
function fromWorkspaceFile(startDir) {
|
|
3252
3586
|
let dir = startDir;
|
|
3253
3587
|
while (true) {
|
|
3254
|
-
const config = readJsonSafe(
|
|
3588
|
+
const config = readJsonSafe(join12(dir, WORKSPACE_FILE));
|
|
3255
3589
|
if (config?.orgId) {
|
|
3256
3590
|
return config.orgId;
|
|
3257
3591
|
}
|
|
3258
|
-
const parent =
|
|
3592
|
+
const parent = dirname8(dir);
|
|
3259
3593
|
if (parent === dir) {
|
|
3260
3594
|
return;
|
|
3261
3595
|
}
|
|
@@ -3365,14 +3699,14 @@ function publicDecisionResponse(value) {
|
|
|
3365
3699
|
}
|
|
3366
3700
|
|
|
3367
3701
|
// src/client.ts
|
|
3368
|
-
import { createHash as
|
|
3369
|
-
import { chmodSync as
|
|
3370
|
-
import { dirname as
|
|
3702
|
+
import { createHash as createHash8 } from "crypto";
|
|
3703
|
+
import { chmodSync as chmodSync4, existsSync as existsSync7, mkdirSync as mkdirSync5, readFileSync as readFileSync11, rmSync as rmSync8 } from "fs";
|
|
3704
|
+
import { dirname as dirname9 } from "path";
|
|
3371
3705
|
|
|
3372
3706
|
// src/daemon/client-instance-id.ts
|
|
3373
|
-
import { randomBytes as
|
|
3374
|
-
import { chmodSync as
|
|
3375
|
-
import { join as
|
|
3707
|
+
import { randomBytes as randomBytes5 } from "crypto";
|
|
3708
|
+
import { chmodSync as chmodSync3, lstatSync as lstatSync6, readFileSync as readFileSync10 } from "fs";
|
|
3709
|
+
import { join as join13 } from "path";
|
|
3376
3710
|
var CONFIG_DIRECTORY_MODE = 448;
|
|
3377
3711
|
var INSTANCE_FILE_MODE = 384;
|
|
3378
3712
|
var INSTANCE_ENTROPY_BYTES = 32;
|
|
@@ -3385,17 +3719,17 @@ function isClientInstanceId(value) {
|
|
|
3385
3719
|
}
|
|
3386
3720
|
function ensurePrivateConfigDirectory(configDir) {
|
|
3387
3721
|
ensureDurableDirectory(configDir, CONFIG_DIRECTORY_MODE);
|
|
3388
|
-
const metadata =
|
|
3722
|
+
const metadata = lstatSync6(configDir);
|
|
3389
3723
|
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
3390
3724
|
throw new Error("prim config path must be a private directory");
|
|
3391
3725
|
}
|
|
3392
|
-
|
|
3726
|
+
chmodSync3(configDir, CONFIG_DIRECTORY_MODE);
|
|
3393
3727
|
syncDirectory(configDir);
|
|
3394
3728
|
}
|
|
3395
3729
|
function readExisting(path) {
|
|
3396
3730
|
let metadata;
|
|
3397
3731
|
try {
|
|
3398
|
-
metadata =
|
|
3732
|
+
metadata = lstatSync6(path);
|
|
3399
3733
|
} catch (error) {
|
|
3400
3734
|
if (error.code === "ENOENT") {
|
|
3401
3735
|
return;
|
|
@@ -3405,11 +3739,11 @@ function readExisting(path) {
|
|
|
3405
3739
|
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
3406
3740
|
throw new Error("stored client instance identity is invalid");
|
|
3407
3741
|
}
|
|
3408
|
-
|
|
3742
|
+
chmodSync3(path, INSTANCE_FILE_MODE);
|
|
3409
3743
|
if (metadata.size > MAX_STORED_BYTES) {
|
|
3410
3744
|
throw new Error("stored client instance identity is invalid");
|
|
3411
3745
|
}
|
|
3412
|
-
const raw =
|
|
3746
|
+
const raw = readFileSync10(path, "utf8");
|
|
3413
3747
|
const value = raw.endsWith("\n") ? raw.slice(0, -1) : raw;
|
|
3414
3748
|
if (raw !== value && raw !== `${value}
|
|
3415
3749
|
` || !isClientInstanceId(value)) {
|
|
@@ -3423,8 +3757,8 @@ function atomicWrite(path, value) {
|
|
|
3423
3757
|
}
|
|
3424
3758
|
async function getOrCreateClientInstanceId(options) {
|
|
3425
3759
|
const configDir = options.configDir;
|
|
3426
|
-
const path =
|
|
3427
|
-
const lockPath =
|
|
3760
|
+
const path = join13(configDir, "client_instance_id");
|
|
3761
|
+
const lockPath = join13(configDir, "client-instance.lock");
|
|
3428
3762
|
ensurePrivateConfigDirectory(configDir);
|
|
3429
3763
|
return withFileLock(
|
|
3430
3764
|
lockPath,
|
|
@@ -3433,7 +3767,7 @@ async function getOrCreateClientInstanceId(options) {
|
|
|
3433
3767
|
if (existing !== void 0) {
|
|
3434
3768
|
return existing;
|
|
3435
3769
|
}
|
|
3436
|
-
const generated = `${INSTANCE_PREFIX}${
|
|
3770
|
+
const generated = `${INSTANCE_PREFIX}${randomBytes5(INSTANCE_ENTROPY_BYTES).toString(
|
|
3437
3771
|
"base64url"
|
|
3438
3772
|
)}`;
|
|
3439
3773
|
if (!isClientInstanceId(generated)) {
|
|
@@ -3479,7 +3813,7 @@ function authExpiredMessage() {
|
|
|
3479
3813
|
}
|
|
3480
3814
|
function readTrimmed2(path) {
|
|
3481
3815
|
try {
|
|
3482
|
-
const value =
|
|
3816
|
+
const value = readFileSync11(path, "utf8").trim();
|
|
3483
3817
|
return value || void 0;
|
|
3484
3818
|
} catch {
|
|
3485
3819
|
return void 0;
|
|
@@ -3495,17 +3829,17 @@ function expiresAtFor(token, expiresIn) {
|
|
|
3495
3829
|
return expiresIn === void 0 ? jwtExpiresAt(token) : Date.now() + expiresIn * 1e3;
|
|
3496
3830
|
}
|
|
3497
3831
|
function ensureConfigDirectory() {
|
|
3498
|
-
const directory =
|
|
3499
|
-
|
|
3500
|
-
|
|
3832
|
+
const directory = dirname9(TOKEN_FILE_PATH);
|
|
3833
|
+
mkdirSync5(directory, { recursive: true, mode: CONFIG_DIR_MODE });
|
|
3834
|
+
chmodSync4(directory, CONFIG_DIR_MODE);
|
|
3501
3835
|
}
|
|
3502
3836
|
function atomicWrite2(path, content2) {
|
|
3503
3837
|
ensureConfigDirectory();
|
|
3504
3838
|
atomicWriteFile(path, content2, { mode: CREDENTIAL_FILE_MODE });
|
|
3505
3839
|
}
|
|
3506
3840
|
function removeCredentialFile(path) {
|
|
3507
|
-
if (!
|
|
3508
|
-
|
|
3841
|
+
if (!existsSync7(path)) return false;
|
|
3842
|
+
rmSync8(path, { force: true });
|
|
3509
3843
|
return true;
|
|
3510
3844
|
}
|
|
3511
3845
|
function getTokenExpiresAt() {
|
|
@@ -3521,7 +3855,7 @@ function isTokenExpiringSoon(credential) {
|
|
|
3521
3855
|
return expiresAt !== void 0 && Date.now() >= expiresAt - REFRESH_THRESHOLD_MS;
|
|
3522
3856
|
}
|
|
3523
3857
|
function refreshFingerprint(refreshToken3) {
|
|
3524
|
-
return
|
|
3858
|
+
return createHash8("sha256").update(refreshToken3).digest("hex");
|
|
3525
3859
|
}
|
|
3526
3860
|
function legacyBrokerCredentialMetadata(accessToken, refreshToken3) {
|
|
3527
3861
|
return {
|
|
@@ -4211,126 +4545,6 @@ import { existsSync as existsSync10 } from "fs";
|
|
|
4211
4545
|
import { existsSync as existsSync9 } from "fs";
|
|
4212
4546
|
import { join as join16 } from "path";
|
|
4213
4547
|
|
|
4214
|
-
// src/lib/hook-runtime.ts
|
|
4215
|
-
import { createHash as createHash8, randomBytes as randomBytes5 } from "crypto";
|
|
4216
|
-
import {
|
|
4217
|
-
constants as constants2,
|
|
4218
|
-
accessSync,
|
|
4219
|
-
chmodSync as chmodSync4,
|
|
4220
|
-
copyFileSync,
|
|
4221
|
-
existsSync as existsSync7,
|
|
4222
|
-
lstatSync as lstatSync6,
|
|
4223
|
-
mkdirSync as mkdirSync5,
|
|
4224
|
-
mkdtempSync,
|
|
4225
|
-
readFileSync as readFileSync11,
|
|
4226
|
-
readdirSync as readdirSync2,
|
|
4227
|
-
realpathSync as realpathSync2,
|
|
4228
|
-
renameSync as renameSync2,
|
|
4229
|
-
rmSync as rmSync8,
|
|
4230
|
-
statSync as statSync2,
|
|
4231
|
-
utimesSync,
|
|
4232
|
-
writeFileSync as writeFileSync4
|
|
4233
|
-
} from "fs";
|
|
4234
|
-
import { delimiter, dirname as dirname9, isAbsolute as isAbsolute4, join as join14, resolve as resolve3 } from "path";
|
|
4235
|
-
|
|
4236
|
-
// src/lib/git-hook-contract.ts
|
|
4237
|
-
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
4238
|
-
# prim git hook entrypoint v1 \u2014 managed by prim; do not edit.
|
|
4239
|
-
# Run by the "# >>> prim <hook> hook >>>" block in a Git hook file. Acts only
|
|
4240
|
-
# where prim is active (git config prim.active true) and never fails the hook.
|
|
4241
|
-
case "$0" in */*) prim_dir=\${0%/*} ;; *) exit 0 ;; esac
|
|
4242
|
-
prim_launcher="$prim_dir/${STABLE_HOOK_LAUNCHER_NAME}"
|
|
4243
|
-
[ -x "$prim_launcher" ] || exit 0
|
|
4244
|
-
[ "$(git config --get prim.active 2>/dev/null)" = "true" ] || exit 0
|
|
4245
|
-
prim_hook=\${1-}
|
|
4246
|
-
if [ "$#" -gt 0 ]; then shift; fi
|
|
4247
|
-
case "$prim_hook" in
|
|
4248
|
-
pre-commit)
|
|
4249
|
-
"$prim_launcher" prim-pre-commit "$@" || :
|
|
4250
|
-
;;
|
|
4251
|
-
post-commit)
|
|
4252
|
-
prim_sha=$(git rev-parse --verify HEAD 2>/dev/null) || exit 0
|
|
4253
|
-
case "$prim_sha" in *[!0-9a-f]* | "") exit 0 ;; esac
|
|
4254
|
-
case "\${#prim_sha}" in 40 | 64) ;; *) exit 0 ;; esac
|
|
4255
|
-
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
4256
|
-
prim_observed=$(mktemp "\${TMPDIR:-/tmp}/prim-post-commit-observed.XXXXXXXX" 2>/dev/null) || prim_observed=
|
|
4257
|
-
(
|
|
4258
|
-
trap '' HUP
|
|
4259
|
-
trap 'if [ -n "$prim_observed" ]; then rm -f "$prim_observed"; fi' 0
|
|
4260
|
-
PRIM_COMMIT_SHA=$prim_sha PRIM_COMMIT_BRANCH=$prim_branch PRIM_COMMIT_OBSERVED_FILE=$prim_observed "$prim_launcher" prim-post-commit
|
|
4261
|
-
) </dev/null >/dev/null 2>&1 &
|
|
4262
|
-
;;
|
|
4263
|
-
post-rewrite)
|
|
4264
|
-
case "\${1-}" in amend | rebase) prim_source=$1 ;; *) exit 0 ;; esac
|
|
4265
|
-
prim_pairs=$(mktemp "\${TMPDIR:-/tmp}/prim-post-rewrite-pairs.XXXXXXXX" 2>/dev/null) || exit 0
|
|
4266
|
-
if ! chmod 600 "$prim_pairs" 2>/dev/null || ! cat >"$prim_pairs"; then
|
|
4267
|
-
rm -f "$prim_pairs"
|
|
4268
|
-
exit 0
|
|
4269
|
-
fi
|
|
4270
|
-
prim_branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || prim_branch=
|
|
4271
|
-
(
|
|
4272
|
-
trap '' HUP
|
|
4273
|
-
trap 'rm -f "$prim_pairs"' 0
|
|
4274
|
-
PRIM_REWRITE_SOURCE=$prim_source PRIM_REWRITE_BRANCH=$prim_branch PRIM_REWRITE_PAIRS_FILE=$prim_pairs "$prim_launcher" prim-post-rewrite
|
|
4275
|
-
) </dev/null >/dev/null 2>&1 &
|
|
4276
|
-
;;
|
|
4277
|
-
esac
|
|
4278
|
-
exit 0
|
|
4279
|
-
`;
|
|
4280
|
-
|
|
4281
|
-
// src/lib/native-keyring.ts
|
|
4282
|
-
import { existsSync as existsSync6 } from "fs";
|
|
4283
|
-
import { createRequire as createRequire2 } from "module";
|
|
4284
|
-
import { dirname as dirname8, join as join13 } from "path";
|
|
4285
|
-
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
4286
|
-
var require3 = createRequire2(import.meta.url);
|
|
4287
|
-
|
|
4288
|
-
// src/lib/hook-runtime.ts
|
|
4289
|
-
var RELEASE_PREFIX = "release-";
|
|
4290
|
-
var MAX_MANIFEST_BYTES = 64 * 1024;
|
|
4291
|
-
var RELEASE_GRACE_MS = 10 * 60 * 1e3;
|
|
4292
|
-
var HOOK_RUNTIME_ENTRIES = {
|
|
4293
|
-
// SessionStart self-healing resolves this exact CLI entry to run
|
|
4294
|
-
// `daemon ensure`; it must travel with the immutable hook release.
|
|
4295
|
-
prim: "dist/index.js",
|
|
4296
|
-
"prim-hook": "dist/hooks/prim-hook.js",
|
|
4297
|
-
"prim-pre-commit": "dist/hooks/pre-commit.js",
|
|
4298
|
-
"prim-post-commit": "dist/hooks/post-commit.js",
|
|
4299
|
-
"prim-post-rewrite": "dist/hooks/post-rewrite.js",
|
|
4300
|
-
"prim-pre-tool-use-worker": "dist/hooks/pre-tool-use-worker.js",
|
|
4301
|
-
"prim-pre-tool-use": "dist/hooks/pre-tool-use.js",
|
|
4302
|
-
"prim-post-tool-use": "dist/hooks/post-tool-use.js",
|
|
4303
|
-
"prim-session-start": "dist/hooks/session-start.js",
|
|
4304
|
-
"prim-session-end": "dist/hooks/session-end.js",
|
|
4305
|
-
"prim-daemon-server": "dist/daemon/server.js",
|
|
4306
|
-
"prim-statusline": "dist/statusline-main.js"
|
|
4307
|
-
};
|
|
4308
|
-
var SOURCE_ENTRIES = Object.fromEntries(
|
|
4309
|
-
Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, target]) => [
|
|
4310
|
-
bin,
|
|
4311
|
-
target.replace(/^dist\//u, "")
|
|
4312
|
-
])
|
|
4313
|
-
);
|
|
4314
|
-
var STABLE_HOOK_LAUNCHER_CONTENT = `#!/bin/sh
|
|
4315
|
-
set -eu
|
|
4316
|
-
case "$0" in */*) prim_launcher_dir=\${0%/*} ;; *) exit 78 ;; esac
|
|
4317
|
-
IFS= read -r prim_release_name < "$prim_launcher_dir/hook-runtime/current" || exit 69
|
|
4318
|
-
case "$prim_release_name" in ${RELEASE_PREFIX}*) ;; *) exit 69 ;; esac
|
|
4319
|
-
prim_release_id=\${prim_release_name#${RELEASE_PREFIX}}
|
|
4320
|
-
case "$prim_release_id" in *[!0-9a-f]*|"") exit 69 ;; esac
|
|
4321
|
-
[ "\${#prim_release_id}" -eq 64 ] || exit 69
|
|
4322
|
-
prim_release="$prim_launcher_dir/hook-runtime/releases/$prim_release_name"
|
|
4323
|
-
case "\${1-}" in
|
|
4324
|
-
${Object.entries(HOOK_RUNTIME_ENTRIES).map(([bin, entry]) => ` ${bin}) prim_entry="$prim_release/${entry}" ;;`).join("\n")}
|
|
4325
|
-
*) exit 64 ;;
|
|
4326
|
-
esac
|
|
4327
|
-
shift
|
|
4328
|
-
IFS= read -r prim_node < "$prim_release/node" || exit 69
|
|
4329
|
-
case "$prim_node" in /*) ;; *) exit 69 ;; esac
|
|
4330
|
-
[ -x "$prim_node" ] && [ -f "$prim_entry" ] || exit 69
|
|
4331
|
-
exec "$prim_node" "$prim_entry" "$@"
|
|
4332
|
-
`;
|
|
4333
|
-
|
|
4334
4548
|
// src/demo/state.ts
|
|
4335
4549
|
import { createHash as createHash9, randomUUID as randomUUID3 } from "crypto";
|
|
4336
4550
|
import { existsSync as existsSync8, lstatSync as lstatSync7, readFileSync as readFileSync12, readdirSync as readdirSync3 } from "fs";
|
|
@@ -32006,18 +32220,18 @@ var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
|
32006
32220
|
function boundedClaim(value) {
|
|
32007
32221
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
32008
32222
|
}
|
|
32009
|
-
function
|
|
32223
|
+
function sha2562(value) {
|
|
32010
32224
|
return createHash10("sha256").update(value).digest("hex");
|
|
32011
32225
|
}
|
|
32012
32226
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
32013
32227
|
if (hasMachineConfiguration()) {
|
|
32014
32228
|
const machine = machineContext();
|
|
32015
32229
|
return machine ? {
|
|
32016
|
-
principalId:
|
|
32230
|
+
principalId: sha2562(
|
|
32017
32231
|
`${machine.issuer}\0${machine.clientId}${machine.authorizedUserId ? `\0${machine.authorizedUserId}` : ""}`
|
|
32018
32232
|
),
|
|
32019
32233
|
organizationId: machine.workosOrganizationId,
|
|
32020
|
-
credentialFingerprint:
|
|
32234
|
+
credentialFingerprint: sha2562(machine.generation)
|
|
32021
32235
|
} : void 0;
|
|
32022
32236
|
}
|
|
32023
32237
|
if (!token) return void 0;
|
|
@@ -32027,9 +32241,9 @@ function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
|
32027
32241
|
if (!(subject && organizationId)) return void 0;
|
|
32028
32242
|
const issuer = boundedClaim(payload?.iss) ?? "unknown-issuer";
|
|
32029
32243
|
return {
|
|
32030
|
-
principalId:
|
|
32244
|
+
principalId: sha2562(`${issuer}\0${subject}`),
|
|
32031
32245
|
organizationId,
|
|
32032
|
-
credentialFingerprint:
|
|
32246
|
+
credentialFingerprint: sha2562(token)
|
|
32033
32247
|
};
|
|
32034
32248
|
}
|
|
32035
32249
|
function daemonPrincipalsMatch(caller, daemon) {
|