Skip to content

Commit d556521

Browse files
test(server): reproduce #30052, cancelled queued Claude continuation drops later wakes
A wake offered while another run is active becomes a queued continuation run. Cancelling it with queued-run.cancel never reaches the Claude adapter, so its requestedContinuations latch stays set and the next wake on that native thread offers no continuation. These tests reproduce #30052. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cefb544 commit d556521

1 file changed

Lines changed: 238 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts‎

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type OrchestrationV2DomainEvent,
1616
} from "@t3tools/contracts";
1717
import * as DateTime from "effect/DateTime";
18+
import * as Deferred from "effect/Deferred";
1819
import * as SecureRandom from "effect/crypto/SecureRandom";
1920
import * as Effect from "effect/Effect";
2021
import * as FileSystem from "effect/FileSystem";
@@ -36,6 +37,7 @@ import * as EffectWorker from "./EffectWorker.ts";
3637
import * as EventSink from "./EventSink.ts";
3738
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3839
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
40+
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
3941
import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
4042
import * as Orchestrator from "./Orchestrator.ts";
4143
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
@@ -490,3 +492,239 @@ it.effect.each(deliveries)(
490492
),
491493
),
492494
);
495+
496+
const backgroundTaskStarted = (taskId: string) =>
497+
frame({
498+
type: "system",
499+
subtype: "task_started",
500+
task_id: taskId,
501+
tool_use_id: `tool-${taskId}`,
502+
description: "Background build",
503+
is_backgrounded: true,
504+
task_type: "local_bash",
505+
uuid: `started-${taskId}`,
506+
session_id: sessionId,
507+
});
508+
509+
const backgroundTaskNotification = (taskId: string) =>
510+
frame({
511+
type: "system",
512+
subtype: "task_notification",
513+
task_id: taskId,
514+
tool_use_id: `tool-${taskId}`,
515+
status: "completed",
516+
output_file: `/tmp/claude-replay/tasks/${taskId}.output`,
517+
summary: `Background build ${taskId} completed`,
518+
uuid: `notification-${taskId}`,
519+
session_id: sessionId,
520+
});
521+
522+
const wakeAssistant = (uuid: string) =>
523+
frame({
524+
type: "assistant",
525+
uuid,
526+
session_id: sessionId,
527+
parent_tool_use_id: null,
528+
message: {
529+
id: `msg-${uuid}`,
530+
role: "assistant",
531+
type: "message",
532+
model: modelSelection.model,
533+
content: [{ type: "text", text: "Background build finished." }],
534+
stop_reason: null,
535+
stop_sequence: null,
536+
usage: { input_tokens: 1, output_tokens: 1 },
537+
},
538+
});
539+
540+
it.effect("offers a continuation for a later wake after a queued continuation is cancelled", () =>
541+
Effect.scoped(
542+
Effect.gen(function* () {
543+
const cwd = yield* checkpointWorkspace("claude-cancelled-continuation");
544+
const sdkMessages = yield* Queue.unbounded<SDKMessage>();
545+
const processed = new WeakMap<SDKMessage, Deferred.Deferred<void>>();
546+
const offerAndWait = Effect.fnUntraced(function* (message: SDKMessage) {
547+
const done = yield* Deferred.make<void>();
548+
processed.set(message, done);
549+
yield* Queue.offer(sdkMessages, message);
550+
yield* Deferred.await(done);
551+
});
552+
const fileSystem = yield* FileSystem.FileSystem;
553+
const path = yield* Path.Path;
554+
const random = yield* SecureRandom.SecureRandom;
555+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
556+
const continuations: ProviderContinuationRequests.ProviderContinuationRequest[] = [];
557+
const services =
558+
yield* Effect.context<
559+
Effect.Services<ReturnType<typeof ClaudeAdapterV2.makeClaudeAdapterV2>>
560+
>();
561+
const registry = ProviderAdapterRegistry.layerFromAdaptersEffect(
562+
Effect.gen(function* () {
563+
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
564+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
565+
instanceId: modelSelection.instanceId,
566+
settings,
567+
environment: {},
568+
attachmentsDir: cwd,
569+
fileSystem,
570+
path,
571+
random,
572+
idAllocator,
573+
continuationRequests: {
574+
offer: (request) =>
575+
Effect.sync(() => continuations.push(request)).pipe(
576+
Effect.andThen(requests.offer(request)),
577+
),
578+
},
579+
queryRunner: {
580+
allocateSessionId: Effect.succeed(sessionId),
581+
open: () =>
582+
Effect.succeed({
583+
// Resolve each frame's Deferred once the adapter pulls the next one.
584+
messages: Stream.fromQueue(sdkMessages).pipe(
585+
Stream.flatMap((message) =>
586+
Stream.make(message).pipe(
587+
Stream.concat(
588+
Stream.fromEffect(
589+
Effect.suspend(() => {
590+
const done = processed.get(message);
591+
return done === undefined
592+
? Effect.void
593+
: Deferred.succeed(done, undefined);
594+
}),
595+
).pipe(Stream.drain),
596+
),
597+
),
598+
),
599+
),
600+
offer: () => Effect.void,
601+
setModel: () => Effect.void,
602+
applyFlagSettings: () => Effect.void,
603+
setPermissionMode: () => Effect.void,
604+
backgroundTasks: () => Effect.succeed(false),
605+
stopTask: () => Effect.void,
606+
interrupt: Effect.void,
607+
close: Effect.void,
608+
}),
609+
forkSession: () => Effect.die("unused"),
610+
subagentLaunchToolUseId: () => Effect.succeed(null),
611+
assertComplete: Effect.void,
612+
},
613+
}).pipe(Effect.provideContext(services));
614+
return [adapter];
615+
}),
616+
);
617+
yield* Effect.gen(function* () {
618+
const orchestrator = yield* Orchestrator.OrchestratorV2;
619+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
620+
const threadId = ThreadId.make("thread:cancelled-continuation");
621+
const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
622+
orchestrator.streamDomainEvents.pipe(
623+
Stream.filter(predicate),
624+
Stream.take(1),
625+
Stream.runDrain,
626+
Effect.forkScoped,
627+
);
628+
const runUpdated = (messageId: string, status: string) =>
629+
watch(
630+
(event) =>
631+
event.type === "run.updated" &&
632+
event.payload.userMessageId === messageId &&
633+
event.payload.status === status,
634+
);
635+
// A user turn that leaves a background task running when it ends.
636+
const userTurnWithBackgroundTask = Effect.fnUntraced(function* (
637+
id: string,
638+
taskId: string,
639+
) {
640+
const running = yield* runUpdated(id, "running");
641+
yield* orchestrator.dispatch({
642+
type: "message.dispatch",
643+
commandId: CommandId.make(id),
644+
threadId,
645+
messageId: MessageId.make(id),
646+
text: "Run the build in the background.",
647+
attachments: [],
648+
dispatchMode: { type: "start_immediately" },
649+
createdBy: "user",
650+
creationSource: "web",
651+
});
652+
yield* worker.drain();
653+
yield* Fiber.join(running);
654+
const waiting = yield* runUpdated(id, "waiting");
655+
yield* Queue.offer(sdkMessages, backgroundTaskStarted(taskId));
656+
yield* Queue.offer(sdkMessages, result(`result-${id}`, false));
657+
yield* Fiber.join(waiting);
658+
});
659+
yield* orchestrator.dispatch({
660+
type: "thread.create",
661+
commandId: CommandId.make("create"),
662+
threadId,
663+
projectId: ProjectId.make("project:cancelled-continuation"),
664+
title: "Cancelled continuation",
665+
modelSelection,
666+
runtimeMode: "full-access",
667+
interactionMode: "default",
668+
branch: null,
669+
worktreePath: cwd,
670+
createdBy: "user",
671+
creationSource: "web",
672+
});
673+
674+
// The first run is still finalizing when its background task wakes Claude.
675+
yield* userTurnWithBackgroundTask("first", "build-1");
676+
const queued = yield* watch(
677+
(event) => event.type === "run.created" && event.payload.status === "queued",
678+
);
679+
yield* offerAndWait(backgroundTaskNotification("build-1"));
680+
yield* offerAndWait(wakeAssistant("wake-1"));
681+
yield* Fiber.join(queued);
682+
assert.lengthOf(continuations, 1);
683+
const continuationRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
684+
(run) => run.status === "queued",
685+
);
686+
assert.ok(continuationRun !== undefined);
687+
yield* orchestrator.dispatch({
688+
type: "queued-run.cancel",
689+
commandId: CommandId.make("cancel"),
690+
threadId,
691+
runId: continuationRun.id,
692+
});
693+
yield* offerAndWait(result("wake-1-result", false));
694+
yield* worker.drain();
695+
yield* orchestrator.resumeQueuedRuns;
696+
yield* worker.drain();
697+
const idle = yield* orchestrator.getThreadProjection(threadId);
698+
assert.equal(idle.runs.find((run) => run.id === continuationRun.id)?.status, "cancelled");
699+
assert.isFalse(idle.runs.some((run) => run.status === "queued"));
700+
701+
yield* userTurnWithBackgroundTask("second", "build-2");
702+
yield* worker.drain();
703+
yield* offerAndWait(backgroundTaskNotification("build-2"));
704+
yield* offerAndWait(wakeAssistant("wake-2"));
705+
assert.lengthOf(
706+
continuations,
707+
2,
708+
"the wake after a cancelled queued continuation must offer a new continuation",
709+
);
710+
}).pipe(
711+
Effect.provide(
712+
ProviderReplayHarness.layerWithRegistry(
713+
{ name: "claude-cancelled-continuation" },
714+
registry,
715+
{ runEffectWorker: false, runContinuationWorker: true },
716+
),
717+
),
718+
);
719+
}).pipe(
720+
Effect.provide(
721+
Layer.mergeAll(
722+
IdAllocator.layer,
723+
McpProviderSessions.layer,
724+
NodeServices.layer,
725+
NodeCrypto.layer,
726+
),
727+
),
728+
),
729+
),
730+
);

0 commit comments

Comments
 (0)