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): veto cancelled continuations and retire failed OAuth links

zqchrisd5d42416bb31
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts
index d951648c8d..f3650cf0a9 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts
@@ -134,7 +134,7 @@ describe('authorization Host frozen plugin policy', () => {
await expect(state.deps.adapter('session', cardTarget)).rejects.toThrow('teammate is unavailable');
await expect(adapter.assess()).rejects.toThrow('teammate is unavailable');
await expect(adapter.execute({ id: 'connect', kind: 'oauth_connect' }, undefined)).rejects.toThrow('teammate is unavailable');
- await expect(state.deps.resume({ sessionId: 'session', target: cardTarget } as BotAuthorizationCard)).rejects.toThrow('teammate is unavailable');
+ await expect(state.deps.resume({ sessionId: 'session', target: cardTarget } as BotAuthorizationCard, () => {})).rejects.toThrow('teammate is unavailable');
}
expect(state.execute).not.toHaveBeenCalled();
expect(state.login).not.toHaveBeenCalled();
@@ -153,7 +153,7 @@ describe('authorization Host frozen plugin policy', () => {
state.policy = JSON.stringify({ toolsets: [] });
await expect(adapter.assess()).rejects.toThrow('disabled in teammate profile');
await expect(adapter.execute({ id: 'connect', kind: 'oauth_connect' }, undefined)).rejects.toThrow('disabled in teammate profile');
- await expect(state.deps.resume({ sessionId: 'session', target } as BotAuthorizationCard)).rejects.toThrow('disabled in teammate profile');
+ await expect(state.deps.resume({ sessionId: 'session', target } as BotAuthorizationCard, () => {})).rejects.toThrow('disabled in teammate profile');
expect(state.execute).not.toHaveBeenCalled();
});
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 6718eab9ec..80abdda4ad 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
@@ -42,7 +42,7 @@ function harness() {
},
execute: vi.fn(async () => ({ ok: true as const, waitingExternal: true })),
};
- const resume = vi.fn(async (_card: BotAuthorizationCard) => {});
+ const resume = vi.fn(async (_card: BotAuthorizationCard, _assertCurrent: () => void) => {});
const deps = {
adapter: vi.fn(async () => adapter),
save: vi.fn(async (card: BotAuthorizationCard) => {
@@ -604,3 +604,45 @@ describe('authorization input CAS', () => {
await h.service.dispose();
});
});
+
+it('cancellation vetoes a continuation waiting to enqueue', async () => {
+ const h = harness();
+ let release!: () => void;
+ const enqueue = vi.fn();
+ h.deps.resume.mockImplementationOnce(async (_card, assertCurrent) => {
+ await new Promise<void>((resolve) => { release = resolve; });
+ await commitBotAuthorizationInput({ validate: async () => {}, assertCurrent }, enqueue);
+ });
+ await h.service.request('s', { kind: 'plugin', id: 'p' });
+ h.complete();
+ await flush();
+ expect(release).toBeTypeOf('function');
+ await h.service.resolve(h.card().snapshot.requestId, {
+ kind: 'plugin_setup', action: 'cancel', expectedRevision: h.card().snapshot.revision,
+ });
+ release();
+ await flush();
+ expect(enqueue).not.toHaveBeenCalled();
+ expect(h.card().snapshot.steps[0].phase).toBe('cancelled');
+ await h.service.dispose();
+});
+
+it.each(['result', 'throw'] as const)('removes the expired OAuth reopen action after failure via %s', async (failure) => {
+ const h = harness();
+ h.adapter.execute = vi.fn(async (_a, _s, _v, onUrl) => {
+ onUrl?.('https://example.invalid/authorize?state=expired');
+ if (failure === 'throw') throw new Error('OAuth timed out');
+ return { ok: false as const, errorCode: 'TIMEOUT' as const };
+ });
+ await h.service.request('s', { kind: 'host', id: 'grok' });
+ await h.click();
+ await flush();
+ expect(h.card().snapshot.steps[0].phase).toBe('failed');
+ expect(h.card().snapshot.reopenActionId).toBeUndefined();
+ await h.service.resolve(h.card().snapshot.requestId, {
+ kind: 'plugin_setup', action: 'run_action', actionId: 'reopen-authorization',
+ expectedRevision: h.card().snapshot.revision,
+ });
+ expect(h.deps.openExternal).not.toHaveBeenCalled();
+ await h.service.dispose();
+});
diff --git a/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts b/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts
index ee79e73b4c..fc4ae285c7 100644
--- a/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts
+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts
@@ -63,7 +63,7 @@ export async function isBotAuthorizationSession(sessionId: string): Promise<bool
/** Adapts existing Host credentials and plugin configuration into the same card lifecycle. */
export function initializeBotAuthorizationHost(
- resume: (card: BotAuthorizationCard, validate: () => Promise<void>) => Promise<void>,
+ resume: (card: BotAuthorizationCard, validate: () => Promise<void>, assertCurrent: () => void) => Promise<void>,
) {
const ownerScopes = new Map<string, ReturnType<typeof captureDataOwnerBroadcastScope>>();
const assertPluginPolicy = async (sessionId: string, pluginId: string) => {
@@ -350,7 +350,7 @@ export function initializeBotAuthorizationHost(
}
return null;
},
- async resume(card) {
+ async resume(card, assertCurrent) {
await resume(card, async () => {
await assertSession(card.sessionId);
if (card.target.kind === 'plugin')
@@ -370,7 +370,7 @@ export function initializeBotAuthorizationHost(
.limit(1);
if (!row) throw new Error('Authorization card is no longer visible');
await assertSession(card.sessionId);
- });
+ }, assertCurrent);
},
onDisposing: () => cancelGrokOAuthLogin(),
onDispose: () => ownerScopes.clear(),
diff --git a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
index 452ec27d76..fb3f3e3039 100644
--- a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
@@ -65,7 +65,7 @@ export interface BotAuthorizationDeps {
sessionId: string,
target: BotAuthorizationTarget,
): Promise<BotAuthorizationCard | null>;
- resume(card: BotAuthorizationCard): Promise<void>;
+ resume(card: BotAuthorizationCard, assertCurrent: () => void): Promise<void>;
warn(error: unknown): void;
openExternal(url: string): Promise<void>;
onDisposing?(): void;
@@ -446,6 +446,10 @@ export class BotAuthorizationService {
errorCode?: 'TIMEOUT' | 'ACTION_FAILED' | import('../../shared/ghost.js').GhostSetupErrorCode,
) {
if (entry.closed || entry.cancelled) return;
+ if (phase === 'failed') {
+ entry.authorizationUrl = undefined;
+ delete entry.card.snapshot.reopenActionId;
+ }
entry.card.snapshot = {
...entry.card.snapshot,
revision: entry.card.snapshot.revision + 1,
@@ -517,7 +521,9 @@ export class BotAuthorizationService {
return;
}
if (entry.closed || entry.cancelled) return;
- await this.deps.resume(entry.card);
+ await this.deps.resume(entry.card, () => {
+ if (entry.closed || entry.cancelled) throw new Error('Authorization was cancelled');
+ });
if (entry.closed || entry.cancelled) return;
entry.card.snapshot = completedSnapshot;
delete entry.card.completionPending;
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 5a7e2ddcbf..9aec08e9b8 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -8973,12 +8973,13 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
return sendToSessionInternal(params);
};
- initializeBotAuthorizationHost(async (card, validate) => {
+ initializeBotAuthorizationHost(async (card, validate, assertAuthorizationCurrent) => {
await inputCoordinator.ensureQueueRestored(card.sessionId);
const generation = inputCoordinator.getGeneration(card.sessionId);
const authorizationGuard: BotAuthorizationInputGuard = {
validate,
assertCurrent: () => {
+ assertAuthorizationCurrent();
assertRemoteInputClearNotInFlight(card.sessionId, true);
if (rewindInputSessions.has(card.sessionId) || !inputCoordinator.isGenerationCurrent(card.sessionId, generation)) {
throw new Error('Authorization input boundary changed');