feat: v0.8.0 流语义补全 · 会话可靠 · 恢复力 — finish_reason 全链路贯通根治"思考中停止" · 2445 用例全量回归
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m16s
CI / 全量测试 (Electron ABI) (push) Failing after 6m4s
CI / 产物编译验证 (push) Successful in 11m1s

P0 会话可靠性收口(根治"模型思考着会话就停止"):
- P0-1 finish_reason 全链路贯通:DONE 事件与 IterationStep 新增 finishReason,OpenAI 共享 SSE / Anthropic message_delta.stop_reason / Ollama done_reason 三路采集,TRACE 层弃用硬编码 'stop' 记录真值
- P0-2 空响应守卫 + 降级重试:零产出流→可重试错误走退避;思考耗尽输出预算(reasoning-only + length)→自动关闭思考降级重试一次;仍失败→OUTPUT_LENGTH_EXCEEDED 结构化错误 + 故障转移;附带根治 abort 恰逢零工具调用轮被 COMPLETED 抢占的真实缺陷
- P0-3 思考×能力×预算三对齐:DeepSeek/MiMo/Agnes/Ollama 四家 supportsThinking=false 强制不发思考参数;小输出预算告警;设置页联动提示
- P0-4 渲染层可见性:截断/空完成/友好错误三类提示,i18n 全部出层
- P0-5 回归四件套:reasoning-only 终止判定、集成级空闲超时、504 引擎重试归类、思考中 abort→USER_INTERRUPT、P4-2 强制收尾路径

FEAT-1:LLM 设置新增「最大输出上限」——Provider 支持矩阵显隐 + 模型上限钳制提示 + 超限保存警告 + llm.maxTokens 热生效

P1 修复面收口:
- 渲染层三缺陷根治:后台会话回放缓冲(2000 条/4MB 有界 + agent:getReplayState + 事件总线)+ abort 双层自愈 + sendMessage 收尾兜底 + 中断卡片清扫
- 工具 abort 信号全覆盖:web_search/web_fetch/http_request/code_search/git 系列/delegate_task 全部接入引擎中断;web_search 时间预算收敛(720s→≤240s);移除伪造 ToolExecutionContext 与死代码
- 安全:本地 Pinned CONNECT 代理根治浏览器通道 DNS rebinding(校验期 IP pinning,可注入 resolver 表测);配置 URL 域名解析深校验(DeepCheckSoftFailure 软失败);SSE 空 error 帧防御修复;Ollama generate/embed AbortSignal.any 合并
- 缺陷清单:UTF-16 BOM 读取、tmp 同毫秒碰撞(nanoid 后缀)、code_search JS 回退参数对称(case_sensitive/前后文独立)、list_directory include_node_modules、崩溃自愈退避(60s 窗 ≥3 次停 reload)、MemoryViewer/Sidebar i18n 收口

P2 能力演进:
- 会话回收站:SCHEMA_VERSION 3 + 迁移 10(deleted_at,存在性守卫),软删除/恢复/彻底删除/30 天自动清理(启动+24h),searchMessages 聚合剔除,Sidebar 回收站面板
- 会话回放播放器:sessions:listRecordings/readRecording(白名单+目录边界+20MB 上限),SessionReplayPlayer 时间轴/步进/变速,Trace 面板入口
- electron-updater 自动更新:双轨(手动 feed 比对保留),生产环境启动静默检查 + update:status 广播 + app:updateInstall + LogsSettings UpdatePanel + builder publish 配置
- @ 文件提及:workspace.listFiles/readFileClip(边界/512KB/NUL 拒绝/MEMORY.md 保护),ChatInput Fuse 联想+键盘导航+附件管线注入
- MCP Resources/Prompts 发现:可选能力 try/catch 降级,mcp:listServerContents,MCPSettings 展开视图
- 文档对齐:内部 API 标准 HTML(Adapter 清单补 MiMo/已实现注记/STREAM_RESET/DONE.finishReason/ repetition_truncation 映射);README v0.8.0 亮点表

P3 测试基建:
- 新增 4 个测试文件:engine-stream-contract(6)、engine-stream-reliability(4:集成空闲超时/504 重试/思考中 abort/P4-2 强制收尾)、thinking-capability-gate(7)、pinned-proxy(9,含深校验 5)、session-trash(5,DB 域)、use-agent-stream hook 级(5)、agent.test 回放缓冲(2)
- 契约更新:orchestrator 被中断 SubAgent success=false(abort 优先级修复语义)、SSE 空 error 帧、UTF-16 正常读取、DeepSeek 未配置思考显式 disabled、迁移矩阵 v2→3
- 弱断言根治:registry WEBP 单向断言、hooks-contracts 自比恒真、memory 空 token 补强

