mocode-ai 1.10.1 → 1.11.0

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.
@@ -11,8 +11,8 @@ import { IGNORE } from './constants.js';
11
11
  import { getSandboxRoot, isInsideRoot } from '../sandbox/index.js';
12
12
  const require = createRequire(import.meta.url);
13
13
  const isWin = process.platform === 'win32';
14
- /** JSON 采集上限:约可容纳数万条命中,超出即杀进程并标 truncated。 */
15
- const MAX_JSON_BYTES = 8 * 1024 * 1024;
14
+ /** stdout 采集上限,超出即杀进程并标 truncated。 */
15
+ const MAX_RG_OUTPUT_BYTES = 8 * 1024 * 1024;
16
16
  /**
17
17
  * 定位 rg 可执行文件:MOCODE_RG_PATH 显式覆盖 → @vscode/ripgrep 随包二进制 → PATH 上的 rg。
18
18
  * 返 null = 三条路都没有,调用方应退 Node 兜底。
@@ -58,21 +58,31 @@ function normalizePath(p) {
58
58
  return p.replace(/^\.[\\/]/, '').replace(/\\/g, '/');
59
59
  }
60
60
  /**
61
- * 跑一次 ripgrep(--json NDJSON),聚合成每文件事件。永不抛错:
61
+ * 跑一次 ripgrep,收集每文件的行事件或 NUL 分隔的命中文件路径。
62
62
  * 进程起不来 → unavailable;非法正则 → regex_error;signal 中止 → aborted。
63
63
  */
