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(bots): revalidate card actions and refresh restored reopen state

zqchris1aa4041b370a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
index 271590ffd0..7b2ac56b0d 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
@@ -239,6 +239,80 @@ describe('Bot authorization transcript lifecycle (Grok parity)', () => {
await flush();
await h.service.dispose();
});
+ it.each(['resolve', 'submit', 'reopen'])('rejects a hidden live card at the %s boundary', async (operation) => {
+ const h = harness();
+ h.adapter.execute = vi.fn(async (_a, _s, _v, onUrl) => {
+ onUrl?.('https://example.invalid/authorize');
+ return { ok: true as const, waitingExternal: true };
+ });
+ await h.service.request('s', { kind: 'host', id: 'grok' });
+ if (operation === 'reopen') {
+ await h.click();
+ await flush();
+ }
+ const card = structuredClone(h.card());
+ h.stored.clear(); // load no longer finds a visible row after clear or rewind
+ vi.mocked(h.adapter.execute).mockClear();
+ const command = {
+ kind: 'plugin_setup', action: 'run_action',
+ actionId: operation === 'reopen' ? 'reopen-authorization' : 'connect',
+ expectedRevision: card.snapshot.revision,
+ };
+ const accepted = operation === 'submit'
+ ? await h.service.submit(card.snapshot.requestId, { actionId: 'connect', expectedRevision: card.snapshot.revision, value: 'fake-secret' })
+ : await h.service.resolve(card.snapshot.requestId, command, h.sender);
+ expect(accepted).toBe(false);
+ await flush();
+ expect(h.adapter.execute).not.toHaveBeenCalled();
+ expect(h.deps.openExternal).not.toHaveBeenCalled();
+ expect(h.listeners.size).toBe(0);
+ expect(h.stored.size).toBe(0);
+ await h.service.dispose();
+ });
+
+ it('rechecks visibility after asynchronous assessment before starting OAuth', async () => {
+ const h = harness();
+ await h.service.request('s', { kind: 'host', id: 'grok' });
+ const assess = h.adapter.assess;
+ h.adapter.assess = async () => {
+ h.stored.clear();
+ return assess();
+ };
+ await h.click();
+ await flush();
+ expect(h.adapter.execute).not.toHaveBeenCalled();
+ expect(h.stored.size).toBe(0);
+ expect(h.listeners.size).toBe(0);
+ await h.service.dispose();
+ });
+
+ it('restoring a stale reopen action broadcasts its removal and accepts the obsolete click', async () => {
+ const h = harness();
+ await h.service.request('s', { kind: 'host', id: 'grok' });
+ const card = h.card();
+ card.snapshot.reopenActionId = 'reopen-authorization';
+ const oldRevision = card.snapshot.revision;
+ await h.service.dispose();
+ h.deps.save.mockClear();
+ const restored = new BotAuthorizationService(h.deps);
+ expect(await restored.resolve(card.snapshot.requestId, {
+ kind: 'plugin_setup', action: 'run_action', actionId: 'reopen-authorization',
+ expectedRevision: oldRevision,
+ }, h.sender)).toBe(true);
+ expect(h.deps.save).toHaveBeenCalled();
+ expect(h.card().snapshot.reopenActionId).toBeUndefined();
+ expect(h.card().snapshot.revision).toBeGreaterThan(oldRevision);
+ expect(h.deps.openExternal).not.toHaveBeenCalled();
+ expect(h.adapter.execute).not.toHaveBeenCalled();
+ await restored.resolve(card.snapshot.requestId, {
+ kind: 'plugin_setup', action: 'run_action', actionId: 'connect',
+ expectedRevision: h.card().snapshot.revision,
+ }, h.sender);
+ await flush();
+ expect(h.adapter.execute).toHaveBeenCalledTimes(1);
+ await restored.dispose();
+ });
+
it('disposal suppresses a callback from an outstanding action', async () => {
const h = harness();
let finish!: () => void;
diff --git a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
index bb6866a9f8..fdf70e6c7a 100644
--- a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
@@ -219,9 +219,20 @@ export class BotAuthorizationService {
entry.writes = write.catch(this.deps.warn);
return write;
}
+ private async isVisible(entry: Entry): Promise<boolean> {
+ await entry.writes;
+ if (entry.closed) return false;
+ const card = await this.deps.load(entry.card.snapshot.requestId);
+ if (entry.closed) return false;
+ if (!card || card.snapshot.terminal) {
+ this.close(entry);
+ return false;
+ }
+ return true;
+ }
private async get(requestId: string): Promise<Entry | null> {
const live = this.entries.get(requestId);
- if (live) return live;
+ if (live) return (await this.isVisible(live)) ? live : null;
let flight = this.restoring.get(requestId);
if (!flight) {
const epoch = this.epoch;
@@ -233,11 +244,20 @@ export class BotAuthorizationService {
card.completionPending ? { ...card.target, reauthorize: false } : card.target,
);
// A retained card can start a fresh flow; an old browser URL is never persisted.
- card.snapshot = { ...card.snapshot, reopenActionId: undefined };
+ const hadReopenAction = !!card.snapshot.reopenActionId;
+ card.snapshot = {
+ ...card.snapshot,
+ reopenActionId: undefined,
+ revision: card.snapshot.revision + (hadReopenAction ? 1 : 0),
+ };
const assessment = await adapter.assess();
if (epoch !== this.epoch) return null;
const entry = this.attach(card, adapter);
entry.assessmentFingerprint = JSON.stringify(assessment);
+ if (!(await this.isVisible(entry))) return null;
+ // Broadcast a newer revision before accepting a stale reopen click. The
+ // retained card can then start a new flow without a generic action error.
+ if (hadReopenAction) await this.save(entry);
return entry.closed ? null : entry;
})().finally(() => this.restoring.delete(requestId));
this.restoring.set(requestId, flight);
@@ -278,7 +298,7 @@ export class BotAuthorizationService {
if (command.actionId === 'reopen-authorization') {
if (!entry.authorizationUrl) return false;
await entry.adapter.assess();
- if (entry.closed || entry.cancelled) return false;
+ if (entry.cancelled || !(await this.isVisible(entry))) return false;
await this.deps.openExternal(entry.authorizationUrl);
return true;
}
@@ -310,7 +330,7 @@ export class BotAuthorizationService {
// Re-resolve the adapter at the action boundary: current owner, bot and plugin policy win.
await this.deps.adapter(entry.card.sessionId, entry.card.target);
const assessment = await entry.adapter.assess();
- if (entry.closed || entry.cancelled) return;
+ if (entry.cancelled || !(await this.isVisible(entry))) return;
entry.assessmentFingerprint = JSON.stringify(assessment);
if (assessment.state === 'ready') {
await this.check(entry);
@@ -327,7 +347,7 @@ export class BotAuthorizationService {
}
this.watch(entry);
await this.phase(entry, 'action_running', actionId);
- if (entry.closed || entry.cancelled) return;
+ if (entry.cancelled || !(await this.isVisible(entry))) return;
this.startPoll(entry);
const result = await this.execute(entry, action, sender, value, (url) => {
if (entry.closed || entry.cancelled) return;