Skip to content

Commit 5e3c54d

Browse files
fix(acp-registry): model refresh picks up models an agent adds upstream
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 94f54a5 commit 5e3c54d

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎packages/provider-acp-registry/src/server/driver.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1+
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
13
import { AcpRegistryOperationError, ProviderInstanceId } from "@t3tools/contracts";
4+
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
5+
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
6+
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
7+
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
28
import { AcpRegistrySettings } from "../settings.ts";
39
import { describe, expect, it } from "@effect/vitest";
410
import * as Effect from "effect/Effect";
11+
import * as Fiber from "effect/Fiber";
12+
import * as FileSystem from "effect/FileSystem";
13+
import * as Layer from "effect/Layer";
514
import * as Option from "effect/Option";
15+
import * as Path from "effect/Path";
616
import * as Schema from "effect/Schema";
17+
import * as Stream from "effect/Stream";
718

19+
import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts";
820
import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
921
import {
22+
AcpRegistryDriver,
1023
applyAcpRegistryAvailableCommands,
1124
applyAcpRegistryLiveConfiguration,
1225
buildCheckedAcpRegistrySnapshot,
1326
checkAcpRegistryProviderReadiness,
1427
checkAcpRegistryProviderStatus,
1528
} from "./driver.ts";
29+
import { normalizeAcpRegistryLiveConfiguration } from "./probe.ts";
1630

1731
const decodeSettings = Schema.decodeSync(AcpRegistrySettings);
1832
const identity = {
@@ -432,3 +446,112 @@ describe("acpRegistrySnapshotReadiness", () => {
432446
}),
433447
);
434448
});
449+
450+
const fakeAgentSource = `
451+
import { readFileSync } from "node:fs";
452+
import { createInterface } from "node:readline";
453+
const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
454+
createInterface({ input: process.stdin }).on("line", (line) => {
455+
const { id, method } = JSON.parse(line);
456+
if (id === undefined) return;
457+
if (method === "initialize") {
458+
send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } });
459+
} else if (method === "session/new") {
460+
const modelIds = JSON.parse(readFileSync(process.argv[2], "utf8"));
461+
send({
462+
id,
463+
result: {
464+
sessionId: "probe-session",
465+
models: {
466+
currentModelId: modelIds[0],
467+
availableModels: modelIds.map((modelId) => ({ modelId, name: modelId })),
468+
},
469+
},
470+
});
471+
send({
472+
method: "session/update",
473+
params: {
474+
sessionId: "probe-session",
475+
update: { sessionUpdate: "available_commands_update", availableCommands: [] },
476+
},
477+
});
478+
} else {
479+
send({ id, result: {} });
480+
}
481+
});
482+
`;
483+
484+
const driverLayer = TestProviderHost.layer({ runBackgroundWork: false }).pipe(
485+
Layer.provideMerge(AcpRegistryRuntimeCoordinator.layer),
486+
Layer.provideMerge(IdAllocator.layer),
487+
Layer.provideMerge(McpProviderSessions.layer),
488+
Layer.provideMerge(
489+
Layer.succeed(
490+
ProviderEventLoggers.ProviderEventLoggers,
491+
ProviderEventLoggers.NoOpProviderEventLoggers,
492+
),
493+
),
494+
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
495+
);
496+
497+
it.layer(driverLayer, { excludeTestServices: true })("AcpRegistryDriver", (it) => {
498+
it.effect("drops cached and session-reported models when its caches are invalidated", () =>
499+
Effect.gen(function* () {
500+
const fileSystem = yield* FileSystem.FileSystem;
501+
const path = yield* Path.Path;
502+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-driver-" });
503+
const agentPath = path.join(directory, "agent.mjs");
504+
const modelsPath = path.join(directory, "models.json");
505+
yield* fileSystem.writeFileString(agentPath, fakeAgentSource);
506+
yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model"]));
507+
const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
508+
509+
const instance = yield* AcpRegistryDriver.create({
510+
instanceId: identity.instanceId,
511+
displayName: identity.displayName,
512+
enabled: true,
513+
environment: [],
514+
config: decodeSettings({ agentId: "test-agent" }),
515+
}).pipe(
516+
Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, {
517+
...catalogWithInspection({
518+
status: "ready",
519+
agentId: "test-agent",
520+
version: "1.0.0",
521+
distribution: "npx",
522+
}),
523+
resolve: () =>
524+
Effect.succeed({
525+
distribution: "npx" as const,
526+
spawn: { command: process.execPath, args: [agentPath, modelsPath] },
527+
}),
528+
}),
529+
);
530+
const modelSlugs = instance.snapshot.streamChanges.pipe(
531+
Stream.map((snapshot) => snapshot.models.map((model) => model.slug)),
532+
);
533+
const waitForModels = (expected: ReadonlyArray<string>) =>
534+
modelSlugs.pipe(
535+
Stream.filter((slugs) => slugs.join() === expected.join()),
536+
Stream.runHead,
537+
);
538+
539+
const probed = yield* waitForModels(["old-model"]).pipe(Effect.forkScoped);
540+
yield* instance.snapshot.refresh;
541+
yield* Fiber.join(probed);
542+
yield* coordinator.publishLiveConfiguration(
543+
identity.instanceId,
544+
normalizeAcpRegistryLiveConfiguration([], undefined, {
545+
currentModelId: "old-model",
546+
availableModels: [{ modelId: "old-model", name: "old-model" }],
547+
}),
548+
);
549+
yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model", "new-model"]));
550+
551+
const refreshed = yield* waitForModels(["old-model", "new-model"]).pipe(Effect.forkScoped);
552+
yield* instance.invalidateCaches ?? Effect.void;
553+
yield* instance.snapshot.refresh;
554+
expect(Option.isSome(yield* Fiber.join(refreshed))).toBe(true);
555+
}),
556+
);
557+
});

‎packages/provider-acp-registry/src/server/driver.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ export const AcpRegistryDriver: ProviderDriver<AcpRegistrySettings, AcpRegistryD
825825
...snapshot,
826826
refresh: snapshot.refresh.pipe(Effect.tap(() => controller.refreshMethods ?? Effect.void)),
827827
},
828+
invalidateCaches: (Option.isSome(runtimeCoordinator)
829+
? runtimeCoordinator.value.clearLiveConfiguration(instanceId)
830+
: Effect.void
831+
).pipe(Effect.andThen(liveSnapshotSemaphore.withPermit(invalidateEnrichmentCache))),
828832
orchestrationAdapter,
829833
textGeneration: makeUnsupportedTextGeneration(),
830834
acpSessionManagement: {

0 commit comments

Comments
 (0)