The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 17:00 UTC
feat(mobile): one cached native-client install command for agents and machines (#24965)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 13:34 UTC
0191d6c647fe0c9af09717d675b91b5a611b3b9e
10 files changed
+995
-132
.agents/skills/test-t3-mobile/SKILL.md
+7
-3
@@ -31,11 +31,15 @@ when inspecting or seeding SQLite. Stop the test server before fixture writes.
31
From the checkout being tested on the selected device host, run:
32
33
```bash
34
-node scripts/mobile-native-client.ts ensure <ios|android> <device-id>
34
+vp run mobile:install <ios|android> <device-id>
35
```
36
37
-This reuses a matching native client or builds and installs one. Authorized
38
-mobile verification includes that build step unless the user prohibits it.
37
+This installs the dev client for the checkout's native fingerprint: from the
38
+device, the local cache, or the shared cache in seconds, building once only
39
+when no machine has that fingerprint yet (5-15 minutes, logged to the file it
40
+names). The device id may be omitted when exactly one device is booted. It
41
+prints one JSON line; on failure it names the next command. Authorized mobile
42
+verification includes that step unless the user prohibits it.
43
44
Start `vp run dev:client` from `apps/mobile`, or reuse a healthy Metro belonging
45
to this checkout. Open its printed development-client URL with AgentDevice
AGENTS.md
+1
-1
@@ -134,7 +134,7 @@ An empty database is a bad test. Seed your worktree's `.t3` with a copy of real
134
135
- Upon request, user-visible frontend changes should get one integrated pass in a real client: `test-t3-app` for web, `test-t3-mobile` for mobile. The primary agent does this once after integrating. Subagents do not launch their own dev servers. Ask permission before doing computer use or spinning up browsers.
136
137
-For authorized mobile verification, a missing or outdated native client is a build step, not a blocker. Run `node scripts/mobile-native-client.ts ensure <ios|android> <device-id>` on the simulator host before starting Metro. It checks the local Expo fingerprint and builds/installs when needed. See `test-t3-mobile` for the full workflow.
137
+For authorized mobile verification, a missing or outdated native client is an install step, not a blocker. Run `vp run mobile:install <ios|android> [device-id]` on the device host before starting Metro; it targets the only booted simulator or connected Android device when the id is omitted. It installs the dev client for this checkout's native fingerprint from the shared cache in seconds and builds only when no machine has built it yet, so prefer it over `expo run:*`, `xcodebuild`, or `gradlew`, which rebuild from scratch. Its stdout is one JSON line; build output goes to the log it names. See `test-t3-mobile` for the full workflow.
138
139
## Pull requests
140
apps/mobile/README.md
+17
-9
@@ -22,20 +22,28 @@ repository-root `.env` or `.env.local`, not an `apps/mobile/.env` file. See
22
23
## Development
24
25
-For simulator/emulator development, select and boot a device, then ensure its native client matches
26
-this checkout before starting Metro:
25
+For simulator, emulator, or Android phone development, boot or connect a device, then install the
26
+native client that matches this checkout before starting Metro:
27
28
```bash
29
-node ../../scripts/mobile-native-client.ts ensure ios <simulator-udid>
30
-# Or: node ../../scripts/mobile-native-client.ts ensure android <emulator-serial>
29
+vp run mobile:install ios [simulator-udid]
30
+# Or: vp run mobile:install android [emulator-or-phone-serial]
31
vp run dev:client
32
```
33
34
-The helper compares a local Expo fingerprint and the installed binary with its last successful
35
-build record. It builds and installs missing, stale, or unverified clients and reuses matching ones.
36
-Use `check` instead of `ensure` for a read-only decision: exit 0 means compatible, 2 means a build is
37
-needed, and 1 means an operational error. Run it on the simulator host; no EAS login is required.
38
-An externally installed client is unverified until the helper builds it once.
34
+The device argument can be omitted when exactly one device is booted or connected. The helper
35
+compares this checkout's Expo fingerprint and the installed binary with its last install record,
36
+and reuses a matching client. Otherwise it installs the build for that fingerprint from
37
+`~/.cache/t3code/native-clients/artifacts`, then the shared task cache, and builds only when
38
+neither has it. One iOS simulator build serves every iPhone and iPad simulator, and one APK per
39
+ABI (x86_64 or arm64-v8a) serves every emulator and phone of that ABI. Build output goes to the
40
+log file it prints (`T3_NATIVE_CLIENT_VERBOSE=1` streams it instead). Builds are shared back when
41
+your task-cache login (`bash infra/task-cache/install.sh`) can write. `T3_NATIVE_CLIENT_CACHE=read`
42
+skips the upload and `off` skips the shared cache. Physical iPhones still use `vp run ios:dev`.
43
+
44
+For a read-only decision, run `node ../../scripts/mobile-native-client.ts check <ios|android>
45
+[device]`: exit 0 means compatible, 2 means an install is needed, and 1 means an operational error.
46
+An externally installed client is unverified until the helper installs it once.
47
48
Start Metro for an already verified dev client:
49
infra/task-cache/README.md
+4
@@ -51,6 +51,10 @@ vp run --filter @t3tools/task-cache typecheck
51
52
The smoke check uploads a unique fixture, restores its output into a cold second directory, changes its input, and confirms recomputation. It also checks that anonymous reads and uploads are rejected. Cacheable commands must run through `vp run`; direct `vp test` or `vp check` invocations do not use this cache.
53
54
+## Native clients
55
+
56
+`/projects/t3code/native-clients/<platform>/<variant>/<expo-fingerprint>` stores simulator and emulator development clients for `scripts/mobile-native-client.ts` in a separate `-native-clients` R2 bucket with 30-day retention. Unlike Vite Task entries, the Expo fingerprint is portable, so a client built on any Mac or Linux machine serves every other one. Readers download; writers upload through R2 multipart in 64 MB parts, because Workers cap request bodies. The uploader's bundle digest is stored as object metadata and checked by the client before it installs anything.
57
+
58
## Current limits
59
60
- Vite Task separates operating systems and CPU architectures. macOS results do not satisfy Linux CI; Linux developer machines can populate compatible CI results.
infra/task-cache/src/NativeClients.test.ts
new
+136
@@ -0,0 +1,136 @@
1
+import { expect, it } from "@effect/vitest";
2
+import * as Effect from "effect/Effect";
3
+import * as Layer from "effect/Layer";
4
+import * as NativeClients from "./NativeClients.ts";
5
+
6
+const tokenDigest = "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984";
7
+const auth = (name: string) => ({
8
+ authorization: `Basic ${btoa(`${name}:fixture-token-012345678901234567890123456789`)}`,
9
+});
10
+const key = `ios/development/${"a".repeat(40)}`;
11
+const base = `https://cache.example${NativeClients.routePrefix}${key}`;
12
+
13
+function memoryBucket() {
14
+ const objects = new Map<string, { bytes: ArrayBuffer; customMetadata: Record<string, string> }>();
15
+ const uploads = new Map<
16
+ string,
17
+ { key: string; customMetadata: Record<string, string>; parts: Map<number, ArrayBuffer> }
18
+ >();
19
+ const resume = (objectKey: string, uploadId: string) => ({
20
+ uploadPart: async (partNumber: number, body: ReadableStream) => {
21
+ uploads.get(uploadId)!.parts.set(partNumber, await new Response(body).arrayBuffer());
22
+ return { partNumber, etag: `etag-${partNumber}` };
23
+ },
24
+ complete: async (parts: ReadonlyArray<{ partNumber: number }>) => {
25
+ const upload = uploads.get(uploadId)!;
26
+ const chunks = parts.map(({ partNumber }) => upload.parts.get(partNumber)!);
27
+ objects.set(objectKey, {
28
+ bytes: await new Blob(chunks).arrayBuffer(),
29
+ customMetadata: upload.customMetadata,
30
+ });
31
+ uploads.delete(uploadId);
32
+ },
33
+ abort: async () => void uploads.delete(uploadId),
34
+ });
35
+ return {
36
+ head: async (objectKey: string) => (objects.has(objectKey) ? {} : null),
37
+ get: async (objectKey: string) => {
38
+ const object = objects.get(objectKey);
39
+ return object
40
+ ? {
41
+ size: object.bytes.byteLength,
42
+ body: new Blob([object.bytes]).stream(),
43
+ customMetadata: object.customMetadata,
44
+ }
45
+ : null;
46
+ },
47
+ createMultipartUpload: async (
48
+ objectKey: string,
49
+ options: { customMetadata: Record<string, string> },
50
+ ) => {
51
+ const uploadId = `upload/${uploads.size}+`;
52
+ uploads.set(uploadId, {
53
+ key: objectKey,
54
+ customMetadata: options.customMetadata,
55
+ parts: new Map(),
56
+ });
57
+ return { uploadId };
58
+ },
59
+ resumeMultipartUpload: resume,
60
+ uploads,
61
+ };
62
+}
63
+
64
+const request = (path: string, init: RequestInit & { name: string }) =>
65
+ Effect.gen(function* () {
66
+ const native = yield* NativeClients.NativeClients;
67
+ return yield* native.handle(
68
+ new Request(`${base}${path}`, {
69
+ ...init,
70
+ headers: { ...auth(init.name), ...init.headers },
71
+ }),
72
+ );
73
+ });
74
+
75
+it.effect("serves a writer's multipart upload to readers with its digest", () => {
76
+ const bucket = memoryBucket();
77
+ const bindings = {
78
+ CLIENTS: bucket,
79
+ developerKeys: JSON.stringify({ julius: tokenDigest }),
80
+ readerKeys: JSON.stringify({ "ci-reader": tokenDigest }),
81
+ readLimit: { limit: async () => ({ success: true }) },
82
+ storeLimit: { limit: async () => ({ success: true }) },
83
+ } as unknown as NativeClients.Bindings["Service"];
84
+ const digest = "d".repeat(64);
85
+ return Effect.gen(function* () {
86
+ const denied = yield* request("/uploads", {
87
+ name: "ci-reader",
88
+ method: "POST",
89
+ headers: { "x-t3-digest": digest },
90
+ });
91
+ expect(denied.status).toBe(403);
92
+
93
+ const created = yield* request("/uploads", {
94
+ name: "julius",
95
+ method: "POST",
96
+ headers: { "x-t3-digest": digest, "x-t3-build-ms": "540000" },
97
+ });
98
+ const { uploadId } = (yield* Effect.promise(() => created.json())) as { uploadId: string };
99
+ const parts = [];
100
+ for (const [index, chunk] of ["native ", "client"].entries()) {
101
+ const response = yield* request(`/uploads/${encodeURIComponent(uploadId)}/${index + 1}`, {
102
+ name: "julius",
103
+ method: "PUT",
104
+ body: chunk,
105
+ headers: { "content-length": String(chunk.length) },
106
+ });
107
+ const { etag } = (yield* Effect.promise(() => response.json())) as { etag: string };
108
+ parts.push({ partNumber: index + 1, etag });
109
+ }
110
+ const completed = yield* request(`/uploads/${encodeURIComponent(uploadId)}/complete`, {
111
+ name: "julius",
112
+ method: "POST",
113
+ body: JSON.stringify({ parts }),
114
+ headers: { "content-length": String(JSON.stringify({ parts }).length) },
115
+ });
116
+ expect(completed.status).toBe(204);
117
+
118
+ const download = yield* request("", { name: "ci-reader", method: "GET" });
119
+ expect(download.status).toBe(200);
120
+ expect(download.headers.get("x-t3-digest")).toBe(digest);
121
+ expect(download.headers.get("x-t3-build-ms")).toBe("540000");
122
+ expect(yield* Effect.promise(() => download.text())).toBe("native client");
123
+
124
+ const again = yield* request("/uploads", {
125
+ name: "julius",
126
+ method: "POST",
127
+ headers: { "x-t3-digest": digest },
128
+ });
129
+ expect(yield* Effect.promise(() => again.json())).toEqual({ exists: true });
130
+ expect(bucket.uploads.size).toBe(0);
131
+ }).pipe(
132
+ Effect.provide(
133
+ NativeClients.layer.pipe(Layer.provide(Layer.succeed(NativeClients.Bindings, bindings))),
134
+ ),
135
+ );
136
+});
infra/task-cache/src/NativeClients.ts
new
+190
@@ -0,0 +1,190 @@
1
+import type * as Cloudflare from "@cloudflare/workers-types";
2
+import * as Context from "effect/Context";
3
+import * as Effect from "effect/Effect";
4
+import * as Layer from "effect/Layer";
5
+import * as Schema from "effect/Schema";
6
+import { developerIdentity } from "./developerAuth.ts";
7
+
8
+export const routePrefix = "/projects/t3code/native-clients/";
9
+
10
+export class NativeClientsError extends Schema.TaggedError<NativeClientsError>()(
11
+ "NativeClientsError",
12
+ { status: Schema.Number, reason: Schema.String, cause: Schema.optional(Schema.Defect()) },
13
+) {}
14
+
15
+export class Bindings extends Context.Service<
16
+ Bindings,
17
+ {
18
+ readonly CLIENTS: Cloudflare.R2Bucket;
19
+ readonly developerKeys: string;
20
+ readonly readerKeys: string;
21
+ readonly readLimit: Cloudflare.RateLimit;
22
+ readonly storeLimit: Cloudflare.RateLimit;
23
+ }
24
+>()("@t3tools/task-cache/NativeClients/Bindings") {}
25
+
26
+export class NativeClients extends Context.Service<
27
+ NativeClients,
28
+ { readonly handle: (request: Request) => Effect.Effect<Response> }
29
+>()("@t3tools/task-cache/NativeClients") {}
30
+
31
+const CompleteBody = Schema.fromJsonString(
32
+ Schema.Struct({
33
+ parts: Schema.Array(Schema.Struct({ partNumber: Schema.Number, etag: Schema.String })).check(
34
+ Schema.isMinLength(1),
35
+ Schema.isMaxLength(64),
36
+ ),
37
+ }),
38
+);
39
+const decodeComplete = Schema.decodeUnknownEffect(CompleteBody);
40
+
41
+// <platform>/<variant>/<fingerprint>[/uploads[/<uploadId>[/<part>|/complete]]]
42
+const route =
43
+ /^((?:ios|android)\/[a-z0-9_-]{1,32}\/[a-f0-9]{40,64})(?:\/uploads(?:\/([^/]{1,1024})(?:\/(complete|[1-9][0-9]?))?)?)?$/;
44
+const digestPattern = /^[a-f0-9]{64}$/;
45
+
46
+const make = Effect.gen(function* () {
47
+ const bindings = yield* Bindings;
48
+
49
+ const r2 = <A>(run: () => Promise<A>) =>
50
+ Effect.tryPromise({
51
+ try: run,
52
+ catch: (cause) =>
53
+ new NativeClientsError({ status: 503, reason: "storage_unavailable", cause }),
54
+ });
55
+
56
+ const limit = Effect.fn("NativeClients.limit")(function* (writing: boolean) {
57
+ const allowed = yield* r2(() =>
58
+ (writing ? bindings.storeLimit : bindings.readLimit).limit({
59
+ key: writing ? "t3code:native-store" : "t3code:native-read",
60
+ }),
61
+ );
62
+ if (!allowed.success)
63
+ return yield* new NativeClientsError({ status: 429, reason: "rate_limit" });
64
+ });
65
+
66
+ const handle = Effect.fn("NativeClients.handle")(
67
+ function* (request: Request) {
68
+ const url = new URL(request.url);
69
+ const match = route.exec(url.pathname.slice(routePrefix.length));
70
+ if (!match || url.search || !url.pathname.startsWith(routePrefix))
71
+ return yield* new NativeClientsError({ status: 404, reason: "unknown_route" });
72
+ const [, key, uploadId, part] = match as unknown as [
73
+ string,
74
+ string,
75
+ string | undefined,
76
+ string | undefined,
77
+ ];
78
+ const operation =
79
+ uploadId === undefined
80
+ ? url.pathname.endsWith("/uploads")
81
+ ? "create"
82
+ : "download"
83
+ : part === undefined
84
+ ? "abort"
85
+ : part === "complete"
86
+ ? "complete"
87
+ : "part";
88
+ const method = {
89
+ download: "GET",
90
+ create: "POST",
91
+ part: "PUT",
92
+ complete: "POST",
93
+ abort: "DELETE",
94
+ }[operation];
95
+ yield* Effect.annotateCurrentSpan({ "native.operation": operation, "native.key": key });
96
+ if (request.method !== method)
97
+ return yield* new NativeClientsError({ status: 405, reason: "method_not_allowed" });
98
+ if (!request.headers.has("authorization"))
99
+ return yield* new NativeClientsError({ status: 401, reason: "missing_identity" });
100
+
101
+ const developer = yield* developerIdentity(request, bindings.developerKeys);
102
+ const writing = operation !== "download";
103
+ if (writing) {
104
+ if (typeof developer !== "string")
105
+ return yield* new NativeClientsError({ status: 403, reason: "write_denied" });
106
+ } else if (
107
+ typeof developer !== "string" &&
108
+ typeof (yield* developerIdentity(request, bindings.readerKeys)) !== "string"
109
+ ) {
110
+ return yield* new NativeClientsError({ status: 403, reason: "read_denied" });
111
+ }
112
+ yield* limit(writing);
113
+
114
+ if (operation === "download") {
115
+ const object = yield* r2(() => bindings.CLIENTS.get(key));
116
+ yield* Effect.annotateCurrentSpan({ "native.result": object ? "hit" : "miss" });
117
+ if (!object) return yield* new NativeClientsError({ status: 404, reason: "missing" });
118
+ return new Response(object.body as unknown as ReadableStream, {
119
+ headers: {
120
+ "Content-Type": "application/octet-stream",
121
+ "Content-Length": String(object.size),
122
+ "Cache-Control": "no-store",
123
+ "X-T3-Digest": object.customMetadata?.digest ?? "",
124
+ "X-T3-Build-Ms": object.customMetadata?.buildMs ?? "0",
125
+ },
126
+ });
127
+ }
128
+ if (operation === "create") {
129
+ const digest = request.headers.get("x-t3-digest") ?? "";
130
+ if (!digestPattern.test(digest))
131
+ return yield* new NativeClientsError({ status: 400, reason: "invalid_digest" });
132
+ if (yield* r2(() => bindings.CLIENTS.head(key))) {
133
+ yield* Effect.annotateCurrentSpan({ "native.result": "exists" });
134
+ return Response.json({ exists: true });
135
+ }
136
+ const buildMs = request.headers.get("x-t3-build-ms") ?? "";
137
+ const upload = yield* r2(() =>
138
+ bindings.CLIENTS.createMultipartUpload(key, {
139
+ customMetadata: {
140
+ digest,
141
+ writer: developer!,
142
+ buildMs: /^\d{1,10}$/.test(buildMs) ? buildMs : "0",
143
+ },
144
+ }),
145
+ );
146
+ return Response.json({ uploadId: upload.uploadId });
147
+ }
148
+
149
+ const decodedId = yield* Effect.try({
150
+ try: () => decodeURIComponent(uploadId!),
151
+ catch: () => new NativeClientsError({ status: 400, reason: "invalid_upload_id" }),
152
+ });
153
+ const upload = bindings.CLIENTS.resumeMultipartUpload(key, decodedId);
154
+ if (operation === "abort") {
155
+ yield* r2(() => upload.abort());
156
+ return new Response(null, { status: 204 });
157
+ }
158
+ if (operation === "part") {
159
+ if (!request.body || !request.headers.has("content-length"))
160
+ return yield* new NativeClientsError({ status: 411, reason: "length_required" });
161
+ const uploaded = yield* r2(() =>
162
+ upload.uploadPart(Number(part), request.body as unknown as Cloudflare.ReadableStream),
163
+ );
164
+ return Response.json({ etag: uploaded.etag });
165
+ }
166
+ // A missing length would let text() buffer an unbounded body.
167
+ const length = request.headers.get("content-length");
168
+ if (length === null || !(Number(length) <= 64 * 1024))
169
+ return yield* new NativeClientsError({ status: 413, reason: "parts_too_large" });
170
+ const body = yield* decodeComplete(yield* r2(() => request.text())).pipe(
171
+ Effect.mapError(() => new NativeClientsError({ status: 400, reason: "invalid_parts" })),
172
+ );
173
+ yield* r2(() => upload.complete([...body.parts]));
174
+ yield* Effect.annotateCurrentSpan({ "native.result": "stored" });
175
+ return new Response(null, { status: 204 });
176
+ },
177
+ Effect.catchTags({
178
+ NativeClientsError: (error) =>
179
+ Effect.gen(function* () {
180
+ if (error.status >= 500)
181
+ yield* Effect.logWarning("Native client request failed", { reason: error.reason });
182
+ return Response.json({ error: error.reason }, { status: error.status });
183
+ }),
184
+ }),
185
+ );
186
+
187
+ return NativeClients.of({ handle });
188
+});
189
+
190
+export const layer = Layer.effect(NativeClients, make);
infra/task-cache/src/resources.ts
+16
@@ -36,6 +36,22 @@ export const Artifacts = cacheName.pipe(
36
),
37
);
38
39
+// Fingerprint-keyed simulator/emulator dev clients; separate from the 9-day task-cache generations.
40
+export const NativeClients = cacheName.pipe(
41
+ Effect.flatMap((name) =>
42
+ Cloudflare.R2.Bucket("NativeClients", {
43
+ name: `${name}-native-clients`,
44
+ lifecycleRules: [
45
+ {
46
+ id: "native-client-retention",
47
+ deleteObjectsTransition: { condition: { type: "Age", maxAge: 30 * 86400 } },
48
+ abortMultipartUploadsTransition: { condition: { type: "Age", maxAge: 86400 } },
49
+ },
50
+ ],
51
+ }).pipe(retain()),
52
+ ),
53
+);
54
+
55
export const Savings = cacheName.pipe(
56
Effect.flatMap((name) =>
57
Cloudflare.D1.Database("Savings", {
infra/task-cache/src/worker.ts
+22
@@ -8,6 +8,7 @@ import * as Redacted from "effect/Redacted";
8
import * as HttpServerResponse from "effect/http/HttpServerResponse";
9
import * as Admission from "./Admission.ts";
10
import * as Cache from "./Cache.ts";
11
+import * as NativeClients from "./NativeClients.ts";
12
import * as Savings from "./Savings.ts";
13
import type { ProtocolBindings } from "./protocolTypes.ts";
14
import * as Resources from "./resources.ts";
@@ -26,6 +27,7 @@ export const layer = Worker.make(
27
const savings = yield* Cloudflare.D1.QueryDatabase(yield* Resources.Savings);
28
const database = yield* Cloudflare.D1.QueryDatabase(yield* Resources.Index);
29
const artifacts = yield* Cloudflare.R2.ReadWriteBucket(yield* Resources.Artifacts);
30
+ const nativeClients = yield* Cloudflare.R2.ReadWriteBucket(yield* Resources.NativeClients);
31
const developerKeys = yield* Config.Redacted("DEVELOPER_KEYS");
32
const readerKeys = yield* Config.Redacted("READER_KEYS");
33
const reads = yield* Cloudflare.RateLimit("READ_LIMITER", {
@@ -47,6 +49,22 @@ export const layer = Worker.make(
49
storeLimit: yield* writes.raw,
50
});
51
});
52
+ const nativeRuntime = NativeClients.layer.pipe(
53
+ Layer.provide(
54
+ Layer.effect(
55
+ NativeClients.Bindings,
56
+ Effect.gen(function* () {
57
+ return NativeClients.Bindings.of({
58
+ CLIENTS: yield* nativeClients.raw,
59
+ developerKeys: Redacted.value(developerKeys),
60
+ readerKeys: Redacted.value(readerKeys),
61
+ readLimit: yield* reads.raw,
62
+ storeLimit: yield* writes.raw,
63
+ });
64
+ }),
65
+ ),
66
+ ),
67
+ );
68
const admission = yield* Admission.Admission.pipe(Effect.provide(Admission.layer));
69
const savingsLayer = Savings.layer.pipe(
70
Layer.provide(Layer.effect(Savings.Database, savings.raw)),
@@ -75,6 +93,10 @@ export const layer = Worker.make(
93
return {
94
fetch: Effect.gen(function* () {
95
const request = yield* Cloudflare.Workers.Request;
96
+ if (new URL(request.url).pathname.startsWith(NativeClients.routePrefix)) {
97
+ const native = yield* NativeClients.NativeClients.pipe(Effect.provide(nativeRuntime));
98
+ return HttpServerResponse.fromWeb(yield* native.handle(request));
99
+ }
100
const context = yield* Cloudflare.Workers.WorkerExecutionContext;
101
const cache = yield* Cache.Cache;
102
return HttpServerResponse.fromWeb(yield* cache.handle(request, context.raw));
package.json
+1
@@ -32,6 +32,7 @@
32
"knip:check": "knip --include files,dependencies --no-config-hints && knip --workspace apps/server --workspace apps/desktop --workspace apps/web --workspace packages/client-runtime --workspace packages/contracts --workspace packages/effect-acp --workspace packages/effect-codex-app-server --workspace packages/shared --workspace packages/ssh --workspace packages/tailscale --workspace packages/provider-core --workspace packages/provider-acp --workspace packages/provider-acp-registry --workspace packages/provider-testing --workspace packages/provider-pi --workspace packages/provider-muse --workspace packages/provider-opencode --workspace packages/provider-cursor --workspace packages/provider-grok --workspace packages/source-control-core --workspace packages/source-control-definitions --workspace packages/source-control-azure-devops --workspace packages/source-control-bitbucket --workspace packages/source-control-forgejo --workspace packages/source-control-gitcafe --workspace packages/source-control-github --workspace packages/source-control-gitlab --workspace packages/source-control-testing --exports --preprocessor ./scripts/knip-schemas.ts --no-config-hints",
33
"knip:production": "knip --production --preprocessor ./scripts/knip-schemas.ts",
34
"lint:mobile": "node scripts/mobile-native-static-check.ts",
35
+ "mobile:install": "node scripts/mobile-native-client.ts ensure",
36
"test": "vp run -r test",
37
"test:cached": "node scripts/test-cached.ts",
38
"test:perf:v2-wire": "vp test run apps/server/src/orchestration-v2/WireProjection.test.ts apps/server/src/orchestration-v2/threadHistoryPaging.test.ts apps/server/src/orchestration-v2/ThreadStream.test.ts apps/server/src/orchestration-v2/ThreadTransportPerformance.test.ts apps/server/src/orchestration-v2/ShellStream.test.ts apps/server/src/orchestration-v2/CommandPolicy.test.ts apps/server/src/orchestration-v2/runtimeLayer.test.ts apps/server/src/ws.test.ts packages/contracts/src/orchestrationV2.test.ts packages/client-runtime/src/operations/commands.test.ts packages/client-runtime/src/operations/commands.performance.test.ts packages/client-runtime/src/state/boundedThreadSnapshotHttp.test.ts packages/client-runtime/src/state/threadHistoryController.test.ts packages/client-runtime/src/state/threadHistoryMerge.test.ts packages/client-runtime/src/state/threads-sync.test.ts packages/client-runtime/src/state/shellReducer.test.ts",
scripts/mobile-native-client.ts
+601
-119
@@ -2,14 +2,20 @@ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2
import * as NodeServices from "@effect/platform-node/NodeServices";
3
import * as HostProcess from "@t3tools/shared/HostProcess";
4
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
5
+import * as Clock from "effect/Clock";
6
import * as Console from "effect/Console";
7
+import * as Context from "effect/Context";
8
+import * as DateTime from "effect/DateTime";
9
import * as Crypto from "effect/Crypto";
10
import * as Effect from "effect/Effect";
11
import * as FileSystem from "effect/FileSystem";
12
+import * as Option from "effect/Option";
13
import * as Path from "effect/Path";
14
+import * as Random from "effect/Random";
15
import * as Schema from "effect/Schema";
16
import * as Stream from "effect/Stream";
17
import { Argument, Command } from "effect/cli";
18
+import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
19
import { ChildProcess, ChildProcessSpawner } from "effect/process";
20
21
export type NativePlatform = "ios" | "android";
@@ -23,7 +29,9 @@ const decodeSimulators = Schema.decodeUnknownEffect(
29
Schema.Struct({
30
devices: Schema.Record(
31
Schema.String,
26
- Schema.Array(Schema.Struct({ udid: Schema.String, state: Schema.String })),
32
+ Schema.Array(
33
+ Schema.Struct({ udid: Schema.String, name: Schema.String, state: Schema.String }),
34
+ ),
35
),
36
}),
37
),
@@ -45,12 +53,14 @@ export function clientStatus(
53
return record.fingerprint === fingerprint ? "compatible" : "stale";
54
}
55
56
+export type ArtifactSource = "local" | "remote" | "build";
57
+
58
/** Keep native sources stable during ensure, as with a normal build; endpoint checks reject detected edits. */
59
export const ensureClient = Effect.fn("ensureClient")(function* <E, R, E2, R2>(operations: {
60
fingerprint: Effect.Effect<string, E, R>;
61
installedBinary: Effect.Effect<string | null, E, R>;
62
readRecord: Effect.Effect<NativeClientRecord | null, E, R>;
53
- build: Effect.Effect<void, E, R>;
63
+ install: (fingerprint: string) => Effect.Effect<ArtifactSource, E, R>;
64
saveRecord: (record: NativeClientRecord) => Effect.Effect<void, E2, R2>;
65
}) {
66
const fingerprint = yield* operations.fingerprint;
@@ -59,27 +69,17 @@ export const ensureClient = Effect.fn("ensureClient")(function* <E, R, E2, R2>(o
69
yield* operations.installedBinary,
70
yield* operations.readRecord,
71
);
62
- const verifyInputs = Effect.gen(function* () {
63
- if ((yield* operations.fingerprint) !== fingerprint) {
64
- return yield* new NativeClientError({
65
- message:
66
- "Native inputs changed during verification. Run ensure again; this build was not recorded.",
67
- });
68
- }
69
- });
70
- yield* verifyInputs;
72
if (status === "compatible") {
72
- return { status, rebuilt: false, fingerprint };
73
+ return { status, installed: false, fingerprint };
74
}
74
- yield* operations.build;
75
+ const source = yield* operations.install(fingerprint);
76
const binary = yield* operations.installedBinary;
77
if (binary === null)
78
return yield* new NativeClientError({
78
- message: "Build finished but the development client is not installed.",
79
+ message: "Install finished but the development client is not on the device.",
80
});
80
- yield* verifyInputs;
81
yield* operations.saveRecord({ fingerprint, binary });
82
- return { status: "compatible" as const, rebuilt: true, fingerprint };
82
+ return { status: "compatible" as const, installed: true, source, fingerprint };
83
});
84
85
const digest = Effect.fn("nativeClient.digest")(function* (value: string | Uint8Array) {
@@ -164,6 +164,11 @@ export const resolveAdb = Effect.gen(function* () {
164
});
165
});
166
167
+/** Build tool output goes to this file instead of the caller's terminal; null streams it. */
168
+const BuildLog = Context.Reference<string | null>("nativeClient/BuildLog", {
169
+ defaultValue: () => null,
170
+});
171
+
172
const command = Effect.fn("nativeClient.command")(function* (
173
program: string,
174
args: string[],
@@ -172,6 +177,7 @@ const command = Effect.fn("nativeClient.command")(function* (
177
) {
178
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
179
const environment = yield* HostProcess.Environment;
180
+ const log = inherit ? yield* BuildLog : null;
181
const spawn = yield* resolveSpawnCommand(program === "adb" ? yield* resolveAdb : program, args);
182
const child = yield* spawner.spawn(
183
ChildProcess.make(spawn.command, spawn.args, {
@@ -184,12 +190,28 @@ const command = Effect.fn("nativeClient.command")(function* (
190
T3CODE_IOS_PERSONAL_TEAM: "0",
191
CI: "1",
192
EXPO_NO_GIT_STATUS: "1",
193
+ // macOS tar otherwise adds AppleDouble entries that change the extracted bundle.
194
+ COPYFILE_DISABLE: "1",
195
},
196
stdin: "ignore",
189
- stdout: inherit ? "inherit" : "pipe",
190
- stderr: inherit ? "inherit" : "pipe",
197
+ stdout: inherit && !log ? "inherit" : "pipe",
198
+ stderr: inherit && !log ? "inherit" : "pipe",
199
}),
200
);
201
+ if (log) {
202
+ const fs = yield* FileSystem.FileSystem;
203
+ const append = <E>(stream: Stream.Stream<Uint8Array, E>) =>
204
+ stream.pipe(Stream.run(fs.sink(log, { flag: "a" })));
205
+ const [, , code] = yield* Effect.all(
206
+ [append(child.stdout), append(child.stderr), child.exitCode],
207
+ { concurrency: "unbounded" },
208
+ );
209
+ if (code === 0) return "";
210
+ const tail = (yield* fs.readFileString(log)).trimEnd().split("\n").slice(-40).join("\n");
211
+ return yield* new NativeClientError({
212
+ message: `${program} ${args[0]} failed (${code}). Last lines:\n${tail}\nFull log: ${log}`,
213
+ });
214
+ }
215
const [stdout, stderr, code] = yield* Effect.all(
216
[collect(child.stdout), collect(child.stderr), child.exitCode],
217
{ concurrency: "unbounded" },
@@ -216,37 +238,101 @@ const fingerprint = Effect.fn("nativeClient.fingerprint")(function* (platform: N
238
return hash;
239
});
240
219
-const validateDevice = Effect.fn("nativeClient.validateDevice")(function* (
241
+const runtimeName = (runtime: string) =>
242
+ runtime
243
+ .replace(/^com\.apple\.CoreSimulator\.SimRuntime\./, "")
244
+ .replace(/-(\d+)-(\d+)$/, " $1.$2");
245
+
246
+/** Validate the named device, or pick the only booted one; otherwise list exact commands to choose from. */
247
+const resolveDevice = Effect.fn("nativeClient.resolveDevice")(function* (
248
platform: NativePlatform,
221
- device: string,
249
+ requested: string | undefined,
250
) {
251
+ const choose = (candidates: ReadonlyArray<{ id: string; label: string }>, problem: string) =>
252
+ new NativeClientError({
253
+ message: [
254
+ problem,
255
+ ...candidates.map(
256
+ ({ id, label }) => ` vp run mobile:install ${platform} ${id} # ${label}`,
257
+ ),
258
+ ].join("\n"),
259
+ });
260
if (platform === "ios") {
261
if ((yield* HostProcess.Platform) !== "darwin")
262
return yield* new NativeClientError({
226
- message: "Run iOS check/ensure on the Mac that hosts the simulator.",
263
+ message: "Run iOS installs on the Mac that hosts the simulator.",
264
+ });
265
+ // The shared iOS client is an arm64 simulator build; Intel simulators cannot run it.
266
+ if ((yield* HostProcess.Architecture) !== "arm64")
267
+ return yield* new NativeClientError({
268
+ message: "mobile:install needs an Apple silicon Mac. Use vp run ios:dev on Intel Macs.",
269
});
270
const listing = yield* decodeSimulators(
271
yield* command("xcrun", ["simctl", "list", "devices", "available", "--json"]),
272
);
231
- const simulator = Object.values(listing.devices)
232
- .flat()
233
- .find((entry) => entry.udid === device);
273
+ const simulators = Object.entries(listing.devices).flatMap(([runtime, devices]) =>
274
+ devices.map((device) => ({ ...device, label: `${device.name} (${runtimeName(runtime)})` })),
275
+ );
276
+ const booted = simulators
277
+ .filter((simulator) => simulator.state === "Booted")
278
+ .map(({ udid, label }) => ({ id: udid, label }));
279
+ if (requested === undefined) {
280
+ if (booted.length === 1) return booted[0]!;
281
+ return yield* booted.length === 0
282
+ ? new NativeClientError({
283
+ message:
284
+ "No iOS simulator is booted. Open one with device_open, or boot one: xcrun simctl boot <udid>",
285
+ })
286
+ : choose(booted, "Several iOS simulators are booted. Pick one:");
287
+ }
288
+ const simulator = simulators.find((entry) => entry.udid === requested);
289
if (!simulator)
235
- return yield* new NativeClientError({
236
- message: `No available iOS simulator with UDID ${device}.`,
237
- });
290
+ return yield* choose(booted, `No available iOS simulator with UDID ${requested}. Booted:`);
291
if (simulator.state !== "Booted")
292
return yield* new NativeClientError({
240
- message: `Boot the selected simulator first: xcrun simctl boot ${device}`,
241
- });
242
- } else {
243
- if ((yield* command("adb", ["-s", device, "get-state"])) !== "device")
244
- return yield* new NativeClientError({ message: "Android device is not connected." });
245
- if ((yield* command("adb", ["-s", device, "shell", "getprop", "ro.kernel.qemu"])) !== "1")
246
- return yield* new NativeClientError({
247
- message: "Select an Android emulator, not a physical device.",
293
+ message: `Boot the selected simulator first: xcrun simctl boot ${requested}`,
294
});
295
+ return { id: simulator.udid, label: simulator.label };
296
}
297
+ const attached = (yield* command("adb", ["devices", "-l"]))
298
+ .split("\n")
299
+ .slice(1)
300
+ .map((line) => line.trim().split(/\s+/))
301
+ .filter((parts) => parts.length >= 2)
302
+ .map(([id, state, ...details]) => ({
303
+ id: id!,
304
+ state: state!,
305
+ label: details.find((detail) => detail.startsWith("model:"))?.slice(6) ?? id!,
306
+ }));
307
+ const ready = attached.filter((device) => device.state === "device");
308
+ const device =
309
+ requested === undefined ? ready : attached.filter((entry) => entry.id === requested);
310
+ if (requested === undefined && ready.length !== 1)
311
+ return yield* ready.length === 0
312
+ ? new NativeClientError({
313
+ message:
314
+ "No Android emulator or phone is connected. Open one with device_open, or start an emulator.",
315
+ })
316
+ : choose(ready, "Several Android devices are connected. Pick one:");
317
+ const selected = device[0];
318
+ if (!selected)
319
+ return yield* choose(ready, `Android device ${requested} is not connected. Ready:`);
320
+ if (selected.state === "unauthorized")
321
+ return yield* new NativeClientError({
322
+ message: `Unlock ${selected.label} and accept the USB debugging prompt, then run this again.`,
323
+ });
324
+ if (selected.state !== "device")
325
+ return yield* new NativeClientError({
326
+ message: `Android device ${selected.id} is ${selected.state}, not ready.`,
327
+ });
328
+ // Emulators report a generic model; their AVD name is what users recognize.
329
+ const avd = selected.id.startsWith("emulator-")
330
+ ? yield* command("adb", ["-s", selected.id, "emu", "avd", "name"]).pipe(
331
+ Effect.map((output) => output.split("\n")[0]?.trim()),
332
+ Effect.orElseSucceed(() => undefined),
333
+ )
334
+ : undefined;
335
+ return { id: selected.id, label: avd || selected.label };
336
});
337
338
export const installedBinary = Effect.fn("installedBinary")(function* (
@@ -315,15 +401,436 @@ export const prebuildAndroid = Effect.fn("nativeClient.prebuildAndroid")(functio
401
);
402
});
403
404
+const artifactNames = { ios: "T3CodeDev.app", android: "app-debug.apk" } as const;
405
+// R2 requires equal-sized parts and Workers cap request bodies at 100 MB.
406
+const partSize = 64 * 1024 * 1024;
407
+const RemoteCredentials = Schema.fromJsonString(
408
+ Schema.Record(Schema.String, Schema.Struct({ name: Schema.String, token: Schema.String })),
409
+);
410
+const decodeCredentials = Schema.decodeUnknownEffect(RemoteCredentials);
411
+const decodeEndpoint = Schema.decodeUnknownEffect(
412
+ Schema.fromJsonString(Schema.Struct({ endpoint: Schema.String })),
413
+);
414
+const decodeUpload = Schema.decodeUnknownEffect(
415
+ Schema.Union([
416
+ Schema.Struct({ exists: Schema.Literal(true) }),
417
+ Schema.Struct({ uploadId: Schema.String }),
418
+ ]),
419
+);
420
+const decodePart = Schema.decodeUnknownEffect(Schema.Struct({ etag: Schema.String }));
421
+
422
+interface RemoteCache {
423
+ readonly url: string;
424
+ readonly telemetry: string;
425
+ readonly name: string;
426
+ readonly token: string;
427
+}
428
+
429
+/** The shared task-cache login (infra/task-cache/install.sh) also authorizes native clients. */
430
+const remoteCache = Effect.fn("nativeClient.remoteCache")(
431
+ function* (home: string) {
432
+ const environment = yield* HostProcess.Environment;
433
+ if (environment.T3_NATIVE_CLIENT_CACHE === "off") return null;
434
+ const fs = yield* FileSystem.FileSystem;
435
+ const path = yield* Path.Path;
436
+ const { endpoint } = yield* decodeEndpoint(
437
+ yield* fs.readFileString(path.join((yield* roots).repo, ".task-cache.json")),
438
+ );
439
+ const credentials = yield* decodeCredentials(
440
+ yield* fs.readFileString(path.join(home, ".config/t3-task-cache/credentials.json")),
441
+ );
442
+ const login = credentials[endpoint];
443
+ if (!login) return null;
444
+ const base = endpoint.replace(/\/$/, "");
445
+ return {
446
+ url: `${base}/native-clients`,
447
+ telemetry: `${base}/telemetry`,
448
+ ...login,
449
+ } satisfies RemoteCache;
450
+ },
451
+ Effect.catch(() => Effect.succeed(null)),
452
+);
453
+
454
+const authorized = (remote: RemoteCache, request: HttpClientRequest.HttpClientRequest) =>
455
+ request.pipe(HttpClientRequest.basicAuth(remote.name, remote.token));
456
+
457
+// Build duration lives beside the artifact so hits can report the time they saved.
458
+const sidecar = (directory: string) => `${directory}.json`;
459
+const decodeSidecar = Schema.decodeUnknownEffect(
460
+ Schema.fromJsonString(Schema.Struct({ buildMs: Schema.Number })),
461
+);
462
+const readBuildMs = (directory: string) =>
463
+ FileSystem.FileSystem.pipe(
464
+ Effect.flatMap((fs) => fs.readFileString(sidecar(directory))),
465
+ Effect.flatMap(decodeSidecar),
466
+ Effect.map(({ buildMs }) => buildMs),
467
+ Effect.orElseSucceed(() => 0),
468
+ );
469
+const writeBuildMs = (directory: string, buildMs: number) =>
470
+ FileSystem.FileSystem.pipe(
471
+ Effect.flatMap((fs) => fs.writeFileString(sidecar(directory), JSON.stringify({ buildMs }))),
472
+ Effect.ignore,
473
+ );
474
+
475
+/** Download into a sibling staging directory and publish only after the digest matches. */
476
+const download = Effect.fn("nativeClient.download")(function* (
477
+ remote: RemoteCache,
478
+ platform: NativePlatform,
479
+ key: string,
480
+ target: string,
481
+) {
482
+ const fs = yield* FileSystem.FileSystem;
483
+ const path = yield* Path.Path;
484
+ const client = yield* HttpClient.HttpClient;
485
+ const response = yield* client.execute(
486
+ authorized(remote, HttpClientRequest.get(`${remote.url}/${key}`)),
487
+ );
488
+ if (response.status === 404) return false;
489
+ yield* HttpClientResponse.filterStatusOk(response);
490
+ const expected = response.headers["x-t3-digest"] ?? "";
491
+ if (!/^[a-f0-9]{64}$/.test(expected))
492
+ return yield* new NativeClientError({ message: "Cached native client has no digest." });
493
+ yield* Console.error(`Downloading native client ${key} from the shared cache...`);
494
+ const staging = yield* fs.makeTempDirectory({
495
+ directory: path.dirname(target),
496
+ prefix: ".download-",
497
+ });
498
+ return yield* Effect.gen(function* () {
499
+ if (platform === "ios") {
500
+ const archive = `${staging}.tgz`;
501
+ yield* response.stream.pipe(
502
+ Stream.run(fs.sink(archive)),
503
+ Effect.andThen(command("tar", ["-xzf", archive, "-C", staging])),
504
+ Effect.ensuring(fs.remove(archive, { force: true }).pipe(Effect.ignore)),
505
+ );
506
+ } else {
507
+ yield* response.stream.pipe(Stream.run(fs.sink(path.join(staging, artifactNames.android))));
508
+ }
509
+ if ((yield* hashBundle(staging)) !== expected)
510
+ return yield* new NativeClientError({ message: "Cached native client failed verification." });
511
+ yield* publish(staging, target);
512
+ const buildMs = Number(response.headers["x-t3-build-ms"] ?? 0);
513
+ yield* writeBuildMs(target, Number.isSafeInteger(buildMs) ? buildMs : 0);
514
+ return true;
515
+ }).pipe(
516
+ Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)),
517
+ );
518
+});
519
+
520
+/** Upload in fixed-size parts; any failure aborts the upload and leaves the cache unchanged. */
521
+const upload = Effect.fn("nativeClient.upload")(function* (
522
+ remote: RemoteCache,
523
+ platform: NativePlatform,
524
+ key: string,
525
+ artifact: string,
526
+) {
527
+ const fs = yield* FileSystem.FileSystem;
528
+ const path = yield* Path.Path;
529
+ const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
530
+ const created = yield* client
531
+ .execute(
532
+ authorized(remote, HttpClientRequest.post(`${remote.url}/${key}/uploads`)).pipe(
533
+ HttpClientRequest.setHeader("x-t3-digest", yield* hashBundle(artifact)),
534
+ HttpClientRequest.setHeader("x-t3-build-ms", String(yield* readBuildMs(artifact))),
535
+ ),
536
+ )
537
+ .pipe(
538
+ Effect.flatMap((response) => response.json),
539
+ Effect.flatMap(decodeUpload),
540
+ );
541
+ if ("exists" in created) return;
542
+ const uploadUrl = `${remote.url}/${key}/uploads/${encodeURIComponent(created.uploadId)}`;
543
+ yield* Console.error(`Uploading native client ${key} to the shared cache...`);
544
+ yield* Effect.gen(function* () {
545
+ const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-upload-" });
546
+ const file =
547
+ platform === "ios"
548
+ ? path.join(directory, "client.tgz")
549
+ : path.join(artifact, artifactNames.android);
550
+ if (platform === "ios")
551
+ yield* command("tar", ["-czf", file, "-C", artifact, artifactNames.ios]);
552
+ const handle = yield* fs.open(file);
553
+ const parts: Array<{ partNumber: number; etag: string }> = [];
554
+ for (let partNumber = 1; ; partNumber++) {
555
+ const buffer = new Uint8Array(partSize);
556
+ let filled = 0;
557
+ while (filled < partSize) {
558
+ const read = Number(yield* handle.read(buffer.subarray(filled)));
559
+ if (read === 0) break;
560
+ filled += read;
561
+ }
562
+ if (filled === 0) break;
563
+ const { etag } = yield* client
564
+ .execute(
565
+ authorized(remote, HttpClientRequest.put(`${uploadUrl}/${partNumber}`)).pipe(
566
+ HttpClientRequest.bodyUint8Array(buffer.subarray(0, filled)),
567
+ ),
568
+ )
569
+ .pipe(
570
+ Effect.flatMap((response) => response.json),
571
+ Effect.flatMap(decodePart),
572
+ );
573
+ parts.push({ partNumber, etag });
574
+ if (filled < partSize) break;
575
+ }
576
+ yield* client.execute(
577
+ authorized(remote, HttpClientRequest.post(`${uploadUrl}/complete`)).pipe(
578
+ HttpClientRequest.bodyJsonUnsafe({ parts }),
579
+ ),
580
+ );
581
+ }).pipe(
582
+ Effect.scoped,
583
+ Effect.onError(() =>
584
+ client.execute(authorized(remote, HttpClientRequest.delete(uploadUrl))).pipe(Effect.ignore),
585
+ ),
586
+ );
587
+});
588
+
589
+const publish = Effect.fn("nativeClient.publish")(function* (staging: string, target: string) {
590
+ const fs = yield* FileSystem.FileSystem;
591
+ // A concurrent ensure may have published the same fingerprint first; either copy is valid.
592
+ if (yield* fs.exists(target)) return;
593
+ yield* fs.rename(staging, target);
594
+});
595
+
596
+const build = Effect.fn("nativeClient.build")(function* (
597
+ platform: NativePlatform,
598
+ abi: string | null,
599
+ target: string,
600
+ expected: string,
601
+) {
602
+ const fs = yield* FileSystem.FileSystem;
603
+ const path = yield* Path.Path;
604
+ const { repo, mobile } = yield* roots;
605
+ const tracked = yield* command("git", ["ls-files", `apps/mobile/${platform}`], false, repo);
606
+ if (tracked)
607
+ return yield* new NativeClientError({
608
+ message: "Native directory contains tracked files; clean prebuild would overwrite them.",
609
+ });
610
+ const prebuild = command(
611
+ "vp",
612
+ ["exec", "expo", "prebuild", "--clean", "--platform", platform, "--no-install"],
613
+ true,
614
+ ).pipe(Effect.asVoid);
615
+ yield* platform === "android" ? prebuildAndroid(mobile, prebuild) : prebuild;
616
+ const staging = yield* fs.makeTempDirectory({
617
+ directory: path.dirname(target),
618
+ prefix: ".build-",
619
+ });
620
+ yield* Effect.gen(function* () {
621
+ if (platform === "ios") {
622
+ const output = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-client-" });
623
+ yield* command("pod", ["install"], true, path.join(mobile, "ios"));
624
+ // One device-independent arm64 simulator build installs on every iPhone and iPad simulator.
625
+ yield* command(
626
+ "xcrun",
627
+ [
628
+ "xcodebuild",
629
+ "-workspace",
630
+ path.join(mobile, "ios/T3CodeDev.xcworkspace"),
631
+ "-scheme",
632
+ "T3CodeDev",
633
+ "-configuration",
634
+ "Debug",
635
+ "-destination",
636
+ "generic/platform=iOS Simulator",
637
+ "-derivedDataPath",
638
+ output,
639
+ "ARCHS=arm64",
640
+ "ONLY_ACTIVE_ARCH=NO",
641
+ "build",
642
+ ],
643
+ true,
644
+ );
645
+ yield* command("ditto", [
646
+ path.join(output, "Build/Products/Debug-iphonesimulator", artifactNames.ios),
647
+ path.join(staging, artifactNames.ios),
648
+ ]);
649
+ } else {
650
+ const android = path.join(mobile, "android");
651
+ yield* command(
652
+ path.join(android, (yield* HostProcess.Platform) === "win32" ? "gradlew.bat" : "gradlew"),
653
+ ["app:assembleDebug", `-PreactNativeArchitectures=${abi}`],
654
+ true,
655
+ android,
656
+ );
657
+ yield* fs.copyFile(
658
+ path.join(android, "app/build/outputs/apk/debug", artifactNames.android),
659
+ path.join(staging, artifactNames.android),
660
+ );
661
+ }
662
+ // Native edits during the build would publish this binary under a fingerprint it doesn't match.
663
+ if ((yield* fingerprint(platform)) !== expected)
664
+ return yield* new NativeClientError({
665
+ message: "Native inputs changed during the build, so it was discarded. Run this again.",
666
+ });
667
+ yield* publish(staging, target);
668
+ }).pipe(
669
+ Effect.scoped,
670
+ Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)),
671
+ );
672
+});
673
+
674
+/** Keep the newest few fingerprints per platform; each artifact is a few hundred megabytes. */
675
+const prune = Effect.fn("nativeClient.prune")(
676
+ function* (parent: string, keep: string) {
677
+ const fs = yield* FileSystem.FileSystem;
678
+ const path = yield* Path.Path;
679
+ const entries = yield* Effect.forEach(
680
+ (yield* fs.readDirectory(parent)).filter(
681
+ (name) => !name.startsWith(".") && !name.endsWith(".json"),
682
+ ),
683
+ (name) =>
684
+ fs.stat(path.join(parent, name)).pipe(
685
+ Effect.map((info) => ({
686
+ name,
687
+ modified: Option.getOrUndefined(info.mtime)?.getTime() ?? 0,
688
+ })),
689
+ ),
690
+ );
691
+ const stale = entries
692
+ .filter(({ name }) => name !== keep)
693
+ .sort((a, b) => b.modified - a.modified)
694
+ .slice(2);
695
+ yield* Effect.forEach(
696
+ stale,
697
+ ({ name }) =>
698
+ fs
699
+ .remove(path.join(parent, name), { recursive: true, force: true })
700
+ .pipe(Effect.andThen(fs.remove(sidecar(path.join(parent, name)), { force: true }))),
701
+ { discard: true },
702
+ );
703
+ },
704
+ (effect) => Effect.ignore(effect),
705
+);
706
+
707
+/** Local artifact, then the shared cache, then a build that is shared back when credentials allow. */
708
+export const resolveArtifact = Effect.fn("nativeClient.resolveArtifact")(function* (
709
+ home: string,
710
+ platform: NativePlatform,
711
+ abi: string | null,
712
+ fingerprint: string,
713
+) {
714
+ const fs = yield* FileSystem.FileSystem;
715
+ const path = yield* Path.Path;
716
+ const key = `${platform}/${abi ? `development-${abi}` : "development"}/${fingerprint}`;
717
+ const directory = path.join(home, ".cache/t3code/native-clients/artifacts", key);
718
+ const artifact = { directory, file: path.join(directory, artifactNames[platform]) };
719
+ if (yield* fs.exists(artifact.file)) {
720
+ yield* Console.error(`Using cached client ${key}.`);
721
+ // Pruning keeps the most recently used, so a client another checkout is installing stays.
722
+ const now = yield* Clock.currentTimeMillis;
723
+ yield* fs.utimes(directory, now / 1000, now / 1000).pipe(Effect.ignore);
724
+ return { ...artifact, source: "local" as const, buildMs: yield* readBuildMs(directory) };
725
+ }
726
+ yield* fs.makeDirectory(path.dirname(directory), { recursive: true });
727
+ const remote = yield* remoteCache(home);
728
+ if (remote) {
729
+ const downloaded = yield* download(remote, platform, key, directory).pipe(
730
+ Effect.catch((error) =>
731
+ Console.error(`Shared native client cache unavailable (${describe(error)}).`).pipe(
732
+ Effect.as(false),
733
+ ),
734
+ ),
735
+ );
736
+ if (downloaded) {
737
+ yield* prune(path.dirname(directory), fingerprint);
738
+ return { ...artifact, source: "remote" as const, buildMs: yield* readBuildMs(directory) };
739
+ }
740
+ }
741
+ const log = yield* BuildLog;
742
+ yield* Console.error(
743
+ `No cached client for ${key}. Building it once for every machine (5-15 min)${log ? `. Log: ${log}` : ""}`,
744
+ );
745
+ const started = yield* Clock.currentTimeMillis;
746
+ yield* build(platform, abi, directory, fingerprint);
747
+ yield* writeBuildMs(directory, (yield* Clock.currentTimeMillis) - started);
748
+ yield* prune(path.dirname(directory), fingerprint);
749
+ if (remote && (yield* HostProcess.Environment).T3_NATIVE_CLIENT_CACHE !== "read") {
750
+ yield* upload(remote, platform, key, directory).pipe(
751
+ Effect.catch((error) => Console.error(`Native client was not shared (${describe(error)}).`)),
752
+ );
753
+ }
754
+ return { ...artifact, source: "build" as const, buildMs: 0 };
755
+});
756
+
757
+const describe = (error: unknown) =>
758
+ typeof error === "object" &&
759
+ error !== null &&
760
+ "message" in error &&
761
+ typeof error.message === "string"
762
+ ? error.message
763
+ : String(error);
764
+
765
+const install = Effect.fn("nativeClient.install")(function* (
766
+ platform: NativePlatform,
767
+ device: string,
768
+ file: string,
769
+) {
770
+ if (platform === "ios") {
771
+ yield* command("xcrun", ["simctl", "install", device, file], true);
772
+ return;
773
+ }
774
+ yield* command("adb", ["-s", device, "install", "-r", "-d", file]).pipe(
775
+ Effect.catchTags({
776
+ NativeClientError: (error) =>
777
+ Effect.fail(
778
+ error.message.includes("INSTALL_FAILED_UPDATE_INCOMPATIBLE")
779
+ ? new NativeClientError({
780
+ message: `The installed client has a different signature. Remove it with adb -s ${device} uninstall ${bundleId} (this clears its pairings), then run ensure again.`,
781
+ })
782
+ : error,
783
+ ),
784
+ }),
785
+ );
786
+ // Emulators reach Metro through 10.0.2.2; a phone on USB needs the port forwarded.
787
+ if ((yield* command("adb", ["-s", device, "shell", "getprop", "ro.kernel.qemu"])) !== "1")
788
+ yield* command("adb", ["-s", device, "reverse", "tcp:8081", "tcp:8081"]).pipe(Effect.ignore);
789
+});
790
+
791
+const reportTelemetry = Effect.fn("nativeClient.reportTelemetry")(
792
+ function* (
793
+ home: string,
794
+ task: string,
795
+ source: "local" | "remote" | "executed" | "failed",
796
+ savedMs: number,
797
+ ) {
798
+ const remote = yield* remoteCache(home);
799
+ if (!remote) return;
800
+ const environment = yield* HostProcess.Environment;
801
+ const recordedAt = yield* Clock.currentTimeMillis;
802
+ const host = yield* HostProcess.Hostname;
803
+ const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
804
+ yield* client.execute(
805
+ authorized(remote, HttpClientRequest.post(remote.telemetry)).pipe(
806
+ HttpClientRequest.bodyJsonUnsafe({
807
+ id: yield* digest(`${host}:${task}:${recordedAt}:${yield* Random.nextInt}`),
808
+ recordedAt,
809
+ host,
810
+ platform: yield* HostProcess.Platform,
811
+ arch: yield* HostProcess.Architecture,
812
+ ci: environment.CI === "true" || environment.CI === "1",
813
+ runId: environment.GITHUB_RUN_ID ?? "",
814
+ tasks: [{ package: "@t3tools/mobile", task, source, savedMs: Math.round(savedMs) }],
815
+ }),
816
+ ),
817
+ );
818
+ },
819
+ (effect) => effect.pipe(Effect.timeout("5 seconds"), Effect.ignore),
820
+);
821
+
822
const main = Command.make(
823
"mobile-native-client",
824
{
825
mode: Argument.Literals("mode", ["check", "ensure"]),
826
platform: Argument.Literals("platform", ["ios", "android"]),
323
- device: Argument.String("device"),
827
+ device: Argument.String("device").pipe(Argument.optional),
828
},
325
- Effect.fn("nativeClient.main")(function* ({ mode, platform, device }) {
326
- yield* validateDevice(platform, device);
829
+ Effect.fn("nativeClient.main")(function* ({ mode, platform, device: requested }) {
830
+ const started = yield* Clock.currentTimeMillis;
831
+ const target = yield* resolveDevice(platform, Option.getOrUndefined(requested));
832
+ const device = target.id;
833
+ yield* Console.error(`Device: ${target.label} (${device})`);
834
const fs = yield* FileSystem.FileSystem;
835
const path = yield* Path.Path;
836
const environment = yield* HostProcess.Environment;
@@ -332,12 +839,18 @@ const main = Command.make(
839
return yield* new NativeClientError({
840
message: "HOME or USERPROFILE must be set to store native client records.",
841
});
335
- const recordPath = path.join(
336
- home,
337
- ".cache/t3code/native-clients",
338
- platform,
339
- `${yield* digest(device)}.json`,
340
- );
842
+ const cache = path.join(home, ".cache/t3code/native-clients");
843
+ const recordPath = path.join(cache, platform, `${yield* digest(device)}.json`);
844
+ // An iOS simulator build is arm64 for every device; an APK carries only the device's own ABI.
845
+ const abi = Effect.gen(function* () {
846
+ if (platform === "ios") return null;
847
+ const value = yield* command("adb", ["-s", device, "shell", "getprop", "ro.product.cpu.abi"]);
848
+ if (value !== "x86_64" && value !== "arm64-v8a")
849
+ return yield* new NativeClientError({ message: `Unsupported Android ABI ${value}.` });
850
+ return value;
851
+ });
852
+ let task = `native-client:${platform}`;
853
+ let savedMs = 0;
854
const operations = {
855
fingerprint: fingerprint(platform),
856
installedBinary: installedBinary(platform, device),
@@ -349,84 +862,21 @@ const main = Command.make(
862
() => Effect.succeed(null),
863
),
864
),
352
- build: Effect.gen(function* () {
353
- yield* Console.error(
354
- "Native client is missing, stale, or unverified. Building and installing a development client...",
355
- );
356
- const tracked = yield* command(
357
- "git",
358
- ["ls-files", `apps/mobile/${platform}`],
359
- false,
360
- (yield* roots).repo,
361
- );
362
- if (tracked)
363
- return yield* new NativeClientError({
364
- message:
365
- "Native directory contains tracked files; clean prebuild would overwrite them.",
366
- });
367
- const prebuild = command(
368
- "vp",
369
- ["exec", "expo", "prebuild", "--clean", "--platform", platform, "--no-install"],
370
- true,
371
- ).pipe(Effect.asVoid);
372
- yield* platform === "android" ? prebuildAndroid((yield* roots).mobile, prebuild) : prebuild;
373
- if (platform === "ios") {
374
- const output = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-client-" });
375
- const { mobile } = yield* roots;
376
- yield* command("pod", ["install"], true, path.join(mobile, "ios"));
377
- yield* command(
378
- "xcrun",
379
- [
380
- "xcodebuild",
381
- "-workspace",
382
- path.join(mobile, "ios/T3CodeDev.xcworkspace"),
383
- "-scheme",
384
- "T3CodeDev",
385
- "-configuration",
386
- "Debug",
387
- "-destination",
388
- `id=${device}`,
389
- "-derivedDataPath",
390
- output,
391
- "build",
392
- ],
393
- true,
394
- );
395
- yield* command(
396
- "xcrun",
397
- [
398
- "simctl",
399
- "install",
400
- device,
401
- path.join(output, "Build/Products/Debug-iphonesimulator/T3CodeDev.app"),
402
- ],
403
- true,
404
- );
405
- } else {
406
- yield* command(
407
- "vp",
408
- [
409
- "exec",
410
- "expo",
411
- "run:android",
412
- "--device",
413
- device,
414
- "--no-bundler",
415
- "--variant",
416
- "debug",
417
- ],
418
- true,
419
- );
420
- }
421
- }).pipe(Effect.scoped),
865
+ install: Effect.fn(function* (fingerprint: string) {
866
+ const deviceAbi = yield* abi;
867
+ if (deviceAbi) task = `native-client:android-${deviceAbi}`;
868
+ const artifact = yield* resolveArtifact(home, platform, deviceAbi, fingerprint);
869
+ savedMs = artifact.buildMs;
870
+ yield* Console.error(`Installing on ${target.label}...`);
871
+ yield* install(platform, device, artifact.file);
872
+ return artifact.source;
873
+ }),
874
saveRecord: Effect.fn(function* (record: NativeClientRecord) {
875
yield* fs.makeDirectory(path.dirname(recordPath), { recursive: true });
876
yield* fs.writeFileString(recordPath, yield* encodeRecord(record));
877
}),
878
};
427
- if (mode === "ensure") {
428
- yield* Console.log(yield* encodeOutput(yield* ensureClient(operations)));
429
- } else {
879
+ if (mode === "check") {
880
const current = yield* operations.fingerprint;
881
const status = clientStatus(
882
current,
@@ -437,21 +887,53 @@ const main = Command.make(
887
yield* encodeOutput({
888
status,
889
fingerprint: current,
890
+ device,
891
next:
892
status === "compatible"
893
? "Start Metro with vp run dev:client"
443
- : `node scripts/mobile-native-client.ts ensure ${platform} ${device}`,
894
+ : `vp run mobile:install ${platform} ${device}`,
895
}),
896
);
897
process.exitCode = status === "compatible" ? 0 : 2;
898
+ return;
899
}
900
+ const logs = path.join(cache, "logs");
901
+ yield* fs.makeDirectory(logs, { recursive: true });
902
+ yield* prune(logs, "");
903
+ const stamp = DateTime.formatIso(DateTime.makeUnsafe(started)).replaceAll(":", "-");
904
+ const log = path.join(logs, `${stamp}-${platform}.log`);
905
+ const result = yield* ensureClient(operations).pipe(
906
+ Effect.provideService(BuildLog, environment.T3_NATIVE_CLIENT_VERBOSE === "1" ? null : log),
907
+ Effect.tapError(() => reportTelemetry(home, task, "failed", 0)),
908
+ );
909
+ if (result.installed && result.source)
910
+ yield* reportTelemetry(
911
+ home,
912
+ task,
913
+ result.source === "build" ? "executed" : result.source,
914
+ result.source === "build" ? 0 : savedMs,
915
+ );
916
+ const seconds = Math.round(((yield* Clock.currentTimeMillis) - started) / 1000);
917
+ yield* Console.error(
918
+ result.installed
919
+ ? `Installed on ${target.label} in ${seconds}s (${result.source}). Next: start Metro with vp run dev:client.`
920
+ : `${target.label} already has the client for this checkout. Next: start Metro with vp run dev:client.`,
921
+ );
922
+ yield* Console.log(yield* encodeOutput({ ...result, device, seconds }));
923
}),
924
);
925
926
if (import.meta.main) {
927
Command.run(main, { version: "0.0.0" }).pipe(
928
+ // Expected failures carry their next step; a stack trace only buries it.
929
+ Effect.catchTags({
930
+ NativeClientError: (error) =>
931
+ Console.error(error.message).pipe(
932
+ Effect.andThen(Effect.sync(() => (process.exitCode = 1))),
933
+ ),
934
+ }),
935
Effect.scoped,
454
- Effect.provide(NodeServices.layer),
936
+ Effect.provide([NodeServices.layer, FetchHttpClient.layer]),
937
NodeRuntime.runMain,
938
);
939
}