@thincoder/core 0.11.2 → 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 +18 -0
- package/advisor/compaction.mjs +7 -5
- package/advisor/history.mjs +24 -11
- package/advisor/loop.mjs +13 -8
- package/advisor/messages.mjs +72 -8
- package/advisor/run.mjs +24 -13
- package/advisor.mjs +7 -3
- package/agent/chat-call.mjs +14 -9
- package/agent/completion.mjs +28 -40
- package/agent/continue-decision.mjs +14 -8
- package/agent/digest-account.mjs +133 -59
- package/agent/dispatch-run.mjs +1 -1
- package/agent/dispatch.mjs +2 -1
- package/agent/family-tools.mjs +12 -13
- package/agent/helpers.mjs +17 -72
- package/agent/image-heal.mjs +18 -4
- package/agent/post-turn.mjs +13 -26
- package/agent/record-results.mjs +21 -6
- package/agent/run-stages.mjs +72 -37
- package/agent/run-start.mjs +148 -113
- package/agent/setup-reminders.mjs +187 -91
- package/agent/setup.mjs +204 -138
- package/agent/spawn-child.mjs +2 -2
- package/agent/suspension.mjs +43 -15
- package/agent/timers.mjs +4 -1
- package/agent/turn-domain-text.mjs +105 -0
- package/agent/turn-loop.mjs +54 -22
- package/agent/turn-projection.mjs +127 -227
- package/agent/turn-tool-process.mjs +32 -37
- package/agent-tools/advisor-async.mjs +6 -3
- package/agent-tools/advisor-launch.mjs +5 -5
- package/agent-tools/advisor.mjs +1 -1
- package/agent-tools/async-discard.mjs +7 -2
- package/agent-tools/async-settle.mjs +18 -12
- package/agent-tools/bash-async.mjs +5 -2
- package/agent-tools/batch-closeout.mjs +12 -62
- package/agent-tools/batch-ledger-bind.mjs +19 -8
- package/agent-tools/batch-lifecycle.mjs +37 -11
- package/agent-tools/batch-read.mjs +151 -4
- package/agent-tools/batch-record-write.mjs +5 -5
- package/agent-tools/batch-segment-vocab.mjs +84 -4
- package/agent-tools/batch-skeleton.mjs +18 -14
- package/agent-tools/batch-supersede.mjs +1 -1
- package/agent-tools/batch-tier-gate.mjs +36 -119
- package/agent-tools/batch.mjs +30 -20
- package/agent-tools/browser-async.mjs +5 -2
- package/agent-tools/consult.mjs +10 -7
- package/agent-tools/context.mjs +47 -31
- package/agent-tools/digest-budget.mjs +10 -9
- package/agent-tools/eng.mjs +3 -4
- package/agent-tools/escalate-async.mjs +3 -3
- package/agent-tools/goal.mjs +5 -2
- package/agent-tools/judge-domain.mjs +347 -0
- package/agent-tools/manifest.mjs +2 -2
- package/agent-tools/parent-channel.mjs +6 -3
- package/agent-tools/plan.mjs +11 -10
- package/agent-tools/review-facts.mjs +9 -3
- package/agent-tools/skill.mjs +7 -8
- package/agent-tools/spawn-gates.mjs +5 -16
- package/agent-tools/ssh-session.mjs +6 -3
- package/agent-tools/ssh-transfer.mjs +1 -1
- package/agent-tools/subagent-actions.mjs +2 -2
- package/agent-tools/subagent-async.mjs +11 -8
- package/agent-tools/subagent-scheduler.mjs +9 -9
- package/agent-tools/subagent-spawn.mjs +2 -2
- package/agent-tools/subagent.mjs +5 -18
- package/agent-tools/task.mjs +0 -8
- package/agent.mjs +15 -15
- package/auto-think.mjs +5 -2
- package/compaction-work-state.mjs +230 -0
- package/compress-form.mjs +41 -6
- package/config-defaults.mjs +5 -2
- package/config.mjs +53 -17
- package/context-degrade.mjs +79 -48
- package/context-echo.mjs +6 -26
- package/context-push.mjs +328 -31
- package/context.mjs +203 -141
- package/conventions.mjs +1 -1
- package/declaration.mjs +95 -17
- package/escape.mjs +6 -5
- package/explore-distill.mjs +35 -8
- package/generate-title.mjs +10 -1
- package/knowledge/prompt-resources.md +5 -1
- package/ledger-tools.mjs +50 -50
- package/ledger-write-ops.mjs +43 -16
- package/memory/docs.mjs +51 -15
- package/memory/file-list.mjs +6 -0
- package/model-specs.mjs +87 -21
- package/origin-guard.mjs +190 -0
- package/package.json +1 -1
- package/prompt-files.mjs +33 -11
- package/prompt-overlays.mjs +12 -5
- package/prompts/advisor-design.md +55 -36
- package/prompts/advisor-round1.md +18 -7
- package/prompts/advisor-round2.md +18 -7
- package/prompts/advisor-round3.md +18 -7
- package/prompts/common.md +89 -51
- package/prompts/consult-base.md +2 -3
- package/prompts/discipline-engineering.md +474 -440
- package/prompts/discipline-normal.md +107 -31
- package/prompts/knowledge/business-gates.md +23 -15
- package/prompts/knowledge/decision-quality.md +16 -18
- package/prompts/knowledge/doc-lookup.md +22 -27
- package/prompts/knowledge/document-writing.md +31 -49
- package/prompts/knowledge/documentation-evaluation.md +37 -45
- package/prompts/knowledge/evidence-discipline.md +36 -55
- package/prompts/knowledge/normal-coding-workflow.md +27 -21
- package/prompts/knowledge/normal-delivery-collaboration.md +33 -17
- package/prompts/knowledge/normal-work-management.md +10 -7
- package/prompts/knowledge/product-security.md +22 -17
- package/prompts/knowledge/project-records.md +63 -52
- package/prompts/knowledge/tool-apply-patch.md +12 -13
- package/prompts/knowledge/tool-bash.md +13 -4
- package/prompts/knowledge/tool-batch-eng-coder.md +13 -6
- package/prompts/knowledge/tool-batch-eng-designer.md +14 -6
- package/prompts/knowledge/tool-batch-review.md +12 -6
- package/prompts/knowledge/tool-batch.md +76 -62
- package/prompts/knowledge/tool-browser.md +45 -34
- package/prompts/knowledge/tool-context.md +2 -2
- package/prompts/knowledge/tool-delete.md +5 -1
- package/prompts/knowledge/tool-doc-check.md +25 -24
- package/prompts/knowledge/tool-doc-graph-read.md +3 -3
- package/prompts/knowledge/tool-doc-graph.md +15 -15
- package/prompts/knowledge/tool-edit.md +26 -23
- package/prompts/knowledge/tool-execute.md +9 -4
- package/prompts/knowledge/tool-fetch.md +7 -1
- package/prompts/knowledge/tool-file-ops.md +6 -1
- package/prompts/knowledge/tool-git-read.md +3 -1
- package/prompts/knowledge/tool-git.md +1 -1
- package/prompts/knowledge/tool-glob.md +3 -1
- package/prompts/knowledge/tool-grep.md +5 -2
- package/prompts/knowledge/tool-hashline-edit.md +4 -2
- package/prompts/knowledge/tool-insert-after.md +3 -1
- package/prompts/knowledge/tool-ledger-read.md +6 -13
- package/prompts/knowledge/tool-ledger.md +14 -10
- package/prompts/knowledge/tool-ls.md +2 -0
- package/prompts/knowledge/tool-manifest.md +10 -7
- package/prompts/knowledge/tool-memory.md +15 -11
- package/prompts/knowledge/tool-operations.md +59 -71
- package/prompts/knowledge/tool-plan.md +1 -2
- package/prompts/knowledge/tool-process.md +1 -1
- package/prompts/knowledge/tool-read-history.md +6 -7
- package/prompts/knowledge/tool-read.md +1 -0
- package/prompts/knowledge/tool-settings.md +6 -7
- package/prompts/knowledge/tool-ssh.md +37 -15
- package/prompts/knowledge/tool-subagent.md +37 -39
- package/prompts/knowledge/tool-task.md +3 -1
- package/prompts/knowledge/tool-team-ledger.md +5 -1
- package/prompts/knowledge/tool-tree.md +2 -0
- package/prompts/knowledge/tool-wait-for.md +16 -16
- package/prompts/knowledge/tool-write.md +4 -1
- package/prompts/knowledge/upstream-protocol.md +39 -37
- package/prompts/persona-coder.md +1 -3
- package/prompts/persona-eng-coder.md +5 -5
- package/prompts/persona-eng-designer.md +34 -17
- package/prompts/persona-engineering.md +532 -501
- package/prompts/persona-explore.md +0 -2
- package/prompts/persona-normal.md +17 -9
- package/prompts/persona-plan.md +0 -2
- package/prompts/persona-tester.md +0 -2
- package/prompts/subagent-base.md +0 -2
- package/provider/anthropic.mjs +4 -2
- package/provider/core.mjs +5 -3
- package/provider/errors.mjs +41 -7
- package/provider/google.mjs +5 -3
- package/provider/reasoning-echo.mjs +16 -0
- package/provider/responses.mjs +4 -2
- package/provider/sse.mjs +11 -6
- package/rules.mjs +11 -10
- package/session-lifecycle.mjs +115 -81
- package/session-slot-write.mjs +1 -1
- package/session.mjs +23 -20
- package/shell-identity.mjs +129 -22
- package/skip-dirs.mjs +24 -0
- package/team-ledger.mjs +22 -22
- package/text-budget.mjs +1 -1
- package/think-off.mjs +27 -0
- package/token-ttl.mjs +1 -1
- package/token-window.mjs +278 -88
- package/tool-docs/bash.md +0 -2
- package/tool-docs/consult_start.md +2 -2
- package/tool-docs/consult_stop.md +2 -2
- package/tools/browser.mjs +8 -8
- package/tools/execute.mjs +2 -2
- package/tools/file.mjs +17 -11
- package/tools/git.mjs +12 -12
- package/tools/ops.mjs +2 -2
- package/tools/patch.mjs +1 -1
- package/tools/process-doc-mark.mjs +33 -4
- package/tools/repomap.mjs +25 -3
- package/tools/search.mjs +74 -26
- package/tools/shared.mjs +4 -3
- 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 +3 -3
- package/window-policy.mjs +118 -78
|
@@ -1,58 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* agent/turn-projection.mjs —
|
|
3
|
-
* `docs/core/design/SESSION.md` §2;批次 = `docs/batches/2026-11-
|
|
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-17-machine-line-snapshot.md`;台账 #2516)。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* 第
|
|
9
|
-
* 在它上面只留前段,`read_history` 对这些消息也只回前 500 字符(射程与标记见 `SESSION.md` §5)——需要原文就
|
|
10
|
-
* 重跑工具,两者都不许诺「原文仍在」。
|
|
5
|
+
* 机读线不按轮记录重拼:线上本来就是一整轮的原消息。轮边界只有一个动作——删更早轮次的
|
|
6
|
+
* `assistant(tool_calls)` 与它按 `tool_call_id` 配对的 `tool` 结果;该轮其余消息原对象、原顺序、原文本。
|
|
7
|
+
* 最近 `agent.keepToolTurns` 轮(保留窗;0 ∥ 缺省 = 全部轮次不删)的工具调用与结果原样留在线上。轮的位置由结构给出:
|
|
8
|
+
* 第 k 轮 = 外层第 k 个轮元素,在途轮 = 外层最后一个轮元素;消息上不带 `turn`,也不另立下标账。
|
|
11
9
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
10
|
+
* 本档三面:
|
|
11
|
+
* - 轮记录成形 `buildTurnRecord` ∥ `turnRecordMessages` ∥ `projectTurns`(`{ userRequest, finalResponse,
|
|
12
|
+
* taskState, toolProcess? }`——记录供人读回查与端侧 `_turns` 恢复,不展开进机读线);
|
|
13
|
+
* - 计数面 `coveredTurnCount`(压缩当刻算已覆盖条数;判据按外层轮元素读——被切走的轮元素里内层非空的计数);
|
|
14
|
+
* - 装配接缝 `registerCompletedTurn`:轮完成登记 + 离开保留窗那一轮的工具过程下线。
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
* `turn-
|
|
18
|
-
* - 折叠 `turnFoldPlan` ∥ `applyTurnFold` ∥ `registerFoldMarks`:按消息身份定位本轮原始段,替换为投影消息
|
|
19
|
-
* ——`beginRun`(下一轮起点)就地折叠并登记折叠身份(压缩按那张账判覆盖条数);
|
|
20
|
-
* - 投影 `buildTurnRecord` ∥ `turnRecordMessages` ∥ `projectTurns`:一轮 → 机读消息的成形(三面共用);
|
|
21
|
-
* - 恢复 `restoreMachineFromTurns` ∥ `machineLineForSlot`:槽带 `turns` 时按同一段拼法重建机读线——
|
|
22
|
-
* 前者是读向(先做配对收口),后者是 `saveSession` 的写向(不改内存线的数组与对象)。
|
|
23
|
-
* 装配接缝 `registerCompletedTurn` ∥ `applyTurnProjection`:轮边界上取用并清空账本、把待折段就地折掉。
|
|
16
|
+
* 保留窗、两道预算与切片那一族(五个常量 ∥ 八个函数)实现在 `turn-tool-process.mjs`——本档按名取用并在
|
|
17
|
+
* 原路径 re-export;轮事实账本与任务状态组装单列在 `turn-task-state.mjs`,本档按名取用并 re-export。
|
|
24
18
|
*
|
|
25
|
-
*
|
|
26
|
-
* `
|
|
27
|
-
*
|
|
19
|
+
* 机读线在槽里只占一个键(`SESSION.md` §2):`contextHistory` = 保存当刻那条线本身,`saveSession` 与
|
|
20
|
+
* `applySession` 都不经本档。保留窗起点不靠运行期对象账:`retentionWindowStart` 与跨轮清理都按外层
|
|
21
|
+
* 轮元素的位置读,装回后仍是同一条嵌套线 ⇒ 跨进程不变。
|
|
28
22
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* 搬走的符号同样在本档原路径 re-export,既有 import 一字不改。
|
|
32
|
-
*
|
|
33
|
-
* 机读线在槽里分三段存(`SESSION.md` §2):`compactCheckpoint`(摘要注记 + 已覆盖的轮记录条数)∥ `turns` ∥
|
|
34
|
-
* `activeRaw`(没有轮记录可依的在途 ∥ 异常段)。保存与恢复两个方向是同一段拼法(`machineLineForSlot` ∥
|
|
35
|
-
* `restoreMachineFromTurns`);覆盖条数按折叠身份账算(`coveredTurnCount`),折叠与压缩都按它判「这一轮是不是已在中段」。
|
|
36
|
-
*
|
|
37
|
-
* 纪律:除轮边界上的账本交接(`registerCompletedTurn` 取用调用方给的账本对象、随后清空账本槽)外,
|
|
38
|
-
* 本档纯函数面零 IO、零抛、零全局态;不改写传入的消息对象(`pushReal` 的双线共享对象只读不写——折叠产出新对象)。
|
|
23
|
+
* 纪律:本档纯函数面零 IO、零抛、零全局态;不改写传入的消息对象(`pushReal` 的双线共享对象只读不写——
|
|
24
|
+
* 清理只删数组项或换一个新对象)。
|
|
39
25
|
*/
|
|
40
26
|
|
|
41
27
|
// 账本 ∥ 事实 ∥ 状态串 ∥ 成败取形住在 `turn-task-state.mjs`(拆档理由见该档头注);搬走的符号在下方原路径
|
|
42
|
-
// re-export
|
|
28
|
+
// re-export——拆档不动调用方。本档从它取 `str` ∥ `capText` 与两条状态面函数。
|
|
43
29
|
import { buildTurnTaskState, capText, str, taskStateText } from "./turn-task-state.mjs"
|
|
44
30
|
export { TASK_STATE_MARK, buildTurnTaskState, createTurnLedger, noteTurnRunError, noteTurnToolFacts, taskStateText } from "./turn-task-state.mjs"
|
|
45
31
|
// 可回查位置(`evidence` 面):会话事件日志按天分档——路径取自核日志单源,本档不自造路径。
|
|
46
|
-
//
|
|
47
|
-
import {
|
|
48
|
-
//
|
|
32
|
+
// 清理 no-op 的留痕随「不删、不留痕」退场;todayLogPath 仍在记录面用
|
|
33
|
+
import { todayLogPath } from "../log.mjs"
|
|
34
|
+
// 拍平投影与位置换算(写面单点档):本轮记录面只读投影、不写机读线。
|
|
35
|
+
import { flattenHistory, flatIndexOf } from "../context-push.mjs"
|
|
36
|
+
// 清理改变线上结构(删 / 换对象),实测基线按 §3.1 重定——与压缩、蒸馏、prune、shrink 同一把尺。
|
|
37
|
+
import { estimateTokens, rebaseMeasured } from "../token-window.mjs"
|
|
38
|
+
// 工具过程保留族(四个常量 ∥ 九个函数)住在 `turn-tool-process.mjs`——拆档理由见该档头注。
|
|
49
39
|
// 搬走的符号在本档原路径 re-export:拆档不动调用方,三个导入方(`context.mjs` ∥ `context-degrade.mjs` ∥
|
|
50
40
|
// `agent-tools/context.mjs`)一字不改。装配接缝仍在本档,故本档也按名取用它其中几个。
|
|
51
|
-
import {
|
|
41
|
+
import { foldHintOf, isCallGroupStart, toolProcessLostOf, toolProcessOf, toolProcessWindow } from "./turn-tool-process.mjs"
|
|
52
42
|
export {
|
|
53
43
|
FOLD_HINT_TOKENS, TOOL_PROCESS_DROP_MARK, TOOL_PROCESS_MSG_CHARS, TOOL_PROCESS_TURN_TOKENS,
|
|
54
|
-
|
|
55
|
-
retentionWindowStart, toolProcessLostOf, toolProcessOf, toolSliceOf, trimToolProcess,
|
|
44
|
+
clampToolMessage, foldHintOf, isCallGroupStart,
|
|
45
|
+
retentionWindowStart, toolProcessLostOf, toolProcessOf, toolProcessWindow, toolSliceOf, trimToolProcess,
|
|
56
46
|
} from "./turn-tool-process.mjs"
|
|
57
47
|
|
|
58
48
|
// ────────────────────────────── 轮记录与投影 ──────────────────────────────
|
|
@@ -70,9 +60,9 @@ export function buildTurnRecord(ledger, facts = {}) {
|
|
|
70
60
|
return record
|
|
71
61
|
}
|
|
72
62
|
|
|
73
|
-
/** 一条轮记录的机读消息(用户请求 ∥ 工具段切片 ∥ 最终回复 ∥
|
|
74
|
-
*
|
|
75
|
-
*
|
|
63
|
+
/** 一条轮记录的机读消息(用户请求 ∥ 工具段切片 ∥ 最终回复 ∥ 任务状态——顺序即记录面顺序)。
|
|
64
|
+
* 工具段按消息逐条展开:切片里有几条发几条。它只用于记录面,**不展开进机读线**——
|
|
65
|
+
* 线上本来就是那一轮的原消息(§1.1);读向只读不写。 */
|
|
76
66
|
export function turnRecordMessages(record) {
|
|
77
67
|
const out = []
|
|
78
68
|
if (str(record?.userRequest)) out.push({ role: "user", content: record.userRequest })
|
|
@@ -86,7 +76,8 @@ export function turnRecordMessages(record) {
|
|
|
86
76
|
return out
|
|
87
77
|
}
|
|
88
78
|
|
|
89
|
-
/** 多条轮记录 →
|
|
79
|
+
/** 多条轮记录 → 机读消息数组(`turnRecordMessages` 的批入口)。当前仓库内零调用方:恢复面不再按
|
|
80
|
+
* `turns` 投影机读线(`docs/core/design/SESSION.md` §2),本符号只作对外导出面保留。 */
|
|
90
81
|
export function projectTurns(turns) {
|
|
91
82
|
const out = []
|
|
92
83
|
for (const record of Array.isArray(turns) ? turns : []) out.push(...turnRecordMessages(record))
|
|
@@ -94,111 +85,26 @@ export function projectTurns(turns) {
|
|
|
94
85
|
}
|
|
95
86
|
|
|
96
87
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
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
|
-
* - 该轮在机读线上一条登记消息都找不到 ⇒ 停(无法证明已覆盖,宁可多装)。
|
|
88
|
+
* 覆盖条数(压缩当刻算;`cut` = 切割位置 `{ turn, msg }`——它之前那一批会被摘要吞掉)。
|
|
89
|
+
* 判据按结构读:起点 = 旧检查点里的条数(不重算、不缩回);再加上本次切走的轮元素里**内层非空**的个数
|
|
90
|
+
* (`cut.turn` 之前的轮元素整条移出外层;被切开那一轮不记——宁可多留一轮,不把半轮当整轮)。
|
|
91
|
+
* 不写固定条数:一轮在线上有几条消息由那一轮自己决定。
|
|
143
92
|
*/
|
|
144
93
|
export function coveredTurnCount(agent, cut) {
|
|
145
94
|
const turns = Array.isArray(agent?._turns) ? agent._turns : []
|
|
146
|
-
const marks = Array.isArray(agent?._turnFoldMarks) ? agent._turnFoldMarks : []
|
|
147
95
|
const history = Array.isArray(agent?.history) ? agent.history : []
|
|
148
96
|
let covered = normalizeCheckpoint(agent?._compactCheckpoint, turns.length)?.turnsCovered ?? 0
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
|
97
|
+
const end = Number.isInteger(cut?.turn) ? Math.min(cut.turn, history.length) : history.length
|
|
98
|
+
for (let i = covered; i < end; i++) {
|
|
99
|
+
if ((history[i]?.messages?.length ?? 0) > 0) covered += 1
|
|
194
100
|
}
|
|
195
|
-
return
|
|
101
|
+
return Math.min(covered, turns.length)
|
|
196
102
|
}
|
|
197
103
|
|
|
198
|
-
//
|
|
104
|
+
// ────────── 槽键归一(写出与恢复共用) ──────────
|
|
199
105
|
|
|
200
106
|
/** 槽里 `toolProcess` 的归一(白名单保留该键——§2:存下去的形状读不回来,等于没存)。盘上是用户数据,
|
|
201
|
-
* 本函数不抛:只认消息面字段,坏项丢弃;全丢光 ⇒ `null`(=
|
|
107
|
+
* 本函数不抛:只认消息面字段,坏项丢弃;全丢光 ⇒ `null`(= 键缺,与「已离开保留窗」同义)。 */
|
|
202
108
|
function normalizeToolProcess(value) {
|
|
203
109
|
if (!Array.isArray(value)) return null
|
|
204
110
|
const out = []
|
|
@@ -208,6 +114,7 @@ function normalizeToolProcess(value) {
|
|
|
208
114
|
const copy = { role: m.role }
|
|
209
115
|
if (typeof m.content === "string") copy.content = m.content
|
|
210
116
|
else if (Array.isArray(m.content)) copy.content = m.content
|
|
117
|
+
// 空串是合法的「没有原文」编码(#2647):是字符串就搬运,含空串。
|
|
211
118
|
if (typeof m.reasoning_content === "string") copy.reasoning_content = m.reasoning_content
|
|
212
119
|
if (typeof m.name === "string") copy.name = m.name
|
|
213
120
|
if (typeof m.tool_call_id === "string") copy.tool_call_id = m.tool_call_id
|
|
@@ -243,72 +150,20 @@ export function slotTurns(turns) {
|
|
|
243
150
|
}
|
|
244
151
|
|
|
245
152
|
/**
|
|
246
|
-
* 压缩检查点归一(`{
|
|
153
|
+
* 压缩检查点归一(`{ compactionSummary, turnsCovered }`——槽是盘上用户数据,本函数不抛):两字段齐且合法 ⇒ 新对象;
|
|
247
154
|
* 否则 `null`(= 「本会话还没压过」,与键缺同义)。`turnsCovered` 夹在当前轮记录条数内——
|
|
248
|
-
*
|
|
155
|
+
* 盘上被改坏的大数不得让覆盖条数越过真实的那一段(越界 = 把没压过的轮当成已压)。
|
|
156
|
+
* 旧键 `note` 装回时也认一次(写侧只写新键):不认它,已压缩的旧槽重开时会读成 `null`,压缩摘要静默丢掉。
|
|
157
|
+
* 同一张槽键表里 `pendingReminders` 已有同款先例。
|
|
249
158
|
*/
|
|
250
159
|
export function normalizeCheckpoint(value, turnsLen = Number.MAX_SAFE_INTEGER) {
|
|
251
160
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null
|
|
252
|
-
const
|
|
253
|
-
if (!
|
|
161
|
+
const compactionSummary = str(value.compactionSummary ?? value.note)
|
|
162
|
+
if (!compactionSummary) return null
|
|
254
163
|
if (!Number.isInteger(value.turnsCovered) || value.turnsCovered < 0) return null
|
|
255
|
-
return {
|
|
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
|
|
164
|
+
return { compactionSummary, turnsCovered: Math.min(value.turnsCovered, Math.max(0, turnsLen)) }
|
|
286
165
|
}
|
|
287
166
|
|
|
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
167
|
|
|
313
168
|
// ────────────────────────────── agent 面装配 ──────────────────────────────
|
|
314
169
|
|
|
@@ -318,27 +173,33 @@ function logsPathOrNull() {
|
|
|
318
173
|
|
|
319
174
|
/**
|
|
320
175
|
* 登记完成轮(`runAgent` 的 finally:`finalizeAgentTurn` 成功返回之后才调)。
|
|
321
|
-
* 不登记 ⇒
|
|
176
|
+
* 不登记 ⇒ 下一轮起点不碰该轮(ContinueError 续跑 ∥ 中止 ∥ 异常不登记 ∥ 不清理)。
|
|
322
177
|
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
178
|
+
* 本轮原始段 = 外层最后一个轮元素的内层(§1.1:在途轮就是它,不用下标账):回合收尾注入的异步结果
|
|
179
|
+
* 只进待发队、不上线,故它们不会混进本轮的记录面。
|
|
325
180
|
*
|
|
326
181
|
* 工具段两步(`CONTEXT-COMPACTION.md` §1.1 ∥ §1.2)都在这里做一次:
|
|
327
|
-
* ① 切片 + 两道预算(单条字符 ∥ 单轮 token
|
|
328
|
-
* ②
|
|
329
|
-
*
|
|
182
|
+
* ① 切片 + 两道预算(单条字符 ∥ 单轮 token),结果写进记录——只落在记录副本上,不改线上原文;
|
|
183
|
+
* ② 本轮的记录把窗口外最旧那一轮挤出保留窗:删它记录上的 `toolProcess` 键(只影响记录面),
|
|
184
|
+
* 并把它自己内层里的调用组删掉(`retireTurnToolProcess`)。
|
|
185
|
+
* 被删掉的量记在**触发它的这一轮**的任务状态上(`folded`,低于 `FOLD_HINT_TOKENS` 不出现)。
|
|
330
186
|
*/
|
|
331
187
|
export function registerCompletedTurn(agent, { finalResponse = null, status = "completed" } = {}) {
|
|
332
188
|
const ledger = agent?._turnLedger
|
|
333
189
|
if (!ledger) return null
|
|
334
190
|
const history = Array.isArray(agent.history) ? agent.history : []
|
|
335
|
-
const
|
|
336
|
-
const start = Math.min(Number.isInteger(agent._turnStartLen) ? agent._turnStartLen : 0, end)
|
|
337
|
-
const segment = history.slice(start, end)
|
|
191
|
+
const segment = Array.isArray(history.at(-1)?.messages) ? history.at(-1).messages : []
|
|
338
192
|
const tool = toolProcessOf(segment)
|
|
339
193
|
if (!Array.isArray(agent._turns)) agent._turns = []
|
|
340
|
-
//
|
|
341
|
-
|
|
194
|
+
// 保留窗外的目标轮(§1「轮记录与嵌套线并存」):两个数组各自按下标取,同一条规则不用同一个下标。
|
|
195
|
+
// 轮记录侧 = 推入本记录之后它就落在窗宽之外(`_turns.length − N`)。
|
|
196
|
+
// 机读线侧按自己的长度取 = `history.length − 1 − N`:压缩把被摘要覆盖的轮元素整条移出外层,
|
|
197
|
+
// `history` 此后比 `_turns` 短 k 项,沿用轮记录侧的下标会落到偏晚 k 轮的位置(k ≤ N 时正落在保留窗内)。
|
|
198
|
+
// 越界即 `null`:窗宽 0(全部保留)时两处都越界,一道闸给出「一条不删」——`expiring` 为 null 时
|
|
199
|
+
// 下面的清理整段不执行,`expiringTurn` 那一刻指向在途轮,绝不能单独授人以柄。
|
|
200
|
+
const keep = toolProcessWindow(agent)
|
|
201
|
+
const expiring = agent._turns[agent._turns.length - keep] ?? null
|
|
202
|
+
const expiringTurn = history[history.length - 1 - keep] ?? null
|
|
342
203
|
const record = buildTurnRecord(ledger, {
|
|
343
204
|
finalResponse,
|
|
344
205
|
status,
|
|
@@ -356,31 +217,70 @@ export function registerCompletedTurn(agent, { finalResponse = null, status = "c
|
|
|
356
217
|
toolProcess: tool.messages ?? [],
|
|
357
218
|
folded: foldHintOf(tool.lost, toolProcessLostOf(expiring)),
|
|
358
219
|
})
|
|
359
|
-
const msgs = new Set(segment)
|
|
360
220
|
agent._turns.push(record)
|
|
361
|
-
agent._turnPending = msgs.size > 0 ? { record, msgs } : null
|
|
362
221
|
agent._turnLedger = null
|
|
363
|
-
|
|
364
|
-
agent._turnResponseEnd = null
|
|
365
|
-
if (expiring && Array.isArray(expiring.toolProcess)) {
|
|
366
|
-
dropToolProcessFromLine(agent, expiring)
|
|
222
|
+
if (expiring) {
|
|
367
223
|
delete expiring.toolProcess
|
|
224
|
+
if (expiringTurn) retireTurnToolProcess(agent, expiringTurn)
|
|
368
225
|
}
|
|
369
226
|
return record
|
|
370
227
|
}
|
|
371
228
|
|
|
372
|
-
/**
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
229
|
+
/**
|
|
230
|
+
* 离开保留窗那一轮的工具过程从它自己的内层删掉(`CONTEXT-COMPACTION.md` §1.1):
|
|
231
|
+
* `assistant(tool_calls)` 与按 `tool_call_id` 配对的 `tool` 结果删掉,该轮其余消息原对象、原顺序、原文本。
|
|
232
|
+
* 混合消息(文本 + `tool_calls`)就地换成 `{ role, content }`,文本逐字不变(content 是多模态 part 数组时原样保留),
|
|
233
|
+
* 思考模式族的 `reasoning_content` 随文本原样留下——它是 provider 要求回传的回声字段,丢掉会让这条
|
|
234
|
+
* 「无回声的 assistant 紧邻另一条 assistant」的形在 DeepSeek 族思考模式被拒(`context-echo.mjs` D-CC18)。
|
|
235
|
+
* 来源标记 `originProvider` 与 `reasoning_content` 同处保留(`docs/core/design/AGENT-LOOP.md` §5.5.4 存行、§5.5.5):换新对象时丢标记,
|
|
236
|
+
* 这条消息就绕过了来源判据,而保留窗外的旧轮恰是最该被隔离的那一段。
|
|
237
|
+
* 线上唯一一处对象被替换的地方。
|
|
238
|
+
* 半可定位:调用已不在内层而结果还在 ⇒ 结果照删;只有调用而结果不在 ⇒ 调用照删。
|
|
239
|
+
* 内层没有调用组 ⇒ 不删、不改线、不留痕:没有可删的项就是没有可删的项(§1.1 ∥ 验收 9)。
|
|
240
|
+
* 真删了东西 ⇒ 收了尾按身份重定 `_usageAt` / `_lastPromptTokens`(§3.1:结构编辑之后不重定,
|
|
241
|
+
* 实测支就会拿旧位置切新线)。
|
|
242
|
+
*/
|
|
243
|
+
function retireTurnToolProcess(agent, turn) {
|
|
244
|
+
const messages = Array.isArray(turn?.messages) ? turn.messages : []
|
|
245
|
+
if (messages.length === 0) return
|
|
246
|
+
const callIds = new Set()
|
|
247
|
+
for (const m of messages) {
|
|
248
|
+
if (!isCallGroupStart(m)) continue
|
|
249
|
+
for (const tc of m.tool_calls) if (typeof tc?.id === "string") callIds.add(tc.id)
|
|
250
|
+
}
|
|
251
|
+
if (callIds.size === 0) return
|
|
252
|
+
const before = flattenHistory(agent.history)
|
|
253
|
+
const measuredEnd = agent?._usageAt ? flatIndexOf(agent.history, agent._usageAt) : before.length
|
|
254
|
+
const removed = []
|
|
255
|
+
const replacedFrom = []
|
|
256
|
+
const replacedTo = []
|
|
257
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
258
|
+
const m = messages[i]
|
|
259
|
+
if (!m || typeof m !== "object") continue
|
|
260
|
+
if (m.role === "tool") {
|
|
261
|
+
if (typeof m.tool_call_id === "string" && callIds.has(m.tool_call_id)) {
|
|
262
|
+
removed.push(m)
|
|
263
|
+
messages.splice(i, 1)
|
|
264
|
+
}
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
if (!isCallGroupStart(m)) continue
|
|
268
|
+
// 文本逐字留下(string 原样;多模态 part 数组原样):调用块是清理对象,回复不是
|
|
269
|
+
const text = typeof m.content === "string" ? m.content : ""
|
|
270
|
+
const parts = !text && Array.isArray(m.content) && m.content.length > 0 ? m.content : null
|
|
271
|
+
if (text.length > 0 || parts) {
|
|
272
|
+
const next = { role: "assistant", content: parts ?? text }
|
|
273
|
+
if (typeof m.reasoning_content === "string") next.reasoning_content = m.reasoning_content
|
|
274
|
+
// 来源标记与 `reasoning_content` 同处保留(AGENT-LOOP.md §5.5.4 存行、§5.5.5):留下回声却丢标记,这条消息就绕过了来源判据,
|
|
275
|
+
// 而保留窗外的旧轮恰是最该被隔离的那一段。
|
|
276
|
+
if (typeof m.originProvider === "string" && m.originProvider) next.originProvider = m.originProvider
|
|
277
|
+
messages[i] = next
|
|
278
|
+
replacedFrom.push(m)
|
|
279
|
+
replacedTo.push(next)
|
|
280
|
+
} else {
|
|
281
|
+
removed.push(m)
|
|
282
|
+
messages.splice(i, 1)
|
|
283
|
+
}
|
|
383
284
|
}
|
|
384
|
-
|
|
385
|
-
return true
|
|
285
|
+
rebaseMeasured(agent, estimateTokens([...removed, ...replacedFrom]), estimateTokens(replacedTo), { before, measuredEnd })
|
|
386
286
|
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* agent/turn-tool-process.mjs — 工具过程保留族(设计 = `docs/core/design/CONTEXT-COMPACTION.md` §1.1 ∥ §1.2;
|
|
3
|
-
* 批次 = `docs/batches/2026-11-
|
|
4
|
-
*
|
|
3
|
+
* 批次 = `docs/batches/2026-11-17-machine-line-snapshot.md`)。四个常量 ∥ 九个函数:保留窗宽与两道
|
|
4
|
+
* 预算、切片与整段移出、折叠提示的记账、保留窗起点。
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
* 档位线(标准 `docs/core/standards/CODE-TIER.md` §2.1
|
|
8
|
-
* 注释语义逐字照搬。先例 = `turn-task-state.mjs` 拆出时同形。
|
|
6
|
+
* 为什么单出一档:这一族与「轮记录成形 ∥ 计数 ∥ 装配接缝」三面不同,同住 `turn-projection.mjs` 会把那档推过 500 内容行的
|
|
7
|
+
* 档位线(标准 `docs/core/standards/CODE-TIER.md` §2.1)。拆档只动文件归属,不动一条判据。先例 = `turn-task-state.mjs` 拆出时同形。
|
|
9
8
|
*
|
|
10
9
|
* 依赖方向单向:`turn-projection.mjs` → 本档。本档只依赖 `buildDualEndPreview`(`./offload.mjs`)与
|
|
11
10
|
* `estimateTokens`(`../token-window.mjs`),不 import 投影面——否则两档成环,拆了等于没拆。
|
|
12
11
|
*
|
|
12
|
+
* 这里的一切只落在轮记录的 `toolProcess` 份上(人读回查与端侧 `_turns` 的机器面),不改机读线原文;
|
|
13
|
+
* 线上的唯一动作是清理(`turn-projection.mjs` 的 `retireTurnToolProcess`)。
|
|
14
|
+
*
|
|
13
15
|
* 对外契约:搬走的符号在 `turn-projection.mjs` 原路径 re-export,既有 import 一字不改(三个导入方 =
|
|
14
16
|
* `context.mjs` ∥ `context-degrade.mjs` ∥ `agent-tools/context.mjs`)。
|
|
15
17
|
*/
|
|
@@ -19,19 +21,28 @@
|
|
|
19
21
|
import { buildDualEndPreview } from "./offload.mjs"
|
|
20
22
|
// 预算与 `folded` 同一把尺:`estimateTokens` 是压缩面的计量尺(§1.2「预算用共享估算器,不另立尺」)。
|
|
21
23
|
import { estimateTokens } from "../token-window.mjs"
|
|
24
|
+
// 配置计数值的取值域谓词(单源 = 窗口策略面):窗宽与压缩阈值共用一份判断。
|
|
25
|
+
import { nonNegativeIntValue } from "../window-policy.mjs"
|
|
22
26
|
|
|
23
27
|
// ────────────────────── 保留窗与工具段预算(CONTEXT-COMPACTION.md §1.1 ∥ §1.2) ──────────────────────
|
|
24
28
|
|
|
25
|
-
/**
|
|
26
|
-
|
|
29
|
+
/** 保留窗宽(§1「保留窗与跨轮清理」)= 配置键 `agent.keepToolTurns`:最近 N 轮的工具调用与结果原样留在
|
|
30
|
+
* 机读线上;**0 或缺省 = 全部轮次不删**。窗宽没有上限:装不下的实情由 `enforceContextFit` 的
|
|
31
|
+
* `context_overflow` 如实报出并点保留窗,静默改小用户配的窗是改写执行事实。
|
|
32
|
+
* 读点兜底:非法值(负数 ∥ 小数 ∥ 非数字)在装载期已清出配置,这一层再给 0——手改、旧槽、直接构造的
|
|
33
|
+
* agent 对象都落在「不删」上,而不是落在一个猜出来的轮数上。 */
|
|
34
|
+
export function toolProcessWindow(agent) {
|
|
35
|
+
return nonNegativeIntValue(agent?.config?.agent?.keepToolTurns) ?? 0
|
|
36
|
+
}
|
|
27
37
|
/** 单条工具内容的字符预算(§1.2):越限取双端——头 + 省略数注记 + 尾。 */
|
|
28
38
|
export const TOOL_PROCESS_MSG_CHARS = 4000
|
|
29
39
|
/** 单轮工具段的 token 预算(§1.2):越限从最旧调用组起整组移出。 */
|
|
30
40
|
export const TOOL_PROCESS_TURN_TOKENS = 8000
|
|
31
41
|
/** 折叠提示行的噪声门(§1.2):真的丢掉量低于它,`folded` 整行不出现。 */
|
|
32
42
|
export const FOLD_HINT_TOKENS = 1000
|
|
33
|
-
/** 段首移出说明的标记(§1.2
|
|
34
|
-
*
|
|
43
|
+
/** 段首移出说明的标记(§1.2 载体):它只活在副本里,读者是回查这条记录的人与端侧 `_turns` 的恢复面——
|
|
44
|
+
* 不进机读线(线上不出现任何机制合成的消息),模型读不到;因此不受 `FOLD_HINT_TOKENS` 门。
|
|
45
|
+
* 角色取 `user` 是记录面沿用既有形状(与压缩摘要同形),不承担也不冲突任何配对语义。 */
|
|
35
46
|
export const TOOL_PROCESS_DROP_MARK = "[Tool process trimmed]"
|
|
36
47
|
|
|
37
48
|
/** 一条调用组的起点:`assistant(tool_calls)`——整组进出机读线的原子单位(§1.1)。 */
|
|
@@ -69,7 +80,7 @@ export function toolSliceOf(segment) {
|
|
|
69
80
|
}
|
|
70
81
|
|
|
71
82
|
/** 整段预算(§1.2):越 `TOOL_PROCESS_TURN_TOKENS` 就从最旧调用组起整组移出——一条 `assistant(tool_calls)`
|
|
72
|
-
* 与其全部 `tool`
|
|
83
|
+
* 与其全部 `tool` 结果同进同出(只动一边留下孤立配对;发送面对两种半边各有归一,补出来的不是原始结果)。
|
|
73
84
|
* 终止规则:只剩最新一组仍超限即停手——那一组保留完整、键仍在、段首那行报「已移出其余全部调用组」。
|
|
74
85
|
* 预算量的是**留下的调用组**:段首那行说明(含它报的组数 ∥ 条数 ∥ token)在预算之外,它约几十 token,
|
|
75
86
|
* 是这次移出的说明本身,不拿它去逼掉一整组真实结果。
|
|
@@ -134,35 +145,19 @@ export function foldHintOf(...sources) {
|
|
|
134
145
|
return total.tokens < FOLD_HINT_TOKENS ? null : total
|
|
135
146
|
}
|
|
136
147
|
|
|
137
|
-
/** 窗口裁切的上机读线部分(§1.1):把挤出去那一轮的工具段消息按**身份**摘除——压缩重建数组后下标会漂、
|
|
138
|
-
* 身份不漂。身份账就是那条轮记录自己的 `toolProcess`(折叠时插进机读线的正是这几个对象),不另立账。 */
|
|
139
|
-
export function dropToolProcessFromLine(agent, record) {
|
|
140
|
-
const gone = new Set(Array.isArray(record?.toolProcess) ? record.toolProcess : [])
|
|
141
|
-
if (gone.size === 0) return
|
|
142
|
-
const history = Array.isArray(agent?.history) ? agent.history : []
|
|
143
|
-
for (let i = history.length - 1; i >= 0; i--) if (gone.has(history[i])) history.splice(i, 1)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
148
|
/**
|
|
147
|
-
* 保留窗起点(§1.2
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
149
|
+
* 保留窗起点(§1.2 结构保护):保留窗 = 最近 `toolProcessWindow(agent)` 轮(按外层轮元素数)。
|
|
150
|
+
* 返回位置 `{ turn, msg }`:`turn` = 外层轮元素数 − 窗宽,`msg` 恒 0(从那一轮的头开始)。
|
|
151
|
+
* 轮元素不足窗宽 ⇒ `null`(不收紧):调用方拿到 `null` 就传拍平条数,不猜位置。
|
|
152
|
+
* 窗宽 0(全部保留)⇒ `{ turn: history.length, msg: 0 }`——合法线尾位置,一次换算得拍平总数,
|
|
153
|
+
* 于是 `Math.min(tailStart, protectFrom)` 自然不收紧,调用方不必另开一条分支。
|
|
154
|
+
* 不返回拍平下标——位置与拍平序的换算只在调用方做一次(`flatIndexOf`),线上不存下标。
|
|
155
|
+
* 消费面 = `splitHistory` 的第四参(经一次换算):压缩的中段 ∥ prune 的合格集 ∥ 尾部预算三处同源受益。
|
|
153
156
|
*/
|
|
154
157
|
export function retentionWindowStart(agent) {
|
|
155
158
|
const history = Array.isArray(agent?.history) ? agent.history : []
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const tp = turns[i]?.toolProcess
|
|
161
|
-
if (!Array.isArray(tp) || tp.length === 0) continue
|
|
162
|
-
for (const m of Array.isArray(marks[i]) ? marks[i] : []) {
|
|
163
|
-
const k = history.indexOf(m)
|
|
164
|
-
if (k >= 0 && (earliest < 0 || k < earliest)) earliest = k
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return earliest >= 0 ? earliest : history.length
|
|
159
|
+
const keep = toolProcessWindow(agent)
|
|
160
|
+
if (keep === 0) return { turn: history.length, msg: 0 }
|
|
161
|
+
if (history.length < keep) return null
|
|
162
|
+
return { turn: history.length - keep, msg: 0 }
|
|
168
163
|
}
|
|
@@ -64,7 +64,7 @@ import { withIdentityLine, scopeSummary } from "../advisor/notice.mjs"
|
|
|
64
64
|
import { runAdvisorReview, resolveAdvisorProvider, ADVISOR_THINKING_PLACEHOLDER } from "../advisor/run.mjs"
|
|
65
65
|
import { stripEventToken } from "../agent/spawn-child.mjs"
|
|
66
66
|
import { escapeXml } from "../agent/helpers.mjs"
|
|
67
|
-
import {
|
|
67
|
+
import { pushInject, pushRecord } from "../context.mjs"
|
|
68
68
|
import { logEvent } from "../log.mjs"
|
|
69
69
|
import { deathLine } from "../abort-provenance.mjs"
|
|
70
70
|
// ASYNC-RESULT-CONTAINER.md D3/D6:settle 公共收尾单点 + child signal 构建单点
|
|
@@ -109,10 +109,13 @@ export function cancelAsyncAdvisor(agent, id, onToken) {
|
|
|
109
109
|
entry.status = "done"
|
|
110
110
|
map.delete(key)
|
|
111
111
|
writeTombstone(agent, key, "cancelled", "advisor")
|
|
112
|
-
|
|
112
|
+
const msg = {
|
|
113
113
|
role: "user",
|
|
114
114
|
content: `[System reminder: async advisor review #${escapeXml(String(entry.id))} cancelled — the review did not settle; token not issued (评审已取消——token 未签发)]`,
|
|
115
|
-
}
|
|
115
|
+
}
|
|
116
|
+
// B 段:一次性投递走待发队,持久写入只落人读线(机读线零触)。
|
|
117
|
+
pushInject(agent, msg)
|
|
118
|
+
pushRecord(agent, msg)
|
|
116
119
|
// 块面事件(零字段;relay 前缀 advisor#<id>/)——不发 ⟦ev⟧stopped(非 settle 通道)
|
|
117
120
|
try { onToken?.(`${entry.relayPrefix}⟦ev⟧cancelled\x1e`) } catch { /* relay 失败不影响池状态 */ }
|
|
118
121
|
logEvent("ev:cancelled", { id: `advisor#${key}` }) // 与 running 面同形(写点互斥 ⇒ 恰一条)
|