@primitive.ai/prim 0.1.0-alpha.127 → 0.1.0-alpha.128
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -5
- package/dist/{chunk-NVDPKW7Z.js → chunk-3KPTVK4N.js} +3 -51
- package/dist/{chunk-URYZDQFH.js → chunk-4DYEBYKM.js} +3 -5
- package/dist/{chunk-HZI5PBN4.js → chunk-6QAXVFRJ.js} +1129 -39
- package/dist/{chunk-A5VTE43F.js → chunk-ANMNGGTN.js} +2 -2
- package/dist/{chunk-TNWI3JWK.js → chunk-B3ICJZXV.js} +4 -2
- package/dist/{chunk-RV3JFSMX.js → chunk-C357P37M.js} +4 -4
- package/dist/{chunk-KFRGVHGB.js → chunk-E57UVUJL.js} +6 -6
- package/dist/{chunk-MGKSZSTV.js → chunk-EQWUCFXK.js} +3 -3
- package/dist/{chunk-72NIZMZN.js → chunk-M6KIDB5R.js} +8 -3
- package/dist/{chunk-O65UVJZH.js → chunk-N6PZE7HX.js} +2 -2
- package/dist/{chunk-WGTUNUIS.js → chunk-NYDM4HSS.js} +5 -5
- package/dist/{chunk-R4EYLEIK.js → chunk-P4O7L7JZ.js} +2 -2
- package/dist/{chunk-UUQT5GMO.js → chunk-PRJ7P6J5.js} +1 -1
- package/dist/{chunk-H74RDXKP.js → chunk-PTZ6BWSU.js} +2 -2
- package/dist/{chunk-F3FDVHR7.js → chunk-QQ3G5SXX.js} +1 -1
- package/dist/{chunk-VVFVENU4.js → chunk-RDT2IV2Z.js} +6 -6
- package/dist/{chunk-L6IL4N6Y.js → chunk-VLT2KGPE.js} +34 -34
- package/dist/{chunk-H6X52UVF.js → chunk-WEGZGF3K.js} +1 -1
- package/dist/{chunk-C4QNBSWB.js → chunk-XINFTNA2.js} +8 -8
- package/dist/{chunk-JCKOW45X.js → chunk-ZCT23NBS.js} +2 -2
- package/dist/daemon/server.js +1311 -364
- package/dist/hook-runtime/daemon/server.js +1311 -364
- package/dist/hook-runtime/hooks/post-commit.js +395 -242
- package/dist/hook-runtime/hooks/post-rewrite.js +391 -238
- package/dist/hook-runtime/hooks/post-tool-use.js +1442 -498
- package/dist/hook-runtime/hooks/pre-commit.js +1293 -349
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +1087 -143
- package/dist/hook-runtime/hooks/prim-hook.js +1457 -513
- package/dist/hook-runtime/hooks/session-end.js +369 -216
- package/dist/hook-runtime/hooks/session-start.js +1209 -219
- package/dist/hook-runtime/index.js +2406 -1036
- package/dist/hook-runtime/statusline-main.js +360 -207
- package/dist/hooks/post-commit.js +7 -7
- package/dist/hooks/post-rewrite.js +6 -6
- package/dist/hooks/post-tool-use.js +18 -18
- 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 +63 -20
- package/dist/index.js +387 -43
- package/dist/statusline-main.js +360 -207
- package/package.json +1 -1
|
@@ -2,28 +2,28 @@
|
|
|
2
2
|
import { createRequire as __primCreateRequire } from "node:module"; const require = __primCreateRequire(import.meta.url);
|
|
3
3
|
|
|
4
4
|
// src/daemon/principal.ts
|
|
5
|
-
import { createHash as
|
|
5
|
+
import { createHash as createHash7 } from "crypto";
|
|
6
6
|
|
|
7
7
|
// src/lib/credentials.ts
|
|
8
|
-
import { createHash as
|
|
9
|
-
import { readFileSync as
|
|
10
|
-
import { join as
|
|
8
|
+
import { createHash as createHash6 } from "crypto";
|
|
9
|
+
import { readFileSync as readFileSync7 } from "fs";
|
|
10
|
+
import { join as join9 } from "path";
|
|
11
11
|
|
|
12
12
|
// src/lib/machine-credentials.ts
|
|
13
|
-
import { execFileSync } from "child_process";
|
|
14
|
-
import { createHash as
|
|
13
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
14
|
+
import { createHash as createHash5, randomBytes as randomBytes3, randomUUID as randomUUID2 } from "crypto";
|
|
15
15
|
import {
|
|
16
16
|
chmodSync,
|
|
17
17
|
existsSync as existsSync3,
|
|
18
18
|
linkSync,
|
|
19
|
-
lstatSync as
|
|
19
|
+
lstatSync as lstatSync4,
|
|
20
20
|
mkdirSync as mkdirSync3,
|
|
21
|
-
readFileSync as
|
|
22
|
-
rmSync as
|
|
21
|
+
readFileSync as readFileSync6,
|
|
22
|
+
rmSync as rmSync6,
|
|
23
23
|
writeFileSync as writeFileSync3
|
|
24
24
|
} from "fs";
|
|
25
25
|
import { createRequire } from "module";
|
|
26
|
-
import { dirname as
|
|
26
|
+
import { dirname as dirname5, join as join8 } from "path";
|
|
27
27
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
28
28
|
|
|
29
29
|
// src/lib/atomic-file.ts
|
|
@@ -102,6 +102,8 @@ import { Readable } from "stream";
|
|
|
102
102
|
function isClaudeCloudSession(env = process.env) {
|
|
103
103
|
return env.CLAUDE_CODE_REMOTE === "true";
|
|
104
104
|
}
|
|
105
|
+
var CLOUD_REJECTED_CREDENTIALS_REMEDY = 'correct the credentials in the cloud environment\'s settings, or if the installation was revoked mint a replacement on your own machine with `prim auth enroll --new-installation "Claude Code cloud" --print-env`; then start a new cloud session';
|
|
106
|
+
var CLOUD_REJECTED_CREDENTIALS_NOTICE = `Primitive is not connected in this Claude Code cloud session: its credentials were rejected. To fix it, ${CLOUD_REJECTED_CREDENTIALS_REMEDY}.`;
|
|
105
107
|
|
|
106
108
|
// src/lib/workos-connect.ts
|
|
107
109
|
var DISCOVERY_MAX_BYTES = 16 * 1024;
|
|
@@ -289,24 +291,320 @@ import { lstatSync as lstatSync2, readFileSync as readFileSync4, readdirSync, rm
|
|
|
289
291
|
import { join as join5 } from "path";
|
|
290
292
|
var MAX_SERVER_WAIT_MS = 15 * 6e4;
|
|
291
293
|
|
|
294
|
+
// src/lib/session-identity.ts
|
|
295
|
+
import { execFile } from "child_process";
|
|
296
|
+
import { createHash as createHash4 } from "crypto";
|
|
297
|
+
import { rmSync as rmSync5 } from "fs";
|
|
298
|
+
import { join as join7 } from "path";
|
|
299
|
+
import { promisify } from "util";
|
|
300
|
+
|
|
301
|
+
// src/lib/git.ts
|
|
302
|
+
import { execFileSync } from "child_process";
|
|
303
|
+
import { createHash as createHash3 } from "crypto";
|
|
304
|
+
import { lstatSync as lstatSync3, readFileSync as readFileSync5, realpathSync } from "fs";
|
|
305
|
+
import { basename as basename2, dirname as dirname4, isAbsolute as isAbsolute3, join as join6, relative, resolve, sep } from "path";
|
|
306
|
+
var GIT_TIMEOUT_MS = 1e3;
|
|
307
|
+
var MAX_SETTINGS_BYTES = 1024 * 1024;
|
|
308
|
+
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
309
|
+
var GITHUB_REPOSITORY_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9._-]{1,100}$/u;
|
|
310
|
+
var CREDENTIAL_VARIABLE = /^(?:PRIM_TOKEN|PRIM_M2M_\w+)$/u;
|
|
311
|
+
function gitChildEnvironment(env = process.env) {
|
|
312
|
+
return Object.fromEntries(
|
|
313
|
+
Object.entries(env).filter(([name]) => !CREDENTIAL_VARIABLE.test(name))
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
function gitToplevel(cwd) {
|
|
317
|
+
try {
|
|
318
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
319
|
+
cwd,
|
|
320
|
+
encoding: "utf-8",
|
|
321
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
322
|
+
timeout: GIT_TIMEOUT_MS,
|
|
323
|
+
env: gitChildEnvironment()
|
|
324
|
+
}).trim();
|
|
325
|
+
} catch {
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
function gitValue(cwd, args) {
|
|
330
|
+
try {
|
|
331
|
+
const value = execFileSync("git", args, {
|
|
332
|
+
cwd,
|
|
333
|
+
encoding: "utf-8",
|
|
334
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
335
|
+
timeout: GIT_TIMEOUT_MS,
|
|
336
|
+
env: gitChildEnvironment()
|
|
337
|
+
}).trim();
|
|
338
|
+
return value || void 0;
|
|
339
|
+
} catch {
|
|
340
|
+
return void 0;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
function parseGithubRepositoryName(path) {
|
|
344
|
+
const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
|
|
345
|
+
if (parts.length !== 2 || !GITHUB_OWNER_PATTERN.test(parts[0] ?? "") || !GITHUB_REPOSITORY_PATTERN.test(parts[1] ?? "")) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
return `${parts[0]}/${parts[1]}`;
|
|
349
|
+
}
|
|
350
|
+
function githubRepositoryFromRemote(remote) {
|
|
351
|
+
if ([...remote].some((char) => char < " " || char === "\x7F")) return null;
|
|
352
|
+
const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
|
|
353
|
+
remote
|
|
354
|
+
);
|
|
355
|
+
const path = match?.[1] ?? match?.[2];
|
|
356
|
+
return path ? parseGithubRepositoryName(path) : null;
|
|
357
|
+
}
|
|
358
|
+
function checkoutSettingsText(repoRoot) {
|
|
359
|
+
const path = join6(repoRoot, ".claude", "settings.json");
|
|
360
|
+
try {
|
|
361
|
+
if (!lstatSync3(join6(repoRoot, ".claude")).isDirectory()) return null;
|
|
362
|
+
const stat = lstatSync3(path);
|
|
363
|
+
if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
|
|
364
|
+
return readFileSync5(path, "utf8");
|
|
365
|
+
} catch {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function checkoutDeclaration(repoRoot, name) {
|
|
370
|
+
const text = checkoutSettingsText(repoRoot);
|
|
371
|
+
if (text === null) return null;
|
|
372
|
+
let parsed;
|
|
373
|
+
try {
|
|
374
|
+
parsed = JSON.parse(text);
|
|
375
|
+
} catch {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
const env = typeof parsed === "object" && parsed !== null ? parsed.env : void 0;
|
|
379
|
+
const value = typeof env === "object" && env !== null ? env[name] : void 0;
|
|
380
|
+
return typeof value === "string" ? value : null;
|
|
381
|
+
}
|
|
382
|
+
function declaredGithubRepository(repoRoot) {
|
|
383
|
+
const value = checkoutDeclaration(repoRoot, "PRIM_REPOSITORY");
|
|
384
|
+
return value === null ? null : parseGithubRepositoryName(value.trim());
|
|
385
|
+
}
|
|
386
|
+
function githubRepositoryFullName(cwd) {
|
|
387
|
+
const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
|
|
388
|
+
if (remote) {
|
|
389
|
+
const fromOrigin = githubRepositoryFromRemote(remote);
|
|
390
|
+
if (fromOrigin || normalizeOriginRemote(remote)) return fromOrigin;
|
|
391
|
+
}
|
|
392
|
+
const root = gitToplevel(cwd);
|
|
393
|
+
return root ? declaredGithubRepository(root) : null;
|
|
394
|
+
}
|
|
395
|
+
function normalizeOriginRemote(remote) {
|
|
396
|
+
const value = remote.trim();
|
|
397
|
+
if (!value) return void 0;
|
|
398
|
+
const scp = !value.includes("://") ? /^(?:[^@/\s]+@)?(?<host>[^:/\s]+):(?<path>.+)$/.exec(value)?.groups : void 0;
|
|
399
|
+
if (scp?.host && scp.path) {
|
|
400
|
+
const path = scp.path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
401
|
+
return path ? `${scp.host.toLowerCase()}/${path}` : void 0;
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
const url = new URL(value);
|
|
405
|
+
url.username = "";
|
|
406
|
+
url.password = "";
|
|
407
|
+
url.search = "";
|
|
408
|
+
url.hash = "";
|
|
409
|
+
const path = url.pathname.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
410
|
+
if (url.host && path) return `${url.host.toLowerCase()}/${path}`;
|
|
411
|
+
return void 0;
|
|
412
|
+
} catch {
|
|
413
|
+
return void 0;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
function opaqueRepoKey(source, value) {
|
|
417
|
+
return `repo_v1_${createHash3("sha256").update(`${source}\0${value}`).digest("hex")}`;
|
|
418
|
+
}
|
|
419
|
+
function resolveRepositoryContext(cwd = process.cwd()) {
|
|
420
|
+
const top = gitToplevel(cwd);
|
|
421
|
+
if (!top) return null;
|
|
422
|
+
let repoRoot;
|
|
423
|
+
try {
|
|
424
|
+
repoRoot = realpathSync.native(top);
|
|
425
|
+
} catch {
|
|
426
|
+
repoRoot = resolve(top);
|
|
427
|
+
}
|
|
428
|
+
const origin = gitValue(repoRoot, ["config", "--get", "remote.origin.url"]);
|
|
429
|
+
const repoFullName = githubRepositoryFullName(repoRoot) ?? void 0;
|
|
430
|
+
const normalizedOrigin = origin ? normalizeOriginRemote(origin) : void 0;
|
|
431
|
+
if (normalizedOrigin) {
|
|
432
|
+
return {
|
|
433
|
+
repoRoot,
|
|
434
|
+
repoKey: opaqueRepoKey("origin", normalizedOrigin),
|
|
435
|
+
identitySource: "origin",
|
|
436
|
+
repoFullName
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
if (repoFullName) {
|
|
440
|
+
return {
|
|
441
|
+
repoRoot,
|
|
442
|
+
repoKey: opaqueRepoKey("origin", `github.com/${repoFullName}`),
|
|
443
|
+
identitySource: "declared",
|
|
444
|
+
repoFullName
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
const roots = gitValue(repoRoot, ["rev-list", "--max-parents=0", "HEAD"])?.split(/\s+/).filter(Boolean).sort();
|
|
448
|
+
if (roots && roots.length > 0) {
|
|
449
|
+
return {
|
|
450
|
+
repoRoot,
|
|
451
|
+
repoKey: opaqueRepoKey("root_commit", roots.join("\n")),
|
|
452
|
+
identitySource: "root_commit",
|
|
453
|
+
repoFullName
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
return { repoRoot, repoFullName };
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/lib/session-identity.ts
|
|
460
|
+
var SESSION_IDENTITY_VARIABLE = "PRIM_CLOUD_IDENTITY";
|
|
461
|
+
var SESSION_IDENTITY_VALUE = "session";
|
|
462
|
+
var SESSION_ID_VARIABLE = "CLAUDE_CODE_REMOTE_SESSION_ID";
|
|
463
|
+
var SESSION_TOKEN_PREFIX = "prim_sess_";
|
|
464
|
+
var SESSION_ID = /^[A-Za-z0-9._:-]{1,200}$/u;
|
|
465
|
+
var DEFAULT_API_URL = "https://api.getprimitive.ai";
|
|
466
|
+
var RENEW_BEFORE_MS = 6e4;
|
|
467
|
+
var BACKGROUND_RETRY_MS = 5 * 6e4;
|
|
468
|
+
var RESPONSE_MAX_BYTES = 64 * 1024;
|
|
469
|
+
function sessionIdentityRequested(env = process.env) {
|
|
470
|
+
return isClaudeCloudSession(env) && env[SESSION_IDENTITY_VARIABLE]?.trim() === SESSION_IDENTITY_VALUE;
|
|
471
|
+
}
|
|
472
|
+
function sessionIdentityMode(deps = {}) {
|
|
473
|
+
const env = deps.env ?? process.env;
|
|
474
|
+
return sessionIdentityRequested(env) && (env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0);
|
|
475
|
+
}
|
|
476
|
+
function failure(code, cause, remedy) {
|
|
477
|
+
return { status: "failed", code, cause, remedy };
|
|
478
|
+
}
|
|
479
|
+
var NEW_SESSION_REMEDY = "start a new cloud session";
|
|
480
|
+
var SESSION_REVOKED = failure(
|
|
481
|
+
"session_revoked",
|
|
482
|
+
"Primitive no longer accepts this session's member token: an organization admin revoked the session or the member's GitHub link, the member left the organization, or the token was renewed elsewhere",
|
|
483
|
+
`${NEW_SESSION_REMEDY}, which identifies you again through GitHub (if your GitHub link was removed, run \`prim auth link\` there when it asks)`
|
|
484
|
+
);
|
|
485
|
+
var CHALLENGE_WINDOW_PASSED = failure(
|
|
486
|
+
"challenge_expired",
|
|
487
|
+
"this session's identity check expired before it was exchanged for a token (a verified check stays usable, including for linking a GitHub account, for 30 minutes)",
|
|
488
|
+
`${NEW_SESSION_REMEDY}, and if it asks you to link your GitHub account, run \`prim auth link\` there right away`
|
|
489
|
+
);
|
|
490
|
+
var CHALLENGE_GONE = failure(
|
|
491
|
+
"challenge_not_found",
|
|
492
|
+
"Primitive no longer knows this session's identity check (the environment credential may have been replaced since)",
|
|
493
|
+
NEW_SESSION_REMEDY
|
|
494
|
+
);
|
|
495
|
+
function hash(value) {
|
|
496
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
497
|
+
}
|
|
498
|
+
function sessionContext(deps) {
|
|
499
|
+
const env = deps.env ?? process.env;
|
|
500
|
+
const environment = environmentCredentials(deps);
|
|
501
|
+
if (!environment)
|
|
502
|
+
return failure(
|
|
503
|
+
"environment_credential_missing",
|
|
504
|
+
"the cloud environment has no environment credential",
|
|
505
|
+
'ask an organization admin to create one with `prim auth enroll --new-environment "<name>" --print-env` and add it to the environment, then start a new cloud session'
|
|
506
|
+
);
|
|
507
|
+
const sessionId = env[SESSION_ID_VARIABLE]?.trim();
|
|
508
|
+
if (!sessionId || !SESSION_ID.test(sessionId))
|
|
509
|
+
return failure(
|
|
510
|
+
"session_id_missing",
|
|
511
|
+
`this Claude Code cloud session does not report a session ID Primitive accepts (${SESSION_ID_VARIABLE})`,
|
|
512
|
+
NEW_SESSION_REMEDY
|
|
513
|
+
);
|
|
514
|
+
const apiUrl = machineApiUrl(env.PRIM_API_URL?.trim() || DEFAULT_API_URL);
|
|
515
|
+
const directory = deps.directory ?? primConfigDirectory({ env });
|
|
516
|
+
const key = hash(`${apiUrl}\0${environment.clientId}\0${sessionId}`);
|
|
517
|
+
return {
|
|
518
|
+
directory,
|
|
519
|
+
apiUrl,
|
|
520
|
+
clientId: environment.clientId,
|
|
521
|
+
generation: environment.generation,
|
|
522
|
+
sessionId,
|
|
523
|
+
tokenPath: join7(directory, `session_identity_${key}.json`),
|
|
524
|
+
statePath: join7(directory, `session_identity_${key}.state.json`),
|
|
525
|
+
lockPath: join7(directory, "session-identity.lock")
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function readRecord(path) {
|
|
529
|
+
try {
|
|
530
|
+
const value = readMachineMetadata(path);
|
|
531
|
+
return value === void 0 ? void 0 : machineRecord(value);
|
|
532
|
+
} catch {
|
|
533
|
+
return void 0;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function finiteNumber(value) {
|
|
537
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
538
|
+
}
|
|
539
|
+
function readToken(context) {
|
|
540
|
+
const data = readRecord(context.tokenPath);
|
|
541
|
+
if (!data || data.version !== 1 || data.apiUrl !== context.apiUrl || data.clientId !== context.clientId || data.generation !== context.generation || data.sessionId !== context.sessionId || !machineString(data.accessToken, 8192) || !data.accessToken.startsWith(SESSION_TOKEN_PREFIX) || !finiteNumber(data.expiresAt) || !finiteNumber(data.refreshAfter) || !machineString(data.userId, 256) || !machineString(data.displayName, 256) || !machineString(data.organizationId, 256) || !machineString(data.issuer, 2048) || !machineString(data.audience, 256))
|
|
542
|
+
return void 0;
|
|
543
|
+
const identity = data.identity;
|
|
544
|
+
if (!identity || identity.clientId !== context.clientId || identity.authorizedUserId !== data.userId || identity.organizationId !== data.organizationId)
|
|
545
|
+
return void 0;
|
|
546
|
+
return data;
|
|
547
|
+
}
|
|
548
|
+
function tokenValid(record, now = Date.now()) {
|
|
549
|
+
return now < record.expiresAt - RENEW_BEFORE_MS;
|
|
550
|
+
}
|
|
551
|
+
function toCredential(record) {
|
|
552
|
+
return {
|
|
553
|
+
issuer: record.issuer,
|
|
554
|
+
audience: record.audience,
|
|
555
|
+
...record.identity,
|
|
556
|
+
accessToken: record.accessToken,
|
|
557
|
+
expiresAt: record.expiresAt,
|
|
558
|
+
apiUrl: record.apiUrl,
|
|
559
|
+
generation: record.generation,
|
|
560
|
+
storage: "environment",
|
|
561
|
+
session: {
|
|
562
|
+
userId: record.userId,
|
|
563
|
+
displayName: record.displayName,
|
|
564
|
+
...record.githubLogin ? { githubLogin: record.githubLogin } : {},
|
|
565
|
+
refreshAfter: record.refreshAfter
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
function cachedSessionCredential(deps = {}) {
|
|
570
|
+
if (!sessionIdentityMode(deps)) return void 0;
|
|
571
|
+
let context;
|
|
572
|
+
try {
|
|
573
|
+
context = sessionContext(deps);
|
|
574
|
+
} catch {
|
|
575
|
+
return void 0;
|
|
576
|
+
}
|
|
577
|
+
if ("status" in context) return void 0;
|
|
578
|
+
const record = readToken(context);
|
|
579
|
+
return record && tokenValid(record) ? toCredential(record) : void 0;
|
|
580
|
+
}
|
|
581
|
+
var SERVER_CONTRACT_FAILURE = failure(
|
|
582
|
+
"server_contract",
|
|
583
|
+
"Primitive's answer did not identify the linked member",
|
|
584
|
+
"update prim (`npx --yes @primitive.ai/prim@latest`) and run `prim setup --cloud`; if it persists, report it to Primitive"
|
|
585
|
+
);
|
|
586
|
+
|
|
292
587
|
// src/lib/machine-credentials.ts
|
|
588
|
+
function sessionScoped(deps) {
|
|
589
|
+
return !deps.localEnvironment && !deps.environmentOnly && sessionIdentityMode(deps);
|
|
590
|
+
}
|
|
293
591
|
var require2 = createRequire(import.meta.url);
|
|
294
|
-
var
|
|
592
|
+
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
295
593
|
var PROFILE_FILE = "machine_profile.json";
|
|
296
594
|
var cached;
|
|
297
595
|
function configurationDirectory(deps) {
|
|
298
596
|
return deps.directory ?? primConfigDirectory({ env: deps.env });
|
|
299
597
|
}
|
|
300
598
|
function apiUrlFor(deps) {
|
|
301
|
-
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() ||
|
|
599
|
+
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL2);
|
|
302
600
|
}
|
|
303
|
-
function
|
|
304
|
-
return
|
|
601
|
+
function hash2(value) {
|
|
602
|
+
return createHash5("sha256").update(value).digest("hex");
|
|
305
603
|
}
|
|
306
604
|
function environmentContextPath(deps, clientId) {
|
|
307
|
-
return
|
|
605
|
+
return join8(
|
|
308
606
|
configurationDirectory(deps),
|
|
309
|
-
`machine_context_${
|
|
607
|
+
`machine_context_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
310
608
|
);
|
|
311
609
|
}
|
|
312
610
|
function readEnvironmentContext(environment, deps) {
|
|
@@ -337,7 +635,7 @@ function protectPath(path, directory) {
|
|
|
337
635
|
}
|
|
338
636
|
const encodedPath = Buffer.from(path).toString("base64");
|
|
339
637
|
const script = `$ErrorActionPreference = 'Stop'; $p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${encodedPath}')); $sid = [Security.Principal.WindowsIdentity]::GetCurrent().User; $acl = [System.Security.AccessControl.${directory ? "DirectorySecurity" : "FileSecurity"}]::new(); $acl.SetOwner($sid); $acl.SetAccessRuleProtection($true, $false); $rule = [System.Security.AccessControl.FileSystemAccessRule]::new($sid, 'FullControl', '${directory ? "ContainerInherit, ObjectInherit" : "None"}', 'None', 'Allow'); $acl.AddAccessRule($rule); [System.IO.${directory ? "Directory" : "File"}]::SetAccessControl($p, $acl)`;
|
|
340
|
-
|
|
638
|
+
execFileSync2(
|
|
341
639
|
"powershell.exe",
|
|
342
640
|
[
|
|
343
641
|
"-NoProfile",
|
|
@@ -350,24 +648,27 @@ function protectPath(path, directory) {
|
|
|
350
648
|
}
|
|
351
649
|
function ensureDirectory(directory) {
|
|
352
650
|
mkdirSync3(directory, { recursive: true, mode: 448 });
|
|
353
|
-
if (
|
|
651
|
+
if (lstatSync4(directory).isSymbolicLink())
|
|
354
652
|
throw new MachineAuthError(
|
|
355
653
|
"unsafe_storage",
|
|
356
654
|
"Credential directory must not be a symbolic link."
|
|
357
655
|
);
|
|
358
656
|
protectPath(directory, true);
|
|
359
657
|
}
|
|
658
|
+
function readMachineMetadata(path) {
|
|
659
|
+
return readPrivate(path);
|
|
660
|
+
}
|
|
360
661
|
function readPrivate(path) {
|
|
361
662
|
let raw;
|
|
362
663
|
try {
|
|
363
|
-
const stat =
|
|
664
|
+
const stat = lstatSync4(path);
|
|
364
665
|
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
365
666
|
throw new MachineAuthError(
|
|
366
667
|
"unsafe_storage",
|
|
367
668
|
"Machine credential files must be owner-only regular files."
|
|
368
669
|
);
|
|
369
670
|
}
|
|
370
|
-
raw =
|
|
671
|
+
raw = readFileSync6(path, "utf8");
|
|
371
672
|
} catch (error) {
|
|
372
673
|
if (error.code === "ENOENT") return void 0;
|
|
373
674
|
throw error;
|
|
@@ -412,7 +713,7 @@ function parseProfile(value) {
|
|
|
412
713
|
};
|
|
413
714
|
}
|
|
414
715
|
function readMachineProfile(deps = {}) {
|
|
415
|
-
const value = readPrivate(
|
|
716
|
+
const value = readPrivate(join8(configurationDirectory(deps), PROFILE_FILE));
|
|
416
717
|
return value === void 0 ? void 0 : parseProfile(value);
|
|
417
718
|
}
|
|
418
719
|
var NETWORK_SECRET_AUTH = "network-secret";
|
|
@@ -421,20 +722,20 @@ var INJECTED_IDENTITY_TTL_MS = 60 * 6e4;
|
|
|
421
722
|
var brokerKeys = /* @__PURE__ */ new Map();
|
|
422
723
|
function networkSecretBrokerKey(deps, clientId) {
|
|
423
724
|
const directory = configurationDirectory(deps);
|
|
424
|
-
const path =
|
|
725
|
+
const path = join8(directory, `machine_network_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.key`);
|
|
425
726
|
const known = brokerKeys.get(path);
|
|
426
727
|
if (known) return known;
|
|
427
728
|
ensureDirectory(directory);
|
|
428
729
|
const read = () => {
|
|
429
730
|
try {
|
|
430
|
-
const stat =
|
|
731
|
+
const stat = lstatSync4(path);
|
|
431
732
|
if (!stat.isFile() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()))
|
|
432
733
|
throw new MachineAuthError(
|
|
433
734
|
"unsafe_storage",
|
|
434
735
|
`The machine broker key at ${path} is not an owner-only file. Delete it and retry.`,
|
|
435
736
|
true
|
|
436
737
|
);
|
|
437
|
-
const value =
|
|
738
|
+
const value = readFileSync6(path, "utf8").trim();
|
|
438
739
|
return { key: /^[a-f0-9]{64}$/u.test(value) ? value : void 0 };
|
|
439
740
|
} catch (error) {
|
|
440
741
|
if (error.code === "ENOENT") return void 0;
|
|
@@ -462,7 +763,7 @@ function networkSecretBrokerKey(deps, clientId) {
|
|
|
462
763
|
} catch (error) {
|
|
463
764
|
if (error.code !== "EEXIST") throw error;
|
|
464
765
|
} finally {
|
|
465
|
-
|
|
766
|
+
rmSync6(staged, { force: true });
|
|
466
767
|
}
|
|
467
768
|
}
|
|
468
769
|
const key = read()?.key;
|
|
@@ -495,7 +796,7 @@ function environmentCredentials(deps) {
|
|
|
495
796
|
"conflicting_credentials",
|
|
496
797
|
`Unset PRIM_M2M_CLIENT_SECRET: with PRIM_M2M_CLIENT_AUTH=${auth} the environment's network secret supplies it.`
|
|
497
798
|
);
|
|
498
|
-
const generation =
|
|
799
|
+
const generation = hash2(`${apiUrlFor(deps)}\0${clientId}\0${auth}\0broker-proxy`);
|
|
499
800
|
const credentials = {
|
|
500
801
|
clientId,
|
|
501
802
|
generation,
|
|
@@ -516,12 +817,12 @@ function environmentCredentials(deps) {
|
|
|
516
817
|
return {
|
|
517
818
|
clientId,
|
|
518
819
|
clientSecret,
|
|
519
|
-
generation:
|
|
820
|
+
generation: hash2(`${apiUrlFor(deps)}\0${clientId}\0${clientSecret}`)
|
|
520
821
|
};
|
|
521
822
|
}
|
|
522
823
|
function hasMachineConfiguration(deps = {}) {
|
|
523
824
|
const env = deps.env ?? process.env;
|
|
524
|
-
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 || existsSync3(
|
|
825
|
+
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0 || existsSync3(join8(configurationDirectory(deps), PROFILE_FILE));
|
|
525
826
|
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
526
827
|
throw new MachineAuthError(
|
|
527
828
|
"conflicting_credentials",
|
|
@@ -533,6 +834,7 @@ function hasMachineConfiguration(deps = {}) {
|
|
|
533
834
|
}
|
|
534
835
|
function machineContext(deps = {}) {
|
|
535
836
|
if (!hasMachineConfiguration(deps)) return void 0;
|
|
837
|
+
if (sessionScoped(deps)) return cachedSessionCredential(deps);
|
|
536
838
|
const environment = environmentCredentials(deps);
|
|
537
839
|
if (environment) {
|
|
538
840
|
if (environmentTerminal === environment.generation) return void 0;
|
|
@@ -547,6 +849,7 @@ function machineContext(deps = {}) {
|
|
|
547
849
|
return profile;
|
|
548
850
|
}
|
|
549
851
|
function cachedMachineCredential(deps = {}) {
|
|
852
|
+
if (sessionScoped(deps)) return cachedSessionCredential(deps);
|
|
550
853
|
const context = machineContext(deps);
|
|
551
854
|
return context && cached?.generation === context.generation && cached.apiUrl === context.apiUrl ? cached : void 0;
|
|
552
855
|
}
|
|
@@ -554,20 +857,20 @@ var environmentTerminal;
|
|
|
554
857
|
|
|
555
858
|
// src/lib/credentials.ts
|
|
556
859
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
557
|
-
var TOKEN_FILE_PATH =
|
|
558
|
-
var REFRESH_TOKEN_PATH =
|
|
559
|
-
var TOKEN_EXPIRES_PATH =
|
|
560
|
-
var TERMINAL_REFRESH_PATH =
|
|
561
|
-
var CREDENTIAL_METADATA_PATH =
|
|
562
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
563
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
564
|
-
var CREDENTIAL_LOCK_PATH =
|
|
860
|
+
var TOKEN_FILE_PATH = join9(CONFIG_DIRECTORY, "token");
|
|
861
|
+
var REFRESH_TOKEN_PATH = join9(CONFIG_DIRECTORY, "refresh_token");
|
|
862
|
+
var TOKEN_EXPIRES_PATH = join9(CONFIG_DIRECTORY, "token_expires_at");
|
|
863
|
+
var TERMINAL_REFRESH_PATH = join9(CONFIG_DIRECTORY, "refresh_terminal");
|
|
864
|
+
var CREDENTIAL_METADATA_PATH = join9(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
865
|
+
var CREDENTIAL_FAMILY_PATH = join9(CONFIG_DIRECTORY, "credential_family.json");
|
|
866
|
+
var CREDENTIAL_MIGRATION_PATH = join9(CONFIG_DIRECTORY, "credential_migration.json");
|
|
867
|
+
var CREDENTIAL_LOCK_PATH = join9(CONFIG_DIRECTORY, "credentials.lock");
|
|
565
868
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
566
869
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
567
870
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
568
871
|
function readTrimmed(path) {
|
|
569
872
|
try {
|
|
570
|
-
const value =
|
|
873
|
+
const value = readFileSync7(path, "utf8").trim();
|
|
571
874
|
return value || void 0;
|
|
572
875
|
} catch {
|
|
573
876
|
return void 0;
|
|
@@ -604,7 +907,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
604
907
|
return { token: stored, source: "token_file" };
|
|
605
908
|
}
|
|
606
909
|
function credentialFingerprint(value) {
|
|
607
|
-
return
|
|
910
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
608
911
|
}
|
|
609
912
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
610
913
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -624,7 +927,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
624
927
|
function readBoundedCredentialJson(path) {
|
|
625
928
|
let raw;
|
|
626
929
|
try {
|
|
627
|
-
raw =
|
|
930
|
+
raw = readFileSync7(path, "utf8");
|
|
628
931
|
} catch (error) {
|
|
629
932
|
return error.code === "ENOENT" ? void 0 : null;
|
|
630
933
|
}
|
|
@@ -697,7 +1000,7 @@ function boundedClaim(value) {
|
|
|
697
1000
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
698
1001
|
}
|
|
699
1002
|
function sha256(value) {
|
|
700
|
-
return
|
|
1003
|
+
return createHash7("sha256").update(value).digest("hex");
|
|
701
1004
|
}
|
|
702
1005
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
703
1006
|
if (hasMachineConfiguration()) {
|
|
@@ -748,8 +1051,8 @@ function parseDaemonResponseEnvelope(value) {
|
|
|
748
1051
|
|
|
749
1052
|
// src/daemon/client.ts
|
|
750
1053
|
import { createConnection } from "net";
|
|
751
|
-
import { join as
|
|
752
|
-
var SOCK_PATH =
|
|
1054
|
+
import { join as join10 } from "path";
|
|
1055
|
+
var SOCK_PATH = join10(primConfigDirectory(), "sock");
|
|
753
1056
|
var DEFAULT_TIMEOUT_MS = 250;
|
|
754
1057
|
var nextRequestId = 1;
|
|
755
1058
|
function daemonRequest(method, params = {}, opts = {}) {
|
|
@@ -842,14 +1145,14 @@ function daemonRequest(method, params = {}, opts = {}) {
|
|
|
842
1145
|
import { execFileSync as execFileSync4 } from "child_process";
|
|
843
1146
|
import { existsSync as existsSync5, readFileSync as readFileSync9 } from "fs";
|
|
844
1147
|
import { homedir as homedir2 } from "os";
|
|
845
|
-
import { join as
|
|
1148
|
+
import { join as join12 } from "path";
|
|
846
1149
|
|
|
847
1150
|
// src/lib/cloud-prepared.ts
|
|
848
|
-
import { existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as
|
|
849
|
-
import { join as
|
|
1151
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as readFileSync8, rmSync as rmSync7, writeFileSync as writeFileSync4 } from "fs";
|
|
1152
|
+
import { join as join11 } from "path";
|
|
850
1153
|
var CLOUD_PREPARED_MARKER = "cloud-prepared";
|
|
851
1154
|
function cloudPreparedPath(env = process.env) {
|
|
852
|
-
return
|
|
1155
|
+
return join11(primConfigDirectory({ env }), CLOUD_PREPARED_MARKER);
|
|
853
1156
|
}
|
|
854
1157
|
function isPreparedCloudSession(env = process.env) {
|
|
855
1158
|
if (!isClaudeCloudSession(env)) return false;
|
|
@@ -861,17 +1164,17 @@ function isPreparedCloudSession(env = process.env) {
|
|
|
861
1164
|
}
|
|
862
1165
|
|
|
863
1166
|
// src/lib/demo-pause.ts
|
|
864
|
-
import { execFileSync as
|
|
1167
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
865
1168
|
var DEMO_PAUSE_MS = 30 * 6e4;
|
|
866
1169
|
var PAUSE_KEY = "prim.demoPause";
|
|
867
|
-
var
|
|
1170
|
+
var GIT_TIMEOUT_MS2 = 1e3;
|
|
868
1171
|
function git(cwd, args) {
|
|
869
1172
|
try {
|
|
870
|
-
return
|
|
1173
|
+
return execFileSync3("git", args, {
|
|
871
1174
|
cwd,
|
|
872
1175
|
encoding: "utf-8",
|
|
873
1176
|
stdio: ["ignore", "pipe", "ignore"],
|
|
874
|
-
timeout:
|
|
1177
|
+
timeout: GIT_TIMEOUT_MS2
|
|
875
1178
|
});
|
|
876
1179
|
} catch {
|
|
877
1180
|
return void 0;
|
|
@@ -879,10 +1182,10 @@ function git(cwd, args) {
|
|
|
879
1182
|
}
|
|
880
1183
|
function setLocal(cwd, key, value) {
|
|
881
1184
|
try {
|
|
882
|
-
|
|
1185
|
+
execFileSync3(
|
|
883
1186
|
"git",
|
|
884
1187
|
value === null ? ["config", "--local", "--unset", key] : ["config", "--local", key, value],
|
|
885
|
-
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout:
|
|
1188
|
+
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout: GIT_TIMEOUT_MS2 }
|
|
886
1189
|
);
|
|
887
1190
|
} catch (error) {
|
|
888
1191
|
if (value !== null || error.status !== 5) throw error;
|
|
@@ -921,156 +1224,6 @@ function healDemoPause(cwd, pause, options = {}) {
|
|
|
921
1224
|
}
|
|
922
1225
|
}
|
|
923
1226
|
|
|
924
|
-
// src/lib/git.ts
|
|
925
|
-
import { execFileSync as execFileSync3 } from "child_process";
|
|
926
|
-
import { createHash as createHash6 } from "crypto";
|
|
927
|
-
import { lstatSync as lstatSync4, readFileSync as readFileSync8, realpathSync } from "fs";
|
|
928
|
-
import { basename as basename2, dirname as dirname5, isAbsolute as isAbsolute3, join as join10, relative, resolve, sep } from "path";
|
|
929
|
-
var GIT_TIMEOUT_MS2 = 1e3;
|
|
930
|
-
var MAX_SETTINGS_BYTES = 1024 * 1024;
|
|
931
|
-
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
932
|
-
var GITHUB_REPOSITORY_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9._-]{1,100}$/u;
|
|
933
|
-
function gitToplevel(cwd) {
|
|
934
|
-
try {
|
|
935
|
-
return execFileSync3("git", ["rev-parse", "--show-toplevel"], {
|
|
936
|
-
cwd,
|
|
937
|
-
encoding: "utf-8",
|
|
938
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
939
|
-
timeout: GIT_TIMEOUT_MS2
|
|
940
|
-
}).trim();
|
|
941
|
-
} catch {
|
|
942
|
-
return null;
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
function gitValue(cwd, args) {
|
|
946
|
-
try {
|
|
947
|
-
const value = execFileSync3("git", args, {
|
|
948
|
-
cwd,
|
|
949
|
-
encoding: "utf-8",
|
|
950
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
951
|
-
timeout: GIT_TIMEOUT_MS2
|
|
952
|
-
}).trim();
|
|
953
|
-
return value || void 0;
|
|
954
|
-
} catch {
|
|
955
|
-
return void 0;
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
function parseGithubRepositoryName(path) {
|
|
959
|
-
const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
|
|
960
|
-
if (parts.length !== 2 || !GITHUB_OWNER_PATTERN.test(parts[0] ?? "") || !GITHUB_REPOSITORY_PATTERN.test(parts[1] ?? "")) {
|
|
961
|
-
return null;
|
|
962
|
-
}
|
|
963
|
-
return `${parts[0]}/${parts[1]}`;
|
|
964
|
-
}
|
|
965
|
-
function githubRepositoryFromRemote(remote) {
|
|
966
|
-
if ([...remote].some((char) => char < " " || char === "\x7F")) return null;
|
|
967
|
-
const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
|
|
968
|
-
remote
|
|
969
|
-
);
|
|
970
|
-
const path = match?.[1] ?? match?.[2];
|
|
971
|
-
return path ? parseGithubRepositoryName(path) : null;
|
|
972
|
-
}
|
|
973
|
-
function checkoutSettingsText(repoRoot) {
|
|
974
|
-
const path = join10(repoRoot, ".claude", "settings.json");
|
|
975
|
-
try {
|
|
976
|
-
if (!lstatSync4(join10(repoRoot, ".claude")).isDirectory()) return null;
|
|
977
|
-
const stat = lstatSync4(path);
|
|
978
|
-
if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
|
|
979
|
-
return readFileSync8(path, "utf8");
|
|
980
|
-
} catch {
|
|
981
|
-
return null;
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
function checkoutDeclaration(repoRoot, name) {
|
|
985
|
-
const text = checkoutSettingsText(repoRoot);
|
|
986
|
-
if (text === null) return null;
|
|
987
|
-
let parsed;
|
|
988
|
-
try {
|
|
989
|
-
parsed = JSON.parse(text);
|
|
990
|
-
} catch {
|
|
991
|
-
return null;
|
|
992
|
-
}
|
|
993
|
-
const env = typeof parsed === "object" && parsed !== null ? parsed.env : void 0;
|
|
994
|
-
const value = typeof env === "object" && env !== null ? env[name] : void 0;
|
|
995
|
-
return typeof value === "string" ? value : null;
|
|
996
|
-
}
|
|
997
|
-
function declaredGithubRepository(repoRoot) {
|
|
998
|
-
const value = checkoutDeclaration(repoRoot, "PRIM_REPOSITORY");
|
|
999
|
-
return value === null ? null : parseGithubRepositoryName(value.trim());
|
|
1000
|
-
}
|
|
1001
|
-
function githubRepositoryFullName(cwd) {
|
|
1002
|
-
const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
|
|
1003
|
-
if (remote) {
|
|
1004
|
-
const fromOrigin = githubRepositoryFromRemote(remote);
|
|
1005
|
-
if (fromOrigin || normalizeOriginRemote(remote)) return fromOrigin;
|
|
1006
|
-
}
|
|
1007
|
-
const root = gitToplevel(cwd);
|
|
1008
|
-
return root ? declaredGithubRepository(root) : null;
|
|
1009
|
-
}
|
|
1010
|
-
function normalizeOriginRemote(remote) {
|
|
1011
|
-
const value = remote.trim();
|
|
1012
|
-
if (!value) return void 0;
|
|
1013
|
-
const scp = !value.includes("://") ? /^(?:[^@/\s]+@)?(?<host>[^:/\s]+):(?<path>.+)$/.exec(value)?.groups : void 0;
|
|
1014
|
-
if (scp?.host && scp.path) {
|
|
1015
|
-
const path = scp.path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
1016
|
-
return path ? `${scp.host.toLowerCase()}/${path}` : void 0;
|
|
1017
|
-
}
|
|
1018
|
-
try {
|
|
1019
|
-
const url = new URL(value);
|
|
1020
|
-
url.username = "";
|
|
1021
|
-
url.password = "";
|
|
1022
|
-
url.search = "";
|
|
1023
|
-
url.hash = "";
|
|
1024
|
-
const path = url.pathname.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
1025
|
-
if (url.host && path) return `${url.host.toLowerCase()}/${path}`;
|
|
1026
|
-
return void 0;
|
|
1027
|
-
} catch {
|
|
1028
|
-
return void 0;
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
function opaqueRepoKey(source, value) {
|
|
1032
|
-
return `repo_v1_${createHash6("sha256").update(`${source}\0${value}`).digest("hex")}`;
|
|
1033
|
-
}
|
|
1034
|
-
function resolveRepositoryContext(cwd = process.cwd()) {
|
|
1035
|
-
const top = gitToplevel(cwd);
|
|
1036
|
-
if (!top) return null;
|
|
1037
|
-
let repoRoot;
|
|
1038
|
-
try {
|
|
1039
|
-
repoRoot = realpathSync.native(top);
|
|
1040
|
-
} catch {
|
|
1041
|
-
repoRoot = resolve(top);
|
|
1042
|
-
}
|
|
1043
|
-
const origin = gitValue(repoRoot, ["config", "--get", "remote.origin.url"]);
|
|
1044
|
-
const repoFullName = githubRepositoryFullName(repoRoot) ?? void 0;
|
|
1045
|
-
const normalizedOrigin = origin ? normalizeOriginRemote(origin) : void 0;
|
|
1046
|
-
if (normalizedOrigin) {
|
|
1047
|
-
return {
|
|
1048
|
-
repoRoot,
|
|
1049
|
-
repoKey: opaqueRepoKey("origin", normalizedOrigin),
|
|
1050
|
-
identitySource: "origin",
|
|
1051
|
-
repoFullName
|
|
1052
|
-
};
|
|
1053
|
-
}
|
|
1054
|
-
if (repoFullName) {
|
|
1055
|
-
return {
|
|
1056
|
-
repoRoot,
|
|
1057
|
-
repoKey: opaqueRepoKey("origin", `github.com/${repoFullName}`),
|
|
1058
|
-
identitySource: "declared",
|
|
1059
|
-
repoFullName
|
|
1060
|
-
};
|
|
1061
|
-
}
|
|
1062
|
-
const roots = gitValue(repoRoot, ["rev-list", "--max-parents=0", "HEAD"])?.split(/\s+/).filter(Boolean).sort();
|
|
1063
|
-
if (roots && roots.length > 0) {
|
|
1064
|
-
return {
|
|
1065
|
-
repoRoot,
|
|
1066
|
-
repoKey: opaqueRepoKey("root_commit", roots.join("\n")),
|
|
1067
|
-
identitySource: "root_commit",
|
|
1068
|
-
repoFullName
|
|
1069
|
-
};
|
|
1070
|
-
}
|
|
1071
|
-
return { repoRoot, repoFullName };
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
1227
|
// src/lib/activation.ts
|
|
1075
1228
|
var PRIM_ACTIVE_KEY = "prim.active";
|
|
1076
1229
|
var PRIM_ACTIVE_DECLARATION = "PRIM_ACTIVE";
|
|
@@ -1098,7 +1251,7 @@ function repoActiveFlag(cwd, strict = false) {
|
|
|
1098
1251
|
function projectPrimInstallAt(root) {
|
|
1099
1252
|
if (root === null || root === homedir2()) return false;
|
|
1100
1253
|
for (const rel of PROJECT_INSTALL_FILES) {
|
|
1101
|
-
const path =
|
|
1254
|
+
const path = join12(root, rel);
|
|
1102
1255
|
if (existsSync5(path)) {
|
|
1103
1256
|
try {
|
|
1104
1257
|
const content = readFileSync9(path, "utf-8");
|
|
@@ -1145,10 +1298,10 @@ function parseAgent(argv) {
|
|
|
1145
1298
|
// src/commands/cursor-install.ts
|
|
1146
1299
|
import { existsSync as existsSync9, readFileSync as readFileSync12 } from "fs";
|
|
1147
1300
|
import { homedir as homedir4 } from "os";
|
|
1148
|
-
import { isAbsolute as isAbsolute5, join as
|
|
1301
|
+
import { isAbsolute as isAbsolute5, join as join16 } from "path";
|
|
1149
1302
|
|
|
1150
1303
|
// src/lib/hook-runtime.ts
|
|
1151
|
-
import { createHash as
|
|
1304
|
+
import { createHash as createHash8, randomBytes as randomBytes4 } from "crypto";
|
|
1152
1305
|
import {
|
|
1153
1306
|
constants as constants2,
|
|
1154
1307
|
accessSync,
|
|
@@ -1162,12 +1315,12 @@ import {
|
|
|
1162
1315
|
readdirSync as readdirSync2,
|
|
1163
1316
|
realpathSync as realpathSync2,
|
|
1164
1317
|
renameSync as renameSync2,
|
|
1165
|
-
rmSync as
|
|
1318
|
+
rmSync as rmSync8,
|
|
1166
1319
|
statSync as statSync2,
|
|
1167
1320
|
utimesSync,
|
|
1168
1321
|
writeFileSync as writeFileSync5
|
|
1169
1322
|
} from "fs";
|
|
1170
|
-
import { delimiter, dirname as dirname7, isAbsolute as isAbsolute4, join as
|
|
1323
|
+
import { delimiter, dirname as dirname7, isAbsolute as isAbsolute4, join as join14, resolve as resolve2 } from "path";
|
|
1171
1324
|
|
|
1172
1325
|
// src/lib/git-hook-contract.ts
|
|
1173
1326
|
var GIT_HOOK_ENTRYPOINT_CONTENT = `#!/bin/sh
|
|
@@ -1217,7 +1370,7 @@ exit 0
|
|
|
1217
1370
|
// src/lib/native-keyring.ts
|
|
1218
1371
|
import { existsSync as existsSync6 } from "fs";
|
|
1219
1372
|
import { createRequire as createRequire2 } from "module";
|
|
1220
|
-
import { dirname as dirname6, join as
|
|
1373
|
+
import { dirname as dirname6, join as join13 } from "path";
|
|
1221
1374
|
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
1222
1375
|
var require3 = createRequire2(import.meta.url);
|
|
1223
1376
|
|
|
@@ -1273,7 +1426,7 @@ import { execFileSync as execFileSync5 } from "child_process";
|
|
|
1273
1426
|
// src/commands/claude-install.ts
|
|
1274
1427
|
import { existsSync as existsSync8, lstatSync as lstatSync6, readFileSync as readFileSync11 } from "fs";
|
|
1275
1428
|
import { homedir as homedir3 } from "os";
|
|
1276
|
-
import { join as
|
|
1429
|
+
import { join as join15 } from "path";
|
|
1277
1430
|
var CAPTURE_BIN = "prim-hook";
|
|
1278
1431
|
var GATE_BIN = "prim-pre-tool-use";
|
|
1279
1432
|
var POST_TOOL_USE_BIN = "prim-post-tool-use";
|
|
@@ -1282,7 +1435,7 @@ var SESSION_END_BIN = "prim-session-end";
|
|
|
1282
1435
|
var STATUSLINE_BIN = "prim-statusline";
|
|
1283
1436
|
var STATUSLINE_ARGS = "";
|
|
1284
1437
|
var STATUSLINE_COMMAND = stableHookCommand(STATUSLINE_BIN, STATUSLINE_ARGS);
|
|
1285
|
-
var USER_SCOPE_PATH =
|
|
1438
|
+
var USER_SCOPE_PATH = join15(homedir3(), ".claude", "settings.json");
|
|
1286
1439
|
function projectRoot() {
|
|
1287
1440
|
return gitToplevel() ?? process.cwd();
|
|
1288
1441
|
}
|
|
@@ -1362,11 +1515,11 @@ function cursorConfigDirectory(env = process.env, home = homedir4(), platform =
|
|
|
1362
1515
|
const explicit = env.CURSOR_CONFIG_DIR?.trim();
|
|
1363
1516
|
if (explicit && isAbsolute5(explicit)) return explicit;
|
|
1364
1517
|
const xdg = env.XDG_CONFIG_HOME?.trim();
|
|
1365
|
-
if (platform !== "darwin" && xdg && isAbsolute5(xdg)) return
|
|
1366
|
-
return
|
|
1518
|
+
if (platform !== "darwin" && xdg && isAbsolute5(xdg)) return join16(xdg, "cursor");
|
|
1519
|
+
return join16(home, ".cursor");
|
|
1367
1520
|
}
|
|
1368
1521
|
function cursorHooksPath(scope, root = projectRoot()) {
|
|
1369
|
-
return scope === "user" ?
|
|
1522
|
+
return scope === "user" ? join16(cursorConfigDirectory(), "hooks.json") : join16(root, ".cursor", "hooks.json");
|
|
1370
1523
|
}
|
|
1371
1524
|
function plainObject(value) {
|
|
1372
1525
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -1518,7 +1671,7 @@ function readHookStdin(timeoutMs = 1e3, maxBytes = DEFAULT_MAX_BYTES) {
|
|
|
1518
1671
|
}
|
|
1519
1672
|
|
|
1520
1673
|
// src/hooks/normalize.ts
|
|
1521
|
-
import { createHash as
|
|
1674
|
+
import { createHash as createHash9 } from "crypto";
|
|
1522
1675
|
import { realpathSync as realpathSync3 } from "fs";
|
|
1523
1676
|
import { isAbsolute as isAbsolute6, resolve as resolve3 } from "path";
|
|
1524
1677
|
var HERMES_EVENT_MAP = {
|
|
@@ -1563,7 +1716,7 @@ function requiredId(value, field) {
|
|
|
1563
1716
|
}
|
|
1564
1717
|
function cursorToolUseId(value) {
|
|
1565
1718
|
if (SAFE_CORRELATION_ID_RE.test(value)) return value;
|
|
1566
|
-
return `cursor:tool:v1:${
|
|
1719
|
+
return `cursor:tool:v1:${createHash9("sha256").update(value).digest("hex")}`;
|
|
1567
1720
|
}
|
|
1568
1721
|
function workspaceCandidates(value) {
|
|
1569
1722
|
if (!Array.isArray(value)) return [];
|
|
@@ -1660,7 +1813,7 @@ function normalizeEnvelope(parsed, agent) {
|
|
|
1660
1813
|
]);
|
|
1661
1814
|
return {
|
|
1662
1815
|
...parsed,
|
|
1663
|
-
tool_use_id: `codex:fallback:v1:${
|
|
1816
|
+
tool_use_id: `codex:fallback:v1:${createHash9("sha256").update(stableToolIdentity).digest("hex")}`
|
|
1664
1817
|
};
|
|
1665
1818
|
}
|
|
1666
1819
|
return parsed;
|