Skip to content

Commit 41a778d

Browse files
authored
fix(browser): recover stalled Copilot gateway connections (#120839)
1 parent ed760cb commit 41a778d

3 files changed

Lines changed: 369 additions & 0 deletions

File tree

‎extensions/browser/chrome-extension/modules/copilot-gateway.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export class CopilotGatewayClient {
125125
this.statusListeners = new Set();
126126
this.lifecycle = null;
127127
this.tokenRecovery = null;
128+
this.tickWatchTimer = null;
129+
this.lastInboundActivityAtMs = null;
128130
}
129131

130132
onEvent(listener) {
@@ -196,6 +198,7 @@ export class CopilotGatewayClient {
196198
onHello: (hello) => {
197199
this.ready = true;
198200
this.hello = hello;
201+
this.#startTickWatch(hello, protocol);
199202
this.#emitStatus({ state: "ready", label: "Gateway connected", hello });
200203
},
201204
onConnectFailure: (error, { plan }) => {
@@ -224,6 +227,7 @@ export class CopilotGatewayClient {
224227
if (this.protocol !== protocol) {
225228
return;
226229
}
230+
this.#stopTickWatch();
227231
this.ready = false;
228232
this.hello = null;
229233
if (!decision.retry) {
@@ -264,6 +268,11 @@ export class CopilotGatewayClient {
264268
},
265269
onConnectError: (error) =>
266270
this.#emitStatus({ state: "error", label: error.message || "Gateway unavailable" }),
271+
onActivity: () => {
272+
if (this.protocol === protocol && this.ready) {
273+
this.lastInboundActivityAtMs = Date.now();
274+
}
275+
},
267276
onEvent: (event) => {
268277
for (const listener of this.listeners) {
269278
listener(event);
@@ -278,6 +287,7 @@ export class CopilotGatewayClient {
278287
}
279288

280289
stop() {
290+
this.#stopTickWatch();
281291
this.ready = false;
282292
this.hello = null;
283293
this.tokenRecovery = null;
@@ -295,6 +305,40 @@ export class CopilotGatewayClient {
295305
return this.protocol.request(method, params, options);
296306
}
297307

308+
#startTickWatch(hello, protocol) {
309+
this.#stopTickWatch();
310+
const advertised = hello?.policy?.tickIntervalMs;
311+
// Gateway policy is remote input; clamp it before allocating a browser timer.
312+
const intervalMs = Math.min(
313+
2_147_483_647,
314+
Math.max(
315+
1_000,
316+
typeof advertised === "number" && Number.isFinite(advertised) && advertised > 0
317+
? Math.floor(advertised)
318+
: 30_000,
319+
),
320+
);
321+
this.lastInboundActivityAtMs = Date.now();
322+
this.tickWatchTimer = setInterval(() => {
323+
if (
324+
this.protocol === protocol &&
325+
this.ready &&
326+
this.lastInboundActivityAtMs !== null &&
327+
Date.now() - this.lastInboundActivityAtMs > intervalMs * 2
328+
) {
329+
protocol.closeSocket(4000, "tick timeout");
330+
}
331+
}, intervalMs);
332+
}
333+
334+
#stopTickWatch() {
335+
if (this.tickWatchTimer !== null) {
336+
clearInterval(this.tickWatchTimer);
337+
this.tickWatchTimer = null;
338+
}
339+
this.lastInboundActivityAtMs = null;
340+
}
341+
298342
#emitStatus(status) {
299343
for (const listener of this.statusListeners) {
300344
listener(status);

0 commit comments

Comments
 (0)