Skip to content

Commit 0cf9428

Browse files
fix(acp): Copilot Stop restarts only when work remains
A plain Copilot Stop no longer restarts the runtime and reloads the session. Stop of a resumed background continuation without session/close now finalizes it, cancels, and restarts instead of timing out. AI-assisted; verified with the tests and tools listed above.
1 parent 0487aa2 commit 0cf9428

2 files changed

Lines changed: 59 additions & 19 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,14 +3395,15 @@ describe("AcpAdapterV2", () => {
33953395
);
33963396

33973397
it.effect.each([
3398-
{ close: true, settled: false, returned: false },
3399-
{ close: false, settled: false, returned: false },
3400-
{ close: true, settled: true, returned: false },
3401-
{ close: false, settled: true, returned: false },
3402-
{ close: false, settled: true, returned: true },
3398+
{ close: true, settled: false, returned: false, continuation: false },
3399+
{ close: false, settled: false, returned: false, continuation: false },
3400+
{ close: true, settled: true, returned: false, continuation: false },
3401+
{ close: false, settled: true, returned: false, continuation: false },
3402+
{ close: false, settled: true, returned: true, continuation: false },
3403+
{ close: false, settled: true, returned: false, continuation: true },
34033404
])(
3404-
"stops Copilot work with close=$close and settled=$settled and returned=$returned",
3405-
({ close, settled, returned }) =>
3405+
"stops Copilot work with close=$close, settled=$settled, returned=$returned, continuation=$continuation",
3406+
({ close, settled, returned, continuation }) =>
34063407
Effect.gen(function* () {
34073408
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
34083409
const path = yield* Path.Path;
@@ -3580,9 +3581,35 @@ describe("AcpAdapterV2", () => {
35803581
assert.equal(terminal?.type === "turn.terminal" ? terminal.status : null, "completed");
35813582
}
35823583
if (returned) yield* event!({ sessionId: "mock-session-1", type: "assistant.idle" });
3584+
let stoppedTurnId = running.providerTurn.id;
3585+
if (continuation) {
3586+
yield* event!({ sessionId: "mock-session-1", type: "assistant.turn_start" });
3587+
yield* sessionUpdate!({
3588+
sessionId: "mock-session-1",
3589+
update: {
3590+
sessionUpdate: "agent_message_chunk",
3591+
content: { type: "text", text: "wake output" },
3592+
},
3593+
});
3594+
yield* runtime.startTurn(
3595+
makeTurnInput({
3596+
threadId,
3597+
providerThread,
3598+
instanceId,
3599+
runtimePolicy,
3600+
now: yield* DateTime.now,
3601+
ordinal: 2,
3602+
messageCreatedBy: "agent",
3603+
messageCreationSource: "provider",
3604+
}),
3605+
);
3606+
const wake = (yield* runtime.readThreadSnapshot({ providerThread })).providerTurns.at(-1);
3607+
assert.equal(wake?.status, "running");
3608+
stoppedTurnId = wake!.id;
3609+
}
35833610
yield* runtime.interruptTurn({
35843611
providerThread,
3585-
providerTurnId: running.providerTurn.id,
3612+
providerTurnId: stoppedTurnId,
35863613
requestRuntimeRestart: true,
35873614
});
35883615
assert.deepEqual(
@@ -3591,7 +3618,7 @@ describe("AcpAdapterV2", () => {
35913618
"Stop must cancel background work after its prompt RPC returns",
35923619
);
35933620
if (returned) assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
3594-
if (!settled) {
3621+
if (!settled || continuation) {
35953622
const terminal = (yield* take("turn.terminal"))[0];
35963623
assert.equal(terminal?.type === "turn.terminal" ? terminal.status : null, "interrupted");
35973624
}
@@ -3603,10 +3630,14 @@ describe("AcpAdapterV2", () => {
36033630
instanceId,
36043631
runtimePolicy,
36053632
now: yield* DateTime.now,
3606-
ordinal: 2,
3633+
ordinal: continuation ? 3 : 2,
36073634
}),
36083635
);
3609-
assert.equal(runtimeCount, 2);
3636+
assert.equal(
3637+
runtimeCount,
3638+
close || settled ? 2 : 1,
3639+
"only Stop with remaining Copilot work restarts the runtime",
3640+
);
36103641
}).pipe(Effect.provide(layerTest), Effect.scoped),
36113642
);
36123643

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9079,15 +9079,24 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
90799079
);
90809080
return undefined;
90819081
}
9082+
// A followed continuation has no prompt RPC to settle it, and quarantine drops its idle.
9083+
const stopsContinuation =
9084+
canFollowForegroundEvents &&
9085+
interruptContext?.providerTurnId === turnInput.providerTurnId &&
9086+
acpIsProviderContinuationMessage(interruptContext.input.message);
90829087
if (
90839088
flavor.followForegroundEvents === true &&
90849089
!supportsClose &&
9085-
interruptContext === null &&
9086-
(yield* sessionRuntime.hasPendingBackgroundWork ?? Effect.succeed(false)) &&
9087-
turnInput.requestRuntimeRestart === true
9090+
turnInput.requestRuntimeRestart === true &&
9091+
(stopsContinuation ||
9092+
(interruptContext === null &&
9093+
(yield* (
9094+
sessionRuntime.hasPendingBackgroundWork ?? Effect.succeed(false)
9095+
))))
90889096
) {
90899097
yield* runtimeCallbackPermit.withPermit(
90909098
Effect.gen(function* () {
9099+
const context = yield* Ref.get(activeTurn);
90919100
const carryover = yield* Ref.get(carryoverSubagents);
90929101
yield* quarantineStoppedRun();
90939102
agentWork = null;
@@ -9096,6 +9105,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
90969105
yield* cancelPendingRuntimeRequests();
90979106
yield* terminalizeCarryoverSubagents(carryover);
90989107
yield* Ref.set(runtimeRestartRequired, true);
9108+
if (stopsContinuation && context === interruptContext) {
9109+
context.interrupted = true;
9110+
yield* finalizeTurn(context, "interrupted");
9111+
}
90999112
}),
91009113
);
91019114
yield* runtime.cancel;
@@ -9302,11 +9315,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
93029315
if (!settledSoftInterrupt && (yield* promptStillRunsAfterHold(context))) {
93039316
yield* runtime.cancel;
93049317
}
9305-
if (
9306-
restartRuntime &&
9307-
(flavor.restartRuntimeAfterInterrupt === true ||
9308-
flavor.followForegroundEvents === true)
9309-
) {
9318+
if (restartRuntime && flavor.restartRuntimeAfterInterrupt === true) {
93109319
yield* Ref.set(runtimeRestartRequired, true);
93119320
}
93129321
}

0 commit comments

Comments
 (0)