Skip to content

Commit 86621c2

Browse files
committed
perf(mobile): renew T3 Connect tokens earlier and skip repeat bootstrap work
Keep access tokens warm across short backgrounds, reuse a stable relay endpoint on renew, and keep the empty state loading until the shell arrives after the socket is up.
1 parent e945434 commit 86621c2

8 files changed

Lines changed: 235 additions & 66 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ function deriveEmptyState(props: {
185185
}
186186

187187
if (
188-
catalogState.hasConnectingEnvironment &&
188+
(catalogState.hasConnectingEnvironment ||
189+
catalogState.hasSyncingConnectedEnvironment ||
190+
catalogState.hasPendingShellSnapshot) &&
189191
!catalogState.hasLoadedShellSnapshot &&
190192
catalogState.connectionError === null
191193
) {
192194
return {
193-
title: "Connecting to environment",
195+
title: catalogState.hasReadyEnvironment ? "Loading environment" : "Connecting to environment",
194196
detail: "Loading projects and threads from the saved environment.",
195197
loading: true,
196198
};

‎apps/mobile/src/features/threads/NewTaskRouteScreen.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ function deriveProjectEmptyState(catalogState: WorkspaceState): {
9292
}
9393

9494
if (
95-
catalogState.hasConnectingEnvironment &&
95+
(catalogState.hasConnectingEnvironment ||
96+
catalogState.hasSyncingConnectedEnvironment ||
97+
catalogState.hasPendingShellSnapshot) &&
9698
!catalogState.hasLoadedShellSnapshot &&
9799
catalogState.connectionError === null
98100
) {
99101
return {
100-
title: "Connecting to environment",
102+
title: catalogState.hasReadyEnvironment ? "Loading environment" : "Connecting to environment",
101103
detail: "Loading projects from the saved environment.",
102104
loading: true,
103105
};

‎docs/internals/connection-runtime.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ require valid credentials.
7676

7777
## Transport health and data freshness are separate
7878

79-
A socket opening is insufficient evidence that the environment is usable. The
80-
[RPC session](../../packages/client-runtime/src/rpc/session.ts) waits for the
81-
initial server configuration before becoming ready. Shell and thread data then
82-
have their own synchronization state. A failed shell subscription can coexist
83-
with a healthy connection; labeling that state "reconnecting" promises a
84-
transport retry that will never happen.
79+
The [RPC session](../../packages/client-runtime/src/rpc/session.ts) becomes ready
80+
when the WebSocket opens so the UI can leave "Connecting…" without waiting on a
81+
large config snapshot. Prefer `initialConfig` / `subscribeServerConfig` when the
82+
snapshot itself is required. Shell and thread data then have their own
83+
synchronization state. A failed shell subscription can coexist with a healthy
84+
connection; labeling that state "reconnecting" promises a transport retry that
85+
will never happen.
8586

8687
Cached projections remain readable offline. They must neither imply a live
8788
connection nor overwrite newer live data during a reconnect. Loading and

‎packages/client-runtime/src/authorization/layer.test.ts‎

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,8 @@ describe("RemoteEnvironmentAuthorization", () => {
500500
});
501501
const harness = yield* makeHarness({
502502
initialToken: expired,
503-
responses: [
504-
Response.json(DESCRIPTOR),
505-
accessToken("fresh-access-token"),
506-
websocketTicket("fresh-ticket"),
507-
],
503+
// Prior tunnel host is reused, so the descriptor fetch is skipped.
504+
responses: [accessToken("fresh-access-token"), websocketTicket("fresh-ticket")],
508505
});
509506

510507
const authorized = yield* Effect.gen(function* () {
@@ -520,9 +517,64 @@ describe("RemoteEnvironmentAuthorization", () => {
520517
expect.objectContaining({
521518
accessToken: "fresh-access-token",
522519
dpopThumbprint: "thumbprint-1",
520+
label: DESCRIPTOR.label,
523521
}),
524522
);
525-
expect(harness.fetch.calls).toHaveLength(3);
523+
expect(harness.fetch.calls).toHaveLength(2);
524+
}),
525+
);
526+
527+
it.effect("renews early when authorizeDpopHttp asks for a wider refresh skew", () =>
528+
Effect.gen(function* () {
529+
const harness = yield* makeHarness({
530+
initialToken: persistedToken({ expiresAtEpochMs: 4 * 60_000 }),
531+
responses: [accessToken("wake-renewed-token")],
532+
});
533+
534+
const authorized = yield* Effect.gen(function* () {
535+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
536+
return yield* remote.authorizeDpopHttp({
537+
expectedEnvironmentId: ENVIRONMENT_ID,
538+
refreshSkewMs: 5 * 60_000,
539+
});
540+
}).pipe(Effect.provide(harness.layer));
541+
542+
expect(authorized.httpAuthorization.accessToken).toBe("wake-renewed-token");
543+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
544+
}),
545+
);
546+
547+
it.effect("fetches a descriptor when the relay returns a new tunnel endpoint", () =>
548+
Effect.gen(function* () {
549+
const moved = {
550+
...ENDPOINT,
551+
httpBaseUrl: "https://moved.example.test",
552+
wsBaseUrl: "wss://moved.example.test",
553+
};
554+
const harness = yield* makeHarness({
555+
initialToken: persistedToken({ expiresAtEpochMs: 0 }),
556+
bootstrap: { ...BOOTSTRAP, endpoint: moved },
557+
responses: [
558+
Response.json({ ...DESCRIPTOR, label: "Moved environment" }),
559+
accessToken("moved-access-token"),
560+
websocketTicket("moved-ticket"),
561+
],
562+
});
563+
564+
const authorized = yield* Effect.gen(function* () {
565+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
566+
return yield* remote.authorizeDpop({
567+
expectedEnvironmentId: ENVIRONMENT_ID,
568+
});
569+
}).pipe(Effect.provide(harness.layer));
570+
571+
expect(authorized.socketUrl).toContain("wsTicket=moved-ticket");
572+
expect(authorized.label).toBe("Moved environment");
573+
expect(harness.fetch.calls.map(([url]) => String(url))).toEqual([
574+
"https://moved.example.test/.well-known/t3/environment",
575+
"https://moved.example.test/oauth/token",
576+
"https://moved.example.test/api/auth/websocket-ticket/rpc",
577+
]);
526578
}),
527579
);
528580

@@ -556,7 +608,6 @@ describe("RemoteEnvironmentAuthorization", () => {
556608
Response.json(DESCRIPTOR),
557609
exchangeGrant,
558610
websocketTicket("granted-ticket"),
559-
Response.json(DESCRIPTOR),
560611
exchangeGrant,
561612
],
562613
});
@@ -621,12 +672,7 @@ describe("RemoteEnvironmentAuthorization", () => {
621672
),
622673
),
623674
initialToken: persistedToken({ expiresAtEpochMs: 3_600_000 }),
624-
responses: [
625-
Response.json(DESCRIPTOR),
626-
accessToken("renewed-1"),
627-
Response.json(DESCRIPTOR),
628-
accessToken("renewed-2"),
629-
],
675+
responses: [accessToken("renewed-1"), accessToken("renewed-2")],
630676
beforePut: Queue.offer(puts, undefined).pipe(Effect.asVoid),
631677
});
632678

