@thincoder/core 0.11.1 → 0.11.2
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 +46 -2
- package/advisor/convergence.mjs +34 -3
- package/advisor/loop.mjs +42 -10
- package/advisor/messages.mjs +42 -25
- package/advisor/project-context.mjs +171 -42
- package/advisor/run.mjs +7 -4
- package/advisor.mjs +29 -43
- package/agent/batch-attribution.mjs +87 -0
- package/agent/batch-changed-surface.mjs +129 -0
- package/agent/completion.mjs +44 -3
- package/agent/dispatch-gates.mjs +6 -0
- package/agent/dispatch.mjs +8 -3
- package/agent/family-tools.mjs +27 -12
- package/agent/helpers.mjs +122 -1
- package/agent/offload.mjs +25 -29
- package/agent/post-turn.mjs +24 -10
- package/agent/record-results.mjs +18 -1
- package/agent/run-stages.mjs +77 -32
- package/agent/run-start.mjs +37 -0
- package/agent/setup-reminders.mjs +9 -2
- package/agent/setup.mjs +24 -11
- package/agent/spawn-child.mjs +38 -1
- package/agent/timers.mjs +5 -3
- package/agent/turn-loop.mjs +30 -5
- package/agent/turn-projection.mjs +386 -0
- package/agent/turn-task-state.mjs +274 -0
- package/agent/turn-tool-process.mjs +168 -0
- package/agent/write-gate-ledger.mjs +569 -0
- package/agent/write-gate.mjs +29 -352
- package/agent-tools/advisor-async.mjs +29 -169
- package/agent-tools/advisor-launch.mjs +178 -0
- package/agent-tools/advisor-settle.mjs +28 -1
- package/agent-tools/advisor.mjs +36 -47
- package/agent-tools/audit-block.mjs +6 -6
- package/agent-tools/batch-closeout.mjs +273 -35
- package/agent-tools/batch-gate.mjs +142 -342
- package/agent-tools/batch-ledger-bind.mjs +226 -17
- package/agent-tools/batch-ledger-view.mjs +109 -0
- package/agent-tools/batch-lifecycle.mjs +169 -46
- package/agent-tools/batch-paths.mjs +29 -8
- 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 +379 -0
- package/agent-tools/batch-skeleton.mjs +192 -239
- package/agent-tools/batch-supersede.mjs +232 -0
- package/agent-tools/batch-tier-gate.mjs +297 -0
- package/agent-tools/batch.mjs +182 -439
- package/agent-tools/consult.mjs +7 -4
- package/agent-tools/context.mjs +44 -17
- 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/doc-check.mjs +43 -33
- package/agent-tools/doc-graph.mjs +140 -0
- package/agent-tools/eng.mjs +9 -8
- package/agent-tools/escalate-async.mjs +2 -2
- package/agent-tools/manifest.mjs +34 -18
- package/agent-tools/read-history.mjs +112 -11
- package/agent-tools/review-facts.mjs +10 -3
- package/agent-tools/settings.mjs +23 -2
- package/agent-tools/spawn-gates.mjs +9 -8
- package/agent-tools/ssh-session.mjs +2 -2
- package/agent-tools/ssh-text.mjs +155 -0
- package/agent-tools/ssh-transport.mjs +19 -148
- package/agent-tools/subagent-actions-query.mjs +10 -6
- package/agent-tools/subagent-async.mjs +8 -9
- 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 +4 -4
- package/agent-tools/subagent-spawn.mjs +90 -110
- package/agent-tools/subagent.mjs +11 -10
- 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 +20 -1
- package/attachments.mjs +3 -3
- 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/config-defaults.mjs +2 -1
- package/config-migrate.mjs +40 -0
- package/config-normalize.mjs +28 -2
- package/config-reload.mjs +59 -0
- package/config.mjs +62 -98
- package/context-degrade.mjs +38 -10
- package/context-echo.mjs +11 -0
- package/context.mjs +149 -83
- package/conventions.mjs +11 -9
- package/declaration.mjs +31 -5
- 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/explore-distill.mjs +11 -5
- 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 +6 -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 +78 -19
- package/ledger-write-ops.mjs +130 -41
- package/ledger.mjs +1 -1
- package/manifest-discovery.mjs +3 -1
- package/manifest-schema.mjs +27 -5
- package/memory/schema.mjs +2 -2
- package/package.json +3 -1
- package/path-resolve.mjs +338 -57
- package/prompt-files.mjs +91 -1
- package/prompt-overlays.mjs +9 -6
- package/prompts/advisor-design.md +62 -21
- package/prompts/advisor-round1.md +34 -1
- package/prompts/advisor-round2.md +40 -7
- package/prompts/advisor-round3.md +39 -6
- package/prompts/common.md +14 -387
- package/prompts/consult-base.md +5 -1
- package/prompts/discipline-engineering.md +226 -51
- package/prompts/discipline-normal.md +4 -255
- package/prompts/knowledge/business-gates.md +22 -0
- package/prompts/knowledge/decision-quality.md +23 -0
- package/prompts/knowledge/doc-lookup.md +35 -0
- package/prompts/knowledge/document-writing.md +51 -0
- package/prompts/knowledge/documentation-evaluation.md +56 -0
- package/prompts/knowledge/evidence-discipline.md +57 -0
- package/prompts/knowledge/normal-coding-workflow.md +104 -0
- package/prompts/knowledge/normal-delivery-collaboration.md +108 -0
- package/prompts/knowledge/normal-work-management.md +57 -0
- package/prompts/knowledge/product-security.md +23 -0
- package/prompts/knowledge/project-records.md +62 -0
- package/prompts/knowledge/tool-advisor.md +29 -0
- package/prompts/knowledge/tool-apply-patch.md +18 -0
- package/prompts/knowledge/tool-bash.md +39 -0
- package/prompts/knowledge/tool-batch-eng-coder.md +12 -0
- package/prompts/knowledge/tool-batch-eng-designer.md +12 -0
- package/prompts/knowledge/tool-batch-review.md +12 -0
- package/prompts/knowledge/tool-batch.md +80 -0
- package/prompts/knowledge/tool-browser.md +57 -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 +17 -0
- package/prompts/knowledge/tool-doc-check.md +70 -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 +38 -0
- package/prompts/knowledge/tool-eng.md +2 -0
- package/prompts/knowledge/tool-execute.md +16 -0
- package/prompts/knowledge/tool-fetch.md +12 -0
- package/prompts/knowledge/tool-file-ops.md +16 -0
- package/prompts/knowledge/tool-get-current-time.md +7 -0
- package/prompts/knowledge/tool-git-read.md +19 -0
- package/prompts/knowledge/tool-git.md +29 -0
- package/prompts/knowledge/tool-glob.md +15 -0
- package/prompts/knowledge/tool-goal.md +8 -0
- package/prompts/knowledge/tool-grep.md +18 -0
- package/prompts/knowledge/tool-hashline-edit.md +14 -0
- package/prompts/knowledge/tool-insert-after.md +15 -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 +35 -0
- package/prompts/knowledge/tool-ledger.md +42 -0
- package/prompts/knowledge/tool-lint.md +16 -0
- package/prompts/knowledge/tool-ls.md +13 -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 +31 -0
- package/prompts/knowledge/tool-memory-read.md +20 -0
- package/prompts/knowledge/tool-memory.md +15 -0
- package/prompts/knowledge/tool-notify-parent.md +12 -0
- package/prompts/knowledge/tool-operations.md +114 -0
- package/prompts/knowledge/tool-peer-instances.md +2 -0
- package/prompts/knowledge/tool-plan.md +6 -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 +36 -0
- package/prompts/knowledge/tool-read-image.md +11 -0
- package/prompts/knowledge/tool-read.md +21 -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 +18 -0
- package/prompts/knowledge/tool-skill.md +6 -0
- package/prompts/knowledge/tool-ssh.md +52 -0
- package/prompts/knowledge/tool-subagent.md +59 -0
- package/prompts/knowledge/tool-task.md +12 -0
- package/prompts/knowledge/tool-team-ledger-read.md +11 -0
- package/prompts/knowledge/tool-team-ledger.md +11 -0
- package/prompts/knowledge/tool-timer.md +23 -0
- package/prompts/knowledge/tool-tree.md +14 -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 +9 -0
- package/prompts/knowledge/upstream-protocol.md +43 -0
- package/prompts/persona-eng-coder.md +21 -3
- package/prompts/persona-eng-designer.md +131 -169
- package/prompts/persona-engineering.md +142 -84
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-plan.md +5 -1
- package/prompts/subagent-base.md +2 -0
- package/provider/anthropic.mjs +6 -4
- package/provider/core.mjs +3 -2
- package/provider/errors.mjs +29 -4
- package/provider/google.mjs +24 -7
- package/provider/responses.mjs +3 -1
- package/provider-flows.mjs +2 -2
- 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 +72 -17
- package/session-slot-write.mjs +37 -5
- package/session.mjs +24 -8
- package/skip-dirs.mjs +2 -2
- package/team-ledger.mjs +13 -9
- package/token-ttl.mjs +73 -4
- package/token-window.mjs +167 -33
- package/tool-docs/advisor.md +2 -27
- package/tool-docs/apply_patch.md +1 -15
- package/tool-docs/bash.md +2 -41
- 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 +52 -7
- 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 +1 -1
- package/tools/file.mjs +16 -8
- package/tools/git.mjs +24 -9
- package/tools/index.mjs +6 -0
- package/tools/knowledge.mjs +143 -0
- package/tools/linter.mjs +3 -3
- package/tools/ops.mjs +3 -1
- package/tools/patch.mjs +123 -43
- package/tools/shared.mjs +28 -27
- package/tools/write-gate-params.mjs +31 -0
- package/tools/write-path.mjs +16 -4
- package/window-policy.mjs +203 -0
- package/doc-check/anchors.mjs +0 -235
package/agent/turn-loop.mjs
CHANGED
|
@@ -10,16 +10,18 @@ import { abortError, annotateAbort } from "../abort-provenance.mjs"
|
|
|
10
10
|
import { specForModel, assistantToolCallMessage } from "../config.mjs"
|
|
11
11
|
import { executeToolCalls } from "./dispatch.mjs"
|
|
12
12
|
import { recordToolResults } from "./record-results.mjs"
|
|
13
|
+
// 归属表(收口取材的判据一):中断分支的记账点与常路同笔——两处 `_touchedFiles.push` 都写归属。
|
|
14
|
+
import { recordAttribution } from "./batch-attribution.mjs"
|
|
13
15
|
import { injectPostTurn } from "./post-turn.mjs"
|
|
14
16
|
import { handleCompletion, isSubstantiveResponse } from "./completion.mjs"
|
|
15
|
-
import { runCompactionCheck, enforceContextFit, injectTurnReminders, injectResponseReminders } from "./run-stages.mjs"
|
|
17
|
+
import { runCompactionCheck, enforceContextFit, preflightWindowFit, injectTurnReminders, injectResponseReminders } from "./run-stages.mjs"
|
|
16
18
|
import { recordTokenSample } from "../token-window.mjs"
|
|
17
19
|
import { callModelTurn } from "./chat-call.mjs"
|
|
18
20
|
import {
|
|
19
21
|
turnFrame, // 第 19 批(TURN-ACROSS-SEGMENTS):跨段累计编号帧(设计 TURN-CAP-CONTINUE.md §4)
|
|
20
22
|
ContinueError,
|
|
21
23
|
FILE_MUTATORS, toolTouchPaths,
|
|
22
|
-
wroteSince, // #934(§6.32.2 ① 判据式):写序是否推进——轮顶 ∥
|
|
24
|
+
wroteSince, // #934(§6.32.2 ① 判据式):写序是否推进——轮顶 ∥ 撞帽收尾 ∥ advisor 预算复位(§6.4 A)三处判据点共用
|
|
23
25
|
} from "./helpers.mjs"
|
|
24
26
|
|
|
25
27
|
export async function runTurnLoop(agent, {
|
|
@@ -49,6 +51,11 @@ export async function runTurnLoop(agent, {
|
|
|
49
51
|
// `context` 工具的 stats 报**同一口径**(不自行重算第二口径;VSC checkAndCompact 同款暂存)。
|
|
50
52
|
agent._ctxBasis = { threshold, overhead: compactionOverhead }
|
|
51
53
|
|
|
54
|
+
// 切换预判(`CONTEXT-COMPACTION.md` §5.2):run 装配之后、第一个回合之前——模型切换 ∥ `providers[].context`
|
|
55
|
+
// 调小 ∥ `reasoningEffort` 变更这三条来路都让 `limit` 变小,而 `agent.history` 一条没少。预判点因此就是
|
|
56
|
+
// 装备点(参数按 run 现算),一处覆盖三条来路,不必为每条各立钩子。越线 ⇒ 跑与安全点同一条处置链。
|
|
57
|
+
await preflightWindowFit(agent, { threshold, callbacks, compactionOverhead, recentCallSigs, signal })
|
|
58
|
+
|
|
52
59
|
// §6.17.1 链三(越窗 400 自适应):单回合调用外包一层 catch——服务端说越窗 ⇒ 强制压一次 + 重试同一回合一次。
|
|
53
60
|
// 排除面:`stage ∈ {compress, distill}` 的请求**不经本函数**(压缩机与蒸馏各自直调 `chat`,续写子请求的
|
|
54
61
|
// 失败已在 `provider/core.mjs` 折进 `_warnings`)⇒ 本 catch 只接回合请求的 typed 越窗,不存在「已流出再重放」。
|
|
@@ -157,7 +164,8 @@ export async function runTurnLoop(agent, {
|
|
|
157
164
|
// 空响应预算复位(AGENT-LOOP.md §6.5① · D-AL28):本轮响应**有实质** ⇒ 计数清 0(实质 = 有正文
|
|
158
165
|
// 或带工具调用;判据单源 = completion.mjs `isSubstantiveResponse`)。落位 = 本轮响应存在的第一处、
|
|
159
166
|
// 也是四支路(下方流规则 abort ∥ 用户中断 ∥ 工具批 ∥ 零工具)发散前的最后一处——落进任一分支都会漏其余三支。
|
|
160
|
-
//
|
|
167
|
+
// 与下方工具批执行处的推回预算复位分工:那处复位 verify ∥ advisor 两面——verify 面看「有工具批」、
|
|
168
|
+
// advisor 面看「本批写序推进」(§6.4 A:成功的文件写 ∥ 子代理合并才算进展,读文件不算);
|
|
161
169
|
// 本处复位空响应预算、看「有实质」——正文本身已是实质,不必等工具批。
|
|
162
170
|
if (isSubstantiveResponse(response)) agent._emptyRetries = 0
|
|
163
171
|
|
|
@@ -277,10 +285,13 @@ export async function runTurnLoop(agent, {
|
|
|
277
285
|
try {
|
|
278
286
|
const args = JSON.parse(toolCall.arguments)
|
|
279
287
|
const paths = toolTouchPaths(tool, args)
|
|
288
|
+
const touched = []
|
|
280
289
|
for (const p of paths) {
|
|
281
290
|
const abs = resolve(agent.cwd, p)
|
|
282
291
|
if (!agent._touchedFiles.includes(abs)) agent._touchedFiles.push(abs)
|
|
292
|
+
touched.push(abs)
|
|
283
293
|
}
|
|
294
|
+
recordAttribution(agent, touched, agent._batchDoc)
|
|
284
295
|
} catch { /* 畸形 args 不影响记账(touchedFiles 尽力而为) */ }
|
|
285
296
|
}
|
|
286
297
|
// The assistant tool_calls were committed above — synthesize placeholder tool
|
|
@@ -297,9 +308,23 @@ export async function runTurnLoop(agent, {
|
|
|
297
308
|
continue
|
|
298
309
|
}
|
|
299
310
|
|
|
300
|
-
// Model is executing tools
|
|
311
|
+
// Model is executing tools.
|
|
312
|
+
// verify 面:任一工具批即回满(`guardPushbacks = 0` 字面与行为逐字不变——批失败时同样清零,本批未动)。
|
|
301
313
|
guardPushbacks = 0
|
|
302
|
-
|
|
314
|
+
// advisor 面(ADVISOR-CONVERGENCE.md §6.4 A):预算只在被审对象真的变了时回满——判据 = 本批写序
|
|
315
|
+
// 推进了(`wroteSince`,与零落笔看门狗同源;`_mutSeqMark` 轮头前移、此处批后比一次)。写序唯一推进面
|
|
316
|
+
// = `noteMutations`,两个写者:成功的文件写工具(`dispatch-gates.mjs` 的 `noteExecutedMutation`,带
|
|
317
|
+
// 非 `Error:` 前缀门)与子代理代码合并(`mergeChildMutations`)——故合入也算进展,不另立判据。
|
|
318
|
+
// 读文件 / 查代码 / bash / git 一概不复位:它们对被审代码零改变,复位就把「同一条提醒给 N 次机会」
|
|
319
|
+
// 变成无限次(实测形态 = 模型读一次文件、提醒来一条,无限重复)。
|
|
320
|
+
// 到顶诚实提醒的一次性位与本预算同点复位(两行手拉手——§1 20:39 裁定):有新进展 ⇒ 该次交付重新需要
|
|
321
|
+
// 一句诚实声明;若在这里只复位预算、不复位位,则「发过诚实提醒 → 又改代码 → 再收尾」会静默放行
|
|
322
|
+
// (未评审即交付不吭声),而那正是本批要堵的形状。`agent._advisorReminders` **不**随进展复位——
|
|
323
|
+
// 它是未评审周期的上限计数(层 2 的 5 条),复位它等于把周期上限取消。
|
|
324
|
+
if (wroteSince(agent, agent._mutSeqMark)) {
|
|
325
|
+
advisorPushbacks = 0
|
|
326
|
+
agent._advisorHonestySent = false
|
|
327
|
+
}
|
|
303
328
|
|
|
304
329
|
// Commit tool results (pairing, multimodal deferral, mutation accounting, reindex)
|
|
305
330
|
await recordToolResults(agent, toolByName, results)
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent/turn-projection.mjs — 跨轮机读历史投影(设计 = `docs/core/design/CONTEXT-COMPACTION.md` §1.1 ∥ §1.2 ∥
|
|
3
|
+
* `docs/core/design/SESSION.md` §2;批次 = `docs/batches/2026-11-14-tool-output-retention.md`;台账 #2431)。
|
|
4
|
+
*
|
|
5
|
+
* 一轮完成后的机读形态 = 一条轮记录 `{ userRequest, finalResponse, taskState, toolProcess? }`。最近 2 轮
|
|
6
|
+
* (每轮 = 一次用户指令到执行完成,其间多次模型↔工具往返属于同一轮)另带 `toolProcess`——该轮原始段里
|
|
7
|
+
* 「第一次 `assistant(tool_calls)` 起、到最后一个 `tool` 结果止」的连续切片,投影时按消息逐条展开;
|
|
8
|
+
* 第 3 轮及更早只剩「用户请求 ∥ 最终回复 ∥ 任务状态」。人读线(记录 sidecar)不动;但长于 500 字符的工具结果
|
|
9
|
+
* 在它上面只留前段,`read_history` 对这些消息也只回前 500 字符(射程与标记见 `SESSION.md` §5)——需要原文就
|
|
10
|
+
* 重跑工具,两者都不许诺「原文仍在」。
|
|
11
|
+
*
|
|
12
|
+
* 工具段两道预算(§1.2,只做一次——投影 ∥ 保存 ∥ 恢复读同一份):单条字符 ∥ 单轮 token;越限各带标记,
|
|
13
|
+
* 不靠沉默。保留窗与 prune 的保护尾是同一个尾:`retentionWindowStart` 供 `splitHistory` 的 `protectFrom`。
|
|
14
|
+
* 这一族(五个常量 ∥ 九个函数)实现在 `turn-tool-process.mjs`,本档按名取用并在原路径 re-export。
|
|
15
|
+
*
|
|
16
|
+
* 三面同尺(这是本档存在的理由——**同一把尺**),账本与事实单列在 `turn-task-state.mjs`,工具过程保留族单列在
|
|
17
|
+
* `turn-tool-process.mjs`:
|
|
18
|
+
* - 折叠 `turnFoldPlan` ∥ `applyTurnFold` ∥ `registerFoldMarks`:按消息身份定位本轮原始段,替换为投影消息
|
|
19
|
+
* ——`beginRun`(下一轮起点)就地折叠并登记折叠身份(压缩按那张账判覆盖条数);
|
|
20
|
+
* - 投影 `buildTurnRecord` ∥ `turnRecordMessages` ∥ `projectTurns`:一轮 → 机读消息的成形(三面共用);
|
|
21
|
+
* - 恢复 `restoreMachineFromTurns` ∥ `machineLineForSlot`:槽带 `turns` 时按同一段拼法重建机读线——
|
|
22
|
+
* 前者是读向(先做配对收口),后者是 `saveSession` 的写向(不改内存线的数组与对象)。
|
|
23
|
+
* 装配接缝 `registerCompletedTurn` ∥ `applyTurnProjection`:轮边界上取用并清空账本、把待折段就地折掉。
|
|
24
|
+
*
|
|
25
|
+
* 为什么账本与事实搬到 `turn-task-state.mjs`:四块同住一档时内容行越过 500 的档位线(标准
|
|
26
|
+
* `docs/core/standards/CODE-TIER.md` §2.1),而「一轮 → 三条消息」的投影判据只该有一处——拆档只动文件归属,
|
|
27
|
+
* 不动判据;搬走的符号在本档原路径 re-export,调用方零改。
|
|
28
|
+
*
|
|
29
|
+
* 为什么工具过程保留族搬到 `turn-tool-process.mjs`:同一把档位尺——保留窗、两道预算与切片同住本档时内容行照样
|
|
30
|
+
* 越过那条线,而它们与投影三面不是一件事。拆法同上:只动文件归属,五个常量值 ∥ 九个函数体 ∥ 注释语义逐字照搬。
|
|
31
|
+
* 搬走的符号同样在本档原路径 re-export,既有 import 一字不改。
|
|
32
|
+
*
|
|
33
|
+
* 机读线在槽里分三段存(`SESSION.md` §2):`compactCheckpoint`(摘要注记 + 已覆盖的轮记录条数)∥ `turns` ∥
|
|
34
|
+
* `activeRaw`(没有轮记录可依的在途 ∥ 异常段)。保存与恢复两个方向是同一段拼法(`machineLineForSlot` ∥
|
|
35
|
+
* `restoreMachineFromTurns`);覆盖条数按折叠身份账算(`coveredTurnCount`),折叠与压缩都按它判「这一轮是不是已在中段」。
|
|
36
|
+
*
|
|
37
|
+
* 纪律:除轮边界上的账本交接(`registerCompletedTurn` 取用调用方给的账本对象、随后清空账本槽)外,
|
|
38
|
+
* 本档纯函数面零 IO、零抛、零全局态;不改写传入的消息对象(`pushReal` 的双线共享对象只读不写——折叠产出新对象)。
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
// 账本 ∥ 事实 ∥ 状态串 ∥ 成败取形住在 `turn-task-state.mjs`(拆档理由见该档头注);搬走的符号在下方原路径
|
|
42
|
+
// re-export——拆档不动调用方,六处 import 一字不改。本档从它取 `str` ∥ `capText` 与两条状态面函数。
|
|
43
|
+
import { buildTurnTaskState, capText, str, taskStateText } from "./turn-task-state.mjs"
|
|
44
|
+
export { TASK_STATE_MARK, buildTurnTaskState, createTurnLedger, noteTurnRunError, noteTurnToolFacts, taskStateText } from "./turn-task-state.mjs"
|
|
45
|
+
// 可回查位置(`evidence` 面):会话事件日志按天分档——路径取自核日志单源,本档不自造路径。
|
|
46
|
+
// 折叠 no-op 的留痕(CONTEXT-COMPACTION.md §1.1)走同一通道。
|
|
47
|
+
import { logEvent, todayLogPath } from "../log.mjs"
|
|
48
|
+
// 工具过程保留族(五个常量 ∥ 九个函数)住在 `turn-tool-process.mjs`——拆档理由见该档头注(500 档位线 + 只搬不改)。
|
|
49
|
+
// 搬走的符号在本档原路径 re-export:拆档不动调用方,三个导入方(`context.mjs` ∥ `context-degrade.mjs` ∥
|
|
50
|
+
// `agent-tools/context.mjs`)一字不改。装配接缝仍在本档,故本档也按名取用它其中几个。
|
|
51
|
+
import { TOOL_PROCESS_WINDOW, dropToolProcessFromLine, foldHintOf, toolProcessLostOf, toolProcessOf } from "./turn-tool-process.mjs"
|
|
52
|
+
export {
|
|
53
|
+
FOLD_HINT_TOKENS, TOOL_PROCESS_DROP_MARK, TOOL_PROCESS_MSG_CHARS, TOOL_PROCESS_TURN_TOKENS,
|
|
54
|
+
TOOL_PROCESS_WINDOW, clampToolMessage, dropToolProcessFromLine, foldHintOf, isCallGroupStart,
|
|
55
|
+
retentionWindowStart, toolProcessLostOf, toolProcessOf, toolSliceOf, trimToolProcess,
|
|
56
|
+
} from "./turn-tool-process.mjs"
|
|
57
|
+
|
|
58
|
+
// ────────────────────────────── 轮记录与投影 ──────────────────────────────
|
|
59
|
+
|
|
60
|
+
/** 轮记录成形(`{ userRequest, finalResponse, taskState, toolProcess? }`——槽 `turns` 逐项形)。
|
|
61
|
+
* 工具段只挂在保留窗内的轮记录上(§1.1):空段不带键——键缺 = 这一轮的工具过程不在机读线上。 */
|
|
62
|
+
export function buildTurnRecord(ledger, facts = {}) {
|
|
63
|
+
const finalResponse = capText(facts.finalResponse) || null
|
|
64
|
+
const record = {
|
|
65
|
+
userRequest: ledger?.userRequest ?? null,
|
|
66
|
+
finalResponse,
|
|
67
|
+
taskState: buildTurnTaskState(ledger, facts),
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(facts.toolProcess) && facts.toolProcess.length > 0) record.toolProcess = facts.toolProcess
|
|
70
|
+
return record
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 一条轮记录的机读消息(用户请求 ∥ 工具段切片 ∥ 最终回复 ∥ 任务状态——顺序即线上顺序)。
|
|
74
|
+
* 工具段按消息逐条展开:切片里有几条发几条(§1.1)。工具段消息按**引用**发出——折叠把它们插进机读线之后,
|
|
75
|
+
* 窗口裁切要按身份把它们再摘下来(`dropToolProcessFromLine`);读向只读不写,引用不会跨面改对象。 */
|
|
76
|
+
export function turnRecordMessages(record) {
|
|
77
|
+
const out = []
|
|
78
|
+
if (str(record?.userRequest)) out.push({ role: "user", content: record.userRequest })
|
|
79
|
+
for (const m of Array.isArray(record?.toolProcess) ? record.toolProcess : []) {
|
|
80
|
+
if (m && typeof m === "object") out.push(m)
|
|
81
|
+
}
|
|
82
|
+
if (str(record?.finalResponse)) out.push({ role: "assistant", content: record.finalResponse })
|
|
83
|
+
const state = taskStateText(record?.taskState)
|
|
84
|
+
// 任务状态是机器面(人读线已保留原始过程)——`transient` 只影响人读线落盘,发送前被剥离。
|
|
85
|
+
if (state) out.push({ role: "user", content: state, transient: true })
|
|
86
|
+
return out
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 多条轮记录 → 机读消息数组(恢复面按同一投影重建机读线)。 */
|
|
90
|
+
export function projectTurns(turns) {
|
|
91
|
+
const out = []
|
|
92
|
+
for (const record of Array.isArray(turns) ? turns : []) out.push(...turnRecordMessages(record))
|
|
93
|
+
return out
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 折叠计划(纯函数):按**消息身份**找出待折叠轮的原始段,返回 `{ indices, at, folded }`;
|
|
98
|
+
* 无可折叠(消息已被压缩摘要吞掉 / 不是本轮那批对象)⇒ `null`——宁可不折,也不切错位置。
|
|
99
|
+
* 身份判据使本函数对压缩(重建数组)、蒸馏(换新数组)、`repairHistory`(过滤)都稳。
|
|
100
|
+
*/
|
|
101
|
+
export function turnFoldPlan(history, pending) {
|
|
102
|
+
const msgs = pending?.msgs
|
|
103
|
+
if (!(msgs instanceof Set) || msgs.size === 0) return null
|
|
104
|
+
if (!Array.isArray(history) || history.length === 0) return null
|
|
105
|
+
const indices = []
|
|
106
|
+
for (let i = 0; i < history.length; i++) if (msgs.has(history[i])) indices.push(i)
|
|
107
|
+
if (indices.length === 0) return null
|
|
108
|
+
const folded = turnRecordMessages(pending.record)
|
|
109
|
+
if (folded.length === 0) return null
|
|
110
|
+
return { indices, at: indices[0], folded, record: pending.record }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 就地折叠(`beginRun` 用):数组身份不变——持有 `agent.history` 引用的端侧(VSC 保存面)不落空。
|
|
114
|
+
* `agent` 在场时顺带登记折叠身份(`registerFoldMarks`)——压缩按那张账判覆盖条数。 */
|
|
115
|
+
export function applyTurnFold(history, plan, agent = null) {
|
|
116
|
+
if (!plan || !Array.isArray(history)) return history
|
|
117
|
+
for (let k = plan.indices.length - 1; k >= 0; k--) history.splice(plan.indices[k], 1)
|
|
118
|
+
history.splice(Math.min(plan.at, history.length), 0, ...plan.folded)
|
|
119
|
+
registerFoldMarks(agent, plan)
|
|
120
|
+
return history
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 折叠身份登记(`CONTEXT-COMPACTION.md` §6 · `SESSION.md` §2 播种条):本轮记录的那几条消息对象按轮下标入账
|
|
125
|
+
* (`agent._turnFoldMarks[i]`)。压缩按它们判「这一轮的记录消息是不是都已在中段」,恢复面按恢复出的轮记录
|
|
126
|
+
* 重建同一张账——不重建的话,重启后的第二次压缩只见到重启之后的轮,`turnsCovered` 会被写小。
|
|
127
|
+
* `plan.record` 不在 `_turns` 里(旧槽 ∥ 记账已清)⇒ 不登记,不改线也不抛。
|
|
128
|
+
*/
|
|
129
|
+
function registerFoldMarks(agent, plan) {
|
|
130
|
+
if (!agent || !plan?.record) return
|
|
131
|
+
const turns = Array.isArray(agent._turns) ? agent._turns : []
|
|
132
|
+
const index = turns.indexOf(plan.record)
|
|
133
|
+
if (index < 0) return
|
|
134
|
+
const marks = Array.isArray(agent._turnFoldMarks) ? agent._turnFoldMarks : (agent._turnFoldMarks = [])
|
|
135
|
+
marks[index] = [...plan.folded]
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 覆盖条数(压缩当刻算;`cut` = 切割点——它之前那一批会被摘要吞掉):
|
|
140
|
+
* - 起点 = 旧检查点里的条数:不重算、不缩回(重启后的第二次压缩从槽里那一段继续数);
|
|
141
|
+
* - 该轮记录消息**全部**落在切割点之前才算覆盖;一轮被切开不记——恢复时宁可多装一轮,不把半轮当整轮;
|
|
142
|
+
* - 该轮在机读线上一条登记消息都找不到 ⇒ 停(无法证明已覆盖,宁可多装)。
|
|
143
|
+
*/
|
|
144
|
+
export function coveredTurnCount(agent, cut) {
|
|
145
|
+
const turns = Array.isArray(agent?._turns) ? agent._turns : []
|
|
146
|
+
const marks = Array.isArray(agent?._turnFoldMarks) ? agent._turnFoldMarks : []
|
|
147
|
+
const history = Array.isArray(agent?.history) ? agent.history : []
|
|
148
|
+
let covered = normalizeCheckpoint(agent?._compactCheckpoint, turns.length)?.turnsCovered ?? 0
|
|
149
|
+
for (let i = covered; i < turns.length; i++) {
|
|
150
|
+
const onLine = (Array.isArray(marks[i]) ? marks[i] : [])
|
|
151
|
+
.map((m) => history.indexOf(m))
|
|
152
|
+
.filter((at) => at >= 0)
|
|
153
|
+
if (onLine.length === 0) break
|
|
154
|
+
if (!onLine.every((at) => at < cut)) break
|
|
155
|
+
covered = i + 1
|
|
156
|
+
}
|
|
157
|
+
return covered
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 非就地投影(`saveSession` 用):同尺不同副作用——不改内存线的数组与对象。
|
|
161
|
+
* 三段拼法(`SESSION.md` §2):摘要注记(检查点在场时)+ 检查点之后那些轮记录的投影 + 在途段原文。
|
|
162
|
+
* 恢复面 `restoreMachineFromTurns` 是同一拼法的读向(差别只在装线前的配对收口)。 */
|
|
163
|
+
export function machineLineForSlot(turns, { checkpoint = null, activeRaw = [] } = {}) {
|
|
164
|
+
const covered = checkpoint?.turnsCovered ?? 0
|
|
165
|
+
const list = Array.isArray(turns) ? turns : []
|
|
166
|
+
const line = checkpoint ? [{ role: "user", content: checkpoint.note }] : []
|
|
167
|
+
line.push(...projectTurns(list.slice(covered)))
|
|
168
|
+
line.push(...(Array.isArray(activeRaw) ? activeRaw : []))
|
|
169
|
+
return line
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 在途 / 异常段(没有轮记录可依):本轮输入之后尚未登记的那一段——**原样保留**(不折叠也不丢)。
|
|
174
|
+
* 登记完成的轮已进 `turns`,不会出现在这里。
|
|
175
|
+
*/
|
|
176
|
+
export function activeTurnRaw(agent) {
|
|
177
|
+
const history = Array.isArray(agent?.history) ? agent.history : []
|
|
178
|
+
const start = agent?._turnStartLen
|
|
179
|
+
if (!agent?._turnLedger || !Number.isInteger(start) || start < 0 || start > history.length) return []
|
|
180
|
+
return history.slice(start)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 本轮原始段的起点:`prepareRun` 推入的用户输入消息下标(`from` = 调用前的历史长度——
|
|
185
|
+
* 只在本次推入的区间里找,不会误取上一轮的同文请求)。找不到(输入非串 / 形态异常)⇒ `from`。
|
|
186
|
+
* 投影的“行”从这里开始:此前的运行注入(环境行 / 文档与记忆注入 / 异步结果)属于上下文,不随折叠消失
|
|
187
|
+
* ——它们是本轮**输入之前**就位的,不是本轮的模型过程。
|
|
188
|
+
*/
|
|
189
|
+
export function findTurnStartIndex(history, input, from) {
|
|
190
|
+
if (typeof input !== "string" || !Array.isArray(history)) return from
|
|
191
|
+
for (let i = history.length - 1; i >= from; i--) {
|
|
192
|
+
const m = history[i]
|
|
193
|
+
if (m?.role === "user" && m.content === input) return i
|
|
194
|
+
}
|
|
195
|
+
return from
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ────────────────────────────── 恢复 ──────────────────────────────
|
|
199
|
+
|
|
200
|
+
/** 槽里 `toolProcess` 的归一(白名单保留该键——§2:存下去的形状读不回来,等于没存)。盘上是用户数据,
|
|
201
|
+
* 本函数不抛:只认消息面字段,坏项丢弃;全丢光 ⇒ `null`(= 键缺,与「已折叠」同义)。 */
|
|
202
|
+
function normalizeToolProcess(value) {
|
|
203
|
+
if (!Array.isArray(value)) return null
|
|
204
|
+
const out = []
|
|
205
|
+
for (const m of value) {
|
|
206
|
+
if (!m || typeof m !== "object" || Array.isArray(m)) continue
|
|
207
|
+
if (typeof m.role !== "string" || !m.role) continue
|
|
208
|
+
const copy = { role: m.role }
|
|
209
|
+
if (typeof m.content === "string") copy.content = m.content
|
|
210
|
+
else if (Array.isArray(m.content)) copy.content = m.content
|
|
211
|
+
if (typeof m.reasoning_content === "string") copy.reasoning_content = m.reasoning_content
|
|
212
|
+
if (typeof m.name === "string") copy.name = m.name
|
|
213
|
+
if (typeof m.tool_call_id === "string") copy.tool_call_id = m.tool_call_id
|
|
214
|
+
if (Array.isArray(m.tool_calls) && m.tool_calls.length > 0) copy.tool_calls = m.tool_calls
|
|
215
|
+
if (typeof m.ts === "number") copy.ts = m.ts
|
|
216
|
+
out.push(copy)
|
|
217
|
+
}
|
|
218
|
+
return out.length > 0 ? out : null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 槽里的 `turns` 归一(旧槽缺键 ⇒ `[]`;非数组项丢弃——盘上是用户数据,不抛)。 */
|
|
222
|
+
export function normalizeSlotTurns(turns) {
|
|
223
|
+
if (!Array.isArray(turns)) return []
|
|
224
|
+
const out = []
|
|
225
|
+
for (const t of turns) {
|
|
226
|
+
if (!t || typeof t !== "object") continue
|
|
227
|
+
const record = {
|
|
228
|
+
userRequest: str(t.userRequest) || null,
|
|
229
|
+
finalResponse: str(t.finalResponse) || null,
|
|
230
|
+
taskState: t.taskState && typeof t.taskState === "object" ? t.taskState : null,
|
|
231
|
+
}
|
|
232
|
+
const toolProcess = normalizeToolProcess(t.toolProcess)
|
|
233
|
+
if (toolProcess) record.toolProcess = toolProcess
|
|
234
|
+
out.push(record)
|
|
235
|
+
}
|
|
236
|
+
return out
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 落槽形态(逐项新建记录:字符串字段逐字拷贝,`taskState` 按引用透传——落盘前只读,
|
|
240
|
+
* 保存之后再改内存记录不是本函数能拦的形)。 */
|
|
241
|
+
export function slotTurns(turns) {
|
|
242
|
+
return normalizeSlotTurns(turns)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 压缩检查点归一(`{ note, turnsCovered }`——槽是盘上用户数据,本函数不抛):两字段齐且合法 ⇒ 新对象;
|
|
247
|
+
* 否则 `null`(= 「本会话还没压过」,与键缺同义)。`turnsCovered` 夹在当前轮记录条数内——
|
|
248
|
+
* 盘上被改坏的大数不得让恢复少装轮(少装 = 静默丢掉已登记的轮)。
|
|
249
|
+
*/
|
|
250
|
+
export function normalizeCheckpoint(value, turnsLen = Number.MAX_SAFE_INTEGER) {
|
|
251
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null
|
|
252
|
+
const note = str(value.note)
|
|
253
|
+
if (!note) return null
|
|
254
|
+
if (!Number.isInteger(value.turnsCovered) || value.turnsCovered < 0) return null
|
|
255
|
+
return { note, turnsCovered: Math.min(value.turnsCovered, Math.max(0, turnsLen)) }
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 在途段尾端配对收口(`CONTEXT-COMPACTION.md` §6 · `SESSION.md` §2 `activeRaw` 行):装线前
|
|
260
|
+
* ① 前面没有对应调用的 `tool` 消息不装(孤立的工具结果——首个请求必 400);
|
|
261
|
+
* ② 尾部没有工具结果的 `assistant(tool_calls)` 不装——来由是一次普通的 Stop(Ctrl+C)落在工具执行中:
|
|
262
|
+
* `turn-loop.mjs` 先 `pushReal` 这条调用消息、再跑工具批,中止在结果入线前抛出(`dispatch-run.mjs`
|
|
263
|
+
* 「if (signal?.aborted) throw error」——用户停不当成工具错误),回合尾保存照旧落盘
|
|
264
|
+
* (`agent-turn.mjs`「Save session after every turn (survives crashes)」)⇒ 槽里的在途段尾就是这条没有结果的调用。
|
|
265
|
+
* 两种消息的原文仍在人读线(`read_history` 取得到);丢掉之后机读线尾落回完整交换边界。
|
|
266
|
+
*/
|
|
267
|
+
export function harvestActiveRaw(raw) {
|
|
268
|
+
const list = Array.isArray(raw) ? raw : []
|
|
269
|
+
const callIds = new Set()
|
|
270
|
+
const kept = []
|
|
271
|
+
for (const m of list) {
|
|
272
|
+
if (m?.role === "tool") {
|
|
273
|
+
if (!callIds.has(m.tool_call_id)) continue // ① 前面没有对应调用
|
|
274
|
+
kept.push(m)
|
|
275
|
+
continue
|
|
276
|
+
}
|
|
277
|
+
for (const tc of Array.isArray(m?.tool_calls) ? m.tool_calls : []) callIds.add(tc.id)
|
|
278
|
+
kept.push(m)
|
|
279
|
+
}
|
|
280
|
+
while (kept.length > 0) {
|
|
281
|
+
const last = kept[kept.length - 1] // ② 尾部无工具结果
|
|
282
|
+
if (last?.role !== "assistant" || !(Array.isArray(last.tool_calls) && last.tool_calls.length > 0)) break
|
|
283
|
+
kept.pop()
|
|
284
|
+
}
|
|
285
|
+
return kept
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 恢复机读线(`applySession` ∥ `sessionReading` 同一判据):槽带 `turns` ⇒ 按同一投影重建——
|
|
290
|
+
* 摘要注记(`compactCheckpoint` 在场时)+ 检查点之后那些轮记录的投影 + 在途段(收口后)。
|
|
291
|
+
* 无 `turns`(旧槽)⇒ `null`,由调用方按既有回退(`contextHistory` → `history`)走。
|
|
292
|
+
* `out`(可选出参)拿回 `{ turns, marks }`:归一后的轮记录与逐轮身份账——与装回线上的对象是同一批
|
|
293
|
+
* (`applySession` 播种折叠标记用;不播种,同一会话的第二次压缩会只见到重启之后的轮)。
|
|
294
|
+
*/
|
|
295
|
+
export function restoreMachineFromTurns(data, out = null) {
|
|
296
|
+
const turns = normalizeSlotTurns(data?.turns)
|
|
297
|
+
if (turns.length === 0) return null
|
|
298
|
+
const checkpoint = normalizeCheckpoint(data?.compactCheckpoint, turns.length)
|
|
299
|
+
const covered = checkpoint?.turnsCovered ?? 0
|
|
300
|
+
const line = checkpoint ? [{ role: "user", content: checkpoint.note }] : []
|
|
301
|
+
const marks = []
|
|
302
|
+
for (let i = 0; i < turns.length; i++) {
|
|
303
|
+
if (i < covered) { marks.push([]); continue } // 已覆盖的轮不装线(仍在 `turns` 里供人读回查)
|
|
304
|
+
const msgs = turnRecordMessages(turns[i])
|
|
305
|
+
line.push(...msgs)
|
|
306
|
+
marks.push(msgs)
|
|
307
|
+
}
|
|
308
|
+
line.push(...harvestActiveRaw(data?.activeRaw))
|
|
309
|
+
if (out) { out.turns = turns; out.marks = marks }
|
|
310
|
+
return line.length > 0 ? line : null
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ────────────────────────────── agent 面装配 ──────────────────────────────
|
|
314
|
+
|
|
315
|
+
function logsPathOrNull() {
|
|
316
|
+
try { return todayLogPath() } catch { return null }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 登记完成轮(`runAgent` 的 finally:`finalizeAgentTurn` 成功返回之后才调)。
|
|
321
|
+
* 不登记 ⇒ 没有 `_turnPending` ⇒ 下一轮起点不会折叠该轮(ContinueError 续跑 ∥ 中止 ∥ 异常不折叠)。
|
|
322
|
+
*
|
|
323
|
+
* 「本轮原始段」= `history[_turnStartLen, _turnResponseEnd)`:终点在 `runTurnLoop` 返回处采集——
|
|
324
|
+
* 回合收尾注入的异步结果(子代理报告)落在此后,属于下一轮要看见的上下文,不随折叠丢弃。
|
|
325
|
+
*
|
|
326
|
+
* 工具段两步(`CONTEXT-COMPACTION.md` §1.1 ∥ §1.2)都在这里做一次:
|
|
327
|
+
* ① 切片 + 两道预算(单条字符 ∥ 单轮 token),结果写进记录——投影 ∥ 保存 ∥ 恢复读同一份;
|
|
328
|
+
* ② 本轮的记录把窗口外最旧那一轮的 `toolProcess` 挤出去:删键 + 按身份摘除机读线上那几条消息。
|
|
329
|
+
* 被折掉的量记在**触发它的这一轮**的任务状态上(`folded`,低于 `FOLD_HINT_TOKENS` 不出现)。
|
|
330
|
+
*/
|
|
331
|
+
export function registerCompletedTurn(agent, { finalResponse = null, status = "completed" } = {}) {
|
|
332
|
+
const ledger = agent?._turnLedger
|
|
333
|
+
if (!ledger) return null
|
|
334
|
+
const history = Array.isArray(agent.history) ? agent.history : []
|
|
335
|
+
const end = Math.min(Number.isInteger(agent._turnResponseEnd) ? agent._turnResponseEnd : history.length, history.length)
|
|
336
|
+
const start = Math.min(Number.isInteger(agent._turnStartLen) ? agent._turnStartLen : 0, end)
|
|
337
|
+
const segment = history.slice(start, end)
|
|
338
|
+
const tool = toolProcessOf(segment)
|
|
339
|
+
if (!Array.isArray(agent._turns)) agent._turns = []
|
|
340
|
+
// 窗口外最旧那一轮:推入本记录之后它就是第 3 轮(`TOOL_PROCESS_WINDOW` 之外的第一个)
|
|
341
|
+
const expiring = agent._turns[agent._turns.length - TOOL_PROCESS_WINDOW] ?? null
|
|
342
|
+
const record = buildTurnRecord(ledger, {
|
|
343
|
+
finalResponse,
|
|
344
|
+
status,
|
|
345
|
+
goal: agent.goal ?? null,
|
|
346
|
+
tasks: Array.isArray(agent.tasks) ? agent.tasks : [],
|
|
347
|
+
touchedFiles: Array.isArray(agent._touchedFiles) ? agent._touchedFiles : [],
|
|
348
|
+
// 设计/需求文档根(调用方注入:`facts.docRoots` 是唯一入口,本档不自读 manifest)——
|
|
349
|
+
// 未注入 ⇒ `changedDocs` 不出现(`changedDocsOf` 的 null 支)。
|
|
350
|
+
docRoots: Array.isArray(agent._docRoots) ? agent._docRoots : [],
|
|
351
|
+
cwd: agent.cwd ?? null,
|
|
352
|
+
// 槽文件(可回查位置):绑定记录存储时由存储持槽路径;未绑定(模式 F / 测试)不带。
|
|
353
|
+
slotFile: agent._recordStore?.slotFile ?? null,
|
|
354
|
+
batchDoc: agent._batchDoc ?? null,
|
|
355
|
+
logsPath: logsPathOrNull(),
|
|
356
|
+
toolProcess: tool.messages ?? [],
|
|
357
|
+
folded: foldHintOf(tool.lost, toolProcessLostOf(expiring)),
|
|
358
|
+
})
|
|
359
|
+
const msgs = new Set(segment)
|
|
360
|
+
agent._turns.push(record)
|
|
361
|
+
agent._turnPending = msgs.size > 0 ? { record, msgs } : null
|
|
362
|
+
agent._turnLedger = null
|
|
363
|
+
agent._turnStartLen = null
|
|
364
|
+
agent._turnResponseEnd = null
|
|
365
|
+
if (expiring && Array.isArray(expiring.toolProcess)) {
|
|
366
|
+
dropToolProcessFromLine(agent, expiring)
|
|
367
|
+
delete expiring.toolProcess
|
|
368
|
+
}
|
|
369
|
+
return record
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** 下一轮起点应用投影(`beginRun` 安全接缝):有一次未折叠的完成轮 ⇒ 就地折叠并消费它。
|
|
373
|
+
* 待折段在机读线上定位不到(被压缩摘要吞掉 ∥ 对象已换)⇒ 不折叠、清掉待折项,并留一行痕:
|
|
374
|
+
* 静默返回与「已经折过」在日志里同形,多留在跨轮线上的那一轮工具过程事后没人查得出来。 */
|
|
375
|
+
export function applyTurnProjection(agent) {
|
|
376
|
+
const pending = agent?._turnPending
|
|
377
|
+
if (!pending) return false
|
|
378
|
+
agent._turnPending = null
|
|
379
|
+
const plan = turnFoldPlan(agent.history, pending)
|
|
380
|
+
if (!plan) {
|
|
381
|
+
logEvent("ev:turn-fold-noop", { reason: "segment-not-on-line", msgs: pending.msgs?.size ?? 0 })
|
|
382
|
+
return false
|
|
383
|
+
}
|
|
384
|
+
applyTurnFold(agent.history, plan, agent)
|
|
385
|
+
return true
|
|
386
|
+
}
|