Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(agent-controller): wait three server beats before calling the socket dead

raullopez-sandbox9a05704c71ef
diff --git a/console/packages/agent-controller/src/stream.test.ts b/console/packages/agent-controller/src/stream.test.ts
index 74f4cc36d..15a7d74cc 100644
--- a/console/packages/agent-controller/src/stream.test.ts
+++ b/console/packages/agent-controller/src/stream.test.ts
@@ -240,6 +240,29 @@ describe('runControllerStream', () => {
expect(Date.now() - started).toBeGreaterThanOrEqual(190);
});
+ it('waits three of the server’s beats before calling a quiet socket dead', async () => {
+ const scripted = new ScriptedClient();
+ // Switch beats every 0.2 s here, as slowly as the 200 ms idle timeout: three beats it is.
+ scripted.sockets = () =>
+ scriptedSocket(
+ [
+ {
+ event: 'connection_state',
+ data: { connection_id: 'connection-1', heartbeat_interval_s: 0.2 },
+ },
+ ],
+ 'open'
+ );
+ const stop = new AbortController();
+ const { ending } = run(scripted, stop.signal);
+ await waitFor(() => scripted.attaches.length === 1, 'an attach');
+ await delay(450);
+ expect(scripted.attaches).toHaveLength(1);
+ await waitFor(() => scripted.attaches.length >= 2, 'a second attach once three beats passed');
+ stop.abort();
+ await ending;
+ });
+
it('opens a new connection, from the cursors as they stand, when Switch no longer knows it', async () => {
const scripted = new ScriptedClient();
scripted.sockets = () =>
diff --git a/console/packages/agent-controller/src/stream.ts b/console/packages/agent-controller/src/stream.ts
index d833ea92d..841e1291d 100644
--- a/console/packages/agent-controller/src/stream.ts
+++ b/console/packages/agent-controller/src/stream.ts
@@ -273,22 +273,28 @@ class ControllerStream {
resolve?.();
};
let idle: ReturnType<typeof setTimeout> | null = null;
+ // Raised to three of the server's beats once it says how often it pings:
+ // a timeout no longer than its interval drops a healthy socket every beat.
+ let idleMs = this.options.idleTimeoutMs;
const touch = () => {
if (idle) clearTimeout(idle);
- idle = setTimeout(
- () => abort.abort(new IdleTimeout(this.options.idleTimeoutMs)),
- this.options.idleTimeoutMs
- );
+ const ms = idleMs;
+ idle = setTimeout(() => abort.abort(new IdleTimeout(ms)), ms);
};
const onMessage = (message: MessageEvent) => {
- touch();
let parsed: { event?: unknown; data?: unknown };
try {
parsed = JSON.parse(String(message.data)) as typeof parsed;
} catch {
+ touch();
this.options.log.error('Dropped a controller socket message that is not JSON');
return;
}
+ const beat = (parsed.data as { heartbeat_interval_s?: unknown } | null | undefined)
+ ?.heartbeat_interval_s;
+ if (parsed.event === 'connection_state' && typeof beat === 'number' && beat > 0)
+ idleMs = Math.max(this.options.idleTimeoutMs, beat * 3000);
+ touch();
if (parsed.event === 'ping') {
socket.send(JSON.stringify({ type: 'pong', cursors: this.options.confirmed() }));
return;