Skip to content

Commit ced85c6

Browse files
fix(client): a message the host never answers fails after 60 seconds instead of spinning forever (pingdotgg#35076)
1 parent 3d6ece2 commit ced85c6

3 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎apps/web/src/components/chat/useComposerSend.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,29 @@ export function useComposerSend(input: ComposerSendInput) {
433433
if (
434434
!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||
435435
!activeThread ||
436-
(isSendBusy && !sendQueuesBehindSetup) ||
437436
isRevertingCheckpoint ||
438437
!clientSettingsHydrated ||
439438
threadDetailLoading ||
440439
readPendingEnvironmentChange() !== null ||
441-
sendInFlightRef.current ||
442440
feedbackUploadsInFlightRef.current.has(routeThreadKey)
443441
) {
444442
notifyDirectAnnotationAttached();
445443
return;
446444
}
445+
if ((isSendBusy && !sendQueuesBehindSetup) || sendInFlightRef.current) {
446+
if (directAnnotation) {
447+
notifyDirectAnnotationAttached();
448+
} else {
449+
toastManager.add(
450+
stackedThreadToast({
451+
type: "info",
452+
title: "Still sending your previous message.",
453+
description: "Send again once it goes through or fails.",
454+
}),
455+
);
456+
}
457+
return;
458+
}
447459
if (needsLoadBalancing) {
448460
const draft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget);
449461
toastManager.add({

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,45 @@ describe("V2 environment commands", () => {
419419
}).pipe(Effect.provide(layerTestRandom)),
420420
);
421421

422+
it.effect("fails a message dispatch the host never answers on a live socket", () =>
423+
Effect.gen(function* () {
424+
const commands: OrchestrationV2Command[] = [];
425+
const supervisor = yield* makeSupervisor({ commands, projects: [] });
426+
const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session));
427+
const silentClient = {
428+
...healthySession.client,
429+
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
430+
Effect.sync(() => commands.push(command)).pipe(Effect.andThen(Effect.never)),
431+
} as unknown as WsRpcProtocolClient;
432+
yield* SubscriptionRef.set(
433+
supervisor.session,
434+
Option.some({ ...healthySession, client: silentClient }),
435+
);
436+
437+
const sendFiber = yield* startThreadTurn({
438+
commandId: CommandId.make("command-unanswered"),
439+
threadId: v2ThreadId,
440+
message: {
441+
messageId: MessageId.make("message-unanswered"),
442+
role: "user",
443+
text: "hello",
444+
attachments: [],
445+
},
446+
runtimeMode: "full-access",
447+
interactionMode: "default",
448+
}).pipe(
449+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
450+
Effect.flip,
451+
Effect.forkChild,
452+
);
453+
yield* TestClock.adjust("1 minute");
454+
455+
expect(sendFiber.pollUnsafe()).toBeDefined();
456+
expect((yield* Fiber.join(sendFiber))._tag).toBe("EnvironmentRpcUnavailableError");
457+
expect(commands.map((command) => command.commandId)).toEqual(["command-unanswered"]);
458+
}).pipe(Effect.provide(layerTestRandom)),
459+
);
460+
422461
it.effect("holds a lost message through reconnect backoff until disconnect", () =>
423462
Effect.gen(function* () {
424463
const commands: OrchestrationV2Command[] = [];

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ import * as Stream from "effect/Stream";
3939
import * as SubscriptionRef from "effect/SubscriptionRef";
4040

4141
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
42-
import { getInitialServerConfig, isRpcClientError, request } from "../rpc/client.ts";
42+
import {
43+
EnvironmentRpcUnavailableError,
44+
getInitialServerConfig,
45+
isRpcClientError,
46+
request,
47+
} from "../rpc/client.ts";
4348

4449
interface CommandMetadata {
4550
readonly commandId?: CommandId;
@@ -293,15 +298,31 @@ const dispatch = (command: OrchestrationV2Command) =>
293298
// stops a frame that kills the socket from looping.
294299
const MAX_LOST_TRANSPORT_RESENDS = 2;
295300

301+
// A live socket can still carry a command the host never answers; the heartbeat
302+
// alone would leave the send pending forever.
303+
const MESSAGE_DISPATCH_TIMEOUT = "60 seconds";
304+
296305
const dispatchMessage = Effect.fn("EnvironmentCommands.dispatchMessage")(function* (
297306
command: Extract<OrchestrationV2Command, { type: "message.dispatch" }>,
298307
resendAfterSocketLoss: boolean,
299308
) {
300-
if (!resendAfterSocketLoss) return yield* dispatch(command);
301309
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
310+
const attempt = dispatch(command).pipe(
311+
Effect.timeoutOrElse({
312+
duration: MESSAGE_DISPATCH_TIMEOUT,
313+
orElse: () =>
314+
Effect.fail(
315+
new EnvironmentRpcUnavailableError({
316+
environmentId: supervisor.target.environmentId,
317+
message: `${supervisor.target.label} did not confirm the message within 60 seconds.`,
318+
}),
319+
),
320+
}),
321+
);
322+
if (!resendAfterSocketLoss) return yield* attempt;
302323
for (let resends = 0; ; resends += 1) {
303324
const attemptSession = yield* SubscriptionRef.get(supervisor.session);
304-
const exit = yield* Effect.exit(dispatch(command));
325+
const exit = yield* Effect.exit(attempt);
305326
if (Exit.isSuccess(exit)) return exit.value;
306327
// Closing the session interrupts the request, which can beat the protocol error.
307328
const lostTransport =

0 commit comments

Comments
 (0)