TypeScript
fix(server): keep relay catch-up requests working after startup (#25201)
maria044775d69712
diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.tsindex 40c741fdd68..4bbaa85fc3b 100644--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts@@ -720,3 +720,35 @@ describe("AgentAwarenessRelay", () => {),);});++it.effect("catches up after startup without polling after success", () =>+ Effect.gen(function* () {+ const { relay, secrets, secretReads, catchUp } = yield* makeTestRelay();+ yield* relay.start();+ yield* TestClock.adjust("1 second");+ assert.equal(catchUp.shellSnapshotReads, 1);++ yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));+ yield* relay.requestCatchUp();+ yield* TestClock.adjust("1 second");+ assert.equal(catchUp.shellSnapshotReads, 1);++ yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));+ yield* relay.requestCatchUp();+ yield* TestClock.adjust("1 second");+ assert.equal(catchUp.shellSnapshotReads, 2);++ const readsBeforeIdle = secretReads.length;+ yield* TestClock.adjust("10 minutes");+ assert.equal(catchUp.shellSnapshotReads, 2);+ assert.equal(secretReads.length, readsBeforeIdle);++ yield* secrets.set(+ RELAY_ENVIRONMENT_CREDENTIAL_SECRET,+ new TextEncoder().encode("credential-2"),+ );+ yield* relay.requestCatchUp();+ yield* TestClock.adjust("1 second");+ assert.equal(catchUp.shellSnapshotReads, 3);+ }).pipe(Effect.scoped),+);diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.tsindex bad5b0d20d2..f3c7b893afa 100644--- a/apps/server/src/relay/AgentAwarenessRelay.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.ts@@ -369,7 +369,6 @@ export const make = Effect.gen(function* () {const scope = yield* Effect.scope;const cloudLinkKeyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets);const startedAt = (yield* DateTime.now).epochMilliseconds;- const activeSnapshotPublishedRef = yield* Ref.make(false);const catchUpRequests = yield* Queue.dropping<void>(1);const publishedStateByThreadRef = yield* Ref.make(new Map<ThreadId, string>());@@ -759,23 +758,25 @@ export const make = Effect.gen(function* () {// s to 60 s. A linked environment keeps the 5 s retry, because `t3 connect// publish` can turn publishing on from another process. Keep this untraced:// an unlinked environment can retry for the lifetime of the process.- const publishActiveThreadsOnceWhenConfigured = Effect.fnUntraced(function* (+ const publishActiveThreadsWhenConfigured = Effect.fnUntraced(function* (logEnabledWhenReady: boolean,) {let unlinkedRetryDelayMs = 5_000;- while (!(yield* Ref.get(activeSnapshotPublishedRef))) {+ while (true) {const result = yield* publishActiveThreadsUnsafe.pipe(Effect.orElseSucceed(() => "failed" as const),);if (result === "published") {- yield* Ref.set(activeSnapshotPublishedRef, true);if (logEnabledWhenReady) {const relayConfig = yield* readRelayConfig.pipe(Effect.orElseSucceed(() => null));yield* Effect.logInfo("agent activity publishing enabled after link reconciliation", {relayUrl: relayConfig?.url,});}- return;+ logEnabledWhenReady = false;+ unlinkedRetryDelayMs = 5_000;+ yield* Queue.take(catchUpRequests);+ continue;}const retryDelayMs = result === "unlinked" ? unlinkedRetryDelayMs : 5_000;yield* Effect.race(Effect.sleep(retryDelayMs), Queue.take(catchUpRequests));@@ -826,7 +827,7 @@ export const make = Effect.gen(function* () {}yield* forkParked(Effect.sleep("1 second").pipe(- Effect.andThen(publishActiveThreadsOnceWhenConfigured(startupState !== "enabled")),+ Effect.andThen(publishActiveThreadsWhenConfigured(startupState !== "enabled")),),);yield* forkParked(