@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.
package/dist/lib/state.js CHANGED
@@ -1,8 +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";
5
- import { LLMDOC_CONFIG_FILENAME } from "./config.js";
8
+ import { LLMDOC_CONFIG_FILENAME, LLMDOC_LOCAL_CONFIG_FILENAME } from "./config.js";
9
+ import { LLMDOC_PROBES_FILENAME } from "./constants.js";
6
10
  export function analyzeDelta(workspace, scope) {
7
11
  const git = readWorkspaceGitState(workspace);
8
12
  const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
@@ -150,7 +154,7 @@ export function computeGrowthState(workspace) {
150
154
  // fingerprint 推进的全部前置条件(git 可推进 + 关联源码无 dirty),
151
155
  // 供 updateMetaRevisions 执行前使用,也供 commit 在创建任何 commit 前 preflight,失败时 fail-closed。
152
156
  export function assertRevisionAdvancePreconditions(input) {
153
- const { workspace, llmdocPaths, updateAll } = input;
157
+ const { workspace, llmdocPaths, updateAll, refreshConvergence = false } = input;
154
158
  if (!workspace.meta) {
155
159
  throw new Error("meta.json is missing");
156
160
  }
@@ -159,25 +163,34 @@ export function assertRevisionAdvancePreconditions(input) {
159
163
  if (!advance.ok) {
160
164
  throw new Error(advance.reason);
161
165
  }
162
- 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;
163
170
  const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
164
171
  const dirtyPaths = uniquePaths([...git.stagedPaths, ...git.unstagedPaths, ...git.untrackedPaths]).filter((filePath) => isImplementationSurfacePath(filePath, ignorePatterns));
165
172
  // 只有映射到某份文档 code.paths 的 dirty 变更才阻塞推进;
166
173
  // 无关的 untracked/dirty 文件不影响 revision 的真实性,不应拦路。
167
- const blocked = targetPaths.filter((docPath) => {
174
+ const blocked = preflightPaths.filter((docPath) => {
168
175
  const document = workspace.documentsByLlmdocPath.get(docPath);
169
176
  return document ? dirtyPaths.some((filePath) => documentMatchesPath(document, filePath)) : false;
170
177
  });
171
178
  if (blocked.length > 0) {
172
179
  throw new Error(updateAll
173
180
  ? `A full fingerprint cannot advance while mapped code for these documents is dirty: ${blocked.join(", ")}`
174
- : `Cannot advance revisions 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(", ")}`);
175
184
  }
176
185
  return { git, targetPaths };
177
186
  }
178
187
  export function updateMetaRevisions(input) {
179
- const { workspace, updateAll } = input;
180
- 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();
181
194
  const nextMeta = structuredClone(workspace.meta);
182
195
  // 文档已被删除的孤儿 ledger 条目在此顺带清理:
183
196
  // recorder 不允许手编辑 meta.json,删除文档后的 ledger 收敛只能由 CLI 完成。
@@ -188,17 +201,50 @@ export function updateMetaRevisions(input) {
188
201
  }
189
202
  for (const docPath of targetPaths) {
190
203
  nextMeta.documents[docPath] = {
191
- validatedRevision: git.headRevision
204
+ validatedRevision: git.headRevision,
205
+ verifiedAt: nowIso
192
206
  };
193
207
  }
194
208
  if (updateAll) {
195
209
  nextMeta.baseline.revision = git.headRevision;
196
- 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
+ };
197
219
  }
198
220
  return { meta: nextMeta, updatedPaths: targetPaths.slice().sort() };
199
221
  }
200
222
  export function writeMeta(metaPath, meta) {
201
- 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
+ }
202
248
  }
203
249
  function applyScope(documents, scope) {
204
250
  if (!scope) {
@@ -212,7 +258,7 @@ function applyScope(documents, scope) {
212
258
  });
213
259
  }
214
260
  function documentMatchesPath(document, repoRelativePath) {
215
- return (document.frontmatter.code?.paths ?? []).some((pattern) => matchesCodePathPattern(pattern, repoRelativePath));
261
+ return getDocumentCodePaths(document).some((pattern) => matchesCodePathPattern(pattern, repoRelativePath));
216
262
  }
217
263
  function uniquePaths(paths) {
218
264
  return [...new Set(paths)].sort();
@@ -257,6 +303,8 @@ export function loadIgnorePatterns(rootDir) {
257
303
  }
258
304
  export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
259
305
  if (repoRelativePath === LLMDOC_CONFIG_FILENAME ||
306
+ repoRelativePath === LLMDOC_LOCAL_CONFIG_FILENAME ||
307
+ repoRelativePath === LLMDOC_PROBES_FILENAME ||
260
308
  repoRelativePath.startsWith("llmdoc/") ||
261
309
  repoRelativePath.startsWith(".llmdoc-tmp/")) {
262
310
  return false;
@@ -270,7 +318,7 @@ export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = [
270
318
  function buildReverseRequires(documents) {
271
319
  const mapping = new Map();
272
320
  for (const document of documents) {
273
- for (const requirement of document.frontmatter.relations?.requires ?? []) {
321
+ for (const requirement of getDocumentRequires(document)) {
274
322
  const bucket = mapping.get(requirement) ?? [];
275
323
  bucket.push(document);
276
324
  mapping.set(requirement, bucket);