Skip to content

Commit 022a907

Browse files
t3dotggclaude
andcommitted
fix(relay): admitted environments restart without asking T3 Chat
Overrides stay the exact cap, the Clerk lookup times out, and the link response is not cached. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1be23cc commit 022a907

5 files changed

Lines changed: 63 additions & 13 deletions

File tree

‎docs/internals/t3-connect.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ rather than by a shared identity. The relay and T3 Chat share one secret
139139
short-lived token naming the Clerk user and email; the user confirms it while
140140
signed in to T3 Chat, and T3 Chat stores the link. T3 Chat owns the link and the
141141
plan, so the relay keeps no copy. It asks T3 Chat only when an account reaches
142-
its normal cap, and any failure keeps the normal cap. Downgrading does not
143-
remove tunnels that already exist
142+
its normal cap for a new environment, and any failure keeps the normal cap.
143+
An environment that already holds an allocation restarts without asking, so a
144+
downgrade or a T3 Chat outage only blocks new environments. A manual override
145+
row stays the exact cap
144146
([limits](../../infra/relay/src/environments/ManagedTunnelLimits.ts)).
145147

146148
## The relay client follows the server's pin

‎infra/relay/src/environments/ManagedTunnelLimits.test.ts‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { relayManagedTunnelLimits } from "../persistence/schema.ts";
88
import * as T3ChatAccounts from "../t3chat/T3ChatAccounts.ts";
99
import * as ManagedTunnelLimits from "./ManagedTunnelLimits.ts";
1010

11-
const makeFakeDb = (input: { readonly override?: number; readonly activeTunnels: number }) =>
11+
const makeFakeDb = (input: {
12+
readonly override?: number;
13+
readonly activeTunnels: number;
14+
readonly existingAllocation?: boolean;
15+
}) =>
1216
({
1317
select: () => ({
1418
from: (table: unknown) =>
@@ -25,13 +29,18 @@ const makeFakeDb = (input: { readonly override?: number; readonly activeTunnels:
2529
innerJoin: () => ({
2630
where: () => Effect.succeed([{ activeTunnels: input.activeTunnels }]),
2731
}),
32+
where: () => ({
33+
limit: () =>
34+
Effect.succeed(input.existingAllocation ? [{ environmentId: "env-new" }] : []),
35+
}),
2836
},
2937
}),
3038
}) as unknown as RelayDb.RelayDb["Service"];
3139

3240
const ensureCapacity = (input: {
3341
readonly override?: number;
3442
readonly activeTunnels: number;
43+
readonly existingAllocation?: boolean;
3544
readonly plan: RelayT3ChatPlan | "lookup-fails";
3645
readonly lookups?: Array<string>;
3746
}) =>
@@ -89,13 +98,28 @@ describe("ManagedTunnelLimits", () => {
8998
}),
9099
);
91100

