Skip to content

Commit 5cf39a2

Browse files
only21milclaude
andcommitted
fix(server): bound the ACP session close and open history imports through the registry
The ACP adapter's session finalizer waited for session/close with no deadline. Finalizers can't be interrupted, so an agent that never answered kept its process running and held the history import, and the ACP session lock, even after the import was interrupted. The close now gets 10 seconds, the bound the Stop path already uses, and the runtime closes after it. The history import now gets its adapter from the provider adapter registry, so the session open has the same sign-in admission as every other open. Refs #22015 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bd31d69 commit 5cf39a2

7 files changed

Lines changed: 90 additions & 15 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,9 @@ const program = Effect.gen(function* () {
742742
yield* agent.handleCloseSession(() =>
743743
Effect.gen(function* () {
744744
yield* requireAuthentication();
745+
if (process.env.T3_ACP_HANG_CLOSE_SESSION === "1") {
746+
return yield* Effect.never;
747+
}
745748
return {};
746749
}),
747750
);

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,67 @@ describe("AcpAdapterV2", () => {
34463446
}).pipe(Effect.provide(layerTest)),
34473447
);
34483448

3449+
it.effect("closes the ACP runtime when the agent never answers session/close", () =>
3450+
Effect.gen(function* () {
3451+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3452+
const path = yield* Path.Path;
3453+
const selfInvocation = yield* resolveSelfInvocation();
3454+
const mockAgentPath = yield* path.fromFileUrl(
3455+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3456+
);
3457+
const protocolEvents = yield* Queue.bounded<EffectAcpProtocol.AcpProtocolLogEvent>(256);
3458+
const makeRuntime = makeMockRuntime({
3459+
childProcessSpawner,
3460+
mockAgentPath,
3461+
protocolEvents,
3462+
environment: { T3_ACP_HANG_CLOSE_SESSION: "1" },
3463+
});
3464+
let runtimeClosed = false;
3465+
const instanceId = ProviderInstanceId.make("acp-test");
3466+
const adapter = yield* makeAcpAdapterV2({
3467+
instanceId,
3468+
flavor: {
3469+
driver: ACP_TEST_DRIVER,
3470+
capabilities: AcpProviderCapabilitiesV2,
3471+
makeRuntime: (runtimeInput) =>
3472+
Effect.addFinalizer(() =>
3473+
Effect.sync(() => {
3474+
runtimeClosed = true;
3475+
}),
3476+
).pipe(Effect.andThen(makeRuntime(runtimeInput))),
3477+
},
3478+
selfInvocation,
3479+
});
3480+
const sessionScope = yield* Scope.make();
3481+
yield* adapter
3482+
.openSession({
3483+
threadId: ThreadId.make("thread-acp-unanswered-close"),
3484+
providerSessionId: ProviderSessionId.make("provider-session-acp-unanswered-close"),
3485+
modelSelection: { instanceId, model: "default" },
3486+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3487+
runtimeMode: "full-access",
3488+
interactionMode: "default",
3489+
cwd: process.cwd(),
3490+
}),
3491+
})
3492+
.pipe(Effect.provideService(Scope.Scope, sessionScope));
3493+
const closing = yield* Scope.close(sessionScope, Exit.void).pipe(Effect.forkDetach);
3494+
yield* Stream.fromQueue(protocolEvents).pipe(
3495+
Stream.filter(
3496+
(event) => event.direction === "outgoing" && rawProtocolMethod(event) === "session/close",
3497+
),
3498+
Stream.runHead,
3499+
);
3500+
yield* TestClock.adjust("10 seconds");
3501+
const closed = yield* Fiber.await(closing).pipe(
3502+
Effect.timeoutOption("5 seconds"),
3503+
TestClock.withLive,
3504+
);
3505+
assert.isTrue(Option.isSome(closed));
3506+
assert.isTrue(runtimeClosed);
3507+
}).pipe(Effect.provide(layerTest)),
3508+
);
3509+
34493510
it.effect.each(["grok-build", "composer-2"])(
34503511
"Grok configures the native session for %s",
34513512
(model) =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ const layerAgentSessionImporterProvided = AgentSessionImporter.layer.pipe(
244244
layerEventSinkProvided,
245245
IdAllocator.layer,
246246
ProviderSessionRuntime.layer,
247+
layerProviderAdapterRegistryProvided,
247248
),
248249
),
249250
);

