Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 296 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {

const makeWakeHarnessWithOptions = (options?: {
readonly close?: (sdkMessages: Queue.Queue<SDKMessage>) => Effect.Effect<void>;
readonly interrupt?: Effect.Effect<void>;
readonly interrupt?: Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"];
readonly stopTask?: (
taskId: string,
Expand Down Expand Up @@ -3454,6 +3454,301 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);

it.effect.each(["hang", "error"] as const)(
"closes the query and permits another turn when Claude interrupt returns %s",
(behavior) =>
Effect.gen(function* () {
const interruptStarted = yield* Deferred.make<void>();
const queryClosed = yield* Deferred.make<void>();
const harness = yield* makeWakeHarnessWithOptions({
interrupt: Deferred.succeed(interruptStarted, undefined).pipe(
Effect.andThen(
behavior === "hang"
? Effect.never
: Effect.fail(
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
method: "interrupt",
cause: new Error("Claude transport stopped responding"),
}),
),
),
),
close: (sdkMessages) =>
Queue.shutdown(sdkMessages).pipe(
Effect.andThen(Deferred.succeed(queryClosed, undefined)),
),
});
const attemptId = RunAttemptId.make("attempt-claude-unresponsive-interrupt");
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
nativeTurnId: `turn:${attemptId}`,
});
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId,
text: "Stop this task.",
attachments: [],
}),
);
const interrupt = yield* harness.runtime
.interruptTurn({ providerThread: harness.providerThread, providerTurnId })
.pipe(Effect.forkScoped);
yield* Deferred.await(interruptStarted);
yield* TestClock.adjust("5 seconds");
yield* Deferred.await(queryClosed);
yield* Fiber.join(interrupt);
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "interrupted");
assert.lengthOf(harness.terminalEvents(), 1);
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("attempt-claude-after-unresponsive-interrupt"),
providerTurnOrdinal: 2,
text: "Start fresh.",
attachments: [],
}),
);
assert.lengthOf(harness.offeredMessages, 2);
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
TestClock.layer(),
),
),
),
);

it.effect.each(["close", "stream"] as const)(
"terminalizes Stop when Claude %s shutdown does not finish",
(behavior) =>
Effect.gen(function* () {
const closeStarted = yield* Deferred.make<void>();
const closeGate = yield* Deferred.make<void>();
const harness = yield* makeWakeHarnessWithOptions({
close: () =>
Deferred.succeed(closeStarted, undefined).pipe(
Effect.andThen(behavior === "close" ? Deferred.await(closeGate) : Effect.void),
),
});
const attemptId = RunAttemptId.make("attempt-claude-unresponsive-close");
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
nativeTurnId: `turn:${attemptId}`,
});
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId,
text: "Stop this task.",
attachments: [],
}),
);
const interrupt = yield* harness.runtime
.interruptTurn({ providerThread: harness.providerThread, providerTurnId })
.pipe(Effect.forkScoped);
yield* Deferred.await(closeStarted);
yield* TestClock.adjust("10 seconds");
yield* Fiber.join(interrupt);
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "interrupted");
assert.lengthOf(harness.terminalEvents(), 1);
yield* Deferred.succeed(closeGate, undefined);
yield* Queue.shutdown(harness.sdkMessages);
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
TestClock.layer(),
),
),
),
);

it.effect.each([{ status: "compacting" }, { status: "requesting" }] as const)(
"terminalizes a stalled Claude $status phase and marks its session invalid",
(input) =>
Effect.gen(function* () {
const closeRequested = yield* Deferred.make<void>();
const harness = yield* makeWakeHarnessWithOptions({
close: (sdkMessages) =>
Deferred.succeed(closeRequested, undefined).pipe(
Effect.andThen(Queue.shutdown(sdkMessages)),
),
});
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make(`attempt-claude-stalled-${input.status}`),
text: input.status === "compacting" ? "/compact" : "Continue.",
attachments: [],
}),
);
yield* harness.offerAndWait(
claudeSdkFrame({
type: "system",
subtype: "status",
status: input.status,
uuid: `status-${input.status}`,
session_id: WAKE_NATIVE_SESSION,
}),
);

yield* TestClock.adjust("2 minutes");
yield* Deferred.await(closeRequested);
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "failed");
if (terminal.status === "failed") {
assert.equal(terminal.failure.code, "native_session_stalled");
assert.equal(terminal.failure.retryable, true);
}
assert.lengthOf(harness.terminalEvents(), 1);
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
TestClock.layer(),
),
),
),
);

it.effect("does not let rate-limit telemetry extend a stalled request deadline", () =>
Effect.gen(function* () {
const closeRequested = yield* Deferred.make<void>();
const harness = yield* makeWakeHarnessWithOptions({
close: (sdkMessages) =>
Deferred.succeed(closeRequested, undefined).pipe(
Effect.andThen(Queue.shutdown(sdkMessages)),
),
});
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("attempt-claude-stalled-through-rate-limit-event"),
text: "Continue.",
attachments: [],
}),
);
yield* harness.offerAndWait(
claudeSdkFrame({
type: "system",
subtype: "status",
status: "requesting",
uuid: "status-requesting-before-rate-limit-event",
session_id: WAKE_NATIVE_SESSION,
}),
);
yield* TestClock.adjust("1 minute");
yield* harness.offerAndWait(
claudeSdkFrame({
type: "rate_limit_event",
rate_limit_info: { status: "allowed" },
uuid: "00000000-0000-4000-8000-0000000002f3",
session_id: WAKE_NATIVE_SESSION,
}),
);

yield* TestClock.adjust("1 minute");
assert.isTrue(yield* Deferred.isDone(closeRequested));
yield* Deferred.await(closeRequested);
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "failed");
if (terminal.status === "failed") {
assert.equal(terminal.failure.code, "native_session_stalled");
}
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
TestClock.layer(),
),
),
),
);

it.effect("keeps a completed Claude compaction from tripping the stall timeout", () =>
Effect.gen(function* () {
const closeRequested = yield* Deferred.make<void>();
const harness = yield* makeWakeHarnessWithOptions({
close: () => Deferred.succeed(closeRequested, undefined),
});
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("attempt-claude-completed-compaction-timeout"),
text: "/compact",
attachments: [],
}),
);
yield* harness.offerAndWait(
claudeSdkFrame({
type: "system",
subtype: "status",
status: "compacting",
uuid: "status-completing-compaction",
session_id: WAKE_NATIVE_SESSION,
}),
);
yield* TestClock.adjust("1 minute");
yield* harness.offerAndWait(
claudeSdkFrame({
type: "system",
subtype: "compact_boundary",
compact_metadata: { trigger: "manual", pre_tokens: 1500, post_tokens: 400 },
uuid: "00000000-0000-4000-8000-0000000002f1",
session_id: WAKE_NATIVE_SESSION,
}),
);
yield* TestClock.adjust("2 minutes");
assert.isFalse(yield* Deferred.isDone(closeRequested));
yield* harness.offerAndWait(
makeResultFrame({
uuid: "00000000-0000-4000-8000-0000000002f2",
result: "Compacted conversation.",
}),
);
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "completed");
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
TestClock.layer(),
),
),
),
);

it.effect.each(["completed", "interrupted"] as const)(
"projects Claude thinking blocks when %s",
(status) =>
Expand Down
Loading
Loading