@primitive.ai/prim 0.1.0-alpha.113 → 0.1.0-alpha.114

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 CHANGED
@@ -362,6 +362,21 @@ creating it; do not guess.
362
362
  own — `<child>` depends on `<parent>`. Both are idempotent and refuse any link that
363
363
  would create a cycle (exit 2); an unresolved id exits 4.
364
364
 
365
+ ### Bash script coverage
366
+
367
+ Conflict Gates also inspect Bash writes expressed as Python `-c`, Node.js
368
+ `-e`/`--eval`, or quoted stdin heredocs. Static paths passed to Python `Path`
369
+ writes or writable `open`, and Node.js `fs`/`fs.promises` writeFile/appendFile
370
+ calls, participate in the existing preflight check. Simple path variables and
371
+ direct import aliases are supported. Analysis never executes the script.
372
+
373
+ Script coverage remains unverified: dynamic paths, reassignment, nested scopes,
374
+ directory changes, wrappers, external scripts, and unsupported syntax can hide
375
+ other writes. Unknown commands display a “not verified” warning in active repos;
376
+ recognized read-only commands stay quiet. Known conflicts still prompt or block
377
+ according to the existing policy. Embedded parsing stops at 64 KiB, targets are
378
+ bounded to 32, and shell proposal truncation at 6,144 bytes is disclosed.
379
+
365
380
  ### Reconcile
366
381
 
