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): measure diagnostic latency monotonically

Dash14663497d607
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
index 0bbf222074..a2f7f458d4 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
@@ -120,6 +120,7 @@ afterEach(() => {
fixture.handlers.clear();
fixture.owner = 'a:1';
fixture.debug.mockClear();
+ vi.restoreAllMocks();
});
const event = (win: any) => ({ sender: win.webContents, senderFrame: win.webContents.mainFrame });
const call = (channel: string, win: any, ...args: unknown[]) =>
@@ -157,6 +158,39 @@ it.each([
expect(JSON.stringify(fixture.debug.mock.calls)).not.toContain('private-text');
});
+it.each([-3600000, 3600000])(
+ 'measures request duration independently of a %s ms clock correction',
+ async (jump) => {
+ const sender: any = { id: 100 };
+ manager = new RemoteDesktopViewerWindows((value) => value === sender);
+ manager.register();
+ manager.open(sender, { deviceId: 'a', name: 'A' });
+ const win = fixture.windows[0];
+ call(REMOTE_VIEWER.READY, win);
+ call(REMOTE_VIEWER.PRESENTED, win);
+ const { generation } = call(REMOTE_VIEWER.STATE, win);
+ await call(REMOTE_VIEWER.REQUEST, win, generation, { op: 'start', displayId: 'screen' });
+ const wall = vi.spyOn(Date, 'now').mockReturnValue(10000000);
+ const monotonic = vi.spyOn(performance, 'now').mockReturnValue(100);
+ vi.mocked(remoteInvoke).mockImplementationOnce(async () => {
+ wall.mockReturnValue(10000000 + jump);
+ monotonic.mockReturnValue(350);
+ throw new Error('INVOKE_TIMEOUT');
+ });
+ await expect(
+ call(REMOTE_VIEWER.REQUEST, win, generation, { op: 'heartbeat', lease: 'lease-a' }),
+ ).rejects.toThrow('INVOKE_TIMEOUT');
+ expect(fixture.debug).toHaveBeenCalledWith(
+ 'desktop request failed',
+ expect.objectContaining({
+ startedAt: 10000000,
+ at: 10000000 + jump,
+ elapsedMs: 250,
+ }),
+ );
+ },
+);
+
it.each(['resolve', 'reject'])(
'closes immediately while stop is pending and ignores its late %s',
async (outcome) => {
diff --git a/apps/desktop/src/main/remote-desktop-viewer/windows.ts b/apps/desktop/src/main/remote-desktop-viewer/windows.ts
index cae6e2313c..2f6434820c 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/windows.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/windows.ts
@@ -52,6 +52,7 @@ async function requestRemote<T>(device: string, request: unknown, check: () => v
if (!getAppCapabilities().canUseDeviceLink || isAppSessionBoundaryPending())
throw new Error('DESKTOP_STOPPED');
const startedAt = Date.now();
+ const startedMonotonic = performance.now();
try {
const result = await remoteInvoke(device, REMOTE_DESKTOP_CHANNEL, [request], {
preSend: () => {
@@ -70,7 +71,7 @@ async function requestRemote<T>(device: string, request: unknown, check: () => v
log.debug('desktop request failed', {
at: Date.now(),
startedAt,
- elapsedMs: Date.now() - startedAt,
+ elapsedMs: performance.now() - startedMonotonic,
operation: parsed.op,
session: lease ? createHash('sha256').update(lease).digest('hex').slice(0, 16) : null,
code: desktopDiagnosticCode(
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
index a3ad38b022..409bfcdf0c 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
@@ -63,9 +63,8 @@ it('records bounded received heartbeats and the unchanged expiration deadline wi
expect(data).toMatchObject({
reason: 'heartbeat-expired',
at: 43000,
- ageMs: 42000,
- lastHeartbeatAgeMs: 12000,
- expiresInMs: 0,
+ startedAt: 1000,
+ expiresAt: 43000,
session: createHash('sha256').update(lease).digest('hex').slice(0, 16),
});
expect(data.heartbeats).toHaveLength(8);
diff --git a/apps/desktop/src/main/remote-desktop/controller.ts b/apps/desktop/src/main/remote-desktop/controller.ts
index 3786a6f2f9..f4cfa27ad2 100644
--- a/apps/desktop/src/main/remote-desktop/controller.ts
+++ b/apps/desktop/src/main/remote-desktop/controller.ts
@@ -374,11 +374,10 @@ export class RemoteDesktopController {
lockScreen,
controlling: active.controlling,
backgroundViewing: active.backgroundViewing === true,
- ageMs: at - active.diagnostics.startedAt,
- expiresInMs: active.expires - at,
- lastHeartbeatAgeMs: heartbeats.length
- ? at - heartbeats[heartbeats.length - 1].receivedAt!
- : null,
+ // Wall timestamps correlate events and expose the existing lease deadline;
+ // subtracting them would misreport durations when the system clock changes.
+ startedAt: active.diagnostics.startedAt,
+ expiresAt: active.expires,
heartbeats,
});
} catch {