diff --git a/apps/server/src/provider/AcpRegistryProbe.test.ts b/apps/server/src/provider/AcpRegistryProbe.test.ts index 6c0766d1cf7d8..c7e33e0a577ec 100644 --- a/apps/server/src/provider/AcpRegistryProbe.test.ts +++ b/apps/server/src/provider/AcpRegistryProbe.test.ts @@ -362,6 +362,7 @@ describe("ACP Registry probe", () => { const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({ search: () => Effect.die("unused search"), prepare: () => Effect.die("unused prepare"), + refreshIndex: () => Effect.die("unused refreshIndex"), inspect: () => Effect.die("unused inspect"), uninstallManagedBinary: () => Effect.die("unused uninstall"), resolve: () => @@ -423,6 +424,7 @@ describe("ACP Registry probe", () => { const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({ search: () => Effect.die("unused search"), prepare: () => Effect.die("unused prepare"), + refreshIndex: () => Effect.die("unused refreshIndex"), inspect: () => Effect.die("unused inspect"), uninstallManagedBinary: () => Effect.die("unused uninstall"), resolve: () => @@ -492,6 +494,7 @@ await import(pathToFileURL(agentPath).href); const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({ search: () => Effect.die("unused search"), prepare: () => Effect.die("unused prepare"), + refreshIndex: () => Effect.die("unused refreshIndex"), inspect: () => Effect.die("unused inspect"), uninstallManagedBinary: () => Effect.die("unused uninstall"), resolve: () => diff --git a/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts b/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts index 398c48be3f68c..c7e0e2a7e9757 100644 --- a/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts +++ b/packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts @@ -960,6 +960,104 @@ describe("AcpRegistrySupport", () => { ); }); + it.effect("refreshes a cached registry index according to maxAgeMs and explicit refresh", () => { + const makeBinaryAgent = (version: string) => ({ + ...makeAgent({ + binary: { + "linux-x86_64": { + archive: `https://registry.test/example-agent-${version}.bin`, + cmd: "./bin/example-agent", + }, + }, + }), + version, + }); + let upstream = makeBinaryAgent("1.2.4"); + let online = true; + let registryRequests = 0; + return Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-acp-registry-refresh-index-", + }); + yield* fileSystem.makeDirectory(`${cacheDir}/acp-registry`, { recursive: true }); + yield* fileSystem.writeFileString( + `${cacheDir}/acp-registry/registry.json`, + makeRegistry(makeBinaryAgent("1.2.3")), + ); + const installedV1 = `${cacheDir}/tools/example-agent/1.2.3/linux-x86_64/bin/example-agent`; + yield* fileSystem.makeDirectory(`${cacheDir}/tools/example-agent/1.2.3/linux-x86_64/bin`, { + recursive: true, + }); + yield* fileSystem.writeFileString(installedV1, "#!/bin/sh\necho 1.2.3\n"); + yield* fileSystem.chmod(installedV1, 0o755); + + const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ + cacheDir, + toolsDir: `${cacheDir}/tools`, + registryUrl, + }); + + // Cold inspection stays on disk with no network request. + expect(yield* resolver.inspect(settings())).toMatchObject({ + status: "ready", + version: "1.2.3", + }); + expect(registryRequests).toBe(0); + + // Refreshing a disk-loaded index fetches the latest version once, then reuses it until maxAgeMs. + yield* resolver.refreshIndex({ maxAgeMs: 15 * 60 * 1_000 }); + expect(registryRequests).toBe(1); + expect(yield* resolver.inspect(settings())).toMatchObject({ + status: "ready", + version: "1.2.4", + }); + + upstream = makeBinaryAgent("1.2.5"); + yield* TestClock.adjust("14 minutes"); + yield* resolver.refreshIndex({ maxAgeMs: 15 * 60 * 1_000 }); + expect(registryRequests).toBe(1); + expect(yield* resolver.inspect(settings())).toMatchObject({ version: "1.2.4" }); + + yield* TestClock.adjust("1 minute"); + yield* resolver.refreshIndex({ maxAgeMs: 15 * 60 * 1_000 }); + expect(registryRequests).toBe(2); + expect(yield* resolver.inspect(settings())).toMatchObject({ version: "1.2.5" }); + + upstream = makeBinaryAgent("1.2.6"); + yield* resolver.refreshIndex(); + expect(registryRequests).toBe(3); + expect(yield* resolver.inspect(settings())).toMatchObject({ version: "1.2.6" }); + + // An offline refresh keeps the cached index and retries on the next call. + online = false; + yield* resolver.refreshIndex(); + expect(registryRequests).toBe(4); + expect(yield* resolver.inspect(settings())).toMatchObject({ version: "1.2.6" }); + + online = true; + upstream = makeBinaryAgent("1.2.7"); + yield* resolver.refreshIndex({ maxAgeMs: 15 * 60 * 1_000 }); + expect(registryRequests).toBe(5); + expect(yield* resolver.inspect(settings())).toMatchObject({ version: "1.2.7" }); + }).pipe( + Effect.scoped, + Effect.provide( + layerResolver((request) => { + registryRequests += 1; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + online + ? new Response(makeRegistry(upstream)) + : new Response("unavailable", { status: 503 }), + ), + ); + }), + ), + ); + }); + it.effect( "prepares the registry version despite an older PATH binary and permits explicit overrides", () => { diff --git a/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts b/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts index d3ab397d7aaeb..44a490de3f88c 100644 --- a/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts +++ b/packages/provider-acp-registry/src/server/AcpRegistrySupport.ts @@ -525,6 +525,9 @@ export class AcpRegistryCatalog extends Context.Service< readonly prepare: ( input: AcpRegistryPrepareInput, ) => Effect.Effect; + readonly refreshIndex: (options?: { + readonly maxAgeMs?: number; + }) => Effect.Effect; readonly inspect: ( settings: AcpRegistrySettings, environment?: NodeJS.ProcessEnv, @@ -974,7 +977,9 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct ); }); - const recentRegistry = Effect.fn("AcpRegistryCatalog.recentRegistry")(function* () { + const recentRegistry = Effect.fn("AcpRegistryCatalog.recentRegistry")(function* ( + maxAgeMs = REGISTRY_SEARCH_MAX_AGE_MS, + ) { const current = yield* Ref.get(registryRef); const fetchedAtMs = yield* Ref.get(registryFetchedAtMs); const now = yield* Clock.currentTimeMillis; @@ -983,21 +988,34 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct current !== undefined && fetchedAtMs !== undefined && now >= fetchedAtMs && - now - fetchedAtMs < REGISTRY_SEARCH_MAX_AGE_MS + now - fetchedAtMs < maxAgeMs ) { return current; } return yield* refreshRegistry(); }); + const refreshIndex: AcpRegistryCatalog["Service"]["refreshIndex"] = Effect.fn( + "AcpRegistryCatalog.refreshIndex", + )(function* (options) { + yield* options?.maxAgeMs === undefined ? refreshRegistry() : recentRegistry(options.maxAgeMs); + }); + const loadRegistry = Effect.fn("AcpRegistryCatalog.loadRegistry")(function* () { const current = yield* Ref.get(registryRef); if (current !== undefined) return current; const cached = yield* readCachedRegistry; if (Option.isNone(cached)) return yield* refreshRegistry(); - yield* Ref.set(registryRef, cached.value); - return cached.value; + return yield* registrySemaphore.withPermits(1)( + Effect.gen(function* () { + // A refresh that completed during the disk read owns the stored index. + const latest = yield* Ref.get(registryRef); + if (latest !== undefined) return latest; + yield* Ref.set(registryRef, cached.value); + return cached.value; + }), + ); }); const runCommand = Effect.fn("AcpRegistryCatalog.runCommand")(function* ( @@ -2154,6 +2172,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct return AcpRegistryCatalog.of({ search, prepare, + refreshIndex, inspect, resolve, uninstallManagedBinary, diff --git a/packages/provider-acp-registry/src/server/auth.test.ts b/packages/provider-acp-registry/src/server/auth.test.ts index 45f72554152b6..5edff88623800 100644 --- a/packages/provider-acp-registry/src/server/auth.test.ts +++ b/packages/provider-acp-registry/src/server/auth.test.ts @@ -54,6 +54,7 @@ const catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"] = { resolve: () => Effect.succeed(resolved), search: () => Effect.die("unused"), prepare: () => Effect.die("unused"), + refreshIndex: () => Effect.void, uninstallManagedBinary: () => Effect.die("unused"), }; diff --git a/packages/provider-acp-registry/src/server/driver.test.ts b/packages/provider-acp-registry/src/server/driver.test.ts index 684eb21d7456b..9451c9313ae7a 100644 --- a/packages/provider-acp-registry/src/server/driver.test.ts +++ b/packages/provider-acp-registry/src/server/driver.test.ts @@ -9,17 +9,22 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { toCustomModelSetting } from "@t3tools/shared/model"; import { AcpRegistrySettings } from "../settings.ts"; import { describe, expect, it } from "@effect/vitest"; +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; +import * as HttpClient from "effect/http/HttpClient"; +import * as HttpClientResponse from "effect/http/HttpClientResponse"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; @@ -61,6 +66,7 @@ function catalogWithInspection( return { search: () => Effect.die("unused search"), prepare: () => Effect.die("unused prepare"), + refreshIndex: () => Effect.void, inspect: () => Effect.succeed(inspection), resolve: () => Effect.die("unused resolve"), uninstallManagedBinary: () => Effect.die("unused uninstall"), @@ -949,6 +955,55 @@ createInterface({ input: process.stdin }).on("line", (line) => { }); `; +type RegistryTarget = "linux-x86_64" | "darwin-aarch64"; + +const registryUrl = "https://registry.test/registry.json"; + +const makeBinaryRegistry = (version: string, target: RegistryTarget = "linux-x86_64") => + JSON.stringify({ + version: "1.0.0", + agents: [ + { + id: "test-agent", + name: "Test Agent", + version, + description: "Test binary agent", + distribution: { + binary: { + [target]: { + archive: `https://registry.test/test-agent-${version}.bin`, + cmd: "./bin/test-agent", + }, + }, + }, + }, + ], + }); + +// A disk-cached 1.0.0 index with its binary installed, as left behind by an earlier session. +const seedInstalledBinaryAgent = Effect.fn("seedInstalledBinaryAgent")(function* (prefix: string) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix }); + const agentPath = path.join(directory, "agent.mjs"); + const modelsPath = path.join(directory, "models.json"); + yield* fileSystem.writeFileString(agentPath, fakeAgentSource); + yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["model-a", "model-b"])); + yield* fileSystem.makeDirectory(path.join(directory, "acp-registry"), { recursive: true }); + yield* fileSystem.writeFileString( + path.join(directory, "acp-registry", "registry.json"), + makeBinaryRegistry("1.0.0"), + ); + const installedV1 = installedBinaryPath(path, directory, "1.0.0"); + yield* fileSystem.makeDirectory(path.dirname(installedV1), { recursive: true }); + yield* fileSystem.writeFileString(installedV1, "#!/bin/sh\necho 1.0.0\n"); + yield* fileSystem.chmod(installedV1, 0o755); + return { directory, agentPath, modelsPath }; +}); + +const installedBinaryPath = (path: Path.Path, directory: string, version: string) => + path.join(directory, "tools", "test-agent", version, "linux-x86_64", "bin", "test-agent"); + const driverLayer = TestProviderHost.layer({ runBackgroundWork: false }).pipe( Layer.provideMerge(AcpRegistryRuntimeCoordinator.layer), Layer.provideMerge(IdAllocator.layer), @@ -1188,4 +1243,210 @@ it.layer(driverLayer, { excludeTestServices: true })("AcpRegistryDriver", (it) = ); }), ); + + it.effect("refreshes a cached registry index on enrichment and explicit cache invalidation", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { directory, agentPath, modelsPath } = + yield* seedInstalledBinaryAgent("acp-driver-update-"); + + let latestVersion = "2.0.0"; + let registryRequests = 0; + const realCatalog = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ + cacheDir: directory, + toolsDir: path.join(directory, "tools"), + registryUrl, + }).pipe( + Effect.provideService(HostProcess.Platform, "linux"), + Effect.provideService(HostProcess.Architecture, "x64"), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request) => { + if (request.url === registryUrl) { + registryRequests += 1; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(makeBinaryRegistry(latestVersion)), + ), + ); + } + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(`#!/bin/sh\necho ${latestVersion}\n`), + ), + ); + }), + ), + ); + const catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"] = { + ...realCatalog, + resolve: (settings, cwd, environment) => + realCatalog.resolve(settings, cwd, environment).pipe( + Effect.map((resolved) => ({ + ...resolved, + spawn: { command: process.execPath, args: [agentPath, modelsPath] }, + })), + ), + }; + + const instance = yield* AcpRegistryDriver.create({ + instanceId: identity.instanceId, + displayName: identity.displayName, + enabled: true, + environment: [], + config: decodeSettings({ agentId: "test-agent" }), + }).pipe(Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog)); + + const waitForEnriched = () => + instance.snapshot.streamChanges.pipe( + Stream.filter((snapshot) => snapshot.nativeSessions !== undefined), + Stream.runHead, + ); + + // Initial enrichment refreshes the disk-cached 1.0.0 index and installs 2.0.0. + const upgradedToV2 = yield* waitForEnriched().pipe( + Effect.forkScoped({ startImmediately: true }), + ); + yield* instance.snapshot.refresh; + const first = Option.getOrThrow(yield* Fiber.join(upgradedToV2)); + expect(first.version).toBe("2.0.0"); + expect(yield* fileSystem.exists(installedBinaryPath(path, directory, "2.0.0"))).toBe(true); + + // Explicit cache invalidation refreshes past the in-memory reuse window and installs 3.0.0. + latestVersion = "3.0.0"; + const upgradedToV3 = yield* waitForEnriched().pipe( + Effect.forkScoped({ startImmediately: true }), + ); + yield* instance.invalidateCaches ?? Effect.void; + yield* instance.snapshot.refresh; + const second = Option.getOrThrow(yield* Fiber.join(upgradedToV3)); + expect(second.version).toBe("3.0.0"); + expect(yield* fileSystem.exists(installedBinaryPath(path, directory, "3.0.0"))).toBe(true); + + // Disabled instances must not fetch the registry on cache invalidation. + const disabledRequestsBefore = registryRequests; + const disabled = yield* AcpRegistryDriver.create({ + instanceId: ProviderInstanceId.make("acpRegistry_disabled"), + displayName: "Disabled ACP", + enabled: false, + environment: [], + config: decodeSettings({ agentId: "test-agent" }), + }).pipe(Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog)); + yield* disabled.invalidateCaches ?? Effect.void; + expect(registryRequests).toBe(disabledRequestsBefore); + }), + ); +}); + +it.layer(driverLayer)("AcpRegistryDriver registry recovery", (it) => { + it.effect( + "recovers when a refreshed index moves from compatible to incompatible and back without cache invalidation", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { directory, agentPath, modelsPath } = + yield* seedInstalledBinaryAgent("acp-driver-recover-"); + + let remoteVersion = "2.0.0"; + let remoteTarget: RegistryTarget = "darwin-aarch64"; + let resolvedProbes = 0; + const restoredRefreshDone = yield* Deferred.make(); + const realCatalog = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ + cacheDir: directory, + toolsDir: path.join(directory, "tools"), + registryUrl, + }).pipe( + Effect.provideService(HostProcess.Platform, "linux"), + Effect.provideService(HostProcess.Architecture, "x64"), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response( + request.url === registryUrl + ? makeBinaryRegistry(remoteVersion, remoteTarget) + : `#!/bin/sh\necho ${remoteVersion}\n`, + ), + ), + ), + ), + ), + ); + const catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"] = { + ...realCatalog, + refreshIndex: (options) => + realCatalog + .refreshIndex(options) + .pipe( + Effect.tap(() => + remoteVersion === "3.0.0" + ? Deferred.succeed(restoredRefreshDone, undefined).pipe(Effect.asVoid) + : Effect.void, + ), + ), + resolve: (settings, cwd, environment) => + realCatalog.resolve(settings, cwd, environment).pipe( + Effect.tap(() => + Effect.sync(() => { + resolvedProbes += 1; + }), + ), + Effect.map((resolved) => ({ + ...resolved, + spawn: { command: process.execPath, args: [agentPath, modelsPath] }, + })), + ), + }; + + const instance = yield* AcpRegistryDriver.create({ + instanceId: identity.instanceId, + displayName: identity.displayName, + enabled: true, + environment: [], + config: decodeSettings({ agentId: "test-agent" }), + }).pipe(Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog)); + + // 1. Compatible 1.0.0 transitions to incompatible 2.0.0. + const becameUnsupported = yield* instance.snapshot.streamChanges.pipe( + Stream.filter((snapshot) => !snapshot.installed && snapshot.version === "2.0.0"), + Stream.runHead, + Effect.forkScoped({ startImmediately: true }), + ); + yield* instance.snapshot.refresh; + const unsupported = Option.getOrThrow(yield* Fiber.join(becameUnsupported)); + expect(unsupported.status).toBe("error"); + + // 2. Upstream restores a compatible 3.0.0; post-TTL refresh updates the index while keeping probe suppressed. + const resolvedBeforeRecovery = resolvedProbes; + remoteVersion = "3.0.0"; + remoteTarget = "linux-x86_64"; + yield* TestClock.adjust("16 minutes"); + yield* instance.snapshot.refresh; + yield* Deferred.await(restoredRefreshDone); + expect(resolvedProbes).toBe(resolvedBeforeRecovery); + + // 3. The next periodic refresh recovers and installs 3.0.0 without invalidateCaches. + const recovered = yield* instance.snapshot.streamChanges.pipe( + Stream.filter( + (snapshot) => + snapshot.installed && + snapshot.version === "3.0.0" && + snapshot.nativeSessions !== undefined, + ), + Stream.runHead, + Effect.forkScoped({ startImmediately: true }), + ); + yield* instance.snapshot.refresh; + const ready = Option.getOrThrow(yield* Fiber.join(recovered)); + expect(ready.status).toBe("ready"); + expect(resolvedProbes).toBeGreaterThan(resolvedBeforeRecovery); + expect(yield* fileSystem.exists(installedBinaryPath(path, directory, "3.0.0"))).toBe(true); + }), + ); }); diff --git a/packages/provider-acp-registry/src/server/driver.ts b/packages/provider-acp-registry/src/server/driver.ts index 46d8ed3219aa4..5f2a70acbc5fa 100644 --- a/packages/provider-acp-registry/src/server/driver.ts +++ b/packages/provider-acp-registry/src/server/driver.ts @@ -793,6 +793,9 @@ export const AcpRegistryDriver: ProviderDriver 0 + ? catalog.refreshIndex({ maxAgeMs: PROBE_SUCCESS_TTL_MS }).pipe(Effect.ignore) + : Effect.void; + } const publishEnrichment = ( Option.isSome(runtimeCoordinator) ? runtimeCoordinator.value.runBackgroundProbe( @@ -1009,6 +1018,13 @@ export const AcpRegistryDriver: ProviderDriver 0 + ? catalog.refreshIndex().pipe(Effect.ignore) + : Effect.void, + ), ), orchestrationAdapter, textGeneration: makeUnsupportedTextGeneration(),