@tokenroll/llmdoc 3.5.3 → 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.
Files changed (48) hide show
  1. package/assets/viewer-app.js +11 -11
  2. package/assets/viewer-detail.js +4 -4
  3. package/assets/viewer-graph.js +3 -3
  4. package/assets/viewer.html +19 -19
  5. package/dist/cli.js +147 -71
  6. package/dist/commands/adopt.js +10 -6
  7. package/dist/commands/commit.js +232 -80
  8. package/dist/commands/context.js +85 -45
  9. package/dist/commands/dream.js +608 -0
  10. package/dist/commands/eval.js +379 -0
  11. package/dist/commands/fingerprint.js +9 -3
  12. package/dist/commands/hook.js +135 -28
  13. package/dist/commands/index.js +52 -7
  14. package/dist/commands/init-state.js +5 -5
  15. package/dist/commands/mv.js +33 -12
  16. package/dist/commands/new.js +12 -5
  17. package/dist/commands/prune.js +14 -3
  18. package/dist/commands/search.js +67 -4
  19. package/dist/commands/serve.js +1 -1
  20. package/dist/commands/show.js +40 -6
  21. package/dist/commands/status.js +46 -18
  22. package/dist/commands/tree.js +94 -6
  23. package/dist/commands/upgrade.js +15 -15
  24. package/dist/lib/config.js +502 -0
  25. package/dist/lib/constants.js +23 -0
  26. package/dist/lib/doc-shape.js +5 -5
  27. package/dist/lib/frontmatter.js +47 -0
  28. package/dist/lib/fs.js +8 -8
  29. package/dist/lib/git.js +147 -42
  30. package/dist/lib/markdown.js +8 -7
  31. package/dist/lib/output-schema.js +3 -3
  32. package/dist/lib/package-root.js +1 -1
  33. package/dist/lib/pagination.js +2 -2
  34. package/dist/lib/rewrite.js +9 -3
  35. package/dist/lib/routing.js +152 -0
  36. package/dist/lib/schema.js +16 -0
  37. package/dist/lib/search.js +35 -16
  38. package/dist/lib/source-id.js +49 -0
  39. package/dist/lib/sources.js +760 -0
  40. package/dist/lib/state.js +74 -25
  41. package/dist/lib/telemetry.js +726 -0
  42. package/dist/lib/viewer-http.js +2 -2
  43. package/dist/lib/workspace.js +187 -43
  44. package/package.json +1 -1
  45. package/schemas/config.schema.json +116 -0
  46. package/schemas/meta.schema.json +5 -1
  47. package/schemas/output.schema.json +442 -0
  48. package/schemas/probes.schema.json +90 -0
package/dist/lib/state.js CHANGED
@@ -1,7 +1,12 @@
1
+ import crypto from "node:crypto";
1
2
  import fs from "node:fs";
3
+ import path from "node:path";
2
4
  import { canAdvanceRevisions, gitCommitExists, readChangedPathsSince, readGitState } from "./git.js";
3
5
  import { normalizeRepoRelativePath } from "./fs.js";
6
+ import { getDocumentCodePaths, getDocumentRequires } from "./routing.js";
4
7
  import { matchesCodePathPattern } from "./search.js";
