@thincoder/core 0.11.4 → 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 +33 -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 +22 -41
- 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,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent/planning-graph-guardrail.mjs — 规划步的**图谱护栏面**(2026-12-03 批,台账 #135 拆出)。
|
|
3
|
+
*
|
|
4
|
+
* 职责 = 谁算图谱种子、被动待发位怎么置、未读清单怎么收敛与注入。它只做本地快速查询
|
|
5
|
+
* (`getMissingReadingPack` 自称 ~5–15ms,永不抛异常),从不阻断主循环。
|
|
6
|
+
* 公开面由 `agent/planning-step.mjs` 原样再导出。拆分是行为不变的搬运。
|
|
7
|
+
*/
|
|
8
|
+
import { isAbsolute, join, relative, resolve } from "node:path"
|
|
9
|
+
|
|
10
|
+
import { pushInject } from "../context.mjs"
|
|
11
|
+
// 未读包的行渲染器单源(`read` 侧的图谱头与这里的注入共用一个实现)。`doc-graph/format.mjs` 是纯函数档,
|
|
12
|
+
// 静态闭包不达 `node:sqlite`,故可以静态引。
|
|
13
|
+
import { graphMissingRows } from "../doc-graph/format.mjs"
|
|
14
|
+
|
|
15
|
+
// 文档档判据(2026-12-03 修,台账 #135):原判据只认含 `docs/` 段的 `.md` 或 AGENTS/CLAUDE/README.md,
|
|
16
|
+
// 于是**文档根声明在 `docs/` 之外**的仓里,真需求/设计档被判成非文档 —— 实测 `v2-vibe-coding` 的文档根在
|
|
17
|
+
// `domains/…`:读 `domains/timecharter/BS-TC.md`(层标识 `[requirements]`)种子为空、通道不触发。
|
|
18
|
+
// 改按**扩展名**判,与 `tools/process-doc-mark.mjs` 的 `LAYERED_EXT`(层标识给不给标签用的就是这一条)同集。
|
|
19
|
+
// 属辖与属层交给图谱自己的声明判,不由这里的路径启发判。
|
|
20
|
+
// 残差(不静默):`changelog.md` 一类仍会算「读过的文档」(`fullReadFiles` / `hasReadDocs` 按扩展名记 ——
|
|
21
|
+
// 那两处喂理解闸门,不是图谱种子)。图谱**种子**的层标识闸在**消费点**:注入侧与规划步 Turn>0 路径各过
|
|
22
|
+
// 一次 `filterSeedsByLayer`;采集点(同步的 `noteReadFootprint`)取不到 `docLayerName`,故闸不在那里。
|
|
23
|
+
export const DOC_FILE_RE = /\.(md|mdx|markdown)$/i
|
|
24
|
+
export const CODE_FILE_RE = /\.(?:mjs|cjs|js|jsx|ts|tsx|py|rs|go|java|kt|cs|c|h|cc|cpp|hpp|vue|svelte|sh|ps1)\b/i
|
|
25
|
+
const MAX_GRAPH_REMIND_PER_PATH = 2
|
|
26
|
+
export const MAX_GRAPH_SEED_FILES = 40
|
|
27
|
+
export const GRAPH_PASSIVE_SEARCH_TOOLS = new Set(["grep", "glob", "ls", "code_search"])
|
|
28
|
+
const RESULT_CODE_PATH_RE = /^(.+?\.(?:mjs|cjs|js|jsx|ts|tsx|py|rs|go|java|kt|cs|c|h|cc|cpp|hpp|vue|svelte|sh|ps1))(?::\d+|\s|$)/i
|
|
29
|
+
|
|
30
|
+
/** 只有默认整档 read 才能解除未读状态;任何显式窗口参数都只是片段证据。 */
|
|
31
|
+
function isFullFileReadArgs(args) {
|
|
32
|
+
return args?.offset === undefined && args?.limit === undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function normalizeRelPath(rawPath, cwd) {
|
|
36
|
+
const s = String(rawPath ?? "").trim()
|
|
37
|
+
if (!s) return ""
|
|
38
|
+
const rel = (cwd && isAbsolute(s)) ? relative(cwd, s) : s
|
|
39
|
+
return rel.replace(/\\/g, "/").replace(/^\.\//, "").toLowerCase()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function resultCodePaths(name, args, resultText) {
|
|
43
|
+
const paths = []
|
|
44
|
+
for (let line of String(resultText ?? "").split("\n")) {
|
|
45
|
+
if (name === "ls") {
|
|
46
|
+
if (!/^\s*-\s{2}/.test(line)) continue
|
|
47
|
+
line = line.replace(/^\s*-\s{2}/, "")
|
|
48
|
+
} else {
|
|
49
|
+
line = line.replace(/^\s*(?:\[[^\]]+\]\s*)*/, "")
|
|
50
|
+
}
|
|
51
|
+
const match = RESULT_CODE_PATH_RE.exec(line)
|
|
52
|
+
if (!match) continue
|
|
53
|
+
let raw = match[1].trim()
|
|
54
|
+
if ((name === "glob" || name === "ls") && !isAbsolute(raw)) raw = join(String(args?.path ?? "."), raw)
|
|
55
|
+
paths.push(raw)
|
|
56
|
+
}
|
|
57
|
+
return paths
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function hasSessionFullRead(sessionFullReadFiles, normPath, rawPath, cwd) {
|
|
61
|
+
if (!(sessionFullReadFiles instanceof Set) || sessionFullReadFiles.size === 0) return false
|
|
62
|
+
if (sessionFullReadFiles.has(normPath)) return true
|
|
63
|
+
const normRaw = normalizeRelPath(rawPath, cwd)
|
|
64
|
+
for (const item of sessionFullReadFiles) {
|
|
65
|
+
if (normalizeRelPath(item, cwd) === normRaw) return true
|
|
66
|
+
}
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* `read` 成功返回后的图谱足迹(2026-12-03 批,台账 #135,需求 F5② 与验收 11)。
|
|
72
|
+
*
|
|
73
|
+
* `read` 与 grep / glob / ls / code_search **同权**进入种子采集与被动待发位 ——「读档之后、下一次模型执行前」
|
|
74
|
+
* 就带出该档的未读关联面,不必等规划步的触发节拍。两类档的未读关联面不同:源码档给所属需求/设计与 1 跳上游
|
|
75
|
+
* 与同级兄弟,文档档给配套需求/设计档与该档点名的实现源码。
|
|
76
|
+
* 两处与旧行为不同,都是有意的:① 文档档也进种子(旧行为只收源码档,于是读一份设计档什么也带不出);
|
|
77
|
+
* ② 源码档不再受旧的 `touchedCodeFiles.size < 3` 窗口节流,改与搜索路径共用 `MAX_GRAPH_SEED_FILES` 整体上限。
|
|
78
|
+
* 整档读与窗口读一律触发(窗口读正是需求 §1 所指的「碎片读码」);该档是否计入已读仍只由整档判据管。
|
|
79
|
+
* 防滚雪球 BFS 不变:护栏自己建议顺带补读的路径(`guardSuggestedPaths`)仍不反向触发。
|
|
80
|
+
*
|
|
81
|
+
* @returns `{ usedFullCodeRead, usedKeyholeRead }` —— 供调用方更新读码方式纠偏计数。
|
|
82
|
+
*/
|
|
83
|
+
export function noteReadFootprint(state, rawPath, parsed, { cwd = null, sessionFullReadFiles = null } = {}) {
|
|
84
|
+
const out = { usedFullCodeRead: false, usedKeyholeRead: false }
|
|
85
|
+
const normPath = normalizeRelPath(rawPath, cwd)
|
|
86
|
+
const isDoc = DOC_FILE_RE.test(rawPath)
|
|
87
|
+
const isCode = CODE_FILE_RE.test(rawPath)
|
|
88
|
+
if (isDoc && isFullFileReadArgs(parsed)) {
|
|
89
|
+
state.hasReadDocs = true
|
|
90
|
+
state.fullReadFiles.add(normPath)
|
|
91
|
+
}
|
|
92
|
+
if (isCode) {
|
|
93
|
+
const alreadyFull = state.fullReadFiles.has(normPath) || hasSessionFullRead(sessionFullReadFiles, normPath, rawPath, cwd)
|
|
94
|
+
if (isFullFileReadArgs(parsed) || alreadyFull) {
|
|
95
|
+
state.fullReadFiles.add(normPath)
|
|
96
|
+
state.hasReadCode = true
|
|
97
|
+
out.usedFullCodeRead = true
|
|
98
|
+
} else {
|
|
99
|
+
out.usedKeyholeRead = true
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (normPath && (isDoc || isCode) && !state.guardSuggestedPaths.has(normPath)
|
|
103
|
+
&& (state.touchedCodeFiles.has(normPath) || state.touchedCodeFiles.size < MAX_GRAPH_SEED_FILES)) {
|
|
104
|
+
const isNew = !state.touchedCodeFiles.has(normPath)
|
|
105
|
+
state.touchedCodeFiles.add(normPath)
|
|
106
|
+
if (isNew) state.graphPassivePending = true
|
|
107
|
+
}
|
|
108
|
+
return out
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function graphRawSignature(rawMissing) {
|
|
112
|
+
if (!rawMissing) return ""
|
|
113
|
+
return ["missingDocs", "missingTargets", "missingCallers", "missingSiblings"]
|
|
114
|
+
.flatMap((key) => (rawMissing[key] ?? []).map((item) => `${key}:${item?.path ?? ""}`))
|
|
115
|
+
.sort().join("\n")
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function mergedGraphFullRead(agent, state) {
|
|
119
|
+
return new Set([
|
|
120
|
+
...(state?.fullReadFiles instanceof Set ? state.fullReadFiles : []),
|
|
121
|
+
...(agent?._graphFullReadFiles instanceof Set ? agent._graphFullReadFiles : []),
|
|
122
|
+
])
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function filterGraphMissing(agent, state, rawMissing, { suppressSiblings = false } = {}) {
|
|
126
|
+
if (!rawMissing) return null
|
|
127
|
+
if (!(state.guardSuggestedPaths instanceof Set)) state.guardSuggestedPaths = new Set()
|
|
128
|
+
if (!(state.graphRemindedCounts instanceof Map)) state.graphRemindedCounts = new Map()
|
|
129
|
+
for (const item of [...(rawMissing.missingCallers ?? []), ...(rawMissing.missingSiblings ?? [])]) {
|
|
130
|
+
const key = normalizeRelPath(item?.path, agent.cwd)
|
|
131
|
+
if (key) state.guardSuggestedPaths.add(key)
|
|
132
|
+
}
|
|
133
|
+
const filterCapped = (arr = []) => arr.filter((item) => {
|
|
134
|
+
const key = normalizeRelPath(item?.path, agent.cwd)
|
|
135
|
+
return key && (state.graphRemindedCounts.get(key) ?? 0) < MAX_GRAPH_REMIND_PER_PATH
|
|
136
|
+
})
|
|
137
|
+
const filtered = {
|
|
138
|
+
missingDocs: filterCapped(rawMissing.missingDocs),
|
|
139
|
+
missingTargets: filterCapped(rawMissing.missingTargets),
|
|
140
|
+
missingCallers: filterCapped(rawMissing.missingCallers),
|
|
141
|
+
missingSiblings: suppressSiblings ? [] : filterCapped(rawMissing.missingSiblings),
|
|
142
|
+
}
|
|
143
|
+
const all = [...filtered.missingDocs, ...filtered.missingTargets, ...filtered.missingCallers, ...filtered.missingSiblings]
|
|
144
|
+
if (all.length === 0) return null
|
|
145
|
+
for (const item of all) {
|
|
146
|
+
const key = normalizeRelPath(item.path, agent.cwd)
|
|
147
|
+
state.graphRemindedCounts.set(key, (state.graphRemindedCounts.get(key) ?? 0) + 1)
|
|
148
|
+
}
|
|
149
|
+
return filtered
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Q4(2026-12-03 用户裁定):种子里的**文档档**只认文档根声明内的档 —— 判据用层标识
|
|
154
|
+
* (`tools/process-doc-mark.mjs` 的 `docLayerName`,与 `read` 输出那行层标识同源),不用路径启发。
|
|
155
|
+
* `changelog.md` 一类落 `unlayered` ⇒ 不进种子,也不带出注入。
|
|
156
|
+
*
|
|
157
|
+
* 为什么落在这里、不落进同步的 `noteReadFootprint`:`docLayerName` 的静态闭包经 `declaration.mjs`
|
|
158
|
+
* → `memory/file-walk.mjs` 达 `memory/schema.mjs`(`node:sqlite`),静态引它会破 W8 契约②
|
|
159
|
+
* (`agent/planning-step.mjs` 的静态闭包零命中 `node:sqlite`)。所以按既有纪律走**动态** `import`,
|
|
160
|
+
* 落在本来就异步、本来就在动态取图谱的这一步。取不到判据时原样返回种子(不因判不了而丢)。
|
|
161
|
+
* 两个消费点都用它:注入侧(`injectPassiveGraphReadingPack`)与规划步的 Turn>0 路径(`runPlanningStep`)。
|
|
162
|
+
*/
|
|
163
|
+
export async function filterSeedsByLayer(seedTargets, cwd) {
|
|
164
|
+
const docs = seedTargets.filter((p) => DOC_FILE_RE.test(String(p)))
|
|
165
|
+
if (docs.length === 0) return seedTargets
|
|
166
|
+
try {
|
|
167
|
+
const { docLayerName } = await import("../tools/process-doc-mark.mjs")
|
|
168
|
+
const drop = new Set()
|
|
169
|
+
for (const p of docs) {
|
|
170
|
+
const abs = isAbsolute(String(p)) ? String(p) : resolve(String(cwd ?? "."), String(p))
|
|
171
|
+
if (docLayerName(abs) === "unlayered") drop.add(p)
|
|
172
|
+
}
|
|
173
|
+
return drop.size === 0 ? seedTargets : seedTargets.filter((p) => !drop.has(p))
|
|
174
|
+
} catch {
|
|
175
|
+
return seedTargets
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 搜索工具或 `read` 返回后、下一次模型执行前确定性补出未读清单。
|
|
181
|
+
* 不经过规划模型,不阻断工具结果;相同原始清单只注入一次,图失败保持原工具链路可用。
|
|
182
|
+
*/
|
|
183
|
+
export async function injectPassiveGraphReadingPack(agent, state) {
|
|
184
|
+
if (!agent?.cwd || state?.graphPassivePending !== true) return null
|
|
185
|
+
state.graphPassivePending = false
|
|
186
|
+
const pinnedPack = Array.isArray(agent._graphReadingPack) ? agent._graphReadingPack : []
|
|
187
|
+
const seedTargets = [
|
|
188
|
+
...(state.primaryTargetFiles instanceof Set ? state.primaryTargetFiles : []),
|
|
189
|
+
...(state.touchedCodeFiles instanceof Set ? state.touchedCodeFiles : []),
|
|
190
|
+
]
|
|
191
|
+
if (seedTargets.length === 0 && pinnedPack.length === 0) return null
|
|
192
|
+
const seedsAfterLayer = await filterSeedsByLayer(seedTargets, agent.cwd)
|
|
193
|
+
// Q4:被判为 unlayered 的文档档不进种子 —— 也从**已记录的**种子里摘掉,否则这个集合与裁定不符
|
|
194
|
+
// (同步的 `noteReadFootprint` 取不到 `docLayerName`,判据只能落在这里)。`primaryTargetFiles` 是
|
|
195
|
+
// 点名目标,不在这一步动它。
|
|
196
|
+
if (state.touchedCodeFiles instanceof Set) {
|
|
197
|
+
const kept = new Set(seedsAfterLayer)
|
|
198
|
+
for (const p of [...state.touchedCodeFiles]) if (!kept.has(p)) state.touchedCodeFiles.delete(p)
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
const { getMissingReadingPack } = await import("../doc-graph.mjs")
|
|
202
|
+
const rawMissing = await getMissingReadingPack(agent.cwd, {
|
|
203
|
+
targetFiles: seedsAfterLayer,
|
|
204
|
+
fullReadFiles: mergedGraphFullRead(agent, state),
|
|
205
|
+
pinnedPack,
|
|
206
|
+
})
|
|
207
|
+
const signature = graphRawSignature(rawMissing)
|
|
208
|
+
if (!signature || signature === state.graphLastPassiveSignature) {
|
|
209
|
+
state.graphLastPassiveSignature = signature
|
|
210
|
+
return null
|
|
211
|
+
}
|
|
212
|
+
state.graphLastPassiveSignature = signature
|
|
213
|
+
const filtered = filterGraphMissing(agent, state, rawMissing, {
|
|
214
|
+
suppressSiblings: Boolean(state.mutatedCodeFiles),
|
|
215
|
+
})
|
|
216
|
+
const rows = graphMissingRows(filtered)
|
|
217
|
+
if (rows.length === 0) return null
|
|
218
|
+
const content = `[System reminder: 图谱被动上下文(搜索或读档后、后续修改前):以下高关联文件尚未整档 read。先用 read 默认整档读取,再修改;图谱注记不等于已读全文。\n${rows.join("\n")}]`
|
|
219
|
+
pushInject(agent, { role: "user", content })
|
|
220
|
+
return content
|
|
221
|
+
} catch {
|
|
222
|
+
return null
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent/planning-prompt.mjs — 规划步的**文案面**(2026-12-03 批,台账 #135 拆出)。
|
|
3
|
+
*
|
|
4
|
+
* 本档只负责把规划步的触发事实渲染成尾部规划指令消息,以及把规划结论截到有界长度;
|
|
5
|
+
* 状态追踪、触发判定与执行仍住 `agent/planning-step.mjs`。公开面由该档原样再导出。
|
|
6
|
+
* 拆分是行为不变的搬运:判据 = 既有常驻集成用例 + 本批单测 + 逐字节文案金标(54 例参数矩阵)。
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from "node:fs"
|
|
9
|
+
import { join } from "node:path"
|
|
10
|
+
|
|
11
|
+
import { PROMPTS_DIR } from "../prompt-files.mjs"
|
|
12
|
+
import { graphMissingRows } from "../doc-graph/format.mjs"
|
|
13
|
+
import { buildPlanningTaskHint } from "./task-lifecycle.mjs"
|
|
14
|
+
|
|
15
|
+
/** B 段注入规划结论的最大字符数(以及空正文回退原始思考流时的尾部截断上限,防止数千字草稿灌爆下轮上下文)。 */
|
|
16
|
+
export const PLAN_INJECT_MAX_CHARS = 1200
|
|
17
|
+
export const PLAN_REASONING_FALLBACK_MAX_CHARS = 900
|
|
18
|
+
|
|
19
|
+
/** 单回合执行满该工具轮数后,规划步追加显式偏航核对提醒(防陷入支线技术细节忘掉主任务);之后每隔若干轮再点一次,不每轮刷屏。 */
|
|
20
|
+
export const PLAN_DRIFT_WARN_TURN = 8
|
|
21
|
+
export const PLAN_DRIFT_WARN_STRIDE = 12
|
|
22
|
+
|
|
23
|
+
/** 将用户本回合原始诉求压成单行主任务锚点(用于长回合中后段与异步消化轮防偏航)。 */
|
|
24
|
+
export function summarizeGoalForAnchor(input, maxChars = 160) {
|
|
25
|
+
const clean = typeof input === "string" ? input.replace(/\s+/g, " ").trim() : ""
|
|
26
|
+
if (!clean) return ""
|
|
27
|
+
return clean.length <= maxChars ? clean : `${clean.slice(0, maxChars - 1)}…`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const TRIGGER_LABELS = Object.freeze({
|
|
31
|
+
start: "开局规划",
|
|
32
|
+
advisor: "评审结论核实规划",
|
|
33
|
+
engDesigner: "设计席交付后复查",
|
|
34
|
+
milestone: "阶段结果复盘",
|
|
35
|
+
error: "连续报错换向复盘",
|
|
36
|
+
probe: "连续排障探测复盘",
|
|
37
|
+
compaction: "上下文压缩后重定锚",
|
|
38
|
+
interval: "长循环防跑偏复盘",
|
|
39
|
+
queuedInput: "用户插话后重定锚",
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const TRIGGER_REASONS = Object.freeze({
|
|
43
|
+
start: "先拆清用户的真正目标与隐含约束:先用 `repo_graph` 定位并拿齐归属需求/设计档与上下游清单;要改代码或设计时再 `read` 完整读入目标源码后动笔;涉及外部软硬件/框架/模型问题首轮即并行上网查官方文档与社区 issue。",
|
|
44
|
+
timerWake: "定时唤醒:先对照用户主目标核对最新状态与是否达到决策条件;任务仍在进行且无其他独立可并行动作时,继续设非阻塞 `timer` 等待。",
|
|
45
|
+
advisor: "刚收到 advisor 评审结论:动手前先把每条高优先级发现映射到具体 `file:line` / 权威文档 / 实跑命令逐条核实,区分真实缺陷、误判与越界,不照单全收也不盲从返工。",
|
|
46
|
+
engDesigner: "刚收到设计席(eng-designer)交付:实现前先审视设计有无过度设计(多余抽象层、新档、门禁、开关),能削薄的先减再落地。",
|
|
47
|
+
milestone: "刚收回子代理/后台任务/分叉调研结果:对照主目标与实盘核验关键结论后再定下一步。",
|
|
48
|
+
error: "同一方向已连续失败:先从报错原文判断是前提、状态还是用法问题,需要时用 `repo_graph` 的 `context` 动作核对文档约束,或上网查该报错的一手文档与已知 bug,再换路径。",
|
|
49
|
+
probe: "已连续多轮跑探针仍未定根因:跳出命令链,用 `repo_graph` 的 `context` 动作核对项目约定、上网查已知 issue,并设计能区分竞争假设的对照实验。",
|
|
50
|
+
compaction: "上下文刚压缩:重新对齐用户原始目标、已证伪的死路与尚未核查的文档,再定下一步。",
|
|
51
|
+
interval: "本回合已连续多轮工具:对照原始任务检查是否被支线带偏,只补直接推进核心交付的关键缺口。",
|
|
52
|
+
queuedInput: "用户插话后重定锚:逐字读最新用户消息,判断是补充、修正还是改道——补充或修正并入主线,改道则按新诉求重排。",
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
let _cachedChecklist = null
|
|
56
|
+
/** 读取 `prompts/planning-next-moves.md` 检查单(允许单测传入覆写路径)。 */
|
|
57
|
+
export function loadPlanningChecklist(filePath = join(PROMPTS_DIR, "planning-next-moves.md")) {
|
|
58
|
+
if (filePath === join(PROMPTS_DIR, "planning-next-moves.md") && _cachedChecklist !== null) return _cachedChecklist
|
|
59
|
+
const text = readFileSync(filePath, "utf8").trim()
|
|
60
|
+
if (filePath === join(PROMPTS_DIR, "planning-next-moves.md")) _cachedChecklist = text
|
|
61
|
+
return text
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function describeAdvisorScope(types = []) {
|
|
65
|
+
const list = Array.isArray(types) ? types.filter((t) => t === "design" || t === "code") : []
|
|
66
|
+
const hasDesign = list.includes("design")
|
|
67
|
+
const hasCode = list.includes("code")
|
|
68
|
+
if (hasDesign && !hasCode) return { label: "文档/设计评审结论核实规划", scope: "文档/设计评审(advisor type=design)" }
|
|
69
|
+
if (hasCode && !hasDesign) return { label: "代码评审结论核实规划", scope: "代码评审(advisor type=code)" }
|
|
70
|
+
return { label: TRIGGER_LABELS.advisor, scope: "文档/代码评审(advisor)" }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 规划轮工具分工速查(一行一工具,只给选型判据)。
|
|
75
|
+
* 规划轮禁止调工具,模型不会去读 schema 里的描述全文;但选型又必须看得到分工,
|
|
76
|
+
* 所以只给一句判据。全文仍在工具 schema 里,不重复。只列实际装配了的工具。
|
|
77
|
+
*/
|
|
78
|
+
const TOOL_ROUTING = [
|
|
79
|
+
["repo_graph", "结构/归属/定位/上下游/影响面——先手;可全工作区查,不必先知道在哪个子项目"],
|
|
80
|
+
["read", "整档读入"],
|
|
81
|
+
["grep", "字面词出现行"],
|
|
82
|
+
["glob", "按文件名找路径"],
|
|
83
|
+
["code_search", "语义找代码"],
|
|
84
|
+
["doc_search", "找设计意图"],
|
|
85
|
+
["manifest", "项目状态与文档根"],
|
|
86
|
+
["question", "问用户"],
|
|
87
|
+
["execute", "跑 JS/测试"],
|
|
88
|
+
["bash", "跑 CLI"],
|
|
89
|
+
["websearch", "查外网"],
|
|
90
|
+
["advisor", "独立评审"],
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
export function buildToolRouting(toolSchemas) {
|
|
94
|
+
const names = new Set(
|
|
95
|
+
(Array.isArray(toolSchemas) ? toolSchemas : [])
|
|
96
|
+
.map((s) => (s?.function ?? s)?.name)
|
|
97
|
+
.filter((n) => typeof n === "string" && n),
|
|
98
|
+
)
|
|
99
|
+
if (names.size === 0) return ""
|
|
100
|
+
const parts = TOOL_ROUTING.filter(([n]) => names.has(n)).map(([n, d]) => `\`${n}\` ${d}`)
|
|
101
|
+
return parts.length ? `工具分工:${parts.join(";")}。` : ""
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 构造规划步尾部指令消息(挂在现有对话前缀之后,保前缀缓存)。 */
|
|
105
|
+
export function buildPlanningPromptMessage({
|
|
106
|
+
trigger, searched, preferredSearch, canAskUser = false, canSyncTask = false, pendingTimerCount = 0,
|
|
107
|
+
goal = "", turn = 0, tasks = [], turnsSinceTaskUpdate = 0,
|
|
108
|
+
advisorReviewTypes = [], asyncDeliveredRoles = [], autoTurn = false, timerTurn = false,
|
|
109
|
+
bashCodeReadCount = 0, keyholeReadCount = 0,
|
|
110
|
+
hasReadDocs = false, hasReadCode = false, hasMutatedFiles = false, mutatedCodeFiles = false, usedSsh = false,
|
|
111
|
+
graphMissing = null, checklist, shortCadence = false, driftWarnDue = null, toolRouting = "",
|
|
112
|
+
}) {
|
|
113
|
+
const reason = (trigger === "interval" && shortCadence)
|
|
114
|
+
? "先对照主任务检查自己是在推进核心交付还是被支线细节吸引走;只看自上次规划之后这 1–2 轮刚返回的新结果;非阻塞旁枝只记备忘,不偏离主目标。"
|
|
115
|
+
: (trigger === "start" && timerTurn)
|
|
116
|
+
? TRIGGER_REASONS.timerWake
|
|
117
|
+
: (TRIGGER_REASONS[trigger] ?? TRIGGER_REASONS.interval)
|
|
118
|
+
const cleanGoal = summarizeGoalForAnchor(goal)
|
|
119
|
+
const showDriftWarn = driftWarnDue === null ? turn >= PLAN_DRIFT_WARN_TURN : Boolean(driftWarnDue)
|
|
120
|
+
const goalHint = cleanGoal && (turn > 0 || autoTurn)
|
|
121
|
+
? (turn > 0
|
|
122
|
+
? `\n主任务锚点(本回合已执行 ${turn} 轮工具):「${cleanGoal}」${showDriftWarn ? "——已运行较多轮,请优先核对是否仍停留在基线复核或支线技术细节里而尚未推进核心交付;非阻塞旁枝只记备忘,立刻回到主线,或用 `question` 让用户决定是否改道。" : ""}`
|
|
123
|
+
: `\n主任务锚点(${timerTurn ? "定时唤醒轮" : "异步消化轮"}):「${cleanGoal}」——请把刚到达的异步结果/唤醒信号放回该主任务下判断下一步。`)
|
|
124
|
+
: ""
|
|
125
|
+
const advisorHint = trigger === "advisor"
|
|
126
|
+
? `\n评审核实专项(${describeAdvisorScope(advisorReviewTypes).scope}):动手前列出每条关键评审意见的核查动作(\`read\`/\`grep\`/\`repo_graph\` 或实跑命令核对被指代码行、文档与真实行为),逐条判清「成立 / 待核 / 误判或越界」再修改或回复评审表。`
|
|
127
|
+
: ""
|
|
128
|
+
const engDesignerHint = trigger === "engDesigner"
|
|
129
|
+
? "\n防过度设计专项(eng-designer 交付):实现前先核查这份设计有无过度设计(多余抽象层、重复文档、可省的门禁或配置开关),写清哪里可以削薄后再推进。"
|
|
130
|
+
: ""
|
|
131
|
+
const asyncScopeHint = trigger === "milestone" && Array.isArray(asyncDeliveredRoles) && asyncDeliveredRoles.length > 0
|
|
132
|
+
? `\n异步消化来源:刚收到后台任务结果(${asyncDeliveredRoles.join(" / ")})——先核对其中与主目标直接相关的关键发现、报错或未闭环项,再定下一步动作。`
|
|
133
|
+
: ""
|
|
134
|
+
const taskHint = buildPlanningTaskHint(tasks, turnsSinceTaskUpdate, { trigger, canSyncTask })
|
|
135
|
+
const searchHint = !searched && preferredSearch
|
|
136
|
+
? `\n联网查一手:外部系统/框架/引擎异常或排障,或对用法/行为/配置没把握、准备断言其行为时,下一步直接调用 \`${preferredSearch}\` 并给出带组件名/版本/症状的具体查询词。`
|
|
137
|
+
: ""
|
|
138
|
+
const askHint = canAskUser
|
|
139
|
+
? "\n`question` 可用:缺关键业务约束或遇互斥路线取舍/高耗时支线改道时,下一步直接调 `question` 一次表单问清。"
|
|
140
|
+
: ""
|
|
141
|
+
const timerHint = pendingTimerCount > 0
|
|
142
|
+
? `\n当前已有 ${pendingTimerCount} 个在途 \`timer\` 定时唤醒:等待长任务结果时直接以等待态文字收尾让定时器唤醒。`
|
|
143
|
+
: ""
|
|
144
|
+
const codeReadHint = (bashCodeReadCount > 0 || keyholeReadCount > 0)
|
|
145
|
+
? `\n读码方式纠偏:刚在靠碎片读源码(${[bashCodeReadCount > 0 ? `\`bash\` 切片 ${bashCodeReadCount} 轮` : "", keyholeReadCount > 0 ? `\`read\` 小窗口 ${keyholeReadCount} 轮` : ""].filter(Boolean).join("、")})——先用 \`repo_graph\` 的 \`context\` 动作拿齐该读清单,再用 \`read\` 一次读入完整文件。`
|
|
146
|
+
: ""
|
|
147
|
+
let readinessHint = ""
|
|
148
|
+
if (turn > 0 && trigger !== "compaction") {
|
|
149
|
+
if (mutatedCodeFiles && !hasReadCode) {
|
|
150
|
+
readinessHint = "\n⚠️ 未完整读码即动笔预警:已开始改源码但尚未整档读过任何代码文件——先用 `read` 补读目标源码全文与上下游,再核对刚写下的改动。"
|
|
151
|
+
} else if (mutatedCodeFiles && !hasReadDocs) {
|
|
152
|
+
readinessHint = "\n⚠️ 未核原有文档即改源码提醒:已开始改源码但尚未读项目需求/设计档——补读拥有该主题的文档,确认改动未违背原有设计并同轮同步。"
|
|
153
|
+
} else if (!hasMutatedFiles && !(usedSsh && !hasReadDocs && !hasReadCode)) {
|
|
154
|
+
if (!hasReadDocs && !hasReadCode) {
|
|
155
|
+
readinessHint = "\n动笔前理解闸门(尚未读原有文档或完整源码):改代码或设计前,先用 `repo_graph` 的 `context` 动作拿齐①需求/设计档、②目标源码、③同类上下游三面清单,再逐一整档 `read`。"
|
|
156
|
+
} else if (!hasReadDocs && hasReadCode) {
|
|
157
|
+
readinessHint = "\n动笔前理解闸门(已读码,尚未读原有需求/设计档):先用 `repo_graph` 的 `context` 动作定位拥有该主题的文档,再 `read` 整档。"
|
|
158
|
+
} else if (hasReadDocs && !hasReadCode) {
|
|
159
|
+
readinessHint = "\n动笔前理解闸门(已读文档,尚未整档读入源码):先用 `repo_graph` 的 `context` 动作拿齐目标源码与同类上下游清单,再逐一 `read` 完整读入。"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
let graphHint = ""
|
|
164
|
+
const graphRows = graphMissingRows(graphMissing)
|
|
165
|
+
if (graphRows.length > 0) graphHint = `\n图谱必读核对(以下高关联文件本回合尚未整档 read):\n${graphRows.join("\n")}`
|
|
166
|
+
const toolHint = toolRouting ? `\n\n${toolRouting}` : ""
|
|
167
|
+
return `[Planning next moves — trigger: ${trigger}]\n场景提示:${reason}${goalHint}${advisorHint}${engDesignerHint}${asyncScopeHint}${codeReadHint}${readinessHint}${graphHint}${taskHint}${searchHint}${askHint}${timerHint}${toolHint}\n\n${checklist}`
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 截断注入 B 段的规划文本,防止过长规划或数千字空正文回退思考灌爆下一轮上下文。 */
|
|
171
|
+
export function capPlanTextForInject(text, { fromReasoning = false, maxChars = PLAN_INJECT_MAX_CHARS } = {}) {
|
|
172
|
+
const clean = typeof text === "string" ? text.trim() : ""
|
|
173
|
+
if (!clean) return ""
|
|
174
|
+
const limit = fromReasoning ? Math.min(maxChars, PLAN_REASONING_FALLBACK_MAX_CHARS) : maxChars
|
|
175
|
+
if (clean.length <= limit) return clean
|
|
176
|
+
return fromReasoning
|
|
177
|
+
? `…(已截断前段思考)\n${clean.slice(-limit)}`
|
|
178
|
+
: `${clean.slice(0, limit)}\n…(已截断)`
|
|
179
|
+
}
|