Skip to content

Commit 44c3d3c

Browse files
committed
fix(server): settle planned detaches after one native interrupt
1 parent a46b4b2 commit 44c3d3c

2 files changed

Lines changed: 37 additions & 53 deletions

File tree

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

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,7 +2896,7 @@ it.effect("does not refresh pull requests for auxiliary or stale provider termin
28962896
);
28972897

28982898
it.effect.each(["pending", "running"] as const)(
2899-
"interrupts open tools and the buffered %s native turn before detach settles",
2899+
"settles open tools after the session manager interrupts the buffered %s native turn",
29002900
(status) =>
29012901
Effect.gen(function* () {
29022902
const key = "workspace-detach-open-tools";
@@ -2931,7 +2931,18 @@ it.effect.each(["pending", "running"] as const)(
29312931
shouldFinalizeRun: () => Effect.succeed(true),
29322932
readAhead: true,
29332933
interruptTurn: ({ providerTurnId }) =>
2934-
Ref.update(interrupted, (current) => [...current, providerTurnId]),
2934+
Ref.update(interrupted, (current) => [...current, providerTurnId]).pipe(
2935+
Effect.andThen(
2936+
Effect.fail(
2937+
new ProviderAdapter.ProviderAdapterInterruptError({
2938+
driver,
2939+
providerThreadId: ids.providerThreadId,
2940+
providerTurnId,
2941+
cause: "native turn is already inactive after session-manager detach",
2942+
}),
2943+
),
2944+
),
2945+
),
29352946
beforeTerminalWrite: () =>
29362947
Ref.update(interrupted, (current) => [...current, "terminal" as const]).pipe(
29372948
Effect.as(true),
@@ -2972,13 +2983,22 @@ it.effect.each(["pending", "running"] as const)(
29722983
node(completedId, "completed"),
29732984
backgroundTurnItemEvent(ids, "dynamic_tool", "completed", 3, completedId),
29742985
]),
2975-
Stream.fail(
2976-
new ProviderAdapter.ProviderAdapterSessionDetached({
2977-
driver,
2978-
providerSessionId: ProviderSessionId.make(`session:${key}`),
2979-
threadId: ids.threadId,
2980-
reason: "workspace_changed",
2981-
}),
2986+
Stream.unwrap(
2987+
Ref.update(interrupted, (current) => [
2988+
...current,
2989+
ProviderTurnId.make(`${ids.rootProviderTurnId}:continuation`),
2990+
]).pipe(
2991+
Effect.as(
2992+
Stream.fail(
2993+
new ProviderAdapter.ProviderAdapterSessionDetached({
2994+
driver,
2995+
providerSessionId: ProviderSessionId.make(`session:${key}`),
2996+
threadId: ids.threadId,
2997+
reason: "workspace_changed",
2998+
}),
2999+
),
3000+
),
3001+
),
29823002
),
29833003
),
29843004
});
@@ -3154,13 +3174,7 @@ it.effect("tells the user why /compact failed on a provider without compaction",
31543174
}),
31553175
);
31563176

