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(desktop): cache unsupported cursor capability per session (#3688)

Dasheedec22b76b1
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/computer.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/computer.test.ts
index 76e765ad93..3baaa458a4 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/computer.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/computer.test.ts
@@ -1124,7 +1124,49 @@ describe('computer mcp integration', () => {
expect(mcpConnectMock).toHaveBeenCalledTimes(1);
});
- it('retries unsupported cursor setup after the MCP session is recreated', async () => {
+ it('does not retry unsupported cursor setup after stale driver recovery', async () => {
+ mcpCallToolMock
+ .mockResolvedValueOnce({ content: [{ type: 'text', text: '{"ok":true}' }] })
+ .mockResolvedValueOnce({
+ isError: true,
+ content: [{ type: 'text', text: "Permission denied: tool 'set_agent_cursor_style' has no reviewed risk classification" }],
+ })
+ .mockImplementationOnce((call: { name: string; arguments: Record<string, unknown> }) => ({
+ isError: true,
+ content: [{
+ type: 'text',
+ text: `session '${String(call.arguments.session)}' has ended; tool call '${call.name}' was rejected`,
+ }],
+ }))
+ .mockResolvedValueOnce({ content: [{ type: 'text', text: '{"ok":true}' }] })
+ .mockResolvedValueOnce({ content: [{ type: 'text', text: '{"ok":true}' }] })
+ .mockResolvedValueOnce({ content: [{ type: 'text', text: '{"ok":true,"clicked":true}' }] });
+
+ await expect(callComputerDriverTool('click', {
+ pid: 123,
+ window_id: 7,
+ x: 10,
+ y: 20,
+ }, { sessionId: 'session-style-stale-recovery' })).resolves.toEqual({ ok: true, clicked: true });
+
+ expect(mcpCallToolMock.mock.calls.map((call) => call[0]?.name)).toEqual([
+ 'set_agent_cursor_motion',
+ 'set_agent_cursor_style',
+ 'click',
+ 'end_session',
+ 'set_agent_cursor_motion',
+ 'click',
+ ]);
+ expect(mcpConnectMock).toHaveBeenCalledTimes(2);
+ expect(mcpCloseMock).toHaveBeenCalledTimes(1);
+ const clickSessions = mcpCallToolMock.mock.calls
+ .map((call) => call[0])
+ .filter((call) => call?.name === 'click')
+ .map((call) => (call?.arguments as { session: string }).session);
+ expectDriverSessionGenerations(clickSessions, 'session-style-stale-recovery', [0, 1]);
+ });
+
+ it('re-probes unsupported cursor setup after the logical MCP session is closed', async () => {
mcpCallToolMock
.mockResolvedValueOnce({ content: [{ type: 'text', text: '{"ok":true}' }] })
.mockResolvedValueOnce({
diff --git a/apps/desktop/src/main/mcp-integrations/computer.ts b/apps/desktop/src/main/mcp-integrations/computer.ts
index 421fc930a9..40c58937ca 100644
--- a/apps/desktop/src/main/mcp-integrations/computer.ts
+++ b/apps/desktop/src/main/mcp-integrations/computer.ts
@@ -278,6 +278,7 @@ interface DaemonStatus {
}
interface CuaMcpSessionEntry {
+ logicalSessionId: string;
client: Client;
transport: StdioClientTransport;
ready: Promise<void>;
@@ -288,6 +289,12 @@ interface CuaMcpSessionEntry {
};
}
type CursorSetupState = 'pending' | 'applied' | 'unavailable';
+type CursorCapabilityState = 'unknown' | 'unavailable';
+
+interface CuaMcpSessionCursorCapabilities {
+ motion: CursorCapabilityState;
+ style: CursorCapabilityState;
+}
interface ProcessSnapshotEntry {
pid: number;
@@ -523,6 +530,9 @@ function probeDriverPermissionsOnce(options: { forceNew?: boolean } = {}): Promi
}
const cuaMcpSessions = new Map<string, CuaMcpSessionEntry>();
const cuaMcpSessionCleanups = new Map<string, Promise<void>>();
+// 永久的能力/策略拒绝属于逻辑 MCP session,而不是某一代 driver transport。
+// applied/pending 仍保留在 entry 内,因为新 generation 必须重新应用成功的设置。
+const cuaMcpSessionCursorCapabilities = new Map<string, CuaMcpSessionCursorCapabilities>();
const cuaDriverSessionGenerations = new Map<string, number>();
const cuaMcpSessionCloseVersions = new Map<string, number>();
@@ -2114,13 +2124,15 @@ function createCuaMcpSession(sessionId: string): CuaMcpSessionEntry {
name: 'xdt-maker-cua-driver',
version: '0.1.0',
});
+ const capabilities = cuaMcpSessionCursorCapabilities.get(sessionId);
const entry: CuaMcpSessionEntry = {
+ logicalSessionId: sessionId,
client,
transport,
driverSessionId: getDriverSessionId(sessionId),
cursorSetup: {
- motion: 'pending',
- style: 'pending',
+ motion: capabilities?.motion === 'unavailable' ? 'unavailable' : 'pending',
+ style: capabilities?.style === 'unavailable' ? 'unavailable' : 'pending',
},
ready: withTimeout(
client.connect(transport),
@@ -2267,6 +2279,7 @@ async function initializeDefaultCursorStyle(
_args: Record<string, unknown>,
entry: CuaMcpSessionEntry,
session: string,
+ sessionCloseVersion: number,
): Promise<void> {
if (!CURSOR_STYLED_TOOL_NAMES.has(name)) return;
@@ -2308,6 +2321,16 @@ async function initializeDefaultCursorStyle(
const message = err instanceof Error ? err.message : String(err);
if (isCursorSetupUnavailableError(message)) {
entry.cursorSetup[call.stateKey] = 'unavailable';
+ if (getCuaMcpSessionCloseVersion(entry.logicalSessionId) === sessionCloseVersion) {
+ const capabilities = cuaMcpSessionCursorCapabilities.get(entry.logicalSessionId) ?? {
+ motion: 'unknown',
+ style: 'unknown',
+ };
+ cuaMcpSessionCursorCapabilities.set(entry.logicalSessionId, {
+ ...capabilities,
+ [call.stateKey]: 'unavailable',
+ });
+ }
logger.info('cua-driver cursor setup unavailable; continuing without it', {
tool: name,
cursorTool: call.tool,
@@ -2327,9 +2350,10 @@ async function initializeDefaultCursorStyle(
}
/**
- * Driver capability/policy failures cannot recover while the same MCP process
- * remains alive. Remember them on the session entry so an optional cursor
- * decoration never adds a rejected tool call to every real computer action.
+ * Driver capability/policy failures cannot recover while the same logical MCP
+ * session remains alive. Remember them outside the generation-specific entry
+ * so an optional cursor decoration never adds a rejected tool call after a
+ * stale driver transport is replaced.
*/
function isCursorSetupUnavailableError(message: string): boolean {
return /has no reviewed risk classification|method not found|unknown tool|tool .+ not found/i.test(message);
@@ -3325,7 +3349,7 @@ export async function callComputerDriverTool(
const entry = await getCuaMcpSession(sessionId);
assertComputerDriverToolDispatchAvailable();
const driverArgs = applyDriverSessionArgs(name, normalizedArgs, entry.driverSessionId);
- await initializeDefaultCursorStyle(name, driverArgs, entry, entry.driverSessionId);
+ await initializeDefaultCursorStyle(name, driverArgs, entry, entry.driverSessionId, sessionCloseVersion);
const timeoutMs = getCuaMcpToolTimeoutMs(name);
try {
const result = await callCuaMcpToolWithTypeTextChunks(entry, name, driverArgs, timeoutMs);
@@ -3372,7 +3396,13 @@ export async function callComputerDriverTool(
}
: normalizedArgs;
const freshArgs = applyDriverSessionArgs(name, retryArgs, freshEntry.driverSessionId);
- await initializeDefaultCursorStyle(name, freshArgs, freshEntry, freshEntry.driverSessionId);
+ await initializeDefaultCursorStyle(
+ name,
+ freshArgs,
+ freshEntry,
+ freshEntry.driverSessionId,
+ sessionCloseVersion,
+ );
if (getCuaMcpSessionCloseVersion(sessionId) !== sessionCloseVersion) {
await cleanupComputerDriverSessionInternal(sessionId, {
resetGeneration: false,
@@ -3448,6 +3478,7 @@ async function cleanupComputerDriverSessionEntry(sessionId: string, entry: CuaMc
}
export function cleanupComputerDriverSession(sessionId: string): Promise<void> {
+ cuaMcpSessionCursorCapabilities.delete(sessionId);
markCuaMcpSessionClosed(sessionId);
const entry = cuaMcpSessions.get(sessionId);
if (entry) {
@@ -3489,6 +3520,7 @@ export async function cleanupAllComputerDriverSessions(): Promise<void> {
stopPermissionGrantFlow('cleanup');
clearProcessSnapshotCache();
await cleanupActiveComputerDriverSessions();
+ cuaMcpSessionCursorCapabilities.clear();
cuaDriverSessionGenerations.clear();
cuaMcpSessionCloseVersions.clear();
}