mocode-ai 1.1.7 → 1.1.9
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 +45 -40
- package/README.zh-CN.md +51 -41
- package/dist/agent/core.js +137 -447
- package/dist/agent/index.js +5 -24
- package/dist/agent/spawn.js +5 -5
- package/dist/agent/work-discipline.js +16 -70
- package/dist/config/index.js +87 -33
- 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 +20 -6
- package/dist/index.js +11 -1
- package/dist/llm/index.js +82 -9
- package/dist/mcp/index.js +0 -1
- package/dist/repl/index.js +69 -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/notes.js +107 -0
- 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 +33 -19
- package/dist/tools/builtins/plan-update.js +144 -0
- package/dist/tools/builtins/screenshot.js +173 -0
- package/dist/tools/builtins/view-image.js +49 -0
- package/dist/tools/constants.js +38 -8
- package/dist/tools/registry.js +5 -29
- package/dist/ui/batch.js +3 -0
- package/dist/ui/content.js +19 -16
- package/dist/ui/layout.js +120 -46
- 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/context/budget.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
// 五区 Context Budget
|
|
1
|
+
// 五区 Context Budget accounting and the shared pressure threshold.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// push-time cap、pipeline、relevance、lifecycle 与 age-aware sweep 负责工具结果优化;
|
|
7
|
-
// scheduler 在这些处理完成后评估,不重复生成 Cold/Hot tool 压缩动作。
|
|
8
|
-
// 本文件保持叶子级,只依赖 ChatMessage / token estimate,具体执行由 session/scheduler.ts 完成。
|
|
9
|
-
// contextBudget 开关关闭时,agent/core.ts 退化为直接调用 maybeCompact。
|
|
3
|
+
// This module only estimates and reports. session/scheduler.ts owns the sole
|
|
4
|
+
// automatic rewrite sequence and starts all pressure cleanup plus compact_history
|
|
5
|
+
// when corrected or raw request occupancy reaches 80%.
|
|
10
6
|
import { chatTools, estimateMessagesTokens, estimateToolSchemaTokens, messageTokens, } from '../llm/index.js';
|
|
11
7
|
import { toText } from './utils.js';
|
|
12
8
|
/** 五区分账(占比对齐 CONTEXT_WINDOW)。顺序固定,便于遍历。 */
|
|
@@ -28,9 +24,8 @@ export const DEFAULT_BUDGET_POLICY = {
|
|
|
28
24
|
reserve: 0.05,
|
|
29
25
|
},
|
|
30
26
|
hotTurnWindow: 4,
|
|
31
|
-
toolOldAge: 2,
|
|
32
27
|
compactKeepRatio: 0.40,
|
|
33
|
-
|
|
28
|
+
pressureTriggerRatio: 0.80,
|
|
34
29
|
schedulerTargetRatio: 0.80,
|
|
35
30
|
estimateSafetyFactor: 1.05,
|
|
36
31
|
compactHeadroomTokens: 1500,
|
|
@@ -38,7 +33,6 @@ export const DEFAULT_BUDGET_POLICY = {
|
|
|
38
33
|
/** 兼容既有调用方的只读别名;配置只在 DEFAULT_BUDGET_POLICY 中维护。 */
|
|
39
34
|
export const BUDGET_RATIO = DEFAULT_BUDGET_POLICY.ratios;
|
|
40
35
|
export const HOT_TURN_WINDOW = DEFAULT_BUDGET_POLICY.hotTurnWindow;
|
|
41
|
-
export const TOOL_OLD_AGE = DEFAULT_BUDGET_POLICY.toolOldAge;
|
|
42
36
|
function msgTokens(m) {
|
|
43
37
|
// 与请求预估复用同一实现,避免角色结构开销、多模态和 tool_calls 在两个预算路径中漂移。
|
|
44
38
|
return messageTokens(m);
|
|
@@ -69,13 +63,17 @@ export function evaluateBudget(history, window, step = 0, correction = 1, active
|
|
|
69
63
|
}
|
|
70
64
|
// 校正后的 token 数:raw * correction,最小 1(raw > 0 时)。
|
|
71
65
|
const adj = (raw) => (raw > 0 ? Math.max(1, Math.round(raw * correction)) : 0);
|
|
66
|
+
// 裸总量(不乘 correction):硬闸用它判断,防止 correction 折扣否决真实溢出。
|
|
67
|
+
let rawTotal = 0;
|
|
72
68
|
const sysMsg = history[0];
|
|
73
69
|
const systemCosts = {
|
|
74
70
|
prompt: sysMsg ? msgTokens(sysMsg) : 0,
|
|
75
71
|
toolSchemas: estimateToolSchemaTokens(activeTools),
|
|
76
72
|
};
|
|
77
73
|
// 工具 schema 与 system prompt 同属请求固定开销;必须计入总量才能可靠触发压缩。
|
|
78
|
-
|
|
74
|
+
const systemRaw = systemCosts.prompt + systemCosts.toolSchemas;
|
|
75
|
+
layers.system.actual = adj(systemRaw);
|
|
76
|
+
rawTotal += systemRaw;
|
|
79
77
|
// Summary 检测:role:'system' 且不是 history[0] 的,视为摘要(compact.ts 摘要插 index 1)。
|
|
80
78
|
// 简单启发:若 history[1]?.role === 'system' 且 content 含「# 会话摘要」特征串,计入 summary。
|
|
81
79
|
// 命中时循环跳过 i=1;不命中时当作普通 message(罕见,落到下方 user/assistant 分支)。
|
|
@@ -83,7 +81,9 @@ export function evaluateBudget(history, window, step = 0, correction = 1, active
|
|
|
83
81
|
if (history.length > 1 && history[1].role === 'system') {
|
|
84
82
|
const c1 = toText(history[1].content);
|
|
85
83
|
if (c1.startsWith('# 会话摘要') || c1.includes('会话摘要')) {
|
|
86
|
-
|
|
84
|
+
const summaryRaw = msgTokens(history[1]);
|
|
85
|
+
layers.summary.actual = adj(summaryRaw);
|
|
86
|
+
rawTotal += summaryRaw;
|
|
87
87
|
summaryHit = true;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
@@ -94,16 +94,18 @@ export function evaluateBudget(history, window, step = 0, correction = 1, active
|
|
|
94
94
|
const m = history[i];
|
|
95
95
|
if (i === 1 && summaryHit)
|
|
96
96
|
continue; // summary 已单独算过
|
|
97
|
+
const raw = msgTokens(m);
|
|
97
98
|
if (m.role === 'tool') {
|
|
98
|
-
const t = adj(msgTokens(m));
|
|
99
99
|
if (i >= hotStart)
|
|
100
|
-
layers.toolRecent.actual +=
|
|
100
|
+
layers.toolRecent.actual += adj(raw);
|
|
101
101
|
else
|
|
102
|
-
layers.toolOld.actual +=
|
|
102
|
+
layers.toolOld.actual += adj(raw);
|
|
103
|
+
rawTotal += raw;
|
|
103
104
|
}
|
|
104
105
|
else if (m.role !== 'system') {
|
|
105
106
|
// user / assistant 全部计入 history(对话轨迹)
|
|
106
|
-
layers.history.actual += adj(
|
|
107
|
+
layers.history.actual += adj(raw);
|
|
108
|
+
rawTotal += raw;
|
|
107
109
|
}
|
|
108
110
|
// 其它 system(几乎不存在)跳过
|
|
109
111
|
}
|
|
@@ -120,10 +122,11 @@ export function evaluateBudget(history, window, step = 0, correction = 1, active
|
|
|
120
122
|
// 按 overRatio 降序
|
|
121
123
|
triggers.sort((a, b) => layers[b].overRatio - layers[a].overRatio);
|
|
122
124
|
const total = BUDGET_LAYERS.reduce((s, k) => s + (k === 'reserve' ? 0 : layers[k].actual), 0);
|
|
123
|
-
const totalOver = total >= DEFAULT_BUDGET_POLICY.
|
|
125
|
+
const totalOver = total >= DEFAULT_BUDGET_POLICY.pressureTriggerRatio * window;
|
|
124
126
|
return {
|
|
125
127
|
step,
|
|
126
128
|
total,
|
|
129
|
+
rawTotal,
|
|
127
130
|
window,
|
|
128
131
|
layers,
|
|
129
132
|
systemCosts,
|
|
@@ -134,15 +137,11 @@ export function evaluateBudget(history, window, step = 0, correction = 1, active
|
|
|
134
137
|
};
|
|
135
138
|
}
|
|
136
139
|
/** 根据 BudgetReport 生成可执行动作。
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* History 或总量超预算时才考虑昂贵的 LLM 摘要。 */
|
|
140
|
+
* History compaction is the final fallback after pressure-only tool stages.
|
|
141
|
+
* Per-layer overages are diagnostics, never independent rewrite triggers. */
|
|
140
142
|
export function scheduleActions(report) {
|
|
141
143
|
const actions = [];
|
|
142
|
-
const { layers
|
|
143
|
-
const policy = DEFAULT_BUDGET_POLICY;
|
|
144
|
-
const headroom = policy.schedulerTargetRatio * report.window
|
|
145
|
-
- total * policy.estimateSafetyFactor;
|
|
144
|
+
const { layers } = report;
|
|
146
145
|
if (layers.system.overBudget) {
|
|
147
146
|
const { prompt, toolSchemas } = report.systemCosts;
|
|
148
147
|
const { actual, budget } = layers.system;
|
|
@@ -155,8 +154,8 @@ export function scheduleActions(report) {
|
|
|
155
154
|
+ `提示 ${prompt} + 工具 ${toolSchemas},×${report.correction.toFixed(2)}。`,
|
|
156
155
|
});
|
|
157
156
|
}
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
const pressureLine = DEFAULT_BUDGET_POLICY.pressureTriggerRatio * report.window;
|
|
158
|
+
if (Math.max(report.rawTotal, report.total) >= pressureLine) {
|
|
160
159
|
actions.push({ kind: 'compact_history' });
|
|
161
160
|
}
|
|
162
161
|
return actions;
|
|
@@ -164,7 +163,7 @@ export function scheduleActions(report) {
|
|
|
164
163
|
/** 拍平成人类可读(供 /context 命令与 check-budget 脚本用)。 */
|
|
165
164
|
export function formatReport(report) {
|
|
166
165
|
const lines = [];
|
|
167
|
-
lines.push(`step ${report.step} total ${report.total}/${report.window} (${((report.total / report.window) * 100).toFixed(1)}%)`);
|
|
166
|
+
lines.push(`step ${report.step} total ${report.total}/${report.window} (${((report.total / report.window) * 100).toFixed(1)}%) raw ${report.rawTotal}`);
|
|
168
167
|
for (const k of BUDGET_LAYERS) {
|
|
169
168
|
const lb = report.layers[k];
|
|
170
169
|
const pct = lb.budget > 0 ? ((lb.actual / lb.budget) * 100).toFixed(0) : '-';
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
// 内置 encoder
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// Phase 2.5:code / graph / doc / summary(覆盖剩余有 encoder 的 kind)。
|
|
6
|
-
// - code(read_file):仅折叠 ≥3 连续空行,行号保真(edit_file 依赖)。
|
|
7
|
-
// - graph(保留 kind,暂无 builtin 工具直接命中)/ doc(web_fetch, use_skill)/ summary(task):
|
|
8
|
-
// 去 ANSI + 折叠空行,保守不重构结构。
|
|
9
|
-
// - status(edit/write/ask_human/mem 增删改)无 encoder:本就是一行,无需编码。
|
|
10
|
-
//
|
|
11
|
-
// 加 encoder:新建 encoders/xxx.ts 导出 ContextEncoder,在此数组加一行。无需动 agent / llm / core。
|
|
1
|
+
// 内置 encoder 清单。仅由 opt-in pressure stage 首次调用时懒注册。
|
|
2
|
+
// Normal tool pushes do not pass through these encoders.
|
|
3
|
+
// Available transforms:tree / search / log / table / memory / code / doc / summary.
|
|
4
|
+
// status 类工具本就是短结果,无专用 encoder。
|
|
12
5
|
import { passthroughEncoder } from './passthrough.js';
|
|
13
6
|
import { treeEncoder } from './tree.js';
|
|
14
7
|
import { searchEncoder } from './search.js';
|
package/dist/context/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
// context/ barrel:
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// 单一入口 runScheduler(agent/core.ts 步前调)接管"何时调用哪一闸"的调度。
|
|
5
|
-
// 不调 LLM、不碰 Tool Calling schema / executeTool / tool_call_id 配对 / TUI 渲染
|
|
6
|
-
// (叶子级:仅 stdlib + tools/constants + session/compact 的 capToolResultForHistory 兜底 + config 开关)。
|
|
1
|
+
// context/ barrel: metadata tracking, optional pressure encoders, and budget reporting.
|
|
2
|
+
// Normal tool pushes stay raw apart from the hard per-result cap. The session
|
|
3
|
+
// scheduler is the only automatic rewrite coordinator at real pressure.
|
|
7
4
|
export { optimizeToolResult } from './pipeline.js';
|
|
8
5
|
export { classify, knownToolKinds } from './classifier.js';
|
|
9
6
|
export { recordArtifact, invalidateArtifacts, rehydrateArtifacts, refreshArtifactFreshness, pruneStaleArtifacts, collectArtifactRefs, formatArtifactTokenSources, } from './artifacts.js';
|
|
10
7
|
export { registerEncoder, registerAll, getEncoder, registeredKinds, } from './registry.js';
|
|
11
8
|
// ── Context Budget Scheduler ───────────────────────────────────────────────
|
|
12
|
-
export { evaluateBudget, scheduleActions, formatReport, quickEstimate, userTurnBoundary, BUDGET_LAYERS, DEFAULT_BUDGET_POLICY, BUDGET_RATIO, HOT_TURN_WINDOW,
|
|
9
|
+
export { evaluateBudget, scheduleActions, formatReport, quickEstimate, userTurnBoundary, BUDGET_LAYERS, DEFAULT_BUDGET_POLICY, BUDGET_RATIO, HOT_TURN_WINDOW, } from './budget.js';
|