‎apps/server/src/project/AgentSessionImporter.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as EventSink from "../orchestration-v2/EventSink.ts";
2626
import * as EventStore from "../orchestration-v2/EventStore.ts";
2727
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
2828
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
29+
import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts";
2930
import {
3031
CLAUDE_MODEL_SELECTION,
3132
CODEX_MODEL_SELECTION,
@@ -166,6 +167,7 @@ const seedOwner = Effect.fn(function* (
166167
const makeImporter = Effect.fn(function* (
167168
candidates: ReadonlyArray<ReturnType<typeof candidate>>,
168169
beforeTransaction: Effect.Effect<void> = Effect.void,
170+
getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"],
169171
) {
170172
const sql = yield* SqlClient.SqlClient;
171173
const events = yield* EventStore.EventStoreV2;
@@ -234,6 +236,9 @@ const makeImporter = Effect.fn(function* (
234236
),
235237
),
236238
}),
239+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)(
240+
getAdapter === undefined ? {} : { get: getAdapter },
241+
),
237242
),
238243
),
239244
),
@@ -400,7 +405,7 @@ it.effect("copies the history a native session load replays into the imported th
400405
const projections = yield* ProjectionStore.ProjectionStoreV2;
401406
const source = candidate(CODEX_MODEL_SELECTION, "native-history");
402407
const threadId = yield* seedOwner(source);
403-
const importer = yield* makeImporter([]);
408+
const resolved: Array<ProviderInstanceId> = [];
404409
const opened: Array<ProviderAdapter.ProviderAdapterV2OpenSessionInput> = [];
405410
let closed = false;
406411
const replayed = (role: "user" | "assistant", text: string) => ({
@@ -442,14 +447,17 @@ it.effect("copies the history a native session load replays into the imported th
442447
}),
443448
),
444449
} as unknown as ProviderAdapter.ProviderAdapterV2["Service"];
450+
const importer = yield* makeImporter([], Effect.void, (instanceId) =>
451+
Effect.sync(() => {
452+
resolved.push(instanceId);
453+
return adapter;
454+
}),
455+
);
445456

446457
expect(
447-
yield* importer.importNativeHistory({
448-
threadId,
449-
adapter,
450-
workspaceRoot: "/workspace/project",
451-
}),
458+
yield* importer.importNativeHistory({ threadId, workspaceRoot: "/workspace/project" }),
452459
).toBe(2);
460+
expect(resolved).toEqual([CODEX_MODEL_SELECTION.instanceId]);
453461
expect(opened).toMatchObject([
454462
{
455463
threadId,

‎apps/server/src/project/AgentSessionImporter.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import * as Stream from "effect/Stream";
3333

3434
import * as EventSink from "../orchestration-v2/EventSink.ts";
3535
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
36-
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3736
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
37+
import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts";
3838
import * as ProviderSessionRuntime from "../persistence/ProviderSessionRuntime.ts";
3939
import * as AgentSessionScanner from "./AgentSessionScanner.ts";
4040
import * as ProjectService from "./ProjectService.ts";
@@ -173,6 +173,7 @@ const make = Effect.gen(function* () {
173173
const eventSink = yield* EventSink.EventSinkV2;
174174
const idAllocator = yield* IdAllocator.IdAllocatorV2;
175175
const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
176+
const registry = yield* ProviderAdapterRegistry.ProviderAdapterRegistryV2;
176177
const importRecentAgentThreads = Effect.fn("importRecentAgentThreadsV2")(function* (
177178
input: AgentSessionImportInput,
178179
) {
@@ -409,7 +410,6 @@ const make = Effect.gen(function* () {
409410
*/
410411
const importNativeHistory = Effect.fn("importNativeHistoryV2")(function* (input: {
411412
readonly threadId: ThreadId;
412-
readonly adapter: ProviderAdapter.ProviderAdapterV2["Service"];
413413
readonly workspaceRoot: string;
414414
}) {
415415
const { thread, providerThreads } = yield* orchestrator.getThreadRecords(input.threadId, [
@@ -424,7 +424,9 @@ const make = Effect.gen(function* () {
424424
providerInstanceId: providerThread.providerInstanceId,
425425
threadId: input.threadId,
426426
});
427-
const snapshot = yield* input.adapter
427+
// The registry admits this open against sign-in changes, like any other open.
428+
const adapter = yield* registry.get(thread.modelSelection.instanceId);
429+
const snapshot = yield* adapter
428430
.openSession({
429431
threadId: input.threadId,
430432
providerSessionId,

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,11 +1486,7 @@ export const layerWsRpc = (
14861486
}
14871487
// The thread is usable without its history, so a failed read only logs.
14881488
yield* agentSessionImporter
1489-
.importNativeHistory({
1490-
threadId,
1491-
adapter: instance.orchestrationAdapter,
1492-
workspaceRoot: project.workspaceRoot,
1493-
})
1489+
.importNativeHistory({ threadId, workspaceRoot: project.workspaceRoot })
14941490
.pipe(
14951491
Effect.catch((cause) =>
14961492
Effect.logWarning("Could not import ACP session history", {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6881,7 +6881,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
68816881
!restartRequired &&
68826882
!transportHadOutstandingResponses
68836883
) {
6884-
yield* runtime.closeSession().pipe(Effect.ignore);
6884+
// Finalizers can't be interrupted, so an agent that never
6885+
// answers would keep its process and this scope open.
6886+
yield* runtime
6887+
.closeSession()
6888+
.pipe(Effect.timeoutOption("10 seconds"), Effect.ignore);
68856889
}
68866890
}),
68876891
);

0 commit comments

Comments
 (0)