Skip to content

Commit 28bfae0

Browse files
ianrowe12claudemaria-rcks
authored
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 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 6525e4c commit 28bfae0

4 files changed

Lines changed: 200 additions & 2 deletions

File tree

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,4 +2169,173 @@ describe("AcpRegistryAdapterV2", () => {
21692169
assert.isFalse(yield* queued.runtime.hasPendingBackgroundWork!);
21702170
}).pipe(Effect.provide(layerTest), Effect.scoped),
21712171
);
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+
);
21722341
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { extractDroidElicitationForm } from "./droidAcp.ts";
1212
import {
1313
copilotClientCapabilitiesMeta,
1414
extractCopilotSubagentUpdate,
15+
isCopilotLostSessionError,
1516
makeCopilotSubagentRouting,
1617
} from "./copilotAcp.ts";
1718
import { defaultInstanceIdForDriver, ProviderDriverKind } from "@t3tools/contracts";
@@ -255,6 +256,7 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
255256
registerExtensions: copilotRouting.registerExtensions,
256257
normalizeSessionUpdate: copilotRouting.normalizeSessionUpdate,
257258
extractSubagentUpdate: extractCopilotSubagentUpdate,
259+
restartRuntimeAfterPromptError: isCopilotLostSessionError,
258260
}),
259261
makeRuntime: options.makeRuntime ?? makeAcpRegistryRuntime(runtimeOptions, catalog),
260262
...(runtimeCoordinator === undefined

‎packages/provider-acp-registry/src/server/copilotAcp.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Effect from "effect/Effect";
22
import * as Schema from "effect/Schema";
33
import type * as AcpSchema from "effect-acp/compat";
4+
import * as AcpErrors from "effect-acp/errors";
45
import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel";
56
import type {
67
AcpAdapterV2ExtensionContext,
@@ -24,6 +25,26 @@ export const copilotClientCapabilitiesMeta = {
2425
},
2526
};
2627

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+
2748
const CopilotSessionEvent = Schema.Struct({
2849
sessionId: Schema.String,
2950
type: Schema.String,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ export interface AcpAdapterV2Flavor {
407407
readonly reportsPerTurnPromptUsage?: boolean;
408408
/** Grok can retain task_already_running until process exit. */
409409
readonly restartRuntimeAfterInterrupt?: boolean;
410+
/** Prompt failures after which the agent process keeps failing until replaced. */
411+
readonly restartRuntimeAfterPromptError?: (error: unknown) => boolean;
410412
readonly restartRuntimeOnEveryInterrupt?: boolean;
411413
readonly terminateRuntimeProcessGroupOnInterrupt?: boolean;
412414
readonly preserveRuntimeOnSettledInterrupt?: boolean;
@@ -7840,12 +7842,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
78407842
Result.isSuccess(error) &&
78417843
error.success._tag === "AcpRequestError" &&
78427844
error.success.code === -32800);
7845+
const failure = Cause.squash(cause);
7846+
if (!interrupted && flavor.restartRuntimeAfterPromptError?.(failure) === true) {
7847+
yield* Ref.set(runtimeRestartRequired, true);
7848+
}
78437849
yield* finalizeTurn(
78447850
context,
78457851
interrupted ? "interrupted" : "failed",
7846-
flavor.promptFailure?.(Cause.squash(cause)) ??
7852+
flavor.promptFailure?.(failure) ??
78477853
makeProviderFailure({
7848-
cause: Cause.squash(cause),
7854+
cause: failure,
78497855
class: "provider_error",
78507856
}),
78517857
).pipe(

0 commit comments

Comments
 (0)