Skip to content

Commit 7d58df0

Browse files
fix(client-runtime): an unanswered send stops waiting without cancelling the server's work
The two-minute deadline interrupted the request itself, which makes the RPC client cancel the server handler. A thread launch commits thread creation and its first message separately, so a cut launch could leave an empty thread while the error said the message may still arrive. The request now runs detached and only the wait is bounded, so the server finishes and the error stays true. AI-assisted; verified with the tests and tools listed above.
1 parent 5ef4585 commit 7d58df0

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

‎packages/client-runtime/src/operations/commands.test.ts‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { describe, expect, it } from "@effect/vitest";
2323
import * as Cause from "effect/Cause";
2424
import * as SecureRandom from "effect/crypto/SecureRandom";
25+
import * as Deferred from "effect/Deferred";
2526
import * as Effect from "effect/Effect";
2627
import * as Exit from "effect/Exit";
2728
import * as Layer from "effect/Layer";
@@ -67,16 +68,25 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
6768
readonly projection?: OrchestrationV2ThreadProjection;
6869
readonly projectionRequests?: ThreadId[];
6970
readonly advertiseThreadStop?: boolean;
70-
readonly neverAnswers?: boolean;
71+
readonly stalled?: { readonly release: Deferred.Deferred<void>; interrupted: boolean };
7172
}) {
73+
const answer = <A>(result: A) =>
74+
input.stalled === undefined
75+
? Effect.succeed(result)
76+
: Deferred.await(input.stalled.release).pipe(
77+
Effect.as(result),
78+
Effect.onInterrupt(() =>
79+
Effect.sync(() => {
80+
input.stalled!.interrupted = true;
81+
}),
82+
),
83+
);
7284
const client = {
7385
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
7486
Effect.sync(() => {
7587
input.commands.push(command);
7688
return { sequence: input.commands.length };
77-
}).pipe(
78-
Effect.andThen((result) => (input.neverAnswers ? Effect.never : Effect.succeed(result))),
79-
),
89+
}).pipe(Effect.andThen(answer)),
8090
[ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: (requestInput: {
8191
readonly threadId: ThreadId;
8292
}) =>
@@ -92,9 +102,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
92102
projection: input.projection ?? v2Projection,
93103
resumed: false,
94104
};
95-
}).pipe(
96-
Effect.andThen((result) => (input.neverAnswers ? Effect.never : Effect.succeed(result))),
97-
),
105+
}).pipe(Effect.andThen(answer)),
98106
[WS_METHODS.projectsMutate]: (mutation: ProjectMutation) =>
99107
Effect.sync(() => {
100108
input.projects.push(mutation);
@@ -307,12 +315,8 @@ describe("V2 environment commands", () => {
307315
function* (bootstrap: StartThreadTurnInput["bootstrap"]) {
308316
const commands: OrchestrationV2Command[] = [];
309317
const launches: OrchestrationV2ThreadLaunchInput[] = [];
310-
const supervisor = yield* makeSupervisor({
311-
commands,
312-
launches,
313-
projects: [],
314-
neverAnswers: true,
315-
});
318+
const stalled = { release: yield* Deferred.make<void>(), interrupted: false };
319+
const supervisor = yield* makeSupervisor({ commands, launches, projects: [], stalled });
316320

317321
const send = yield* startThreadTurn({
318322
commandId: CommandId.make("command-unanswered"),
@@ -342,6 +346,9 @@ describe("V2 environment commands", () => {
342346
"The server did not confirm this message. It may still arrive; check the thread before sending it again.",
343347
}),
344348
);
349+
// A launch commits in several steps, so the late request must finish rather than be cut.
350+
expect(stalled.interrupted).toBe(false);
351+
yield* Deferred.succeed(stalled.release, undefined);
345352
},
346353
);
347354

‎packages/client-runtime/src/operations/commands.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2Pend
3232
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
3333
import * as SecureRandom from "effect/crypto/SecureRandom";
3434
import * as Effect from "effect/Effect";
35+
import * as Fiber from "effect/Fiber";
3536
import * as Schema from "effect/Schema";
3637

3738
import { getInitialServerConfig, request } from "../rpc/client.ts";
@@ -287,12 +288,17 @@ export class MessageDispatchUnansweredError extends Schema.TaggedError<MessageDi
287288

288289
// The server answers a message dispatch or thread launch once it commits the command; worktree
289290
// preparation runs after that answer. A healthy socket that stays silent this long means a
290-
// stalled pipeline. The server may still commit later, so the error says so instead of
291-
// retrying, which could duplicate the message.
291+
// stalled pipeline. The deadline bounds only the wait: interrupting the request would cancel
292+
// the server handler mid-launch, so it keeps running and the error says the message may arrive.
292293
const MESSAGE_DISPATCH_ANSWER_TIMEOUT = "2 minutes";
293294

294-
const requireMessageAnswer = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
295-
effect.pipe(
295+
const requireMessageAnswer = Effect.fn("EnvironmentCommands.requireMessageAnswer")(function* <
296+
A,
297+
E,
298+
R,
299+
>(effect: Effect.Effect<A, E, R>) {
300+
const answer = yield* Effect.forkDetach(effect, { startImmediately: true });
301+
return yield* Fiber.join(answer).pipe(
296302
Effect.timeoutOrElse({
297303
duration: MESSAGE_DISPATCH_ANSWER_TIMEOUT,
298304
orElse: () =>
@@ -304,6 +310,7 @@ const requireMessageAnswer = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
304310
),
305311
}),
306312
);
313+
});
307314

308315
const dispatchMessage = (command: OrchestrationV2Command) =>
309316
requireMessageAnswer(dispatch(command));

0 commit comments

Comments
 (0)