|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
1 | 3 | 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"; |
2 | 8 | import { AcpRegistrySettings } from "../settings.ts"; |
3 | 9 | import { describe, expect, it } from "@effect/vitest"; |
4 | 10 | 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"; |
5 | 14 | import * as Option from "effect/Option"; |
| 15 | +import * as Path from "effect/Path"; |
6 | 16 | import * as Schema from "effect/Schema"; |
| 17 | +import * as Stream from "effect/Stream"; |
7 | 18 |
|
| 19 | +import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; |
8 | 20 | import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; |
9 | 21 | import { |
| 22 | + AcpRegistryDriver, |
10 | 23 | applyAcpRegistryAvailableCommands, |
11 | 24 | applyAcpRegistryLiveConfiguration, |
12 | 25 | buildCheckedAcpRegistrySnapshot, |
13 | 26 | checkAcpRegistryProviderReadiness, |
14 | 27 | checkAcpRegistryProviderStatus, |
15 | 28 | } from "./driver.ts"; |
| 29 | +import { normalizeAcpRegistryLiveConfiguration } from "./probe.ts"; |
16 | 30 |
|
17 | 31 | const decodeSettings = Schema.decodeSync(AcpRegistrySettings); |
18 | 32 | const identity = { |
@@ -432,3 +446,112 @@ describe("acpRegistrySnapshotReadiness", () => { |
432 | 446 | }), |
433 | 447 | ); |
434 | 448 | }); |
| 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 | +}); |
0 commit comments