mocode-ai 1.1.7 → 1.1.8
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/README.md +23 -37
- package/README.zh-CN.md +46 -38
- package/dist/agent/core.js +100 -446
- package/dist/agent/index.js +2 -21
- package/dist/agent/spawn.js +3 -5
- package/dist/agent/work-discipline.js +16 -70
- package/dist/config/index.js +6 -7
- package/dist/context/age-aware.js +18 -48
- package/dist/context/artifacts.js +19 -17
- package/dist/context/budget.js +27 -28
- package/dist/context/classifier.js +0 -1
- package/dist/context/encoders/index.js +4 -11
- package/dist/context/index.js +4 -7
- package/dist/context/lifecycle.js +115 -483
- package/dist/context/pipeline.js +8 -15
- package/dist/context/relevance.js +77 -55
- package/dist/host/stdio.js +0 -6
- package/dist/i18n/index.js +0 -6
- package/dist/index.js +11 -1
- package/dist/llm/index.js +72 -3
- package/dist/mcp/index.js +0 -1
- package/dist/repl/index.js +21 -15
- package/dist/runtime/browser-manager.js +299 -0
- package/dist/runtime/dev-server-manager.js +354 -0
- package/dist/runtime/shutdown.js +26 -0
- package/dist/session/compact.js +86 -102
- package/dist/session/index.js +0 -1
- package/dist/session/scheduler.js +88 -92
- package/dist/session/trace-metrics.js +5 -92
- package/dist/session/trace.js +1 -10
- package/dist/tools/builtins/browser.js +199 -0
- package/dist/tools/builtins/dev-server.js +99 -0
- package/dist/tools/builtins/index.js +28 -19
- package/dist/tools/builtins/screenshot.js +173 -0
- package/dist/tools/builtins/view-image.js +49 -0
- package/dist/tools/constants.js +3 -0
- package/dist/tools/registry.js +5 -29
- package/dist/ui/layout.js +28 -5
- package/dist/ui/render.js +11 -0
- package/package.json +2 -2
- package/dist/agent/middleware/checklist.js +0 -59
- package/dist/session/drop.d.ts +0 -19
- package/dist/session/drop.js +0 -93
- package/dist/tools/builtins/drop-context.d.ts +0 -18
- package/dist/tools/builtins/drop-context.js +0 -68
- package/dist/verification/diagnostics.js +0 -108
- package/dist/verification/fingerprint.js +0 -54
- package/dist/verification/index.js +0 -333
- package/dist/verification/postconditions.js +0 -98
- package/dist/verification/targeted-tests.js +0 -96
- package/dist/verification/types.js +0 -1
package/dist/agent/core.js
CHANGED
|
@@ -10,14 +10,11 @@ import { executeToolOutcome, getToolCapabilities, isFileMutationTool, tools, } f
|
|
|
10
10
|
import { checkPermission } from '../permissions/index.js';
|
|
11
11
|
import { validateToolArguments } from '../tools/validation.js';
|
|
12
12
|
import { getPlanDisabledTools, getRuntimeDisabledTools } from '../tools/constants.js';
|
|
13
|
-
import { createPreCompletionChecklistMiddleware, } from './middleware/checklist.js';
|
|
14
|
-
import { inferModelFamily } from './work-discipline.js';
|
|
15
|
-
import { reflectionHint, classifyError } from './retry-classifier.js';
|
|
16
13
|
import { getAgentMode, setAgentMode } from './mode.js';
|
|
17
|
-
import { maybeCompact, contextState,
|
|
14
|
+
import { maybeCompact, contextState, createTraceEvent, summarizeToolArguments, safeProviderId, } from '../session/index.js';
|
|
15
|
+
import { capToolResultForHistory } from '../session/compact.js';
|
|
18
16
|
import { createBudgetScheduler } from '../session/scheduler.js';
|
|
19
|
-
import {
|
|
20
|
-
import { createAgeAwareEncodingState, } from '../context/age-aware.js';
|
|
17
|
+
import { recordArtifact, invalidateArtifacts, rehydrateArtifacts, } from '../context/index.js';
|
|
21
18
|
import { createRelevancePruner } from '../context/relevance.js';
|
|
22
19
|
import { isToolResultSuccess } from '../context/utils.js';
|
|
23
20
|
import { config } from '../config/index.js';
|
|
@@ -26,18 +23,7 @@ import { jailResolve } from '../sandbox/index.js';
|
|
|
26
23
|
import { createLifecycleEngine } from '../context/lifecycle.js';
|
|
27
24
|
import { getTokenCalibration, updateTokenCalibration, } from '../context/token-calibration.js';
|
|
28
25
|
import { getCurrentTurnId, getCurrentTurnMutationState } from '../rollback/index.js';
|
|
29
|
-
import { createAutomaticValidator, } from '../verification/index.js';
|
|
30
26
|
import { getCurrentSessionId } from '../session/state.js';
|
|
31
|
-
/** Stable per-history age state survives user turns; WeakMap avoids retaining closed sessions. */
|
|
32
|
-
const ageAwareStateByHistory = new WeakMap();
|
|
33
|
-
function ageAwareStateFor(history) {
|
|
34
|
-
const existing = ageAwareStateByHistory.get(history);
|
|
35
|
-
if (existing)
|
|
36
|
-
return existing;
|
|
37
|
-
const created = createAgeAwareEncodingState(history);
|
|
38
|
-
ageAwareStateByHistory.set(history, created);
|
|
39
|
-
return created;
|
|
40
|
-
}
|
|
41
27
|
/** 解析工具 arguments JSON;非法或空返 null(调用方据此降级到普通 preview)。 */
|
|
42
28
|
function parseArgs(raw) {
|
|
43
29
|
try {
|
|
@@ -47,49 +33,6 @@ function parseArgs(raw) {
|
|
|
47
33
|
return null;
|
|
48
34
|
}
|
|
49
35
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Thrashing 检测:同一工具 + 完全相同 arguments 在本轮重复 ≥ THRASH_THRESHOLD 次,
|
|
52
|
-
* 返一段提示(注入到工具结果尾部),引导模型换思路而不是再试一次。
|
|
53
|
-
* 阈值 2 = "试过两次同样的调用还没好,该停了"。指纹 = `${name}\\x00${args}`
|
|
54
|
-
* (直接拼,不哈希——避免热路径开销;args 长度本身有限,内存压力可忽略)。
|
|
55
|
-
* null 表示未触发,不污染输出。
|
|
56
|
-
*/
|
|
57
|
-
const THRASH_THRESHOLD = 2;
|
|
58
|
-
function thrashHint(name, args, count) {
|
|
59
|
-
if (count < THRASH_THRESHOLD)
|
|
60
|
-
return null;
|
|
61
|
-
return (`\n\n[hint] This is call #${count} of \`${name}\` with identical arguments — ` +
|
|
62
|
-
'either failing or returning the same content. STOP retrying and switch strategy:\n' +
|
|
63
|
-
'- read_file / glob → path likely wrong; call `glob` to discover paths, or `ask_human`\n' +
|
|
64
|
-
'- run_command → Windows path-escaping issue; use `read_file` / `glob` with absolute paths instead\n' +
|
|
65
|
-
'- write_file / edit_file CHANGE_CONFLICT → do not resend; read_file the same path and use its latest hash (use null only when read_file says the path is missing)\n' +
|
|
66
|
-
'- edit_file old_string mismatch → re-read the exact region and copy it verbatim\n' +
|
|
67
|
-
'- otherwise → re-read the tool description; the argument shape may be wrong');
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Detect only a strict streak of identical failures. Successful calls, or a
|
|
71
|
-
* different tool/argument pair, end the streak. This keeps intentional phased
|
|
72
|
-
* read_file/glob calls from being mislabeled after the underlying files change.
|
|
73
|
-
*/
|
|
74
|
-
export function createThrashTracker() {
|
|
75
|
-
let lastFailedFingerprint = null;
|
|
76
|
-
let consecutiveFailures = 0;
|
|
77
|
-
return (name, args, succeeded) => {
|
|
78
|
-
if (succeeded) {
|
|
79
|
-
lastFailedFingerprint = null;
|
|
80
|
-
consecutiveFailures = 0;
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
const fingerprint = `${name}\x00${args}`;
|
|
84
|
-
if (fingerprint === lastFailedFingerprint)
|
|
85
|
-
consecutiveFailures += 1;
|
|
86
|
-
else {
|
|
87
|
-
lastFailedFingerprint = fingerprint;
|
|
88
|
-
consecutiveFailures = 1;
|
|
89
|
-
}
|
|
90
|
-
return thrashHint(name, args, consecutiveFailures);
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
36
|
/** 只有显式声明 parallel 且无需权限确认的工具才进入普通并发组。 */
|
|
94
37
|
function isParallelTool(name) {
|
|
95
38
|
const tool = tools.find((candidate) => candidate.name === name);
|
|
@@ -158,86 +101,18 @@ function readDiffContext(tc, parsed) {
|
|
|
158
101
|
}
|
|
159
102
|
return { preWriteOld: null, editStartLine: 1 };
|
|
160
103
|
}
|
|
161
|
-
/** 回灌 tool 结果到 history
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* - pruner 在每个 runAgentCore 实例化一次(本闭包持有),会话级状态。
|
|
166
|
-
* - 开关关闭时 pruner 不创建(零开销、零行为变化)。
|
|
167
|
-
* 出口再经 Lifecycle Engine 做引用追踪:LIVE→REFERENCED→OBSOLETE→STUB 四态。
|
|
168
|
-
* - lifecycle 也在每个 runAgentCore 实例化一次,登记 grep/glob/web_search/web_fetch 等 producer
|
|
169
|
-
* 与 read/edit/write 的 consumer 关系;孤立+老化自动 STUB(观察类工具永不到 STUB)。
|
|
170
|
-
* - 开关关闭时 lifecycle=null 完全跳过。 */
|
|
171
|
-
/** RETRY-01: 把 thrash hint + 反思指针一次性拼到 output 尾部。
|
|
172
|
-
* - thrash hint 永远可能存在(连续失败才出现);
|
|
173
|
-
* - 反思指针仅在 error/denied 状态追加,success 跳过避免噪声。
|
|
174
|
-
* 抽出 helper 是为了 sequential + parallel 两处共用,避免拼装逻辑漂移。
|
|
175
|
-
* 返回追加后的字符串 + 反思 category(用于 QUAL-01 trace 硬事件),
|
|
176
|
-
* success / aborted 时 category === null,调用方据此决定是否 emit 'retry_reflection' 事件。 */
|
|
177
|
-
function appendRetryAnnotations(output, status, code, thrashHint) {
|
|
178
|
-
let result = thrashHint ? `${output}${thrashHint}` : output;
|
|
179
|
-
let reflectionCategory = null;
|
|
180
|
-
if (status !== 'success' && status !== 'aborted') {
|
|
181
|
-
reflectionCategory = classifyError(code);
|
|
182
|
-
const reflection = reflectionHint(reflectionCategory);
|
|
183
|
-
result = `${result}\n\n[retry reflection: ${reflectionCategory}]\n${reflection}`;
|
|
184
|
-
}
|
|
185
|
-
return { output: result, reflectionCategory };
|
|
186
|
-
}
|
|
187
|
-
/** ASK-01: ask_human 工具调用本 turn 计数器。纯函数无副作用,
|
|
188
|
-
* 计数规则简单:1-based(本 turn 第 1 次 = 1, 第 2 次 = 2, 第 3+ 次 = exceeded)。
|
|
189
|
-
* 预算上限常量与工作纪律段 "Budget: at most 2 ask_human calls per turn" 保持一致;
|
|
190
|
-
* 改这里时,fixture 与纪律段都要同步。 */
|
|
191
|
-
export const ASK_HUMAN_PER_TURN_BUDGET = 2;
|
|
192
|
-
export function askHumanBudgetAnnotation(askHumanCountThisTurn, status) {
|
|
193
|
-
// 工具失败 / aborted 不追加(避免噪声;模型已被错误消息告知失败)。
|
|
194
|
-
if (status !== 'success')
|
|
195
|
-
return null;
|
|
196
|
-
if (askHumanCountThisTurn < ASK_HUMAN_PER_TURN_BUDGET)
|
|
197
|
-
return null;
|
|
198
|
-
if (askHumanCountThisTurn === ASK_HUMAN_PER_TURN_BUDGET) {
|
|
199
|
-
return `\n\n[ask budget] This was your ${askHumanCountThisTurn}nd ask_human call this turn (budget = ${ASK_HUMAN_PER_TURN_BUDGET}). For the rest of this turn, prefer the safer default and disclose the choice in your final reply — do not silently guess.`;
|
|
200
|
-
}
|
|
201
|
-
// 第 3+ 次(超过预算):强硬提示,鼓励模型停下问自己是否还有意义。
|
|
202
|
-
return `\n\n[ask budget EXCEEDED] This is ask_human call #${askHumanCountThisTurn} this turn (budget = ${ASK_HUMAN_PER_TURN_BUDGET}). Stop asking; choose a default, implement, and disclose the choice in your final reply. Continuing to ask is more harmful than a documented guess.`;
|
|
203
|
-
}
|
|
204
|
-
/** NARR-01: 工具轮旁白(assistant 消息同时带 content + tool_calls)的软预算。
|
|
205
|
-
* 超出即追加回压提示;未超只 emit trace(可度量,不打扰)。
|
|
206
|
-
* 单位是 code point 而非字节——中文一字一 point,英文一字母一 point,
|
|
207
|
-
* 对两种语言都是"一句话大约多长"的直觉量级。 */
|
|
208
|
-
export const NARRATION_CHAR_BUDGET = 120;
|
|
209
|
-
/** 分类工具轮旁白。返回 null 表示这一轮没有旁白(纯工具调用,理想情况)。
|
|
210
|
-
* hint 仅在超预算时非 null:prompt 里的"工具轮保持静默"是软约束,
|
|
211
|
-
* 这里给出机制层面的回压,同时让 trace 能统计旁白率。 */
|
|
212
|
-
export function classifyNarration(content, toolCallCount) {
|
|
213
|
-
const text = content?.trim() ?? '';
|
|
214
|
-
if (!text)
|
|
215
|
-
return null;
|
|
216
|
-
const chars = [...text].length;
|
|
217
|
-
if (chars <= NARRATION_CHAR_BUDGET) {
|
|
218
|
-
return { chars, overBudget: false, hint: null };
|
|
219
|
-
}
|
|
220
|
-
const plural = toolCallCount === 1 ? '' : 's';
|
|
221
|
-
return {
|
|
222
|
-
chars,
|
|
223
|
-
overBudget: true,
|
|
224
|
-
hint: `\n\n[narration] Your previous message emitted ${chars} characters of prose alongside ` +
|
|
225
|
-
`${toolCallCount} tool call${plural} (soft budget = ${NARRATION_CHAR_BUDGET}). ` +
|
|
226
|
-
'Interstitial commentary costs the user screen space and costs you context on every ' +
|
|
227
|
-
'later step. For the rest of this turn, emit prose mid-turn ONLY to (a) flag a genuine ' +
|
|
228
|
-
'decision fork needing user input, (b) disclose an error or risk, or (c) deliver the ' +
|
|
229
|
-
'final answer once tools are done. Otherwise call the next tool with no preamble.',
|
|
230
|
-
};
|
|
231
|
-
}
|
|
104
|
+
/** 回灌 tool 结果到 history。
|
|
105
|
+
* 正常路径只做单条 hard cap;原始 output 同时供 TUI 展示,因此用户与模型
|
|
106
|
+
* 看到同一事实。Artifact/Relevance/Lifecycle 仅登记 metadata/provenance,
|
|
107
|
+
* 不在这里改写旧正文;所有自动清理与压缩统一由 80% pressure scheduler 决定。 */
|
|
232
108
|
function pushToolResult(history, tc, output, pruner, lifecycle, _scheduler, runtimeContextState = contextState, succeededOverride) {
|
|
233
109
|
const succeeded = succeededOverride ?? isToolResultSuccess(output);
|
|
234
|
-
const ageAware = config.contextOptimize ? ageAwareStateFor(history) : null;
|
|
235
|
-
const encodingContext = ageAware?.preparePush(tc, succeeded);
|
|
236
110
|
const msg = {
|
|
237
111
|
role: 'tool',
|
|
238
112
|
tool_call_id: tc.id,
|
|
239
|
-
//
|
|
240
|
-
|
|
113
|
+
// Preserve evidence verbatim in normal operation; the hard per-result cap
|
|
114
|
+
// remains solely as a request-size safety rail.
|
|
115
|
+
content: capToolResultForHistory(tc.name, output),
|
|
241
116
|
};
|
|
242
117
|
history.push(msg);
|
|
243
118
|
const messageIndex = history.length - 1;
|
|
@@ -253,8 +128,8 @@ function pushToolResult(history, tc, output, pruner, lifecycle, _scheduler, runt
|
|
|
253
128
|
* agent 核心循环(纯逻辑):
|
|
254
129
|
* 流式调 LLM(经 hooks.onText 实时渲染)→ 有 tool_calls 就分组执行并回灌
|
|
255
130
|
* → 否则流式正文即最终回复。history 在调用间持久,由调用方持有。
|
|
256
|
-
*
|
|
257
|
-
*
|
|
131
|
+
* 步前由 session scheduler 检查真实 context pressure;达到 80% 时统一清理并压缩历史。
|
|
132
|
+
* 工具结果正常只经 capToolResultForHistory 的单条 hard safety cap。
|
|
258
133
|
*
|
|
259
134
|
* 中断语义:signal 经 executeTool(name, args, signal) 串进工具;run_command/web_fetch 等 abort 即时杀
|
|
260
135
|
* (树杀子进程 / 取消 fetch),循环顶 if(signal.aborted) 兜底还原。不会留下未配对的 tool_call_id。
|
|
@@ -267,25 +142,8 @@ function pushToolResult(history, tc, output, pruner, lifecycle, _scheduler, runt
|
|
|
267
142
|
export async function runAgentCore(opts) {
|
|
268
143
|
const { history, userInput, signal, onContextUpdate, hooks } = opts;
|
|
269
144
|
const runtimeContextState = opts.contextState ?? contextState;
|
|
270
|
-
|
|
271
|
-
// 预算超过时,工具结果尾部追加 ask budget 提示,不直接拒绝调用(更轻量,
|
|
272
|
-
// 也避免和现有 permission 系统的"拒绝"语义重叠)。
|
|
145
|
+
/** 本轮 ask_human 成功调用次数,仅用于 trace 观测,不影响工具执行或模型上下文。 */
|
|
273
146
|
let askHumanCountThisTurn = 0;
|
|
274
|
-
// NARR-01: 上一条 assistant 消息(带 tool_calls)超出旁白预算时,待注入的回压提示。
|
|
275
|
-
// 只挂在本批次的第一条工具结果上并立即清空——重复注入会变成新的噪声源。
|
|
276
|
-
let pendingNarrationHint = null;
|
|
277
|
-
/** 取出并清空待注入的旁白回压提示(每批工具只消费一次)。 */
|
|
278
|
-
const takeNarrationHint = () => {
|
|
279
|
-
const hint = pendingNarrationHint;
|
|
280
|
-
pendingNarrationHint = null;
|
|
281
|
-
return hint;
|
|
282
|
-
};
|
|
283
|
-
// PROMPT-02: 解析 preCompletionChecklist 选项。undefined = 启用默认 middleware;
|
|
284
|
-
// false = opt-out(checklist 自身调试用);function = 调用方自定义。
|
|
285
|
-
const _checklistMiddleware = createPreCompletionChecklistMiddleware();
|
|
286
|
-
const preCompletionChecklistHandler = opts.preCompletionChecklist === false
|
|
287
|
-
? null
|
|
288
|
-
: (opts.preCompletionChecklist ?? _checklistMiddleware.handler);
|
|
289
147
|
const maxSteps = opts.maxSteps ?? config.maxSteps;
|
|
290
148
|
// 中断还原:repl 的 /plan / /auto / Shift+Tab 等用户面触发 setAgentMode 中途切了模式,
|
|
291
149
|
// abort 时连同模式一起还原回轮首。模型不再持有 switch_mode 工具,无法自切。
|
|
@@ -318,14 +176,13 @@ export async function runAgentCore(opts) {
|
|
|
318
176
|
let done = false; // 正常完毕 / 达上限 true;中断 false(不显摘要)
|
|
319
177
|
let traceStatus = 'error';
|
|
320
178
|
let toolCallCount = 0;
|
|
321
|
-
let latestValidation;
|
|
322
|
-
const initialMutationVersion = getCurrentTurnMutationState().version;
|
|
323
|
-
let validatedMutationVersion = initialMutationVersion;
|
|
324
|
-
let fullyValidatedMutationVersion = initialMutationVersion;
|
|
325
|
-
const validator = opts.validator ?? createAutomaticValidator();
|
|
326
179
|
// 本轮 token 累计:每步 chat() 返回后把 result.usage 累加,供 onDone 摘要行 + AgentRunResult.usage
|
|
327
180
|
// 透传给 repl(显示在底栏模式 chip 右边)。未开启 include_usage 或全失败时为 undefined。
|
|
328
181
|
let turnUsage;
|
|
182
|
+
// 实时 chip ↻ 估算用:上一步 chat 实测 prompt(前缀缓存下当前步命中 ≈ 它)+ 后端是否报过 cache 命中
|
|
183
|
+
// (从不报 cache 的后端不估算,避免虚显 ↻)。
|
|
184
|
+
let lastStepPromptTokens = 0;
|
|
185
|
+
let providerCacheSeen = false;
|
|
329
186
|
const addUsage = (u) => {
|
|
330
187
|
if (!u)
|
|
331
188
|
return;
|
|
@@ -340,48 +197,30 @@ export async function runAgentCore(opts) {
|
|
|
340
197
|
: u;
|
|
341
198
|
};
|
|
342
199
|
const addToolUsage = (outcome) => addUsage(outcome.usage);
|
|
343
|
-
// Only consecutive identical failures are thrashing. Any success (including
|
|
344
|
-
// a mutation between two reads) or different call resets the streak.
|
|
345
|
-
const recordAndHint = createThrashTracker();
|
|
346
200
|
history.push({ role: 'user', content: userInput });
|
|
347
201
|
// 中断回滚快照:push 用户消息后整段浅拷贝。abort 时 length=0;push(...saved) 还原。
|
|
348
202
|
// 这样中断时至少保留用户消息(及之前的历史);每步工具全部执行完毕后刷新快照,
|
|
349
203
|
// 保留已完成的 assistant+tool_calls+tool 结果,只丢弃当前未完成步骤的消息。
|
|
350
204
|
// 用 slice() 而非 length:maybeCompact 会原地重建(length=0;push(...rebuilt)),savedLen 会失效。
|
|
351
205
|
let savedHistory = history.slice();
|
|
352
|
-
//
|
|
353
|
-
//
|
|
354
|
-
// 子 agent 也在自己的 history 上操作(子 agent 独立 history),文件回滚事务则与主轮共享。
|
|
355
|
-
const dropContext = (filter) => dropContextFromHistory(history, filter);
|
|
356
|
-
// 相关性裁剪 pruner:每个 runAgentCore 实例一个,纯静态、不调 LLM、自动判定 read_file 失效。
|
|
357
|
-
// 开关关闭时为 null,所有 pushToolResult 调用走无 pruner 路径(零行为变化)。
|
|
206
|
+
// Relevance and lifecycle collect provenance during normal work. Neither path
|
|
207
|
+
// rewrites history; exact supersession is applied only by the pressure scheduler.
|
|
358
208
|
const relprune = config.contextRelprune ? createRelevancePruner() : null;
|
|
359
|
-
// 观察者生命周期引擎:每个 runAgentCore 实例一个,纯静态、自动维护 grep/glob/web_search/web_fetch 等
|
|
360
|
-
// producer 与 read/edit/write 的 consumer 引用关系;孤立+老化的非观察类工具自动 STUB。
|
|
361
|
-
// 开关关闭时为 null,所有 pushToolResult / mutation 调用走无 lifecycle 路径(零行为变化)。
|
|
362
|
-
// 引擎需要从已有会话 history 恢复观察结果的年龄和 path 索引;不能只追踪本次
|
|
363
|
-
// runAgentCore,否则跨用户轮次的 grep/glob 永远不会衰减。
|
|
364
209
|
let lifecycle = config.contextLifecycle
|
|
365
210
|
? createLifecycleEngine(history)
|
|
366
211
|
: null;
|
|
367
212
|
runtimeContextState.lifecycleStats = lifecycle?.stats();
|
|
368
213
|
rehydrateArtifacts(runtimeContextState, history);
|
|
369
|
-
//
|
|
370
|
-
//
|
|
214
|
+
// The scheduler is the sole automatic history-rewrite entry point. It runs
|
|
215
|
+
// superseded → stale artifact → old logs/search → compact at real pressure.
|
|
216
|
+
// contextBudget=false keeps only the infrastructure compact fallback.
|
|
371
217
|
const scheduler = config.contextBudget !== false
|
|
372
|
-
? createBudgetScheduler(runtimeContextState)
|
|
218
|
+
? createBudgetScheduler(runtimeContextState)
|
|
373
219
|
: null;
|
|
374
|
-
// age-aware encoder 与 history 数组同寿命;每轮重建一次以覆盖外部 /compact 等原地修改。
|
|
375
|
-
const ageAware = config.contextOptimize ? ageAwareStateFor(history) : null;
|
|
376
|
-
ageAware?.rehydrate(history);
|
|
377
220
|
// 本轮流式状态:首个正文 token 到达即停 spinner(思考期间 spinner 持续转「思考中…」,不写思考内容)。
|
|
378
221
|
let mode = 'idle';
|
|
379
222
|
let gotText = false;
|
|
380
223
|
let lastChar = '';
|
|
381
|
-
// 早退重探:本 turn 已执行过工具但模型突然返回无工具调用 + 空文本 → 推一条提示让模型继续。
|
|
382
|
-
// 短回复也可能是合法完成结果,不再按字符数误判;每 turn 最多触发 1 次以防死循环。
|
|
383
|
-
let nudgeCount = 0;
|
|
384
|
-
let hadToolsThisTurn = false;
|
|
385
224
|
const onText = (s) => {
|
|
386
225
|
hooks.onText?.(s); // 主 agent:走 markdown 渲染写内容区
|
|
387
226
|
mode = 'text';
|
|
@@ -408,7 +247,6 @@ export async function runAgentCore(opts) {
|
|
|
408
247
|
hooks.onAbort?.();
|
|
409
248
|
history.length = 0;
|
|
410
249
|
history.push(...savedHistory);
|
|
411
|
-
ageAware?.rehydrate(history);
|
|
412
250
|
setAgentMode(savedMode);
|
|
413
251
|
};
|
|
414
252
|
try {
|
|
@@ -427,7 +265,6 @@ export async function runAgentCore(opts) {
|
|
|
427
265
|
terminationReason: 'aborted',
|
|
428
266
|
finalText: null,
|
|
429
267
|
usage: turnUsage,
|
|
430
|
-
validation: latestValidation,
|
|
431
268
|
changedFiles: mutation.changedFiles.map((item) => item.path),
|
|
432
269
|
};
|
|
433
270
|
}
|
|
@@ -439,10 +276,9 @@ export async function runAgentCore(opts) {
|
|
|
439
276
|
const storedCalibration = getTokenCalibration(requestBaseURL, requestModel, activeTools);
|
|
440
277
|
runtimeContextState.correction = storedCalibration.correction;
|
|
441
278
|
runtimeContextState.calibrationSamples = storedCalibration.samples;
|
|
442
|
-
//
|
|
443
|
-
//
|
|
444
|
-
|
|
445
|
-
// 步前:五区 Budget Scheduler 在优化后的 history 上决策;开关关闭时退化回原 maybeCompact 路径。
|
|
279
|
+
// The scheduler is the only automatic path that may compress old evidence.
|
|
280
|
+
// Normal tool pushes and lifecycle tracking remain metadata-only.
|
|
281
|
+
// 步前:五区 Budget Scheduler 在当前完整 history 上决策;开关关闭时退化回 maybeCompact 路径。
|
|
446
282
|
// 此时 spinner 已停,通知行干净。
|
|
447
283
|
let historyRebuilt = false;
|
|
448
284
|
const compactStartedAt = Date.now();
|
|
@@ -478,7 +314,6 @@ export async function runAgentCore(opts) {
|
|
|
478
314
|
lifecycle = createLifecycleEngine(history);
|
|
479
315
|
runtimeContextState.lifecycleStats = lifecycle.stats();
|
|
480
316
|
}
|
|
481
|
-
ageAware?.rehydrate(history);
|
|
482
317
|
rehydrateArtifacts(runtimeContextState, history);
|
|
483
318
|
}
|
|
484
319
|
hooks.onStepStart?.(); // 主 agent:spinner.start('思考中')
|
|
@@ -489,12 +324,9 @@ export async function runAgentCore(opts) {
|
|
|
489
324
|
const modelStartedAt = Date.now();
|
|
490
325
|
const provider = safeProviderId(requestBaseURL);
|
|
491
326
|
emitTrace('model_start', { model: requestModel, provider });
|
|
492
|
-
const dynamicSystemSuffix =
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
? '## Post-compaction recovery\nContext was compacted before this request. Re-establish the current objective and unresolved work from retained evidence or the session note, avoid repeating completed investigation, and re-read exact file context before any dependent edit.'
|
|
496
|
-
: '',
|
|
497
|
-
].filter(Boolean).join('\n\n');
|
|
327
|
+
const dynamicSystemSuffix = historyRebuilt
|
|
328
|
+
? '## Post-compaction recovery\nContext was compacted before this request. Re-establish the current objective and unresolved work from retained evidence or the session note, avoid repeating completed investigation, and re-read exact file context before any dependent edit.'
|
|
329
|
+
: '';
|
|
498
330
|
const systemMessage = history[0];
|
|
499
331
|
const requestHistory = dynamicSystemSuffix
|
|
500
332
|
&& systemMessage?.role === 'system'
|
|
@@ -507,10 +339,32 @@ export async function runAgentCore(opts) {
|
|
|
507
339
|
...history.slice(1),
|
|
508
340
|
]
|
|
509
341
|
: history;
|
|
342
|
+
// 实时用量:当前步 prompt 估算(含校准系数)+ 流式累计 completion 估算,
|
|
343
|
+
// 叠上已完成步的实测 turnUsage,经 onLiveUsage 推给底栏实时 chip。
|
|
344
|
+
// turnUsage 在闭包里被 addUsage 原地更新,reportLive 每次调用读最新值。
|
|
345
|
+
const stepPromptEst = estimatePromptTokens(requestHistory, activeTools, runtimeContextState.correction);
|
|
346
|
+
const reportLive = (p) => {
|
|
347
|
+
// 当前步 prompt:末尾 usage chunk 到达后用实测,流式期间用估算(含校准)。
|
|
348
|
+
// 当前步 cache 命中同理:上报即用实测;流式期间按前缀缓存估算 ≈ 上一步实测 prompt
|
|
349
|
+
// (当前 prompt 总含其为前缀),不超过当前步 prompt;后端从不报 cache 时不估算。
|
|
350
|
+
// 口径与轮末摘要一致:chip ↑ 显计费 prompt(裸 - cached),↓/↻ 同。
|
|
351
|
+
const curPrompt = p.promptTokens ?? stepPromptEst;
|
|
352
|
+
const curCached = p.cachedTokens ?? (providerCacheSeen
|
|
353
|
+
? Math.min(lastStepPromptTokens, curPrompt)
|
|
354
|
+
: 0);
|
|
355
|
+
hooks.onLiveUsage?.({
|
|
356
|
+
promptTokens: (turnUsage?.promptTokens ?? 0) + curPrompt,
|
|
357
|
+
completionTokens: (turnUsage?.completionTokens ?? 0) + p.completionTokens,
|
|
358
|
+
totalTokens: (turnUsage?.totalTokens ?? 0) + curPrompt + p.completionTokens,
|
|
359
|
+
cachedTokens: (turnUsage?.cachedTokens ?? 0) + curCached,
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
reportLive({ completionTokens: 0 }); // 思考阶段先显 ↑ prompt 估算,首 token 到达后 ↓ 开始涨
|
|
510
363
|
try {
|
|
511
364
|
result = await chat(requestHistory, {
|
|
512
365
|
onText,
|
|
513
366
|
onToolCall,
|
|
367
|
+
onProgress: reportLive,
|
|
514
368
|
onRetry: (retry) => emitTrace('model_retry', {
|
|
515
369
|
model: requestModel,
|
|
516
370
|
provider,
|
|
@@ -547,7 +401,6 @@ export async function runAgentCore(opts) {
|
|
|
547
401
|
terminationReason: 'aborted',
|
|
548
402
|
finalText: null,
|
|
549
403
|
usage: turnUsage,
|
|
550
|
-
validation: latestValidation,
|
|
551
404
|
changedFiles: mutation.changedFiles.map((item) => item.path),
|
|
552
405
|
};
|
|
553
406
|
}
|
|
@@ -566,6 +419,11 @@ export async function runAgentCore(opts) {
|
|
|
566
419
|
});
|
|
567
420
|
runtimeContextState.lastUsage = result.usage; // 供 /context 与状态行显示实测 token
|
|
568
421
|
addUsage(result.usage); // 本轮累计:onDone 摘要行 + AgentRunResult.usage 透传
|
|
422
|
+
if (result.usage) {
|
|
423
|
+
lastStepPromptTokens = result.usage.promptTokens; // 下一步流式期 ↻ 估算的前缀基准
|
|
424
|
+
if (result.usage.cachedTokens > 0)
|
|
425
|
+
providerCacheSeen = true;
|
|
426
|
+
}
|
|
569
427
|
// 用本次实际发送的 tools 计算分母,再以 EWMA 更新 provider/model/tool-set 校准。
|
|
570
428
|
// 只持久化比例与样本数;无 usage 或短 prompt 时保持既有值。
|
|
571
429
|
if (result.usage?.promptTokens && result.usage.promptTokens > 100) {
|
|
@@ -579,7 +437,6 @@ export async function runAgentCore(opts) {
|
|
|
579
437
|
onContextUpdate?.();
|
|
580
438
|
if (result.toolCalls.length > 0) {
|
|
581
439
|
toolCallCount += result.toolCalls.length;
|
|
582
|
-
hadToolsThisTurn = true;
|
|
583
440
|
// 流式正文末尾补换行(若 onToolCall 已补则 lastChar='\n',此处 no-op);防 ● 行黏在正文行尾
|
|
584
441
|
if (mode !== 'idle' && lastChar !== '\n')
|
|
585
442
|
hooks.onTextEnd?.();
|
|
@@ -593,21 +450,15 @@ export async function runAgentCore(opts) {
|
|
|
593
450
|
function: { name: tc.name, arguments: tc.arguments },
|
|
594
451
|
})),
|
|
595
452
|
});
|
|
596
|
-
//
|
|
597
|
-
//
|
|
598
|
-
|
|
599
|
-
// - 超预算时把回压提示挂到本批第一条工具结果尾部(复用 thrash/ask
|
|
600
|
-
// budget 已验证的注入接缝,不新增消息、不打断工具流)。
|
|
601
|
-
const narration = classifyNarration(result.content, result.toolCalls.length);
|
|
453
|
+
// Record interstitial narration for observability only. It never changes tool output
|
|
454
|
+
// or injects instructions back into the model context.
|
|
455
|
+
const narration = result.content?.trim() ?? '';
|
|
602
456
|
if (narration) {
|
|
603
457
|
emitTrace('narration', {
|
|
604
|
-
chars: narration.
|
|
458
|
+
chars: [...narration].length,
|
|
605
459
|
toolCalls: result.toolCalls.length,
|
|
606
|
-
budget: NARRATION_CHAR_BUDGET,
|
|
607
|
-
overBudget: narration.overBudget,
|
|
608
460
|
step,
|
|
609
461
|
});
|
|
610
|
-
pendingNarrationHint = narration.hint;
|
|
611
462
|
}
|
|
612
463
|
// 工具分组执行(保 tool_calls 原顺序):safe parallel 工具照常并发;连续
|
|
613
464
|
// resource-locked mutation 先按序完成权限预检,再按 canonical resource lock 启动。
|
|
@@ -615,6 +466,7 @@ export async function runAgentCore(opts) {
|
|
|
615
466
|
// 串行工具仍是本调用列表内的屏障;渲染/history 回灌始终按原 tool_calls 顺序。
|
|
616
467
|
// executeToolOutcome 永不抛错,失败通过结构化 status/code 返回。
|
|
617
468
|
const calls = result.toolCalls;
|
|
469
|
+
const modelAttachments = [];
|
|
618
470
|
const tracedCalls = calls.map((tc, index) => ({
|
|
619
471
|
toolCallId: `${traceTurnId}:step:${step}:tool:${index}`,
|
|
620
472
|
args: summarizeToolArguments(tc.arguments),
|
|
@@ -626,28 +478,15 @@ export async function runAgentCore(opts) {
|
|
|
626
478
|
tool: tc.name,
|
|
627
479
|
argumentHash: traceCall.args.sha256,
|
|
628
480
|
arguments: traceCall.args,
|
|
629
|
-
attempt: 1,
|
|
630
|
-
retry: 0,
|
|
631
481
|
}, {
|
|
632
482
|
toolCallId: traceCall.toolCallId,
|
|
633
483
|
...(tc.id ? { providerToolCallId: tc.id } : {}),
|
|
634
484
|
});
|
|
635
485
|
}
|
|
636
|
-
const traceToolRetry = (tc, index, retry) => {
|
|
637
|
-
const traceCall = tracedCalls[index];
|
|
638
|
-
emitTrace('tool_retry', {
|
|
639
|
-
tool: tc.name,
|
|
640
|
-
argumentHash: traceCall.args.sha256,
|
|
641
|
-
attempt: retry.attempt,
|
|
642
|
-
nextAttempt: retry.nextAttempt,
|
|
643
|
-
waitMs: retry.waitMs,
|
|
644
|
-
code: retry.code,
|
|
645
|
-
}, {
|
|
646
|
-
toolCallId: traceCall.toolCallId,
|
|
647
|
-
...(tc.id ? { providerToolCallId: tc.id } : {}),
|
|
648
|
-
});
|
|
649
|
-
};
|
|
650
486
|
const traceToolEnd = (tc, index, outcome) => {
|
|
487
|
+
if (outcome.status === 'success' && outcome.modelAttachments?.length) {
|
|
488
|
+
modelAttachments.push(...outcome.modelAttachments);
|
|
489
|
+
}
|
|
651
490
|
const traceCall = tracedCalls[index];
|
|
652
491
|
emitTrace('tool_call_end', {
|
|
653
492
|
tool: tc.name,
|
|
@@ -656,9 +495,6 @@ export async function runAgentCore(opts) {
|
|
|
656
495
|
code: outcome.code,
|
|
657
496
|
retryable: outcome.retryable,
|
|
658
497
|
durationMs: outcome.durationMs ?? 0,
|
|
659
|
-
attempt: outcome.attempts ?? 1,
|
|
660
|
-
retry: Math.max(0, (outcome.attempts ?? 1) - 1),
|
|
661
|
-
retryDelayMs: outcome.retryDelayMs ?? 0,
|
|
662
498
|
changedFiles: outcome.changedFiles ?? [],
|
|
663
499
|
staleFiles: outcome.staleFiles ?? [],
|
|
664
500
|
...(outcome.changeSet ? { changeSet: outcome.changeSet } : {}),
|
|
@@ -683,8 +519,7 @@ export async function runAgentCore(opts) {
|
|
|
683
519
|
durationMs: 0,
|
|
684
520
|
};
|
|
685
521
|
hooks.onToolResult?.(currentCall, error, null, null, 1);
|
|
686
|
-
|
|
687
|
-
pushToolResult(history, currentCall, hint ? `${error}${hint}` : error, relprune, lifecycle, scheduler, runtimeContextState, false);
|
|
522
|
+
pushToolResult(history, currentCall, error, relprune, lifecycle, scheduler, runtimeContextState, false);
|
|
688
523
|
traceToolEnd(currentCall, i, outcome);
|
|
689
524
|
i++;
|
|
690
525
|
continue;
|
|
@@ -702,10 +537,7 @@ export async function runAgentCore(opts) {
|
|
|
702
537
|
for (const tc of batch)
|
|
703
538
|
hooks.onToolHeader?.(tc);
|
|
704
539
|
hooks.onToolStart?.(batch[0].name);
|
|
705
|
-
const started = batch.map((tc
|
|
706
|
-
dropContext,
|
|
707
|
-
onRetry: (retry) => traceToolRetry(tc, i + offset, retry),
|
|
708
|
-
}));
|
|
540
|
+
const started = batch.map((tc) => executeToolOutcome(tc.name, tc.arguments, signal));
|
|
709
541
|
for (let k = 0; k < batch.length; k++) {
|
|
710
542
|
const tc = batch[k];
|
|
711
543
|
const outcome = await started[k];
|
|
@@ -714,34 +546,15 @@ export async function runAgentCore(opts) {
|
|
|
714
546
|
traceToolEnd(tc, i + k, outcome);
|
|
715
547
|
const output = outcome.output;
|
|
716
548
|
hooks.onToolResult?.(tc, output, null, null, 1); // 并行工具无 diff
|
|
717
|
-
// Thrashing:history 里附 hint(UI 已用干净 output 渲染,避免屏幕噪声)
|
|
718
|
-
const hint = recordAndHint(tc.name, tc.arguments, outcome.status === 'success');
|
|
719
|
-
const { output: annotated, reflectionCategory } = appendRetryAnnotations(output, outcome.status, outcome.code, hint);
|
|
720
|
-
// QUAL-01 trace 硬事件:反思指针注入计数(history 文本扫描会因 LLM 在
|
|
721
|
-
// 正文里提一句 "[retry reflection]" 而误算,改用 core 接缝处显式 emit)。
|
|
722
|
-
if (reflectionCategory !== null) {
|
|
723
|
-
emitTrace('retry_reflection', {
|
|
724
|
-
tool: tc.name,
|
|
725
|
-
code: outcome.code,
|
|
726
|
-
category: reflectionCategory,
|
|
727
|
-
attempt: outcome.attempts ?? 1,
|
|
728
|
-
}, tc.id ? { providerToolCallId: tc.id } : {});
|
|
729
|
-
}
|
|
730
|
-
// ASK-01: 计数 + 预算提示
|
|
731
549
|
if (tc.name === 'ask_human' && outcome.status === 'success') {
|
|
732
550
|
askHumanCountThisTurn += 1;
|
|
733
|
-
// QUAL-01 trace 硬事件:ask_human 真实调用计数(取代 tool_call_end.name 推断)。
|
|
734
551
|
emitTrace('ask_human_call', {
|
|
735
552
|
tool: tc.name,
|
|
736
553
|
status: outcome.status,
|
|
737
554
|
perTurnCount: askHumanCountThisTurn,
|
|
738
555
|
}, tc.id ? { providerToolCallId: tc.id } : {});
|
|
739
556
|
}
|
|
740
|
-
|
|
741
|
-
// NARR-01: 旁白回压提示只挂本批第一条结果(takeNarrationHint 自清空)。
|
|
742
|
-
const narrationHint = takeNarrationHint();
|
|
743
|
-
const finalOutput = `${annotated}${askBudget ?? ''}${narrationHint ?? ''}`;
|
|
744
|
-
pushToolResult(history, tc, finalOutput, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
557
|
+
pushToolResult(history, tc, output, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
745
558
|
}
|
|
746
559
|
hooks.onToolDone?.();
|
|
747
560
|
i = j;
|
|
@@ -794,14 +607,12 @@ export async function runAgentCore(opts) {
|
|
|
794
607
|
const firstAllowed = entries.find((entry) => !entry.denied);
|
|
795
608
|
if (firstAllowed)
|
|
796
609
|
hooks.onToolStart?.(firstAllowed.tc.name);
|
|
797
|
-
const started = entries.map((entry
|
|
610
|
+
const started = entries.map((entry) => entry.denied
|
|
798
611
|
? Promise.resolve(entry.denied)
|
|
799
612
|
: executeToolOutcome(entry.tc.name, entry.tc.arguments, signal, {
|
|
800
|
-
dropContext,
|
|
801
613
|
onLockAcquired: (lockedArgs) => {
|
|
802
614
|
entry.diff = readDiffContext(entry.tc, lockedArgs);
|
|
803
615
|
},
|
|
804
|
-
onRetry: (retry) => traceToolRetry(entry.tc, i + offset, retry),
|
|
805
616
|
}));
|
|
806
617
|
for (let k = 0; k < entries.length; k++) {
|
|
807
618
|
const entry = entries[k];
|
|
@@ -810,8 +621,7 @@ export async function runAgentCore(opts) {
|
|
|
810
621
|
opts.onToolOutcome?.(entry.tc.name, entry.parsed ?? {}, outcome);
|
|
811
622
|
traceToolEnd(entry.tc, i + k, outcome);
|
|
812
623
|
hooks.onToolResult?.(entry.tc, outcome.output, entry.denied ? null : entry.parsed, entry.diff.preWriteOld, entry.diff.editStartLine);
|
|
813
|
-
|
|
814
|
-
pushToolResult(history, entry.tc, hint ? `${outcome.output}${hint}` : outcome.output, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
624
|
+
pushToolResult(history, entry.tc, outcome.output, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
815
625
|
const invalidatedFiles = [...new Set([
|
|
816
626
|
...(outcome.changedFiles ?? []),
|
|
817
627
|
...(outcome.staleFiles ?? []),
|
|
@@ -846,9 +656,7 @@ export async function runAgentCore(opts) {
|
|
|
846
656
|
durationMs: 0,
|
|
847
657
|
};
|
|
848
658
|
hooks.onToolResult?.(tc, err, null, null, 1);
|
|
849
|
-
|
|
850
|
-
const hint = recordAndHint(tc.name, tc.arguments, false);
|
|
851
|
-
pushToolResult(history, tc, hint ? `${err}${hint}` : err, relprune, lifecycle, scheduler);
|
|
659
|
+
pushToolResult(history, tc, err, relprune, lifecycle, scheduler);
|
|
852
660
|
traceToolEnd(tc, i, outcome);
|
|
853
661
|
i++;
|
|
854
662
|
continue;
|
|
@@ -877,8 +685,7 @@ export async function runAgentCore(opts) {
|
|
|
877
685
|
hooks.onToolHeader?.(tc);
|
|
878
686
|
const outcome = deniedOutcome(tc.name);
|
|
879
687
|
hooks.onToolResult?.(tc, outcome.output, null, null, 1);
|
|
880
|
-
|
|
881
|
-
pushToolResult(history, tc, hint ? `${outcome.output}${hint}` : outcome.output, relprune, lifecycle, scheduler, runtimeContextState, false);
|
|
688
|
+
pushToolResult(history, tc, outcome.output, relprune, lifecycle, scheduler, runtimeContextState, false);
|
|
882
689
|
traceToolEnd(tc, i, outcome);
|
|
883
690
|
i++;
|
|
884
691
|
continue;
|
|
@@ -891,12 +698,10 @@ export async function runAgentCore(opts) {
|
|
|
891
698
|
let diff = readDiffContext(tc, mutationParsed);
|
|
892
699
|
hooks.onToolStart?.(tc.name);
|
|
893
700
|
const outcome = await executeToolOutcome(tc.name, tc.arguments, signal, {
|
|
894
|
-
dropContext,
|
|
895
701
|
onLockAcquired: (lockedArgs) => {
|
|
896
702
|
if (mutationParsed)
|
|
897
703
|
diff = readDiffContext(tc, lockedArgs);
|
|
898
704
|
},
|
|
899
|
-
onRetry: (retry) => traceToolRetry(tc, i, retry),
|
|
900
705
|
});
|
|
901
706
|
addToolUsage(outcome);
|
|
902
707
|
opts.onToolOutcome?.(tc.name, parsed ?? {}, outcome);
|
|
@@ -904,35 +709,15 @@ export async function runAgentCore(opts) {
|
|
|
904
709
|
const output = outcome.output;
|
|
905
710
|
hooks.onToolDone?.();
|
|
906
711
|
hooks.onToolResult?.(tc, output, mutationParsed, diff.preWriteOld, diff.editStartLine);
|
|
907
|
-
// Thrashing:同上(history 附 hint,UI 干净)
|
|
908
|
-
const hint = recordAndHint(tc.name, tc.arguments, outcome.status === 'success');
|
|
909
|
-
// RETRY-01: 同 parallel 路径,在非成功状态追加反思指针。
|
|
910
|
-
const { output: annotated, reflectionCategory } = appendRetryAnnotations(output, outcome.status, outcome.code, hint);
|
|
911
|
-
// QUAL-01 trace 硬事件:反思指针注入计数(history 文本扫描会因 LLM 在
|
|
912
|
-
// 正文里提一句 "[retry reflection]" 而误算,改用 core 接缝处显式 emit)。
|
|
913
|
-
if (reflectionCategory !== null) {
|
|
914
|
-
emitTrace('retry_reflection', {
|
|
915
|
-
tool: tc.name,
|
|
916
|
-
code: outcome.code,
|
|
917
|
-
category: reflectionCategory,
|
|
918
|
-
attempt: outcome.attempts ?? 1,
|
|
919
|
-
}, tc.id ? { providerToolCallId: tc.id } : {});
|
|
920
|
-
}
|
|
921
|
-
// ASK-01: ask_human 计数 + 预算提示(同 parallel 路径)。
|
|
922
712
|
if (tc.name === 'ask_human' && outcome.status === 'success') {
|
|
923
713
|
askHumanCountThisTurn += 1;
|
|
924
|
-
// QUAL-01 trace 硬事件:ask_human 真实调用计数(取代 tool_call_end.name 推断)。
|
|
925
714
|
emitTrace('ask_human_call', {
|
|
926
715
|
tool: tc.name,
|
|
927
716
|
status: outcome.status,
|
|
928
717
|
perTurnCount: askHumanCountThisTurn,
|
|
929
718
|
}, tc.id ? { providerToolCallId: tc.id } : {});
|
|
930
719
|
}
|
|
931
|
-
|
|
932
|
-
// NARR-01: 旁白回压提示只挂本批第一条结果(takeNarrationHint 自清空)。
|
|
933
|
-
const narrationHint = takeNarrationHint();
|
|
934
|
-
const finalOutput = `${annotated}${askBudget ?? ''}${narrationHint ?? ''}`;
|
|
935
|
-
pushToolResult(history, tc, finalOutput, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
720
|
+
pushToolResult(history, tc, output, relprune, lifecycle, scheduler, runtimeContextState, outcome.status === 'success');
|
|
936
721
|
const invalidatedFiles = [...new Set([
|
|
937
722
|
...(outcome.changedFiles ?? []),
|
|
938
723
|
...(outcome.staleFiles ?? []),
|
|
@@ -948,6 +733,29 @@ export async function runAgentCore(opts) {
|
|
|
948
733
|
i++;
|
|
949
734
|
}
|
|
950
735
|
}
|
|
736
|
+
if (modelAttachments.length > 0) {
|
|
737
|
+
const names = modelAttachments.map((attachment) => attachment.name).join(', ');
|
|
738
|
+
const content = [
|
|
739
|
+
{
|
|
740
|
+
type: 'text',
|
|
741
|
+
text: `The view_image tool loaded the following visual input: ${names}. Analyze the attached image content directly.`,
|
|
742
|
+
},
|
|
743
|
+
...modelAttachments.map((attachment) => ({
|
|
744
|
+
type: 'image_url',
|
|
745
|
+
image_url: {
|
|
746
|
+
url: attachment.dataUrl,
|
|
747
|
+
// 不默认补 auto: OpenAI 省略时等同 auto,但 MiniMax 仅接受 low/default/high。
|
|
748
|
+
// 让各 provider 采用默认枚举;仅保留工具明确请求的 low/high。
|
|
749
|
+
...(attachment.detail === 'low' || attachment.detail === 'high'
|
|
750
|
+
? { detail: attachment.detail }
|
|
751
|
+
: {}),
|
|
752
|
+
},
|
|
753
|
+
})),
|
|
754
|
+
];
|
|
755
|
+
// OpenAI tool-call protocol requires every tool result to immediately follow the
|
|
756
|
+
// assistant tool_calls message; append visual input only after the full batch.
|
|
757
|
+
history.push({ role: 'user', content });
|
|
758
|
+
}
|
|
951
759
|
// 工具步末尾补一空行:与下一轮的思考 / 正文分隔(否则 ↳ 后紧接 ▎ 思考,无空行不好看;
|
|
952
760
|
// 与正文→● 的 1 空行对称)。工具结果已以 \n 收尾,此处再补 \n 恰好 1 空行。
|
|
953
761
|
hooks.onToolBatchEnd?.();
|
|
@@ -958,169 +766,20 @@ export async function runAgentCore(opts) {
|
|
|
958
766
|
}
|
|
959
767
|
if (mode !== 'idle' && lastChar !== '\n')
|
|
960
768
|
hooks.onTextEnd?.(); // 流式末尾补换行
|
|
961
|
-
//
|
|
962
|
-
//
|
|
963
|
-
// 早退重探只处理真正的空回复。短回复可能是合法完成结果(例如精确状态标记);
|
|
964
|
-
// 仅凭字符数继续调用会重复输出,并产生一次完整的额外模型请求。
|
|
965
|
-
const replyIsEmpty = (result.content?.trim().length ?? 0) === 0;
|
|
966
|
-
if (hadToolsThisTurn && nudgeCount < 1 && replyIsEmpty) {
|
|
967
|
-
nudgeCount++;
|
|
968
|
-
history.push({ role: 'assistant', content: result.content });
|
|
969
|
-
history.push({
|
|
970
|
-
role: 'user',
|
|
971
|
-
content: 'You stopped before completing the task. Please continue investigating — call more tools if needed, or provide a complete answer based on what you have gathered so far.',
|
|
972
|
-
});
|
|
973
|
-
continue; // 带着提示再调一次 LLM
|
|
974
|
-
}
|
|
975
|
-
// 没有工具调用:候选正文已流式打印。若本轮有新的代码变更,先通过框架验证门;
|
|
976
|
-
// failed 作为 system observation 风格的 user 消息回灌,不能伪造无配对的 tool 消息。
|
|
769
|
+
// 没有工具调用:接受 agent 的完成判断。框架不自动运行测试、构建或完成门,
|
|
770
|
+
// 也不因缺少验证证据强制追加模型轮次;agent 仍可自行调用工具验证。
|
|
977
771
|
if (!gotText)
|
|
978
772
|
hooks.onNoReply?.();
|
|
979
|
-
|
|
980
|
-
const mutationBeforeValidation = getCurrentTurnMutationState();
|
|
981
|
-
const shouldValidate = opts.autoValidate === true &&
|
|
982
|
-
getAgentMode() !== 'plan' &&
|
|
983
|
-
(mutationBeforeValidation.version > validatedMutationVersion || latestValidation?.status === 'failed');
|
|
984
|
-
if (shouldValidate) {
|
|
985
|
-
history.push(candidate);
|
|
986
|
-
emitTrace('validation_start', {
|
|
987
|
-
mutationVersion: mutationBeforeValidation.version,
|
|
988
|
-
changedFiles: mutationBeforeValidation.changedFiles.map((item) => item.path),
|
|
989
|
-
});
|
|
990
|
-
try {
|
|
991
|
-
latestValidation = await validator(signal, {
|
|
992
|
-
onCommandStart: (command) => hooks.onValidationStart?.(command),
|
|
993
|
-
onPermissionDecision: (permission) => {
|
|
994
|
-
const summary = summarizeToolArguments(JSON.stringify(permission.arguments));
|
|
995
|
-
emitTrace('permission', {
|
|
996
|
-
source: 'automatic_validation',
|
|
997
|
-
tool: permission.tool,
|
|
998
|
-
decision: permission.decision,
|
|
999
|
-
argumentHash: summary.sha256,
|
|
1000
|
-
});
|
|
1001
|
-
},
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
catch (error) {
|
|
1005
|
-
const mutation = getCurrentTurnMutationState();
|
|
1006
|
-
const message = `Automatic validation failed to run: ${error instanceof Error ? error.message : String(error)}`;
|
|
1007
|
-
const status = signal?.aborted ? 'aborted' : 'failed';
|
|
1008
|
-
latestValidation = {
|
|
1009
|
-
status,
|
|
1010
|
-
level: 'V0',
|
|
1011
|
-
output: message,
|
|
1012
|
-
durationMs: 0,
|
|
1013
|
-
diagnostics: [{
|
|
1014
|
-
level: 'V0', source: 'verifier', severity: 'error', code: 'VERIFIER_ERROR', message,
|
|
1015
|
-
}],
|
|
1016
|
-
stages: [],
|
|
1017
|
-
verificationComplete: false,
|
|
1018
|
-
fingerprint: `verifier-error-${mutation.version}`,
|
|
1019
|
-
inputFingerprint: `mutation-${mutation.version}`,
|
|
1020
|
-
inputMutationVersion: mutation.version,
|
|
1021
|
-
affectedPackages: [],
|
|
1022
|
-
changedFiles: mutation.changedFiles.map((item) => item.path),
|
|
1023
|
-
mutationVersion: mutation.version,
|
|
1024
|
-
};
|
|
1025
|
-
}
|
|
1026
|
-
emitTrace('validation_end', {
|
|
1027
|
-
status: latestValidation.status,
|
|
1028
|
-
level: latestValidation.level,
|
|
1029
|
-
durationMs: latestValidation.durationMs,
|
|
1030
|
-
verificationComplete: latestValidation.verificationComplete,
|
|
1031
|
-
skipReason: latestValidation.skipReason,
|
|
1032
|
-
fingerprint: latestValidation.fingerprint,
|
|
1033
|
-
mutationVersion: latestValidation.mutationVersion,
|
|
1034
|
-
stages: latestValidation.stages.map((stage) => ({
|
|
1035
|
-
level: stage.level,
|
|
1036
|
-
status: stage.status,
|
|
1037
|
-
adapter: stage.adapter,
|
|
1038
|
-
code: stage.diagnostics[0]?.code,
|
|
1039
|
-
durationMs: stage.durationMs,
|
|
1040
|
-
cached: stage.cached === true,
|
|
1041
|
-
})),
|
|
1042
|
-
});
|
|
1043
|
-
hooks.onValidationResult?.(latestValidation);
|
|
1044
|
-
validatedMutationVersion = latestValidation.mutationVersion;
|
|
1045
|
-
if (latestValidation.status === 'passed' && latestValidation.verificationComplete) {
|
|
1046
|
-
fullyValidatedMutationVersion = latestValidation.mutationVersion;
|
|
1047
|
-
}
|
|
1048
|
-
if (signal?.aborted || latestValidation.status === 'aborted') {
|
|
1049
|
-
abortRestore();
|
|
1050
|
-
traceStatus = 'aborted';
|
|
1051
|
-
const mutation = getCurrentTurnMutationState();
|
|
1052
|
-
return {
|
|
1053
|
-
completed: false,
|
|
1054
|
-
terminationReason: 'aborted',
|
|
1055
|
-
finalText: null,
|
|
1056
|
-
usage: turnUsage,
|
|
1057
|
-
validation: latestValidation,
|
|
1058
|
-
changedFiles: mutation.changedFiles.map((item) => item.path),
|
|
1059
|
-
};
|
|
1060
|
-
}
|
|
1061
|
-
if (latestValidation.status === 'failed') {
|
|
1062
|
-
history.push({
|
|
1063
|
-
role: 'user',
|
|
1064
|
-
content: '[System observation: automatic validation failed]\n' +
|
|
1065
|
-
`Command: ${latestValidation.command ?? '(internal verifier)'}\n` +
|
|
1066
|
-
`${latestValidation.output}\n\n` +
|
|
1067
|
-
'Fix the reported problem, then finish the task. Do not claim success until validation passes.',
|
|
1068
|
-
});
|
|
1069
|
-
// 验证及其失败观察均已完整落入 history;下一步中断时可安全保留。
|
|
1070
|
-
savedHistory = history.slice();
|
|
1071
|
-
continue;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
else {
|
|
1075
|
-
// PROMPT-02: 硬关卡 — 收尾路径上,如果本 turn 有 mutation 且未通过
|
|
1076
|
-
// validation(autoValidate=false / 验证已 passed 但 LLM 又宣称完成),
|
|
1077
|
-
// 推一条 [checklist] user 消息,强制模型再走一轮显式确认。
|
|
1078
|
-
// 防死循环:checklist 已推过 2 次仍无新工具调用 → 放行(走原本的 push)。
|
|
1079
|
-
const finalMutationForChecklist = getCurrentTurnMutationState();
|
|
1080
|
-
const checklistCtx = {
|
|
1081
|
-
hadMutation: finalMutationForChecklist.version > 0,
|
|
1082
|
-
lastValidationStatus: (latestValidation?.status === 'failed' || latestValidation?.status === 'passed')
|
|
1083
|
-
? latestValidation.status
|
|
1084
|
-
: 'none',
|
|
1085
|
-
mode: getAgentMode(),
|
|
1086
|
-
modelFamily: inferModelFamily(config.model),
|
|
1087
|
-
};
|
|
1088
|
-
const checklistRetryCount = history.__checklistStreak ?? 0;
|
|
1089
|
-
const shouldFireChecklist = preCompletionChecklistHandler !== null
|
|
1090
|
-
&& preCompletionChecklistHandler(checklistCtx)
|
|
1091
|
-
&& checklistRetryCount < 2;
|
|
1092
|
-
if (shouldFireChecklist) {
|
|
1093
|
-
history.push(candidate);
|
|
1094
|
-
history.push({
|
|
1095
|
-
role: 'user',
|
|
1096
|
-
content: _checklistMiddleware.buildUserMessage(checklistCtx.modelFamily),
|
|
1097
|
-
});
|
|
1098
|
-
history.__checklistStreak = checklistRetryCount + 1;
|
|
1099
|
-
// QUAL-01 trace 硬事件:PROMPT-02 触发计数(取代 history 文本扫描
|
|
1100
|
-
// [checklist] marker — LLM 在正文里提一句 "[checklist]" 也会被误算)。
|
|
1101
|
-
emitTrace('checklist_triggered', {
|
|
1102
|
-
streak: checklistRetryCount + 1,
|
|
1103
|
-
validationStatus: checklistCtx.lastValidationStatus,
|
|
1104
|
-
hadMutation: checklistCtx.hadMutation,
|
|
1105
|
-
modelFamily: checklistCtx.modelFamily ?? 'other',
|
|
1106
|
-
});
|
|
1107
|
-
// 重要: 不置 done,让主循环继续下一轮(模型被迫用工具或写出可验证声明)。
|
|
1108
|
-
continue;
|
|
1109
|
-
}
|
|
1110
|
-
history.push(candidate);
|
|
1111
|
-
}
|
|
773
|
+
history.push({ role: 'assistant', content: result.content });
|
|
1112
774
|
const finalMutation = getCurrentTurnMutationState();
|
|
1113
775
|
done = true;
|
|
1114
776
|
traceStatus = 'completed';
|
|
1115
|
-
const verified = finalMutation.version <= fullyValidatedMutationVersion;
|
|
1116
777
|
return {
|
|
1117
778
|
completed: true,
|
|
1118
|
-
terminationReason:
|
|
779
|
+
terminationReason: 'completed',
|
|
1119
780
|
finalText: result.content,
|
|
1120
781
|
usage: turnUsage,
|
|
1121
|
-
validation: latestValidation,
|
|
1122
782
|
changedFiles: finalMutation.changedFiles.map((item) => item.path),
|
|
1123
|
-
history: history.slice(),
|
|
1124
783
|
};
|
|
1125
784
|
}
|
|
1126
785
|
finally {
|
|
@@ -1134,15 +793,12 @@ export async function runAgentCore(opts) {
|
|
|
1134
793
|
done = true;
|
|
1135
794
|
traceStatus = 'max_steps';
|
|
1136
795
|
const finalMutation = getCurrentTurnMutationState();
|
|
1137
|
-
const hasUnverifiedChanges = finalMutation.version > fullyValidatedMutationVersion;
|
|
1138
796
|
return {
|
|
1139
797
|
completed: false,
|
|
1140
|
-
terminationReason:
|
|
798
|
+
terminationReason: 'max_steps',
|
|
1141
799
|
finalText: null,
|
|
1142
800
|
usage: turnUsage,
|
|
1143
|
-
validation: latestValidation,
|
|
1144
801
|
changedFiles: finalMutation.changedFiles.map((item) => item.path),
|
|
1145
|
-
history: history.slice(),
|
|
1146
802
|
};
|
|
1147
803
|
}
|
|
1148
804
|
finally {
|
|
@@ -1154,7 +810,6 @@ export async function runAgentCore(opts) {
|
|
|
1154
810
|
toolCalls: toolCallCount,
|
|
1155
811
|
changedFiles: finalMutation.changedFiles.map((item) => item.path),
|
|
1156
812
|
totalTokens: turnUsage?.totalTokens,
|
|
1157
|
-
validationStatus: latestValidation?.status,
|
|
1158
813
|
});
|
|
1159
814
|
try {
|
|
1160
815
|
opts.onTrace?.({
|
|
@@ -1166,7 +821,6 @@ export async function runAgentCore(opts) {
|
|
|
1166
821
|
toolCalls: toolCallCount,
|
|
1167
822
|
changedFiles: finalMutation.changedFiles.map((item) => item.path),
|
|
1168
823
|
usage: turnUsage,
|
|
1169
|
-
validation: latestValidation,
|
|
1170
824
|
});
|
|
1171
825
|
}
|
|
1172
826
|
catch {
|