Skip to content

Commit e0c6627

Browse files
wSedlacekclaude
andcommitted
fix(providers): keep live agent updates during a snapshot reload
Bound Copilot text generation startup too, so a stalled CLI is force stopped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c8d9b35 commit e0c6627

4 files changed

Lines changed: 78 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5215,12 +5215,18 @@ describe("AcpAdapterV2", () => {
52155215
const instanceId = ProviderInstanceId.make("acp-test-native-rewind");
52165216
const prompts: Array<string> = [];
52175217
let loads = 0;
5218+
let extensions: AcpAdapterV2ExtensionContext | undefined;
52185219
const adapter = yield* makeAcpAdapterV2({
52195220
instanceId,
52205221
flavor: {
52215222
driver: ACP_TEST_DRIVER,
52225223
capabilities: AcpProviderCapabilitiesV2,
5224+
enablePostSettleContinuation: true,
52235225
rollbackSession: () => Effect.void,
5226+
registerExtensions: (context) =>
5227+
Effect.sync(() => {
5228+
extensions = context;
5229+
}),
52245230
makeRuntime: makeMockRuntime({
52255231
childProcessSpawner,
52265232
mockAgentPath,
@@ -5232,12 +5238,15 @@ describe("AcpAdapterV2", () => {
52325238
},
52335239
loadSession: (sessionId, activation) => {
52345240
loads += 1;
5235-
return nativeRuntime.loadSession(sessionId, activation);
5241+
return extensions!
5242+
.applyBackgroundTaskMutation({ sessionId, taskId: "task-1", status: "running" })
5243+
.pipe(Effect.andThen(nativeRuntime.loadSession(sessionId, activation)));
52365244
},
52375245
}),
52385246
}),
52395247
},
52405248
selfInvocation,
5249+
continuationRequests: { offer: () => Effect.void },
52415250
});
52425251
const threadId = ThreadId.make("thread-acp-native-rewind");
52435252
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
@@ -5301,6 +5310,7 @@ describe("AcpAdapterV2", () => {
53015310
const snapshot = yield* runtime.readThreadSnapshot({ providerThread });
53025311
assert.strictEqual(loads, 1);
53035312
assert.deepEqual(snapshot.providerTurns, []);
5313+
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
53045314

53055315
yield* runTurn(2);
53065316
assert.strictEqual(prompts.length, 2);

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
20192019
loadingIndex: 0,
20202020
});
20212021
const snapshotReloadRequired = yield* Ref.make(false);
2022+
// Replayed history for the active session must not read as post-turn output.
2023+
const snapshotReloading = yield* Ref.make(false);
20222024
const resetConversationCaches = Effect.all(
20232025
[
20242026
Ref.set(promptInstructionStates, new Map()),
@@ -4757,7 +4759,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
47574759
};
47584760
}
47594761
const rootSessionId = yield* Ref.get(activeSessionId);
4760-
if (rootSessionId === null || notification.sessionId !== rootSessionId) {
4762+
if (
4763+
rootSessionId === null ||
4764+
notification.sessionId !== rootSessionId ||
4765+
(yield* Ref.get(snapshotReloading))
4766+
) {
47614767
return {
47624768
buffered: false,
47634769
offerContinuation: false,
@@ -5076,6 +5082,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
50765082
if (
50775083
followsAgentWorkState &&
50785084
!(yield* Ref.get(stoppedRunQuarantine)) &&
5085+
!(yield* Ref.get(snapshotReloading)) &&
50795086
notification.sessionId === (yield* Ref.get(activeSessionId))
50805087
) {
50815088
if (update.sessionUpdate === "state_update") agentWork = update;
@@ -9330,16 +9337,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
93309337
loadingMessageId: null,
93319338
loadingIndex: 0,
93329339
});
9333-
// Replayed history for the active session would read as post-turn output.
9334-
yield* Ref.set(activeSessionId, null);
93359340
const mcpContext = yield* readMcpContext(
93369341
snapshotInput.providerThread.appThreadId,
93379342
);
93389343
prepareTerminalEnvironment(mcpContext, sessionId);
9339-
const activated = yield* runtime.loadSession(
9340-
sessionId,
9341-
acpMcpActivation(mcpContext),
9342-
);
9344+
yield* Ref.set(snapshotReloading, reload);
9345+
const activated = yield* runtime
9346+
.loadSession(sessionId, acpMcpActivation(mcpContext))
9347+
.pipe(Effect.ensuring(Ref.set(snapshotReloading, false)));
93439348
yield* Ref.set(snapshotReloadRequired, false);
93449349
rememberTerminalEnvironment(
93459350
activated.sessionId,

‎packages/provider-copilot/src/server/textGeneration.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { assert, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { ProviderInstanceId } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
5+
import * as Fiber from "effect/Fiber";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Path from "effect/Path";
78
import * as Schema from "effect/Schema";
9+
import * as TestClock from "effect/testing/TestClock";
810

911
import { GitHubCopilotSettings } from "../settings.ts";
1012
import type { CopilotSdkClient } from "./copilotSdkRuntime.ts";
@@ -43,4 +45,40 @@ it.layer(NodeServices.layer)("GitHub Copilot text generation", (it) => {
4345
assert.strictEqual(model, "claude-haiku-4.5");
4446
}),
4547
);
48+
49+
it.effect("force stops a CLI that never answers", () =>
50+
Effect.gen(function* () {
51+
let forced = false;
52+
const generation = makeGitHubCopilotTextGeneration({
53+
settings,
54+
environment: {},
55+
fileSystem: yield* FileSystem.FileSystem,
56+
path: yield* Path.Path,
57+
makeClient: () =>
58+
({
59+
generateText: () => new Promise(() => undefined),
60+
forceStop: async () => {
61+
forced = true;
62+
},
63+
}) as unknown as CopilotSdkClient,
64+
});
65+
66+
const title = yield* generation
67+
.generateThreadTitle({
68+
cwd: "/repo",
69+
message: "the login is broken",
70+
modelSelection: {
71+
instanceId: ProviderInstanceId.make("githubCopilot"),
72+
model: "claude-haiku-4.5",
73+
},
74+
})
75+
.pipe(Effect.flip, Effect.forkChild);
76+
yield* Effect.yieldNow;
77+
yield* TestClock.adjust("210 seconds");
78+
79+
const failure = yield* Fiber.join(title);
80+
assert.include(failure.detail, "did not respond");
81+
assert.isTrue(forced);
82+
}),
83+
);
4684
});

‎packages/provider-copilot/src/server/textGeneration.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { makeCopilotSdkClient } from "./copilotSdkRuntime.ts";
1717

1818
const PROVIDER = "GitHub Copilot";
1919
const TIMEOUT_MS = 180_000;
20+
// `TIMEOUT_MS` only covers the reply, so a stalled CLI handshake needs its own limit.
21+
const STARTUP_TIMEOUT_MS = 30_000;
2022

2123
export function makeGitHubCopilotTextGeneration(input: {
2224
readonly settings: GitHubCopilotSettings;
@@ -53,7 +55,21 @@ export function makeGitHubCopilotTextGeneration(input: {
5355
detail: `${PROVIDER} could not generate text.`,
5456
cause,
5557
}),
56-
});
58+
}).pipe(
59+
Effect.timeoutOrElse({
60+
duration: TIMEOUT_MS + STARTUP_TIMEOUT_MS,
61+
orElse: () => {
62+
const timedOut = new TextGenerationError({
63+
operation: request.operation,
64+
detail: `${PROVIDER} did not respond in time.`,
65+
});
66+
return Effect.tryPromise({ try: () => client.forceStop(), catch: () => timedOut }).pipe(
67+
Effect.ignore,
68+
Effect.andThen(Effect.fail(timedOut)),
69+
);
70+
},
71+
}),
72+
);
5773
return yield* decodeJsonReply(request, PROVIDER, reply);
5874
});
5975
return fromRunner("GitHubCopilotTextGeneration", withoutInvestigation(PROVIDER, run));

0 commit comments

Comments
 (0)