@primitive.ai/prim 0.1.0-alpha.126 → 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 +211 -1
- package/SKILL.md +6 -1
- package/dist/{chunk-ZKXNJB6U.js → chunk-3KPTVK4N.js} +70 -78
- package/dist/{chunk-6KXQQ7CQ.js → chunk-4DYEBYKM.js} +3 -5
- package/dist/{chunk-NJBN3WXF.js → chunk-6QAXVFRJ.js} +1900 -179
- package/dist/{chunk-R6NDP7OF.js → chunk-ANMNGGTN.js} +2 -2
- package/dist/{chunk-PPZ56EVJ.js → chunk-B3ICJZXV.js} +4 -2
- package/dist/{chunk-5D2C6RCH.js → chunk-C357P37M.js} +4 -4
- package/dist/{chunk-DT7E3SD5.js → chunk-E57UVUJL.js} +6 -6
- package/dist/{chunk-KKUDAQQZ.js → chunk-EQWUCFXK.js} +3 -3
- package/dist/{chunk-JFWJN2ME.js → chunk-M6KIDB5R.js} +119 -5
- package/dist/{chunk-NBNQI3YJ.js → chunk-N6PZE7HX.js} +6 -3
- package/dist/{chunk-O73J3KQQ.js → chunk-NYDM4HSS.js} +8 -5
- package/dist/{chunk-74DQDWLW.js → chunk-P4O7L7JZ.js} +7 -2
- package/dist/{chunk-L32WYFJT.js → chunk-PRJ7P6J5.js} +135 -12
- package/dist/{chunk-ND6TYHNI.js → chunk-PTZ6BWSU.js} +2 -2
- package/dist/{chunk-BFYJLI7V.js → chunk-QQ3G5SXX.js} +1 -1
- package/dist/{chunk-EZXIKEMA.js → chunk-RDT2IV2Z.js} +9 -7
- package/dist/{chunk-TI37ME2D.js → chunk-VLT2KGPE.js} +47 -38
- package/dist/{chunk-YP4OXKNX.js → chunk-WEGZGF3K.js} +1 -1
- package/dist/{chunk-C5VBPHOS.js → chunk-XINFTNA2.js} +23 -9
- package/dist/{chunk-FYYHBOOM.js → chunk-ZCT23NBS.js} +2 -2
- package/dist/daemon/server.js +1976 -442
- package/dist/hook-runtime/daemon/server.js +1976 -442
- package/dist/hook-runtime/hooks/post-commit.js +592 -212
- package/dist/hook-runtime/hooks/post-rewrite.js +584 -204
- package/dist/hook-runtime/hooks/post-tool-use.js +1997 -575
- package/dist/hook-runtime/hooks/pre-commit.js +1742 -350
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +1656 -261
- package/dist/hook-runtime/hooks/prim-hook.js +1992 -573
- package/dist/hook-runtime/hooks/session-end.js +549 -180
- package/dist/hook-runtime/hooks/session-start.js +2213 -596
- package/dist/hook-runtime/index.js +5318 -2423
- package/dist/hook-runtime/statusline-main.js +531 -150
- 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 +14 -15
- package/dist/hooks/prim-hook.js +16 -16
- package/dist/hooks/session-end.js +4 -4
- package/dist/hooks/session-start.js +95 -21
- package/dist/index.js +1386 -299
- package/dist/statusline-main.js +531 -150
- package/package.json +1 -1
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
import { createRequire as __primCreateRequire } from "node:module"; const require = __primCreateRequire(import.meta.url);
|
|
3
3
|
|
|
4
4
|
// src/commands/statusline.ts
|
|
5
|
-
import { readFileSync as
|
|
5
|
+
import { readFileSync as readFileSync12 } from "fs";
|
|
6
6
|
import { dirname as dirname7, resolve as resolve3 } from "path";
|
|
7
7
|
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
8
8
|
|
|
9
|
+
// src/lib/cloud-env.ts
|
|
10
|
+
function isClaudeCloudSession(env = process.env) {
|
|
11
|
+
return env.CLAUDE_CODE_REMOTE === "true";
|
|
12
|
+
}
|
|
13
|
+
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';
|
|
14
|
+
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}.`;
|
|
15
|
+
|
|
9
16
|
// src/client.ts
|
|
10
|
-
import { createHash as
|
|
11
|
-
import { chmodSync as chmodSync3, existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as
|
|
12
|
-
import { dirname as
|
|
17
|
+
import { createHash as createHash7 } from "crypto";
|
|
18
|
+
import { chmodSync as chmodSync3, existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as readFileSync9, rmSync as rmSync7 } from "fs";
|
|
19
|
+
import { dirname as dirname6 } from "path";
|
|
13
20
|
|
|
14
21
|
// src/lib/atomic-file.ts
|
|
15
22
|
import { randomUUID } from "crypto";
|
|
@@ -27,16 +34,25 @@ import {
|
|
|
27
34
|
import { dirname } from "path";
|
|
28
35
|
|
|
29
36
|
// src/lib/credentials.ts
|
|
30
|
-
import { createHash as
|
|
31
|
-
import { readFileSync as
|
|
32
|
-
import { join as
|
|
37
|
+
import { createHash as createHash6 } from "crypto";
|
|
38
|
+
import { readFileSync as readFileSync7 } from "fs";
|
|
39
|
+
import { join as join9 } from "path";
|
|
33
40
|
|
|
34
41
|
// src/lib/machine-credentials.ts
|
|
35
|
-
import { execFileSync } from "child_process";
|
|
36
|
-
import { createHash as
|
|
37
|
-
import {
|
|
42
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
43
|
+
import { createHash as createHash5, randomBytes as randomBytes3, randomUUID as randomUUID2 } from "crypto";
|
|
44
|
+
import {
|
|
45
|
+
chmodSync,
|
|
46
|
+
existsSync as existsSync3,
|
|
47
|
+
linkSync,
|
|
48
|
+
lstatSync as lstatSync4,
|
|
49
|
+
mkdirSync as mkdirSync3,
|
|
50
|
+
readFileSync as readFileSync6,
|
|
51
|
+
rmSync as rmSync6,
|
|
52
|
+
writeFileSync as writeFileSync3
|
|
53
|
+
} from "fs";
|
|
38
54
|
import { createRequire } from "module";
|
|
39
|
-
import { dirname as
|
|
55
|
+
import { dirname as dirname5, join as join8 } from "path";
|
|
40
56
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
41
57
|
|
|
42
58
|
// src/lib/file-lock.ts
|
|
@@ -97,6 +113,10 @@ var PRIM_CONFIG_SHELL_RESOLVER = [
|
|
|
97
113
|
"fi; fi; "
|
|
98
114
|
].join("");
|
|
99
115
|
|
|
116
|
+
// src/lib/loopback-fetch.ts
|
|
117
|
+
import { request } from "http";
|
|
118
|
+
import { Readable } from "stream";
|
|
119
|
+
|
|
100
120
|
// src/lib/workos-connect.ts
|
|
101
121
|
var DISCOVERY_MAX_BYTES = 16 * 1024;
|
|
102
122
|
var WORKOS_CONNECT_RESPONSE_MAX_BYTES = 64 * 1024;
|
|
@@ -272,31 +292,328 @@ function primConfigDirectory(options = {}) {
|
|
|
272
292
|
|
|
273
293
|
// src/lib/machine-broker.ts
|
|
274
294
|
var MAX_RESPONSE_BYTES = 96 * 1024;
|
|
295
|
+
var MAX_FORWARD_REQUEST_BYTES = 12 * 1024 * 1024;
|
|
296
|
+
var MAX_API_RESPONSE_BYTES = 8 * 1024 * 1024;
|
|
297
|
+
var MAX_FORWARD_RESPONSE_BYTES = 36 * 1024 * 1024;
|
|
275
298
|
var IDLE_MS = 5 * 6e4;
|
|
276
299
|
|
|
277
300
|
// src/lib/machine-retry.ts
|
|
278
301
|
import { createHash as createHash2 } from "crypto";
|
|
279
|
-
import { lstatSync as lstatSync2, readFileSync as readFileSync4, rmSync as rmSync4 } from "fs";
|
|
302
|
+
import { lstatSync as lstatSync2, readFileSync as readFileSync4, readdirSync, rmSync as rmSync4 } from "fs";
|
|
280
303
|
import { join as join5 } from "path";
|
|
304
|
+
var MAX_SERVER_WAIT_MS = 15 * 6e4;
|
|
305
|
+
|
|
306
|
+
// src/lib/session-identity.ts
|
|
307
|
+
import { execFile } from "child_process";
|
|
308
|
+
import { createHash as createHash4 } from "crypto";
|
|
309
|
+
import { rmSync as rmSync5 } from "fs";
|
|
310
|
+
import { join as join7 } from "path";
|
|
311
|
+
import { promisify } from "util";
|
|
312
|
+
|
|
313
|
+
// src/lib/git.ts
|
|
314
|
+
import { execFileSync } from "child_process";
|
|
315
|
+
import { createHash as createHash3 } from "crypto";
|
|
316
|
+
import { lstatSync as lstatSync3, readFileSync as readFileSync5, realpathSync } from "fs";
|
|
317
|
+
import { basename as basename2, dirname as dirname4, isAbsolute as isAbsolute3, join as join6, relative, resolve, sep } from "path";
|
|
318
|
+
var GIT_TIMEOUT_MS = 1e3;
|
|
319
|
+
var MAX_SETTINGS_BYTES = 1024 * 1024;
|
|
320
|
+
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
321
|
+
var GITHUB_REPOSITORY_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9._-]{1,100}$/u;
|
|
322
|
+
var CREDENTIAL_VARIABLE = /^(?:PRIM_TOKEN|PRIM_M2M_\w+)$/u;
|
|
323
|
+
function gitChildEnvironment(env = process.env) {
|
|
324
|
+
return Object.fromEntries(
|
|
325
|
+
Object.entries(env).filter(([name]) => !CREDENTIAL_VARIABLE.test(name))
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
function gitToplevel(cwd) {
|
|
329
|
+
try {
|
|
330
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
331
|
+
cwd,
|
|
332
|
+
encoding: "utf-8",
|
|
333
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
334
|
+
timeout: GIT_TIMEOUT_MS,
|
|
335
|
+
env: gitChildEnvironment()
|
|
336
|
+
}).trim();
|
|
337
|
+
} catch {
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function gitValue(cwd, args) {
|
|
342
|
+
try {
|
|
343
|
+
const value = execFileSync("git", args, {
|
|
344
|
+
cwd,
|
|
345
|
+
encoding: "utf-8",
|
|
346
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
347
|
+
timeout: GIT_TIMEOUT_MS,
|
|
348
|
+
env: gitChildEnvironment()
|
|
349
|
+
}).trim();
|
|
350
|
+
return value || void 0;
|
|
351
|
+
} catch {
|
|
352
|
+
return void 0;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function parseGithubRepositoryName(path) {
|
|
356
|
+
const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
|
|
357
|
+
if (parts.length !== 2 || !GITHUB_OWNER_PATTERN.test(parts[0] ?? "") || !GITHUB_REPOSITORY_PATTERN.test(parts[1] ?? "")) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
return `${parts[0]}/${parts[1]}`;
|
|
361
|
+
}
|
|
362
|
+
function githubRepositoryFromRemote(remote) {
|
|
363
|
+
if ([...remote].some((char) => char < " " || char === "\x7F")) return null;
|
|
364
|
+
const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
|
|
365
|
+
remote
|
|
366
|
+
);
|
|
367
|
+
const path = match?.[1] ?? match?.[2];
|
|
368
|
+
return path ? parseGithubRepositoryName(path) : null;
|
|
369
|
+
}
|
|
370
|
+
function checkoutSettingsText(repoRoot) {
|
|
371
|
+
const path = join6(repoRoot, ".claude", "settings.json");
|
|
372
|
+
try {
|
|
373
|
+
if (!lstatSync3(join6(repoRoot, ".claude")).isDirectory()) return null;
|
|
374
|
+
const stat = lstatSync3(path);
|
|
375
|
+
if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
|
|
376
|
+
return readFileSync5(path, "utf8");
|
|
377
|
+
} catch {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function checkoutDeclaration(repoRoot, name) {
|
|
382
|
+
const text = checkoutSettingsText(repoRoot);
|
|
383
|
+
if (text === null) return null;
|
|
384
|
+
let parsed;
|
|
385
|
+
try {
|
|
386
|
+
parsed = JSON.parse(text);
|
|
387
|
+
} catch {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
const env = typeof parsed === "object" && parsed !== null ? parsed.env : void 0;
|
|
391
|
+
const value = typeof env === "object" && env !== null ? env[name] : void 0;
|
|
392
|
+
return typeof value === "string" ? value : null;
|
|
393
|
+
}
|
|
394
|
+
function declaredGithubRepository(repoRoot) {
|
|
395
|
+
const value = checkoutDeclaration(repoRoot, "PRIM_REPOSITORY");
|
|
396
|
+
return value === null ? null : parseGithubRepositoryName(value.trim());
|
|
397
|
+
}
|
|
398
|
+
function githubRepositoryFullName(cwd) {
|
|
399
|
+
const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
|
|
400
|
+
if (remote) {
|
|
401
|
+
const fromOrigin = githubRepositoryFromRemote(remote);
|
|
402
|
+
if (fromOrigin || normalizeOriginRemote(remote)) return fromOrigin;
|
|
403
|
+
}
|
|
404
|
+
const root = gitToplevel(cwd);
|
|
405
|
+
return root ? declaredGithubRepository(root) : null;
|
|
406
|
+
}
|
|
407
|
+
function normalizeOriginRemote(remote) {
|
|
408
|
+
const value = remote.trim();
|
|
409
|
+
if (!value) return void 0;
|
|
410
|
+
const scp = !value.includes("://") ? /^(?:[^@/\s]+@)?(?<host>[^:/\s]+):(?<path>.+)$/.exec(value)?.groups : void 0;
|
|
411
|
+
if (scp?.host && scp.path) {
|
|
412
|
+
const path = scp.path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
413
|
+
return path ? `${scp.host.toLowerCase()}/${path}` : void 0;
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const url = new URL(value);
|
|
417
|
+
url.username = "";
|
|
418
|
+
url.password = "";
|
|
419
|
+
url.search = "";
|
|
420
|
+
url.hash = "";
|
|
421
|
+
const path = url.pathname.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
422
|
+
if (url.host && path) return `${url.host.toLowerCase()}/${path}`;
|
|
423
|
+
return void 0;
|
|
424
|
+
} catch {
|
|
425
|
+
return void 0;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/lib/terminal-safe.ts
|
|
430
|
+
function replaceIsolatedSurrogates(value) {
|
|
431
|
+
let out = "";
|
|
432
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
433
|
+
const unit = value.charCodeAt(index);
|
|
434
|
+
if (unit >= 55296 && unit <= 56319) {
|
|
435
|
+
const next = value.charCodeAt(index + 1);
|
|
436
|
+
if (next >= 56320 && next <= 57343) {
|
|
437
|
+
out += value[index] + value[index + 1];
|
|
438
|
+
index += 1;
|
|
439
|
+
} else {
|
|
440
|
+
out += "\uFFFD";
|
|
441
|
+
}
|
|
442
|
+
} else if (unit >= 56320 && unit <= 57343) {
|
|
443
|
+
out += "\uFFFD";
|
|
444
|
+
} else {
|
|
445
|
+
out += value[index];
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return out;
|
|
449
|
+
}
|
|
450
|
+
function isUnsafeTerminalCodePoint(point) {
|
|
451
|
+
const c0OrC1 = point <= 31 || point >= 127 && point <= 159;
|
|
452
|
+
const bidiOrZeroWidth = point === 173 || // soft hyphen
|
|
453
|
+
point === 1564 || // Arabic letter mark
|
|
454
|
+
point === 6158 || // Mongolian vowel separator
|
|
455
|
+
point >= 8203 && point <= 8207 || // zero-width + directional marks
|
|
456
|
+
point >= 8234 && point <= 8238 || // bidi embeddings/overrides
|
|
457
|
+
point >= 8288 && point <= 8303 || // word joiner + bidi isolates/controls
|
|
458
|
+
point >= 65529 && point <= 65531 || // interlinear annotation controls
|
|
459
|
+
point === 65279 || // zero-width no-break space / BOM
|
|
460
|
+
point >= 917504 && point <= 917631;
|
|
461
|
+
return c0OrC1 || bidiOrZeroWidth;
|
|
462
|
+
}
|
|
463
|
+
function terminalSafeText(value) {
|
|
464
|
+
return Array.from(replaceIsolatedSurrogates(value)).filter((character) => !isUnsafeTerminalCodePoint(character.codePointAt(0) ?? 0)).join("");
|
|
465
|
+
}
|
|
466
|
+
var stripControlChars = terminalSafeText;
|
|
467
|
+
|
|
468
|
+
// src/lib/session-identity.ts
|
|
469
|
+
var SESSION_IDENTITY_VARIABLE = "PRIM_CLOUD_IDENTITY";
|
|
470
|
+
var SESSION_IDENTITY_VALUE = "session";
|
|
471
|
+
var SESSION_ID_VARIABLE = "CLAUDE_CODE_REMOTE_SESSION_ID";
|
|
472
|
+
var SESSION_TOKEN_PREFIX = "prim_sess_";
|
|
473
|
+
var SESSION_ID = /^[A-Za-z0-9._:-]{1,200}$/u;
|
|
474
|
+
var DEFAULT_API_URL = "https://api.getprimitive.ai";
|
|
475
|
+
var RENEW_BEFORE_MS = 6e4;
|
|
476
|
+
var BACKGROUND_RETRY_MS = 5 * 6e4;
|
|
477
|
+
var RESPONSE_MAX_BYTES = 64 * 1024;
|
|
478
|
+
function sessionIdentityRequested(env = process.env) {
|
|
479
|
+
return isClaudeCloudSession(env) && env[SESSION_IDENTITY_VARIABLE]?.trim() === SESSION_IDENTITY_VALUE;
|
|
480
|
+
}
|
|
481
|
+
function sessionIdentityMode(deps = {}) {
|
|
482
|
+
const env = deps.env ?? process.env;
|
|
483
|
+
return sessionIdentityRequested(env) && (env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || env.PRIM_M2M_CLIENT_AUTH !== void 0);
|
|
484
|
+
}
|
|
485
|
+
function failure(code, cause, remedy) {
|
|
486
|
+
return { status: "failed", code, cause, remedy };
|
|
487
|
+
}
|
|
488
|
+
var NEW_SESSION_REMEDY = "start a new cloud session";
|
|
489
|
+
var SESSION_REVOKED = failure(
|
|
490
|
+
"session_revoked",
|
|
491
|
+
"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",
|
|
492
|
+
`${NEW_SESSION_REMEDY}, which identifies you again through GitHub (if your GitHub link was removed, run \`prim auth link\` there when it asks)`
|
|
493
|
+
);
|
|
494
|
+
var CHALLENGE_WINDOW_PASSED = failure(
|
|
495
|
+
"challenge_expired",
|
|
496
|
+
"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)",
|
|
497
|
+
`${NEW_SESSION_REMEDY}, and if it asks you to link your GitHub account, run \`prim auth link\` there right away`
|
|
498
|
+
);
|
|
499
|
+
var CHALLENGE_GONE = failure(
|
|
500
|
+
"challenge_not_found",
|
|
501
|
+
"Primitive no longer knows this session's identity check (the environment credential may have been replaced since)",
|
|
502
|
+
NEW_SESSION_REMEDY
|
|
503
|
+
);
|
|
504
|
+
function hash(value) {
|
|
505
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
506
|
+
}
|
|
507
|
+
function sessionContext(deps) {
|
|
508
|
+
const env = deps.env ?? process.env;
|
|
509
|
+
const environment = environmentCredentials(deps);
|
|
510
|
+
if (!environment)
|
|
511
|
+
return failure(
|
|
512
|
+
"environment_credential_missing",
|
|
513
|
+
"the cloud environment has no environment credential",
|
|
514
|
+
'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'
|
|
515
|
+
);
|
|
516
|
+
const sessionId = env[SESSION_ID_VARIABLE]?.trim();
|
|
517
|
+
if (!sessionId || !SESSION_ID.test(sessionId))
|
|
518
|
+
return failure(
|
|
519
|
+
"session_id_missing",
|
|
520
|
+
`this Claude Code cloud session does not report a session ID Primitive accepts (${SESSION_ID_VARIABLE})`,
|
|
521
|
+
NEW_SESSION_REMEDY
|
|
522
|
+
);
|
|
523
|
+
const apiUrl = machineApiUrl(env.PRIM_API_URL?.trim() || DEFAULT_API_URL);
|
|
524
|
+
const directory = deps.directory ?? primConfigDirectory({ env });
|
|
525
|
+
const key = hash(`${apiUrl}\0${environment.clientId}\0${sessionId}`);
|
|
526
|
+
return {
|
|
527
|
+
directory,
|
|
528
|
+
apiUrl,
|
|
529
|
+
clientId: environment.clientId,
|
|
530
|
+
generation: environment.generation,
|
|
531
|
+
sessionId,
|
|
532
|
+
tokenPath: join7(directory, `session_identity_${key}.json`),
|
|
533
|
+
statePath: join7(directory, `session_identity_${key}.state.json`),
|
|
534
|
+
lockPath: join7(directory, "session-identity.lock")
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function readRecord(path) {
|
|
538
|
+
try {
|
|
539
|
+
const value = readMachineMetadata(path);
|
|
540
|
+
return value === void 0 ? void 0 : machineRecord(value);
|
|
541
|
+
} catch {
|
|
542
|
+
return void 0;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function finiteNumber(value) {
|
|
546
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
547
|
+
}
|
|
548
|
+
function readToken(context) {
|
|
549
|
+
const data = readRecord(context.tokenPath);
|
|
550
|
+
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))
|
|
551
|
+
return void 0;
|
|
552
|
+
const identity = data.identity;
|
|
553
|
+
if (!identity || identity.clientId !== context.clientId || identity.authorizedUserId !== data.userId || identity.organizationId !== data.organizationId)
|
|
554
|
+
return void 0;
|
|
555
|
+
return data;
|
|
556
|
+
}
|
|
557
|
+
function tokenValid(record, now = Date.now()) {
|
|
558
|
+
return now < record.expiresAt - RENEW_BEFORE_MS;
|
|
559
|
+
}
|
|
560
|
+
function toCredential(record) {
|
|
561
|
+
return {
|
|
562
|
+
issuer: record.issuer,
|
|
563
|
+
audience: record.audience,
|
|
564
|
+
...record.identity,
|
|
565
|
+
accessToken: record.accessToken,
|
|
566
|
+
expiresAt: record.expiresAt,
|
|
567
|
+
apiUrl: record.apiUrl,
|
|
568
|
+
generation: record.generation,
|
|
569
|
+
storage: "environment",
|
|
570
|
+
session: {
|
|
571
|
+
userId: record.userId,
|
|
572
|
+
displayName: record.displayName,
|
|
573
|
+
...record.githubLogin ? { githubLogin: record.githubLogin } : {},
|
|
574
|
+
refreshAfter: record.refreshAfter
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
function cachedSessionCredential(deps = {}) {
|
|
579
|
+
if (!sessionIdentityMode(deps)) return void 0;
|
|
580
|
+
let context;
|
|
581
|
+
try {
|
|
582
|
+
context = sessionContext(deps);
|
|
583
|
+
} catch {
|
|
584
|
+
return void 0;
|
|
585
|
+
}
|
|
586
|
+
if ("status" in context) return void 0;
|
|
587
|
+
const record = readToken(context);
|
|
588
|
+
return record && tokenValid(record) ? toCredential(record) : void 0;
|
|
589
|
+
}
|
|
590
|
+
var SERVER_CONTRACT_FAILURE = failure(
|
|
591
|
+
"server_contract",
|
|
592
|
+
"Primitive's answer did not identify the linked member",
|
|
593
|
+
"update prim (`npx --yes @primitive.ai/prim@latest`) and run `prim setup --cloud`; if it persists, report it to Primitive"
|
|
594
|
+
);
|
|
281
595
|
|
|
282
596
|
// src/lib/machine-credentials.ts
|
|
597
|
+
function sessionScoped(deps) {
|
|
598
|
+
return !deps.localEnvironment && !deps.environmentOnly && sessionIdentityMode(deps);
|
|
599
|
+
}
|
|
283
600
|
var require2 = createRequire(import.meta.url);
|
|
284
|
-
var
|
|
601
|
+
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
285
602
|
var PROFILE_FILE = "machine_profile.json";
|
|
286
603
|
var cached;
|
|
287
604
|
function configurationDirectory(deps) {
|
|
288
605
|
return deps.directory ?? primConfigDirectory({ env: deps.env });
|
|
289
606
|
}
|
|
290
607
|
function apiUrlFor(deps) {
|
|
291
|
-
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() ||
|
|
608
|
+
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL2);
|
|
292
609
|
}
|
|
293
|
-
function
|
|
294
|
-
return
|
|
610
|
+
function hash2(value) {
|
|
611
|
+
return createHash5("sha256").update(value).digest("hex");
|
|
295
612
|
}
|
|
296
613
|
function environmentContextPath(deps, clientId) {
|
|
297
|
-
return
|
|
614
|
+
return join8(
|
|
298
615
|
configurationDirectory(deps),
|
|
299
|
-
`machine_context_${
|
|
616
|
+
`machine_context_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
300
617
|
);
|
|
301
618
|
}
|
|
302
619
|
function readEnvironmentContext(environment, deps) {
|
|
@@ -320,17 +637,47 @@ function readEnvironmentContext(environment, deps) {
|
|
|
320
637
|
return void 0;
|
|
321
638
|
}
|
|
322
639
|
}
|
|
640
|
+
function protectPath(path, directory) {
|
|
641
|
+
if (process.platform !== "win32") {
|
|
642
|
+
chmodSync(path, directory ? 448 : 384);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
const encodedPath = Buffer.from(path).toString("base64");
|
|
646
|
+
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)`;
|
|
647
|
+
execFileSync2(
|
|
648
|
+
"powershell.exe",
|
|
649
|
+
[
|
|
650
|
+
"-NoProfile",
|
|
651
|
+
"-NonInteractive",
|
|
652
|
+
"-EncodedCommand",
|
|
653
|
+
Buffer.from(script, "utf16le").toString("base64")
|
|
654
|
+
],
|
|
655
|
+
{ stdio: "pipe", encoding: "utf8", windowsHide: true }
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
function ensureDirectory(directory) {
|
|
659
|
+
mkdirSync3(directory, { recursive: true, mode: 448 });
|
|
660
|
+
if (lstatSync4(directory).isSymbolicLink())
|
|
661
|
+
throw new MachineAuthError(
|
|
662
|
+
"unsafe_storage",
|
|
663
|
+
"Credential directory must not be a symbolic link."
|
|
664
|
+
);
|
|
665
|
+
protectPath(directory, true);
|
|
666
|
+
}
|
|
667
|
+
function readMachineMetadata(path) {
|
|
668
|
+
return readPrivate(path);
|
|
669
|
+
}
|
|
323
670
|
function readPrivate(path) {
|
|
324
671
|
let raw;
|
|
325
672
|
try {
|
|
326
|
-
const stat =
|
|
673
|
+
const stat = lstatSync4(path);
|
|
327
674
|
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
328
675
|
throw new MachineAuthError(
|
|
329
676
|
"unsafe_storage",
|
|
330
677
|
"Machine credential files must be owner-only regular files."
|
|
331
678
|
);
|
|
332
679
|
}
|
|
333
|
-
raw =
|
|
680
|
+
raw = readFileSync6(path, "utf8");
|
|
334
681
|
} catch (error) {
|
|
335
682
|
if (error.code === "ENOENT") return void 0;
|
|
336
683
|
throw error;
|
|
@@ -375,13 +722,101 @@ function parseProfile(value) {
|
|
|
375
722
|
};
|
|
376
723
|
}
|
|
377
724
|
function readMachineProfile(deps = {}) {
|
|
378
|
-
const value = readPrivate(
|
|
725
|
+
const value = readPrivate(join8(configurationDirectory(deps), PROFILE_FILE));
|
|
379
726
|
return value === void 0 ? void 0 : parseProfile(value);
|
|
380
727
|
}
|
|
728
|
+
var NETWORK_SECRET_AUTH = "network-secret";
|
|
729
|
+
var NETWORK_TOKEN_AUTH = "network-token";
|
|
730
|
+
var INJECTED_IDENTITY_TTL_MS = 60 * 6e4;
|
|
731
|
+
var brokerKeys = /* @__PURE__ */ new Map();
|
|
732
|
+
function networkSecretBrokerKey(deps, clientId) {
|
|
733
|
+
const directory = configurationDirectory(deps);
|
|
734
|
+
const path = join8(directory, `machine_network_${hash2(`${apiUrlFor(deps)}\0${clientId}`)}.key`);
|
|
735
|
+
const known = brokerKeys.get(path);
|
|
736
|
+
if (known) return known;
|
|
737
|
+
ensureDirectory(directory);
|
|
738
|
+
const read = () => {
|
|
739
|
+
try {
|
|
740
|
+
const stat = lstatSync4(path);
|
|
741
|
+
if (!stat.isFile() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()))
|
|
742
|
+
throw new MachineAuthError(
|
|
743
|
+
"unsafe_storage",
|
|
744
|
+
`The machine broker key at ${path} is not an owner-only file. Delete it and retry.`,
|
|
745
|
+
true
|
|
746
|
+
);
|
|
747
|
+
const value = readFileSync6(path, "utf8").trim();
|
|
748
|
+
return { key: /^[a-f0-9]{64}$/u.test(value) ? value : void 0 };
|
|
749
|
+
} catch (error) {
|
|
750
|
+
if (error.code === "ENOENT") return void 0;
|
|
751
|
+
throw error;
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
755
|
+
const existing = read();
|
|
756
|
+
if (existing?.key) {
|
|
757
|
+
brokerKeys.set(path, existing.key);
|
|
758
|
+
return existing.key;
|
|
759
|
+
}
|
|
760
|
+
if (existing)
|
|
761
|
+
throw new MachineAuthError(
|
|
762
|
+
"unsafe_storage",
|
|
763
|
+
`The machine broker key at ${path} is damaged. Delete it and retry.`,
|
|
764
|
+
true
|
|
765
|
+
);
|
|
766
|
+
const staged = `${path}.${process.pid}.${randomBytes3(6).toString("hex")}`;
|
|
767
|
+
try {
|
|
768
|
+
writeFileSync3(staged, `${randomBytes3(32).toString("hex")}
|
|
769
|
+
`, { mode: 384, flag: "wx" });
|
|
770
|
+
protectPath(staged, false);
|
|
771
|
+
linkSync(staged, path);
|
|
772
|
+
} catch (error) {
|
|
773
|
+
if (error.code !== "EEXIST") throw error;
|
|
774
|
+
} finally {
|
|
775
|
+
rmSync6(staged, { force: true });
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
const key = read()?.key;
|
|
779
|
+
if (!key)
|
|
780
|
+
throw new MachineAuthError("unsafe_storage", "The machine broker key is unreadable.", true);
|
|
781
|
+
brokerKeys.set(path, key);
|
|
782
|
+
return key;
|
|
783
|
+
}
|
|
381
784
|
function environmentCredentials(deps) {
|
|
382
785
|
const env = deps.env ?? process.env;
|
|
383
|
-
const {
|
|
384
|
-
|
|
786
|
+
const {
|
|
787
|
+
PRIM_M2M_CLIENT_ID: clientId,
|
|
788
|
+
PRIM_M2M_CLIENT_SECRET: clientSecret,
|
|
789
|
+
PRIM_M2M_CLIENT_AUTH: auth
|
|
790
|
+
} = env;
|
|
791
|
+
if (clientId === void 0 && clientSecret === void 0 && auth === void 0) return void 0;
|
|
792
|
+
if (auth !== void 0 && auth !== NETWORK_SECRET_AUTH && auth !== NETWORK_TOKEN_AUTH)
|
|
793
|
+
throw new MachineAuthError(
|
|
794
|
+
"incomplete_machine_credentials",
|
|
795
|
+
`PRIM_M2M_CLIENT_AUTH accepts ${NETWORK_SECRET_AUTH} or ${NETWORK_TOKEN_AUTH}.`
|
|
796
|
+
);
|
|
797
|
+
if (auth !== void 0) {
|
|
798
|
+
if (!clientId || !/^client_[\w-]+$/u.test(clientId))
|
|
799
|
+
throw new MachineAuthError(
|
|
800
|
+
"incomplete_machine_credentials",
|
|
801
|
+
`Set PRIM_M2M_CLIENT_ID with PRIM_M2M_CLIENT_AUTH=${auth}.`
|
|
802
|
+
);
|
|
803
|
+
if (clientSecret !== void 0)
|
|
804
|
+
throw new MachineAuthError(
|
|
805
|
+
"conflicting_credentials",
|
|
806
|
+
`Unset PRIM_M2M_CLIENT_SECRET: with PRIM_M2M_CLIENT_AUTH=${auth} the environment's network secret supplies it.`
|
|
807
|
+
);
|
|
808
|
+
const generation = hash2(`${apiUrlFor(deps)}\0${clientId}\0${auth}\0broker-proxy`);
|
|
809
|
+
const credentials = {
|
|
810
|
+
clientId,
|
|
811
|
+
generation,
|
|
812
|
+
...auth === NETWORK_TOKEN_AUTH ? { tokenInjection: "network" } : { secretInjection: "network" }
|
|
813
|
+
};
|
|
814
|
+
Object.defineProperty(credentials, "clientSecret", {
|
|
815
|
+
enumerable: true,
|
|
816
|
+
get: () => networkSecretBrokerKey(deps, clientId)
|
|
817
|
+
});
|
|
818
|
+
return credentials;
|
|
819
|
+
}
|
|
385
820
|
if (!clientId || !clientSecret || !/^client_[\w-]+$/u.test(clientId) || !machineString(clientSecret)) {
|
|
386
821
|
throw new MachineAuthError(
|
|
387
822
|
"incomplete_machine_credentials",
|
|
@@ -391,12 +826,12 @@ function environmentCredentials(deps) {
|
|
|
391
826
|
return {
|
|
392
827
|
clientId,
|
|
393
828
|
clientSecret,
|
|
394
|
-
generation:
|
|
829
|
+
generation: hash2(`${apiUrlFor(deps)}\0${clientId}\0${clientSecret}`)
|
|
395
830
|
};
|
|
396
831
|
}
|
|
397
832
|
function hasMachineConfiguration(deps = {}) {
|
|
398
833
|
const env = deps.env ?? process.env;
|
|
399
|
-
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || existsSync3(
|
|
834
|
+
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));
|
|
400
835
|
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
401
836
|
throw new MachineAuthError(
|
|
402
837
|
"conflicting_credentials",
|
|
@@ -408,6 +843,7 @@ function hasMachineConfiguration(deps = {}) {
|
|
|
408
843
|
}
|
|
409
844
|
function machineContext(deps = {}) {
|
|
410
845
|
if (!hasMachineConfiguration(deps)) return void 0;
|
|
846
|
+
if (sessionScoped(deps)) return cachedSessionCredential(deps);
|
|
411
847
|
const environment = environmentCredentials(deps);
|
|
412
848
|
if (environment) {
|
|
413
849
|
if (environmentTerminal === environment.generation) return void 0;
|
|
@@ -422,6 +858,7 @@ function machineContext(deps = {}) {
|
|
|
422
858
|
return profile;
|
|
423
859
|
}
|
|
424
860
|
function cachedMachineCredential(deps = {}) {
|
|
861
|
+
if (sessionScoped(deps)) return cachedSessionCredential(deps);
|
|
425
862
|
const context = machineContext(deps);
|
|
426
863
|
return context && cached?.generation === context.generation && cached.apiUrl === context.apiUrl ? cached : void 0;
|
|
427
864
|
}
|
|
@@ -429,20 +866,20 @@ var environmentTerminal;
|
|
|
429
866
|
|
|
430
867
|
// src/lib/credentials.ts
|
|
431
868
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
432
|
-
var TOKEN_FILE_PATH =
|
|
433
|
-
var REFRESH_TOKEN_PATH =
|
|
434
|
-
var TOKEN_EXPIRES_PATH =
|
|
435
|
-
var TERMINAL_REFRESH_PATH =
|
|
436
|
-
var CREDENTIAL_METADATA_PATH =
|
|
437
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
438
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
439
|
-
var CREDENTIAL_LOCK_PATH =
|
|
869
|
+
var TOKEN_FILE_PATH = join9(CONFIG_DIRECTORY, "token");
|
|
870
|
+
var REFRESH_TOKEN_PATH = join9(CONFIG_DIRECTORY, "refresh_token");
|
|
871
|
+
var TOKEN_EXPIRES_PATH = join9(CONFIG_DIRECTORY, "token_expires_at");
|
|
872
|
+
var TERMINAL_REFRESH_PATH = join9(CONFIG_DIRECTORY, "refresh_terminal");
|
|
873
|
+
var CREDENTIAL_METADATA_PATH = join9(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
874
|
+
var CREDENTIAL_FAMILY_PATH = join9(CONFIG_DIRECTORY, "credential_family.json");
|
|
875
|
+
var CREDENTIAL_MIGRATION_PATH = join9(CONFIG_DIRECTORY, "credential_migration.json");
|
|
876
|
+
var CREDENTIAL_LOCK_PATH = join9(CONFIG_DIRECTORY, "credentials.lock");
|
|
440
877
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
441
878
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
442
879
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
443
880
|
function readTrimmed(path) {
|
|
444
881
|
try {
|
|
445
|
-
const value =
|
|
882
|
+
const value = readFileSync7(path, "utf8").trim();
|
|
446
883
|
return value || void 0;
|
|
447
884
|
} catch {
|
|
448
885
|
return void 0;
|
|
@@ -451,7 +888,7 @@ function readTrimmed(path) {
|
|
|
451
888
|
function resolveAuthCredential(options = {}) {
|
|
452
889
|
if (!options.tokenFilePath && hasMachineConfiguration({ env: options.env })) {
|
|
453
890
|
const machine = cachedMachineCredential({ env: options.env });
|
|
454
|
-
return machine ? { token: machine.accessToken, source: "machine" } : void 0;
|
|
891
|
+
return machine?.accessToken ? { token: machine.accessToken, source: "machine" } : void 0;
|
|
455
892
|
}
|
|
456
893
|
const envToken = (options.env ?? process.env).PRIM_TOKEN?.trim();
|
|
457
894
|
if (envToken) return { token: envToken, source: "environment" };
|
|
@@ -479,7 +916,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
479
916
|
return { token: stored, source: "token_file" };
|
|
480
917
|
}
|
|
481
918
|
function credentialFingerprint(value) {
|
|
482
|
-
return
|
|
919
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
483
920
|
}
|
|
484
921
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
485
922
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -499,7 +936,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
499
936
|
function readBoundedCredentialJson(path) {
|
|
500
937
|
let raw;
|
|
501
938
|
try {
|
|
502
|
-
raw =
|
|
939
|
+
raw = readFileSync7(path, "utf8");
|
|
503
940
|
} catch (error) {
|
|
504
941
|
return error.code === "ENOENT" ? void 0 : null;
|
|
505
942
|
}
|
|
@@ -567,9 +1004,9 @@ function decodeJwtPayload(token) {
|
|
|
567
1004
|
}
|
|
568
1005
|
|
|
569
1006
|
// src/daemon/client-instance-id.ts
|
|
570
|
-
import { randomBytes as
|
|
571
|
-
import { chmodSync as chmodSync2, lstatSync as
|
|
572
|
-
import { join as
|
|
1007
|
+
import { randomBytes as randomBytes4 } from "crypto";
|
|
1008
|
+
import { chmodSync as chmodSync2, lstatSync as lstatSync5, readFileSync as readFileSync8 } from "fs";
|
|
1009
|
+
import { join as join10 } from "path";
|
|
573
1010
|
var INSTANCE_BODY_CHARS = 43;
|
|
574
1011
|
var INSTANCE_PREFIX = "pci_";
|
|
575
1012
|
var MAX_STORED_BYTES = INSTANCE_PREFIX.length + INSTANCE_BODY_CHARS + 1;
|
|
@@ -577,63 +1014,24 @@ var MAX_STORED_BYTES = INSTANCE_PREFIX.length + INSTANCE_BODY_CHARS + 1;
|
|
|
577
1014
|
// src/lib/ingest-uploader.ts
|
|
578
1015
|
var runtimeVersion = process.env.PRIM_RUNTIME_VERSION ?? packageVersion();
|
|
579
1016
|
|
|
580
|
-
// src/lib/terminal-safe.ts
|
|
581
|
-
function replaceIsolatedSurrogates(value) {
|
|
582
|
-
let out = "";
|
|
583
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
584
|
-
const unit = value.charCodeAt(index);
|
|
585
|
-
if (unit >= 55296 && unit <= 56319) {
|
|
586
|
-
const next = value.charCodeAt(index + 1);
|
|
587
|
-
if (next >= 56320 && next <= 57343) {
|
|
588
|
-
out += value[index] + value[index + 1];
|
|
589
|
-
index += 1;
|
|
590
|
-
} else {
|
|
591
|
-
out += "\uFFFD";
|
|
592
|
-
}
|
|
593
|
-
} else if (unit >= 56320 && unit <= 57343) {
|
|
594
|
-
out += "\uFFFD";
|
|
595
|
-
} else {
|
|
596
|
-
out += value[index];
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return out;
|
|
600
|
-
}
|
|
601
|
-
function isUnsafeTerminalCodePoint(point) {
|
|
602
|
-
const c0OrC1 = point <= 31 || point >= 127 && point <= 159;
|
|
603
|
-
const bidiOrZeroWidth = point === 173 || // soft hyphen
|
|
604
|
-
point === 1564 || // Arabic letter mark
|
|
605
|
-
point === 6158 || // Mongolian vowel separator
|
|
606
|
-
point >= 8203 && point <= 8207 || // zero-width + directional marks
|
|
607
|
-
point >= 8234 && point <= 8238 || // bidi embeddings/overrides
|
|
608
|
-
point >= 8288 && point <= 8303 || // word joiner + bidi isolates/controls
|
|
609
|
-
point >= 65529 && point <= 65531 || // interlinear annotation controls
|
|
610
|
-
point === 65279 || // zero-width no-break space / BOM
|
|
611
|
-
point >= 917504 && point <= 917631;
|
|
612
|
-
return c0OrC1 || bidiOrZeroWidth;
|
|
613
|
-
}
|
|
614
|
-
function terminalSafeText(value) {
|
|
615
|
-
return Array.from(replaceIsolatedSurrogates(value)).filter((character) => !isUnsafeTerminalCodePoint(character.codePointAt(0) ?? 0)).join("");
|
|
616
|
-
}
|
|
617
|
-
var stripControlChars = terminalSafeText;
|
|
618
|
-
|
|
619
1017
|
// src/client.ts
|
|
620
|
-
var
|
|
1018
|
+
var DEFAULT_API_URL3 = "https://api.getprimitive.ai";
|
|
621
1019
|
function getSiteUrl() {
|
|
622
1020
|
return getSiteUrlForEnvironment(process.env.PRIM_API_URL);
|
|
623
1021
|
}
|
|
624
1022
|
function getSiteUrlForEnvironment(primApiUrl) {
|
|
625
|
-
return primApiUrl?.trim() ||
|
|
1023
|
+
return primApiUrl?.trim() || DEFAULT_API_URL3;
|
|
626
1024
|
}
|
|
627
1025
|
var CLI_MANAGEMENT_RESPONSE_MAX_BYTES = 2 * 1024 * 1024;
|
|
628
1026
|
|
|
629
1027
|
// src/daemon/principal.ts
|
|
630
|
-
import { createHash as
|
|
1028
|
+
import { createHash as createHash8 } from "crypto";
|
|
631
1029
|
var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
632
1030
|
function boundedClaim(value) {
|
|
633
1031
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
634
1032
|
}
|
|
635
1033
|
function sha256(value) {
|
|
636
|
-
return
|
|
1034
|
+
return createHash8("sha256").update(value).digest("hex");
|
|
637
1035
|
}
|
|
638
1036
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
639
1037
|
if (hasMachineConfiguration()) {
|
|
@@ -684,14 +1082,14 @@ function parseDaemonResponseEnvelope(value) {
|
|
|
684
1082
|
|
|
685
1083
|
// src/daemon/client.ts
|
|
686
1084
|
import { createConnection } from "net";
|
|
687
|
-
import { join as
|
|
688
|
-
var SOCK_PATH =
|
|
1085
|
+
import { join as join12 } from "path";
|
|
1086
|
+
var SOCK_PATH = join12(primConfigDirectory(), "sock");
|
|
689
1087
|
var DEFAULT_TIMEOUT_MS = 250;
|
|
690
1088
|
var nextRequestId = 1;
|
|
691
1089
|
function daemonRequest(method, params = {}, opts = {}) {
|
|
692
1090
|
const id = nextRequestId++;
|
|
693
1091
|
const caller = resolveDaemonPrincipal();
|
|
694
|
-
const
|
|
1092
|
+
const request2 = {
|
|
695
1093
|
id,
|
|
696
1094
|
method,
|
|
697
1095
|
params,
|
|
@@ -730,7 +1128,7 @@ function daemonRequest(method, params = {}, opts = {}) {
|
|
|
730
1128
|
});
|
|
731
1129
|
socket.on("connect", () => {
|
|
732
1130
|
try {
|
|
733
|
-
socket?.write(`${JSON.stringify(
|
|
1131
|
+
socket?.write(`${JSON.stringify(request2)}
|
|
734
1132
|
`);
|
|
735
1133
|
} catch {
|
|
736
1134
|
clearTimeout(timer);
|
|
@@ -776,22 +1174,38 @@ function daemonRequest(method, params = {}, opts = {}) {
|
|
|
776
1174
|
|
|
777
1175
|
// src/lib/activation.ts
|
|
778
1176
|
import { execFileSync as execFileSync4 } from "child_process";
|
|
779
|
-
import { existsSync as
|
|
1177
|
+
import { existsSync as existsSync6, readFileSync as readFileSync11 } from "fs";
|
|
780
1178
|
import { homedir as homedir2 } from "os";
|
|
781
|
-
import { join as
|
|
1179
|
+
import { join as join14 } from "path";
|
|
1180
|
+
|
|
1181
|
+
// src/lib/cloud-prepared.ts
|
|
1182
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync5, readFileSync as readFileSync10, rmSync as rmSync8, writeFileSync as writeFileSync4 } from "fs";
|
|
1183
|
+
import { join as join13 } from "path";
|
|
1184
|
+
var CLOUD_PREPARED_MARKER = "cloud-prepared";
|
|
1185
|
+
function cloudPreparedPath(env = process.env) {
|
|
1186
|
+
return join13(primConfigDirectory({ env }), CLOUD_PREPARED_MARKER);
|
|
1187
|
+
}
|
|
1188
|
+
function isPreparedCloudSession(env = process.env) {
|
|
1189
|
+
if (!isClaudeCloudSession(env)) return false;
|
|
1190
|
+
try {
|
|
1191
|
+
return existsSync5(cloudPreparedPath(env));
|
|
1192
|
+
} catch {
|
|
1193
|
+
return false;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
782
1196
|
|
|
783
1197
|
// src/lib/demo-pause.ts
|
|
784
|
-
import { execFileSync as
|
|
1198
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
785
1199
|
var DEMO_PAUSE_MS = 30 * 6e4;
|
|
786
1200
|
var PAUSE_KEY = "prim.demoPause";
|
|
787
|
-
var
|
|
1201
|
+
var GIT_TIMEOUT_MS2 = 1e3;
|
|
788
1202
|
function git(cwd, args) {
|
|
789
1203
|
try {
|
|
790
|
-
return
|
|
1204
|
+
return execFileSync3("git", args, {
|
|
791
1205
|
cwd,
|
|
792
1206
|
encoding: "utf-8",
|
|
793
1207
|
stdio: ["ignore", "pipe", "ignore"],
|
|
794
|
-
timeout:
|
|
1208
|
+
timeout: GIT_TIMEOUT_MS2
|
|
795
1209
|
});
|
|
796
1210
|
} catch {
|
|
797
1211
|
return void 0;
|
|
@@ -799,10 +1213,10 @@ function git(cwd, args) {
|
|
|
799
1213
|
}
|
|
800
1214
|
function setLocal(cwd, key, value) {
|
|
801
1215
|
try {
|
|
802
|
-
|
|
1216
|
+
execFileSync3(
|
|
803
1217
|
"git",
|
|
804
1218
|
value === null ? ["config", "--local", "--unset", key] : ["config", "--local", key, value],
|
|
805
|
-
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout:
|
|
1219
|
+
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout: GIT_TIMEOUT_MS2 }
|
|
806
1220
|
);
|
|
807
1221
|
} catch (error) {
|
|
808
1222
|
if (value !== null || error.status !== 5) throw error;
|
|
@@ -841,56 +1255,9 @@ function healDemoPause(cwd, pause, options = {}) {
|
|
|
841
1255
|
}
|
|
842
1256
|
}
|
|
843
1257
|
|
|
844
|
-
// src/lib/git.ts
|
|
845
|
-
import { execFileSync as execFileSync3 } from "child_process";
|
|
846
|
-
import { createHash as createHash7 } from "crypto";
|
|
847
|
-
import { lstatSync as lstatSync5, realpathSync } from "fs";
|
|
848
|
-
import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute3, relative, resolve as resolve2, sep } from "path";
|
|
849
|
-
var GIT_TIMEOUT_MS2 = 1e3;
|
|
850
|
-
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
851
|
-
var GITHUB_REPOSITORY_PATTERN = /^[A-Za-z0-9._-]{1,100}$/u;
|
|
852
|
-
function gitToplevel(cwd) {
|
|
853
|
-
try {
|
|
854
|
-
return execFileSync3("git", ["rev-parse", "--show-toplevel"], {
|
|
855
|
-
cwd,
|
|
856
|
-
encoding: "utf-8",
|
|
857
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
858
|
-
timeout: GIT_TIMEOUT_MS2
|
|
859
|
-
}).trim();
|
|
860
|
-
} catch {
|
|
861
|
-
return null;
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
function gitValue(cwd, args) {
|
|
865
|
-
try {
|
|
866
|
-
const value = execFileSync3("git", args, {
|
|
867
|
-
cwd,
|
|
868
|
-
encoding: "utf-8",
|
|
869
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
870
|
-
timeout: GIT_TIMEOUT_MS2
|
|
871
|
-
}).trim();
|
|
872
|
-
return value || void 0;
|
|
873
|
-
} catch {
|
|
874
|
-
return void 0;
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
function githubRepositoryFullName(cwd) {
|
|
878
|
-
const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
|
|
879
|
-
if (!remote || [...remote].some((char) => char < " " || char === "\x7F")) return null;
|
|
880
|
-
const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
|
|
881
|
-
remote
|
|
882
|
-
);
|
|
883
|
-
const path = match?.[1] ?? match?.[2];
|
|
884
|
-
if (!path) return null;
|
|
885
|
-
const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
|
|
886
|
-
if (parts.length !== 2 || !GITHUB_OWNER_PATTERN.test(parts[0] ?? "") || !GITHUB_REPOSITORY_PATTERN.test(parts[1] ?? "")) {
|
|
887
|
-
return null;
|
|
888
|
-
}
|
|
889
|
-
return `${parts[0]}/${parts[1]}`;
|
|
890
|
-
}
|
|
891
|
-
|
|
892
1258
|
// src/lib/activation.ts
|
|
893
1259
|
var PRIM_ACTIVE_KEY = "prim.active";
|
|
1260
|
+
var PRIM_ACTIVE_DECLARATION = "PRIM_ACTIVE";
|
|
894
1261
|
var PRIM_REPO_SYNC_ID_KEY = "prim.repoSyncId";
|
|
895
1262
|
var PRIM_REPO_SYNC_REPOSITORY_KEY = "prim.repoSyncRepository";
|
|
896
1263
|
var PRIM_REPO_BINDING_STATE_KEY = "prim.repoBindingState";
|
|
@@ -947,14 +1314,13 @@ function repositoryBindingState(cwd) {
|
|
|
947
1314
|
if (marker !== void 0) return "invalid";
|
|
948
1315
|
return rawId === void 0 ? "unbound" : repoSyncId(cwd) ? void 0 : "unbound";
|
|
949
1316
|
}
|
|
950
|
-
function
|
|
951
|
-
const root = gitToplevel(cwd);
|
|
1317
|
+
function projectPrimInstallAt(root) {
|
|
952
1318
|
if (root === null || root === homedir2()) return false;
|
|
953
1319
|
for (const rel of PROJECT_INSTALL_FILES) {
|
|
954
|
-
const path =
|
|
955
|
-
if (
|
|
1320
|
+
const path = join14(root, rel);
|
|
1321
|
+
if (existsSync6(path)) {
|
|
956
1322
|
try {
|
|
957
|
-
const content =
|
|
1323
|
+
const content = readFileSync11(path, "utf-8");
|
|
958
1324
|
if (PRIM_HOOK_BINS.some((bin) => content.includes(bin))) return true;
|
|
959
1325
|
} catch {
|
|
960
1326
|
}
|
|
@@ -962,12 +1328,27 @@ function hasProjectPrimInstall(cwd) {
|
|
|
962
1328
|
}
|
|
963
1329
|
return false;
|
|
964
1330
|
}
|
|
1331
|
+
var UNPARSED_OPT_OUT = /"PRIM_ACTIVE"\s*:\s*"\s*false\s*"/u;
|
|
1332
|
+
function declaresInactiveAt(root) {
|
|
1333
|
+
if (root === null || root === homedir2()) return false;
|
|
1334
|
+
if (checkoutDeclaration(root, PRIM_ACTIVE_DECLARATION)?.trim() === "false") return true;
|
|
1335
|
+
const text = checkoutSettingsText(root);
|
|
1336
|
+
if (text === null || !UNPARSED_OPT_OUT.test(text)) return false;
|
|
1337
|
+
try {
|
|
1338
|
+
JSON.parse(text);
|
|
1339
|
+
return false;
|
|
1340
|
+
} catch {
|
|
1341
|
+
return true;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
965
1344
|
function isRepoActiveForCapture(cwd, options = {}) {
|
|
966
1345
|
let { active: flag, pause } = readCaptureConfig(cwd);
|
|
967
1346
|
if (pause && healDemoPause(cwd, pause, options)) flag = repoActiveFlag(cwd);
|
|
968
1347
|
if (flag === "true") return true;
|
|
969
1348
|
if (flag === "false") return false;
|
|
970
|
-
|
|
1349
|
+
const root = gitToplevel(cwd);
|
|
1350
|
+
if (declaresInactiveAt(root)) return false;
|
|
1351
|
+
return projectPrimInstallAt(root) || isPreparedCloudSession() && root !== null;
|
|
971
1352
|
}
|
|
972
1353
|
function decisionIngestionStatus(cwd) {
|
|
973
1354
|
return isRepoActiveForCapture(cwd) ? "enabled" : "disabled";
|
|
@@ -1112,7 +1493,7 @@ function readPackageVersion() {
|
|
|
1112
1493
|
];
|
|
1113
1494
|
for (const path of candidates) {
|
|
1114
1495
|
try {
|
|
1115
|
-
const pkg = JSON.parse(
|
|
1496
|
+
const pkg = JSON.parse(readFileSync12(path, "utf-8"));
|
|
1116
1497
|
if (pkg.version) {
|
|
1117
1498
|
return pkg.version;
|
|
1118
1499
|
}
|