diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 46a8f4c888296..741f0fc6ac11a 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -78,6 +78,7 @@ import { } from "@t3tools/provider-grok/testing"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; +import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; import { makeTurnInput } from "./AcpAdapterV2.testkit.ts"; import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; import { @@ -6848,6 +6849,69 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("counts a running ACP agent for port discovery until its session closes", () => + Effect.gen(function* () { + const host = yield* ProviderHost.ProviderHost; + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const selfInvocation = yield* resolveSelfInvocation(); + const mockAgentPath = yield* bundledMockAgentPath; + const instanceId = ProviderInstanceId.make("acp-test-track-agent"); + const threadId = ThreadId.make("thread-acp-track-agent"); + let running = 0; + yield* Effect.gen(function* () { + const adapter = yield* makeAcpAdapterV2({ + instanceId, + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + makeRuntime: makeMockRuntime({ childProcessSpawner, mockAgentPath }), + }, + selfInvocation, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-acp-track-agent"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + assert.equal(running, 1); + yield* runtime.rollbackThread({ + providerThread, + providerThreadTurns: [], + target: { + type: "thread_start", + checkpointId: CheckpointId.make("checkpoint-acp-track-agent"), + appRunOrdinal: 0, + }, + }); + assert.equal(running, 1, "the replacement agent must stay counted"); + }).pipe( + Effect.scoped, + Effect.provideService(ProviderHost.ProviderHost, { + ...host, + trackAgent: () => { + running += 1; + return () => { + running -= 1; + }; + }, + }), + ); + assert.equal(running, 0); + }).pipe(Effect.provide(layerTest)), + ); + it.effect("replaces the ACP session and clears conversation state on rollback", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index a00ea8625e697..dcc92b63b794d 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -7654,6 +7654,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( return mcpBridge; }); + const trackRunningAgent = Effect.fn("AcpAdapterV2.trackRunningAgent")(function* ( + scope: Scope.Scope, + ) { + const trackAgent = host.trackAgent; + if (trackAgent === undefined) return; + yield* Effect.acquireRelease(Effect.sync(trackAgent), (release) => + Effect.sync(release), + ).pipe(Effect.provideService(Scope.Scope, scope)); + }); + const spawnAcpRuntime = Effect.fnUntraced(function* ( threadId: ThreadId | null, resumeSessionId?: string, @@ -7662,6 +7672,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Scope.close(runtimeScope, Exit.void); } runtimeScope = yield* Scope.make(); + // Let port discovery scan dev servers while this agent runs. + yield* trackRunningAgent(runtimeScope); const mcpContext = yield* readMcpContext(threadId); runtimeMcpBridge = yield* makeRuntimeMcpBridge(mcpContext, runtimeScope); const runtimeGeneration = yield* Ref.get(runtimeCallbackGeneration); @@ -7742,6 +7754,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }); let replacementMcpBridge: AcpMcpOverAcpBridge | undefined; const startup = Effect.gen(function* () { + yield* trackRunningAgent(replacementScope); const mcpContext = yield* readMcpContext(threadId); replacementMcpBridge = yield* makeRuntimeMcpBridge(mcpContext, replacementScope); const replacementRuntime = yield* flavor