@fro.bot/systematic 3.29.0 → 3.30.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.
@@ -23,7 +23,7 @@ function parseFrontmatter(content) {
23
23
  }
24
24
 
25
25
  // src/lib/config.ts
26
- import fs2 from "fs";
26
+ import fs3 from "fs";
27
27
  import path2 from "path";
28
28
 
29
29
  // node_modules/.bun/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
@@ -1506,7 +1506,8 @@ var BUNDLED_SKILL_NAMES = [
1506
1506
  var CONFIG_PROTECTED_FIELD_PATHS = [
1507
1507
  "profiles",
1508
1508
  "allow_project_profiles",
1509
- "search",
1509
+ "search.enabled",
1510
+ "search.allow_project_sources",
1510
1511
  "agents.*.model",
1511
1512
  "agents.*.permission",
1512
1513
  "agents.*.skills",
@@ -7478,31 +7479,121 @@ var REMOVED_CONFIG_KEYS = [
7478
7479
  ];
7479
7480
 
7480
7481
  // src/lib/search-user-config.ts
7481
- import fs from "fs";
7482
+ import fs2 from "fs";
7482
7483
  import os from "os";
7483
7484
  import path from "path";
7485
+
7486
+ // src/lib/search-bounded-read.ts
7487
+ import fs from "fs";
7488
+ var READ_CHUNK_BYTES = 64 * 1024;
7489
+ function errorCode(error) {
7490
+ return typeof error === "object" && error !== null && "code" in error ? error.code : undefined;
7491
+ }
7492
+ function readBytesBounded(filePath, maxBytes) {
7493
+ let fd;
7494
+ try {
7495
+ fd = fs.openSync(filePath, fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0));
7496
+ } catch (error) {
7497
+ return errorCode(error) === "ENOENT" ? { kind: "absent" } : { kind: "failed" };
7498
+ }
7499
+ try {
7500
+ if (!fs.fstatSync(fd).isFile())
7501
+ return { kind: "not-regular" };
7502
+ const limit = maxBytes + 1;
7503
+ const chunks = [];
7504
+ let total = 0;
7505
+ while (total < limit) {
7506
+ const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, limit - total));
7507
+ const count = fs.readSync(fd, chunk, 0, chunk.length, null);
7508
+ if (count === 0)
7509
+ break;
7510
+ chunks.push(chunk.subarray(0, count));
7511
+ total += count;
7512
+ }
7513
+ if (total >= limit)
7514
+ return { kind: "too-large", bytesRead: total };
7515
+ return { kind: "bytes", bytes: Buffer.concat(chunks) };
7516
+ } catch {
7517
+ return { kind: "failed" };
7518
+ } finally {
7519
+ try {
7520
+ fs.closeSync(fd);
7521
+ } catch {}
7522
+ }
7523
+ }
7524
+
7525
+ // src/lib/search-user-config.ts
7526
+ var SEARCH_SOURCE_FORMATS = ["markdown", "deciduous"];
7527
+ var SOURCE_NAME_PATTERN = /^[A-Za-z0-9_-]{1,40}$/;
7528
+ var MAX_SOURCE_ROOTS = 8;
7529
+ var sourceNameSchema = string().regex(SOURCE_NAME_PATTERN).meta({
7530
+ description: "Source name: letters, digits, `-` and `_`, at most 40 characters. Case-sensitive. Search results label the source with it, and a user source with the same name as a project source replaces it when the user source is mapped to the project.",
7531
+ examples: ["team-rfcs", "ops_runbooks"]
7532
+ });
7533
+ var sourceFormatSchema = _enum(SEARCH_SOURCE_FORMATS).meta({
7534
+ description: "How the source stores records. `markdown` reads `.md` files under each root; `deciduous` reads Deciduous sync JSON (`nodes/` and `edges/`).",
7535
+ examples: ["markdown", "deciduous"]
7536
+ });
7537
+ var sourceRootsSchema = array(string().min(1)).min(1).max(MAX_SOURCE_ROOTS).meta({
7538
+ description: "One to eight folders to read. In a project config each root is a relative path under the checkout (no `.`, no `..`, no dot segment, except `.deciduous/<subfolder>` for a `deciduous` source). In user config each root is an absolute or `~`-prefixed path. Symlinks inside a root are skipped.",
7539
+ examples: [["docs/rfcs"], ["~/src/wiki/pages", "/srv/runbooks"]]
7540
+ });
7541
+ var sourceProjectsSchema = array(string().min(1)).optional().meta({
7542
+ description: "User config only, and required there: at least one absolute or `~`-prefixed main repository root this source applies to. A user source that omits it or leaves it empty is dropped with a warning, so a source can never apply to every project by accident. A project config may not set it.",
7543
+ examples: [["~/src/github.com/me/app"]]
7544
+ });
7545
+ var SearchSourceSchema = object({
7546
+ name: sourceNameSchema,
7547
+ format: sourceFormatSchema,
7548
+ roots: sourceRootsSchema,
7549
+ projects: sourceProjectsSchema
7550
+ }).strict().meta({
7551
+ description: "One opt-in decision source: a name, a format and one or more roots.",
7552
+ examples: [{ name: "team-rfcs", format: "markdown", roots: ["docs/rfcs"] }]
7553
+ });
7484
7554
  var SearchSettingsSchema = object({
7485
- enabled: boolean().optional().meta({
7486
- description: "Set to false to remove the search tool. Defaults to true when unset.",
7487
- examples: [false]
7555
+ enabled: boolean().meta({
7556
+ description: "Set to false to remove the search tool. Defaults to true when unset. User-owned: only valid in user config or OPENCODE_CONFIG_DIR config.",
7557
+ examples: [false],
7558
+ trust: "project-or-higher"
7559
+ }).optional(),
7560
+ allow_project_sources: array(string().min(1)).meta({
7561
+ description: "Main repository roots (absolute or `~`-prefixed) whose project config may propose `search.sources`. A project's sources are read only when its main root is listed here; matching is exact, a linked worktree matches its main checkout, and an allowed project covers whatever any of its branches proposes. User-owned: only valid in user config or OPENCODE_CONFIG_DIR config; the lists in both are combined.",
7562
+ examples: [["~/src/github.com/me/app"]],
7563
+ trust: "project-or-higher"
7564
+ }).optional(),
7565
+ sources: array(SearchSourceSchema).optional().meta({
7566
+ description: 'Opt-in external decision sources for `kind: "external"` search: folders of Markdown or Deciduous sync JSON. A project config may propose sources inside its own checkout, but none is read until the user lists that project in `allow_project_sources`. User config may map folders anywhere on disk to projects with `projects`, which every user source must list (at least one project root). An invalid entry is dropped with a warning and its valid siblings stay.',
7567
+ examples: [
7568
+ [{ name: "team-rfcs", format: "markdown", roots: ["docs/rfcs"] }],
7569
+ [
7570
+ {
7571
+ name: "decisions",
7572
+ format: "deciduous",
7573
+ roots: ["~/src/github.com/me/app/.deciduous/sync"],
7574
+ projects: ["~/src/github.com/me/app"]
7575
+ }
7576
+ ]
7577
+ ]
7488
7578
  })
7489
7579
  }).strict().meta({
7490
- description: "Durable-output search settings. Only user config and OPENCODE_CONFIG_DIR config may set this field; a project config that sets it has the whole field ignored with a warning, so a repository cannot switch search off. An invalid block is dropped with a warning; the rest of the file still loads.",
7491
- examples: [{ enabled: false }]
7492
- });
7493
- function validateSearchBlock(value) {
7494
- const parsed = SearchSettingsSchema.safeParse(value);
7495
- if (parsed.success)
7496
- return { ok: true, settings: parsed.data };
7497
- const fields = [];
7498
- for (const issue of parsed.error.issues) {
7499
- const field = ["search", ...issue.path.map(String)].join(".");
7500
- if (!fields.includes(field))
7501
- fields.push(field);
7502
- }
7503
- return { ok: false, fields };
7504
- }
7505
- var MAX_WARNING_FIELDS = 5;
7580
+ description: "Search settings. `enabled` and `allow_project_sources` are user-owned: only user config and OPENCODE_CONFIG_DIR config may set them, and a project config that sets either has that field ignored with a warning. A project config may set `sources`. An invalid field or entry is dropped with a warning; the rest of the file still loads.",
7581
+ examples: [
7582
+ { enabled: false },
7583
+ {
7584
+ allow_project_sources: ["~/src/github.com/me/app"],
7585
+ sources: [
7586
+ {
7587
+ name: "team-rfcs",
7588
+ format: "markdown",
7589
+ roots: ["/srv/rfcs"],
7590
+ projects: ["~/src/github.com/me/app"]
7591
+ }
7592
+ ]
7593
+ }
7594
+ ]
7595
+ });
7596
+ var MAX_DETAIL_WARNINGS = 5;
7506
7597
  var MAX_PATH_CHARS = 200;
7507
7598
  function displaySafe(text) {
7508
7599
  let out = "";
@@ -7515,30 +7606,273 @@ function displaySafe(text) {
7515
7606
  }
7516
7607
  return out;
7517
7608
  }
