@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
package/explore-distill.mjs
CHANGED
|
@@ -17,26 +17,50 @@ import { estimateTokens, rebaseMeasured } from "./token-window.mjs"
|
|
|
17
17
|
import { specForModel } from "./model-specs.mjs"
|
|
18
18
|
import { isThinkingRequest } from "./think-off.mjs"
|
|
19
19
|
// 拍平投影与位置换算(写面单点档):本档只读投影、写回落在在途轮的内层。
|
|
20
|
-
import { flattenHistory, flatIndexOf } from "./context-push.mjs"
|
|
20
|
+
import { flattenHistory, flatIndexOf, INJECT_SLOTS, turnBlockOf } from "./context-push.mjs"
|
|
21
|
+
|
|
22
|
+
function activeCompactionSummaryOf(agent) {
|
|
23
|
+
const summaryAt = INJECT_SLOTS.indexOf("compaction-summary")
|
|
24
|
+
const slot = Array.isArray(agent?._inject) ? agent._inject[summaryAt] : null
|
|
25
|
+
if (typeof slot?.content === "string" && slot.content) return slot.content
|
|
26
|
+
const ck = agent?._compactCheckpoint?.compactionSummary
|
|
27
|
+
return typeof ck === "string" && ck ? ck : null
|
|
28
|
+
}
|
|
21
29
|
|
|
22
30
|
/** Read-only knowledge tools counted as "exploration" (execute writes files → never exploration). */
|
|
23
31
|
export const EXPLORE_TOOLS = new Set([
|
|
24
|
-
"read", "grep", "glob", "ls", "code_search", "doc_search", "
|
|
32
|
+
"read", "grep", "glob", "ls", "code_search", "doc_search", "repo_graph",
|
|
25
33
|
])
|
|
26
34
|
|
|
27
35
|
/** Summary prompt for turning a burst of exploration results into a semantic summary. */
|
|
28
|
-
export const EXPLORE_SUMMARY_PROMPT = `You are distilling
|
|
36
|
+
export const EXPLORE_SUMMARY_PROMPT = `You are distilling the read-only exploration already present in the preceding messages of this request.
|
|
37
|
+
|
|
38
|
+
Those preceding read, grep, glob, ls, code_search, doc_search, and repo_graph results are the exploration. Nothing follows this instruction, so do not report the exploration log as empty when those results are present.
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
- Capture WHAT was discovered, WHERE (which files / directories / symbols), and the KEY CONCLUSIONS — do not list tool calls mechanically
|
|
32
|
-
- Keep actionable facts the main agent needs to continue: code locations, function names, file paths, structure, and open questions the exploration raised
|
|
33
|
-
- Drop raw tool-output noise, repeated lines, and verbatim file dumps — keep only what must be remembered
|
|
34
|
-
- Be honest: mark anything not actually verified as "unverified"; do not present guesses as facts
|
|
35
|
-
- Use bullet points; aim for information completeness, not a hard word limit
|
|
40
|
+
Summarize only facts those results actually show: what was found, where (files, directories, symbols), and the key conclusions. Do not list tool calls mechanically. Drop raw dumps and repeated lines. Mark anything those results do not verify as "unverified".
|
|
36
41
|
|
|
37
|
-
|
|
42
|
+
This request ends at the last read-only exploration block. Later edits, batch or ledger changes, verification, and the final answer are absent. Do not state batch status, ledger status, or whether the task is finished. Later messages stay in history and override this summary.
|
|
43
|
+
|
|
44
|
+
Use bullet points.
|
|
38
45
|
`
|
|
39
46
|
|
|
47
|
+
const EXPLORATION_SUMMARY_HEAD = "[Exploration summary — historical evidence only; later messages in this turn override this summary]"
|
|
48
|
+
|
|
49
|
+
/** Names of tool calls that happen after the distilled exploration and therefore stay authoritative. */
|
|
50
|
+
function laterAuthorityLine(history, cut) {
|
|
51
|
+
const names = []
|
|
52
|
+
for (const message of history.slice(cut)) {
|
|
53
|
+
if (message?.role !== "assistant" || !Array.isArray(message.tool_calls)) continue
|
|
54
|
+
for (const call of message.tool_calls) {
|
|
55
|
+
const name = toolCallName(call)
|
|
56
|
+
if (name && !names.includes(name)) names.push(name)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return names.length === 0
|
|
60
|
+
? ""
|
|
61
|
+
: `\n[Later actions remain in history and override this summary: ${names.join(", ")}]`
|
|
62
|
+
}
|
|
63
|
+
|
|
40
64
|
/** tool_calls name across both stored shapes ({function:{name}} and flat {name}). */
|
|
41
65
|
function toolCallName(tc) {
|
|
42
66
|
return tc?.function?.name ?? tc?.name ?? ""
|
|
@@ -96,26 +120,34 @@ async function distillExplorations(history, start, provider, signal, agent, dept
|
|
|
96
120
|
let summary
|
|
97
121
|
try {
|
|
98
122
|
const spec = specForModel(provider?.model)
|
|
99
|
-
|
|
123
|
+
// 模型语义参数逐调用下发(2026-12-03 批,台账 #133):蒸馏要关思考(`thinking: null`,族别 off 标记)
|
|
124
|
+
// 且继承会话其余设置——它们不再是 provider 克隆上的字段(`distillerModelParams` 同时供
|
|
125
|
+
// `isThinkingRequest` 判据与 `opts.modelParams` 一个对象)。
|
|
126
|
+
const distillerModelParams = { ...(agent?.modelParams ?? {}), thinking: null }
|
|
100
127
|
// Silent by design (D11): thinking:null and no onToken/onReasoning — this internal
|
|
101
128
|
// distillation must not stream to the frontend. signal propagates user cancellation.
|
|
102
129
|
// 会话续写形态(§6.15 / D-CC21):messages 由单源构造器 buildCompressMessages 产出(不设第二构造
|
|
103
130
|
// 点);tools 取与回合请求同一声明面(**不带** tool_choice——实测该参数使服务端丢弃 tools 区);
|
|
104
131
|
// 不覆盖 reasoningEffort ⇒ 与回合侧同源(v3 形态)。extras 缺省 ⇒ 退化面 1(无 system / 无 tools)。
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
const resp = await chat(
|
|
132
|
+
// 前缀缓存对齐:已压缩会话的回合请求在 `system` 之后紧跟 `compaction-summary` 格,蒸馏透传同一条
|
|
133
|
+
// 生效摘要(未压缩会话为 `null`),避免从第 1 条消息起击穿 KV Prefix Cache(摘要本身不参与累积)。
|
|
134
|
+
const resp = await chat({ ...provider }, {
|
|
135
|
+
// 会话设置(模型窗口声明 ∥ 档位上限)——蒸馏调用的窗口封顶与会话同源(2026-12-03 批)。
|
|
136
|
+
settings: agent?.config,
|
|
108
137
|
// 来源面(AGENT-LOOP.md §5.5.4):当前 provider 名与当下 spec 由本调用点显式传入,构造器不补默认。
|
|
138
|
+
// 模型语义参数同处下发:与 `isThinkingRequest` 判据同一个对象(不另算一份)。
|
|
139
|
+
modelParams: distillerModelParams,
|
|
109
140
|
messages: buildCompressMessages(
|
|
110
141
|
history,
|
|
111
142
|
cut,
|
|
112
143
|
extras?.systemPrompt,
|
|
113
144
|
EXPLORE_SUMMARY_PROMPT,
|
|
114
|
-
|
|
145
|
+
activeCompactionSummaryOf(agent),
|
|
115
146
|
provider?.name,
|
|
116
147
|
spec,
|
|
117
|
-
isThinkingRequest(
|
|
148
|
+
isThinkingRequest(distillerModelParams, spec),
|
|
118
149
|
Array.isArray(extras?.tools) && extras.tools.length > 0,
|
|
150
|
+
{ sessionHead: agent?._sessionHead, retained: agent?._retained, turnBlock: turnBlockOf(agent) },
|
|
119
151
|
),
|
|
120
152
|
tools: extras?.tools,
|
|
121
153
|
signal,
|
|
@@ -136,7 +168,10 @@ async function distillExplorations(history, start, provider, signal, agent, dept
|
|
|
136
168
|
|
|
137
169
|
const drop = new Set()
|
|
138
170
|
for (const b of blocks) for (let k = b.start; k < b.end; k++) drop.add(k)
|
|
139
|
-
const note = {
|
|
171
|
+
const note = {
|
|
172
|
+
role: "user",
|
|
173
|
+
content: `${EXPLORATION_SUMMARY_HEAD}\n${summary}${laterAuthorityLine(history, cut)}`,
|
|
174
|
+
}
|
|
140
175
|
const next = []
|
|
141
176
|
let inserted = false
|
|
142
177
|
for (let k = 0; k < history.length; k++) {
|
package/generate-title.mjs
CHANGED
|
@@ -17,10 +17,10 @@ import { isRealUserMsg } from "./history-window.mjs"
|
|
|
17
17
|
// #632(缺面族批补 · @ 文件引用对齐 · 2026-09-29):标题源剥离单源——注入形不得进标题请求(核件
|
|
18
18
|
// `file-refs.mjs`;CLI 无注入 ⇒ 零动作;VSC 同位转口 = `panel-session-write.mjs` 的 `generateTitle`)。
|
|
19
19
|
import { stripAtRefs } from "./file-refs.mjs"
|
|
20
|
-
// #363③(off
|
|
20
|
+
// #363③(off 形族单源——`MODEL-SPECS.md` §4「effort 与 off」):禁思考形与「是否可禁」一律引核
|
|
21
21
|
// (`thinkOffShape` / `thinkOffPath`——本档零副本);spec 查表同单源(`specForModel`)。
|
|
22
22
|
import { specForModel } from "./model-specs.mjs"
|
|
23
|
-
import { thinkOffShape, thinkOffPath } from "./think-off.mjs"
|
|
23
|
+
import { thinkOffShape, thinkOffPath, googleThinkingOff } from "./think-off.mjs"
|
|
24
24
|
// 拍平投影(回退支读机读线时用):机读线按轮嵌套。
|
|
25
25
|
import { flattenHistory } from "./context-push.mjs"
|
|
26
26
|
|
|
@@ -47,6 +47,9 @@ export async function generateTitle(userContent, provider) {
|
|
|
47
47
|
// 约定,本档无门)⇒ 不发禁形(假标记:被服务端拒 / 静默失效)。
|
|
48
48
|
const spec = specForModel(provider.model)
|
|
49
49
|
const offShape = thinkOffPath(spec) ? thinkOffShape(spec) : null
|
|
50
|
+
// google 的 wire off 形单源(`googleThinkingOff`——本档零副本)。2026-12-01 实测:`thinkingLevel:"none"`
|
|
51
|
+
// 在上游是枚举外的值(所有受测模型均 400);强制思考族零发,其余族发 `thinkingBudget:0`。
|
|
52
|
+
const googleOff = provider.format === "google" ? googleThinkingOff(spec) : null
|
|
50
53
|
|
|
51
54
|
try {
|
|
52
55
|
// #57(provider-config-family 批):凭据值位 `${env:VAR}` 消费侧解析(CONFIG.md §6.3)——解析抛错
|
|
@@ -73,8 +76,8 @@ export async function generateTitle(userContent, provider) {
|
|
|
73
76
|
contents: [{ role: "user", parts: [{ text }] }],
|
|
74
77
|
generationConfig: {
|
|
75
78
|
maxOutputTokens: MAX_TITLE_TOKENS,
|
|
76
|
-
// google 格式自身的关思考形(wire
|
|
77
|
-
...(
|
|
79
|
+
// google 格式自身的关思考形(wire 原生——经单源 `googleThinkingOff`);无有效 off 路径 ⇒ 零发
|
|
80
|
+
...(googleOff ? { thinkingConfig: googleOff } : {}),
|
|
78
81
|
}, // IK9UZ8
|
|
79
82
|
})
|
|
80
83
|
extract = (data) => data.candidates?.[0]?.content?.parts?.map((p) => p.text || "").join("")
|
package/ledger-close.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* thincoder-core/ledger-close.mjs — 台账收口重锚族(从 `ledger-cmd.mjs` 整族搬出,2026-11-11 ·
|
|
3
|
-
* `docs/core/design/BATCH-RECORD.md` §4.18
|
|
3
|
+
* `docs/core/design/BATCH-RECORD.md` §4.18「收口分流」①)。
|
|
4
4
|
*
|
|
5
|
-
* 为什么拆:`ledger-cmd.mjs` 改前已越 500(判据单源 = `docs/core/
|
|
5
|
+
* 为什么拆:`ledger-cmd.mjs` 改前已越 500(判据单源 = `docs/core/standards/CODE-TIER.md`「文件档位与两个时点」),
|
|
6
6
|
* 而收口重锚族还要在本批加一支(被取代的收口要关掉「每条在途行都须点名」那一臂);刀法 = 按符号边界
|
|
7
7
|
* 整族搬、零语义改。
|
|
8
8
|
*
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* 出口:`ledger-cmd.mjs` 逐名转出(`ledger.mjs` 的公共面零改 ⇒ 调用方零改)。
|
|
11
11
|
* `localDay` 随族搬入(`closeEvidenceText` 依赖它):`ledger-cmd.mjs` 反向 import 取用——依赖单向,无环。
|
|
12
12
|
*
|
|
13
|
-
* 判据原文 = `docs/core/design/LEDGER.md` §10.2 ∥ §2.
|
|
14
|
-
*
|
|
13
|
+
* 判据原文 = `docs/core/design/LEDGER.md` §10.2 ∥ `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.19
|
|
14
|
+
* 写序与补偿;本档只实现。
|
|
15
15
|
*/
|
|
16
16
|
import { openLedger, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
17
17
|
import { filePartOf } from "./ledger-close-proof.mjs"
|
|
@@ -25,7 +25,7 @@ export const localDay = (ms) => { const d = new Date(ms); const p = (n) => Strin
|
|
|
25
25
|
/** 完成清单共用判据:必填数字数组、无重复、每个 id 都属于本批且现态为在途;`requireAllInFlight = true`
|
|
26
26
|
* 时还要求所有在途行都点名。
|
|
27
27
|
* `认账不排期` 只是非活跃行的重锚政策,不能让在途行绕过完成清单。
|
|
28
|
-
* **`requireAllInFlight` 无缺省**(`docs/core/design/BATCH-RECORD.md` §4.18
|
|
28
|
+
* **`requireAllInFlight` 无缺省**(`docs/core/design/BATCH-RECORD.md` §4.18「③ 的实现面」):缺省值就等于
|
|
29
29
|
* 替调用方决定「没做完的行算不算完成」——正常两路传 `true`,被取代的收口传 `false`,写成必传。 */
|
|
30
30
|
function completedSetForRows(completed, rows, { requireAllInFlight } = {}) {
|
|
31
31
|
if (typeof requireAllInFlight !== "boolean") {
|
|
@@ -123,7 +123,7 @@ function retreatInFlightRow(db, base, row, boundary, sentence) {
|
|
|
123
123
|
* 两跳逐跳走既有写核(迁移表 ∥ 写门 ∥ 核销门逐跳判),不直写 SQL;每行结清时把 `closeEvidence`
|
|
124
124
|
* 按既有追加规则写入 `evidence`,前置 `收口 <YYYY-MM-DD>:`(人手证据与收口证据在行上可分)。
|
|
125
125
|
* 任一行拒 ⇒ `ROLLBACK`、台账零改、返回形 `failed`;档账跨存储的写序与补偿不在此承诺
|
|
126
|
-
* (单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.
|
|
126
|
+
* (单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.19)。
|
|
127
127
|
* @returns {{batch:string, settled:{id:number,title:string,from:"在途"|"待核销"}[], conditioned:object[],
|
|
128
128
|
* retreated:object[], alreadyAnchored:object[], failed:string|null}} `settled` 逐行点名每一枚被结清的行(含起点——
|
|
129
129
|
* 两种起点的回执各一行);`conditioned` = 改挂条件的行;`retreated` = 退回边走过的行(仅 `requireAllInFlight = false` 那一路可能非空)。 */
|
package/ledger-cmd.mjs
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
* 的 depthOnly 分支)——一律**动态 import** 本模块链(ledger-db.mjs 静态 import node:sqlite ⇒
|
|
28
28
|
* 消费侧静态 import 会破 W8 契约②)。
|
|
29
29
|
*/
|
|
30
|
-
import { AGING_DAYS, EVIDENCE_TOO_LONG_MAX, PENDING_STATUSES, nowIso, openLedger, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
30
|
+
import { AGING_DAYS, EVIDENCE_TOO_LONG_MAX, PENDING_STATUSES, newUid, nowIso, openLedger, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
31
31
|
import { resolveDeclaredRef } from "./declaration.mjs"
|
|
32
32
|
import { filePartOf, namesPointer } from "./ledger-close-proof.mjs"
|
|
33
33
|
// 本地日界随收口重锚族搬进 `ledger-close.mjs`(`closeEvidenceText` 依赖它)——本档反向 import 取用,
|
|
@@ -160,8 +160,8 @@ export function ledgerAdd({ cwd, row, boundary }) {
|
|
|
160
160
|
wroteBatchTrigger: row.trigger === "归批",
|
|
161
161
|
}, boundary)
|
|
162
162
|
const now = nowIso()
|
|
163
|
-
const r = db.prepare(`INSERT INTO items (kind, status, title, board, req_doc, task_book, evidence, trigger, resume_when, created_at, updated_at) VALUES (?, '待讨论', ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
164
|
-
.run(row.kind, row.title, row.board ?? null, row.req_doc ?? null, row.task_book ?? null, row.evidence ?? null, row.trigger ?? null, row.resume_when ?? null, now, now)
|
|
163
|
+
const r = db.prepare(`INSERT INTO items (uid, kind, status, title, board, req_doc, task_book, evidence, trigger, resume_when, created_at, updated_at) VALUES (?, ?, '待讨论', ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
164
|
+
.run(newUid(), row.kind, row.title, row.board ?? null, row.req_doc ?? null, row.task_book ?? null, row.evidence ?? null, row.trigger ?? null, row.resume_when ?? null, now, now)
|
|
165
165
|
return Number(r.lastInsertRowid)
|
|
166
166
|
} finally { db.close() }
|
|
167
167
|
}
|
package/ledger-consistency.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* thincoder-core/ledger-consistency.mjs — 台账批-账一致性读数族(从 `ledger-cmd.mjs` 整族搬出,
|
|
3
|
-
* 2026-11-11 · `docs/core/design/BATCH-RECORD.md` §4.18
|
|
3
|
+
* 2026-11-11 · `docs/core/design/BATCH-RECORD.md` §4.18「收口分流」②)。
|
|
4
4
|
*
|
|
5
|
-
* 为什么拆:`ledger-cmd.mjs` 改前已越 500(判据单源 = `docs/core/
|
|
5
|
+
* 为什么拆:`ledger-cmd.mjs` 改前已越 500(判据单源 = `docs/core/standards/CODE-TIER.md`「文件档位与两个时点」),
|
|
6
6
|
* 而收口重锚族还要在本批加一支;刀法 = 按符号边界整族搬、零语义改。
|
|
7
7
|
*
|
|
8
8
|
* 族 = 半状态出路表 ∥ `unknown` 出口句 ∥ 半状态项构造 ∥ 批-账一致性读数。
|
|
@@ -25,7 +25,8 @@ import { assertBatchArg, resolveBatchRows } from "./ledger-write-ops.mjs"
|
|
|
25
25
|
|
|
26
26
|
// ── 半状态查询面(设计档 §2.17「半状态查询面」· AC-M2-40——**读,零写**)────────────────────────
|
|
27
27
|
|
|
28
|
-
/**
|
|
28
|
+
/** 半状态六类的出路句(**实现侧持有**:设计档 `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.17
|
|
29
|
+
* 「一致性读数」只列六类半状态名,出路句无设计面——本表即出处,不称设计档单源)。 */
|
|
29
30
|
const HALF_STATE_EXITS = {
|
|
30
31
|
"closed-but-open-rows": "重开(batch status §1 进行中 + note)后重试收口",
|
|
31
32
|
"closed-but-settling": "同上,或 ledger 批量结算单笔补齐",
|
|
@@ -45,14 +46,15 @@ const halfStateOf = (name, rows, exit) => ({
|
|
|
45
46
|
})
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
|
-
* 批-账一致性读数(设计档 §2.17
|
|
49
|
-
*
|
|
49
|
+
* 批-账一致性读数(设计档 §2.17「一致性读数」· AC-M2-40——**只读、零写**:「明确的半状态查询面」,人不必靠一次失败的
|
|
50
|
+
* 写去猜)。六类半状态名逐条照同一节;出路句**实现侧持有**(`HALF_STATE_EXITS` 本表即出处,不称设计档单源);
|
|
51
|
+
* `record` 三态判据单源 = `readBatchStatusLine`。
|
|
50
52
|
* **预定行不计入 `pointer-stale`**:档未建是它的合法态(单源 = `docs/core/design/LEDGER.md` §6.4)。
|
|
51
53
|
* `open-but-unwritable` 的三合取(`record = open` ∧ 本批**无** `在途` 行 ∧ 存在 `待核销` 行)缺一不可——
|
|
52
54
|
* 只按前两条判,会把每个「一行先做完、其余还在做」的合法在途批报成不一致,而恒报不一致的读数等于没有
|
|
53
55
|
* 读数;加上第三条,它判的仍是同一件真事:该批此刻没有一条在途行,所以一个字也写不进去。
|
|
54
56
|
* **`open-but-settled` 是预期读形**(重开一份非空全归档批的必然结果——`已核销` 不回收),不是缺陷;空行集不命中。
|
|
55
|
-
* 判据 = `record !== "closed"` ∧ 行集非空 ∧
|
|
57
|
+
* 判据 = `record !== "closed"` ∧ 行集非空 ∧ 全归档(单源 = `docs/core/design/LEDGER.md` §11.3「补关」——§11.2 是锚批门,全归档补关在 §11.3):`unknown` 照报
|
|
56
58
|
* (不可判必须可见,不许静默当作已收口),但出路句换成「先修 §1」、不印「直接收口」——补关入口恒为
|
|
57
59
|
* `record = open` 且 `halfStates` 恰为唯一本项目。
|
|
58
60
|
* `closed-but-open-rows` 同读 `status` 与 `trigger`:在途恒算;等待态只在 trigger 非条件且非认账不排期时算。
|
package/ledger-db.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ledger-db.mjs — 台账 SQLite 连接 / DDL / 枚举常量(M2
|
|
3
|
-
* docs/core/design/
|
|
2
|
+
* ledger-db.mjs — 台账 SQLite 连接 / DDL / 枚举常量(M2 模块拆分件——机制正文现住
|
|
3
|
+
* docs/core/design/LEDGER.md,拆分计划已随旧模块档退役)。
|
|
4
4
|
*
|
|
5
5
|
* items 表 + 三 CHECK 枚举(kind / status / trigger)+ task_book 咬合 CHECK——枚举机械锁死
|
|
6
6
|
* 靠 DDL(AC-M2-2),不靠应用层 if。归档 = 软删除(状态值,无物理 DELETE——KD-M2-4)。
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* 键归一(2026-09-25 批 ledger-key-normalize · 台账 #286):盘符大写契约直引 `session-slots.mjs`
|
|
18
18
|
* `normalizeCwd`——与 session / checkpoint / traces / peers 同契约,跨端同 cwd 同键同库。
|
|
19
19
|
*/
|
|
20
|
-
import { createHash } from "node:crypto"
|
|
20
|
+
import { createHash, randomBytes } from "node:crypto"
|
|
21
21
|
import { existsSync, mkdirSync, opendirSync } from "node:fs"
|
|
22
22
|
import { join, resolve } from "node:path"
|
|
23
23
|
import { DatabaseSync } from "node:sqlite"
|
|
@@ -29,6 +29,22 @@ import { resolvePathTarget } from "./path-resolve.mjs"
|
|
|
29
29
|
// 归一步单源(KD-LN1——直引,不抽新 util:先例 = peer-domains.mjs / traces/trace-store.mjs)。
|
|
30
30
|
import { normalizeCwd } from "./session-slots.mjs"
|
|
31
31
|
|
|
32
|
+
/** 行身份(跨机唯一——**与整数 `id` 两件事**:`id` 给人读(`#102`、批档头部、对话引用),`uid` 给机器认
|
|
33
|
+
* (将来本地账 ↔ 团队账的关联字段)。UUIDv7(RFC 9562:48 位毫秒时间戳 + 4 位版本 7 + 12 位 rand_a +
|
|
34
|
+
* 2 位变体 + 62 位 rand_b)——选 v7 不选 v4 的理由:时间前缀让字典序 = 创建序,将来按 `uid` 排序或做
|
|
35
|
+
* keyset 游标不需要额外列。
|
|
36
|
+
* @param {number} [now] 毫秒时间戳(注入缝——确定性用例)
|
|
37
|
+
* @returns {string} 36 字符 UUIDv7 文本形 */
|
|
38
|
+
export function newUid(now = Date.now()) {
|
|
39
|
+
const b = Buffer.alloc(16)
|
|
40
|
+
b.writeUIntBE(now, 0, 6) // 48 位毫秒时间戳(大端)——2^48 ms ≈ 公元 10889 年
|
|
41
|
+
randomBytes(10).copy(b, 6) // 其余 80 位随机
|
|
42
|
+
b[6] = (b[6] & 0x0f) | 0x70 // 版本 7
|
|
43
|
+
b[8] = (b[8] & 0x3f) | 0x80 // 变体 10xx
|
|
44
|
+
const h = b.toString("hex")
|
|
45
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`
|
|
46
|
+
}
|
|
47
|
+
|
|
32
48
|
/** 台账库目录(默认 = 用户数据目录;测试注入面 `_setLedgerDirForTest` 覆盖)。 */
|
|
33
49
|
let ledgerDir = join(configDir, "ledger")
|
|
34
50
|
|
|
@@ -156,9 +172,10 @@ export const QUERY_PAGE_SIZE = 10
|
|
|
156
172
|
/** 台账库结构版本(N2 标记**单源**——`PRAGMA user_version` 落标:写面开库 `v < 本值 ⇒ 置`;
|
|
157
173
|
* 读面如实回读(旧库未标 = 0);结构变更(新列 / 新表)同步升号——2026-10-03 read-data-interface 批。
|
|
158
174
|
* 2 = `resume_when` 可空列(设计档 §3.4 P5)。
|
|
159
|
-
* 3 = 同库第二张表 `doc_edges` 与两条索引(文档锚点图 · 机制单源 = `docs/core/design/
|
|
160
|
-
* ——边表与台账行住同一个 SQLite
|
|
161
|
-
|
|
175
|
+
* 3 = 同库第二张表 `doc_edges` 与两条索引(文档锚点图 · 机制单源 = `docs/core/design/LEDGER.md` §2
|
|
176
|
+
* ——边表与台账行住同一个 SQLite 文件,不新建第二个库;历史兼容面,现役图设计归 `docs/core/design/REPO-GRAPH.md`)。
|
|
177
|
+
* 4 = `items.uid` 跨机唯一行身份(UUIDv7——设计档 §2「行身份」;整数 `id` 原样不动)。 */
|
|
178
|
+
export const LEDGER_SCHEMA_VERSION = 4
|
|
162
179
|
|
|
163
180
|
/** 六态允许迁移表(ledgerUpdate 迁移前判,不在表内 → 拒)。
|
|
164
181
|
* `待讨论 → 在途` 是建档点火边;`在途 / 待核销 → 待讨论` 是未完成行的退回边;
|
|
@@ -172,19 +189,21 @@ export const ALLOWED_MIGRATIONS = {
|
|
|
172
189
|
"已废弃": [],
|
|
173
190
|
}
|
|
174
191
|
|
|
175
|
-
/** `doc_edges.kind`
|
|
192
|
+
/** `doc_edges.kind` 的闭集(历史兼容边表 · 机制归属 = `docs/core/design/LEDGER.md` §2「同库保留历史
|
|
193
|
+
* `doc_edges` 表结构」;本常量即闭集单源,不另指设计档小节)。
|
|
176
194
|
* **一份闭集、两个消费面**:本档 DDL 的 CHECK 由它生成;查询面的 `kind` 收窄入参也按它校验
|
|
177
195
|
* (`doc-graph.mjs` 动态 import 本常量)——两边不各持一份字面,两份一漂就是「能存不能查」。 */
|
|
178
196
|
export const DOC_EDGE_KINDS = ["cites", "cites-req", "mentions-case", "case-file", "cites-symbol", "imports"]
|
|
179
197
|
|
|
180
198
|
/** 建表 DDL(幂等——设计档 §2.2 逐字;CHECK 字符串用单引号——KD7 实核)。
|
|
181
199
|
* 两张表同一个 `db.exec`、同一个文件:`items`(台账行)∥ `doc_edges`(文档锚点图的边——机制单源 =
|
|
182
|
-
* `docs/core/design/
|
|
200
|
+
* `docs/core/design/LEDGER.md` §2「同库保留历史 `doc_edges` 表结构」)。边表的三值 `state` 与本档台账的
|
|
183
201
|
* 六态无关:它是「目标在不在盘」的三态(`ok` ∥ `目标不在盘` ∥ `判不了存在性`),与 `doc_check` 的
|
|
184
202
|
* `档缺` / `unverified` 同义不同词。 */
|
|
185
203
|
const DDL = `
|
|
186
204
|
CREATE TABLE IF NOT EXISTS items (
|
|
187
205
|
id INTEGER PRIMARY KEY,
|
|
206
|
+
uid TEXT, -- 跨机唯一行身份(UUIDv7——§2「行身份」;老库回填,见 openLedger)
|
|
188
207
|
kind TEXT NOT NULL CHECK(kind IN ('requirement','tech_todo')),
|
|
189
208
|
status TEXT NOT NULL CHECK(status IN ('待讨论','待设计','在途','待核销','已核销','已废弃')),
|
|
190
209
|
title TEXT NOT NULL,
|
|
@@ -217,8 +236,8 @@ CREATE INDEX IF NOT EXISTS doc_edges_dst ON doc_edges(dst);
|
|
|
217
236
|
CREATE INDEX IF NOT EXISTS doc_edges_scope ON doc_edges(scope);
|
|
218
237
|
`
|
|
219
238
|
|
|
220
|
-
/** 老库幂等迁移共件:items 表缺该可空列 ⇒ ALTER 补列(`executor` ∥ `resume_when`
|
|
221
|
-
* 「复判列在否」逻辑——设计档 §3.4
|
|
239
|
+
/** 老库幂等迁移共件:items 表缺该可空列 ⇒ ALTER 补列(`executor` ∥ `resume_when` ∥ `uid` 三列共用一份
|
|
240
|
+
* 「复判列在否」逻辑——设计档 §3.4 ∥ §2「行身份」)。判据 = PRAGMA table_info 实查(不猜版本号);ALTER
|
|
222
241
|
* duplicate column 竞争 → 复核列在吞 / 不在抛真错。列存在性谓词抽成单元(param exists),
|
|
223
242
|
* 便于并发竞争拍直测。K-LX5:老库零删改、零重建。 */
|
|
224
243
|
export function ensureColumn(db, name, { exists } = {}) {
|
|
@@ -237,6 +256,18 @@ export function ensureColumn(db, name, { exists } = {}) {
|
|
|
237
256
|
/** `executor` 列补列(F-LX1——共件别名,公共面形零改)。 */
|
|
238
257
|
export const ensureExecutorColumn = (db, opts) => ensureColumn(db, "executor", opts)
|
|
239
258
|
|
|
259
|
+
/** 老库 `uid` 回填(设计档 §2「行身份」判据 2):对 `uid IS NULL` 的行逐行补生成 UUIDv7。
|
|
260
|
+
* **只在写面开库跑**(读面 `readOnly` 零 DDL / 零 ALTER / 零回填——与「读面不建库」同一条纪律)。
|
|
261
|
+
* 幂等:无 NULL 行时只是一次空 SELECT(自愈式——任何一次写面开库都把漏网行补齐)。
|
|
262
|
+
* @returns {number} 本笔回填行数(0 = 无待补) */
|
|
263
|
+
function backfillUids(db) {
|
|
264
|
+
const rows = db.prepare("SELECT id FROM items WHERE uid IS NULL").all()
|
|
265
|
+
if (rows.length === 0) return 0
|
|
266
|
+
const upd = db.prepare("UPDATE items SET uid = ? WHERE id = ?")
|
|
267
|
+
for (const r of rows) upd.run(newUid(), r.id)
|
|
268
|
+
return rows.length
|
|
269
|
+
}
|
|
270
|
+
|
|
240
271
|
/** 只读态开库(read-data-interface 批 FR2③——readOnly 句柄 + **零 DDL / 零 ALTER**;先例 = 本包
|
|
241
272
|
* `ledger-migrate.mjs` 的 `openRead`):文件必已在盘;items 表缺失探针 ⇒ 视同空账返回 null
|
|
242
273
|
* (非台账库的 SQLite 档 / 空档——观测面保持「空集」,文件零触)。不可开(坏档 / 非 SQLite)⇒
|
|
@@ -292,6 +323,10 @@ export function openLedger(cwd, { create = false, readOnly = false, boundary } =
|
|
|
292
323
|
db.exec(DDL)
|
|
293
324
|
ensureColumn(db, "executor")
|
|
294
325
|
ensureColumn(db, "resume_when")
|
|
326
|
+
ensureColumn(db, "uid")
|
|
327
|
+
backfillUids(db)
|
|
328
|
+
// 部分唯一索引:回填前允许 NULL 并存(老库过渡态),回填后自然唯一。
|
|
329
|
+
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS items_uid ON items(uid) WHERE uid IS NOT NULL")
|
|
295
330
|
markSchemaVersion(db)
|
|
296
331
|
} catch (e) {
|
|
297
332
|
db.close()
|
package/ledger-migrate.mjs
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* ② `ledger migrate --dry-run | --confirm`:变体键库一次性合并——三件套 = dry-run 零写 / 先备份(拷贝 + 读回同计数)/ 幂等(全字段等值);
|
|
4
4
|
* 六步 = 备份 → 源就绪与列集判 → 单事务迁入(事务内读回核验)→ 回收源(rename 进 `ledger-trash`,不 unlink)→ 报告 → 汇总。执行主体 = 父侧 ops(KD-LN6)。
|
|
5
5
|
* ③ `ledger audit`:逐 `*.db` 定性(目标库 / 变体源 / 空库 / 不可归因(有行)/ 不可读(坏档);stat 失败 ⇒ 读取失败态)+ 候选根集合归因;只报告零动作。
|
|
6
|
-
* 铁律:**
|
|
7
|
-
* 落地后 12 ⇒ 13,设计档 §3.4
|
|
6
|
+
* 铁律:**14 数据列逐字复制(不含 id)+ id 重发**(两库 id 空间重叠;列集单源 = `DATA_COLUMNS`——`resume_when`
|
|
7
|
+
* 落地后 12 ⇒ 13、`uid` 落地后 13 ⇒ 14,设计档 §3.4 ∥ §2「行身份」);备份 / 回收目录 = `ledgerDirPath()` 的**兄弟位**(§2.11#6 ⇒ 夹具根外零写);
|
|
8
8
|
* 迁移走直接 SQL(§6.1 写门不拦迁移本身 ⇒ 风险行出旗不拦截);本档静态 import node:sqlite ⇒ 消费侧一律动态 import(W8 契约②——同 ledger-db.mjs)。
|
|
9
9
|
*/
|
|
10
10
|
import { createHash } from "node:crypto"
|
|
@@ -13,23 +13,33 @@ import { basename, dirname, join, resolve } from "node:path"
|
|
|
13
13
|
import { DatabaseSync } from "node:sqlite"
|
|
14
14
|
|
|
15
15
|
import { resolveDeclaredRef } from "./declaration.mjs"
|
|
16
|
-
import { ledgerDirPath, ledgerKey, openLedger, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
16
|
+
import { ledgerDirPath, ledgerKey, newUid, openLedger, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
17
17
|
import { resolveProjectRoot } from "./manifest.mjs"
|
|
18
18
|
|
|
19
|
-
/**
|
|
19
|
+
/** 14 数据列(**等值比对列集**——不含 id:id 重发,§2.2 语义保全 / AC-M2-12;`resume_when` 落地后
|
|
20
|
+
* 12 ⇒ 13——设计档 §3.4;`uid` 落地后 13 ⇒ 14——设计档 §2「行身份」判据 3)。
|
|
21
|
+
* **`uid` 追加在末尾**:`writeGateFlag` 按位置取 `values[1]`(status)与 `values[5]`(task_book),
|
|
22
|
+
* 插在中间会静默错位。 */
|
|
20
23
|
export const DATA_COLUMNS = [
|
|
21
24
|
"kind", "status", "title", "board", "req_doc", "task_book",
|
|
22
25
|
"evidence", "trigger", "executor", "created_at", "updated_at", "closed_at", "resume_when",
|
|
26
|
+
"uid",
|
|
23
27
|
]
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
|
|
28
|
+
/** 旧库可缺列容忍集:`executor`(F-LX1 前的旧 DDL)∥ `resume_when`(P5 落地前的旧 DDL——设计档 §3.4)
|
|
29
|
+
* ∥ `uid`(行身份落地前的旧 DDL——设计档 §2「行身份」判据 3)。缺的列全落在集内 ⇒ 照迁:前两列按
|
|
30
|
+
* NULL 映射,`uid` **当场生成**(留 NULL 会让迁入行在下次写面开库前无身份,读面回 `uid: null`)。
|
|
31
|
+
* 其余任一数据列缺 ⇒ 列集不可归因,拒(fail-closed)。 */
|
|
32
|
+
const TOLERATED_MISSING = ["executor", "resume_when", "uid"]
|
|
27
33
|
/** 备份面根 / 回收面根(`ledgerDirPath()` 的**兄弟位**——同派生于可覆盖基,§2.11#6)。 */
|
|
28
34
|
export function ledgerBackupRoot() { return join(dirname(ledgerDirPath()), "ledger-backup") }
|
|
29
35
|
export function ledgerTrashRoot() { return join(dirname(ledgerDirPath()), "ledger-trash") }
|
|
30
36
|
|
|
31
37
|
const sha16 = (s) => createHash("sha1").update(s).digest("hex").slice(0, 16)
|
|
32
38
|
const isFile = (p) => { try { return statSync(p).isFile() } catch { return false } }
|
|
39
|
+
/** 去重签名(**不含 `uid`**):`uid` 是行身份,不是内容——把它算进等值护栏会让「同内容两行」不再去重,
|
|
40
|
+
* 也会让重跑迁移把同一行判成新行。等值核验(`verifyImported`)仍用全列 `sig`——目标行不得被动过。 */
|
|
41
|
+
const UID_AT = DATA_COLUMNS.indexOf("uid")
|
|
42
|
+
const dedupSig = (values) => JSON.stringify(values.filter((_, i) => i !== UID_AT))
|
|
33
43
|
const sig = (values) => JSON.stringify(values)
|
|
34
44
|
const byStatus = (rows) => rows.reduce((m, r) => { const s = r.values[1]; m[s] = (m[s] ?? 0) + 1; return m }, {})
|
|
35
45
|
const openRead = (file) => new DatabaseSync(file, { readOnly: true }) // 源库 / 备份读回 / 审计一律只读
|
|
@@ -56,7 +66,12 @@ function batchStamp(date) {
|
|
|
56
66
|
function readRows(db, missing = []) {
|
|
57
67
|
const cols = DATA_COLUMNS.filter((c) => !missing.includes(c))
|
|
58
68
|
return db.prepare(`SELECT id, ${cols.join(", ")} FROM items ORDER BY id`).all()
|
|
59
|
-
.map((r) => ({
|
|
69
|
+
.map((r) => ({
|
|
70
|
+
id: Number(r.id),
|
|
71
|
+
// 缺 `uid` 的旧源:**当场生成**而不是留 NULL——否则迁入行在下次写面开库前一直无身份,
|
|
72
|
+
// 读面回 `uid: null`,与 AC-M2-43「每行有跨机唯一 uid」不符。生成值随行落库,等值核验两侧一致。
|
|
73
|
+
values: DATA_COLUMNS.map((c) => (missing.includes(c) ? (c === "uid" ? newUid() : null) : r[c] ?? null)),
|
|
74
|
+
}))
|
|
60
75
|
}
|
|
61
76
|
/** 源描述(dry-run / 步 2 / 备份读回 / 审计共用的只读面;失败收为 `error` 而非抛)。`refusal` = 就绪判:不可读 / 无 items 表 / 缺 `executor` 以外数据列 ⇒ 拒;仅缺 `executor`(旧 DDL 正常态)⇒ NULL 映射照迁(§2.2 步 2 / KD-LN10)。 */
|
|
62
77
|
function describeSource(file) {
|
|
@@ -198,7 +213,7 @@ export function runLedgerMigrate(args, { cwd = process.cwd(), out = console.log,
|
|
|
198
213
|
try {
|
|
199
214
|
db.exec(`PRAGMA busy_timeout = ${Math.max(0, Math.floor(busyMs))}`)
|
|
200
215
|
const existing = readRows(db)
|
|
201
|
-
const seen = new Set(existing.map((r) =>
|
|
216
|
+
const seen = new Set(existing.map((r) => dedupSig(r.values)))
|
|
202
217
|
const existingById = new Map(existing.map((r) => [r.id, sig(r.values)]))
|
|
203
218
|
const inserted = []
|
|
204
219
|
db.exec("BEGIN")
|
|
@@ -207,7 +222,7 @@ export function runLedgerMigrate(args, { cwd = process.cwd(), out = console.log,
|
|
|
207
222
|
for (const s of plan.sources) {
|
|
208
223
|
const entry = { key: s.key, path: s.path, rows: s.desc.rows, migrated: 0, skipped: 0, idMap: {}, missingColumns: s.desc.missing }
|
|
209
224
|
for (const row of readRowsOf(s.path, s.desc.missing)) {
|
|
210
|
-
const s0 =
|
|
225
|
+
const s0 = dedupSig(row.values)
|
|
211
226
|
if (seen.has(s0)) { entry.skipped++; report.skippedRows.push({ source: s.key, id: row.id }); continue }
|
|
212
227
|
const newId = Number(insert.run(...row.values).lastInsertRowid)
|
|
213
228
|
entry.idMap[String(row.id)] = newId
|
package/ledger-read.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* 两出口共用的**统一序列化单源**(N1:零 SQL 复刻、零表名泄漏——行集直用核 `ledgerQuery`);
|
|
7
7
|
* ② `runLedgerList(args, opts)` —— CLI 命令 runner(严格解析 · fail-closed——§2.7 / §3.1)。
|
|
8
8
|
*
|
|
9
|
-
* 载荷(§3.2 冻结形):`{ projects: [{ root, name, schemaVersion, rows }] }`;行 = `id` +
|
|
9
|
+
* 载荷(§3.2 冻结形):`{ projects: [{ root, name, schemaVersion, rows }] }`;行 = `id` + 14 数据列
|
|
10
10
|
* (列集单源 = `DATA_COLUMNS`——D-10 直引,不另立副本;`evidence` 仅 full 入行);缺列(旧库)按
|
|
11
11
|
* null 归一;`schemaVersion` = 库 `PRAGMA user_version` 如实回读(旧库未标 = 0);库不在 / 非台账库
|
|
12
12
|
* ⇒ 项目不出项(空集)。
|
package/ledger-write-ops.mjs
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
import { basename, isAbsolute, relative, resolve } from "node:path"
|
|
21
21
|
|
|
22
|
-
import { ALLOWED_MIGRATIONS, nowIso, openLedger, PENDING_STATUSES, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
22
|
+
import { ALLOWED_MIGRATIONS, newUid, nowIso, openLedger, PENDING_STATUSES, resolveLedgerRoot } from "./ledger-db.mjs"
|
|
23
23
|
import { declaredPublicRoots, resolveDeclaredRef } from "./declaration.mjs"
|
|
24
24
|
import { batchDirs, isBatchFace } from "./doc-check/existence.mjs" // 批档根集与前缀包含判据单源(§6.4 第三支——不另造根枚举)
|
|
25
25
|
import { assertAnchorBatchSettleable, assertCloseOutProof, filePartOf, isReservedPointer, namesPointer, RESERVED_POINTER_MARKER } from "./ledger-close-proof.mjs"
|
|
@@ -329,7 +329,7 @@ async function sameFaceScan({ cwd, boundary, face }) {
|
|
|
329
329
|
* @param {{cwd:string, ledgerNo:number|null, newDebt:string|null, targetAbs:string, taskBook:string, title:string,
|
|
330
330
|
* kind:string, board:string|null, boundary?:string, executorSessionId?:string|null, agent?:object}} o
|
|
331
331
|
* `newDebt` = 非空字符串(为什么这不是候选里那几条);空串 ∥ 全空白 ⇒ 按未给判;与 `ledger` 同携 ⇒ 拒。
|
|
332
|
-
* @param {(id:number)=>void} writeRecord — 写批档(`wx`
|
|
332
|
+
* @param {(id:number, rowTitle:string)=>void} writeRecord — 写批档(`wx` 独占创建;真实行号与行标题已可得)
|
|
333
333
|
* @returns {Promise<{id:number, created:boolean, claimed?:boolean, tookOver:"new"|"discussed"|"designed"|"live",
|
|
334
334
|
* normalized:boolean, sameFace:{face:string|null, n:number, m:number, declared:string|null}|null}>} `claimed` = 本笔认领了预定行
|
|
335
335
|
* (不开新行);`normalized` = 接手支把带预定标记的指针同笔改写成普通形(`true` ⇒ 本笔有一次台账写;
|
|
@@ -375,8 +375,8 @@ export async function ledgerBindBatch({ cwd, ledgerNo = null, newDebt = null, ta
|
|
|
375
375
|
// 步 2:先落一行不带指针的 `待讨论`(`在途` 被 DDL 咬合 CHECK 强制非空指针)+ `trigger` 不填;
|
|
376
376
|
// 声明新事者同拍带理由入 `evidence`(步 4 的值计算在 `patch.evidence` 缺省时保留行现值)
|
|
377
377
|
const now = nowIso()
|
|
378
|
-
const r = db.prepare(`INSERT INTO items (kind, status, title, board, evidence, created_at, updated_at) VALUES (?, '待讨论', ?, ?, ?, ?, ?)`)
|
|
379
|
-
.run(kind, title, board, newDebtReason ? `${NEW_DEBT_EVIDENCE_PREFIX}${newDebtReason}` : null, now, now)
|
|
378
|
+
const r = db.prepare(`INSERT INTO items (uid, kind, status, title, board, evidence, created_at, updated_at) VALUES (?, ?, '待讨论', ?, ?, ?, ?, ?)`)
|
|
379
|
+
.run(newUid(), kind, title, board, newDebtReason ? `${NEW_DEBT_EVIDENCE_PREFIX}${newDebtReason}` : null, now, now)
|
|
380
380
|
id = Number(r.lastInsertRowid)
|
|
381
381
|
created = true
|
|
382
382
|
row = db.prepare("SELECT * FROM items WHERE id = ?").get(id)
|
|
@@ -393,7 +393,7 @@ export async function ledgerBindBatch({ cwd, ledgerNo = null, newDebt = null, ta
|
|
|
393
393
|
// 判成「另一批持有」——`hit.ok` 判的恰恰是在盘。
|
|
394
394
|
if (pathKey(abs) !== pathKey(targetAbs)) throw new Error(bindOwnedError(id, row.task_book))
|
|
395
395
|
// 接管(不动状态):档可能已不在盘(残留态)——照写一次 `wx`,撞已有档时由调用方报
|
|
396
|
-
writeRecord(id)
|
|
396
|
+
writeRecord(id, row.title ?? title ?? "")
|
|
397
397
|
// 接手同笔收编预定标记(§6.4):档此刻已由本批建立,标记的「档未建」语义当场失效;留着的指针还会
|
|
398
398
|
// 让一致性检查把它当预定行整条豁免(`ledger-consistency` 对 `isReservedPointer` 的行不判)⇒ 从此
|
|
399
399
|
// 不再受检。普通指针这一支零台账写——带标记才是接手支唯一的那笔 UPDATE,且档已落盘、过得了写门。
|
|
@@ -404,7 +404,7 @@ export async function ledgerBindBatch({ cwd, ledgerNo = null, newDebt = null, ta
|
|
|
404
404
|
}
|
|
405
405
|
}
|
|
406
406
|
// 步 3:落档——必须先于步 4(写门两形都判「解析出的档在盘」)
|
|
407
|
-
writeRecord(id)
|
|
407
|
+
writeRecord(id, row?.title ?? title ?? "")
|
|
408
408
|
// 步 4:行写入走同一函数体(迁移表 → 机检 → executor → 写门 → UPDATE),不直写 SQL。
|
|
409
409
|
// 认领径的 `task_book` 写规范形(`taskBook` 由调用方按 `relative(root, abs)` 给)⇒ 标记剥掉,
|
|
410
410
|
// 该行此后是普通行集成员(`filePartOf` 同址,选行判据零改)。
|
package/ledger.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ledger.mjs — 台账(需求池 / 技术待办)单一权威源(M2
|
|
3
|
-
* docs/core/design/
|
|
2
|
+
* ledger.mjs — 台账(需求池 / 技术待办)单一权威源(M2 模块——机制正文现住
|
|
3
|
+
* docs/core/design/LEDGER.md,拆分计划已随旧模块档退役)。
|
|
4
4
|
*
|
|
5
5
|
* v1 md 台账(docs/TODO.md 扫描器)→ SQLite 单表(ledger.db,项目级、不进 git):六态 CHECK 机械
|
|
6
6
|
* 锁死、COUNT 单源计数、事务保证收口原子、归档 = 软删除。行龄源 = SQLite 时间戳(无 git blame)。
|