Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

perf(browser): 将无 guard 配方提取接入固定 DOM 读取

Dashd01fea8f8c75
diff --git a/packages/browser-control-runtime/upstream/MAINTAINING.md b/packages/browser-control-runtime/upstream/MAINTAINING.md
index a745e78b85..66f186cac6 100644
--- a/packages/browser-control-runtime/upstream/MAINTAINING.md
+++ b/packages/browser-control-runtime/upstream/MAINTAINING.md
@@ -199,6 +199,7 @@ pnpm --filter @cindy/browser-control-runtime test # 契约 + SSRF guard
### 固定 DOM 读取的生命周期边界(2026-10-27)
+- 无 guard 的配方 extract 与直接 extract 使用相同结构化读取。含 guard 的配方合并身份校验与提取后,必须删除 `domRead` 元数据,保持普通 evaluate 路径,避免 runtime 忽略合并函数而绕过 guard。
- 读取沿用已有弹窗中断信号,并给 CDP 命令设置总时限。弹窗期间 Chrome 可能暂停 detach:及时返回 `blockedByDialog`,原 scoped session 的 finally 在 Chrome 恢复后完成释放,不新增连接缓存或重试。
- 读取前后的主 frame `loaderId` 必须一致,防止同网址刷新后返回旧文档的结果;页面 URL 与导航策略检查仍保留。同网址内的动态内容没有通用“已加载”判据,调用方仍需等待真实业务文字或元素。
- `scripts/browser-dom-read-live-check.mjs` 包含真实弹窗、同网址刷新、慢加载与连续 500 次读取检查。资源样本在垃圾回收后读取页面 JS heap、命名隔离世界数量和 CDP session 释放情况;不等同于长时间整进程内存保证。
diff --git a/packages/lizi-mcps/src/browser/recipe-runner.test.ts b/packages/lizi-mcps/src/browser/recipe-runner.test.ts
index 0cacd6231c..e3b694637e 100644
--- a/packages/lizi-mcps/src/browser/recipe-runner.test.ts
+++ b/packages/lizi-mcps/src/browser/recipe-runner.test.ts
@@ -76,6 +76,11 @@ describe('stepToRequest', () => {
expect(req.action).toBe('act');
expect(req.request?.kind).toBe('evaluate');
expect(req.request?.fn).toContain('querySelectorAll');
+ // PR #7432: unguarded recipe reads must reach the same fixed DOM path as
+ // direct extracts, while retaining fn for older runtime backends.
+ expect(req.request?.domRead).toEqual({ kind: 'extract',
+ extract: { from: '.row', multiple: true, fields: { t: '.t' } },
+ });
});
it('maps an evaluate step to act:evaluate, interpolating vars into the fn source', () => {
@@ -455,6 +460,9 @@ describe('recipe guards and output checks', () => {
guard: { url: 'https://x.com/', selector: '#account', text: 'account' },
extract: { fields: { title: 'h1' } }, expect: { requiredFields: ['title'] }, as: 'data' }], output: '{{data}}' }, {}, {
call: async (req) => {
+ // The runtime ignores fn when domRead is present: leaving this metadata
+ // on the combined request would bypass the page identity guard.
+ expect(req.request?.domRead).toBeUndefined();
calls++;
const value = await runInNewContext(`(${req.request!.fn})()`, {
location: { href: scenario.url }, getComputedStyle: () => ({ visibility: 'visible' }),
diff --git a/packages/lizi-mcps/src/browser/recipe-runner.ts b/packages/lizi-mcps/src/browser/recipe-runner.ts
index e3addded9b..5d55d73d86 100644
--- a/packages/lizi-mcps/src/browser/recipe-runner.ts
+++ b/packages/lizi-mcps/src/browser/recipe-runner.ts
@@ -200,7 +200,8 @@ export function stepToRequest(step: RecipeStep, vars: Record<string, unknown>):
if (!step.extract) throw new Error('extract step requires `extract`');
return {
action: 'act',
- request: { kind: 'evaluate', fn: buildExtractFnSource(step.extract as ExtractSpec), timeoutMs: step.timeoutMs },
+ request: { kind: 'evaluate', fn: buildExtractFnSource(step.extract as ExtractSpec),
+ domRead: { kind: 'extract', extract: step.extract as ExtractSpec }, timeoutMs: step.timeoutMs },
};
case 'evaluate':
// Raw page-context JS (function-expression source). Unlike `extract` (which
@@ -396,6 +397,9 @@ export async function runRecipe(
if (!((${guard.request!.fn})())) return { matched: false };
return { matched: true, value: await ((${extractFn})()) };
}` } };
+ // The fixed read path ignores fn, so a composite guard must use the
+ // regular evaluate path instead of bypassing its identity check.
+ delete req.request!.domRead;
combinedGuard = true;
} else if (guard) {
let matched = false;
diff --git a/scripts/browser-dom-read-live-check.mjs b/scripts/browser-dom-read-live-check.mjs
index e789c776a4..56d87cc658 100644
--- a/scripts/browser-dom-read-live-check.mjs
+++ b/scripts/browser-dom-read-live-check.mjs
@@ -66,6 +66,8 @@ const { BrowserToolRegistry } =
await import("../packages/lizi-mcps/src/browser/tool-registry.ts");
const { registerBrowserTools } =
await import("../packages/lizi-mcps/src/browser/tools.ts");
+const { runRecipe } =
+ await import("../packages/lizi-mcps/src/browser/recipe-runner.ts");
const { LOGIN_WALL_FN } =
await import("../packages/browser-control-runtime/src/dom-read.ts");
const { getPageForTargetId, respondToObservedDialogOnPage } =
@@ -267,6 +269,43 @@ try {
.records[0].status,
"ready",
);
+ // PR #7432: use the production recipe runner, checking the unguarded fast
+ // path and that adding a guard still enforces identity before reading.
+ const recipeRead = (guard) =>
+ runRecipe(
+ {
+ id: "dom-read-check",
+ steps: [
+ {
+ action: "extract",
+ extract: statusSpec,
+ ...(guard ? { guard } : {}),
+ as: "value",
+ },
+ ],
+ output: "{{value}}",
+ },
+ {},
+ { call: (request) => runtime.call({ ...request, targetId }) },
+ );
+ const beforeRecipeTimers = navigationGraceTimers;
+ assert.equal((await recipeRead()).output.records[0].status, "ready");
+ assert.equal(navigationGraceTimers, beforeRecipeTimers);
+ assert.equal(
+ (await recipeRead({ url: base + "/wrong" })).failureKind,
+ "guard",
+ );
+ assert.equal(
+ (
+ await recipeRead({
+ url: base + "/dynamic",
+ selector: "#status",
+ text: "ready",
+ })
+ ).output.records[0].status,
+ "ready",
+ );
+ assert.ok(navigationGraceTimers > beforeRecipeTimers);
await call({ action: "navigate", url: base + "/cross-only" });
// Count actual sessions and execution contexts on this disposable page.
@@ -371,6 +410,7 @@ try {
"password detection",
"no arbitrary fn on read path",
"general evaluate retains grace",
+ "unguarded recipe uses fixed read and guarded recipe preserves identity check",
"modal read returns blockedByDialog",
"same-URL delayed content requires business wait",
"same-URL reload rejects stale result",