367
382
  ```bash
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  analyzeShellTargets,
6
6
  extractFileTargets
7
- } from "./chunk-XV6C5ZV2.js";
7
+ } from "./chunk-RUUSUF43.js";
8
8
  import {
9
9
  scrubEnvironmentPaths
10
10
  } from "./chunk-3KKEV7VI.js";
@@ -350,6 +350,213 @@ function demoteForMode(verdict, mode) {
350
350
 
351
351
  // src/hooks/shell-targets.ts
352
352
  import { parse } from "unbash";
353
+
354
+ // src/hooks/script-targets.ts
355
+ import { parser as javascript } from "@lezer/javascript";
356
+ import { parser as python } from "@lezer/python";
357
+ var MAX_SCRIPT_BYTES = 65536;
358
+ var WRITERS = /* @__PURE__ */ new Set(["writeFile", "writeFileSync", "appendFile", "appendFileSync"]);
359
+ var FS_MODULES = /* @__PURE__ */ new Set(["fs", "node:fs", "fs/promises", "node:fs/promises"]);
360
+ var IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
361
+ function children(node) {
362
+ const nodes = [];
363
+ for (let child = node?.firstChild; child; child = child.nextSibling) nodes.push(child);
364
+ return nodes;
365
+ }
366
+ function literal(text) {
367
+ const quote = text[0];
368
+ if (quote !== '"' && quote !== "'" || text.at(-1) !== quote) return;
369
+ let value = "";
370
+ for (let i = 1; i < text.length - 1; i++) {
371
+ const char = text[i];
372
+ if (char === quote || char === "\n" || char === "\r") return;
373
+ if (char !== "\\") {
374
+ value += char;
375
+ continue;
376
+ }
377
+ const escaped = text[++i];
378
+ const simple = {
379
+ "\\": "\\",
380
+ "'": "'",
381
+ '"': '"',
382
+ n: "\n",
383
+ r: "\r",
384
+ t: " "
385
+ };
386
+ if (Object.hasOwn(simple, escaped)) value += simple[escaped];
387
+ else if (escaped === "u" || escaped === "x") {
388
+ const length = escaped === "u" ? 4 : 2;
389
+ const digits = text.slice(i + 1, i + 1 + length);
390
+ if (digits.length !== length || !/^[\da-f]+$/i.test(digits)) return;
391
+ value += String.fromCharCode(Number.parseInt(digits, 16));
392
+ i += length;
393
+ } else return;
394
+ }
395
+ return value;
396
+ }
397
+ function scriptWriteTargets(source, language) {
398
+ if (Buffer.byteLength(source) > MAX_SCRIPT_BYTES) return [];
399
+ try {
400
+ const tree = (language === "python" ? python : javascript).parse(source);
401
+ const cursor = tree.cursor();
402
+ let nodes = 0;
403
+ do {
404
+ if (++nodes > 2e4 || cursor.type.isError) return [];
405
+ if (language === "javascript" && cursor.name === "VariableDefinition" && source.slice(cursor.from, cursor.to) === "require")
406
+ return [];
407
+ if (["PropertyName", "VariableName", "VariableDefinition"].includes(cursor.name) && ["chdir", "exec", "eval"].includes(source.slice(cursor.from, cursor.to)))
408
+ return [];
409
+ } while (cursor.next());
410
+ const bindings = /* @__PURE__ */ new Map();
411
+ if (language === "python") bindings.set("open", { kind: "symbol", value: "open" });
412
+ else bindings.set("require", { kind: "symbol", value: "require" });
413
+ if (language === "javascript") {
414
+ for (const statement of children(tree.topNode)) {
415
+ if (statement.name !== "FunctionDeclaration") continue;
416
+ const name = statement.getChild("VariableDefinition");
417
+ if (name) bindings.set(source.slice(name.from, name.to), void 0);
418
+ }
419
+ }
420
+ const paths = /* @__PURE__ */ new Set();
421
+ const text = (node) => source.slice(node.from, node.to);
422
+ const symbol = (value) => ({ kind: "symbol", value });
423
+ const bind = (name, value) => {
424
+ bindings.set(name, bindings.has(name) ? void 0 : value);
425
+ };
426
+ const forget = () => {
427
+ for (const name of bindings.keys()) bindings.set(name, void 0);
428
+ };
429
+ const args = (node) => children(node.getChild("ArgList")).filter((child) => !["(", ")", ","].includes(child.name));
430
+ const resolveValue = (node) => {
431
+ if (!node) return;
432
+ if (node.name === "String") {
433
+ const value = literal(text(node));
434
+ return value === void 0 ? void 0 : { kind: "string", value };
435
+ }
436
+ if (node.name === "VariableName") return bindings.get(text(node));
437
+ if (node.name === "MemberExpression") {
438
+ const [base, dot, property] = children(node);
439
+ if (dot?.name !== "." || property?.name !== "PropertyName") return;
440
+ const owner = resolveValue(base);
441
+ const name = text(property);
442
+ if (owner?.kind === "symbol") {
443
+ if (owner.value === "pathlib" && name === "Path") return symbol("Path");
444
+ if (owner.value === "fs" && name === "promises") return symbol("fs");
445
+ if (owner.value === "fs" && WRITERS.has(name)) return symbol("write");
446
+ }
447
+ }
448
+ if (node.name === "CallExpression") {
449
+ const callee = resolveValue(node.firstChild ?? void 0);
450
+ const parameters = args(node);
451
+ const first = resolveValue(parameters[0]);
452
+ if (callee?.kind === "symbol" && first?.kind === "string" && parameters.length === 1) {
453
+ if (callee.value === "Path") return { kind: "path", value: first.value };
454
+ if (callee.value === "require" && FS_MODULES.has(first.value)) return symbol("fs");
455
+ }
456
+ }
457
+ };
458
+ const inspect = (node) => {
459
+ if (/Function|Class|Lambda|ArrowFunction/.test(node.name)) return;
460
+ if (["AssignmentExpression", "UpdateExpression", "NamedExpression"].includes(node.name) || node.name.includes("Comprehension")) {
461
+ forget();
462
+ return;
463
+ }
464
+ if (node.name === "CallExpression") {
465
+ const calleeNode = node.firstChild;
466
+ if (!calleeNode) return;
467
+ const callee = resolveValue(calleeNode);
468
+ const parameters = args(node);
469
+ const first = resolveValue(parameters[0]);
470
+ if (callee?.kind === "symbol" && callee.value === "write" && first?.kind === "string")
471
+ paths.add(first.value);
472
+ if (callee?.kind === "symbol" && callee.value === "open") {
473
+ const modeAt = parameters.findIndex(
474
+ (p) => p.name === "VariableName" && text(p) === "mode"
475
+ );
476
+ const mode = resolveValue(modeAt < 0 ? parameters[1] : parameters[modeAt + 2]);
477
+ if (first && ["string", "path"].includes(first.kind) && mode?.kind === "string" && /^[rwax][bt+]*$/.test(mode.value) && /[wax+]/.test(mode.value))
478
+ paths.add(first.value);
479
+ }
480
+ if (calleeNode.name === "MemberExpression") {
481
+ const [base, dot, property] = children(calleeNode);
482
+ const owner = resolveValue(base);
483
+ if (dot?.name === "." && property && owner?.kind === "path" && ["write_text", "write_bytes"].includes(text(property)))
484
+ paths.add(owner.value);
485
+ }
486
+ }
487
+ for (const child of children(node)) inspect(child);
488
+ };
489
+ const importStatement = (node) => {
490
+ const statement = text(node).trim().replace(/;$/, "");
491
+ if (language === "python") {
492
+ const module = /^import pathlib(?: as (\w+))?$/.exec(statement);
493
+ const path = /^from pathlib import Path(?: as (\w+))?$/.exec(statement);
494
+ if (module) bind(module[1] ?? "pathlib", symbol("pathlib"));
495
+ else if (path) bind(path[1] ?? "Path", symbol("Path"));
496
+ else forget();
497
+ return;
498
+ }
499
+ const sourceNode = node.getChild("String");
500
+ if (!sourceNode || !FS_MODULES.has(literal(text(sourceNode)) ?? "")) {
501
+ forget();
502
+ return;
503
+ }
504
+ const group = node.getChild("ImportGroup");
505
+ if (group) {
506
+ for (const entry of text(group).slice(1, -1).split(",")) {
507
+ const match = /^\s*(\w+)(?:\s+as\s+(\w+))?\s*$/.exec(entry);
508
+ if (match)
509
+ bind(match[2] ?? match[1], WRITERS.has(match[1]) ? symbol("write") : void 0);
510
+ }
511
+ }
512
+ const name = node.getChild("VariableDefinition");
513
+ if (name) bind(text(name), symbol("fs"));
514
+ };
515
+ const statements = children(tree.topNode).flatMap(
516
+ (statement) => statement.name === "StatementGroup" ? children(statement) : [statement]
517
+ );
518
+ for (const statement of statements) {
519
+ if (["Comment", "LineComment", "BlockComment", ";"].includes(statement.name)) continue;
520
+ if (["ImportStatement", "ImportDeclaration"].includes(statement.name)) {
521
+ importStatement(statement);
522
+ continue;
523
+ }
524
+ if (statement.name === "AssignStatement" || statement.name === "VariableDeclaration") {
525
+ const parts = children(statement).filter(
526
+ (p) => !["const", "let", "var", ";"].includes(p.name)
527
+ );
528
+ const [target, operator, expression] = parts;
529
+ if (parts.length !== 3 || !target || !operator || text(operator) !== "=") {
530
+ forget();
531
+ continue;
532
+ }
533
+ inspect(expression);
534
+ if (["VariableName", "VariableDefinition"].includes(target.name) && IDENTIFIER.test(text(target)))
535
+ bind(text(target), resolveValue(expression));
536
+ else if (target.name === "ObjectPattern" && resolveValue(expression)?.kind === "symbol" && resolveValue(expression)?.value === "fs") {
537
+ for (const entry of text(target).slice(1, -1).split(",")) {
538
+ const match = /^\s*(\w+)(?:\s*:\s*(\w+))?\s*$/.exec(entry);
539
+ if (match)
540
+ bind(match[2] ?? match[1], WRITERS.has(match[1]) ? symbol("write") : void 0);
541
+ else forget();
542
+ }
543
+ } else forget();
544
+ } else if (statement.name === "ExpressionStatement") inspect(statement);
545
+ else if (statement.name === "WithStatement") {
546
+ for (const child of children(statement)) {
547
+ if (child.name === "as") forget();
548
+ if (child.name === "CallExpression") inspect(child);
549
+ }
550
+ forget();
551
+ } else forget();
552
+ }
553
+ return [...paths];
554
+ } catch {
555
+ return [];
556
+ }
557
+ }
558
+
559
+ // src/hooks/shell-targets.ts
353
560
  var OUTPUT_REDIRECTS = /* @__PURE__ */ new Set([">", ">>", ">|", "&>", "&>>"]);
354
561
  var READ_ONLY_COMMANDS = new Set(": cat echo false printf pwd test true [".split(" "));
355
562
  var SINKS = /* @__PURE__ */ new Set(["/dev/null", "/dev/stdout", "/dev/stderr"]);
@@ -865,6 +1072,39 @@ function operands(words, short = "", long = /* @__PURE__ */ new Set()) {
865
1072
  }
866
1073
  return result;
867
1074
  }
1075
+ function embeddedScript(command, name, state) {
1076
+ unknown(state);
1077
+ if (state.cwdUncertain || command.prefix.length) return;
1078
+ const words = command.suffix.map(
1079
+ (word) => (word.parts ? word.parts.every(staticPart) : staticWord(word) !== void 0) && !word.value.includes("\0") ? word.value : void 0
1080
+ );
1081
+ if (words.some((word) => word === void 0)) return;
1082
+ const args = words;
1083
+ const language = name === "node" ? "javascript" : "python";
1084
+ if (language === "javascript" && /^--input-type=(module|commonjs)$/.test(args[0] ?? ""))
1085
+ args.shift();
1086
+ const inlineFlag = language === "python" ? ["-c"] : ["-e", "--eval"];
1087
+ let source;
1088
+ if (args.length === 2 && inlineFlag.includes(args[0])) source = args[1];
1089
+ else if (args.length === 0 || args.length === 1 && args[0] === "-") {
1090
+ const input = command.redirects.filter((redirect) => redirect.operator.startsWith("<"));
1091
+ const heredoc = input[0];
1092
+ if (input.length === 1 && heredoc.operator === "<<" && heredoc.heredocQuoted === true)
1093
+ source = heredoc.content;
1094
+ }
1095
+ if (source !== void 0)
1096
+ for (const path of scriptWriteTargets(source, language)) state.paths.add(path);
1097
+ }
1098
+ function changesDirectory(value) {
1099
+ if (Array.isArray(value)) return value.some(changesDirectory);
1100
+ if (!value || typeof value !== "object") return false;
1101
+ const node = value;
1102
+ if (node.type === "Command" && ["cd", "pushd", "popd", "eval", "source", "."].includes(
1103
+ staticWord(value.name) ?? ""
1104
+ ))
1105
+ return true;
1106
+ return Object.values(node).some(changesDirectory);
1107
+ }
868
1108
  function visitCommand(command, state) {
869
1109
  const name = staticWord(command.name);
870
1110
  const literalApplyPatch = name === "apply_patch" && !command.redirects.some((redirect) => redirect.operator === "<<-");
@@ -873,7 +1113,9 @@ function visitCommand(command, state) {
873
1113
  } else {
874
1114
  for (const redirect of command.redirects) visitRedirect(redirect, state);
875
1115
  }
876
- if (!name || name.includes("/") || !literalApplyPatch && command.suffix.some((word) => !staticWord(word))) {
1116
+ if (name && (name === "node" || /^python(?:3(?:\.\d+)?)?$/.test(name))) {
1117
+ embeddedScript(command, name, state);
1118
+ } else if (!name || name.includes("/") || !literalApplyPatch && command.suffix.some((word) => !staticWord(word))) {
877
1119
  unknown(state);
878
1120
  } else if (literalApplyPatch) {
879
1121
  } else if (!READ_ONLY_COMMANDS.has(name) && !knownNoWorktreeMutation(name, staticArgs(command.suffix))) {
@@ -932,7 +1174,8 @@ function analyzeShellTargets(source) {
932
1174
  paths: /* @__PURE__ */ new Set(),
933
1175
  complete: !script.errors?.length,
934
1176
  mutation: false,
935
- definiteEdit: false
1177
+ definiteEdit: false,
1178
+ cwdUncertain: changesDirectory(script)
936
1179
  };
937
1180
  for (const statement of script.commands) visitStatement(statement, state);
938
1181
  if (script.errors?.length) state.mutation = true;