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/src/provider/AcpRegistryProbe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () =>
Expand Down Expand Up @@ -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: () =>
Expand Down Expand Up @@ -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: () =>
Expand Down
110 changes: 110 additions & 0 deletions packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,116 @@ 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" });

// Explicit refresh bypasses the reuse window and lets resolve install the new binary.
upstream = makeBinaryAgent("1.2.6");
yield* resolver.refreshIndex();
expect(registryRequests).toBe(3);
const resolved = yield* resolver.resolve(settings(), "/workspace", {});
expect(resolved.spawn.command).toContain(
"/tools/example-agent/1.2.6/linux-x86_64/bin/example-agent",
);

// Offline refresh keeps the cached index and retries on the next refreshIndex 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) => {
if (request.url === registryUrl) {
registryRequests += 1;
return Effect.succeed(
HttpClientResponse.fromWeb(
request,
online
? new Response(makeRegistry(upstream))
: new Response("unavailable", { status: 503 }),
),
);
}
return Effect.succeed(
HttpClientResponse.fromWeb(
request,
new Response(`#!/bin/sh\necho ${upstream.version}\n`),
),
);
}),
),
);
});

it.effect(
"prepares the registry version despite an older PATH binary and permits explicit overrides",
() => {
Expand Down
27 changes: 23 additions & 4 deletions packages/provider-acp-registry/src/server/AcpRegistrySupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ export class AcpRegistryCatalog extends Context.Service<
readonly prepare: (
input: AcpRegistryPrepareInput,
) => Effect.Effect<AcpRegistryPrepareResult, AcpRegistryError>;
readonly refreshIndex: (options?: {
readonly maxAgeMs?: number;
}) => Effect.Effect<void, AcpRegistryError>;
readonly inspect: (
settings: AcpRegistrySettings,
environment?: NodeJS.ProcessEnv,
Expand Down Expand Up @@ -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;
Expand All @@ -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* (
Expand Down Expand Up @@ -2154,6 +2172,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
return AcpRegistryCatalog.of({
search,
prepare,
refreshIndex,
inspect,
resolve,
uninstallManagedBinary,
Expand Down
1 change: 1 addition & 0 deletions packages/provider-acp-registry/src/server/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

Expand Down
Loading
Loading