@@ -56,6 +56,7 @@ import {
5656} from "@t3tools/contracts";
5757import { mcpAppFromToolItem } from "@t3tools/shared/toolOutput";
5858import { assert, describe, it } from "@effect/vitest";
59+ import * as Cause from "effect/Cause";
5960import * as Context from "effect/Context";
6061import * as DateTime from "effect/DateTime";
6162import * as Deferred from "effect/Deferred";
@@ -2466,7 +2467,7 @@ describe("ClaudeAdapterV2 native fork", () => {
24662467 Effect.sync(() => {
24672468 openedQueries.push(input);
24682469 return {
2469- messages: Stream.empty ,
2470+ messages: Stream.never ,
24702471 offer: () => Effect.void,
24712472 setModel: () => Effect.void,
24722473 applyFlagSettings: () => Effect.void,
@@ -2808,7 +2809,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
28082809 Effect.sync(() => {
28092810 openedQueries.push(input);
28102811 return {
2811- messages: Stream.empty ,
2812+ messages: Stream.never ,
28122813 offer: () => Effect.void,
28132814 setModel: () => Effect.void,
28142815 applyFlagSettings: () => Effect.void,
@@ -10979,6 +10980,134 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1097910980 ),
1098010981 );
1098110982
10983+ const openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame = Effect.fnUntraced(function* (
10984+ name: string,
10985+ ) {
10986+ const fileSystem = yield* FileSystem.FileSystem;
10987+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
10988+ const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
10989+ prefix: `t3-claude-v2-${name}-`,
10990+ });
10991+ const deadStreamEnded = yield* Deferred.make<void>();
10992+ let opened = 0;
10993+ const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
10994+ instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
10995+ settings: DEFAULT_CLAUDE_SETTINGS,
10996+ environment: {},
10997+ attachmentsDir,
10998+ // startTurn reads the auto-compact window from settings between opening the query and
10999+ // attaching the turn; holding that read until the CLI's stream has ended pins the race.
11000+ fileSystem: {
11001+ ...fileSystem,
11002+ readFileString: (filePath, encoding) =>
11003+ opened === 1 && filePath.endsWith("settings.json")
11004+ ? Deferred.await(deadStreamEnded).pipe(
11005+ Effect.andThen(fileSystem.readFileString(filePath, encoding)),
11006+ )
11007+ : fileSystem.readFileString(filePath, encoding),
11008+ },
11009+ path: yield* Path.Path,
11010+ random: yield* SecureRandom.SecureRandom,
11011+ idAllocator,
11012+ continuationRequests: { offer: () => Effect.void },
11013+ queryRunner: {
11014+ allocateSessionId: Effect.succeed(`native-${name}`),
11015+ open: () =>
11016+ Effect.gen(function* () {
11017+ opened += 1;
11018+ const sdkMessages = yield* Queue.unbounded<SDKMessage>();
11019+ return {
11020+ // The first CLI exits without writing a frame.
11021+ messages:
11022+ opened === 1
11023+ ? Stream.empty.pipe(Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined)))
11024+ : Stream.fromQueue(sdkMessages),
11025+ offer: () => Effect.void,
11026+ setModel: () => Effect.void,
11027+ applyFlagSettings: () => Effect.void,
11028+ setPermissionMode: () => Effect.void,
11029+ backgroundTasks: () => Effect.succeed(false),
11030+ mcpServerStatus: Effect.succeed([]),
11031+ readMcpResource: () => Effect.die("unexpected MCP resource read"),
11032+ callMcpTool: () => Effect.die("unexpected MCP tool call"),
11033+ listPermissionRules: Effect.die("unexpected permission rules read"),
11034+ stopTask: () => Effect.void,
11035+ interrupt: Effect.void,
11036+ close: Queue.shutdown(sdkMessages),
11037+ };
11038+ }),
11039+ forkSession: () => Effect.die("unused forkSession"),
11040+ subagentLaunchToolUseId: () => Effect.succeed(null),
11041+ assertComplete: Effect.void,
11042+ },
11043+ });
11044+ const threadId = ThreadId.make(`thread-${name}`);
11045+ const runtime = yield* adapter.openSession({
11046+ threadId,
11047+ providerSessionId: ProviderSessionId.make(`provider-session-${name}`),
11048+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
11049+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
11050+ });
11051+ const providerThread = yield* runtime.ensureThread({
11052+ threadId,
11053+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
11054+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
11055+ });
11056+ const now = yield* DateTime.now;
11057+ const turnInput = (attempt: string, providerTurnOrdinal: number) =>
11058+ makeClaudeTestTurnInput({
11059+ threadId,
11060+ providerThread,
11061+ now,
11062+ attemptId: RunAttemptId.make(`attempt-${name}-${attempt}`),
11063+ text: "Continue with the plan.",
11064+ attachments: [],
11065+ providerTurnOrdinal,
11066+ });
11067+ return { runtime, turnInput };
11068+ });
11069+
11070+ it.effect("fails a turn whose Claude query ends before its first frame", () =>
11071+ Effect.scoped(
11072+ Effect.gen(function* () {
11073+ const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame(
11074+ "claude-query-dies-before-frame",
11075+ );
11076+ const started = yield* harness.runtime
11077+ .startTurn(harness.turnInput("a", 1))
11078+ .pipe(Effect.exit);
11079+ assert.isTrue(
11080+ Exit.isFailure(started),
11081+ "a turn whose Claude query ended before its first frame started running on the dead query",
11082+ );
11083+ if (Exit.isFailure(started)) {
11084+ assert.include(
11085+ Cause.pretty(started.cause),
11086+ "Claude provider query ended before the turn could start.",
11087+ );
11088+ }
11089+ const followUp = yield* harness.runtime
11090+ .startTurn(harness.turnInput("b", 2))
11091+ .pipe(Effect.exit);
11092+ assert.isTrue(
11093+ Exit.isSuccess(followUp),
11094+ `the turn whose query ended before its first frame is still active: ${
11095+ Exit.isFailure(followUp) ? Cause.pretty(followUp.cause) : ""
11096+ }`,
11097+ );
11098+ }).pipe(
11099+ Effect.provide(
11100+ Layer.mergeAll(
11101+ IdAllocator.layer,
11102+ McpProviderSessions.layer,
11103+ NodeServices.layer,
11104+ NodeCrypto.layer,
11105+ ),
11106+ ),
11107+ ),
11108+ ),
11109+ );
11110+
1098211111 it.effect(
1098311112 "clears the replaced sibling native thread roster when openQuery switches processes",
1098411113 () =>
0 commit comments