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
64 changes: 64 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Loading