@markstanden/opencode-docs-plugin 0.4.0 → 0.5.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.
package/src/index.ts CHANGED
@@ -9,14 +9,24 @@ import { spawnSync } from "node:child_process";
9
9
  import {
10
10
  type Dirent,
11
11
  existsSync,
12
+ lstatSync,
12
13
  mkdirSync,
13
14
  readFileSync,
14
15
  readdirSync,
16
+ realpathSync,
15
17
  statSync,
16
18
  writeFileSync,
17
19
  } from "node:fs";
18
20
  import { homedir } from "node:os";
19
- import { dirname, isAbsolute, join, relative, sep } from "node:path";
21
+ import {
22
+ basename,
23
+ dirname,
24
+ isAbsolute,
25
+ join,
26
+ normalize,
27
+ relative,
28
+ sep,
29
+ } from "node:path";
20
30
  import { fileURLToPath } from "node:url";
21
31
  import { Schema } from "effect";
22
32
 
@@ -24,36 +34,50 @@ import { Schema } from "effect";
24
34
  * V2 docs plugin.
25
35
  *
26
36
  * Registers a model-callable `docs` tool so the agent can discover the
27
- * knowledge tree mid-task — the docs root is configurable (`root` option,
28
- * default `<location>/docs`): a blank search returns the most recently changed
29
- * docs (`path — Area — date — Summary`), a search ranks the docs by
30
- * how well their front-matter, path and body identifiers match, falling back
31
- * to the closest matches rather than a dead end. `add` writes knowledge,
32
- * `check` validates the convention. Reading a matched doc stays the built-in
33
- * `read`.
37
+ * knowledge lakes mid-task. The lakes are configurable (`lakes`, `lakesFile`,
38
+ * or the single-lake `root` sugar; default `<location>/docs`): a blank search
39
+ * returns the most recently changed docs (`path — Area — date — Summary`), a
40
+ * search ranks the docs by how well their front-matter, path and body
41
+ * identifiers match, falling back to the closest matches rather than a dead
42
+ * end. `add` writes knowledge, `check` validates the knowledge lakes. Reading
43
+ * a matched doc stays the built-in `read` for project files; the tool's own
44
+ * `read` serves lake docs and wiki pages.
34
45
  *
35
46
  * Everything below the default export is pure or dependency-injected so it can
36
47
  * be exercised directly against Node's built-in test runner without OpenCode.
37
48
  */
38
49
 
50
+ /** The doc classes the plugin serves. `instructions` is the implicit README source. */
51
+ export type DocClass = "instructions" | "repo" | "machine" | "global" | "wiki";
52
+
53
+ /** The classes that can be a configured lake. */
54
+ export type LakeClass = Exclude<DocClass, "instructions">;
55
+
56
+ /** The two writing conventions a lake can use. */
57
+ export type LakeProfile = "knowledge" | "wiki";
58
+
39
59
  /** One knowledge doc, as the tool sees it. */
40
60
  export interface DocEntry {
41
61
  /** Root-relative path, e.g. `decisions/choose-a-queue.md`. */
42
62
  path: string;
63
+ /** The lake name; empty for a project README (the instructions source). */
64
+ lake: string;
65
+ /** The source class. */
66
+ class: DocClass;
43
67
  /** Front-matter `Area`, else the first path segment under the root. */
44
68
  area: string;
45
69
  /** Resolved at read time: last git commit, else file mtime. Never front-matter. */
46
70
  date: string;
47
71
  /** First `# ` heading in the body, else the file name. */
48
72
  title: string;
49
- /** Front-matter `Summary`, else an empty string. */
73
+ /** Front-matter `Summary`, else the wiki's first prose paragraph, else "". */
50
74
  summary: string;
51
- /** Front-matter `Status: superseded` — the doc is marked as replaced. */
52
- superseded: boolean;
53
75
  /** Front-matter `Keywords`, split on commas and trimmed. */
54
76
  keywords: readonly string[];
55
77
  /** Body H2/H3 headings plus inline `code` spans, trimmed and de-duplicated. */
56
78
  identifiers: readonly string[];
79
+ /** Wiki prose and table text; empty for knowledge docs and READMEs. */
80
+ text: string;
57
81
  }
58
82
 
59
83
  /**
@@ -65,7 +89,13 @@ export const DOCS_INPUT = Schema.Struct({
65
89
  search: Schema.optional(
66
90
  Schema.String.annotate({
67
91
  description:
68
- "Case-insensitive search over title, path, keywords, body headings and code spans, area and summary. Omit or leave blank to list the most recently changed docs; a partial match returns the closest docs.",
92
+ "Case-insensitive search over title, path, keywords, body headings and code spans, area, summary and wiki prose. Omit or leave blank to list the most recently changed docs; a partial match returns the closest docs.",
93
+ }),
94
+ ),
95
+ lake: Schema.optional(
96
+ Schema.String.annotate({
97
+ description:
98
+ "Optional lake name. Search and check are limited to it; read and add target it. Omit to search every lake, read the unique match, or add to the first writable repo lake.",
69
99
  }),
70
100
  ),
71
101
  add: Schema.optional(
@@ -73,43 +103,36 @@ export const DOCS_INPUT = Schema.Struct({
73
103
  path: Schema.optional(
74
104
  Schema.String.annotate({
75
105
  description:
76
- "Update target, e.g. `guides/getting-started.md` (relative to the docs root). Omit to create `<Area>/<slug>.md` from the H1.",
106
+ "Update target, e.g. `guides/getting-started.md` (relative to the lake root). Omit to create `<Area>/<slug>.md` from the H1.",
77
107
  }),
78
108
  ),
79
109
  metadata: Schema.Struct({
80
110
  Area: Schema.String,
81
111
  Keywords: Schema.Array(Schema.String),
82
112
  Summary: Schema.String,
83
- Status: Schema.optional(
84
- Schema.Union([
85
- Schema.Literal("active"),
86
- Schema.Literal("superseded"),
87
- ]),
88
- ),
89
- Supersedes: Schema.optional(Schema.String),
90
113
  }).annotate({
91
114
  description:
92
115
  "Front-matter metadata; serialised, never copied. All three fields are required and must not be blank.",
93
116
  }),
94
117
  body: Schema.String.annotate({
95
118
  description:
96
- "Markdown body: an H1, then the claim sections (Facts / Inferred / Tested / Open questions).",
119
+ "Markdown body: an H1, then the claim sections (Facts / Inferred / Tested / Failed approaches / Open questions).",
97
120
  }),
98
121
  }).annotate({
99
122
  description:
100
- "The only mutating mode: writes a new knowledge doc, or wholesale-replaces the doc at `path`. Blank required metadata, path traversal, a non-.md target, a body without an H1, and a same-area near-twin (when `path` is omitted) are all refused.",
123
+ "The only mutating mode: writes a new knowledge doc, or wholesale-replaces the doc at `path`. Blank required metadata, path traversal, a non-.md target, a symlinked target, and a same-area near-twin (when `path` is omitted) are all refused. Wiki lakes are read-only.",
101
124
  }),
102
125
  ),
103
126
  check: Schema.optional(
104
127
  Schema.Boolean.annotate({
105
128
  description:
106
- "Validate every doc under the docs root against the convention: required front-matter, an H1, claim sections, no hand-maintained Date, a distinctive H1+Keywords identity, a Supersedes naming a doc in the tree, and same-area near-twins. An empty report means compliant.",
129
+ "Validate every knowledge doc against the convention: required front-matter, an H1, claim sections, no hand-maintained Date, a distinctive H1+Keywords identity, same-area near-twins, broken relative links, and a soft length note. Wiki lakes and READMEs are not validated.",
107
130
  }),
108
131
  ),
109
132
  read: Schema.optional(
110
133
  Schema.String.annotate({
111
134
  description:
112
- "A root-relative doc path to return verbatim — front-matter and body — so find-then-read is one call. Lake docs only: project files and READMEs stay with the built-in `read` tool.",
135
+ "A root-relative doc path, or `lake:path`, to return verbatim — front-matter and body — so find-then-read is one call. Lake docs and wiki pages only: project READMEs stay with the built-in `read` tool.",
113
136
  }),
114
137
  ),
115
138
  });
@@ -174,8 +197,6 @@ export const FRONT_MATTER_FIELDS = [
174
197
  { name: "Area", required: true },
175
198
  { name: "Keywords", required: true },
176
199
  { name: "Summary", required: true },
177
- { name: "Status", required: false },
178
- { name: "Supersedes", required: false },
179
200
  ] as const;
180
201
 
181
202
  /**
@@ -187,13 +208,10 @@ export const FRONT_MATTER_FIELDS = [
187
208
  * @returns The full file contents to write.
188
209
  */