全量验证:typecheck 0 错误 / lint 0 问题 / 系统 Node 2144 通过(301 DB 用例按 ABI 跳过)/ Electron ABI 2445/2445 全量通过 0 跳过
This commit is contained in:
2026-09-05 20:06:26 +08:00
parent 99d0c54129
commit 5b9d4d19b3
78 changed files with 5128 additions and 411 deletions
@@ -104,16 +104,13 @@ describe('ToolRegistry.truncateResult', () => {
expect(truncate(shot)).toBe(shot);
});
it('WEBP 魔数(RIFF....WEBP)的裸 base64 放行', () => {
const shot = { image: `UklGRg==${'A'.repeat(200_000)}WEBP` };
// RIFF 头 base64 'UklGRg==' 解码为 'RIFF\x00\x00\x00\x00'WEBP 魔数在 8-11 字节
const result = truncate(shot) as { image?: string; _truncated?: boolean };
if (result._truncated === true) {
// WEBP 魔数未命中(base64 头部 64 字节内未含完整 RIFF+WEBP)→ 走常规截断(保守)
expect(result.image).toBeUndefined();
} else {
expect(result).toBe(shot);
}
it('WEBP 魔数(RIFF....WEBP)的裸 base64 放行(v0.8.0 P3: 单向断言替代恒真分支)', () => {
// v0.8.0 P3 根治: 旧断言按 _truncated 分支双向放行(无论实现如何都通过,恒真)。
// 现构造 8-11 字节确为 'WEBP' 的确定性载荷:base64('RIFF\0\0\0\0WEBP') 前缀,
// isInlineImagePayload 必命中白名单 → 整段放行。
const head = Buffer.from('RIFF\0\0\0\0WEBP', 'latin1').toString('base64');
const shot = { image: head + 'A'.repeat(200_000) };
expect(truncate(shot)).toBe(shot);
});
it('image 字段但非图片内容(普通长文本)仍按常规 50KB 截断(堵住旧白名单漏洞)', () => {
@@ -248,22 +248,25 @@ describe('filesystem 工具 — read_file', () => {
expect(String(r.content).charCodeAt(0)).not.toBe(0xfeff);
});
it('UTF-16LE 文件被二进制检测拒绝(0x00 字节触发,编码探测死代码', async () => {
// 已知源码缺陷:isBinaryFile 的 NUL 字节检测拒绝一切 UTF-16 文件,
// decodeBufferWithDetection 的 UTF-16 分支因此不可达。此处锁定实际契约。
it('UTF-16LE 文件正常读取(v0.8.0 P1-3.3 BOM 检测根治后契约', async () => {
// v0.8.0 P1-3.3 根治: isBinaryFile 此前的 NUL 字节启发式拒绝一切 UTF-16
// 文件,decodeBufferWithDetection 的 UTF-16 分支不可达(原缺陷被本测试
// 锁定留档)。现 UTF-16 BOMFF FE / FE FF)视为文本,编码探测生效。
const r = (await tool.execute({ file_path: 'utf16le.txt' }, ctxFor(ws))) as {
success: boolean;
error?: string;
content?: string;
encoding?: string;
};
expect(r.success).toBe(false);
expect(String((r as { error?: string }).error)).toContain('Binary');
expect(r.success).toBe(true);
expect(r.encoding).toBe('utf-16le');
});
it('UTF-16BE 文件同样被二进制检测拒绝(实况契约', async () => {
it('UTF-16BE 文件正常读取(v0.8.0 P1-3.3', async () => {
const r = (await tool.execute({ file_path: 'utf16be.txt' }, ctxFor(ws))) as {
success: boolean;
content?: string;
};
expect(r.success).toBe(false);
expect(r.success).toBe(true);
});
it('GBK 字节样本 → 降级 gbk 编码并正确解码', async () => {
@@ -11,6 +11,9 @@ import { BrowserWindow, session } from 'electron';
import log from 'electron-log';
// v0.7.3 P2-2: CORS Origin 回显(纯函数在 network-utils,可表测)
import { corsAllowOrigin, extractOriginHeader } from './network-utils';
// v0.8.0 P1-4: 浏览器通道 SSRF 根治 —— 分区流量走本地 Pinned CONNECT 代理
import { ensurePinnedProxy } from '../../../utils/pinned-proxy';
import { isProxyActive } from '../../../utils/network-proxy';
/** Agent 浏览器专用 session partition — 与主应用 default session 完全隔离 */
const AGENT_PARTITION = 'persist:metona-agent-browser';
@@ -146,6 +149,28 @@ export class BrowserWindowManager {
// 跨域 Origin / 无 Origin 一律不加 ACAO 头 —— 保持默认同源策略阻止读取,
// 彻底堵死"第三方页面借该分区凭据读取跨域资源"的通道。
const agentSession = session.fromPartition(AGENT_PARTITION);
// v0.8.0 P1-4 根治: 浏览器通道 SSRF —— 分区全部流量经本地 Pinned CONNECT
// 代理(入口同源校验 + 校验期 IP pinningChromium 端 TLS/SNI 端到端不变),
// 关闭"入口校验通过 → Chromium 独立 DNS 解析"之间的 rebinding 残余窗口。
// 用户已显式配置 network.proxyUrl 时不启用(代理模式下 DNS 在代理端解析、
// pinning 不可实现,保持 M7 已知限制口径);代理启动失败降级可用性优先。
if (!isProxyActive()) {
const proxyPort = await ensurePinnedProxy();
if (proxyPort) {
try {
await agentSession.setProxy({
proxyRules: `http=127.0.0.1:${proxyPort};https=127.0.0.1:${proxyPort}`,
});
log.info(
`[BrowserWindowManager] agent partition routed via pinned proxy :${proxyPort}`,
);
} catch (err) {
log.warn('[BrowserWindowManager] pinned proxy setProxy failed:', err);
}
}
}
agentSession.webRequest.onHeadersReceived((details, callback) => {
// Electron 类型在此版本的 OnHeadersReceivedListenerDetails 上不暴露
// requestHeaders —— 显式声明读取面(Origin 大小写不敏感提取)
@@ -308,21 +333,48 @@ export class BrowserWindowManager {
* 各自直接 open/evaluate 共享单例,后到者销毁前者的窗口(崩溃根因,
* 见 openChain 注释)。排队后同一时刻只有一个抓取在使用窗口。
*
* v0.8.0 P1-2: 新增可选 abort signal —— 中断时停止页面加载(webContents.stop
* 并在序列各步骤间检查,终止整个 open→等待→evaluate 链。
*
* @returns 提取的页面正文;失败/内容过短返回 null(调用方走失败路径)
*/
async fetchPageText(url: string): Promise<string | null> {
async fetchPageText(url: string, signal?: AbortSignal): Promise<string | null> {
const run = async (): Promise<string | null> => {
await this.openInternal({ url });
await this.sleep(2_500);
const text = (await this.evaluate(`
(function() {
var clone = document.body.cloneNode(true);
var noise = clone.querySelectorAll('script, style, noscript, nav, header, footer, aside, iframe, svg');
noise.forEach(function(el) { el.remove(); });
return clone.innerText || '';
})();
`)) as string;
return text && text.trim() ? text : null;
const throwIfAborted = (): void => {
if (signal?.aborted) throw new Error('Browser fetch aborted');
};
// abort 时停止页面加载 —— loadURL/evaluate 会以 ERR_ABORTED 拒绝,
// 序列随之终止(窗口由单例管理,不在此销毁)
let onAbort: (() => void) | null = null;
if (signal && !signal.aborted) {
onAbort = () => {
try {
this.win?.webContents.stop();
} catch {
/* 窗口可能已销毁 */
}
};
signal.addEventListener('abort', onAbort, { once: true });
}
try {
throwIfAborted();
await this.openInternal({ url });
throwIfAborted();
await this.sleep(2_500);
throwIfAborted();
const text = (await this.evaluate(`
(function() {
var clone = document.body.cloneNode(true);
var noise = clone.querySelectorAll('script, style, noscript, nav, header, footer, aside, iframe, svg');
noise.forEach(function(el) { el.remove(); });
return clone.innerText || '';
})();
`)) as string;
throwIfAborted();
return text && text.trim() ? text : null;
} finally {
if (signal && onAbort) signal.removeEventListener('abort', onAbort);
}
};
this.openChain = this.openChain.then(run, run);
return this.openChain as Promise<string | null>;
+80 -17
View File
@@ -40,7 +40,8 @@ export class CodeSearchTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'code_search',
description: 'Search code using ripgrep. Supports regex patterns, file type filtering, and context lines. Much faster than search_files for large codebases. Falls back to JS implementation if ripgrep is not installed.',
description:
'Search code using ripgrep. Supports regex patterns, file type filtering, and context lines. Much faster than search_files for large codebases. Falls back to JS implementation if ripgrep is not installed.',
parameters: {
type: 'object',
properties: {
@@ -48,8 +49,14 @@ export class CodeSearchTool implements IMetonaTool {
path: { type: 'string', description: 'Search directory (default: workspace root)' },
file_glob: { type: 'string', description: 'File name glob filter (e.g., "*.ts", "*.py")' },
case_sensitive: { type: 'boolean', description: 'Case sensitive search (default false)' },
context_before: { type: 'number', description: 'Lines of context before match (default 0, max 5)' },
context_after: { type: 'number', description: 'Lines of context after match (default 0, max 5)' },
context_before: {
type: 'number',
description: 'Lines of context before match (default 0, max 5)',
},
context_after: {
type: 'number',
description: 'Lines of context after match (default 0, max 5)',
},
max_results: { type: 'number', description: 'Maximum results (default 50, max 200)' },
},
required: ['pattern'],
@@ -98,7 +105,13 @@ export class CodeSearchTool implements IMetonaTool {
private async searchWithRipgrep(
pattern: string,
searchPath: string,
opts: { fileGlob?: string; caseSensitive: boolean; contextBefore: number; contextAfter: number; maxResults: number },
opts: {
fileGlob?: string;
caseSensitive: boolean;
contextBefore: number;
contextAfter: number;
maxResults: number;
},
context: ToolExecutionContext,
): Promise<unknown> {
const rgArgs: string[] = ['--json'];
@@ -118,12 +131,31 @@ export class CodeSearchTool implements IMetonaTool {
const { stdout } = await execFileAsync('rg', rgArgs, {
maxBuffer: 10 * 1024 * 1024,
timeout: 25_000,
// v0.8.0 P1-2: 引擎级 abort 信号 —— 用户中断会话时立即 kill rg 子进程
//Node execFile 原生支持 signal 选项,触发时以 AbortError 拒绝)
signal: context.signal,
});
const results = this.parseRipgrepJsonOutput(stdout);
return { results: results.slice(0, opts.maxResults), count: results.length, engine: 'ripgrep' };
return {
results: results.slice(0, opts.maxResults),
count: results.length,
engine: 'ripgrep',
};
} catch (error) {
const err = error as { code?: number; signal?: string; stdout?: string; stderr?: string; killed?: boolean; message?: string };
const err = error as {
code?: number;
signal?: string;
stdout?: string;
stderr?: string;
killed?: boolean;
message?: string;
name?: string;
};
// v0.8.0 P1-2: 引擎 abort → 直接以中断语义返回(不再回退 JS 搜索)
if (err.name === 'AbortError' || context.signal?.aborted) {
return { results: [], count: 0, error: 'Search aborted', engine: 'ripgrep', aborted: true };
}
// rg 退出码 1 = 无匹配,不是错误
if (err.code === 1) {
return { results: [], count: 0, engine: 'ripgrep' };
@@ -148,10 +180,24 @@ export class CodeSearchTool implements IMetonaTool {
before?: string[];
after?: string[];
}> {
const results: Array<{ path: string; line: number; column: number; match: string; before?: string[]; after?: string[] }> = [];
const results: Array<{
path: string;
line: number;
column: number;
match: string;
before?: string[];
after?: string[];
}> = [];
const lines = output.split('\n').filter((l) => l.trim());
let currentMatch: { path: string; line: number; column: number; match: string; before?: string[]; after?: string[] } | null = null;
let currentMatch: {
path: string;
line: number;
column: number;
match: string;
before?: string[];
after?: string[];
} | null = null;
let beforeBuffer: string[] = [];
let afterBuffer: string[] = [];
@@ -185,7 +231,9 @@ export class CodeSearchTool implements IMetonaTool {
}
const text = (data.lines as { text?: string } | undefined)?.text ?? '';
const submatches = (data.submatches as Array<{ match: { text?: string }; start?: number }> | undefined) ?? [];
const submatches =
(data.submatches as Array<{ match: { text?: string }; start?: number }> | undefined) ??
[];
const matchText = submatches[0]?.match?.text ?? text;
const column = (submatches[0]?.start ?? 0) + 1;
@@ -214,18 +262,33 @@ export class CodeSearchTool implements IMetonaTool {
private async searchWithJs(
pattern: string,
searchPath: string,
opts: { fileGlob?: string; caseSensitive: boolean; contextBefore: number; contextAfter: number; maxResults: number },
opts: {
fileGlob?: string;
caseSensitive: boolean;
contextBefore: number;
contextAfter: number;
maxResults: number;
},
context: ToolExecutionContext,
): Promise<unknown> {
// 动态导入以避免循环依赖
const { SearchFilesTool } = await import('./filesystem');
const searchTool = new SearchFilesTool();
return searchTool.execute({
pattern,
target: 'content',
path: searchPath,
file_glob: opts.fileGlob,
limit: opts.maxResults,
}, context);
// v0.8.0 P1-3.5 根治: 回退路径此前静默丢失 case_sensitive / context_before /
// context_after —— 同一调用在 ripgrep 存在与否会产生不同结果(行为不对称)。
// SearchFilesTool 现已支持这三个参数(filesystem.ts P1-3.5),原样透传。
return searchTool.execute(
{
pattern,
target: 'content',
path: searchPath,
file_glob: opts.fileGlob,
limit: opts.maxResults,
case_sensitive: opts.caseSensitive,
context_before: opts.contextBefore,
context_after: opts.contextAfter,
},
context,
);
}
}
@@ -15,18 +15,21 @@ import type { TaskOrchestrator } from '../../orchestration/orchestrator';
export class DelegateTaskTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'delegate_task',
description: 'Delegate a sub-task to an independent SubAgent for parallel or isolated execution. The SubAgent runs its own ReAct loop and returns the final result. Use this for complex sub-tasks that benefit from focused reasoning.',
description:
'Delegate a sub-task to an independent SubAgent for parallel or isolated execution. The SubAgent runs its own ReAct loop and returns the final result. Use this for complex sub-tasks that benefit from focused reasoning.',
parameters: {
type: 'object',
properties: {
description: {
type: 'string',
description: 'Clear and detailed description of the sub-task to delegate. This will be the SubAgent\'s user message.',
description:
"Clear and detailed description of the sub-task to delegate. This will be the SubAgent's user message.",
},
tools: {
type: 'array',
items: { type: 'string', description: 'Tool name' },
description: 'Optional whitelist of tool names the SubAgent can use (e.g. ["read_file", "web_search"]). If omitted, SubAgent inherits all tools.',
description:
'Optional whitelist of tool names the SubAgent can use (e.g. ["read_file", "web_search"]). If omitted, SubAgent inherits all tools.',
},
maxIterations: {
type: 'number',
@@ -58,6 +61,8 @@ export class DelegateTaskTool implements IMetonaTool {
parentSessionId: context.sessionId,
maxIterations,
tools,
// v0.8.0 P1-2: 引擎级 abort 信号透传 —— 用户中断会话时联动终止 SubAgent
signal: context.signal,
});
return {
@@ -19,6 +19,7 @@
import { readFile, writeFile, rename, mkdir, stat, unlink } from 'fs/promises';
import { dirname } from 'path';
import { existsSync } from 'fs';
import { nanoid } from 'nanoid';
import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';
@@ -422,7 +423,8 @@ export class FileEditorTool implements IMetonaTool {
const newContent = newLines.join('\n');
// F1.7: 原子写入 - 临时文件 + rename
const tmpPath = `${filePath}.tmp_${Date.now()}`;
// v0.8.0 P1-3.4: tmp 文件名追加 nanoid 随机段(同毫秒并发写防碰撞)
const tmpPath = `${filePath}.tmp_${Date.now()}_${nanoid(6)}`;
try {
await writeFile(tmpPath, newContent, 'utf-8');
await rename(tmpPath, filePath);
+71 -10
View File
@@ -38,6 +38,7 @@ import {
} from 'fs/promises';
import { join, relative, resolve, dirname } from 'path';
import { existsSync, realpathSync } from 'fs';
import { nanoid } from 'nanoid';
import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';
@@ -61,6 +62,13 @@ import {
* v0.3.2 修复 FAIL: 使用 bytesRead 限制循环上界,而非 buffer.length。
* Buffer.alloc(8192) 初始化为全 0fd.read 只覆盖 [0, bytesRead) 区间,
* 若用 buffer.length 遍历会误判所有 < 8192 字节的纯文本文件为二进制。
*
* v0.8.0 P1-3.3 根治: UTF-16 文件(LE: FF FE / BE: FE FF BOM)几乎每个字符
* 都含 0x00 字节 —— NULL 字节启发式把一切 UTF-16 文本判为二进制并整体拒绝,
* 使 decodeBufferWithDetection 的 UTF-16 解码分支不可达(原缺陷被
* filesystem-tools.test.ts 锁定留档,本次随修复同步改约)。现先检测 UTF-16
* BOM:命中即视为文本(UTF-16 解码由 decodeBufferWithDetection 承担),
* 未命中 BOM 才走 NULL 字节启发式。
*/
async function isBinaryFile(filePath: string): Promise<boolean> {
// M-20 修复: 使用 finally 块确保文件句柄关闭,防止 fd 泄漏
@@ -69,6 +77,9 @@ async function isBinaryFile(filePath: string): Promise<boolean> {
const buffer = Buffer.alloc(8192);
fd = await open(filePath, 'r');
const { bytesRead } = await fd.read(buffer, 0, 8192, 0);
// v0.8.0 P1-3.3: UTF-16 LE/BE BOM → 文本(NUL 启发式对其无效)
if (bytesRead >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) return false; // UTF-16 LE BOM
if (bytesRead >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) return false; // UTF-16 BE BOM
// 仅检查实际读取的字节范围 [0, bytesRead)
// 含 NULL 字节 → 二进制;空文件(bytesRead=0)→ 非二进制
for (let i = 0; i < bytesRead; i++) {
@@ -346,7 +357,9 @@ export class WriteFileTool implements IMetonaTool {
}
// v0.3.2: overwrite 模式使用原子写入(临时文件 + rename,与 file_editor 一致)
const tmpPath = `${filePath}.tmp_${Date.now()}`;
// v0.8.0 P1-3.4: tmp 文件名追加 nanoid 随机段 —— 同毫秒并发写同一文件时
// Date.now() 精度的 tmp 名会互相覆盖(并行工具调用场景)
const tmpPath = `${filePath}.tmp_${Date.now()}_${nanoid(6)}`;
try {
await writeFile(tmpPath, content, 'utf-8');
await rename(tmpPath, filePath);
@@ -395,6 +408,11 @@ export class ListDirectoryTool implements IMetonaTool {
type: 'boolean',
description: 'Include hidden files/dirs starting with "." (default false)',
},
include_node_modules: {
type: 'boolean',
description:
'Include node_modules directories (default false — skipped for performance; set true to inspect dependency packages)',
},
},
},
category: MetonaToolCategory.FILESYSTEM,
@@ -411,6 +429,8 @@ export class ListDirectoryTool implements IMetonaTool {
const depth = Math.min(5, Math.max(1, (args.depth as number) ?? 1));
const glob = args.glob as string | undefined;
const includeHidden = (args.include_hidden as boolean) ?? false;
// v0.8.0 P1-3.6: node_modules 可选纳入(默认跳过保持兼容)
const includeNodeModules = (args.include_node_modules as boolean) ?? false;
// v0.3.2 修复 WARN-3: listDir 内部提前终止,避免大目录全量遍历
const MAX_ENTRIES = 1000;
@@ -421,7 +441,17 @@ export class ListDirectoryTool implements IMetonaTool {
size?: number;
modified?: string;
}> = [];
await this.listDir(dirPath, dirPath, depth, glob, includeHidden, 0, entries, MAX_ENTRIES);
await this.listDir(
dirPath,
dirPath,
depth,
glob,
includeHidden,
includeNodeModules,
0,
entries,
MAX_ENTRIES,
);
return {
entries,
count: entries.length,
@@ -439,6 +469,7 @@ export class ListDirectoryTool implements IMetonaTool {
maxDepth: number,
glob: string | undefined,
includeHidden: boolean,
includeNodeModules: boolean,
currentDepth: number,
results: Array<{ name: string; path: string; type: string; size?: number; modified?: string }>,
maxEntries: number,
@@ -453,8 +484,9 @@ export class ListDirectoryTool implements IMetonaTool {
if (results.length >= maxEntries) return;
// v0.3.2: include_hidden 参数控制隐藏文件
if (!includeHidden && entry.name.startsWith('.')) continue;
// node_modules 始终跳过(太大)
if (entry.name === 'node_modules') continue;
// v0.8.0 P1-3.6: node_modules 默认跳过(太大)include_node_modules=true
// 时纳入(分析依赖包场景;上限保护仍生效)
if (entry.name === 'node_modules' && !includeNodeModules) continue;
const fullPath = join(dirPath, entry.name);
// 相对路径始终以根请求目录为基准
@@ -470,6 +502,7 @@ export class ListDirectoryTool implements IMetonaTool {
maxDepth,
glob,
includeHidden,
includeNodeModules,
currentDepth + 1,
results,
maxEntries,
@@ -526,6 +559,19 @@ export class SearchFilesTool implements IMetonaTool {
description:
'Lines of context to show around content matches (default 0, max 5). Only for target="content".',
},
case_sensitive: {
type: 'boolean',
description:
'Case sensitive search (default false — v0.8.0 P1-3.5: previously hardcoded case-insensitive, breaking code_search fallback symmetry)',
},
context_before: {
type: 'number',
description: 'Lines of context before match (default = context_lines, max 5)',
},
context_after: {
type: 'number',
description: 'Lines of context after match (default = context_lines, max 5)',
},
include_hidden: {
type: 'boolean',
description: 'Include hidden files/dirs starting with "." (default false)',
@@ -549,6 +595,15 @@ export class SearchFilesTool implements IMetonaTool {
const fileGlob = args.file_glob as string | undefined;
const limit = Math.min(200, Math.max(1, (args.limit as number) ?? 50));
const contextLines = Math.min(5, Math.max(0, (args.context_lines as number) ?? 0));
// v0.8.0 P1-3.5: 与 code_search 参数对称 —— case_sensitive 此前被硬编码
// 'gi' 忽略;context_before/after 此前在 code_search JS 回退路径静默丢失。
// 缺省时回退 context_lines(向后兼容)。
const caseSensitive = (args.case_sensitive as boolean) ?? false;
const contextBefore = Math.min(
5,
Math.max(0, (args.context_before as number) ?? contextLines),
);
const contextAfter = Math.min(5, Math.max(0, (args.context_after as number) ?? contextLines));
const includeHidden = (args.include_hidden as boolean) ?? false;
if (target === 'files') {
@@ -566,7 +621,9 @@ export class SearchFilesTool implements IMetonaTool {
pattern,
fileGlob,
limit,
contextLines,
contextBefore,
contextAfter,
caseSensitive,
includeHidden,
context.workspacePath,
);
@@ -606,7 +663,9 @@ export class SearchFilesTool implements IMetonaTool {
pattern: string,
fileGlob: string | undefined,
limit: number,
contextLines: number,
contextBefore: number,
contextAfter: number,
caseSensitive: boolean,
includeHidden: boolean,
workspacePath: string,
): Promise<unknown> {
@@ -635,7 +694,8 @@ export class SearchFilesTool implements IMetonaTool {
}
let regex: RegExp;
try {
regex = new RegExp(pattern, 'gi');
// v0.8.0 P1-3.5: case_sensitive 参数生效(此前硬编码 'gi' 恒大小写不敏感)
regex = new RegExp(pattern, caseSensitive ? 'g' : 'gi');
} catch {
return { results: [], count: 0, error: `Invalid regex pattern: ${pattern}`, success: false };
}
@@ -668,10 +728,11 @@ export class SearchFilesTool implements IMetonaTool {
if (regex.test(lines[i])) {
const match = lines[i].trim().slice(0, 200);
// v0.3.2: context lines 支持
// v0.8.0 P1-3.5: 前后文行数独立(code_search 对称性)
let context: string[] | undefined;
if (contextLines > 0) {
const start = Math.max(0, i - contextLines);
const end = Math.min(lines.length - 1, i + contextLines);
if (contextBefore > 0 || contextAfter > 0) {
const start = Math.max(0, i - contextBefore);
const end = Math.min(lines.length - 1, i + contextAfter);
context = lines.slice(start, end + 1);
}
results.push({
+42 -11
View File
@@ -28,18 +28,23 @@ const MAX_DIFF_SIZE = 50 * 1024;
*
* v0.3.1 修复 WARN-4: 允许调用方覆盖 maxBuffer,默认 1MB
* git_diff 传 5MB 以支持超大变更集(避免 maxBuffer exceeded 报错而非截断返回)。
*
* v0.8.0 P1-2: 新增可选 signal —— 引擎中断(用户停止/超时)时立即 kill git
* 子进程(Node execFile 原生支持 AbortSignal 选项),各工具经 context 透传。
*/
async function runGit(
args: string[],
cwd: string,
timeout = 10_000,
maxBuffer = 1024 * 1024, // 默认 1MB
signal?: AbortSignal,
): Promise<string> {
const { stdout } = await execFileAsync('git', args, {
cwd,
maxBuffer,
timeout,
encoding: 'utf-8',
...(signal ? { signal } : {}),
});
return stdout;
}
@@ -84,11 +89,15 @@ function extractRenamedFile(file: string): string {
export class GitStatusTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'git_status',
description: 'Show the working tree status. Returns current branch, ahead/behind counts, and lists of staged, unstaged, and untracked files.',
description:
'Show the working tree status. Returns current branch, ahead/behind counts, and lists of staged, unstaged, and untracked files.',
parameters: {
type: 'object',
properties: {
pathspec: { type: 'string', description: 'Limit to a path scope, e.g. "src/" or "package.json"' },
pathspec: {
type: 'string',
description: 'Limit to a path scope, e.g. "src/" or "package.json"',
},
},
},
category: MetonaToolCategory.FILESYSTEM,
@@ -191,11 +200,15 @@ export class GitStatusTool implements IMetonaTool {
export class GitDiffTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'git_diff',
description: 'Show changes between the working tree and the index (or staged changes). Returns unified diff text. Output is truncated at 50KB.',
description:
'Show changes between the working tree and the index (or staged changes). Returns unified diff text. Output is truncated at 50KB.',
parameters: {
type: 'object',
properties: {
cached: { type: 'boolean', description: 'Show only staged (cached) changes (default false)' },
cached: {
type: 'boolean',
description: 'Show only staged (cached) changes (default false)',
},
pathspec: { type: 'string', description: 'Limit to a path scope, e.g. "src/"' },
contextLines: { type: 'number', description: 'Number of context lines (default 3)' },
},
@@ -269,7 +282,8 @@ export class GitDiffTool implements IMetonaTool {
export class GitLogTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'git_log',
description: 'Show commit history. Returns commits with hash, message, and optionally author/date. Supports filtering by author and path.',
description:
'Show commit history. Returns commits with hash, message, and optionally author/date. Supports filtering by author and path.',
parameters: {
type: 'object',
properties: {
@@ -357,17 +371,25 @@ export class GitLogTool implements IMetonaTool {
export class GitCommitTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'git_commit',
description: 'Stage files and create a git commit. Supports amending the previous commit. In amend mode, message is optional (omitting it keeps the original commit message). Requires user confirmation.',
description:
'Stage files and create a git commit. Supports amending the previous commit. In amend mode, message is optional (omitting it keeps the original commit message). Requires user confirmation.',
parameters: {
type: 'object',
properties: {
message: { type: 'string', description: 'Commit message. Required for new commits. Optional in amend mode (omitting keeps original message).' },
message: {
type: 'string',
description:
'Commit message. Required for new commits. Optional in amend mode (omitting keeps original message).',
},
files: {
type: 'array',
items: { type: 'string', description: 'File path to stage' },
description: 'Files to stage (default: all changes via git add -A)',
},
amend: { type: 'boolean', description: 'Amend the previous commit instead of creating a new one (default false)' },
amend: {
type: 'boolean',
description: 'Amend the previous commit instead of creating a new one (default false)',
},
},
// F1-3: message 不再硬性 required — amend 模式下可省略(保留原 message)
// 校验在 execute 内根据 amend 标志动态进行
@@ -385,11 +407,17 @@ export class GitCommitTool implements IMetonaTool {
// F1-3: 非 amend 模式必须有 messageamend 模式可省略(保留原 message)
if (!amend && (!message || message.trim().length === 0)) {
return { success: false, error: 'Commit message is required (use amend: true to reuse the previous message)' };
return {
success: false,
error: 'Commit message is required (use amend: true to reuse the previous message)',
};
}
// amend 模式下若提供 message,需校验非空字符串
if (amend && message !== undefined && message.trim().length === 0) {
return { success: false, error: 'Commit message must not be empty (or omit message field to keep original)' };
return {
success: false,
error: 'Commit message must not be empty (or omit message field to keep original)',
};
}
try {
@@ -402,7 +430,10 @@ export class GitCommitTool implements IMetonaTool {
return {
success: false,
error: `File path outside workspace: ${f}`,
commit: '', branch: '', message: '', filesChanged: 0,
commit: '',
branch: '',
message: '',
filesChanged: 0,
};
}
}
@@ -82,7 +82,7 @@ export class HttpRequestTool implements IMetonaTool {
timeoutMs: 30_000,
};
async execute(args: Record<string, unknown>, _context: ToolExecutionContext): Promise<unknown> {
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
try {
const url = args.url as string;
const method = ((args.method as string) ?? 'GET').toUpperCase();
@@ -127,7 +127,9 @@ export class HttpRequestTool implements IMetonaTool {
// v0.7.3 P2-1: pinned fetch —— 校验通过的 IP pin 到连接层,
// 关闭校验-连接之间的 DNS rebinding 窗口
const response = await ssrfPinnedFetch(url, fetchOptions, timeout);
// v0.8.0 P1-2: 第 4 参透传引擎级 abort signal —— 用户中断会话时底层
// fetch 立即取消(此前最长 60s 资源悬挂)
const response = await ssrfPinnedFetch(url, fetchOptions, timeout, context.signal);
const text = await response.text();
// 截断到 50KB
@@ -83,13 +83,23 @@ export async function fetchWithTimeout(
url: string,
options: RequestInit = {},
timeoutMs = 20_000,
externalSignal?: AbortSignal,
): Promise<Response> {
// v0.8.0 P1-2: 合并外部 abort 信号(引擎中断时立即取消,不再跑到自身超时)
const controller = new AbortController();
const onExternalAbort = (): void => controller.abort();
if (externalSignal) {
if (externalSignal.aborted) controller.abort();
else externalSignal.addEventListener('abort', onExternalAbort, { once: true });
}
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...options, signal: controller.signal });
} finally {
clearTimeout(timer);
if (externalSignal) {
externalSignal.removeEventListener('abort', onExternalAbort);
}
}
}
+89 -1
View File
@@ -17,6 +17,12 @@
import { lookup } from 'node:dns/promises';
import { isIP } from 'node:net';
/**
* v0.8.0 P1-5: DNS lookup 可替换点 —— 生产恒为 node:dns/promises 的 lookup
* 测试通过替换 current 注入受控解析结果(ESM namespace 只读,无法直接打桩)。
*/
export const __dnsLookup: { current: typeof lookup } = { current: lookup };
/**
* 检查 IP 是否为私有/内网/回环/元数据地址
*
@@ -98,7 +104,7 @@ export async function resolvePublicAddresses(url: string): Promise<string[]> {
// 域名 — DNS 解析后检测所有 IP
let addresses: Array<{ address: string }>;
try {
addresses = await lookup(hostname, { all: true });
addresses = await __dnsLookup.current(hostname, { all: true });
} catch (err) {
throw new Error(
`Blocked SSRF: DNS resolution failed for ${hostname}: ${(err as Error).message}`,
@@ -231,3 +237,85 @@ export function assertSafeConfigTarget(url: string): void {
}
// 域名(非 IP):允许(DNS 可能解析到内网,但用户显式配置的本地服务是合法场景)
}
/**
* v0.8.0 P1-5: 配置类 URL 的**深校验** —— 在 assertSafeConfigTarget 的静态规则
* 之上对域名做真实 DNS 解析,任一解析结果命中高危段(链路本地/云元数据、0/8、
* 组播保留、::ffff: 映射等价段)即拒绝。
*
* 与 assertSafeConfigTarget 的差异:后者对"域名放行"(不解析 DNS,本地实例
* 域名合法);本函数补上"域名解析到云元数据 IP"的绕过窗口
* (如 attacker.example 解析到 169.254.169.254)。
*
* 放行语义不变:127.0.0.1 / RFC1918 私网(本地 MCP/SearXNG 合法);DNS 解析
* 失败也放行(配置期校验为纵深手段 —— 离线配置合法,且运行时工具路径仍有
* 独立校验;失败仅 WARN 由调用方留痕)。
*
* @throws 如果 URL 解析结果指向高危目标或协议不被允许
*/
export async function assertSafeConfigTargetDeep(url: string): Promise<void> {
// 先执行静态规则(协议白名单/元数据主机名/IP 字面量段位)
assertSafeConfigTarget(url);
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return; // assertSafeConfigTarget 已抛,此处不可达(防御)
}
const rawHostname = parsed.hostname.toLowerCase();
// IPv6 字面量去方括号(与 assertSafeConfigTarget 同口径)
const hostname =
rawHostname.startsWith('[') && rawHostname.endsWith(']')
? rawHostname.slice(1, -1)
: rawHostname;
if (isIP(hostname) !== 0) return; // IP 字面量已由静态规则判定,无需解析
let addresses: Array<{ address: string }>;
try {
addresses = await __dnsLookup.current(hostname, { all: true });
} catch (err) {
// DNS 解析失败:放行(配置期纵深校验不做可用性裁决),由调用方日志留痕
throw new DeepCheckSoftFailure(
`config target DNS resolution failed for ${hostname}: ${(err as Error).message}`,
);
}
for (const { address } of addresses) {
// 复用 isPrivateIP 的完整段位表做"高危段"判定?——不行:isPrivateIP 把
// 回环/RFC1918 也判为私有,而配置路径放行它们。此处仅拦"静态规则拦不到、
// 但解析后才暴露"的高危段:链路本地(含云元数据)、0/8、组播保留。
if (isHighRiskResolvedIP(address)) {
throw new Error(
`Blocked: ${hostname} resolves to high-risk address ${address} (link-local/metadata/multicast)`,
);
}
}
}
/** 高危段判定(仅配置深校验使用):链路本地/0/8/组播保留,放行回环与 RFC1918 */
function isHighRiskResolvedIP(ip: string): boolean {
if (isIP(ip) === 4) {
const parts = ip.split('.').map(Number);
return (parts[0] === 169 && parts[1] === 254) || parts[0] === 0 || parts[0] >= 224;
}
if (isIP(ip) === 6) {
const lower = ip.toLowerCase();
if (lower.startsWith('fe80:') || lower.startsWith('ff')) return true;
if (lower === '::') return true;
const v4MappedMatch = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);
if (v4MappedMatch) return isHighRiskResolvedIP(v4MappedMatch[1]);
return false;
}
return false;
}
/**
* v0.8.0 P1-5: 深校验的"软失败"信号 —— DNS 解析失败不算配置错误(放行),
* 但调用方需要区分"校验通过"与"跳过校验"以便日志留痕。
*/
export class DeepCheckSoftFailure extends Error {
constructor(message: string) {
super(message);
this.name = 'DeepCheckSoftFailure';
}
}
+61 -10
View File
@@ -84,7 +84,7 @@ export class WebFetchTool implements IMetonaTool {
timeoutMs: 240_000,
};
async execute(args: Record<string, unknown>, _context: ToolExecutionContext): Promise<unknown> {
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
const url = args.url as string;
const mobileUA = (args.mobile_ua as boolean) ?? false;
const enableRetry = (args.retry as boolean) ?? true;
@@ -96,6 +96,11 @@ export class WebFetchTool implements IMetonaTool {
return { url, content: '', success: false, error: 'URL must start with http:// or https://' };
}
// v0.8.0 P1-2: abort 立即生效 —— 引擎中断(超时/用户停止)时不再继续后续阶段
if (context.signal?.aborted) {
return { url, content: '', success: false, error: 'Fetch aborted' };
}
// v0.6.4 P2-2: SSRF 校验 —— 覆盖 Phase1 HTTP 与 Phase3 浏览器两条通道的入口。
// 协议白名单 / 私有段 IP / 云元数据地址一律拒绝。
try {
@@ -117,7 +122,12 @@ export class WebFetchTool implements IMetonaTool {
logTool('web_fetch', `Fetching: ${url}`);
// ===== Phase 1: HTTP 抓取 =====
const phase1Result = await this.httpFetch(url, mobileUA, enableRetry);
const phase1Result = await this.httpFetch(url, mobileUA, enableRetry, context.signal);
// v0.8.0 P1-2: HTTP 阶段被 abort —— 直接终止,不进浏览器回退
if (context.signal?.aborted) {
return { url, content: '', success: false, error: 'Fetch aborted' };
}
if (phase1Result.success && !phase1Result.intercepted) {
// 根据 extract_mode 选择返回内容:'html' 模式返回清理后的 HTML'text' 模式返回纯文本
@@ -136,7 +146,7 @@ export class WebFetchTool implements IMetonaTool {
'web_fetch',
`Phase 2: Content too short (${phase1Content.length} chars), upgrading to browser`,
);
const browserResult = await this.browserFetch(url);
const browserResult = await this.browserFetch(url, context.signal);
if (browserResult) {
return this.buildSuccess(url, browserResult, 'browser', maxChars);
}
@@ -159,7 +169,7 @@ export class WebFetchTool implements IMetonaTool {
};
}
logTool('web_fetch', `Phase 3: Falling back to browser (${phase1Result.reason})`);
const browserResult = await this.browserFetch(url);
const browserResult = await this.browserFetch(url, context.signal);
if (browserResult) {
return this.buildSuccess(url, browserResult, 'browser', maxChars);
}
@@ -179,6 +189,7 @@ export class WebFetchTool implements IMetonaTool {
url: string,
mobileUA: boolean,
enableRetry: boolean,
signal?: AbortSignal,
): Promise<{
success: boolean;
html: string;
@@ -206,7 +217,14 @@ export class WebFetchTool implements IMetonaTool {
let redirectBlocked: string | null = null;
for (let hop = 0; hop <= MAX_REDIRECT_HOPS; hop++) {
response = await ssrfPinnedFetch(currentUrl, { headers, redirect: 'manual' }, 20_000);
// v0.8.0 P1-2: 每跳透传引擎级 abort signal —— 用户中断时底层 fetch
// 立即取消(含重定向链中间跳)
response = await ssrfPinnedFetch(
currentUrl,
{ headers, redirect: 'manual' },
20_000,
signal,
);
const next = resolveRedirectTarget(response, currentUrl);
if (next === null) break; // 非重定向(或无/非法 Location)—— 当前响应即终态
if (hop === MAX_REDIRECT_HOPS) {
@@ -261,6 +279,7 @@ export class WebFetchTool implements IMetonaTool {
if (response.status >= 500 && attempt < maxRetries - 1) {
await this.sleep(
backoffBase * Math.pow(2, attempt) + Math.random() * backoffBase * 0.6,
signal,
);
continue;
}
@@ -293,9 +312,22 @@ export class WebFetchTool implements IMetonaTool {
return { success: true, html, text, intercepted: false, reason: '' };
} catch (err) {
const errorMsg = (err as Error).message;
// v0.8.0 P1-2: abort 语义 —— 不重试、不睡眠,直接终止本阶段
if (signal?.aborted || (err as Error & { name?: string }).name === 'AbortError') {
return {
success: false,
html: '',
text: '',
intercepted: false,
reason: 'Fetch aborted',
};
}
if (attempt < maxRetries - 1) {
logTool('web_fetch', `Attempt ${attempt + 1} failed: ${errorMsg}, retrying...`);
await this.sleep(backoffBase * Math.pow(2, attempt) + Math.random() * backoffBase * 0.6);
await this.sleep(
backoffBase * Math.pow(2, attempt) + Math.random() * backoffBase * 0.6,
signal,
);
continue;
}
return { success: false, html: '', text: '', intercepted: false, reason: errorMsg };
@@ -313,7 +345,7 @@ export class WebFetchTool implements IMetonaTool {
// ===== Phase 2/3: 浏览器回退(使用共享 BrowserWindowManager 单例) =====
private async browserFetch(url: string): Promise<string | null> {
private async browserFetch(url: string, signal?: AbortSignal): Promise<string | null> {
// 查缓存(浏览器阶段产出的是纯文本,与 text 模式同键)
const cached = fetchCache.get(`text:${url}`);
if (cached) {
@@ -322,10 +354,13 @@ export class WebFetchTool implements IMetonaTool {
}
try {
// v0.8.0 P1-2: 引擎级 abort 透传 —— 中断时 BrowserWindowManager 停止页面
// 加载并终止抓取序列(此前浏览器回退完全不受 abort 控制)
if (signal?.aborted) return null;
// 崩溃修复: 走 manager.fetchPageText(内部串行化完整的 open→等待→evaluate 序列)。
// 原实现直接 open/evaluate 共享单例 —— web_search 并行抓取触发多个回退同时进入时,
// 后到者销毁前者的窗口(ERR_ABORTED ×3 = 应用崩溃 ×3,见 manager 注释)。
const text = await getBrowserManager().fetchPageText(url);
const text = await getBrowserManager().fetchPageText(url, signal);
if (text && text.trim().length >= 80) {
// 拦截检测(浏览器渲染后仍可能是验证码挑战页)
@@ -383,7 +418,23 @@ export class WebFetchTool implements IMetonaTool {
};
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
/** v0.8.0 P1-2: abort 感知的 sleep —— 中断期间不再等待退避间隔 */
private sleep(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve) => {
if (signal?.aborted) return resolve();
const cleanup = (): void => {
clearTimeout(timer);
signal?.removeEventListener('abort', onAbort);
};
const onAbort = (): void => {
cleanup();
resolve();
};
const timer = setTimeout(() => {
cleanup();
resolve();
}, ms);
signal?.addEventListener('abort', onAbort, { once: true });
});
}
}
+83 -18
View File
@@ -338,9 +338,15 @@ function parse360Regex(html: string): SearchResult[] {
// 探测经 ssrfPinnedFetchDNS pinning),重定向不自动跟随(3xx 即视为可达 ——
// 链接活性已证明,且跟跳目标不再绕过校验)。
async function checkReachability(urls: string[], concurrency = 5): Promise<Map<string, boolean>> {
async function checkReachability(
urls: string[],
signal?: AbortSignal,
concurrency = 5,
): Promise<Map<string, boolean>> {
const result = new Map<string, boolean>();
for (let i = 0; i < urls.length; i += concurrency) {
// v0.8.0 P1-2: abort 后立即终止预检批次
if (signal?.aborted) break;
const batch = urls.slice(i, i + concurrency);
const checks = batch.map(async (url) => {
try {
@@ -349,7 +355,13 @@ async function checkReachability(urls: string[], concurrency = 5): Promise<Map<s
result.set(url, false);
return;
}
const resp = await ssrfPinnedFetch(url, { method: 'HEAD', redirect: 'manual' }, 3_000);
// v0.8.0 P1-2: 透传引擎级 abort signal
const resp = await ssrfPinnedFetch(
url,
{ method: 'HEAD', redirect: 'manual' },
3_000,
signal,
);
result.set(url, resp.ok || (resp.status >= 300 && resp.status < 400));
} catch {
result.set(url, false);
@@ -424,9 +436,14 @@ export class WebSearchTool implements IMetonaTool {
private webFetchTool: WebFetchTool,
) {}
async execute(args: Record<string, unknown>, _context: ToolExecutionContext): Promise<unknown> {
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
const query = args.query as string;
// v0.8.0 P1-2: abort 立即生效(后续各阶段均携带本 signal)
if (context.signal?.aborted) {
return { success: false, query, results: [], error: 'Search aborted' };
}
// 读取 SearXNG 配置
const searxngConfig = this.readSearXNGConfig();
const useSearXNG = searxngConfig.enabled && !!searxngConfig.url;
@@ -463,12 +480,23 @@ export class WebSearchTool implements IMetonaTool {
let engineStats: Record<string, string>;
if (useSearXNG) {
const searxResult = await this.searchSearXNG(query, searxngConfig, maxResults, timeRange);
const searxResult = await this.searchSearXNG(
query,
searxngConfig,
maxResults,
timeRange,
context.signal,
);
results = searxResult.results;
mode = 'searxng';
engineStats = searxResult.engineStats;
} else {
const builtinResult = await this.searchBuiltinEngines(query, maxResults, timeRange);
const builtinResult = await this.searchBuiltinEngines(
query,
maxResults,
timeRange,
context.signal,
);
results = builtinResult.results;
mode = 'builtin';
engineStats = builtinResult.engineStats;
@@ -479,7 +507,7 @@ export class WebSearchTool implements IMetonaTool {
// 可达性预检
const topUrls = deduped.slice(0, Math.min(20, deduped.length)).map((r) => r.url);
const reachabilityMap = await checkReachability(topUrls);
const reachabilityMap = await checkReachability(topUrls, context.signal);
for (const r of deduped) {
r.reachable = reachabilityMap.get(r.url) ?? false;
}
@@ -489,11 +517,17 @@ export class WebSearchTool implements IMetonaTool {
// 摘要增强
if (enhanceSnippets) {
await this.enhanceSnippets(sorted, 3);
await this.enhanceSnippets(sorted, 3, context);
}
// 自动抓取完整内容
const fetchedContent = await this.autoFetch(query, sorted, searxngConfig.fetch_mode, fetchTop);
const fetchedContent = await this.autoFetch(
query,
sorted,
searxngConfig.fetch_mode,
fetchTop,
context,
);
// 格式化输出
const formatted = this.formatResults(query, sorted);
@@ -536,6 +570,7 @@ export class WebSearchTool implements IMetonaTool {
config: SearXNGConfig,
maxResults: number,
timeRange: string,
signal?: AbortSignal,
): Promise<{ results: SearchResult[]; engineStats: Record<string, string> }> {
const results: SearchResult[] = [];
const engineStats: Record<string, string> = {};
@@ -549,6 +584,8 @@ export class WebSearchTool implements IMetonaTool {
let page = 1;
while (results.length < maxResults && page <= maxPages) {
// v0.8.0 P1-2: abort 后停止翻页
if (signal?.aborted) break;
const params = new URLSearchParams();
params.set('q', query);
params.set('format', config.format || 'json');
@@ -564,7 +601,8 @@ export class WebSearchTool implements IMetonaTool {
const pageResults: SearchResult[] = [];
try {
const response = await fetchWithTimeout(searchUrl, { headers }, 15_000);
// v0.8.0 P1-2: SearXNG 请求携带引擎级 abort signal
const response = await fetchWithTimeout(searchUrl, { headers }, 15_000, signal);
if (!response.ok) {
throw new Error(`SearXNG API error: ${response.status} ${response.statusText}`);
}
@@ -625,6 +663,7 @@ export class WebSearchTool implements IMetonaTool {
query: string,
maxResults: number,
timeRange: string,
signal?: AbortSignal,
): Promise<{ results: SearchResult[]; engineStats: Record<string, string> }> {
const searchPromises = ENGINES.map(async (engine) => {
try {
@@ -639,6 +678,7 @@ export class WebSearchTool implements IMetonaTool {
},
},
8_000,
signal,
);
if (!response.ok) {
@@ -689,16 +729,23 @@ export class WebSearchTool implements IMetonaTool {
// ===== 摘要增强(委托给 WebFetchTool =====
private async enhanceSnippets(results: SearchResult[], maxEnhance: number): Promise<void> {
private async enhanceSnippets(
results: SearchResult[],
maxEnhance: number,
context: ToolExecutionContext,
): Promise<void> {
let enhanced = 0;
for (const r of results) {
// v0.8.0 P1-2: abort 后停止增强
if (context.signal?.aborted) break;
if (enhanced >= maxEnhance) break;
if (r.snippet.length < 30 && r.reachable) {
try {
const fetchResult = (await this.webFetchTool.execute(
{ url: r.url },
{ sessionId: '', workspacePath: '', iteration: 0, requestId: '' },
)) as { success: boolean; content?: string };
// v0.8.0 P1-2: 透传真实执行上下文(signal)—— 移除"伪造空上下文"
const fetchResult = (await this.webFetchTool.execute({ url: r.url }, context)) as {
success: boolean;
content?: string;
};
if (fetchResult.success && fetchResult.content) {
const text = fetchResult.content.slice(0, 200);
@@ -729,13 +776,21 @@ export class WebSearchTool implements IMetonaTool {
results: SearchResult[],
fetchMode: string,
fetchTop: number,
context: ToolExecutionContext,
): Promise<Array<{ url: string; title: string; content: string }>> {
// v0.8.0 P1-2 根治: 时间预算收敛 —— 旧实现最坏耗时(8 条 × 3 并发 × 单次
// web_fetch 240s ≈ 720s)远超工具自身 300s 超时,registry 层超时后整次调用
// 作废(已抓取内容全部丢弃)。现以 deadline 为界:剩余预算不足一次最小
// 抓取(30s)即停,已抓取内容照常返回。
const BUDGET_RATIO = 0.8;
const MIN_FETCH_BUDGET_MS = 30_000;
const deadlineMs = Date.now() + 300_000 * BUDGET_RATIO;
// 相关性评分(不过滤,relevance=0 的结果也参与抓取候选)
const withRelevance = results.map((r) => ({
const filtered = results.map((r) => ({
result: r,
relevance: computeRelevance(query, r),
}));
const filtered = withRelevance.length > 0 ? withRelevance : [];
// 确定抓取数量:fetchTop 已在 execute() 中综合了配置面板和工具参数
const topN = Math.min(fetchTop, 8, filtered.length);
@@ -761,9 +816,10 @@ export class WebSearchTool implements IMetonaTool {
}): Promise<{ url: string; title: string; content: string } | null> => {
try {
// 委托给 WebFetchTool — 享受三阶段回退策略(HTTP + 反爬 + 浏览器渲染)
// v0.8.0 P1-2: 透传真实执行上下文(signal)—— 移除"伪造空上下文"
const fetchResult = (await this.webFetchTool.execute(
{ url: item.result.url },
{ sessionId: '', workspacePath: '', iteration: 0, requestId: '' },
context,
)) as { success: boolean; content?: string };
if (fetchResult.success && fetchResult.content) {
@@ -779,9 +835,18 @@ export class WebSearchTool implements IMetonaTool {
}
};
// 并行批次抓取(并发 3
// 并行批次抓取(并发 3,时间预算收敛
const CONCURRENCY = 3;
for (let i = 0; i < toFetch.length; i += CONCURRENCY) {
// v0.8.0 P1-2: 剩余预算不足一次最小抓取 → 停止,保留已抓取内容
const remaining = deadlineMs - Date.now();
if (context.signal?.aborted || remaining < MIN_FETCH_BUDGET_MS) {
logTool(
'web_search',
`Auto-fetch budget stop: fetched=${fetched.length}, remaining=${Math.max(0, remaining)}ms, aborted=${!!context.signal?.aborted}`,
);
break;
}
const batch = toFetch.slice(i, i + CONCURRENCY);
const settled = await Promise.allSettled(batch.map((item) => fetchOne(item)));
for (const r of settled) {