@tokenroll/llmdoc 3.6.0 → 3.9.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.
@@ -1,15 +1,18 @@
1
+ import { spawnSync } from "node:child_process";
1
2
  import fs from "node:fs";
2
3
  import path from "node:path";
3
- import matter from "gray-matter";
4
4
  import { DOC_LINE_WARNING_LIMIT } from "./constants.js";
5
5
  import { CliError } from "./errors.js";
6
6
  import { estimateTokens, extractCodeRefs, extractLinks, extractTitle, resolveDocLink, stripMarkdownLiterals, validateCodeRefTags } from "./markdown.js";
7
7
  import { assertDocKindMatchesShape, parseDocTargetShape } from "./doc-shape.js";
8
8
  import { normalizeRepoRelativePath, repoPath, resolveInsideRoot } from "./fs.js";
9
- import { gitCommitExists, isShallowRepository } from "./git.js";
9
+ import { gitCommitExists, isShallowRepository, listGitDeletedLlmdocPaths } from "./git.js";
10
10
  import { matchesCodePathPattern } from "./search.js";
11
11
  import { validateDocFrontmatter, validateMeta } from "./schema.js";
12
- import { loadLlmdocConfig } from "./config.js";
12
+ import { computeLocalPreloadBudget, LLMDOC_CONFIG_FILENAME, LLMDOC_LOCAL_CONFIG_FILENAME, loadLlmdocConfig } from "./config.js";
13
+ import { parseYamlFrontmatter } from "./frontmatter.js";
14
+ import { isSourceRef, parseSourceDocId } from "./source-id.js";
15
+ import { loadSourcesReadOnly } from "./sources.js";
13
16
  export function loadWorkspace(rootDir) {
14
17
  const configuredLlmdocDir = path.join(rootDir, "llmdoc");
15
18
  if (!fs.existsSync(configuredLlmdocDir) || !fs.statSync(configuredLlmdocDir).isDirectory()) {
@@ -18,7 +21,8 @@ export function loadWorkspace(rootDir) {
18
21
  resolveInsideRoot(rootDir, "llmdoc");
19
22
  const llmdocDir = configuredLlmdocDir;
20
23
  const preloadIssues = [];
21
- const documents = scanDocuments(rootDir, llmdocDir, preloadIssues);
24
+ const unparsedDocPaths = new Set();
25
+ const documents = scanDocuments(rootDir, llmdocDir, preloadIssues, unparsedDocPaths);
22
26
  const documentsByLlmdocPath = new Map(documents.map((document) => [document.llmdocPath, document]));
23
27
  const topics = new Map();
24
28
  const rootSingletons = [];
@@ -39,6 +43,7 @@ export function loadWorkspace(rootDir) {
39
43
  const metaPath = path.join(llmdocDir, "meta.json");
40
44
  const meta = loadMeta(metaPath, preloadIssues);
41
45
  const llmdocConfig = loadLlmdocConfig(rootDir, documentsByLlmdocPath);
46
+ const loadedSources = loadSourcesReadOnly(rootDir, llmdocConfig.sources);
42
47
  return {
43
48
  rootDir,
44
49
  llmdocDir,
@@ -49,10 +54,15 @@ export function loadWorkspace(rootDir) {
49
54
  rootSingletons,
50
55
  meta,
51
56
  llmdocConfig,
52
- preloadIssues
57
+ preloadIssues,
58
+ sources: loadedSources.sources,
59
+ sourcesByName: loadedSources.sourcesByName,
60
+ sourceDocuments: loadedSources.sourceDocuments,
61
+ sourceDocumentsById: loadedSources.sourceDocumentsById,
62
+ unparsedDocPaths
53
63
  };
54
64
  }
55
- function scanDocuments(rootDir, llmdocDir, preloadIssues) {
65
+ function scanDocuments(rootDir, llmdocDir, preloadIssues, unparsedDocPaths) {
56
66
  const results = [];
57
67
  walk(llmdocDir, (absolutePath) => {
58
68
  const repoRelativePath = repoPath(rootDir, absolutePath);
@@ -67,12 +77,14 @@ function scanDocuments(rootDir, llmdocDir, preloadIssues) {
67
77
  }
68
78
  return;
69
79
  }
80
+ const llmdocPath = repoPath(llmdocDir, absolutePath);
70
81
  const raw = fs.readFileSync(absolutePath, "utf8");
71
82
  let parsed;
72
83
  try {
73
- parsed = matter(raw);
84
+ parsed = parseYamlFrontmatter(raw);
74
85
  }
75
86
  catch (error) {
87
+ unparsedDocPaths.add(llmdocPath);
76
88
  preloadIssues.push({
77
89
  severity: "error",
78
90
  code: "frontmatter.parse",
@@ -82,7 +94,6 @@ function scanDocuments(rootDir, llmdocDir, preloadIssues) {
82
94
  return;
83
95
  }
84
96
  const frontmatter = parsed.data;
85
- const llmdocPath = repoPath(llmdocDir, absolutePath);
86
97
  const segments = llmdocPath.split("/");
87
98
  results.push({
88
99
  absolutePath,
@@ -97,7 +108,10 @@ function scanDocuments(rootDir, llmdocDir, preloadIssues) {
97
108
  links: extractLinks(parsed.content),
98
109
  codeRefs: extractCodeRefs(parsed.content),
99
110
  estimatedTokens: estimateTokens(parsed.content),
100
- lineCount: raw.split(/\r?\n/).length
111
+ lineCount: raw.split(/\r?\n/).length,
112
+ sourceName: null,
113
+ sourceRelativePath: null,
114
+ sourceMountPath: null
101
115
  });
102
116
  });
103
117
  results.sort((left, right) => left.llmdocPath.localeCompare(right.llmdocPath));
@@ -150,6 +164,7 @@ export function validateWorkspace(workspace) {
150
164
  const issues = [...workspace.preloadIssues];
151
165
  issues.push(...workspace.llmdocConfig.issues);
152
166
  const seenDocPaths = new Set();
167
+ const declaredSourceAliases = new Set(Object.keys(workspace.llmdocConfig.config?.sources ?? {}));
153
168
  if (!workspace.meta) {
154
169
  issues.push({
155
170
  severity: "error",
@@ -190,6 +205,14 @@ export function validateWorkspace(workspace) {
190
205
  message: "A topic cannot contain nested subdirectories."
191
206
  });
192
207
  }
208
+ if (workspace.llmdocConfig.schemaVersion === 2 && document.topic?.startsWith("@")) {
209
+ issues.push({
210
+ severity: "error",
211
+ code: "hierarchy.reserved-prefix",
212
+ path: document.repoPath,
213
+ message: `Topic "${document.topic}" starts with "@", which is reserved for external sources in llmdoc.config/v2. Rename it with \`llmdoc mv llmdoc/${document.topic} llmdoc/${document.topic.slice(1)}\`.`
214
+ });
215
+ }
193
216
  const mdxBodyErrors = validateMdxBody(document.body);
194
217
  for (const message of mdxBodyErrors) {
195
218
  issues.push({
@@ -208,6 +231,48 @@ export function validateWorkspace(workspace) {
208
231
  });
209
232
  }
210
233
  for (const relationPath of document.frontmatter.relations?.requires ?? []) {
234
+ if (isSourceRef(relationPath)) {
235
+ let parsedRef;
236
+ try {
237
+ parsedRef = parseSourceDocId(relationPath);
238
+ }
239
+ catch (error) {
240
+ issues.push({
241
+ severity: "error",
242
+ code: "relations.requires.invalid-path",
243
+ path: document.repoPath,
244
+ message: error.message
245
+ });
246
+ continue;
247
+ }
248
+ if (!declaredSourceAliases.has(parsedRef.alias)) {
249
+ issues.push({
250
+ severity: "error",
251
+ code: "relations.requires.undeclared-source",
252
+ path: document.repoPath,
253
+ message: `requires references an undeclared source: @${parsedRef.alias} (${relationPath})`
254
+ });
255
+ continue;
256
+ }
257
+ const loadedSource = workspace.sourcesByName.get(parsedRef.alias);
258
+ if (!loadedSource || loadedSource.status !== "ready") {
259
+ issues.push({
260
+ severity: "warning",
261
+ code: "relations.requires.source-unavailable",
262
+ path: document.repoPath,
263
+ message: `requires target ${parsedRef.canonicalId} could not be verified because source @${parsedRef.alias} is unavailable${loadedSource?.remedy ? ` (${loadedSource.remedy})` : ""}.`
264
+ });
265
+ }
266
+ else if (!workspace.sourceDocumentsById.has(parsedRef.canonicalId)) {
267
+ issues.push({
268
+ severity: "warning",
269
+ code: "relations.requires.source-missing",
270
+ path: document.repoPath,
271
+ message: `requires points to a missing document in @${parsedRef.alias}${loadedSource.revision ? ` (${loadedSource.revision.slice(0, 7)})` : ""}: ${parsedRef.canonicalId}`
272
+ });
273
+ }
274
+ continue;
275
+ }
211
276
  const normalizedRelation = validateLlmdocRelativePath(relationPath);
212
277
  if (!normalizedRelation.ok) {
213
278
  issues.push({
@@ -321,6 +386,61 @@ export function validateWorkspace(workspace) {
321
386
  });
322
387
  }
323
388
  }
389
+ for (const source of workspace.sources) {
390
+ if (source.status !== "ready") {
391
+ issues.push({
392
+ severity: "warning",
393
+ code: source.status === "missing" ? "source.unavailable" : "source.error",
394
+ path: LLMDOC_CONFIG_FILENAME,
395
+ message: `Source @${source.name} is unavailable: ${source.error ?? "no local snapshot"}.${source.remedy ? ` ${source.remedy}` : ""}`
396
+ });
397
+ }
398
+ for (const sourceIssue of source.issues) {
399
+ issues.push({
400
+ ...sourceIssue,
401
+ severity: "warning"
402
+ });
403
+ }
404
+ }
405
+ for (const preloadEntry of workspace.llmdocConfig.preloadEntries) {
406
+ if (preloadEntry.kind !== "source" || !preloadEntry.sourceName) {
407
+ continue;
408
+ }
409
+ const source = workspace.sourcesByName.get(preloadEntry.sourceName);
410
+ if (!source || source.status !== "ready") {
411
+ issues.push({
412
+ severity: "warning",
413
+ code: "config.startup.preload.source-unavailable",
414
+ path: LLMDOC_CONFIG_FILENAME,
415
+ message: `startup.preload source document could not be verified because @${preloadEntry.sourceName} is unavailable: ${preloadEntry.normalized}`
416
+ });
417
+ }
418
+ else if (!workspace.sourceDocumentsById.has(preloadEntry.normalized)) {
419
+ issues.push({
420
+ severity: "warning",
421
+ code: "config.startup.preload.source-missing",
422
+ path: LLMDOC_CONFIG_FILENAME,
423
+ message: `startup.preload source document does not exist in @${preloadEntry.sourceName}${source.revision ? ` (${source.revision.slice(0, 7)})` : ""}: ${preloadEntry.normalized}`
424
+ });
425
+ }
426
+ }
427
+ const localPreloadBudget = computeLocalPreloadBudget(workspace.llmdocConfig, workspace.documentsByLlmdocPath);
428
+ if (localPreloadBudget.exceedsBudget) {
429
+ issues.push({
430
+ severity: "warning",
431
+ code: "config.startup.preload.budget-exceeded",
432
+ path: workspace.llmdocConfig.preloadBudgetSourceFile ?? LLMDOC_CONFIG_FILENAME,
433
+ message: `Local startup.preload totals ~${localPreloadBudget.totalEstimatedTokens} tokens, exceeding the soft budget of ${localPreloadBudget.budgetTokens} tokens. Consider removing less-frequent documents from startup.preload so they stay on progressive description/tree disclosure (or raise startup.preloadBudgetTokens).`
434
+ });
435
+ }
436
+ if (workspace.llmdocConfig.localConfigExists && isGitTrackedFile(workspace.rootDir, LLMDOC_LOCAL_CONFIG_FILENAME)) {
437
+ issues.push({
438
+ severity: "warning",
439
+ code: "config.local.tracked",
440
+ path: LLMDOC_LOCAL_CONFIG_FILENAME,
441
+ message: `${LLMDOC_LOCAL_CONFIG_FILENAME} is tracked by Git. Add it to .gitignore so machine-specific paths are not committed.`
442
+ });
443
+ }
324
444
  for (const [topic, docs] of workspace.topics) {
325
445
  if (docs.length === 0) {
326
446
  issues.push({
@@ -347,6 +467,7 @@ export function validateWorkspace(workspace) {
347
467
  }
348
468
  const metaPaths = Object.keys(workspace.meta.documents).sort();
349
469
  const docPaths = [...seenDocPaths].sort();
470
+ const gitDeletedDocs = listGitDeletedLlmdocPaths(workspace.rootDir);
350
471
  for (const docPath of docPaths) {
351
472
  if (!(docPath in workspace.meta.documents)) {
352
473
  issues.push({
@@ -358,13 +479,23 @@ export function validateWorkspace(workspace) {
358
479
  }
359
480
  }
360
481
  for (const metaPath of metaPaths) {
361
- if (!seenDocPaths.has(metaPath)) {
362
- issues.push({
363
- severity: "error",
364
- code: "meta.entry.orphaned",
365
- path: `llmdoc/${metaPath}`,
366
- message: "meta.json contains an orphan documents entry."
367
- });
482
+ if (!seenDocPaths.has(metaPath) && !workspace.unparsedDocPaths?.has(metaPath)) {
483
+ if (gitDeletedDocs.has(metaPath)) {
484
+ issues.push({
485
+ severity: "warning",
486
+ code: "meta.entry.pending-removal",
487
+ path: `llmdoc/${metaPath}`,
488
+ message: "Document was deleted in Git; finalize with `llmdoc commit` to remove its ledger entry."
489
+ });
490
+ }
491
+ else {
492
+ issues.push({
493
+ severity: "error",
494
+ code: "meta.entry.orphaned",
495
+ path: `llmdoc/${metaPath}`,
496
+ message: "meta.json contains an orphan documents entry."
497
+ });
498
+ }
368
499
  }
369
500
  const revision = workspace.meta.documents[metaPath]?.validatedRevision;
370
501
  if (revision && !gitCommitExists(workspace.rootDir, revision)) {
@@ -379,6 +510,14 @@ export function validateWorkspace(workspace) {
379
510
  }
380
511
  return issues;
381
512
  }
513
+ function isGitTrackedFile(rootDir, repoRelativePath) {
514
+ const result = spawnSync("git", ["ls-files", "--error-unmatch", "--", repoRelativePath], {
515
+ cwd: rootDir,
516
+ encoding: "utf8",
517
+ stdio: ["ignore", "pipe", "ignore"]
518
+ });
519
+ return result.status === 0;
520
+ }
382
521
  function isGlob(input) {
383
522
  return /[*?[\]{}]/.test(input);
384
523
  }
@@ -447,6 +586,7 @@ function validateCodePathPattern(rootDir, input) {
447
586
  }
448
587
  }
449
588
  function globMatchesExistingFile(rootDir, staticPrefixPath, pattern) {
589
+ const rootRealPath = fs.realpathSync(rootDir);
450
590
  const pending = [staticPrefixPath];
451
591
  while (pending.length > 0) {
452
592
  const currentPath = pending.pop();
@@ -455,7 +595,7 @@ function globMatchesExistingFile(rootDir, staticPrefixPath, pattern) {
455
595
  continue;
456
596
  }
457
597
  if (stat.isFile()) {
458
- if (matchesCodePathPattern(pattern, repoPath(rootDir, currentPath))) {
598
+ if (matchesCodePathPattern(pattern, repoPath(rootRealPath, currentPath))) {
459
599
  return true;
460
600
  }
461
601
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenroll/llmdoc",
3
- "version": "3.6.0",
3
+ "version": "3.9.0",
4
4
  "description": "V3 runtime CLI for llmdoc repositories",
5
5
  "homepage": "https://github.com/TokenRollAI/llmdoc",
6
6
  "repository": {
@@ -1,17 +1,52 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "https://llmdoc.tokenroll.ai/schemas/config.schema.json",
4
- "type": "object",
5
- "additionalProperties": false,
6
- "required": ["schema"],
7
- "properties": {
8
- "$schema": {
9
- "type": "string"
10
- },
11
- "schema": {
12
- "type": "string",
13
- "const": "llmdoc.config/v1"
4
+ "oneOf": [
5
+ {
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema"],
9
+ "properties": {
10
+ "$schema": {
11
+ "type": "string"
12
+ },
13
+ "schema": {
14
+ "type": "string",
15
+ "const": "llmdoc.config/v1"
16
+ },
17
+ "startup": {
18
+ "$ref": "#/$defs/startup"
19
+ }
20
+ }
14
21
  },
22
+ {
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "required": ["schema"],
26
+ "properties": {
27
+ "$schema": {
28
+ "type": "string"
29
+ },
30
+ "schema": {
31
+ "type": "string",
32
+ "const": "llmdoc.config/v2"
33
+ },
34
+ "sources": {
35
+ "type": "object",
36
+ "propertyNames": {
37
+ "pattern": "^[a-z0-9][a-z0-9._-]{0,63}$"
38
+ },
39
+ "additionalProperties": {
40
+ "$ref": "#/$defs/sourceEntry"
41
+ }
42
+ },
43
+ "startup": {
44
+ "$ref": "#/$defs/startup"
45
+ }
46
+ }
47
+ }
48
+ ],
49
+ "$defs": {
15
50
  "startup": {
16
51
  "type": "object",
17
52
  "additionalProperties": false,
@@ -27,8 +62,55 @@
27
62
  "minLength": 1
28
63
  },
29
64
  "default": []
65
+ },
66
+ "preloadBudgetTokens": {
67
+ "type": "integer",
68
+ "minimum": 1,
69
+ "default": 4000
30
70
  }
31
71
  }
72
+ },
73
+ "sourceEntry": {
74
+ "oneOf": [
75
+ {
76
+ "type": "object",
77
+ "additionalProperties": false,
78
+ "required": ["git"],
79
+ "properties": {
80
+ "git": {
81
+ "type": "string",
82
+ "minLength": 1
83
+ },
84
+ "ref": {
85
+ "type": "string",
86
+ "minLength": 1
87
+ },
88
+ "dir": {
89
+ "type": "string",
90
+ "minLength": 1
91
+ },
92
+ "refresh": {
93
+ "type": "string",
94
+ "pattern": "^[1-9][0-9]*[mhd]$"
95
+ }
96
+ }
97
+ },
98
+ {
99
+ "type": "object",
100
+ "additionalProperties": false,
101
+ "required": ["path"],
102
+ "properties": {
103
+ "path": {
104
+ "type": "string",
105
+ "minLength": 1
106
+ },
107
+ "dir": {
108
+ "type": "string",
109
+ "minLength": 1
110
+ }
111
+ }
112
+ }
113
+ ]
32
114
  }
33
115
  }
34
116
  }
@@ -34,6 +34,10 @@
34
34
  "validatedRevision": {
35
35
  "type": ["string", "null"],
36
36
  "minLength": 1
37
+ },
38
+ "verifiedAt": {
39
+ "type": "string",
40
+ "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$"
37
41
  }
38
42
  }
39
43
  }
@@ -49,7 +53,7 @@
49
53
  },
50
54
  "source": {
51
55
  "type": "string",
52
- "enum": ["init", "prune"]
56
+ "enum": ["init", "prune", "dream"]
53
57
  },
54
58
  "documentCount": {
55
59
  "type": "integer",