189
210
  export function serializeDoc(metadata: DocMetadata, body: string): string {
190
- const lines = FRONT_MATTER_FIELDS.flatMap(({ name }) => {
211
+ const lines = FRONT_MATTER_FIELDS.map(({ name }) => {
191
212
  const value = metadata[name];
192
- if (value === undefined) {
193
- return [];
194
- }
195
213
  const text = Array.isArray(value) ? value.join(", ") : value;
196
- return [`${name}: ${text}`];
214
+ return `${name}: ${text}`;
197
215
  });
198
216
  return `---\n${lines.join("\n")}\n---\n\n${body.replace(/(?<!\n)\n+$/, "")}\n`;
199
217
  }
@@ -304,27 +322,156 @@ export function identifiersOf(body: string): string[] {
304
322
  return [...new Set([...headings, ...spans])];
305
323
  }
306
324
 
325
+ /** The text inside inline links, dropping the URL; manual scan keeps it linear. */
326
+ function stripInlineLinks(text: string): string {
327
+ let result = "";
328
+ let index = 0;
329
+ while (index < text.length) {
330
+ if (text[index] === "[" && text[index - 1] !== "!") {
331
+ const close = text.indexOf("](", index + 1);
332
+ if (close !== -1) {
333
+ const end = text.indexOf(")", close + 2);
334
+ if (end !== -1) {
335
+ result += text.slice(index + 1, close);
336
+ index = end + 1;
337
+ continue;
338
+ }
339
+ }
340
+ }
341
+ result += text[index];
342
+ index += 1;
343
+ }
344
+ return result;
345
+ }
346
+
347
+ /** Strip inline Markdown from a prose fragment: links keep their text, code keeps its content. */
348
+ function plainProse(text: string): string {
349
+ return stripInlineLinks(text)
350
+ .replace(/`([^`]+)`/g, "$1")
351
+ .replace(/\*\*([^*]+)\*\*/g, "$1")
352
+ .replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, "$1")
353
+ .replace(/__([^_]+)__/g, "$1")
354
+ .replace(/(?<!_)_([^_]+)_(?!_)/g, "$1")
355
+ .replace(/\s+/g, " ")
356
+ .trim();
357
+ }
358
+
359
+ /** The body with fenced code blocks removed. */
360
+ function withoutFences(body: string): string {
361
+ const lines: string[] = [];
362
+ let fenced = false;
363
+ for (const line of body.split("\n")) {
364
+ if (isFence(line)) {
365
+ fenced = !fenced;
366
+ continue;
367
+ }
368
+ if (!fenced) {
369
+ lines.push(line);
370
+ }
371
+ }
372
+ return lines.join("\n");
373
+ }
374
+
375
+ /** Block prefixes that are never a wiki summary. */
376
+ const SUMMARY_SKIP_PREFIXES = ["#", "|", ">", "<", "---", ":::", "!["] as const;
377
+
378
+ /** Whether a blank-line-separated block is prose worth summarising. */
379
+ function isSummaryBlock(block: string): boolean {
380
+ const trimmed = block.trim();
381
+ if (trimmed === "" || trimmed.startsWith("[![")) {
382
+ return false;
383
+ }
384
+ if (SUMMARY_SKIP_PREFIXES.some((prefix) => trimmed.startsWith(prefix))) {
385
+ return false;
386
+ }
387
+ if (/^([-*+]|\d+\.)\s/.test(trimmed)) {
388
+ return false;
389
+ }
390
+ return plainProse(trimmed) !== "";
391
+ }
392
+
393
+ /** The wiki summary: the first prose paragraph after the H1, capped. */
394
+ export function summaryOf(body: string): string {
395
+ const text = withoutFences(body);
396
+ const heading = text.indexOf("# ");
397
+ if (heading === -1) {
398
+ return "";
399
+ }
400
+ const lineEnd = text.indexOf("\n", heading);
401
+ const after = lineEnd === -1 ? "" : text.slice(lineEnd + 1);
402
+ for (const block of after.split(/\n\s*\n/)) {
403
+ if (isSummaryBlock(block)) {
404
+ return plainProse(block).slice(0, 240);
405
+ }
406
+ }
407
+ return "";
408
+ }
409
+
410
+ /** The indexable wiki prose: the whole body, front-matter already stripped. */
411
+ export function proseOf(body: string): string {
412
+ return body;
413
+ }
414
+
415
+ /** The doc's area: front-matter `Area`, else the first path segment. */
416
+ function areaOf(meta: Record<string, string>, path: string): string {
417
+ if (meta.Area !== undefined) {
418
+ return meta.Area;
419
+ }
420
+ const segments = path.split("/");
421
+ return segments.length > 1 ? (segments[0] ?? "") : "";
422
+ }
423
+
424
+ /** The doc's summary: front-matter, else a derived wiki paragraph, else "". */
425
+ function summaryFor(
426
+ meta: Record<string, string>,
427
+ body: string,
428
+ profile: LakeProfile,
429
+ ): string {
430
+ if (meta.Summary !== undefined) {
431
+ return meta.Summary;
432
+ }
433
+ return profile === "wiki" ? summaryOf(body) : "";
434
+ }
435
+
307
436
  /**
308
437
  * Build a doc entry from its path, raw contents and the date to show.
309
438
  * @param path Repo-relative doc path.
310
439
  * @param raw Full file contents.
311
440
  * @param date The resolved date (last git commit, else file mtime). Front-matter
312
441
  * `Date` is deliberately ignored — the date is derived, never hand-maintained.
442
+ * @param options The source lake/class and the profile's indexing rules.
443
+ * @param options.lake The lake name stamped on the entry.
444
+ * @param options.class The source class stamped on the entry.
445
+ * @param options.profile The writing convention; `wiki` derives prose and summary.
313
446
  * @returns The entry the tool ranks and renders.
314
447
  */
315
- export function toEntry(path: string, raw: string, date: string): DocEntry {
448
+ export function toEntry(
449
+ path: string,
450
+ raw: string,
451
+ date: string,
452
+ options: {
453
+ lake?: string;
454
+ class?: DocClass;
455
+ profile?: LakeProfile;
456
+ } = {},
457
+ ): DocEntry {
458
+ const {
459
+ lake = "",
460
+ class: docClass = "repo",
461
+ profile = "knowledge",
462
+ } = options;
316
463
  const { meta, body } = parseFrontMatter(raw);
317
- const segments = path.split("/");
318
- const area = meta.Area ?? (segments.length > 1 ? (segments[0] ?? "") : "");
319
464
  return {
320
465
  path,
321
- area,
466
+ lake,
467
+ class: docClass,
468
+ area: areaOf(meta, path),
322
469
  date,
323
470
  title: titleOf(path, body),
324
- summary: meta.Summary ?? "",
325
- superseded: meta.Status?.trim().toLowerCase() === "superseded",
471
+ summary: summaryFor(meta, body, profile),
326
472
  keywords: splitKeywords(meta.Keywords ?? ""),
327
473
  identifiers: identifiersOf(body),
474
+ text: profile === "wiki" ? proseOf(body) : "",
328
475
  };
329
476
  }
330
477
 
@@ -374,6 +521,7 @@ export function matches(entry: DocEntry, query: string): boolean {
374
521
  entry.area,
375
522
  entry.title,
376
523
  entry.summary,
524
+ entry.text,
377
525
  ...entry.keywords,
378
526
  ...entry.identifiers,
379
527
  ]
@@ -395,6 +543,7 @@ export const FIELD_WEIGHTS = {
395
543
  identifiers: 3,
396
544
  area: 2,
397
545
  summary: 1,
546
+ text: 1,
398
547
  } as const;
399
548
 
400
549
  /** The weighted searchable fields, weights from {@link FIELD_WEIGHTS}. */
@@ -407,6 +556,7 @@ const FIELDS: ReadonlyArray<
407
556
  [FIELD_WEIGHTS.identifiers, (entry) => entry.identifiers],
408
557
  [FIELD_WEIGHTS.area, (entry) => [entry.area]],
409
558
  [FIELD_WEIGHTS.summary, (entry) => [entry.summary]],
559
+ [FIELD_WEIGHTS.text, (entry) => [entry.text]],
410
560
  ];
411
561
 
412
562
  /**
@@ -465,13 +615,37 @@ export interface RankedDocs {
465
615
  }
466
616
 
467
617
  /**
468
- * Most recently changed first, then the active doc of a same-date tie, then
469
- * path ascending; an empty date is oldest.
618
+ * Class precedence when scores tie. Narrow, local, vetted first.
619
+ * The single source of the order — dial here.
620
+ */
621
+ export const CLASS_RANK: Readonly<Record<DocClass, number>> = {
622
+ repo: 0,
623
+ instructions: 1,
624
+ global: 2,
625
+ machine: 3,
626
+ wiki: 4,
627
+ };
628
+
629
+ /**
630
+ * How many results each source reserves in the first pass, so a broad wiki
631
+ * cannot bury a focused repo doc. A reservation, not a cap: unused slots are
632
+ * filled by score. `0` disables reservation. The single knob — tune here.
633
+ */
634
+ export const DEFAULT_PER_LAKE_QUOTA = 3;
635
+
636
+ /** The reservation key: a lake name, or the implicit instructions source. */
637
+ function sourceKey(entry: DocEntry): string {
638
+ return entry.lake === "" ? "instructions" : entry.lake;
639
+ }
640
+
641
+ /**
642
+ * Most recently changed first, then class precedence, then path ascending;
643
+ * an empty date is oldest.
470
644
  */
471
645
  function byRecency(a: DocEntry, b: DocEntry): number {
472
646
  return (
473
647
  b.date.localeCompare(a.date) ||
474
- Number(a.superseded) - Number(b.superseded) ||
648
+ CLASS_RANK[a.class] - CLASS_RANK[b.class] ||
475
649
  a.path.localeCompare(b.path)
476
650
  );
477
651
  }
@@ -489,10 +663,41 @@ function capped(entries: DocEntry[]): Pick<RankedDocs, "entries" | "omitted"> {
489
663
  }
490
664
 
491
665
  /**
492
- * Sort by score, best first, then the active doc of a same-score tie, then
493
- * recency, then the stable path order. Each doc is scored once into the
494
- * decorated list — the comparator sorts on precomputed points instead of
495
- * re-scoring every comparison.
666
+ * Fill the result cap with a per-source reservation first, then the best
667
+ * leftovers. This keeps every configured source visible without hard-capping a
668
+ * single-lake tree.
669
+ * @param entries Docs in ranked order.
670
+ * @param quota Results reserved per source; `0` disables reservation.
671
+ * @returns The shown entries and the omitted count.
672
+ */
673
+ function reserved(
674
+ entries: DocEntry[],
675
+ quota: number,
676
+ ): Pick<RankedDocs, "entries" | "omitted"> {
677
+ if (quota <= 0) {
678
+ return capped(entries);
679
+ }
680
+ const first: DocEntry[] = [];
681
+ const rest: DocEntry[] = [];
682
+ const counts = new Map<string, number>();
683
+ for (const entry of entries) {
684
+ const key = sourceKey(entry);
685
+ const seen = counts.get(key) ?? 0;
686
+ if (seen < quota) {
687
+ counts.set(key, seen + 1);
688
+ first.push(entry);
689
+ } else {
690
+ rest.push(entry);
691
+ }
692
+ }
693
+ return capped([...first, ...rest]);
694
+ }
695
+
696
+ /**
697
+ * Sort by score, best first, then class precedence, then recency, then the
698
+ * stable path order. Each doc is scored once into the decorated list — the
699
+ * comparator sorts on precomputed points instead of re-scoring every
700
+ * comparison.
496
701
  */
497
702
  function byScore(
498
703
  entries: readonly DocEntry[],
@@ -503,7 +708,7 @@ function byScore(
503
708
  .sort(
504
709
  (a, b) =>
505
710
  b.points - a.points ||
506
- Number(a.doc.superseded) - Number(b.doc.superseded) ||
711
+ CLASS_RANK[a.doc.class] - CLASS_RANK[b.doc.class] ||
507
712
  b.doc.date.localeCompare(a.doc.date) ||
508
713
  a.doc.path.localeCompare(b.doc.path),
509
714
  )
@@ -514,45 +719,58 @@ function byScore(
514
719
  * Rank the docs for a query. A blank query returns the most recently changed
515
720
  * docs as the catalogue; a query that some doc satisfies fully returns those
516
721
  * docs ranked; otherwise it falls back to the docs that match any term as a
517
- * substring, ranked, flagged `closest`. Every outcome caps at
722
+ * substring, ranked, flagged `closest`. Every outcome reserves
723
+ * {@link DEFAULT_PER_LAKE_QUOTA} results per source, caps at
518
724
  * {@link MAX_RESULTS} and reports the cut as `omitted`.
519
725
  * @param entries Docs to rank, already path-ordered.
520
726
  * @param query Raw query text.
727
+ * @param options Ranking options.
728
+ * @param options.perLakeQuota Results reserved per source; `0` disables.
521
729
  * @returns The docs to show, the fallback flag, and the omitted count.
522
730
  */
523
731
  export function rankDocs(
524
732
  entries: readonly DocEntry[],
525
733
  query: string,
734
+ options: { perLakeQuota?: number } = {},
526
735
  ): RankedDocs {
736
+ const quota = options.perLakeQuota ?? DEFAULT_PER_LAKE_QUOTA;
527
737
  const terms = termsOf(query);
528
738
  if (terms.length === 0) {
529
- return { ...capped([...entries].sort(byRecency)), closest: false };
739
+ return {
740
+ ...reserved([...entries].sort(byRecency), quota),
741
+ closest: false,
742
+ };
530
743
  }
531
744
  const full = entries.filter((entry) => matches(entry, query));
532
745
  if (full.length > 0) {
533
- return { ...capped(byScore(full, terms)), closest: false };
746
+ return { ...reserved(byScore(full, terms), quota), closest: false };
534
747
  }
535
748
  const partial = entries.filter((entry) => score(entry, terms) > 0);
536
749
  return {
537
- ...capped(byScore(partial, terms)),
750
+ ...reserved(byScore(partial, terms), quota),
538
751
  closest: partial.length > 0,
539
752
  };
540
753
  }
541
754
 
542
755
  /**
543
756
  * Render matched docs as `path — Area — date` lines with the summary indented
544
- * beneath; a superseded doc carries a `(superseded)` marker. A closest-match
545
- * fallback set is labelled; a cap cut is counted in a trailing note; nothing
546
- * matched is stated plainly.
757
+ * beneath; a non-primary lake is prefixed `lake:` so provenance is clear. A
758
+ * closest-match fallback set is labelled; a cap cut is counted in a trailing
759
+ * note; nothing matched is stated plainly.
547
760
  * @param entries Docs to render, already ranked and capped.
548
761
  * @param options Render options.
549
762
  * @param options.closest True when the entries are a closest-match fallback.
550
763
  * @param options.omitted Docs cut by the cap, shown as a trailing note.
764
+ * @param options.primaryLake The lake rendered without a prefix (the local repo).
551
765
  * @returns The tool's text result.
552
766
  */
553
767
  export function render(
554
768
  entries: readonly DocEntry[],
555
- options: { closest?: boolean; omitted?: number } = {},
769
+ options: {
770
+ closest?: boolean;
771
+ omitted?: number;
772
+ primaryLake?: string;
773
+ } = {},
556
774
  ): string {
557
775
  if (entries.length === 0) {
558
776
  return "No knowledge docs matched.";
@@ -562,12 +780,17 @@ export function render(
562
780
  const parts = [entry.area, entry.date].filter(
563
781
  (part) => part !== "",
564
782
  );
565
- if (entry.superseded) {
566
- parts.push("(superseded)");
567
- }
568
783
  const meta = parts.join(" — ");
569
784
  const head = meta === "" ? entry.path : `${entry.path} — ${meta}`;
570
- return entry.summary === "" ? head : `${head}\n ${entry.summary}`;
785
+ const labelled =
786
+ entry.class === "instructions"
787
+ ? `instructions:${head}`
788
+ : entry.lake !== "" && entry.lake !== options.primaryLake
789
+ ? `${entry.lake}:${head}`
790
+ : head;
791
+ return entry.summary === ""
792
+ ? labelled
793
+ : `${labelled}\n ${entry.summary}`;
571
794
  })
572
795
  .join("\n");
573
796
  const headed = options.closest
@@ -599,22 +822,74 @@ export function isHelp(query: string): boolean {
599
822
  export function explainRanking(): string {
600
823
  return [
601
824
  "How docs search ranks:",
602
- `- Fields weighted: title ${FIELD_WEIGHTS.title} > path ${FIELD_WEIGHTS.path} > keywords ${FIELD_WEIGHTS.keywords} > identifiers ${FIELD_WEIGHTS.identifiers} > area ${FIELD_WEIGHTS.area} > summary ${FIELD_WEIGHTS.summary}.`,
825
+ `- Fields weighted: title ${FIELD_WEIGHTS.title} > path ${FIELD_WEIGHTS.path} > keywords ${FIELD_WEIGHTS.keywords} > identifiers ${FIELD_WEIGHTS.identifiers} > area ${FIELD_WEIGHTS.area} > summary ${FIELD_WEIGHTS.summary} > wiki prose ${FIELD_WEIGHTS.text}.`,
603
826
  `- Each term scores its best field as weight × quality; quality is exact > prefix > substring.`,
604
827
  `- Terms shorter than MIN_TERM_LENGTH (${MIN_TERM_LENGTH}) are ignored unless they are the whole query.`,
605
- "- Search also covers nested `README.md` files in the project tree, honouring `.gitignore` (dependency and build directories are skipped when the project is not in git). They are searchable only: `add` and `check` ignore them.",
606
- `- Every call returns at most MAX_RESULTS (${MAX_RESULTS}), best matches first, then the most recently edited; anything cut is counted as "… and N more not shown."`,
828
+ "- Search covers every configured lake and nested project `README.md` files, honouring `.gitignore` (dependency and build directories are skipped when the project is not in git). READMEs are searchable only: `add` and `check` ignore them, and their results are prefixed `instructions:` so they read with the built-in `read` tool.",
829
+ `- Each source reserves up to ${DEFAULT_PER_LAKE_QUOTA} results (configurable), then leftover slots fill by score. Every call returns at most MAX_RESULTS (${MAX_RESULTS}), best matches first, then the most recently edited; anything cut is counted as "… and N more not shown."`,
830
+ "- Class precedence on a tie: repo > instructions > global > machine > wiki.",
607
831
  '- Outcomes: a blank query lists the most recently changed docs; a doc carrying every term ranks the full matches; otherwise the docs matching any term as a substring are listed under "Closest matches"; nothing hits is "No knowledge docs matched."',
608
832
  ].join("\n");
609
833
  }
610
834
 
835
+ /** One configured lake, as the help text describes it. */
836
+ export interface LakeInfo {
837
+ /** The lake's addressing name. */
838
+ name: string;
839
+ /** The source class. */
840
+ class: DocClass;
841
+ /** The absolute lake root. */
842
+ root: string;
843
+ /** Whether `add` may write to it. */
844
+ writable: boolean;
845
+ /** Whether `check` validates it. */
846
+ validated: boolean;
847
+ }
848
+
849
+ /**
850
+ * Describe the configured lakes, then the ranking. The agent learns where
851
+ * knowledge lives without loading the full convention.
852
+ * @param lakes The configured lakes.
853
+ * @returns The help text.
854
+ */
855
+ export function helpText(lakes: readonly LakeInfo[]): string {
856
+ const lines = ["Configured lakes:"];
857
+ for (const lake of lakes) {
858
+ const access = lake.writable ? "writable" : "read-only";
859
+ const checked = lake.validated ? "checked" : "not checked";
860
+ lines.push(
861
+ `- ${lake.name} (${lake.class}, ${access}, ${checked}): ${lake.root}`,
862
+ );
863
+ }
864
+ lines.push(
865
+ "- instructions (read-only, not checked): nested project README.md " +
866
+ "files; results are prefixed `instructions:` and read with the " +
867
+ "built-in read tool",
868
+ );
869
+ return `${lines.join("\n")}\n\n${explainRanking()}`;
870
+ }
871
+
611
872
  /** A resolved `docs` tool call: exactly one verb wins (`add` > `check` > `read` > `help` > `search`). */
612
873
  export type DocsRequest =
613
- | { verb: "add"; add: DocsAdd }
614
- | { verb: "check" }
615
- | { verb: "read"; read: string }
874
+ | { verb: "add"; add: DocsAdd; lake?: string }
875
+ | { verb: "check"; lake?: string }
876
+ | { verb: "read"; read: string; lake?: string }
616
877
  | { verb: "help" }
617
- | { verb: "search"; search: string };
878
+ | { verb: "search"; search: string; lake?: string };
879
+
880
+ /** The trimmed lake selector, or undefined when unset. */
881
+ function lakeOf(input: DocsInput): string | undefined {
882
+ const lake = input.lake?.trim() ?? "";
883
+ return lake === "" ? undefined : lake;
884
+ }
885
+
886
+ /** Add a lake selector to a request when one was given. */
887
+ function requestWithLake<T extends object>(
888
+ request: T,
889
+ lake: string | undefined,
890
+ ): T {
891
+ return lake === undefined ? request : ({ ...request, lake } as T);
892
+ }
618
893
 
619
894
  /**
620
895
  * Resolve a tool call to one verb. An explicit `add` is never downgraded; a
@@ -624,31 +899,36 @@ export type DocsRequest =
624
899
  * @param input.search The raw search text, when given.
625
900
  * @param input.add The typed add DTO, when given.
626
901
  * @param input.check The check flag, when given.
627
- * @param input.read The root-relative doc path to read, when given.
902
+ * @param input.read The root-relative doc path (or `lake:path`) to read, when given.
903
+ * @param input.lake The optional lake selector.
628
904
  * @returns The single resolved request.
629
905
  */
630
906
  export function resolveRequest(input: DocsInput): DocsRequest {
907
+ const lake = lakeOf(input);
631
908
  if (input.add !== undefined) {
632
- return { verb: "add", add: input.add };
909
+ return requestWithLake({ verb: "add", add: input.add }, lake);
633
910
  }
634
911
  if (input.check === true) {
635
- return { verb: "check" };
912
+ return requestWithLake({ verb: "check" }, lake);
636
913
  }
637
914
  const read = input.read?.trim() ?? "";
638
915
  if (read !== "") {
639
- return { verb: "read", read };
916
+ return requestWithLake({ verb: "read", read }, lake);
640
917
  }
641
918
  if (isHelp(input.search ?? "")) {
642
919
  return { verb: "help" };
643
920
  }
644
- return { verb: "search", search: input.search ?? "" };
921
+ return requestWithLake(
922
+ { verb: "search", search: input.search ?? "" },
923
+ lake,
924
+ );
645
925
  }
646
926
 
647
927
  /**
648
928
  * The newest commit date per path, in one `git log` pass over all paths.
649
929
  * `git log` lists commits newest first, so the first date seen for a path is
650
930
  * its latest edit; paths absent from history are absent from the map.
651
- * @param cwd Directory inside the docs root's work tree; git resolves the
931
+ * @param cwd Directory inside the lake root's work tree; git resolves the
652
932
  * work-tree root itself.
653
933
  * @param paths Root-relative paths.
654
934
  * @returns A `YYYY-MM-DD` per known path.
@@ -663,7 +943,10 @@ export function gitDates(
663
943
  }
664
944
  const result = spawnSync(
665
945
  "git",
666
- ["log", "--format=%cs", "--name-only", "--", ...paths],
946
+ // `--relative` keeps the output paths root-relative to `cwd`; without
947
+ // it a lake rooted at `<repo>/docs` maps `docs/foo.md` while the
948
+ // requested paths are `foo.md`, silently falling back to mtimes.
949
+ ["log", "--relative", "--format=%cs", "--name-only", "--", ...paths],
667
950
  // The default 1MB cap silently truncates a long history's output, which
668
951
  // would drop every git date back to mtime — so give it room.
669
952
  { cwd, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 },
@@ -689,7 +972,7 @@ export function gitDates(
689
972
  /**
690
973
  * A date per path in one batch: git's newest commit date, else the file
691
974
  * mtime, else "". One git pass for the whole tree, however many docs.
692
- * @param cwd Directory inside the docs root's work tree.
975
+ * @param cwd Directory inside the lake root's work tree.
693
976
  * @param paths Root-relative paths.
694
977
  * @returns A date per path.
695
978
  */
@@ -714,40 +997,145 @@ export function fallbackDates(
714
997
  return dates;
715
998
  }
716
999
 
1000
+ /** How a lake's tree is collected. */
1001
+ export interface CollectOptions {
1002
+ /** The writing convention: knowledge excludes scratch and mirrors; wiki does not. */
1003
+ profile?: LakeProfile;
1004
+ /** The lake name stamped on each entry. */
1005
+ lake?: string;
1006
+ /** The class stamped on each entry. */
1007
+ class?: DocClass;
1008
+ /** Absolute roots to skip (another lake nested inside this one). */
1009
+ skip?: readonly string[];
1010
+ }
1011
+
1012
+ /**
1013
+ * Walk a lake root, returning its `.md` paths relative to the root. Dot
1014
+ * directories are skipped (which drops `.git` and `.attachments`), as are
1015
+ * symlinked directories and any other configured lake root nested inside.
1016
+ * Knowledge lakes also skip the scratch and mirror areas.
1017
+ * @param root The lake root.
1018
+ * @param options Collection rules.
1019
+ * @returns Root-relative paths, ordered by path.
1020
+ */
1021
+ /** Read a directory, returning no entries when it is unreadable. */
1022
+ function readDirectory(directory: string): Dirent[] {
1023
+ try {
1024
+ return readdirSync(directory, { withFileTypes: true });
1025
+ } catch {
1026
+ return [];
1027
+ }
1028
+ }
1029
+
717
1030
  /**
718
- * Collect every knowledge doc under the docs root, skipping scratch and
719
- * mirrors.
720
- * @param root Docs root — the directory that is the knowledge tree.
1031
+ * Whether a directory is outside the lake's collection rules.
1032
+ * @param item The directory entry.
1033
+ * @param directory Its parent directory.
1034
+ * @param root The lake root.
1035
+ * @param excluded Top-level areas the profile excludes.
1036
+ * @param nested Other lake roots nested inside this one.
1037
+ * @returns True when the walk should not enter it.
1038
+ */
1039
+ function skipDirectory(
1040
+ item: Dirent,
1041
+ directory: string,
1042
+ root: string,
1043
+ excluded: readonly string[],
1044
+ nested: readonly string[],
1045
+ ): boolean {
1046
+ if (item.name.startsWith(".") || item.name === "node_modules") {
1047
+ return true;
1048
+ }
1049
+ if (item.isSymbolicLink()) {
1050
+ return true;
1051
+ }
1052
+ const path = join(directory, item.name);
1053
+ if (nested.some((other) => isUnder(path, other))) {
1054
+ return true;
1055
+ }
1056
+ return directory === root && excluded.includes(item.name);
1057
+ }
1058
+
1059
+ /**
1060
+ * Walk a lake root, returning its `.md` paths relative to the root. Dot
1061
+ * directories are skipped (which drops `.git` and `.attachments`), as are
1062
+ * symlinked directories, `node_modules` and any other configured lake root
1063
+ * nested inside. Knowledge lakes also skip the scratch and mirror areas.
1064
+ * @param root The lake root.
1065
+ * @param options Collection rules.
1066
+ * @param options.profile The writing convention.
1067
+ * @param options.lake The lake name (unused by the path walk).
1068
+ * @param options.class The source class (unused by the path walk).
1069
+ * @param options.skip Absolute roots of other lakes nested inside this one.
1070
+ * @returns Root-relative paths, ordered by path.
1071
+ */
1072
+ export function collectDocPaths(
1073
+ root: string,
1074
+ options: CollectOptions = {},
1075
+ ): string[] {
1076
+ const profile = options.profile ?? "knowledge";
1077
+ const skip = options.skip ?? [];
1078
+ const nested = skip.filter(
1079
+ (other) => other !== root && isUnder(other, root),
1080
+ );
1081
+ const excluded =
1082
+ profile === "knowledge" ? (EXCLUDED_AREAS as readonly string[]) : [];
1083
+ const paths: string[] = [];
1084
+ const walk = (directory: string): void => {
1085
+ for (const item of readDirectory(directory)) {
1086
+ const path = join(directory, item.name);
1087
+ if (item.isDirectory()) {
1088
+ if (!skipDirectory(item, directory, root, excluded, nested)) {
1089
+ walk(path);
1090
+ }
1091
+ continue;
1092
+ }
1093
+ if (
1094
+ !item.isFile() ||
1095
+ item.isSymbolicLink() ||
1096
+ !item.name.endsWith(".md")
1097
+ ) {
1098
+ continue;
1099
+ }
1100
+ paths.push(relative(root, path).split(sep).join("/"));
1101
+ }
1102
+ };
1103
+ if (existsSync(root)) {
1104
+ walk(root);
1105
+ }
1106
+ return paths.sort();
1107
+ }
1108
+
1109
+ /**
1110
+ * Collect every doc under a lake root, honouring the profile's exclusions and
1111
+ * the other lakes nested inside it.
1112
+ * @param root Lake root — the directory that is the tree.
721
1113
  * @param resolve Resolves every doc path's date in one batched call.
1114
+ * @param options Collection rules.
1115
+ * @param options.profile The writing convention.
1116
+ * @param options.lake The lake name stamped on each entry.
1117
+ * @param options.class The source class stamped on each entry.
1118
+ * @param options.skip Absolute roots of other lakes nested inside this one.
722
1119
  * @returns Entries ordered by path, or none when the root is absent.
723
1120
  */
724
1121
  export function collectDocs(
725
1122
  root: string,
726
1123
  resolve: (paths: readonly string[]) => ReadonlyMap<string, string>,
1124
+ options: CollectOptions = {},
727
1125
  ): DocEntry[] {
728
- let files: string[];
729
- try {
730
- files = readdirSync(root, {
731
- recursive: true,
732
- encoding: "utf8",
733
- });
734
- } catch {
735
- return [];
736
- }
737
- const excluded = EXCLUDED_AREAS as readonly string[];
738
- const paths = files
739
- .filter((relativePath) => relativePath.endsWith(".md"))
740
- .filter(
741
- (relativePath) =>
742
- !excluded.includes(relativePath.split("/")[0] ?? ""),
743
- )
744
- .sort();
1126
+ const profile = options.profile ?? "knowledge";
1127
+ const paths = collectDocPaths(root, options);
745
1128
  const dates = resolve(paths);
746
1129
  return paths.map((path) =>
747
1130
  toEntry(
748
1131
  path,
749
1132
  readFileSync(join(root, path), "utf8"),
750
1133
  dates.get(path) ?? "",
1134
+ {
1135
+ lake: options.lake ?? "",
1136
+ class: options.class ?? (profile === "wiki" ? "wiki" : "repo"),
1137
+ profile,
1138
+ },
751
1139
  ),
752
1140
  );
753
1141
  }
@@ -780,6 +1168,30 @@ function isUnder(path: string, root: string): boolean {
780
1168
  );
781
1169
  }
782
1170
 
1171
+ /**
1172
+ * Resolve symlinks in the existing prefix of a path, appending any missing
1173
+ * suffix. Used to compare lake roots by their real filesystem location.
1174
+ * @param path The path to resolve.
1175
+ * @returns The real path, or a normalised path when it cannot be resolved.
1176
+ */
1177
+ function realPathOf(path: string): string {
1178
+ let current = path;
1179
+ const missing: string[] = [];
1180
+ while (!existsSync(current)) {
1181
+ const parent = dirname(current);
1182
+ if (parent === current) {
1183
+ return normalize(path);
1184
+ }
1185
+ missing.unshift(basename(current));
1186
+ current = parent;
1187
+ }
1188
+ try {
1189
+ return join(realpathSync(current), ...missing);
1190
+ } catch {
1191
+ return normalize(path);
1192
+ }
1193
+ }
1194
+
783
1195
  /** Whether a file name is a README in any case (`README.md`, `readme.md`). */
784
1196
  function isReadme(name: string): boolean {
785
1197
  return name.toLowerCase() === "readme.md";
@@ -860,12 +1272,16 @@ function walkedReadmePaths(location: string): string[] {
860
1272
  * @param location The project directory the plugin loaded in.
861
1273
  * @param resolve Resolves every path's date in one batched call.
862
1274
  * @param skip Absolute roots whose READMEs are collected elsewhere.
1275
+ * @param options The source class/lake stamped on each entry.
1276
+ * @param options.class The source class; defaults to `instructions`.
1277
+ * @param options.lake The lake name; empty for the implicit instructions source.
863
1278
  * @returns Entries ordered by path, or none when the project is unreadable.
864
1279
  */
865
1280
  export function collectReadmes(
866
1281
  location: string,
867
1282
  resolve: (paths: readonly string[]) => ReadonlyMap<string, string>,
868
1283
  skip: readonly string[] = [],
1284
+ options: { class?: DocClass; lake?: string } = {},
869
1285
  ): DocEntry[] {
870
1286
  const paths = readmePaths(location, skip);
871
1287
  const dates = resolve(paths);
@@ -874,6 +1290,10 @@ export function collectReadmes(
874
1290
  path,
875
1291
  readFileSync(join(location, path), "utf8"),
876
1292
  dates.get(path) ?? "",
1293
+ {
1294
+ lake: options.lake ?? "",
1295
+ class: options.class ?? "instructions",
1296
+ },
877
1297
  ),
878
1298
  );
879
1299
  }
@@ -901,6 +1321,8 @@ export type AddOutcome =
901
1321
  contents: string;
902
1322
  /** Same-area docs already on disk, shown alongside the write. */
903
1323
  neighbours: readonly DocEntry[];
1324
+ /** Soft advisory lines (e.g. a long doc worth splitting). */
1325
+ notes: readonly string[];
904
1326
  }
905
1327
  | {
906
1328
  action: "refuse";
@@ -915,10 +1337,7 @@ const REFUSE = "Rejected:";
915
1337
 
916
1338
  /** The first blank required front-matter field, else null. */
917
1339
  function blankRequiredField(metadata: DocMetadata): string | null {
918
- for (const { name, required } of FRONT_MATTER_FIELDS) {
919
- if (!required) {
920
- continue;
921
- }
1340
+ for (const { name } of FRONT_MATTER_FIELDS) {
922
1341
  const value = metadata[name];
923
1342
  const blank = Array.isArray(value)
924
1343
  ? value.length === 0 ||
@@ -932,7 +1351,7 @@ function blankRequiredField(metadata: DocMetadata): string | null {
932
1351
  }
933
1352
 
934
1353
  /**
935
- * Validate an explicit `add` path: relative, inside the docs root, naming a
1354
+ * Validate an explicit `add` path: relative, inside the lake root, naming a
936
1355
  * `.md` file.
937
1356
  * @param given The trimmed, non-empty `path` the caller supplied.
938
1357
  * @returns The root-relative target path, or the refusal reason.
@@ -945,7 +1364,7 @@ function explicitTarget(given: string): { path: string } | { reason: string } {
945
1364
  given.includes(":")
946
1365
  ) {
947
1366
  return {
948
- reason: "path must be relative and stay inside the docs root.",
1367
+ reason: "path must be relative and stay inside the lake root.",
949
1368
  };
950
1369
  }
951
1370
  if (!given.endsWith(".md")) {
@@ -953,14 +1372,14 @@ function explicitTarget(given: string): { path: string } | { reason: string } {
953
1372
  }
954
1373
  const stem = given.slice(0, -".md".length);
955
1374
  if (stem === "" || stem.endsWith("/")) {
956
- return { reason: "path must name a .md file under the docs root." };
1375
+ return { reason: "path must name a .md file under the lake root." };
957
1376
  }
958
1377
  return { path: given };
959
1378
  }
960
1379
 
961
1380
  /**
962
1381
  * The root-relative target path for an `add`, or the reason it is refused.
963
- * An explicit path must stay inside the docs root and name a `.md` file; an
1382
+ * An explicit path must stay inside the lake root and name a `.md` file; an
964
1383
  * omitted path derives `<Area>/<slug>.md` from the H1.
965
1384
  * @param add The typed request.
966
1385
  * @returns The target path, or the refusal reason.
@@ -1037,11 +1456,44 @@ export function nearTwins(
1037
1456
  ).slice(0, MAX_RESULTS);
1038
1457
  }
1039
1458
 
1459
+ /**
1460
+ * The near-twin refusal for an `add`, or null when the write may proceed.
1461
+ * An explicit path is the update contract and skips reconcile.
1462
+ * @param add The typed request.
1463
+ * @param entries The current docs.
1464
+ * @returns The refusal, or null.
1465
+ */
1466
+ function nearTwinRefusal(
1467
+ add: DocsAdd,
1468
+ entries: readonly DocEntry[],
1469
+ ): AddOutcome | null {
1470
+ if ((add.path?.trim() ?? "") !== "") {
1471
+ return null;
1472
+ }
1473
+ const nearTwinDocs = nearTwins(
1474
+ reconcileQuery(add),
1475
+ add.metadata.Area,
1476
+ entries,
1477
+ );
1478
+ if (nearTwinDocs.length === 0) {
1479
+ return null;
1480
+ }
1481
+ return {
1482
+ action: "refuse",
1483
+ reason:
1484
+ `${REFUSE} ${add.metadata.Area} already holds a near-twin of ` +
1485
+ "this doc. Pass its path to update it, or a different path " +
1486
+ "to write anyway:\n\n" +
1487
+ render(nearTwinDocs),
1488
+ candidates: nearTwinDocs,
1489
+ };
1490
+ }
1491
+
1040
1492
  /**
1041
1493
  * Validate an `add` request and prepare it for writing — pure, no fs.
1042
1494
  *
1043
1495
  * Trust boundary: the caller is a model. Blank required metadata, a body
1044
- * without an H1, a path or `Area` escaping the docs root, a non-`.md` target
1496
+ * without an H1, a path or `Area` escaping the lake root, a non-`.md` target
1045
1497
  * and a title that yields no slug are all refused. With `path` omitted the
1046
1498
  * target is `<Area>/<slug>.md` and a same-area near-twin — a doc matching every
1047
1499
  * reconcile query term ({@link reconcileQuery}) — refuses the write with the
@@ -1049,11 +1501,14 @@ export function nearTwins(
1049
1501
  * replacement) and skips reconcile.
1050
1502
  * @param add The typed request.
1051
1503
  * @param entries The current docs, for reconcile and neighbours.
1504
+ * @param options Soft-limit configuration.
1505
+ * @param options.maxDocLines Note when the body exceeds this line count; `0` disables.
1052
1506
  * @returns The prepared write, or the refusal.
1053
1507
  */
1054
1508
  export function prepareAdd(
1055
1509
  add: DocsAdd,
1056
1510
  entries: readonly DocEntry[],
1511
+ options: { maxDocLines?: number } = {},
1057
1512
  ): AddOutcome {
1058
1513
  const refuse = (reason: string): AddOutcome => ({
1059
1514
  action: "refuse",
@@ -1066,7 +1521,7 @@ export function prepareAdd(
1066
1521
  return refuse(`${blank} is required and must not be blank.`);
1067
1522
  }
1068
1523
  if (/[/\\]|\.\./.test(add.metadata.Area)) {
1069
- return refuse("Area must be a single folder name under the docs root.");
1524
+ return refuse("Area must be a single folder name under the lake root.");
1070
1525
  }
1071
1526
  if (h1Of(add.body) === undefined) {
1072
1527
  return refuse("The body must carry an H1 (`# Title`).");
@@ -1083,20 +1538,9 @@ export function prepareAdd(
1083
1538
  return refuse(target.reason);
1084
1539
  }
1085
1540
 
1086
- if ((add.path?.trim() ?? "") === "") {
1087
- const query = reconcileQuery(add);
1088
- const nearTwinDocs = nearTwins(query, add.metadata.Area, entries);
1089
- if (nearTwinDocs.length > 0) {
1090
- return {
1091
- action: "refuse",
1092
- reason:
1093
- `${REFUSE} ${add.metadata.Area} already holds a near-twin of ` +
1094
- "this doc. Pass its path to update it, or a different path " +
1095
- "to write anyway:\n\n" +
1096
- render(nearTwinDocs),
1097
- candidates: nearTwinDocs,
1098
- };
1099
- }
1541
+ const nearTwin = nearTwinRefusal(add, entries);
1542
+ if (nearTwin !== null) {
1543
+ return nearTwin;
1100
1544
  }
1101
1545
 
1102
1546
  const neighbours = entries
@@ -1109,14 +1553,39 @@ export function prepareAdd(
1109
1553
  path: target.path,
1110
1554
  contents: serializeDoc(add.metadata, add.body),
1111
1555
  neighbours,
1556
+ notes: longDocNotes(add.body, options.maxDocLines),
1112
1557
  };
1113
1558
  }
1114
1559
 
1560
+ /** Default soft limit for a single doc's body, in lines. `0` disables the note. */
1561
+ export const DEFAULT_MAX_DOC_LINES = 200;
1562
+
1563
+ /**
1564
+ * The soft length notes for a body: one note when it exceeds the configured
1565
+ * limit. A note only — the write always proceeds.
1566
+ * @param body Markdown body.
1567
+ * @param limit The configured line limit; `0` disables.
1568
+ * @returns Zero or one note.
1569
+ */
1570
+ function longDocNotes(body: string, limit = DEFAULT_MAX_DOC_LINES): string[] {
1571
+ if (limit <= 0) {
1572
+ return [];
1573
+ }
1574
+ const lines = body.replace(/\n$/, "").split("\n").length;
1575
+ return lines > limit
1576
+ ? [
1577
+ `note: ${lines} body lines exceeds maxDocLines (${limit}); ` +
1578
+ "consider splitting by topic or scope.",
1579
+ ]
1580
+ : [];
1581
+ }
1582
+
1115
1583
  /** The claim sections a knowledge doc must carry at least one of. */
1116
1584
  export const CLAIM_SECTIONS = [
1117
1585
  "Facts",
1118
1586
  "Inferred",
1119
1587
  "Tested",
1588
+ "Failed approaches",
1120
1589
  "Open questions",
1121
1590
  ] as const;
1122
1591
 
@@ -1133,8 +1602,8 @@ function blankMetaFields(meta: Record<string, string>): string[] {
1133
1602
  name === "Keywords"
1134
1603
  ? splitKeywords(value ?? "").length === 0
1135
1604
  : (value ?? "").trim() === "";
1136
- return FRONT_MATTER_FIELDS.filter(
1137
- ({ name, required }) => required && blank(name, meta[name]),
1605
+ return FRONT_MATTER_FIELDS.filter(({ name }) =>
1606
+ blank(name, meta[name]),
1138
1607
  ).map(({ name }) => name);
1139
1608
  }
1140
1609
 
@@ -1169,7 +1638,7 @@ export function validateDoc(path: string, raw: string): string[] {
1169
1638
  !scanBody(body).headings.some((h) => claims.includes(h.toLowerCase()))
1170
1639
  ) {
1171
1640
  findings.push(
1172
- "no claim section (Facts / Inferred / Tested / Open questions).",
1641
+ "no claim section (Facts / Inferred / Tested / Failed approaches / Open questions).",
1173
1642
  );
1174
1643
  }
1175
1644
  const query = identityQuery(
@@ -1209,108 +1678,328 @@ function nearTwinPairs(
1209
1678
  }
1210
1679
 
1211
1680
  /**
1212
- * Check the whole tree: per-doc {@link validateDoc} findings, a
1213
- * `Supersedes` that names no doc, then the {@link nearTwinPairs}. An empty
1214
- * report means compliant.
1215
- * @param entries The docs to check.
1216
- * @param read Resolves a root-relative doc path to its raw contents;
1217
- * injected so the check is testable.
1218
- * @param notes Environment notes (e.g. an unversioned root) appended to the
1219
- * report; findings they describe are still reported per doc.
1220
- * @returns The report text.
1681
+ * Resolve a relative Markdown link target against the linking doc's folder.
1682
+ * @param from The linking doc's root-relative path.
1683
+ * @param target The link target, anchors/query already stripped.
1684
+ * @returns The resolved root-relative path, or null when it escapes the root.
1221
1685
  */
1222
- export function checkDocs(
1223
- entries: readonly DocEntry[],
1224
- read: (path: string) => string,
1225
- notes: readonly string[] = [],
1226
- ): string {
1227
- const paths = new Set(entries.map((entry) => entry.path));
1228
- const lines = entries.flatMap((entry) => {
1229
- const raw = read(entry.path);
1230
- const findings = validateDoc(entry.path, raw);
1231
- const supersedes = parseFrontMatter(raw).meta.Supersedes?.trim() ?? "";
1232
- if (supersedes !== "" && !paths.has(supersedes)) {
1233
- findings.push(`Supersedes names no doc: ${supersedes}.`);
1686
+ function resolveLink(from: string, target: string): string | null {
1687
+ const base = from.includes("/") ? from.slice(0, from.lastIndexOf("/")) : "";
1688
+ const stack = base === "" ? [] : base.split("/");
1689
+ for (const segment of target.split("/")) {
1690
+ if (segment === "" || segment === ".") {
1691
+ continue;
1234
1692
  }
1235
- return findings.map((finding) => `${entry.path}: ${finding}`);
1236
- });
1237
- for (const [doc, twin] of nearTwinPairs(entries)) {
1238
- lines.push(
1239
- `${doc.path} and ${twin.path}: near-twins — reconcile ` +
1240
- "(merge, or repoint one).",
1241
- );
1693
+ if (segment === "..") {
1694
+ if (stack.length === 0) {
1695
+ return null;
1696
+ }
1697
+ stack.pop();
1698
+ continue;
1699
+ }
1700
+ stack.push(segment);
1242
1701
  }
1243
- lines.push(...notes);
1244
- return lines.length === 0
1245
- ? `All ${entries.length} docs comply.`
1246
- : lines.join("\n");
1702
+ return stack.join("/");
1247
1703
  }
1248
1704
 
1249
- /** The built-in tools the miss-nudge watches: content searches, both miss-prone. */
1250
- export const NUDGED_TOOLS = ["grep", "glob"] as const;
1251
-
1252
- /**
1253
- * The text of a tool result's content: the string itself, or its text parts
1254
- * joined. Non-text parts and foreign shapes contribute nothing.
1255
- * @param content A `Tool.Result` content of any provenance.
1256
- * @returns The searchable text, possibly empty.
1257
- */
1258
- function contentText(content: unknown): string {
1259
- if (typeof content === "string") {
1260
- return content;
1261
- }
1262
- if (Array.isArray(content)) {
1263
- return content
1264
- .map((part) =>
1265
- typeof part === "object" &&
1266
- part !== null &&
1267
- "text" in part &&
1268
- typeof (part as { text: unknown }).text === "string"
1269
- ? (part as { text: string }).text
1270
- : "",
1271
- )
1272
- .join("\n");
1705
+ /** Inline `[text](target)` destinations on one line; images are skipped. */
1706
+ function inlineTargets(line: string): string[] {
1707
+ const targets: string[] = [];
1708
+ let index = 0;
1709
+ while (index < line.length) {
1710
+ if (line[index] === "[" && line[index - 1] !== "!") {
1711
+ const close = line.indexOf("](", index + 1);
1712
+ if (close !== -1) {
1713
+ const end = line.indexOf(")", close + 2);
1714
+ if (end !== -1) {
1715
+ const destination = line
1716
+ .slice(close + 2, end)
1717
+ .trim()
1718
+ .split(/\s+/)[0];
1719
+ if (destination !== undefined && destination !== "") {
1720
+ targets.push(destination);
1721
+ }
1722
+ index = end + 1;
1723
+ continue;
1724
+ }
1725
+ }
1726
+ }
1727
+ index += 1;
1273
1728
  }
1274
- return "";
1729
+ return targets;
1275
1730
  }
1276
1731
 
1277
1732
  /**
1278
- * Whether a grep/glob result reads as a search miss. Raw rg prints nothing
1279
- * on a miss (exit 1, empty stdout) — "No matches found" and "No files found"
1280
- * are the host wrapper's phrasings — so emptiness is the primary signal and
1281
- * the phrases are a bonus that keeps working if the wrapper rewords.
1282
- * @param content The tool result's content (string or content parts).
1283
- * @returns True when the result is a miss.
1733
+ * The local `.md` link targets in a body: inline links and reference
1734
+ * definitions, fence-aware. Images, URLs, anchors and `lake:` links are
1735
+ * dropped by the caller.
1736
+ * @param body Markdown body, front-matter stripped.
1737
+ * @returns Targets in first-seen order.
1284
1738
  */
1285
- export function isSearchMiss(content: unknown): boolean {
1286
- const trimmed = contentText(content).trim();
1287
- return trimmed === "" || /no (matches|files) found/i.test(trimmed);
1288
- }
1289
-
1290
- /** The grep/glob call's `pattern`, when the input carries a string one. */
1291
- function patternOf(input: unknown): string {
1292
- if (typeof input === "object" && input !== null && "pattern" in input) {
1293
- const pattern = (input as { pattern: unknown }).pattern;
1294
- if (typeof pattern === "string") {
1295
- return pattern.trim();
1739
+ export function markdownTargets(body: string): string[] {
1740
+ const targets: string[] = [];
1741
+ let fenced = false;
1742
+ for (const line of body.split("\n")) {
1743
+ if (isFence(line)) {
1744
+ fenced = !fenced;
1745
+ continue;
1746
+ }
1747
+ if (fenced) {
1748
+ continue;
1749
+ }
1750
+ targets.push(...inlineTargets(line));
1751
+ const reference = /^\s*\[[^\]]+\]:\s*(\S+)/.exec(line);
1752
+ if (reference?.[1] !== undefined) {
1753
+ targets.push(reference[1]);
1296
1754
  }
1297
1755
  }
1298
- return "";
1756
+ return targets;
1757
+ }
1758
+
1759
+ /** Whether a link target is external or otherwise outside the lake's concern. */
1760
+ function isExternalTarget(target: string): boolean {
1761
+ return (
1762
+ target === "" ||
1763
+ target.startsWith("#") ||
1764
+ target.startsWith("//") ||
1765
+ target.startsWith("/") ||
1766
+ target.startsWith("lake:") ||
1767
+ /^[a-z][a-z0-9+.-]*:/i.test(target)
1768
+ );
1299
1769
  }
1300
1770
 
1301
1771
  /**
1302
- * The one-line pointer appended to a grep/glob miss: the docs tool indexes
1303
- * the same knowledge, so echo the failed pattern when there is one.
1304
- * @param pattern The failed call's pattern, possibly blank.
1305
- * @returns The nudge line.
1772
+ * The broken-link findings for one doc. Only relative `.md` targets that stay
1773
+ * inside the lake are checked; project links, URLs, anchors and images are
1774
+ * left alone.
1775
+ * @param path The doc's root-relative path.
1776
+ * @param raw Full file contents.
1777
+ * @param exists Whether a root-relative path exists in the lake.
1778
+ * @returns Findings for targets that do not resolve.
1306
1779
  */
1307
- export function missNudge(pattern: string): string {
1780
+ export function linkFindings(
1781
+ path: string,
1782
+ raw: string,
1783
+ exists: (path: string) => boolean,
1784
+ ): string[] {
1785
+ const { body } = parseFrontMatter(raw);
1786
+ const findings: string[] = [];
1787
+ const seen = new Set<string>();
1788
+ for (const target of markdownTargets(body)) {
1789
+ if (isExternalTarget(target)) {
1790
+ continue;
1791
+ }
1792
+ const clean = target.split("#")[0]?.split("?")[0] ?? "";
1793
+ if (!clean.toLowerCase().endsWith(".md")) {
1794
+ continue;
1795
+ }
1796
+ let decoded = clean;
1797
+ try {
1798
+ decoded = decodeURIComponent(clean);
1799
+ } catch {
1800
+ // Keep the raw target when it is not valid percent-encoding.
1801
+ }
1802
+ const resolved = resolveLink(path, decoded);
1803
+ if (resolved === null || seen.has(resolved)) {
1804
+ continue;
1805
+ }
1806
+ seen.add(resolved);
1807
+ if (!exists(resolved)) {
1808
+ findings.push(`broken link to ${target} (no ${resolved}).`);
1809
+ }
1810
+ }
1811
+ return findings;
1812
+ }
1813
+
1814
+ /**
1815
+ * The soft notes for one doc: obsolete lifecycle fields and a long body. These
1816
+ * are advisory, not compliance failures.
1817
+ * @param path The doc's root-relative path.
1818
+ * @param raw Full file contents.
1819
+ * @param maxDocLines The body line limit; `0` disables the length note.
1820
+ * @returns The note lines, without the path prefix.
1821
+ */
1822
+ export function docNotes(
1823
+ path: string,
1824
+ raw: string,
1825
+ maxDocLines = DEFAULT_MAX_DOC_LINES,
1826
+ ): string[] {
1827
+ const { meta, body } = parseFrontMatter(raw);
1828
+ const notes: string[] = [];
1829
+ for (const field of ["Status", "Supersedes"] as const) {
1830
+ if (meta[field] !== undefined) {
1831
+ notes.push(
1832
+ `obsolete field \`${field}\` — the convention has no lifecycle ` +
1833
+ "fields; remove it or delete the doc.",
1834
+ );
1835
+ }
1836
+ }
1837
+ notes.push(
1838
+ ...longDocNotes(body, maxDocLines).map((note) =>
1839
+ note.replace(/^note: /, ""),
1840
+ ),
1841
+ );
1842
+ return notes;
1843
+ }
1844
+
1845
+ /** One doc's findings and advisory notes for `check`. */
1846
+ interface DocCheck {
1847
+ /** Structural failures. */
1848
+ findings: string[];
1849
+ /** Advisory notes. */
1850
+ notes: string[];
1851
+ }
1852
+
1853
+ /**
1854
+ * Collect one doc's check findings and notes.
1855
+ * @param entry The doc to check.
1856
+ * @param read Resolves its raw contents.
1857
+ * @param options Check options.
1858
+ * @param options.maxDocLines The body line limit.
1859
+ * @param options.exists Resolves a root-relative path for link checking.
1860
+ * @returns The findings and notes, path-prefixed.
1861
+ */
1862
+ function checkOne(
1863
+ entry: DocEntry,
1864
+ read: (path: string) => string,
1865
+ options: {
1866
+ maxDocLines?: number;
1867
+ exists?: (path: string) => boolean;
1868
+ },
1869
+ ): DocCheck {
1870
+ const raw = read(entry.path);
1871
+ const findings = validateDoc(entry.path, raw).map(
1872
+ (finding) => `${entry.path}: ${finding}`,
1873
+ );
1874
+ if (options.exists !== undefined) {
1875
+ findings.push(
1876
+ ...linkFindings(entry.path, raw, options.exists).map(
1877
+ (finding) => `${entry.path}: ${finding}`,
1878
+ ),
1879
+ );
1880
+ }
1881
+ const notes = docNotes(entry.path, raw, options.maxDocLines).map(
1882
+ (note) => `${entry.path}: ${note}`,
1883
+ );
1884
+ return { findings, notes };
1885
+ }
1886
+
1887
+ /** The near-twin findings for a tree, one line per pair. */
1888
+ function nearTwinFindings(entries: readonly DocEntry[]): string[] {
1889
+ return nearTwinPairs(entries).map(
1890
+ ([doc, twin]) =>
1891
+ `${doc.path} and ${twin.path}: near-twins — reconcile ` +
1892
+ "(merge, or repoint one).",
1893
+ );
1894
+ }
1895
+
1896
+ /**
1897
+ * Check the whole tree: per-doc {@link validateDoc} findings, broken relative
1898
+ * links, then the {@link nearTwinPairs}. Obsolete fields, long docs and
1899
+ * environment notes are advisory notes, not findings.
1900
+ * @param entries The docs to check.
1901
+ * @param read Resolves a root-relative doc path to its raw contents;
1902
+ * injected so the check is testable.
1903
+ * @param notes Environment notes (e.g. an unversioned root) appended to the
1904
+ * report; findings they describe are still reported per doc.
1905
+ * @param options Check options.
1906
+ * @param options.maxDocLines The body line limit; `0` disables the length note.
1907
+ * @param options.exists Resolves a root-relative path for link checking; when
1908
+ * omitted, links are not checked.
1909
+ * @returns The report text.
1910
+ */
1911
+ export function checkDocs(
1912
+ entries: readonly DocEntry[],
1913
+ read: (path: string) => string,
1914
+ notes: readonly string[] = [],
1915
+ options: {
1916
+ maxDocLines?: number;
1917
+ exists?: (path: string) => boolean;
1918
+ } = {},
1919
+ ): string {
1920
+ const findings: string[] = [];
1921
+ const advisory: string[] = [...notes];
1922
+ for (const entry of entries) {
1923
+ const report = checkOne(entry, read, options);
1924
+ findings.push(...report.findings);
1925
+ advisory.push(...report.notes);
1926
+ }
1927
+ findings.push(...nearTwinFindings(entries));
1928
+ const head =
1929
+ findings.length === 0
1930
+ ? `All ${entries.length} docs comply.`
1931
+ : findings.join("\n");
1932
+ if (advisory.length === 0) {
1933
+ return head;
1934
+ }
1935
+ return `${head}\n\nNotes:\n${advisory.map((note) => `- ${note}`).join("\n")}`;
1936
+ }
1937
+
1938
+ /** The built-in tools the miss-nudge watches: content searches, both miss-prone. */
1939
+ export const NUDGED_TOOLS = ["grep", "glob"] as const;
1940
+
1941
+ /**
1942
+ * The text of a tool result's content: the string itself, or its text parts
1943
+ * joined. Non-text parts and foreign shapes contribute nothing.
1944
+ * @param content A `Tool.Result` content of any provenance.
1945
+ * @returns The searchable text, possibly empty.
1946
+ */
1947
+ function contentText(content: unknown): string {
1948
+ if (typeof content === "string") {
1949
+ return content;
1950
+ }
1951
+ if (Array.isArray(content)) {
1952
+ return content
1953
+ .map((part) =>
1954
+ typeof part === "object" &&
1955
+ part !== null &&
1956
+ "text" in part &&
1957
+ typeof (part as { text: unknown }).text === "string"
1958
+ ? (part as { text: string }).text
1959
+ : "",
1960
+ )
1961
+ .join("\n");
1962
+ }
1963
+ return "";
1964
+ }
1965
+
1966
+ /**
1967
+ * Whether a grep/glob result reads as a search miss. Raw rg prints nothing
1968
+ * on a miss (exit 1, empty stdout) — "No matches found" and "No files found"
1969
+ * are the host wrapper's phrasings — so emptiness is the primary signal and
1970
+ * the phrases are a bonus that keeps working if the wrapper rewords.
1971
+ * @param content The tool result's content (string or content parts).
1972
+ * @returns True when the result is a miss.
1973
+ */
1974
+ export function isSearchMiss(content: unknown): boolean {
1975
+ const trimmed = contentText(content).trim();
1976
+ return trimmed === "" || /no (matches|files) found/i.test(trimmed);
1977
+ }
1978
+
1979
+ /** The grep/glob call's `pattern`, when the input carries a string one. */
1980
+ function patternOf(input: unknown): string {
1981
+ if (typeof input === "object" && input !== null && "pattern" in input) {
1982
+ const pattern = (input as { pattern: unknown }).pattern;
1983
+ if (typeof pattern === "string") {
1984
+ return pattern.trim();
1985
+ }
1986
+ }
1987
+ return "";
1988
+ }
1989
+
1990
+ /**
1991
+ * The one-line pointer appended to a grep/glob miss: the docs tool indexes
1992
+ * the same knowledge, so echo the failed pattern when there is one.
1993
+ * @param pattern The failed call's pattern, possibly blank.
1994
+ * @returns The nudge line.
1995
+ */
1996
+ export function missNudge(pattern: string): string {
1308
1997
  const suggestion =
1309
1998
  pattern === ""
1310
1999
  ? "a knowledge question may already be answered there."
1311
2000
  : `try \`docs search\` with terms from "${pattern}".`;
1312
2001
  return (
1313
- "Note: the `docs` tool also indexes the knowledge docs and project " +
2002
+ "Note: the `docs` tool also indexes the knowledge lakes and project " +
1314
2003
  `READMEs before you move on — ${suggestion}`
1315
2004
  );
1316
2005
  }
@@ -1362,9 +2051,10 @@ export const RECORD_NUDGE =
1362
2051
  "Before summarising: if this session established durable knowledge " +
1363
2052
  "(decisions, traps, runbooks, verified behaviour), open the " +
1364
2053
  "continuation with one `Record with docs add:` block per finding — " +
1365
- "never one session dump. Each block names its own artifact's `Area`, " +
1366
- "`H1` title and `Keywords`, plus a one-line what-and-why, so the next " +
1367
- "agent can persist each as a separate focused doc.";
2054
+ "never one session dump, and never one mixed-scope doc. Each block names " +
2055
+ "its own artifact's class (repo, machine, or global), `Area`, `H1` title " +
2056
+ "and `Keywords`, plus a one-line what-and-why, so the next agent can " +
2057
+ "persist each as a separate focused doc at the narrowest true scope.";
1368
2058
 
1369
2059
  /**
1370
2060
  * Resolve the plugin's `nudge` option: unset means on; a boolean passes
@@ -1385,36 +2075,895 @@ export function resolveNudge(configured: unknown): boolean {
1385
2075
  );
1386
2076
  }
1387
2077
 
1388
- export interface DocsToolDeps {
1389
- /** Collects the lake docs; `add` and `check` use only these. */
2078
+ /** A configured lake, before its runtime seams are attached. */
2079
+ export interface LakeSpec {
2080
+ /** The lake's addressing name, unique within the configuration. */
2081
+ name: string;
2082
+ /** The source class. */
2083
+ class: LakeClass;
2084
+ /** The absolute lake root. */
2085
+ root: string;
2086
+ /** The writing convention. */
2087
+ profile: LakeProfile;
2088
+ /** Whether `add` may write to it. */
2089
+ writable: boolean;
2090
+ }
2091
+
2092
+ /** A lake with its runtime seams. */
2093
+ export interface Lake extends LakeSpec {
2094
+ /** Collects this lake's docs. */
1390
2095
  collect: () => DocEntry[];
1391
- /** Collects everything searchable — lake docs plus local READMEs; defaults to `collect`. */
1392
- search?: () => DocEntry[];
1393
- /** Whether a root-relative doc path exists; injected so writes are testable. */
2096
+ /** Reads a root-relative path. */
2097
+ read: (path: string) => string;
2098
+ /** Whether a root-relative path exists. */
1394
2099
  exists: (path: string) => boolean;
1395
- /** Writes a root-relative doc path; injected so writes are testable. */
2100
+ /** Writes a root-relative path, creating parent directories. */
1396
2101
  write: (path: string, contents: string) => void;
1397
- /** Reads a root-relative doc path's raw contents; injected so `check` is testable. */
1398
- read: (path: string) => string;
1399
- /** Environment notes appended to the `check` report; injected for testability. */
2102
+ /** Environment notes for `check` (missing root, unversioned root). */
2103
+ notes: readonly string[];
2104
+ /** Whether `check` should verify relative links; false for the legacy seam. */
2105
+ linkCheck: boolean;
2106
+ }
2107
+
2108
+ /**
2109
+ * A lake path that would leave its root or pass through a symlink. The lake
2110
+ * seams raise it so `add` can refuse the request rather than fail the call.
2111
+ */
2112
+ class UnsafePathError extends Error {
2113
+ constructor(path: string) {
2114
+ super(`path '${path}' escapes the lake root or crosses a symlink.`);
2115
+ this.name = "UnsafePathError";
2116
+ }
2117
+ }
2118
+
2119
+ /** Whether `path` is outside `root` or is the root itself. */
2120
+ function escapesRoot(root: string, path: string): boolean {
2121
+ const rest = relative(root, path);
2122
+ return (
2123
+ rest === "" ||
2124
+ rest === ".." ||
2125
+ rest.startsWith(`..${sep}`) ||
2126
+ isAbsolute(rest)
2127
+ );
2128
+ }
2129
+
2130
+ /**
2131
+ * Resolve a root-relative path to an absolute target, refusing an escape and
2132
+ * any symlink in the path. Missing trailing segments are allowed, so a write
2133
+ * can create them beneath a real directory.
2134
+ *
2135
+ * ponytail: the check and the caller's file operation are separate syscalls,
2136
+ * so a local process that can swap a component for a symlink in between still
2137
+ * wins the race. The threat model is a symlink committed to the tree, not a
2138
+ * live local attacker; closing the gap needs per-component `O_NOFOLLOW` opens.
2139
+ * @param root The lake root.
2140
+ * @param path The root-relative path.
2141
+ * @returns The target, or null when the path escapes or crosses a symlink.
2142
+ */
2143
+ function safeLakeTarget(root: string, path: string): string | null {
2144
+ const target = join(root, path);
2145
+ if (escapesRoot(root, target)) {
2146
+ return null;
2147
+ }
2148
+ let current = root;
2149
+ for (const segment of relative(root, target).split(sep)) {
2150
+ current = join(current, segment);
2151
+ try {
2152
+ const stats = lstatSync(current);
2153
+ if (
2154
+ stats.isSymbolicLink() ||
2155
+ (current !== target && !stats.isDirectory())
2156
+ ) {
2157
+ return null;
2158
+ }
2159
+ } catch (error) {
2160
+ return (error as NodeJS.ErrnoException).code === "ENOENT"
2161
+ ? target
2162
+ : null;
2163
+ }
2164
+ }
2165
+ return target;
2166
+ }
2167
+
2168
+ /**
2169
+ * Attach the filesystem seams to a lake spec.
2170
+ * @param spec The configured lake.
2171
+ * @param options Runtime options.
2172
+ * @param options.skip Absolute roots of other lakes, so a nested lake is not
2173
+ * collected twice.
2174
+ * @returns The lake the tool serves.
2175
+ */
2176
+ export function createLake(
2177
+ spec: LakeSpec,
2178
+ options: { skip?: readonly string[] } = {},
2179
+ ): Lake {
2180
+ const guarded = (path: string): string => {
2181
+ const target = safeLakeTarget(spec.root, path);
2182
+ if (target === null) {
2183
+ throw new UnsafePathError(path);
2184
+ }
2185
+ return target;
2186
+ };
2187
+ return {
2188
+ ...spec,
2189
+ notes: unversionedNote(spec.root),
2190
+ linkCheck: true,
2191
+ collect: () =>
2192
+ collectDocs(spec.root, (paths) => fallbackDates(spec.root, paths), {
2193
+ profile: spec.profile,
2194
+ lake: spec.name,
2195
+ class: spec.class,
2196
+ skip: options.skip ?? [],
2197
+ }),
2198
+ read: (path) => readFileSync(guarded(path), "utf8"),
2199
+ exists: (path) => {
2200
+ const target = safeLakeTarget(spec.root, path);
2201
+ return target !== null && existsSync(target);
2202
+ },
2203
+ write: (path, contents) => {
2204
+ const target = guarded(path);
2205
+ mkdirSync(dirname(target), { recursive: true });
2206
+ writeFileSync(target, contents);
2207
+ },
2208
+ };
2209
+ }
2210
+
2211
+ /**
2212
+ * Build every configured lake, each aware of the others so a nested root is
2213
+ * collected once.
2214
+ * @param specs The configured lakes.
2215
+ * @returns The runtime lakes.
2216
+ */
2217
+ export function createLakes(specs: readonly LakeSpec[]): Lake[] {
2218
+ return specs.map((spec) =>
2219
+ createLake(spec, {
2220
+ skip: specs
2221
+ .filter((other) => other !== spec)
2222
+ .map((other) => other.root),
2223
+ }),
2224
+ );
2225
+ }
2226
+
2227
+ /** The lake classes the configuration accepts. */
2228
+ export const LAKE_CLASSES = ["repo", "machine", "global", "wiki"] as const;
2229
+
2230
+ /** The repo-local lake additions file, relative to the location. */
2231
+ export const REPO_LAKES_FILE = ".opencode/docs-lakes.json";
2232
+
2233
+ /** The plugin's options, as the host delivers them. */
2234
+ export interface DocsPluginOptions {
2235
+ /** The single repo-lake override; mutually exclusive with `lakes`. */
2236
+ root?: unknown;
2237
+ /** Base lake declarations, merged after `lakesFile` and the repo file. */
2238
+ lakes?: unknown;
2239
+ /** A JSON file of base lakes (`{ "lakes": [...] }`). */
2240
+ lakesFile?: unknown;
2241
+ /** The runtime nudges' switch; defaults to on. */
2242
+ nudge?: unknown;
2243
+ /** The soft body-line limit; defaults to 200, `0` disables. */
2244
+ maxDocLines?: unknown;
2245
+ /** The per-source result reservation; defaults to 3, `0` disables. */
2246
+ perLakeQuota?: unknown;
2247
+ }
2248
+
2249
+ /**
2250
+ * Resolve a non-negative integer option, or throw loudly.
2251
+ * @param value The raw option value.
2252
+ * @param name The option name, for the error.
2253
+ * @param fallback The value when unset.
2254
+ * @returns The validated number.
2255
+ */
2256
+ export function resolveNonNegativeInt(
2257
+ value: unknown,
2258
+ name: string,
2259
+ fallback: number,
2260
+ ): number {
2261
+ if (value === undefined) {
2262
+ return fallback;
2263
+ }
2264
+ if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
2265
+ return value;
2266
+ }
2267
+ throw new Error(
2268
+ `docs plugin option '${name}' must be a non-negative integer, ` +
2269
+ `got ${JSON.stringify(value)}.`,
2270
+ );
2271
+ }
2272
+
2273
+ /** Resolve the `lakesFile` option to an absolute path, or undefined. */
2274
+ function resolveLakesFilePath(
2275
+ configured: unknown,
2276
+ location: string,
2277
+ ): string | undefined {
2278
+ if (configured === undefined) {
2279
+ return undefined;
2280
+ }
2281
+ if (typeof configured !== "string" || configured.trim() === "") {
2282
+ throw new Error(
2283
+ `docs plugin option 'lakesFile' must be a non-empty string path, ` +
2284
+ `got ${JSON.stringify(configured)}.`,
2285
+ );
2286
+ }
2287
+ return resolveRoot(configured, location);
2288
+ }
2289
+
2290
+ /**
2291
+ * Read a lakes JSON file. A base file is required; the repo file is optional.
2292
+ * @param path Absolute file path.
2293
+ * @param label Human-readable source name, for errors.
2294
+ * @param required Whether a missing file is an error.
2295
+ * @returns The raw lake entries.
2296
+ */
2297
+ function readLakesJson(
2298
+ path: string,
2299
+ label: string,
2300
+ required: boolean,
2301
+ ): unknown[] {
2302
+ if (!existsSync(path)) {
2303
+ if (required) {
2304
+ throw new Error(`docs plugin ${label} not found: ${path}.`);
2305
+ }
2306
+ return [];
2307
+ }
2308
+ let parsed: unknown;
2309
+ try {
2310
+ parsed = JSON.parse(readFileSync(path, "utf8"));
2311
+ } catch (error) {
2312
+ throw new Error(
2313
+ `docs plugin ${label} is not valid JSON (${path}): ${String(error)}`,
2314
+ { cause: error },
2315
+ );
2316
+ }
2317
+ const lakes = Array.isArray(parsed)
2318
+ ? parsed
2319
+ : typeof parsed === "object" && parsed !== null && "lakes" in parsed
2320
+ ? (parsed as { lakes: unknown }).lakes
2321
+ : undefined;
2322
+ if (!Array.isArray(lakes)) {
2323
+ throw new Error(
2324
+ `docs plugin ${label} must contain a "lakes" array: ${path}.`,
2325
+ );
2326
+ }
2327
+ return lakes;
2328
+ }
2329
+
2330
+ /** A safe lake name: lower-case word characters and dashes. */
2331
+ const LAKE_NAME = /^[a-z0-9][a-z0-9-]*$/;
2332
+
2333
+ /** The raw shape a lake declaration may take. */
2334
+ interface RawLake {
2335
+ class?: unknown;
2336
+ root?: unknown;
2337
+ name?: unknown;
2338
+ writable?: unknown;
2339
+ }
2340
+
2341
+ /** Narrow an unknown lake declaration to its raw shape. */
2342
+ function asLakeEntry(raw: unknown): RawLake {
2343
+ if (typeof raw !== "object" || raw === null) {
2344
+ throw new Error(
2345
+ `docs plugin lake entry must be an object, got ${JSON.stringify(raw)}.`,
2346
+ );
2347
+ }
2348
+ return raw as RawLake;
2349
+ }
2350
+
2351
+ /** Validate the lake class and the repo-file restriction. */
2352
+ function lakeClassOf(entry: RawLake, fromRepoFile: boolean): LakeClass {
2353
+ const given = entry.class ?? "repo";
2354
+ if (
2355
+ typeof given !== "string" ||
2356
+ !(LAKE_CLASSES as readonly string[]).includes(given)
2357
+ ) {
2358
+ throw new Error(
2359
+ `docs plugin lake class must be one of ${LAKE_CLASSES.join(", ")}, ` +
2360
+ `got ${JSON.stringify(entry.class)}.`,
2361
+ );
2362
+ }
2363
+ const lakeClass = given as LakeClass;
2364
+ if (fromRepoFile && lakeClass !== "repo") {
2365
+ throw new Error(
2366
+ `docs plugin repo lake file may only declare "repo" lakes; ` +
2367
+ `got ${JSON.stringify(lakeClass)}.`,
2368
+ );
2369
+ }
2370
+ return lakeClass;
2371
+ }
2372
+
2373
+ /** Resolve and validate a lake root, applying the repo default. */
2374
+ function lakeRootOf(
2375
+ entry: RawLake,
2376
+ lakeClass: LakeClass,
2377
+ location: string,
2378
+ fromRepoFile: boolean,
2379
+ ): string {
2380
+ const blank =
2381
+ entry.root === undefined ||
2382
+ (typeof entry.root === "string" && entry.root.trim() === "");
2383
+ if (blank && lakeClass !== "repo") {
2384
+ throw new Error(
2385
+ `docs plugin ${lakeClass} lake needs a root path, ` +
2386
+ `got ${JSON.stringify(entry.root)}.`,
2387
+ );
2388
+ }
2389
+ if (blank) {
2390
+ return join(location, "docs");
2391
+ }
2392
+ if (typeof entry.root !== "string") {
2393
+ throw new Error(
2394
+ `docs plugin ${lakeClass} lake root must be a string, ` +
2395
+ `got ${JSON.stringify(entry.root)}.`,
2396
+ );
2397
+ }
2398
+ const root = resolveRoot(entry.root, location);
2399
+ if (fromRepoFile && !isUnder(realPathOf(root), realPathOf(location))) {
2400
+ throw new Error(
2401
+ `docs plugin repo lake root must stay inside the location: ${root}.`,
2402
+ );
2403
+ }
2404
+ return root;
2405
+ }
2406
+
2407
+ /** Resolve and validate a lake name. */
2408
+ function lakeNameOf(
2409
+ entry: RawLake,
2410
+ root: string,
2411
+ lakeClass: LakeClass,
2412
+ ): string {
2413
+ const derived = slugify(basename(root)) || lakeClass;
2414
+ const name =
2415
+ entry.name === undefined
2416
+ ? derived
2417
+ : typeof entry.name === "string"
2418
+ ? entry.name.trim()
2419
+ : "";
2420
+ if (!LAKE_NAME.test(name)) {
2421
+ throw new Error(
2422
+ `docs plugin lake name must match ${LAKE_NAME}, ` +
2423
+ `got ${JSON.stringify(entry.name ?? derived)}.`,
2424
+ );
2425
+ }
2426
+ if (name === "instructions") {
2427
+ throw new Error(
2428
+ "docs plugin lake name 'instructions' is reserved for project READMEs.",
2429
+ );
2430
+ }
2431
+ return name;
2432
+ }
2433
+
2434
+ /** Resolve and validate a lake's writability. */
2435
+ function lakeWritableOf(
2436
+ entry: RawLake,
2437
+ lakeClass: LakeClass,
2438
+ name: string,
2439
+ ): boolean {
2440
+ if (entry.writable !== undefined && typeof entry.writable !== "boolean") {
2441
+ throw new Error(
2442
+ `docs plugin lake '${name}' writable must be a boolean, ` +
2443
+ `got ${JSON.stringify(entry.writable)}.`,
2444
+ );
2445
+ }
2446
+ const writable =
2447
+ entry.writable === undefined ? lakeClass !== "wiki" : entry.writable;
2448
+ if (lakeClass === "wiki" && writable) {
2449
+ throw new Error(
2450
+ `docs plugin lake '${name}' is a wiki lake and cannot be writable.`,
2451
+ );
2452
+ }
2453
+ return writable;
2454
+ }
2455
+
2456
+ /**
2457
+ * Validate one raw lake declaration and resolve its defaults.
2458
+ * @param raw The raw entry from a config file or inline option.
2459
+ * @param location The project directory the plugin loaded in.
2460
+ * @param fromRepoFile Whether the entry came from the repo-local file, which
2461
+ * may only add repo lakes rooted inside the location.
2462
+ * @returns The resolved lake spec.
2463
+ */
2464
+ function lakeSpecFrom(
2465
+ raw: unknown,
2466
+ location: string,
2467
+ fromRepoFile: boolean,
2468
+ ): LakeSpec {
2469
+ const entry = asLakeEntry(raw);
2470
+ const lakeClass = lakeClassOf(entry, fromRepoFile);
2471
+ const root = lakeRootOf(entry, lakeClass, location, fromRepoFile);
2472
+ const name = lakeNameOf(entry, root, lakeClass);
2473
+ return {
2474
+ name,
2475
+ class: lakeClass,
2476
+ root,
2477
+ profile: lakeClass === "wiki" ? "wiki" : "knowledge",
2478
+ writable: lakeWritableOf(entry, lakeClass, name),
2479
+ };
2480
+ }
2481
+
2482
+ /**
2483
+ * Resolve the plugin's lakes: the base `lakesFile`, the repo-local additions,
2484
+ * inline `lakes`, then `root` sugar. One implicit repo lake at
2485
+ * `<location>/docs` is added when no repo lake is configured, so a repo needs
2486
+ * no lake file unless its docs live elsewhere.
2487
+ * @param options The plugin options.
2488
+ * @param location The directory the plugin loaded in.
2489
+ * @returns The configured lake specs.
2490
+ */
2491
+ /** Reject the ambiguous option combinations before reading any file. */
2492
+ function assertLakesOptions(options: DocsPluginOptions): void {
2493
+ if (options.lakes !== undefined && !Array.isArray(options.lakes)) {
2494
+ throw new Error(
2495
+ `docs plugin option 'lakes' must be an array, ` +
2496
+ `got ${JSON.stringify(options.lakes)}.`,
2497
+ );
2498
+ }
2499
+ if (options.root !== undefined && options.lakes !== undefined) {
2500
+ throw new Error(
2501
+ "docs plugin options 'root' and 'lakes' are mutually exclusive; " +
2502
+ "use 'root' for one repo lake or 'lakes' for the full list.",
2503
+ );
2504
+ }
2505
+ }
2506
+
2507
+ /** Map raw lake entries from a file to specs. */
2508
+ function specsFromFile(
2509
+ path: string,
2510
+ location: string,
2511
+ fromRepoFile: boolean,
2512
+ ): LakeSpec[] {
2513
+ const label = fromRepoFile ? "repo lake file" : "lakesFile";
2514
+ return readLakesJson(path, label, !fromRepoFile).map((raw) =>
2515
+ lakeSpecFrom(raw, location, fromRepoFile),
2516
+ );
2517
+ }
2518
+
2519
+ /** Map the inline `lakes` option to specs. */
2520
+ function specsFromInline(rawLakes: unknown, location: string): LakeSpec[] {
2521
+ return Array.isArray(rawLakes)
2522
+ ? rawLakes.map((raw) => lakeSpecFrom(raw, location, false))
2523
+ : [];
2524
+ }
2525
+
2526
+ /** Throw when two lakes share a name. */
2527
+ function assertUniqueLakeNames(specs: readonly LakeSpec[]): void {
2528
+ const names = new Set<string>();
2529
+ for (const spec of specs) {
2530
+ if (names.has(spec.name)) {
2531
+ throw new Error(
2532
+ `docs plugin lake name '${spec.name}' is declared twice; ` +
2533
+ "give each lake a unique name.",
2534
+ );
2535
+ }
2536
+ names.add(spec.name);
2537
+ }
2538
+ }
2539
+
2540
+ /** Throw when two differently named lakes resolve to the same root. */
2541
+ function assertUniqueLakeRoots(specs: readonly LakeSpec[]): void {
2542
+ const roots = new Map<string, string>();
2543
+ for (const spec of specs) {
2544
+ const key = realPathOf(spec.root);
2545
+ const existing = roots.get(key);
2546
+ if (existing !== undefined) {
2547
+ throw new Error(
2548
+ `docs plugin lakes '${existing}' and '${spec.name}' share ` +
2549
+ `the root ${spec.root}; give each lake its own root.`,
2550
+ );
2551
+ }
2552
+ roots.set(key, spec.name);
2553
+ }
2554
+ }
2555
+
2556
+ /**
2557
+ * Resolve the plugin's lakes: the base `lakesFile`, the repo-local additions,
2558
+ * inline `lakes`, then `root` sugar. One implicit repo lake at
2559
+ * `<location>/docs` is added when no repo lake is configured, so a repo needs
2560
+ * no lake file unless its docs live elsewhere.
2561
+ * @param options The plugin options.
2562
+ * @param location The directory the plugin loaded in.
2563
+ * @returns The configured lake specs.
2564
+ */
2565
+ export function resolveLakes(
2566
+ options: DocsPluginOptions,
2567
+ location: string,
2568
+ ): LakeSpec[] {
2569
+ assertLakesOptions(options);
2570
+ const basePath = resolveLakesFilePath(options.lakesFile, location);
2571
+ const specs: LakeSpec[] = [
2572
+ ...(basePath === undefined
2573
+ ? []
2574
+ : specsFromFile(basePath, location, false)),
2575
+ ...specsFromFile(join(location, REPO_LAKES_FILE), location, true),
2576
+ ...specsFromInline(options.lakes, location),
2577
+ ];
2578
+ if (options.root !== undefined) {
2579
+ specs.push(
2580
+ lakeSpecFrom(
2581
+ { class: "repo", root: options.root },
2582
+ location,
2583
+ false,
2584
+ ),
2585
+ );
2586
+ }
2587
+ if (!specs.some((spec) => spec.class === "repo")) {
2588
+ specs.push(
2589
+ lakeSpecFrom(
2590
+ { class: "repo", root: join(location, "docs") },
2591
+ location,
2592
+ false,
2593
+ ),
2594
+ );
2595
+ }
2596
+ assertUniqueLakeNames(specs);
2597
+ assertUniqueLakeRoots(specs);
2598
+ return specs;
2599
+ }
2600
+
2601
+ /** Runtime deps shared by the tool and the command. */
2602
+ export interface RuntimeInput {
2603
+ /** The configured lakes; omit for the legacy single-lake seams. */
2604
+ lakes?: readonly Lake[];
2605
+ /** Collects the lake docs; legacy seam. */
2606
+ collect?: () => DocEntry[];
2607
+ /** Collects everything searchable — lake docs plus local READMEs. */
2608
+ search?: () => DocEntry[];
2609
+ /** Whether a root-relative doc path exists; legacy seam. */
2610
+ exists?: (path: string) => boolean;
2611
+ /** Writes a root-relative doc path; legacy seam. */
2612
+ write?: (path: string, contents: string) => void;
2613
+ /** Reads a root-relative doc path's raw contents; legacy seam. */
2614
+ read?: (path: string) => string;
2615
+ /** Environment notes appended to the `check` report; legacy seam. */
1400
2616
  notes?: readonly string[];
2617
+ /** The soft body-line limit; defaults to {@link DEFAULT_MAX_DOC_LINES}. */
2618
+ maxDocLines?: number;
2619
+ /** The per-source result reservation; defaults to {@link DEFAULT_PER_LAKE_QUOTA}. */
2620
+ perLakeQuota?: number;
2621
+ }
2622
+
2623
+ /** A resolved runtime: lakes plus the search and check knobs. */
2624
+ interface Runtime {
2625
+ lakes: readonly Lake[];
2626
+ search: () => DocEntry[];
2627
+ maxDocLines: number;
2628
+ perLakeQuota: number;
2629
+ }
2630
+
2631
+ /** The legacy single-lake closure wrapped as a {@link Lake}. */
2632
+ function legacyLake(deps: RuntimeInput): Lake {
2633
+ const collect = deps.collect ?? deps.search ?? (() => []);
2634
+ return {
2635
+ name: "docs",
2636
+ class: "repo",
2637
+ root: "",
2638
+ profile: "knowledge",
2639
+ writable: true,
2640
+ collect,
2641
+ read: deps.read ?? (() => ""),
2642
+ exists: deps.exists ?? (() => false),
2643
+ write: deps.write ?? (() => {}),
2644
+ notes: deps.notes ?? [],
2645
+ linkCheck: deps.exists !== undefined,
2646
+ };
2647
+ }
2648
+
2649
+ /** Normalise the old closure seams and the lake seams into one runtime. */
2650
+ function runtimeOf(deps: RuntimeInput): Runtime {
2651
+ const lakes = deps.lakes ?? [legacyLake(deps)];
2652
+ return {
2653
+ lakes,
2654
+ search: deps.search ?? (() => lakes.flatMap((lake) => lake.collect())),
2655
+ maxDocLines: deps.maxDocLines ?? DEFAULT_MAX_DOC_LINES,
2656
+ perLakeQuota: deps.perLakeQuota ?? DEFAULT_PER_LAKE_QUOTA,
2657
+ };
2658
+ }
2659
+
2660
+ /** The first repo lake, else the first lake; the one rendered without a prefix. */
2661
+ function primaryLakeName(lakes: readonly Lake[]): string | undefined {
2662
+ return (lakes.find((lake) => lake.class === "repo") ?? lakes[0])?.name;
2663
+ }
2664
+
2665
+ /** Find a lake by name. */
2666
+ function findLake(lakes: readonly Lake[], name: string): Lake | undefined {
2667
+ return lakes.find((lake) => lake.name === name);
2668
+ }
2669
+
2670
+ /** The lake `add` writes to, or the refusal reason. */
2671
+ function writableLake(
2672
+ lakes: readonly Lake[],
2673
+ name: string | undefined,
2674
+ ): Lake | { reason: string } {
2675
+ if (name !== undefined) {
2676
+ const lake = findLake(lakes, name);
2677
+ if (lake === undefined) {
2678
+ return { reason: `${REFUSE} no lake named '${name}'.` };
2679
+ }
2680
+ if (!lake.writable) {
2681
+ return {
2682
+ reason: `${REFUSE} lake '${name}' is read-only (${lake.profile}).`,
2683
+ };
2684
+ }
2685
+ return lake;
2686
+ }
2687
+ return (
2688
+ lakes.find((lake) => lake.class === "repo" && lake.writable) ??
2689
+ lakes.find((lake) => lake.profile === "knowledge" && lake.writable) ?? {
2690
+ reason:
2691
+ `${REFUSE} no writable knowledge lake is configured; ` +
2692
+ "add a repo, machine, or global lake.",
2693
+ }
2694
+ );
1401
2695
  }
1402
2696
 
1403
- /** Delivers a rendered result to a session. */
2697
+ /** Qualify a path with its lake when more than one lake is configured. */
2698
+ function address(lake: Lake, path: string, lakes: readonly Lake[]): string {
2699
+ return lakes.length > 1 ? `${lake.name}:${path}` : path;
2700
+ }
2701
+
2702
+ /** Search every lake, optionally limited to one, and render the ranked result. */
2703
+ function searchDocs(
2704
+ runtime: Runtime,
2705
+ query: string,
2706
+ lakeName: string | undefined,
2707
+ ): { content: string; count: number; omitted: number } {
2708
+ const all = runtime.search();
2709
+ let entries = all;
2710
+ if (lakeName !== undefined) {
2711
+ if (lakeName === "instructions") {
2712
+ entries = all.filter((entry) => entry.class === "instructions");
2713
+ } else if (findLake(runtime.lakes, lakeName) === undefined) {
2714
+ return {
2715
+ content: `No lake named '${lakeName}'.`,
2716
+ count: 0,
2717
+ omitted: 0,
2718
+ };
2719
+ } else {
2720
+ entries = all.filter((entry) => entry.lake === lakeName);
2721
+ }
2722
+ }
2723
+ const {
2724
+ entries: ranked,
2725
+ closest,
2726
+ omitted,
2727
+ } = rankDocs(entries, query, { perLakeQuota: runtime.perLakeQuota });
2728
+ return {
2729
+ content: render(ranked, {
2730
+ closest,
2731
+ omitted,
2732
+ primaryLake: primaryLakeName(runtime.lakes) ?? "",
2733
+ }),
2734
+ count: ranked.length,
2735
+ omitted,
2736
+ };
2737
+ }
2738
+
2739
+ /** Check the knowledge lakes, optionally limited to one. */
2740
+ function checkLakes(
2741
+ runtime: Runtime,
2742
+ lakeName: string | undefined,
2743
+ ): { content: string; checked: number } {
2744
+ if (lakeName !== undefined) {
2745
+ const lake = findLake(runtime.lakes, lakeName);
2746
+ if (lake === undefined) {
2747
+ return { content: `No lake named '${lakeName}'.`, checked: 0 };
2748
+ }
2749
+ if (lake.profile === "wiki") {
2750
+ return {
2751
+ content:
2752
+ `Lake '${lake.name}' is a wiki lake — read-only reference, ` +
2753
+ "not validated.",
2754
+ checked: 0,
2755
+ };
2756
+ }
2757
+ const entries = lake.collect();
2758
+ return {
2759
+ content: checkDocs(entries, lake.read, lake.notes, {
2760
+ maxDocLines: runtime.maxDocLines,
2761
+ ...(lake.linkCheck ? { exists: lake.exists } : {}),
2762
+ }),
2763
+ checked: entries.length,
2764
+ };
2765
+ }
2766
+ const knowledge = runtime.lakes.filter(
2767
+ (lake) => lake.profile === "knowledge",
2768
+ );
2769
+ let checked = 0;
2770
+ const reports = knowledge.map((lake) => {
2771
+ const entries = lake.collect();
2772
+ checked += entries.length;
2773
+ const report = checkDocs(entries, lake.read, lake.notes, {
2774
+ maxDocLines: runtime.maxDocLines,
2775
+ ...(lake.linkCheck ? { exists: lake.exists } : {}),
2776
+ });
2777
+ return knowledge.length > 1 || runtime.lakes.length > 1
2778
+ ? `${lake.name}:\n${report}`
2779
+ : report;
2780
+ });
2781
+ const wiki = runtime.lakes
2782
+ .filter((lake) => lake.profile === "wiki")
2783
+ .map((lake) => lake.name);
2784
+ if (wiki.length > 0) {
2785
+ reports.push(
2786
+ `note: wiki lakes are read-only reference and are not validated: ` +
2787
+ `${wiki.join(", ")}.`,
2788
+ );
2789
+ }
2790
+ return {
2791
+ content:
2792
+ reports.length > 0
2793
+ ? reports.join("\n\n")
2794
+ : "No knowledge lakes configured.",
2795
+ checked,
2796
+ };
2797
+ }
2798
+
2799
+ /** Add a doc to the target lake. */
2800
+ function addDoc(
2801
+ runtime: Runtime,
2802
+ add: DocsAdd,
2803
+ lakeName: string | undefined,
2804
+ ): { content: string; written: boolean } {
2805
+ const target = writableLake(runtime.lakes, lakeName);
2806
+ if ("reason" in target) {
2807
+ return { content: target.reason, written: false };
2808
+ }
2809
+ const entries = target.collect();
2810
+ const outcome = prepareAdd(add, entries, {
2811
+ maxDocLines: runtime.maxDocLines,
2812
+ });
2813
+ if (outcome.action === "refuse") {
2814
+ return { content: outcome.reason, written: false };
2815
+ }
2816
+ let updated: boolean;
2817
+ try {
2818
+ updated = target.exists(outcome.path);
2819
+ target.write(outcome.path, outcome.contents);
2820
+ } catch (error) {
2821
+ if (error instanceof UnsafePathError) {
2822
+ return {
2823
+ content: `${REFUSE} ${error.message}`,
2824
+ written: false,
2825
+ };
2826
+ }
2827
+ throw error;
2828
+ }
2829
+ const head = `${address(target, outcome.path, runtime.lakes)} — ${
2830
+ updated ? "updated" : "created"
2831
+ }.`;
2832
+ const neighbours =
2833
+ outcome.neighbours.length > 0
2834
+ ? `\n\nNearby docs in the same area:\n\n${render(
2835
+ outcome.neighbours,
2836
+ { primaryLake: target.name },
2837
+ )}`
2838
+ : "";
2839
+ const notes =
2840
+ outcome.notes.length > 0 ? `\n\n${outcome.notes.join("\n")}` : "";
2841
+ return { content: `${head}${neighbours}${notes}`, written: true };
2842
+ }
2843
+
2844
+ /** A parsed read request: the lake (when known) and the path. */
2845
+ interface ReadTarget {
2846
+ lake?: Lake;
2847
+ path: string;
2848
+ reason?: string;
2849
+ }
2850
+
2851
+ /** Split `lake:path` or an explicit lake selector into a target. */
2852
+ function readTargetOf(
2853
+ runtime: Runtime,
2854
+ given: string,
2855
+ lakeName: string | undefined,
2856
+ ): ReadTarget {
2857
+ const path = given.trim();
2858
+ if (lakeName !== undefined) {
2859
+ const lake = findLake(runtime.lakes, lakeName);
2860
+ const prefix = `${lakeName}:`;
2861
+ const selectedPath = path.startsWith(prefix)
2862
+ ? path.slice(prefix.length)
2863
+ : path;
2864
+ return lake === undefined
2865
+ ? { path, reason: `${REFUSE} no lake named '${lakeName}'.` }
2866
+ : { lake, path: selectedPath };
2867
+ }
2868
+ const colon = path.indexOf(":");
2869
+ if (colon <= 0) {
2870
+ return { path };
2871
+ }
2872
+ if (path.startsWith("instructions:")) {
2873
+ return {
2874
+ path,
2875
+ reason:
2876
+ `${REFUSE} instructions: paths are project READMEs; ` +
2877
+ "use the built-in read tool.",
2878
+ };
2879
+ }
2880
+ const lake = findLake(runtime.lakes, path.slice(0, colon));
2881
+ return lake === undefined
2882
+ ? { path }
2883
+ : { lake, path: path.slice(colon + 1) };
2884
+ }
2885
+
2886
+ /** The unique lake holding a path, or the refusal reason. */
2887
+ function uniqueHolder(
2888
+ runtime: Runtime,
2889
+ path: string,
2890
+ ): Lake | { reason: string } {
2891
+ const holders = runtime.lakes.filter((lake) => lake.exists(path));
2892
+ if (holders.length === 0) {
2893
+ return { reason: `No doc at ${path}.` };
2894
+ }
2895
+ if (holders.length > 1) {
2896
+ return {
2897
+ reason:
2898
+ `${REFUSE} ${path} exists in multiple lakes ` +
2899
+ `(${holders.map((lake) => lake.name).join(", ")}); pass lake.`,
2900
+ };
2901
+ }
2902
+ return holders[0] as Lake;
2903
+ }
2904
+
2905
+ /** Read one doc, resolving a `lake:path` prefix or the unique match. */
2906
+ function readDoc(
2907
+ runtime: Runtime,
2908
+ given: string,
2909
+ lakeName: string | undefined,
2910
+ ): { content: string; read: string | null } {
2911
+ const target = readTargetOf(runtime, given, lakeName);
2912
+ if (target.reason !== undefined) {
2913
+ return { content: target.reason, read: null };
2914
+ }
2915
+ const guarded = explicitTarget(target.path);
2916
+ if ("reason" in guarded) {
2917
+ return {
2918
+ content: `${REFUSE} ${guarded.reason}`,
2919
+ read: null,
2920
+ };
2921
+ }
2922
+ const selected = target.lake ?? uniqueHolder(runtime, guarded.path);
2923
+ if ("reason" in selected) {
2924
+ return { content: selected.reason, read: null };
2925
+ }
2926
+ if (!selected.exists(guarded.path)) {
2927
+ return {
2928
+ content: `No doc at ${address(
2929
+ selected,
2930
+ guarded.path,
2931
+ runtime.lakes,
2932
+ )}.`,
2933
+ read: null,
2934
+ };
2935
+ }
2936
+ const raw = selected.read(guarded.path);
2937
+ const { body } = parseFrontMatter(raw);
2938
+ const label = address(selected, guarded.path, runtime.lakes);
2939
+ return {
2940
+ content: `${label} — ${titleOf(guarded.path, body)}\n\n${raw}`,
2941
+ read: label,
2942
+ };
2943
+ }
2944
+
2945
+ /** The lake help view shared by the tool and command. */
2946
+ function lakeInfo(lakes: readonly Lake[]): LakeInfo[] {
2947
+ return lakes.map((lake) => ({
2948
+ name: lake.name,
2949
+ class: lake.class,
2950
+ root: lake.root,
2951
+ writable: lake.writable,
2952
+ validated: lake.profile === "knowledge",
2953
+ }));
2954
+ }
2955
+
2956
+ /** Deliver a rendered result to a session. */
1404
2957
  export type PromptSender = (
1405
2958
  sessionID: CommandInvocation["sessionID"],
1406
2959
  text: string,
1407
2960
  ) => Promise<unknown>;
1408
2961
 
1409
- export interface DocsCommandDeps {
1410
- /** Collects the lake docs; `add` and `check` use only these. */
1411
- collect: () => DocEntry[];
1412
- /** Collects everything searchable — lake docs plus local READMEs; defaults to `collect`. */
1413
- search?: () => DocEntry[];
1414
- /** Reads a root-relative doc path's raw contents; injected so `check` is testable. */
1415
- read: (path: string) => string;
1416
- /** Environment notes appended to the `check` report; injected for testability. */
1417
- notes?: readonly string[];
2962
+ /** Tool deps: lakes (or the legacy single-lake closures) plus knobs. */
2963
+ export type DocsToolDeps = RuntimeInput;
2964
+
2965
+ /** Command deps: tool deps plus the session prompt sender. */
2966
+ export interface DocsCommandDeps extends RuntimeInput {
1418
2967
  /** Delivers the rendered result to the session. */
1419
2968
  prompt: PromptSender;
1420
2969
  }
@@ -1425,12 +2974,11 @@ export interface DocsCommandDeps {
1425
2974
  * the full doctrine the native `docs` description carries.
1426
2975
  */
1427
2976
  const TWIN_DESCRIPTION =
1428
- "Search, read, add and check the knowledge docs under the configured " +
1429
- "docs root (plus nested project `README.md` files, search-only) from " +
1430
- "inside `execute` — the `docs` tool's Code Mode twin. A blank search " +
1431
- "lists the most recently changed docs; `read` returns a matched doc " +
1432
- "verbatim; `add` writes through a typed DTO; `check` validates the " +
1433
- "convention.";
2977
+ "Search, read, add and check the configured knowledge lakes (plus nested " +
2978
+ "project `README.md` files, search-only) from inside `execute` — the " +
2979
+ "`docs` tool's Code Mode twin. A blank search lists the most recently " +
2980
+ "changed docs; `read` returns a matched doc verbatim; `add` writes through " +
2981
+ "a typed DTO; `check` validates the knowledge lakes.";
1434
2982
 
1435
2983
  /**
1436
2984
  * Build the `docs` tool from its dependencies.
@@ -1443,8 +2991,7 @@ const TWIN_DESCRIPTION =
1443
2991
  * (the catalog renders `tools.<namespace>.<name>(…)`). Pass a distinct
1444
2992
  * `name` and `codemode: true` to register a Code Mode twin of the same core;
1445
2993
  * the registry is name-keyed, so the twin cannot also be called `docs`.
1446
- * @param deps Runtime seams used on each call: the docs to serve, and the
1447
- * write seam {@link DocsToolDeps} for `add`.
2994
+ * @param deps Runtime seams used on each call.
1448
2995
  * @param options Registration shape.
1449
2996
  * @param options.name The tool name (default `"docs"`); a Code Mode twin
1450
2997
  * passes a distinct name — the registry is name-keyed, so two tools cannot
@@ -1458,58 +3005,18 @@ export function createDocsTool(
1458
3005
  options: { name?: string; codemode?: boolean } = {},
1459
3006
  ): Info<typeof DOCS_INPUT> {
1460
3007
  const { name = "docs", codemode = false } = options;
1461
- const addDoc = (add: DocsAdd) => {
1462
- const outcome = prepareAdd(add, deps.collect());
1463
- if (outcome.action === "refuse") {
1464
- return { content: outcome.reason, metadata: { written: false } };
1465
- }
1466
- const updated = deps.exists(outcome.path);
1467
- deps.write(outcome.path, outcome.contents);
1468
- const head = `${outcome.path} — ${updated ? "updated" : "created"}.`;
1469
- const neighbours =
1470
- outcome.neighbours.length > 0
1471
- ? `\n\nNearby docs in the same area:\n\n${render(outcome.neighbours)}`
1472
- : "";
1473
- return {
1474
- content: `${head}${neighbours}`,
1475
- metadata: { written: true },
1476
- };
1477
- };
1478
- const readDoc = (given: string) => {
1479
- // The same guard an explicit `add` path passes: relative, inside the
1480
- // root, a `.md` file — the read verb never widens the surface.
1481
- const target = explicitTarget(given.trim());
1482
- if ("reason" in target) {
1483
- return {
1484
- content: `${REFUSE} ${target.reason}`,
1485
- metadata: { read: null },
1486
- };
1487
- }
1488
- if (!deps.exists(target.path)) {
1489
- return {
1490
- content: `No doc at ${target.path}.`,
1491
- metadata: { read: null },
1492
- };
1493
- }
1494
- const raw = deps.read(target.path);
1495
- const { body } = parseFrontMatter(raw);
1496
- return {
1497
- content: `${target.path} — ${titleOf(target.path, body)}\n\n${raw}`,
1498
- metadata: { read: target.path },
1499
- };
1500
- };
3008
+ const runtime = runtimeOf(deps);
1501
3009
  return {
1502
3010
  name,
1503
3011
  description: codemode
1504
3012
  ? TWIN_DESCRIPTION
1505
- : "Discover the knowledge docs under the configured docs root " +
1506
- "(default: the location's `docs/` directory), plus nested `README.md` " +
3013
+ : "Discover the configured knowledge lakes plus nested `README.md` " +
1507
3014
  "files in the project tree. Prefer it over a broad grep when the " +
1508
3015
  "question is how-to knowledge rather than where code lives. A blank search lists the " +
1509
3016
  MAX_RESULTS +
1510
- " most recently changed docs (path — Area — date — Summary); a search ranks docs over title, path, keywords, body headings and code spans, area and summary, falling back to the closest matches. Every call returns at most " +
3017
+ " most recently changed docs (path — Area — date — Summary); a search ranks docs over title, path, keywords, body headings and code spans, area, summary and wiki prose, falling back to the closest matches. Every source reserves a few results; every call caps at " +
1511
3018
  MAX_RESULTS +
1512
- " results and counts the rest. Search exactly `help` explains the ranking. `read` returns a matched doc verbatim (front-matter and body). `add` writes a new knowledge doc (or wholesale-replaces the doc at `path`) from a typed DTO — metadata becomes front-matter, the body must start with an H1; blank required fields, path traversal, non-.md targets and same-area near-twins are refused. `check` validates every doc against the convention (required front-matter, H1, claim sections, no hand-maintained Date, distinctive H1+Keywords, a Supersedes naming a doc in the tree, near-twins) and reports findings; an empty report means compliant. Local READMEs are search-only: `add` and `check` ignore them. Read a matched project file with the built-in read tool.",
3019
+ " and counts the rest. Search exactly `help` for the lakes and ranking. `read` returns a doc verbatim; pass a `lake` or a `lake:path`. `add` writes a new knowledge doc (or replaces the doc at `path`) from a typed DTO — metadata becomes front-matter, the body must start with an H1; blank fields, path traversal, non-.md targets, symlinked targets, read-only lakes and same-area near-twins are refused. `check` validates the knowledge lakes (front-matter, H1, claim sections, no hand-maintained Date, distinctive identity, near-twins, broken links, soft length note); wiki lakes are read-only and not validated. READMEs are search-only and render as `instructions:path`; read them with the built-in read tool.",
1513
3020
  input: DOCS_INPUT,
1514
3021
  options: codemode
1515
3022
  ? { namespace: "docs", codemode: true }
@@ -1517,31 +3024,39 @@ export function createDocsTool(
1517
3024
  async execute(input) {
1518
3025
  const request = resolveRequest(input);
1519
3026
  if (request.verb === "add") {
1520
- return addDoc(request.add);
3027
+ const result = addDoc(runtime, request.add, request.lake);
3028
+ return {
3029
+ content: result.content,
3030
+ metadata: { written: result.written },
3031
+ };
1521
3032
  }
1522
3033
  if (request.verb === "check") {
1523
- const entries = deps.collect();
3034
+ const result = checkLakes(runtime, request.lake);
1524
3035
  return {
1525
- content: checkDocs(entries, deps.read, deps.notes ?? []),
1526
- metadata: { checked: entries.length },
3036
+ content: result.content,
3037
+ metadata: { checked: result.checked },
1527
3038
  };
1528
3039
  }
1529
3040
  if (request.verb === "read") {
1530
- return readDoc(request.read);
3041
+ const result = readDoc(runtime, request.read, request.lake);
3042
+ return {
3043
+ content: result.content,
3044
+ metadata: { read: result.read },
3045
+ };
1531
3046
  }
1532
3047
  if (request.verb === "help") {
1533
3048
  return {
1534
- content: explainRanking(),
3049
+ content: helpText(lakeInfo(runtime.lakes)),
1535
3050
  metadata: { count: 0, omitted: 0 },
1536
3051
  };
1537
3052
  }
1538
- const { entries, closest, omitted } = rankDocs(
1539
- (deps.search ?? deps.collect)(),
1540
- request.search,
1541
- );
3053
+ const result = searchDocs(runtime, request.search, request.lake);
1542
3054
  return {
1543
- content: render(entries, { closest, omitted }),
1544
- metadata: { count: entries.length, omitted },
3055
+ content: result.content,
3056
+ metadata: {
3057
+ count: result.count,
3058
+ omitted: result.omitted,
3059
+ },
1545
3060
  };
1546
3061
  },
1547
3062
  };
@@ -1564,7 +3079,7 @@ function addPrompt(subject: string): string {
1564
3079
  return "`/docs add` needs the text to record: `/docs add <fact>`.";
1565
3080
  }
1566
3081
  return (
1567
- "Record this as durable knowledge in the docs root: call the `docs` " +
3082
+ "Record this as durable knowledge in the docs lakes: call the `docs` " +
1568
3083
  "tool's `add` with a typed DTO (metadata Area/Keywords/Summary, body starting " +
1569
3084
  "with an H1) for:\n\n" +
1570
3085
  subject
@@ -1575,21 +3090,22 @@ function addPrompt(subject: string): string {
1575
3090
  * Build the `/docs` command over the same core as the tool: a search ranks the
1576
3091
  * catalogue (`search <text>`, or bare text), `add <text>` asks the agent to
1577
3092
  * write the knowledge through the tool's typed `add`, and `check` validates
1578
- * the tree against the convention.
3093
+ * the knowledge lakes.
1579
3094
  * @param deps Runtime seams used on each call.
1580
3095
  * @returns The command the host registers.
1581
3096
  */
1582
3097
  export function createDocsCommand(deps: DocsCommandDeps): CommandDefinition {
3098
+ const runtime = runtimeOf(deps);
1583
3099
  return {
1584
3100
  name: "docs",
1585
3101
  description:
1586
- "Search the knowledge docs under the configured docs root, ranked, plus nested project `README.md` files (search only); `add <text>` records new knowledge through the tool's typed `add`; `check` validates the convention; a blank search lists the " +
3102
+ "Search the configured knowledge lakes, ranked, plus nested project `README.md` files (search only); `add <text>` records new knowledge through the tool's typed `add`; `check` validates the knowledge lakes; a blank search lists the " +
1587
3103
  MAX_RESULTS +
1588
3104
  " most recently changed.",
1589
3105
  async execute({ sessionID, prompt }) {
1590
3106
  const text = prompt.text.trim();
1591
3107
  if (isHelp(text)) {
1592
- await deps.prompt(sessionID, explainRanking());
3108
+ await deps.prompt(sessionID, helpText(lakeInfo(runtime.lakes)));
1593
3109
  return;
1594
3110
  }
1595
3111
  const [verb, ...rest] = text.split(/\s+/);
@@ -1601,22 +3117,19 @@ export function createDocsCommand(deps: DocsCommandDeps): CommandDefinition {
1601
3117
  if (lower === CHECK_VERB) {
1602
3118
  await deps.prompt(
1603
3119
  sessionID,
1604
- checkDocs(deps.collect(), deps.read, deps.notes ?? []),
3120
+ checkLakes(runtime, undefined).content,
1605
3121
  );
1606
3122
  return;
1607
3123
  }
1608
3124
  const query = lower === SEARCH_VERB ? rest.join(" ") : text;
1609
- const { entries, closest, omitted } = rankDocs(
1610
- (deps.search ?? deps.collect)(),
1611
- query,
1612
- );
1613
- await deps.prompt(sessionID, render(entries, { closest, omitted }));
3125
+ const result = searchDocs(runtime, query, undefined);
3126
+ await deps.prompt(sessionID, result.content);
1614
3127
  },
1615
3128
  };
1616
3129
  }
1617
3130
 
1618
3131
  /**
1619
- * Resolve the plugin's docs root from its `root` option. Unset → the
3132
+ * Resolve a lake root from its `root` option. Unset → the
1620
3133
  * location's `docs/` directory — a blank string counts as unset, since it is
1621
3134
  * almost always an empty environment variable or half-finished config rather
1622
3135
  * than a deliberate path; `~` or `~/…` expands against the home
@@ -1626,7 +3139,7 @@ export function createDocsCommand(deps: DocsCommandDeps): CommandDefinition {
1626
3139
  * must fail loudly, not silently fall back to the wrong tree.
1627
3140
  * @param configured The raw option value as the host delivers it.
1628
3141
  * @param locationDirectory The directory the plugin instance loaded in.
1629
- * @returns The docs root.
3142
+ * @returns The lake root.
1630
3143
  */
1631
3144
  export function resolveRoot(
1632
3145
  configured: unknown,
@@ -1653,13 +3166,19 @@ export function resolveRoot(
1653
3166
  }
1654
3167
 
1655
3168
  /**
1656
- * Probe whether the docs root sits inside a git work tree, as `check`-report
3169
+ * Probe whether the lake root sits inside a git work tree, as `check`-report
1657
3170
  * notes. An unversioned root still works — dates fall back to file mtimes and
1658
3171
  * the knowledge has no history — so it is surfaced, not refused.
1659
- * @param root The docs root to probe.
3172
+ * @param root The lake root to probe.
1660
3173
  * @returns The note lines; empty when the root is version-controlled.
1661
3174
  */
1662
3175
  export function unversionedNote(root: string): readonly string[] {
3176
+ if (!existsSync(root)) {
3177
+ return [
3178
+ "note: this lake root does not exist yet — it is ignored until " +
3179
+ "it does. A missing root is not an error.",
3180
+ ];
3181
+ }
1663
3182
  const probe = spawnSync("git", ["rev-parse", "--show-toplevel"], {
1664
3183
  cwd: root,
1665
3184
  });
@@ -1667,24 +3186,12 @@ export function unversionedNote(root: string): readonly string[] {
1667
3186
  return [];
1668
3187
  }
1669
3188
  return [
1670
- "note: the docs root is not inside a git work tree — dates fall " +
3189
+ "note: the lake root is not inside a git work tree — dates fall " +
1671
3190
  "back to file mtimes and the knowledge has no history. Put the " +
1672
3191
  "root under version control to fix both.",
1673
3192
  ];
1674
3193
  }
1675
3194
 
1676
- /**
1677
- * The docs plugin's options, as the host delivers them. The framework types
1678
- * `ctx.options` as `Readonly<Record<string, any>>`, so the shape the plugin
1679
- * reads is declared here.
1680
- */
1681
- interface DocsPluginOptions {
1682
- /** The docs root override; defaults to `docs` beside the plugin. */
1683
- root?: unknown;
1684
- /** The runtime nudges' switch; defaults to on. */
1685
- nudge?: unknown;
1686
- }
1687
-
1688
3195
  /** The registered tool names that count as using the lake. */
1689
3196
  const DOCS_TOOL_NAMES = new Set(["docs", "docs_search"]);
1690
3197
 
@@ -1707,36 +3214,38 @@ function appendLine(
1707
3214
  export default Plugin.define({
1708
3215
  id: "docs",
1709
3216
  async setup(ctx) {
1710
- const options = ctx.options as DocsPluginOptions | undefined;
1711
- const root = resolveRoot(options?.root, ctx.location.directory);
1712
- const nudge = resolveNudge(options?.nudge);
1713
- const notes = unversionedNote(root);
1714
- const collect = () =>
1715
- collectDocs(root, (paths) => fallbackDates(root, paths));
3217
+ const options = (ctx.options ?? {}) as DocsPluginOptions;
3218
+ const nudge = resolveNudge(options.nudge);
3219
+ const maxDocLines = resolveNonNegativeInt(
3220
+ options.maxDocLines,
3221
+ "maxDocLines",
3222
+ DEFAULT_MAX_DOC_LINES,
3223
+ );
3224
+ const perLakeQuota = resolveNonNegativeInt(
3225
+ options.perLakeQuota,
3226
+ "perLakeQuota",
3227
+ DEFAULT_PER_LAKE_QUOTA,
3228
+ );
3229
+ const lakes = createLakes(
3230
+ resolveLakes(options, ctx.location.directory),
3231
+ );
1716
3232
  const search = () => [
1717
- ...collect(),
3233
+ ...lakes.flatMap((lake) => lake.collect()),
1718
3234
  ...collectReadmes(
1719
3235
  ctx.location.directory,
1720
3236
  (paths) => fallbackDates(ctx.location.directory, paths),
1721
- [root],
3237
+ lakes.map((lake) => lake.root),
1722
3238
  ),
1723
3239
  ];
1724
- const read = (path: string) => readFileSync(join(root, path), "utf8");
1725
3240
  const toolDeps = {
1726
- collect,
3241
+ lakes,
1727
3242
  search,
1728
- exists: (path: string) => existsSync(join(root, path)),
1729
- write: (path: string, contents: string) => {
1730
- const target = join(root, path);
1731
- mkdirSync(dirname(target), { recursive: true });
1732
- writeFileSync(target, contents);
1733
- },
1734
- read,
1735
- notes,
3243
+ maxDocLines,
3244
+ perLakeQuota,
1736
3245
  };
1737
3246
  // Two registrations of one core: the native `docs` for direct calls,
1738
3247
  // and the Code Mode twin `docs_search` so `execute` reaches the same
1739
- // lake as `tools.docs.docs_search(…)`. The registry is name-keyed, so
3248
+ // lakes as `tools.docs.docs_search(…)`. The registry is name-keyed, so
1740
3249
  // the twin must carry its own name.
1741
3250
  await ctx.tool.transform((editor) => {
1742
3251
  editor.add(createDocsTool(toolDeps));
@@ -1750,12 +3259,12 @@ export default Plugin.define({
1750
3259
  await ctx.command.transform((editor) => {
1751
3260
  editor.add(
1752
3261
  createDocsCommand({
1753
- collect,
3262
+ lakes,
1754
3263
  search,
1755
- read,
1756
- notes,
3264
+ maxDocLines,
3265
+ perLakeQuota,
1757
3266
  prompt: (sessionID, text) => {
1758
- // Any `/docs` use counts as consulting the lake.
3267
+ // Any `/docs` use counts as consulting the lakes.
1759
3268
  nudged.add(sessionID);
1760
3269
  return ctx.session.prompt({ sessionID, text });
1761
3270
  },
@@ -1814,17 +3323,17 @@ export default Plugin.define({
1814
3323
  id: Skill.ID.make("docs"),
1815
3324
  name: Skill.Name.make("Docs"),
1816
3325
  description:
1817
- "The entry point to the knowledge docs under the " +
1818
- "configured docs root, plus the project's nested " +
1819
- "README.md files (search only): find the right file with " +
1820
- "the docs tool, read it, and record plans and task " +
1821
- "documentation for the next human or agent. Use before " +
1822
- "searching the codebase for how-to knowledge, when a " +
1823
- "task names a service, flag or error string the lake " +
1824
- "may already explain, at the start of unfamiliar work, " +
1825
- "when hunting for prior decisions or traps, when a " +
1826
- "session learns something the next one will need, and " +
1827
- "when closing out a session.",
3326
+ "The entry point to the configured knowledge lakes " +
3327
+ "(repo, machine, global and read-only wiki), plus the " +
3328
+ "project's nested README.md files (search only): find the " +
3329
+ "right doc with the docs tool, read it, and record focused " +
3330
+ "task knowledge for the next human or agent at the " +
3331
+ "narrowest true scope. Use before searching the codebase " +
3332
+ "for how-to knowledge, when a task names a service, flag " +
3333
+ "or error string the lake may already explain, at the " +
3334
+ "start of unfamiliar work, when hunting for prior " +
3335
+ "decisions or traps, when a session learns something the " +
3336
+ "next one will need, and when closing out a session.",
1828
3337
  path: AbsolutePath.make(fileURLToPath(skillURL)),
1829
3338
  content: skill,
1830
3339
  });