Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/server/scripts/acp-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ const program = Effect.gen(function* () {
yield* agent.handleCloseSession(() =>
Effect.gen(function* () {
yield* requireAuthentication();
if (process.env.T3_ACP_HANG_CLOSE_SESSION === "1") {
return yield* Effect.never;
}
return {};
}),
);
Expand Down
61 changes: 61 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4652,6 +4652,67 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("closes the ACP runtime when the agent never answers session/close", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const path = yield* Path.Path;
const selfInvocation = yield* resolveSelfInvocation();
const mockAgentPath = yield* path.fromFileUrl(
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
);
const protocolEvents = yield* Queue.bounded<EffectAcpProtocol.AcpProtocolLogEvent>(256);
const makeRuntime = makeMockRuntime({
childProcessSpawner,
mockAgentPath,
protocolEvents,
environment: { T3_ACP_HANG_CLOSE_SESSION: "1" },
});
let runtimeClosed = false;
const instanceId = ProviderInstanceId.make("acp-test");
const adapter = yield* makeAcpAdapterV2({
instanceId,
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
makeRuntime: (runtimeInput) =>
Effect.addFinalizer(() =>
Effect.sync(() => {
runtimeClosed = true;
}),
).pipe(Effect.andThen(makeRuntime(runtimeInput))),
},
selfInvocation,
});
const sessionScope = yield* Scope.make();
yield* adapter
.openSession({
threadId: ThreadId.make("thread-acp-unanswered-close"),
providerSessionId: ProviderSessionId.make("provider-session-acp-unanswered-close"),
modelSelection: { instanceId, model: "default" },
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "full-access",
interactionMode: "default",
cwd: process.cwd(),
}),
})
.pipe(Effect.provideService(Scope.Scope, sessionScope));
const closing = yield* Scope.close(sessionScope, Exit.void).pipe(Effect.forkDetach);
yield* Stream.fromQueue(protocolEvents).pipe(
Stream.filter(
(event) => event.direction === "outgoing" && rawProtocolMethod(event) === "session/close",
),
Stream.runHead,
);
yield* TestClock.adjust("10 seconds");
const closed = yield* Fiber.await(closing).pipe(
Effect.timeoutOption("5 seconds"),
TestClock.withLive,
);
assert.isTrue(Option.isSome(closed));
assert.isTrue(runtimeClosed);
}).pipe(Effect.provide(layerTest)),
);

// Oracle: Grok incident frames. Command completion must release the turn even
// when the CLI keeps the prompt RPC and process alive. Command polls stay visible.
it.live(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ it("reissues imported context until a V2 run completes", () => {
legacyImportItemCount: 2,
}),
);
assert.isFalse(
shouldPrepareLegacyImportHandoff({
historyOrigin: "v1_import",
hasCompletedRun: false,
legacyImportItemCount: 2,
hasNativeHistory: true,
}),
);
assert.isFalse(
shouldPrepareLegacyImportHandoff({
historyOrigin: "v1_import",
Expand Down
9 changes: 8 additions & 1 deletion apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,16 @@ function latestContextTransfer(
}

export function shouldPrepareLegacyImportHandoff(input: {
readonly hasNativeHistory?: boolean;
readonly hasCompletedRun: boolean;
readonly historyOrigin: OrchestrationV2AppThread["historyOrigin"];
readonly legacyImportItemCount: number;
}): boolean {
return (
input.historyOrigin === "v1_import" && !input.hasCompletedRun && input.legacyImportItemCount > 0
input.historyOrigin === "v1_import" &&
!input.hasNativeHistory &&
!input.hasCompletedRun &&
input.legacyImportItemCount > 0
);
}

Expand Down Expand Up @@ -6248,6 +6252,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
nativeThreadId: `pending:${runId}`,
});
const legacyImportHandoff = shouldPrepareLegacyImportHandoff({
hasNativeHistory:
activeProviderThread?.driver === "acpRegistry" &&
activeProviderThread.nativeThreadRef !== null,
historyOrigin: projection.thread.historyOrigin,
hasCompletedRun: latestCompletedRun !== undefined,
legacyImportItemCount: legacyImportItems.length,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ const layerAgentSessionImporterProvided = AgentSessionImporter.layer.pipe(
layerEventSink,
IdAllocator.layer,
ProviderSessionRuntime.layer,
ProviderAdapterRegistry.layerFromProviderInstanceRegistry,
ThreadCommandExecutor.layer,
),
),
);
Expand Down
Loading
Loading