Load long threads as a bounded window, reconnect when the socket drops

Full snapshots of long threads exceeded the server's WebSocket message
limit (close 1009), which killed the shared socket and every thread on it.
Subscribe with acceptBoundedSnapshot like the web client, page older turns
over the history endpoint, and reconnect with backoff, resubscribing
watched threads, whenever the socket closes.
This commit is contained in:
Callum Alpass committed 2026-11-02 17:19:22 +11:00
1 parent cea1409f40
commit 4f42407099
3 files changed
+134 -19

No files matched your search

+8 -4
View File
@@ -18,6 +18,7 @@ import {
resolveSocketUrl, resolveSocketUrl,
UnauthorizedError, UnauthorizedError,
type ServerTarget, type ServerTarget,
type StoredCredential,
} from "./auth.ts"; } from "./auth.ts";
const makeProtocolClient = RpcClient.make(WsRpcGroup); const makeProtocolClient = RpcClient.make(WsRpcGroup);
@@ -26,6 +27,8 @@ export type T3Rpc = Effect.Success<typeof makeProtocolClient>;
export interface Connection { export interface Connection {
readonly rpc: T3Rpc; readonly rpc: T3Rpc;
readonly target: ServerTarget; readonly target: ServerTarget;
/** For the HTTP endpoints, such as older thread history. */
readonly credential: StoredCredential;
} }
const socketUrlFor = async (target: ServerTarget) => { const socketUrlFor = async (target: ServerTarget) => {
@@ -33,19 +36,20 @@ const socketUrlFor = async (target: ServerTarget) => {
const existing = await readCredential(target); const existing = await readCredential(target);
if (existing) { if (existing) {
try { try {
return await resolveSocketUrl(target, existing, protocol); return { url: await resolveSocketUrl(target, existing, protocol), credential: existing };
} catch (error) { } catch (error) {
// A revoked or expired token falls through to a fresh one; TUI3_TOKEN never does. // A revoked or expired token falls through to a fresh one; TUI3_TOKEN never does.
if (!(error instanceof UnauthorizedError) || process.env.TUI3_TOKEN) throw error; if (!(error instanceof UnauthorizedError) || process.env.TUI3_TOKEN) throw error;
} }
} }
return resolveSocketUrl(target, await issueLocalCredential(target), protocol); const credential = await issueLocalCredential(target);
return { url: await resolveSocketUrl(target, credential, protocol), credential };
}; };
/** Opens one authenticated socket to the T3 Code server for the life of the scope. */ /** Opens one authenticated socket to the T3 Code server for the life of the scope. */
export const connect: Effect.Effect<Connection, Error, Scope.Scope> = Effect.gen(function* () { export const connect: Effect.Effect<Connection, Error, Scope.Scope> = Effect.gen(function* () {
const target = yield* Effect.tryPromise({ try: resolveLocalServer, catch: toError }); const target = yield* Effect.tryPromise({ try: resolveLocalServer, catch: toError });
const socketUrl = yield* Effect.tryPromise({ try: () => socketUrlFor(target), catch: toError }); const { url: socketUrl, credential } = yield* Effect.tryPromise({ try: () => socketUrlFor(target), catch: toError });
const protocol = Layer.effect( const protocol = Layer.effect(
RpcClient.Protocol, RpcClient.Protocol,
RpcClient.makeProtocolSocket({ RpcClient.makeProtocolSocket({
@@ -65,7 +69,7 @@ export const connect: Effect.Effect<Connection, Error, Scope.Scope> = Effect.gen
); );
const context = yield* Layer.build(protocol); const context = yield* Layer.build(protocol);
const rpc = yield* makeProtocolClient.pipe(Effect.provide(context)); const rpc = yield* makeProtocolClient.pipe(Effect.provide(context));
return { rpc, target }; return { rpc, target, credential };
}); });
const toError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause))); const toError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause)));
+118 -14
View File
@@ -1,7 +1,10 @@
import { import {
CommandId, CommandId,
MessageId, MessageId,
ORCHESTRATION_PROTOCOL_HEADER,
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
ORCHESTRATION_V2_WS_METHODS, ORCHESTRATION_V2_WS_METHODS,
OrchestrationV2ThreadHistoryPage,
type OrchestrationV2Command, type OrchestrationV2Command,
type OrchestrationV2ShellSnapshot, type OrchestrationV2ShellSnapshot,
type OrchestrationV2ThreadProjection, type OrchestrationV2ThreadProjection,
@@ -10,11 +13,14 @@ import {
type ThreadId, type ThreadId,
} from "@t3tools/contracts"; } from "@t3tools/contracts";
import { applyOrchestrationV2ProjectionEvent } from "@t3tools/client-runtime/state/orchestration-v2-projection"; import { applyOrchestrationV2ProjectionEvent } from "@t3tools/client-runtime/state/orchestration-v2-projection";
import { mergeOlderHistoryIntoProjection } from "@t3tools/client-runtime/state/thread-history-merge";
import { boundedSnapshotProjection } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
import { appendFileSync } from "node:fs"; import { appendFileSync } from "node:fs";
import * as Effect from "effect/Effect"; import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit"; import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber"; import * as Fiber from "effect/Fiber";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope"; import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream"; import * as Stream from "effect/Stream";
@@ -24,6 +30,7 @@ import { connect, type Connection } from "./rpc.ts";
export type ConnectionStatus = export type ConnectionStatus =
| { readonly type: "connecting" } | { readonly type: "connecting" }
| { readonly type: "live" } | { readonly type: "live" }
| { readonly type: "reconnecting"; readonly message: string }
| { readonly type: "error"; readonly message: string }; | { readonly type: "error"; readonly message: string };
export interface ShellState { export interface ShellState {
@@ -32,9 +39,18 @@ export interface ShellState {
readonly serverUrl: string | null; readonly serverUrl: string | null;
} }
/** Older turns the server left out of the snapshot, fetched page by page. */
export interface ThreadHistory {
readonly cursor: string | null;
readonly hasMore: boolean;
readonly loading: boolean;
readonly error?: string;
}
export interface ThreadState { export interface ThreadState {
readonly status: "loading" | "live" | "error"; readonly status: "loading" | "live" | "error";
readonly projection: OrchestrationV2ThreadProjection | null; readonly projection: OrchestrationV2ThreadProjection | null;
readonly history: ThreadHistory;
readonly error?: string; readonly error?: string;
} }
@@ -44,6 +60,10 @@ type Listener<T> = (value: T) => void;
const THREAD_NOTIFY_MS = 33; const THREAD_NOTIFY_MS = 33;
/** Threads you leave stay subscribed this long, so switching back is instant. */ /** Threads you leave stay subscribed this long, so switching back is instant. */
const THREAD_LINGER_MS = 60_000; const THREAD_LINGER_MS = 60_000;
/** Waits between reconnect attempts after the socket drops. */
const RECONNECT_DELAYS_MS = [500, 1_000, 2_000, 5_000, 10_000];
const NO_HISTORY: ThreadHistory = { cursor: null, hasMore: false, loading: false };
const debugPath = process.env.TUI3_DEBUG; const debugPath = process.env.TUI3_DEBUG;
/** Appends to the file named by TUI3_DEBUG; the TUI owns stdout and stderr. */ /** Appends to the file named by TUI3_DEBUG; the TUI owns stdout and stderr. */
@@ -51,6 +71,8 @@ export function debugLog(message: string): void {
if (debugPath) appendFileSync(debugPath, `${new Date().toISOString()} ${message}\n`); if (debugPath) appendFileSync(debugPath, `${new Date().toISOString()} ${message}\n`);
} }
const decodeHistoryPage = Schema.decodeUnknownSync(Schema.toCodecJson(OrchestrationV2ThreadHistoryPage));
interface ThreadEntry { interface ThreadEntry {
state: ThreadState; state: ThreadState;
readonly listeners: Set<Listener<ThreadState>>; readonly listeners: Set<Listener<ThreadState>>;
@@ -61,11 +83,16 @@ interface ThreadEntry {
/** /**
* One connection to T3 Code: a live shell (projects and threads) plus on-demand * One connection to T3 Code: a live shell (projects and threads) plus on-demand
* thread subscriptions, exposed as plain listeners for the UI. * thread subscriptions, exposed as plain listeners for the UI. When the socket
* drops, it reconnects and resubscribes everything.
*/ */
export class T3Session { export class T3Session {
private scope: Scope.Closeable | null = null; private scope: Scope.Closeable | null = null;
private connection: Connection | null = null; private connection: Connection | null = null;
private stopped = false;
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
private reconnectAttempt = 0;
private recovering = false;
private shell: ShellState = { status: { type: "connecting" }, snapshot: null, serverUrl: null }; private shell: ShellState = { status: { type: "connecting" }, snapshot: null, serverUrl: null };
private readonly shellListeners = new Set<Listener<ShellState>>(); private readonly shellListeners = new Set<Listener<ShellState>>();
private readonly threads = new Map<ThreadId, ThreadEntry>(); private readonly threads = new Map<ThreadId, ThreadEntry>();
@@ -76,17 +103,22 @@ export class T3Session {
try { try {
this.connection = await Effect.runPromise(connect.pipe(Scope.provide(scope))); this.connection = await Effect.runPromise(connect.pipe(Scope.provide(scope)));
} catch (error) { } catch (error) {
this.scope = null;
await Effect.runPromise(Scope.close(scope, Exit.void));
if (this.reconnectAttempt > 0) return this.scheduleReconnect(messageOf(error));
this.setShell({ status: { type: "error", message: messageOf(error) } }); this.setShell({ status: { type: "error", message: messageOf(error) } });
return; return;
} }
this.recovering = false;
this.setShell({ serverUrl: this.connection.target.httpBaseUrl }); this.setShell({ serverUrl: this.connection.target.httpBaseUrl });
this.runInScope(this.shellStream()); this.runInScope(this.shellStream());
for (const [threadId, entry] of this.threads) entry.fiber = this.runInScope(this.threadStream(threadId));
} }
async stop(): Promise<void> { async stop(): Promise<void> {
const scope = this.scope; this.stopped = true;
this.scope = null; if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
if (scope) await Effect.runPromise(Scope.close(scope, Exit.void)); await this.closeScope();
} }
getShell(): ShellState { getShell(): ShellState {
@@ -106,9 +138,9 @@ export class T3Session {
watchThread(threadId: ThreadId, listener: Listener<ThreadState>): () => void { watchThread(threadId: ThreadId, listener: Listener<ThreadState>): () => void {
let entry = this.threads.get(threadId); let entry = this.threads.get(threadId);
if (!entry) { if (!entry) {
entry = { state: { status: "loading", projection: null }, listeners: new Set(), fiber: null, notifyTimer: null, lingerTimer: null }; entry = { state: { status: "loading", projection: null, history: NO_HISTORY }, listeners: new Set(), fiber: null, notifyTimer: null, lingerTimer: null };
this.threads.set(threadId, entry); this.threads.set(threadId, entry);
entry.fiber = this.runInScope(this.threadStream(threadId)); if (this.connection) entry.fiber = this.runInScope(this.threadStream(threadId));
debugLog(`subscribe ${threadId}`); debugLog(`subscribe ${threadId}`);
} }
if (entry.lingerTimer) clearTimeout(entry.lingerTimer); if (entry.lingerTimer) clearTimeout(entry.lingerTimer);
@@ -129,6 +161,34 @@ export class T3Session {
}; };
} }
/** Fetches the next page of older turns and prepends it. Resolves false when there was nothing to load. */
async loadEarlier(threadId: ThreadId): Promise<boolean> {
const entry = this.threads.get(threadId);
const cursor = entry?.state.history.cursor;
if (!entry || !cursor || !entry.state.history.hasMore || entry.state.history.loading) return false;
const { target, credential } = this.requireConnection();
this.setThread(threadId, { history: { ...entry.state.history, loading: true } });
try {
const url = new URL(`/api/orchestration/threads/${encodeURIComponent(threadId)}/history`, target.httpBaseUrl);
url.searchParams.set("cursor", cursor);
const response = await fetch(url, {
headers: { authorization: `Bearer ${credential.bearerToken}`, [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT },
});
if (!response.ok) throw new Error(`${response.status} ${(await response.text()).slice(0, 200)}`);
const page = decodeHistoryPage(await response.json());
// A resubscribe while the page was in flight replaced the projection; drop the stale page.
if (entry.state.history.cursor !== cursor || !entry.state.projection) return false;
this.setThread(threadId, {
projection: mergeOlderHistoryIntoProjection(entry.state.projection, page.items),
history: { cursor: page.nextCursor, hasMore: page.hasMoreHistory, loading: false },
});
return true;
} catch (error) {
this.setThread(threadId, { history: { ...entry.state.history, loading: false, error: messageOf(error) } });
throw error;
}
}
sendMessage(threadId: ThreadId, text: string, mode: "queue" | "steer" = "queue"): Promise<void> { sendMessage(threadId: ThreadId, text: string, mode: "queue" | "steer" = "queue"): Promise<void> {
const commandId = newCommandId(); const commandId = newCommandId();
const running = this.threads.get(threadId)?.state.projection?.runs.findLast((run) => isActiveRun(run.status)); const running = this.threads.get(threadId)?.state.projection?.runs.findLast((run) => isActiveRun(run.status));
@@ -170,6 +230,7 @@ export class T3Session {
const previous = this.shell.snapshot; const previous = this.shell.snapshot;
switch (item.kind) { switch (item.kind) {
case "synchronized": case "synchronized":
this.reconnectAttempt = 0;
this.setShell({ status: { type: "live" } }); this.setShell({ status: { type: "live" } });
return; return;
case "snapshot": case "snapshot":
@@ -186,13 +247,17 @@ export class T3Session {
} }
}), }),
), ),
Effect.catchCause((cause) => Effect.sync(() => this.setShell({ status: { type: "error", message: prettyCause(cause) } }))), Effect.catchCause((cause) => Effect.sync(() => this.connectionLost(prettyCause(cause)))),
// The shell stream only ends when the socket does.
Effect.ensuring(Effect.sync(() => this.connectionLost("connection closed"))),
); );
} }
private threadStream(threadId: ThreadId) { private threadStream(threadId: ThreadId) {
const rpc = this.requireConnection().rpc; const rpc = this.requireConnection().rpc;
return rpc[ORCHESTRATION_V2_WS_METHODS.subscribeThread]({ threadId, requestCompletionMarker: true }).pipe( // Bounded: the server sends the recent window and a cursor for older turns,
// which keeps long threads under the socket's message size limit.
return rpc[ORCHESTRATION_V2_WS_METHODS.subscribeThread]({ threadId, requestCompletionMarker: true, acceptBoundedSnapshot: true }).pipe(
Stream.runForEach((item) => Stream.runForEach((item) =>
Effect.sync(() => { Effect.sync(() => {
const entry = this.threads.get(threadId); const entry = this.threads.get(threadId);
@@ -201,10 +266,15 @@ export class T3Session {
case "synchronized": case "synchronized":
this.setThread(threadId, { status: "live" }); this.setThread(threadId, { status: "live" });
return; return;
case "snapshot": case "snapshot": {
debugLog(`snapshot ${threadId} items=${item.projection.visibleTurnItems.length}`); const projection = boundedSnapshotProjection(item);
this.setThread(threadId, { projection: item.projection }); debugLog(`snapshot ${threadId} items=${projection.visibleTurnItems.length} more=${item.hasMoreHistory === true}`);
this.setThread(threadId, {
projection,
history: { cursor: item.historyCursor ?? null, hasMore: item.hasMoreHistory === true, loading: false },
});
return; return;
}
case "event": case "event":
this.setThread(threadId, { projection: applyOrchestrationV2ProjectionEvent(entry.state.projection, item.event) }); this.setThread(threadId, { projection: applyOrchestrationV2ProjectionEvent(entry.state.projection, item.event) });
return; return;
@@ -213,10 +283,42 @@ export class T3Session {
} }
}), }),
), ),
Effect.catchCause((cause) => Effect.sync(() => this.setThread(threadId, { status: "error", error: prettyCause(cause) }))), Effect.catchCause((cause) =>
Effect.sync(() => {
const message = prettyCause(cause);
// A dropped socket is the shell's to recover from; the thread resubscribes after.
if (!isSocketFailure(message)) this.setThread(threadId, { status: "error", error: message });
}),
),
); );
} }
private connectionLost(message: string): void {
if (this.stopped || this.recovering) return;
this.recovering = true;
debugLog(`connection lost: ${message}`);
void this.closeScope().then(() => this.scheduleReconnect(message));
}
private scheduleReconnect(message: string): void {
if (this.stopped || this.reconnectTimer) return;
const delay = RECONNECT_DELAYS_MS[Math.min(this.reconnectAttempt, RECONNECT_DELAYS_MS.length - 1)]!;
this.reconnectAttempt++;
this.setShell({ status: { type: "reconnecting", message } });
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
void this.start();
}, delay);
}
private async closeScope(): Promise<void> {
const scope = this.scope;
this.scope = null;
this.connection = null;
for (const entry of this.threads.values()) entry.fiber = null;
if (scope) await Effect.runPromise(Scope.close(scope, Exit.void));
}
private runInScope<A, E>(effect: Effect.Effect<A, E>): Fiber.Fiber<unknown, unknown> { private runInScope<A, E>(effect: Effect.Effect<A, E>): Fiber.Fiber<unknown, unknown> {
const scope = this.scope; const scope = this.scope;
if (!scope) throw new Error("Session is not started."); if (!scope) throw new Error("Session is not started.");
@@ -239,8 +341,8 @@ export class T3Session {
if (!entry) return; if (!entry) return;
entry.state = { ...entry.state, ...patch }; entry.state = { ...entry.state, ...patch };
if (entry.notifyTimer) return; if (entry.notifyTimer) return;
// The first snapshot and status changes show at once; streamed events batch. // The first snapshot, status and history changes show at once; streamed events batch.
const immediate = patch.status !== undefined || entry.state.status === "loading"; const immediate = patch.status !== undefined || patch.history !== undefined || entry.state.status === "loading";
const notify = () => { const notify = () => {
entry.notifyTimer = null; entry.notifyTimer = null;
for (const listener of entry.listeners) listener(entry.state); for (const listener of entry.listeners) listener(entry.state);
@@ -252,6 +354,8 @@ export class T3Session {
const isActiveRun = (status: string) => status === "preparing" || status === "starting" || status === "running" || status === "waiting"; const isActiveRun = (status: string) => status === "preparing" || status === "starting" || status === "running" || status === "waiting";
const isSocketFailure = (message: string) => /Socket(Close|Open|Generic)?Error|socket/i.test(message);
const newCommandId = () => CommandId.make(crypto.randomUUID()); const newCommandId = () => CommandId.make(crypto.randomUUID());
const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error)); const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error));
+8 -1
View File
@@ -175,7 +175,14 @@ export function App(props: { readonly session: T3Session; readonly attachThreadI
return { needs, active }; return { needs, active };
}, [shell.snapshot]); }, [shell.snapshot]);
const status = shell.status.type === "error" ? `error: ${shell.status.message}` : shell.status.type === "connecting" ? "connecting…" : (shell.serverUrl ?? ""); const status =
shell.status.type === "error"
? `error: ${shell.status.message}`
: shell.status.type === "connecting"
? "connecting…"
: shell.status.type === "reconnecting"
? "reconnecting…"
: (shell.serverUrl ?? "");
const listWidth = attachThreadId ? 0 : Math.min(46, Math.max(28, Math.floor(width * 0.3))); const listWidth = attachThreadId ? 0 : Math.min(46, Math.max(28, Math.floor(width * 0.3)));
const subtitle = threadShell const subtitle = threadShell
? [threadShell.modelSelection.model === "default" ? "" : threadShell.modelSelection.model, threadShell.branch, relativeTime(lastActivity(threadShell))].filter(Boolean).join(" · ") ? [threadShell.modelSelection.model === "default" ? "" : threadShell.modelSelection.model, threadShell.branch, relativeTime(lastActivity(threadShell))].filter(Boolean).join(" · ")