@tokenroll/llmdoc 3.2.0 → 3.4.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 +82 -43
- package/dist/commands/adopt.js +65 -0
- package/dist/commands/commit.js +18 -2
- package/dist/commands/hook.js +26 -8
- package/dist/commands/status.js +33 -2
- package/dist/lib/git.js +25 -0
- package/dist/lib/state.js +9 -2
- package/dist/lib/workspace.js +1 -1
- package/package.json +1 -1
- package/schemas/output.schema.json +32 -0
package/dist/cli.js
CHANGED
|
@@ -41,11 +41,39 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
41
41
|
}
|
|
42
42
|
throw error;
|
|
43
43
|
}
|
|
44
|
-
program
|
|
45
|
-
|
|
44
|
+
program
|
|
45
|
+
.name("llmdoc")
|
|
46
|
+
.description("面向 LLM 的项目知识库 CLI:渐进检索 llmdoc/ 文档,维护 revision 台账")
|
|
47
|
+
.version(readPackageVersion(), "--version", "输出 CLI 版本")
|
|
48
|
+
.helpOption("-h, --help", "显示帮助")
|
|
49
|
+
.helpCommand("help [command]", "显示指定命令的帮助")
|
|
50
|
+
.showHelpAfterError("(用 --help 查看用法)");
|
|
51
|
+
program
|
|
52
|
+
.option("--json", "以 JSON 输出(经 schema 校验)")
|
|
53
|
+
.option("--cursor <cursor>", "上次输出截断处的游标,从该处继续")
|
|
54
|
+
.option("--budget <tokens>", "输出 token 预算,超出即截断并返回 cursor", parseInteger)
|
|
55
|
+
.option("--limit <n>", "最多返回条数", parseInteger);
|
|
56
|
+
program.addHelpText("after", [
|
|
57
|
+
"",
|
|
58
|
+
"按用途速查:",
|
|
59
|
+
" 检索(只读) tree → index / search / context → show",
|
|
60
|
+
" 状态诊断 status · delta · validate",
|
|
61
|
+
" 结构改写 new · adopt · mv · fingerprint · init-state · commit",
|
|
62
|
+
" 维护诊断 prune · upgrade",
|
|
63
|
+
" 集成 hook · serve",
|
|
64
|
+
"",
|
|
65
|
+
"常用示例:",
|
|
66
|
+
" llmdoc tree --docs 全局地图,展开到文档级",
|
|
67
|
+
" llmdoc search \"重试策略\" --limit 5 词法检索文档",
|
|
68
|
+
" llmdoc context --files src/api/retry.ts 按源码反查应读文档",
|
|
69
|
+
" llmdoc show api-client/retry-policy.mdx 读取正文",
|
|
70
|
+
" llmdoc commit -m \"docs: ...\" 校验并提交 llmdoc 写集",
|
|
71
|
+
"",
|
|
72
|
+
"所有检索命令支持 --json / --budget / --limit;输出被截断时带 --cursor 继续。"
|
|
73
|
+
].join("\n"));
|
|
46
74
|
program
|
|
47
75
|
.command("tree")
|
|
48
|
-
.description("输出 llmdoc 全局地图")
|
|
76
|
+
.description("输出 llmdoc 全局地图(默认停在 topic 层)")
|
|
49
77
|
.option("--docs", "展开到文档级")
|
|
50
78
|
.action((commandOptions) => {
|
|
51
79
|
const rootDir = findProjectRoot(cwd);
|
|
@@ -54,9 +82,9 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
54
82
|
});
|
|
55
83
|
program
|
|
56
84
|
.command("index")
|
|
57
|
-
.description("列出文档元数据索引")
|
|
58
|
-
.option("--topic <topic>")
|
|
59
|
-
.option("--kind <kind>")
|
|
85
|
+
.description("列出文档元数据索引(不读正文即可判断相关性)")
|
|
86
|
+
.option("--topic <topic>", "只列指定 topic 下的文档")
|
|
87
|
+
.option("--kind <kind>", "只列指定类型: architecture | guide | reference")
|
|
60
88
|
.action((commandOptions) => {
|
|
61
89
|
const rootDir = findProjectRoot(cwd);
|
|
62
90
|
output.push(writeOutput("index", runIndex({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
@@ -64,25 +92,25 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
64
92
|
program
|
|
65
93
|
.command("show")
|
|
66
94
|
.description("读取一个或多个文档正文")
|
|
67
|
-
.argument("<path...>")
|
|
95
|
+
.argument("<path...>", "llmdoc/ 下的相对路径,如 api-client/retry-policy.mdx")
|
|
68
96
|
.action((paths) => {
|
|
69
97
|
const rootDir = findProjectRoot(cwd);
|
|
70
98
|
output.push(writeOutput("show", runShow({ ...globalOptions, cwd: rootDir, paths }), globalOptions.json));
|
|
71
99
|
});
|
|
72
100
|
program
|
|
73
101
|
.command("search")
|
|
74
|
-
.description("按词法检索 llmdoc 文档")
|
|
75
|
-
.argument("<query>")
|
|
76
|
-
.option("--topic <topic>")
|
|
77
|
-
.option("--kind <kind>")
|
|
102
|
+
.description("按词法检索 llmdoc 文档(front matter、标题与正文,返回 snippet)")
|
|
103
|
+
.argument("<query>", "检索词")
|
|
104
|
+
.option("--topic <topic>", "限定 topic")
|
|
105
|
+
.option("--kind <kind>", "限定类型: architecture | guide | reference")
|
|
78
106
|
.action((query, commandOptions) => {
|
|
79
107
|
const rootDir = findProjectRoot(cwd);
|
|
80
108
|
output.push(writeOutput("search", runSearch({ ...globalOptions, ...commandOptions, cwd: rootDir, query }), globalOptions.json));
|
|
81
109
|
});
|
|
82
110
|
program
|
|
83
111
|
.command("context")
|
|
84
|
-
.description("按源码文件反查应读文档")
|
|
85
|
-
.requiredOption("--files <files...>")
|
|
112
|
+
.description("按源码文件反查应读文档(含 requires 前置闭包)")
|
|
113
|
+
.requiredOption("--files <files...>", "源码文件路径,可多个")
|
|
86
114
|
.action((commandOptions) => {
|
|
87
115
|
const rootDir = findProjectRoot(cwd);
|
|
88
116
|
output.push(writeOutput("context", runContext({
|
|
@@ -109,17 +137,17 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
109
137
|
});
|
|
110
138
|
program
|
|
111
139
|
.command("delta")
|
|
112
|
-
.description("查看代码变化对应的文档影响面")
|
|
113
|
-
.option("--scope <scope...>")
|
|
140
|
+
.description("查看代码变化对应的文档影响面(决定 update 走 light 还是 deep)")
|
|
141
|
+
.option("--scope <scope...>", "限定参与比对的代码路径")
|
|
114
142
|
.action((commandOptions) => {
|
|
115
143
|
const rootDir = findProjectRoot(cwd);
|
|
116
144
|
output.push(writeOutput("delta", runDelta({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
117
145
|
});
|
|
118
146
|
program
|
|
119
147
|
.command("fingerprint")
|
|
120
|
-
.description("
|
|
121
|
-
.option("--update <path...>")
|
|
122
|
-
.option("--all", "
|
|
148
|
+
.description("把文档 validatedRevision 刷新到当前 HEAD")
|
|
149
|
+
.option("--update <path...>", "只刷新指定文档")
|
|
150
|
+
.option("--all", "刷新全部文档并推进 baseline")
|
|
123
151
|
.action((commandOptions) => {
|
|
124
152
|
const rootDir = findProjectRoot(cwd);
|
|
125
153
|
output.push(writeOutput("fingerprint", runFingerprint({
|
|
@@ -154,9 +182,43 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
154
182
|
noVerify: commandOptions.verify === false
|
|
155
183
|
}), globalOptions.json));
|
|
156
184
|
});
|
|
185
|
+
program
|
|
186
|
+
.command("new")
|
|
187
|
+
.description("在 llmdoc/ 下生成文档脚手架")
|
|
188
|
+
.argument("<path>", "目标相对路径,如 api-client/retry-policy.mdx")
|
|
189
|
+
.requiredOption("--kind <kind>", "文档类型: architecture | guide | reference")
|
|
190
|
+
.option("--description <description>", "front matter 一句话描述")
|
|
191
|
+
.action((targetPath, commandOptions) => {
|
|
192
|
+
const rootDir = findProjectRoot(cwd);
|
|
193
|
+
output.push(writeOutput("new", runNew({
|
|
194
|
+
...globalOptions,
|
|
195
|
+
cwd: rootDir,
|
|
196
|
+
path: targetPath,
|
|
197
|
+
kind: commandOptions.kind,
|
|
198
|
+
description: commandOptions.description
|
|
199
|
+
}), globalOptions.json));
|
|
200
|
+
});
|
|
201
|
+
program
|
|
202
|
+
.command("adopt")
|
|
203
|
+
.description("无损登记已存在的 .mdx 到 meta.json(validatedRevision: null,不改正文,幂等)")
|
|
204
|
+
.argument("<path...>", "已存在的 llmdoc/ 下相对路径,可多个")
|
|
205
|
+
.action(async (paths) => {
|
|
206
|
+
const { runAdopt } = await import("./commands/adopt.js");
|
|
207
|
+
const rootDir = findProjectRoot(cwd);
|
|
208
|
+
output.push(writeOutput("adopt", runAdopt({ ...globalOptions, cwd: rootDir, paths }), globalOptions.json));
|
|
209
|
+
});
|
|
210
|
+
program
|
|
211
|
+
.command("mv")
|
|
212
|
+
.description("移动/重命名文档或整个 topic,并重写内部引用")
|
|
213
|
+
.argument("<from>", "源路径")
|
|
214
|
+
.argument("<to>", "目标路径")
|
|
215
|
+
.action((from, to) => {
|
|
216
|
+
const rootDir = findProjectRoot(cwd);
|
|
217
|
+
output.push(writeOutput("mv", runMove({ ...globalOptions, cwd: rootDir, from, to }), globalOptions.json));
|
|
218
|
+
});
|
|
157
219
|
program
|
|
158
220
|
.command("prune")
|
|
159
|
-
.description("
|
|
221
|
+
.description("输出只读收敛报告(增长趋势、重复候选、小文档合并候选)")
|
|
160
222
|
.option("--report", "输出只读收敛报告")
|
|
161
223
|
.action((commandOptions) => {
|
|
162
224
|
const rootDir = findProjectRoot(cwd);
|
|
@@ -170,7 +232,7 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
170
232
|
const rootDir = findProjectRootOrNull(cwd) ?? cwd;
|
|
171
233
|
output.push(writeOutput("upgrade", await runUpgrade({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
172
234
|
});
|
|
173
|
-
const hookCommand = program.command("hook");
|
|
235
|
+
const hookCommand = program.command("hook").description("供编辑器/Agent hooks 调用的只读信号(异常时 fail-open)");
|
|
174
236
|
hookCommand
|
|
175
237
|
.command("session-start")
|
|
176
238
|
.description("输出 SessionStart 短状态信号")
|
|
@@ -201,29 +263,6 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
201
263
|
const rootDir = findProjectRoot(cwd);
|
|
202
264
|
output.push(await runServe({ cwd: rootDir, port: commandOptions.port }));
|
|
203
265
|
});
|
|
204
|
-
program
|
|
205
|
-
.command("new")
|
|
206
|
-
.argument("<path>")
|
|
207
|
-
.requiredOption("--kind <kind>")
|
|
208
|
-
.option("--description <description>")
|
|
209
|
-
.action((targetPath, commandOptions) => {
|
|
210
|
-
const rootDir = findProjectRoot(cwd);
|
|
211
|
-
output.push(writeOutput("new", runNew({
|
|
212
|
-
...globalOptions,
|
|
213
|
-
cwd: rootDir,
|
|
214
|
-
path: targetPath,
|
|
215
|
-
kind: commandOptions.kind,
|
|
216
|
-
description: commandOptions.description
|
|
217
|
-
}), globalOptions.json));
|
|
218
|
-
});
|
|
219
|
-
program
|
|
220
|
-
.command("mv")
|
|
221
|
-
.argument("<from>")
|
|
222
|
-
.argument("<to>")
|
|
223
|
-
.action((from, to) => {
|
|
224
|
-
const rootDir = findProjectRoot(cwd);
|
|
225
|
-
output.push(writeOutput("mv", runMove({ ...globalOptions, cwd: rootDir, from, to }), globalOptions.json));
|
|
226
|
-
});
|
|
227
266
|
try {
|
|
228
267
|
await program.parseAsync(argv, { from: "user" });
|
|
229
268
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { assertDocKindMatchesShape, parseDocTargetShape } from "../lib/doc-shape.js";
|
|
3
|
+
import { CliError } from "../lib/errors.js";
|
|
4
|
+
import { findProjectRoot, normalizeRepoRelativePath, resolveInsideRoot } from "../lib/fs.js";
|
|
5
|
+
import { writeMeta } from "../lib/state.js";
|
|
6
|
+
import { loadWorkspace, validateWorkspace } from "../lib/workspace.js";
|
|
7
|
+
// 无损登记已存在的合法 .mdx 到 meta.json:只新增 validatedRevision: null 条目,
|
|
8
|
+
// 不重写正文;已登记路径幂等 no-op。有效 revision 仍需后续 fingerprint 验证后写入。
|
|
9
|
+
export function runAdopt(options) {
|
|
10
|
+
const rootDir = findProjectRoot(options.cwd);
|
|
11
|
+
const workspace = loadWorkspace(rootDir);
|
|
12
|
+
if (!workspace.meta) {
|
|
13
|
+
throw new CliError("缺少 llmdoc/meta.json,先运行 llmdoc init-state。");
|
|
14
|
+
}
|
|
15
|
+
const llmdocPaths = options.paths.map((input) => {
|
|
16
|
+
const repoRelativePath = normalizeRepoRelativePath(input.startsWith("llmdoc/") ? input : `llmdoc/${input}`);
|
|
17
|
+
if (!repoRelativePath.startsWith("llmdoc/") || !repoRelativePath.endsWith(".mdx")) {
|
|
18
|
+
throw new CliError(`adopt 只能登记 llmdoc/ 下的 .mdx 文档: ${input}`);
|
|
19
|
+
}
|
|
20
|
+
const shape = parseDocTargetShape(repoRelativePath);
|
|
21
|
+
assertDocKindMatchesShape(shape);
|
|
22
|
+
const absolutePath = resolveInsideRoot(rootDir, repoRelativePath, { allowMissing: true });
|
|
23
|
+
if (!fs.existsSync(absolutePath)) {
|
|
24
|
+
throw new CliError(`目标不存在,adopt 只登记已有文档(新建请用 llmdoc new): ${repoRelativePath}`);
|
|
25
|
+
}
|
|
26
|
+
if (!workspace.documentsByLlmdocPath.has(shape.llmdocPath)) {
|
|
27
|
+
throw new CliError(`文档未被 workspace 识别(front matter 可能不合法): ${repoRelativePath}`);
|
|
28
|
+
}
|
|
29
|
+
return shape.llmdocPath;
|
|
30
|
+
});
|
|
31
|
+
// 目标文档自身必须通过结构校验;meta.entry.missing 正是 adopt 要修复的问题,予以豁免。
|
|
32
|
+
const targetRepoPaths = new Set(llmdocPaths.map((llmdocPath) => `llmdoc/${llmdocPath}`));
|
|
33
|
+
const blockingIssues = validateWorkspace(workspace).filter((issue) => issue.severity === "error" && issue.path !== undefined && targetRepoPaths.has(issue.path) && issue.code !== "meta.entry.missing");
|
|
34
|
+
if (blockingIssues.length > 0) {
|
|
35
|
+
throw new CliError(`目标文档未通过校验,拒绝登记:\n${blockingIssues.map((issue) => ` ${issue.code} (${issue.path}) ${issue.message}`).join("\n")}`);
|
|
36
|
+
}
|
|
37
|
+
const adopted = [];
|
|
38
|
+
const alreadyRegistered = [];
|
|
39
|
+
for (const llmdocPath of llmdocPaths) {
|
|
40
|
+
if (workspace.meta.documents[llmdocPath]) {
|
|
41
|
+
alreadyRegistered.push(llmdocPath);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
workspace.meta.documents[llmdocPath] = { validatedRevision: null };
|
|
45
|
+
adopted.push(llmdocPath);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (adopted.length > 0) {
|
|
49
|
+
writeMeta(workspace.metaPath, workspace.meta);
|
|
50
|
+
}
|
|
51
|
+
if (options.json) {
|
|
52
|
+
return {
|
|
53
|
+
adopted: adopted.slice().sort(),
|
|
54
|
+
alreadyRegistered: alreadyRegistered.slice().sort()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const lines = [`adopted: ${adopted.length} document(s)${adopted.length > 0 ? ` — ${adopted.join(", ")}` : ""}`];
|
|
58
|
+
if (alreadyRegistered.length > 0) {
|
|
59
|
+
lines.push(`already registered (no-op): ${alreadyRegistered.join(", ")}`);
|
|
60
|
+
}
|
|
61
|
+
if (adopted.length > 0) {
|
|
62
|
+
lines.push("validatedRevision 登记为 null;经 validate 后用 fingerprint/commit 写入有效 revision。");
|
|
63
|
+
}
|
|
64
|
+
return lines.join("\n");
|
|
65
|
+
}
|
package/dist/commands/commit.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import { CliError } from "../lib/errors.js";
|
|
3
3
|
import { findProjectRoot } from "../lib/fs.js";
|
|
4
|
-
import { updateMetaRevisions, writeMeta } from "../lib/state.js";
|
|
4
|
+
import { assertRevisionAdvancePreconditions, updateMetaRevisions, writeMeta } from "../lib/state.js";
|
|
5
5
|
import { loadWorkspace, validateWorkspace } from "../lib/workspace.js";
|
|
6
6
|
// llmdoc 写入的一体化收尾:validate 门控 → commit llmdoc 写集 → fingerprint → meta 单独小 commit。
|
|
7
7
|
// 消灭手工三步曲及其 amend 追尾陷阱;--no-verify 透传给 git(husky 等重钩子仓库)。
|
|
8
8
|
export function runCommit(options) {
|
|
9
9
|
const rootDir = findProjectRoot(options.cwd);
|
|
10
|
-
const
|
|
10
|
+
const preflightWorkspace = loadWorkspace(rootDir);
|
|
11
|
+
const issues = validateWorkspace(preflightWorkspace);
|
|
11
12
|
const errors = issues.filter((issue) => issue.severity === "error");
|
|
12
13
|
if (errors.length > 0) {
|
|
13
14
|
throw new CliError(`validate 未通过,拒绝提交:\n${errors.map((issue) => ` ${issue.code} (${issue.path})`).join("\n")}`);
|
|
@@ -23,6 +24,21 @@ export function runCommit(options) {
|
|
|
23
24
|
.map((filePath) => (filePath.includes(" -> ") ? filePath.split(" -> ").pop() : filePath))
|
|
24
25
|
.filter((filePath) => filePath.startsWith("llmdoc/") && filePath.endsWith(".mdx"));
|
|
25
26
|
const verifyFlags = options.noVerify ? ["--no-verify"] : [];
|
|
27
|
+
// 在创建任何 commit 前预检 fingerprint 的全部前置条件(git 可推进、关联源码无 dirty):
|
|
28
|
+
// 预检失败时 fail-closed,工作树保持调用前状态,避免留下 docs 已提交但 meta 未刷新的半完成状态。
|
|
29
|
+
const preflightDocPaths = changedDocPaths
|
|
30
|
+
.map((filePath) => filePath.slice("llmdoc/".length))
|
|
31
|
+
.filter((llmdocPath) => preflightWorkspace.documentsByLlmdocPath.has(llmdocPath));
|
|
32
|
+
try {
|
|
33
|
+
assertRevisionAdvancePreconditions({
|
|
34
|
+
workspace: preflightWorkspace,
|
|
35
|
+
llmdocPaths: preflightDocPaths,
|
|
36
|
+
updateAll: options.all ?? false
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw new CliError(`fingerprint 预检未通过,未创建任何 commit: ${error.message}`, 70);
|
|
41
|
+
}
|
|
26
42
|
runGit(rootDir, ["add", "--", "llmdoc"]);
|
|
27
43
|
runGit(rootDir, ["commit", ...verifyFlags, "-m", options.message ?? "docs(llmdoc): update knowledge", "--", "llmdoc"]);
|
|
28
44
|
const docsCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
package/dist/commands/hook.js
CHANGED
|
@@ -29,26 +29,44 @@ export function runHook(options) {
|
|
|
29
29
|
}
|
|
30
30
|
function runSessionStart(cwd, stdin) {
|
|
31
31
|
const source = inferSource(stdin);
|
|
32
|
+
const lifecycle = source === "compact" ? "compact re-entry" : "cold start";
|
|
32
33
|
try {
|
|
33
34
|
const workspace = loadWorkspace(cwd);
|
|
34
35
|
const delta = analyzeDelta(workspace);
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
36
|
+
const baseline = workspace.meta?.baseline.revision ? workspace.meta.baseline.revision.slice(0, 7) : "缺失";
|
|
37
|
+
const freshness = delta.git.baselineBehindHead === null ? "落后未知" : delta.git.baselineBehindHead === 0 ? "与 HEAD 同步" : `落后 HEAD ${delta.git.baselineBehindHead} commit`;
|
|
38
|
+
const parts = [`llmdoc ${lifecycle}`, `baseline ${baseline}(${freshness})`];
|
|
39
|
+
if (delta.impacts.length === 0 && delta.needsReview.length === 0) {
|
|
40
|
+
parts.push("文档无待处理影响");
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
parts.push(`受影响 ${delta.impacts.length} 篇, 待复核 ${delta.needsReview.length} 篇 → 建议先看 npx @tokenroll/llmdoc delta`);
|
|
44
|
+
}
|
|
45
|
+
return parts.join("; ");
|
|
39
46
|
}
|
|
40
47
|
catch {
|
|
41
|
-
return
|
|
48
|
+
return `llmdoc ${lifecycle}; 状态读取失败,检索不受影响`;
|
|
42
49
|
}
|
|
43
50
|
}
|
|
44
51
|
function runStop(cwd) {
|
|
45
52
|
try {
|
|
46
53
|
const workspace = loadWorkspace(cwd);
|
|
47
54
|
const delta = analyzeDelta(workspace);
|
|
48
|
-
const
|
|
55
|
+
const unmappedCount = delta.unmappedCommittedPaths.length + delta.unmappedDirtyPaths.length;
|
|
56
|
+
const shouldUpdate = delta.impacts.length > 0 || unmappedCount > 0;
|
|
57
|
+
if (!shouldUpdate) {
|
|
58
|
+
return { continue: true };
|
|
59
|
+
}
|
|
60
|
+
const summary = [
|
|
61
|
+
delta.impacts.length > 0 ? `${delta.impacts.length} 篇文档受代码变更影响` : null,
|
|
62
|
+
unmappedCount > 0 ? `${unmappedCount} 个代码路径未映射到任何文档` : null
|
|
63
|
+
]
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join(", ");
|
|
66
|
+
const reasons = delta.reasons.length > 0 ? `信号: ${delta.reasons.join("; ")}` : "";
|
|
49
67
|
return {
|
|
50
68
|
continue: true,
|
|
51
|
-
|
|
69
|
+
systemMessage: `llmdoc: ${summary},建议运行 /llmdoc:update(先用 npx @tokenroll/llmdoc delta 查看影响面)。${reasons}`
|
|
52
70
|
};
|
|
53
71
|
}
|
|
54
72
|
catch (error) {
|
|
@@ -61,7 +79,7 @@ function runStop(cwd) {
|
|
|
61
79
|
function runCompact() {
|
|
62
80
|
return {
|
|
63
81
|
continue: true,
|
|
64
|
-
systemMessage: "
|
|
82
|
+
systemMessage: "即将 compact:请在 summary 中写入 LLMDOC_STATE(active goal、已读 llmdoc 文档、关键结论与不变量、用户决策、next step、open risks)。恢复后若该状态仍充分,直接继续,不要重放 tree/show。"
|
|
65
83
|
};
|
|
66
84
|
}
|
|
67
85
|
function inferSource(stdin) {
|
package/dist/commands/status.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readCommitsWithChangedPathsSince } from "../lib/git.js";
|
|
2
|
+
import { computeGrowthState, analyzeDelta, isImplementationSurfacePath, loadIgnorePatterns } from "../lib/state.js";
|
|
2
3
|
import { loadWorkspace } from "../lib/workspace.js";
|
|
3
4
|
export function runStatus(options) {
|
|
4
5
|
const workspace = loadWorkspace(options.cwd);
|
|
5
6
|
const delta = analyzeDelta(workspace);
|
|
6
7
|
const growth = computeGrowthState(workspace);
|
|
8
|
+
const relevantCommitsBehindHead = countRelevantCommitsBehindHead(workspace.rootDir, workspace.meta?.baseline.revision ?? null, delta);
|
|
7
9
|
if (options.json) {
|
|
8
10
|
return {
|
|
9
11
|
baseline: workspace.meta?.baseline.revision ?? null,
|
|
10
12
|
head: delta.git.headRevision,
|
|
11
13
|
commitsBehindHead: delta.git.baselineBehindHead,
|
|
14
|
+
relevantCommitsBehindHead,
|
|
12
15
|
degradedReason: delta.git.degradedReason,
|
|
13
16
|
documents: {
|
|
14
17
|
total: workspace.documents.length,
|
|
@@ -24,7 +27,7 @@ export function runStatus(options) {
|
|
|
24
27
|
};
|
|
25
28
|
}
|
|
26
29
|
const baseline = workspace.meta?.baseline.revision ?? "missing";
|
|
27
|
-
const behind = delta.git.baselineBehindHead
|
|
30
|
+
const behind = formatBehindLabel(delta.git.baselineBehindHead, relevantCommitsBehindHead);
|
|
28
31
|
const unmapped = [...delta.unmappedCommittedPaths, ...delta.unmappedDirtyPaths];
|
|
29
32
|
const growthLabel = growth.baselineDocumentCount === null
|
|
30
33
|
? `growth: ${growth.currentDocumentCount} docs, ~${growth.currentTotalEstimatedTokens} tokens (no convergence baseline)`
|
|
@@ -44,3 +47,31 @@ export function runStatus(options) {
|
|
|
44
47
|
}
|
|
45
48
|
return lines.join("\n");
|
|
46
49
|
}
|
|
50
|
+
// "有效源码落后"计数:baseline..HEAD 中至少触碰一个 implementation surface 路径的提交数。
|
|
51
|
+
// 只改 llmdoc/** 的提交(尤其 commit 收尾的 meta follow-up)不代表知识过期,不应计入。
|
|
52
|
+
function countRelevantCommitsBehindHead(rootDir, baselineRevision, delta) {
|
|
53
|
+
if (!baselineRevision || !delta.git.headRevision || delta.git.baselineBehindHead === null) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (delta.git.baselineBehindHead === 0) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
const commits = readCommitsWithChangedPathsSince(rootDir, baselineRevision, delta.git.headRevision);
|
|
60
|
+
if (commits === null) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const ignorePatterns = loadIgnorePatterns(rootDir);
|
|
64
|
+
return commits.filter((commit) => commit.paths.some((filePath) => isImplementationSurfacePath(filePath, ignorePatterns))).length;
|
|
65
|
+
}
|
|
66
|
+
function formatBehindLabel(commitsBehindHead, relevantCommitsBehindHead) {
|
|
67
|
+
if (commitsBehindHead === null) {
|
|
68
|
+
return "unknown";
|
|
69
|
+
}
|
|
70
|
+
if (commitsBehindHead > 0 && relevantCommitsBehindHead === 0) {
|
|
71
|
+
return `${commitsBehindHead} commits behind HEAD, metadata-only; knowledge clean`;
|
|
72
|
+
}
|
|
73
|
+
if (commitsBehindHead > 0 && relevantCommitsBehindHead !== null) {
|
|
74
|
+
return `${commitsBehindHead} commits behind HEAD, ${relevantCommitsBehindHead} relevant source commits`;
|
|
75
|
+
}
|
|
76
|
+
return `${commitsBehindHead} commits behind HEAD`;
|
|
77
|
+
}
|
package/dist/lib/git.js
CHANGED
|
@@ -83,6 +83,31 @@ export function readChangedPathsSince(rootDir, revision, headRevision) {
|
|
|
83
83
|
}
|
|
84
84
|
return readPathList(rootDir, ["diff", "--name-only", "--no-renames", `${revision}..${headRevision}`]);
|
|
85
85
|
}
|
|
86
|
+
// 逐 commit 列出 baseline..HEAD 中每个提交触碰的路径,供"有效源码落后"计数:
|
|
87
|
+
// \x01 前缀作 commit 分隔哨兵,避免 40 位 hex 路径名的歧义;merge commit 默认不列文件,视为无自身变更。
|
|
88
|
+
export function readCommitsWithChangedPathsSince(rootDir, revision, headRevision) {
|
|
89
|
+
if (!gitCommitExists(rootDir, revision)) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const output = runGitSafe(rootDir, ["log", "--format=%x01%H", "--name-only", "--no-renames", `${revision}..${headRevision}`]);
|
|
93
|
+
if (output === null) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const commits = [];
|
|
97
|
+
for (const line of output.split(/\r?\n/)) {
|
|
98
|
+
const trimmed = line.trim();
|
|
99
|
+
if (!trimmed) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (trimmed.startsWith("\u0001")) {
|
|
103
|
+
commits.push({ revision: trimmed.slice(1), paths: [] });
|
|
104
|
+
}
|
|
105
|
+
else if (commits.length > 0) {
|
|
106
|
+
commits[commits.length - 1].paths.push(trimmed);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return commits;
|
|
110
|
+
}
|
|
86
111
|
export function canAdvanceRevisions(gitState) {
|
|
87
112
|
if (!gitState.available || !gitState.headRevision) {
|
|
88
113
|
return { ok: false, reason: gitState.degradedReason ?? "git 状态不可用。" };
|
package/dist/lib/state.js
CHANGED
|
@@ -148,7 +148,9 @@ export function computeGrowthState(workspace) {
|
|
|
148
148
|
exceedsGate
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
|
-
|
|
151
|
+
// fingerprint 推进的全部前置条件(git 可推进 + 关联源码无 dirty),
|
|
152
|
+
// 供 updateMetaRevisions 执行前使用,也供 commit 在创建任何 commit 前 preflight,失败时 fail-closed。
|
|
153
|
+
export function assertRevisionAdvancePreconditions(input) {
|
|
152
154
|
const { workspace, llmdocPaths, updateAll } = input;
|
|
153
155
|
if (!workspace.meta) {
|
|
154
156
|
throw new Error("缺少 meta.json");
|
|
@@ -172,6 +174,11 @@ export function updateMetaRevisions(input) {
|
|
|
172
174
|
? `全量 fingerprint 时以下文档关联代码存在 dirty 变更,不能伪造 revision: ${blocked.join(", ")}`
|
|
173
175
|
: `以下文档关联代码存在 dirty 变更,不能伪造 revision: ${blocked.join(", ")}`);
|
|
174
176
|
}
|
|
177
|
+
return { git, targetPaths };
|
|
178
|
+
}
|
|
179
|
+
export function updateMetaRevisions(input) {
|
|
180
|
+
const { workspace, updateAll } = input;
|
|
181
|
+
const { git, targetPaths } = assertRevisionAdvancePreconditions(input);
|
|
175
182
|
const nextMeta = structuredClone(workspace.meta);
|
|
176
183
|
// 文档已被删除的孤儿 ledger 条目在此顺带清理:
|
|
177
184
|
// recorder 不允许手编辑 meta.json,删除文档后的 ledger 收敛只能由 CLI 完成。
|
|
@@ -249,7 +256,7 @@ export function loadIgnorePatterns(rootDir) {
|
|
|
249
256
|
ignorePatternsCache.set(rootDir, patterns);
|
|
250
257
|
return patterns;
|
|
251
258
|
}
|
|
252
|
-
function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
|
|
259
|
+
export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
|
|
253
260
|
if (repoRelativePath.startsWith("llmdoc/") || repoRelativePath.startsWith(".llmdoc-tmp/")) {
|
|
254
261
|
return false;
|
|
255
262
|
}
|
package/dist/lib/workspace.js
CHANGED
|
@@ -339,7 +339,7 @@ export function validateWorkspace(workspace) {
|
|
|
339
339
|
severity: "error",
|
|
340
340
|
code: "meta.entry.missing",
|
|
341
341
|
path: `llmdoc/${docPath}`,
|
|
342
|
-
message: "meta.json 缺少对应 documents entry
|
|
342
|
+
message: "meta.json 缺少对应 documents entry。可用 `llmdoc adopt <path>` 无损登记。"
|
|
343
343
|
});
|
|
344
344
|
}
|
|
345
345
|
}
|
package/package.json
CHANGED
|
@@ -478,6 +478,7 @@
|
|
|
478
478
|
"baseline",
|
|
479
479
|
"head",
|
|
480
480
|
"commitsBehindHead",
|
|
481
|
+
"relevantCommitsBehindHead",
|
|
481
482
|
"degradedReason",
|
|
482
483
|
"documents",
|
|
483
484
|
"unmapped",
|
|
@@ -503,6 +504,13 @@
|
|
|
503
504
|
],
|
|
504
505
|
"minimum": 0
|
|
505
506
|
},
|
|
507
|
+
"relevantCommitsBehindHead": {
|
|
508
|
+
"type": [
|
|
509
|
+
"integer",
|
|
510
|
+
"null"
|
|
511
|
+
],
|
|
512
|
+
"minimum": 0
|
|
513
|
+
},
|
|
506
514
|
"degradedReason": {
|
|
507
515
|
"type": [
|
|
508
516
|
"string",
|
|
@@ -852,6 +860,30 @@
|
|
|
852
860
|
}
|
|
853
861
|
}
|
|
854
862
|
},
|
|
863
|
+
"adopt": {
|
|
864
|
+
"type": "object",
|
|
865
|
+
"additionalProperties": false,
|
|
866
|
+
"required": [
|
|
867
|
+
"adopted",
|
|
868
|
+
"alreadyRegistered"
|
|
869
|
+
],
|
|
870
|
+
"properties": {
|
|
871
|
+
"adopted": {
|
|
872
|
+
"type": "array",
|
|
873
|
+
"items": {
|
|
874
|
+
"type": "string",
|
|
875
|
+
"minLength": 1
|
|
876
|
+
}
|
|
877
|
+
},
|
|
878
|
+
"alreadyRegistered": {
|
|
879
|
+
"type": "array",
|
|
880
|
+
"items": {
|
|
881
|
+
"type": "string",
|
|
882
|
+
"minLength": 1
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
},
|
|
855
887
|
"mv": {
|
|
856
888
|
"type": "object",
|
|
857
889
|
"additionalProperties": false,
|