64
64
  export function runRipgrep(params, signal) {
65
- const rgPath = resolveRgPath();
66
- if (!rgPath || signal?.aborted)
65
+ if (signal?.aborted)
67
66
  return Promise.resolve({ status: 'aborted' });
67
+ const rgPath = resolveRgPath();
68
+ if (!rgPath)
69
+ return Promise.resolve({ status: 'unavailable' });
68
70
  const cwd = getSandboxRoot() ?? process.cwd();
71
+ const maxFiles = params.maxFiles;
72
+ const pathsOnly = maxFiles !== undefined;
69
73
  // --no-require-git:rg 默认只在 git 仓库内才读 .gitignore;加上后非 git 项目的 .gitignore
70
74
  // 同样生效(实测不加时 ignored/ 会被扫到)。
71
- const args = ['--json', '--hidden', '--no-require-git', '--path-separator', '/'];
75
+ const args = [
76
+ ...(pathsOnly ? ['--no-json', '--files-with-matches', '--null', '--color', 'never'] : ['--json']),
77
+ '--hidden',
78
+ '--no-require-git',
79
+ '--path-separator',
80
+ '/',
81
+ ];
72
82
  // glob 显式指向 gitignore 内的 .mocode(回捞压缩归档等)时放开 VCS ignore。
73
83
  if (params.noIgnoreVcs)
74
84
  args.push('--no-ignore-vcs');
75
- if (params.context > 0)
85
+ if (!pathsOnly && params.context > 0)
76
86
  args.push('-C', String(params.context));
77
87
  if (params.glob && params.glob !== '**/*')
78
88
  args.push('-g', params.glob);
@@ -91,6 +101,7 @@ export function runRipgrep(params, signal) {
91
101
  let stderr = '';
92
102
  let buffer = '';
93
103
  const byPath = new Map();
104
+ const matchedPaths = new Set();
94
105
  let scanned = 0;
95
106
  let completeMatches = 0;
96
107
  const finish = (result) => {
@@ -109,7 +120,17 @@ export function runRipgrep(params, signal) {
109
120
  buffer = '';
110
121
  killTree(child);
111
122
  };
112
- const handleLine = (line) => {
123
+ const handleRecord = (line) => {
124
+ if (maxFiles !== undefined) {
125
+ const path = isWin ? normalizePath(line) : line.replace(/^\.\//, '');
126
+ if (!isInsideRoot(path) || matchedPaths.has(path))
127
+ return;
128
+ if (matchedPaths.size >= maxFiles)
129
+ stopCollecting();
130
+ else
131
+ matchedPaths.add(path);
132
+ return;
133
+ }
113
134
  let evt;
114
135
  try {
115
136
  evt = JSON.parse(line);
@@ -160,21 +181,22 @@ export function runRipgrep(params, signal) {
160
181
  bucket.contexts.push(event);
161
182
  };
162
183
  child.stdout.setEncoding('utf8');
184
+ const separator = pathsOnly ? '\0' : '\n';
163
185
  child.stdout.on('data', (chunk) => {
164
186
  if (settled || truncated)
165
187
  return;
166
188
  collected += Buffer.byteLength(chunk);
167
- if (collected > MAX_JSON_BYTES) {
189
+ if (collected > MAX_RG_OUTPUT_BYTES) {
168
190
  stopCollecting();
169
191
  return;
170
192
  }
171
193
  buffer += chunk;
172
194
  let idx;
173
- while ((idx = buffer.indexOf('\n')) >= 0) {
195
+ while (!truncated && (idx = buffer.indexOf(separator)) >= 0) {
174
196
  const line = buffer.slice(0, idx);
175
197
  buffer = buffer.slice(idx + 1);
176
198
  if (line)
177
- handleLine(line);
199
+ handleRecord(line);
178
200
  }
179
201
  });
180
202
  child.stderr.on('data', (chunk) => {
@@ -185,23 +207,27 @@ export function runRipgrep(params, signal) {
185
207
  child.on('error', (error) => {
186
208
  // ENOENT / EACCES / exec 格式错误 = 二进制不可用,交给 Node 兜底。
187
209
  const code = error.code;
188
- finish({ status: code === 'ENOENT' || code === 'EACCES' ? 'unavailable' : 'aborted' });
210
+ finish({ status: code === 'ENOENT' || code === 'EACCES' || code === 'ENOEXEC' ? 'unavailable' : 'aborted' });
189
211
  });
190
212
  child.on('close', (code) => {
191
- if (buffer.trim())
192
- handleLine(buffer.trim());
213
+ if (!pathsOnly && buffer.trim())
214
+ handleRecord(buffer.trim());
193
215
  // exit 2 + stderr = 正则/参数错误;无命中事件时按 regex_error 上报。
194
- if (code === 2 && byPath.size === 0 && stderr.trim()) {
216
+ if (code === 2 && byPath.size === 0 && matchedPaths.size === 0 && stderr.trim()) {
195
217
  const message = stderr.trim();
196
218
  // 过滤器把所有文件排除(典型:glob 指向 .gitignore 内的 .mocode 却没放开 ignore)
197
219
  // 不是正则错误——按无匹配处理,避免误导模型去改 pattern。
198
220
  if (/No files were searched/i.test(message)) {
199
- finish({ status: 'no-match', scanned, truncated });
221
+ finish(pathsOnly ? { status: 'paths', paths: [], truncated } : { status: 'no-match', scanned, truncated });
200
222
  return;
201
223
  }
202
224
  finish({ status: 'regex_error', message });
203
225
  return;
204
226
  }
227
+ if (pathsOnly) {
228
+ finish({ status: 'paths', paths: [...matchedPaths], truncated: truncated || (code !== 0 && code !== 1) });
229
+ return;
230
+ }
205
231
  if (byPath.size === 0) {
206
232
  finish({ status: 'no-match', scanned, truncated });
207
233
  return;
package/dist/ui/render.js CHANGED
@@ -579,6 +579,9 @@ export function summarizeToolResult(name, output) {
579
579
  // 「无匹配」结果不含任何文件头:必须显式识别,否则会被当成 1 处匹配(nonEmpty 只有 1 行)。
580
580
  if (!nonEmpty.length || nonEmpty[0].startsWith('无匹配'))
581
581
  return t('toolSummary.noMatches');
582
+ const paths = /^\[grep files=(\d+)\]$/.exec(nonEmpty[0]);
583
+ if (paths)
584
+ return t('toolSummary.files', { count: Number(paths[1]) });
582
585
  // 带 context 时 body 行数 ≠ 命中数(邻居行 + ` --` 分隔都在里面),
583
586
  // 按每文件头的真实命中数求和才对得上模型看到的「N 处匹配」。
584
587
  let matches = 0;
@@ -0,0 +1,52 @@
1
+ const VALIDATION_COMMAND = /^(?:(?:npm|pnpm|yarn|bun) (?:run )?(?:test|typecheck|lint|build)(?: --workspace [@\w./-]+)?|node --test(?: [\w./-]+)*|node (?:[\w./-]+\/)?verify[\w.-]*\.(?:[cm]?js))$/;
2
+ const PRESERVES_EVIDENCE = new Set(['read_file', 'grep', 'glob', 'plan_update', 'note_append']);
3
+ const MAX_CHECKS = 3;
4
+ /** Advisory evidence for one turn. Commands still execute normally if the agent requests a rerun. */
5
+ export function createVerificationEvidence() {
6
+ const passed = new Map();
7
+ let edited = false;
8
+ let pending = false;
9
+ const invalidate = () => {
10
+ passed.clear();
11
+ pending = false;
12
+ };
13
+ return {
14
+ invalidate,
15
+ observe(tool, args, outcome) {
16
+ if (outcome.changedFiles?.length || outcome.changeSet?.changes.length || outcome.staleFiles?.length) {
17
+ invalidate();
18
+ if (outcome.status === 'success' && (tool === 'edit_file' || tool === 'write_file'))
19
+ edited = true;
20
+ return;
21
+ }
22
+ if (!edited || PRESERVES_EVIDENCE.has(tool))
23
+ return;
24
+ const command = tool === 'run_command' && typeof args?.command === 'string' ? args.command.trim() : '';
25
+ if (command.length > 160 || !VALIDATION_COMMAND.test(command)) {
26
+ invalidate();
27
+ return;
28
+ }
29
+ const shell = typeof args?.shell === 'string' ? args.shell : '';
30
+ const key = JSON.stringify([command, shell]);
31
+ if (outcome.status !== 'success' || outcome.code !== 'OK') {
32
+ passed.delete(key);
33
+ return;
34
+ }
35
+ passed.delete(key);
36
+ passed.set(key, shell ? `${command} (shell: ${shell})` : command);
37
+ if (passed.size > MAX_CHECKS)
38
+ passed.delete(passed.keys().next().value);
39
+ pending = true;
40
+ },
41
+ takeReminder() {
42
+ if (!pending || !passed.size)
43
+ return;
44
+ pending = false;
45
+ return [
46
+ '## Validation evidence for the current edits',
47
+ ...[...passed.values()].map((command) => `- Passed: \`${command}\``),
48
+ 'If these checks cover the requested outcomes and their inputs and environment have not changed, use the results above and finish. Run any gaps, explicitly requested fresh checks, or checks invalidated by a change.',
49
+ ].join('\n');
50
+ },
51
+ };
52
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocode-ai",
3
- "version": "1.10.1",
3
+ "version": "1.11.0",
4
4
  "description": "终端编码 agent:LLM + tool-call 循环 + 流式输出(含思考)+ 27 个工具,接任意 OpenAI 兼容后端。",
5
5
  "type": "module",
6
6
  "bin": {