8
+ import { LLMDOC_CONFIG_FILENAME, LLMDOC_LOCAL_CONFIG_FILENAME } from "./config.js";
9
+ import { LLMDOC_PROBES_FILENAME } from "./constants.js";
5
10
  export function analyzeDelta(workspace, scope) {
6
11
  const git = readWorkspaceGitState(workspace);
7
12
  const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
@@ -30,8 +35,8 @@ export function analyzeDelta(workspace, scope) {
30
35
  if (revisionInvalid) {
31
36
  invalidRevisionDocuments.push(document);
32
37
  invalidRevisionReasons.push(!validatedRevision
33
- ? `文档 ${document.llmdocPath} 缺少 validatedRevision`
34
- : `文档 ${document.llmdocPath} 的 validatedRevision 不存在于当前 git 历史: ${validatedRevision}`);
38
+ ? `Document ${document.llmdocPath} has no validatedRevision`
39
+ : `Document ${document.llmdocPath} has a validatedRevision that does not exist in current Git history: ${validatedRevision}`);
35
40
  }
36
41
  const changedCommittedPaths = !revisionInvalid && git.headRevision && validatedRevision !== git.headRevision
37
42
  ? getChangedPathsForRevision(workspace.rootDir, validatedRevision, git.headRevision, changedPathsByRevision).filter((filePath) => isSurface(filePath) && documentMatchesPath(document, filePath))
@@ -71,26 +76,24 @@ export function analyzeDelta(workspace, scope) {
71
76
  const unmappedDirtyPaths = dirtyPaths.filter((filePath) => !workspace.documents.some((document) => documentMatchesPath(document, filePath)));
72
77
  const reasons = [];
73
78
  if (!git.available || git.degradedReason) {
74
- reasons.push(git.degradedReason ?? "git 状态不可用");
79
+ reasons.push(git.degradedReason ?? "Git state is unavailable");
75
80
  }
76
81
  if (invalidRevisionReasons.length > 0) {
77
82
  reasons.push(...invalidRevisionReasons);
78
83
  }
79
84
  if (unmappedCommittedPaths.length > 0 || unmappedDirtyPaths.length > 0) {
80
- reasons.push("存在未映射代码路径");
85
+ reasons.push("Unmapped code paths exist");
81
86
  }
82
87
  if (needsReview.length > 0) {
83
- reasons.push("存在 requires 反向一跳需复核文档");
88
+ reasons.push("Documents in the reverse requires closure need review");
84
89
  }
85
90
  if (directImpacts.length > 8) {
86
- reasons.push("受影响文档过多,建议 deep");
91
+ reasons.push("Too many documents are impacted; deep mode is recommended");
87
92
  }
88
93
  if (dirtyDocuments.length > 0) {
89
- reasons.push("存在未提交 dirty 关联代码");
94
+ reasons.push("Mapped implementation code has uncommitted changes");
90
95
  }
91
- const suggestedMode = reasons.some((reason) => ["未映射", "需复核", "过多", "dirty", "不可用", "不存在于当前 git 历史", "validatedRevision", "缺少"].some((keyword) => reason.includes(keyword)))
92
- ? "deep"
93
- : "light";
96
+ const suggestedMode = reasons.length > 0 ? "deep" : "light";
94
97
  return {
95
98
  git,
96
99
  impacts: directImpacts.sort((left, right) => left.document.llmdocPath.localeCompare(right.document.llmdocPath)),
@@ -124,7 +127,7 @@ export function parseScope(values, workspace) {
124
127
  continue;
125
128
  }
126
129
  if (!workspace.documentsByLlmdocPath.has(normalized)) {
127
- throw new Error(`scope 未命中任何 topic 或文档: ${raw}`);
130
+ throw new Error(`Scope did not match any topic or document: ${raw}`);
128
131
  }
129
132
  documentPaths.add(normalized);
130
133
  }
@@ -151,34 +154,43 @@ export function computeGrowthState(workspace) {
151
154
  // fingerprint 推进的全部前置条件(git 可推进 + 关联源码无 dirty),
152
155
  // 供 updateMetaRevisions 执行前使用,也供 commit 在创建任何 commit 前 preflight,失败时 fail-closed。
153
156
  export function assertRevisionAdvancePreconditions(input) {
154
- const { workspace, llmdocPaths, updateAll } = input;
157
+ const { workspace, llmdocPaths, updateAll, refreshConvergence = false } = input;
155
158
  if (!workspace.meta) {
156
- throw new Error("缺少 meta.json");
159
+ throw new Error("meta.json is missing");
157
160
  }
158
161
  const git = readWorkspaceGitState(workspace);
159
162
  const advance = canAdvanceRevisions(git);
160
163
  if (!advance.ok) {
161
164
  throw new Error(advance.reason);
162
165
  }
163
- const targetPaths = updateAll ? workspace.documents.map((document) => document.llmdocPath) : llmdocPaths;
166
+ const allDocPaths = workspace.documents.map((document) => document.llmdocPath);
167
+ const targetPaths = updateAll ? allDocPaths : llmdocPaths;
168
+ // A full-repo convergence snapshot summarizes every surviving document, so require every mapped owner to be clean.
169
+ const preflightPaths = updateAll || refreshConvergence ? allDocPaths : llmdocPaths;
164
170
  const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
165
171
  const dirtyPaths = uniquePaths([...git.stagedPaths, ...git.unstagedPaths, ...git.untrackedPaths]).filter((filePath) => isImplementationSurfacePath(filePath, ignorePatterns));
166
172
  // 只有映射到某份文档 code.paths 的 dirty 变更才阻塞推进;
167
173
  // 无关的 untracked/dirty 文件不影响 revision 的真实性,不应拦路。
168
- const blocked = targetPaths.filter((docPath) => {
174
+ const blocked = preflightPaths.filter((docPath) => {
169
175
  const document = workspace.documentsByLlmdocPath.get(docPath);
170
176
  return document ? dirtyPaths.some((filePath) => documentMatchesPath(document, filePath)) : false;
171
177
  });
172
178
  if (blocked.length > 0) {
173
179
  throw new Error(updateAll
174
- ? `全量 fingerprint 时以下文档关联代码存在 dirty 变更,不能伪造 revision: ${blocked.join(", ")}`
175
- : `以下文档关联代码存在 dirty 变更,不能伪造 revision: ${blocked.join(", ")}`);
180
+ ? `A full fingerprint cannot advance while mapped code for these documents is dirty: ${blocked.join(", ")}`
181
+ : refreshConvergence
182
+ ? `Cannot refresh convergence snapshot while mapped code for these documents is dirty: ${blocked.join(", ")}`
183
+ : `Cannot advance revisions while mapped code for these documents is dirty: ${blocked.join(", ")}`);
176
184
  }
177
185
  return { git, targetPaths };
178
186
  }
179
187
  export function updateMetaRevisions(input) {
180
- const { workspace, updateAll } = input;
181
- const { git, targetPaths } = assertRevisionAdvancePreconditions(input);
188
+ const { workspace, updateAll, refreshConvergenceSource } = input;
189
+ const { git, targetPaths } = assertRevisionAdvancePreconditions({
190
+ ...input,
191
+ refreshConvergence: Boolean(refreshConvergenceSource)
192
+ });
193
+ const nowIso = new Date().toISOString();
182
194
  const nextMeta = structuredClone(workspace.meta);
183
195
  // 文档已被删除的孤儿 ledger 条目在此顺带清理:
184
196
  // recorder 不允许手编辑 meta.json,删除文档后的 ledger 收敛只能由 CLI 完成。
@@ -189,17 +201,50 @@ export function updateMetaRevisions(input) {
189
201
  }
190
202
  for (const docPath of targetPaths) {
191
203
  nextMeta.documents[docPath] = {
192
- validatedRevision: git.headRevision
204
+ validatedRevision: git.headRevision,
205
+ verifiedAt: nowIso
193
206
  };
194
207
  }
195
208
  if (updateAll) {
196
209
  nextMeta.baseline.revision = git.headRevision;
197
- nextMeta.baseline.verifiedAt = new Date().toISOString();
210
+ nextMeta.baseline.verifiedAt = nowIso;
211
+ }
212
+ if (refreshConvergenceSource) {
213
+ nextMeta.convergence = {
214
+ capturedAt: nowIso,
215
+ source: refreshConvergenceSource,
216
+ documentCount: workspace.documents.length,
217
+ totalEstimatedTokens: workspace.documents.reduce((sum, document) => sum + document.estimatedTokens, 0)
218
+ };
198
219
  }
199
220
  return { meta: nextMeta, updatedPaths: targetPaths.slice().sort() };
200
221
  }
201
222
  export function writeMeta(metaPath, meta) {
202
- fs.writeFileSync(metaPath, `${JSON.stringify(meta, null, 2)}\n`);
223
+ writeFileAtomically(metaPath, Buffer.from(`${JSON.stringify(meta, null, 2)}\n`, "utf8"));
224
+ }
225
+ export function writeFileAtomically(targetPath, content) {
226
+ const dir = path.dirname(targetPath);
227
+ const tempPath = path.join(dir, `.meta.${process.pid}.${Date.now()}.${crypto.randomBytes(6).toString("hex")}.tmp`);
228
+ const noFollow = fs.constants.O_NOFOLLOW ?? 0;
229
+ const fd = fs.openSync(tempPath, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | noFollow, 0o644);
230
+ try {
231
+ fs.writeSync(fd, content, 0, content.length, 0);
232
+ }
233
+ finally {
234
+ fs.closeSync(fd);
235
+ }
236
+ try {
237
+ fs.renameSync(tempPath, targetPath);
238
+ }
239
+ catch (error) {
240
+ try {
241
+ fs.unlinkSync(tempPath);
242
+ }
243
+ catch {
244
+ // Ignore cleanup errors.
245
+ }
246
+ throw error;
247
+ }
203
248
  }
204
249
  function applyScope(documents, scope) {
205
250
  if (!scope) {
@@ -213,7 +258,7 @@ function applyScope(documents, scope) {
213
258
  });
214
259
  }
215
260
  function documentMatchesPath(document, repoRelativePath) {
216
- return (document.frontmatter.code?.paths ?? []).some((pattern) => matchesCodePathPattern(pattern, repoRelativePath));
261
+ return getDocumentCodePaths(document).some((pattern) => matchesCodePathPattern(pattern, repoRelativePath));
217
262
  }
218
263
  function uniquePaths(paths) {
219
264
  return [...new Set(paths)].sort();
@@ -257,7 +302,11 @@ export function loadIgnorePatterns(rootDir) {
257
302
  return patterns;
258
303
  }
259
304
  export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
260
- if (repoRelativePath.startsWith("llmdoc/") || repoRelativePath.startsWith(".llmdoc-tmp/")) {
305
+ if (repoRelativePath === LLMDOC_CONFIG_FILENAME ||
306
+ repoRelativePath === LLMDOC_LOCAL_CONFIG_FILENAME ||
307
+ repoRelativePath === LLMDOC_PROBES_FILENAME ||
308
+ repoRelativePath.startsWith("llmdoc/") ||
309
+ repoRelativePath.startsWith(".llmdoc-tmp/")) {
261
310
  return false;
262
311
  }
263
312
  const basename = repoRelativePath.split("/").pop() ?? repoRelativePath;
@@ -269,7 +318,7 @@ export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = [
269
318
  function buildReverseRequires(documents) {
270
319
  const mapping = new Map();
271
320
  for (const document of documents) {
272
- for (const requirement of document.frontmatter.relations?.requires ?? []) {
321
+ for (const requirement of getDocumentRequires(document)) {
273
322
  const bucket = mapping.get(requirement) ?? [];
274
323
  bucket.push(document);
275
324
  mapping.set(requirement, bucket);