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。

fix(browser): 统一预检与执行边界并提前解析结果条件

Dash5234f421df1a
diff --git a/packages/lizi-mcps/src/browser/browserMcpServer.test.ts b/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
index 157dc5da38..a563e6d16c 100644
--- a/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
+++ b/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
@@ -1204,8 +1204,9 @@ describe('recipe on-use maintenance', () => {
});
// Review #6822: saved-use coverage above did not exercise repair preflight.
// MCP entry/unit: all three preflight paths must leave the page and budget untouched.
- it.each(['missing', 'non-scalar', 'interpolation'] as const)('preserves the repair trial after %s input preflight failure', async (failure) => {
+ it.each(['missing', 'non-scalar', 'interpolation', 'equals'] as const)('preserves the repair trial after %s input preflight failure', async (failure) => {
const candidate = { ...draft, inputs: { query: { required: true } }, steps: [{ ...draft.steps[0],
+ ...(failure === 'equals' ? { expect: { equals: { title: '{{missing}}' } } } : {}),
guard: { url: 'https://example.com/', ...(failure === 'interpolation' ? { selector: '#{{missing}}' } : {}) } }] };
const stored = { ...candidate, validation: { site: 'example.com', state: 'suspended' as const } };
const saved: unknown[] = [];
@@ -1219,9 +1220,9 @@ describe('recipe on-use maintenance', () => {
expect((await invoke(h, { ...run, inputs })).ok).toBe(false);
expect(h.calls).toHaveLength(0);
expect(saved).toHaveLength(0);
- expect((await invoke(h, { ...run, inputs: { query: 'test', missing: 'account' } })).verified).toBe(true);
+ expect((await invoke(h, { ...run, inputs: { query: 'test', missing: 'found' } })).verified).toBe(true);
const calls = h.calls.length;
- expect((await invoke(h, { ...run, inputs: { query: 'test', missing: 'account' } })).message).toContain('次数已用完');
+ expect((await invoke(h, { ...run, inputs: { query: 'test', missing: 'found' } })).message).toContain('次数已用完');
expect(h.calls).toHaveLength(calls);
} finally { await h.cleanup(); }
});
@@ -1256,11 +1257,38 @@ describe('recipe on-use maintenance', () => {
expect(h.calls).toHaveLength(0);
} finally { await h.cleanup(); }
});
- it('does not bypass password-handoff restrictions through candidate guards', async () => {
- const h = await makeHarness({ pageReadsRestricted: async () => true });
+ // #6822 dispatch checkpoint: permission denial is neither recipe aging nor a
+ // repair attempt. Exercise denial -> permission restored -> real execution,
+ // through separate evaluate guards and combined extract reads at the MCP entry.
+ it.each([
+ { candidate: true, extract: false }, { candidate: true, extract: true },
+ { candidate: false, extract: false }, { candidate: false, extract: true },
+ ])('preserves recipe state and repair budget across read restriction: %j', async ({ candidate, extract }) => {
+ const definition = extract ? { ...draft, steps: [{ action: 'extract' as const,
+ guard: draft.steps[0].guard, expect: draft.steps[0].expect,
+ extract: { fields: { title: 'h1' } }, as: 'items' }] } : draft;
+ const stored = { ...definition, validation: { site: 'example.com', state: candidate ? 'suspended' as const : 'active' as const } };
+ const saved: unknown[] = [];
+ let restricted = true;
+ const h = await makeHarness({
+ pageReadsRestricted: async () => restricted,
+ getUserRecipes: async () => ({ version: `restricted-${candidate}-${extract}`, recipes: new Map([[stored.id, stored]]), siteGuides: new Map() }),
+ saveUserRecipe: async (x) => { saved.push(x); return { ok: true }; },
+ }, (_req, i) => ({ result: extract ? { matched: true, value: { records: [{ title: 'found' }] } } : i === 0 ? true : { title: 'found' } }));
+ const run = candidate ? { action: 'recipe', site: 'example.com', recipeDraft: definition } : { action: 'recipe', recipeId: stored.id };
try {
- expect((await invoke(h, { action: 'recipe', site: 'example.com', recipeDraft: draft })).ok).toBe(false);
+ expect((await invoke(h, run)).ok).toBe(false);
expect(h.calls).toHaveLength(0);
+ expect(saved).toHaveLength(0);
+ restricted = false;
+ expect((await invoke(h, run)).ok).toBe(true);
+ expect(h.calls.length).toBeGreaterThan(0);
+ expect(saved).toHaveLength(0);
+ if (candidate) {
+ const calls = h.calls.length;
+ expect((await invoke(h, run)).message).toContain('次数已用完');
+ expect(h.calls).toHaveLength(calls);
+ }
} finally { await h.cleanup(); }
});
it('persists failure of a legacy recipe using its guide, without replacing it with a failed draft', async () => {
diff --git a/packages/lizi-mcps/src/browser/prompts/rules/recipe-author.md b/packages/lizi-mcps/src/browser/prompts/rules/recipe-author.md
index 04c0b38f14..f617f78c5c 100644
--- a/packages/lizi-mcps/src/browser/prompts/rules/recipe-author.md
+++ b/packages/lizi-mcps/src/browser/prompts/rules/recipe-author.md
@@ -59,6 +59,7 @@
- **分页**:第一版把页码/cursor 做成 `inputs`,一次配方跑一页,翻页多调几次。
- 偶发任务直接精确读取;只有重复使用、收益明确的流程才沉淀。正常 guard/expect 由代码执行,不调用额外模型、不定时刷新。
-- 配方失败后先观察当前页面;当前任务同一 id 最多试跑一轮修订,成功后保存,失败则普通操作完成任务。不要换 id 绕过预算。已有自动维护配方会暂停复用,跨任务保留,后续任务可重新验证修订版。
+- 配方失败后先观察当前页面;当前任务同一 id 最多试跑一轮修订,成功后保存,失败则普通操作完成任务。不要换 id 绕过预算。输入预检或敏感页面读取预检未下发调用时,不消耗修复机会,也不暂停已保存版本。已有自动维护配方实际执行失败会暂停复用,跨任务保留,后续任务可重新验证修订版。
+- `expect.minCount` 必须是正整数;`equals` 引用的变量在该步骤执行前必须已提供或由前一步产生,缺失属于输入错误。
- 写操作失败或后置读取失败,先核实写入是否已经生效;不要为了验证配方再次提交、发送、购买或发布。修复验证用只读检查、测试环境或下一次确有授权的操作。工具不会自动重放任何步骤。
- 不把密码、验证码、Cookie、令牌或实际业务数据写进配方,变动值用 inputs。
diff --git a/packages/lizi-mcps/src/browser/recipe-loader.test.ts b/packages/lizi-mcps/src/browser/recipe-loader.test.ts
index 65ef3bf730..2749f5fb72 100644
--- a/packages/lizi-mcps/src/browser/recipe-loader.test.ts
+++ b/packages/lizi-mcps/src/browser/recipe-loader.test.ts
@@ -42,6 +42,13 @@ describe('parseRecipes', () => {
expect(() => parseRecipes({ a: bad })).toThrow();
});
+ // #6822: the production parser must reject result checks that can verify an
+ // absent result. Existing shape tests did not cover expectation semantics (unit).
+ it.each([{ minCount: 0 }, {}])('rejects a vacuous result expectation: %j', (expectation) => {
+ expect(() => parseRecipes({ a: JSON.stringify({ id: 'empty-check',
+ steps: [{ action: 'evaluate', fn: '() => undefined', expect: expectation }] }) })).toThrow(/expect/);
+ });
+
it('throws on duplicate recipe id', () => {
expect(() =>
parseRecipes({ a: VALID_RECIPE, b: VALID_RECIPE }),
diff --git a/packages/lizi-mcps/src/browser/recipe-loader.ts b/packages/lizi-mcps/src/browser/recipe-loader.ts
index 46571ecdaf..50bbd1b8b1 100644
--- a/packages/lizi-mcps/src/browser/recipe-loader.ts
+++ b/packages/lizi-mcps/src/browser/recipe-loader.ts
@@ -27,7 +27,7 @@ const RecipeGuardSchema = z.object({
text: z.string().optional(),
});
const RecipeExpectationSchema = z.object({
- minCount: z.number().int().nonnegative().optional(),
+ minCount: z.number().int().positive().optional(),
requiredFields: z.array(z.string().min(1)).optional(),
equals: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(),
}).refine((v) => v.minCount !== undefined || Boolean(v.requiredFields?.length) || Boolean(Object.keys(v.equals ?? {}).length), 'expect must contain a result check');
diff --git a/packages/lizi-mcps/src/browser/recipe-runner.test.ts b/packages/lizi-mcps/src/browser/recipe-runner.test.ts
index 2acf566a3e..0cacd6231c 100644
--- a/packages/lizi-mcps/src/browser/recipe-runner.test.ts
+++ b/packages/lizi-mcps/src/browser/recipe-runner.test.ts
@@ -427,6 +427,17 @@ describe('findUnsafeEvaluateInterpolations', () => {
describe('recipe guards and output checks', () => {
+ // #6822: resolving equals after a browser call misclassified missing input as
+ // stale data. Unit production runner; both guarded read implementations must
+ // reject before any page execution, rather than suspending a saved recipe.
+ it.each(['extract', 'evaluate'] as const)('preflights equals variables before a guarded %s', async (action) => {
+ const m = mockCall([{ data: { result: { matched: true, value: { title: 'found' } } } }]);
+ const result = await runRecipe({ id: 'missing-equals', steps: [{ action,
+ fn: '() => ({ title: "found" })', extract: { fields: { title: 'h1' } },
+ guard: { url: 'https://example.com/' }, expect: { equals: { title: '{{missing}}' } } }] }, {}, m);
+ expect(result).toMatchObject({ ok: false, failureKind: 'input', steps: [] });
+ expect(m.calls).toHaveLength(0);
+ });
it.each([
{ visible: 1, hidden: 1, url: 'https://x.com/', text: 'account', ok: true },
{ visible: 2, hidden: 0, url: 'https://x.com/', text: 'account', ok: false },
diff --git a/packages/lizi-mcps/src/browser/recipe-runner.ts b/packages/lizi-mcps/src/browser/recipe-runner.ts
index 6e3f01034f..2262448af3 100644
--- a/packages/lizi-mcps/src/browser/recipe-runner.ts
+++ b/packages/lizi-mcps/src/browser/recipe-runner.ts
@@ -280,11 +280,11 @@ function guardRequest(step: RecipeStep, vars: Record<string, unknown>): BrowserC
/** Validate actual records, not a producer's claimed count. Empty results cannot
* satisfy required fields/equals through Array.every's vacuous truth. */
-function expectationError(step: RecipeStep, value: unknown, vars: Record<string, unknown>): string | undefined {
- if (!step.expect) return undefined;
+function expectationError(expectation: RecipeStep['expect'], value: unknown): string | undefined {
+ if (!expectation) return undefined;
const record = value && typeof value === 'object' ? value as Record<string, unknown> : undefined;
const rows = Array.isArray(value) ? value : Array.isArray(record?.records) ? record.records : record ? [record] : [];
- const { minCount, requiredFields, equals } = step.expect;
+ const { minCount, requiredFields, equals } = expectation;
if (minCount !== undefined && rows.length < minCount) return `expect: 至少 ${minCount} 条,实际 ${rows.length} 条`;
if ((requiredFields?.length || Object.keys(equals ?? {}).length) && !rows.length) return 'expect: 结果为空';
for (const row of rows) {
@@ -294,8 +294,7 @@ function expectationError(step: RecipeStep, value: unknown, vars: Record<string,
if (v == null || (typeof v === 'string' && !v.trim())) return `expect: 必需字段 ${field} 为空`;
}
for (const [field, expected] of Object.entries(equals ?? {})) {
- const target = typeof expected === 'string' ? interpolate(expected, vars) : expected;
- if ((row as Record<string, unknown>)[field] !== target) return `expect: 字段 ${field} 不匹配`;
+ if ((row as Record<string, unknown>)[field] !== expected) return `expect: 字段 ${field} 不匹配`;
}
}
return undefined;
@@ -356,10 +355,15 @@ export async function runRecipe(
for (let i = 0; i < recipe.steps.length; i++) {
const step = recipe.steps[i];
let req: BrowserControlRequest;
+ let expectation: RecipeStep['expect'];
let combinedGuard = false;
try {
req = stepToRequest(step, vars);
// Construct all input-dependent checks before touching the page.
+ expectation = step.expect && { ...step.expect,
+ equals: step.expect.equals && Object.fromEntries(Object.entries(step.expect.equals)
+ .map(([field, expected]) => [field, typeof expected === 'string' ? interpolate(expected, vars) : expected])),
+ };
const guard = guardRequest(step, vars);
if (guard && step.action === 'extract') {
// Read-only extraction can verify identity and read fields in one page
@@ -409,7 +413,7 @@ export async function runRecipe(
const innerError = innerEvaluateError(step, res);
let expectError: string | undefined;
try {
- if (res.ok && !innerError) expectError = expectationError(step, stepResultValue(step, res), vars);
+ if (res.ok && !innerError) expectError = expectationError(expectation, stepResultValue(step, res));
} catch (err) {
expectError = err instanceof Error ? err.message : String(err);
}
diff --git a/packages/lizi-mcps/src/browser/tools.ts b/packages/lizi-mcps/src/browser/tools.ts
index 18576f9361..fbb8facdb0 100644
--- a/packages/lizi-mcps/src/browser/tools.ts
+++ b/packages/lizi-mcps/src/browser/tools.ts
@@ -121,14 +121,14 @@ function readsPageState(request: BrowserControlRequest): boolean {
return request.action === 'act' && (act?.kind === 'evaluate' || (act?.kind === 'wait' && act.fn !== undefined));
}
-function getRuntime(deps: BrowserMcpDeps): BrowserControlRuntime {
+function getRuntime(deps: BrowserMcpDeps, onDispatch?: () => void): BrowserControlRuntime {
const runtime = deps.getRuntime();
const restricted = deps.pageReadsRestricted;
- if (!restricted) return runtime;
+ if (!restricted && !onDispatch) return runtime;
// Every path (tool actions, extract, recipes, hints) goes through this check.
return {
call: async (request) => {
- if (readsPageState(request)
+ if (restricted && readsPageState(request)
&& await restricted({
...(request.profile ? { profile: request.profile } : {}),
...(request.targetId ? { targetId: request.targetId } : {}),
@@ -141,6 +141,9 @@ function getRuntime(deps: BrowserMcpDeps): BrowserControlRuntime {
data: { reason: 'secret_entered' },
};
}
+ // This is the sole boundary after host preflight and before execution;
+ // callers must count attempts here, including throws/timeouts afterwards.
+ onDispatch?.();
return runtime.call(request);
},
};
@@ -676,7 +679,8 @@ export function registerBrowserTools(registry: BrowserToolRegistry, deps: Browse
);
}
}
- const runtime = getRuntime(deps);
+ let onRecipeDispatch: (() => void) | undefined;
+ const runtime = getRuntime(deps, () => onRecipeDispatch?.());
// Block non-web schemes at the boundary: navigate/open to file:// /
// chrome:// / data: would let the agent reach local files or browser
// internals. Only the scheme is constrained — localhost/private HTTP
@@ -766,12 +770,15 @@ export function registerBrowserTools(registry: BrowserToolRegistry, deps: Browse
// which is ambiguous with multiple tabs open). Once a navigate hands back
// a target, the whole recipe stays pinned to it.
let activeTargetId = args.targetId;
+ let lastCallDispatched = false;
+ onRecipeDispatch = () => {
+ lastCallDispatched = true;
+ if (candidate && suspended) repairUsed.add(id);
+ };
runningRecipes.add(id);
const runResult = await runRecipe(entry.recipe, args.inputs as Record<string, unknown> | undefined, {
call: async (req) => {
- // Input/request preflight can fail before touching the page. Only
- // an actual browser attempt consumes the single repair trial.
- if (candidate && suspended) repairUsed.add(id);
+ lastCallDispatched = false;
const res = await runtime.call({
...req,
...(args.profile ? { profile: args.profile } : {}),
@@ -786,7 +793,7 @@ export function registerBrowserTools(registry: BrowserToolRegistry, deps: Browse
let maintenanceMessage: string | undefined;
if (runResult.ok && candidate) {
verifiedDrafts.set(id, { site: args.site!, content: JSON.stringify(entry.recipe) });
- } else if (!runResult.ok && runResult.failureKind !== 'input' && runResult.failedStep !== undefined) {
+ } else if (!runResult.ok && lastCallDispatched && runResult.failureKind !== 'input' && runResult.failedStep !== undefined) {
// A failed draft is not evidence that the saved active version is
// stale. Keep its state, while limiting new/previously failed drafts.
if (!candidate || !persisted || suspended) failedRecipes.add(id);