92-
it.effect("leaves larger manual overrides alone", () =>
101+
it.effect("treats a manual override as the exact cap", () =>
102+
Effect.gen(function* () {
103+
const lookups: Array<string> = [];
104+
for (const override of [1, 200]) {
105+
const error = yield* Effect.flip(
106+
ensureCapacity({ override, activeTunnels: override, plan: "premier", lookups }),
107+
);
108+
expect(error).toMatchObject({ _tag: "ManagedTunnelLimitExceeded", maxTunnels: override });
109+
}
110+
expect(lookups).toEqual([]);
111+
}),
112+
);
113+
114+
it.effect("lets an admitted environment restart without asking T3 Chat", () =>
93115
Effect.gen(function* () {
94116
const lookups: Array<string> = [];
95-
const error = yield* Effect.flip(
96-
ensureCapacity({ override: 200, activeTunnels: 200, plan: "premier", lookups }),
97-
);
98-
expect(error).toMatchObject({ _tag: "ManagedTunnelLimitExceeded", maxTunnels: 200 });
117+
yield* ensureCapacity({
118+
activeTunnels: 10,
119+
plan: "lookup-fails",
120+
existingAllocation: true,
121+
lookups,
122+
});
99123
expect(lookups).toEqual([]);
100124
}),
101125
);

‎infra/relay/src/environments/ManagedTunnelLimits.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const DEFAULT_MANAGED_TUNNEL_LIMIT = 3;
1818
export class ManagedTunnelLimitPersistenceError extends Schema.TaggedError<ManagedTunnelLimitPersistenceError>()(
1919
"ManagedTunnelLimitPersistenceError",
2020
{
21-
operation: Schema.Literals(["load-limit", "count-tunnels"]),
21+
operation: Schema.Literals(["load-limit", "count-tunnels", "load-allocation"]),
2222
userId: Schema.String,
2323
cause: Schema.Defect(),
2424
},
@@ -118,10 +118,33 @@ const make = Effect.gen(function* () {
118118
const activeTunnels = counted[0]?.activeTunnels ?? 0;
119119
if (activeTunnels < baseMaxTunnels) return;
120120

121+
// Allocations survive tunnel release, so a restart of an admitted environment
122+
// passes without asking T3 Chat. A downgrade only blocks new environments.
123+
const existing = yield* db
124+
.select({ environmentId: relayManagedEndpointAllocations.environmentId })
125+
.from(relayManagedEndpointAllocations)
126+
.where(
127+
and(
128+
eq(relayManagedEndpointAllocations.userId, input.userId),
129+
eq(relayManagedEndpointAllocations.environmentId, input.environmentId),
130+
),
131+
)
132+
.limit(1)
133+
.pipe(
134+
Effect.mapError(
135+
(cause) =>
136+
new ManagedTunnelLimitPersistenceError({
137+
operation: "load-allocation",
138+
userId: input.userId,
139+
cause,
140+
}),
141+
),
142+
);
143+
if (existing.length > 0) return;
144+
145+
// A manual override is the exact cap; Premier raises only the default.
121146
const maxTunnels =
122-
t3Chat.available &&
123-
baseMaxTunnels < T3CHAT_PREMIER_TUNNEL_LIMIT &&
124-
(yield* hasT3ChatPremier(input.userId))
147+
t3Chat.available && overrides.length === 0 && (yield* hasT3ChatPremier(input.userId))
125148
? T3CHAT_PREMIER_TUNNEL_LIMIT
126149
: baseMaxTunnels;
127150
if (activeTunnels >= maxTunnels) {

‎infra/relay/src/http/Api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ export const layerClientApi = HttpApiBuilder.group(
995995
.handle(
996996
"getT3ChatLink",
997997
Effect.fn("relay.api.client.getT3ChatLink")(function* () {
998+
yield* appendRelayCredentialResponseHeaders;
998999
if (!t3Chat.available) return { available: false, link: null };
9991000
const { userId } = yield* RelayClientPrincipal;
10001001
const link = yield* t3Chat

‎packages/client-runtime/src/relay/errorPresentation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function relayProtectedErrorMessage(error: RelayProtectedError): string {
6868
}`;
6969
case "RelayEnvironmentLinkLimitExceededError":
7070
return error.maxTunnels < T3CHAT_PREMIER_TUNNEL_LIMIT
71-
? `Relay refused the link: this account already has its maximum of ${error.maxTunnels} managed tunnels. Unlink an environment, or link a T3 Chat Premier account in your T3 Connect account settings to raise the limit to ${T3CHAT_PREMIER_TUNNEL_LIMIT}.`
71+
? `Relay refused the link: this account already has its maximum of ${error.maxTunnels} managed tunnels. Unlink an environment, or link a T3 Chat Premier account in your T3 Connect account settings on web or desktop to raise the limit to ${T3CHAT_PREMIER_TUNNEL_LIMIT}.`
7272
: `Relay refused the link: this account already has its maximum of ${error.maxTunnels} managed tunnels. Unlink an environment to free one up.`;
7373
case "RelayAgentActivityPublishProofExpiredError":
7474
return "Relay rejected an expired agent activity publish proof.";

0 commit comments

Comments
 (0)