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): 登录复用切换统一使旧配方证据失效

Dash99701a9f3156
diff --git a/apps/desktop/src/main/mcp-integrations/browser-backend/__tests__/controller.test.ts b/apps/desktop/src/main/mcp-integrations/browser-backend/__tests__/controller.test.ts
index e9168f41fa..fbbb0a594b 100644
--- a/apps/desktop/src/main/mcp-integrations/browser-backend/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/browser-backend/__tests__/controller.test.ts
@@ -51,7 +51,7 @@ describe('BrowserBackendController', () => {
release();
await changed;
const applied = controller.getBackendScope();
- expect(applied.lifecycle).toBe(queued.lifecycle);
+ expect(applied.lifecycle).not.toBe(queued.lifecycle);
expect(applied.backend).not.toBe(queued.backend);
await controller.setKind('external');
const returned = controller.getBackendScope();
@@ -59,6 +59,60 @@ describe('BrowserBackendController', () => {
expect(returned.lifecycle).not.toBe(initial.lifecycle);
});
+ // Same failure family as backend switching: consent changes must invalidate
+ // evidence even when kind/profile later return to their original values.
+ // Unit/host protocol: queued and settled changes differ; ordinary page work
+ // does not change the scope. This distinguishes the previous partial fix.
+ it.each(['resolve', 'reject'] as const)('scopes profile consent changes without another queue or backend selection (%s)', async outcome => {
+ const profileQueue = createBrowserProfileLifecycleQueue();
+ const controller = new BrowserBackendController({
+ initialKind: 'external', externalBackend: fakeBackend('external'),
+ createRsbBackend: () => fakeBackend('rsb-webview'), logger: fakeLogger(),
+ });
+ let enabled = true;
+ const initial = controller.getBackendScope();
+ await profileQueue.run(async () => undefined); // Ordinary page work.
+ expect(controller.getBackendScope()).toEqual(initial);
+ let release!: () => void;
+ const change = controller.runLifecycle(() => profileQueue.run(async () => {
+ await new Promise<void>(resolve => { release = resolve; });
+ if (outcome === 'reject') throw new Error('stop failed');
+ enabled = false;
+ return enabled;
+ }));
+ const queued = controller.getBackendScope();
+ expect(queued.lifecycle).not.toBe(initial.lifecycle);
+ expect(queued.backend).toBe(initial.backend);
+ await vi.waitFor(() => expect(release).toBeTypeOf('function'));
+ const settled = outcome === 'reject'
+ ? expect(change).rejects.toThrow('stop failed') : expect(change).resolves.toBe(false);
+ let releaseLater!: () => void;
+ const later = controller.runLifecycle(() => profileQueue.run(() => new Promise<void>(resolve => { releaseLater = resolve; })));
+ const bothQueued = controller.getBackendScope();
+ release();
+ await settled;
+ expect(controller.getBackendScope().lifecycle).not.toBe(queued.lifecycle);
+ const firstSettled = controller.getBackendScope();
+ await vi.waitFor(() => expect(releaseLater).toBeTypeOf('function'));
+ releaseLater();
+ await later;
+ expect(firstSettled.lifecycle).not.toBe(bothQueued.lifecycle);
+ expect(controller.getBackendScope().lifecycle).not.toBe(firstSettled.lifecycle);
+ expect(enabled).toBe(outcome === 'reject');
+ await controller.runLifecycle(() => profileQueue.run(async () => { enabled = true; }));
+ expect(enabled).toBe(true);
+ expect(controller.getBackendScope().lifecycle).not.toBe(initial.lifecycle);
+ expect(controller.getBackendScope().backend).toBe(initial.backend);
+ // A later backend choice cannot be undone by a profile setter that
+ // captured the earlier kind; the lifecycle operation never selects it.
+ await Promise.all([
+ controller.setKind('rsb-webview'),
+ controller.runLifecycle(() => profileQueue.run(async () => { enabled = false; })),
+ ]);
+ expect(controller.kind).toBe('rsb-webview');
+ expect(enabled).toBe(false);
+ });
+
it('serializes reset cleanup after outgoing profile disposal without deadlocking', async () => {
const queue = createBrowserProfileLifecycleQueue();
const external = fakeBackend('external');
diff --git a/apps/desktop/src/main/mcp-integrations/browser-backend/controller.ts b/apps/desktop/src/main/mcp-integrations/browser-backend/controller.ts
index 2ca137c6b5..ccc0698de9 100644
--- a/apps/desktop/src/main/mcp-integrations/browser-backend/controller.ts
+++ b/apps/desktop/src/main/mcp-integrations/browser-backend/controller.ts
@@ -58,7 +58,7 @@ export class BrowserBackendController implements BrowserBackend {
kind: BackendKind,
persistKind: ((kind: BackendKind) => void | Promise<void>) | undefined = this.opts.persistKind,
): Promise<boolean> {
- return this.enqueue(async () => {
+ return this.runLifecycle(async () => {
const changed = this.router.getCurrentBackendKind() !== kind;
const next = changed ? this.createBackend(kind) : undefined;
// Save before activation: a disk failure must leave the old backend live.
@@ -72,7 +72,7 @@ export class BrowserBackendController implements BrowserBackend {
/** Replace the active embedded backend even though its kind is unchanged. */
restartEmbedded(): Promise<boolean> {
- return this.enqueue(async () => {
+ return this.runLifecycle(async () => {
if (this.router.getCurrentBackendKind() !== 'rsb-webview') return false;
await this.router.restartBackend(this.createBackend('rsb-webview'));
return true;
@@ -80,7 +80,7 @@ export class BrowserBackendController implements BrowserBackend {
}
dispose(): Promise<void> {
- return this.enqueue(() => this.router.dispose());
+ return this.runLifecycle(() => this.router.dispose());
}
private createBackend(kind: BackendKind): BrowserBackend {
@@ -91,12 +91,23 @@ export class BrowserBackendController implements BrowserBackend {
return backend;
}
- private enqueue<T>(operation: () => Promise<T>): Promise<T> {
+ /**
+ * One owner for backend and copied-login identity changes. Callers that also
+ * need the profile queue acquire it inside this operation, matching reset.
+ * Ordinary page calls bypass this lifecycle queue.
+ */
+ runLifecycle<T>(operation: () => Promise<T>): Promise<T> {
const result = this.transition.then(operation, operation);
- this.transition = result.then(
+ const settled = result.then(
() => undefined,
() => undefined,
);
- return result;
+ this.transition = settled;
+ return result.finally(() => {
+ // Evidence produced during a same-kind change must expire on settlement,
+ // including when another change is still queued. Preserve that entire
+ // queue in the new tail instead of resetting it to an already-done promise.
+ this.transition = this.transition.then(() => undefined);
+ });
}
}
diff --git a/apps/desktop/src/main/mcp-integrations/browser.ts b/apps/desktop/src/main/mcp-integrations/browser.ts
index b7e8786145..9091ea1ddb 100644
--- a/apps/desktop/src/main/mcp-integrations/browser.ts
+++ b/apps/desktop/src/main/mcp-integrations/browser.ts
@@ -447,7 +447,9 @@ async function applyBrowserUseRealProfile(enabled: boolean): Promise<boolean> {
}
export function setBrowserUseRealProfile(enabled: boolean): Promise<boolean> {
- return browserProfileLifecycleQueue.run(() => applyBrowserUseRealProfile(enabled));
+ return backendController.runLifecycle(() =>
+ browserProfileLifecycleQueue.run(() => applyBrowserUseRealProfile(enabled)),
+ );
}
/**
diff --git a/packages/browser-control-runtime/upstream/MAINTAINING.md b/packages/browser-control-runtime/upstream/MAINTAINING.md
index 8dcd46aae9..7acc8c8d4d 100644
--- a/packages/browser-control-runtime/upstream/MAINTAINING.md
+++ b/packages/browser-control-runtime/upstream/MAINTAINING.md
@@ -208,7 +208,7 @@ pnpm --filter @cindy/browser-control-runtime test # 契约 + SSRF guard
### 经验缓存与按需学习(2026-10-27)
- Desktop `resolveProfile` 先把省略值解析为实际 `Cindy` / `Cindy-real` 或嵌入端唯一 `rsb`;发现、试跑、保存、执行与补读检查共用该值,不能把空字符串当实际身份。显式 profile 和解析后的默认 profile 等价,旧配方不新增隐式身份限定。
-- `getBackendScope` 只读复用 controller 的既有 transition Promise 与 router 的当前后端引用,不新增计数器、锁或队列。任一变化使会话原文、发现缓存、未完成观察与试跑凭证失效;异步预检后和结果返回时再次核对,避免设置或另一任务切换宿主后继续引用旧页面。动作已成功时保留成功,切换后的失败不误标配方过时,不重放。
+- `getBackendScope` 只读复用 controller 的既有 transition Promise 与 router 的当前后端引用,不新增计数器、锁或队列。`setKind`、reset、恢复、dispose 和登录复用 setter 共用 `runLifecycle`,按 controller → profile 的顺序复用既有队列;每次受理和 resolve/reject 后更换 Promise 引用,结算时保留整条后续队列,不让同名 profile 关闭/重开或排队期间生成的证据复活。普通页面调用不走生命周期入口。scope 变化使会话原文、发现缓存、未完成观察与试跑凭证失效;异步预检后和结果返回时再次核对。动作已成功时保留成功,身份变化后的失败不误标配方过时,不重放。
- `open/navigate` 随结果返回最多三条站点配方候选,可用 `intent` 进行本地词匹配;匹配仅建议,不执行。域名按边界匹配,profile 不匹配或 suspended 不建议;同任务同站点/意图/目录版本不重复提示。目录内容变化后重新发现。`siteguide(site,intent)` 支持主动窄查询,旧完整目录接口保留。
- `list_tools` 仅加载通用工作流,`recipeHelp` 才提供完整编写手册。当前 Agent 结合已完成步骤整理,不另起后台模型、不扫描完整聊天。重复形状观察仅留动作枚举和站点,第二次才提示考虑学习;这不是业务成功证明,不能自动猜参数或自动执行配方。
- 流程由成功导航开始,按 tab/profile 分开,第一次成功 extract/evaluate 结束;错误、敏感页、配方执行和后端切换清除未完成观察。只作为廉价学习线索,不完整覆盖含多次读取或跨站导航的业务流程。持久结构观察与现有配方共用站点目录,具体边界见 credentials-and-local-storage.md。