Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/await-progress-reconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@systemfsoftware/agent-platform-cloudflare": patch
---

`CloudflareThreadClient.awaitProgress` now re-issues its wait when a reset reaches it as a bare remote rejection or as an answer that cannot be decoded, so a wait for durable progress survives a Durable Object reset instead of ending with the reset's error.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ it runs, and the budget is the suite's configured `testTimeout`, not the machine
135 sequential SQLite operations plus a cold reconstruction, so 10 s was below what the work
needs when the CPUs are shared.

Measured with a probe: an in-flight `awaitProgress` RPC is not rejected by `state.abort()`. Workerd
re-runs the request on the fresh incarnation, the fresh incarnation reports the waiter, and the
client promise resolves when progress arrives. The client's retry classification therefore never
runs for an eviction.
Measured with a probe: `state.abort()` rejects an in-flight `awaitProgress` RPC with an error
carrying `remote` and `durableObjectReset` (200/200 on a plain RPC, 160/160 through the client), and
workerd does not replay the request on the fresh incarnation. The client therefore has to re-issue
it, and a test that waits for the fresh incarnation to report a subscription stalls for its whole cap
whenever that re-issue does not happen.

## What does not work

Expand All @@ -48,8 +49,11 @@ runs for an eviction.

## What works

- Count subscriptions monotonically per incarnation, so a replay is observed whether or not it is
still parked, and latch on the count rather than on a gauge.
- Re-issue the wait from the client after a reset-shaped transport failure (a bare remote rejection
or an answer that cannot be decoded), and keep an overload-flagged rejection and a typed host
failure terminal.
- Count subscriptions monotonically per incarnation, so a re-issued request is observed whether or
not it is still parked, and latch on the count rather than on a gauge.
- Fork client work through a harness that tracks every fiber, and interrupt and await them after
each test instead of at the end of the file.
- After each test, disarm the eviction failpoints it armed and delete the persisted alarm of every
Expand Down
23 changes: 14 additions & 9 deletions packages/platform-cloudflare/src/CloudflareThreadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,21 +875,26 @@ export class CloudflareThreadClient extends Context.Service<
),
)

const reissue = (
error: ThreadClientError | HostProtocolError,
retry: number,
): Effect.Effect<void, ClientProgressFailure> =>
retry < 5
? Effect.sleep(Duration.millis(10 * 2 ** retry)).pipe(
Effect.andThen(attempt(retry + 1)),
)
: Effect.fail(error)

const attempt = (retry: number): Effect.Effect<void, ClientProgressFailure> =>
call(threadId, 'awaitProgress', encoded).pipe(
Effect.flatMap(
expect(threadId, 'awaitProgress', ProgressObserved, ClientObserveHostFailure),
),
Effect.asVoid,
Effect.catchTag(
'ThreadClientError',
(error) =>
error.retryable === true && error.overloaded !== true && retry < 5
? Effect.sleep(Duration.millis(10 * 2 ** retry)).pipe(
Effect.andThen(attempt(retry + 1)),
)
: Effect.fail(error),
),
Effect.catchTags({
ThreadClientError: (error) => error.overloaded === true ? Effect.fail(error) : reissue(error, retry),
HostProtocolError: (error) => reissue(error, retry),
}),
)

yield* attempt(0).pipe(Effect.onInterrupt(() => cancelProgress(threadId, waiterId)))
Expand Down
11 changes: 7 additions & 4 deletions packages/platform-cloudflare/test/progress-wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ describe('#94 Cloudflare durable progress wait', () => {
state.abort('#94 forced wait eviction')
}).catch(() => undefined)

const reconstructedIncarnation = await awaitReconstructedProgressWaiter(
thread,
priorIncarnation,
1,
const surfaceClientWaitFailure = Fiber.join(waiting).pipe(Effect.flatMap(() => Effect.never))

const reconstructedIncarnation = await Effect.runPromise(
Effect.raceFirst(
Effect.promise(() => awaitReconstructedProgressWaiter(thread, priorIncarnation, 1)),
surfaceClientWaitFailure,
),
)

expect(reconstructedIncarnation).not.toBe(priorIncarnation)
Expand Down
Loading