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