Skip to content

Commit f225a41

Browse files
saphidcodex
andcommitted
fix(server): bound activity publishes so stalled responses release the worker
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 511d20c commit f225a41

2 files changed

Lines changed: 66 additions & 10 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,60 @@ describe("AgentAwarenessRelay", () => {
431431
}),
432432
);
433433

434+
it.effect("a stalled response body releases later threads and uses bounded retries", () =>
435+
Effect.gen(function* () {
436+
const bodyStarted = Promise.withResolvers<void>();
437+
const secondRead = yield* Deferred.make<void>();
438+
const currentShell = yield* Ref.make(shell());
439+
const { relay, publications } = yield* makeTestRelay({
440+
readShell: (threadId) =>
441+
threadId === SECOND_THREAD_ID
442+
? Deferred.succeed(secondRead, undefined).pipe(
443+
Effect.as(shell({ id: SECOND_THREAD_ID })),
444+
)
445+
: Ref.get(currentShell),
446+
respond: (attempt) => {
447+
if (attempt !== 1) return new Response("relay unavailable", { status: 503 });
448+
const response = new Response(null, {
449+
headers: { "content-type": "application/json" },
450+
});
451+
return Object.assign(response, {
452+
arrayBuffer: () => {
453+
bodyStarted.resolve();
454+
return new Promise<ArrayBuffer>(() => {});
455+
},
456+
});
457+
},
458+
});
459+
const first = yield* relay.publishThread(THREAD_ID).pipe(Effect.forkChild);
460+
yield* Effect.promise(() => bodyStarted.promise);
461+
const second = yield* relay.publishThread(SECOND_THREAD_ID).pipe(Effect.forkChild);
462+
yield* TestClock.adjust("9999 millis");
463+
assert.equal(publications.length, 1);
464+
yield* Ref.set(currentShell, shell({ title: "Latest title" }));
465+
yield* TestClock.adjust("1 millis");
466+
// Assert progress before draining so the base fails instead of hanging.
467+
assert.isTrue(yield* Deferred.isDone(secondRead));
468+
yield* Fiber.join(first);
469+
yield* Fiber.join(second);
470+
assert.equal(publications.length, 2);
471+
assert.isTrue(publications[1]?.url.includes(SECOND_THREAD_ID) ?? false);
472+
for (const delay of [1_000, 2_000, 4_000, 8_000, 16_000]) {
473+
const attempts = publications.length;
474+
yield* TestClock.adjust(delay - 1);
475+
yield* relay.drain;
476+
assert.equal(publications.length, attempts);
477+
yield* TestClock.adjust(1);
478+
yield* relay.drain;
479+
assert.equal(publications.length, attempts + 2);
480+
assert.equal(publications[attempts]?.state?.threadTitle, "Latest title");
481+
}
482+
yield* TestClock.adjust("1 minute");
483+
yield* relay.drain;
484+
assert.equal(publications.length, 12);
485+
}),
486+
);
487+
434488
it.effect("retries a failed final state without another thread event", () =>
435489
Effect.gen(function* () {
436490
const { relay, currentShell, shellReads, publications } = yield* makeTestRelay({

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,18 @@ export const make = Effect.gen(function* () {
501501
reason: input.reason,
502502
});
503503

504-
const response = yield* relayClient.server.publishAgentActivity({
505-
params: {
506-
environmentId,
507-
threadId,
508-
},
509-
payload: {
510-
state: input.state,
511-
proof,
512-
},
513-
});
504+
const response = yield* relayClient.server
505+
.publishAgentActivity({
506+
params: {
507+
environmentId,
508+
threadId,
509+
},
510+
payload: {
511+
state: input.state,
512+
proof,
513+
},
514+
})
515+
.pipe(Effect.timeout("10 seconds"));
514516

515517
yield* Effect.logInfo("agent activity publish completed", {
516518
environmentId,

0 commit comments

Comments
 (0)