@thincoder/core 0.10.7 → 0.10.8
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 +15 -0
- package/advisor/compaction.mjs +1 -1
- package/advisor/run.mjs +1 -1
- package/agent/chat-call.mjs +40 -27
- package/agent/child-marks.mjs +1 -1
- package/agent/dispatch-gates.mjs +7 -4
- package/agent/dispatch-run.mjs +1 -0
- package/agent/family-tools.mjs +11 -6
- package/agent/helpers.mjs +6 -4
- package/agent/image-heal.mjs +65 -0
- package/agent/light-round.mjs +4 -4
- package/agent/live-beat.mjs +4 -4
- package/agent/record-results.mjs +9 -1
- package/agent/run-stages.mjs +2 -1
- package/agent/setup-reminders.mjs +14 -13
- package/agent/setup.mjs +62 -46
- package/agent/spawn-child.mjs +1 -1
- package/agent/subagent-event.mjs +1 -1
- package/agent/suspension.mjs +2 -1
- package/agent/timers.mjs +8 -8
- package/agent/turn-loop.mjs +1 -1
- package/agent-tools/advisor.mjs +1 -1
- package/agent-tools/async-discard.mjs +40 -5
- package/agent-tools/async-settle.mjs +3 -1
- package/agent-tools/audit-block.mjs +1 -1
- package/agent-tools/batch-paths.mjs +20 -20
- package/agent-tools/checkpoint.mjs +1 -1
- package/agent-tools/design-slots.mjs +44 -5
- package/agent-tools/doc-check.mjs +37 -0
- package/agent-tools/eng.mjs +5 -3
- package/agent-tools/manifest.mjs +18 -18
- package/agent-tools/parent-channel.mjs +3 -3
- package/agent-tools/review-facts.mjs +2 -2
- package/agent-tools/settings.mjs +1 -1
- package/agent-tools/spawn-gates.mjs +8 -6
- package/agent-tools/ssh-session.mjs +439 -0
- package/agent-tools/ssh-transport.mjs +311 -0
- package/agent-tools/subagent-scheduler.mjs +6 -5
- package/agent-tools/subagent.mjs +7 -5
- package/agent-tools/task.mjs +1 -1
- package/agent-tools/timer.mjs +6 -0
- package/agent-tools/zero-write-watch.mjs +2 -2
- package/agent-tools.mjs +3 -2
- package/attachments.mjs +4 -4
- package/browser/cdp.mjs +2 -2
- package/browser/launch.mjs +50 -4
- package/browser/snapshot.mjs +1 -1
- package/config-io.mjs +7 -2
- package/config-watch.mjs +2 -2
- package/config.mjs +7 -0
- package/declaration.mjs +5 -1
- package/doc-check/anchors.mjs +100 -0
- package/doc-check/index.mjs +61 -0
- package/doc-check/width.mjs +131 -0
- package/file-refs.mjs +6 -6
- package/generate-title.mjs +1 -1
- package/hooks.mjs +1 -1
- package/i18n.mjs +11 -1
- package/ledger-cmd.mjs +4 -5
- package/ledger-db.mjs +100 -21
- package/ledger-migrate.mjs +3 -3
- package/ledger-read.mjs +6 -7
- package/ledger-tools.mjs +24 -9
- package/ledger.mjs +23 -54
- package/log.mjs +17 -3
- package/manifest-discovery.mjs +21 -18
- package/manifest-schema.mjs +2 -13
- package/manifest.mjs +37 -22
- package/mcp/transport-stdio.mjs +1 -1
- package/mcp.mjs +2 -2
- package/memory/core.mjs +1 -1
- package/memory/scan.mjs +1 -1
- package/memory/sweep.mjs +1 -1
- package/model-specs.mjs +7 -7
- package/notify-policy.mjs +6 -6
- package/package.json +2 -1
- package/permission.mjs +14 -0
- package/prompts/advisor-design.md +43 -13
- package/prompts/advisor-round1.md +25 -5
- package/prompts/advisor-round2.md +17 -4
- package/prompts/advisor-round3.md +17 -4
- package/prompts/common.md +60 -6
- package/prompts/consult-base.md +8 -2
- package/prompts/discipline-engineering.md +11 -10
- package/prompts/discipline-normal.md +22 -14
- package/prompts/persona-coder.md +1 -1
- package/prompts/persona-eng-designer.md +8 -3
- package/prompts/persona-engineering.md +21 -5
- package/prompts/persona-normal.md +4 -1
- package/provider/errors.mjs +8 -0
- package/provider/google.mjs +3 -0
- package/provider/normalize.mjs +42 -12
- package/provider/responses-request.mjs +1 -1
- package/provider/responses.mjs +2 -2
- package/provider/wait-status.mjs +1 -1
- package/provider-flows.mjs +1 -1
- package/queued.mjs +2 -2
- package/rules.mjs +4 -4
- package/session-gc.mjs +1 -1
- package/session-lifecycle.mjs +1 -1
- package/session-slot-write.mjs +2 -2
- package/session-slots-manifest.mjs +1 -1
- package/session.mjs +1 -1
- package/shell-candidates.mjs +2 -2
- package/shell-identity.mjs +1 -1
- package/team.mjs +61 -4
- package/think-off.mjs +3 -3
- package/token-ttl.mjs +1 -1
- package/tool-docs/bash.md +1 -1
- package/tool-docs/browser.md +1 -1
- package/tool-docs/doc_check.md +31 -0
- package/tool-docs/manifest.md +14 -2
- package/tool-docs/ssh.md +34 -0
- package/tool-docs/subagent.md +2 -0
- package/tool-docs/timer.md +2 -1
- package/tools/browser.mjs +1 -1
- package/tools/execute.mjs +1 -1
- package/tools/git-run.mjs +2 -2
- package/tools/git.mjs +8 -8
- package/tools/index.mjs +3 -0
- package/tools/ops.mjs +1 -1
- package/tools/process-tree.mjs +2 -2
- package/tools/ssh-askpass.mjs +35 -0
- package/tools/ssh.mjs +92 -0
- package/vision-reader.mjs +4 -4
package/config-watch.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* config-watch.mjs — config.json 外部写盘感知·**纯逻辑面**(R8 · 桌面功能对位批「上提」KD-T2——
|
|
3
|
-
* 自 `thincoder-vscode/src/extension/config-watch.mjs
|
|
3
|
+
* 自 `thincoder-vscode/src/extension/config-watch.mjs` 的 `startConfigWatch` 逐字随迁:去抖 300ms ∕ stat 元组
|
|
4
4
|
* (`mtimeMs:size`)比对 ∕ 自写抑制;原批 = 第 21 批 B5 · `docs/design/SETTINGS.md` §2.6)。
|
|
5
5
|
*
|
|
6
6
|
* `~/.thincoder/config.json` 为多端共享单文件:CLI(`/advisor`、`settings set`)或手工编辑写盘后,
|
|
7
7
|
* 常开面板原本零感知(只在打开面板/保存时拉快照)。机制(零语义改):
|
|
8
8
|
* 宿主 fs 事件 →(端落子)`onEvent` → 去抖 → 元组比对 → 变化才 `onChange`(事件驱动零空转)。
|
|
9
9
|
* 自写抑制:本端自身写盘同样改元组 ⇒ 裸推送会在用户编辑中被快照重建;写盘成功后核 `config-io`
|
|
10
|
-
* `saveRaw` 同步回调 `onConfigSelfWrite`(`config-io.mjs
|
|
10
|
+
* `saveRaw` 同步回调 `onConfigSelfWrite`(`config-io.mjs`)⇒ `noteSelfWrite()` 把基线刷到当前
|
|
11
11
|
* 元组 ⇒ 事件到达时元组已等于基线 ⇒ 零推送;外部写无回调 ⇒ 元组异于基线 ⇒ 推送。
|
|
12
12
|
*
|
|
13
13
|
* 平台面两注入(核内零宿主绑定):① `attach`(fs 事件源工厂——端落子:VSC = `createFileSystemWatcher`、
|
package/config.mjs
CHANGED
|
@@ -64,6 +64,13 @@ export const DEFAULTS = {
|
|
|
64
64
|
// §6.30.3 门三件③(D-TW5):timer 空闲自唤醒开关——默认开(工具描述面已承诺到点提醒);
|
|
65
65
|
// false ⇒ 端侧不武装闩(回既有回合边界投递——零调度器面)。键面随 DEFAULTS 派生进 settings 类型表。
|
|
66
66
|
timerWake: true,
|
|
67
|
+
// 注入开关(PROMPT-SYSTEM.md §6.24 · 台账 #1420):文档类 ∥ 记忆类自动注入——默认开;
|
|
68
|
+
// 判据 `!== false`(缺省 = 现行为零变)。消费点 = 核 `agent/setup.mjs` 判点(逐 run 现读)。
|
|
69
|
+
// 2026-10-15(§6.27 · 台账 #1465):① 项目指令块独立成键——`injectDocs` 收窄为只管 ② 相关文档块;
|
|
70
|
+
// 两键完全正交(关任一方另一方照常)。`injectProjectRules` 判点 = 核 `agent/setup.mjs`(`prepareRun` 项目指令段)。
|
|
71
|
+
injectDocs: true,
|
|
72
|
+
injectProjectRules: true,
|
|
73
|
+
injectMemories: true,
|
|
67
74
|
},
|
|
68
75
|
memory: {
|
|
69
76
|
dbPath: join(configDir, "memory.db"),
|
package/declaration.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { existsSync, readdirSync, statSync } from "node:fs"
|
|
9
9
|
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path"
|
|
10
|
-
import { DEFAULT_MANIFEST, MANIFEST_REL, docRootPaths, manifestFilePath, owningProject, readManifest } from "./manifest.mjs"
|
|
10
|
+
import { DEFAULT_MANIFEST, MANIFEST_REL, docRootPaths, manifestFilePath, onManifestInit, owningProject, readManifest } from "./manifest.mjs"
|
|
11
11
|
import { isSkippedRelPath } from "./memory/file-walk.mjs" // 面⑦ 子目录扫描的跳过段谓词单源(零环:file-walk 不引本档)
|
|
12
12
|
import { logEvent } from "./log.mjs"
|
|
13
13
|
|
|
@@ -311,6 +311,10 @@ export function clearDeclarationCache() {
|
|
|
311
311
|
_scopeCache.clear()
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
+
// #1376:eng 建档路(`manifest.mjs` `resolveEngineeringManifest` init 支)成功 ⇒ 同清两缓存——
|
|
315
|
+
// 反向注册(本档已向依赖 manifest.mjs,其侧零出边保持——KD-M1-33);失败路径不广播、文件面零变。
|
|
316
|
+
onManifestInit(clearDeclarationCache)
|
|
317
|
+
|
|
314
318
|
/** Retired-carrier warning (existence check only — zero content parsing, zero fallback). */
|
|
315
319
|
function warnRetiredCarrier(root) {
|
|
316
320
|
console.warn(`[declaration] ${RETIRED_REL_PATH} is retired and no longer read — move codePaths / index.*Extensions / advisor.{docMap,standardsDoc} into PROJECT-MANIFEST.json`)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doc-check/anchors.mjs — 文档纪律机检 · 起步判据 ①(锚):指针抽取 + 解析序 + 节在盘判定。
|
|
3
|
+
*
|
|
4
|
+
* 判据句单源 = `docs/core/design/DOC-DISCIPLINE.md` §3.20 判据 ①(本节只实现,不重述口径):
|
|
5
|
+
* · 文档指针 = 档 token(含 `.md`,可带目录段)± 节指示(紧随 `§` + 编号 N / N.M / N.M.K);
|
|
6
|
+
* **行号坐标(`:N`)不作节指示**——行号不作引用;
|
|
7
|
+
* · 档不在盘 ⇒ `档缺`;带节指示 ∧ 节不在盘 ⇒ `节缺`。
|
|
8
|
+
*
|
|
9
|
+
* 抽取面:逐行扫;fenced 块整块跳过;可执行行(`isExecutableLine`)整行跳过(命令保字面)。
|
|
10
|
+
* 解析序:① 仓根相对 → ② 域根相对(`scanDirs` 逐根)→ ③ 引用方档所在目录相对 →
|
|
11
|
+
* ④ 域内唯一 basename 索引;同名 ≥2 ⇒ **不判**(列报告面)。
|
|
12
|
+
* 节在盘:目标档内存在标题行,其标题编号(标题文本去 `^#{1,6}\s` 后的首个 `\d+(?:\.\d+)*`)= 指针节号。
|
|
13
|
+
* 残余登记(fail-open 方向——归评审核验面):裸档名在域内多义 ⇒ 不判;未闭合围栏 ⇒ 至档尾免;
|
|
14
|
+
* 指针行的坐标尾不入判、同行的档 token 照判。
|
|
15
|
+
*/
|
|
16
|
+
import { readFileSync, statSync } from "node:fs"
|
|
17
|
+
import { basename, dirname, resolve } from "node:path"
|
|
18
|
+
import { fenceMark, isExecutableLine } from "./width.mjs"
|
|
19
|
+
|
|
20
|
+
/** 文档指针 token:`(目录段/)*名.md` + 可选行号坐标尾 + 可选节指示(`§` + 编号)。
|
|
21
|
+
* 左界守卫排除更长 token 的尾段;右界排除后续字母数字(`.md` 之后紧邻 `[\w]` ⇒ 非完整 token)。 */
|
|
22
|
+
const ANCHOR_RE = /(?<![A-Za-z0-9_.\-\\/@])((?:[A-Za-z0-9_.\-]+\/)*[A-Za-z0-9_.\-]+\.md)(?::\d+(?:-\d+)?)?(?:\s*§\s*(\d+(?:\.\d+)*))?(?![\w])/g
|
|
23
|
+
|
|
24
|
+
/** 文件存在性(目录 / 读错 ⇒ false)。 */
|
|
25
|
+
function isFile(p) { try { return statSync(p).isFile() } catch { return false } }
|
|
26
|
+
|
|
27
|
+
/** 标题编号(标题文本去 `^#{1,6}\s` 后的首个 `\d+(?:\.\d+)*`);非标题行 ⇒ null。 */
|
|
28
|
+
function headingNumber(line) {
|
|
29
|
+
const m = /^#{1,6}\s+/.exec(line)
|
|
30
|
+
if (!m) return null
|
|
31
|
+
const n = /\d+(?:\.\d+)*/.exec(line.slice(m[0].length))
|
|
32
|
+
return n ? n[0] : null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 节在盘:目标档内任一标题行的编号 = 指针节号(字符串逐字等值——两处同归一形)。 */
|
|
36
|
+
function sectionOnDisk(path, num) {
|
|
37
|
+
let text = ""
|
|
38
|
+
try { text = readFileSync(path, "utf8") } catch { return false }
|
|
39
|
+
for (const l of text.split("\n")) if (headingNumber(l) === num) return true
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 档解析(判据 ① 的解析序 ①–④):命中 ⇒ `{ ok, path }`;同名 ≥2 ⇒ `{ ambiguous, candidates }`;零命中 ⇒ `{ ok:false }`。
|
|
44
|
+
* 行号坐标尾(`:N`)不参与档解析——token 只取 `.md` 段(抽取面已剥离坐标尾与节指示)。 */
|
|
45
|
+
function resolveDoc(token, refFile, base, scanDirs, index) {
|
|
46
|
+
const p1 = resolve(base, token)
|
|
47
|
+
if (isFile(p1)) return { ok: true, path: p1 } // ① 仓根相对
|
|
48
|
+
for (const d of scanDirs) { const p = resolve(base, d, token); if (isFile(p)) return { ok: true, path: p } } // ② 域根相对
|
|
49
|
+
const p3 = resolve(dirname(refFile), token)
|
|
50
|
+
if (isFile(p3)) return { ok: true, path: p3 } // ③ 引用方档所在目录相对
|
|
51
|
+
const cands = index.get(basename(token)) ?? [] // ④ 域内唯一 basename 索引
|
|
52
|
+
if (cands.length === 1) return { ok: true, path: cands[0] }
|
|
53
|
+
if (cands.length > 1) return { ambiguous: true, candidates: cands }
|
|
54
|
+
return { ok: false }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 锚判据主行程:域内逐档逐行抽取 + 解析 + 节在盘判定。
|
|
59
|
+
* @param {string} root 项目根(判据参数已由调用方自 `checkConfig` 读出)
|
|
60
|
+
* @param {{scanDirs?:string[], files?:string[]}} o `files` = 域内 `.md` 全集(单源 = `scanDomain`)
|
|
61
|
+
* @returns {{hits:Array, reports:Array}} hits = 命中(`档缺` / `节缺`);reports = 报告面(多义——不作判)
|
|
62
|
+
*/
|
|
63
|
+
export function checkAnchors(root, { scanDirs = [], files = [] } = {}) {
|
|
64
|
+
const base = resolve(root)
|
|
65
|
+
const dirs = Array.isArray(scanDirs) ? scanDirs : []
|
|
66
|
+
const index = new Map()
|
|
67
|
+
for (const f of files) {
|
|
68
|
+
const k = basename(f)
|
|
69
|
+
if (!index.has(k)) index.set(k, [])
|
|
70
|
+
index.get(k).push(f)
|
|
71
|
+
}
|
|
72
|
+
const hits = []
|
|
73
|
+
const reports = []
|
|
74
|
+
for (const f of files) {
|
|
75
|
+
let text = ""
|
|
76
|
+
try { text = readFileSync(f, "utf8") } catch { continue }
|
|
77
|
+
const re = new RegExp(ANCHOR_RE.source, "g") // 每档一份实例(零共享 lastIndex 状态)
|
|
78
|
+
let fence = null // 围栏标记(` ∥ ~);null = 不在块内
|
|
79
|
+
text.split("\n").forEach((l, i) => {
|
|
80
|
+
const mark = fenceMark(l)
|
|
81
|
+
if (mark !== null) { // 围栏块整块跳过:起于围栏行、止于同形围栏行(未闭合 ⇒ 至档尾免)
|
|
82
|
+
if (fence === null) fence = mark
|
|
83
|
+
else if (mark === fence) fence = null
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
if (fence !== null) return
|
|
87
|
+
if (isExecutableLine(l)) return // 可执行行整行跳过
|
|
88
|
+
let m
|
|
89
|
+
while ((m = re.exec(l)) !== null) {
|
|
90
|
+
const token = m[1]
|
|
91
|
+
const num = m[2] ?? null
|
|
92
|
+
const r = resolveDoc(token, f, base, dirs, index)
|
|
93
|
+
if (r.ambiguous) { reports.push({ file: f, line: i + 1, token, candidates: r.candidates }); continue }
|
|
94
|
+
if (!r.ok) { hits.push({ file: f, line: i + 1, token, clazz: "档缺" }); continue }
|
|
95
|
+
if (num !== null && !sectionOnDisk(r.path, num)) hits.push({ file: f, line: i + 1, token, clazz: "节缺", section: num, target: r.path })
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
return { hits, reports }
|
|
100
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doc-check/index.mjs — 文档纪律机检 · 引擎入口(域扫描 + 声明面读取 + 报面行)。
|
|
3
|
+
*
|
|
4
|
+
* 判据两条(起步 · 闭集——口径单源 = `docs/core/design/DOC-DISCIPLINE.md` §3.20 条目 S):
|
|
5
|
+
* ① 锚(档缺 / 节缺)——实现 = `./anchors.mjs`;② 行宽——口径 = §3.18 条目 Q,实现 = `./width.mjs`。
|
|
6
|
+
* 判据参数由**被开发项目**的 `PROJECT-MANIFEST.json` `checkConfig` 声明(不硬编码任何本仓路径)。
|
|
7
|
+
* 纯函数:**零门禁 / 零退出码 / 零阻断**——报告态由工具面 `agent-tools/doc-check.mjs` 呈现;
|
|
8
|
+
* 输出不得被读作拒执行的理由(用户令 > 机检)。
|
|
9
|
+
*/
|
|
10
|
+
import { relative, resolve } from "node:path"
|
|
11
|
+
import { MANIFEST_REL, readManifest } from "../manifest.mjs"
|
|
12
|
+
import { scanDomain, checkDocWidths } from "./width.mjs"
|
|
13
|
+
import { checkAnchors } from "./anchors.mjs"
|
|
14
|
+
|
|
15
|
+
/** 相对路径显示形(正斜杠——跨平台同形)。 */
|
|
16
|
+
const rel = (root, p) => relative(root, p).replace(/\\/g, "/")
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 跑一次文档纪律机检(报告态)。
|
|
20
|
+
* @param {string} root 项目根(判据参数取该根的 `PROJECT-MANIFEST.json` `checkConfig`)
|
|
21
|
+
* @returns {{report:string, hits:{total:number, anchors:number, width:number}, anchorHits:Array,
|
|
22
|
+
* widthHits:Array, anchorReports:Array, files:string[], scanDirs:string[], lineWidth:number,
|
|
23
|
+
* zones:string[], exclude:string[]}}
|
|
24
|
+
*/
|
|
25
|
+
export function runDocCheck(root) {
|
|
26
|
+
const base = resolve(root)
|
|
27
|
+
const read = readManifest(base)
|
|
28
|
+
if (!read.ok) {
|
|
29
|
+
throw new Error(`doc_check: cannot read ${MANIFEST_REL} at ${base} — the check parameters come from that project's checkConfig (${read.reason ?? read.errors.join(";")})`)
|
|
30
|
+
}
|
|
31
|
+
const cfg = read.manifest.checkConfig ?? {}
|
|
32
|
+
const scanDirs = (Array.isArray(cfg.scanDirs) ? cfg.scanDirs : []).filter((d) => typeof d === "string" && d !== "")
|
|
33
|
+
const exclude = Array.isArray(cfg.anchors?.exclude) ? cfg.anchors.exclude : []
|
|
34
|
+
const lineWidth = cfg.lineWidth
|
|
35
|
+
const zones = (Array.isArray(cfg.widthExemptZones) ? cfg.widthExemptZones : []).filter((z) => typeof z === "string" && z !== "")
|
|
36
|
+
|
|
37
|
+
const files = scanDomain(base, { scanDirs, exclude })
|
|
38
|
+
const anchors = checkAnchors(base, { scanDirs, files })
|
|
39
|
+
const widths = checkDocWidths(base, { lineWidth, scanDirs, exclude, exemptZones: zones, files })
|
|
40
|
+
|
|
41
|
+
const total = anchors.hits.length + widths.length
|
|
42
|
+
const out = [
|
|
43
|
+
"文档纪律机检(报告态 · 非门禁)",
|
|
44
|
+
`射程:${base} · ${scanDirs.join(" + ") || "(无)"}(${files.length} 档)· 阈值 ${lineWidth} 字符 · 区带豁免:${zones.length ? zones.join(" ∥ ") : "无"} · 排除:${exclude.length ? exclude.join(" ∥ ") : "(无)"}`,
|
|
45
|
+
"声明:判据参数依据 `PROJECT-MANIFEST.json` `checkConfig`",
|
|
46
|
+
`命中 ${total} 条(锚 ${anchors.hits.length} · 行宽 ${widths.length})`,
|
|
47
|
+
]
|
|
48
|
+
// 报告面(不作判):域内同名 ≥2 ⇒ 多义不判(残余登记——fail-open 方向,归评审核验面)
|
|
49
|
+
for (const r of anchors.reports) {
|
|
50
|
+
out.push(`报告 锚·多义 ${rel(base, r.file)}:${r.line} \`${r.token}\`(同名 ${r.candidates.length}:${r.candidates.map((c) => rel(base, c)).join(" ∥ ")}——报告面,不作判)`)
|
|
51
|
+
}
|
|
52
|
+
for (const h of anchors.hits) {
|
|
53
|
+
out.push(h.clazz === "档缺"
|
|
54
|
+
? `✗ 锚·档缺 ${rel(base, h.file)}:${h.line} \`${h.token}\``
|
|
55
|
+
: `✗ 锚·节缺 ${rel(base, h.file)}:${h.line} \`${h.token}\`(目标 ${rel(base, h.target)} 无 §${h.section})`)
|
|
56
|
+
}
|
|
57
|
+
for (const w of widths) out.push(`✗ 行宽 ${rel(base, w.file)}:${w.line}(${w.len} 字符)`)
|
|
58
|
+
out.push(total ? `结论:${total} 条命中——报告态,不作拒执行的理由` : "结论:0 条命中(锚 ∥ 行宽 均清)")
|
|
59
|
+
|
|
60
|
+
return { report: out.join("\n"), hits: { total, anchors: anchors.hits.length, width: widths.length }, anchorHits: anchors.hits, widthHits: widths, anchorReports: anchors.reports, files, scanDirs, lineWidth, zones, exclude }
|
|
61
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doc-check/width.mjs — 文档纪律机检的产品档位(行宽谓词族 + 域扫描 + 行宽判据——实现单源)。
|
|
3
|
+
*
|
|
4
|
+
* 口径单源 = `docs/core/design/DOC-DISCIPLINE.md` §3.18 条目 Q(度量 / 域 / 豁免类 / 阈值);
|
|
5
|
+
* 产品内建裁定 = 同档 §3.20 条目 S——谓词单源随迁:本仓 `scripts/doc-check-width.mjs` 改述为
|
|
6
|
+
* **再出口缝**(薄档 · 零逻辑 · 逐名再出口),盘上不留第二份谓词实现。
|
|
7
|
+
* 度量 = `[...line].length`(CJK 计 1——不引显示宽度 / 字体);阈值 / 域 / 排除全部读
|
|
8
|
+
* `PROJECT-MANIFEST.json` 的 `checkConfig`——零硬编码本仓路径,换项目开箱可用。
|
|
9
|
+
* 豁免五类 = 表格行 ∥ fenced 块 ∥ 可执行行 ∥ 连续引文 ∥ 区带:**只认结构谓词**,
|
|
10
|
+
* 无行级白名单(按行号 / 注释声明的豁免不入判)。
|
|
11
|
+
* 边界:本档零门禁 / 零退出码 / 零阻断——报告态由 `agent-tools/doc-check.mjs` 呈现。
|
|
12
|
+
*/
|
|
13
|
+
import { readdirSync, readFileSync, statSync } from "node:fs"
|
|
14
|
+
import { join, resolve } from "node:path"
|
|
15
|
+
import { MANIFEST_REL } from "../manifest.mjs"
|
|
16
|
+
|
|
17
|
+
/** 域发现跳过目录(不视作候选产品域;递归亦不进入)。 */
|
|
18
|
+
const SKIP_DIRS = new Set(["node_modules", ".git", "dist", "build", "coverage", ".turbo", ".thincoder"])
|
|
19
|
+
|
|
20
|
+
/** 域发现(声明面):cwd 根(持 manifest)+ 各持自身 manifest 的一级子目录——判据 = manifest 在场,无路径硬编码。 */
|
|
21
|
+
export function discoverDomains(root) {
|
|
22
|
+
const rootAbs = resolve(root)
|
|
23
|
+
const hasManifest = (p) => { try { return statSync(join(p, MANIFEST_REL)).isFile() } catch { return false } }
|
|
24
|
+
const out = []
|
|
25
|
+
if (hasManifest(rootAbs)) out.push(rootAbs)
|
|
26
|
+
let names = []
|
|
27
|
+
try { names = readdirSync(rootAbs) } catch { /* 不可读:仅根域 */ }
|
|
28
|
+
for (const n of names) {
|
|
29
|
+
if (SKIP_DIRS.has(n) || n.startsWith("dist-")) continue
|
|
30
|
+
const p = join(rootAbs, n)
|
|
31
|
+
try { if (statSync(p).isDirectory() && hasManifest(p)) out.push(p) } catch { /* 跳过 */ }
|
|
32
|
+
}
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 递归列 .md(跳过 SKIP_DIRS ∧ 声明面 exclude 段——exclude 逐层生效)。 */
|
|
37
|
+
function walkMd(dir, exclude, out) {
|
|
38
|
+
let names = []
|
|
39
|
+
try { names = readdirSync(dir) } catch { return out }
|
|
40
|
+
for (const n of names) {
|
|
41
|
+
if (SKIP_DIRS.has(n) || n.startsWith("dist-") || exclude.includes(n)) continue
|
|
42
|
+
const p = join(dir, n)
|
|
43
|
+
let st
|
|
44
|
+
try { st = statSync(p) } catch { continue }
|
|
45
|
+
if (st.isDirectory()) walkMd(p, exclude, out)
|
|
46
|
+
else if (n.endsWith(".md")) out.push(p)
|
|
47
|
+
}
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 源域(单源):`scanDirs` × `exclude` 声明面下的全部 .md——锚检查与行宽检查同一文件集。 */
|
|
52
|
+
export function scanDomain(base, { scanDirs = [], exclude = [] } = {}) {
|
|
53
|
+
const ex = Array.isArray(exclude) ? exclude : []
|
|
54
|
+
const out = []
|
|
55
|
+
for (const d of Array.isArray(scanDirs) ? scanDirs : []) walkMd(resolve(base, d), ex, out)
|
|
56
|
+
return [...new Set(out)].sort()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 表格行谓词(markdown 表格行结构性不可折行——行宽豁免,与锚检查同源)。 */
|
|
60
|
+
export const isTableRow = (l) => /^\s*\|.*\|\s*$/.test(String(l))
|
|
61
|
+
|
|
62
|
+
/** 围栏标记(行首可选空白 + 三反引号 ∥ 三波浪号 ⇒ 标记字符;非围栏行 ⇒ null)——**单源**:
|
|
63
|
+
* `isFenceLine` 与两处块扫描(本节行宽 ∥ `./anchors.mjs` 锚抽取)同取此式,使「同形围栏行止」只此一解
|
|
64
|
+
* (同一模块内同符两义 = 单源可漂的结构口子)。 */
|
|
65
|
+
export function fenceMark(l) {
|
|
66
|
+
const m = /^\s*(`{3,}|~{3,})/.exec(String(l))
|
|
67
|
+
return m ? m[1][0] : null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 围栏行谓词(单源 = `fenceMark`)——围栏行自身与块内整行均豁免。 */
|
|
71
|
+
export const isFenceLine = (l) => fenceMark(l) !== null
|
|
72
|
+
|
|
73
|
+
/** 引文块行谓词(行首可选空白 + `>`)——连续 `>` 行整块豁免(引证须逐字连续、不可折,同表行理据)。 */
|
|
74
|
+
export const isQuoteLine = (l) => /^\s*>/.test(String(l))
|
|
75
|
+
|
|
76
|
+
/** 区带头谓词(§3.15 谓词面):标题文本去首部编号(`^\d+(?:\.\d+)*[.、]?\s*`)后——等于区带名 ∨ 以
|
|
77
|
+
* 「区带名 + (∕(」起(容括注后缀,如 `变更记录(续)`);闭集外一律不纳(fail-closed)。 */
|
|
78
|
+
export function isZoneHead(headingText, zones = []) {
|
|
79
|
+
const text = String(headingText).replace(/^\d+(?:\.\d+)*[.、]?\s*/, "")
|
|
80
|
+
return zones.some((z) => typeof z === "string" && z !== "" && (text === z || text.startsWith(z + "(") || text.startsWith(z + "(")))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 行内可执行坐标(命令 / 搜索模式串码段)——整行豁免(证据与命令保字面,与锚判据同源)。 */
|
|
84
|
+
export function isExecutableLine(line) {
|
|
85
|
+
const l = String(line)
|
|
86
|
+
if (/`[^`\n]*&&[^`\n]*`/.test(l)) return true // 命令链(cd … && …)
|
|
87
|
+
if (/`[^`\n]*\b(?:cd|node|npm|npx|grep|rg|git)\s[^`\n]*`/.test(l)) return true // 码段内命令词
|
|
88
|
+
if (/`[^`\n]*(?:\[\^|\\\.|\(\?<|\(\?:|\{\d)/.test(l)) return true // 码段内搜索模式串片段
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 行内位置是否在反引号码段内(引述面不判)。 */
|
|
93
|
+
export function inCodeSpan(line, idx) {
|
|
94
|
+
return (String(line).slice(0, idx).match(/`/g) ?? []).length % 2 === 1
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 行宽判据(单判据 = `checkConfig.lineWidth`):`[...line].length` > 阈值 ∧ 非表格行 ∧ 非 fenced 块内
|
|
98
|
+
* ∧ 非可执行行 ∧ 非连续引文 ∧ 非区带内 ⇔ 命中(口径逐字 = §3.18 条目 Q)。
|
|
99
|
+
* 单遍扫描携标题栈(`^(#{1,6})\s` 行入栈——同级 ∥ 更高级出栈):行所属标题链任一为区带头 ⇒ 豁免。
|
|
100
|
+
* 围栏未闭合 ⇒ 至档尾整块免(fail-open 方向——§3.18 残余登记 ①);`exemptZones` 缺省 ∥ 空集 ⇒ 零豁免。 */
|
|
101
|
+
export function checkDocWidths(base, { lineWidth, scanDirs, exclude, exemptZones = [], files } = {}) {
|
|
102
|
+
const zones = (Array.isArray(exemptZones) ? exemptZones : []).filter((z) => typeof z === "string" && z !== "")
|
|
103
|
+
const hits = []
|
|
104
|
+
// `files` 给定时直接用(调用方已扫同一域 ⇒ 域只走一遍、文件集由值保证同一);缺省自扫(既有调用面不变)。
|
|
105
|
+
for (const f of files ?? scanDomain(base, { scanDirs, exclude })) {
|
|
106
|
+
let text = ""
|
|
107
|
+
try { text = readFileSync(f, "utf8") } catch { continue }
|
|
108
|
+
const stack = [] // 标题栈 —— { level, exempt };exempt = 自身 ∥ 祖先命中区带
|
|
109
|
+
let fence = null // 围栏标记(` ∥ ~);null = 不在块内
|
|
110
|
+
text.split("\n").forEach((l, i) => {
|
|
111
|
+
const mark = fenceMark(l)
|
|
112
|
+
if (mark !== null) {
|
|
113
|
+
if (fence === null) fence = mark
|
|
114
|
+
else if (mark === fence) fence = null
|
|
115
|
+
return // 围栏行自身免
|
|
116
|
+
}
|
|
117
|
+
if (fence !== null) return // 块内整块免(未闭合 ⇒ 至档尾免)
|
|
118
|
+
const hm = /^(#{1,6})\s+(.*)$/.exec(l)
|
|
119
|
+
if (hm) {
|
|
120
|
+
while (stack.length && stack[stack.length - 1].level >= hm[1].length) stack.pop()
|
|
121
|
+
const self = isZoneHead(hm[2].trim(), zones)
|
|
122
|
+
stack.push({ level: hm[1].length, exempt: self || (stack[stack.length - 1]?.exempt ?? false) })
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
const inZone = stack[stack.length - 1]?.exempt === true
|
|
126
|
+
const len = [...l].length
|
|
127
|
+
if (len > lineWidth && !isTableRow(l) && !isExecutableLine(l) && !isQuoteLine(l) && !inZone) hits.push({ file: f, line: i + 1, len })
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
return hits
|
|
131
|
+
}
|
package/file-refs.mjs
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
* 上提源 = VSC `thincoder-vscode/src/extension/file-refs.mjs` 自持档(逐字搬——regex ∕ 4000 截断 ∕
|
|
4
4
|
* `[File: …]` 围栏 ∕ 摘要块格式零语义改良);本档 = **单源**;两端各持薄壳(端侧 `node:fs` 探针注入):
|
|
5
5
|
* VSC `thincoder-vscode/src/extension/file-refs.mjs` ∥ 桌面 `thincoder-desktop/src/main/file-refs.mjs`。
|
|
6
|
-
* 消费面:VSC 注入 `panel-chat.mjs
|
|
7
|
-
* `panel-session-write.mjs
|
|
8
|
-
* 恢复剥离 = `session-slots.mjs` `pageHistory` ∥ 标题剥离 = 核 `generate-title.mjs
|
|
9
|
-
* 文本形逐字冻结(`docs/vsc/design/WEBVIEW.md
|
|
6
|
+
* 消费面:VSC 注入 `panel-chat.mjs` 的 `injectAtRefs` ∕ 恢复剥离 `panel-session.mjs` 的 `stripAtRefs` ∕ 标题剥离
|
|
7
|
+
* `panel-session-write.mjs` 的 `generateTitle`;桌面注入缝 = `turn-driver.mjs`(起跑前单点,`turn-face.mjs`)∥
|
|
8
|
+
* 恢复剥离 = `session-slots.mjs` `pageHistory` ∥ 标题剥离 = 核 `generate-title.mjs` 的 `ensureSessionTitle`(读源处)。
|
|
9
|
+
* 文本形逐字冻结(`docs/vsc/design/WEBVIEW.md` §4.4「落线文本逐字不变」契约):上提不携任何行为面改动。
|
|
10
10
|
* 判据 = **盘面探针注入缝**(`probe = { readFileSync, existsSync, statSync }` —— `node:fs` 同名面;
|
|
11
|
-
* 缺 ∕ 形违 ⇒ 抛 —— fail-loud:盘面闸未接线不得静默扮成「零引用」,沿 `file-links.mjs
|
|
11
|
+
* 缺 ∕ 形违 ⇒ 抛 —— fail-loud:盘面闸未接线不得静默扮成「零引用」,沿 `file-links.mjs` 的 `extractFileLinks` 先例)。
|
|
12
12
|
* `stripAtRefs` = 注入语法的逆变换:fail-closed 判据逐字依赖注入输出格式 —— 语法与其逆变换同住一处,
|
|
13
13
|
* 零第二实现(本档为唯一产者)。
|
|
14
14
|
*/
|
|
@@ -26,7 +26,7 @@ function probeOf(probe) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/** Scan text for @path references and replace with inline file content.
|
|
29
|
-
* 零文本(非串 ∥ 空串)先于探针校验早退(零判据——探针不检,同序沿核 `file-links.mjs
|
|
29
|
+
* 零文本(非串 ∥ 空串)先于探针校验早退(零判据——探针不检,同序沿核 `file-links.mjs` 的 `extractFileLinks`)。 */
|
|
30
30
|
export function injectAtRefs(text, cwd, probe) {
|
|
31
31
|
if (typeof text !== "string" || text === "") return text
|
|
32
32
|
const io = probeOf(probe)
|
package/generate-title.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { resolveProviderSecrets } from "./env-ref.mjs"
|
|
|
14
14
|
// (`isRealUserMsg`——纯函数零依赖,无环)。
|
|
15
15
|
import { isRealUserMsg } from "./history-window.mjs"
|
|
16
16
|
// #632(缺面族批补 · @ 文件引用对齐 · 2026-09-29):标题源剥离单源——注入形不得进标题请求(核件
|
|
17
|
-
// `file-refs.mjs`;CLI 无注入 ⇒ 零动作;VSC 同位转口 = `panel-session-write.mjs
|
|
17
|
+
// `file-refs.mjs`;CLI 无注入 ⇒ 零动作;VSC 同位转口 = `panel-session-write.mjs` 的 `generateTitle`)。
|
|
18
18
|
import { stripAtRefs } from "./file-refs.mjs"
|
|
19
19
|
// #363③(off 形族单源——MODEL-SPECS.md §16.2 / §16.4):禁思考形与「是否可禁」一律引核
|
|
20
20
|
// (`thinkOffShape` / `thinkOffPath`——本档零副本);spec 查表同单源(`specForModel`)。
|
package/hooks.mjs
CHANGED
|
@@ -98,7 +98,7 @@ async function runOneHook(event, hook, ctx) {
|
|
|
98
98
|
// Timeout guard(#135-④:`timeout` ms 到点杀掉子进程 + 放行——原由 spawn 的 `timeout`
|
|
99
99
|
// 选项承担,见上)。句柄捕获 + `unref()`:进程存活期间守卫照常触发(真 CLI 运行事件循环
|
|
100
100
|
// 另有 handle),但不再单独维持事件循环存活 ⇒ 子进程钩子用例的测试进程零尾挂;
|
|
101
|
-
// 任一出口经 `done` 收尾 clearTimeout(house 先例 = mcp/transport-stdio.mjs
|
|
101
|
+
// 任一出口经 `done` 收尾 clearTimeout(house 先例 = `mcp/transport-stdio.mjs` 的 `killTree`)。
|
|
102
102
|
guard = setTimeout(() => {
|
|
103
103
|
try { proc.kill() } catch { /* already gone */ }
|
|
104
104
|
done(0)
|
package/i18n.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* - `projectDictionary(locale)` = **VSC 字典投影**:核常量 → VSC `locales/*.json` 的扁平
|
|
8
8
|
* 字典形态(VSC 的 `i18n.mjs` `t()` 壳在 S2 接线时并入本投影,壳内零文案)。
|
|
9
9
|
*
|
|
10
|
-
* 契约(CORE-UNIFICATION §2.12.2
|
|
10
|
+
* 契约(CORE-UNIFICATION §2.12.2「输出文案类」):**机器消费面冻结**——本表键值
|
|
11
11
|
* 与现 VSC `locales/{en,zh}.json` 逐字一致(已锁定串不得改);人读文案后续变更须登记 +
|
|
12
12
|
* CHANGELOG(S2 落地物)。`${name}` = 插值占位(两端同约定)。
|
|
13
13
|
*
|
|
@@ -76,6 +76,16 @@ export const CORE_MESSAGES = Object.freeze({
|
|
|
76
76
|
"tool.done": { en: "done", zh: "完成" },
|
|
77
77
|
// ── 台账(ledger)· F-LX3 变体键库首跑提示(批 2026-10-05-ledger-variant-db-notice · 台账 #935 · LEDGER.md §12.2 逐字;消费面 = CLI TUI 启动行)
|
|
78
78
|
"ledger.variantDbNotice": { en: "Legacy variant-key ledger DB found for this project (pre-upgrade; rows may be outside the current ledger): thincoder ledger audit to inspect; thincoder ledger migrate to reconcile", zh: "检测到本项目的存量变体键台账库(升级遗留,数据可能未并入当前库):thincoder ledger audit 查看;thincoder ledger migrate 收正" },
|
|
79
|
+
// ── SSH 会话(批 ssh-session · 台账 #1436 · TOOLS.md §6.22 ⑧——凭据窗词面单源;三端经本字典投影)──
|
|
80
|
+
"ssh.credentialTitle": { en: "SSH credential", zh: "SSH 凭据" },
|
|
81
|
+
"ssh.credentialPrompt": { en: "Enter the login credential for ${host}", zh: "输入 ${host} 的登录凭据" },
|
|
82
|
+
"ssh.secretTitle": { en: "SSH secret", zh: "SSH 密文" },
|
|
83
|
+
"ssh.secretPrompt": { en: "Enter the secret for ${host}", zh: "输入 ${host} 的密文" },
|
|
84
|
+
"ssh.field.user": { en: "Username", zh: "用户名" },
|
|
85
|
+
"ssh.field.password": { en: "Password", zh: "密码" },
|
|
86
|
+
"ssh.field.secret": { en: "Secret", zh: "密文" },
|
|
87
|
+
// 无 UI 失败句(E6;三端投影单源):en 侧 = CLI 端 `SECRET_NO_UI_TEXT` 逐字(端常量收正为核键)。
|
|
88
|
+
"ssh.no_ui": { en: "ssh credential input requires an interactive UI (none in this context) - use key-based auth or ssh-agent", zh: "ssh 凭据输入需要交互界面(当前场合没有)——请改用密钥认证或 ssh-agent" },
|
|
79
89
|
})
|
|
80
90
|
// 深度冻结(机器消费面冻结——嵌套 { en, zh } 也冻结;浅冻结下值可被静默改写)。
|
|
81
91
|
for (const entry of Object.values(CORE_MESSAGES)) Object.freeze(entry)
|
package/ledger-cmd.mjs
CHANGED
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
* 消费侧静态 import 会破 W8 契约②)。
|
|
12
12
|
*/
|
|
13
13
|
import { resolve } from "node:path"
|
|
14
|
-
import { ALLOWED_MIGRATIONS, nowIso, openLedger, PENDING_STATUSES,
|
|
15
|
-
import { resolveProjectRoot } from "./manifest.mjs"
|
|
14
|
+
import { AGING_DAYS, ALLOWED_MIGRATIONS, nowIso, openLedger, PENDING_STATUSES, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
16
15
|
import { resolveDeclaredRef } from "./declaration.mjs"
|
|
17
16
|
|
|
18
17
|
/** 逐行老化判真(LEDGER.md §13.4 · 2026-10-07 批 ledger-tool):未决四态(`PENDING_STATUSES`)∧
|
|
@@ -60,8 +59,8 @@ export function ledgerCount({ cwd } = {}) {
|
|
|
60
59
|
* (首个 `§` 前子串,trim)须经基准 `resolve(base, …)` 指向存在的档;缺文件部分(`§2` / 空串 /
|
|
61
60
|
* 全空白)⇒ 拒;不在册 / 非文件 ⇒ 拒(throw,行不变)。
|
|
62
61
|
* 判位与迁移表判同层(落盘前);文案前缀 = 调用函数名(与同函数既有两条文案同款)。
|
|
63
|
-
* 基准 `base` =
|
|
64
|
-
* resolve(cwd ?? ".")
|
|
62
|
+
* 基准 `base` = **与台账库关联键同源**(设计档 §6.1 口径 2——根推导单源):`resolveLedgerRoot(cwd)?.root
|
|
63
|
+
* ?? resolve(cwd ?? ".")`——同一子表达式见 `ledger-db.mjs` `ledgerDbPath`;容器根会话(锚 cwd 下唯一注册
|
|
65
64
|
* 子仓 P)与子仓会话 ⇒ 库键与指针基准同取 P(同库同基准;原实现 `resolve(cwd, …)` = 原始 cwd 会误拒)。
|
|
66
65
|
* #832 ∥ #834:声明源前缀形同判——判定单源 = `declaration.mjs` `resolveDeclaredRef`(① 仓根解析 →
|
|
67
66
|
* ② 声明源前缀解析序;同名多仓 ⇒ 声明序首者)——**与迁移旗(`ledger-migrate.mjs` `writeGateFlag`)
|
|
@@ -71,7 +70,7 @@ function assertTaskBookGate(cwd, fnName, status, taskBook) {
|
|
|
71
70
|
if (taskBook == null) throw new Error(`${fnName}:task_book 必填(在途 / 待核销 须携任务书指针)`)
|
|
72
71
|
const part = String(taskBook).split("§")[0].trim()
|
|
73
72
|
if (!part) throw new Error(`${fnName}:task_book 不可解析(缺文件部分):${taskBook}`)
|
|
74
|
-
const base =
|
|
73
|
+
const base = resolveLedgerRoot(cwd)?.root ?? resolve(cwd ?? ".")
|
|
75
74
|
const { ok, abs } = resolveDeclaredRef(base, part)
|
|
76
75
|
if (!ok) throw new Error(`${fnName}:task_book 指向的档不存在:${taskBook}(解析 = ${abs})`)
|
|
77
76
|
}
|
package/ledger-db.mjs
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* 靠 DDL(AC-M2-2),不靠应用层 if。归档 = 软删除(状态值,无物理 DELETE——KD-M2-4)。
|
|
7
7
|
* 本档静态 import node:sqlite ⇒ 消费侧一律动态 import 本模块链(W8 契约②——KD-M2-3)。
|
|
8
8
|
*
|
|
9
|
+
* 项目根推导单源(`resolveLedgerRoot`)+ 同级枚举(`ledgerChildren`)居本档——键 / 库位 / 根同源
|
|
10
|
+
* (设计档 `LEDGER.md` §2.1「根解析面」/ §3.8):读写同一判据(档在盘 ∨ 以目录自身路径为键的库在盘)。
|
|
11
|
+
*
|
|
9
12
|
* 落点(2026-09-17 用户裁定收正):台账库**不在项目目录**——住用户数据目录
|
|
10
13
|
* `~/.thincoder/ledger/<sha1(normalizeCwd(项目根绝对路径))[:16]>.db`(工作树外、天然不进 git;
|
|
11
14
|
* 同区先例 = configDir 的 memory.db / tool-results)。项目根仅作**关联键**,不在项目内留任何文件。
|
|
@@ -13,12 +16,12 @@
|
|
|
13
16
|
* `normalizeCwd`——与 session / checkpoint / traces / peers 同契约,跨端同 cwd 同键同库。
|
|
14
17
|
*/
|
|
15
18
|
import { createHash } from "node:crypto"
|
|
16
|
-
import { existsSync, mkdirSync } from "node:fs"
|
|
17
|
-
import { join, resolve } from "node:path"
|
|
19
|
+
import { existsSync, mkdirSync, opendirSync } from "node:fs"
|
|
20
|
+
import { dirname, join, resolve } from "node:path"
|
|
18
21
|
import { DatabaseSync } from "node:sqlite"
|
|
19
22
|
|
|
20
23
|
import { configDir } from "./config-io.mjs"
|
|
21
|
-
import {
|
|
24
|
+
import { MANIFEST_REL, projectRootView } from "./manifest.mjs"
|
|
22
25
|
// 归一步单源(KD-LN1——直引,不抽新 util:先例 = peer-domains.mjs / traces/trace-store.mjs)。
|
|
23
26
|
import { normalizeCwd } from "./session-slots.mjs"
|
|
24
27
|
|
|
@@ -42,14 +45,89 @@ export function ledgerKey(root) {
|
|
|
42
45
|
return createHash("sha1").update(normalizeCwd(root)).digest("hex").slice(0, 16)
|
|
43
46
|
}
|
|
44
47
|
|
|
48
|
+
/** 键控库文件路径(根 → 库位)——**内部形**:根推导内部判「库在盘」时用它,**不得经 `ledgerDbPath`**
|
|
49
|
+
* (互指即环,设计档 §3.8「无自指」)。库目录 = `ledgerDir`(`_setLedgerDirForTest` 覆盖 ⇒ 夹具根外零写)。 */
|
|
50
|
+
const keyedDbFile = (root) => join(ledgerDir, `${ledgerKey(root)}.db`)
|
|
51
|
+
|
|
52
|
+
/** 「已注册」判据(设计档 §2.1——**唯一**事实,两臂取一即成立):
|
|
53
|
+
* 档臂 = 该目录带项目声明档 `PROJECT-MANIFEST.json`(档在盘);
|
|
54
|
+
* 库臂 = 以该目录自身路径为键的键控库在盘 ∧ 该目录**非工作区态**(键控库存在 ≠ 可作项目);
|
|
55
|
+
* 工作区判据只在库在盘时才查(`projectRootView` 含 fs 扫描——档臂已中即短路)。 */
|
|
56
|
+
function isRegistered(dir) {
|
|
57
|
+
if (existsSync(join(dir, MANIFEST_REL))) return true
|
|
58
|
+
if (!existsSync(keyedDbFile(dir))) return false
|
|
59
|
+
return projectRootView(dir).state !== "workspace"
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 同级枚举上限(成本有界:父目录超此规模 = 缓存 / 临时等非仓族形态 → 退化空集)。
|
|
63
|
+
* 2026-10-16 批 ledger-root-derivation 自 `ledger.mjs` 随迁(根推导收敛序 ∥ 族发现同级枚举同源)。 */
|
|
64
|
+
export const MAX_SIBLING_SCAN = 100
|
|
65
|
+
|
|
66
|
+
/** 目录下**已注册**(档 ∨ 库——§2.1 单谓词)的直接子目录(目录名升序;不可读 ∥ 超限 ⇒ 空集;
|
|
67
|
+
* **惰性枚举 + 上限**——大目录零全量扫描)。消费面 = 根推导收敛序 ∥ 族发现 ∥ 轻通道闸候选枚举。
|
|
68
|
+
* `ledger` = 该子目录根的键控库路径(谓词含档臂 ⇒ 可能尚未在盘)。2026-10-16 批自 `ledger.mjs` 随迁。 */
|
|
69
|
+
export function ledgerChildren(dir) {
|
|
70
|
+
const out = []
|
|
71
|
+
let handle
|
|
72
|
+
try { handle = opendirSync(dir) } catch { return out }
|
|
73
|
+
try {
|
|
74
|
+
let n = 0
|
|
75
|
+
for (;;) {
|
|
76
|
+
const ent = handle.readSync()
|
|
77
|
+
if (!ent) break
|
|
78
|
+
if (++n > MAX_SIBLING_SCAN) return []
|
|
79
|
+
if (!ent.isDirectory()) continue
|
|
80
|
+
const root = join(dir, ent.name)
|
|
81
|
+
if (!isRegistered(root)) continue
|
|
82
|
+
out.push({ root, ledger: keyedDbFile(root) })
|
|
83
|
+
}
|
|
84
|
+
} catch { /* 读中断 → 已得子集(尽力而为) */ }
|
|
85
|
+
finally { try { handle.closeSync() } catch { /* 已关 */ } }
|
|
86
|
+
return out.sort((a, b) => (a.root < b.root ? -1 : a.root > b.root ? 1 : 0))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 项目根推导**单源**(设计档 §2.1「根解析面」/ §3.8——读写同一判据、同一实现):
|
|
90
|
+
* 自 `target`(「动作目标路径」;缺省 = `.`)起沿**祖先链**向上(含自身)取**最近一个「已注册」目录**
|
|
91
|
+
* ⇒ `{ root, basis: 'ancestor' }`(自明——回执逐字零改)。
|
|
92
|
+
* 祖先链无命中 ∧ `descend: true` ⇒ **向下收敛**锚的直接子目录(`ledgerChildren` 同款惰性枚举 +
|
|
93
|
+
* 上限、名序升序):恰一 ⇒ 取之;≥2 ⇒ ① 先取**有键控库**者(账已在盘 = 事实)② 该集名序首者 ⇒
|
|
94
|
+
* `{ root, basis: 'descendant' }`(向下发现——**须报明**);零已注册子目录 ⇒ `null`(无根)。
|
|
95
|
+
* `descend: false` ⇒ 只问祖先支(族发现 ∥ 标记面——容器 ∥ 工作区锚 ⇒ `null` ⇒ 族求和)。
|
|
96
|
+
* **无自指**:内部「库在盘」判据只用 `ledgerKey` + `keyedDbFile`(不经 `ledgerDbPath`)。
|
|
97
|
+
* @param {string} [target] 动作目标路径(目录)
|
|
98
|
+
* @param {{descend?: boolean}} [opts] `descend` 缺省 `true`(动作面——`openLedger` / `ledgerDbPath` / 单范围读)
|
|
99
|
+
* @returns {{root: string, basis: 'ancestor'|'descendant'}|null} 项目根 + 判据来源 / 无根(`null`) */
|
|
100
|
+
export function resolveLedgerRoot(target, { descend = true } = {}) {
|
|
101
|
+
const start = resolve(target ?? ".")
|
|
102
|
+
let dir = start
|
|
103
|
+
for (;;) {
|
|
104
|
+
if (isRegistered(dir)) return { root: dir, basis: "ancestor" }
|
|
105
|
+
const parent = dirname(dir)
|
|
106
|
+
if (parent === dir) break // 盘根 → 祖先链无已注册者
|
|
107
|
+
dir = parent
|
|
108
|
+
}
|
|
109
|
+
if (!descend) return null
|
|
110
|
+
const children = ledgerChildren(start)
|
|
111
|
+
if (children.length === 0) return null
|
|
112
|
+
if (children.length === 1) return { root: children[0].root, basis: "descendant" }
|
|
113
|
+
const withDb = children.filter((c) => existsSync(c.ledger))
|
|
114
|
+
return { root: (withDb.length > 0 ? withDb : children)[0].root, basis: "descendant" }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 无根文案(单源——设计档 §3.8「无根契约」逐字;`openLedger` 写面 ∥ `ledgerDbPath` 共用同一句)。 */
|
|
118
|
+
const noRootMessage = (start) =>
|
|
119
|
+
`台账:自 ${start} 未能推导项目根——祖先链与本级子目录均无已注册项目(带 PROJECT-MANIFEST.json 或已有键控台账库)`
|
|
120
|
+
|
|
45
121
|
/** 项目根 → 库路径(稳定键 = `ledgerKey` 前 16 位——路径唯一标识,不含分隔符)。
|
|
46
|
-
*
|
|
47
|
-
*
|
|
122
|
+
* **归单源**(2026-10-16 批 ledger-root-derivation):根 = `resolveLedgerRoot(cwd)`——与读面 / 写面
|
|
123
|
+
* 同一推导;无根 ⇒ 抛**同一**无根文案(键需根,不产第二答案)。
|
|
48
124
|
* v2 键归一(2026-09-25 批):盘符本大写(CLI)⇒ 键恒等零变;小写盘符(VSC `uri.fsPath`)
|
|
49
125
|
* 并入同键 ⇒ 同项目两端同库(收正前 = 同项目两键两库)。 */
|
|
50
126
|
export function ledgerDbPath(cwd) {
|
|
51
|
-
const
|
|
52
|
-
|
|
127
|
+
const start = resolve(cwd ?? ".")
|
|
128
|
+
const derived = resolveLedgerRoot(start)
|
|
129
|
+
if (!derived) throw new Error(noRootMessage(start))
|
|
130
|
+
return keyedDbFile(derived.root)
|
|
53
131
|
}
|
|
54
132
|
|
|
55
133
|
/** 未决四态(「活条目」口径 = 计数单源 WHERE 集;已核销 / 已废弃 = 归档态)。 */
|
|
@@ -135,25 +213,26 @@ function markSchemaVersion(db) {
|
|
|
135
213
|
if (v < LEDGER_SCHEMA_VERSION) db.exec(`PRAGMA user_version = ${LEDGER_SCHEMA_VERSION}`)
|
|
136
214
|
}
|
|
137
215
|
|
|
138
|
-
/** 开库(cwd =
|
|
139
|
-
*
|
|
140
|
-
*
|
|
216
|
+
/** 开库(cwd = 「动作目标路径」)→ DatabaseSync 句柄 + 幂等建表 + 老库幂等迁移(executor 列)。
|
|
217
|
+
* **根推导**(设计档 §2.1 / §3.8):项目根 = `resolveLedgerRoot(cwd)`——读写同一判据;嵌套取近者、
|
|
218
|
+
* 容器锚向下收敛。**无根(`null`)**:写面(`create:true`)抛(fail-loud、零落盘零建库);读面空账
|
|
219
|
+
* (`null`——与「库不在 = 空账」同形)。
|
|
220
|
+
* 读面(create=false):库文件不存在 → 返回 null(读面不建库、无副作用)。
|
|
221
|
+
* 写面(create=true):库文件不存在自动建(台账目录随之创建)。
|
|
141
222
|
* **只读态**(readOnly=true——读面专用):只读句柄 + 零 DDL / ALTER + items 探针(见 openReadOnly);
|
|
142
223
|
* 如实副作用:旧库(缺 executor 列)**读**不再自动补列——补列归写面开库;读出行缺列按 null 归一。
|
|
143
|
-
*
|
|
144
|
-
* 零写——不静默回退锚;族锚「项目不可解析」,AC-M2-17);兜底 `resolve(cwd ?? ".")` 只属 none 态。 */
|
|
224
|
+
* 「目录不存在」的真错照抛(fail-loud——先于推导,文案逐字零改)。 */
|
|
145
225
|
export function openLedger(cwd, { create = false, readOnly = false } = {}) {
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (view.state === "ambiguous") {
|
|
150
|
-
const list = view.candidates.map((c) => `- ${c}`).join("\n")
|
|
151
|
-
throw new Error(`项目不可解析:cwd ${dir} 下候选项目不是恰好一个(下列 ${view.candidates.length} 个)——台账按项目根键控,请显式给出项目根 cwd:\n${list}`)
|
|
226
|
+
const start = resolve(cwd ?? ".")
|
|
227
|
+
if (!existsSync(start)) {
|
|
228
|
+
throw new Error(`项目目录不存在:${start}——cwd 请给存在的项目根(相对路径按当前工作目录解析;缺省 = 会话项目根)`)
|
|
152
229
|
}
|
|
153
|
-
const
|
|
154
|
-
if (!
|
|
155
|
-
throw new Error(
|
|
230
|
+
const derived = resolveLedgerRoot(start)
|
|
231
|
+
if (!derived) {
|
|
232
|
+
if (create) throw new Error(noRootMessage(start)) // 写面抛——零落盘零建库
|
|
233
|
+
return null // 读面空账(未注册目录下旧行为亦为空 ⇒ 零回归)
|
|
156
234
|
}
|
|
235
|
+
const file = keyedDbFile(derived.root)
|
|
157
236
|
if (!existsSync(file) && !create) return null
|
|
158
237
|
if (readOnly) return openReadOnly(file)
|
|
159
238
|
try { mkdirSync(ledgerDir, { recursive: true }) } catch { /* 已存在 / 并发建目录竞争——忽略 */ }
|