Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

environment.ts

294 lines15.2 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3​
4import {
5 EnvironmentId,
6 ForwardCompatibleOptional,
7 ProjectId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11​
12/** Wire version for orchestration snapshots, streams, commands, and RPC payloads. */
13export const ORCHESTRATION_PROTOCOL_VERSION = 2;
14export const ORCHESTRATION_PROTOCOL_VERSION_TEXT = "2";
15export const ORCHESTRATION_PROTOCOL_QUERY_PARAM = "orchestrationProtocol";
16export const ORCHESTRATION_PROTOCOL_HEADER = "x-t3-orchestration-protocol";
17​
18export const ExecutionEnvironmentPlatformOs = Schema.Literals([
19 "darwin",
20 "linux",
21 "windows",
22 "unknown",
23]);
24export type ExecutionEnvironmentPlatformOs = typeof ExecutionEnvironmentPlatformOs.Type;
25​
26export const ExecutionEnvironmentPlatformArch = Schema.Literals(["arm64", "x64", "other"]);
27export type ExecutionEnvironmentPlatformArch = typeof ExecutionEnvironmentPlatformArch.Type;
28​
29/**
30 * The curated set of machine shapes and OS identities an environment can wear as its icon.
31 * Servers detect one from the hardware they run on (`platform.machine`), and
32 * the `environmentIcon` server setting lets a user pick one instead.
33 */
34export const ENVIRONMENT_MACHINE_KINDS = [
35 "server",
36 "cloud",
37 "linux",
38 "desktop",
39 "laptop",
40 "mac-mini",
41 "mac-studio",
42] as const;
43export const EnvironmentMachineKind = Schema.Literals(ENVIRONMENT_MACHINE_KINDS);
44export type EnvironmentMachineKind = typeof EnvironmentMachineKind.Type;
45export const isEnvironmentMachineKind = Schema.is(EnvironmentMachineKind);
46​
47export const ExecutionEnvironmentPlatform = Schema.Struct({
48 os: ExecutionEnvironmentPlatformOs,
49 arch: ExecutionEnvironmentPlatformArch,
50 /** Hardware shape detected at startup. Absent when the host gives no usable
51 signal (containers, Windows, unknown DMI), on servers that predate it, or
52 when a newer server names a kind this build cannot draw. */
53 machine: ForwardCompatibleOptional(EnvironmentMachineKind),
54});
55​
56/**
57 * Where a new thread runs: the project's current checkout ("local") or a
58 * fresh git worktree ("worktree"). Lives here (not settings.ts) so
59 * orchestration contracts can reference it without an import cycle.
60 */
61export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
62export type ThreadEnvMode = typeof ThreadEnvMode.Type;
63​
64/**
65 * How a new worktree populates git submodules: every nested level, only the
66 * ones this repository declares, or not at all.
67 */
68export const WorktreeSubmodules = Schema.Literals(["recursive", "top-level", "none"]);
69export type WorktreeSubmodules = typeof WorktreeSubmodules.Type;
70export type ExecutionEnvironmentPlatform = typeof ExecutionEnvironmentPlatform.Type;
71​
72/** How a server can replace itself with another version when asked over RPC.
73 New servers only advertise the stable launcher-backed "boot-service" path;
74 "respawn" remains decodable for compatibility with older servers.
75 "desktop-app" means the supervising desktop app updated and relaunched
76 itself, bringing the server back with it. */
77export const ServerSelfUpdateMethod = Schema.Literals(["boot-service", "respawn", "desktop-app"]);
78export type ServerSelfUpdateMethod = typeof ServerSelfUpdateMethod.Type;
79​
80/** Proven ownership for a manual update; unknown installs omit this descriptor. */
81export const ServerInstallation = Schema.Union([
82 Schema.Struct({ kind: Schema.Literals(["npx", "pnpm-dlx", "bunx"]) }),
83 Schema.Struct({ kind: Schema.Literal("npm-global"), prefix: TrimmedNonEmptyString }),
84]);
85export type ServerInstallation = typeof ServerInstallation.Type;
86​
87/** What update path a client should offer for a server: one of the RPC
88 self-update methods above, or "desktop-managed" when the backend's
89 version belongs to the T3 Code desktop app supervising it — updating the
90 app on that machine is the only way to update the server. */
91export const ServerSelfUpdateCapability = Schema.Literals([
92 "boot-service",
93 "respawn",
94 "desktop-managed",
95]);
96export type ServerSelfUpdateCapability = typeof ServerSelfUpdateCapability.Type;
97​
98export const ExecutionEnvironmentCapabilities = Schema.Struct({
99 repositoryIdentity: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
100 connectionProbe: Schema.optionalKey(Schema.Boolean),
101 /** Missing on older servers, which still accept inline image attachments. */
102 attachmentUploads: Schema.optionalKey(Schema.Boolean),
103 /** Uploaded files may accompany question answers. */
104 questionAttachments: Schema.optionalKey(Schema.Boolean),
105 /** Missing on servers that only accept image attachments. */
106 fileAttachments: Schema.optionalKey(
107 Schema.Struct({
108 maxUploadBytes: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),
109 }),
110 ),
111 /** Server exposes the pull-request list, detail, activity, diff, and mutation APIs. Absent on
112 servers from before the pull-request workspace shipped, so clients must not probe them. */
113 pullRequests: Schema.optionalKey(Schema.Boolean),
114 pullRequestChecks: Schema.optionalKey(Schema.Boolean),
115 /** Server understands canonical inline context links plus their message context records.
116 Absent on servers from before inline context shipped, which drop the records and forward
117 the links as literal text -- so a client must serialize context the legacy way for them. */
118 inlineMessageContext: Schema.optionalKey(Schema.Boolean),
119 /** Server rejects required worktrees instead of falling back to the project checkout. */
120 requiredWorktreeBootstrap: Schema.optionalKey(Schema.Boolean),
121 /** Server understands thread.settle / thread.unsettle commands. Absent on
122 pre-settlement servers, so clients treat missing as unsupported and
123 never send the commands under version skew. */
124 threadSettlement: Schema.optionalKey(Schema.Boolean),
125 /** Server evaluates merge and inactivity settlement without a client. */
126 threadAutoSettlement: Schema.optionalKey(Schema.Boolean),
127 storageCleanup: Schema.optionalKey(Schema.Boolean),
128 storageCleanupRun: Schema.optionalKey(Schema.Boolean),
129 /** Server checks OTLP endpoints for the telemetry export settings. */
130 otlpEndpointCheck: Schema.optionalKey(Schema.Boolean),
131 projectWorktreeCleanup: Schema.optionalKey(Schema.Boolean),
132 settledWorktreeCleanup: Schema.optionalKey(Schema.Boolean),
133 /** Server honors the `worktreesDirectory` setting. */
134 worktreesDirectory: Schema.optionalKey(Schema.Boolean),
135 /** Server can exclude PR-linked threads from inactivity settlement. */
136 threadAutoSettlementScope: Schema.optionalKey(Schema.Boolean),
137 /** Server persists the opt-in for continuing interrupted threads after restarts. */
138 threadRestartContinuation: Schema.optionalKey(Schema.Boolean),
139 /** Server resolves `projectSettingsOverrides`; older servers ignore the key. */
140 projectSettingsOverrides: Schema.optionalKey(Schema.Boolean),
141 /** Server resolves a project's `providerInstances` override entry. Older
142 servers decode the key away (unknown fields are stripped, not
143 rejected), so the per-project provider switch would silently do
144 nothing; clients hide it instead of sending a patch that goes nowhere. */
145 projectProviderInstanceOverrides: Schema.optionalKey(Schema.Boolean),
146 /** Server understands thread.snooze / thread.unsnooze commands. Same
147 version-skew contract as threadSettlement. */
148 threadSnooze: Schema.optionalKey(Schema.Boolean),
149 /** Server streams themes an environment publishes. Absent on servers from
150 before environment themes shipped, which never emit the events -- so a
151 client reconnecting to one must drop published themes rather than keep
152 showing a set nothing will ever update. */
153 environmentThemes: Schema.optionalKey(Schema.Boolean),
154 /** Server streams quota from configured usage-limit sources. Same
155 version-skew contract as environmentThemes. */
156 usageLimitSources: Schema.optionalKey(Schema.Boolean),
157 /** Server persists custom model rates and applies them to usage summaries. */
158 usagePriceOverrides: Schema.optionalKey(Schema.Boolean),
159 /** Server persists model mappings and folds mapped usage into the target model. */
160 usageModelAliases: Schema.optionalKey(Schema.Boolean),
161 /** Server understands thread.pin / thread.unpin commands. Same
162 version-skew contract as threadSettlement. */
163 threadPinning: Schema.optionalKey(Schema.Boolean),
164 /** Server understands thread.pin.reorder (and orderKey on thread.pin).
165 Same version-skew contract as threadSettlement. */
166 threadPinReorder: Schema.optionalKey(Schema.Boolean),
167 /** Server persists manual Active order through thread.active.reorder. */
168 threadActiveReorder: Schema.optionalKey(Schema.Boolean),
169 /** Server understands thread.auto-settle.set (per-thread auto-settle off).
170 Same version-skew contract as threadSettlement. */
171 threadAutoSettleOptOut: Schema.optionalKey(Schema.Boolean),
172 /** Server understands regenerateTitle on thread.meta.update. Absent on
173 older servers, so clients hide the action instead of sending it. */
174 threadTitleRegeneration: Schema.optionalKey(Schema.Boolean),
175 /** Server understands thread.visit / thread.mark-unread commands and
176 projects lastVisitedAt on thread shells. Same version-skew contract as
177 threadSettlement: clients keep their local visited state against
178 servers that lack this. */
179 threadVisitedTracking: Schema.optionalKey(Schema.Boolean),
180 /** Server persists a pull request reference on thread.meta.update. */
181 threadPullRequestLinking: Schema.optionalKey(Schema.Boolean),
182 /** Server resolves message delivery and model-selection context and validates
183 identified rollback readiness. Clients retain projection-based command
184 shaping and validation when this is absent. */
185 serverResolvedCommandContext: Schema.optionalKey(Schema.Boolean),
186 threadPullRequests: Schema.optionalKey(Schema.Boolean),
187 /** Server understands thread.pull-request.watch and wakes agents on pull request changes. */
188 threadPullRequestWatch: Schema.optionalKey(Schema.Boolean),
189 pullRequestStackActions: Schema.optionalKey(Schema.Boolean),
190 /** The update path clients should offer for this server. Absent on
191 servers that must be relaunched manually (dev checkouts, Windows
192 foreground runs, pre-update servers). */
193 serverSelfUpdate: Schema.optionalKey(ServerSelfUpdateCapability),
194 /** Manual commands must update this install, not the host's default global prefix. */
195 serverInstallation: ForwardCompatibleOptional(ServerInstallation),
196 /** Server can stream self-update progress before acknowledging the
197 restart. Clients fall back to server.updateServer when absent. */
198 serverSelfUpdateProgress: Schema.optionalKey(Schema.Boolean),
199 /** Server can durably mark running provider turns before a self-update and
200 continue them after the replacement process starts. */
201 serverUpdateThreadContinuation: Schema.optionalKey(Schema.Boolean),
202 /** Agent-activity publishes (push notifications and Live Activities)
203 currently leave this environment: the publish opt-in is enabled and the
204 relay link credentials exist. Clients skip seeding a Live Activity when
205 this is false — no update would ever repaint it. Absent on older
206 servers, which may still publish, so only an explicit false skips. */
207 agentActivityPublishing: Schema.optionalKey(Schema.Boolean),
208 /** Server runs repository clones for new projects in the background and
209 streams their progress (`projectClone.*`, `subscribeProjectClones`).
210 Absent on older servers, where clients must clone with the blocking
211 `sourceControl.cloneRepository` call instead. */
212 projectCloneTracking: Schema.optionalKey(Schema.Boolean),
213 /** Server detects `platform.machine` and persists the `environmentIcon`
214 setting. Older servers drop the key on write, so clients show the
215 picker inert rather than offering a choice that would never stick. */
216 environmentIcon: Schema.optionalKey(Schema.Boolean),
217 /** The desktop app supervising this server can be driven over RPC:
218 server.updateServer runs its check -> download -> relaunch. Absent on
219 desktop servers whose app predates the remote trigger, where clients
220 must keep telling the user to update the app on that machine. */
221 desktopAppUpdate: Schema.optionalKey(Schema.Boolean),
222 /** Server hosts preview tabs in its own headless Chromium (`runtime:
223 "server"`) and streams them over `/api/preview-stream`. Clients
224 without a local browser runtime open server tabs here. */
225 serverBrowser: Schema.optionalKey(Schema.Boolean),
226});
227export type ExecutionEnvironmentCapabilities = typeof ExecutionEnvironmentCapabilities.Type;
228​
229export const ExecutionEnvironmentDescriptor = Schema.Struct({
230 environmentId: EnvironmentId,
231 label: TrimmedNonEmptyString,
232 platform: ExecutionEnvironmentPlatform,
233 serverVersion: TrimmedNonEmptyString,
234 /** Absent on hosts from before explicit orchestration protocol negotiation. */
235 orchestrationProtocolVersion: Schema.optionalKey(Schema.Int),
236 capabilities: ExecutionEnvironmentCapabilities,
237});
238export type ExecutionEnvironmentDescriptor = typeof ExecutionEnvironmentDescriptor.Type;
239​
240export const RepositoryIdentityLocator = Schema.Struct({
241 source: Schema.Literal("git-remote"),
242 remoteName: TrimmedNonEmptyString,
243 remoteUrl: TrimmedNonEmptyString,
244});
245export type RepositoryIdentityLocator = typeof RepositoryIdentityLocator.Type;
246​
247/**
248 * The checkout's own remote when it names a different repository than the canonical one, such as
249 * a fork that tracks its upstream. Clients group and label by it so a fork stays distinct from the
250 * repository it forked, while pull request features keep the canonical identity.
251 */
252export const RepositoryOrigin = Schema.Struct({
253 canonicalKey: TrimmedNonEmptyString,
254 displayName: Schema.optionalKey(TrimmedNonEmptyString),
255});
256export type RepositoryOrigin = typeof RepositoryOrigin.Type;
257​
258export const RepositoryIdentity = Schema.Struct({
259 canonicalKey: TrimmedNonEmptyString,
260 locator: RepositoryIdentityLocator,
261 /** Repository browser URL resolved from the server's configured hosting account. */
262 webUrl: Schema.optionalKey(TrimmedNonEmptyString),
263 rootPath: Schema.optionalKey(TrimmedNonEmptyString),
264 displayName: Schema.optionalKey(TrimmedNonEmptyString),
265 provider: Schema.optionalKey(TrimmedNonEmptyString),
266 owner: Schema.optionalKey(TrimmedNonEmptyString),
267 name: Schema.optionalKey(TrimmedNonEmptyString),
268 origin: Schema.optionalKey(RepositoryOrigin),
269});
270export type RepositoryIdentity = typeof RepositoryIdentity.Type;
271​
272/** Key clients group checkouts by: a fork's own remote, otherwise the canonical repository. */
273export function repositoryGroupingKeyOf(identity: RepositoryIdentity): string {
274 return identity.origin?.canonicalKey ?? identity.canonicalKey;
276​
277/** Label clients show for a checkout's repository, matching `repositoryGroupingKeyOf`. */
278export function repositoryGroupingDisplayNameOf(identity: RepositoryIdentity): string | undefined {
279 return identity.origin
280 ? (identity.origin.displayName ?? identity.origin.canonicalKey)
281 : identity.displayName;
283​
284export const ScopedProjectRef = Schema.Struct({
285 environmentId: EnvironmentId,
286 projectId: ProjectId,
287});
288export type ScopedProjectRef = typeof ScopedProjectRef.Type;
289​
290export const ScopedThreadRef = Schema.Struct({
291 environmentId: EnvironmentId,
292 threadId: ThreadId,
293});
294export type ScopedThreadRef = typeof ScopedThreadRef.Type;