@thincoder/core 0.11.5 → 0.12.1
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/CHANGELOG.md +27 -0
- package/abort-provenance.mjs +23 -0
- package/advisor/citations.mjs +5 -1
- package/advisor/compaction.mjs +13 -10
- package/advisor/history.mjs +6 -1
- package/advisor/loop.mjs +35 -10
- package/advisor/messages.mjs +31 -43
- package/advisor/project-context.mjs +49 -45
- package/advisor/run.mjs +82 -26
- package/advisor.mjs +40 -11
- package/agent/assemble.mjs +5 -5
- package/agent/chat-call.mjs +21 -1
- package/agent/completion.mjs +1 -20
- package/agent/dispatch-gates.mjs +8 -3
- package/agent/dispatch-run.mjs +131 -20
- package/agent/dispatch.mjs +23 -22
- package/agent/family-tools.mjs +4 -7
- package/agent/goal-judge.mjs +99 -0
- package/agent/goal-state.mjs +96 -0
- package/agent/helpers.mjs +13 -4
- package/agent/offload.mjs +1 -1
- package/agent/planning-graph-guardrail.mjs +224 -0
- package/agent/planning-prompt.mjs +179 -0
- package/agent/planning-step.mjs +460 -173
- package/agent/post-turn.mjs +23 -33
- package/agent/record-results.mjs +8 -5
- package/agent/run-stages.mjs +18 -38
- package/agent/run-start.mjs +26 -19
- package/agent/setup-reminders.mjs +16 -14
- package/agent/setup.mjs +3 -2
- package/agent/spawn-child.mjs +17 -4
- package/agent/suspension.mjs +42 -21
- package/agent/task-lifecycle.mjs +165 -52
- package/agent/timers.mjs +9 -1
- package/agent/turn-loop.mjs +160 -69
- package/agent/turn-projection.mjs +26 -15
- package/agent/turn-task-state.mjs +4 -4
- package/agent/write-gate-ledger.mjs +1 -1
- package/agent/write-gate.mjs +14 -11
- package/agent-tools/advisor-async.mjs +2 -0
- package/agent-tools/advisor-launch.mjs +27 -13
- package/agent-tools/advisor-settle.mjs +19 -18
- package/agent-tools/advisor.mjs +90 -13
- package/agent-tools/bash-async.mjs +3 -3
- package/agent-tools/batch-ledger-bind.mjs +8 -5
- package/agent-tools/batch-read.mjs +42 -0
- package/agent-tools/batch-record-write.mjs +20 -12
- package/agent-tools/batch-segment-vocab.mjs +6 -4
- package/agent-tools/batch-skeleton.mjs +43 -12
- package/agent-tools/consult.mjs +5 -1
- package/agent-tools/context.mjs +5 -4
- package/agent-tools/design-slots.mjs +7 -4
- package/agent-tools/design-token.mjs +3 -2
- package/agent-tools/doc-graph.mjs +257 -95
- package/agent-tools/escalate-async.mjs +4 -1
- package/agent-tools/goal.mjs +28 -95
- package/agent-tools/plan.mjs +1 -1
- package/agent-tools/review-facts.mjs +56 -7
- package/agent-tools/settings.mjs +35 -1
- package/agent-tools/ssh-transport.mjs +23 -22
- package/agent-tools/step.mjs +24 -4
- package/agent-tools/subagent-actions.mjs +6 -2
- package/agent-tools/subagent-async.mjs +4 -2
- package/agent-tools/subagent.mjs +1 -1
- package/agent-tools/task.mjs +73 -65
- package/agent-tools.mjs +3 -3
- package/agent.mjs +12 -3
- package/auto-think.mjs +16 -3
- package/browser/actions.mjs +5 -1
- package/browser/cdp.mjs +1 -1
- package/browser/pool.mjs +110 -17
- package/compaction-work-state.mjs +103 -13
- package/compress-form.mjs +29 -3
- package/config-defaults.mjs +27 -3
- package/config-io.mjs +3 -14
- package/config-normalize.mjs +24 -4
- package/config-presets.mjs +40 -26
- package/config-warn.mjs +30 -0
- package/config.mjs +139 -63
- package/context-degrade.mjs +4 -4
- package/context-push.mjs +20 -9
- package/context.mjs +65 -20
- package/doc-graph/anchors.mjs +39 -40
- package/doc-graph/clusters.mjs +49 -0
- package/doc-graph/code-parsers.mjs +494 -0
- package/doc-graph/context-pack.mjs +392 -0
- package/doc-graph/dir-signals.mjs +228 -0
- package/doc-graph/edges.mjs +231 -0
- package/doc-graph/format.mjs +70 -0
- package/doc-graph/gc-cmd.mjs +104 -0
- package/doc-graph/gc.mjs +112 -0
- package/doc-graph/glob-work-graph.mjs +307 -0
- package/doc-graph/golden-set.mjs +131 -0
- package/doc-graph/ls-work-graph.mjs +534 -0
- package/doc-graph/outline.mjs +377 -0
- package/doc-graph/output-annotations.mjs +85 -0
- package/doc-graph/process-docs.mjs +68 -0
- package/doc-graph/read-work-graph.mjs +91 -0
- package/doc-graph/rebuild-cmd.mjs +75 -0
- package/doc-graph/resolvers.mjs +322 -0
- package/doc-graph/roots.mjs +86 -0
- package/doc-graph/schema.mjs +110 -0
- package/doc-graph/search-augment.mjs +418 -0
- package/doc-graph/store.mjs +231 -0
- package/doc-graph/target-resolve.mjs +352 -0
- package/doc-graph/walk.mjs +87 -0
- package/doc-graph.mjs +583 -707
- package/explore-distill.mjs +52 -17
- package/generate-title.mjs +7 -4
- package/ledger-close.mjs +6 -6
- package/ledger-cmd.mjs +3 -3
- package/ledger-consistency.mjs +8 -6
- package/ledger-db.mjs +45 -10
- package/ledger-migrate.mjs +25 -10
- package/ledger-read.mjs +1 -1
- package/ledger-write-ops.mjs +6 -6
- package/ledger.mjs +2 -2
- package/model-context.mjs +54 -18
- package/model-specs.mjs +86 -67
- package/package.json +1 -1
- package/prompt-overlays.mjs +3 -3
- package/prompts/advisor-round1.md +6 -5
- package/prompts/common.md +16 -6
- package/prompts/consult-base.md +2 -1
- package/prompts/discipline-engineering.md +1 -1
- package/prompts/discipline-normal.md +12 -8
- package/prompts/knowledge/doc-lookup.md +2 -2
- package/prompts/knowledge/normal-batch-discipline.md +2 -2
- package/prompts/knowledge/normal-delivery-collaboration.md +3 -3
- package/prompts/knowledge/tool-advisor.md +2 -2
- package/prompts/knowledge/tool-apply-patch.md +1 -1
- package/prompts/knowledge/tool-bash.md +2 -2
- package/prompts/knowledge/tool-batch.md +4 -4
- package/prompts/knowledge/tool-glob.md +9 -2
- package/prompts/knowledge/tool-goal.md +9 -6
- package/prompts/knowledge/tool-grep.md +7 -2
- package/prompts/knowledge/tool-ls.md +11 -5
- package/prompts/knowledge/tool-lsp.md +1 -1
- package/prompts/knowledge/tool-operations.md +3 -2
- package/prompts/knowledge/tool-plan.md +1 -1
- package/prompts/knowledge/tool-read.md +7 -6
- package/prompts/knowledge/tool-repo-graph-read.md +12 -0
- package/prompts/knowledge/tool-repo-graph.md +12 -0
- package/prompts/knowledge/tool-step.md +1 -1
- package/prompts/knowledge/tool-subagent.md +2 -2
- package/prompts/knowledge/tool-task.md +10 -9
- package/prompts/knowledge/tool-tree.md +7 -1
- package/prompts/persona-eng-coder.md +1 -1
- package/prompts/persona-eng-designer.md +1 -1
- package/prompts/persona-engineering.md +2 -1
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-normal.md +7 -6
- package/prompts/persona-plan.md +2 -2
- package/prompts/planning-next-moves.md +7 -8
- package/provider/anthropic.mjs +53 -51
- package/provider/core.mjs +85 -34
- package/provider/errors.mjs +33 -10
- package/provider/google.mjs +244 -37
- package/provider/list-models.mjs +28 -14
- package/provider/responses-request.mjs +15 -9
- package/provider/responses.mjs +10 -8
- package/provider/sse.mjs +3 -6
- package/proxy-transport.mjs +228 -119
- package/rules-file.mjs +79 -0
- package/rules.mjs +13 -51
- package/session-lifecycle.mjs +32 -20
- package/team-ledger.mjs +7 -7
- package/think-off.mjs +37 -18
- package/token-window.mjs +48 -31
- package/tool-args.mjs +8 -8
- package/tool-docs/bash.md +1 -1
- package/tool-docs/glob.md +3 -1
- package/tool-docs/goal.md +2 -2
- package/tool-docs/grep.md +3 -1
- package/tool-docs/ls.md +7 -1
- package/tool-docs/read.md +1 -1
- package/tool-docs/repo_graph-read.md +3 -0
- package/tool-docs/repo_graph.md +3 -0
- package/tool-docs/tree.md +1 -1
- package/tools/bash.mjs +53 -8
- package/tools/browser.mjs +4 -4
- package/tools/file.mjs +34 -7
- package/tools/glob-dialect.mjs +57 -7
- package/tools/index.mjs +3 -7
- package/tools/patch.mjs +8 -7
- package/tools/process-doc-mark.mjs +13 -0
- package/tools/search.mjs +152 -37
- package/tools/shared.mjs +6 -0
- package/tools/tree.mjs +211 -8
- package/window-policy.mjs +205 -126
- package/prompts/knowledge/tool-doc-graph-read.md +0 -20
- package/prompts/knowledge/tool-doc-graph.md +0 -49
- package/prompts/knowledge/tool-peek.md +0 -10
- package/prompts/knowledge/tool-repo-outline.md +0 -4
- package/tool-docs/doc_graph-read.md +0 -3
- package/tool-docs/doc_graph.md +0 -3
- package/tool-docs/peek.md +0 -3
- package/tool-docs/repo_outline.md +0 -3
- package/tools/peek.mjs +0 -447
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* glob-work-graph.mjs — turn glob's exact file matches into a bounded working graph.
|
|
3
|
+
* Dynamically imported by tools/search.mjs: this module reaches node:sqlite through store.mjs.
|
|
4
|
+
*/
|
|
5
|
+
import { isAbsolute, posix, relative, resolve, win32 } from "node:path"
|
|
6
|
+
|
|
7
|
+
import { clusterGraph } from "./clusters.mjs"
|
|
8
|
+
import { processDocPredicate } from "./process-docs.mjs"
|
|
9
|
+
import { resolveProjectRootForFile, resolveQueryRoot } from "./roots.mjs"
|
|
10
|
+
import { syncGraph } from "./store.mjs"
|
|
11
|
+
|
|
12
|
+
export const GLOB_WORK_GRAPH_BUDGET = 6000
|
|
13
|
+
export const slash = (p) => String(p ?? "").replace(/\\/g, "/")
|
|
14
|
+
export const pathKey = (p) => process.platform === "win32"
|
|
15
|
+
? slash(win32.resolve(p)).toLowerCase()
|
|
16
|
+
: posix.resolve(p)
|
|
17
|
+
export const edgeText = (symbols) => symbols.length > 0 ? symbols.slice(0, 6).join(", ") : "side-effect import"
|
|
18
|
+
|
|
19
|
+
function rootsForHits(args, ctx, hitPaths) {
|
|
20
|
+
const cwd = resolve(String(ctx.cwd ?? ctx.agent?.cwd ?? "."))
|
|
21
|
+
const base = resolve(cwd, String(args.path ?? "."))
|
|
22
|
+
const queryRoot = resolveQueryRoot(base)
|
|
23
|
+
const fallback = queryRoot.ok ? queryRoot.root : base
|
|
24
|
+
const roots = new Map()
|
|
25
|
+
for (const hit of hitPaths) {
|
|
26
|
+
const abs = isAbsolute(hit) ? resolve(hit) : resolve(cwd, hit)
|
|
27
|
+
const root = resolveProjectRootForFile(abs, fallback)
|
|
28
|
+
if (!roots.has(root)) roots.set(root, [])
|
|
29
|
+
roots.get(root).push(abs)
|
|
30
|
+
}
|
|
31
|
+
return roots
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function importEdges(db) {
|
|
35
|
+
const rows = db.prepare("SELECT src, dst, symbol FROM edges WHERE kind = 'imports' AND state = 'ok'").all()
|
|
36
|
+
const grouped = new Map()
|
|
37
|
+
for (const row of rows) {
|
|
38
|
+
const key = `${row.src}\u0000${row.dst}`
|
|
39
|
+
if (!grouped.has(key)) grouped.set(key, { src: row.src, dst: row.dst, symbols: new Set() })
|
|
40
|
+
if (row.symbol) grouped.get(key).symbols.add(row.symbol)
|
|
41
|
+
}
|
|
42
|
+
return [...grouped.values()].map((edge) => ({ ...edge, symbols: [...edge.symbols].sort() }))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function selectConnectors(seeds, internal, imports, byPath) {
|
|
46
|
+
const candidates = new Map()
|
|
47
|
+
for (const edge of imports) {
|
|
48
|
+
if (!seeds.has(edge.dst) || seeds.has(edge.src) || byPath.get(edge.src)?.kind !== "code") continue
|
|
49
|
+
if (!candidates.has(edge.src)) candidates.set(edge.src, [])
|
|
50
|
+
candidates.get(edge.src).push(edge)
|
|
51
|
+
}
|
|
52
|
+
const hubs = new Set(internal.map((edge) => edge.src))
|
|
53
|
+
const ranked = [...candidates.entries()]
|
|
54
|
+
.filter(([, edges]) => seeds.size === 1 || edges.length >= 2 || edges.some((edge) => hubs.has(edge.dst)))
|
|
55
|
+
.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]))
|
|
56
|
+
return { items: ranked.slice(0, 4), omitted: Math.max(0, ranked.length - 4) }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function directDocuments(db, seeds, byPath, isProc) {
|
|
60
|
+
const docs = new Map()
|
|
61
|
+
const rows = db.prepare("SELECT src, dst, line FROM edges WHERE kind = 'doc-covers' AND state = 'ok'").all()
|
|
62
|
+
for (const row of rows) {
|
|
63
|
+
if (!seeds.has(row.dst) || byPath.get(row.src)?.kind !== "doc" || isProc(row.src)) continue
|
|
64
|
+
if (!docs.has(row.src)) docs.set(row.src, { path: row.src, lines: [], seeds: new Set() })
|
|
65
|
+
docs.get(row.src).lines.push(row.line)
|
|
66
|
+
docs.get(row.src).seeds.add(row.dst)
|
|
67
|
+
}
|
|
68
|
+
return [...docs.values()].sort((a, b) => b.seeds.size - a.seeds.size || a.path.localeCompare(b.path))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function directTests(imports, seeds, byPath, isProc) {
|
|
72
|
+
const tests = new Map()
|
|
73
|
+
for (const edge of imports) {
|
|
74
|
+
if (!seeds.has(edge.dst) || byPath.get(edge.src)?.kind !== "test" || isProc(edge.src)) continue
|
|
75
|
+
if (!tests.has(edge.src)) tests.set(edge.src, { path: edge.src, seeds: new Set() })
|
|
76
|
+
tests.get(edge.src).seeds.add(edge.dst)
|
|
77
|
+
}
|
|
78
|
+
return [...tests.values()].sort((a, b) => b.seeds.size - a.seeds.size || a.path.localeCompare(b.path))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function sectionIndex(db) {
|
|
82
|
+
const index = new Map()
|
|
83
|
+
for (const row of db.prepare("SELECT file, title, line FROM headings ORDER BY line").all()) {
|
|
84
|
+
if (!index.has(row.file)) index.set(row.file, [])
|
|
85
|
+
index.get(row.file).push(row)
|
|
86
|
+
}
|
|
87
|
+
return index
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function pairedDocumentPath(path, byPath) {
|
|
91
|
+
const peer = path.includes("/design/")
|
|
92
|
+
? path.replace("/design/", "/requirements/")
|
|
93
|
+
: path.includes("/requirements/") ? path.replace("/requirements/", "/design/") : ""
|
|
94
|
+
return peer && byPath.has(peer) ? peer : ""
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function documentLine(doc, sections, byPath) {
|
|
98
|
+
const line = Math.min(...doc.lines)
|
|
99
|
+
const section = (sections.get(doc.path) ?? []).filter((item) => item.line <= line).at(-1)
|
|
100
|
+
const location = section ? `${doc.path} §${section.title}:L${line}` : `${doc.path}:L${line}`
|
|
101
|
+
const peer = pairedDocumentPath(doc.path, byPath)
|
|
102
|
+
return ` - ${location}${peer ? ` — paired: ${peer}` : ""} · covers ${doc.seeds.size} matched file${doc.seeds.size === 1 ? "" : "s"}`
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function addKnowledgeLines(lines, model, sharedOnly = false) {
|
|
106
|
+
const docs = sharedOnly ? model.docs.filter((doc) => doc.seeds.size >= 2) : model.docs
|
|
107
|
+
const tests = sharedOnly ? model.tests.filter((test) => test.seeds.size >= 2) : model.tests
|
|
108
|
+
if (docs.length > 0) {
|
|
109
|
+
lines.push("Contracts:", ...docs.slice(0, 4).map((doc) => documentLine(doc, model.sections, model.byPath)))
|
|
110
|
+
if (docs.length > 4) lines.push(` … ${docs.length - 4} contract documents omitted`)
|
|
111
|
+
}
|
|
112
|
+
if (tests.length > 0) {
|
|
113
|
+
lines.push("Direct-import tests:", ...tests.slice(0, 4).map((test) => ` - ${test.path} · directly imports ${test.seeds.size} matched file${test.seeds.size === 1 ? "" : "s"}`))
|
|
114
|
+
if (tests.length > 4) lines.push(` … ${tests.length - 4} direct tests omitted`)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Header counts are totals; a display sample is always called out as `N shown`.
|
|
119
|
+
* `indexed of M matched` 只在有命中未入图时出现——把入图种子数写成 matched,读者会以为 glob 只命中这么多。 */
|
|
120
|
+
const matchedLabel = (seeds, matchedCount) => matchedCount > seeds.size
|
|
121
|
+
? `${seeds.size} indexed of ${matchedCount} matched`
|
|
122
|
+
: `${seeds.size} matched`
|
|
123
|
+
|
|
124
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`
|
|
125
|
+
const cellName = (path) => String(path).split("/").pop().replace(/\.[^.]+$/, "")
|
|
126
|
+
|
|
127
|
+
/** 消费者速记:`basename(覆盖命中数)`;basename 撞车时改写相对路径 —— 与 `ls` 目录行的 hubs 同一条消歧规则。 */
|
|
128
|
+
function summariseOrchestrators(connectors, omitted) {
|
|
129
|
+
const entries = connectors.map(([path, edges]) => ({ path, n: new Set(edges.map((edge) => edge.dst)).size }))
|
|
130
|
+
const names = entries.map((entry) => cellName(entry.path))
|
|
131
|
+
const dupes = new Set(names.filter((name, index) => names.indexOf(name) !== index))
|
|
132
|
+
const cells = entries.map((entry, index) => `${dupes.has(names[index]) ? entry.path : names[index]}(${entry.n})`)
|
|
133
|
+
return { total: cells.length + omitted, line: `${cells.join(" · ")}${omitted > 0 ? ` … ${omitted} omitted` : ""}` }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 契约速记:`path §chapter covers N`,章节取引用行之前最近的标题(与 `ls` 的 Contracts 同一个取法)。 */
|
|
137
|
+
function contractCells(model, sharedOnly) {
|
|
138
|
+
const docs = sharedOnly ? model.docs.filter((doc) => doc.seeds.size >= 2) : model.docs
|
|
139
|
+
const cells = docs.slice(0, 4).map((doc) => {
|
|
140
|
+
const line = Math.min(...doc.lines)
|
|
141
|
+
const section = (model.sections.get(doc.path) ?? []).filter((item) => item.line <= line).at(-1)
|
|
142
|
+
const peer = pairedDocumentPath(doc.path, model.byPath)
|
|
143
|
+
return `${doc.path}${section ? ` §${section.title}` : ""}${peer ? ` — paired: ${peer}` : ""} covers ${doc.seeds.size}`
|
|
144
|
+
})
|
|
145
|
+
return { total: docs.length, line: `${cells.join(" · ")}${docs.length > 4 ? ` … ${docs.length - 4} omitted` : ""}` }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function testCells(tests) {
|
|
149
|
+
const cells = tests.slice(0, 4).map((test) => `${test.path}(${test.seeds.size})`)
|
|
150
|
+
return `${cells.join(" · ")}${tests.length > 4 ? ` … ${tests.length - 4} omitted` : ""}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function renderSmall(model) {
|
|
154
|
+
const { seeds, connectors, connectorOmitted, docs } = model
|
|
155
|
+
const orc = summariseOrchestrators(connectors, connectorOmitted)
|
|
156
|
+
const con = contractCells(model, false)
|
|
157
|
+
const head = `${matchedLabel(seeds, model.matchedCount)} · ${plural(orc.total, "orchestrator")}${orc.total > connectors.length ? ` (${connectors.length} shown)` : ""} · ${plural(con.total, "contract")}${con.total > 4 ? ` (4 shown)` : ""}`
|
|
158
|
+
const lines = [head]
|
|
159
|
+
if (connectors.length > 0) lines.push(` 消费者: ${orc.line}`)
|
|
160
|
+
if (con.total > 0) lines.push(` 契约: ${con.line}`)
|
|
161
|
+
if (model.tests.length > 0) lines.push(` 直接导入测试: ${testCells(model.tests)}`)
|
|
162
|
+
return lines.join("\n")
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function renderClusters(model, architecture) {
|
|
166
|
+
const { groups, internalCounts } = clusterGraph(model, architecture)
|
|
167
|
+
const orc = summariseOrchestrators(model.connectors, model.connectorOmitted)
|
|
168
|
+
const con = contractCells(model, architecture)
|
|
169
|
+
const lines = [`${matchedLabel(model.seeds, model.matchedCount)} · ${plural(groups.length, "cluster")}${architecture ? " · architecture" : ""}${architecture ? ` · ${plural(orc.total, "orchestrator")}${orc.total > model.connectors.length ? ` (${model.connectors.length} shown)` : ""}` : ""} · ${plural(con.total, "contract")}${con.total > 4 ? " (4 shown)" : ""}`]
|
|
170
|
+
if (architecture && model.connectors.length > 0) lines.push(` 消费者: ${orc.line}`)
|
|
171
|
+
const cells = groups.slice(0, 12).map((group) => `${group.key}${group.kind === "orchestrator" ? "" : "/"}(${group.paths.size} 档, ${internalCounts.get(group.key)} 内部边)`)
|
|
172
|
+
const hidden = groups.slice(12)
|
|
173
|
+
const clusterLine = `${cells.join(" · ")}${hidden.length > 0 ? ` … ${hidden.length} omitted` : ""}`
|
|
174
|
+
lines.push(` 簇: ${clusterLine}`)
|
|
175
|
+
if (con.total > 0) lines.push(` 契约: ${con.line}`)
|
|
176
|
+
if (model.tests.length > 0) lines.push(` 直接导入测试: ${testCells(model.tests)}`)
|
|
177
|
+
return lines.join("\n")
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function renderSeedGraph(model) {
|
|
181
|
+
return model.seeds.size <= 12 ? renderSmall(model) : renderClusters(model, model.seeds.size > 100)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Keep complete lines and an exact omission count; the caller's list is outside this string. */
|
|
185
|
+
export function capWorkGraph(text, maxChars = GLOB_WORK_GRAPH_BUDGET, listLabel = "match list") {
|
|
186
|
+
const source = String(text)
|
|
187
|
+
if (source.length <= maxChars) return source
|
|
188
|
+
const lines = source.split("\n")
|
|
189
|
+
const kept = []
|
|
190
|
+
let used = 0
|
|
191
|
+
for (const line of lines) {
|
|
192
|
+
if (used + line.length + 1 > maxChars - 140) break
|
|
193
|
+
kept.push(line)
|
|
194
|
+
used += line.length + 1
|
|
195
|
+
}
|
|
196
|
+
let note = ""
|
|
197
|
+
while (true) {
|
|
198
|
+
const omitted = lines.length - kept.length
|
|
199
|
+
note = `[graph] … ${omitted} summary lines omitted by the ${maxChars}-character budget; ${listLabel} unchanged.`
|
|
200
|
+
if (used + note.length <= maxChars || kept.length === 0) break
|
|
201
|
+
used -= kept.pop().length + 1
|
|
202
|
+
}
|
|
203
|
+
return [...kept, note].join("\n")
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function capGlobWorkGraph(text, maxChars = GLOB_WORK_GRAPH_BUDGET) {
|
|
207
|
+
return capWorkGraph(text, maxChars, "match list")
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function buildRootGraph(root, absoluteHits, sync, sink = null, base = null) {
|
|
211
|
+
const { db, fileCount, skippedCold } = sync(root, { maxColdFiles: 2500 })
|
|
212
|
+
if (skippedCold || !db) return `[graph] Cold graph skipped: ${fileCount} files exceed 2500; match list unchanged. Run repo_graph(action="overview", target=${JSON.stringify(root)}) to build it explicitly.`
|
|
213
|
+
try {
|
|
214
|
+
const files = db.prepare("SELECT path, kind FROM files").all()
|
|
215
|
+
const byPath = new Map(files.map((file) => [file.path, file]))
|
|
216
|
+
const dbKeys = new Map(files.map((file) => [pathKey(file.path), file.path]))
|
|
217
|
+
const seeds = new Set()
|
|
218
|
+
for (const abs of absoluteHits) {
|
|
219
|
+
const rel = slash(relative(root, abs))
|
|
220
|
+
const path = dbKeys.get(pathKey(rel))
|
|
221
|
+
if (path) seeds.add(path)
|
|
222
|
+
}
|
|
223
|
+
if (seeds.size === 0) return `[graph] No matched files are indexed in ${root}; match list unchanged.`
|
|
224
|
+
const isProc = processDocPredicate(root)
|
|
225
|
+
const imports = importEdges(db)
|
|
226
|
+
// 清单行的 `←N` 与建图共用这一次 sync:`syncGraph` 没有进程级缓存,开第二次库会再扫一遍 mtime。
|
|
227
|
+
if (sink && base) fillRowNotes(sink, root, base, absoluteHits, byPath, dbKeys, imports, seeds)
|
|
228
|
+
const internal = imports.filter((edge) => seeds.has(edge.src) && seeds.has(edge.dst))
|
|
229
|
+
const connectorSelection = selectConnectors(seeds, internal, imports, byPath)
|
|
230
|
+
const connectors = connectorSelection.items
|
|
231
|
+
const model = {
|
|
232
|
+
seeds, internal, connectors, imports,
|
|
233
|
+
matchedCount: absoluteHits.length,
|
|
234
|
+
connectorOmitted: connectorSelection.omitted,
|
|
235
|
+
docs: directDocuments(db, seeds, byPath, isProc),
|
|
236
|
+
tests: directTests(imports, seeds, byPath, isProc),
|
|
237
|
+
sections: sectionIndex(db), byPath,
|
|
238
|
+
}
|
|
239
|
+
return renderSeedGraph(model)
|
|
240
|
+
} finally {
|
|
241
|
+
try { db.close() } catch { /* graph cleanup cannot affect matches */ }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function buildGlobWorkGraph({ args = {}, ctx = {}, hitPaths = [], noteSink = null }, sync = syncGraph) {
|
|
246
|
+
const roots = rootsForHits(args, ctx, hitPaths)
|
|
247
|
+
const base = resolve(String(ctx.cwd ?? ctx.agent?.cwd ?? "."), String(args.path ?? "."))
|
|
248
|
+
const sections = []
|
|
249
|
+
for (const [root, hits] of roots) {
|
|
250
|
+
let section
|
|
251
|
+
try { section = buildRootGraph(root, hits, sync, noteSink, base) }
|
|
252
|
+
catch (error) { section = `[graph] ${root}: ${error?.message || String(error)}; match list unchanged.` }
|
|
253
|
+
sections.push(roots.size > 1 ? `Project: ${root}\n${section}` : section)
|
|
254
|
+
}
|
|
255
|
+
return capGlobWorkGraph(sections.join("\n"))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export async function appendGlobWorkGraph({ args = {}, ctx = {}, rawResult = "", hitPaths = [] }, sync = syncGraph) {
|
|
259
|
+
const raw = String(rawResult)
|
|
260
|
+
if (hitPaths.length === 0) return raw
|
|
261
|
+
const notes = new Map()
|
|
262
|
+
let graph
|
|
263
|
+
try {
|
|
264
|
+
graph = buildGlobWorkGraph({ args, ctx, hitPaths, noteSink: notes }, sync)
|
|
265
|
+
} catch (error) {
|
|
266
|
+
// 失败面只作用于图:清单与已算出的行注记都保留(`rootsForHits` 阶段就失败时注记还是空的,清单便原样返回),
|
|
267
|
+
// 图的位置换一行降级说明,不让降级说明比它依赖的数据更新。
|
|
268
|
+
return `${annotateMatchRows(raw, notes)}\n[graph] ${error?.message || String(error)}; match list unchanged.`
|
|
269
|
+
}
|
|
270
|
+
const listed = annotateMatchRows(raw, notes)
|
|
271
|
+
return graph ? `${graph}\n\n${listed}` : listed
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 清单行注记(` · ←N`)——`N` = 该档被多少个源码档 import,与 `ls` 文件行、`tree` 的 hub 入度**同源同口径**
|
|
276
|
+
* (`dir-signals` 的 `inboundOfFile`:`kind='imports' AND state='ok'` 的 `DISTINCT src,dst`,两端都是 `kind='code'`)。
|
|
277
|
+
* 这里不另建判据:`importEdges` 已按 `src,dst` 去重,`byPath` 给 kind。
|
|
278
|
+
*
|
|
279
|
+
* 只写 code 行:`←N` 问的是「被源码导入几次」、`→hits N` 问的是「导入了多少个同一次命中集内的档」,
|
|
280
|
+
* doc/test 行上这两个问题都没有对应读数。
|
|
281
|
+
* 入图的 code 行**一律**写数(含 `0`)——`0` 与「没入图所以没数」是两件事,只写非零会让两者看起来一样;
|
|
282
|
+
* `→hits 0` 不排除命中集之外的依赖,因此不用这两个数判定全仓孤立,也不另列孤立段。
|
|
283
|
+
*/
|
|
284
|
+
function fillRowNotes(sink, root, base, absoluteHits, byPath, dbKeys, imports, seeds) {
|
|
285
|
+
const inDeg = new Map()
|
|
286
|
+
const outDeg = new Map()
|
|
287
|
+
for (const edge of imports) {
|
|
288
|
+
if (byPath.get(edge.src)?.kind !== "code" || byPath.get(edge.dst)?.kind !== "code") continue
|
|
289
|
+
inDeg.set(edge.dst, (inDeg.get(edge.dst) ?? 0) + 1)
|
|
290
|
+
if (seeds.has(edge.src) && seeds.has(edge.dst)) outDeg.set(edge.src, (outDeg.get(edge.src) ?? 0) + 1)
|
|
291
|
+
}
|
|
292
|
+
for (const abs of absoluteHits) {
|
|
293
|
+
const path = dbKeys.get(pathKey(slash(relative(root, abs))))
|
|
294
|
+
if (!path || byPath.get(path)?.kind !== "code") continue
|
|
295
|
+
sink.set(slash(relative(base, abs)), ` · ←${inDeg.get(path) ?? 0} · →hits ${outDeg.get(path) ?? 0}`)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Append each row's own note; non-row lines (process-doc report, limit notice) carry no note and pass through. */
|
|
300
|
+
export function annotateMatchRows(rawResult, notes) {
|
|
301
|
+
if (!notes || notes.size === 0) return String(rawResult)
|
|
302
|
+
return String(rawResult).split("\n").map((line) => {
|
|
303
|
+
const text = line.replace(/^\[[^\]]*\] /, "").trimEnd()
|
|
304
|
+
const note = notes.get(text)
|
|
305
|
+
return note ? `${line}${note}` : line
|
|
306
|
+
}).join("\n")
|
|
307
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doc-graph/golden-set.mjs — 知识图谱的 golden set 回归面(P2-10)。
|
|
3
|
+
*
|
|
4
|
+
* 判据只问**行为**,不问规模:每条要么断言「某个已知关系出现在输出里」,要么断言一条不变量。
|
|
5
|
+
* 绝不锁总数、锁行号、锁原文 —— 仓库长大、文档增删都是正常的,锁规模会把正常变化变成红灯。
|
|
6
|
+
*
|
|
7
|
+
* 用途:第 4 批「符号级边」改 `edges` 结构时,先用它确认既有关系一根没断。
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync } from "node:fs"
|
|
10
|
+
import { resolve } from "node:path"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{id: string, kind: "refs"|"impact"|"context"|"overview"|"invariant",
|
|
14
|
+
* query?: string, contains?: string[], readingPackIncludes?: string[],
|
|
15
|
+
* expect?: string}} GoldenQuery
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** @type {GoldenQuery[]} */
|
|
19
|
+
export const GOLDEN_QUERIES = [
|
|
20
|
+
{
|
|
21
|
+
id: "code-cite-reaches-design-doc",
|
|
22
|
+
kind: "refs",
|
|
23
|
+
query: "thincoder-core/doc-graph.mjs",
|
|
24
|
+
contains: ["--code-cites-->"],
|
|
25
|
+
expect: "代码头注引的设计档要落成 code-cites 边(P0-2 的可观测面)",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "design-doc-covers-source",
|
|
29
|
+
kind: "refs",
|
|
30
|
+
query: "docs/core/design/REPO-GRAPH.md",
|
|
31
|
+
contains: ["--doc-covers-->"],
|
|
32
|
+
expect: "设计档里写出的源码路径要落成 doc-covers 边",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "context-pairs-requirements-with-design",
|
|
36
|
+
kind: "context",
|
|
37
|
+
query: "REPO-GRAPH.md",
|
|
38
|
+
readingPackIncludes: ["docs/core/requirements/REPO-GRAPH.md"],
|
|
39
|
+
expect: "同主题的需求档要自动配进必读包",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "context-pack-has-target",
|
|
43
|
+
kind: "context",
|
|
44
|
+
query: "thincoder-core/doc-graph/store.mjs",
|
|
45
|
+
readingPackIncludes: ["thincoder-core/doc-graph/store.mjs"],
|
|
46
|
+
expect: "必读包必须含目标档本身(否则模型会漏读正在改的档)",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: "impact-lists-authoritative-doc",
|
|
50
|
+
kind: "impact",
|
|
51
|
+
query: "thincoder-core/doc-graph/store.mjs",
|
|
52
|
+
contains: ["受影响权威文档"],
|
|
53
|
+
expect: "impact 必须给出「改这个档要同轮核对哪些文档」这一段",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: "overview-lists-hubs",
|
|
57
|
+
kind: "overview",
|
|
58
|
+
contains: ["Top Hub"],
|
|
59
|
+
expect: "overview 必须给出被导入最多的枢纽源码",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "symbol-level-import-edge",
|
|
63
|
+
kind: "refs",
|
|
64
|
+
query: "thincoder-core/doc-graph.mjs",
|
|
65
|
+
// 锁的是「具名导入的边要带出符号」这一行为,不是某个符号名:被导入的符号换名 / 换档时,把这里换成
|
|
66
|
+
// 一个仍然被具名导入的符号即可(`store.mjs` 拆分后改指 `graphDbPath`,`doc-graph.mjs` 拆分后改指 `fmtItem`)。
|
|
67
|
+
contains: ["--imports-->", "[fmtItem]"],
|
|
68
|
+
expect: "具名导入的边要带出符号(P0-1 符号级边的可观测面)",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "no-missing-edge-pointing-at-existing-file",
|
|
72
|
+
kind: "invariant",
|
|
73
|
+
expect: "missing 边不得指向盘上真实存在的档(那是「被排除出图」,不是断链 —— P1-4)",
|
|
74
|
+
},
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 跑整套 golden set。
|
|
79
|
+
* @returns {Promise<{total: number, passed: number, failed: number, results: Array<{id: string, pass: boolean, detail: string}>}>}
|
|
80
|
+
*/
|
|
81
|
+
export async function runGoldenSet(root) {
|
|
82
|
+
const absRoot = resolve(String(root ?? "."))
|
|
83
|
+
const mod = await import("../doc-graph.mjs")
|
|
84
|
+
const results = []
|
|
85
|
+
|
|
86
|
+
for (const item of GOLDEN_QUERIES) {
|
|
87
|
+
let pass = false
|
|
88
|
+
let detail = ""
|
|
89
|
+
try {
|
|
90
|
+
if (item.kind === "refs" || item.kind === "impact") {
|
|
91
|
+
const text = item.kind === "refs"
|
|
92
|
+
? await mod.queryRefs(absRoot, item.query)
|
|
93
|
+
: await mod.queryImpact(absRoot, item.query)
|
|
94
|
+
const missing = (item.contains ?? []).filter((s) => !text.includes(s))
|
|
95
|
+
pass = missing.length === 0
|
|
96
|
+
detail = pass ? "ok" : `缺:${missing.join(" / ")}`
|
|
97
|
+
} else if (item.kind === "context") {
|
|
98
|
+
const pack = await mod.queryContextPack(absRoot, item.query)
|
|
99
|
+
const paths = (pack?.readingPack ?? []).map((x) => x.path)
|
|
100
|
+
const missing = (item.readingPackIncludes ?? []).filter((p) => !paths.includes(p))
|
|
101
|
+
pass = missing.length === 0
|
|
102
|
+
detail = pass ? "ok" : `必读包缺:${missing.join(" / ")}`
|
|
103
|
+
} else if (item.kind === "overview") {
|
|
104
|
+
const text = await mod.queryOverview(absRoot)
|
|
105
|
+
const missing = (item.contains ?? []).filter((s) => !text.includes(s))
|
|
106
|
+
pass = missing.length === 0
|
|
107
|
+
detail = pass ? "ok" : `缺:${missing.join(" / ")}`
|
|
108
|
+
} else if (item.kind === "invariant") {
|
|
109
|
+
const { openGraphDb } = await import("./store.mjs")
|
|
110
|
+
const db = openGraphDb(absRoot)
|
|
111
|
+
try {
|
|
112
|
+
const rows = db.prepare("SELECT src, dst FROM edges WHERE state = 'missing'").all()
|
|
113
|
+
const bad = rows.filter((r) => existsSync(resolve(absRoot, r.dst)))
|
|
114
|
+
pass = bad.length === 0
|
|
115
|
+
detail = pass
|
|
116
|
+
? `ok(missing ${rows.length} 条,全部盘上不存在)`
|
|
117
|
+
: `有 ${bad.length} 条 missing 指向盘上存在的档:${bad.slice(0, 3).map((r) => r.dst).join(" / ")}`
|
|
118
|
+
} finally {
|
|
119
|
+
try { db.close() } catch { /* ignore */ }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch (err) {
|
|
123
|
+
pass = false
|
|
124
|
+
detail = `抛错:${String(err?.message ?? err)}`
|
|
125
|
+
}
|
|
126
|
+
results.push({ id: item.id, pass, detail })
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const passed = results.filter((r) => r.pass).length
|
|
130
|
+
return { total: results.length, passed, failed: results.length - passed, results }
|
|
131
|
+
}
|