Open History to see the last commit
1# Provider architecture
2
3> For maintainers. Using T3 Code? See [docs/user](../user/).
4
5A provider is the agent runtime that does the actual work. T3 Code supports several, and the
6orchestration layer does not know which one is behind a thread.
7
8## Built-in drivers
9
10[`builtInDrivers.ts`][drivers] exports `BUILT_IN_DRIVERS` with five entries:
11
12| Driver kind | Driver source |
13| ------------- | --------------------------------------- |
14| `codex` | [`Drivers/CodexDriver.ts`][codex] |
15| `claudeAgent` | [`Drivers/ClaudeDriver.ts`][claude] |
16| `cursor` | [`Drivers/CursorDriver.ts`][cursor] |
17| `grok` | [`Drivers/GrokDriver.ts`][grok] |
18| `opencode` | [`Drivers/OpenCodeDriver.ts`][opencode] |
19
20Each driver declares its `driverKind`, a `configSchema`, and a `create` function that builds an
21adapter in a child scope. Adapter implementations live beside them in
22`apps/server/src/provider/Layers/` (`CodexAdapter.ts`, `ClaudeAdapter.ts`, and so on) and conform to
23[`ProviderAdapter.ts`][adapter]. Read the driver plus its adapter to see how a specific agent's
24transport, config, and event shapes are mapped.
25
26## Registry and routing
27
28Two registries separate configuration from live processes:
29
30- [`ProviderInstanceRegistry`][instances] keys configured instances by `ProviderInstanceId`. Creating
31 one looks up the driver by `driverKind`, decodes `entry.config` with that driver's schema, opens a
32 child scope, and calls `driver.create`.
33- [`ProviderAdapterRegistry`][registry] resolves an instance ID to its live adapter via
34 `getByInstance`.
35
36[`ProviderService`][service] sits on top. It combines the adapter registry with the provider session
37directory to route session and turn operations for a thread, so callers name a thread, not an agent.
38
39Adding a driver means writing the driver plus adapter and adding it to `BUILT_IN_DRIVERS`. No
40orchestration, contract, or client change is required for the common case.
41
42## OpenCode server ownership and catalog
43
44Each OpenCode provider instance owns one lazy local server for catalog discovery and
45text-generation helpers through [`OpenCodeServerOwner.ts`][opencode-server-owner]. Concurrent
46borrowers share startup. The server closes 30 seconds after the last borrower releases it, or
47when the provider instance closes. A failed or exited process can be started again on the next
48use. An externally configured OpenCode server remains externally owned.
49
50The local server and its SDK clients use one resolved password. An explicit provider password
51overrides `OPENCODE_SERVER_PASSWORD` in the spawned environment. Without an explicit password,
52the client uses the password from the environment that the process inherits. External servers use
53only their explicit provider password and never inherit the host's local password.
54
55Every server connection must pass the authenticated `/global/health` check before inventory or
56session operations start. The response must contain a valid version at or above 1.14.19. Local
57owners cache this result for the lifetime of the spawned process. External actions check once when
58they create their server connection, not for each model or SDK request.
59
60Chat adapters keep their own server per thread. They register a thread-specific `t3-code` MCP
61connection, while OpenCode stores MCP connections by directory. Sharing these chat servers
62without changing MCP routing would let two threads in one directory replace each other's
63connection.
64
65OpenCode loads its catalog through the HTTP API when an enabled provider instance starts. The
66provider registry keeps the snapshot in memory and persists it in the existing per-instance cache.
67Each `subscribeServerConfig` connection refreshes all providers, so a client reconnect reloads the
68OpenCode catalog from the current helper. The `serverRefreshProviders` request also refreshes it.
69Periodic OpenCode probes remain disabled. OpenCode reads credentials for each inventory request,
70but its native configuration files can remain cached for the lifetime of the helper process. The
71helper closes 30 seconds after its last inventory or text-generation borrower releases it. A
72refresh after that idle period starts a new helper and reads file changes. Repeated refreshes and
73active text-generation work can extend process reuse. Changes to the provider configuration or
74environment replace the instance and start a new discovery. Changes to unrelated settings only
75update snapshot enrichment. Other providers retain their existing refresh policy.
76
77T3 Code does not own an external OpenCode process. Native configuration changes there can require
78an external reload or restart before T3 Code's next refresh sees them.
79
80The shared server's idle shutdown does not clear the catalog. Failed discovery keeps the last
81known models, slash commands, and skills through the registry's existing merge rules. A successful
82empty inventory is authoritative. Existing threads keep their explicit model identifier and
83options when catalog metadata is missing; the catalog is not permission to choose a different
84model for a thread.
85
86## Model manifest
87
88The model picker's legacy section is driven by `apps/server/src/provider/model-manifest.json`, which
89lists the current (non-legacy) model slugs per driver kind. The `ModelManifest` service
90(`apps/server/src/provider/ModelManifest.ts`) refreshes that data from the same file on `main` via
91raw.githubusercontent.com, so moving a model in or out of the legacy section is a commit, not a
92release. Preference order is remote fetch, then the on-disk copy of the last successful fetch (in
93the state directory), then the bundled copy. Fetches are TTL-gated, run concurrently with provider
94probes, respect the `enableProviderUpdateChecks` setting, and never fail a provider check. The
95Codex and Claude drivers apply the classification to every snapshot with `applyModelManifest`;
96driver kinds absent from the manifest have no legacy concept.
97
98## Attachment access
99
100The server stores uploaded attachments in its attachment directory, outside the project workspace.
101`ProviderService` adds the absolute path of each attachment to the turn text, then passes every
102attachment to the provider adapter. Each adapter decides what its provider ingests natively:
103
104- Codex, Claude, Cursor, and Grok send images as native image inputs and skip generic files. For
105 these providers, generic files reach the agent only as file paths in the turn text.
106- OpenCode sends PNG/JPEG/GIF/WebP images, text files, and PDFs up to 20 MB as native file parts
107 with their real mime type. Everything else (ZIP and other binaries, image formats model APIs
108 reject, oversized files) falls back to the file path in the turn text, like the other providers.
109
110Claude receives the attachment directory as an allowed additional directory. Codex keeps its
111configured sandbox policy, so access depends on that policy and the selected runtime mode. OpenCode
112allows all paths in full-access mode and requests approval for directories outside the workspace in
113restricted modes. Cursor and Grok use their own provider permission rules.
114
115The server does not copy attachments into a project or bypass provider approval rules. If an agent
116cannot read an attachment, the user must approve the access or select a runtime mode that permits it.
117
118Updated attachment schemas tolerate unknown attachment members, but old image-only clients still
119cannot decode messages that contain file attachments. Client file-picking rollouts must account for
120this limit.
121
122Do not run an old image-only server against state that contains file attachments. Replay decodes
123each persisted event before projection. A file-bearing event can make `ProjectionPipeline` bootstrap
124and `OrchestrationEngine` startup fail for the entire environment, not only the affected thread.
125
126## How provider work is requested
127
128Clients never call a provider directly. They dispatch orchestration commands over the RPC method
129`orchestration.dispatchCommand`, defined with the rest of the orchestration surface in
130[`orchestration.ts`][contracts]. The client-dispatchable provider-facing commands are
131`thread.turn.start`, `thread.turn.interrupt`, `thread.approval.respond`,
132`thread.user-input.respond`, `thread.checkpoint.revert`, and `thread.session.stop`, plus the mode
133setters `thread.runtime-mode.set` and `thread.interaction-mode.set`.
134
135The engine persists an event for the command, and a server-side reactor performs the provider call.
136Provider output comes back as internal commands such as `thread.message.assistant.delta` and
137`thread.session.set`, which clients observe through `orchestration.subscribeThread`. See
138[overview.md](./overview.md) for the command/event loop.
139
140## Server-side workers
141
142Provider work flows through three queue-backed workers. All three are built with
143`makeDrainableWorker` from [`DrainableWorker.ts`][worker] and expose `drain` for deterministic test
144synchronization.
145
1461. [`ProviderRuntimeIngestion`][ingest] consumes provider runtime streams and emits orchestration
147 commands.
1482. [`ProviderCommandReactor`][cmd] reacts to orchestration intent events and dispatches provider
149 calls.
1503. [`CheckpointReactor`][checkpoint] captures workspace checkpoints on turn start and completion, and
151 performs reverts.
152
153### Buffered assistant delivery
154
155A thread in `buffered` assistant delivery mode accumulates assistant text instead of streaming each
156delta. The buffer is not held until turn completion. In [`ProviderRuntimeIngestion`][ingest],
157`MAX_BUFFERED_ASSISTANT_CHARS` is 24,000: the append that would exceed it invalidates the buffer and
158spills the whole accumulated text as one delta. The buffer also flushes at interaction boundaries,
159when a request opens (approval) or user input is requested, via
160`flushBufferedAssistantMessagesForTurn`.
161
162[drivers]: ../../apps/server/src/provider/builtInDrivers.ts
163[codex]: ../../apps/server/src/provider/Drivers/CodexDriver.ts
164[claude]: ../../apps/server/src/provider/Drivers/ClaudeDriver.ts
165[cursor]: ../../apps/server/src/provider/Drivers/CursorDriver.ts
166[grok]: ../../apps/server/src/provider/Drivers/GrokDriver.ts
167[opencode]: ../../apps/server/src/provider/Drivers/OpenCodeDriver.ts
168[opencode-server-owner]: ../../apps/server/src/provider/OpenCodeServerOwner.ts
169[adapter]: ../../apps/server/src/provider/Services/ProviderAdapter.ts
170[instances]: ../../apps/server/src/provider/Services/ProviderInstanceRegistry.ts
171[registry]: ../../apps/server/src/provider/Services/ProviderAdapterRegistry.ts
172[service]: ../../apps/server/src/provider/Layers/ProviderService.ts
173[contracts]: ../../packages/contracts/src/orchestration.ts
174[worker]: ../../packages/shared/src/DrainableWorker.ts
175[ingest]: ../../apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
176[cmd]: ../../apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
177[checkpoint]: ../../apps/server/src/orchestration/Layers/CheckpointReactor.ts