@thincoder/core 0.11.4 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/abort-provenance.mjs +23 -0
- package/advisor/citations.mjs +5 -1
- package/advisor/compaction.mjs +13 -10
- package/advisor/history.mjs +6 -1
- package/advisor/loop.mjs +35 -10
- package/advisor/messages.mjs +31 -43
- package/advisor/project-context.mjs +49 -45
- package/advisor/run.mjs +82 -26
- package/advisor.mjs +40 -11
- package/agent/assemble.mjs +5 -5
- package/agent/chat-call.mjs +21 -1
- package/agent/completion.mjs +1 -20
- package/agent/dispatch-gates.mjs +8 -3
- package/agent/dispatch-run.mjs +131 -20
- package/agent/dispatch.mjs +23 -22
- package/agent/family-tools.mjs +4 -7
- package/agent/goal-judge.mjs +99 -0
- package/agent/goal-state.mjs +96 -0
- package/agent/helpers.mjs +13 -4
- package/agent/offload.mjs +1 -1
- package/agent/planning-graph-guardrail.mjs +224 -0
- package/agent/planning-prompt.mjs +179 -0
- package/agent/planning-step.mjs +460 -173
- package/agent/post-turn.mjs +23 -33
- package/agent/record-results.mjs +8 -5
- package/agent/run-stages.mjs +22 -41
- package/agent/run-start.mjs +26 -19
- package/agent/setup-reminders.mjs +16 -14
- package/agent/setup.mjs +3 -2
- package/agent/spawn-child.mjs +17 -4
- package/agent/suspension.mjs +42 -21
- package/agent/task-lifecycle.mjs +165 -52
- package/agent/timers.mjs +9 -1
- package/agent/turn-loop.mjs +160 -69
- package/agent/turn-projection.mjs +26 -15
- package/agent/turn-task-state.mjs +4 -4
- package/agent/write-gate-ledger.mjs +1 -1
- package/agent/write-gate.mjs +14 -11
- package/agent-tools/advisor-async.mjs +2 -0
- package/agent-tools/advisor-launch.mjs +27 -13
- package/agent-tools/advisor-settle.mjs +19 -18
- package/agent-tools/advisor.mjs +90 -13
- package/agent-tools/bash-async.mjs +3 -3
- package/agent-tools/batch-ledger-bind.mjs +8 -5
- package/agent-tools/batch-read.mjs +42 -0
- package/agent-tools/batch-record-write.mjs +20 -12
- package/agent-tools/batch-segment-vocab.mjs +6 -4
- package/agent-tools/batch-skeleton.mjs +43 -12
- package/agent-tools/consult.mjs +5 -1
- package/agent-tools/context.mjs +5 -4
- package/agent-tools/design-slots.mjs +7 -4
- package/agent-tools/design-token.mjs +3 -2
- package/agent-tools/doc-graph.mjs +257 -95
- package/agent-tools/escalate-async.mjs +4 -1
- package/agent-tools/goal.mjs +28 -95
- package/agent-tools/plan.mjs +1 -1
- package/agent-tools/review-facts.mjs +56 -7
- package/agent-tools/settings.mjs +35 -1
- package/agent-tools/ssh-transport.mjs +23 -22
- package/agent-tools/step.mjs +24 -4
- package/agent-tools/subagent-actions.mjs +6 -2
- package/agent-tools/subagent-async.mjs +4 -2
- package/agent-tools/subagent.mjs +1 -1
- package/agent-tools/task.mjs +73 -65
- package/agent-tools.mjs +3 -3
- package/agent.mjs +12 -3
- package/auto-think.mjs +16 -3
- package/browser/actions.mjs +5 -1
- package/browser/cdp.mjs +1 -1
- package/browser/pool.mjs +110 -17
- package/compaction-work-state.mjs +103 -13
- package/compress-form.mjs +29 -3
- package/config-defaults.mjs +27 -3
- package/config-io.mjs +3 -14
- package/config-normalize.mjs +24 -4
- package/config-presets.mjs +40 -26
- package/config-warn.mjs +30 -0
- package/config.mjs +139 -63
- package/context-degrade.mjs +4 -4
- package/context-push.mjs +20 -9
- package/context.mjs +65 -20
- package/doc-graph/anchors.mjs +39 -40
- package/doc-graph/clusters.mjs +49 -0
- package/doc-graph/code-parsers.mjs +494 -0
- package/doc-graph/context-pack.mjs +392 -0
- package/doc-graph/dir-signals.mjs +228 -0
- package/doc-graph/edges.mjs +231 -0
- package/doc-graph/format.mjs +70 -0
- package/doc-graph/gc-cmd.mjs +104 -0
- package/doc-graph/gc.mjs +112 -0
- package/doc-graph/glob-work-graph.mjs +307 -0
- package/doc-graph/golden-set.mjs +131 -0
- package/doc-graph/ls-work-graph.mjs +534 -0
- package/doc-graph/outline.mjs +377 -0
- package/doc-graph/output-annotations.mjs +85 -0
- package/doc-graph/process-docs.mjs +68 -0
- package/doc-graph/read-work-graph.mjs +91 -0
- package/doc-graph/rebuild-cmd.mjs +75 -0
- package/doc-graph/resolvers.mjs +322 -0
- package/doc-graph/roots.mjs +86 -0
- package/doc-graph/schema.mjs +110 -0
- package/doc-graph/search-augment.mjs +418 -0
- package/doc-graph/store.mjs +231 -0
- package/doc-graph/target-resolve.mjs +352 -0
- package/doc-graph/walk.mjs +87 -0
- package/doc-graph.mjs +583 -707
- package/explore-distill.mjs +52 -17
- package/generate-title.mjs +7 -4
- package/ledger-close.mjs +6 -6
- package/ledger-cmd.mjs +3 -3
- package/ledger-consistency.mjs +8 -6
- package/ledger-db.mjs +45 -10
- package/ledger-migrate.mjs +25 -10
- package/ledger-read.mjs +1 -1
- package/ledger-write-ops.mjs +6 -6
- package/ledger.mjs +2 -2
- package/model-context.mjs +54 -18
- package/model-specs.mjs +86 -67
- package/package.json +1 -1
- package/prompt-overlays.mjs +3 -3
- package/prompts/advisor-round1.md +6 -5
- package/prompts/common.md +16 -6
- package/prompts/consult-base.md +2 -1
- package/prompts/discipline-engineering.md +1 -1
- package/prompts/discipline-normal.md +12 -8
- package/prompts/knowledge/doc-lookup.md +2 -2
- package/prompts/knowledge/normal-batch-discipline.md +2 -2
- package/prompts/knowledge/normal-delivery-collaboration.md +3 -3
- package/prompts/knowledge/tool-advisor.md +2 -2
- package/prompts/knowledge/tool-apply-patch.md +1 -1
- package/prompts/knowledge/tool-bash.md +2 -2
- package/prompts/knowledge/tool-batch.md +4 -4
- package/prompts/knowledge/tool-glob.md +9 -2
- package/prompts/knowledge/tool-goal.md +9 -6
- package/prompts/knowledge/tool-grep.md +7 -2
- package/prompts/knowledge/tool-ls.md +11 -5
- package/prompts/knowledge/tool-lsp.md +1 -1
- package/prompts/knowledge/tool-operations.md +3 -2
- package/prompts/knowledge/tool-plan.md +1 -1
- package/prompts/knowledge/tool-read.md +7 -6
- package/prompts/knowledge/tool-repo-graph-read.md +12 -0
- package/prompts/knowledge/tool-repo-graph.md +12 -0
- package/prompts/knowledge/tool-step.md +1 -1
- package/prompts/knowledge/tool-subagent.md +2 -2
- package/prompts/knowledge/tool-task.md +10 -9
- package/prompts/knowledge/tool-tree.md +7 -1
- package/prompts/persona-eng-coder.md +1 -1
- package/prompts/persona-eng-designer.md +1 -1
- package/prompts/persona-engineering.md +2 -1
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-normal.md +7 -6
- package/prompts/persona-plan.md +2 -2
- package/prompts/planning-next-moves.md +7 -8
- package/provider/anthropic.mjs +53 -51
- package/provider/core.mjs +85 -34
- package/provider/errors.mjs +33 -10
- package/provider/google.mjs +244 -37
- package/provider/list-models.mjs +28 -14
- package/provider/responses-request.mjs +15 -9
- package/provider/responses.mjs +10 -8
- package/provider/sse.mjs +3 -6
- package/proxy-transport.mjs +228 -119
- package/rules-file.mjs +79 -0
- package/rules.mjs +13 -51
- package/session-lifecycle.mjs +32 -20
- package/team-ledger.mjs +7 -7
- package/think-off.mjs +37 -18
- package/token-window.mjs +48 -31
- package/tool-args.mjs +8 -8
- package/tool-docs/bash.md +1 -1
- package/tool-docs/glob.md +3 -1
- package/tool-docs/goal.md +2 -2
- package/tool-docs/grep.md +3 -1
- package/tool-docs/ls.md +7 -1
- package/tool-docs/read.md +1 -1
- package/tool-docs/repo_graph-read.md +3 -0
- package/tool-docs/repo_graph.md +3 -0
- package/tool-docs/tree.md +1 -1
- package/tools/bash.mjs +53 -8
- package/tools/browser.mjs +4 -4
- package/tools/file.mjs +34 -7
- package/tools/glob-dialect.mjs +57 -7
- package/tools/index.mjs +3 -7
- package/tools/patch.mjs +8 -7
- package/tools/process-doc-mark.mjs +13 -0
- package/tools/search.mjs +152 -37
- package/tools/shared.mjs +6 -0
- package/tools/tree.mjs +211 -8
- package/window-policy.mjs +205 -126
- package/prompts/knowledge/tool-doc-graph-read.md +0 -20
- package/prompts/knowledge/tool-doc-graph.md +0 -49
- package/prompts/knowledge/tool-peek.md +0 -10
- package/prompts/knowledge/tool-repo-outline.md +0 -4
- package/tool-docs/doc_graph-read.md +0 -3
- package/tool-docs/doc_graph.md +0 -3
- package/tool-docs/peek.md +0 -3
- package/tool-docs/repo_outline.md +0 -3
- package/tools/peek.mjs +0 -447
package/agent/turn-loop.mjs
CHANGED
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { resolve } from "node:path"
|
|
8
8
|
import { pushReal, pushInject, pushRecord, flattenHistory, summarizeRunExplorations } from "../context.mjs"
|
|
9
|
-
import { abortError, annotateAbort } from "../abort-provenance.mjs"
|
|
9
|
+
import { abortError, annotateAbort, interruptLine } from "../abort-provenance.mjs"
|
|
10
10
|
import { specForModel, assistantEchoMessage, assistantTextMessage, isThinkingRequest } from "../config.mjs"
|
|
11
11
|
import { executeToolCalls } from "./dispatch.mjs"
|
|
12
12
|
import { recordToolResults } from "./record-results.mjs"
|
|
13
13
|
/** 正文收尾入线(#2647):回声键在写历史那一刻落键——有原文写原文,无原文在思考模式下写空串。 */
|
|
14
14
|
function pushAssistantText(agent, response) {
|
|
15
15
|
const spec = specForModel(agent.provider?.model)
|
|
16
|
-
pushReal(agent, assistantTextMessage(response, spec, isThinkingRequest(agent.
|
|
16
|
+
pushReal(agent, assistantTextMessage(response, spec, isThinkingRequest(agent.modelParams ?? null, spec), agent.provider ?? null))
|
|
17
17
|
}
|
|
18
18
|
// 归属表(收口取材的判据一):中断分支的记账点与常路同笔——两处 `_touchedFiles.push` 都写归属。
|
|
19
19
|
import { recordAttribution } from "./batch-attribution.mjs"
|
|
@@ -21,9 +21,12 @@ import { injectPostTurn, createWebSearchNudgeState } from "./post-turn.mjs"
|
|
|
21
21
|
import {
|
|
22
22
|
resolvePlanningConfig,
|
|
23
23
|
createPlanningState,
|
|
24
|
+
mergePlanningContinuationState,
|
|
24
25
|
pickPlanningTrigger,
|
|
25
26
|
recordPlanningTurnOutcome,
|
|
27
|
+
injectPassiveGraphReadingPack,
|
|
26
28
|
runPlanningStep,
|
|
29
|
+
extractLastRealUserGoal,
|
|
27
30
|
} from "./planning-step.mjs"
|
|
28
31
|
import { clearTurnDomainIfEmpty, clearUpstreamDomainIfEmpty } from "./turn-domain-text.mjs"
|
|
29
32
|
import { handleCompletion, isSubstantiveResponse } from "./completion.mjs"
|
|
@@ -36,11 +39,15 @@ import {
|
|
|
36
39
|
ContinueError,
|
|
37
40
|
FILE_MUTATORS, toolTouchPaths,
|
|
38
41
|
wroteSince, // #934(§6.32.2 ① 判据式):写序是否推进——轮顶 ∥ 撞帽收尾 ∥ advisor 预算复位(§6.4 A)三处判据点共用
|
|
42
|
+
DEFAULT_GOAL_TURNS,
|
|
39
43
|
} from "./helpers.mjs"
|
|
44
|
+
// goal mode 退出点钩子(设计 = docs/core/design/GOAL.md §3):裁判单源 + 状态机单源。
|
|
45
|
+
import { judgeGoal } from "./goal-judge.mjs"
|
|
46
|
+
import { pauseGoal, recordVerdict, recordJudgeFailure, addGoalTokens } from "./goal-state.mjs"
|
|
40
47
|
|
|
41
48
|
export async function runTurnLoop(agent, {
|
|
42
49
|
maxTurns, threshold, toolSchemas, toolByName, systemPrompt,
|
|
43
|
-
depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
|
|
50
|
+
depth, signal, autoTurn, upstreamTurn, timerTurn = false, suspDriven, streamOutput,
|
|
44
51
|
consumeInjected, consumeQueuedInput, callbacks, distillSignal,
|
|
45
52
|
}) {
|
|
46
53
|
// Compaction overhead for the pure-estimation path: system prompt + tools schema are
|
|
@@ -64,7 +71,7 @@ export async function runTurnLoop(agent, {
|
|
|
64
71
|
try {
|
|
65
72
|
return await runTurnLoopBody(agent, {
|
|
66
73
|
maxTurns, threshold, toolSchemas, toolByName, systemPrompt,
|
|
67
|
-
depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
|
|
74
|
+
depth, signal, autoTurn, upstreamTurn, timerTurn, suspDriven, streamOutput,
|
|
68
75
|
consumeInjected, consumeQueuedInput, callbacks, distillSignal,
|
|
69
76
|
compactionOverhead,
|
|
70
77
|
})
|
|
@@ -75,7 +82,7 @@ export async function runTurnLoop(agent, {
|
|
|
75
82
|
|
|
76
83
|
async function runTurnLoopBody(agent, {
|
|
77
84
|
maxTurns, threshold, toolSchemas, toolByName, systemPrompt,
|
|
78
|
-
depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
|
|
85
|
+
depth, signal, autoTurn, upstreamTurn, timerTurn = false, suspDriven, streamOutput,
|
|
79
86
|
consumeInjected, consumeQueuedInput, callbacks, distillSignal,
|
|
80
87
|
compactionOverhead,
|
|
81
88
|
}) {
|
|
@@ -85,25 +92,33 @@ async function runTurnLoopBody(agent, {
|
|
|
85
92
|
const recentCallSigs = []
|
|
86
93
|
const streamRuleFired = new Set()
|
|
87
94
|
const searchNudgeState = createWebSearchNudgeState()
|
|
88
|
-
const
|
|
95
|
+
const deliveredAsync = Array.isArray(agent._deliveredAsyncItems) ? [...agent._deliveredAsyncItems] : []
|
|
96
|
+
const legacyAdvisorTypes = Array.isArray(agent._deliveredAdvisorReviews)
|
|
89
97
|
? agent._deliveredAdvisorReviews.map((r) => (r?.reviewType === "design" || r?.reviewType === "code" ? r.reviewType : "unknown"))
|
|
90
98
|
: []
|
|
99
|
+
agent._deliveredAsyncItems = []
|
|
91
100
|
agent._deliveredAdvisorReviews = []
|
|
101
|
+
// 三档主会话(tiny/normal/engineering)一律生效,不再按 mode 特判;子代理 / plan 模式 / 上行问询轮
|
|
102
|
+
// 分别由 depth、agent.planMode、upstreamTurn 排除。曾经传过的 `mode` 实参已随特判拆除而删除(形参从未接收它)。
|
|
92
103
|
const planningCfg = resolvePlanningConfig(agent, {
|
|
93
|
-
depth, autoTurn, upstreamTurn,
|
|
94
|
-
hasAdvisorDelivery: deliveredAdvisorTypes.length > 0,
|
|
104
|
+
depth, autoTurn, upstreamTurn, timerTurn,
|
|
95
105
|
})
|
|
96
|
-
|
|
106
|
+
// 撞帽续跑段(`resume: true`):`run-start.mjs` 在非续跑链起点才清 `agent._planningRunState`,
|
|
107
|
+
// 续跑段直接沿用上一段已读/护栏/修改记忆,防止第 2 段失忆误报切片读码或重启图谱催读。
|
|
108
|
+
const planningState = agent._planningRunState
|
|
109
|
+
? mergePlanningContinuationState(agent._planningRunState, { deliveredAsync, advisorReviewTypes: legacyAdvisorTypes })
|
|
110
|
+
: (agent._planningRunState = createPlanningState({ deliveredAsync, advisorReviewTypes: legacyAdvisorTypes }))
|
|
97
111
|
const lastInitial = flattenHistory(agent.history).at(-1)
|
|
98
|
-
const initialUserInput = lastInitial?.role === "user"
|
|
112
|
+
const initialUserInput = !autoTurn && lastInitial?.role === "user"
|
|
99
113
|
? (typeof lastInitial.content === "string"
|
|
100
114
|
? lastInitial.content
|
|
101
115
|
: Array.isArray(lastInitial.content)
|
|
102
116
|
? lastInitial.content.map((p) => (typeof p?.text === "string" ? p.text : "")).join("\n")
|
|
103
117
|
: "")
|
|
104
118
|
: ""
|
|
119
|
+
const anchorGoal = initialUserInput || agent?.goal?.condition || extractLastRealUserGoal(agent.history)
|
|
105
120
|
|
|
106
|
-
// 切换预判(`CONTEXT-COMPACTION.md` §5.2):run 装配之后、第一个回合之前——模型切换 ∥ `
|
|
121
|
+
// 切换预判(`CONTEXT-COMPACTION.md` §5.2):run 装配之后、第一个回合之前——模型切换 ∥ `modelContext`
|
|
107
122
|
// 调小 ∥ `reasoningEffort` 变更这三条来路都让 `limit` 变小,而 `agent.history` 一条没少。预判点因此就是
|
|
108
123
|
// 装备点(参数按 run 现算),一处覆盖三条来路,不必为每条各立钩子。越线 ⇒ 跑与安全点同一条处置链。
|
|
109
124
|
await preflightWindowFit(agent, { threshold, callbacks, compactionOverhead, recentCallSigs, signal })
|
|
@@ -116,8 +131,8 @@ async function runTurnLoopBody(agent, {
|
|
|
116
131
|
return await callModelTurn(agent, turnCtx)
|
|
117
132
|
} catch (e) {
|
|
118
133
|
if (e?.kind !== "context_overflow") throw e
|
|
119
|
-
// 死循环守卫(批档 D6):额度 =
|
|
120
|
-
// (三实数 + `/compact`
|
|
134
|
+
// 死循环守卫(批档 D6):额度 = **每回合一次**——环头(CONTEXT-COMPACTION.md §5.2)复位,已用过 ⇒ 原 typed 错误透传
|
|
135
|
+
// (三实数 + `/compact` 提示已在报文体里);同一回合内第二次独立越窗不再自愈,用户 `/compact` 后可继续。
|
|
121
136
|
if (agent._overflowRetried) throw e
|
|
122
137
|
agent._overflowRetried = true
|
|
123
138
|
// 越窗 typed 错误有两个出口(同 kind,§6.17):HTTP 400 分类 ∥ 本地发送面净判——两面都归本链处理,
|
|
@@ -149,6 +164,10 @@ async function runTurnLoopBody(agent, {
|
|
|
149
164
|
// #793(核面小修批——AGENT-LOOP.md §3 环边界中止前置 ∥ 需求 F5):环头检查点——中止恒以
|
|
150
165
|
// AbortError 收束、不开启新轮(环头注入族 / 压缩 / 模型调用零触)。
|
|
151
166
|
if (signal?.aborted) throw abortError(signal, "agent", "turn-head")
|
|
167
|
+
// 越窗自愈额度(CONTEXT-COMPACTION.md §5.2):**每回合一次**——本行是唯一复位点(run 起点的
|
|
168
|
+
// `setup.mjs` 复位仍在,管本 run 的首回合)。run 级一次会让一次误报在第二个回合直接升级成硬错误;
|
|
169
|
+
// 每回合一次把额度还给真正需要它的那一回合,同时仍限住「一回合内恰一次强压 + 一次重试」。
|
|
170
|
+
agent._overflowRetried = false
|
|
152
171
|
// 第 19 批(TURN-ACROSS-SEGMENTS——设计 TURN-CAP-CONTINUE.md §4):编号帧——
|
|
153
172
|
// `_turnSeq` 每轮 +1(跨段累计,仅 `!resume` 链起点复位);面向消费面的两字段
|
|
154
173
|
// (状态行 + ⟦ev⟧turn / ⟦ev⟧approval 事件共用)在此同点赋值(编号唯一权威;帧
|
|
@@ -205,7 +224,10 @@ async function runTurnLoopBody(agent, {
|
|
|
205
224
|
// ③ 用户 → 主会话投递(步边界 pickup——queue-visible 批 fix 轮 2026-09-24 · F16):循环头
|
|
206
225
|
// 回合边界缝(核只给缝、不给策略——取批 / pushReal / 呈现归端侧闭包);缺省 null,系统轮不传
|
|
207
226
|
// (`AGENT-LOOP-ASYNC-POOL.md` §6.8「步边界 pickup」· 适用面 = 用户回合)。
|
|
208
|
-
|
|
227
|
+
// 闭包真值返回 = 本步真取到了一条排队消息(端侧只在消费成功时 `return true`;空队列 / 斜杠首动作 /
|
|
228
|
+
// 贴图让位都返回 falsy)⇒ 置插话触发位,紧接其后的规划步据此出一次「用户插话后重定锚」。
|
|
229
|
+
// 旧端壳回调无返回值 ⇒ 不置位(向后兼容,行为与改动前一致)。
|
|
230
|
+
if (consumeQueuedInput?.(agent)) planningState.queuedInputHit = true
|
|
209
231
|
|
|
210
232
|
const lastRole = flattenHistory(agent.history).at(-1)?.role
|
|
211
233
|
if (lastRole === "user" || lastRole === "tool") {
|
|
@@ -224,7 +246,7 @@ async function runTurnLoopBody(agent, {
|
|
|
224
246
|
if (planTrigger) {
|
|
225
247
|
await runPlanningStep(agent, {
|
|
226
248
|
trigger: planTrigger, cfg: planningCfg, state: planningState,
|
|
227
|
-
systemPrompt, toolSchemas, toolByName, callbacks, signal, depth, turn, input: initialUserInput,
|
|
249
|
+
systemPrompt, toolSchemas, toolByName, callbacks, signal, depth, turn, input: initialUserInput, goal: anchorGoal,
|
|
228
250
|
})
|
|
229
251
|
}
|
|
230
252
|
|
|
@@ -247,20 +269,40 @@ async function runTurnLoopBody(agent, {
|
|
|
247
269
|
agent._lastRunOutput = `${agent._lastRunOutput ?? ""}${response.content}\n`
|
|
248
270
|
}
|
|
249
271
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
272
|
+
if (response.usage) {
|
|
273
|
+
callbacks.onUsage?.(response.usage)
|
|
274
|
+
// Core-side cumulative cache tally (batch 2026-10-18-context-cache-visibility):
|
|
275
|
+
// every agent (parent or child) keeps its own hit/miss so `context` can report
|
|
276
|
+
// cache efficiency instead of the terminal faces being the only tally holders.
|
|
277
|
+
agent._cacheHitTokens = (agent._cacheHitTokens ?? 0) + (response.usage.prompt_cache_hit_tokens ?? 0)
|
|
278
|
+
agent._cacheMissTokens = (agent._cacheMissTokens ?? 0) + (response.usage.prompt_cache_miss_tokens ?? 0)
|
|
279
|
+
agent._usageTurns = (agent._usageTurns ?? 0) + 1
|
|
280
|
+
if (response.usage.prompt_tokens != null) {
|
|
281
|
+
agent._lastPromptTokens = response.usage.prompt_tokens
|
|
282
|
+
// 基线位置 = 真值覆盖到的**最后一个位置**,写成「最后一个轮元素 + 它此刻的内层长度」。
|
|
283
|
+
// 放在 builtinToolResults / assistant 入线之前,确保基线位置精确对应本次请求发送时的历史尾。
|
|
284
|
+
const lastTurn = Array.isArray(agent.history) ? agent.history.at(-1) : null
|
|
285
|
+
agent._usageAt = lastTurn
|
|
286
|
+
? { turn: agent.history.length - 1, msg: Array.isArray(lastTurn.messages) ? lastTurn.messages.length : 0 }
|
|
287
|
+
: { turn: 0, msg: 0 }
|
|
288
|
+
// 真值那一刻的非对话面(§2.5):与 `_lastPromptTokens` 同一个写点、同一个条件——两个字段
|
|
289
|
+
// 一起写、一起置 null。此刹 `_retained` ∥ `_inject` ∥ `_pendingInject` 仍是那条请求发出去时的样子
|
|
290
|
+
// (清待发队在回合结束时),所以这个基线就是真值里已含的非对话面,`fixed` 与它的差就是这段时间的增量。
|
|
291
|
+
agent._usageFixedBase = faceEstimate(agent, agent._ctxBasis?.overhead ?? compactionOverhead).fixed
|
|
292
|
+
}
|
|
293
|
+
// §6.17 估算自校准:服务端真值 ÷ 输入侧估算(`estimated` = 发送面净判同一个 estPrompt)
|
|
294
|
+
recordTokenSample(agent, agent.provider, response.estimated, response.usage)
|
|
295
|
+
// goal mode 的 token 账(设计 = docs/core/design/GOAL.md §1 `tokensSpent`):只在目标在跑时累加。
|
|
296
|
+
if (agent.goal?.status === "active") addGoalTokens(agent, response.usage)
|
|
260
297
|
}
|
|
261
298
|
|
|
299
|
+
// Google 搜索接地(`PROVIDER.md` §10.1):本回合模型自行发起过 Google 搜索 ⇒ 端面一行读数。
|
|
300
|
+
// **只转发不落历史**:元数据是「这次回答怎么来的」的读音,不是对话内容(故不进 `agent.history`,
|
|
301
|
+
// 也不会被回放进下一次请求);无接地渠/接地关时 `grounding` 为 null ⇒ 零回调零开销。
|
|
302
|
+
if (response.grounding) callbacks.onGrounding?.(response.grounding)
|
|
303
|
+
|
|
262
304
|
// Stream rule triggered mid-generation (action: "abort"): halt, inject the rule's
|
|
263
|
-
// message as a reminder, retry from the same context.
|
|
305
|
+
// message as a reminder, retry from the same context (skip orphan builtinToolResults).
|
|
264
306
|
if (response.ruleTriggered) {
|
|
265
307
|
if (response.content) {
|
|
266
308
|
pushAssistantText(agent, response)
|
|
@@ -270,6 +312,7 @@ async function runTurnLoopBody(agent, {
|
|
|
270
312
|
role: "user",
|
|
271
313
|
content: `[System reminder${label}: ${response.ruleMessage}]`,
|
|
272
314
|
})
|
|
315
|
+
callbacks.onTurnEnd?.(agent, turn)
|
|
273
316
|
continue
|
|
274
317
|
}
|
|
275
318
|
|
|
@@ -283,44 +326,37 @@ async function runTurnLoopBody(agent, {
|
|
|
283
326
|
if (response.content) {
|
|
284
327
|
pushAssistantText(agent, response)
|
|
285
328
|
}
|
|
329
|
+
// 消费本轮已随请求发出的 B 段旧副本,避免它们与中断提醒一起重投。
|
|
330
|
+
if (Array.isArray(agent._pendingInject) && agent._sentInjectRefs instanceof Set) {
|
|
331
|
+
agent._pendingInject = agent._pendingInject.filter((m) => !agent._sentInjectRefs.has(m))
|
|
332
|
+
}
|
|
333
|
+
agent._sentInjectRefs = null
|
|
334
|
+
// 两种中断形(CLI TUI.md 的判据):有 message = Ctrl+I(重建 controller 续跑);无 message =
|
|
335
|
+
// Ctrl+C 首按停回合。文案拼装单源 = `interruptLine`(不写 `[User interrupt: undefined]`)。
|
|
336
|
+
const line = interruptLine(response.interruptMessage)
|
|
286
337
|
pushInject(agent, {
|
|
287
338
|
role: "user",
|
|
288
|
-
content:
|
|
339
|
+
content: line,
|
|
289
340
|
})
|
|
290
341
|
pushRecord(agent, {
|
|
291
342
|
role: "user",
|
|
292
|
-
content:
|
|
343
|
+
content: line,
|
|
293
344
|
})
|
|
294
345
|
// AGENT-LOOP-SUBAGENT.md §6.12 站点 #8(第 24 批):错误对象已自带 name/message——只补来源标注(缺 abortInfo 才补)
|
|
295
346
|
throw annotateAbort(Object.assign(new Error("User interrupted"), { name: "AbortError" }), signal, "agent", "interrupted-response")
|
|
296
347
|
}
|
|
297
348
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
// 不能写 `{ turn: agent.history.length, msg: 0 }`:`flatIndexOf` 对 `turn === 轮元素数` 是拿
|
|
310
|
-
// 各轮内层之和现算的,而真值之后的消息恰恰追加进**最后一个轮元素的内层**(`pushReal` 追加到
|
|
311
|
-
// `agent.history.at(-1).messages`)——那个位置会跟着追加前移,后缀恒空,工具结果一条都补不上
|
|
312
|
-
// (§2.5「两轮调用之间追加的消息计进尾部估算」)。同形写法见 `agent/run-stages.mjs` 的 `endPositionOf`。
|
|
313
|
-
const lastTurn = Array.isArray(agent.history) ? agent.history.at(-1) : null
|
|
314
|
-
agent._usageAt = lastTurn
|
|
315
|
-
? { turn: agent.history.length - 1, msg: Array.isArray(lastTurn.messages) ? lastTurn.messages.length : 0 }
|
|
316
|
-
: { turn: 0, msg: 0 }
|
|
317
|
-
// 真值那一刻的非对话面(§2.5):与 `_lastPromptTokens` 同一个写点、同一个条件——两个字段
|
|
318
|
-
// 一起写、一起置 null。此刹 `_retained` ∥ `_inject` ∥ `_pendingInject` 仍是那条请求发出去时的样子
|
|
319
|
-
// (清待发队在回合结束时),所以这个基线就是真值里已含的非对话面,`fixed` 与它的差就是这段时间的增量。
|
|
320
|
-
agent._usageFixedBase = faceEstimate(agent, agent._ctxBasis?.overhead ?? compactionOverhead).fixed
|
|
321
|
-
}
|
|
322
|
-
// §6.17 估算自校准:服务端真值 ÷ 输入侧估算(`estimated` = 发送面净判同一个 estPrompt)
|
|
323
|
-
recordTokenSample(agent, agent.provider, response.estimated, response.usage)
|
|
349
|
+
// 内置工具(Responses web_search)结果本地化:服务端已执行——入历史为 tool 消息;
|
|
350
|
+
// 置于 ruleTriggered / interrupted 之后,避免流中止/中断时遗留孤儿 tool 消息。
|
|
351
|
+
// 服务端 item id 是 msg_xxx 非 web_search_call_ 前缀——必须合成前缀(toItems 识别锚点),
|
|
352
|
+
// 原始 id 存入 content(真机冒烟 2026-08-31 验证)。
|
|
353
|
+
for (const btr of response.builtinToolResults ?? []) {
|
|
354
|
+
if (!btr?.id) continue
|
|
355
|
+
pushReal(agent, {
|
|
356
|
+
role: "tool",
|
|
357
|
+
tool_call_id: `web_search_call_${btr.id}`,
|
|
358
|
+
content: JSON.stringify({ id: btr.id, query: btr.query ?? "", sources: btr.sources ?? [], status: btr.status ?? "completed" }),
|
|
359
|
+
})
|
|
324
360
|
}
|
|
325
361
|
|
|
326
362
|
// Warn on abnormal finish reasons — the response may be incomplete/truncated.
|
|
@@ -333,17 +369,59 @@ async function runTurnLoopBody(agent, {
|
|
|
333
369
|
advisorPushbacks = cr.advisorPushbacks
|
|
334
370
|
if (cr.action === "continue") continue
|
|
335
371
|
if (depth === 0) {
|
|
336
|
-
//
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
372
|
+
// 正常收尾结账(§7.4 + 蒸馏):goal 的停止支与普通收尾共用同一段——否则目标跑完时
|
|
373
|
+
// 已完成清单不清、本次 run 的探索蒸馏不产出。未闭 task 只保留状态,不为清表再开模型轮。
|
|
374
|
+
const finishRun = (text) => {
|
|
375
|
+
// §7.4(AGENT-LOOP.md):正常收尾时全员 `done` 的任务清单自动清空并经 `_onTaskUpdate([])` 同步四端,不留陈旧面板。
|
|
376
|
+
clearCompletedTasks(agent)
|
|
377
|
+
// End-of-run exploration distillation (CONTEXT-COMPACTION §5 + SEND-STALL-DISTILL
|
|
378
|
+
// §2.1): async — the promise hangs on _pendingDistill, settling at the next run's
|
|
379
|
+
// start or the TUI exit flush. Silent (N3): failure never blocks return/history.
|
|
380
|
+
// TRACES.md §6.1 D-TR4:depth 透传(distill 轨迹元数据——与 compress 同通道);extras = 会话续写前缀面(§6.15)
|
|
381
|
+
// §2.5-B(P4-I):端壳经 `opts.distillSignal` 供蒸馏专用中止信号(与运行 signal 分离——
|
|
382
|
+
// 运行 controller 重建不杀蒸馏);缺省(null)⇒ `signal` 逐字零变。
|
|
383
|
+
const distill = summarizeRunExplorations(agent, callbacks, distillSignal ?? signal, depth,
|
|
384
|
+
{ systemPrompt, tools: toolSchemas }).catch(() => {}) // 与回合请求同源(无第二构造点)
|
|
385
|
+
agent._pendingDistill = distill
|
|
386
|
+
return text
|
|
387
|
+
}
|
|
388
|
+
// goal mode 退出点钩子(设计 = docs/core/design/GOAL.md §3):模型想停时,裁判决定能不能停。
|
|
389
|
+
// 放在完成守卫与 task sweep 之后——那些是「活没干完」的硬门,goal 是「目标没达成」的软门。
|
|
390
|
+
if (agent.goal?.status === "active" && !signal?.aborted) {
|
|
391
|
+
const goalTurns = agent.config?.agent?.goalTurns ?? DEFAULT_GOAL_TURNS
|
|
392
|
+
if ((agent.goal.turnsEvaluated ?? 0) >= goalTurns) {
|
|
393
|
+
pauseGoal(agent, `turn budget exhausted (${goalTurns} evaluated turns)`)
|
|
394
|
+
return finishRun(`${cr.content}\n\n[goal paused: turn budget exhausted (${goalTurns} evaluated turns) — /goal resume to continue, /goal clear to drop it]`)
|
|
395
|
+
}
|
|
396
|
+
let verdict = null
|
|
397
|
+
try {
|
|
398
|
+
verdict = await judgeGoal(agent, { signal })
|
|
399
|
+
} catch (e) {
|
|
400
|
+
if (signal?.aborted) throw e
|
|
401
|
+
recordJudgeFailure(agent, { limit: 3, reason: `judge error: ${e?.message ?? e}` })
|
|
402
|
+
if (agent.goal.status === "paused") {
|
|
403
|
+
return finishRun(`${cr.content}\n\n[goal paused: judge unavailable 3 times (${e?.message ?? e}) — /goal resume to retry, /goal clear to drop it]`)
|
|
404
|
+
}
|
|
405
|
+
pushInject(agent, { role: "user", content: `[System reminder: goal judge unavailable (${e?.message ?? e}). The goal is NOT complete — keep working, or report blocked if this is a true impasse.]` })
|
|
406
|
+
callbacks.onTurnEnd?.(agent, turn)
|
|
407
|
+
continue
|
|
408
|
+
}
|
|
409
|
+
recordVerdict(agent, verdict)
|
|
410
|
+
if (verdict.verdict === "not_yet") {
|
|
411
|
+
if ((agent.goal.noProgressTurns ?? 0) >= 3) {
|
|
412
|
+
pauseGoal(agent, "no progress for 3 evaluated turns")
|
|
413
|
+
return finishRun(`${cr.content}\n\n[goal paused: no progress for 3 evaluated turns — /goal resume to continue, /goal clear to drop it]`)
|
|
414
|
+
}
|
|
415
|
+
pushInject(agent, { role: "user", content: `[System reminder: goal judge says NOT YET — ${verdict.reason ?? "the condition does not hold yet"}. Keep working toward the goal condition; do not stop until it holds.]` })
|
|
416
|
+
callbacks.onTurnEnd?.(agent, turn)
|
|
417
|
+
continue
|
|
418
|
+
}
|
|
419
|
+
if (verdict.verdict === "met") {
|
|
420
|
+
return finishRun(`${cr.content}\n\n[goal complete: ${verdict.reason ?? "condition met"}]`)
|
|
421
|
+
}
|
|
422
|
+
return finishRun(`${cr.content}\n\n[goal blocked: ${verdict.reason ?? "judge says the condition cannot be reached"}]`)
|
|
423
|
+
}
|
|
424
|
+
return finishRun(cr.content)
|
|
347
425
|
}
|
|
348
426
|
return cr.content
|
|
349
427
|
}
|
|
@@ -353,7 +431,7 @@ async function runTurnLoopBody(agent, {
|
|
|
353
431
|
|
|
354
432
|
// 回声键的写法归构造点,模式归 `isThinkingRequest`(单源 = think-off.mjs):调用点只把两者交给它。
|
|
355
433
|
const echoSpec = specForModel(agent.provider.model)
|
|
356
|
-
pushReal(agent, assistantEchoMessage(response, echoSpec, isThinkingRequest(agent.
|
|
434
|
+
pushReal(agent, assistantEchoMessage(response, echoSpec, isThinkingRequest(agent.modelParams ?? null, echoSpec), agent.provider ?? null))
|
|
357
435
|
|
|
358
436
|
const results = await executeToolCalls(agent, toolByName, response.toolCalls, callbacks, depth, signal)
|
|
359
437
|
|
|
@@ -387,13 +465,18 @@ async function runTurnLoopBody(agent, {
|
|
|
387
465
|
}
|
|
388
466
|
// 中断占位 `tool` 结果不入线:它们是配对占位,由发送层 `normalizeToolPairing` 按结构补
|
|
389
467
|
// (§1.1 注入三段:机制合成的消息一条都不落 `agent.history`)。
|
|
468
|
+
// 消费掉本轮发出请求时已携带的 B 段旧副本,避免它们与中断提醒一起重复发送。
|
|
469
|
+
if (Array.isArray(agent._pendingInject) && agent._sentInjectRefs instanceof Set) {
|
|
470
|
+
agent._pendingInject = agent._pendingInject.filter((m) => !agent._sentInjectRefs.has(m))
|
|
471
|
+
}
|
|
472
|
+
agent._sentInjectRefs = null
|
|
390
473
|
pushInject(agent, {
|
|
391
474
|
role: "user",
|
|
392
|
-
content:
|
|
475
|
+
content: interruptLine(signal?.reason?.message),
|
|
393
476
|
})
|
|
394
477
|
pushRecord(agent, {
|
|
395
478
|
role: "user",
|
|
396
|
-
content:
|
|
479
|
+
content: interruptLine(signal?.reason?.message),
|
|
397
480
|
})
|
|
398
481
|
callbacks.onTurnEnd?.(agent, turn)
|
|
399
482
|
continue
|
|
@@ -419,8 +502,16 @@ async function runTurnLoopBody(agent, {
|
|
|
419
502
|
|
|
420
503
|
// Commit tool results (pairing, multimodal deferral, mutation accounting, reindex)
|
|
421
504
|
await recordToolResults(agent, toolByName, results)
|
|
505
|
+
// 有工具调用 ⇒ 有进展:goal 无进展计数归零(设计 = docs/core/design/GOAL.md §3「无进展」)。
|
|
506
|
+
if (agent.goal?.status === "active") agent.goal.noProgressTurns = 0
|
|
422
507
|
|
|
423
|
-
|
|
508
|
+
const passiveGraphGuard = depth === 0 && !upstreamTurn && !agent.planMode
|
|
509
|
+
if (planningCfg || passiveGraphGuard) {
|
|
510
|
+
recordPlanningTurnOutcome(planningState, {
|
|
511
|
+
results, toolByName, cwd: agent.cwd, sessionFullReadFiles: agent._graphFullReadFiles,
|
|
512
|
+
})
|
|
513
|
+
}
|
|
514
|
+
if (passiveGraphGuard) await injectPassiveGraphReadingPack(agent, planningState)
|
|
424
515
|
injectPostTurn(agent, results, recentCallSigs, callbacks, turn, {
|
|
425
516
|
suspDriven, depth, toolByName, searchNudgeState: planningCfg ? null : searchNudgeState,
|
|
426
517
|
})
|
|
@@ -198,8 +198,14 @@ export function registerCompletedTurn(agent, { finalResponse = null, status = "c
|
|
|
198
198
|
// 越界即 `null`:窗宽 0(全部保留)时两处都越界,一道闸给出「一条不删」——`expiring` 为 null 时
|
|
199
199
|
// 下面的清理整段不执行,`expiringTurn` 那一刻指向在途轮,绝不能单独授人以柄。
|
|
200
200
|
const keep = toolProcessWindow(agent)
|
|
201
|
-
const
|
|
202
|
-
|
|
201
|
+
const expiringList = []
|
|
202
|
+
if (keep > 0) {
|
|
203
|
+
const turnsCutoff = agent._turns.length - keep
|
|
204
|
+
for (let i = 0; i <= turnsCutoff; i++) {
|
|
205
|
+
const t = agent._turns[i]
|
|
206
|
+
if (t && Array.isArray(t.toolProcess) && t.toolProcess.length > 0) expiringList.push(t)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
203
209
|
const record = buildTurnRecord(ledger, {
|
|
204
210
|
finalResponse,
|
|
205
211
|
status,
|
|
@@ -215,13 +221,16 @@ export function registerCompletedTurn(agent, { finalResponse = null, status = "c
|
|
|
215
221
|
batchDoc: agent._batchDoc ?? null,
|
|
216
222
|
logsPath: logsPathOrNull(),
|
|
217
223
|
toolProcess: tool.messages ?? [],
|
|
218
|
-
folded: foldHintOf(tool.lost, toolProcessLostOf(
|
|
224
|
+
folded: foldHintOf(tool.lost, ...expiringList.map((t) => toolProcessLostOf(t))),
|
|
219
225
|
})
|
|
220
226
|
agent._turns.push(record)
|
|
221
227
|
agent._turnLedger = null
|
|
222
|
-
if (
|
|
223
|
-
delete
|
|
224
|
-
|
|
228
|
+
if (keep > 0) {
|
|
229
|
+
for (const t of expiringList) delete t.toolProcess
|
|
230
|
+
const histCutoff = history.length - 1 - keep
|
|
231
|
+
for (let i = 0; i <= histCutoff; i++) {
|
|
232
|
+
if (history[i]) retireTurnToolProcess(agent, history[i])
|
|
233
|
+
}
|
|
225
234
|
}
|
|
226
235
|
return record
|
|
227
236
|
}
|
|
@@ -243,12 +252,13 @@ export function registerCompletedTurn(agent, { finalResponse = null, status = "c
|
|
|
243
252
|
function retireTurnToolProcess(agent, turn) {
|
|
244
253
|
const messages = Array.isArray(turn?.messages) ? turn.messages : []
|
|
245
254
|
if (messages.length === 0) return
|
|
246
|
-
|
|
255
|
+
let hasCallGroup = false
|
|
256
|
+
let hasToolMsg = false
|
|
247
257
|
for (const m of messages) {
|
|
248
|
-
if (
|
|
249
|
-
|
|
258
|
+
if (isCallGroupStart(m)) hasCallGroup = true
|
|
259
|
+
else if (m?.role === "tool") hasToolMsg = true
|
|
250
260
|
}
|
|
251
|
-
if (
|
|
261
|
+
if (!hasCallGroup && !hasToolMsg) return
|
|
252
262
|
const before = flattenHistory(agent.history)
|
|
253
263
|
const measuredEnd = agent?._usageAt ? flatIndexOf(agent.history, agent._usageAt) : before.length
|
|
254
264
|
const removed = []
|
|
@@ -258,10 +268,9 @@ function retireTurnToolProcess(agent, turn) {
|
|
|
258
268
|
const m = messages[i]
|
|
259
269
|
if (!m || typeof m !== "object") continue
|
|
260
270
|
if (m.role === "tool") {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
271
|
+
// 半可定位:本轮已离开保留窗,无论其对应 assistant(tool_calls) 是否仍在内层,tool 结果一律清理。
|
|
272
|
+
removed.push(m)
|
|
273
|
+
messages.splice(i, 1)
|
|
265
274
|
continue
|
|
266
275
|
}
|
|
267
276
|
if (!isCallGroupStart(m)) continue
|
|
@@ -282,5 +291,7 @@ function retireTurnToolProcess(agent, turn) {
|
|
|
282
291
|
messages.splice(i, 1)
|
|
283
292
|
}
|
|
284
293
|
}
|
|
285
|
-
|
|
294
|
+
if (removed.length > 0 || replacedFrom.length > 0) {
|
|
295
|
+
rebaseMeasured(agent, estimateTokens([...removed, ...replacedFrom]), estimateTokens(replacedTo), { before, measuredEnd })
|
|
296
|
+
}
|
|
286
297
|
}
|
|
@@ -99,7 +99,7 @@ export function createTurnLedger({ input, autoTurn = false, goal = null } = {})
|
|
|
99
99
|
return {
|
|
100
100
|
userRequest: autoTurn === true ? null : request,
|
|
101
101
|
autoTurn: autoTurn === true,
|
|
102
|
-
objective: request || capText(goal?.
|
|
102
|
+
objective: request || capText(goal?.condition) || null,
|
|
103
103
|
verification: [],
|
|
104
104
|
toolCalls: 0,
|
|
105
105
|
/** 本轮内被观察到的异常原因(续跑链保留;异常轮不登记,故只作 blockedBy 的次选来源)。 */
|
|
@@ -186,8 +186,8 @@ function remainingOf(tasks, goal) {
|
|
|
186
186
|
if (title) out.push(`${title} (${status})`)
|
|
187
187
|
if (out.length >= MAX_REMAINING) break
|
|
188
188
|
}
|
|
189
|
-
if (goal && goal.status === "active" && str(goal.
|
|
190
|
-
const objective = line(goal.
|
|
189
|
+
if (goal && goal.status === "active" && str(goal.condition)) {
|
|
190
|
+
const objective = line(goal.condition, 120)
|
|
191
191
|
if (objective && !out.includes(objective)) out.push(`goal (active): ${objective}`)
|
|
192
192
|
}
|
|
193
193
|
return out.slice(0, MAX_REMAINING)
|
|
@@ -195,7 +195,7 @@ function remainingOf(tasks, goal) {
|
|
|
195
195
|
|
|
196
196
|
function blockedOf(goal, ledger) {
|
|
197
197
|
if (goal && goal.status === "blocked") {
|
|
198
|
-
const reason = line(goal.
|
|
198
|
+
const reason = line(goal.lastReason ?? "", SUMMARY_CAP)
|
|
199
199
|
return reason ? `goal blocked: ${reason}` : "goal marked blocked"
|
|
200
200
|
}
|
|
201
201
|
// 异常轮不登记(设计 §2)——账本里的异常只在同一链后续正常收尾时充作阻塞来源。
|
|
@@ -472,7 +472,7 @@ function ledgerRowRefusal(parsed, at) {
|
|
|
472
472
|
*/
|
|
473
473
|
export async function ledgerBindWriteConflict(agent, depth, tool, toolName, args) {
|
|
474
474
|
if (!agent?.config?.agent?.engineering) return null // 普通模式零改(纪律自持)
|
|
475
|
-
if (!(depth === 0)) return null // 子代理走既有 token
|
|
475
|
+
if (!(depth === 0 || agent._forkedStep)) return null // 子代理走既有 token 门;主会话分叉微步沿用父侧写锁
|
|
476
476
|
const name = String(toolName ?? tool?.name ?? "")
|
|
477
477
|
if (!LEDGER_ENTRY_TOOLS.has(name)) return null // 入口集之外 ⇒ 本门不问
|
|
478
478
|
if (!ledgerGateApplies(name, tool, args)) return null // 只读 action ∥ 不写项目内档的枚举值 ⇒ 不要号
|
package/agent/write-gate.mjs
CHANGED
|
@@ -103,22 +103,25 @@ export function resolveReviewTargetPaths(agent) {
|
|
|
103
103
|
export function freezeWindowConflict(agent, absPaths) {
|
|
104
104
|
const declared = inflightDesignReviewConflict(agent, absPaths)
|
|
105
105
|
if (declared) return declared
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
// P0-2:批次档分支同样两池都扫(同步设计评审的绑定档也要护)。
|
|
107
|
+
const pools = [agent?._asyncAdvisors, agent?._syncAdvisors]
|
|
108
108
|
const wanted = (absPaths ?? [])
|
|
109
109
|
.filter((p) => typeof p === "string" && p.length > 0)
|
|
110
110
|
.map((p) => normAbs(p, agent?.cwd))
|
|
111
111
|
if (wanted.length === 0) return null
|
|
112
112
|
const wantedSet = new Set(wanted)
|
|
113
|
-
for (const
|
|
114
|
-
if (!
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
for (const pool of pools) {
|
|
114
|
+
if (!(pool instanceof Map) || pool.size === 0) continue
|
|
115
|
+
for (const entry of pool.values()) {
|
|
116
|
+
if (!entry || entry.reviewType !== "design") continue
|
|
117
|
+
if (entry.status !== "running" || entry.cancelled || entry.done) continue
|
|
118
|
+
// 逐笔绑定快照(发起时点):同实例的后一笔发起会覆盖 `run.batchDoc`,冻结窗要护的是
|
|
119
|
+
// **本条目被指派的那本档**(判据单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。
|
|
120
|
+
const batchDoc = entry.batchDoc ?? entry.run?.batchDoc
|
|
121
|
+
if (typeof batchDoc !== "string" || batchDoc.length === 0) continue
|
|
122
|
+
const batchAbs = normAbs(batchDoc, agent?.cwd)
|
|
123
|
+
if (wantedSet.has(batchAbs)) return { id: String(entry.id), path: batchAbs }
|
|
124
|
+
}
|
|
122
125
|
}
|
|
123
126
|
return null
|
|
124
127
|
}
|
|
@@ -290,6 +290,8 @@ export function launchAsyncAdvisor(parent, ctx, launch) {
|
|
|
290
290
|
onOutput: (chunk) => relayAdvisorOutput(ctx?.callbacks, entry.relayPrefix, chunk),
|
|
291
291
|
signal: entry.controller.signal,
|
|
292
292
|
onStall: (rounds) => pushAdvisorAlert(parent, `advisor#${id}`, rounds), // #934:连续无产出越阈 ⇒ 自动上行提醒(只推——§6.32)
|
|
293
|
+
// 整会话 usage(TUI.md §4):异步评审的模型轮也计进状态行;父回调缺 onUsage ⇒ 派 null,不吞读数也不造 0。
|
|
294
|
+
onUsage: (usage) => ctx?.callbacks?.onUsage?.(usage),
|
|
293
295
|
// 本笔发起时点的绑定快照(入口条目字段),与评审轨无关(判据单源 =
|
|
294
296
|
// `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。不读活的 `run.batchDoc`:
|
|
295
297
|
// 排队条目启动时那个字段已被后一笔发起覆盖。
|