TypeScript
fix(server): bound activity publishes so stalled responses release the worker (#18830)
Alex Southwell173a97097659
diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.tsindex efb9ec3995b..681f2381bb9 100644--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts@@ -431,6 +431,60 @@ describe("AgentAwarenessRelay", () => {}),);+ it.effect("a stalled response body releases later threads and uses bounded retries", () =>+ Effect.gen(function* () {+ const bodyStarted = Promise.withResolvers<void>();+ const secondRead = yield* Deferred.make<void>();+ const currentShell = yield* Ref.make(shell());+ const { relay, publications } = yield* makeTestRelay({+ readShell: (threadId) =>+ threadId === SECOND_THREAD_ID+ ? Deferred.succeed(secondRead, undefined).pipe(+ Effect.as(shell({ id: SECOND_THREAD_ID })),+ )+ : Ref.get(currentShell),+ respond: (attempt) => {+ if (attempt !== 1) return new Response("relay unavailable", { status: 503 });+ const response = new Response(null, {+ headers: { "content-type": "application/json" },+ });+ return Object.assign(response, {+ arrayBuffer: () => {+ bodyStarted.resolve();+ return new Promise<ArrayBuffer>(() => {});+ },+ });+ },+ });+ const first = yield* relay.publishThread(THREAD_ID).pipe(Effect.forkChild);+ yield* Effect.promise(() => bodyStarted.promise);+ const second = yield* relay.publishThread(SECOND_THREAD_ID).pipe(Effect.forkChild);+ yield* TestClock.adjust("9999 millis");+ assert.equal(publications.length, 1);+ yield* Ref.set(currentShell, shell({ title: "Latest title" }));+ yield* TestClock.adjust("1 millis");+ // Assert progress before draining so the base fails instead of hanging.+ assert.isTrue(yield* Deferred.isDone(secondRead));+ yield* Fiber.join(first);+ yield* Fiber.join(second);+ assert.equal(publications.length, 2);+ assert.isTrue(publications[1]?.url.includes(SECOND_THREAD_ID) ?? false);+ for (const delay of [1_000, 2_000, 4_000, 8_000, 16_000]) {+ const attempts = publications.length;+ yield* TestClock.adjust(delay - 1);+ yield* relay.drain;+ assert.equal(publications.length, attempts);+ yield* TestClock.adjust(1);+ yield* relay.drain;+ assert.equal(publications.length, attempts + 2);+ assert.equal(publications[attempts]?.state?.threadTitle, "Latest title");+ }+ yield* TestClock.adjust("1 minute");+ yield* relay.drain;+ assert.equal(publications.length, 12);+ }),+ );+it.effect("retries a failed final state without another thread event", () =>Effect.gen(function* () {const { relay, currentShell, shellReads, publications } = yield* makeTestRelay({diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.tsindex 447192fce47..ae9d053ac1f 100644--- a/apps/server/src/relay/AgentAwarenessRelay.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.ts@@ -502,16 +502,18 @@ export const make = Effect.gen(function* () {reason: input.reason,});- const response = yield* relayClient.server.publishAgentActivity({- params: {- environmentId,- threadId,- },- payload: {- state: input.state,- proof,- },- });+ const response = yield* relayClient.server+ .publishAgentActivity({+ params: {+ environmentId,+ threadId,+ },+ payload: {+ state: input.state,+ proof,+ },+ })+ .pipe(Effect.timeout("10 seconds"));yield* Effect.logInfo("agent activity publish completed", {environmentId,