@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.
- package/assets/viewer-app.js +11 -11
- package/assets/viewer-detail.js +4 -4
- package/assets/viewer-graph.js +3 -3
- package/assets/viewer.html +19 -19
- package/dist/cli.js +147 -71
- package/dist/commands/adopt.js +10 -6
- package/dist/commands/commit.js +232 -80
- package/dist/commands/context.js +85 -45
- package/dist/commands/dream.js +608 -0
- package/dist/commands/eval.js +379 -0
- package/dist/commands/fingerprint.js +9 -3
- package/dist/commands/hook.js +135 -28
- package/dist/commands/index.js +52 -7
- package/dist/commands/init-state.js +5 -5
- package/dist/commands/mv.js +33 -12
- package/dist/commands/new.js +12 -5
- package/dist/commands/prune.js +14 -3
- package/dist/commands/search.js +67 -4
- package/dist/commands/serve.js +1 -1
- package/dist/commands/show.js +40 -6
- package/dist/commands/status.js +46 -18
- package/dist/commands/tree.js +94 -6
- package/dist/commands/upgrade.js +15 -15
- package/dist/lib/config.js +502 -0
- package/dist/lib/constants.js +23 -0
- package/dist/lib/doc-shape.js +5 -5
- package/dist/lib/frontmatter.js +47 -0
- package/dist/lib/fs.js +8 -8
- package/dist/lib/git.js +147 -42
- package/dist/lib/markdown.js +8 -7
- package/dist/lib/output-schema.js +3 -3
- package/dist/lib/package-root.js +1 -1
- package/dist/lib/pagination.js +2 -2
- package/dist/lib/rewrite.js +9 -3
- package/dist/lib/routing.js +152 -0
- package/dist/lib/schema.js +16 -0
- package/dist/lib/search.js +35 -16
- package/dist/lib/source-id.js +49 -0
- package/dist/lib/sources.js +760 -0
- package/dist/lib/state.js +74 -25
- package/dist/lib/telemetry.js +726 -0
- package/dist/lib/viewer-http.js +2 -2
- package/dist/lib/workspace.js +187 -43
- package/package.json +1 -1
- package/schemas/config.schema.json +116 -0
- package/schemas/meta.schema.json +5 -1
- package/schemas/output.schema.json +442 -0
- package/schemas/probes.schema.json +90 -0
package/dist/commands/commit.js
CHANGED
|
@@ -1,38 +1,70 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import { spawnSync } from "node:child_process";
|
|
2
3
|
import { CliError } from "../lib/errors.js";
|
|
3
4
|
import { findProjectRoot } from "../lib/fs.js";
|
|
4
|
-
import {
|
|
5
|
+
import { parseGitPorcelainZ, readCommitTimestampMs, resolveCommitObjectId } from "../lib/git.js";
|
|
6
|
+
import { isSourceRef } from "../lib/source-id.js";
|
|
7
|
+
import { assertRevisionAdvancePreconditions, readWorkspaceGitState, updateMetaRevisions, writeFileAtomically, writeMeta } from "../lib/state.js";
|
|
5
8
|
import { loadWorkspace, validateWorkspace } from "../lib/workspace.js";
|
|
9
|
+
const VERIFIED_AT_IDEMPOTENCY_MS = 60 * 60 * 1000;
|
|
6
10
|
// llmdoc 写入的一体化收尾:validate 门控 → commit llmdoc 写集 → fingerprint → meta 单独小 commit。
|
|
7
11
|
// 消灭手工三步曲及其 amend 追尾陷阱;--no-verify 透传给 git(husky 等重钩子仓库)。
|
|
8
12
|
export function runCommit(options) {
|
|
9
13
|
const rootDir = findProjectRoot(options.cwd);
|
|
10
14
|
const preflightWorkspace = loadWorkspace(rootDir);
|
|
15
|
+
for (const value of options.verified ?? []) {
|
|
16
|
+
if (isSourceRef(value)) {
|
|
17
|
+
throw new CliError(`External source documents (@...) are read-only and cannot be passed to commit --verified: ${value}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
11
20
|
const verifiedPaths = uniquePaths((options.verified ?? []).map((value) => (value.startsWith("llmdoc/") ? value.slice("llmdoc/".length) : value)));
|
|
12
21
|
if (options.all && verifiedPaths.length > 0) {
|
|
13
|
-
throw new CliError("commit
|
|
22
|
+
throw new CliError("commit cannot use --all and --verified together.");
|
|
23
|
+
}
|
|
24
|
+
if (options.convergenceSource && !options.refreshConvergence) {
|
|
25
|
+
throw new CliError("--convergence-source requires --refresh-convergence.");
|
|
26
|
+
}
|
|
27
|
+
if (options.convergenceSource &&
|
|
28
|
+
options.convergenceSource !== "prune" &&
|
|
29
|
+
options.convergenceSource !== "dream") {
|
|
30
|
+
throw new CliError(`Invalid convergence source: ${String(options.convergenceSource)}. Expected prune or dream.`);
|
|
14
31
|
}
|
|
32
|
+
const refreshConvergenceSource = options.refreshConvergence
|
|
33
|
+
? (options.convergenceSource ?? "prune")
|
|
34
|
+
: undefined;
|
|
15
35
|
for (const docPath of verifiedPaths) {
|
|
16
36
|
if (!preflightWorkspace.documentsByLlmdocPath.has(docPath)) {
|
|
17
|
-
throw new CliError(
|
|
37
|
+
throw new CliError(`Document does not exist: ${docPath}`);
|
|
18
38
|
}
|
|
19
39
|
}
|
|
20
|
-
const
|
|
40
|
+
const preflightGit = readWorkspaceGitState(preflightWorkspace);
|
|
41
|
+
if (preflightGit.available && preflightGit.detached) {
|
|
42
|
+
throw new CliError("Cannot run commit on a detached HEAD. Create or switch to a branch first (e.g. `git checkout -b <branch>`).", 70);
|
|
43
|
+
}
|
|
44
|
+
const porcelainRaw = runGitRaw(rootDir, ["status", "--porcelain=v1", "-z", "--no-renames", "--", "llmdoc"]);
|
|
45
|
+
const porcelainEntries = parseGitPorcelainZ(porcelainRaw);
|
|
46
|
+
const hasLlmdocChanges = porcelainEntries.length > 0;
|
|
47
|
+
const changedDocPaths = uniquePaths(porcelainEntries
|
|
48
|
+
.map((entry) => entry.path)
|
|
49
|
+
.filter((filePath) => filePath.startsWith("llmdoc/") && filePath.endsWith(".mdx")));
|
|
50
|
+
const gitDeletedDocPaths = new Set(porcelainEntries
|
|
51
|
+
.filter((entry) => (entry.indexStatus === "D" || entry.worktreeStatus === "D") &&
|
|
52
|
+
entry.path.startsWith("llmdoc/") &&
|
|
53
|
+
entry.path.endsWith(".mdx"))
|
|
54
|
+
.map((entry) => entry.path.slice("llmdoc/".length))
|
|
55
|
+
.filter((llmdocPath) => !preflightWorkspace.documentsByLlmdocPath.has(llmdocPath) &&
|
|
56
|
+
!preflightWorkspace.unparsedDocPaths?.has(llmdocPath)));
|
|
57
|
+
// Build candidate validation workspace with only Git-confirmed deleted docs stripped from meta.documents
|
|
58
|
+
// so prune deletions pass preflight while dangling links, requires, preload refs, or unparsed docs still fail.
|
|
59
|
+
const validationWorkspace = buildCandidateValidationWorkspace(preflightWorkspace, gitDeletedDocPaths);
|
|
60
|
+
const issues = validateWorkspace(validationWorkspace);
|
|
21
61
|
const errors = issues.filter((issue) => issue.severity === "error");
|
|
22
62
|
if (errors.length > 0) {
|
|
23
|
-
throw new CliError(`
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return options.json ? { status: "no_change", commits: [], updated: [] } : "no_change: llmdoc/ 无待提交变更";
|
|
29
|
-
}
|
|
30
|
-
const changedDocPaths = porcelain
|
|
31
|
-
.split(/\r?\n/)
|
|
32
|
-
.filter(Boolean)
|
|
33
|
-
.map((line) => line.slice(3).trim())
|
|
34
|
-
.map((filePath) => (filePath.includes(" -> ") ? filePath.split(" -> ").pop() : filePath))
|
|
35
|
-
.filter((filePath) => filePath.startsWith("llmdoc/") && filePath.endsWith(".mdx"));
|
|
63
|
+
throw new CliError(`Validation failed; refusing to commit:\n${errors.map((issue) => ` ${issue.code} (${issue.path})`).join("\n")}`);
|
|
64
|
+
}
|
|
65
|
+
if (!hasLlmdocChanges && !options.all && verifiedPaths.length === 0 && !refreshConvergenceSource) {
|
|
66
|
+
return options.json ? { status: "no_change", commits: [], updated: [] } : "no_change: llmdoc/ has no changes to commit";
|
|
67
|
+
}
|
|
36
68
|
const existingChangedDocPaths = changedDocPaths
|
|
37
69
|
.map((filePath) => filePath.slice("llmdoc/".length))
|
|
38
70
|
.filter((llmdocPath) => preflightWorkspace.documentsByLlmdocPath.has(llmdocPath));
|
|
@@ -44,11 +76,12 @@ export function runCommit(options) {
|
|
|
44
76
|
assertRevisionAdvancePreconditions({
|
|
45
77
|
workspace: preflightWorkspace,
|
|
46
78
|
llmdocPaths: targetPaths,
|
|
47
|
-
updateAll: options.all ?? false
|
|
79
|
+
updateAll: options.all ?? false,
|
|
80
|
+
refreshConvergence: Boolean(refreshConvergenceSource)
|
|
48
81
|
});
|
|
49
82
|
}
|
|
50
83
|
catch (error) {
|
|
51
|
-
throw new CliError(`
|
|
84
|
+
throw new CliError(`Fingerprint preflight failed; no commit was created: ${error.message}`, 70);
|
|
52
85
|
}
|
|
53
86
|
if (changedDocPaths.length === 0) {
|
|
54
87
|
return commitMetaOnly({
|
|
@@ -56,79 +89,190 @@ export function runCommit(options) {
|
|
|
56
89
|
workspace: preflightWorkspace,
|
|
57
90
|
verifiedPaths,
|
|
58
91
|
updateAll: options.all ?? false,
|
|
92
|
+
refreshConvergenceSource,
|
|
59
93
|
hasLlmdocChanges,
|
|
60
94
|
verifyFlags,
|
|
61
95
|
json: options.json ?? false
|
|
62
96
|
});
|
|
63
97
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
const snapshot = captureRollbackSnapshot(rootDir, preflightWorkspace.metaPath);
|
|
99
|
+
let docsCommit = null;
|
|
100
|
+
try {
|
|
101
|
+
runGit(rootDir, ["add", "--", "llmdoc"]);
|
|
102
|
+
runGit(rootDir, ["commit", ...verifyFlags, "-m", options.message ?? "docs(llmdoc): update knowledge", "--", "llmdoc"]);
|
|
103
|
+
docsCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
104
|
+
// 重新加载:提交后的 workspace 是 fingerprint 的事实基础;已删除文档的 ledger 孤儿由 updateMetaRevisions 清理。
|
|
105
|
+
const workspace = loadWorkspace(rootDir);
|
|
106
|
+
const existingDocPaths = changedDocPaths
|
|
107
|
+
.map((filePath) => filePath.slice("llmdoc/".length))
|
|
108
|
+
.filter((llmdocPath) => workspace.documentsByLlmdocPath.has(llmdocPath));
|
|
109
|
+
const { meta, updatedPaths } = updateMetaRevisions({
|
|
110
|
+
workspace,
|
|
111
|
+
llmdocPaths: uniquePaths([...existingDocPaths, ...verifiedPaths]),
|
|
112
|
+
updateAll: options.all ?? false,
|
|
113
|
+
refreshConvergenceSource
|
|
114
|
+
});
|
|
115
|
+
writeMeta(workspace.metaPath, meta);
|
|
116
|
+
runGit(rootDir, ["add", "--", "llmdoc/meta.json"]);
|
|
117
|
+
runGit(rootDir, ["commit", ...verifyFlags, "-m", "chore(llmdoc): refresh fingerprints", "--", "llmdoc/meta.json"]);
|
|
118
|
+
const metaCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
119
|
+
if (options.json) {
|
|
120
|
+
return {
|
|
121
|
+
status: "success",
|
|
122
|
+
commits: [docsCommit, metaCommit],
|
|
123
|
+
updated: updatedPaths,
|
|
124
|
+
baselineAdvanced: options.all ?? false,
|
|
125
|
+
...(refreshConvergenceSource ? { convergenceRefreshed: true } : {})
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return [
|
|
129
|
+
`committed: ${docsCommit.slice(0, 7)} (docs) + ${metaCommit.slice(0, 7)} (meta)`,
|
|
130
|
+
`fingerprints: ${updatedPaths.length} document(s)${options.all ? ", baseline advanced" : ""}${refreshConvergenceSource ? `, convergence refreshed (${refreshConvergenceSource})` : ""}`
|
|
131
|
+
].join("\n");
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
rollbackCommitTransaction(rootDir, snapshot, docsCommit);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
93
137
|
}
|
|
94
138
|
function commitMetaOnly(input) {
|
|
95
|
-
const { rootDir, workspace, verifiedPaths, updateAll, hasLlmdocChanges, verifyFlags, json } = input;
|
|
139
|
+
const { rootDir, workspace, verifiedPaths, updateAll, refreshConvergenceSource, hasLlmdocChanges, verifyFlags, json } = input;
|
|
96
140
|
const headRevision = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
141
|
+
const nowMs = Date.now();
|
|
97
142
|
const requestedPaths = updateAll ? workspace.documents.map((document) => document.llmdocPath) : verifiedPaths;
|
|
98
|
-
const
|
|
143
|
+
const convergenceRefreshNeeded = Boolean(refreshConvergenceSource) &&
|
|
144
|
+
!convergenceIsCurrent(rootDir, workspace, refreshConvergenceSource, nowMs);
|
|
145
|
+
const revisionRefreshNeeded = updateAll
|
|
99
146
|
? !revisionIsCurrent(rootDir, workspace.meta.baseline.revision, headRevision) ||
|
|
100
|
-
requestedPaths.some((docPath) => !
|
|
101
|
-
: requestedPaths.some((docPath) => !
|
|
147
|
+
requestedPaths.some((docPath) => !documentVerificationIsCurrent(rootDir, workspace.meta.documents[docPath], headRevision, nowMs))
|
|
148
|
+
: requestedPaths.some((docPath) => !documentVerificationIsCurrent(rootDir, workspace.meta.documents[docPath], headRevision, nowMs));
|
|
149
|
+
const refreshNeeded = convergenceRefreshNeeded || revisionRefreshNeeded;
|
|
102
150
|
if (!hasLlmdocChanges && !refreshNeeded) {
|
|
103
151
|
return json
|
|
104
152
|
? { status: "no_change", commits: [], updated: [] }
|
|
105
153
|
: "no_change: requested fingerprints are already current";
|
|
106
154
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
155
|
+
const snapshot = captureRollbackSnapshot(rootDir, workspace.metaPath);
|
|
156
|
+
try {
|
|
157
|
+
let updatedPaths = [];
|
|
158
|
+
if (refreshNeeded) {
|
|
159
|
+
const result = updateMetaRevisions({
|
|
160
|
+
workspace,
|
|
161
|
+
llmdocPaths: verifiedPaths,
|
|
162
|
+
updateAll,
|
|
163
|
+
refreshConvergenceSource
|
|
164
|
+
});
|
|
165
|
+
writeMeta(workspace.metaPath, result.meta);
|
|
166
|
+
updatedPaths = result.updatedPaths;
|
|
167
|
+
}
|
|
168
|
+
runGit(rootDir, ["add", "--", "llmdoc/meta.json"]);
|
|
169
|
+
runGit(rootDir, ["commit", ...verifyFlags, "-m", "chore(llmdoc): refresh fingerprints", "--", "llmdoc/meta.json"]);
|
|
170
|
+
const metaCommit = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
171
|
+
if (json) {
|
|
172
|
+
return {
|
|
173
|
+
status: "success",
|
|
174
|
+
commits: [metaCommit],
|
|
175
|
+
updated: updatedPaths,
|
|
176
|
+
baselineAdvanced: updateAll,
|
|
177
|
+
...(refreshConvergenceSource ? { convergenceRefreshed: true } : {})
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return [
|
|
181
|
+
`committed: ${metaCommit.slice(0, 7)} (meta)`,
|
|
182
|
+
`fingerprints: ${updatedPaths.length} document(s)${updateAll ? ", baseline advanced" : ""}${refreshConvergenceSource ? `, convergence refreshed (${refreshConvergenceSource})` : ""}`
|
|
183
|
+
].join("\n");
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
rollbackCommitTransaction(rootDir, snapshot, null);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function buildCandidateValidationWorkspace(workspace, gitDeletedDocPaths) {
|
|
191
|
+
if (!workspace.meta || gitDeletedDocPaths.size === 0) {
|
|
192
|
+
return workspace;
|
|
193
|
+
}
|
|
194
|
+
const candidateMeta = structuredClone(workspace.meta);
|
|
195
|
+
for (const deletedPath of gitDeletedDocPaths) {
|
|
196
|
+
delete candidateMeta.documents[deletedPath];
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
...workspace,
|
|
200
|
+
meta: candidateMeta
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function captureRollbackSnapshot(rootDir, metaPath) {
|
|
204
|
+
const origHead = runGit(rootDir, ["rev-parse", "HEAD"]).trim();
|
|
205
|
+
const origBranchRef = runGit(rootDir, ["symbolic-ref", "HEAD"]).trim();
|
|
206
|
+
const origIndexTree = runGit(rootDir, ["write-tree"]).trim();
|
|
207
|
+
const origMetaBytes = fs.existsSync(metaPath) ? fs.readFileSync(metaPath) : null;
|
|
208
|
+
return {
|
|
209
|
+
origHead,
|
|
210
|
+
origBranchRef,
|
|
211
|
+
origIndexTree,
|
|
212
|
+
metaPath,
|
|
213
|
+
origMetaBytes
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function rollbackCommitTransaction(rootDir, snapshot, docsCommit) {
|
|
217
|
+
try {
|
|
218
|
+
if (snapshot.origMetaBytes !== null) {
|
|
219
|
+
writeFileAtomically(snapshot.metaPath, snapshot.origMetaBytes);
|
|
220
|
+
}
|
|
221
|
+
else if (fs.existsSync(snapshot.metaPath)) {
|
|
222
|
+
fs.unlinkSync(snapshot.metaPath);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
// Best-effort restore of meta.json.
|
|
227
|
+
}
|
|
228
|
+
if (docsCommit) {
|
|
229
|
+
spawnSync("git", ["update-ref", snapshot.origBranchRef, snapshot.origHead, docsCommit], {
|
|
230
|
+
cwd: rootDir,
|
|
231
|
+
encoding: "utf8"
|
|
113
232
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
233
|
+
}
|
|
234
|
+
spawnSync("git", ["read-tree", snapshot.origIndexTree], {
|
|
235
|
+
cwd: rootDir,
|
|
236
|
+
encoding: "utf8"
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function documentVerificationIsCurrent(rootDir, docMeta, headRevision, nowMs) {
|
|
240
|
+
if (!docMeta || !revisionIsCurrent(rootDir, docMeta.validatedRevision, headRevision)) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
if (docMeta.verifiedAt) {
|
|
244
|
+
const verifiedMs = Date.parse(docMeta.verifiedAt);
|
|
245
|
+
if (Number.isNaN(verifiedMs) || nowMs - verifiedMs >= VERIFIED_AT_IDEMPOTENCY_MS) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
// Legacy ledger without verifiedAt: allow zero-diff re-verification when the commit itself is old.
|
|
251
|
+
const commitMs = docMeta.validatedRevision ? readCommitTimestampMs(rootDir, docMeta.validatedRevision) : null;
|
|
252
|
+
if (commitMs === null || nowMs - commitMs >= VERIFIED_AT_IDEMPOTENCY_MS) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
function convergenceIsCurrent(rootDir, workspace, source, nowMs) {
|
|
258
|
+
const convergence = workspace.meta?.convergence;
|
|
259
|
+
if (!convergence || convergence.source !== source) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
const currentDocumentCount = workspace.documents.length;
|
|
263
|
+
const currentTotalEstimatedTokens = workspace.documents.reduce((sum, document) => sum + document.estimatedTokens, 0);
|
|
264
|
+
if (convergence.documentCount !== currentDocumentCount ||
|
|
265
|
+
convergence.totalEstimatedTokens !== currentTotalEstimatedTokens) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
const capturedMs = Date.parse(convergence.capturedAt);
|
|
269
|
+
if (Number.isNaN(capturedMs) || nowMs - capturedMs >= VERIFIED_AT_IDEMPOTENCY_MS) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
const headFiles = runGitRaw(rootDir, ["show", "--pretty=", "--name-only", "-z", "HEAD"])
|
|
273
|
+
.split("\0")
|
|
274
|
+
.filter(Boolean);
|
|
275
|
+
return headFiles.length > 0 && headFiles.every((filePath) => filePath === "llmdoc/meta.json");
|
|
132
276
|
}
|
|
133
277
|
// commit 自身会在 fingerprint 锚点之后创建一个只修改 meta.json 的 follow-up commit。
|
|
134
278
|
// 这种差异不代表知识又过期了,否则重复 commit 会无限追逐自己创建的 meta commit。
|
|
@@ -136,11 +280,16 @@ function revisionIsCurrent(rootDir, revision, headRevision) {
|
|
|
136
280
|
if (!revision) {
|
|
137
281
|
return false;
|
|
138
282
|
}
|
|
139
|
-
|
|
283
|
+
const revOid = resolveCommitObjectId(rootDir, revision);
|
|
284
|
+
const headOid = resolveCommitObjectId(rootDir, headRevision);
|
|
285
|
+
if (!revOid || !headOid) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (revOid === headOid) {
|
|
140
289
|
return true;
|
|
141
290
|
}
|
|
142
|
-
const changedPaths =
|
|
143
|
-
.split(
|
|
291
|
+
const changedPaths = runGitRaw(rootDir, ["diff", "-z", "--name-only", `${revOid}..${headOid}`, "--"])
|
|
292
|
+
.split("\0")
|
|
144
293
|
.filter(Boolean);
|
|
145
294
|
return changedPaths.every((filePath) => filePath === "llmdoc/meta.json");
|
|
146
295
|
}
|
|
@@ -148,9 +297,12 @@ function uniquePaths(paths) {
|
|
|
148
297
|
return [...new Set(paths)].sort();
|
|
149
298
|
}
|
|
150
299
|
function runGit(rootDir, args) {
|
|
300
|
+
return runGitRaw(rootDir, args).trim();
|
|
301
|
+
}
|
|
302
|
+
function runGitRaw(rootDir, args) {
|
|
151
303
|
const result = spawnSync("git", ["-c", "core.quotePath=false", ...args], { cwd: rootDir, encoding: "utf8" });
|
|
152
304
|
if (result.status !== 0) {
|
|
153
|
-
throw new CliError((result.stderr || result.stdout || `git ${args[0]}
|
|
305
|
+
throw new CliError((result.stderr || result.stdout || `git ${args[0]} failed`).trim());
|
|
154
306
|
}
|
|
155
307
|
return result.stdout;
|
|
156
308
|
}
|
package/dist/commands/context.js
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
import { loadWorkspace } from "../lib/workspace.js";
|
|
2
|
-
import { matchesCodePathPattern } from "../lib/search.js";
|
|
3
2
|
import { CliError } from "../lib/errors.js";
|
|
4
3
|
import { formatPaginationSummary } from "../lib/format.js";
|
|
5
4
|
import { paginate, paginationMetadata } from "../lib/pagination.js";
|
|
6
|
-
import { normalizeRepoRelativePath, resolveInsideRoot } from "../lib/fs.js";
|
|
7
5
|
import { estimateTokens } from "../lib/markdown.js";
|
|
6
|
+
import { getDocumentRequires, matchLocalContextFiles, resolveContextFileInput, resolveContextFiles } from "../lib/routing.js";
|
|
7
|
+
import { formatDocumentPath, isSourceRef, parseSourceDocId } from "../lib/source-id.js";
|
|
8
|
+
import { ensureSourcesForRead } from "../lib/sources.js";
|
|
9
|
+
import { recordTelemetryEvent } from "../lib/telemetry.js";
|
|
8
10
|
export function runContext(options) {
|
|
9
11
|
if (options.files.length === 0) {
|
|
10
|
-
throw new CliError("context
|
|
12
|
+
throw new CliError("context requires at least one --files input.");
|
|
11
13
|
}
|
|
12
14
|
const workspace = loadWorkspace(options.cwd);
|
|
13
|
-
const normalizedFiles = options.files.map((file) =>
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const normalizedFiles = options.files.map((file) => resolveContextFileInput(workspace.rootDir, file));
|
|
16
|
+
const prematchedLocal = matchLocalContextFiles(workspace, normalizedFiles);
|
|
17
|
+
const { localImpacted, unmappedFiles } = prematchedLocal;
|
|
18
|
+
const warnings = [];
|
|
19
|
+
const warnedAliases = new Set();
|
|
20
|
+
const relevantGitAliases = collectRelevantGitAliases(workspace, localImpacted);
|
|
21
|
+
if (relevantGitAliases.length > 0) {
|
|
22
|
+
const cold = ensureSourcesForRead(workspace, relevantGitAliases);
|
|
23
|
+
warnings.push(...cold.warnings);
|
|
24
|
+
for (const alias of relevantGitAliases) {
|
|
25
|
+
const source = workspace.sourcesByName.get(alias);
|
|
26
|
+
if (source && source.status !== "ready") {
|
|
27
|
+
warnedAliases.add(alias);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const source of workspace.sources) {
|
|
32
|
+
if (source.kind === "git" && source.status !== "ready" && !warnedAliases.has(source.name)) {
|
|
33
|
+
warnedAliases.add(source.name);
|
|
34
|
+
warnings.push(`Source @${source.name} was not searched because it is unavailable: ${source.error ?? "no local snapshot"}.${source.remedy ? ` ${source.remedy}` : ""}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const { sourceImpacted, impacted, prerequisites, missingPrerequisites } = resolveContextFiles(workspace, normalizedFiles, {
|
|
38
|
+
includeSources: true,
|
|
39
|
+
prematchedLocal
|
|
22
40
|
});
|
|
23
|
-
const documentsByFile = normalizedFiles.map((file) => ({
|
|
24
|
-
file,
|
|
25
|
-
documents: workspace.documents.filter((document) => (document.frontmatter.code?.paths ?? []).some((pattern) => matchesCodePathPattern(pattern, file.normalized)))
|
|
26
|
-
}));
|
|
27
|
-
const impactedPaths = new Set(documentsByFile.flatMap(({ documents }) => documents.map((document) => document.llmdocPath)));
|
|
28
|
-
const impacted = workspace.documents.filter((document) => impactedPaths.has(document.llmdocPath));
|
|
29
|
-
const unmappedFiles = documentsByFile
|
|
30
|
-
.filter(({ documents }) => documents.length === 0)
|
|
31
|
-
.map(({ file }) => file.normalized);
|
|
32
|
-
const prerequisites = collectRequires(workspace, impacted);
|
|
33
41
|
const rows = [
|
|
34
42
|
...impacted.map((document) => ({ role: "impacted", document })),
|
|
35
43
|
...prerequisites.map((document) => ({ role: "requires", document }))
|
|
@@ -39,53 +47,85 @@ export function runContext(options) {
|
|
|
39
47
|
estimate: (row) => estimateTokens(JSON.stringify({ role: row.role, ...toPayload(row.document) })),
|
|
40
48
|
options
|
|
41
49
|
});
|
|
50
|
+
recordTelemetryEvent(workspace.rootDir, {
|
|
51
|
+
type: "context",
|
|
52
|
+
docs: localImpacted.map((document) => document.llmdocPath),
|
|
53
|
+
unmapped: unmappedFiles
|
|
54
|
+
});
|
|
42
55
|
if (options.json) {
|
|
43
56
|
return {
|
|
44
57
|
impacted: result.items.filter((row) => row.role === "impacted").map((row) => toPayload(row.document)),
|
|
45
58
|
prerequisites: result.items.filter((row) => row.role === "requires").map((row) => toPayload(row.document)),
|
|
59
|
+
...(missingPrerequisites.length > 0 ? { missingPrerequisites } : {}),
|
|
46
60
|
unmappedFiles,
|
|
61
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
47
62
|
pagination: paginationMetadata(result)
|
|
48
63
|
};
|
|
49
64
|
}
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
lines.push(
|
|
55
|
-
|
|
65
|
+
const impactedWord = impacted.length === 1 ? "document" : "documents";
|
|
66
|
+
const prereqWord = prerequisites.length === 1 ? "prerequisite" : "prerequisites";
|
|
67
|
+
const lines = [`${impacted.length} ${impactedWord} impacted, ${prerequisites.length} recommended ${prereqWord}:`];
|
|
68
|
+
if (result.items.length > 0) {
|
|
69
|
+
lines.push("");
|
|
70
|
+
for (const row of result.items) {
|
|
71
|
+
const prefix = row.role === "requires" ? " requires -> " : " ";
|
|
72
|
+
const document = row.document;
|
|
73
|
+
lines.push(`${prefix}${formatDocumentPath(document)} [${document.frontmatter.kind}]`);
|
|
74
|
+
lines.push(` ${document.frontmatter.description}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (missingPrerequisites.length > 0) {
|
|
78
|
+
lines.push("", `missing external prerequisites: ${missingPrerequisites.length}`);
|
|
79
|
+
for (const missing of missingPrerequisites) {
|
|
80
|
+
lines.push(` missing -> ${missing.path} (${missing.reason})`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (warnings.length > 0) {
|
|
84
|
+
lines.push("");
|
|
85
|
+
for (const warning of warnings) {
|
|
86
|
+
lines.push(`warning: ${warning}`);
|
|
87
|
+
}
|
|
56
88
|
}
|
|
57
|
-
|
|
89
|
+
const hasExternalHitsWithUnmapped = sourceImpacted.length > 0 && unmappedFiles.length > 0;
|
|
90
|
+
lines.push("", hasExternalHitsWithUnmapped
|
|
91
|
+
? `unmapped files: ${unmappedFiles.length} (no local llmdoc owner)`
|
|
92
|
+
: `unmapped files: ${unmappedFiles.length}`);
|
|
58
93
|
for (const file of unmappedFiles) {
|
|
59
94
|
lines.push(` unmapped -> ${file}`);
|
|
60
95
|
}
|
|
61
96
|
lines.push("", ...formatPaginationSummary(result));
|
|
62
97
|
return lines.join("\n");
|
|
63
98
|
}
|
|
64
|
-
function
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
99
|
+
function collectRelevantGitAliases(workspace, localImpacted) {
|
|
100
|
+
const aliases = new Set();
|
|
101
|
+
for (const source of workspace.sources) {
|
|
102
|
+
if (source.kind === "git" && source.status === "missing") {
|
|
103
|
+
aliases.add(source.name);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const document of localImpacted) {
|
|
107
|
+
for (const req of getDocumentRequires(document)) {
|
|
108
|
+
if (!isSourceRef(req)) {
|
|
72
109
|
continue;
|
|
73
110
|
}
|
|
74
|
-
|
|
75
|
-
|
|
111
|
+
try {
|
|
112
|
+
const parsed = parseSourceDocId(req);
|
|
113
|
+
if (workspace.sourcesByName.get(parsed.alias)?.kind === "git") {
|
|
114
|
+
aliases.add(parsed.alias);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
76
118
|
continue;
|
|
77
119
|
}
|
|
78
|
-
seen.add(requirement);
|
|
79
|
-
results.push(next);
|
|
80
|
-
queue.push(next);
|
|
81
120
|
}
|
|
82
121
|
}
|
|
83
|
-
return
|
|
122
|
+
return [...aliases];
|
|
84
123
|
}
|
|
85
124
|
function toPayload(document) {
|
|
86
125
|
return {
|
|
87
|
-
path:
|
|
126
|
+
path: formatDocumentPath(document),
|
|
88
127
|
kind: document.frontmatter.kind,
|
|
89
|
-
description: document.frontmatter.description
|
|
128
|
+
description: document.frontmatter.description,
|
|
129
|
+
...(document.sourceName ? { source: document.sourceName } : {})
|
|
90
130
|
};
|
|
91
131
|
}
|