@@ -10502,6 +10502,165 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1050210502 ),
1050310503 );
1050410504
10505+ it.effect("interrupts a stopped subagent when the replacement process fails to open", () =>
10506+ Effect.scoped(
10507+ Effect.gen(function* () {
10508+ const SUBAGENT_TASK_ID = "task-replace-open-fail-subagent";
10509+ const fileSystem = yield* FileSystem.FileSystem;
10510+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
10511+ const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
10512+ prefix: "t3-claude-v2-replace-open-fail-subagent-",
10513+ });
10514+ let openCount = 0;
10515+ const processQueues: Array<Queue.Queue<SDKMessage>> = [];
10516+ const events: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
10517+ const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
10518+ instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
10519+ settings: DEFAULT_CLAUDE_SETTINGS,
10520+ environment: {},
10521+ attachmentsDir,
10522+ fileSystem,
10523+ path: yield* Path.Path,
10524+ crypto: yield* Crypto.Crypto,
10525+ idAllocator,
10526+ continuationRequests: { offer: () => Effect.void },
10527+ queryRunner: {
10528+ allocateSessionId: Effect.succeed(WAKE_NATIVE_SESSION),
10529+ open: () => {
10530+ openCount += 1;
10531+ if (openCount === 2) {
10532+ return Effect.fail(
10533+ new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
10534+ method: "open",
10535+ cause: "forced replacement open failure",
10536+ }),
10537+ );
10538+ }
10539+ return Effect.gen(function* () {
10540+ const sdkMessages = yield* Queue.unbounded<SDKMessage>();
10541+ processQueues.push(sdkMessages);
10542+ return {
10543+ messages: Stream.fromQueue(sdkMessages),
10544+ offer: () => Effect.void,
10545+ setModel: () => Effect.void,
10546+ setPermissionMode: () => Effect.void,
10547+ stopTask: () => Effect.void,
10548+ interrupt: Effect.void,
10549+ close: Queue.shutdown(sdkMessages),
10550+ };
10551+ });
10552+ },
10553+ forkSession: () => Effect.die("unused forkSession"),
10554+ subagentLaunchToolUseId: () => Effect.succeed(null),
10555+ assertComplete: Effect.void,
10556+ },
10557+ });
10558+ const threadId = ThreadId.make("thread-claude-replace-open-fail-subagent");
10559+ const runtime = yield* adapter.openSession({
10560+ threadId,
10561+ providerSessionId: ProviderSessionId.make(
10562+ "provider-session-claude-replace-open-fail-subagent",
10563+ ),
10564+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
10565+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
10566+ });
10567+ const providerThread = yield* runtime.ensureThread({
10568+ threadId,
10569+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
10570+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
10571+ });
10572+ yield* runtime.events.pipe(
10573+ Stream.runForEach((event) =>
10574+ Effect.sync(() => {
10575+ events.push(event);
10576+ }),
10577+ ),
10578+ Effect.forkScoped,
10579+ );
10580+ const terminals = () => events.filter((event) => event.type === "turn.terminal");
10581+ const now = yield* DateTime.now;
10582+
10583+ yield* runtime.startTurn(
10584+ makeClaudeTestTurnInput({
10585+ threadId,
10586+ providerThread,
10587+ now,
10588+ attemptId: RunAttemptId.make("attempt-claude-replace-open-fail-subagent-a"),
10589+ text: "Spawn a background subagent and stop.",
10590+ attachments: [],
10591+ }),
10592+ );
10593+ yield* Queue.offer(
10594+ processQueues[0]!,
10595+ claudeSdkFrame({
10596+ type: "system",
10597+ subtype: "task_started",
10598+ task_id: SUBAGENT_TASK_ID,
10599+ tool_use_id: "toolu-replace-open-fail-subagent",
10600+ description: "Background research",
10601+ subagent_type: "general-purpose",
10602+ task_type: "local_agent",
10603+ prompt: "Research, then report.",
10604+ uuid: "00000000-0000-4000-8000-000000000911",
10605+ session_id: WAKE_NATIVE_SESSION,
10606+ }),
10607+ );
10608+ yield* Queue.offer(
10609+ processQueues[0]!,
10610+ makeResultFrame({
10611+ uuid: "00000000-0000-4000-8000-000000000912",
10612+ result: "Spawned the subagent in the background.",
10613+ }),
10614+ );
10615+ yield* awaitUntil(() => terminals().length === 1, "first turn terminal");
10616+
10617+ yield* runtime.interruptTurn({
10618+ providerThread,
10619+ providerTurnId: terminals()[0]!.providerTurnId,
10620+ requestRuntimeRestart: true,
10621+ });
10622+ const failedStart = yield* runtime
10623+ .startTurn(
10624+ makeClaudeTestTurnInput({
10625+ threadId,
10626+ providerThread: { ...providerThread, status: "active" },
10627+ now,
10628+ attemptId: RunAttemptId.make("attempt-claude-replace-open-fail-subagent-b"),
10629+ text: "Switch model after Stop.",
10630+ attachments: [],
10631+ providerTurnOrdinal: 2,
10632+ modelSelection: {
10633+ ...CLAUDE_TEST_MODEL_SELECTION,
10634+ model: "claude-haiku-4-5-20251001",
10635+ },
10636+ }),
10637+ )
10638+ .pipe(Effect.exit);
10639+ assert.isTrue(Exit.isFailure(failedStart));
10640+ assert.equal(openCount, 2);
10641+
10642+ // The first process is closed and no replacement started, so nothing
10643+ // will ever report the subagent's end.
10644+ const subagentUpdates = () =>
10645+ events.flatMap((event) =>
10646+ event.type === "subagent.updated" &&
10647+ event.subagent.nativeTaskRef?.nativeId === SUBAGENT_TASK_ID
10648+ ? [event.subagent]
10649+ : [],
10650+ );
10651+ yield* awaitUntil(
10652+ () => subagentUpdates().at(-1)?.status === "interrupted",
10653+ "orphaned subagent interrupted",
10654+ );
10655+ assert.equal(subagentUpdates().at(-1)?.runId, subagentUpdates()[0]?.runId);
10656+ }).pipe(
10657+ Effect.provide(
10658+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
10659+ ),
10660+ ),
10661+ ),
10662+ );
10663+
1050510664 it.effect(
1050610665 "keeps the process and its roster when a model change meets a running background shell",
1050710666 () =>
0 commit comments