3157-
it.effect.each([
3158-
"failure",
3159-
"defect",
3160-
"interrupt-failure",
3161-
"ownership-lost",
3162-
"detach-interrupt-failure",
3163-
] as const)(
3177+
it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as const)(
31643178
"stops the live provider and background work before settling an ingestion %s",
31653179
(failureMode) =>
31663180
Effect.gen(function* () {
@@ -3201,19 +3215,6 @@ it.effect.each([
32013215
}),
32023216
),
32033217
),
3204-
).pipe(
3205-
Stream.concat(
3206-
failureMode === "detach-interrupt-failure"
3207-
? Stream.fail(
3208-
new ProviderAdapter.ProviderAdapterSessionDetached({
3209-
driver,
3210-
providerSessionId: ProviderSessionId.make(`session:ingestion:${failureMode}`),
3211-
threadId: ids.threadId,
3212-
reason: "workspace_changed",
3213-
}),
3214-
)
3215-
: Stream.empty,
3216-
),
32173218
),
32183219
ingestNormalized: () => {
32193220
const error = new ProviderEventIngestor.ProviderEventPublishError({
@@ -3227,24 +3228,18 @@ it.effect.each([
32273228
}),
32283229
),
32293230
});
3230-
return failureMode === "detach-interrupt-failure"
3231-
? Effect.succeed([])
3232-
: failureMode === "defect"
3233-
? Effect.die(error)
3234-
: Effect.fail(error);
3231+
return failureMode === "defect" ? Effect.die(error) : Effect.fail(error);
32353232
},
32363233
interruptTurn: (input) =>
32373234
Ref.update(interrupts, (current) => [...current, input]).pipe(
32383235
Effect.andThen(
3239-
input.requestRuntimeRestart === true &&
3240-
failureMode !== "interrupt-failure" &&
3241-
failureMode !== "detach-interrupt-failure"
3236+
input.requestRuntimeRestart === true && failureMode !== "interrupt-failure"
32423237
? Ref.set(backgroundWorkRunning, false)
32433238
: Effect.void,
32443239
),
32453240
Effect.andThen(Deferred.succeed(stopped, undefined)),
32463241
Effect.andThen(
3247-
failureMode === "interrupt-failure" || failureMode === "detach-interrupt-failure"
3242+
failureMode === "interrupt-failure"
32483243
? Effect.fail(
32493244
new ProviderAdapter.ProviderAdapterInterruptError({
32503245
driver,
@@ -3268,18 +3263,17 @@ it.effect.each([
32683263
} else {
32693264
assert.equal(
32703265
yield* Ref.get(backgroundWorkAtFinalization),
3271-
failureMode === "interrupt-failure" || failureMode === "detach-interrupt-failure",
3266+
failureMode === "interrupt-failure",
32723267
);
32733268
assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);
32743269
const error = written.find((item) => item.type === "error");
3275-
if (failureMode !== "detach-interrupt-failure")
3276-
assert.include(error?.failure.message ?? "", "(SQLITE_CANTOPEN)");
3270+
assert.include(error?.failure.message ?? "", "(SQLITE_CANTOPEN)");
32773271
const calls = yield* Ref.get(interrupts);
3278-
assert.equal(calls.length, failureMode === "detach-interrupt-failure" ? 2 : 1);
3272+
assert.equal(calls.length, 1);
32793273
assert.equal(calls.at(-1)?.providerTurnId, ids.rootProviderTurnId);
32803274
assert.isTrue(calls.at(-1)?.requestRuntimeRestart);
32813275
const repaired = finalEvents.find((event) => event.type === "provider-turn.updated");
3282-
if (failureMode === "interrupt-failure" || failureMode === "detach-interrupt-failure") {
3276+
if (failureMode === "interrupt-failure") {
32833277
assert.isUndefined(repaired);
32843278
} else {
32853279
assert.equal(repaired?.payload.id, ids.rootProviderTurnId);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,16 +1726,6 @@ export const layer: Layer.Layer<
17261726
yield* Ref.set(interruptionMessage, "Run interrupted because the workspace changed");
17271727
const routing = yield* Ref.get(eventRouting);
17281728
const providerTurn = yield* Ref.get(latestRootProviderTurn);
1729-
if (
1730-
!rootFinalized &&
1731-
providerTurn !== null &&
1732-
isOrchestrationV2WorkActive(providerTurn.status)
1733-
) {
1734-
yield* input.session.interruptTurn({
1735-
providerThread: yield* Ref.get(latestProviderThread),
1736-
providerTurnId: providerTurn.id,
1737-
});
1738-
}
17391729
const completedAt = yield* DateTime.now;
17401730
const terminal = {
17411731
type: "turn.terminal",

0 commit comments

Comments
 (0)