@devries-cameron20 / t3code-1 / commits / 28bfae00d4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(acp): a lost or locked Copilot session restarts on the next message instead of failing every time (#26651)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Ian Rowe committed Oct 31, 2026 at 20:34 UTC 28bfae00d4a481b83ee587f9c86661a38230afb1
4 files changed +200 -2
apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
+169
@@ -2169,4 +2169,173 @@ describe("AcpRegistryAdapterV2", () => {
2169 assert.isFalse(yield* queued.runtime.hasPendingBackgroundWork!);
2170 }).pipe(Effect.provide(layerTest), Effect.scoped),
2171 );
2172 +
2173 + it.effect.each([
2174 + {
2175 + failure: "a lost session",
2176 + error: {
2177 + code: -32603,
2178 + message: "Internal error",
2179 + data: {
2180 + details:
2181 + "Request session.send failed with message: session event delivery failed: session not found: copilot-session",
2182 + },
2183 + },
2184 + restarts: true,
2185 + },
2186 + {
2187 + failure: "a lost session reported in the message",
2188 + error: {
2189 + code: -32603,
2190 + message:
2191 + "Request session.send failed with message: session event delivery failed: session not found: copilot-session",
2192 + },
2193 + restarts: true,
2194 + },
2195 + {
2196 + failure: "a session-lock timeout",
2197 + error: {
2198 + code: -32603,
2199 + message: "Internal error",
2200 + data: {
2201 + details:
2202 + "session lock unavailable during is_session_generation_current: acquisition timed out",
2203 + },
2204 + },
2205 + restarts: true,
2206 + },
2207 + {
2208 + failure: "a session-lock timeout reported in the message",
2209 + error: {
2210 + code: -32603,
2211 + message:
2212 + "Request session.send failed with message: session lock unavailable during is_session_generation_current: acquisition timed out",
2213 + },
2214 + restarts: true,
2215 + },
2216 + {
2217 + failure: "an unrelated internal error",
2218 + error: {
2219 + code: -32603,
2220 + message: "Request session.send failed with message: Session directory no longer exists",
2221 + },
2222 + restarts: false,
2223 + },
2224 + ] as const)("runs Copilot's next message after $failure", ({ error, restarts }) =>
2225 + Effect.gen(function* () {
2226 + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2227 + const fileSystem = yield* FileSystem.FileSystem;
2228 + const path = yield* Path.Path;
2229 + const replayDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-copilot-lost-" });
2230 + const scriptPath = yield* path.fromFileUrl(
2231 + new URL("../../../scripts/acp-replay-agent.ts", import.meta.url),
2232 + );
2233 + const request = (method: string, params: unknown = "<any>") => ({
2234 + type: "expect_outbound",
2235 + frame: { kind: "request", method, params },
2236 + });
2237 + const respond = (method: string, response: object) => ({
2238 + type: "emit_inbound",
2239 + frame: { kind: "response", method, ...response },
2240 + });
2241 + const start = (sessionId: string) => [
2242 + request("initialize"),
2243 + respond("initialize", {
2244 + result: { protocolVersion: 1, agentCapabilities: { loadSession: true } },
2245 + }),
2246 + request("session/new"),
2247 + respond("session/new", { result: { sessionId } }),
2248 + ];
2249 + const failedPrompt = [
2250 + ...start("copilot-session"),
2251 + request("session/prompt"),
2252 + respond("session/prompt", { error }),
2253 + ];
2254 + const completedPrompt = [
2255 + request("session/prompt"),
2256 + respond("session/prompt", { result: { stopReason: "end_turn" } }),
2257 + ];
2258 + const processes = restarts
2259 + ? [
2260 + failedPrompt,
2261 + [
2262 + ...start("replacement-session"),
2263 + request("session/load", {
2264 + sessionId: "copilot-session",
2265 + cwd: "<workspace>",
2266 + mcpServers: "<any>",
2267 + }),
2268 + respond("session/load", { result: {} }),
2269 + ...completedPrompt,
2270 + ],
2271 + ]
2272 + : [[...failedPrompt, ...completedPrompt]];
2273 + let spawned = 0;
2274 + const instanceId = ProviderInstanceId.make("copilot-lost-session");
2275 + const adapter = yield* makeAcpRegistryAdapterV2({
2276 + selfInvocation: yield* resolveSelfInvocation(),
2277 + instanceId,
2278 + settings: yield* decodeAcpRegistryAdapterSettings({ agentId: "github-copilot-cli" }),
2279 + environment: {},
2280 + makeRuntime: (runtimeInput) =>
2281 + Effect.gen(function* () {
2282 + const index = spawned++;
2283 + const transcript = yield* decodeAcpReplayTranscript(
2284 + {
2285 + provider: ACP_REGISTRY_PROVIDER,
2286 + protocol: "acp.ndjson-jsonrpc",
2287 + version: "1",
2288 + scenario: `copilot-lost-session-${index}`,
2289 + entries: processes[index] as never,
2290 + },
2291 + ACP_REGISTRY_PROVIDER,
2292 + ).pipe(Effect.orDie);
2293 + return yield* makeAcpReplayRuntime({
2294 + transcript,
2295 + statusPath: path.join(replayDir, `status-${index}.json`),
2296 + scriptPath,
2297 + childProcessSpawner,
2298 + fileSystem,
2299 + })(runtimeInput);
2300 + }),
2301 + }).pipe(Effect.provide(layerInjectedRuntimeCatalog));
2302 + const threadId = ThreadId.make("thread-copilot-lost-session");
2303 + const modelSelection = { instanceId, model: "default" } as const;
2304 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2305 + runtimeMode: "approval-required",
2306 + interactionMode: "default",
2307 + cwd: replayDir,
2308 + });
2309 + const runtime = yield* adapter.openSession({
2310 + threadId,
2311 + providerSessionId: ProviderSessionId.make("session-copilot-lost-session"),
2312 + modelSelection,
2313 + runtimePolicy,
2314 + });
2315 + const providerThread = yield* runtime.ensureThread({
2316 + threadId,
2317 + modelSelection,
2318 + runtimePolicy,
2319 + });
2320 + const now = yield* DateTime.now;
2321 + const runTurn = (ordinal: number) =>
2322 + runtime
2323 + .startTurn(
2324 + makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
2325 + )
2326 + .pipe(
2327 + Effect.andThen(
2328 + runtime.events.pipe(
2329 + Stream.filter((event) => event.type === "turn.terminal"),
2330 + Stream.take(1),
2331 + Stream.runCollect,
2332 + ),
2333 + ),
2334 + Effect.map(([terminal]) => terminal?.status),
2335 + );
2336 + assert.equal(yield* runTurn(1), "failed");
2337 + assert.equal(yield* runTurn(2), "completed");
2338 + assert.equal(spawned, restarts ? 2 : 1);
2339 + }).pipe(Effect.provide(layerTest), Effect.scoped),
2340 + );
2341 });
packages/provider-acp-registry/src/server/adapter.ts
+2
@@ -12,6 +12,7 @@ import { extractDroidElicitationForm } from "./droidAcp.ts";
12 import {
13 copilotClientCapabilitiesMeta,
14 extractCopilotSubagentUpdate,
15 + isCopilotLostSessionError,
16 makeCopilotSubagentRouting,
17 } from "./copilotAcp.ts";
18 import { defaultInstanceIdForDriver, ProviderDriverKind } from "@t3tools/contracts";
@@ -255,6 +256,7 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
256 registerExtensions: copilotRouting.registerExtensions,
257 normalizeSessionUpdate: copilotRouting.normalizeSessionUpdate,
258 extractSubagentUpdate: extractCopilotSubagentUpdate,
259 + restartRuntimeAfterPromptError: isCopilotLostSessionError,
260 }),
261 makeRuntime: options.makeRuntime ?? makeAcpRegistryRuntime(runtimeOptions, catalog),
262 ...(runtimeCoordinator === undefined
packages/provider-acp-registry/src/server/copilotAcp.ts
+21
@@ -1,6 +1,7 @@
1 import * as Effect from "effect/Effect";
2 import * as Schema from "effect/Schema";
3 import type * as AcpSchema from "effect-acp/compat";
4 +import * as AcpErrors from "effect-acp/errors";
5 import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel";
6 import type {
7 AcpAdapterV2ExtensionContext,
@@ -24,6 +25,26 @@ export const copilotClientCapabilitiesMeta = {
25 },
26 };
27
28 +const isAcpRequestError = Schema.is(AcpErrors.AcpRequestError);
29 +
30 +/** Copilot keeps failing every prompt with these until its process is replaced. */
31 +const COPILOT_LOST_SESSION_MARKERS = [
32 + "session event delivery failed: session not found: ",
33 + "session lock unavailable during is_session_generation_current: acquisition timed out",
34 +];
35 +
36 +export function isCopilotLostSessionError(error: unknown): boolean {
37 + if (!isAcpRequestError(error) || error.method !== "session/prompt" || error.code !== -32603) {
38 + return false;
39 + }
40 + // Older Copilot reports the cause in data.details; its native ACP layer (1.0.105) in the message.
41 + return [error.errorMessage, record(error.data)?.details].some(
42 + (text) =>
43 + typeof text === "string" &&
44 + COPILOT_LOST_SESSION_MARKERS.some((marker) => text.includes(marker)),
45 + );
46 +}
47 +
48 const CopilotSessionEvent = Schema.Struct({
49 sessionId: Schema.String,
50 type: Schema.String,
packages/provider-acp/src/server/adapter.ts
+8 -2
@@ -407,6 +407,8 @@ export interface AcpAdapterV2Flavor {
407 readonly reportsPerTurnPromptUsage?: boolean;
408 /** Grok can retain task_already_running until process exit. */
409 readonly restartRuntimeAfterInterrupt?: boolean;
410 + /** Prompt failures after which the agent process keeps failing until replaced. */
411 + readonly restartRuntimeAfterPromptError?: (error: unknown) => boolean;
412 readonly restartRuntimeOnEveryInterrupt?: boolean;
413 readonly terminateRuntimeProcessGroupOnInterrupt?: boolean;
414 readonly preserveRuntimeOnSettledInterrupt?: boolean;
@@ -7840,12 +7842,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7842 Result.isSuccess(error) &&
7843 error.success._tag === "AcpRequestError" &&
7844 error.success.code === -32800);
7845 + const failure = Cause.squash(cause);
7846 + if (!interrupted && flavor.restartRuntimeAfterPromptError?.(failure) === true) {
7847 + yield* Ref.set(runtimeRestartRequired, true);
7848 + }
7849 yield* finalizeTurn(
7850 context,
7851 interrupted ? "interrupted" : "failed",
7846 - flavor.promptFailure?.(Cause.squash(cause)) ??
7852 + flavor.promptFailure?.(failure) ??
7853 makeProviderFailure({
7848 - cause: Cause.squash(cause),
7854 + cause: failure,
7855 class: "provider_error",
7856 }),
7857 ).pipe(