@devries-cameron20 / t3code-1 / commits / cc927400c7
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Simlock renewals no longer exceed the gateway's lease TTL cap (#35988)

Julius Marminge committed Nov 12, 2026 at 21:25 UTC cc927400c7fdebb0ae4ca23cb297e5bd2b1843c8
2 files changed +36 -4
apps/server/src/device/SimlockDeviceHost.test.ts
+34 -1
@@ -4,6 +4,7 @@ import { SecretProviderId, type SimlockDeviceHostConfig } from "@t3tools/contrac
4 import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
5 import * as HostProcess from "@t3tools/shared/HostProcess";
6 import * as Effect from "effect/Effect";
7 +import * as Clock from "effect/Clock";
8 import * as Fiber from "effect/Fiber";
9 import * as Deferred from "effect/Deferred";
10 import * as Exit from "effect/Exit";
@@ -56,6 +57,8 @@ const fakePool = (
57 readonly entered: Deferred.Deferred<void>;
58 readonly proceed: Deferred.Deferred<void>;
59 };
60 + /** The gateway's `lease.maxTtlMs`: leases expire one TTL after their last renewal. */
61 + readonly maxTtlMs?: number;
62 } = {},
63 ) =>
64 Effect.gen(function* () {
@@ -66,8 +69,10 @@ const fakePool = (
69 let leases = 0;
70 let renewStatus = 200;
71 let catalog: unknown = CATALOG;
72 + let leaseDeadline = Infinity;
73 const http = HttpClient.make((request) =>
70 - Effect.sync(() => {
74 + Effect.gen(function* () {
75 + const now = yield* Clock.currentTimeMillis;
76 const url = new URL(request.url);
77 requests.push(`${request.method} ${url.pathname}`);
78 const json = (body: unknown, status = 200) =>
@@ -75,6 +80,17 @@ const fakePool = (
80 if (request.headers.authorization !== "Bearer agent-token")
81 return json({ error: { code: "UNAUTHENTICATED", message: "Unknown bearer token" } }, 401);
82 if (url.pathname === "/v1/catalog") return json(catalog);
83 + if (url.pathname.endsWith("/renew") && options.maxTtlMs !== undefined) {
84 + if (now >= leaseDeadline) return json({}, 404);
85 + const body =
86 + request.body._tag === "Uint8Array"
87 + ? (JSON.parse(new TextDecoder().decode(request.body.body)) as { ttlMs?: number })
88 + : {};
89 + if ((body.ttlMs ?? 0) > options.maxTtlMs)
90 + return json({ error: { code: "BAD_REQUEST", message: "ttlMs exceeds maxTtlMs" } }, 400);
91 + leaseDeadline = now + (body.ttlMs ?? options.maxTtlMs);
92 + return json({});
93 + }
94 if (url.pathname.endsWith("/renew")) return json({}, renewStatus);
95 if (request.method === "DELETE") return json({}, 202);
96 return json({}, 404);
@@ -114,6 +130,8 @@ const fakePool = (
130 path.join(dir, `${session}.json`),
131 JSON.stringify({ leaseId: `lease-${++leases}` }),
132 );
133 + if (options.maxTtlMs !== undefined)
134 + leaseDeadline = (yield* Clock.currentTimeMillis) + options.maxTtlMs;
135 }
136 if (command === "screenshot") yield* fs.writeFile(args[1]!, new Uint8Array([1, 2, 3]));
137 return {
@@ -475,6 +493,21 @@ describe("Simlock device host", () => {
493 }).pipe(Effect.scoped, Effect.provide(layer)),
494 );
495
496 + it.effect("keeps a watched lease renewed when the gateway caps lease TTLs", () =>
497 + Effect.gen(function* () {
498 + const pool = yield* fakePool({ maxTtlMs: 5 * 60_000 });
499 + const host = yield* makeHost(pool, config(), yield* Scope.make());
500 + const ready = yield* host.ensureReady(() => Effect.void);
501 + const [iphone] = (yield* ready.devices.list).devices;
502 + const leaseId = yield* ready.devices.boot(iphone!);
503 +
504 + yield* TestClock.adjust("6 minutes");
505 + expect((yield* ready.devices.list).devices.some((device) => device.id === leaseId)).toBe(
506 + true,
507 + );
508 + }).pipe(Effect.scoped, Effect.provide(layer)),
509 + );
510 +
511 it.effect("returns a lease whose open finishes after the host stopped", () =>
512 Effect.gen(function* () {
513 const holdOpen = {
apps/server/src/device/SimlockDeviceHost.ts
+2 -3
@@ -42,9 +42,9 @@ const REQUEST_TIMEOUT = Duration.seconds(15);
42 // The gateway queues a lease request for up to five minutes when every device is busy.
43 const OPEN_TIMEOUT = Duration.minutes(7);
44 const COMMAND_TIMEOUT = Duration.minutes(2);
45 -// Renewal keeps a watched but idle device; a lease this server stops renewing ends after LEASE_TTL.
45 +// Renewal keeps a watched but idle device. It sends no ttlMs: Simlock then keeps the lease's own
46 +// length, while one above the gateway's lease.maxTtlMs is refused rather than shortened.
47 const RENEW_INTERVAL = Duration.minutes(2);
47 -const LEASE_TTL_MS = 10 * 60_000;
48 const STREAM_PATH = /^\/vendor\/serve-sim\/helper\/([^/]+)\/stream\.avcc$/;
49 const INPUT_PATH = "/vendor/serve-sim/helper/ws";
50
@@ -422,7 +422,6 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
422 lease.token,
423 "POST",
424 `/v1/leases/${encodeURIComponent(lease.leaseId)}/renew`,
425 - { ttlMs: LEASE_TTL_MS },
425 ).pipe(Effect.result);
426 if (renewed._tag === "Success") continue;
427 const error = renewed.failure;