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(remote-desktop): isolate renewals and first-frame capture

Dashcefbca1db703
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
index b91af993cc..1a3e95026f 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
@@ -12,6 +12,7 @@ const h = vi.hoisted(() => ({
dispose: vi.fn(),
stop: vi.fn(),
nativeStop: vi.fn(),
+ nativeFrame: vi.fn(async () => 'frame'),
}));
vi.mock('electron', () => ({
app: { on: vi.fn() },
@@ -93,7 +94,7 @@ vi.mock('../controller', () => ({
vi.mock('../nativeCapture', () => ({
NativeDesktopCapture: class {
stop = h.nativeStop;
- frame = vi.fn(async () => 'frame');
+ frame = h.nativeFrame;
},
}));
vi.mock('../inputHost', () => ({
@@ -150,12 +151,41 @@ beforeEach(() => {
h.dispose.mockClear();
h.stop.mockClear();
h.nativeStop.mockClear();
+ h.nativeFrame.mockClear();
registerRemoteDesktopIpc();
});
afterEach(() => {
h.deps.stopVideo();
vi.clearAllTimers();
vi.useRealTimers();
+ vi.unstubAllGlobals();
+});
+
+it.each(['success', 'timeout'])('keeps relay polls out of native preparation and resumes after %s', async (outcome) => {
+ vi.stubGlobal('process', { ...process, platform: 'darwin' });
+ let sourcesReady!: (sources: any[]) => void;
+ h.source = new Promise((resolve) => { sourcesReady = resolve; });
+ const pending = offer();
+ const settled = outcome === 'success'
+ ? expect(pending).resolves.toBe('answer')
+ : expect(pending).rejects.toThrow('DESKTOP_VIDEO_TIMEOUT');
+ expect(h.nativeStop).toHaveBeenCalled();
+ await expect(h.deps.frame('1', true)).resolves.toBeNull();
+ h.handlers.get(DESKTOP_LOCAL.REGISTER)(event());
+ await flush();
+ await expect(h.deps.frame('1', true)).resolves.toBeNull();
+ sourcesReady([{ id: 'screen:1', display_id: '1' }]);
+ await flush();
+ await expect(h.deps.frame('1', true)).resolves.toBeNull();
+ expect(h.nativeFrame).not.toHaveBeenCalled();
+ await expect(h.handlers.get(DESKTOP_LOCAL.NATIVE_FRAME)(event(), 'lease')).resolves.toBe('frame');
+ if (outcome === 'success') {
+ const id = h.owner.send.mock.calls[0][1].id;
+ h.handlers.get(DESKTOP_LOCAL.REPLY)(event(), id, 'answer');
+ } else await vi.advanceTimersByTimeAsync(18_000);
+ await settled;
+ await h.deps.frame('1', true);
+ expect(h.nativeFrame).toHaveBeenCalledTimes(2);
});
it.each(['ready', 'sources'])(
diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.ts
index f52e6997cc..6fa553f5d3 100644
--- a/apps/desktop/src/main/remote-desktop/index.ts
+++ b/apps/desktop/src/main/remote-desktop/index.ts
@@ -314,6 +314,9 @@ export const remoteDesktop = new RemoteDesktopController({
return permissions.read();
},
frame: async (displayId, cursorOverlay) => {
+ // offer() cancels the previous read before acquiring the capture owner.
+ // Leave its required first frame uncontested; relay polling resumes afterwards.
+ if (preparingOffer) return null;
// Compatibility viewers must also wake/capture without waiting for
// Chromium's thumbnail enumeration, which may hang on a sleeping display.
if (process.platform === 'darwin' || windowsAvailable) {
diff --git a/apps/mobile/src/__tests__/invokeTimeouts.test.ts b/apps/mobile/src/__tests__/invokeTimeouts.test.ts
index cb0bb631d4..0428b6bca0 100644
--- a/apps/mobile/src/__tests__/invokeTimeouts.test.ts
+++ b/apps/mobile/src/__tests__/invokeTimeouts.test.ts
@@ -12,6 +12,8 @@ describe("resolveMobileInvokeTimeoutMs", () => {
59_000,
);
expect(resolveMobileInvokeTimeoutMs("device-link:state")).toBeUndefined();
+ expect(resolveMobileInvokeTimeoutMs("device-link:remote-desktop:v1", [{ op: "heartbeat" }])).toBe(5_000);
+ expect(resolveMobileInvokeTimeoutMs("device-link:remote-desktop:v1", [{ op: "offer" }])).toBe(59_000);
});
it("allows a large history row to finish on a slow link without widening small status reads", () => {
expect(resolveMobileInvokeTimeoutMs("local-db:messages:list")).toBe(30_000);
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index d3fd5eed33..2fcfbed239 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -2135,7 +2135,7 @@ describe('new session worktree wiring (source locks)', () => {
resolve(process.cwd(), 'src/device-link/DeviceLinkContext.tsx'),
'utf8',
);
- expect(contextSource).toContain('resolveMobileInvokeTimeoutMs(channel)');
+ expect(contextSource).toContain('resolveMobileInvokeTimeoutMs(channel, args)');
const timeoutsSource = readTextLf(
resolve(process.cwd(), 'src/device-link/invokeTimeouts.ts'),
'utf8',
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 0b659c40fc..5fba843168 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -1672,7 +1672,7 @@ async function sendInvoke<T>(
{ channel, args },
// 长通道(media / 文件搜索 / schedule 就绪窗口等)按 invokeTimeouts 解析
// 规则保留更长窗口,避免 mobile 收紧的默认 15s 误伤合法慢操作。
- resolveMobileInvokeTimeoutMs(channel),
+ resolveMobileInvokeTimeoutMs(channel, args),
);
} catch (err) {
settleDeviceSend(deviceId, slot, classifyDeviceSendFailure(err));
diff --git a/apps/mobile/src/device-link/invokeTimeouts.ts b/apps/mobile/src/device-link/invokeTimeouts.ts
index 75d3bb0132..0459c5ed48 100644
--- a/apps/mobile/src/device-link/invokeTimeouts.ts
+++ b/apps/mobile/src/device-link/invokeTimeouts.ts
@@ -69,7 +69,11 @@ export const MOBILE_INVOKE_TIMEOUT_OVERRIDES_MS: Record<string, number> = {
export const MOBILE_SCHEDULE_CHANNEL_TIMEOUT_MS = 40_000;
-export function resolveMobileInvokeTimeoutMs(channel: string): number | undefined {
+export function resolveMobileInvokeTimeoutMs(channel: string, args?: unknown[]): number | undefined {
+ // Renewals must settle before the 12s lease, independently of slow media offers.
+ const request = args?.[0];
+ if (channel === 'device-link:remote-desktop:v1' && request &&
+ typeof request === 'object' && 'op' in request && request.op === 'heartbeat') return 5_000;
const exact = MOBILE_INVOKE_TIMEOUT_OVERRIDES_MS[channel];
if (exact !== undefined) return exact;
if (channel.startsWith('maker:schedule:')) return MOBILE_SCHEDULE_CHANNEL_TIMEOUT_MS;
diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
index ff651d4c93..107e472a7d 100644
--- a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
+++ b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
@@ -581,6 +581,9 @@ export default function RemoteDesktopScreen() {
heartbeatBusy = current.lease;
void request({ op: "heartbeat", lease: current.lease })
.catch((cause) => {
+ // A missing reply does not prove renewal failed; the next interval
+ // retries within the lease. Explicit host revocation still stops us.
+ if (cause && typeof cause === "object" && cause.code === "INVOKE_TIMEOUT") return;
if (active.current === current && !presentation.current) fail(cause);
})
.finally(() => {
diff --git a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
index fea44c3293..0cfc61e2c9 100644
--- a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
+++ b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
@@ -568,6 +568,21 @@ describe("remote desktop controls", () => {
expect(fixture.invoke).toHaveBeenCalledTimes(1);
expect(requests().some((r) => r.op === "start")).toBe(false);
});
+ it("renews again after a lost heartbeat reply without replacing the live lease", async () => {
+ await connect();
+ const original = fixture.invoke.getMockImplementation()!;
+ fixture.invoke.mockImplementation((...args) =>
+ args[2][0].op === "heartbeat"
+ ? new Promise((_resolve, reject) => setTimeout(() => reject(
+ Object.assign(new Error("INVOKE_TIMEOUT"), { code: "INVOKE_TIMEOUT" }),
+ ), 5000))
+ : original(...args),
+ );
+ await act(async () => vi.advanceTimersByTimeAsync(21_000));
+ expect(requests().filter((r) => r.op === "heartbeat")).toHaveLength(4);
+ expect(requests().filter((r) => r.op === "start")).toHaveLength(1);
+ expect(requests().filter((r) => r.op === "stop")).toHaveLength(0);
+ });
it.each([
["ACCESS_REVOKED", "accessRevoked"],
["REMOTE_DISABLED", "remoteDisabled"],