Skip to content
Merged
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
19 changes: 18 additions & 1 deletion apps/server/src/provider/Layers/ClaudeAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ describe("ClaudeAdapterLive", () => {
);
});

it.effect("interruptTurn stops every live task before interrupting the turn", () => {
it.effect("interruptTurn settles every acknowledged live task before interrupting", () => {
const harness = makeHarness();
return Effect.gen(function* () {
const adapter = yield* ClaudeAdapter;
Expand Down Expand Up @@ -1568,11 +1568,28 @@ describe("ClaudeAdapterLive", () => {

yield* Fiber.join(taskEventsFiber);

const stoppedTaskEventFiber = yield* adapter.streamEvents.pipe(
Stream.filter((event) => event.type === "task.completed"),
Stream.take(1),
Stream.runCollect,
Effect.forkChild,
);
yield* adapter.interruptTurn(session.threadId);

// Only the still-live task is stopped; interrupt always fires after.
assert.deepEqual(harness.query.stopTaskCalls, ["task-live"]);
assert.equal(harness.query.interruptCalls.length, 1);

const stoppedTaskEvents = Array.from(yield* Fiber.join(stoppedTaskEventFiber));
assert.equal(stoppedTaskEvents.length, 1);
const stoppedTaskEvent = stoppedTaskEvents[0];
assert.equal(stoppedTaskEvent?.type, "task.completed");
if (stoppedTaskEvent?.type === "task.completed") {
assert.equal(String(stoppedTaskEvent.payload.taskId), "task-live");
assert.equal(stoppedTaskEvent.payload.status, "stopped");
assert.equal(stoppedTaskEvent.payload.taskType, "local_agent");
assert.equal(stoppedTaskEvent.payload.title, "Agent A");
}
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
Expand Down
40 changes: 35 additions & 5 deletions apps/server/src/provider/Layers/ClaudeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
Expand Down Expand Up @@ -4419,11 +4420,40 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
yield* Effect.forEach(
liveIds,
(taskId) =>
Effect.tryPromise({
// Invoke through the query object: SDK methods rely on `this`.
try: () => context.query.stopTask!(taskId),
catch: () => undefined,
}).pipe(Effect.timeoutOption("3 seconds"), Effect.ignore),
Effect.gen(function* () {
const stopAcknowledged = yield* Effect.tryPromise({
// Invoke through the query object: SDK methods rely on `this`.
try: () => context.query.stopTask!(taskId),
catch: () => undefined,
}).pipe(
Effect.timeoutOption("3 seconds"),
Effect.orElseSucceed(() => Option.none()),
);
if (Option.isNone(stopAcknowledged) || !context.liveTaskIds.delete(taskId)) {
return;
}

// stopTask only acknowledges the control request. Its separate
// task_notification can lose the race with interrupt(), so make
// the acknowledged stop authoritative for the durable UI state.
const stamp = yield* makeEventStamp();
yield* offerRuntimeEvent({
type: "task.completed",
eventId: stamp.eventId,
provider: PROVIDER,
createdAt: stamp.createdAt,
threadId: context.session.threadId,
...(context.turnState
? { turnId: asCanonicalTurnId(context.turnState.turnId) }
: {}),
payload: {
taskId: RuntimeTaskId.make(taskId),
status: "stopped",
...taskLinkageFor(context.taskAgents, taskId),
},
providerRefs: nativeProviderRefs(context),
});
Comment thread
t3dotgg marked this conversation as resolved.
}).pipe(Effect.ignore),
{ concurrency: 8, discard: true },
).pipe(Effect.timeoutOption("10 seconds"), Effect.ignore);
}
Expand Down
Loading