cadre-ai 2.0.0 → 2.2.0

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.
@@ -39,7 +39,7 @@ __export(cadre_lsp_review_exports, {
39
39
  runReview: () => runReview
40
40
  });
41
41
  module.exports = __toCommonJS(cadre_lsp_review_exports);
42
- var import_node_path7 = __toESM(require("node:path"));
42
+ var import_node_path8 = __toESM(require("node:path"));
43
43
 
44
44
  // src/guards.ts
45
45
  function isRecord(value) {
@@ -412,11 +412,127 @@ function commandAvailability(command) {
412
412
  }
413
413
 
414
414
  // src/lsp/review/run-review.ts
415
- var import_node_fs4 = __toESM(require("node:fs"));
416
- var import_node_path6 = __toESM(require("node:path"));
415
+ var import_node_fs5 = __toESM(require("node:fs"));
416
+ var import_node_path7 = __toESM(require("node:path"));
417
417
 
418
- // src/lsp/ignore-policy.ts
418
+ // src/infrastructure/project-control-file.ts
419
+ var import_node_fs2 = __toESM(require("node:fs"));
419
420
  var import_node_path3 = __toESM(require("node:path"));
421
+ function errorMessage2(error) {
422
+ return error instanceof Error ? error.message : String(error);
423
+ }
424
+ function canonicalProjectRoot(root) {
425
+ try {
426
+ const canonical = import_node_fs2.default.realpathSync(import_node_path3.default.resolve(root));
427
+ return import_node_fs2.default.statSync(canonical).isDirectory() ? canonical : null;
428
+ } catch {
429
+ return null;
430
+ }
431
+ }
432
+ function requestedSegments(requested) {
433
+ if (!requested || requested.includes("\0") || import_node_path3.default.posix.isAbsolute(requested) || import_node_path3.default.win32.isAbsolute(requested)) {
434
+ return null;
435
+ }
436
+ const segments = requested.replace(/\\/g, "/").split("/");
437
+ if (segments.some((segment) => !segment || segment === "." || segment === "..")) return null;
438
+ return segments;
439
+ }
440
+ function lstatIfPresent(file) {
441
+ try {
442
+ return import_node_fs2.default.lstatSync(file);
443
+ } catch (error) {
444
+ if (error.code === "ENOENT") return null;
445
+ throw error;
446
+ }
447
+ }
448
+ function safeExistingControlPlane(controlDir) {
449
+ try {
450
+ const stat = lstatIfPresent(controlDir);
451
+ if (!stat) return null;
452
+ if (stat.isSymbolicLink()) return { ok: false, error: "Cadre config directory must not be a symbolic link" };
453
+ if (!stat.isDirectory()) return { ok: false, error: "Cadre config directory is not a directory" };
454
+ return null;
455
+ } catch (error) {
456
+ return { ok: false, error: `Cannot inspect Cadre config directory: ${errorMessage2(error)}` };
457
+ }
458
+ }
459
+ function safeExistingConfigFile(file) {
460
+ try {
461
+ const stat = lstatIfPresent(file);
462
+ if (!stat) return null;
463
+ if (stat.isSymbolicLink()) return { ok: false, error: "Cadre config file must not be a symbolic link" };
464
+ if (!stat.isFile()) return { ok: false, error: "Cadre config path is not a regular file" };
465
+ return null;
466
+ } catch (error) {
467
+ return { ok: false, error: `Cannot inspect Cadre config file: ${errorMessage2(error)}` };
468
+ }
469
+ }
470
+ function resolveProjectControlJsonPath(root, requested, defaultRelative) {
471
+ const canonicalRoot = canonicalProjectRoot(root);
472
+ if (!canonicalRoot) return { ok: false, error: "Project root does not exist or is not a directory" };
473
+ const relativeInput = requested || defaultRelative;
474
+ const segments = requestedSegments(relativeInput);
475
+ const defaultSegments = requestedSegments(defaultRelative);
476
+ const defaultName = defaultSegments?.length === 2 && defaultSegments[0] === "cadre" ? defaultSegments[1] : "";
477
+ const defaultStem = import_node_path3.default.basename(defaultName, ".json");
478
+ const requestedName = segments?.[1] || "";
479
+ const namespaceMatch = requestedName === defaultName || requestedName.startsWith(`${defaultStem}-`) && requestedName.length > `${defaultStem}-.json`.length;
480
+ if (!segments || segments.length !== 2 || segments[0] !== "cadre" || import_node_path3.default.extname(requestedName).toLowerCase() !== ".json" || !namespaceMatch) {
481
+ return {
482
+ ok: false,
483
+ error: `Cadre config must be ${defaultRelative} or a relative cadre/${defaultStem}-*.json path without traversal`
484
+ };
485
+ }
486
+ const controlDir = import_node_path3.default.join(canonicalRoot, "cadre");
487
+ const file = import_node_path3.default.join(controlDir, segments[1]);
488
+ const controlError = safeExistingControlPlane(controlDir);
489
+ if (controlError) return controlError;
490
+ const fileError = safeExistingConfigFile(file);
491
+ if (fileError) return fileError;
492
+ return {
493
+ ok: true,
494
+ root: canonicalRoot,
495
+ file,
496
+ relative: `cadre/${segments[1]}`,
497
+ controlDir
498
+ };
499
+ }
500
+ function resolveOwnedProjectControlJsonPath(workspaceRoot, ownerRoot, requested, defaultRelative) {
501
+ const canonicalWorkspace = canonicalProjectRoot(workspaceRoot);
502
+ if (!canonicalWorkspace) return { ok: false, error: "Review workspace root does not exist or is not a directory" };
503
+ const owned = resolveProjectControlJsonPath(ownerRoot, requested, defaultRelative);
504
+ if (!owned.ok) return owned;
505
+ return { ...owned, workspaceRoot: canonicalWorkspace };
506
+ }
507
+ function readProjectControlJson(resolved) {
508
+ let descriptor = null;
509
+ try {
510
+ const pathStat = import_node_fs2.default.lstatSync(resolved.file);
511
+ if (pathStat.isSymbolicLink() || !pathStat.isFile()) {
512
+ return { ok: false, error: "Cadre config path is not a regular non-symlink file" };
513
+ }
514
+ const noFollow = import_node_fs2.default.constants.O_NOFOLLOW || 0;
515
+ descriptor = import_node_fs2.default.openSync(resolved.file, import_node_fs2.default.constants.O_RDONLY | noFollow);
516
+ const openStat = import_node_fs2.default.fstatSync(descriptor);
517
+ if (!openStat.isFile() || openStat.dev !== pathStat.dev || openStat.ino !== pathStat.ino) {
518
+ return { ok: false, error: "Cadre config file changed during secure open" };
519
+ }
520
+ const value = JSON.parse(import_node_fs2.default.readFileSync(descriptor, "utf8"));
521
+ return { ok: true, value };
522
+ } catch (error) {
523
+ return { ok: false, error: `Cannot read Cadre config: ${errorMessage2(error)}` };
524
+ } finally {
525
+ if (descriptor !== null) {
526
+ try {
527
+ import_node_fs2.default.closeSync(descriptor);
528
+ } catch {
529
+ }
530
+ }
531
+ }
532
+ }
533
+
534
+ // src/lsp/ignore-policy.ts
535
+ var import_node_path4 = __toESM(require("node:path"));
420
536
  var DEFAULT_IGNORES = /* @__PURE__ */ new Set([
421
537
  ".git",
422
538
  ".hg",
@@ -468,16 +584,16 @@ var DEFAULT_IGNORE_PATHS = [
468
584
  "plugins/cadre-antigravity"
469
585
  ];
470
586
  function normalizeRel(file) {
471
- return file.split(import_node_path3.default.sep).join("/");
587
+ return file.split(import_node_path4.default.sep).join("/");
472
588
  }
473
589
  function shouldIgnore(root, fullPath, name) {
474
590
  if (DEFAULT_IGNORES.has(name)) return true;
475
- const rel = normalizeRel(import_node_path3.default.relative(root, fullPath));
591
+ const rel = normalizeRel(import_node_path4.default.relative(root, fullPath));
476
592
  return DEFAULT_IGNORE_PATHS.some(
477
593
  (ignored) => rel === ignored || rel.startsWith(`${ignored}/`)
478
594
  );
479
595
  }
480
- function isIgnoredFile(root, file) {
596
+ function isIgnoredFile(_root, file) {
481
597
  const rel = normalizeRel(file);
482
598
  if (rel.split("/").some((part) => DEFAULT_IGNORES.has(part))) return true;
483
599
  return DEFAULT_IGNORE_PATHS.some(
@@ -486,8 +602,8 @@ function isIgnoredFile(root, file) {
486
602
  }
487
603
 
488
604
  // src/lsp/review/git-diff.ts
489
- var import_node_fs2 = __toESM(require("node:fs"));
490
- var import_node_path4 = __toESM(require("node:path"));
605
+ var import_node_fs3 = __toESM(require("node:fs"));
606
+ var import_node_path5 = __toESM(require("node:path"));
491
607
  var import_node_child_process3 = require("node:child_process");
492
608
  function runGit(root, args) {
493
609
  const result = (0, import_node_child_process3.spawnSync)("git", args, { cwd: root, encoding: "utf8" });
@@ -518,7 +634,7 @@ function changedEntries(root, base, head) {
518
634
  kind,
519
635
  path: file || "",
520
636
  oldPath,
521
- exists: file ? import_node_fs2.default.existsSync(import_node_path4.default.join(root, file)) : false
637
+ exists: file ? import_node_fs3.default.existsSync(import_node_path5.default.join(root, file)) : false
522
638
  };
523
639
  }).filter((entry) => Boolean(entry.path) && !isIgnoredFile(root, entry.path));
524
640
  }
@@ -585,8 +701,8 @@ function changedSymbolCandidates(root, base, head, entry) {
585
701
  }
586
702
 
587
703
  // src/lsp/review/scanners.ts
588
- var import_node_fs3 = __toESM(require("node:fs"));
589
- var import_node_path5 = __toESM(require("node:path"));
704
+ var import_node_fs4 = __toESM(require("node:fs"));
705
+ var import_node_path6 = __toESM(require("node:path"));
590
706
  var import_node_url2 = require("node:url");
591
707
  function flattenSymbols(symbols, out = []) {
592
708
  if (!Array.isArray(symbols)) return out;
@@ -603,8 +719,8 @@ function escapeRegExp(value) {
603
719
  function serverFileMatch(file, server) {
604
720
  const extensionSet = new Set(server.extensions || []);
605
721
  const filenameSet = new Set((server.filenames || []).map((name) => name.toLowerCase()));
606
- const ext = import_node_path5.default.extname(file);
607
- const basename = import_node_path5.default.basename(file).toLowerCase();
722
+ const ext = import_node_path6.default.extname(file);
723
+ const basename = import_node_path6.default.basename(file).toLowerCase();
608
724
  return extensionSet.has(ext) || filenameSet.has(basename);
609
725
  }
610
726
  function scanTextReferences(root, symbol, changedPathSet, server) {
@@ -615,12 +731,12 @@ function scanTextReferences(root, symbol, changedPathSet, server) {
615
731
  function visit(dir) {
616
732
  let entries;
617
733
  try {
618
- entries = import_node_fs3.default.readdirSync(dir, { withFileTypes: true });
734
+ entries = import_node_fs4.default.readdirSync(dir, { withFileTypes: true });
619
735
  } catch {
620
736
  return;
621
737
  }
622
738
  for (const entry of entries) {
623
- const full = import_node_path5.default.join(dir, entry.name);
739
+ const full = import_node_path6.default.join(dir, entry.name);
624
740
  if (shouldIgnore(root, full, entry.name)) continue;
625
741
  if (entry.isDirectory()) {
626
742
  visit(full);
@@ -629,17 +745,17 @@ function scanTextReferences(root, symbol, changedPathSet, server) {
629
745
  }
630
746
  if (!entry.isFile()) continue;
631
747
  if (!serverFileMatch(full, server)) continue;
632
- if (changedPathSet.has(import_node_path5.default.resolve(full))) continue;
748
+ if (changedPathSet.has(import_node_path6.default.resolve(full))) continue;
633
749
  let stat;
634
750
  try {
635
- stat = import_node_fs3.default.statSync(full);
751
+ stat = import_node_fs4.default.statSync(full);
636
752
  } catch {
637
753
  continue;
638
754
  }
639
755
  if (stat.size > MAX_SCAN_FILE_BYTES) continue;
640
756
  let text;
641
757
  try {
642
- text = import_node_fs3.default.readFileSync(full, "utf8");
758
+ text = import_node_fs4.default.readFileSync(full, "utf8");
643
759
  } catch {
644
760
  continue;
645
761
  }
@@ -649,7 +765,7 @@ function scanTextReferences(root, symbol, changedPathSet, server) {
649
765
  if (!pattern.test(line)) continue;
650
766
  results.push({
651
767
  file: full,
652
- relativeFile: normalizeRel(import_node_path5.default.relative(root, full)),
768
+ relativeFile: normalizeRel(import_node_path6.default.relative(root, full)),
653
769
  line: i + 1,
654
770
  snippet: line.trim().slice(0, 160)
655
771
  });
@@ -699,7 +815,7 @@ function lspRefToLocation(root, ref) {
699
815
  const range = location.range || location.targetSelectionRange || location.targetRange;
700
816
  return {
701
817
  file,
702
- relativeFile: normalizeRel(import_node_path5.default.relative(root, file)),
818
+ relativeFile: normalizeRel(import_node_path6.default.relative(root, file)),
703
819
  line: (range?.start ? range.start.line : 0) + 1
704
820
  };
705
821
  } catch {
@@ -749,25 +865,25 @@ function nearbyFileHints(root, files) {
749
865
  "build.gradle.kts"
750
866
  ]);
751
867
  for (const file of files || []) {
752
- let dir = import_node_path5.default.dirname(import_node_path5.default.join(root, file));
868
+ let dir = import_node_path6.default.dirname(import_node_path6.default.join(root, file));
753
869
  while (dir.startsWith(root)) {
754
870
  for (const name of manifestNames) {
755
- const candidate = import_node_path5.default.join(dir, name);
756
- if (import_node_fs3.default.existsSync(candidate)) manifests.add(normalizeRel(import_node_path5.default.relative(root, candidate)));
871
+ const candidate = import_node_path6.default.join(dir, name);
872
+ if (import_node_fs4.default.existsSync(candidate)) manifests.add(normalizeRel(import_node_path6.default.relative(root, candidate)));
757
873
  }
758
874
  if (dir === root) break;
759
- dir = import_node_path5.default.dirname(dir);
875
+ dir = import_node_path6.default.dirname(dir);
760
876
  }
761
- const parsed = import_node_path5.default.parse(file);
877
+ const parsed = import_node_path6.default.parse(file);
762
878
  const candidates = [
763
- import_node_path5.default.join(parsed.dir, `${parsed.name}.test${parsed.ext}`),
764
- import_node_path5.default.join(parsed.dir, `${parsed.name}.spec${parsed.ext}`),
765
- import_node_path5.default.join(parsed.dir, `${parsed.name}_test${parsed.ext}`),
766
- import_node_path5.default.join("test", file),
767
- import_node_path5.default.join("tests", file)
879
+ import_node_path6.default.join(parsed.dir, `${parsed.name}.test${parsed.ext}`),
880
+ import_node_path6.default.join(parsed.dir, `${parsed.name}.spec${parsed.ext}`),
881
+ import_node_path6.default.join(parsed.dir, `${parsed.name}_test${parsed.ext}`),
882
+ import_node_path6.default.join("test", file),
883
+ import_node_path6.default.join("tests", file)
768
884
  ];
769
885
  for (const candidate of candidates) {
770
- if (import_node_fs3.default.existsSync(import_node_path5.default.join(root, candidate))) tests.add(normalizeRel(candidate));
886
+ if (import_node_fs4.default.existsSync(import_node_path6.default.join(root, candidate))) tests.add(normalizeRel(candidate));
771
887
  }
772
888
  }
773
889
  return {
@@ -777,28 +893,38 @@ function nearbyFileHints(root, files) {
777
893
  }
778
894
 
779
895
  // src/lsp/review/run-review.ts
896
+ var DEFAULT_LSP_CONFIG = "cadre/lsp.json";
897
+ function unavailable(reason) {
898
+ return {
899
+ available: false,
900
+ reason,
901
+ changedFiles: [],
902
+ changedEntries: [],
903
+ servers: [],
904
+ findings: []
905
+ };
906
+ }
780
907
  async function runReview(options = {}) {
781
908
  const args = {
782
909
  base: options.base || "main",
783
910
  head: options.head || "HEAD",
784
- config: options.config || "cadre/lsp.json"
911
+ config: options.config || DEFAULT_LSP_CONFIG
785
912
  };
786
913
  const root = options.root || process.cwd();
787
914
  const clientPool = options.clientPool || null;
788
- const configPath = import_node_path6.default.resolve(root, args.config);
789
- if (!import_node_fs4.default.existsSync(configPath)) {
790
- return {
791
- available: false,
792
- reason: `No LSP config found at ${args.config}`,
793
- changedFiles: [],
794
- changedEntries: [],
795
- servers: [],
796
- findings: []
797
- };
798
- }
915
+ const configPath = resolveOwnedProjectControlJsonPath(
916
+ root,
917
+ options.configOwnerRoot || root,
918
+ args.config,
919
+ DEFAULT_LSP_CONFIG
920
+ );
921
+ if (!configPath.ok) return unavailable(configPath.error);
922
+ if (!import_node_fs5.default.existsSync(configPath.file)) return unavailable(`No LSP config found at ${configPath.relative}`);
799
923
  const entries = changedEntries(root, args.base, args.head);
800
924
  const files = entries.map((entry) => entry.path);
801
- const config = asJsonObject(JSON.parse(import_node_fs4.default.readFileSync(configPath, "utf8")));
925
+ const configRead = readProjectControlJson(configPath);
926
+ if (!configRead.ok) return unavailable(`Cannot read LSP config ${configPath.relative}: ${configRead.error}`);
927
+ const config = asJsonObject(configRead.value);
802
928
  const servers = Array.isArray(config.servers) ? config.servers.map((server) => asJsonObject(server)).map((server) => ({
803
929
  ...server,
804
930
  id: asOptionalString(server.id),
@@ -815,8 +941,8 @@ async function runReview(options = {}) {
815
941
  const serverReports = [];
816
942
  const changedSet = /* @__PURE__ */ new Set();
817
943
  for (const entry of entries) {
818
- if (entry.path) changedSet.add(import_node_path6.default.resolve(root, entry.path));
819
- if (entry.oldPath) changedSet.add(import_node_path6.default.resolve(root, entry.oldPath));
944
+ if (entry.path) changedSet.add(import_node_path7.default.resolve(root, entry.path));
945
+ if (entry.oldPath) changedSet.add(import_node_path7.default.resolve(root, entry.oldPath));
820
946
  }
821
947
  for (const server of servers) {
822
948
  const serverEntries = entries.filter((entry) => {
@@ -938,7 +1064,7 @@ async function runReview(options = {}) {
938
1064
  typeDefinitions,
939
1065
  implementations
940
1066
  });
941
- const externalRefs = refs.map((ref) => lspRefToLocation(root, ref)).filter((ref) => ref !== null).filter((ref) => !changedSet.has(import_node_path6.default.resolve(ref.file))).filter((ref) => !isIgnoredFile(root, ref.relativeFile));
1067
+ const externalRefs = refs.map((ref) => lspRefToLocation(root, ref)).filter((ref) => ref !== null).filter((ref) => !changedSet.has(import_node_path7.default.resolve(ref.file))).filter((ref) => !isIgnoredFile(root, ref.relativeFile));
942
1068
  if (externalRefs.length > 0) {
943
1069
  findings.push(externalReferenceFinding(server, candidate, externalRefs, "lsp"));
944
1070
  }
@@ -946,7 +1072,7 @@ async function runReview(options = {}) {
946
1072
  }
947
1073
  for (const candidate of allCandidates.values()) {
948
1074
  if (candidate.changeType !== "removed") continue;
949
- if (candidate.changedFile && import_node_fs4.default.existsSync(import_node_path6.default.join(root, candidate.changedFile))) continue;
1075
+ if (candidate.changedFile && import_node_fs5.default.existsSync(import_node_path7.default.join(root, candidate.changedFile))) continue;
950
1076
  const refs = scanTextReferences(root, candidate.name, changedSet, server);
951
1077
  if (refs.length > 0) {
952
1078
  findings.push(externalReferenceFinding(server, candidate, refs, "text"));
@@ -971,6 +1097,7 @@ async function runReview(options = {}) {
971
1097
  }
972
1098
  return {
973
1099
  available: true,
1100
+ reason: void 0,
974
1101
  base: args.base,
975
1102
  head: args.head,
976
1103
  config: args.config,
@@ -1052,7 +1179,7 @@ async function runCli() {
1052
1179
  }
1053
1180
 
1054
1181
  // src/cadre-lsp-review.ts
1055
- if (["cadre-lsp-review.js", "cadre-lsp-review.ts"].includes(import_node_path7.default.basename(process.argv[1] || ""))) {
1182
+ if (["cadre-lsp-review.js", "cadre-lsp-review.ts"].includes(import_node_path8.default.basename(process.argv[1] || ""))) {
1056
1183
  runCli().catch((error) => {
1057
1184
  console.error(errorMessage(error));
1058
1185
  process.exit(1);