@@ -670,7 +716,6 @@ describe("RemoteEnvironmentAuthorization", () => {
670716
initialToken: cached,
671717
responses: [
672718
authInvalid(),
673-
Response.json(DESCRIPTOR),
674719
accessToken("replacement-access-token"),
675720
websocketTicket("replacement-ticket"),
676721
],
@@ -690,7 +735,7 @@ describe("RemoteEnvironmentAuthorization", () => {
690735
accessToken: "replacement-access-token",
691736
}),
692737
);
693-
expect(harness.fetch.calls).toHaveLength(4);
738+
expect(harness.fetch.calls).toHaveLength(3);
694739
}),
695740
);
696741

@@ -709,7 +754,6 @@ describe("RemoteEnvironmentAuthorization", () => {
709754
initialToken: cached,
710755
responses: [
711756
new Response("endpoint unavailable", { status: 503 }),
712-
Response.json(DESCRIPTOR),
713757
accessToken("replacement-access-token"),
714758
websocketTicket("replacement-ticket"),
715759
],
@@ -729,7 +773,7 @@ describe("RemoteEnvironmentAuthorization", () => {
729773
accessToken: "replacement-access-token",
730774
}),
731775
);
732-
expect(harness.fetch.calls).toHaveLength(4);
776+
expect(harness.fetch.calls).toHaveLength(3);
733777
}),
734778
);
735779

@@ -760,7 +804,7 @@ describe("RemoteEnvironmentAuthorization", () => {
760804
Effect.gen(function* () {
761805
const harness = yield* makeHarness({
762806
initialToken: persistedToken(),
763-
responses: [Response.json(DESCRIPTOR), accessToken("replacement-token")],
807+
responses: [accessToken("replacement-token")],
764808
});
765809
yield* Effect.gen(function* () {
766810
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
@@ -776,7 +820,7 @@ describe("RemoteEnvironmentAuthorization", () => {
776820
}).pipe(Effect.provide(harness.layer));
777821

778822
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
779-
expect(harness.fetch.calls).toHaveLength(2);
823+
expect(harness.fetch.calls).toHaveLength(1);
780824
expect((yield* Ref.get(harness.tokens)).get(ENVIRONMENT_ID)?.accessToken).toBe(
781825
"replacement-token",
782826
);
@@ -792,7 +836,7 @@ describe("RemoteEnvironmentAuthorization", () => {
792836
beforeBootstrap: Deferred.succeed(started, undefined).pipe(
793837
Effect.andThen(Deferred.await(release)),
794838
),
795-
responses: [Response.json(DESCRIPTOR), accessToken("shared-token")],
839+
responses: [accessToken("shared-token")],
796840
});
797841
yield* Effect.gen(function* () {
798842
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
@@ -813,7 +857,7 @@ describe("RemoteEnvironmentAuthorization", () => {
813857
}).pipe(Effect.provide(harness.layer));
814858

815859
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
816-
expect(harness.fetch.calls).toHaveLength(2);
860+
expect(harness.fetch.calls).toHaveLength(1);
817861
}),
818862
);
819863

@@ -1025,7 +1069,8 @@ describe("RemoteEnvironmentAuthorization", () => {
10251069
Effect.gen(function* () {
10261070
const harness = yield* makeHarness({
10271071
initialToken: persistedToken(),
1028-
responses: [Response.json(DESCRIPTOR), accessToken("new-session-token")],
1072+
// Same account can reuse the prior tunnel host; only the oauth exchange runs.
1073+
responses: [accessToken("new-session-token")],
10291074
});
10301075
yield* Effect.gen(function* () {
10311076
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
@@ -1036,6 +1081,7 @@ describe("RemoteEnvironmentAuthorization", () => {
10361081
expect(signedIn.httpAuthorization.accessToken).toBe("new-session-token");
10371082
}).pipe(Effect.provide(harness.layer));
10381083
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
1084+
expect(harness.fetch.calls).toHaveLength(1);
10391085
}),
10401086
);
10411087

0 commit comments

Comments
 (0)