Skip to content

Reconnect and recover ​

Connections end: the app-server crashes, the machine sleeps, a socket closes. The SDK reports what happened and leaves the recovery to you, because only your app knows which work matters.

What the client does ​

  • Rejects every pending request and calls onDisconnect listeners when the transport reports an error.
  • Resets initialization, so a new connection starts from a clean handshake.
  • Reports malformed frames through onProtocolError without treating them as disconnection.
  • Rejects a request after its deadline (15 seconds by default, 120 seconds for thread/revert). The server may still be doing the work.

What your app does ​

  1. Detect the loss with onDisconnect.
  2. Close the client to release the child process, then create a fresh client and transport.
  3. Start, initialize, and re-register your notification and server-request handlers.
  4. Read or resume the threads your app owns, and reconcile what you show with the server's history.
  5. Only then accept new work.
ts
import {
  CodexAppServerClient,
  CodexAppServerStdioTransport,
  type CodexAppServerClientOptions,
} from '@korus-dev/codex-sdk';

export type SupervisorOptions = {
  /** Called after every successful connection, including reconnections. */
  onConnected(client: CodexAppServerClient): Promise<void> | void;
  onError?(error: Error): void;
  clientOptions?: CodexAppServerClientOptions;
  retryDelayMs?: number;
};

/**
 * Keeps one initialized client available. The SDK never reconnects by itself,
 * so the host decides when to retry and which threads to resume afterwards.
 */
export function superviseCodex({ onConnected, onError, clientOptions, retryDelayMs = 2_000 }: SupervisorOptions) {
  let stopped = false;
  let current: CodexAppServerClient | null = null;
  let timer: ReturnType<typeof setTimeout> | null = null;

  const scheduleRetry = () => {
    if (stopped || timer) return;
    timer = setTimeout(() => {
      timer = null;
      void connect();
    }, retryDelayMs);
  };

  const connect = async (): Promise<void> => {
    if (stopped) return;
    const client = new CodexAppServerClient(new CodexAppServerStdioTransport(), clientOptions);
    const lost = (error: Error) => {
      if (current === client) current = null;
      onError?.(error);
      // Release the child process; pending requests were already rejected.
      void client.close().catch(() => undefined);
      scheduleRetry();
    };

    try {
      client.onDisconnect(lost);
      await client.start();
      await client.initialize({
        clientInfo: { name: 'my_app', title: 'My App', version: '1.0.0' },
        capabilities: null,
      });
      current = client;
      // Read or resume the threads your app owns here, before accepting new work.
      await onConnected(client);
    } catch (error) {
      if (current === client) current = null;
      onError?.(error instanceof Error ? error : new Error(String(error)));
      await client.close().catch(() => undefined);
      scheduleRetry();
    }
  };

  void connect();

  return {
    get client() {
      return current;
    },
    async stop() {
      stopped = true;
      if (timer) clearTimeout(timer);
      const client = current;
      current = null;
      await client?.close();
    },
  };
}

Replaying work safely ​

A request that was in flight when the connection dropped may have succeeded. Before repeating a mutation, read the authoritative state:

  • After turn/start, call thread/read or thread/turns/list and look for the turn before sending another.
  • After thread/fork or thread/archive, list threads and check the result.
  • A turn that was running may still be running. Resume the thread, observe its status, and use turn/interrupt only if the user asked to stop.

Treat an approval as belonging to the connection that received it. After a disconnect, discard its UI and let the server ask again rather than answering on a new connection.

Observe, do not guess ​

Use onRequestSettled to record each request's method, outcome (ok, error, or timeout), and duration. It never receives parameters or results, so it is safe to forward to metrics. Use onProtocolError and the stdio transport's onStderr and onExit when you need to explain why a connection ended.

See troubleshooting for common failure modes.

Released under the Apache License 2.0.