7518
- function formatSearchBlockWarning(sourcePath, fields) {
7519
- const shown = fields.slice(0, MAX_WARNING_FIELDS).join(", ");
7520
- const more = fields.length > MAX_WARNING_FIELDS ? ` and ${fields.length - MAX_WARNING_FIELDS} more` : "";
7521
- return `[systematic] \`search\` in ${displaySafe(sourcePath)} is invalid (${shown}${more}) and has been ignored; search still runs and its results carry a \`config unreadable\` note. The rest of the file still loads.`;
7609
+ function hasControlChar(text) {
7610
+ for (let index = 0;index < text.length; index += 1) {
7611
+ const code = text.charCodeAt(index);
7612
+ if (code <= 31 || code === 127)
7613
+ return true;
7614
+ }
7615
+ return false;
7616
+ }
7617
+ function isRecord2(value) {
7618
+ return typeof value === "object" && value !== null && !Array.isArray(value);
7619
+ }
7620
+ function unreadableTail(trust) {
7621
+ return trust === "user" ? "; search still runs and its results carry a `config unreadable` note. The rest of the file still loads." : ". The rest of the file still loads.";
7622
+ }
7623
+ function fieldWarning(trust, sourcePath, field) {
7624
+ return `[systematic] \`${field}\` in ${sourcePath} is invalid and has been ignored${unreadableTail(trust)}`;
7625
+ }
7626
+ function unknownKeyWarning(trust, sourcePath) {
7627
+ return `[systematic] \`search\` in ${sourcePath} contains an unknown field (its name is not shown) that has been ignored${unreadableTail(trust)}`;
7628
+ }
7629
+ function entryWarning(sourcePath, label, reason) {
7630
+ return `[systematic] search source ${label} in ${sourcePath} was dropped: ${reason}. Its valid siblings still load.`;
7631
+ }
7632
+ function isTildePath(value) {
7633
+ return value === "~" || value.startsWith("~/") || value.startsWith("~\\");
7634
+ }
7635
+ function isUserPathForm(value) {
7636
+ return typeof value === "string" && value.length > 0 && !hasControlChar(value) && (isTildePath(value) || path.isAbsolute(value));
7637
+ }
7638
+ function isAbsoluteAnywhere(value) {
7639
+ return path.isAbsolute(value) || path.posix.isAbsolute(value) || path.win32.isAbsolute(value);
7640
+ }
7641
+ function isProjectRoot(root, format) {
7642
+ if (root.length === 0 || hasControlChar(root))
7643
+ return false;
7644
+ if (isAbsoluteAnywhere(root))
7645
+ return false;
7646
+ const segments = root.split(/[\\/]+/).filter((segment) => segment.length > 0);
7647
+ if (segments.length === 0)
7648
+ return false;
7649
+ const exempt = format === "deciduous" && segments[0] === ".deciduous" && segments.length > 1;
7650
+ const checked = exempt ? segments.slice(1) : segments;
7651
+ return checked.every((segment) => !segment.startsWith("."));
7652
+ }
7653
+ var ENTRY_KEYS = new Set([
7654
+ "name",
7655
+ "format",
7656
+ "roots",
7657
+ "projects"
7658
+ ]);
7659
+ function isFormat(value) {
7660
+ return typeof value === "string" && SEARCH_SOURCE_FORMATS.includes(value);
7661
+ }
7662
+ function rejectEntry(label, reason) {
7663
+ return { ok: false, label, reason };
7664
+ }
7665
+ function validateRoots(roots, format, trust) {
7666
+ if (!Array.isArray(roots) || roots.length < 1 || roots.length > MAX_SOURCE_ROOTS) {
7667
+ return `it needs 1 to ${MAX_SOURCE_ROOTS} roots`;
7668
+ }
7669
+ const valid = [];
7670
+ for (const root of roots) {
7671
+ if (typeof root !== "string")
7672
+ return "a root is not a string";
7673
+ if (trust === "user" && !isUserPathForm(root)) {
7674
+ return "a root must be an absolute or ~-prefixed path";
7675
+ }
7676
+ if (trust === "project" && !isProjectRoot(root, format)) {
7677
+ return 'a root must be a relative subfolder of the project with no ".", ".." or dot segment (only .deciduous/<subfolder> is allowed, for deciduous sources)';
7678
+ }
7679
+ valid.push(root);
7680
+ }
7681
+ return valid;
7682
+ }
7683
+ function validateProjects(projects, trust) {
7684
+ if (trust === "project") {
7685
+ return projects === undefined ? undefined : "projects is only valid in user config";
7686
+ }
7687
+ if (!Array.isArray(projects) || projects.length === 0) {
7688
+ return "a user source needs a projects list with at least one project root";
7689
+ }
7690
+ if (!projects.every(isUserPathForm)) {
7691
+ return "a projects entry must be an absolute or ~-prefixed path";
7692
+ }
7693
+ return projects;
7694
+ }
7695
+ function validateEntry(raw, index, trust) {
7696
+ const indexLabel = `#${index}`;
7697
+ if (!isRecord2(raw))
7698
+ return rejectEntry(indexLabel, "it is not an object");
7699
+ const name = raw.name;
7700
+ if (typeof name !== "string" || !SOURCE_NAME_PATTERN.test(name)) {
7701
+ return rejectEntry(indexLabel, 'the name must be 1 to 40 letters, digits, "-" or "_"');
7702
+ }
7703
+ const label = `"${name}"`;
7704
+ if (Object.keys(raw).some((key) => !ENTRY_KEYS.has(key))) {
7705
+ return rejectEntry(label, "it has an unknown field");
7706
+ }
7707
+ const format = raw.format;
7708
+ if (!isFormat(format)) {
7709
+ return rejectEntry(label, "the format must be markdown or deciduous");
7710
+ }
7711
+ const roots = validateRoots(raw.roots, format, trust);
7712
+ if (typeof roots === "string")
7713
+ return rejectEntry(label, roots);
7714
+ const projects = validateProjects(raw.projects, trust);
7715
+ if (typeof projects === "string")
7716
+ return rejectEntry(label, projects);
7717
+ return {
7718
+ ok: true,
7719
+ entry: {
7720
+ name,
7721
+ format,
7722
+ roots: [...roots],
7723
+ ...projects !== undefined && { projects: [...projects] }
7724
+ }
7725
+ };
7726
+ }
7727
+ function warn(state, message) {
7728
+ if (state.warnings.length < MAX_DETAIL_WARNINGS)
7729
+ state.warnings.push(message);
7730
+ else
7731
+ state.suppressed += 1;
7732
+ }
7733
+ function validateSources(raw, state) {
7734
+ if (!Array.isArray(raw)) {
7735
+ state.structural = true;
7736
+ state.unreadable = true;
7737
+ warn(state, fieldWarning(state.trust, state.sourcePath, "search.sources"));
7738
+ return;
7739
+ }
7740
+ const kept = [];
7741
+ const seen = new Set;
7742
+ raw.forEach((item, index) => {
7743
+ const result = validateEntry(item, index, state.trust);
7744
+ if (!result.ok) {
7745
+ warn(state, entryWarning(state.sourcePath, result.label, result.reason));
7746
+ return;
7747
+ }
7748
+ if (seen.has(result.entry.name)) {
7749
+ warn(state, entryWarning(state.sourcePath, `"${result.entry.name}"`, "the name is already used earlier in this file, so the first entry is kept"));
7750
+ return;
7751
+ }
7752
+ seen.add(result.entry.name);
7753
+ kept.push(result.entry);
7754
+ });
7755
+ return kept;
7756
+ }
7757
+ function validateAllowList(raw, state) {
7758
+ if (!Array.isArray(raw)) {
7759
+ state.unreadable = true;
7760
+ warn(state, fieldWarning(state.trust, state.sourcePath, "search.allow_project_sources"));
7761
+ return;
7762
+ }
7763
+ const kept = [];
7764
+ let rejected = 0;
7765
+ for (const item of raw) {
7766
+ if (!isUserPathForm(item))
7767
+ rejected += 1;
7768
+ else if (!kept.includes(item))
7769
+ kept.push(item);
7770
+ }
7771
+ if (rejected > 0) {
7772
+ state.unreadable = true;
7773
+ warn(state, `[systematic] ${rejected} ${rejected === 1 ? "entry" : "entries"} in \`search.allow_project_sources\` in ${state.sourcePath} ${rejected === 1 ? "is" : "are"} not an absolute or ~-prefixed path and ${rejected === 1 ? "has" : "have"} been ignored; ${rejected === 1 ? "it allows" : "they allow"} nothing${unreadableTail(state.trust)}`);
7774
+ }
7775
+ return kept;
7776
+ }
7777
+ var USER_KEYS = new Set([
7778
+ "enabled",
7779
+ "allow_project_sources",
7780
+ "sources"
7781
+ ]);
7782
+ function hasUnknownKey(value) {
7783
+ return Object.keys(value).some((key) => !USER_KEYS.has(key));
7784
+ }
7785
+ function validateUserOnlyFields(value, state, settings) {
7786
+ if (Object.hasOwn(value, "enabled")) {
7787
+ if (typeof value.enabled === "boolean")
7788
+ settings.enabled = value.enabled;
7789
+ else {
7790
+ state.unreadable = true;
7791
+ warn(state, fieldWarning(state.trust, state.sourcePath, "search.enabled"));
7792
+ }
7793
+ }
7794
+ if (Object.hasOwn(value, "allow_project_sources")) {
7795
+ const allow = validateAllowList(value.allow_project_sources, state);
7796
+ if (allow !== undefined && allow.length > 0) {
7797
+ settings.allow_project_sources = allow;
7798
+ }
7799
+ }
7800
+ }
7801
+ function validateSearchConfig(value, trust, sourcePath) {
7802
+ const state = {
7803
+ trust,
7804
+ sourcePath: displaySafe(sourcePath),
7805
+ warnings: [],
7806
+ suppressed: 0,
7807
+ unreadable: false,
7808
+ structural: false
7809
+ };
7810
+ if (!isRecord2(value)) {
7811
+ warn(state, fieldWarning(trust, state.sourcePath, "search"));
7812
+ return {
7813
+ settings: undefined,
7814
+ unreadable: true,
7815
+ structural: true,
7816
+ warnings: state.warnings
7817
+ };
7818
+ }
7819
+ const settings = {};
7820
+ if (hasUnknownKey(value)) {
7821
+ state.unreadable = true;
7822
+ warn(state, unknownKeyWarning(trust, state.sourcePath));
7823
+ }
7824
+ if (trust === "user")
7825
+ validateUserOnlyFields(value, state, settings);
7826
+ if (Object.hasOwn(value, "sources")) {
7827
+ const sources = validateSources(value.sources, state);
7828
+ if (sources !== undefined && sources.length > 0)
7829
+ settings.sources = sources;
7830
+ }
7831
+ if (state.suppressed > 0) {
7832
+ state.warnings.push(`[systematic] ${state.suppressed} more \`search\` problem(s) in ${state.sourcePath} were not listed.`);
7833
+ }
7834
+ return {
7835
+ settings,
7836
+ unreadable: state.unreadable,
7837
+ structural: state.structural,
7838
+ warnings: state.warnings
7839
+ };
7522
7840
  }
7523
7841
  function formatSearchFileWarning(sourcePath, reason) {
7524
7842
  return `[systematic] search settings in ${displaySafe(sourcePath)} could not be read (${reason}); the settings were ignored, search still runs and its results carry a \`config unreadable\` note.`;
7525
7843
  }
7526
7844
  function mergeSearchSettings(sources) {
7527
- let enabled;
7845
+ const acc = { allow: [], byName: new Map };
7528
7846
  let unreadable = false;
7529
7847
  for (const source of sources) {
7530
7848
  if (source.unreadable)
7531
7849
  unreadable = true;
7532
- if (source.settings?.enabled !== undefined)
7533
- enabled = source.settings.enabled;
7850
+ if (source.settings !== undefined)
7851
+ absorb(acc, source.settings);
7534
7852
  }
7535
- if (enabled === undefined)
7853
+ const merged = {
7854
+ ...acc.enabled !== undefined && { enabled: acc.enabled },
7855
+ ...acc.allow.length > 0 && { allow_project_sources: acc.allow },
7856
+ ...acc.byName.size > 0 && { sources: [...acc.byName.values()] }
7857
+ };
7858
+ if (Object.keys(merged).length === 0) {
7536
7859
  return { settings: undefined, unreadable };
7537
- return { settings: { enabled }, unreadable };
7860
+ }
7861
+ return { settings: merged, unreadable };
7862
+ }
7863
+ function absorb(acc, settings) {
7864
+ if (settings.enabled !== undefined)
7865
+ acc.enabled = settings.enabled;
7866
+ for (const entry of settings.allow_project_sources ?? []) {
7867
+ if (!acc.allow.includes(entry))
7868
+ acc.allow.push(entry);
7869
+ }
7870
+ for (const entry of settings.sources ?? [])
7871
+ acc.byName.set(entry.name, entry);
7538
7872
  }
7539
7873
  function selectConfigFile(dir, basename) {
7540
7874
  const jsoncPath = path.join(dir, `${basename}.jsonc`);
7541
- if (fs.existsSync(jsoncPath))
7875
+ if (fs2.existsSync(jsoncPath))
7542
7876
  return jsoncPath;
7543
7877
  return path.join(dir, `${basename}.json`);
7544
7878
  }
@@ -7548,19 +7882,42 @@ function resolveUserLevelConfigDirs(options) {
7548
7882
  const rawCustom = options !== undefined && Object.hasOwn(options, "customConfigDir") ? options.customConfigDir?.trim() : process.env.OPENCODE_CONFIG_DIR?.trim();
7549
7883
  return { homeDir, userConfigDir, customConfigDir: rawCustom || undefined };
7550
7884
  }
7551
- function isRecord2(value) {
7552
- return typeof value === "object" && value !== null && !Array.isArray(value);
7885
+ function errorCode2(error) {
7886
+ return error instanceof Error && "code" in error ? error.code : undefined;
7553
7887
  }
7554
- function readSearchBlock(filePath) {
7555
- let content;
7888
+ var MAX_PROJECT_CONFIG_BYTES = 1024 * 1024;
7889
+ function readTextBounded(filePath, maxBytes) {
7890
+ const read = readBytesBounded(filePath, maxBytes);
7891
+ switch (read.kind) {
7892
+ case "bytes":
7893
+ return { kind: "text", content: read.bytes.toString("utf-8") };
7894
+ case "absent":
7895
+ return { kind: "absent" };
7896
+ case "not-regular":
7897
+ return { kind: "failed", reason: "not a regular file" };
7898
+ case "too-large":
7899
+ return {
7900
+ kind: "failed",
7901
+ reason: `file is larger than ${maxBytes / (1024 * 1024)} MB`
7902
+ };
7903
+ case "failed":
7904
+ return { kind: "failed", reason: "unable to read file" };
7905
+ }
7906
+ }
7907
+ function readTextWhole(filePath) {
7556
7908
  try {
7557
- content = fs.readFileSync(filePath, "utf-8");
7909
+ return { kind: "text", content: fs2.readFileSync(filePath, "utf-8") };
7558
7910
  } catch (error) {
7559
- const code = error instanceof Error && "code" in error ? error.code : undefined;
7560
- if (code === "ENOENT")
7911
+ if (errorCode2(error) === "ENOENT")
7561
7912
  return { kind: "absent" };
7562
7913
  return { kind: "failed", reason: "unable to read file" };
7563
7914
  }
7915
+ }
7916
+ function readSearchBlock(filePath, maxBytes) {
7917
+ const text = maxBytes === undefined ? readTextWhole(filePath) : readTextBounded(filePath, maxBytes);
7918
+ if (text.kind !== "text")
7919
+ return text;
7920
+ const content = text.content;
7564
7921
  const errors = [];
7565
7922
  const parsed = parse(content, errors);
7566
7923
  const firstError = errors[0];
@@ -7598,17 +7955,41 @@ function loadUserLevelSearchSettings(options) {
7598
7955
  }
7599
7956
  if (!read.has)
7600
7957
  continue;
7601
- const validation = validateSearchBlock(read.searchBlock);
7602
- if (validation.ok) {
7603
- sources.push({ settings: validation.settings, unreadable: false });
7604
- } else {
7605
- warnings.push(formatSearchBlockWarning(filePath, validation.fields));
7606
- sources.push({ unreadable: true });
7607
- }
7958
+ const validation = validateSearchConfig(read.searchBlock, "user", filePath);
7959
+ warnings.push(...validation.warnings);
7960
+ sources.push({
7961
+ settings: validation.settings,
7962
+ unreadable: validation.unreadable
7963
+ });
7608
7964
  }
7609
7965
  const merged = mergeSearchSettings(sources);
7610
7966
  return { ...merged, warnings };
7611
7967
  }
7968
+ function formatProjectFileWarning(sourcePath, reason) {
7969
+ return `[systematic] project search settings in ${displaySafe(sourcePath)} could not be read (${reason}); the sources it proposes were ignored.`;
7970
+ }
7971
+ function loadProjectSearchSources(checkoutRoot) {
7972
+ const filePath = selectConfigFile(path.join(checkoutRoot, ".opencode"), "systematic");
7973
+ const read = readSearchBlock(filePath, MAX_PROJECT_CONFIG_BYTES);
7974
+ if (read.kind === "absent") {
7975
+ return { sources: [], warnings: [], unreadable: false };
7976
+ }
7977
+ if (read.kind === "failed") {
7978
+ return {
7979
+ sources: [],
7980
+ warnings: [formatProjectFileWarning(filePath, read.reason)],
7981
+ unreadable: true
7982
+ };
7983
+ }
7984
+ if (!read.has)
7985
+ return { sources: [], warnings: [], unreadable: false };
7986
+ const validation = validateSearchConfig(read.searchBlock, "project", filePath);
7987
+ return {
7988
+ sources: validation.structural ? [] : validation.settings?.sources ?? [],
7989
+ warnings: validation.warnings,
7990
+ unreadable: validation.structural
7991
+ };
7992
+ }
7612
7993
 
7613
7994
  // src/lib/config-schema.ts
7614
7995
  var permissionSettingSchema = _enum(["ask", "allow", "deny"]);
@@ -7899,7 +8280,7 @@ function createSystematicConfigSchema(opts) {
7899
8280
  description: "Allow a repository's project config to define its own `profiles` map. User-owned only \u2014 only valid in user config or OPENCODE_CONFIG_DIR config; a project config setting this field has it ignored (a project cannot grant itself a permission it does not already have). Defaults to false.",
7900
8281
  examples: [true, false]
7901
8282
  })).default(false),
