The best way to control Claude Code, Codex and more from any computer or phone
perf(scripts): scenarios no longer start two CLI processes to add and remove each project (#35986)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Donny/강동윤 committed
Nov 17, 2026 at 15:53 UTC
85a90b60ac7aef2a84d8cd92b2fd383938d82fe4
2 files changed
+95
-8
docs/operations/scenarios.md
+4
-2
@@ -46,8 +46,10 @@ adds one fixture for what e2e cannot know about T3 Code:
46
- `t3.open(path)` opens the app, already signed in, at a path. `t3.open(thread)` opens it on a
47
thread.
48
49
-State is created through the app's own CLI, so it is real events and works on a base commit
50
-whose schema differs. New threads default to the replay agent's `showcase` recording: type a
49
+State is created through the server's own CLI and HTTP API, so it is real events and works on a
50
+base commit whose database schema differs. Projects go through the API (`/api/projects/mutate`),
51
+since each CLI call starts a process that takes about 0.7 s; a base older than that endpoint
52
+cannot run a scenario. New threads default to the replay agent's `showcase` recording: type a
53
message and send it. To play something else, add a recording to the
54
[replay agent](development.md#replay-provider).
55
scripts/scenarios/t3.ts
+91
-6
@@ -1,17 +1,22 @@
1
// `test` for web scenarios: e2e's web test plus `t3`, which seeds state and opens the signed-in
2
// app. `t3-mobile.ts` is the same for the iOS app.
3
+import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
4
import * as NodeServices from "@effect/platform-node/NodeServices";
5
import { test as base } from "@e2e-dev/web";
6
import { expect } from "e2e";
7
+import { CommandId, EnvironmentHttpApi, type ProjectMutation, ProjectId } from "@t3tools/contracts";
8
import * as HostProcess from "@t3tools/shared/HostProcess";
9
+import * as SecureRandom from "effect/crypto/SecureRandom";
10
import * as Effect from "effect/Effect";
11
import * as FileSystem from "effect/FileSystem";
12
+import * as HttpApiClient from "effect/http-api/HttpApiClient";
13
import * as Path from "effect/Path";
14
+import * as Schedule from "effect/Schedule";
15
import * as Schema from "effect/Schema";
16
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";
17
18
import { APP_DIR, T3_HOME } from "../e2e.config.ts";
14
-import { run } from "./shell.ts";
19
+import { run, ScenarioError } from "./shell.ts";
20
21
export interface T3State {
22
/** Creates a git repository holding `files` and adds it as a project. Returns its path. */
@@ -52,7 +57,28 @@ const decodeLaunched = Schema.decodeUnknownEffect(
57
Schema.fromJsonString(Schema.Struct({ threadId: Schema.String })),
58
);
59
55
-/** State for the server on `home`, made through the app's own CLI: real events, not inserted rows. */
60
+const decodeRuntimeState = Schema.decodeUnknownEffect(
61
+ Schema.fromJsonString(Schema.Struct({ origin: Schema.String, startedAt: Schema.String })),
62
+);
63
+
64
+type ServerApi = {
65
+ readonly client: HttpApiClient.ForApi<typeof EnvironmentHttpApi>;
66
+ readonly headers: { readonly authorization: string };
67
+};
68
+
69
+/**
70
+ * One API client per running server, signed in once. Each CLI call starts a process that takes
71
+ * about 0.7 s before it does anything, so per-test state goes through the server's HTTP API. A
72
+ * server started again on the same home, as the second server is for each test, gets a new one.
73
+ */
74
+const serverApis = new Map<string, Promise<ServerApi>>();
75
+
76
+const uuid = SecureRandom.SecureRandom.pipe(
77
+ Effect.flatMap((random) => random.uuidV4()),
78
+ Effect.provide(NodeCrypto.layer),
79
+);
80
+
81
+/** State for the server on `home`, made through the app's own CLI and API: real events, not inserted rows. */
82
export function makeState(home: string, env: NodeJS.ProcessEnv = {}) {
83
const cli = Effect.fn("scenario.cli")(function* (
84
args: ReadonlyArray<string>,
@@ -69,7 +95,52 @@ export function makeState(home: string, env: NodeJS.ProcessEnv = {}) {
95
},
96
);
97
});
72
- const projects: Array<string> = [];
98
+
99
+ // The server writes its origin beside its database once it listens. It also prints its pairing
100
+ // token, which can come first, so a missing file is read again.
101
+ const runtimeState = Effect.gen(function* () {
102
+ const fs = yield* FileSystem.FileSystem;
103
+ const path = yield* Path.Path;
104
+ return yield* decodeRuntimeState(
105
+ yield* fs.readFileString(path.join(home, "userdata", "server-runtime.json")),
106
+ );
107
+ }).pipe(
108
+ Effect.retry({
109
+ schedule: Schedule.spaced("50 millis"),
110
+ times: 100,
111
+ while: (error) => error._tag === "PlatformError" && error.reason._tag === "NotFound",
112
+ }),
113
+ );
114
+ const connect = Effect.fn("scenario.connect")(function* (origin: string) {
115
+ const issued = yield* cli(["auth", "session", "issue", "--token-only", "--label", "scenario"]);
116
+ const client = yield* HttpApiClient.make(EnvironmentHttpApi, { baseUrl: origin });
117
+ return { client, headers: { authorization: `Bearer ${issued.stdout.trim()}` } };
118
+ }, Effect.provide(FetchHttpClient.layer));
119
+ const api = Effect.gen(function* () {
120
+ const { origin, startedAt } = yield* runtimeState;
121
+ const key = `${home}\n${origin}\n${startedAt}`;
122
+ return yield* Effect.tryPromise({
123
+ try: () => {
124
+ const known = serverApis.get(key);
125
+ if (known !== undefined) return known;
126
+ const connecting = runPromise(connect(origin));
127
+ serverApis.set(key, connecting);
128
+ // A failed sign-in is not kept: the next test tries again.
129
+ connecting.catch(() => serverApis.delete(key));
130
+ return connecting;
131
+ },
132
+ catch: (cause) => new ScenarioError({ message: `Could not reach the server API: ${cause}` }),
133
+ });
134
+ });
135
+ const mutate = Effect.fn("scenario.mutate")(function* (mutation: ProjectMutation) {
136
+ const { client, headers } = yield* api;
137
+ return yield* client.projects
138
+ .mutate({ headers, payload: mutation })
139
+ .pipe(
140
+ Effect.mapError((cause) => new ScenarioError({ message: `${mutation.type}: ${cause}` })),
141
+ );
142
+ });
143
+ const projects: Array<ProjectId> = [];
144
145
const project = Effect.fn("scenario.project")(function* (
146
title: string,
@@ -95,8 +166,14 @@ export function makeState(home: string, env: NodeJS.ProcessEnv = {}) {
166
yield* git(["init", "--quiet", "--initial-branch=main"]);
167
yield* git(["add", "--all"]);
168
yield* git([...author, "commit", "--quiet", "--message", "Initial commit"]);
98
- yield* cli(["project", "add", workspace, "--title", title]);
99
- projects.push(workspace);
169
+ const created = yield* mutate({
170
+ type: "project.create",
171
+ commandId: CommandId.make(yield* uuid),
172
+ projectId: ProjectId.make(yield* uuid),
173
+ title,
174
+ workspaceRoot: workspace,
175
+ });
176
+ projects.push(created.id);
177
return workspace;
178
});
179
@@ -126,7 +203,15 @@ export function makeState(home: string, env: NodeJS.ProcessEnv = {}) {
203
// The server outlives the test, so the next one must not see this one's projects.
204
cleanup: Effect.forEach(
205
projects,
129
- (workspace) => cli(["project", "remove", workspace, "--force"]),
206
+ (projectId) =>
207
+ Effect.flatMap(uuid, (commandId) =>
208
+ mutate({
209
+ type: "project.delete",
210
+ commandId: CommandId.make(commandId),
211
+ projectId,
212
+ force: true,
213
+ }),
214
+ ),
215
{ discard: true },
216
),
217
};