@thincoder/core 0.11.1 → 0.11.3
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 +64 -2
- package/advisor/compaction.mjs +7 -5
- package/advisor/convergence.mjs +34 -3
- package/advisor/history.mjs +24 -11
- package/advisor/loop.mjs +49 -12
- package/advisor/messages.mjs +114 -33
- package/advisor/project-context.mjs +171 -42
- package/advisor/run.mjs +24 -10
- package/advisor.mjs +36 -46
- package/agent/batch-attribution.mjs +87 -0
- package/agent/batch-changed-surface.mjs +129 -0
- package/agent/chat-call.mjs +14 -9
- package/agent/completion.mjs +70 -41
- package/agent/continue-decision.mjs +14 -8
- package/agent/digest-account.mjs +133 -59
- package/agent/dispatch-gates.mjs +6 -0
- package/agent/dispatch-run.mjs +1 -1
- package/agent/dispatch.mjs +10 -4
- package/agent/family-tools.mjs +37 -23
- package/agent/helpers.mjs +139 -73
- package/agent/image-heal.mjs +18 -4
- package/agent/offload.mjs +25 -29
- package/agent/post-turn.mjs +36 -35
- package/agent/record-results.mjs +39 -7
- package/agent/run-stages.mjs +132 -52
- package/agent/run-start.mjs +150 -78
- package/agent/setup-reminders.mjs +194 -91
- package/agent/setup.mjs +219 -140
- package/agent/spawn-child.mjs +40 -3
- package/agent/suspension.mjs +43 -15
- package/agent/timers.mjs +9 -4
- package/agent/turn-domain-text.mjs +105 -0
- package/agent/turn-loop.mjs +84 -27
- package/agent/turn-projection.mjs +286 -0
- package/agent/turn-task-state.mjs +274 -0
- package/agent/turn-tool-process.mjs +163 -0
- package/agent/write-gate-ledger.mjs +569 -0
- package/agent/write-gate.mjs +29 -352
- package/agent-tools/advisor-async.mjs +35 -172
- package/agent-tools/advisor-launch.mjs +178 -0
- package/agent-tools/advisor-settle.mjs +28 -1
- package/agent-tools/advisor.mjs +37 -48
- package/agent-tools/async-discard.mjs +7 -2
- package/agent-tools/async-settle.mjs +18 -12
- package/agent-tools/audit-block.mjs +6 -6
- package/agent-tools/bash-async.mjs +5 -2
- package/agent-tools/batch-closeout.mjs +223 -35
- package/agent-tools/batch-gate.mjs +142 -342
- package/agent-tools/batch-ledger-bind.mjs +240 -20
- package/agent-tools/batch-ledger-view.mjs +109 -0
- package/agent-tools/batch-lifecycle.mjs +197 -48
- package/agent-tools/batch-paths.mjs +29 -8
- package/agent-tools/batch-read.mjs +151 -4
- package/agent-tools/batch-record-write.mjs +417 -0
- package/agent-tools/batch-regions.mjs +215 -0
- package/agent-tools/batch-reopen.mjs +23 -11
- package/agent-tools/batch-segment-vocab.mjs +459 -0
- package/agent-tools/batch-skeleton.mjs +196 -239
- package/agent-tools/batch-supersede.mjs +232 -0
- package/agent-tools/batch-tier-gate.mjs +214 -0
- package/agent-tools/batch.mjs +192 -439
- package/agent-tools/browser-async.mjs +5 -2
- package/agent-tools/consult.mjs +17 -11
- package/agent-tools/context.mjs +76 -33
- package/agent-tools/design-review-record.mjs +225 -0
- package/agent-tools/design-slots.mjs +1 -1
- package/agent-tools/design-token.mjs +279 -81
- package/agent-tools/digest-budget.mjs +10 -9
- package/agent-tools/doc-check.mjs +43 -33
- package/agent-tools/doc-graph.mjs +140 -0
- package/agent-tools/eng.mjs +12 -12
- package/agent-tools/escalate-async.mjs +5 -5
- package/agent-tools/goal.mjs +5 -2
- package/agent-tools/judge-domain.mjs +347 -0
- package/agent-tools/manifest.mjs +34 -18
- package/agent-tools/parent-channel.mjs +6 -3
- package/agent-tools/plan.mjs +11 -10
- package/agent-tools/read-history.mjs +112 -11
- package/agent-tools/review-facts.mjs +19 -6
- package/agent-tools/settings.mjs +23 -2
- package/agent-tools/skill.mjs +7 -8
- package/agent-tools/spawn-gates.mjs +14 -24
- package/agent-tools/ssh-session.mjs +8 -5
- package/agent-tools/ssh-text.mjs +155 -0
- package/agent-tools/ssh-transfer.mjs +1 -1
- package/agent-tools/ssh-transport.mjs +19 -148
- package/agent-tools/subagent-actions-query.mjs +10 -6
- package/agent-tools/subagent-actions.mjs +2 -2
- package/agent-tools/subagent-async.mjs +19 -17
- package/agent-tools/subagent-brief.mjs +76 -0
- package/agent-tools/subagent-design-token.mjs +168 -0
- package/agent-tools/subagent-run.mjs +1 -1
- package/agent-tools/subagent-scheduler.mjs +13 -13
- package/agent-tools/subagent-spawn.mjs +92 -112
- package/agent-tools/subagent.mjs +16 -28
- package/agent-tools/task.mjs +0 -8
- package/agent-tools/taskbook-block.mjs +94 -0
- package/agent-tools/timer.mjs +12 -8
- package/agent-tools/verify.mjs +13 -4
- package/agent-tools.mjs +7 -3
- package/agent.mjs +26 -7
- package/attachments.mjs +3 -3
- package/auto-think.mjs +5 -2
- package/browser/actions.mjs +9 -3
- package/browser/cdp.mjs +45 -17
- package/browser/launch.mjs +25 -43
- package/browser/pool.mjs +299 -0
- package/browser/queue.mjs +2 -0
- package/browser/session-state.mjs +450 -0
- package/browser/session.mjs +127 -151
- package/browser/snapshot.mjs +89 -0
- package/client-identity.mjs +1 -1
- package/compaction-work-state.mjs +230 -0
- package/compress-form.mjs +41 -6
- package/config-defaults.mjs +7 -3
- package/config-migrate.mjs +40 -0
- package/config-normalize.mjs +28 -2
- package/config-reload.mjs +59 -0
- package/config.mjs +110 -110
- package/context-degrade.mjs +93 -34
- package/context-echo.mjs +6 -15
- package/context-push.mjs +328 -31
- package/context.mjs +298 -170
- package/conventions.mjs +11 -9
- package/declaration.mjs +126 -22
- package/declared-repos.mjs +94 -0
- package/doc-check/doc-names.mjs +73 -0
- package/doc-check/doc-tokens.mjs +110 -0
- package/doc-check/existence.mjs +160 -0
- package/doc-check/index.mjs +309 -213
- package/doc-check/line-refs.mjs +27 -101
- package/doc-check/paragraph.mjs +218 -102
- package/doc-graph/anchors.mjs +474 -0
- package/doc-graph/doc-batch-face.mjs +59 -0
- package/doc-graph/doc-suffix.mjs +27 -0
- package/doc-graph/graph-tokens.mjs +160 -0
- package/doc-graph.mjs +778 -0
- package/escape.mjs +6 -5
- package/explore-distill.mjs +41 -8
- package/generate-title.mjs +10 -1
- package/i18n.mjs +1 -1
- package/instance-inbox-watch.mjs +272 -0
- package/instance-inbox.mjs +218 -0
- package/instance-registry.mjs +179 -0
- package/knowledge/README.md +3 -0
- package/knowledge/prompt-resources.md +10 -0
- package/ledger-close-proof.mjs +100 -49
- package/ledger-close.mjs +188 -0
- package/ledger-cmd.mjs +47 -91
- package/ledger-consistency.mjs +113 -0
- package/ledger-db.mjs +47 -15
- package/ledger-executors.mjs +1 -1
- package/ledger-tools.mjs +121 -62
- package/ledger-write-ops.mjs +162 -46
- package/ledger.mjs +1 -1
- package/manifest-discovery.mjs +3 -1
- package/manifest-schema.mjs +27 -5
- package/memory/docs.mjs +51 -15
- package/memory/file-list.mjs +6 -0
- package/memory/schema.mjs +2 -2
- package/model-specs.mjs +87 -21
- package/origin-guard.mjs +190 -0
- package/package.json +3 -1
- package/path-resolve.mjs +338 -57
- package/prompt-files.mjs +114 -2
- package/prompt-overlays.mjs +21 -11
- package/prompts/advisor-design.md +96 -36
- package/prompts/advisor-round1.md +48 -4
- package/prompts/advisor-round2.md +54 -10
- package/prompts/advisor-round3.md +53 -9
- package/prompts/common.md +99 -434
- package/prompts/consult-base.md +7 -4
- package/prompts/discipline-engineering.md +504 -295
- package/prompts/discipline-normal.md +106 -281
- package/prompts/knowledge/business-gates.md +30 -0
- package/prompts/knowledge/decision-quality.md +21 -0
- package/prompts/knowledge/doc-lookup.md +30 -0
- package/prompts/knowledge/document-writing.md +33 -0
- package/prompts/knowledge/documentation-evaluation.md +48 -0
- package/prompts/knowledge/evidence-discipline.md +38 -0
- package/prompts/knowledge/normal-coding-workflow.md +110 -0
- package/prompts/knowledge/normal-delivery-collaboration.md +124 -0
- package/prompts/knowledge/normal-work-management.md +60 -0
- package/prompts/knowledge/product-security.md +28 -0
- package/prompts/knowledge/project-records.md +73 -0
- package/prompts/knowledge/tool-advisor.md +29 -0
- package/prompts/knowledge/tool-apply-patch.md +17 -0
- package/prompts/knowledge/tool-bash.md +48 -0
- package/prompts/knowledge/tool-batch-eng-coder.md +19 -0
- package/prompts/knowledge/tool-batch-eng-designer.md +20 -0
- package/prompts/knowledge/tool-batch-review.md +18 -0
- package/prompts/knowledge/tool-batch.md +94 -0
- package/prompts/knowledge/tool-browser.md +68 -0
- package/prompts/knowledge/tool-code-search.md +4 -0
- package/prompts/knowledge/tool-consult-start.md +12 -0
- package/prompts/knowledge/tool-consult-stop.md +6 -0
- package/prompts/knowledge/tool-context.md +6 -0
- package/prompts/knowledge/tool-delete.md +21 -0
- package/prompts/knowledge/tool-doc-check.md +71 -0
- package/prompts/knowledge/tool-doc-graph-read.md +20 -0
- package/prompts/knowledge/tool-doc-graph.md +49 -0
- package/prompts/knowledge/tool-doc-search.md +6 -0
- package/prompts/knowledge/tool-edit.md +41 -0
- package/prompts/knowledge/tool-eng.md +2 -0
- package/prompts/knowledge/tool-execute.md +21 -0
- package/prompts/knowledge/tool-fetch.md +18 -0
- package/prompts/knowledge/tool-file-ops.md +21 -0
- package/prompts/knowledge/tool-get-current-time.md +7 -0
- package/prompts/knowledge/tool-git-read.md +21 -0
- package/prompts/knowledge/tool-git.md +29 -0
- package/prompts/knowledge/tool-glob.md +17 -0
- package/prompts/knowledge/tool-goal.md +8 -0
- package/prompts/knowledge/tool-grep.md +21 -0
- package/prompts/knowledge/tool-hashline-edit.md +16 -0
- package/prompts/knowledge/tool-insert-after.md +17 -0
- package/prompts/knowledge/tool-instance-list.md +6 -0
- package/prompts/knowledge/tool-instance-send.md +10 -0
- package/prompts/knowledge/tool-knowledge.md +18 -0
- package/prompts/knowledge/tool-ledger-read.md +28 -0
- package/prompts/knowledge/tool-ledger.md +46 -0
- package/prompts/knowledge/tool-lint.md +16 -0
- package/prompts/knowledge/tool-ls.md +15 -0
- package/prompts/knowledge/tool-lsp.md +11 -0
- package/prompts/knowledge/tool-main-history.md +2 -0
- package/prompts/knowledge/tool-manifest-read.md +14 -0
- package/prompts/knowledge/tool-manifest.md +34 -0
- package/prompts/knowledge/tool-memory-read.md +20 -0
- package/prompts/knowledge/tool-memory.md +19 -0
- package/prompts/knowledge/tool-notify-parent.md +12 -0
- package/prompts/knowledge/tool-operations.md +102 -0
- package/prompts/knowledge/tool-peer-instances.md +2 -0
- package/prompts/knowledge/tool-plan.md +5 -0
- package/prompts/knowledge/tool-process-read.md +13 -0
- package/prompts/knowledge/tool-process.md +15 -0
- package/prompts/knowledge/tool-question.md +16 -0
- package/prompts/knowledge/tool-read-history-batch.md +22 -0
- package/prompts/knowledge/tool-read-history.md +35 -0
- package/prompts/knowledge/tool-read-image.md +11 -0
- package/prompts/knowledge/tool-read.md +22 -0
- package/prompts/knowledge/tool-recent-changes.md +6 -0
- package/prompts/knowledge/tool-repo-outline.md +4 -0
- package/prompts/knowledge/tool-settings-read.md +12 -0
- package/prompts/knowledge/tool-settings.md +17 -0
- package/prompts/knowledge/tool-skill.md +6 -0
- package/prompts/knowledge/tool-ssh.md +74 -0
- package/prompts/knowledge/tool-subagent.md +57 -0
- package/prompts/knowledge/tool-task.md +14 -0
- package/prompts/knowledge/tool-team-ledger-read.md +11 -0
- package/prompts/knowledge/tool-team-ledger.md +15 -0
- package/prompts/knowledge/tool-timer.md +23 -0
- package/prompts/knowledge/tool-tree.md +16 -0
- package/prompts/knowledge/tool-verify.md +19 -0
- package/prompts/knowledge/tool-wait-for.md +25 -0
- package/prompts/knowledge/tool-websearch.md +19 -0
- package/prompts/knowledge/tool-write.md +12 -0
- package/prompts/knowledge/upstream-protocol.md +45 -0
- package/prompts/persona-coder.md +1 -3
- package/prompts/persona-eng-coder.md +24 -6
- package/prompts/persona-eng-designer.md +150 -171
- package/prompts/persona-engineering.md +531 -442
- package/prompts/persona-explore.md +1 -3
- package/prompts/persona-normal.md +17 -9
- package/prompts/persona-plan.md +5 -3
- package/prompts/persona-tester.md +0 -2
- package/prompts/subagent-base.md +2 -2
- package/provider/anthropic.mjs +10 -6
- package/provider/core.mjs +8 -5
- package/provider/errors.mjs +63 -4
- package/provider/google.mjs +29 -10
- package/provider/reasoning-echo.mjs +16 -0
- package/provider/responses.mjs +7 -3
- package/provider/sse.mjs +11 -6
- package/provider-flows.mjs +2 -2
- package/rules.mjs +11 -10
- package/session-index-build.mjs +1 -1
- package/session-index-pass.mjs +1 -1
- package/session-index-query.mjs +1 -1
- package/session-index.mjs +1 -1
- package/session-lifecycle.mjs +131 -42
- package/session-slot-write.mjs +38 -6
- package/session.mjs +30 -11
- package/shell-identity.mjs +129 -22
- package/skip-dirs.mjs +26 -2
- package/team-ledger.mjs +34 -30
- package/text-budget.mjs +1 -1
- package/think-off.mjs +27 -0
- package/token-ttl.mjs +74 -5
- package/token-window.mjs +371 -47
- package/tool-docs/advisor.md +2 -27
- package/tool-docs/apply_patch.md +1 -15
- package/tool-docs/bash.md +2 -43
- package/tool-docs/batch-eng-coder.md +3 -0
- package/tool-docs/batch-eng-designer.md +3 -0
- package/tool-docs/batch-review.md +3 -0
- package/tool-docs/batch.md +2 -84
- package/tool-docs/browser.md +2 -49
- package/tool-docs/code_search.md +2 -2
- package/tool-docs/consult_start.md +2 -8
- package/tool-docs/consult_stop.md +2 -4
- package/tool-docs/context.md +1 -5
- package/tool-docs/delete.md +2 -17
- package/tool-docs/doc_check.md +2 -107
- package/tool-docs/doc_graph-read.md +3 -0
- package/tool-docs/doc_graph.md +3 -0
- package/tool-docs/doc_search.md +2 -4
- package/tool-docs/edit.md +1 -38
- package/tool-docs/eng.md +3 -1
- package/tool-docs/execute.md +2 -18
- package/tool-docs/fetch.md +2 -14
- package/tool-docs/file_ops.md +2 -18
- package/tool-docs/get_current_time.md +1 -6
- package/tool-docs/git-read.md +3 -0
- package/tool-docs/git.md +2 -35
- package/tool-docs/glob.md +2 -10
- package/tool-docs/goal.md +1 -6
- package/tool-docs/grep.md +2 -18
- package/tool-docs/hashline_edit.md +2 -15
- package/tool-docs/insert_after.md +2 -16
- package/tool-docs/instance_list.md +3 -0
- package/tool-docs/instance_send.md +3 -0
- package/tool-docs/knowledge.md +3 -0
- package/tool-docs/ledger-read.md +3 -0
- package/tool-docs/ledger.md +3 -8
- package/tool-docs/lint.md +2 -10
- package/tool-docs/ls.md +2 -11
- package/tool-docs/lsp.md +2 -9
- package/tool-docs/main_history.md +3 -1
- package/tool-docs/manifest-read.md +2 -8
- package/tool-docs/manifest.md +2 -35
- package/tool-docs/memory-read.md +3 -0
- package/tool-docs/memory.md +2 -20
- package/tool-docs/notify_parent.md +2 -13
- package/tool-docs/peer_instances.md +3 -1
- package/tool-docs/plan.md +1 -5
- package/tool-docs/process-read.md +3 -0
- package/tool-docs/process.md +2 -13
- package/tool-docs/question.md +3 -17
- package/tool-docs/read.md +2 -19
- package/tool-docs/read_history-batch.md +3 -0
- package/tool-docs/read_history.md +2 -18
- package/tool-docs/read_image.md +2 -9
- package/tool-docs/recent_changes.md +2 -2
- package/tool-docs/repo_outline.md +2 -2
- package/tool-docs/settings-read.md +3 -0
- package/tool-docs/settings.md +1 -12
- package/tool-docs/skill.md +2 -6
- package/tool-docs/ssh.md +1 -62
- package/tool-docs/subagent.md +2 -100
- package/tool-docs/task.md +1 -7
- package/tool-docs/team_ledger-read.md +3 -0
- package/tool-docs/team_ledger.md +3 -9
- package/tool-docs/timer.md +2 -19
- package/tool-docs/tree.md +2 -13
- package/tool-docs/verify.md +2 -12
- package/tool-docs/wait_for.md +2 -32
- package/tool-docs/websearch.md +2 -19
- package/tool-docs/write.md +2 -12
- package/tools/browser.mjs +54 -9
- package/tools/doc-write-gate.mjs +73 -0
- package/tools/edit-batch.mjs +170 -92
- package/tools/edit-diff.mjs +78 -62
- package/tools/edit-receipt.mjs +39 -0
- package/tools/execute.mjs +3 -3
- package/tools/file.mjs +28 -14
- package/tools/git.mjs +36 -21
- package/tools/index.mjs +6 -0
- package/tools/knowledge.mjs +143 -0
- package/tools/linter.mjs +3 -3
- package/tools/ops.mjs +5 -3
- package/tools/patch.mjs +124 -44
- package/tools/process-doc-mark.mjs +33 -4
- package/tools/repomap.mjs +25 -3
- package/tools/search.mjs +74 -26
- package/tools/shared.mjs +29 -27
- package/tools/ssh.mjs +5 -5
- package/tools/tree.mjs +32 -10
- package/tools/web-deepseek.mjs +1 -1
- package/tools/web.mjs +2 -2
- package/tools/write-gate-params.mjs +31 -0
- package/tools/write-path.mjs +16 -4
- package/window-policy.mjs +243 -0
- package/doc-check/anchors.mjs +0 -235
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* agent-tools/batch-read.mjs — 批次档只读读面(read-data-interface 批 · 设计档
|
|
3
|
-
* `docs/cli/design/READ-DATA-INTERFACE.md` §2.5 / §3.3 · 台账 #887
|
|
3
|
+
* `docs/cli/design/READ-DATA-INTERFACE.md` §2.5 / §3.3 · 台账 #887;`read` 动作与评审侧批档简报批 ·
|
|
4
|
+
* `docs/core/design/BATCH-RECORD.md` §4.25 ∥ `docs/core/design/ADVISOR-GUARDS.md` §2.5 · 台账 #2616)。
|
|
4
5
|
*
|
|
5
6
|
* `listBatchRecords({ cwd, bases })` —— ACP `batch/list` 与变更观察器共用的**文件集 + 段状态单源**:
|
|
6
7
|
* 文件集 = 基底根(`batchDocBases`——声明面单源)下全部 `*.md`(递归;收集器 `collectMarkdownFiles`
|
|
7
8
|
* 转导出复用);路径升序稳定;单档读错跳过(尽力面)。各段状态 = `readSectionStatusWord`(段头 /
|
|
8
9
|
* 状态行正则 / 词表全消费 batch-skeleton 单源——与冻结门字面零漂移;`§4` / `§6` = 字面「无状态词」)。
|
|
10
|
+
*
|
|
11
|
+
* `read` 面(`BATCH-RECORD.md` §4.25)三件共用同一把尺——段界与状态词不各判一次:
|
|
12
|
+
* - `batchRecordIndex(src)` —— 档头区三行(编制 ∥ 台账 ∥ 批起 sha)+ 逐段一行(状态词 ∥ 正文行数)。
|
|
13
|
+
* - `batchSectionWindow(src, seg, offset)` —— 段体行集按 `READ_WINDOW` 切片(1 基、段体首行 = 1)。
|
|
14
|
+
* - `readBatchRecord({ args, pickTarget })` —— action 执行体:目标经 `pickTarget`(D-BR21 一处判据:
|
|
15
|
+
* 绑定 ∥ 在飞 ∥ depth-0 path),**零写盘、零台账事务、零 doc-check 门、零 `_batchWrites`**;
|
|
16
|
+
* 冻结档读得到(冻结是写门,读一份已收口档正是下一批学上一批的方式)。
|
|
17
|
+
* - `buildBatchBrief(batchDoc, cwd)` —— 评审消息里的 `## Batch record` 块(`ADVISOR-GUARDS.md` §2.5):
|
|
18
|
+
* 解析后的绝对路径 + 同一份索引 + 两句指令句;未绑定 ⇒ 空串(块不出现,空串是调用方算出来的结果)。
|
|
19
|
+
*
|
|
20
|
+
* 依赖单向:本档 import `batch-lifecycle.mjs`(文件集收集器 ∥ 段号谓词)与 `batch-segment-vocab.mjs`
|
|
21
|
+
* (段体行集 ∥ 状态词)——两者都不 import 本档。
|
|
9
22
|
*/
|
|
10
23
|
import { readFileSync } from "node:fs"
|
|
11
|
-
import { basename } from "node:path"
|
|
24
|
+
import { basename, resolve } from "node:path"
|
|
12
25
|
|
|
13
|
-
import { collectMarkdownFiles } from "./batch-lifecycle.mjs"
|
|
26
|
+
import { collectMarkdownFiles, segmentNumber } from "./batch-lifecycle.mjs"
|
|
14
27
|
import { batchDocBases } from "./batch-paths.mjs"
|
|
15
|
-
import { readSectionStatusWord } from "./batch-
|
|
28
|
+
import { readSectionStatusWord, sectionBodyLines } from "./batch-segment-vocab.mjs"
|
|
16
29
|
|
|
17
30
|
/** 段号表(1–6——输出映射键序固定;`readSectionStatusWord` 消费数形段号)。 */
|
|
18
31
|
const SEGMENTS = [1, 2, 3, 4, 5, 6]
|
|
19
32
|
|
|
33
|
+
/** 一次 `read` 窗口至多取回的段体行数(契约值只住 `BATCH-RECORD.md` §4.25;本档是实现面)。 */
|
|
34
|
+
export const READ_WINDOW = 200
|
|
35
|
+
|
|
20
36
|
/** 批次档列举(只读):`{ batches: [{ file, path, sections }] }`——`path` = 绝对路径(`/` 归一)、
|
|
21
37
|
* `file` = basename、`batches` 按路径升序;单档读错跳过(尽力面)。`bases` 缺省 = `batchDocBases(cwd)`。 */
|
|
22
38
|
export function listBatchRecords({ cwd, bases } = {}) {
|
|
@@ -34,3 +50,134 @@ export function listBatchRecords({ cwd, bases } = {}) {
|
|
|
34
50
|
}
|
|
35
51
|
return { batches }
|
|
36
52
|
}
|
|
53
|
+
|
|
54
|
+
/** 档头区一行的取法(行首 `> 编制` / `> 台账` / `批起 sha =` 三种结构行);取不到 ⇒ 缺行读数。
|
|
55
|
+
* 「缺行」与「无状态行」同形:索引要说的是这份档此刻有什么,不是它本该有什么。 */
|
|
56
|
+
function headerLineOf(src, re, missing) {
|
|
57
|
+
const m = re.exec(src)
|
|
58
|
+
return m ? m[0].trim().replace(/^>\s*/, "") : missing
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 段索引(纯函数 · 零 fs):档头区三行 + 逐段一行(状态词 ∥ 正文行数)。
|
|
63
|
+
* 与 `batchSectionWindow` 读同一组判据(`sectionBodyLines` ∥ `readSectionStatusWord`)——索引里的行数
|
|
64
|
+
* 就是窗口回复的「共 T 行」,两处不各数一次。
|
|
65
|
+
* @param {string} src — 档全文
|
|
66
|
+
* @returns {string} 索引正文(不含绝对路径与下一次调用的形——那两件由调用方拼)
|
|
67
|
+
*/
|
|
68
|
+
export function batchRecordIndex(src) {
|
|
69
|
+
const header = [
|
|
70
|
+
headerLineOf(src, /^>\s*编制.*$/m, "(无编制行)"),
|
|
71
|
+
headerLineOf(src, /^>\s*台账.*$/m, "(无台账行)"),
|
|
72
|
+
headerLineOf(src, /^批起 sha\s*=.*$/m, "(无批起 sha 行)"),
|
|
73
|
+
]
|
|
74
|
+
const sections = SEGMENTS.map((seg) => {
|
|
75
|
+
const word = readSectionStatusWord(src, seg)
|
|
76
|
+
const count = (sectionBodyLines(src, seg) ?? []).length
|
|
77
|
+
return `§${seg} · ${word ?? "无状态行"} · ${count} 行`
|
|
78
|
+
})
|
|
79
|
+
return [...header, "", ...sections].join("\n")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 段体窗口(纯函数 · 零 fs):`offset` 1 基、段体首行 = 1、至多 `READ_WINDOW` 行。
|
|
84
|
+
* 两条拒面都点名被替换的参数与可判读数(**无默认值**:默认值会替调用方选起点,错的那一次静默从中间读,
|
|
85
|
+
* 而读者看不出少了半段):
|
|
86
|
+
* - 段头缺失 ⇒ 拒并点名 `segment`(不回退整档——回退之后读者以为手里是那一节,实际是六段全文);
|
|
87
|
+
* - `offset` 非 ≥1 的整数 ∥ 超过该段正文行数 ⇒ 拒并点名 `offset` 与该段总行数(空窗口读起来像
|
|
88
|
+
* 「这段读完了」,而它与「这段就到这里」是两件事)。
|
|
89
|
+
* @param {string} src — 档全文
|
|
90
|
+
* @param {number} seg — 段号(已由 `segmentNumber` 解出的数形)
|
|
91
|
+
* @param {number} offset — 段体内的 1 基起始行
|
|
92
|
+
* @returns {{start:number, end:number, total:number, lines:string[], nextOffset:number|null}}
|
|
93
|
+
*/
|
|
94
|
+
export function batchSectionWindow(src, seg, offset) {
|
|
95
|
+
const body = sectionBodyLines(src, seg)
|
|
96
|
+
if (body === null) {
|
|
97
|
+
throw new Error(`batch: read got segment "§${seg}" but this batch record has no "## §${seg}" section header — pass a section the record carries (§1–§6, e.g. "§2"). Nothing was written.`)
|
|
98
|
+
}
|
|
99
|
+
const total = body.length
|
|
100
|
+
if (!Number.isInteger(offset) || offset < 1 || offset > total) {
|
|
101
|
+
throw new Error(`batch: read got offset ${JSON.stringify(offset ?? null)} for §${seg} — offset must be an integer ≥ 1 and at most ${total} (the §${seg} body length; 1-based, the first body line is 1). Nothing was written.`)
|
|
102
|
+
}
|
|
103
|
+
const end = Math.min(total, offset + READ_WINDOW - 1)
|
|
104
|
+
return { start: offset, end, total, lines: body.slice(offset - 1, end), nextOffset: end < total ? end + 1 : null }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 段索引回执(尾行 = 绝对路径 ∥ 下一次调用的形)。路径归一 `/`:与 `listBatchRecords` ∥ 批档简报同一形,
|
|
108
|
+
* 读它的人拿到的是可执行的路径,而不是一串可能被当成转义的 `\`。 */
|
|
109
|
+
function renderIndexReceipt(abs, src) {
|
|
110
|
+
return [
|
|
111
|
+
batchRecordIndex(src),
|
|
112
|
+
"",
|
|
113
|
+
`${abs.replace(/\\/g, "/")} · next: batch({"action":"read","segment":"§1","offset":1}) — each window carries up to ${READ_WINDOW} lines`,
|
|
114
|
+
].join("\n")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 段窗口回执(首行 `§N · 行 a-b / 共 T`;未完则尾行给下一 `offset`)。 */
|
|
118
|
+
function renderWindowReceipt(src, segRaw, offset) {
|
|
119
|
+
const seg = segmentNumber(segRaw)
|
|
120
|
+
if (seg === null) {
|
|
121
|
+
throw new Error(`batch: read got segment ${JSON.stringify(segRaw ?? null)} — pass the section number the record carries ("§2" and "2" are the same section, e.g. "§2"). Nothing was written.`)
|
|
122
|
+
}
|
|
123
|
+
if (offset === undefined || offset === null) {
|
|
124
|
+
throw new Error(`batch: read needs offset whenever segment is given — segment names a section, offset names where in its body to start (1-based: the first body line is 1, at most ${READ_WINDOW} lines per window). Omit segment for the whole-record index, or pass offset to open a window. Nothing was written.`)
|
|
125
|
+
}
|
|
126
|
+
const w = batchSectionWindow(src, seg, offset)
|
|
127
|
+
const lines = [`§${seg} · 行 ${w.start}-${w.end} / 共 ${w.total}`, "", ...w.lines]
|
|
128
|
+
if (w.nextOffset !== null) {
|
|
129
|
+
lines.push("", `next: offset: ${w.nextOffset} — batch({"action":"read","segment":"§${seg}","offset":${w.nextOffset}})`)
|
|
130
|
+
}
|
|
131
|
+
return lines.join("\n")
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* `read` 执行体(只读):目标经 `pickTarget`(D-BR21 一处判据——绑定 ∥ 在飞 ∥ depth-0 path,`path`
|
|
136
|
+
* 的子代理拒面也在那里),零写盘、零台账事务、零 doc-check 门、零 `_batchWrites` 计数。
|
|
137
|
+
* 两种调用形(§4.25 的表):无 `segment` ⇒ 段索引;`segment` + 必填 `offset` ⇒ 该段一个窗口。
|
|
138
|
+
* 段号与 `offset` 的两条拒面见 `renderWindowReceipt` ∥ `batchSectionWindow`。
|
|
139
|
+
* @param {{args: object, ctx: object, review: boolean, pickTarget: (raw?: string) => string}} p —
|
|
140
|
+
* `ctx` ∥ `review` 不消费(目标定位全在 `pickTarget`);形与 `checkBatchRecord` 同款,调用面只换函数名
|
|
141
|
+
* @returns {string} 回执(索引 ∥ 窗口)
|
|
142
|
+
*/
|
|
143
|
+
export function readBatchRecord({ args, ctx, review, pickTarget }) {
|
|
144
|
+
const abs = pickTarget(args?.path)
|
|
145
|
+
const src = readFileSync(abs, "utf8")
|
|
146
|
+
const segRaw = args?.segment
|
|
147
|
+
if (segRaw === undefined || segRaw === null) return renderIndexReceipt(abs, src)
|
|
148
|
+
return renderWindowReceipt(src, segRaw, args?.offset)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 评审消息的批档简报块(`ADVISOR-GUARDS.md` §2.5):块标题 + 三件(解析后的绝对路径 ∥ 档头区 ∥ 逐段一行)
|
|
153
|
+
* + 两句指令句(过程证据不是评审对象;检索面那句真话)。**只给路径与索引,不复制正文**——正文每轮重发、
|
|
154
|
+
* 随实施增长,路径加索引是常量大小,读者按需 `read`。
|
|
155
|
+
* 未绑定(`null` / `undefined` / 空串)⇒ 空串:块不出现,且这个空串是调用方算出来的结果(§2.5 参数面)。
|
|
156
|
+
* @param {string|null|undefined} batchDoc — 本笔绑定的批档(启动时点的快照值)
|
|
157
|
+
* @param {string} cwd — 解析绝对路径的基准(`buildBatchBrief` 不给默认值:基准错了,路径就是别人的档)
|
|
158
|
+
* @returns {string} 简报块(含尾空行——与对象声明块的段间空行同形)∥ 空串
|
|
159
|
+
*/
|
|
160
|
+
export function buildBatchBrief(batchDoc, cwd) {
|
|
161
|
+
if (batchDoc === undefined || batchDoc === null || batchDoc === "") return ""
|
|
162
|
+
if (typeof batchDoc !== "string") {
|
|
163
|
+
throw new TypeError("buildBatchBrief: batchDoc must be a string path, or empty/null for an unbound review — the binding value is passed through as it is, so a non-string here is a call-site defect, never a record to guess at.")
|
|
164
|
+
}
|
|
165
|
+
if (typeof cwd !== "string" || !cwd.trim()) {
|
|
166
|
+
throw new TypeError("buildBatchBrief: cwd must be a non-empty string — the brief prints the resolved absolute path, so the base it resolves against is required and is never defaulted.")
|
|
167
|
+
}
|
|
168
|
+
const abs = resolve(cwd, batchDoc).replace(/\\/g, "/")
|
|
169
|
+
let src
|
|
170
|
+
try {
|
|
171
|
+
src = readFileSync(abs, "utf8")
|
|
172
|
+
} catch (e) {
|
|
173
|
+
throw new Error(`buildBatchBrief: the bound batch record could not be read at ${abs} — ${e?.message ?? e}. The brief is built from the record itself, never from memory: fix the binding or restore the record before a review is briefed on it.`)
|
|
174
|
+
}
|
|
175
|
+
return [
|
|
176
|
+
"## Batch record",
|
|
177
|
+
`${abs} — read this file in full.`,
|
|
178
|
+
batchRecordIndex(src),
|
|
179
|
+
"",
|
|
180
|
+
"The batch record is process evidence, not a review target: §2 is the task book the work must satisfy, §1 is the ruling on it and §3 is the previous round's record — none of the three is a review basis. When you read it, the search face applies: `grep` / `glob` / `ls` / `tree` omit process documents unless you pass `includeProcessDocs: true`, and an empty default search does not mean the record is absent.",
|
|
181
|
+
"",
|
|
182
|
+
].join("\n")
|
|
183
|
+
}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-tools/batch-record-write.mjs — 批次档**段写入执行面**(append ∥ edit 两条 action 的读写与门)。
|
|
3
|
+
*
|
|
4
|
+
* 2026-11-05 拆档族从 `batch.mjs` 迁出:工具面(`batch` 的 schema 与 action 分发)留原档,执行面住本档。
|
|
5
|
+
* 分界判据 = 「改 schema 的人不该翻过 300 行执行体」:本档全是逐 action 的段读写、拒面与文档机检门挂点。
|
|
6
|
+
*
|
|
7
|
+
* 搬家形 = **具名符号整块外移、不重写体、不改名**:`appendBatchRecord` ∥ `editBatchRecord` 两具执行体、
|
|
8
|
+
* 共用五助手(`segmentNumber` ∥ `allowedSegment` ∥ `noSegmentError` ∥ `sanitizeText` ∥ `insertIntoSection`)、
|
|
9
|
+
* edit 面一助手(`sectionBodySpan`)、`MAX_TEXT_CHARS` 及其私有件
|
|
10
|
+
* (`CRED_RE` ∥ `CRED_TEST_RE`)、十一条拒串逐字随迁。原档 import 回来供分发调用 ⇒ 调用点零改、
|
|
11
|
+
* 拒文零变——这是本批「行为零变」的机械保证。
|
|
12
|
+
*
|
|
13
|
+
* **记账注入缝(`injectedOnWrite` ∥ `configureBatchSegment` ∥ `resetBatchSegment`)不随迁**:缝的唯一
|
|
14
|
+
* 读者是原档的 `batchTool.execute`(它取值后经 `onWritten` 形参递给 create / append / edit / status /
|
|
15
|
+
* close 五条 action)——把缝搬到本档会让工具面反向 import 本档(两档互 import),或逼出新导出名;
|
|
16
|
+
* 留原档两头都不必,且 `onWritten` 形参本就在两条执行体的签名里(零改)。
|
|
17
|
+
*
|
|
18
|
+
* 依赖单向:`batch-segment-vocab ← batch-skeleton ← …(gate ∥ closeout ∥ reopen ∥ lifecycle)← 本档 ← batch.mjs`
|
|
19
|
+
* (本档不 import `batch.mjs`,两条 action 的调用者只在原档的分发里)。
|
|
20
|
+
*/
|
|
21
|
+
import { readFileSync, writeFileSync } from "node:fs"
|
|
22
|
+
import { DEPTH0_SEGMENTS, ROUND_HEADING_RE, SEGMENT_BY_ROLE, SEGMENT_BY_ROLE_NORMAL, STATUS_LINE_RE, findPlaceholderResidue, firstZoneStatusLineRange, goAheadEndLine, goAheadReceiptLine, nextRoundNumber, placeholderResidueError, readBatchStatusLine, reviewWritableSegment, sectionHeaderRe, sectionThreeDesignTrackError, statusDonePlan, statusPassPlan, statusResetPlan, updateSectionStatusLine, verdictGapOf, zoneStatusCount } from "./batch-skeleton.mjs"
|
|
23
|
+
import { batchDocBases, resolveBatchDocPath } from "./batch-paths.mjs"
|
|
24
|
+
import { gateReceiptLine, gateRefusalMessage, runBatchGate } from "./batch-gate.mjs"
|
|
25
|
+
import { assertSectionFiveGate } from "./batch-closeout.mjs"
|
|
26
|
+
import { findInFlightBatch } from "./batch-lifecycle.mjs"
|
|
27
|
+
// 日期戳单源(`todayLocal`——重开 §1 状态行与本节留痕行同一枚,日期口径只一处)。
|
|
28
|
+
import { todayLocal } from "./batch-reopen.mjs"
|
|
29
|
+
|
|
30
|
+
/** text 单次上限(BATCH-RECORD.md §4.1——超出引导分段追加,不承诺"不新盖戳")。append 迁移域私有区常量
|
|
31
|
+
* (随执行体自 batch-segment.mjs 迁入——骨架档只住骨架/状态行单源,lifecycle 档头枚举口径)。 */
|
|
32
|
+
export const MAX_TEXT_CHARS = 20000
|
|
33
|
+
/** 凭证形态(`BATCH-RECORD.md` §4.1——三形):`[DESIGN-TOKEN:…]` 闭括号形 ∥ `designId: …` 键形 ∥ 无标两段
|
|
34
|
+
* token 值形 `⟨uuid⟩:⟨epoch⟩`(与单源 `design-token.mjs` 判据对表);裸 uuid 非凭证不剥(KD-7)。两字面勿
|
|
35
|
+
* 合并:`CRED_RE` 走 `.replace()` 须 `/g` ∥ `CRED_TEST_RE` 逐行 `.test()` 须非 `/g`(lastIndex 有状态会漏剥)。 */
|
|
36
|
+
const CRED_RE = /\[DESIGN-TOKEN:[^\]]*\]|designId\s*:\s*\S+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:\d{10,16}/g
|
|
37
|
+
const CRED_TEST_RE = /\[DESIGN-TOKEN:[^\]]*\]|designId\s*:\s*\S+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:\d{10,16}/
|
|
38
|
+
|
|
39
|
+
// ─── append 迁移面(原 batch-segment.mjs 的 append 私有区——逐字迁移,错误串逐字保序)───
|
|
40
|
+
|
|
41
|
+
/** 段号解析:`§2` / `2` / `§2 批次任务` 均可;无法解析 → null。 */
|
|
42
|
+
function segmentNumber(raw) {
|
|
43
|
+
const m = /^§?\s*(\d+)/.exec(String(raw ?? "").trim())
|
|
44
|
+
return m ? Number(m[1]) : null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 身份 → 可写段号;无写权身份 → null。评审实例绑定优先(评审者的身份即"设计评审"),**评审轨那一维就在这里判**
|
|
48
|
+
* (`BATCH-RECORD.md` §4.3:两张面判同一对输入——模式 × 评审轨):工程模式**设计轨** ⇒ null(`§3` 只由结算写),
|
|
49
|
+
* 工程**代码轨**与普通模式 ⇒ 3。
|
|
50
|
+
* depth-0 的段白名单(append——模式感知,§4.16)由 appendBatchRecord 分支,不经本表;
|
|
51
|
+
* 普通模式分支额外合并 coder 增表(`SEGMENT_BY_ROLE_NORMAL`——**合并查只在普通模式分支**)。 */
|
|
52
|
+
function allowedSegment(agent, review, engineering, reviewTrack) {
|
|
53
|
+
if (review) return reviewWritableSegment(engineering, reviewTrack)
|
|
54
|
+
const role = agent?._role
|
|
55
|
+
return SEGMENT_BY_ROLE[role] ?? (engineering ? null : SEGMENT_BY_ROLE_NORMAL[role]) ?? null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 无写权身份拒串(`append` 与 `edit` 共用一条——两动作读同一张段白名单,同一件事不各写一份串)。 */
|
|
59
|
+
function noSegmentError(engineering) {
|
|
60
|
+
return engineering
|
|
61
|
+
? "batch_segment: no segment is writable by this caller — the channel exists for eng-designer (§2), eng-coder (§5) and design reviews bound to a batch record (§3); the parent agent writes §1/§4/§6 through ordinary document writes."
|
|
62
|
+
: "batch_segment: no segment is writable by this caller — the channel exists for eng-designer (§2), eng-coder (§5) and design reviews bound to a batch record (§3); in normal mode the parent agent writes §1/§2/§4/§5/§6 through ordinary document writes."
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 凭证剥除 + 伪造轮次标题丢弃(BATCH-RECORD.md §4.1 F6/AC30/AC34)。
|
|
67
|
+
* 逐行处理:含凭证形态的行 → 剥除该子串,剥后为空则整行丢弃(其余内容逐字保留)。
|
|
68
|
+
* `dropStamp`(仅 §3 目标段):调用方自带的 `### 轮次 N(评审子代理)` 行被丢弃——
|
|
69
|
+
* 工具生成才是唯一来源戳(否则伪造戳会污染 N 计数)。
|
|
70
|
+
*/
|
|
71
|
+
function sanitizeText(text, dropStamp) {
|
|
72
|
+
const kept = []
|
|
73
|
+
for (const line of text.replace(/\r\n?/g, "\n").split("\n")) {
|
|
74
|
+
if (dropStamp && ROUND_HEADING_RE.test(line)) continue
|
|
75
|
+
if (!CRED_TEST_RE.test(line)) { kept.push(line); continue }
|
|
76
|
+
const cleaned = line.replace(CRED_RE, "").replace(/[ \t]+$/, "")
|
|
77
|
+
if (cleaned.trim()) kept.push(cleaned)
|
|
78
|
+
}
|
|
79
|
+
return kept.join("\n")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 段尾定位 + 插入(append-only:只在段尾插入,既有字节不变;返回新全文 + 本轮 N)。
|
|
83
|
+
* `blockLead`(仅 §3——打回重置的留痕行):落在**工具戳标题之后、评审正文之前**——与
|
|
84
|
+
* `### 轮次 N(评审子代理)` 同属工具戳面,append-only 不变。
|
|
85
|
+
*/
|
|
86
|
+
function insertIntoSection(src, seg, text, blockLead = "") {
|
|
87
|
+
const hdr = sectionHeaderRe(seg).exec(src)
|
|
88
|
+
if (!hdr) throw new Error(missingSectionError(seg))
|
|
89
|
+
const nextRe = /^## §\d/gm
|
|
90
|
+
nextRe.lastIndex = hdr.index + hdr[0].length
|
|
91
|
+
const next = nextRe.exec(src)
|
|
92
|
+
const endIdx = next ? next.index : src.length
|
|
93
|
+
// 来源戳(仅 §3):工具生成标题 + N——调用方写不进自己的标题(sanitize 已丢弃同名行)。
|
|
94
|
+
// N 的单源 = `nextRoundNumber`(= `countRoundBlocks + 1`)——留痕行的「第 N 轮」同取这一枚。
|
|
95
|
+
const roundN = seg === 3 ? nextRoundNumber(src, 3) : 0
|
|
96
|
+
const stamp = seg === 3 ? `### 轮次 ${roundN}(评审子代理)\n\n` + (blockLead ? `${blockLead}\n\n` : "") : ""
|
|
97
|
+
const payload = stamp + text
|
|
98
|
+
const before = src.slice(0, endIdx)
|
|
99
|
+
const after = src.slice(endIdx)
|
|
100
|
+
const eol = src.includes("\r\n") ? "\r\n" : "\n"
|
|
101
|
+
const lead = before === "" ? "" : before.endsWith(eol + eol) ? "" : before.endsWith(eol) ? eol : eol + eol
|
|
102
|
+
// 尾随 eol 恒加(块自成行);后面还有段标题时再补一个空行(保持模板的空行分隔)。
|
|
103
|
+
const written = before + lead + payload.replaceAll("\n", eol) + eol + (after === "" ? "" : eol) + after
|
|
104
|
+
return { written, roundN }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* append 执行体(action=append——原 batch_segment execute 迁移 + depth-0 分支):
|
|
109
|
+
* - 段白名单(**模式感知**——§4.16):depth-0 工程 → §1/§4/§6(新错误面,"batch:" 前缀)∥
|
|
110
|
+
* depth-0 普通 → §1/§2/§4/§5/§6(模式化新串);eng-designer → §2 · 评审 → §3(**工程设计轨除外**:
|
|
111
|
+
* 那一路的 §3 只由结算写,本面直接拒——§4.4)· eng-coder → §5 · coder(普通模式分支增表)→ §5
|
|
112
|
+
* (迁移错误面,"batch_segment:" 前缀**逐字保持**);
|
|
113
|
+
* - 目标定位(D-BR21):depth-0 path 可选(缺省 = findInFlightBatch 唯一在飞批);子代理/评审
|
|
114
|
+
* 传 path ⇒ 拒,目标 = spawn/实例绑定;
|
|
115
|
+
* - 其余(gate / text 校验 / 剥凭证 / 骨架保护 / **状态行拒写(J3——行首形态行不得入档,§4.21.4)** /
|
|
116
|
+
* **死占位机检(F11-C)** / 插入(候选全文成形 = 盘上现文 + 这一段 + **状态行改写(§4.12 被动写:
|
|
117
|
+
* `§3` 随裁定行置位 ∥ 重置,`§2`/`§5` 写正文即置完成词)**) / **文档机检门
|
|
118
|
+
* (§3.20 结构兜底 ①——**全部 append 同挂**(含 depth-0 与评审写 §3):判的是候选全文里这一笔写的行
|
|
119
|
+
* (判面单源 = `docs/core/design/DOC-CHECK.md` §6「写时点怎么取区间」),命中 ⇒ 拒写且零字节(写盘在门之后),
|
|
120
|
+
* ERROR ⇒ 写 + 回执明示;批档已落段不再入判)** / 写盘 / 记账 / 回执)逐字保持原语义(新增的一道 = 状态行拒写)。
|
|
121
|
+
*/
|
|
122
|
+
export function appendBatchRecord({ args, ctx, review, reviewTrack, batchDoc, onWritten }) {
|
|
123
|
+
const agent = ctx?.agent ?? {}
|
|
124
|
+
const cwd = agent.cwd
|
|
125
|
+
const depth0 = ctx?.depth === 0
|
|
126
|
+
// 模式位执行期实读(§4.16——同款先例 = task.mjs execute 门;翻转即时生效——D-BR26)
|
|
127
|
+
const engineering = !!agent?.config?.agent?.engineering
|
|
128
|
+
const seg = allowedSegment(agent, review, engineering, reviewTrack)
|
|
129
|
+
if (!depth0 && seg === null) {
|
|
130
|
+
// 评审实例被拒时点名真因(§4.4 只读);其余无写权身份给段白名单那句。
|
|
131
|
+
if (review) throw new Error(sectionThreeDesignTrackError("batch_segment"))
|
|
132
|
+
throw new Error(noSegmentError(engineering))
|
|
133
|
+
}
|
|
134
|
+
const n = segmentNumber(args?.segment)
|
|
135
|
+
if (n === null) {
|
|
136
|
+
if (depth0) {
|
|
137
|
+
throw new Error(engineering
|
|
138
|
+
? `batch: unknown segment ${JSON.stringify(args?.segment ?? null)} — depth-0 append writes §1/§4/§6 of the in-flight record (pass the section number you write, e.g. "§4").`
|
|
139
|
+
: `batch: unknown segment ${JSON.stringify(args?.segment ?? null)} — in normal mode depth-0 append writes §1/§2/§4/§5/§6 of the in-flight record (pass the section number you write, e.g. "§4").`)
|
|
140
|
+
}
|
|
141
|
+
throw new Error(`batch_segment: unknown segment ${JSON.stringify(args?.segment ?? null)} — pass the section number you write (e.g. "§${seg}").`)
|
|
142
|
+
}
|
|
143
|
+
if (depth0) {
|
|
144
|
+
const domain = engineering ? DEPTH0_SEGMENTS.append.engineering : DEPTH0_SEGMENTS.append.normal
|
|
145
|
+
if (!domain.includes(n)) {
|
|
146
|
+
throw new Error(engineering
|
|
147
|
+
? `batch: §${n} is not yours to write — depth-0 append writes §1/§4/§6 only (一段一作者: §2 = eng-designer, §3 = design review, §5 = eng-coder).`
|
|
148
|
+
: `batch: §${n} is not yours to write — in normal mode depth-0 append writes §1/§2/§4/§5/§6 (一段一作者: §3 = design review only). Nothing was written.`)
|
|
149
|
+
}
|
|
150
|
+
} else if (n !== seg) {
|
|
151
|
+
throw new Error(`batch_segment: §${n} is not yours to write — this caller writes §${seg} only (一段一作者: eng-designer → §2, design review → §3, eng-coder → §5).`)
|
|
152
|
+
}
|
|
153
|
+
// 目标定位(D-BR21)——path 拒面先于绑定解析(与 status 同序:段白名单 → path 门 → 取值)。
|
|
154
|
+
const givenPath = typeof args?.path === "string" && args.path.trim() ? args.path : null
|
|
155
|
+
let abs
|
|
156
|
+
if (givenPath) {
|
|
157
|
+
if (!depth0) {
|
|
158
|
+
throw new Error("batch: path is a depth-0-only parameter (D-BR21) — your target record arrives via the spawn binding / the review instance key. Nothing was written.")
|
|
159
|
+
}
|
|
160
|
+
abs = resolveBatchDocPath(cwd, givenPath)
|
|
161
|
+
} else if (depth0) {
|
|
162
|
+
abs = findInFlightBatch(cwd, batchDocBases(cwd)) // 歧义锚 ⇒ 基底 = 候选并集(#828——唯一在飞批照取 / ≥2 照列)
|
|
163
|
+
} else {
|
|
164
|
+
if (!batchDoc) {
|
|
165
|
+
throw new Error("batch_segment: no batch record is bound to this caller — there is no path parameter by design (the target arrives via the spawn binding / the review instance key). Report the section as not written.")
|
|
166
|
+
}
|
|
167
|
+
abs = resolveBatchDocPath(cwd, batchDoc)
|
|
168
|
+
}
|
|
169
|
+
const src = readFileSync(abs, "utf8")
|
|
170
|
+
const status = readBatchStatusLine(src)
|
|
171
|
+
if (status === "closed") throw new Error(FROZEN_RECORD_ERROR)
|
|
172
|
+
if (status === "unknown") throw new Error(UNRESOLVED_STATUS_ERROR)
|
|
173
|
+
if (typeof args?.text !== "string") {
|
|
174
|
+
throw new Error("batch_segment: text must be a string (the markdown to append).")
|
|
175
|
+
}
|
|
176
|
+
if (args.text.length > MAX_TEXT_CHARS) {
|
|
177
|
+
throw new Error(`batch_segment: text is ${args.text.length} characters — the limit is ${MAX_TEXT_CHARS} per call. Split it into multiple calls: each call becomes its own section and the round number N continues (分段追加——每次调用各成节、N 顺延).`)
|
|
178
|
+
}
|
|
179
|
+
if (/^## §\d/m.test(args.text)) throw new Error(SKELETON_GUARD_ERROR)
|
|
180
|
+
// J3(§4.21.4 写面第一道——`append` 是唯一还开着的入口:`edit` 早拒「引入状态行」,`append` 没有):
|
|
181
|
+
// 段尾追加的正文落在段头区内时(该段还没有 `^#{2,}` 行时整个段体都是段头区),一条手写行就成了第二条。
|
|
182
|
+
// 判定面 = 原始 `text`(剥凭证之前):「行首」即 `STATUS_LINE_RE` 的 `^\s*\*\*状态行\*\*[::]`——
|
|
183
|
+
// 块引用 `> …` 与表格行 `| …` 天然不命中(拒串直接给这两种写法)。位次 = 骨架保护之后、剥凭证之前(拒 = 零字节)。
|
|
184
|
+
if (args.text.split(/\r?\n/).some((line) => STATUS_LINE_RE.test(line))) throw new Error(STATUS_LINE_IN_APPEND_ERROR)
|
|
185
|
+
const body = sanitizeText(args.text, n === 3).replace(/^\n+/, "").replace(/\s+$/, "")
|
|
186
|
+
if (!body.trim()) {
|
|
187
|
+
throw new Error("batch_segment: nothing to append — the text is empty after credential stripping (credential values never reach the record).")
|
|
188
|
+
}
|
|
189
|
+
// F11-C(挂点:gate / text 校验后 · insertIntoSection 前):档头结构行死占位残留 ⇒ 拒
|
|
190
|
+
// (段体引用豁免——判据可辨性收正,`TOOLS.md` §6.20;骨架枚举单源 = skeleton 档 TEMPLATE_PLACEHOLDERS)。
|
|
191
|
+
const residues = findPlaceholderResidue(src)
|
|
192
|
+
if (residues.length) throw new Error(placeholderResidueError(residues))
|
|
193
|
+
// §5 写入门(段序门 · 设计档 §2.6 `R-G2`):实现发生在评审通过之后,序在这里判得出真假;这一门没有旁路
|
|
194
|
+
// (`§1` 声明 §3 不适用已随声明机制退役)。
|
|
195
|
+
if (n === 5) assertSectionFiveGate(src)
|
|
196
|
+
// §3 写入闸(设计档 `BATCH-LEDGER-REVIEW-UNIFY.md` §2.6.1 `R-G3`——裁定行在写的时刻就要在):
|
|
197
|
+
// 能写 §3 的只有绑本档的评审实例,它结算后即离场 ⇒ 把错误退回**还有权补那一行的人在场的那一刻**。
|
|
198
|
+
// 判定输入 = 净化后正文 `body`(落档的就是它;每笔 §3 自成一块 ⇒ 「写前正文 ≡ 写后末块」只对 body 成立);
|
|
199
|
+
// 判据单源 = 骨架档 `verdictGapOf`(与 §5 门判据 2 ∥ 签发面同一个函数)。
|
|
200
|
+
// 放行面 = **只拒两格**(`无裁定行` ∥ `两者同携`):判打回的一笔(`VERDICT: changes-required` ∥ `结论:不通过`)
|
|
201
|
+
// 照写——§5 门判据 2 与打回重置都靠那一块自带打回裁定;拦它 = 把打回这条业务路堵死。
|
|
202
|
+
// 位次 = 既有拒因之后、文档机检门之前(拒 = 零字节落盘、轮次号不消耗——N 在 insertIntoSection 时才取)。
|
|
203
|
+
if (n === 3) {
|
|
204
|
+
const gap = verdictGapOf(body)
|
|
205
|
+
if (gap === "无裁定行" || gap === "两者同携") throw new Error(SECTION3_VERDICT_GAP_ERROR)
|
|
206
|
+
}
|
|
207
|
+
// 裁定行 → 状态行(BLRU §2.5——**工具被动写**,两个方向共用同一处判定与同一条写入路径):
|
|
208
|
+
// 打回即重置(三合取:本笔写 §3 ∧ 本笔命中打回裁定行 ∧ 写前 §3 状态行 = 评审通过)⇒ 本轮块首留痕行
|
|
209
|
+
// (随 `blockLead` 进插入)+ 状态行重置;通过即置位(本笔写 §3 ∧ 本笔命中 pass 裁定行且不命中打回行 ∧
|
|
210
|
+
// 写前 §3 状态行 ≠ 评审通过)⇒ 状态行置成评审通过,无留痕行(块本身即记录)。
|
|
211
|
+
// 两条靠各自的三合取天然互斥;`??` 的左先右后与签发面 `verdictGateFailure` 同序(先打回、后 pass)。
|
|
212
|
+
// 状态行仍**只经 `updateSectionStatusLine` 这一个写入函数**改写(不新开第二条写路径);仍是
|
|
213
|
+
// 一次 `writeFileSync`——块与字进的是同一个字符串(无「块已落、字未到」的窗口)。
|
|
214
|
+
// 留痕行不经文档机检门——门位在上面(判集 = 本次评审正文 `pending.body`)。
|
|
215
|
+
const date = todayLocal()
|
|
216
|
+
const plan = statusResetPlan(src, n, body, date) ?? statusPassPlan(src, n, body, date)
|
|
217
|
+
const { written: inserted, roundN } = insertIntoSection(src, n, body, plan?.traceLine ?? "")
|
|
218
|
+
// 状态行进同一笔(`BATCH-RECORD.md` §4.12 ∥ §4.21):§3 按裁定置位 ∥ 重置(两条计划,三合取互斥);
|
|
219
|
+
// §2/§5 按正文置完成词(`statusDonePlan`——正文只有占位行 ⇒ 段仍判空 ⇒ 零写);
|
|
220
|
+
// §1/§4/§6 不动。仍是**一次** `writeFileSync`——块与字进的是同一个字符串(无「块已落、字未到」的窗口)。
|
|
221
|
+
const done = plan ? null : statusDonePlan(inserted, n)
|
|
222
|
+
const written = plan
|
|
223
|
+
? updateSectionStatusLine(inserted, n, plan.statusValue)
|
|
224
|
+
: done ? updateSectionStatusLine(inserted, n, done.statusValue) : inserted
|
|
225
|
+
// 文档机检门(§3.20「结构兜底」①——门位 = 既有拒因全部之后、写入之前):
|
|
226
|
+
// **工程模式 append 同挂**(§1–§6,含 depth-0 与评审写 §3——规则一条,不按身份分支);普通模式跳过内容门。
|
|
227
|
+
// `pending.body` = **候选全文**(`written`:盘上现文加上这一段及其状态行改写的整档视图)——门拿它与盘上现文两份
|
|
228
|
+
// 求变更区,判的就是这一笔写的行(判面单源 = `docs/core/design/DOC-CHECK.md` §6「写时点怎么取区间」)。
|
|
229
|
+
// 交半段文本就与盘上现文不同形,求出来的区间不是这一笔的行。REFUSED ⇒ throw(零字节落盘——`writeFileSync` 在门之后);
|
|
230
|
+
// PASS ⇒ 照写;ERROR / 门自身异常 ⇒ 写 + 回执明示。
|
|
231
|
+
const gate = ctx.agent?.config?.agent?.engineering === false ? null : runBatchGate({ abs, cwd, agent, scope: "writer", pending: { section: n, body: written } })
|
|
232
|
+
if (gate?.state === "refused") throw new Error(gateRefusalMessage(gate, "append"))
|
|
233
|
+
writeFileSync(abs, written)
|
|
234
|
+
// #84:记账面按端注入(缺省 no-op = CLI 语义零行为变——端装配经 configureBatchSegment 覆盖,缝住 `batch.mjs`)。
|
|
235
|
+
// 本条 = `AGENT-LOOP.md` §7.1 `wrote` 判据的计数面(成功写盘才增;上面各门已过)。
|
|
236
|
+
agent._batchWrites = (agent._batchWrites ?? 0) + 1
|
|
237
|
+
onWritten?.(agent, abs)
|
|
238
|
+
return `batch_segment: appended ${body.length} characters to §${n} of the batch record${roundN ? ` (### 轮次 ${roundN}(评审子代理))` : ""}.${done ? ` §${n} 状态行 = ${done.statusValue}(同笔置位——不另调 status).` : ""}${n === 1 ? `${goAheadReceiptLine(written)}${goAheadEndLine(src, written)}` : ""}${gateReceiptLine(gate)}`
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ─── 拒串单源(append / edit 共用——两动作只差写法,不差判据)───────────────
|
|
242
|
+
|
|
243
|
+
/** 拒串:`§3` 写入闸(`R-G3` 逐字 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.6.1)——一笔 `§3` 追加读成
|
|
244
|
+
* 「无裁定行」∥「两者同携」⇒ 拒写(零字节落盘、轮次号不消耗)。两个裁定词**内联在同一句里,不各占独立一行**:
|
|
245
|
+
* 各占一行时这段拒文自己就读成「两者同携」,把拒文原样引用并另写一行裁定的一笔仍被拒——想解释这道闸的人恰被
|
|
246
|
+
* 它挡在门外;缩进 ∥ 列表符同样不取(照抄的行不命中整行锚 ⇒ 补的那行永不生效)。混形反例与「不消耗轮次号」照留。 */
|
|
247
|
+
const SECTION3_VERDICT_GAP_ERROR = "batch_segment: §3 append refused — this round block states no readable verdict (块内裁定 = 无裁定行 | 两者同携). A §3 block must carry exactly one verdict as a line of its own; put exactly one of `VERDICT: pass` ∥ `VERDICT: changes-required` on such a line in the text you append — copying this refusal verbatim copies the words, not a verdict line.\n(本地化整行形 `结论:通过` / `结论:不通过` 同认,中文形的尾句号可省可带(`结论:通过。` 同认)。不作独立整行的写法不认——例如 `**结论** = VERDICT: pass`。) Nothing was written — no bytes landed and no round number was consumed: append the same text again with the verdict line added."
|
|
248
|
+
/** 拒串:已收口档(§4.9 门 ②——append / edit / status 共用一条,串逐字含「已收口档不回改」)。 */
|
|
249
|
+
const FROZEN_RECORD_ERROR = "batch_segment: 已收口档不回改 — the bound batch record's §1 status line contains 「已收口」, so the record is frozen: its body is never written to again (整档冻结;改 = 新批新档). Nothing was written. Report the section as not written."
|
|
250
|
+
/** 拒串:§1 状态行不可解析或缺失(fail-closed 视为冻结——与冻结门同一条解析器)。 */
|
|
251
|
+
const UNRESOLVED_STATUS_ERROR = "batch_segment: 状态行不可解析或缺失 — the bound batch record has no §1 `**状态行**:` line inside the section header zone whose value contains 已收口 or 进行中 (fail-closed: the write is refused as if frozen). If the record does carry such a line further down — in a round block or in the section body — move it up under `## §1`: a status line outside the header zone is never read. Nothing was written. Do NOT close the record after a refused append — nothing landed, and closing freezes the missing section for good (失败 ⇒ 勿 close)."
|
|
252
|
+
/** 拒串:段标题缺失(create 是骨架唯一权威入口,append / edit 都不补骨架)。 */
|
|
253
|
+
const missingSectionError = (seg) => `batch_segment: the bound batch record has no "## §${seg}" section header — the six-section skeleton is written by the record's creator before any segment write. Fix: ask the parent/creator to add the "## §${seg} …" heading (with the template's sub-headings) first, then call batch_segment again. Nothing was written.`
|
|
254
|
+
/** 拒串:骨架保护(写入文本 ∥ 替换文本含 `## §N` 段头形态 ⇒ 段定位失效——append / edit 共用)。
|
|
255
|
+
* 两个动作都不许把段头写进段体:段定位靠 `## §N` 标题,写进去就把定位面改掉(§4.1 fail-closed 行)。 */
|
|
256
|
+
const SKELETON_GUARD_ERROR = "batch_segment: the text contains a section header line matching `^## §N` — that would break section location. Rewrite it (escape the heading, or drop the leading `## §N`), then call again (骨架保护). Nothing was written. Do NOT close the record after a refused append — nothing landed, and closing freezes the missing section for good (失败 ⇒ 勿 close)."
|
|
257
|
+
/** 拒串:edit 的锚触及**受保护行** ∥ 替换引入一条 `**状态行**:` 行(§4.19 锚判据 + §4.21.2——保护面是段头区内
|
|
258
|
+
* 第一个形态行〔位置义〕,不是“任何状态行”;串逐字 = `TOOLS.md` §6.24 ⑤ E5,未改)。 */
|
|
259
|
+
const EDIT_STATUS_LINE_ERROR = "batch: edit would rewrite or introduce a **状态行** line — that line has exactly one writer: status, with a value from the section's word list. Nothing was written."
|
|
260
|
+
/** 拒串:edit 空锚(E1——空锚的命中计数不会 ≥2,按「恰一次」会放行并插在段首)。 */
|
|
261
|
+
const EDIT_BLANK_ANCHOR_ERROR = "batch: edit needs a non-empty oldText — a blank anchor cannot match exactly once. Nothing was written."
|
|
262
|
+
/** 拒串:edit 的 newText 缺参 / 非串(`type: string` 的 fail-closed 面——缺参若当空串处理就是静默删区间)。 */
|
|
263
|
+
const EDIT_NO_TEXT_ERROR = "batch: newText must be a string — pass an empty string to delete the anchored range. Nothing was written."
|
|
264
|
+
/** 拒串:edit 的 `newText` 单次超限(§4.21.1 第二道门——上限与 `append` **同一条**:一个通道能装下的,
|
|
265
|
+
* 另一个不能装;上限常量复用 `MAX_TEXT_CHARS`,不另立第二个数)。 */
|
|
266
|
+
const EDIT_TEXT_OVER_LIMIT_ERROR = `batch: edit newText is over the ${MAX_TEXT_CHARS}-character single-call limit — the same cap append enforces (one write channel must not carry what the other refuses). Split it into several edits: each call replaces one anchored range. Nothing was written.`
|
|
267
|
+
/** 拒串:edit 把一条状态行**顶进**段头区(§4.21.2 第二道——改写后区内条数不得多于改写前)。
|
|
268
|
+
* 它堆的是「删掉隔在中间的子标题、把段体那条顶进区」:这条路一个字都不写状态行,却能让一条手写行
|
|
269
|
+
* 变成**受保护行**(机器不读的副本一夜变成唯一可读行)。 */
|
|
270
|
+
const EDIT_STATUS_LINE_PROMOTION_ERROR = (seg, before, after) => `batch: edit would promote a status line into the §${seg} header zone — the zone would hold ${after} \`**状态行**\` lines against ${before} before this edit, and a section's status is read only when the zone holds exactly one. This edit writes no status line: it moves one in by deleting the heading that fenced it off. Nothing was written. Keep the §${seg} header zone at one status line at most: delete the surplus line itself instead of the heading, or move the surplus line out of the zone.`
|
|
271
|
+
/** 拒串:append 的 text 携行首状态行形态行(§4.21.4 写面第一道——J3:工具通道只能让形态行变少)。
|
|
272
|
+
* 两个合法写法**内联在同一句**(不各占独立一行):拒文自己若以该形态起行,照抄拒文的一笔会被同一道闸再拒一次。 */
|
|
273
|
+
const STATUS_LINE_IN_APPEND_ERROR = "batch_segment: append refused — the text carries a line that starts with the status-line form (行首 `**状态行**:…`), and that line has exactly one writer: status (J3: the tool channel may only make status-line rows fewer, never more). Nothing was written. Two legal ways: quote it instead — start the line with `> ` (块引用) — or rewrite it as prose that does not begin with that form; to change a section's own status line, call batch with action \"status\"."
|
|
274
|
+
|
|
275
|
+
// ─── edit 面(段内就地改:`BATCH-RECORD.md` §4.19 的代码单源)─────────────────────
|
|
276
|
+
|
|
277
|
+
/** 段体**字符区间**(`## §N` 标题行的行尾之后 → 下一 `## §N` 段头之前;段头缺失 ⇒ null)。
|
|
278
|
+
* 段定位判据与 `batch-skeleton.mjs` 同源(`sectionHeaderRe` + `/^## §\d/gm`)——本档只取**偏移**。
|
|
279
|
+
* 区间函数住这里而不进骨架档:骨架档当年已过 500 行,本批不带出助手函数(分批记录 2.4 贴线判)。 */
|
|
280
|
+
function sectionBodySpan(src, seg) {
|
|
281
|
+
const hdr = sectionHeaderRe(seg).exec(src)
|
|
282
|
+
if (!hdr) return null
|
|
283
|
+
const nextRe = /^## §\d/gm
|
|
284
|
+
nextRe.lastIndex = hdr.index + hdr[0].length
|
|
285
|
+
const next = nextRe.exec(src)
|
|
286
|
+
const end = next ? next.index : src.length
|
|
287
|
+
const nl = src.indexOf("\n", hdr.index) // 段头行余文(`## §N …` 的标题部分)不入段体
|
|
288
|
+
return { start: nl < 0 || nl + 1 > end ? end : nl + 1, end }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* edit 执行体(action=edit——作者对自己那一段的**受认可通道**,不是唯一通道;判据单源 = `docs/core/design/BATCH-RECORD.md` §4.19):
|
|
293
|
+
* 锚定区间替换——`oldText` 在**目标段段体**内恰一次命中(逐字节,含行尾)⇒ 换为 `newText`;`newText` 空串 = 删区间。
|
|
294
|
+
* 判定与写盘**全部复用既有面**(零新写盘路径):段域 = 同模式 append 白名单(§4.1 一处单源,一张表两个动作)· path 门(D-BR21)·
|
|
295
|
+
* 冻结拒写(§4.9 门 ②,串与 append 逐字同)· 剥凭证(同一条 `sanitizeText`;§3 另丢伪造轮次戳——§4.7 不变量 5)· 骨架保护 ·
|
|
296
|
+
* `newText` 单次上限 ∥ F11-C 死占位(§4.21.1 两道门,与 append 同一批判据)·
|
|
297
|
+
* 文档机检门(段长类判 `newText`、锚类判改后视图的本次替换区)· 状态行被动写(`§2`/`§5` 改后段内有正文 ⇒
|
|
298
|
+
* 同笔置完成词,判据单源 = 骨架档 `statusDonePlan`,§4.12)· `onWrite` 记账 · 一次 `writeFileSync`。
|
|
299
|
+
* **不跑 §5 段序门**(§4.19「段序门」行:edit 须有既有锚 ⇒ 写不出原本不存在的内容,造不出「先实施」;
|
|
300
|
+
* 反过来在 §3 进行中时拒掉 §5 的旧行,正是本批要消的那种死锁)。
|
|
301
|
+
* **不做整段替换**(段体实测两成超 `MAX_TEXT_CHARS`——重发全文 = 静默丢内容)∥ **不做模糊匹配**(一次近似改写 = 一次
|
|
302
|
+
* 没人看见的错改;匹配 = 逐字节,`\r\n` 档上的 LF 锚零命中 ⇒ 拒,安全方向)。
|
|
303
|
+
* 拒面(全 fail-closed,零字节落盘):空锚 E1 · 零命中 E2 · ≥2 命中(列命中行号)E3 · 锚越段界 E4 · 锚触及**受保护行** ∥
|
|
304
|
+
* 替换**引入**一条形态行 E5 · 改写后段头区条数多于改写前 E5b(§4.21.2 第二道)· 骨架保护 ∥ 冻结 ∥ 段域(含工程**设计轨**的
|
|
305
|
+
* `§3`——§4.4 只读)∥ newText 缺参 ∥ `newText` 超单次上限(§4.21.1)∥ 档头死占位残留(§4.21.1 F11-C)。
|
|
306
|
+
*/
|
|
307
|
+
export function editBatchRecord({ args, ctx, review, reviewTrack, batchDoc, onWritten }) {
|
|
308
|
+
const agent = ctx?.agent ?? {}
|
|
309
|
+
const cwd = agent.cwd
|
|
310
|
+
const depth0 = ctx?.depth === 0
|
|
311
|
+
const engineering = !!agent?.config?.agent?.engineering
|
|
312
|
+
// 段域(§4.19「段域」行):**= 同模式 append 白名单**——能 append 哪段就 edit 哪段,段域一处单源、一张表两个动作。
|
|
313
|
+
// 子代理侧取身份段(§4.1:身份 → 段;含「模式 × 评审轨」那一维——工程设计轨的 §3 取到 null);
|
|
314
|
+
// depth-0 侧取同模式 append 白名单(工程 §1/§4/§6 ∥ 普通 §1/§2/§4/§5/§6,§4.16)。
|
|
315
|
+
const own = allowedSegment(agent, review, engineering, reviewTrack)
|
|
316
|
+
if (!depth0 && own === null) {
|
|
317
|
+
if (review) throw new Error(sectionThreeDesignTrackError("batch"))
|
|
318
|
+
throw new Error(noSegmentError(engineering))
|
|
319
|
+
}
|
|
320
|
+
const n = segmentNumber(args?.segment)
|
|
321
|
+
if (n === null) {
|
|
322
|
+
throw new Error(depth0
|
|
323
|
+
? `batch: unknown segment ${JSON.stringify(args?.segment ?? null)} — depth-0 edit rewrites a section of the in-flight record that you author (${engineering ? "§1/§4/§6" : "§1/§2/§4/§5/§6"} — pass the section number you write, e.g. "§4"). Nothing was written.`
|
|
324
|
+
: `batch_segment: unknown segment ${JSON.stringify(args?.segment ?? null)} — pass the section number you write (e.g. "§${own}").`)
|
|
325
|
+
}
|
|
326
|
+
if (depth0) {
|
|
327
|
+
const domain = engineering ? DEPTH0_SEGMENTS.append.engineering : DEPTH0_SEGMENTS.append.normal
|
|
328
|
+
if (!domain.includes(n)) {
|
|
329
|
+
throw new Error(engineering
|
|
330
|
+
? `batch: §${n} is not yours to write — in engineering mode depth-0 edit rewrites §1/§4/§6 (一段一作者: §2 = eng-designer, §3 = design review, §5 = eng-coder). Nothing was written.`
|
|
331
|
+
: `batch: §${n} is not yours to write — in normal mode depth-0 edit rewrites §1/§2/§4/§5/§6 (一段一作者: §3 = design review only). Nothing was written.`)
|
|
332
|
+
}
|
|
333
|
+
} else if (n !== own) {
|
|
334
|
+
throw new Error(`batch_segment: §${n} is not yours to write — this caller writes §${own} only (一段一作者: eng-designer → §2, design review → §3, eng-coder → §5). Nothing was written.`)
|
|
335
|
+
}
|
|
336
|
+
// 目标定位(D-BR21)——与 append / status 同序:段域 → path 门 → 取值。
|
|
337
|
+
const givenPath = typeof args?.path === "string" && args.path.trim() ? args.path : null
|
|
338
|
+
let abs
|
|
339
|
+
if (givenPath) {
|
|
340
|
+
if (!depth0) {
|
|
341
|
+
throw new Error("batch: path is a depth-0-only parameter (D-BR21) — your target record arrives via the spawn binding / the review instance key. Nothing was written.")
|
|
342
|
+
}
|
|
343
|
+
abs = resolveBatchDocPath(cwd, givenPath)
|
|
344
|
+
} else if (depth0) {
|
|
345
|
+
abs = findInFlightBatch(cwd, batchDocBases(cwd)) // 歧义锚 ⇒ 基底 = 候选并集(#828——唯一在飞批照取 / ≥2 照列)
|
|
346
|
+
} else {
|
|
347
|
+
if (!batchDoc) {
|
|
348
|
+
throw new Error("batch_segment: no batch record is bound to this caller — there is no path parameter by design (the target arrives via the spawn binding / the review instance key). Report the section as not written.")
|
|
349
|
+
}
|
|
350
|
+
abs = resolveBatchDocPath(cwd, batchDoc)
|
|
351
|
+
}
|
|
352
|
+
const src = readFileSync(abs, "utf8")
|
|
353
|
+
const status = readBatchStatusLine(src)
|
|
354
|
+
if (status === "closed") throw new Error(FROZEN_RECORD_ERROR)
|
|
355
|
+
if (status === "unknown") throw new Error(UNRESOLVED_STATUS_ERROR)
|
|
356
|
+
const oldText = args?.oldText
|
|
357
|
+
if (typeof oldText !== "string" || !oldText.trim()) throw new Error(EDIT_BLANK_ANCHOR_ERROR)
|
|
358
|
+
if (typeof args?.newText !== "string") throw new Error(EDIT_NO_TEXT_ERROR)
|
|
359
|
+
// 两道门(§4.21.1——J1:同一本档的两条写入通道过同一批骨架判据;位次 = `newText` 形态判据之后、锚解析之前):
|
|
360
|
+
// ① 单次上限:上限只挂 `append` 时等于没有(`edit` 是另一条能装下同样内容的通道);
|
|
361
|
+
// ② F11-C 死占位:档头仍带骨架占位 ⇒ 这本档还不是真档,`edit` 是唯一还能在这种档上落字的通道。
|
|
362
|
+
// 两个挂点均复用既有单源(`MAX_TEXT_CHARS` ∥ `findPlaceholderResidue`)——零新常量、零新依赖。
|
|
363
|
+
if (args.newText.length > MAX_TEXT_CHARS) throw new Error(EDIT_TEXT_OVER_LIMIT_ERROR)
|
|
364
|
+
if (/^## §\d/m.test(args.newText)) throw new Error(SKELETON_GUARD_ERROR)
|
|
365
|
+
const editResidues = findPlaceholderResidue(src)
|
|
366
|
+
if (editResidues.length) throw new Error(placeholderResidueError(editResidues))
|
|
367
|
+
const span = sectionBodySpan(src, n)
|
|
368
|
+
if (!span) throw new Error(missingSectionError(n))
|
|
369
|
+
const body = src.slice(span.start, span.end)
|
|
370
|
+
const hits = []
|
|
371
|
+
for (let at = body.indexOf(oldText); at >= 0; at = body.indexOf(oldText, at + 1)) hits.push(at)
|
|
372
|
+
if (!hits.length) {
|
|
373
|
+
// 零命中 vs 越段界:全档别处有 ⇒ 锚越出目标段段体(E4);全档无 ⇒ 真零命中(E2)。两者都 fail-closed:
|
|
374
|
+
// 「删那行作废判据」若零命中放行,会报成功而一字未删——比不做更坏。
|
|
375
|
+
throw new Error(src.includes(oldText)
|
|
376
|
+
? `batch: edit anchor crosses the §${n} section boundary — it starts outside the section body or reaches the next §N header, and rewriting across it would break section location. Nothing was written.`
|
|
377
|
+
: `batch: edit anchor not found — oldText must match the §${n} body exactly once, byte for byte (line endings included). Nothing was written. Re-read the section and pass its current text verbatim.`)
|
|
378
|
+
}
|
|
379
|
+
if (hits.length > 1) {
|
|
380
|
+
const lines = hits.map((at) => src.slice(0, span.start + at).split("\n").length).join(", ")
|
|
381
|
+
throw new Error(`batch: edit anchor is not unique — it matches ${hits.length} places inside §${n} (lines ${lines}). Extend the anchor until it matches once. Nothing was written.`)
|
|
382
|
+
}
|
|
383
|
+
const hit = hits[0]
|
|
384
|
+
// 状态行只有一个写者(status):锚**触及受保护行**(§4.21.2「位置义」= 段头区内第一个形态行)⇒ 拒;
|
|
385
|
+
// 锚触及**非受保护**的形态行 ⇒ 放行(`newText` 空串 = 删除惰性副本——机器不读的那行,删了不改变任何判据);
|
|
386
|
+
// 替换**引入**一条 ⇒ 仍拒(既有判据,串逐字不变)。
|
|
387
|
+
const guarded = firstZoneStatusLineRange(src, n)
|
|
388
|
+
const hitStart = hit + span.start
|
|
389
|
+
const hitEnd = hitStart + oldText.length
|
|
390
|
+
if (guarded && hitStart < guarded.end && hitEnd > guarded.start) throw new Error(EDIT_STATUS_LINE_ERROR)
|
|
391
|
+
if (args.newText.split(/\r?\n/).some((line) => STATUS_LINE_RE.test(line))) throw new Error(EDIT_STATUS_LINE_ERROR)
|
|
392
|
+
// 替换文本落盘取目标档的 EOL 形(`sanitizeText` 逐行处理后以 LF 相连;同 append 的 `insertIntoSection` 口径)。
|
|
393
|
+
// 来源戳只有工具能盖(§4.7 不变量 5)⇒ `edit §3` 的 `newText` 同 append 丢伪造轮次戳行;其余段无戳可丢。
|
|
394
|
+
const eol = src.includes("\r\n") ? "\r\n" : "\n"
|
|
395
|
+
const replacement = sanitizeText(args.newText, n === 3).replaceAll("\n", eol)
|
|
396
|
+
const newBody = body.slice(0, hit) + replacement + body.slice(hit + oldText.length)
|
|
397
|
+
const postEdit = src.slice(0, span.start) + newBody + src.slice(span.end)
|
|
398
|
+
// 第二道(§4.21.2 E5b——J3 在 `edit` 面的机检形):改写后该段段头区内状态行条数**不得多于**改写前。
|
|
399
|
+
// 堆的是「删掉隔在中间的子标题、把段体那条顶进段头区」:那条路一个字都不写状态行,却能让一条手写行
|
|
400
|
+
// 变成受保护行。纯函数单源 `zoneStatusCount`(前后各取一次,不另写区界判断)。
|
|
401
|
+
const zoneBefore = zoneStatusCount(src, n)
|
|
402
|
+
const zoneAfter = zoneStatusCount(postEdit, n)
|
|
403
|
+
if (zoneAfter > zoneBefore) throw new Error(EDIT_STATUS_LINE_PROMOTION_ERROR(n, zoneBefore, zoneAfter))
|
|
404
|
+
// 状态行同笔(§4.12——与 append 同一条被动写,判据单源 `statusDonePlan`):`§2`/`§5` 改后段内有正文
|
|
405
|
+
// ⇒ 该段状态行置完成词;`§3` 的置位归裁定计划(`edit` 不挂裁定行闸,也不置位——§4.4),其余段不动。
|
|
406
|
+
const done = statusDonePlan(postEdit, n)
|
|
407
|
+
const finalSrc = done ? updateSectionStatusLine(postEdit, n, done.statusValue) : postEdit
|
|
408
|
+
// 文档机检门:工程模式的段长类判本次写入的 `newText`;锚类判改后视图 `recordView` 里的本次替换区;普通模式跳过。
|
|
409
|
+
// 区外锚类不入判集、也不出行(判域 = 本次替换区),别让另一段的失效引文与本段互锁。REFUSED ⇒ 拒(零字节落盘)∥ ERROR ⇒ 写 + 回执明示。
|
|
410
|
+
const gate = ctx.agent?.config?.agent?.engineering === false ? null : runBatchGate({ abs, cwd, agent, scope: "writer", pending: { section: n, body: replacement, recordView: finalSrc } })
|
|
411
|
+
if (gate?.state === "refused") throw new Error(gateRefusalMessage(gate, "edit"))
|
|
412
|
+
writeFileSync(abs, finalSrc)
|
|
413
|
+
// #84:记账面按端注入(与 append 同一条缝——缺省 no-op = CLI 语义零行为变)。
|
|
414
|
+
agent._batchWrites = (agent._batchWrites ?? 0) + 1 // §7.1 `wrote` 判据:成功写盘计数(与 append 同一条面)
|
|
415
|
+
onWritten?.(agent, abs)
|
|
416
|
+
return `batch: edited §${n} of the batch record — replaced ${oldText.length} characters with ${replacement.length}.${done ? ` §${n} 状态行 = ${done.statusValue}(同笔置位——不另调 status).` : ""}${n === 1 ? `${goAheadReceiptLine(finalSrc)}${goAheadEndLine(src, finalSrc)}` : ""}${gateReceiptLine(gate)}`
|
|
417
|
+
}
|