Skip to content

Commit 44fcde8

Browse files
fix(server): also interrupt open tool calls when a turn fails to start
A turn that fails to start is failed the same way as one whose provider stream is lost: no provider reports its end. Tool calls it reported before failing stayed running. Pass them to the final run write here too.
1 parent 69fe4e5 commit 44fcde8

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎apps/server/src/orchestration-v2/RunExecutionService.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,6 +3535,43 @@ it.effect("refreshes pull requests only once when startup failure closes its eve
35353535
}),
35363536
);
35373537

3538+
it.effect("interrupts a tool call reported before the turn fails to start", () =>
3539+
Effect.gen(function* () {
3540+
const key = "start-failure-open-tool";
3541+
const toolReported = yield* Deferred.make<void>();
3542+
const { written } = yield* captureRootRunTermination({
3543+
key,
3544+
shouldFinalizeRun: () => Effect.succeed(true),
3545+
events: (ids) =>
3546+
Stream.concat(
3547+
Stream.fromIterable([backgroundTurnItemEvent(ids, "dynamic_tool", "running", 1)]),
3548+
Stream.unwrap(Deferred.succeed(toolReported, undefined).pipe(Effect.as(Stream.never))),
3549+
),
3550+
startTurn: (input) =>
3551+
Deferred.await(toolReported).pipe(
3552+
Effect.andThen(
3553+
Effect.fail(
3554+
new ProviderAdapter.ProviderAdapterTurnStartError({
3555+
driver,
3556+
threadId: input.threadId,
3557+
providerThreadId: input.providerThread.id,
3558+
runId: input.runId,
3559+
cause: "provider failed after reporting a tool call",
3560+
}),
3561+
),
3562+
),
3563+
),
3564+
});
3565+
assert.deepEqual(
3566+
written.map((item) => [item.type, item.status]),
3567+
[
3568+
["dynamic_tool", "interrupted"],
3569+
["error", "failed"],
3570+
],
3571+
);
3572+
}),
3573+
);
3574+
35383575
it.effect("keeps completed runs completed when pull request refresh fails", () =>
35393576
Effect.gen(function* () {
35403577
const { observed } = yield* captureRootRunTermination({

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ function emptyOpenRunToolCalls(): OpenRunToolCalls {
333333
}
334334

335335
/**
336-
* Ends the tool calls a run left open when its provider event stream was lost,
337-
* such as when a provider switch released the session mid-turn. No process is
336+
* Ends the tool calls a run left open when the run fails without its provider
337+
* ending the turn: the event stream was lost, such as when a provider switch
338+
* released the session mid-turn, or the turn failed to start. No process is
338339
* left to report how they ended, so they would otherwise stay running. They
339340
* were cut short, not failed: a call such as t3_thread_configure may have done
340341
* its work before the session went away.
@@ -632,7 +633,7 @@ export const layer: Layer.Layer<
632633
readonly shouldFinalizeRun?: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
633634
readonly hasUnpairedRunInterruptRequest?: () => Effect.Effect<boolean, never>;
634635
readonly openRunOwnedSubagents?: OpenRunOwnedSubagentProjection;
635-
/** Set only when the provider stream was lost before the turn ended. */
636+
/** Set only when the run fails without its provider ending the turn. */
636637
readonly abandonedToolCalls?: OpenRunToolCalls;
637638
readonly terminal: ProviderTerminalEvent;
638639
readonly failureItemPersisted: boolean;
@@ -1539,8 +1540,8 @@ export const layer: Layer.Layer<
15391540
Effect.flatMap((providerThread) =>
15401541
Ref.get(latestTurnItemOrdinal).pipe(
15411542
Effect.flatMap((latestItemOrdinal) =>
1542-
Ref.get(openRunOwnedSubagents).pipe(
1543-
Effect.flatMap((openSubagents) =>
1543+
Effect.all([Ref.get(openRunOwnedSubagents), Ref.get(openToolCalls)]).pipe(
1544+
Effect.flatMap(([openSubagents, abandonedToolCalls]) =>
15441545
writeFinalRunEvents({
15451546
run: input.run,
15461547
rootNode: input.rootNode,
@@ -1554,6 +1555,7 @@ export const layer: Layer.Layer<
15541555
expectedStatus: "running",
15551556
},
15561557
openRunOwnedSubagents: openSubagents,
1558+
abandonedToolCalls,
15571559
terminal: makeFailedTerminalEvent(
15581560
makeProviderFailure({
15591561
cause: Cause.squash(cause),

0 commit comments

Comments
 (0)