@tokenroll/llmdoc 3.0.0 → 3.1.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/cli.js +25 -0
- package/dist/commands/commit.js +62 -0
- package/dist/commands/init-state.js +44 -0
- package/dist/commands/upgrade.js +2 -2
- package/dist/lib/state.js +36 -6
- package/dist/lib/workspace.js +8 -0
- package/package.json +1 -1
- package/schemas/output.schema.json +62 -0
package/dist/cli.js
CHANGED
|
@@ -129,6 +129,31 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
129
129
|
all: commandOptions.all
|
|
130
130
|
}), globalOptions.json));
|
|
131
131
|
});
|
|
132
|
+
program
|
|
133
|
+
.command("init-state")
|
|
134
|
+
.description("首次生成 llmdoc/meta.json 台账骨架(validatedRevision 全部为 null)")
|
|
135
|
+
.action(async () => {
|
|
136
|
+
const { runInitState } = await import("./commands/init-state.js");
|
|
137
|
+
const rootDir = findProjectRoot(cwd);
|
|
138
|
+
output.push(writeOutput("initState", runInitState({ ...globalOptions, cwd: rootDir }), globalOptions.json));
|
|
139
|
+
});
|
|
140
|
+
program
|
|
141
|
+
.command("commit")
|
|
142
|
+
.description("一体化收尾:validate 门控 → 提交 llmdoc 写集 → fingerprint → meta 小 commit")
|
|
143
|
+
.option("-m, --message <message>", "docs commit message")
|
|
144
|
+
.option("--all", "fingerprint 全部文档并推进 baseline")
|
|
145
|
+
.option("--no-verify", "透传 git commit --no-verify(husky 等重钩子仓库)")
|
|
146
|
+
.action(async (commandOptions) => {
|
|
147
|
+
const { runCommit } = await import("./commands/commit.js");
|
|
148
|
+
const rootDir = findProjectRoot(cwd);
|
|
149
|
+
output.push(writeOutput("commit", runCommit({
|
|
150
|
+
...globalOptions,
|
|
151
|
+
cwd: rootDir,
|
|
152
|
+
message: commandOptions.message,
|
|
153
|
+
all: commandOptions.all,
|
|
154
|
+
noVerify: commandOptions.verify === false
|
|
155
|
+
}), globalOptions.json));
|
|
156
|
+
});
|
|
132
157
|
program
|
|
133
158
|
.command("prune")
|
|
134
159
|
.description("输出只读收敛候选报告")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { CliError } from "../lib/errors.js";
|
|
3
|
+
import { findProjectRoot } from "../lib/fs.js";
|
|
4
|
+
import { updateMetaRevisions, writeMeta } from "../lib/state.js";
|
|
5
|
+
import { loadWorkspace, validateWorkspace } from "../lib/workspace.js";
|
|
6
|
+
// llmdoc 写入的一体化收尾:validate 门控 → commit llmdoc 写集 → fingerprint → meta 单独小 commit。
|
|
7
|
+
// 消灭手工三步曲及其 amend 追尾陷阱;--no-verify 透传给 git(husky 等重钩子仓库)。
|
|
8
|
+
export function runCommit(options) {
|
|
9
|
+
const rootDir = findProjectRoot(options.cwd);
|
|
10
|
+
const issues = validateWorkspace(loadWorkspace(rootDir));
|
|
11
|
+
const errors = issues.filter((issue) => issue.severity === "error");
|
|
12
|
+
if (errors.length > 0) {
|
|
13
|
+
throw new CliError(`validate 未通过,拒绝提交:\n${errors.map((issue) => ` ${issue.code} (${issue.path})`).join("\n")}`);
|
|
14
|
+
}
|
|
15
|
+
const porcelain = runGit(rootDir, ["status", "--porcelain", "--", "llmdoc"]);
|
|
16
|
+
if (!porcelain.trim()) {
|
|
17
|
+
return options.json ? { status: "no_change", commits: [], updated: [] } : "no_change: llmdoc/ 无待提交变更";
|
|
18
|
+
}
|
|
19
|
+
const changedDocPaths = porcelain
|
|
20
|
+
.split(/\r?\n/)
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.map((line) => line.slice(3).trim())
|
|
23
|
+
.map((filePath) => (filePath.includes(" -> ") ? filePath.split(" -> ").pop() : filePath))
|
|
24
|
+
.filter((filePath) => filePath.startsWith("llmdoc/") && filePath.endsWith(".mdx"));
|
|
25
|
+
const verifyFlags = options.noVerify ? ["--no-verify"] : [];
|
|
26
|
+
runGit(rootDir, ["add", "--", "llmdoc"]);
|
|
27
|
+
runGit(rootDir, ["commit", ...verifyFlags, "-m", options.message ?? "docs(llmdoc): update knowledge", "--", "llmdoc"]);
|
|
28
|
+
const docsCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
29
|
+
// 重新加载:提交后的 workspace 是 fingerprint 的事实基础;已删除文档的 ledger 孤儿由 updateMetaRevisions 清理。
|
|
30
|
+
const workspace = loadWorkspace(rootDir);
|
|
31
|
+
const existingDocPaths = changedDocPaths
|
|
32
|
+
.map((filePath) => filePath.slice("llmdoc/".length))
|
|
33
|
+
.filter((llmdocPath) => workspace.documentsByLlmdocPath.has(llmdocPath));
|
|
34
|
+
const { meta, updatedPaths } = updateMetaRevisions({
|
|
35
|
+
workspace,
|
|
36
|
+
llmdocPaths: existingDocPaths,
|
|
37
|
+
updateAll: options.all ?? false
|
|
38
|
+
});
|
|
39
|
+
writeMeta(workspace.metaPath, meta);
|
|
40
|
+
runGit(rootDir, ["add", "--", "llmdoc/meta.json"]);
|
|
41
|
+
runGit(rootDir, ["commit", ...verifyFlags, "-m", "chore(llmdoc): refresh fingerprints", "--", "llmdoc/meta.json"]);
|
|
42
|
+
const metaCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
43
|
+
if (options.json) {
|
|
44
|
+
return {
|
|
45
|
+
status: "success",
|
|
46
|
+
commits: [docsCommit, metaCommit],
|
|
47
|
+
updated: updatedPaths,
|
|
48
|
+
baselineAdvanced: options.all ?? false
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return [
|
|
52
|
+
`committed: ${docsCommit.slice(0, 7)} (docs) + ${metaCommit.slice(0, 7)} (meta)`,
|
|
53
|
+
`fingerprints: ${updatedPaths.length} document(s)${options.all ? ", baseline advanced" : ""}`
|
|
54
|
+
].join("\n");
|
|
55
|
+
}
|
|
56
|
+
function runGit(rootDir, args) {
|
|
57
|
+
const result = spawnSync("git", ["-c", "core.quotePath=false", ...args], { cwd: rootDir, encoding: "utf8" });
|
|
58
|
+
if (result.status !== 0) {
|
|
59
|
+
throw new CliError((result.stderr || result.stdout || `git ${args[0]} 失败`).trim());
|
|
60
|
+
}
|
|
61
|
+
return result.stdout;
|
|
62
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { CliError } from "../lib/errors.js";
|
|
3
|
+
import { findProjectRoot } from "../lib/fs.js";
|
|
4
|
+
import { readWorkspaceGitState } from "../lib/state.js";
|
|
5
|
+
import { loadWorkspace } from "../lib/workspace.js";
|
|
6
|
+
// init/upgrade 场景的 meta.json 骨架生成:
|
|
7
|
+
// 全部文档登记为 validatedRevision: null(未验证,delta 会如实标 impacted),
|
|
8
|
+
// convergence 用当前实测统计。真正的 revision 烙印仍由 fingerprint 完成。
|
|
9
|
+
export function runInitState(options) {
|
|
10
|
+
const rootDir = findProjectRoot(options.cwd);
|
|
11
|
+
const workspace = loadWorkspace(rootDir);
|
|
12
|
+
if (workspace.meta) {
|
|
13
|
+
throw new CliError("llmdoc/meta.json 已存在;init-state 只用于首次建立台账,不覆盖现有状态。");
|
|
14
|
+
}
|
|
15
|
+
const git = readWorkspaceGitState(workspace);
|
|
16
|
+
if (!git.available || !git.headRevision) {
|
|
17
|
+
throw new CliError(git.degradedReason ?? "无法解析 HEAD commit,init-state 需要 git 仓库。");
|
|
18
|
+
}
|
|
19
|
+
const now = new Date().toISOString().replace(/\.\d+Z$/, "Z");
|
|
20
|
+
const meta = {
|
|
21
|
+
schema: "llmdoc.meta/v3",
|
|
22
|
+
baseline: {
|
|
23
|
+
revision: git.headRevision,
|
|
24
|
+
verifiedAt: now
|
|
25
|
+
},
|
|
26
|
+
documents: Object.fromEntries(workspace.documents.map((document) => [document.llmdocPath, { validatedRevision: null }])),
|
|
27
|
+
convergence: {
|
|
28
|
+
capturedAt: now,
|
|
29
|
+
source: "init",
|
|
30
|
+
documentCount: workspace.documents.length,
|
|
31
|
+
totalEstimatedTokens: workspace.documents.reduce((sum, document) => sum + document.estimatedTokens, 0)
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
fs.writeFileSync(workspace.metaPath, `${JSON.stringify(meta, null, 2)}\n`);
|
|
35
|
+
if (options.json) {
|
|
36
|
+
return {
|
|
37
|
+
status: "success",
|
|
38
|
+
documents: workspace.documents.length,
|
|
39
|
+
baselineRevision: git.headRevision,
|
|
40
|
+
next: "llmdoc fingerprint --all"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return `initialized llmdoc/meta.json: ${workspace.documents.length} documents (validatedRevision: null), baseline ${git.headRevision.slice(0, 7)}\nnext: 验证文档内容后运行 \`llmdoc fingerprint --all\` 烙印 revision`;
|
|
44
|
+
}
|
package/dist/commands/upgrade.js
CHANGED
|
@@ -92,7 +92,7 @@ function inspectLlmdocDirectory(llmdocDir) {
|
|
|
92
92
|
status: "dry_run",
|
|
93
93
|
summary: "检测到 llmdoc/ 目录,但没有明确的 V2 或 V3 结构特征。",
|
|
94
94
|
legacyPaths: [],
|
|
95
|
-
targetStructure: ["llmdoc/meta.json", "llmdoc/architecture.mdx", "llmdoc/<topic
|
|
95
|
+
targetStructure: ["llmdoc/meta.json", "llmdoc/architecture.mdx", "llmdoc/<topic>/*.mdx (纯目录,无 index.mdx 入口节点)"],
|
|
96
96
|
requiresRecorderSemanticMigration: false,
|
|
97
97
|
notes: ["需要人工确认该目录是否为遗留知识面。"]
|
|
98
98
|
};
|
|
@@ -101,7 +101,7 @@ function inspectLlmdocDirectory(llmdocDir) {
|
|
|
101
101
|
status: "dry_run",
|
|
102
102
|
summary: "检测到 legacy/V2 结构,需要 Recorder 参与语义迁移到 V3。",
|
|
103
103
|
legacyPaths,
|
|
104
|
-
targetStructure: ["llmdoc/meta.json", "llmdoc/architecture.mdx", "llmdoc/<topic
|
|
104
|
+
targetStructure: ["llmdoc/meta.json", "llmdoc/architecture.mdx", "llmdoc/<topic>/*.mdx (纯目录,无 index.mdx 入口节点)"],
|
|
105
105
|
requiresRecorderSemanticMigration: true,
|
|
106
106
|
notes: [
|
|
107
107
|
hasV3Meta ? "存在部分 V3 迹象,但 legacy 结构仍在,需要整理边界后再迁移。" : "未发现完整 V3 ledger,需要生成新的 meta.json baseline。",
|
package/dist/lib/state.js
CHANGED
|
@@ -4,8 +4,10 @@ import { normalizeRepoRelativePath } from "./fs.js";
|
|
|
4
4
|
import { matchesCodePathPattern } from "./search.js";
|
|
5
5
|
export function analyzeDelta(workspace, scope) {
|
|
6
6
|
const git = readWorkspaceGitState(workspace);
|
|
7
|
+
const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
|
|
8
|
+
const isSurface = (repoRelativePath) => isImplementationSurfacePath(repoRelativePath, ignorePatterns);
|
|
7
9
|
const scopedDocuments = applyScope(workspace.documents, scope);
|
|
8
|
-
const dirtyPaths = uniquePaths([...git.stagedPaths, ...git.unstagedPaths, ...git.untrackedPaths].filter(
|
|
10
|
+
const dirtyPaths = uniquePaths([...git.stagedPaths, ...git.unstagedPaths, ...git.untrackedPaths].filter(isSurface));
|
|
9
11
|
const invalidRevisionDocuments = [];
|
|
10
12
|
const invalidRevisionReasons = [];
|
|
11
13
|
const changedPathsByRevision = new Map();
|
|
@@ -32,7 +34,7 @@ export function analyzeDelta(workspace, scope) {
|
|
|
32
34
|
: `文档 ${document.llmdocPath} 的 validatedRevision 不存在于当前 git 历史: ${validatedRevision}`);
|
|
33
35
|
}
|
|
34
36
|
const changedCommittedPaths = !revisionInvalid && git.headRevision && validatedRevision !== git.headRevision
|
|
35
|
-
? getChangedPathsForRevision(workspace.rootDir, validatedRevision, git.headRevision, changedPathsByRevision).filter((filePath) =>
|
|
37
|
+
? getChangedPathsForRevision(workspace.rootDir, validatedRevision, git.headRevision, changedPathsByRevision).filter((filePath) => isSurface(filePath) && documentMatchesPath(document, filePath))
|
|
36
38
|
: [];
|
|
37
39
|
const dirtyDocumentPaths = dirtyPaths.filter((filePath) => documentMatchesPath(document, filePath));
|
|
38
40
|
return {
|
|
@@ -64,7 +66,7 @@ export function analyzeDelta(workspace, scope) {
|
|
|
64
66
|
}
|
|
65
67
|
const needsReview = [...needsReviewMap.values()].sort((left, right) => left.llmdocPath.localeCompare(right.llmdocPath));
|
|
66
68
|
const dirtyDocuments = directImpacts.filter((impact) => impact.dirtyPaths.length > 0).map((impact) => impact.document);
|
|
67
|
-
const baselineCommittedPaths = git.committedChangedPaths.filter(
|
|
69
|
+
const baselineCommittedPaths = git.committedChangedPaths.filter(isSurface);
|
|
68
70
|
const unmappedCommittedPaths = baselineCommittedPaths.filter((filePath) => !workspace.documents.some((document) => documentMatchesPath(document, filePath)));
|
|
69
71
|
const unmappedDirtyPaths = dirtyPaths.filter((filePath) => !workspace.documents.some((document) => documentMatchesPath(document, filePath)));
|
|
70
72
|
const reasons = [];
|
|
@@ -157,7 +159,8 @@ export function updateMetaRevisions(input) {
|
|
|
157
159
|
throw new Error(advance.reason);
|
|
158
160
|
}
|
|
159
161
|
const targetPaths = updateAll ? workspace.documents.map((document) => document.llmdocPath) : llmdocPaths;
|
|
160
|
-
const
|
|
162
|
+
const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
|
|
163
|
+
const dirtyPaths = uniquePaths([...git.stagedPaths, ...git.unstagedPaths, ...git.untrackedPaths]).filter((filePath) => isImplementationSurfacePath(filePath, ignorePatterns));
|
|
161
164
|
// 只有映射到某份文档 code.paths 的 dirty 变更才阻塞推进;
|
|
162
165
|
// 无关的 untracked/dirty 文件不影响 revision 的真实性,不应拦路。
|
|
163
166
|
const blocked = targetPaths.filter((docPath) => {
|
|
@@ -222,12 +225,39 @@ const NON_IMPLEMENTATION_BASENAMES = new Set([
|
|
|
222
225
|
"Gemfile.lock",
|
|
223
226
|
"go.sum"
|
|
224
227
|
]);
|
|
225
|
-
|
|
228
|
+
const ignorePatternsCache = new Map();
|
|
229
|
+
// .llmdocignore:仓库根的可选文件,每行一个 minimatch pattern(# 开头为注释),
|
|
230
|
+
// 匹配的路径不参与 unmapped/dirty 信号(本地运行时文件、生成物等非知识面路径)。
|
|
231
|
+
export function loadIgnorePatterns(rootDir) {
|
|
232
|
+
const cached = ignorePatternsCache.get(rootDir);
|
|
233
|
+
if (cached) {
|
|
234
|
+
return cached;
|
|
235
|
+
}
|
|
236
|
+
const ignoreFile = `${rootDir}/.llmdocignore`;
|
|
237
|
+
let patterns = [];
|
|
238
|
+
try {
|
|
239
|
+
patterns = fs
|
|
240
|
+
.readFileSync(ignoreFile, "utf8")
|
|
241
|
+
.split(/\r?\n/)
|
|
242
|
+
.map((line) => line.trim())
|
|
243
|
+
.filter((line) => line && !line.startsWith("#"))
|
|
244
|
+
.map((line) => line.replace(/^\.\//, "").replace(/\/$/, "/**"));
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
patterns = [];
|
|
248
|
+
}
|
|
249
|
+
ignorePatternsCache.set(rootDir, patterns);
|
|
250
|
+
return patterns;
|
|
251
|
+
}
|
|
252
|
+
function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
|
|
226
253
|
if (repoRelativePath.startsWith("llmdoc/") || repoRelativePath.startsWith(".llmdoc-tmp/")) {
|
|
227
254
|
return false;
|
|
228
255
|
}
|
|
229
256
|
const basename = repoRelativePath.split("/").pop() ?? repoRelativePath;
|
|
230
|
-
|
|
257
|
+
if (NON_IMPLEMENTATION_BASENAMES.has(basename)) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
return !ignorePatterns.some((pattern) => matchesCodePathPattern(pattern, repoRelativePath));
|
|
231
261
|
}
|
|
232
262
|
function buildReverseRequires(documents) {
|
|
233
263
|
const mapping = new Map();
|
package/dist/lib/workspace.js
CHANGED
|
@@ -194,6 +194,14 @@ export function validateWorkspace(workspace) {
|
|
|
194
194
|
message
|
|
195
195
|
});
|
|
196
196
|
}
|
|
197
|
+
if (/\[\[[^\]\n]+\]\]/.test(stripMarkdownLiterals(document.body))) {
|
|
198
|
+
issues.push({
|
|
199
|
+
severity: "warning",
|
|
200
|
+
code: "link.wikilink",
|
|
201
|
+
path: document.repoPath,
|
|
202
|
+
message: "正文包含 [[wikilink]] 语法;llmdoc 不解析它,请改用标准 Markdown 相对链接。"
|
|
203
|
+
});
|
|
204
|
+
}
|
|
197
205
|
for (const relationPath of document.frontmatter.relations?.requires ?? []) {
|
|
198
206
|
const normalizedRelation = validateLlmdocRelativePath(relationPath);
|
|
199
207
|
if (!normalizedRelation.ok) {
|
package/package.json
CHANGED
|
@@ -903,6 +903,68 @@
|
|
|
903
903
|
"minLength": 1
|
|
904
904
|
}
|
|
905
905
|
}
|
|
906
|
+
},
|
|
907
|
+
"initState": {
|
|
908
|
+
"type": "object",
|
|
909
|
+
"additionalProperties": false,
|
|
910
|
+
"required": [
|
|
911
|
+
"status",
|
|
912
|
+
"documents",
|
|
913
|
+
"baselineRevision",
|
|
914
|
+
"next"
|
|
915
|
+
],
|
|
916
|
+
"properties": {
|
|
917
|
+
"status": {
|
|
918
|
+
"type": "string",
|
|
919
|
+
"const": "success"
|
|
920
|
+
},
|
|
921
|
+
"documents": {
|
|
922
|
+
"type": "integer",
|
|
923
|
+
"minimum": 0
|
|
924
|
+
},
|
|
925
|
+
"baselineRevision": {
|
|
926
|
+
"type": "string",
|
|
927
|
+
"minLength": 1
|
|
928
|
+
},
|
|
929
|
+
"next": {
|
|
930
|
+
"type": "string"
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
},
|
|
934
|
+
"commit": {
|
|
935
|
+
"type": "object",
|
|
936
|
+
"additionalProperties": false,
|
|
937
|
+
"required": [
|
|
938
|
+
"status",
|
|
939
|
+
"commits",
|
|
940
|
+
"updated"
|
|
941
|
+
],
|
|
942
|
+
"properties": {
|
|
943
|
+
"status": {
|
|
944
|
+
"type": "string",
|
|
945
|
+
"enum": [
|
|
946
|
+
"success",
|
|
947
|
+
"no_change"
|
|
948
|
+
]
|
|
949
|
+
},
|
|
950
|
+
"commits": {
|
|
951
|
+
"type": "array",
|
|
952
|
+
"items": {
|
|
953
|
+
"type": "string",
|
|
954
|
+
"minLength": 1
|
|
955
|
+
}
|
|
956
|
+
},
|
|
957
|
+
"updated": {
|
|
958
|
+
"type": "array",
|
|
959
|
+
"items": {
|
|
960
|
+
"type": "string",
|
|
961
|
+
"minLength": 1
|
|
962
|
+
}
|
|
963
|
+
},
|
|
964
|
+
"baselineAdvanced": {
|
|
965
|
+
"type": "boolean"
|
|
966
|
+
}
|
|
967
|
+
}
|
|
906
968
|
}
|
|
907
969
|
}
|
|
908
970
|
}
|