Skip to content
TypeScript

fix(acp-registry): installing one agent no longer blocks other agents (#23684)

Victor Vogel5b0cf58f682d
diff --git a/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts b/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts
index c5612082f31..e119922970d 100644
--- a/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts
+++ b/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts
@@ -1547,6 +1547,76 @@ describe("AcpRegistrySupport", () => {
});
});
+ it.effect("resolves an installed agent while another agent's download is still running", () => {
+ const slowArchiveUrl = "https://registry.test/slow-agent.bin";
+ const agent = makeAgent({
+ binary: { "linux-x86_64": { archive: archiveUrl, cmd: "./bin/example-agent" } },
+ });
+ const slowAgent = {
+ ...agent,
+ distribution: {
+ binary: { "linux-x86_64": { archive: slowArchiveUrl, cmd: "./bin/slow-agent" } },
+ },
+ id: "slow-agent",
+ name: "Slow Agent",
+ };
+ const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
+ return Effect.gen(function* () {
+ const downloadStarted = yield* Deferred.make<void>();
+ const releaseDownload = yield* Deferred.make<void>();
+ return yield* Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
+ prefix: "t3-acp-registry-parallel-install-",
+ });
+ const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({
+ cacheDir,
+ toolsDir: `${cacheDir}/tools`,
+ registryUrl,
+ });
+
+ yield* resolver.resolve(settings(), "/workspace");
+
+ const prepareFiber = yield* resolver
+ .prepare({ agentId: slowAgent.id })
+ .pipe(Effect.forkChild({ startImmediately: true }));
+ yield* Deferred.await(downloadStarted);
+
+ const resolved = yield* resolver.resolve(settings(), "/workspace");
+ expect(resolved.spawn.command).toContain(
+ "/tools/example-agent/1.2.3/linux-x86_64/bin/example-agent",
+ );
+ expect(prepareFiber.pollUnsafe()).toBeUndefined();
+
+ yield* Deferred.succeed(releaseDownload, undefined);
+ expect(yield* Fiber.join(prepareFiber)).toMatchObject({ prepared: true });
+ }).pipe(
+ Effect.scoped,
+ Effect.provide(
+ layerResolver((request) => {
+ if (request.url === registryUrl) {
+ return Effect.succeed(
+ HttpClientResponse.fromWeb(
+ request,
+ new Response(JSON.stringify({ version: "1.0.0", agents: [slowAgent, agent] })),
+ ),
+ );
+ }
+ const response = HttpClientResponse.fromWeb(
+ request,
+ new Response(binaryBytes.buffer as ArrayBuffer),
+ );
+ if (request.url !== slowArchiveUrl) return Effect.succeed(response);
+ return Deferred.succeed(downloadStarted, undefined).pipe(
+ Effect.andThen(Deferred.await(releaseDownload)),
+ Effect.as(response),
+ );
+ }),
+ ),
+ );
+ });
+ });
+
it.effect("reserves a prepared binary until a configured instance inspects it", () => {
const agent = makeAgent({
binary: {
diff --git a/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts b/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts
index 910048b55a7..1c9466dd141 100644
--- a/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts
+++ b/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts
@@ -668,7 +668,13 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
const registryFetchedAtMs = yield* Ref.make<number | undefined>(undefined);
const registryRevision = yield* Ref.make(0);
const registrySemaphore = yield* Semaphore.make(1);
- const installSemaphore = yield* Semaphore.make(1);
+ // Long installs must only block operations on the same agent.
+ const installPermits = new Map<string, Semaphore.Semaphore>();
+ const withInstallPermit = (agentId: string) => {
+ const permit = installPermits.get(agentId) ?? Semaphore.makeUnsafe(1);
+ installPermits.set(agentId, permit);
+ return permit.withPermits(1);
+ };
const preparedBinaryReservations = yield* Ref.make<ReadonlyMap<string, number>>(new Map());
const managerBinDirectory = Effect.fn("AcpRegistryCatalog.managerBinDirectory")(function* (
@@ -1671,13 +1677,13 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
const distribution = yield* compatibleDistribution(agent, "auto");
if (distribution.kind === "binary") {
// Only explicit command overrides may use PATH copies with different ACP capabilities.
- yield* installSemaphore.withPermits(1)(
+ yield* withInstallPermit(agent.id)(
installBinary(agent, distribution.binaryTarget!).pipe(
Effect.tap(() => reservePreparedBinary(agent.id)),
),
);
} else {
- yield* installSemaphore.withPermits(1)(
+ yield* withInstallPermit(agent.id)(
ensureGlobalPackage(agent, distribution.kind, distribution.packageName!, hostEnvironment),
);
}
@@ -1754,7 +1760,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
}
if (distribution.kind === "binary") {
- return yield* installSemaphore.withPermits(1)(
+ return yield* withInstallPermit(agent.id)(
Effect.gen(function* () {
const paths = binaryPaths(agent, distribution.binaryTarget!);
if (paths === undefined) {
@@ -1866,7 +1872,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
command = resolvedOverride;
args = distribution.args;
} else if (distribution.kind === "npx" || distribution.kind === "uvx") {
- const installed = yield* installSemaphore.withPermits(1)(
+ const installed = yield* withInstallPermit(agent.id)(
ensureGlobalPackage(
agent,
distribution.kind,
@@ -1879,7 +1885,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
commandBinDirectory = installed.binDirectory;
} else {
const target = distribution.binaryTarget!;
- command = yield* installSemaphore.withPermits(1)(
+ command = yield* withInstallPermit(agent.id)(
installBinary(agent, target).pipe(
Effect.tap(() => consumePreparedBinaryReservation(agent.id)),
),
@@ -1912,7 +1918,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
const uninstallManagedBinary: AcpRegistryCatalog["Service"]["uninstallManagedBinary"] = (input) =>
hostSettings
.withSnapshot((settings) =>
- installSemaphore.withPermits(1)(
+ withInstallPermit(input.agentId)(
Effect.gen(function* () {
const safeAgentId = yield* decodeBoundedAgentId(input.agentId).pipe(
Effect.mapError(