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.
Files changed (55) hide show
  1. package/README.md +45 -40
  2. package/README.zh-CN.md +51 -41
  3. package/dist/agent/core.js +137 -447
  4. package/dist/agent/index.js +5 -24
  5. package/dist/agent/spawn.js +5 -5
  6. package/dist/agent/work-discipline.js +16 -70
  7. package/dist/config/index.js +87 -33
  8. package/dist/context/age-aware.js +18 -48
  9. package/dist/context/artifacts.js +19 -17
  10. package/dist/context/budget.js +27 -28
  11. package/dist/context/classifier.js +0 -1
  12. package/dist/context/encoders/index.js +4 -11
  13. package/dist/context/index.js +4 -7
  14. package/dist/context/lifecycle.js +115 -483
  15. package/dist/context/pipeline.js +8 -15
  16. package/dist/context/relevance.js +77 -55
  17. package/dist/host/stdio.js +0 -6
  18. package/dist/i18n/index.js +20 -6
  19. package/dist/index.js +11 -1
  20. package/dist/llm/index.js +82 -9
  21. package/dist/mcp/index.js +0 -1
  22. package/dist/repl/index.js +69 -15
  23. package/dist/runtime/browser-manager.js +299 -0
  24. package/dist/runtime/dev-server-manager.js +354 -0
  25. package/dist/runtime/shutdown.js +26 -0
  26. package/dist/session/compact.js +86 -102
  27. package/dist/session/index.js +0 -1
  28. package/dist/session/notes.js +107 -0
  29. package/dist/session/scheduler.js +88 -92
  30. package/dist/session/trace-metrics.js +5 -92
  31. package/dist/session/trace.js +1 -10
  32. package/dist/tools/builtins/browser.js +199 -0
  33. package/dist/tools/builtins/dev-server.js +99 -0
  34. package/dist/tools/builtins/index.js +33 -19
  35. package/dist/tools/builtins/plan-update.js +144 -0
  36. package/dist/tools/builtins/screenshot.js +173 -0
  37. package/dist/tools/builtins/view-image.js +49 -0
  38. package/dist/tools/constants.js +38 -8
  39. package/dist/tools/registry.js +5 -29
  40. package/dist/ui/batch.js +3 -0
  41. package/dist/ui/content.js +19 -16
  42. package/dist/ui/layout.js +120 -46
  43. package/dist/ui/render.js +11 -0
  44. package/package.json +2 -2
  45. package/dist/agent/middleware/checklist.js +0 -59
  46. package/dist/session/drop.d.ts +0 -19
  47. package/dist/session/drop.js +0 -93
  48. package/dist/tools/builtins/drop-context.d.ts +0 -18
  49. package/dist/tools/builtins/drop-context.js +0 -68
  50. package/dist/verification/diagnostics.js +0 -108
  51. package/dist/verification/fingerprint.js +0 -54
  52. package/dist/verification/index.js +0 -333
  53. package/dist/verification/postconditions.js +0 -98
  54. package/dist/verification/targeted-tests.js +0 -96
  55. package/dist/verification/types.js +0 -1
@@ -1,12 +1,8 @@
1
- // 五区 Context Budget Scheduler。
1
+ // 五区 Context Budget accounting and the shared pressure threshold.
2
2
  //
3
- // 目的:把当前请求拆成 System / History / Tool-Recent / Tool-Old / Summary + Reserve,
4
- // 统一报告各区占用,并只调度执行层能够真正落地的 warn / compact_history。
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
- totalTriggerRatio: 0.82,
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
- layers.system.actual = adj(systemCosts.prompt + systemCosts.toolSchemas);
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
- layers.summary.actual = adj(msgTokens(history[1]));
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 += t;
100
+ layers.toolRecent.actual += adj(raw);
101
101
  else
102
- layers.toolOld.actual += t;
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(msgTokens(m));
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.totalTriggerRatio * window;
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
- * push-time cap、relevance、lifecycle 与 age-aware sweep 已在评估前完成,
138
- * 因此这里不再生成无法执行的 Cold/Hot tool action。
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, totalOver, total } = report;
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
- if ((layers.history.overBudget || totalOver)
159
- && headroom < -policy.compactHeadroomTokens) {
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) : '-';
@@ -29,7 +29,6 @@ const BY_NAME = {
29
29
  edit_file: 'status',
30
30
  write_file: 'status',
31
31
  ask_human: 'status',
32
- drop_context: 'status',
33
32
  memory_save: 'status',
34
33
  memory_update: 'status',
35
34
  memory_forget: 'status',
@@ -1,14 +1,7 @@
1
- // 内置 encoder 清单。启动期 pipeline 首次调用时经 registerAll 注册到 registry。
2
- //
3
- // Phase 1:passthrough(identity 兜底)→ 全链路零行为变化。
4
- // Phase 2:tree / search / log / table / memory(高价值低风险)。
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';
@@ -1,12 +1,9 @@
1
- // context/ barrel:Context Optimization Pipeline + 五区 Budget Scheduler。
2
- //
3
- // 单一入口 optimizeToolResult(agent/core.ts pushToolResult 调)接管"工具结果进 LLM 前"的表示。
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, TOOL_OLD_AGE, } from './budget.js';
9
+ export { evaluateBudget, scheduleActions, formatReport, quickEstimate, userTurnBoundary, BUDGET_LAYERS, DEFAULT_BUDGET_POLICY, BUDGET_RATIO, HOT_TURN_WINDOW, } from './budget.js';