Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/sdk/src/realtime/telemetry-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ export class TelemetryReporter implements ITelemetryReporter {
this.sendReport(false);
}

/** Stop the reporter and send a final report with keepalive. */
/** Stop the reporter and discard any buffered data. */
stop(): void {
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
this.sendReport(true);
this.statsBuffer = [];
this.diagnosticsBuffer = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendReport keepalive parameter is now dead code

Low Severity

The keepalive parameter of sendReport is now always false since stop() no longer calls sendReport(true) and flush() always passes false. This makes the keepalive parameter, the isLast computation on line 152, and the keepalive: keepalive && isLast expression dead code that could confuse future readers into thinking there's still a keepalive code path.

Fix in Cursor Fix in Web

}

/**
Expand Down
25 changes: 11 additions & 14 deletions packages/sdk/tests/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ describe("Subscribe Client", () => {
}
});

it("buffers pre-session telemetry diagnostics and flushes them after session_id", async () => {
it("buffers pre-session telemetry diagnostics and discards them on disconnect", async () => {
const { createRealTimeClient } = await import("../src/realtime/client.js");
const { WebRTCManager } = await import("../src/realtime/webrtc-manager.js");

Expand Down Expand Up @@ -1587,17 +1587,11 @@ describe("Subscribe Client", () => {
});
}

// disconnect() calls stop() which discards buffered data instead of sending it.
// This avoids 401s when the API key has been invalidated on reconnect.
client.disconnect();

await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});

const [, options] = fetchMock.mock.calls[0];
const body = JSON.parse(options.body);
expect(body.diagnostics).toHaveLength(1);
expect(body.diagnostics[0].name).toBe("phaseTiming");
expect(body.diagnostics[0].data.phase).toBe("websocket");
expect(fetchMock).not.toHaveBeenCalled();
} finally {
connectSpy.mockRestore();
stateSpy.mockRestore();
Expand Down Expand Up @@ -2255,7 +2249,7 @@ describe("TelemetryReporter", () => {
}
});

it("stop sends final report with keepalive", async () => {
it("stop discards buffered data without sending a request", async () => {
const { TelemetryReporter } = await import("../src/realtime/telemetry-reporter.js");

const fetchMock = vi.fn().mockResolvedValue({ ok: true });
Expand All @@ -2279,9 +2273,12 @@ describe("TelemetryReporter", () => {

reporter.stop();

expect(fetchMock).toHaveBeenCalledTimes(1);
const [, options] = fetchMock.mock.calls[0];
expect(options.keepalive).toBe(true);
// stop() should NOT send any network request
expect(fetchMock).not.toHaveBeenCalled();

// flush() after stop() should be a no-op (buffers were cleared)
reporter.flush();
expect(fetchMock).not.toHaveBeenCalled();
} finally {
vi.unstubAllGlobals();
}
Expand Down
Loading