7902
- search: trustProtected(SearchSettingsSchema).optional(),
8283
+ search: SearchSettingsSchema.optional(),
7903
8284
  disabled_skills: array(_enum([...skillNames, ...removedSkillNames])).default([]).meta({
7904
8285
  description: "Array of bundled skill names to disable globally. Unknown skill names are rejected at parse time.",
7905
8286
  examples: [["ce:plan", "ce:review"]]
@@ -8332,10 +8713,11 @@ var PROTECTED_OVERLAY_FIELD_PATHS = {
8332
8713
  pi: "categories.*.pi"
8333
8714
  }
8334
8715
  };
8335
- var PROJECT_ALWAYS_PROTECTED_FIELDS = new Set([
8336
- "allow_project_profiles",
8337
- "search"
8338
- ]);
8716
+ var PROJECT_ALWAYS_PROTECTED_FIELDS = new Set(["allow_project_profiles"]);
8717
+ var PROJECT_PROTECTED_SEARCH_FIELDS = [
8718
+ ["enabled", "search.enabled"],
8719
+ ["allow_project_sources", "search.allow_project_sources"]
8720
+ ];
8339
8721
  var MAX_DIAGNOSTIC_TEXT = 200;
8340
8722
  var MAX_STRIP_WARNINGS = 20;
8341
8723
  var DIAGNOSTIC_ELLIPSIS = "\u2026";
@@ -8418,7 +8800,7 @@ function isErrorWithCode(error) {
8418
8800
  function loadJsoncFile(filePath) {
8419
8801
  let content;
8420
8802
  try {
8421
- content = fs2.readFileSync(filePath, "utf-8");
8803
+ content = fs3.readFileSync(filePath, "utf-8");
8422
8804
  } catch (error) {
8423
8805
  if (isErrorWithCode(error) && error.code === "ENOENT")
8424
8806
  return null;
@@ -8534,7 +8916,7 @@ function loadConfigSource(filePath, trust, invalidSource, warningSink, allowProj
8534
8916
  const protectedFields = collectProjectProtectedFields(rawConfig, trust, allowProjectProfiles);
8535
8917
  const topLevelStripped = trust === "project" ? stripProjectProtectedFields(rawConfig, allowProjectProfiles) : rawConfig;
8536
8918
  const config = trust === "project" ? stripProjectSecurityOverlayFields(topLevelStripped, filePath, warningSink) : topLevelStripped;
8537
- const { config: configWithoutSearch, search } = splitSearchBlock(config, filePath, warningSink);
8919
+ const { config: configWithoutSearch, search } = splitSearchBlock(config, filePath, trust, warningSink);
8538
8920
  const result = SystematicConfigSchema.safeParse(configWithoutSearch);
8539
8921
  if (!result.success) {
8540
8922
  throwTopLevelConfigSchemaError(filePath, trust, result.error.issues, configWithoutSearch);
@@ -8565,21 +8947,16 @@ function loadConfigSource(filePath, trust, invalidSource, warningSink, allowProj
8565
8947
  };
8566
8948
  }
8567
8949
  }
8568
- function splitSearchBlock(config, filePath, warningSink) {
8950
+ function splitSearchBlock(config, filePath, trust, warningSink) {
8569
8951
  if (!Object.hasOwn(config, "search"))
8570
8952
  return { config, search: undefined };
8571
8953
  const { search: rawSearch, ...rest } = config;
8572
- const validation = validateSearchBlock(rawSearch);
8573
- if (validation.ok) {
8574
- return {
8575
- config: rest,
8576
- search: { settings: validation.settings, unreadable: false }
8577
- };
8578
- }
8579
- warningSink(formatSearchBlockWarning(filePath, validation.fields));
8954
+ const validation = validateSearchConfig(rawSearch, trust === "project" ? "project" : "user", filePath);
8955
+ for (const message of validation.warnings)
8956
+ warningSink(message);
8580
8957
  return {
8581
8958
  config: rest,
8582
- search: { unreadable: true }
8959
+ search: trust === "project" ? undefined : { settings: validation.settings, unreadable: validation.unreadable }
8583
8960
  };
8584
8961
  }
8585
8962
  function classifyConfigSourceError(error) {
@@ -8598,7 +8975,7 @@ function isConfigSchemaError(error) {
8598
8975
  }
8599
8976
  function resolveConfigSourcePath(filePath) {
8600
8977
  try {
8601
- return fs2.realpathSync(filePath);
8978
+ return fs3.realpathSync(filePath);
8602
8979
  } catch {
8603
8980
  return path2.resolve(filePath);
8604
8981
  }
@@ -8610,6 +8987,11 @@ function emitTopLevelProtectedFieldWarning(projectSource, fieldPath, messageTemp
8610
8987
  const safePath = sanitizeDiagnosticText(projectSource.path);
8611
8988
  sink(`[systematic] ${messageTemplate.replace("%PATH%", safePath)}`);
8612
8989
  }
8990
+ function emitProjectSearchProtectedWarnings(projectSource, sink) {
8991
+ for (const [, fieldPath] of PROJECT_PROTECTED_SEARCH_FIELDS) {
8992
+ emitTopLevelProtectedFieldWarning(projectSource, fieldPath, `\`${fieldPath}\` in project config (%PATH%) is only valid in user config or OPENCODE_CONFIG_DIR config and has been ignored. A project config may set \`search.sources\` only.`, sink);
8993
+ }
8994
+ }
8613
8995
  function collectProjectProtectedFields(rawConfig, trust, allowProjectProfiles) {
8614
8996
  if (trust !== "project")
8615
8997
  return [];
@@ -8628,17 +9010,20 @@ function collectProjectProtectedFields(rawConfig, trust, allowProjectProfiles) {
8628
9010
  sourceKind: "project"
8629
9011
  }
8630
9012
  ] : [],
8631
- ...Object.hasOwn(rawConfig, "search") ? [
8632
- {
8633
- fieldPath: "search",
8634
- outcome: "blocked",
8635
- sourceKind: "project"
8636
- }
8637
- ] : [],
9013
+ ...collectSearchProtectedFields(rawConfig.search),
8638
9014
  ...collectOverlayProtectedFields(rawConfig.agents, "agents"),
8639
9015
  ...collectOverlayProtectedFields(rawConfig.categories, "categories")
8640
9016
  ];
8641
9017
  }
9018
+ function collectSearchProtectedFields(search) {
9019
+ if (!isRecord3(search))
9020
+ return [];
9021
+ return PROJECT_PROTECTED_SEARCH_FIELDS.filter(([key]) => Object.hasOwn(search, key)).map(([, fieldPath]) => ({
9022
+ fieldPath,
9023
+ outcome: "blocked",
9024
+ sourceKind: "project"
9025
+ }));
9026
+ }
8642
9027
  function collectOverlayProtectedFields(overlayMap, mapKey) {
8643
9028
  if (!isRecord3(overlayMap))
8644
9029
  return [];
@@ -8901,7 +9286,7 @@ function loadConfigWithSources(projectDir, options) {
8901
9286
  const projectSource = project.source;
8902
9287
  emitTopLevelProtectedFieldWarning(projectSource, "profiles", `\`profiles\` in project config (%PATH%) is only valid in user config or OPENCODE_CONFIG_DIR config and has been ignored. Its bundles are not selectable even if this project also sets \`profile\`.`, boundedProjectStrip.sink);
8903
9288
  emitTopLevelProtectedFieldWarning(projectSource, "allow_project_profiles", `\`allow_project_profiles\` in project config (%PATH%) is only valid in user config or OPENCODE_CONFIG_DIR config and has been ignored.`, boundedProjectStrip.sink);
8904
- emitTopLevelProtectedFieldWarning(projectSource, "search", `\`search\` in project config (%PATH%) is only valid in user config or OPENCODE_CONFIG_DIR config and has been ignored.`, boundedProjectStrip.sink);
9289
+ emitProjectSearchProtectedWarnings(projectSource, boundedProjectStrip.sink);
8905
9290
  const suppressedStripCount = boundedProjectStrip.suppressedCount();
8906
9291
  if (suppressedStripCount > 0) {
8907
9292
  projectStrip.sink(`[systematic] ${suppressedStripCount} additional project-config protected-field warning(s) in (${sanitizeDiagnosticText(paths.projectConfig)}) were suppressed.`);
@@ -9337,7 +9722,7 @@ function getConfigPaths(projectDir, options) {
9337
9722
  }
9338
9723
 
9339
9724
  // src/lib/walk-dir.ts
9340
- import fs3 from "fs";
9725
+ import fs4 from "fs";
9341
9726
  import path3 from "path";
9342
9727
  function isDiscoverableMarkdown(fileName) {
9343
9728
  return fileName.endsWith(".md") && fileName.toLowerCase() !== "readme.md";
@@ -9345,12 +9730,12 @@ function isDiscoverableMarkdown(fileName) {
9345
9730
  function walkDir(rootDir, options = {}) {
9346
9731
  const { maxDepth = 3, filter } = options;
9347
9732
  const results = [];
9348
- if (!fs3.existsSync(rootDir))
9733
+ if (!fs4.existsSync(rootDir))
9349
9734
  return results;
9350
9735
  function recurse(currentDir, depth, category) {
9351
9736
  if (depth > maxDepth)
9352
9737
  return;
9353
- const entries = fs3.readdirSync(currentDir, { withFileTypes: true });
9738
+ const entries = fs4.readdirSync(currentDir, { withFileTypes: true });
9354
9739
  for (const entry of entries) {
9355
9740
  const fullPath = path3.join(currentDir, entry.name);
9356
9741
  const walkEntry = {
@@ -9447,11 +9832,11 @@ function extractCommandFrontmatter(content) {
9447
9832
  }
9448
9833
 
9449
9834
  // src/lib/discovered-skills.ts
9450
- import fs5 from "fs";
9835
+ import fs6 from "fs";
9451
9836
  import path5 from "path";
9452
9837
 
9453
9838
  // src/lib/skills.ts
9454
- import fs4 from "fs";
9839
+ import fs5 from "fs";
9455
9840
  import path4 from "path";
9456
9841
  function parseMetadata(data) {
9457
9842
  const metadataRaw = data.metadata;
@@ -9489,7 +9874,7 @@ function extractFrontmatterFromContent(content) {
9489
9874
  }
9490
9875
  function extractFrontmatter(filePath) {
9491
9876
  try {
9492
- const content = fs4.readFileSync(filePath, "utf8");
9877
+ const content = fs5.readFileSync(filePath, "utf8");
9493
9878
  return extractFrontmatterFromContent(content);
9494
9879
  } catch {
9495
9880
  return { name: "", description: "" };
@@ -9503,7 +9888,7 @@ function findSkillsInDir(dir, maxDepth = 3) {
9503
9888
  });
9504
9889
  for (const entry of entries) {
9505
9890
  const skillFile = path4.join(entry.path, "SKILL.md");
9506
- if (fs4.existsSync(skillFile)) {
9891
+ if (fs5.existsSync(skillFile)) {
9507
9892
  const frontmatter = extractFrontmatter(skillFile);
9508
9893
  skills.push({
9509
9894
  path: entry.path,
@@ -9535,7 +9920,7 @@ function findGitWorktreeRoot(startDir) {
9535
9920
  let current = path5.resolve(startDir);
9536
9921
  while (true) {
9537
9922
  try {
9538
- if (fs5.existsSync(path5.join(current, ".git"))) {
9923
+ if (fs6.existsSync(path5.join(current, ".git"))) {
9539
9924
  return current;
9540
9925
  }
9541
9926
  } catch {
@@ -9555,7 +9940,7 @@ function upWalk(targets, start, stop) {
9555
9940
  for (const target of targets) {
9556
9941
  const candidate = path5.join(current, target);
9557
9942
  try {
9558
- if (fs5.existsSync(candidate)) {
9943
+ if (fs6.existsSync(candidate)) {
9559
9944
  results.push(candidate);
9560
9945
  }
9561
9946
  } catch {}
@@ -9586,7 +9971,7 @@ function globSkillFiles(rootDir, subdirNames) {
9586
9971
  for (const subdirName of subdirNames) {
9587
9972
  const scanRoot = path5.join(rootDir, subdirName);
9588
9973
  try {
9589
- if (!fs5.existsSync(scanRoot))
9974
+ if (!fs6.existsSync(scanRoot))
9590
9975
  continue;
9591
9976
  const entries = walkDir(scanRoot, {
9592
9977
  maxDepth: 10,
@@ -9602,7 +9987,7 @@ function globSkillFiles(rootDir, subdirNames) {
9602
9987
  function toDiscoveredSkill(skillPath, rootId, onIssue) {
9603
9988
  let content;
9604
9989
  try {
9605
- content = fs5.readFileSync(skillPath, "utf8");
9990
+ content = fs6.readFileSync(skillPath, "utf8");
9606
9991
  } catch {
9607
9992
  onIssue?.("read-failed");
9608
9993
  return;
@@ -9656,15 +10041,21 @@ function discoverSkills(options) {
9656
10041
  return Array.from(byName.values());
9657
10042
  }
9658
10043
 
10044
+ // src/lib/search-core.ts
10045
+ import fs10 from "fs";
10046
+ import path11 from "path";
10047
+
9659
10048
  // src/lib/search-corpus.ts
9660
- import fs6 from "fs";
10049
+ import fs7 from "fs";
9661
10050
  import path6 from "path";
9662
10051
  var MAX_FILE_BYTES = 256 * 1024;
9663
10052
  var MAX_FILES = 2000;
9664
10053
  var MAX_DEPTH = 6;
9665
10054
  var MAX_TOTAL_BYTES = 16 * 1024 * 1024;
9666
10055
  var MAX_DISCOVERED_FILES = MAX_FILES * 4;
10056
+ var MAX_LISTED_ENTRIES = MAX_DISCOVERED_FILES * 4;
9667
10057
  var MAX_WARNING_LENGTH = 200;
10058
+ var DEFAULT_EXTENSIONS = [".md"];
9668
10059
  var KIND_TABLE = {
9669
10060
  solution: {
9670
10061
  dir: "docs/solutions",
@@ -9707,14 +10098,14 @@ function compareCodeUnits(a, b) {
9707
10098
  }
9708
10099
  function safeLstat(target) {
9709
10100
  try {
9710
- return fs6.lstatSync(target, { throwIfNoEntry: false }) ?? null;
10101
+ return fs7.lstatSync(target, { throwIfNoEntry: false }) ?? null;
9711
10102
  } catch {
9712
10103
  return null;
9713
10104
  }
9714
10105
  }
9715
10106
  function safeRealpath(target) {
9716
10107
  try {
9717
- return fs6.realpathSync(target);
10108
+ return fs7.realpathSync(target);
9718
10109
  } catch {
9719
10110
  return null;
9720
10111
  }
@@ -9725,14 +10116,14 @@ function isAbsence(error) {
9725
10116
  }
9726
10117
  function probeRealpath(target) {
9727
10118
  try {
9728
- return { kind: "ok", value: fs6.realpathSync(target) };
10119
+ return { kind: "ok", value: fs7.realpathSync(target) };
9729
10120
  } catch (error) {
9730
10121
  return { kind: isAbsence(error) ? "absent" : "failed" };
9731
10122
  }
9732
10123
  }
9733
10124
  function probeLstat(target) {
9734
10125
  try {
9735
- const stat = fs6.lstatSync(target, { throwIfNoEntry: false });
10126
+ const stat = fs7.lstatSync(target, { throwIfNoEntry: false });
9736
10127
  return stat === undefined ? { kind: "absent" } : { kind: "ok", value: stat };
9737
10128
  } catch (error) {
9738
10129
  return { kind: isAbsence(error) ? "absent" : "failed" };
@@ -9740,9 +10131,52 @@ function probeLstat(target) {
9740
10131
  }
9741
10132
  function safeReaddir(target) {
9742
10133
  try {
9743
- return fs6.readdirSync(target);
10134
+ return fs7.readdirSync(target);
10135
+ } catch {
10136
+ return null;
10137
+ }
10138
+ }
10139
+ function newEntryBudget(limit = MAX_LISTED_ENTRIES) {
10140
+ return { remaining: limit, exhausted: false };
10141
+ }
10142
+ function listDirectoryWithin(target, budget) {
10143
+ if (budget.remaining <= 0) {
10144
+ budget.exhausted = true;
10145
+ return { names: [], truncated: true };
10146
+ }
10147
+ const listing = listDirectoryBounded(target, budget.remaining);
10148
+ if (listing === null)
10149
+ return null;
10150
+ budget.remaining -= listing.names.length;
10151
+ if (listing.truncated) {
10152
+ budget.remaining = 0;
10153
+ budget.exhausted = true;
10154
+ }
10155
+ return listing;
10156
+ }
10157
+ function listDirectoryBounded(target, limit) {
10158
+ let directory;
10159
+ try {
10160
+ directory = fs7.opendirSync(target);
10161
+ } catch {
10162
+ return null;
10163
+ }
10164
+ const names = [];
10165
+ try {
10166
+ while (true) {
10167
+ const entry = directory.readSync();
10168
+ if (entry === null)
10169
+ return { names, truncated: false };
10170
+ if (names.length >= limit)
10171
+ return { names, truncated: true };
10172
+ names.push(entry.name);
10173
+ }
9744
10174
  } catch {
9745
10175
  return null;
10176
+ } finally {
10177
+ try {
10178
+ directory.closeSync();
10179
+ } catch {}
9746
10180
  }
9747
10181
  }
9748
10182
  function resolveSearchRoot(startDir) {
@@ -9751,7 +10185,7 @@ function resolveSearchRoot(startDir) {
9751
10185
  let current = start;
9752
10186
  while (true) {
9753
10187
  try {
9754
- if (fs6.lstatSync(path6.join(current, ".git"), { throwIfNoEntry: false })) {
10188
+ if (fs7.lstatSync(path6.join(current, ".git"), { throwIfNoEntry: false })) {
9755
10189
  return current;
9756
10190
  }
9757
10191
  } catch {
@@ -9767,6 +10201,22 @@ function isInside(root, candidate) {
9767
10201
  const relative = path6.relative(root, candidate);
9768
10202
  return relative !== "" && relative !== ".." && !relative.startsWith(`..${path6.sep}`) && !path6.isAbsolute(relative);
9769
10203
  }
10204
+ function symlinkedComponent(container, target) {
10205
+ if (!isInside(container, target))
10206
+ return;
10207
+ const segments = path6.relative(container, target).split(path6.sep);
10208
+ let current = container;
10209
+ for (const [index, segment] of segments.entries()) {
10210
+ current = path6.join(current, segment);
10211
+ const stat = safeLstat(current);
10212
+ if (stat === null)
10213
+ return;
10214
+ if (stat.isSymbolicLink()) {
10215
+ return index === segments.length - 1 ? "root" : "ancestor";
10216
+ }
10217
+ }
10218
+ return;
10219
+ }
9770
10220
  function classifyEntry(spec, name, stat) {
9771
10221
  if (stat === null || stat.isSymbolicLink())
9772
10222
  return "ignored";
@@ -9776,14 +10226,30 @@ function classifyEntry(spec, name, stat) {
9776
10226
  if (!stat.isFile())
9777
10227
  return "ignored";
9778
10228
  const lower = name.toLowerCase();
9779
- const indexable = lower.endsWith(".md") && !spec.excludedFiles.includes(lower);
10229
+ const extensions = spec.extensions ?? DEFAULT_EXTENSIONS;
10230
+ const indexable = extensions.some((extension) => lower.endsWith(extension)) && !spec.excludedFiles.includes(lower);
9780
10231
  return indexable ? "file" : "ignored";
9781
10232
  }
9782
- function discoverFiles(spec, directory, prefix, depth, found, warnings) {
10233
+ function wholeListing(directory) {
9783
10234
  const names = safeReaddir(directory);
10235
+ return names === null ? null : { names, truncated: false };
10236
+ }
10237
+ function listWalkedDirectory(budget, label, directory, prefix, warnings) {
10238
+ const listing = budget === undefined ? wholeListing(directory) : listDirectoryWithin(directory, budget);
10239
+ if (listing === null) {
10240
+ if (prefix !== "") {
10241
+ const shown = `${label}/${prefix.slice(0, -1)}`;
10242
+ warnings.push(cleanForWarning(`could not read directory ${shown}`));
10243
+ }
10244
+ return null;
10245
+ }
10246
+ return listing.names;
10247
+ }
10248
+ function discoverFiles(spec, label, directory, prefix, depth, found, warnings) {
10249
+ const names = listWalkedDirectory(found.budget, label, directory, prefix, warnings);
9784
10250
  if (names === null) {
9785
- const shown = prefix === "" ? spec.dir : `${spec.dir}/${prefix.slice(0, -1)}`;
9786
- warnings.push(cleanForWarning(`could not read directory ${shown}`));
10251
+ if (depth === 0)
10252
+ found.rootUnreadable = true;
9787
10253
  return;
9788
10254
  }
9789
10255
  names.sort(compareCodeUnits);
@@ -9795,7 +10261,7 @@ function discoverFiles(spec, directory, prefix, depth, found, warnings) {
9795
10261
  const entryKind = classifyEntry(spec, name, stat);
9796
10262
  if (entryKind === "directory" && depth + 1 <= MAX_DEPTH) {
9797
10263
  const child = `${prefix}${name}/`;
9798
- discoverFiles(spec, absolute, child, depth + 1, found, warnings);
10264
+ discoverFiles(spec, label, absolute, child, depth + 1, found, warnings);
9799
10265
  } else if (entryKind === "file" && stat !== null) {
9800
10266
  if (found.candidates.length >= MAX_DISCOVERED_FILES) {
9801
10267
  found.truncated = true;
@@ -9834,7 +10300,7 @@ function narrowSupersededBy(value) {
9834
10300
  return { state: "value", value };
9835
10301
  return { state: "invalid" };
9836
10302
  }
9837
- function parseCorpusDocument(kind, relativePath, content) {
10303
+ function parseMarkdownFrontmatter(content) {
9838
10304
  let text = content.charCodeAt(0) === 65279 ? content.slice(1) : content;
9839
10305
  if (!text.endsWith(`
9840
10306
  `))
@@ -9846,7 +10312,17 @@ function parseCorpusDocument(kind, relativePath, content) {
9846
10312
  if (parsed.hadFrontmatter && !isRecord(parsed.data)) {
9847
10313
  return { ok: false, reason: "frontmatter is not a mapping" };
9848
10314
  }
9849
- const data = isRecord(parsed.data) ? parsed.data : {};
10315
+ return {
10316
+ ok: true,
10317
+ data: isRecord(parsed.data) ? parsed.data : {},
10318
+ body: parsed.body
10319
+ };
10320
+ }
10321
+ function parseCorpusDocument(kind, relativePath, content) {
10322
+ const parsed = parseMarkdownFrontmatter(content);
10323
+ if (!parsed.ok)
10324
+ return parsed;
10325
+ const data = parsed.data;
9850
10326
  const fileName = relativePath.slice(relativePath.lastIndexOf("/") + 1);
9851
10327
  const title = narrowNonEmptyString(data.title) ?? narrowNonEmptyString(data.topic) ?? fileName.replace(/\.md$/i, "");
9852
10328
  return {
@@ -9868,112 +10344,1108 @@ function parseCorpusDocument(kind, relativePath, content) {
9868
10344
  }
9869
10345
  };
9870
10346
  }
9871
- function readCandidates(spec, kind, candidates, considered, warnings) {
9872
- const documents = [];
10347
+ function discoverSourceFiles(options, warnings) {
10348
+ const { directory, containedIn, walk, label, entryBudget } = options;
10349
+ const directoryProbe = probeRealpath(directory);
10350
+ const containerProbe = containedIn === undefined ? undefined : probeRealpath(containedIn);
10351
+ if (containerProbe?.kind === "failed" || directoryProbe.kind === "failed") {
10352
+ warnings.push(`could not read ${label}; not loaded`);
10353
+ return { state: "unreadable" };
10354
+ }
10355
+ if (containerProbe !== undefined && containerProbe.kind !== "ok" || directoryProbe.kind !== "ok") {
10356
+ return { state: "missing" };
10357
+ }
10358
+ const directoryReal = directoryProbe.value;
10359
+ const statProbe = probeLstat(directoryReal);
10360
+ if (statProbe.kind === "failed") {
10361
+ warnings.push(`could not read ${label}; not loaded`);
10362
+ return { state: "unreadable" };
10363
+ }
10364
+ if (statProbe.kind !== "ok" || !statProbe.value.isDirectory()) {
10365
+ return { state: "missing" };
10366
+ }
10367
+ if (containerProbe !== undefined && !isInside(containerProbe.value, directoryReal)) {
10368
+ warnings.push(`${label} resolves outside the project root; not loaded`);
10369
+ return { state: "outside" };
10370
+ }
10371
+ const found = {
10372
+ candidates: [],
10373
+ truncated: false,
10374
+ rootUnreadable: false,
10375
+ budget: entryBudget
10376
+ };
10377
+ discoverFiles(walk, label, directoryReal, "", 0, found, warnings);
10378
+ if (found.rootUnreadable) {
10379
+ warnings.push(`could not read ${label}; not loaded`);
10380
+ return { state: "unreadable" };
10381
+ }
10382
+ return {
10383
+ state: "found",
10384
+ candidates: found.candidates.sort((a, b) => compareCodeUnits(a.relative, b.relative)),
10385
+ truncated: found.truncated
10386
+ };
10387
+ }
10388
+ function readAllowance(used, maxFileBytes, sharedBytes) {
10389
+ const source = MAX_TOTAL_BYTES - used;
10390
+ const shared = sharedBytes === undefined ? Infinity : sharedBytes - used;
10391
+ const left = Math.max(Math.min(source, shared), 0);
10392
+ if (maxFileBytes <= left)
10393
+ return { limit: maxFileBytes, bound: "file" };
10394
+ return { limit: left, bound: shared < source ? "shared" : "source" };
10395
+ }
10396
+ function readWithinBudgets(options, candidate, used) {
10397
+ const { maxFileBytes, sharedBytes } = options;
10398
+ const tooLarge = `larger than ${maxFileBytes} bytes`;
10399
+ if (candidate.size > maxFileBytes) {
10400
+ return { kind: "skip", reason: tooLarge, bytes: 0 };
10401
+ }
10402
+ if (used + candidate.size > MAX_TOTAL_BYTES) {
10403
+ return { kind: "stop", bound: "source", bytes: 0 };
10404
+ }
10405
+ if (sharedBytes !== undefined && used + candidate.size > sharedBytes) {
10406
+ return { kind: "stop", bound: "shared", bytes: 0 };
10407
+ }
10408
+ const allowance = readAllowance(used, maxFileBytes, sharedBytes);
10409
+ const read = readBytesBounded(candidate.absolute, allowance.limit);
10410
+ if (read.kind === "too-large") {
10411
+ return allowance.bound === "file" ? { kind: "skip", reason: tooLarge, bytes: read.bytesRead } : { kind: "stop", bound: allowance.bound, bytes: allowance.limit };
10412
+ }
10413
+ if (read.kind !== "bytes") {
10414
+ return { kind: "skip", reason: "could not be read", bytes: 0 };
10415
+ }
10416
+ return {
10417
+ kind: "text",
10418
+ text: read.bytes.toString("utf8"),
10419
+ bytes: read.bytes.byteLength
10420
+ };
10421
+ }
10422
+ function readCandidates(options, candidates, considered, warnings) {
10423
+ const items = [];
9873
10424
  let totalBytes = 0;
9874
10425
  for (let index = 0;index < considered; index++) {
9875
10426
  const candidate = candidates[index];
9876
10427
  if (candidate === undefined)
9877
10428
  break;
9878
- const logicalPath = `${spec.dir}/${candidate.relative}`;
10429
+ const logicalPath = `${options.displayPrefix}/${candidate.relative}`;
9879
10430
  const display = cleanForWarning(logicalPath);
9880
- if (candidate.size > MAX_FILE_BYTES) {
9881
- warnings.push(`skipped ${display}: larger than ${MAX_FILE_BYTES} bytes`);
9882
- continue;
9883
- }
9884
- if (totalBytes + candidate.size > MAX_TOTAL_BYTES) {
9885
- return { documents, budgetSkipped: considered - index };
9886
- }
9887
- totalBytes += candidate.size;
9888
- let bytes;
9889
- try {
9890
- bytes = fs6.readFileSync(candidate.absolute);
9891
- } catch {
9892
- warnings.push(`skipped ${display}: could not be read`);
9893
- continue;
10431
+ const read = readWithinBudgets(options, candidate, totalBytes);
10432
+ totalBytes += read.bytes;
10433
+ if (read.kind === "stop") {
10434
+ return {
10435
+ items,
10436
+ bytesRead: totalBytes,
10437
+ budgetSkipped: considered - index,
10438
+ budgetBound: read.bound
10439
+ };
9894
10440
  }
9895
- if (bytes.byteLength > MAX_FILE_BYTES) {
9896
- warnings.push(`skipped ${display}: larger than ${MAX_FILE_BYTES} bytes`);
10441
+ if (read.kind === "skip") {
10442
+ warnings.push(`skipped ${display}: ${read.reason}`);
9897
10443
  continue;
9898
10444
  }
9899
- const parsed = parseCorpusDocument(kind, logicalPath, bytes.toString("utf8"));
10445
+ const parsed = options.parse(logicalPath, read.text);
9900
10446
  if (parsed.ok)
9901
- documents.push(parsed.document);
10447
+ items.push(parsed.value);
9902
10448
  else
9903
10449
  warnings.push(`skipped ${display}: ${parsed.reason}`);
9904
10450
  }
9905
- return { documents, budgetSkipped: 0 };
10451
+ return {
10452
+ items,
10453
+ bytesRead: totalBytes,
10454
+ budgetSkipped: 0,
10455
+ budgetBound: "source"
10456
+ };
9906
10457
  }
9907
- function loadCorpus(root, kind = "solution") {
9908
- const spec = KIND_TABLE[kind];
9909
- const warnings = [];
9910
- const none = { state: "none", documents: [], warnings };
9911
- const unreadable = () => {
9912
- warnings.push(`could not read ${spec.dir}; not loaded`);
9913
- return none;
9914
- };
9915
- const rootProbe = probeRealpath(root);
9916
- const directoryProbe = probeRealpath(path6.join(root, ...spec.dir.split("/")));
9917
- if (rootProbe.kind === "failed" || directoryProbe.kind === "failed") {
9918
- return unreadable();
9919
- }
9920
- if (rootProbe.kind !== "ok" || directoryProbe.kind !== "ok")
9921
- return none;
9922
- const rootReal = rootProbe.value;
9923
- const directoryReal = directoryProbe.value;
9924
- const statProbe = probeLstat(directoryReal);
9925
- if (statProbe.kind === "failed")
9926
- return unreadable();
9927
- if (statProbe.kind !== "ok" || !statProbe.value.isDirectory())
9928
- return none;
9929
- if (!isInside(rootReal, directoryReal)) {
9930
- warnings.push(`${spec.dir} resolves outside the project root; not loaded`);
9931
- return none;
9932
- }
9933
- const found = { candidates: [], truncated: false };
9934
- discoverFiles(spec, directoryReal, "", 0, found, warnings);
9935
- const candidates = found.candidates.sort((a, b) => compareCodeUnits(a.relative, b.relative));
10458
+ function readSourceFiles(options, candidates, truncated, warnings) {
9936
10459
  const considered = Math.min(candidates.length, MAX_FILES);
9937
- const { documents, budgetSkipped } = readCandidates(spec, kind, candidates, considered, warnings);
10460
+ const { items, bytesRead, budgetSkipped, budgetBound } = readCandidates(options, candidates, considered, warnings);
9938
10461
  const capSkipped = candidates.length - considered;
9939
10462
  if (capSkipped > 0) {
9940
- const atLeast = found.truncated ? "at least " : "";
9941
- warnings.push(`${spec.dir}: file limit of ${MAX_FILES} reached; skipped ${atLeast}${capSkipped} files`);
10463
+ const atLeast = truncated ? "at least " : "";
10464
+ warnings.push(`${options.label}: file limit of ${MAX_FILES} reached; skipped ${atLeast}${capSkipped} files`);
9942
10465
  }
9943
10466
  if (budgetSkipped > 0) {
9944
- warnings.push(`${spec.dir}: text budget of ${MAX_TOTAL_BYTES} bytes reached; skipped ${budgetSkipped} files`);
10467
+ warnings.push(budgetBound === "shared" ? `${options.label}: read budget of this call reached; skipped ${budgetSkipped} files` : `${options.label}: text budget of ${MAX_TOTAL_BYTES} bytes reached; skipped ${budgetSkipped} files`);
9945
10468
  }
9946
- return { state: "loaded", documents, warnings };
10469
+ return { items, bytesRead };
9947
10470
  }
9948
-
9949
- // src/lib/search-lexical.ts
9950
- var LEXICAL_FIELDS = [
9951
- "title",
9952
- "tags",
9953
- "module",
9954
- "symptoms",
9955
- "applies_when",
9956
- "component",
9957
- "problem_type",
9958
- "body"
9959
- ];
9960
- var BM25F_PARAMS = {
9961
- k1: 1.2,
9962
- fields: {
9963
- title: { weight: 3, b: 0 },
9964
- tags: { weight: 2.5, b: 0 },
9965
- module: { weight: 2, b: 0 },
9966
- symptoms: { weight: 3, b: 0 },
9967
- applies_when: { weight: 2, b: 0 },
9968
- component: { weight: 1.5, b: 0 },
9969
- problem_type: { weight: 1.5, b: 0 },
9970
- body: { weight: 1, b: 0.75 }
10471
+ function loadSourceFiles(discover, read) {
10472
+ const warnings = [];
10473
+ const discovery = discoverSourceFiles(discover, warnings);
10474
+ if (discovery.state !== "found") {
10475
+ return { state: discovery.state, warnings };
9971
10476
  }
9972
- };
9973
- var MAX_QUERY_TERMS = 64;
9974
- var IDENTIFIER_RUN = /[a-z0-9]+(?:[_-]+[a-z0-9]+)*/g;
9975
- var WORD = /[a-z0-9]+/g;
9976
- function tokenize(text) {
10477
+ const { items, bytesRead } = readSourceFiles(read, discovery.candidates, discovery.truncated, warnings);
10478
+ return { state: "loaded", items, bytesRead, warnings };
10479
+ }
10480
+ function loadCorpus(root, kind = "solution") {
10481
+ const spec = KIND_TABLE[kind];
10482
+ const loaded = loadSourceFiles({
10483
+ directory: path6.join(root, ...spec.dir.split("/")),
10484
+ containedIn: root,
10485
+ walk: spec,
10486
+ label: spec.dir
10487
+ }, {
10488
+ displayPrefix: spec.dir,
10489
+ label: spec.dir,
10490
+ maxFileBytes: MAX_FILE_BYTES,
10491
+ parse: (displayPath, content) => {
10492
+ const parsed = parseCorpusDocument(kind, displayPath, content);
10493
+ return parsed.ok ? { ok: true, value: parsed.document } : { ok: false, reason: parsed.reason };
10494
+ }
10495
+ });
10496
+ if (loaded.state !== "loaded") {
10497
+ return { state: "none", documents: [], warnings: loaded.warnings };
10498
+ }
10499
+ return {
10500
+ state: "loaded",
10501
+ documents: loaded.items,
10502
+ warnings: loaded.warnings
10503
+ };
10504
+ }
10505
+
10506
+ // src/lib/search-external.ts
10507
+ import path8 from "path";
10508
+
10509
+ // src/lib/search-external-deciduous.ts
10510
+ import fs8 from "fs";
10511
+ import path7 from "path";
10512
+ var RECORD_NODE_TYPES = new Set([
10513
+ "decision",
10514
+ "option",
10515
+ "outcome",
10516
+ "goal"
10517
+ ]);
10518
+ var ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
10519
+ var JSON_SUFFIX = ".json";
10520
+ var MAX_RATIONALE_CHARS_PER_EDGE = 300;
10521
+ var MAX_RATIONALE_EDGES_PER_NODE = 20;
10522
+ var MAX_RATIONALE_CHARS_PER_NODE = 2048;
10523
+ function plural(count, noun) {
10524
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
10525
+ }
10526
+ function isAbsence2(error) {
10527
+ const code = typeof error === "object" && error !== null && "code" in error ? error.code : undefined;
10528
+ return code === "ENOENT" || code === "ENOTDIR";
10529
+ }
10530
+ function probeRoot(root, containedIn) {
10531
+ let real;
10532
+ let container;
10533
+ try {
10534
+ real = fs8.realpathSync(root);
10535
+ container = containedIn === undefined ? undefined : fs8.realpathSync(containedIn);
10536
+ } catch (error) {
10537
+ return { kind: isAbsence2(error) ? "missing" : "unreadable" };
10538
+ }
10539
+ let stat;
10540
+ try {
10541
+ stat = fs8.lstatSync(real, { throwIfNoEntry: false });
10542
+ } catch (error) {
10543
+ return { kind: isAbsence2(error) ? "missing" : "unreadable" };
10544
+ }
10545
+ if (stat === undefined || !stat.isDirectory())
10546
+ return { kind: "missing" };
10547
+ if (container !== undefined && !isInside(container, real)) {
10548
+ return { kind: "outside" };
10549
+ }
10550
+ return { kind: "ok", real };
10551
+ }
10552
+ function classifyJsonEntry(directory, folder, name, skips) {
10553
+ if (!name.endsWith(JSON_SUFFIX))
10554
+ return;
10555
+ const absolute = path7.join(directory, name);
10556
+ const entry = safeLstat(absolute);
10557
+ if (entry === null)
10558
+ return;
10559
+ if (entry.isSymbolicLink()) {
10560
+ skips.symlinks++;
10561
+ return;
10562
+ }
10563
+ if (!entry.isFile())
10564
+ return;
10565
+ const stem = name.slice(0, -JSON_SUFFIX.length);
10566
+ if (!ID_PATTERN.test(stem)) {
10567
+ skips.badIds++;
10568
+ return;
10569
+ }
10570
+ return { relative: `${folder}/${name}`, stem, absolute, size: entry.size };
10571
+ }
10572
+ function folderScan(status) {
10573
+ return { status, candidates: [], truncated: false };
10574
+ }
10575
+ function statFolder(directory) {
10576
+ try {
10577
+ const stat = fs8.lstatSync(directory, { throwIfNoEntry: false });
10578
+ return stat === undefined ? { kind: "absent" } : { kind: "stat", stat };
10579
+ } catch (error) {
10580
+ return { kind: isAbsence2(error) ? "absent" : "failed" };
10581
+ }
10582
+ }
10583
+ function scanFolder(rootReal, folder, limit, skips, budget) {
10584
+ const directory = path7.join(rootReal, folder);
10585
+ const probed = statFolder(directory);
10586
+ if (probed.kind === "absent")
10587
+ return folderScan("absent");
10588
+ if (probed.kind === "failed")
10589
+ return folderScan("unreadable");
10590
+ if (probed.stat.isSymbolicLink()) {
10591
+ skips.symlinks++;
10592
+ return folderScan("absent");
10593
+ }
10594
+ if (!probed.stat.isDirectory())
10595
+ return folderScan("absent");
10596
+ const listing = listDirectoryWithin(directory, budget);
10597
+ if (listing === null)
10598
+ return folderScan("unreadable");
10599
+ const { names } = listing;
10600
+ names.sort(compareCodeUnits);
10601
+ const candidates = [];
10602
+ for (const name of names) {
10603
+ const candidate = classifyJsonEntry(directory, folder, name, skips);
10604
+ if (candidate === undefined)
10605
+ continue;
10606
+ if (candidates.length >= limit) {
10607
+ return { status: "listed", candidates, truncated: true };
10608
+ }
10609
+ candidates.push(candidate);
10610
+ }
10611
+ return { status: "listed", candidates, truncated: false };
10612
+ }
10613
+ function probeRoots(input, warnings) {
10614
+ const { name, containedIn } = input;
10615
+ const reals = [];
10616
+ let unreadable = false;
10617
+ for (const root of input.roots) {
10618
+ const symlinked = containedIn === undefined ? undefined : symlinkedComponent(containedIn, root);
10619
+ if (symlinked === "root") {
10620
+ warnings.push(`${name}: a project root is a symlink; source dropped`);
10621
+ return { kind: "dropped" };
10622
+ }
10623
+ if (symlinked === "ancestor") {
10624
+ warnings.push(`${name}: a project root passes through a symlink; source dropped`);
10625
+ return { kind: "dropped" };
10626
+ }
10627
+ const probe = probeRoot(root, containedIn);
10628
+ if (probe.kind === "outside") {
10629
+ warnings.push(`${name} resolves outside the project root; not loaded`);
10630
+ return { kind: "dropped" };
10631
+ }
10632
+ if (probe.kind === "unreadable") {
10633
+ warnings.push(`could not read ${name}; not loaded`);
10634
+ unreadable = true;
10635
+ continue;
10636
+ }
10637
+ if (probe.kind === "missing")
10638
+ continue;
10639
+ reals.push(probe.real);
10640
+ }
10641
+ return { kind: "probed", reals, unreadable };
10642
+ }
10643
+ function newTally() {
10644
+ return {
10645
+ files: new Map,
10646
+ listed: false,
10647
+ unreadable: false,
10648
+ truncated: false
10649
+ };
10650
+ }
10651
+ function scanRoots(reals, folder, tally, otherCount, skips, budget) {
10652
+ for (const real of reals) {
10653
+ const scan = scanFolder(real, folder, Math.max(MAX_DISCOVERED_FILES - otherCount - tally.files.size, 0), skips, budget);
10654
+ tally.listed = tally.listed || scan.status === "listed";
10655
+ tally.unreadable = tally.unreadable || scan.status === "unreadable";
10656
+ tally.truncated = tally.truncated || scan.truncated;
10657
+ for (const candidate of scan.candidates) {
10658
+ if (tally.files.has(candidate.relative))
10659
+ skips.sharedPaths++;
10660
+ else
10661
+ tally.files.set(candidate.relative, candidate);
10662
+ }
10663
+ }
10664
+ }
10665
+ function folderWarnings(name, folder, tally) {
10666
+ const lines = [];
10667
+ if (tally.unreadable) {
10668
+ lines.push(folder === "nodes" ? `${name}: could not read a nodes/ folder` : `${name}: could not read an edges/ folder; records load without its rationale`);
10669
+ }
10670
+ return lines;
10671
+ }
10672
+ function gather(input, skips, warnings) {
10673
+ const probed = probeRoots(input, warnings);
10674
+ if (probed.kind === "dropped")
10675
+ return { kind: "dropped" };
10676
+ const { reals } = probed;
10677
+ const limit = input.entryLimit ?? MAX_LISTED_ENTRIES;
10678
+ const budget = newEntryBudget(limit);
10679
+ const nodes = newTally();
10680
+ const edges = newTally();
10681
+ scanRoots(reals, "nodes", nodes, 0, skips, budget);
10682
+ scanRoots(reals, "edges", edges, nodes.files.size, skips, budget);
10683
+ warnings.push(...folderWarnings(input.name, "nodes", nodes), ...folderWarnings(input.name, "edges", edges));
10684
+ if (budget.exhausted) {
10685
+ warnings.push(`${input.name}: entry limit of ${limit} reached; some entries were not examined`);
10686
+ }
10687
+ return {
10688
+ kind: "gathered",
10689
+ gathered: {
10690
+ nodes: [...nodes.files.values()],
10691
+ edges: [...edges.files.values()],
10692
+ rootsFound: reals.length,
10693
+ anyNodesFolder: nodes.listed,
10694
+ anyEdgesFolder: edges.listed,
10695
+ nodesUnreadable: nodes.unreadable,
10696
+ edgesUnreadable: edges.unreadable,
10697
+ truncated: nodes.truncated || edges.truncated,
10698
+ unreadable: probed.unreadable
10699
+ }
10700
+ };
10701
+ }
10702
+ function isJsonObject(value) {
10703
+ return typeof value === "object" && value !== null && !Array.isArray(value);
10704
+ }
10705
+ function parseJsonObject(text) {
10706
+ try {
10707
+ const parsed = JSON.parse(text.charCodeAt(0) === 65279 ? text.slice(1) : text);
10708
+ return isJsonObject(parsed) ? parsed : undefined;
10709
+ } catch {
10710
+ return;
10711
+ }
10712
+ }
10713
+ function trimmedText(value) {
10714
+ return narrowNonEmptyString(value)?.trim();
10715
+ }
10716
+ function isTombstone(data) {
10717
+ const deletedAt = data.deleted_at;
10718
+ return deletedAt !== undefined && deletedAt !== null && deletedAt !== "" && deletedAt !== false;
10719
+ }
10720
+ function firstLine(text) {
10721
+ if (text === undefined)
10722
+ return;
10723
+ for (const line of text.split(/\r?\n/)) {
10724
+ const trimmed = line.trim();
10725
+ if (trimmed !== "")
10726
+ return trimmed;
10727
+ }
10728
+ return;
10729
+ }
10730
+ function addRationale(entry, text) {
10731
+ if (entry.rationale.length >= MAX_RATIONALE_EDGES_PER_NODE || entry.rationaleChars >= MAX_RATIONALE_CHARS_PER_NODE) {
10732
+ return;
10733
+ }
10734
+ const room = MAX_RATIONALE_CHARS_PER_NODE - entry.rationaleChars;
10735
+ const capped = text.slice(0, Math.min(MAX_RATIONALE_CHARS_PER_EDGE, room));
10736
+ entry.rationale.push(capped);
10737
+ entry.rationaleChars += capped.length;
10738
+ }
10739
+ function bodyOf(entry) {
10740
+ if (entry.rationale.length === 0)
10741
+ return entry.description;
10742
+ const rationale = entry.rationale.join(`
10743
+ `);
10744
+ return entry.description === "" ? rationale : `${entry.description}
10745
+
10746
+ ${rationale}`;
10747
+ }
10748
+ function skipForBudget(reader, bound, exhaust) {
10749
+ reader.budgetSkipped++;
10750
+ reader.budgetBound = bound;
10751
+ if (exhaust)
10752
+ reader.exhausted = true;
10753
+ return;
10754
+ }
10755
+ function readCandidate(candidate, input, reader, skips) {
10756
+ if (reader.exhausted)
10757
+ return skipForBudget(reader, reader.budgetBound, true);
10758
+ if (candidate.size > input.maxFileBytes) {
10759
+ skips.tooLarge++;
10760
+ return;
10761
+ }
10762
+ if (reader.bytesUsed + candidate.size > MAX_TOTAL_BYTES) {
10763
+ return skipForBudget(reader, "source", false);
10764
+ }
10765
+ if (reader.bytesUsed + candidate.size > input.remainingBytes) {
10766
+ return skipForBudget(reader, "shared", false);
10767
+ }
10768
+ const allowance = readAllowance(reader.bytesUsed, input.maxFileBytes, input.remainingBytes);
10769
+ const read = readBytesBounded(candidate.absolute, allowance.limit);
10770
+ if (read.kind === "too-large") {
10771
+ if (allowance.bound !== "file") {
10772
+ reader.bytesUsed += allowance.limit;
10773
+ return skipForBudget(reader, allowance.bound, true);
10774
+ }
10775
+ reader.bytesUsed += read.bytesRead;
10776
+ skips.tooLarge++;
10777
+ return;
10778
+ }
10779
+ if (read.kind !== "bytes") {
10780
+ skips.unreadable++;
10781
+ return;
10782
+ }
10783
+ const bytes = read.bytes;
10784
+ reader.bytesUsed += bytes.byteLength;
10785
+ const data = parseJsonObject(bytes.toString("utf8"));
10786
+ if (data === undefined)
10787
+ skips.invalid++;
10788
+ return data;
10789
+ }
10790
+ function liveNodeId(data, candidate, live, skips) {
10791
+ const changeId = data.change_id === undefined ? candidate.stem : data.change_id;
10792
+ if (typeof changeId !== "string" || !ID_PATTERN.test(changeId)) {
10793
+ skips.badIds++;
10794
+ return;
10795
+ }
10796
+ if (live.has(changeId)) {
10797
+ skips.duplicates++;
10798
+ return;
10799
+ }
10800
+ live.add(changeId);
10801
+ return changeId;
10802
+ }
10803
+ function readNodes(nodes, input, reader, skips) {
10804
+ const entries = new Map;
10805
+ const live = new Set;
10806
+ let recordCapSkipped = 0;
10807
+ for (const candidate of nodes) {
10808
+ const data = readCandidate(candidate, input, reader, skips);
10809
+ if (data === undefined || isTombstone(data))
10810
+ continue;
10811
+ const changeId = liveNodeId(data, candidate, live, skips);
10812
+ if (changeId === undefined)
10813
+ continue;
10814
+ const nodeType = trimmedText(data.node_type);
10815
+ if (nodeType === undefined || !RECORD_NODE_TYPES.has(nodeType))
10816
+ continue;
10817
+ if (entries.size >= MAX_FILES) {
10818
+ recordCapSkipped++;
10819
+ continue;
10820
+ }
10821
+ const description = trimmedText(data.description) ?? "";
10822
+ entries.set(changeId, {
10823
+ path: `${input.name}/${candidate.relative}`,
10824
+ title: trimmedText(data.title) ?? firstLine(description) ?? changeId,
10825
+ nodeType,
10826
+ description,
10827
+ rationale: [],
10828
+ rationaleChars: 0
10829
+ });
10830
+ }
10831
+ return { entries, live, recordCapSkipped };
10832
+ }
10833
+ function applyEdge(scan, from, to, rationale) {
10834
+ const { entries, live } = scan;
10835
+ const fromEntry = entries.get(from);
10836
+ if (fromEntry !== undefined && live.has(to))
10837
+ addRationale(fromEntry, rationale);
10838
+ if (from === to)
10839
+ return;
10840
+ const toEntry = entries.get(to);
10841
+ if (toEntry !== undefined && live.has(from))
10842
+ addRationale(toEntry, rationale);
10843
+ }
10844
+ function readEdges(edges, scan, input, reader, skips) {
10845
+ for (const candidate of edges) {
10846
+ const data = readCandidate(candidate, input, reader, skips);
10847
+ if (data === undefined || isTombstone(data))
10848
+ continue;
10849
+ const from = data.from_change_id;
10850
+ const to = data.to_change_id;
10851
+ if (typeof from !== "string" || typeof to !== "string") {
10852
+ skips.invalid++;
10853
+ continue;
10854
+ }
10855
+ const rationale = trimmedText(data.rationale)?.replace(/\s+/g, " ");
10856
+ if (rationale !== undefined)
10857
+ applyEdge(scan, from, to, rationale);
10858
+ }
10859
+ }
10860
+ function skipWarnings(name, skips, maxFileBytes) {
10861
+ const lines = [
10862
+ [
10863
+ skips.sharedPaths,
10864
+ `${plural(skips.sharedPaths, "file")} whose path is already provided by an earlier root`
10865
+ ],
10866
+ [
10867
+ skips.badIds,
10868
+ `${plural(skips.badIds, "file")} with an id outside [A-Za-z0-9_-]{1,64}`
10869
+ ],
10870
+ [skips.symlinks, plural(skips.symlinks, "symlink")],
10871
+ [skips.invalid, `${plural(skips.invalid, "file")} with invalid JSON`],
10872
+ [
10873
+ skips.duplicates,
10874
+ `${plural(skips.duplicates, "node")} with a duplicate change_id`
10875
+ ],
10876
+ [
10877
+ skips.unreadable,
10878
+ `${plural(skips.unreadable, "file")} that could not be read`
10879
+ ],
10880
+ [
10881
+ skips.tooLarge,
10882
+ `${plural(skips.tooLarge, "file")} larger than ${maxFileBytes} bytes`
10883
+ ]
10884
+ ];
10885
+ return lines.filter(([count]) => count > 0).map(([, text]) => `${name}: skipped ${text}`);
10886
+ }
10887
+ function limitWarnings(name, cuts) {
10888
+ const lines = [];
10889
+ if (cuts.scanTruncated) {
10890
+ lines.push(`${name}: scan limit of ${MAX_DISCOVERED_FILES} files reached; some files were not scanned`);
10891
+ }
10892
+ if (cuts.recordCapSkipped > 0) {
10893
+ lines.push(`${name}: record limit of ${MAX_FILES} reached; skipped ${plural(cuts.recordCapSkipped, "record")}`);
10894
+ }
10895
+ if (cuts.budgetSkipped > 0) {
10896
+ lines.push(cuts.budgetBound === "shared" ? `${name}: read budget of this call reached; skipped ${plural(cuts.budgetSkipped, "file")}` : `${name}: text budget of ${MAX_TOTAL_BYTES} bytes reached; skipped ${plural(cuts.budgetSkipped, "file")}`);
10897
+ }
10898
+ return lines;
10899
+ }
10900
+ function emptySkips() {
10901
+ return {
10902
+ badIds: 0,
10903
+ symlinks: 0,
10904
+ invalid: 0,
10905
+ unreadable: 0,
10906
+ tooLarge: 0,
10907
+ duplicates: 0,
10908
+ sharedPaths: 0
10909
+ };
10910
+ }
10911
+ function nothing(state, warnings) {
10912
+ return { state, records: [], warnings, bytesUsed: 0 };
10913
+ }
10914
+ function nodelessState(gathered) {
10915
+ if (!gathered.anyNodesFolder && gathered.nodesUnreadable)
10916
+ return "unreadable";
10917
+ if (!gathered.anyNodesFolder || gathered.nodes.length === 0)
10918
+ return "empty";
10919
+ return;
10920
+ }
10921
+ function bytesToRead(gathered, maxFileBytes) {
10922
+ let needed = 0;
10923
+ for (const candidate of [...gathered.nodes, ...gathered.edges]) {
10924
+ if (candidate.size <= maxFileBytes)
10925
+ needed += candidate.size;
10926
+ }
10927
+ return needed;
10928
+ }
10929
+ function loadDeciduousRecords(input) {
10930
+ const { name } = input;
10931
+ const warnings = [];
10932
+ const skips = emptySkips();
10933
+ const result = gather(input, skips, warnings);
10934
+ if (result.kind === "dropped")
10935
+ return nothing("dropped", warnings);
10936
+ const { gathered } = result;
10937
+ if (gathered.rootsFound === 0) {
10938
+ return nothing(gathered.unreadable ? "unreadable" : "missing", warnings);
10939
+ }
10940
+ const nodeless = nodelessState(gathered);
10941
+ if (nodeless === "unreadable") {
10942
+ return nothing("unreadable", warnings.map(cleanForWarning));
10943
+ }
10944
+ if (nodeless === "empty") {
10945
+ if (!gathered.anyNodesFolder) {
10946
+ warnings.push(`${name}: no nodes/ folder; nothing to index`);
10947
+ }
10948
+ warnings.push(...skipWarnings(name, skips, input.maxFileBytes));
10949
+ return nothing("empty", warnings.map(cleanForWarning));
10950
+ }
10951
+ const needed = bytesToRead(gathered, input.maxFileBytes);
10952
+ if (needed > input.remainingBytes) {
10953
+ warnings.push(`${name}: needs ${needed} bytes but only ${Math.max(input.remainingBytes, 0)} bytes of the read budget are left; source dropped`);
10954
+ return nothing("dropped", warnings.map(cleanForWarning));
10955
+ }
10956
+ const reader = {
10957
+ bytesUsed: 0,
10958
+ budgetSkipped: 0,
10959
+ budgetBound: "source",
10960
+ exhausted: false
10961
+ };
10962
+ const scan = readNodes(gathered.nodes, input, reader, skips);
10963
+ readEdges(gathered.edges, scan, input, reader, skips);
10964
+ if (!gathered.anyEdgesFolder && !gathered.edgesUnreadable) {
10965
+ warnings.push(`${name}: no edges/ folder; records carry no rationale`);
10966
+ }
10967
+ warnings.push(...skipWarnings(name, skips, input.maxFileBytes), ...limitWarnings(name, {
10968
+ scanTruncated: gathered.truncated,
10969
+ recordCapSkipped: scan.recordCapSkipped,
10970
+ budgetSkipped: reader.budgetSkipped,
10971
+ budgetBound: reader.budgetBound
10972
+ }));
10973
+ const records = [...scan.entries.values()].map((entry) => ({
10974
+ path: entry.path,
10975
+ title: entry.title,
10976
+ nodeType: entry.nodeType,
10977
+ body: bodyOf(entry)
10978
+ }));
10979
+ return {
10980
+ state: records.length === 0 ? "empty" : "loaded",
10981
+ records,
10982
+ warnings: warnings.map(cleanForWarning),
10983
+ bytesUsed: reader.bytesUsed
10984
+ };
10985
+ }
10986
+
10987
+ // src/lib/search-external.ts
10988
+ var EXTERNAL_MAX_FILE_BYTES = 1024 * 1024;
10989
+ var EXTERNAL_TOTAL_BUDGET_BYTES = 32 * 1024 * 1024;
10990
+ var MARKDOWN_WALK = { excludedFiles: [], excludedDirs: [] };
10991
+ function nothing2(source, state, warnings) {
10992
+ return {
10993
+ name: source.name,
10994
+ origin: source.origin,
10995
+ state,
10996
+ records: [],
10997
+ warnings,
10998
+ bytesUsed: 0
10999
+ };
11000
+ }
11001
+ var HEADING = /^ {0,3}#[ \t]+(.+?)(?:[ \t]+#+)?[ \t]*$/;
11002
+ var FENCE = /^ {0,3}(`{3,}|~{3,})/;
11003
+ function firstHeading(body) {
11004
+ let fence;
11005
+ for (const line of body.split(/\r?\n/)) {
11006
+ const opening = FENCE.exec(line)?.[1];
11007
+ if (opening !== undefined) {
11008
+ if (fence === undefined)
11009
+ fence = opening;
11010
+ else if (opening[0] === fence[0] && opening.length >= fence.length) {
11011
+ fence = undefined;
11012
+ }
11013
+ continue;
11014
+ }
11015
+ if (fence !== undefined)
11016
+ continue;
11017
+ const heading = HEADING.exec(line)?.[1]?.trim();
11018
+ if (heading)
11019
+ return heading;
11020
+ }
11021
+ return;
11022
+ }
11023
+ function parseMarkdownRecord(source, displayPath, content) {
11024
+ const parsed = parseMarkdownFrontmatter(content);
11025
+ if (!parsed.ok)
11026
+ return parsed;
11027
+ const fileName = displayPath.slice(displayPath.lastIndexOf("/") + 1);
11028
+ const status = narrowNonEmptyString(parsed.data.status);
11029
+ return {
11030
+ ok: true,
11031
+ value: {
11032
+ source: source.name,
11033
+ origin: source.origin,
11034
+ path: displayPath,
11035
+ title: narrowNonEmptyString(parsed.data.title) ?? firstHeading(parsed.body) ?? fileName.replace(/\.md$/i, ""),
11036
+ ...status === undefined ? {} : { status },
11037
+ body: parsed.body
11038
+ }
11039
+ };
11040
+ }
11041
+ function plural2(count, noun) {
11042
+ return `${count} ${noun}${count === 1 ? "" : "s"}`;
11043
+ }
11044
+ function mergeDiscovery(merge, discovery) {
11045
+ merge.found = true;
11046
+ merge.truncated = merge.truncated || discovery.truncated;
11047
+ for (const candidate of discovery.candidates) {
11048
+ if (merge.candidates.has(candidate.relative))
11049
+ merge.duplicates++;
11050
+ else
11051
+ merge.candidates.set(candidate.relative, candidate);
11052
+ }
11053
+ }
11054
+ function finishGather(source, merge, warnings) {
11055
+ if (!merge.found) {
11056
+ const state = merge.unreadable ? "unreadable" : "missing";
11057
+ return { kind: "done", outcome: nothing2(source, state, warnings) };
11058
+ }
11059
+ if (merge.duplicates > 0) {
11060
+ warnings.push(`${source.name}: skipped ${plural2(merge.duplicates, "file")} whose path is already provided by an earlier root`);
11061
+ }
11062
+ const candidates = [...merge.candidates.values()].sort((a, b) => compareCodeUnits(a.relative, b.relative));
11063
+ return { kind: "gathered", candidates, truncated: merge.truncated };
11064
+ }
11065
+ function projectRootProblem(source, context, root) {
11066
+ if (source.origin !== "project")
11067
+ return;
11068
+ switch (symlinkedComponent(context.checkoutRoot, root)) {
11069
+ case "root":
11070
+ return "a project root is a symlink";
11071
+ case "ancestor":
11072
+ return "a project root passes through a symlink";
11073
+ default:
11074
+ return;
11075
+ }
11076
+ }
11077
+ function gatherCandidates(source, context, warnings) {
11078
+ const { name, origin } = source;
11079
+ const merge = {
11080
+ candidates: new Map,
11081
+ truncated: false,
11082
+ found: false,
11083
+ unreadable: false,
11084
+ duplicates: 0
11085
+ };
11086
+ const drop = () => ({
11087
+ kind: "done",
11088
+ outcome: nothing2(source, "dropped", warnings)
11089
+ });
11090
+ const entryBudget = newEntryBudget(context.entryLimit);
11091
+ for (const root of source.roots) {
11092
+ const problem = projectRootProblem(source, context, root);
11093
+ if (problem !== undefined) {
11094
+ warnings.push(`${name}: ${problem}; source dropped`);
11095
+ return drop();
11096
+ }
11097
+ const discovery = discoverSourceFiles({
11098
+ directory: root,
11099
+ containedIn: origin === "project" ? context.checkoutRoot : undefined,
11100
+ walk: MARKDOWN_WALK,
11101
+ entryBudget,
11102
+ label: name
11103
+ }, warnings);
11104
+ if (discovery.state === "outside")
11105
+ return drop();
11106
+ if (discovery.state === "unreadable")
11107
+ merge.unreadable = true;
11108
+ if (discovery.state === "found")
11109
+ mergeDiscovery(merge, discovery);
11110
+ }
11111
+ if (entryBudget.exhausted) {
11112
+ warnings.push(`${name}: entry limit of ${context.entryLimit} reached; some entries were not examined`);
11113
+ }
11114
+ return finishGather(source, merge, warnings);
11115
+ }
11116
+ function bytesToRead2(candidates) {
11117
+ let needed = 0;
11118
+ for (const candidate of candidates.slice(0, MAX_FILES)) {
11119
+ if (candidate.size <= EXTERNAL_MAX_FILE_BYTES)
11120
+ needed += candidate.size;
11121
+ }
11122
+ return needed;
11123
+ }
11124
+ function loadMarkdownSource(source, context) {
11125
+ const { name, origin } = source;
11126
+ const warnings = [];
11127
+ const gathered = gatherCandidates(source, context, warnings);
11128
+ if (gathered.kind === "done")
11129
+ return gathered.outcome;
11130
+ const { candidates, truncated } = gathered;
11131
+ if (candidates.length === 0)
11132
+ return nothing2(source, "empty", warnings);
11133
+ const needed = bytesToRead2(candidates);
11134
+ if (needed > context.remainingBytes) {
11135
+ warnings.push(`${name}: needs ${needed} bytes but only ${Math.max(context.remainingBytes, 0)} bytes of the read budget are left; source dropped`);
11136
+ return nothing2(source, "dropped", warnings);
11137
+ }
11138
+ const read = readSourceFiles({
11139
+ displayPrefix: name,
11140
+ label: name,
11141
+ maxFileBytes: EXTERNAL_MAX_FILE_BYTES,
11142
+ sharedBytes: context.remainingBytes,
11143
+ parse: (displayPath, content) => parseMarkdownRecord(source, displayPath, content)
11144
+ }, candidates, truncated, warnings);
11145
+ return {
11146
+ name,
11147
+ origin,
11148
+ state: read.items.length === 0 ? "empty" : "loaded",
11149
+ records: read.items,
11150
+ warnings,
11151
+ bytesUsed: read.bytesRead
11152
+ };
11153
+ }
11154
+ function loadDeciduousSource(source, context) {
11155
+ const { name, origin } = source;
11156
+ const load = loadDeciduousRecords({
11157
+ name,
11158
+ roots: source.roots,
11159
+ containedIn: origin === "project" ? context.checkoutRoot : undefined,
11160
+ remainingBytes: context.remainingBytes,
11161
+ maxFileBytes: EXTERNAL_MAX_FILE_BYTES,
11162
+ entryLimit: context.entryLimit
11163
+ });
11164
+ return {
11165
+ name,
11166
+ origin,
11167
+ state: load.state,
11168
+ records: load.records.map((record) => ({ source: name, origin, ...record })),
11169
+ warnings: load.warnings,
11170
+ bytesUsed: load.bytesUsed
11171
+ };
11172
+ }
11173
+ var LOADERS = {
11174
+ markdown: loadMarkdownSource,
11175
+ deciduous: loadDeciduousSource
11176
+ };
11177
+ function loadSource(source, context) {
11178
+ return LOADERS[source.format](source, context);
11179
+ }
11180
+ function loadExternalSources(input) {
11181
+ const checkoutRoot = path8.resolve(input.checkoutRoot);
11182
+ let remainingBytes = input.budgetBytes ?? EXTERNAL_TOTAL_BUDGET_BYTES;
11183
+ const loads = [];
11184
+ for (const source of input.sources) {
11185
+ const { bytesUsed, ...load } = loadSource(source, {
11186
+ checkoutRoot,
11187
+ remainingBytes,
11188
+ entryLimit: input.entryBudget ?? MAX_LISTED_ENTRIES
11189
+ });
11190
+ remainingBytes -= bytesUsed;
11191
+ loads.push(load);
11192
+ }
11193
+ return loads;
11194
+ }
11195
+
11196
+ // src/lib/search-external-sources.ts
11197
+ import os3 from "os";
11198
+ import path10 from "path";
11199
+
11200
+ // src/lib/search-project-identity.ts
11201
+ import fs9 from "fs";
11202
+ import os2 from "os";
11203
+ import path9 from "path";
11204
+ var MAX_IDENTITY_FILE_BYTES = 1024 * 1024;
11205
+ function realpath(target) {
11206
+ return fs9.realpathSync.native(target);
11207
+ }
11208
+ function readSmallFile(file) {
11209
+ const read = readBytesBounded(file, MAX_IDENTITY_FILE_BYTES);
11210
+ if (read.kind !== "bytes") {
11211
+ throw new Error(`${path9.basename(file)} cannot be read as an identity file`);
11212
+ }
11213
+ return read.bytes.toString("utf8");
11214
+ }
11215
+ function firstLine2(text) {
11216
+ return (text.split(`
11217
+ `, 1)[0] ?? "").replace(/\r$/, "").trim();
11218
+ }
11219
+ function resolveFrom(base, value) {
11220
+ return path9.isAbsolute(value) ? value : path9.resolve(base, value);
11221
+ }
11222
+ function setsCoreWorktree(config) {
11223
+ let inCore = false;
11224
+ for (const raw of config.split(`
11225
+ `)) {
11226
+ let line = raw.trim();
11227
+ if (line.startsWith("[")) {
11228
+ const end = line.indexOf("]");
11229
+ if (end === -1) {
11230
+ inCore = false;
11231
+ continue;
11232
+ }
11233
+ inCore = line.slice(1, end).trim().toLowerCase() === "core";
11234
+ line = line.slice(end + 1).trim();
11235
+ }
11236
+ if (inCore && /^worktree\s*(=|$)/i.test(line)) {
11237
+ return true;
11238
+ }
11239
+ }
11240
+ return false;
11241
+ }
11242
+ function isSubmoduleGitdir(gitdir) {
11243
+ return gitdir.split(path9.sep).includes("modules");
11244
+ }
11245
+ function resolveLinkedWorktree(gitFile, gitdir) {
11246
+ const commonFile = path9.join(gitdir, "commondir");
11247
+ if (!fs9.existsSync(commonFile)) {
11248
+ return { reason: "missing-commondir" };
11249
+ }
11250
+ const commonRaw = firstLine2(readSmallFile(commonFile));
11251
+ if (commonRaw === "") {
11252
+ return { reason: "corrupt-git-file" };
11253
+ }
11254
+ const common = realpath(resolveFrom(gitdir, commonRaw));
11255
+ if (gitdir !== path9.join(common, "worktrees", path9.basename(gitdir))) {
11256
+ return { reason: "inconsistent" };
11257
+ }
11258
+ const backRaw = firstLine2(readSmallFile(path9.join(gitdir, "gitdir")));
11259
+ if (backRaw === "") {
11260
+ return { reason: "corrupt-git-file" };
11261
+ }
11262
+ if (realpath(resolveFrom(gitdir, backRaw)) !== realpath(gitFile)) {
11263
+ return { reason: "inconsistent" };
11264
+ }
11265
+ if (path9.basename(common) !== ".git") {
11266
+ return { reason: "unsupported-layout" };
11267
+ }
11268
+ if (setsCoreWorktree(readSmallFile(path9.join(common, "config")))) {
11269
+ return { reason: "core-worktree" };
11270
+ }
11271
+ return { root: path9.dirname(common) };
11272
+ }
11273
+ function deriveMainRoot(checkoutRoot) {
11274
+ const root = realpath(checkoutRoot);
11275
+ const gitPath = path9.join(root, ".git");
11276
+ let stat;
11277
+ try {
11278
+ stat = fs9.lstatSync(gitPath);
11279
+ } catch {
11280
+ return { reason: "not-a-checkout" };
11281
+ }
11282
+ if (stat.isSymbolicLink()) {
11283
+ return { reason: "symlinked-git" };
11284
+ }
11285
+ if (stat.isDirectory()) {
11286
+ return { root };
11287
+ }
11288
+ if (!stat.isFile()) {
11289
+ return { reason: "corrupt-git-file" };
11290
+ }
11291
+ const match = /^gitdir:[ \t]*(.+)$/.exec(firstLine2(readSmallFile(gitPath)));
11292
+ const claimed = match?.[1]?.trim();
11293
+ if (claimed === undefined || claimed === "") {
11294
+ return { reason: "corrupt-git-file" };
11295
+ }
11296
+ const gitdir = realpath(resolveFrom(root, claimed));
11297
+ if (!fs9.statSync(gitdir).isDirectory()) {
11298
+ return { reason: "corrupt-git-file" };
11299
+ }
11300
+ if (path9.basename(path9.dirname(gitdir)) === "worktrees") {
11301
+ return resolveLinkedWorktree(gitPath, gitdir);
11302
+ }
11303
+ if (isSubmoduleGitdir(gitdir)) {
11304
+ return { root };
11305
+ }
11306
+ return { reason: "unsupported-layout" };
11307
+ }
11308
+ function resolveProjectIdentity(checkoutRoot) {
11309
+ try {
11310
+ const derived = deriveMainRoot(checkoutRoot);
11311
+ return "root" in derived ? { status: "resolved", mainRoot: derived.root } : { status: "unresolved", reason: derived.reason };
11312
+ } catch {
11313
+ return { status: "unresolved", reason: "unreadable" };
11314
+ }
11315
+ }
11316
+ function expandHome(entry, homeDir) {
11317
+ if (entry === "~") {
11318
+ return homeDir;
11319
+ }
11320
+ if (entry.startsWith("~/") || entry.startsWith(`~${path9.sep}`)) {
11321
+ return path9.join(homeDir, entry.slice(2));
11322
+ }
11323
+ return entry;
11324
+ }
11325
+ function isAllowedProject(identity, allowList, options = {}) {
11326
+ if (identity.status !== "resolved") {
11327
+ return false;
11328
+ }
11329
+ let mainRoot;
11330
+ try {
11331
+ mainRoot = realpath(identity.mainRoot);
11332
+ } catch {
11333
+ return false;
11334
+ }
11335
+ const homeDir = options.homeDir ?? os2.homedir();
11336
+ for (const entry of allowList) {
11337
+ if (typeof entry !== "string" || entry === "") {
11338
+ continue;
11339
+ }
11340
+ const expanded = expandHome(entry, homeDir);
11341
+ if (expanded === undefined || !path9.isAbsolute(expanded)) {
11342
+ continue;
11343
+ }
11344
+ try {
11345
+ if (realpath(expanded) === mainRoot) {
11346
+ return true;
11347
+ }
11348
+ } catch {}
11349
+ }
11350
+ return false;
11351
+ }
11352
+
11353
+ // src/lib/search-external-sources.ts
11354
+ var MAX_ACTIVE_SOURCES = 8;
11355
+ var MAX_LISTED_DROPPED = 8;
11356
+ function expandHome2(root, homeDir) {
11357
+ if (root === "~")
11358
+ return homeDir;
11359
+ if (root.startsWith("~/") || root.startsWith("~\\")) {
11360
+ return path10.join(homeDir, root.slice(2));
11361
+ }
11362
+ return root;
11363
+ }
11364
+ function capWarning(dropped) {
11365
+ const listed = dropped.slice(0, MAX_LISTED_DROPPED).map((s) => s.name);
11366
+ const more = dropped.length - listed.length;
11367
+ const names = `${listed.join(", ")}${more > 0 ? ` and ${more} more` : ""}`;
11368
+ return `[systematic] more than ${MAX_ACTIVE_SOURCES} search sources are active for this project; these were dropped: ${names}. Remove a source or narrow its \`projects\` to bring the count down.`;
11369
+ }
11370
+ function mappedUserSources(declared, identity, homeDir) {
11371
+ return declared.filter((entry) => entry.projects !== undefined && isAllowedProject(identity, entry.projects, { homeDir })).map((entry) => ({
11372
+ name: entry.name,
11373
+ format: entry.format,
11374
+ roots: entry.roots.map((root) => expandHome2(root, homeDir)),
11375
+ origin: "user"
11376
+ }));
11377
+ }
11378
+ function resolveExternalSources(input) {
11379
+ const dirs = resolveUserLevelConfigDirs(input.configOptions);
11380
+ const homeDir = dirs.homeDir || os3.homedir();
11381
+ const checkoutRoot = resolveSearchRoot(input.startDir);
11382
+ const identity = input.identity ?? resolveProjectIdentity(checkoutRoot);
11383
+ const user = loadUserLevelSearchSettings(input.configOptions);
11384
+ const allow = user.settings?.allow_project_sources ?? [];
11385
+ const project = loadProjectSearchSources(checkoutRoot);
11386
+ const userSources = mappedUserSources(user.settings?.sources ?? [], identity, homeDir);
11387
+ let projectState;
11388
+ let projectSources = [];
11389
+ const proposals = project.sources.length;
11390
+ if (project.unreadable) {
11391
+ projectState = { kind: "config-unreadable" };
11392
+ } else if (proposals === 0) {
11393
+ projectState = { kind: "none" };
11394
+ } else if (isAllowedProject(identity, allow, { homeDir })) {
11395
+ projectState = { kind: "allowed", proposals };
11396
+ const shadowed = new Set(userSources.map((source) => source.name));
11397
+ projectSources = project.sources.filter((entry) => !shadowed.has(entry.name)).map((entry) => ({
11398
+ name: entry.name,
11399
+ format: entry.format,
11400
+ roots: entry.roots.map((root) => path10.resolve(checkoutRoot, root)),
11401
+ origin: "project"
11402
+ }));
11403
+ } else {
11404
+ projectState = identity.status === "resolved" ? { kind: "not-allowed", proposals, mainRoot: identity.mainRoot } : { kind: "not-allowed", proposals };
11405
+ }
11406
+ const ordered = [...userSources, ...projectSources];
11407
+ const sources = ordered.slice(0, MAX_ACTIVE_SOURCES);
11408
+ const dropped = ordered.slice(MAX_ACTIVE_SOURCES);
11409
+ return {
11410
+ sources,
11411
+ project: projectState,
11412
+ userConfigUnreadable: user.unreadable,
11413
+ warnings: [
11414
+ ...dropped.length > 0 ? [capWarning(dropped)] : [],
11415
+ ...user.warnings,
11416
+ ...project.warnings
11417
+ ]
11418
+ };
11419
+ }
11420
+
11421
+ // src/lib/search-lexical.ts
11422
+ var LEXICAL_FIELDS = [
11423
+ "title",
11424
+ "tags",
11425
+ "module",
11426
+ "symptoms",
11427
+ "applies_when",
11428
+ "component",
11429
+ "problem_type",
11430
+ "body"
11431
+ ];
11432
+ var BM25F_PARAMS = {
11433
+ k1: 1.2,
11434
+ fields: {
11435
+ title: { weight: 3, b: 0 },
11436
+ tags: { weight: 2.5, b: 0 },
11437
+ module: { weight: 2, b: 0 },
11438
+ symptoms: { weight: 3, b: 0 },
11439
+ applies_when: { weight: 2, b: 0 },
11440
+ component: { weight: 1.5, b: 0 },
11441
+ problem_type: { weight: 1.5, b: 0 },
11442
+ body: { weight: 1, b: 0.75 }
11443
+ }
11444
+ };
11445
+ var MAX_QUERY_TERMS = 64;
11446
+ var IDENTIFIER_RUN = /[a-z0-9]+(?:[_-]+[a-z0-9]+)*/g;
11447
+ var WORD = /[a-z0-9]+/g;
11448
+ function tokenize(text) {
9977
11449
  const tokens = [];
9978
11450
  for (const match of text.toLowerCase().matchAll(IDENTIFIER_RUN)) {
9979
11451
  const run = match[0];
@@ -10269,7 +11741,7 @@ function extractSnippet(document, queryTerms) {
10269
11741
  return symptoms;
10270
11742
  return sanitizeInline(document.title, MAX_SNIPPET_CHARS);
10271
11743
  }
10272
- function plural(count, singular) {
11744
+ function plural3(count, singular) {
10273
11745
  return `${count} ${singular}${count === 1 ? "" : "s"}`;
10274
11746
  }
10275
11747
  function joinOr(items) {
@@ -10295,7 +11767,7 @@ function renderHeader(result) {
10295
11767
  case "empty":
10296
11768
  return single ? `${prefix} corpus: empty (${only.dir}/ holds no indexable docs).` : `${prefix} corpus: empty (no indexable docs: ${kindTokens(corpus.kinds)}).`;
10297
11769
  case "docs": {
10298
- const counts = `${plural(corpus.docCount, "doc")}, ${plural(result.hits.length, "hit")}`;
11770
+ const counts = `${plural3(corpus.docCount, "doc")}, ${plural3(result.hits.length, "hit")}`;
10299
11771
  const searched = single ? only.dir : kindTokens(corpus.kinds);
10300
11772
  return `${prefix} ${counts}. Corpus: ${searched}.`;
10301
11773
  }
@@ -10355,6 +11827,187 @@ function renderSearchResult(result) {
10355
11827
  return truncateUtf8(lines.join(`
10356
11828
  `), MAX_OUTPUT_BYTES, TRUNCATION_MARKER);
10357
11829
  }
11830
+ var EXTERNAL_MAX_OUTPUT_BYTES = 12 * 1024;
11831
+ var EXTERNAL_TRUNCATION_MARKER = "[Systematic: search output truncated at 12 KB]";
11832
+ var EXTERNAL_PREFIX = "External sources for";
11833
+ var MAX_SECTION_NAME_CHARS = 40;
11834
+ var MAX_ROOTS_CHARS = 400;
11835
+ var MAX_NODE_TYPE_CHARS = 40;
11836
+ function proposalCount(count) {
11837
+ return Number.isSafeInteger(count) && count >= 0 ? count : 0;
11838
+ }
11839
+ function proposedNoun(count, singular) {
11840
+ return count === 1 ? singular : `${singular}s`;
11841
+ }
11842
+ function allowAdvice(count, mainRoot) {
11843
+ if (mainRoot === undefined) {
11844
+ return "Its identity could not be verified, so it cannot be allowed.";
11845
+ }
11846
+ const root = sanitizeInline(mainRoot, MAX_PATH_CHARS2);
11847
+ return `To allow ${count === 1 ? "it" : "them"}, add "${root}" to search.allow_project_sources in your user config.`;
11848
+ }
11849
+ var EXTERNAL_REPLIES = {
11850
+ noProject: "no project: there is no .git directory at or above the start directory, so there are no external sources to search.",
11851
+ noActiveSource: "no external sources are active for this project.",
11852
+ notAllowed(proposals, mainRoot) {
11853
+ const count = proposalCount(proposals);
11854
+ return `this project proposes ${count} ${proposedNoun(count, "external source")}, but it is not in search.allow_project_sources, so nothing was read. ${allowAdvice(count, mainRoot)}`;
11855
+ }
11856
+ };
11857
+ function externalPrefix(query) {
11858
+ const echoed = typeof query === "string" ? sanitizeInline(query, MAX_QUERY_ECHO_CHARS) : "";
11859
+ return `${EXTERNAL_PREFIX} "${echoed}":`;
11860
+ }
11861
+ function byteLength(text) {
11862
+ return new TextEncoder().encode(text).byteLength;
11863
+ }
11864
+ function projectLine(project) {
11865
+ switch (project.kind) {
11866
+ case "none":
11867
+ case "allowed":
11868
+ return;
11869
+ case "config-unreadable":
11870
+ return "Project sources: the project config could not be read, so it proposes none.";
11871
+ case "not-allowed": {
11872
+ const count = proposalCount(project.proposals);
11873
+ return `Project sources: ${count} ${proposedNoun(count, "proposed source")} not read: this project is not in search.allow_project_sources. ${allowAdvice(count, project.mainRoot)}`;
11874
+ }
11875
+ }
11876
+ }
11877
+ function noteAndWarningLines(notes, warnings, middle = []) {
11878
+ const lines = [];
11879
+ if (notes.length > 0) {
11880
+ lines.push(`Notes: ${notes.map((note) => sanitizeInline(note, MAX_NOTE_CHARS)).join("; ")}`);
11881
+ }
11882
+ lines.push(...middle);
11883
+ if (warnings.length > 0) {
11884
+ lines.push("Warnings:");
11885
+ for (const warning of warnings.slice(0, MAX_WARNINGS)) {
11886
+ lines.push(`- ${sanitizeInline(warning, MAX_WARNING_CHARS)}`);
11887
+ }
11888
+ const hidden = warnings.length - MAX_WARNINGS;
11889
+ if (hidden > 0)
11890
+ lines.push(`- +${hidden} more`);
11891
+ }
11892
+ return lines;
11893
+ }
11894
+ function renderExternalFixedReply(reply, context) {
11895
+ const prefix = externalPrefix(context.query);
11896
+ let lines;
11897
+ switch (reply.kind) {
11898
+ case "no-project":
11899
+ lines = [
11900
+ `${prefix} ${EXTERNAL_REPLIES.noProject}`,
11901
+ ...noteAndWarningLines(context.notes, [])
11902
+ ];
11903
+ break;
11904
+ case "not-allowed":
11905
+ lines = [
11906
+ `${prefix} ${EXTERNAL_REPLIES.notAllowed(reply.proposals, reply.mainRoot)}`,
11907
+ ...noteAndWarningLines(context.notes, [])
11908
+ ];
11909
+ break;
11910
+ case "no-active-source": {
11911
+ const state = context.project === undefined ? undefined : projectLine(context.project);
11912
+ lines = [
11913
+ `${prefix} ${EXTERNAL_REPLIES.noActiveSource}`,
11914
+ ...noteAndWarningLines(context.notes, context.warnings, state === undefined ? [] : [state])
11915
+ ];
11916
+ break;
11917
+ }
11918
+ }
11919
+ return truncateUtf8(lines.join(`
11920
+ `), EXTERNAL_MAX_OUTPUT_BYTES, EXTERNAL_TRUNCATION_MARKER);
11921
+ }
11922
+ function collapseHome(root, homeDir) {
11923
+ if (homeDir === "")
11924
+ return root;
11925
+ if (root === homeDir)
11926
+ return "~";
11927
+ for (const separator of ["/", "\\"]) {
11928
+ if (root.startsWith(`${homeDir}${separator}`)) {
11929
+ return `~${root.slice(homeDir.length)}`;
11930
+ }
11931
+ }
11932
+ return root;
11933
+ }
11934
+ function sectionHeader(section, homeDir) {
11935
+ const roots = section.roots.map((root) => collapseHome(root, homeDir)).join(", ");
11936
+ return `${sanitizeInline(section.name, MAX_SECTION_NAME_CHARS)} (${section.origin}): ${sanitizeInline(roots, MAX_ROOTS_CHARS)}`;
11937
+ }
11938
+ function sectionStateLine(section) {
11939
+ switch (section.state) {
11940
+ case "missing":
11941
+ return " missing: root not found.";
11942
+ case "empty":
11943
+ return " empty: no readable records.";
11944
+ case "unreadable":
11945
+ return " unreadable: root could not be read.";
11946
+ case "dropped":
11947
+ return " dropped: see the warnings.";
11948
+ case "loaded":
11949
+ return section.hits.length === 0 ? " No matching records." : undefined;
11950
+ }
11951
+ }
11952
+ function renderExternalHit(hit, queryTerms) {
11953
+ const { record } = hit;
11954
+ const title = `Title: ${sanitizeInline(record.title, MAX_TITLE_CHARS)}`;
11955
+ const label = record.nodeType !== undefined ? `node_type ${sanitizeInline(record.nodeType, MAX_NODE_TYPE_CHARS)}` : record.status !== undefined ? `Status: ${sanitizeInline(record.status, MAX_LABEL_CHARS)}` : undefined;
11956
+ return [
11957
+ `${hit.rank}. ${sanitizeInline(record.path, MAX_PATH_CHARS2)}`,
11958
+ ` ${label === undefined ? title : `${title} | ${label}`}`,
11959
+ ` Snippet: ${extractSnippet({ body: record.body, symptoms: [], title: record.title }, queryTerms)}`
11960
+ ];
11961
+ }
11962
+ function moreHitsLine(count) {
11963
+ return `+${count} more hits not shown`;
11964
+ }
11965
+ function hitCost(lines) {
11966
+ return 2 + byteLength(lines.join(`
11967
+ `));
11968
+ }
11969
+ function fitHits(hits, share) {
11970
+ for (let kept = hits.length;kept > 0; kept--) {
11971
+ const used = hits.slice(0, kept).reduce((sum, lines) => sum + hitCost(lines), 0);
11972
+ const more = hits.length - kept;
11973
+ const reserve = more > 0 ? 1 + byteLength(moreHitsLine(more)) : 0;
11974
+ if (used + reserve <= share)
11975
+ return { kept, more };
11976
+ }
11977
+ return { kept: 0, more: hits.length };
11978
+ }
11979
+ function renderExternalResult(result) {
11980
+ const { sections } = result;
11981
+ const hitTotal = sections.reduce((sum, { hits }) => sum + hits.length, 0);
11982
+ const state = projectLine(result.project);
11983
+ const head = [
11984
+ `${externalPrefix(result.query)} ${plural3(sections.length, "source")}, ${plural3(hitTotal, "hit")}.`,
11985
+ ...noteAndWarningLines(result.notes, result.warnings, state === undefined ? [] : [state])
11986
+ ];
11987
+ const fixed = sections.map((section) => {
11988
+ const line = sectionStateLine(section);
11989
+ return [
11990
+ sectionHeader(section, result.homeDir),
11991
+ ...line === undefined ? [] : [line]
11992
+ ];
11993
+ });
11994
+ const baseBytes = byteLength([...head, ...fixed.flatMap((lines) => ["", ...lines])].join(`
11995
+ `));
11996
+ const withHits = sections.filter(({ hits }) => hits.length > 0).length;
11997
+ const share = withHits === 0 ? 0 : Math.floor(Math.max(0, EXTERNAL_MAX_OUTPUT_BYTES - baseBytes) / withHits);
11998
+ const lines = [...head];
11999
+ sections.forEach((section, index) => {
12000
+ lines.push("", ...fixed[index] ?? []);
12001
+ const rendered = section.hits.map((hit) => renderExternalHit(hit, result.queryTerms));
12002
+ const { kept, more } = fitHits(rendered, share);
12003
+ for (const hit of rendered.slice(0, kept))
12004
+ lines.push("", ...hit);
12005
+ if (more > 0)
12006
+ lines.push(moreHitsLine(more));
12007
+ });
12008
+ return truncateUtf8(lines.join(`
12009
+ `), EXTERNAL_MAX_OUTPUT_BYTES, EXTERNAL_TRUNCATION_MARKER);
12010
+ }
10358
12011
 
10359
12012
  // src/lib/search-core.ts
10360
12013
  var SEARCH_TOOL_NAME = "systematic_search";
@@ -10368,13 +12021,19 @@ var MIN_LIMIT = 1;
10368
12021
  var MAX_LIMIT = 20;
10369
12022
  var MAX_QUERY_CHARS = 2000;
10370
12023
  var KIND_SELECTOR_ALL = "all";
12024
+ var KIND_SELECTOR_EXTERNAL = "external";
12025
+ var MAX_EXTERNAL_HITS_PER_SOURCE = 5;
10371
12026
  var DOCUMENT_KINDS = Object.keys(KIND_TABLE).filter(isDocumentKind);
10372
- var KIND_NAMES = [...DOCUMENT_KINDS, KIND_SELECTOR_ALL];
10373
- var SEARCH_TOOL_DESCRIPTION = "Search this project's durable outputs by symptom, error text, module or topic: the docs/solutions learnings, and the docs/plans, docs/brainstorms and docs/ideation documents that earlier work wrote. Returns ranked hits with path, title, kind, snippet, tags and status. A superseded document is replaced by its successor in the same kind, a successor in another kind is named on a Successor line, and a stale one carries a caution. Read a hit's path for the full text. Use it before debugging a failure or planning work that a past learning, plan, brainstorm or ideation doc may already cover.";
12027
+ var KIND_NAMES = [
12028
+ ...DOCUMENT_KINDS,
12029
+ KIND_SELECTOR_ALL,
12030
+ KIND_SELECTOR_EXTERNAL
12031
+ ];
12032
+ var SEARCH_TOOL_DESCRIPTION = "Search this project's durable outputs by symptom, error text, module or topic: the docs/solutions learnings, and the docs/plans, docs/brainstorms and docs/ideation documents that earlier work wrote. Returns ranked hits with path, title, kind, snippet, tags and status. A superseded document is replaced by its successor in the same kind, a successor in another kind is named on a Successor line, and a stale one carries a caution. Read a hit's path for the full text. Use it before debugging a failure or planning work that a past learning, plan, brainstorm or ideation doc may already cover. With kind external, a hit's path is `<source>/<path>`: the source name, then a path inside one of that source's roots, which the source's section header lists. Strip the source name to resolve it. The reply does not say which root a hit came from; with several roots, the file is under the first listed root that has it.";
10374
12033
  var SEARCH_ARG_DESCRIPTIONS = {
10375
12034
  query: `What to look for: a symptom, error message, module or topic, in plain words. At most ${MAX_QUERY_CHARS} characters.`,
10376
- kind: `Optional. The kind of durable output to search: ${DOCUMENT_KINDS.map((kind) => `"${kind}" (${KIND_TABLE[kind].dir}/)`).join(", ")}, or "${KIND_SELECTOR_ALL}" for every kind in one ranking. Defaults to "solution".`,
10377
- limit: `Optional. How many hits to return, an integer from ${MIN_LIMIT} to ${MAX_LIMIT}. Defaults to ${DEFAULT_LIMIT}.`
12035
+ kind: `Optional. The kind of durable output to search: ${DOCUMENT_KINDS.map((kind) => `"${kind}" (${KIND_TABLE[kind].dir}/)`).join(", ")}, or "${KIND_SELECTOR_ALL}" for every kind in one ranking, or "${KIND_SELECTOR_EXTERNAL}" for the external decision sources the user allowed, each ranked on its own. Defaults to "solution".`,
12036
+ limit: `Optional. How many hits to return, an integer from ${MIN_LIMIT} to ${MAX_LIMIT}. Defaults to ${DEFAULT_LIMIT}. With kind "${KIND_SELECTOR_EXTERNAL}" it applies to each source instead, and at most ${MAX_EXTERNAL_HITS_PER_SOURCE} hits per source are returned.`
10378
12037
  };
10379
12038
  var SEARCH_NOTICES = {
10380
12039
  failed: "Error: search failed.",
@@ -10393,6 +12052,7 @@ var SEARCH_ERRORS = {
10393
12052
  kindUnknown: `Error: kind must be one of: ${KIND_NAMES.join(", ")}.`,
10394
12053
  limitInvalid: `Error: limit must be an integer from ${MIN_LIMIT} to ${MAX_LIMIT}.`
10395
12054
  };
12055
+ var defaultExternalResolver = resolveExternalSources;
10396
12056
  function isSuccessorPathForm(value, kind = "solution") {
10397
12057
  const spec = KIND_TABLE[kind];
10398
12058
  if (!value.startsWith(`${spec.dir}/`))
@@ -10546,6 +12206,14 @@ function requestedKinds(value) {
10546
12206
  return DOCUMENT_KINDS;
10547
12207
  return isDocumentKind(value) ? [value] : undefined;
10548
12208
  }
12209
+ function requestedLimit(value) {
12210
+ if (value === undefined)
12211
+ return DEFAULT_LIMIT;
12212
+ if (typeof value !== "number" || !Number.isInteger(value) || value < MIN_LIMIT || value > MAX_LIMIT) {
12213
+ return;
12214
+ }
12215
+ return value;
12216
+ }
10549
12217
  function validate2(request) {
10550
12218
  const { query } = request;
10551
12219
  if (typeof query !== "string" || query.trim() === "") {
@@ -10554,19 +12222,16 @@ function validate2(request) {
10554
12222
  if (query.length > MAX_QUERY_CHARS) {
10555
12223
  return { ok: false, error: SEARCH_ERRORS.queryTooLong };
10556
12224
  }
10557
- const kinds = requestedKinds(request.kind);
12225
+ const external = request.kind === KIND_SELECTOR_EXTERNAL;
12226
+ const kinds = external ? [] : requestedKinds(request.kind);
10558
12227
  if (kinds === undefined) {
10559
12228
  return { ok: false, error: SEARCH_ERRORS.kindUnknown };
10560
12229
  }
10561
- let limit = DEFAULT_LIMIT;
10562
- if (request.limit !== undefined) {
10563
- const value = request.limit;
10564
- if (typeof value !== "number" || !Number.isInteger(value) || value < MIN_LIMIT || value > MAX_LIMIT) {
10565
- return { ok: false, error: SEARCH_ERRORS.limitInvalid };
10566
- }
10567
- limit = value;
12230
+ const limit = requestedLimit(request.limit);
12231
+ if (limit === undefined) {
12232
+ return { ok: false, error: SEARCH_ERRORS.limitInvalid };
10568
12233
  }
10569
- return { ok: true, query, kinds, limit };
12234
+ return external ? { ok: true, scope: "external", query, limit } : { ok: true, scope: "builtin", query, kinds, limit };
10570
12235
  }
10571
12236
  var SKIPPED_PREFIX = "skipped ";
10572
12237
  function skipReason(warning) {
@@ -10575,13 +12240,13 @@ function skipReason(warning) {
10575
12240
  const colon = warning.lastIndexOf(": ");
10576
12241
  return colon === -1 ? undefined : warning.slice(colon + 2);
10577
12242
  }
10578
- function plural2(count, singular) {
12243
+ function plural4(count, singular) {
10579
12244
  return `${count} ${singular}${count === 1 ? "" : "s"}`;
10580
12245
  }
10581
12246
  function skipSummary(dir, reasons) {
10582
12247
  const parts = [...reasons].sort(([a, x], [b, y]) => y - x || compareCodeUnits3(a, b)).map(([reason, count]) => `${count} ${reason}`);
10583
12248
  const total = [...reasons.values()].reduce((sum, count) => sum + count, 0);
10584
- return `${dir}: skipped ${plural2(total, "file")} (${parts.join(", ")})`;
12249
+ return `${dir}: skipped ${plural4(total, "file")} (${parts.join(", ")})`;
10585
12250
  }
10586
12251
  function aggregateWarnings(loads) {
10587
12252
  const structural = [];
@@ -10616,9 +12281,14 @@ function combinedState(kinds) {
10616
12281
  return kinds.every((report) => report.state === "none") ? "none" : "empty";
10617
12282
  }
10618
12283
  async function runSearch(request, options) {
12284
+ if (request.kind === KIND_SELECTOR_EXTERNAL && !hasCheckout(options.startDir)) {
12285
+ return externalFixedReply({ kind: "no-project" }, request.query, options);
12286
+ }
10619
12287
  const checked = validate2(request);
10620
12288
  if (!checked.ok)
10621
12289
  return { ok: false, text: checked.error };
12290
+ if (checked.scope === "external")
12291
+ return runExternalSearch(checked, options);
10622
12292
  const { query, kinds, limit } = checked;
10623
12293
  const root = resolveSearchRoot(options.startDir);
10624
12294
  const loader = options.corpusLoader ?? loadCorpus;
@@ -10640,6 +12310,7 @@ async function runSearch(request, options) {
10640
12310
  noteList.push("query cut to its first 64 distinct terms");
10641
12311
  const finish = (result) => ({
10642
12312
  ok: true,
12313
+ selector: "builtin",
10643
12314
  text: renderSearchResult(result),
10644
12315
  result
10645
12316
  });
@@ -10664,5 +12335,123 @@ async function runSearch(request, options) {
10664
12335
  }));
10665
12336
  return finish({ ...base, corpus, hits, notes: noteList });
10666
12337
  }
12338
+ function hasCheckout(startDir) {
12339
+ try {
12340
+ const root = resolveSearchRoot(startDir);
12341
+ return fs10.lstatSync(path11.join(root, ".git"), { throwIfNoEntry: false }) !== undefined;
12342
+ } catch {
12343
+ return false;
12344
+ }
12345
+ }
12346
+ function externalNotes(options, userConfigUnreadable) {
12347
+ const notes = [...options.notes ?? []];
12348
+ if (userConfigUnreadable && !notes.includes(SEARCH_CONFIG_UNREADABLE_NOTE)) {
12349
+ notes.push(SEARCH_CONFIG_UNREADABLE_NOTE);
12350
+ }
12351
+ return notes;
12352
+ }
12353
+ function externalFixedReply(reply, query, options, resolution) {
12354
+ const notes = externalNotes(options, resolution?.userConfigUnreadable === true);
12355
+ return {
12356
+ ok: true,
12357
+ selector: "external",
12358
+ text: renderExternalFixedReply(reply, {
12359
+ query,
12360
+ notes,
12361
+ warnings: resolution?.warnings ?? [],
12362
+ project: resolution?.project
12363
+ }),
12364
+ external: { kind: "fixed", reply },
12365
+ ...projectUnreadableFlag(resolution)
12366
+ };
12367
+ }
12368
+ function projectUnreadableFlag(resolution) {
12369
+ return resolution?.project.kind === "config-unreadable" ? { projectConfigUnreadable: true } : {};
12370
+ }
12371
+ function compareSections(a, b) {
12372
+ return compareCodeUnits3(a.name, b.name) || compareCodeUnits3(a.origin, b.origin);
12373
+ }
12374
+ function rankExternalSources(entries, queryTerms, limit, params = BM25F_PARAMS) {
12375
+ const cap = Math.min(limit, MAX_EXTERNAL_HITS_PER_SOURCE);
12376
+ return entries.map(({ source, load }) => {
12377
+ const records = new Map(load.records.map((record) => [record.path, record]));
12378
+ const index = buildLexicalIndex(load.records.map((record) => ({
12379
+ id: record.path,
12380
+ fields: {
12381
+ title: record.title,
12382
+ tags: record.nodeType ?? "",
12383
+ body: record.body
12384
+ }
12385
+ })), params);
12386
+ const hits = searchLexical(index, queryTerms).flatMap((hit) => {
12387
+ const record = records.get(hit.id);
12388
+ return record === undefined ? [] : [record];
12389
+ }).slice(0, cap).map((record, position) => ({ rank: position + 1, record }));
12390
+ return {
12391
+ name: source.name,
12392
+ origin: source.origin,
12393
+ roots: source.roots,
12394
+ state: load.state,
12395
+ recordCount: load.records.length,
12396
+ hits
12397
+ };
12398
+ }).sort(compareSections);
12399
+ }
12400
+ function externalWarnings(resolution, loads) {
12401
+ const dropped = loads.filter(({ state }) => state === "dropped");
12402
+ const others = loads.filter(({ state }) => state !== "dropped");
12403
+ return [
12404
+ ...dropped.flatMap(({ warnings }) => warnings),
12405
+ ...resolution.warnings,
12406
+ ...others.flatMap(({ warnings }) => warnings)
12407
+ ];
12408
+ }
12409
+ async function runExternalSearch(checked, options) {
12410
+ const { query, limit } = checked;
12411
+ const resolver = options.externalResolver ?? defaultExternalResolver;
12412
+ const resolution = resolver({
12413
+ startDir: options.startDir,
12414
+ configOptions: options.configOptions
12415
+ });
12416
+ if (resolution.sources.length === 0) {
12417
+ const { project } = resolution;
12418
+ const reply = project.kind === "not-allowed" ? {
12419
+ kind: "not-allowed",
12420
+ proposals: project.proposals,
12421
+ mainRoot: project.mainRoot
12422
+ } : { kind: "no-active-source" };
12423
+ return externalFixedReply(reply, query, options, resolution);
12424
+ }
12425
+ const loads = loadExternalSources({
12426
+ sources: resolution.sources,
12427
+ checkoutRoot: resolveSearchRoot(options.startDir)
12428
+ });
12429
+ const { terms, truncated } = uniqueQueryTerms(query);
12430
+ const entries = resolution.sources.flatMap((source, position) => {
12431
+ const load = loads[position];
12432
+ return load === undefined ? [] : [{ source, load }];
12433
+ });
12434
+ const sections = rankExternalSources(entries, terms, limit, options.lexicalParams ?? BM25F_PARAMS);
12435
+ const notes = externalNotes(options, resolution.userConfigUnreadable);
12436
+ if (truncated)
12437
+ notes.push("query cut to its first 64 distinct terms");
12438
+ const warnings = externalWarnings(resolution, loads);
12439
+ const external = {
12440
+ query,
12441
+ queryTerms: terms,
12442
+ homeDir: resolveUserLevelConfigDirs(options.configOptions).homeDir,
12443
+ project: resolution.project,
12444
+ sections,
12445
+ notes,
12446
+ warnings
12447
+ };
12448
+ return {
12449
+ ok: true,
12450
+ selector: "external",
12451
+ text: renderExternalResult(external),
12452
+ external: { kind: "sections", result: external },
12453
+ ...projectUnreadableFlag(resolution)
12454
+ };
12455
+ }
10667
12456
 
10668
- export { parseFrontmatter, parse, parseTree, modify, applyEdits, CONFIG_PROTECTED_FIELD_PATHS, _isoDateTime, _isoDate, _isoTime, _isoDuration, string, ZodISODateTime, ZodISODate, ZodISOTime, ZodISODuration, number, boolean, array, object, union, discriminatedUnion, _enum, literal, loadUserLevelSearchSettings, AgentOverlaySchema, CategoryOverlaySchema, isRecord, isPermissionSetting, isAgentMode, extractString, toSourcedPiSubagentsOverlays, resolveRouting, loadConfig, loadConfigWithSources, isAgentDisabled, collectRoutingTargets, getConfigPaths, isDiscoverableMarkdown, findAgentsInDir, extractAgentFrontmatter, findCommandsInDir, extractCommandFrontmatter, findSkillsInDir, discoverSkills, resolveSearchRoot, SEARCH_TOOL_NAME, SEARCH_ARGS, MIN_LIMIT, MAX_LIMIT, SEARCH_TOOL_DESCRIPTION, SEARCH_ARG_DESCRIPTIONS, SEARCH_NOTICES, SEARCH_CONFIG_UNREADABLE_NOTE, runSearch };
12457
+ export { parseFrontmatter, parse, parseTree, modify, applyEdits, CONFIG_PROTECTED_FIELD_PATHS, _isoDateTime, _isoDate, _isoTime, _isoDuration, string, ZodISODateTime, ZodISODate, ZodISOTime, ZodISODuration, number, boolean, array, object, union, discriminatedUnion, _enum, literal, loadUserLevelSearchSettings, AgentOverlaySchema, CategoryOverlaySchema, isRecord, isPermissionSetting, isAgentMode, extractString, toSourcedPiSubagentsOverlays, resolveRouting, loadConfig, loadConfigWithSources, isAgentDisabled, collectRoutingTargets, getConfigPaths, isDiscoverableMarkdown, findAgentsInDir, extractAgentFrontmatter, findCommandsInDir, extractCommandFrontmatter, findSkillsInDir, discoverSkills, resolveSearchRoot, SEARCH_TOOL_NAME, SEARCH_ARGS, MIN_LIMIT, MAX_LIMIT, KIND_SELECTOR_EXTERNAL, SEARCH_TOOL_DESCRIPTION, SEARCH_ARG_DESCRIPTIONS, SEARCH_NOTICES, SEARCH_CONFIG_UNREADABLE_NOTE, runSearch };