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(plugin): 允许焦点解析失败后重试

DavidShenbd9d94500e80
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/sessionFocusRetryContract.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/sessionFocusRetryContract.test.ts
new file mode 100644
index 0000000000..16e9cc3d3f
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/__tests__/sessionFocusRetryContract.test.ts
@@ -0,0 +1,35 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+
+import { describe, expect, it } from 'vitest';
+
+describe('session focus retry wiring', () => {
+ const mainSource = readFileSync(
+ resolve(process.cwd(), 'src/main/cindy-brain/index.ts'),
+ 'utf8',
+ ).replace(/\r\n/g, '\n');
+ const gatewaySource = readFileSync(
+ resolve(process.cwd(), 'src/main/cindy-brain/subscriptionGateway.ts'),
+ 'utf8',
+ ).replace(/\r\n/g, '\n');
+
+ it('forwards same-id IPC focus reports to the primary tracker for retries', () => {
+ const start = mainSource.indexOf('function noteGhostWindowSessionFocused(');
+ const end = mainSource.indexOf('\n}\n\nfunction mainShellWindows', start);
+ const body = mainSource.slice(start, end);
+
+ expect(body).toContain('if (previous !== sessionId)');
+ expect(body).toContain('noteGhostSessionFocused(sessionId);');
+ expect(body).not.toContain('if (previous === sessionId) return;');
+ });
+
+ it('releases raw focus after a failed publication recheck', () => {
+ const start = gatewaySource.indexOf('export function createGhostPrimarySessionFocusTracker(');
+ const end = gatewaySource.indexOf('\n}\n\n/* ──', start);
+ const body = gatewaySource.slice(start, end);
+
+ expect(body).toContain('recheckedPrimarySessionId = await resolve(sessionId);');
+ expect(body).toContain('lastRawSessionId = null;');
+ expect(body).toContain('primarySessionId === lastPrimarySessionId');
+ });
+});
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/subscriptionGateway.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/subscriptionGateway.test.ts
index 9141592174..d1585a7fb4 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/subscriptionGateway.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/subscriptionGateway.test.ts
@@ -208,6 +208,27 @@ describe('createGhostPrimarySessionFocusTracker', () => {
expect(published).toEqual(['lead-a']);
});
+ it('allows the same focus to retry when the publication recheck fails', async () => {
+ let attempts = 0;
+ const pending: Array<{ sessionId: string; claim: () => Promise<boolean> }> = [];
+ const tracker = createGhostPrimarySessionFocusTracker(
+ async () => {
+ attempts += 1;
+ return attempts === 2 ? null : 'lead';
+ },
+ (sessionId, claim) => pending.push({ sessionId, claim }),
+ );
+
+ tracker.note('worker');
+ await vi.waitFor(() => expect(pending).toHaveLength(1));
+ await expect(pending[0]?.claim()).resolves.toBe(false);
+
+ tracker.note('worker');
+ await vi.waitFor(() => expect(pending).toHaveLength(2));
+ await expect(pending[1]?.claim()).resolves.toBe(true);
+ expect(attempts).toBe(4);
+ });
+
it('clears deduplication when focus explicitly leaves the session', async () => {
const published: string[] = [];
const notify = vi.fn(async (sessionId: string, claim: () => Promise<boolean>) => {
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 8c8ad882b8..9216927b3f 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -1929,17 +1929,20 @@ function noteGhostWindowSessionFocused(sender: WebContents, sessionId: string |
// Renderer route reports are not an authority to grant Simulator access. They
// may only remove a stale Main-owned grant when this window family moves away
// from the task for which it was explicitly authorized.
- revokeIOSSimulatorRendererAccessForSessionChange(sender, sessionId);
const previous = ghostSessionFocusByWebContents.get(sender.id);
- if (previous === sessionId) return;
- ghostSessionFocusByWebContents.set(sender.id, sessionId);
- if (!ghostSessionFocusTrackedWebContents.has(sender.id)) {
- ghostSessionFocusTrackedWebContents.add(sender.id);
- sender.once('destroyed', () => {
- ghostSessionFocusByWebContents.delete(sender.id);
- ghostSessionFocusTrackedWebContents.delete(sender.id);
- });
+ if (previous !== sessionId) {
+ revokeIOSSimulatorRendererAccessForSessionChange(sender, sessionId);
+ ghostSessionFocusByWebContents.set(sender.id, sessionId);
+ if (!ghostSessionFocusTrackedWebContents.has(sender.id)) {
+ ghostSessionFocusTrackedWebContents.add(sender.id);
+ sender.once('destroyed', () => {
+ ghostSessionFocusByWebContents.delete(sender.id);
+ ghostSessionFocusTrackedWebContents.delete(sender.id);
+ });
+ }
}
+ // Keep forwarding same-id reports: the primary tracker may have cleared its
+ // raw-id dedupe marker after a transient resolution failure and needs a retry.
noteGhostSessionFocused(sessionId);
}
diff --git a/apps/desktop/src/main/cindy-brain/subscriptionGateway.ts b/apps/desktop/src/main/cindy-brain/subscriptionGateway.ts
index 6c479ae552..fa5cd1404f 100644
--- a/apps/desktop/src/main/cindy-brain/subscriptionGateway.ts
+++ b/apps/desktop/src/main/cindy-brain/subscriptionGateway.ts
@@ -655,7 +655,19 @@ export function createGhostPrimarySessionFocusTracker(
notify(primarySessionId, async () => {
if (currentGeneration !== generation) return false;
// Team 归属也可能在首次解析后变化;发布前重新解析,绝不投递过期 Lead。
- if ((await resolve(sessionId)) !== primarySessionId) return false;
+ let recheckedPrimarySessionId: string | null;
+ try {
+ recheckedPrimarySessionId = await resolve(sessionId);
+ } catch {
+ if (currentGeneration === generation) lastRawSessionId = null;
+ return false;
+ }
+ if (recheckedPrimarySessionId !== primarySessionId) {
+ // 发布前复查失败或映射已变化时,释放 raw marker;同一焦点的后续
+ // 上报必须能够重新归一,而已发布的 primary 去重状态保持不变。
+ if (currentGeneration === generation) lastRawSessionId = null;
+ return false;
+ }
if (currentGeneration !== generation) return false;
if (primarySessionId === lastPrimarySessionId) return false;
lastPrimarySessionId = primarySessionId;