Open History to see the last commit
1import {
2 DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
3 type EnvironmentId,
4 type TerminalAttachInput,
5 type TerminalCloseInput,
6 type TerminalOpenInput,
7 type TerminalResizeInput,
8 type TerminalSummary,
9 type TerminalWriteInput,
10 type ThreadId,
11 WS_METHODS,
12} from "@t3tools/contracts";
13import * as Effect from "effect/Effect";
14import * as Stream from "effect/Stream";
15import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";
16
17import {
18 type AtomCommand,
19 createAtomCommandScheduler,
20 createEnvironmentRpcCommand,
21 createEnvironmentSubscriptionAtomFamily,
22 runAtomCommand,
23} from "./runtime.ts";
24import type { EnvironmentRegistry } from "../connection/registry.ts";
25import { subscribe, subscribeDynamic, type EnvironmentRpcInput } from "../rpc/client.ts";
26import {
27 applyTerminalAttachStreamEvent,
28 applyTerminalMetadataStreamEvent,
29 EMPTY_TERMINAL_BUFFER_STATE,
30 nextTerminalAttachSeedState,
31 type TerminalBufferState,
32} from "./terminalSession.ts";
33import { terminalOutputText } from "./terminalOutput.ts";
34
35export function createTerminalEnvironmentAtoms<R, E>(
36 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
37) {
38 const lifecycleScheduler = createAtomCommandScheduler();
39 const resizeScheduler = createAtomCommandScheduler();
40 const terminalThreadKey = ({
41 environmentId,
42 input,
43 }: {
44 readonly environmentId: string;
45 readonly input: { readonly threadId: string; readonly terminalId?: string | undefined };
46 }) => JSON.stringify([environmentId, input.threadId]);
47 const terminalSessionKey = ({
48 environmentId,
49 input,
50 }: {
51 readonly environmentId: string;
52 readonly input: { readonly threadId: string; readonly terminalId?: string | undefined };
53 }) => JSON.stringify([environmentId, input.threadId, input.terminalId ?? null]);
54 const lifecycleConcurrency = { mode: "serial" as const, key: terminalThreadKey };
55 const atoms = {
56 observe: createEnvironmentSubscriptionAtomFamily(runtime, {
57 label: "environment-data:terminal:observe",
58 subscribe: (input: EnvironmentRpcInput<typeof WS_METHODS.terminalObserve>) =>
59 Stream.suspend(() =>
60 subscribe(WS_METHODS.terminalObserve, input).pipe(
61 Stream.scan(nextTerminalAttachSeedState, applyTerminalAttachStreamEvent),
62 ),
63 ),
64 }),
65 attach: createEnvironmentSubscriptionAtomFamily(runtime, {
66 label: "environment-data:terminal:attach",
67 subscribe: (input: EnvironmentRpcInput<typeof WS_METHODS.terminalAttach>) => {
68 // Retry an unanswered first attach unchanged so it can still open the shell.
69 let replied = false;
70 return subscribeDynamic(WS_METHODS.terminalAttach, () =>
71 Effect.sync(() => ({
72 ...input,
73 ...(replied ? { resume: true } : {}),
74 maxHistoryBytes: DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
75 })),
76 ).pipe(
77 Stream.onFirst(() =>
78 Effect.sync(() => {
79 replied = true;
80 }),
81 ),
82 Stream.scan(nextTerminalAttachSeedState, applyTerminalAttachStreamEvent),
83 );
84 },
85 }),
86 metadata: createEnvironmentSubscriptionAtomFamily(runtime, {
87 label: "environment-data:terminal:metadata",
88 subscribe: (_input: null) =>
89 subscribe(WS_METHODS.subscribeTerminalMetadata, {}).pipe(
90 Stream.scan((): ReadonlyArray<TerminalSummary> => [], applyTerminalMetadataStreamEvent),
91 // The scan seed is not an authoritative empty server snapshot.
92 Stream.drop(1),
93 ),
94 }),
95 open: createEnvironmentRpcCommand(runtime, {
96 label: "environment-data:terminal:open",
97 tag: WS_METHODS.terminalOpen,
98 scheduler: lifecycleScheduler,
99 concurrency: lifecycleConcurrency,
100 }),
101 write: createEnvironmentRpcCommand(runtime, {
102 label: "environment-data:terminal:write",
103 tag: WS_METHODS.terminalWrite,
104 }),
105 resize: createEnvironmentRpcCommand(runtime, {
106 label: "environment-data:terminal:resize",
107 tag: WS_METHODS.terminalResize,
108 scheduler: resizeScheduler,
109 concurrency: { mode: "latest", key: terminalSessionKey },
110 }),
111 clear: createEnvironmentRpcCommand(runtime, {
112 label: "environment-data:terminal:clear",
113 tag: WS_METHODS.terminalClear,
114 scheduler: lifecycleScheduler,
115 concurrency: lifecycleConcurrency,
116 }),
117 close: createEnvironmentRpcCommand(runtime, {
118 label: "environment-data:terminal:close",
119 tag: WS_METHODS.terminalClose,
120 scheduler: lifecycleScheduler,
121 concurrency: lifecycleConcurrency,
122 }),
123 };
124 return {
125 ...atoms,
126 createSession: (options: TerminalSessionOptions) => createTerminalSession(atoms, options),
127 };
128}
129
130export interface TerminalLifecycleState {
131 readonly wasRunning: boolean;
132 /** The user has seen this terminal without operate permission. */
133 readonly observed: boolean;
134 readonly reopened: boolean;
135}
136
137export const INITIAL_TERMINAL_LIFECYCLE: TerminalLifecycleState = {
138 wasRunning: false,
139 observed: false,
140 reopened: false,
141};
142
143export type TerminalLifecycleEvent =
144 /** A viewer without operate permission is following this terminal. */
145 | { readonly type: "observed" }
146 | { readonly type: "detached"; readonly canOperate: boolean }
147 | {
148 readonly type: "status";
149 readonly status: TerminalBufferState["status"];
150 readonly version: number;
151 }
152 | { readonly type: "reopen-failed" };
153
154export type TerminalLifecycleCommand = "exit" | "reopen" | null;
155
156export function transitionTerminalLifecycle(
157 state: TerminalLifecycleState,
158 event: TerminalLifecycleEvent,
159): { readonly state: TerminalLifecycleState; readonly command: TerminalLifecycleCommand } {
160 switch (event.type) {
161 case "observed":
162 return { state: { ...state, observed: true }, command: null };
163 case "detached":
164 return {
165 state: {
166 ...state,
167 // Losing operate while the shell runs leaves the viewer reading its history.
168 observed: state.observed || (!event.canOperate && state.wasRunning),
169 wasRunning: false,
170 },
171 command: null,
172 };
173 case "reopen-failed":
174 return { state: { ...state, reopened: false }, command: null };
175 case "status": {
176 if (event.version === 0) return { state, command: null };
177 if (event.status === "running" || event.status === "starting") {
178 return { state: { ...state, wasRunning: true, reopened: false }, command: null };
179 }
180 if (event.status !== "closed" && event.status !== "exited") return { state, command: null };
181 if (state.wasRunning) {
182 return { state: { ...state, wasRunning: false, reopened: true }, command: "exit" };
183 }
184 // An explicit visit can reopen a cached ended session. Granting a viewer
185 // operate permission must leave the history they were reading intact.
186 if (state.observed || state.reopened) return { state, command: null };
187 return { state: { ...state, reopened: true }, command: "reopen" };
188 }
189 }
190}
191
192export interface TerminalGridSize {
193 readonly cols: number;
194 readonly rows: number;
195}
196
197export type TerminalSessionMode =
198 | { readonly kind: "observing" }
199 | { readonly kind: "attached"; readonly input: TerminalAttachInput };
200
201type EnvironmentCommand<Input> = AtomCommand<
202 { readonly environmentId: EnvironmentId; readonly input: Input },
203 unknown,
204 unknown
205>;
206
207export interface TerminalSessionAtoms {
208 readonly attach: (target: {
209 readonly environmentId: EnvironmentId;
210 readonly input: TerminalAttachInput;
211 }) => Atom.Atom<AsyncResult.AsyncResult<TerminalBufferState, unknown>>;
212 readonly observe: (target: {
213 readonly environmentId: EnvironmentId;
214 readonly input: { readonly threadId: ThreadId; readonly terminalId: string };
215 }) => Atom.Atom<AsyncResult.AsyncResult<TerminalBufferState, unknown>>;
216 readonly open: EnvironmentCommand<TerminalOpenInput>;
217 readonly resize: EnvironmentCommand<TerminalResizeInput>;
218 readonly write: EnvironmentCommand<TerminalWriteInput>;
219 readonly close: EnvironmentCommand<TerminalCloseInput>;
220}
221
222export interface TerminalSessionOptions {
223 readonly target: {
224 readonly environmentId: EnvironmentId;
225 readonly threadId: ThreadId;
226 readonly terminalId: string;
227 };
228 readonly gridSize: TerminalGridSize;
229 /** Written once, after the first attachment snapshot lands and the shell has the measured grid. */
230 readonly initialInput?: string | undefined;
231 /** Read right before every command, so revoking operate stops them at once. */
232 readonly canOperate: () => boolean;
233 /** The shell ended while attached; the store has already closed it. */
234 readonly onExit: () => void;
235 /** Fires when the shell starts or stops accepting input, and on every restart. */
236 readonly onLiveChange?: (live: boolean) => void;
237 readonly debugLog?: (message: string, data: Record<string, unknown>) => void;
238}
239
240export interface TerminalSession {
241 /**
242 * Mount while the session follows a terminal: attached to operate it, or
243 * observing it read-only. `null` follows nothing. A session keeps the first
244 * attach input it is given; a different input needs a new session.
245 */
246 readonly driver: (mode: TerminalSessionMode | null) => Atom.Atom<void>;
247 /** The surface measured a new grid; the shell is resized while it runs. */
248 readonly reportGridSize: (size: TerminalGridSize) => void;
249}
250
251/**
252 * Owns one shown terminal's lifecycle: it reopens an ended shell on an explicit
253 * visit, closes and reports a shell that exits while attached, replays the
254 * measured grid whenever the server starts a new output generation, and writes
255 * the launch's initial input once that grid has reached the shell.
256 */
257export function createTerminalSession(
258 atoms: TerminalSessionAtoms,
259 options: TerminalSessionOptions,
260): TerminalSession {
261 const { environmentId, threadId, terminalId } = options.target;
262 let lifecycle = INITIAL_TERMINAL_LIFECYCLE;
263 let gridSize = options.gridSize;
264 // The seeded grid is a cache or default guess; launch input waits for the surface's own measurement.
265 let gridMeasured = false;
266 let generation: number | null = null;
267 let liveLifecycleVersion: number | null = null;
268 let initialInputPending = Boolean(options.initialInput);
269 let lastStatus: TerminalBufferState["status"] | null = null;
270 let loggedFirstOutput = false;
271 let commandRegistry: AtomRegistry.AtomRegistry | null = null;
272 // Only a successful resize of the current measured grid may release initial input.
273 let shellResized: ReturnType<typeof runAtomCommand> | null = null;
274
275 const sendResize = () => {
276 const registry = commandRegistry;
277 if (generation === null || registry === null || !options.canOperate()) return;
278 const resized = runAtomCommand(registry, atoms.resize, {
279 environmentId,
280 input: { threadId, terminalId, cols: gridSize.cols, rows: gridSize.rows },
281 });
282 shellResized = resized;
283 void resized.then((result) => {
284 const data = options.initialInput;
285 if (
286 result._tag !== "Success" ||
287 resized !== shellResized ||
288 !gridMeasured ||
289 generation === null ||
290 !initialInputPending ||
291 !data ||
292 !options.canOperate()
293 )
294 return;
295 initialInputPending = false;
296 void runAtomCommand(registry, atoms.write, {
297 environmentId,
298 input: { threadId, terminalId, data },
299 });
300 });
301 };
302
303 const setLive = (next: number | null) => {
304 if (next === liveLifecycleVersion) return;
305 liveLifecycleVersion = next;
306 options.onLiveChange?.(next !== null);
307 };
308
309 const apply = (
310 event: TerminalLifecycleEvent,
311 registry: AtomRegistry.AtomRegistry,
312 input: TerminalAttachInput,
313 ) => {
314 const step = transitionTerminalLifecycle(lifecycle, event);
315 lifecycle = step.state;
316 if (step.command === "exit") {
317 if (!options.canOperate()) return;
318 void runAtomCommand(registry, atoms.close, {
319 environmentId,
320 input: { threadId, terminalId },
321 });
322 options.onExit();
323 return;
324 }
325 if (step.command !== "reopen") return;
326 const reopen = async () => {
327 if (input.cwd === undefined || !options.canOperate()) return false;
328 const result = await runAtomCommand(registry, atoms.open, {
329 environmentId,
330 input: {
331 threadId,
332 terminalId,
333 cwd: input.cwd,
334 worktreePath: input.worktreePath,
335 cols: input.cols,
336 rows: input.rows,
337 ...(input.env ? { env: input.env } : {}),
338 },
339 });
340 return result._tag === "Success";
341 };
342 void reopen().then((succeeded) => {
343 if (!succeeded)
344 lifecycle = transitionTerminalLifecycle(lifecycle, { type: "reopen-failed" }).state;
345 });
346 };
347
348 const onAttachment = (
349 registry: AtomRegistry.AtomRegistry,
350 input: TerminalAttachInput,
351 result: AsyncResult.AsyncResult<TerminalBufferState, unknown>,
352 ) => {
353 commandRegistry = registry;
354 const buffer = AsyncResult.getOrElse(result, () => EMPTY_TERMINAL_BUFFER_STATE);
355 const status = result._tag === "Failure" ? "error" : buffer.status;
356 if (status !== lastStatus) {
357 lastStatus = status;
358 options.debugLog?.("session:status", {
359 terminalId,
360 status,
361 error: buffer.error,
362 version: buffer.version,
363 });
364 }
365 if (!loggedFirstOutput && options.debugLog) {
366 const text = terminalOutputText(buffer.output);
367 if (text.length > 0) {
368 loggedFirstOutput = true;
369 options.debugLog("session:first-nonempty-buffer", {
370 terminalId,
371 length: text.length,
372 preview: text.slice(0, 160),
373 });
374 }
375 }
376
377 setLive(status === "running" || status === "starting" ? buffer.lifecycleVersion : null);
378
379 // Resize before the initial input so a launched command starts at the measured grid.
380 const nextGeneration =
381 buffer.version > 0 && status === "running" ? buffer.output.generation : null;
382 if (nextGeneration !== generation) {
383 generation = nextGeneration;
384 shellResized = null;
385 sendResize();
386 }
387
388 apply({ type: "status", status, version: buffer.version }, registry, input);
389 };
390
391 const label = `terminal-session:${environmentId}:${threadId}:${terminalId}`;
392 const idle = Atom.make<void>(undefined);
393 // Viewing read-only counts as observed before the first snapshot lands, so a
394 // grant that arrives early still leaves the ended shell's history alone.
395 const observing = Atom.make((get) => {
396 // `subscribe`, not `get`: a rebuild would resubscribe on every output chunk.
397 get.subscribe(
398 atoms.observe({ environmentId, input: { threadId, terminalId } }),
399 () => {
400 if (options.canOperate()) return;
401 lifecycle = transitionTerminalLifecycle(lifecycle, { type: "observed" }).state;
402 },
403 { immediate: true },
404 );
405 }).pipe(Atom.withLabel(`${label}:observing`));
406 const makeAttached = (input: TerminalAttachInput) =>
407 Atom.make((get) => {
408 const registry = get.registry;
409 get.addFinalizer(() => {
410 generation = null;
411 shellResized = null;
412 lastStatus = null;
413 setLive(null);
414 lifecycle = transitionTerminalLifecycle(lifecycle, {
415 type: "detached",
416 canOperate: options.canOperate(),
417 }).state;
418 });
419 get.subscribe(
420 atoms.attach({ environmentId, input }),
421 (result) => onAttachment(registry, input, result),
422 { immediate: true },
423 );
424 }).pipe(Atom.withLabel(`${label}:attached`));
425 let attached: Atom.Atom<void> | null = null;
426
427 return {
428 driver: (mode) => {
429 if (mode === null) return idle;
430 if (mode.kind === "observing") return observing;
431 attached ??= makeAttached(mode.input);
432 return attached;
433 },
434 reportGridSize: (size) => {
435 if (gridMeasured && size.cols === gridSize.cols && size.rows === gridSize.rows) return;
436 gridMeasured = true;
437 gridSize = size;
438 sendResize();
439 },
440 };
441}
442
443export * from "./terminalSession.ts";