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): fence pre-clear requests and close failed cancellations

zqchrisc32a7db19b30
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 f3650cf0a9..c23bf59aa2 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationHost.test.ts
@@ -76,6 +76,21 @@ describe('authorization Host frozen plugin policy', () => {
});
});
+ it('rejects a card prepared before a clear even when its timestamp is newer', async () => {
+ let generation = 0;
+ initializeBotAuthorizationHost(async () => {}, () => {
+ const captured = generation;
+ return () => { if (generation !== captured) throw new Error('cleared'); };
+ });
+ const assertCurrent = state.deps.captureRequestGuard!('session');
+ await state.deps.adapter('session', target);
+ generation++;
+ await expect(state.deps.save({ v: 1, sessionId: 'session', target, createdAt: Date.now(),
+ snapshot: { kind: 'plugin_setup', requestId: 'pre-clear', revision: 1,
+ ghost: { id: 'art', name: 'Art' }, steps: [] } }, assertCurrent)).rejects.toThrow('cleared');
+ expect(state.save).not.toHaveBeenCalled();
+ });
+
it('rejects a prepared card when rewind wins the shared write boundary', async () => {
await state.deps.adapter('session', target);
const assertCurrent = state.deps.captureRequestGuard!('session');
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 80abdda4ad..c8ce6bf5c8 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botAuthorizationService.test.ts
@@ -646,3 +646,18 @@ it.each(['result', 'throw'] as const)('removes the expired OAuth reopen action a
expect(h.deps.openExternal).not.toHaveBeenCalled();
await h.service.dispose();
});
+
+it('retires a cancelled entry when its terminal save fails so a later request is actionable', async () => {
+ const h = harness();
+ await h.service.request('s', { kind: 'plugin', id: 'p' });
+ h.deps.save.mockRejectedValueOnce(new Error('profile paused'));
+ await expect(h.service.resolve(h.card().snapshot.requestId, {
+ kind: 'plugin_setup', action: 'cancel', expectedRevision: h.card().snapshot.revision,
+ })).rejects.toThrow('profile paused');
+ expect(h.listeners.size).toBe(0);
+ await h.service.request('s', { kind: 'plugin', id: 'p' });
+ await h.click();
+ await flush();
+ expect(h.adapter.execute).toHaveBeenCalledTimes(1);
+ 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 fc4ae285c7..d90ddb0f5d 100644
--- a/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts
+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts
@@ -64,6 +64,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>, assertCurrent: () => void) => Promise<void>,
+ captureInputGuard: (sessionId: string) => () => void = () => () => {},
) {
const ownerScopes = new Map<string, ReturnType<typeof captureDataOwnerBroadcastScope>>();
const assertPluginPolicy = async (sessionId: string, pluginId: string) => {
@@ -252,8 +253,10 @@ export function initializeBotAuthorizationHost(
};
},
captureRequestGuard(sessionId) {
+ const assertInputCurrent = captureInputGuard(sessionId);
const generation = getSessionRewindGeneration(sessionId);
return () => {
+ assertInputCurrent();
if (getSessionRewindGeneration(sessionId) !== generation)
throw new Error('Authorization request was rewound');
};
@@ -280,6 +283,7 @@ export function initializeBotAuthorizationHost(
const phase = card.snapshot.steps.find((s) => s.phase !== 'satisfied')?.phase ?? 'satisfied';
const fallback = `${card.snapshot.ghost.name} · ${t(`newChat.pluginSetup.phase.${phase}`)}${card.snapshot.terminal ? '' : ` · ${t('newChat.pluginSetup.completeOnDesktop')}`}`;
// Only the presentation is stored. Current assessment/actions are re-read on every click.
+ assertCurrent?.();
await createMessage(
card.sessionId,
{
diff --git a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
index fb3f3e3039..4293f38b3f 100644
--- a/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationService.ts
@@ -293,8 +293,11 @@ export class BotAuthorizationService {
action: undefined,
})),
};
- await this.save(entry);
- this.close(entry);
+ try {
+ await this.save(entry);
+ } finally {
+ this.close(entry);
+ }
return true;
}
if (!sender) return false;
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 9aec08e9b8..4554a3786a 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -8990,6 +8990,13 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
const result = await dispatchBotSessionMessage({ ...buildBotAuthorizationContinuation(card), authorizationGuard });
if (!result.ok) throw new Error('Authorization continuation not accepted');
await awaitAgentInputQueueSnapshotPersistence(card.sessionId);
+ }, (sessionId) => {
+ const generation = inputCoordinator.getGeneration(sessionId);
+ return () => {
+ assertRemoteInputClearNotInFlight(sessionId, true);
+ if (!inputCoordinator.isGenerationCurrent(sessionId, generation))
+ throw new Error('Authorization request input boundary changed');
+ };
});
botDirectMessageServiceHolder = createBotDirectMessageService({