Skip to content

Commit c6aba40

Browse files
t3dotggclaude
andcommitted
fix(server): keep the 5 s relay retry while linked
Only this server writes the T3 Connect link, and it wakes the relay when it does, so the catch-up retry backs off to 60 s only while unlinked. A linked server keeps the 5 s retry, because `t3 connect publish` can turn publishing on from another process. Also test that the link and preferences handlers wake the relay. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 988189d commit c6aba40

3 files changed

Lines changed: 102 additions & 24 deletions

File tree

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -978,17 +978,17 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
978978
});
979979

980980
describe.sequential("startup catch-up", () => {
981-
// An unlinked relay. `link` writes the secrets a link and opt-in store.
982-
// Counts link checks (publish setting reads) and catch-up publishes (shell
983-
// snapshot reads).
981+
// An unlinked relay with publishing off. `link` writes the link secrets and
982+
// `enablePublishing` the opt-in. Counts link checks (relay URL reads) and
983+
// catch-up publishes (shell snapshot reads).
984984
function makeUnlinkedRelay() {
985985
const secrets = makeMemorySecretStore();
986986
const counts = { linkChecks: 0, catchUpPublishes: 0 };
987987
const countingStore = {
988988
...secrets.store,
989989
get: (name: string) =>
990990
Effect.suspend(() => {
991-
if (name === PUBLISH_AGENT_ACTIVITY_SECRET) counts.linkChecks += 1;
991+
if (name === RELAY_URL_SECRET) counts.linkChecks += 1;
992992
return secrets.store.get(name);
993993
}),
994994
} satisfies ServerSecretStore.ServerSecretStore["Service"];
@@ -1024,17 +1024,18 @@ describe.sequential("startup catch-up", () => {
10241024
[
10251025
secrets.setString(RELAY_URL_SECRET, "https://relay.example.test"),
10261026
secrets.setString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "relay-credential"),
1027-
secrets.setString(PUBLISH_AGENT_ACTIVITY_SECRET, "true"),
10281027
],
10291028
{ discard: true },
10301029
);
1031-
return { counts, layer, link };
1030+
const enablePublishing = secrets.setString(PUBLISH_AGENT_ACTIVITY_SECRET, "true");
1031+
return { counts, layer, link, enablePublishing };
10321032
}
10331033

10341034
it.effect("checks an unlinked environment once a minute and still catches up once linked", () => {
1035-
const { counts, layer, link } = makeUnlinkedRelay();
1035+
const { counts, layer, link, enablePublishing } = makeUnlinkedRelay();
10361036
return Effect.gen(function* () {
10371037
const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
1038+
yield* enablePublishing;
10381039
yield* relay.start();
10391040

10401041
// Get past the backoff ramp, then count checks in a steady window.
@@ -1051,9 +1052,10 @@ describe.sequential("startup catch-up", () => {
10511052
});
10521053

10531054
it.effect("publishes at once when this process links while the check is backed off", () => {
1054-
const { counts, layer, link } = makeUnlinkedRelay();
1055+
const { counts, layer, link, enablePublishing } = makeUnlinkedRelay();
10551056
return Effect.gen(function* () {
10561057
const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
1058+
yield* enablePublishing;
10571059
yield* relay.start();
10581060

10591061
// Backed off to 60 s: the next check is still seconds away.
@@ -1067,4 +1069,21 @@ describe.sequential("startup catch-up", () => {
10671069
expect(counts.catchUpPublishes).toBe(1);
10681070
}).pipe(Effect.provide(layer), Effect.scoped);
10691071
});
1072+
1073+
it.effect("catches up within 5 s when another process enables publishing on a link", () => {
1074+
const { counts, layer, link, enablePublishing } = makeUnlinkedRelay();
1075+
return Effect.gen(function* () {
1076+
const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
1077+
yield* link;
1078+
yield* relay.start();
1079+
1080+
yield* TestClock.adjust("10 minutes");
1081+
expect(counts.catchUpPublishes).toBe(0);
1082+
1083+
// `t3 connect publish` writes the opt-in without waking this process.
1084+
yield* enablePublishing;
1085+
yield* TestClock.adjust("5 seconds");
1086+
expect(counts.catchUpPublishes).toBe(1);
1087+
}).pipe(Effect.provide(layer), Effect.scoped);
1088+
});
10701089
});

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -539,18 +539,20 @@ export const make = Effect.gen(function* () {
539539
withRelayClientTracing,
540540
);
541541

542+
// Publishes the active threads once. Returns why it did not, so the retry
543+
// knows whether it is waiting on a link or on the publish setting.
542544
const publishActiveThreadsUnsafe = Effect.gen(function* () {
545+
const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));
546+
if (!relayConfig) {
547+
yield* Effect.logDebug("agent activity snapshot skipped; relay link credentials unavailable");
548+
return "unlinked" as const;
549+
}
543550
const publishAgentActivity = yield* readPublishAgentActivityEnabled.pipe(
544551
Effect.orElseSucceed(() => false),
545552
);
546553
if (!publishAgentActivity) {
547554
yield* Effect.logDebug("agent activity snapshot skipped; publication disabled");
548-
return false;
549-
}
550-
const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));
551-
if (!relayConfig) {
552-
yield* Effect.logDebug("agent activity snapshot skipped; relay link credentials unavailable");
553-
return false;
555+
return "disabled" as const;
554556
}
555557
const environmentId = yield* serverEnvironment.getEnvironmentId;
556558
const snapshot = yield* snapshotQuery.getShellSnapshot();
@@ -562,26 +564,29 @@ export const make = Effect.gen(function* () {
562564
});
563565
if (activeThreadIds.length === 0) {
564566
yield* Effect.logDebug("agent activity snapshot has no publishable threads");
565-
return true;
567+
return "published" as const;
566568
}
567569
yield* Effect.logInfo("publishing active agent activity snapshot", {
568570
count: activeThreadIds.length,
569571
});
570572
yield* Effect.forEach(activeThreadIds, publishThread, { concurrency: 4, discard: true });
571-
return true;
573+
return "published" as const;
572574
});
573575

574576
// Publishes the catch-up snapshot of active threads once the environment is
575-
// linked and publishing is enabled. Many environments never link, so the
576-
// retry backs off from 5 s to 60 s. It polls because `t3 connect` can write
577-
// the secrets from another process. Links and preference changes made by
578-
// this process call `requestCatchUp`, which ends the wait early.
577+
// linked and publishing is enabled. Many environments never link, so while
578+
// unlinked the retry backs off from 5 s to 60 s. Only this process writes
579+
// the link, and it calls `requestCatchUp`, which ends the wait early. A
580+
// linked environment keeps the 5 s retry, because `t3 connect publish` can
581+
// turn publishing on from another process.
579582
const publishActiveThreadsOnceWhenConfigured = (logEnabledWhenReady: boolean) =>
580583
Effect.gen(function* () {
581-
let retryDelayMs = 5_000;
584+
let unlinkedRetryDelayMs = 5_000;
582585
while (!(yield* Ref.get(activeSnapshotPublishedRef))) {
583-
const published = yield* publishActiveThreadsUnsafe.pipe(Effect.orElseSucceed(() => false));
584-
if (published) {
586+
const result = yield* publishActiveThreadsUnsafe.pipe(
587+
Effect.orElseSucceed(() => "failed" as const),
588+
);
589+
if (result === "published") {
585590
yield* Ref.set(activeSnapshotPublishedRef, true);
586591
if (logEnabledWhenReady) {
587592
const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));
@@ -591,8 +596,11 @@ export const make = Effect.gen(function* () {
591596
}
592597
return;
593598
}
599+
const retryDelayMs = result === "unlinked" ? unlinkedRetryDelayMs : 5_000;
594600
yield* Effect.race(Effect.sleep(retryDelayMs), Queue.take(catchUpRequests));
595-
retryDelayMs = Math.min(retryDelayMs * 2, 60_000);
601+
if (result === "unlinked") {
602+
unlinkedRetryDelayMs = Math.min(unlinkedRetryDelayMs * 2, 60_000);
603+
}
596604
}
597605
});
598606

‎apps/server/src/server.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ const buildAppUnderTest = (options?: {
563563
CloudManagedEndpointRuntime.CloudManagedEndpointRuntime["Service"]
564564
>;
565565
relayClient?: Partial<RelayClient.RelayClient["Service"]>;
566+
agentAwarenessRelay?: Partial<AgentAwarenessRelay.AgentAwarenessRelay["Service"]>;
566567
cloudCliTokenManager?: Partial<CloudCliTokenManager.CloudCliTokenManager["Service"]>;
567568
httpClient?: HttpClient.HttpClient;
568569
nativeTelemetryClient?: Partial<NativeTelemetryClient.NativeTelemetryClient["Service"]>;
@@ -1187,6 +1188,7 @@ const buildAppUnderTest = (options?: {
11871188
),
11881189
Layer.mock(AgentAwarenessRelay.AgentAwarenessRelay)({
11891190
requestCatchUp: () => Effect.void,
1191+
...options?.layers?.agentAwarenessRelay,
11901192
}),
11911193
),
11921194
),
@@ -3120,6 +3122,55 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
31203122
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
31213123
);
31223124

3125+
it.effect("wakes the agent awareness relay when this server links or changes publishing", () =>
3126+
Effect.gen(function* () {
3127+
let catchUpRequests = 0;
3128+
yield* buildAppUnderTest({
3129+
layers: {
3130+
agentAwarenessRelay: {
3131+
requestCatchUp: () =>
3132+
Effect.sync(() => {
3133+
catchUpRequests += 1;
3134+
}),
3135+
},
3136+
},
3137+
});
3138+
3139+
const cloudKeyPair = NodeCrypto.generateKeyPairSync("ed25519", {
3140+
privateKeyEncoding: { format: "pem", type: "pkcs8" },
3141+
publicKeyEncoding: { format: "pem", type: "spki" },
3142+
});
3143+
const ownerCookie = yield* getAuthenticatedSessionCookieHeader();
3144+
const relayConfigResponse = yield* fetchEffect(
3145+
yield* getHttpServerUrl("/api/connect/relay-config"),
3146+
{
3147+
method: "POST",
3148+
headers: { cookie: ownerCookie, "content-type": "application/json" },
3149+
body: jsonRequestBody({
3150+
relayUrl: "https://relay.example.test",
3151+
cloudUserId: "user_123",
3152+
environmentCredential: "t3env_test_credential",
3153+
cloudMintPublicKey: cloudKeyPair.publicKey,
3154+
endpointRuntime: null,
3155+
}),
3156+
},
3157+
);
3158+
assert.equal(relayConfigResponse.status, 200);
3159+
assert.equal(catchUpRequests, 1);
3160+
3161+
const preferencesResponse = yield* fetchEffect(
3162+
yield* getHttpServerUrl("/api/connect/preferences"),
3163+
{
3164+
method: "POST",
3165+
headers: { cookie: ownerCookie, "content-type": "application/json" },
3166+
body: jsonRequestBody({ publishAgentActivity: true }),
3167+
},
3168+
);
3169+
assert.equal(preferencesResponse.status, 200);
3170+
assert.equal(catchUpRequests, 2);
3171+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
3172+
);
3173+
31233174
it.effect("rejects relay config with an invalid cloud mint public key", () =>
31243175
Effect.gen(function* () {
31253176
yield* buildAppUnderTest();

0 commit comments

Comments
 (0)