Open History to see the last commit
1# Architecture
2
3T3 Code runs as a **Node.js WebSocket server** that wraps `codex app-server` (JSON-RPC over stdio) and serves a React web app.
4
5```
6┌─────────────────────────────────┐
7│ Browser (React + Vite) │
8│ wsTransport (state machine) │
9│ Typed push decode at boundary │
10└──────────┬──────────────────────┘
11 │ ws://localhost:3773
12┌──────────▼──────────────────────┐
13│ apps/server (Node.js) │
14│ WebSocket + HTTP static server │
15│ ServerPushBus (ordered pushes) │
16│ ServerReadiness (startup gate) │
17│ OrchestrationEngine │
18│ ProviderService │
19│ CheckpointReactor │
20│ RuntimeReceiptBus │
21└──────────┬──────────────────────┘
22 │ JSON-RPC over stdio
23┌──────────▼──────────────────────┐
24│ codex app-server │
25└─────────────────────────────────┘
26```
27
28## Components
29
30- **Browser app**: The React app renders session state, owns the client-side WebSocket transport, and treats typed push events as the boundary between server runtime details and UI state.
31
32- **Server**: `apps/server` is the main coordinator. It serves the web app, accepts WebSocket requests, waits for startup readiness before welcoming clients, and sends all outbound pushes through a single ordered push path.
33
34- **Provider runtime**: `codex app-server` does the actual provider/session work. The server talks to it over JSON-RPC on stdio and translates those runtime events into the app's orchestration model.
35
36- **Background workers**: Long-running async flows such as runtime ingestion, command reaction, and checkpoint processing run as queue-backed workers. This keeps work ordered, reduces timing races, and gives tests a deterministic way to wait for the system to go idle.
37
38- **Runtime signals**: The server emits lightweight typed receipts when important async milestones finish, such as checkpoint capture, diff finalization, or a turn becoming fully quiescent. Tests and orchestration code wait on these signals instead of polling internal state.
39
40## Event Lifecycle
41
42### Startup and client connect
43
44```mermaid
45sequenceDiagram
46 participant Browser
47 participant Transport as WsTransport
48 participant Server as wsServer
49 participant Layers as serverLayers
50 participant Ready as ServerReadiness
51 participant Push as ServerPushBus
52
53 Browser->>Transport: Load app and open WebSocket
54 Transport->>Server: Connect
55 Server->>Layers: Start runtime services
56 Server->>Ready: Wait for startup barriers
57 Ready-->>Server: Ready
58 Server->>Push: Publish server.welcome
59 Push-->>Transport: Ordered welcome push
60 Transport-->>Browser: Hydrate initial state
61```
62
631. The browser boots [`WsTransport`][1] and registers typed listeners in [`wsNativeApi`][2].
642. The server accepts the connection in [`wsServer`][3] and brings up the runtime graph defined in [`serverLayers`][7].
653. [`ServerReadiness`][4] waits until the key startup barriers are complete.
664. Once the server is ready, [`wsServer`][3] sends `server.welcome` from the contracts in [`ws.ts`][6] through [`ServerPushBus`][5].
675. The browser receives that ordered push through [`WsTransport`][1], and [`wsNativeApi`][2] uses it to seed local client state.
68
69### User turn flow
70
71```mermaid
72sequenceDiagram
73 participant Browser
74 participant Transport as WsTransport
75 participant Server as wsServer
76 participant Provider as ProviderService
77 participant Codex as codex app-server
78 participant Ingest as ProviderRuntimeIngestion
79 participant Engine as OrchestrationEngine
80 participant Push as ServerPushBus
81
82 Browser->>Transport: Send user action
83 Transport->>Server: Typed WebSocket request
84 Server->>Provider: Route request
85 Provider->>Codex: JSON-RPC over stdio
86 Codex-->>Ingest: Provider runtime events
87 Ingest->>Engine: Normalize into orchestration events
88 Engine-->>Server: Domain events
89 Server->>Push: Publish orchestration.domainEvent
90 Push-->>Browser: Typed push
91```
92
931. A user action in the browser becomes a typed request through [`WsTransport`][1] and the browser API layer in [`nativeApi`][12].
942. [`wsServer`][3] decodes that request using the shared WebSocket contracts in [`ws.ts`][6] and routes it to the right service.
953. [`ProviderService`][8] starts or resumes a session and talks to `codex app-server` over JSON-RPC on stdio.
964. Provider-native events are pulled back into the server by [`ProviderRuntimeIngestion`][9], which converts them into orchestration events.
975. [`OrchestrationEngine`][10] persists those events, updates the read model, and exposes them as domain events.
986. [`wsServer`][3] pushes those updates to the browser through [`ServerPushBus`][5] on channels defined in [`orchestration.ts`][11].
99
100### Async completion flow
101
102```mermaid
103sequenceDiagram
104 participant Server as wsServer
105 participant Worker as Queue-backed workers
106 participant Cmd as ProviderCommandReactor
107 participant Checkpoint as CheckpointReactor
108 participant Receipt as RuntimeReceiptBus
109 participant Push as ServerPushBus
110 participant Browser
111
112 Server->>Worker: Enqueue follow-up work
113 Worker->>Cmd: Process provider commands
114 Worker->>Checkpoint: Process checkpoint tasks
115 Checkpoint->>Receipt: Publish completion receipt
116 Cmd-->>Server: Produce orchestration changes
117 Checkpoint-->>Server: Produce orchestration changes
118 Server->>Push: Publish resulting state updates
119 Push-->>Browser: User-visible push
120```
121
1221. Some work continues after the initial request returns, especially in [`ProviderRuntimeIngestion`][9], [`ProviderCommandReactor`][13], and [`CheckpointReactor`][14].
1232. These flows run as queue-backed workers using [`DrainableWorker`][16], which helps keep side effects ordered and test synchronization deterministic.
1243. When a milestone completes, the server emits a typed receipt on [`RuntimeReceiptBus`][15], such as checkpoint completion or turn quiescence.
1254. Tests and orchestration code wait on those receipts instead of polling git state, projections, or timers.
1265. Any user-visible state changes produced by that async work still go back through [`wsServer`][3] and [`ServerPushBus`][5].
127
128[1]: ../apps/web/src/wsTransport.ts
129[2]: ../apps/web/src/wsNativeApi.ts
130[3]: ../apps/server/src/wsServer.ts
131[4]: ../apps/server/src/wsServer/readiness.ts
132[5]: ../apps/server/src/wsServer/pushBus.ts
133[6]: ../packages/contracts/src/ws.ts
134[7]: ../apps/server/src/serverLayers.ts
135[8]: ../apps/server/src/provider/Layers/ProviderService.ts
136[9]: ../apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
137[10]: ../apps/server/src/orchestration/Layers/OrchestrationEngine.ts
138[11]: ../packages/contracts/src/orchestration.ts
139[12]: ../apps/web/src/nativeApi.ts
140[13]: ../apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
141[14]: ../apps/server/src/orchestration/Layers/CheckpointReactor.ts
142[15]: ../apps/server/src/orchestration/Layers/RuntimeReceiptBus.ts
143[16]: ../packages/shared/src/DrainableWorker.ts