The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { assert, it } from "@effect/vitest"; |
| 4 | import { |
| 5 | ProviderDriverKind, |
| 6 | ProviderInstanceId, |
| 7 | ProviderSessionId, |
| 8 | ThreadId, |
| 9 | ProviderSetupError, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as Deferred from "effect/Deferred"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Exit from "effect/Exit"; |
| 14 | import * as Fiber from "effect/Fiber"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | |
| 18 | import * as ProviderAuthFlow from "@t3tools/provider-core/server/providerAuthFlow"; |
| 19 | import type { ProviderAuthController } from "../provider/ProviderAuthService.ts"; |
| 20 | import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; |
| 21 | import * as ModelManifest from "../provider/ModelManifest.ts"; |
| 22 | import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; |
| 23 | import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 24 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 25 | |
| 26 | const driver = ProviderDriverKind.make("codex"); |
| 27 | const personalId = ProviderInstanceId.make("codex_personal"); |
| 28 | const workId = ProviderInstanceId.make("codex_work"); |
| 29 | |
| 30 | const makeAdapter = ( |
| 31 | instanceId: ProviderInstanceId, |
| 32 | ): ProviderAdapter.ProviderAdapterV2["Service"] => |
| 33 | ({ |
| 34 | instanceId, |
| 35 | driver, |
| 36 | getCapabilities: () => Effect.die("capabilities are not used by this registry test"), |
| 37 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 38 | openSession: () => Effect.die("sessions are not used by this registry test"), |
| 39 | }) as ProviderAdapter.ProviderAdapterV2["Service"]; |
| 40 | |
| 41 | const makeInstance = ( |
| 42 | instanceId: ProviderInstanceId, |
| 43 | orchestrationAdapter: ProviderAdapter.ProviderAdapterV2["Service"], |
| 44 | version: string | null = null, |
| 45 | refreshedVersion = version, |
| 46 | ): ProviderInstance => ({ |
| 47 | instanceId, |
| 48 | driverKind: driver, |
| 49 | continuationIdentity: { |
| 50 | driverKind: driver, |
| 51 | continuationKey: `codex:test:${instanceId}`, |
| 52 | }, |
| 53 | displayName: String(instanceId), |
| 54 | enabled: true, |
| 55 | snapshot: { |
| 56 | getSnapshot: Effect.succeed({ version }), |
| 57 | refresh: Effect.succeed({ version: refreshedVersion }), |
| 58 | } as unknown as ProviderInstance["snapshot"], |
| 59 | orchestrationAdapter, |
| 60 | textGeneration: {} as ProviderInstance["textGeneration"], |
| 61 | }); |
| 62 | |
| 63 | const personalAdapter = makeAdapter(personalId); |
| 64 | const workAdapter = makeAdapter(workId); |
| 65 | const instances = [ |
| 66 | makeInstance(personalId, personalAdapter), |
| 67 | makeInstance(workId, workAdapter), |
| 68 | ] as const; |
| 69 | const layerInstanceRegistry = Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { |
| 70 | getInstance: (instanceId) => |
| 71 | Effect.succeed(instances.find((instance) => instance.instanceId === instanceId)), |
| 72 | listInstances: Effect.succeed(instances), |
| 73 | listUnavailable: Effect.succeed([]), |
| 74 | streamChanges: Stream.empty, |
| 75 | subscribeChanges: Effect.never, |
| 76 | }); |
| 77 | const layerRegistry = ProviderAdapterRegistry.layerFromProviderInstanceRegistry.pipe( |
| 78 | Layer.provide(ModelManifest.layerTest), |
| 79 | ); |
| 80 | const layerTest = layerRegistry.pipe(Layer.provide(layerInstanceRegistry)); |
| 81 | |
| 82 | it.effect("routes two configured instances of the same driver independently", () => |
| 83 | Effect.gen(function* () { |
| 84 | const registry = yield* ProviderAdapterRegistry.ProviderAdapterRegistryV2; |
| 85 | |
| 86 | assert.strictEqual((yield* registry.get(personalId)).instanceId, personalId); |
| 87 | assert.strictEqual((yield* registry.get(workId)).instanceId, workId); |
| 88 | assert.deepEqual(yield* registry.list(), [personalId, workId]); |
| 89 | }).pipe(Effect.provide(layerTest)), |
| 90 | ); |
| 91 | |
| 92 | const openOn = Effect.fn("openOn")(function* (instance: ProviderInstance) { |
| 93 | const registry = yield* Effect.service(ProviderAdapterRegistry.ProviderAdapterRegistryV2).pipe( |
| 94 | Effect.provide( |
| 95 | layerRegistry.pipe( |
| 96 | Layer.provide( |
| 97 | Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ |
| 98 | getInstance: () => Effect.succeed(instance), |
| 99 | }), |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | ); |
| 104 | return yield* (yield* registry.get(instance.instanceId)) |
| 105 | .openSession({ |
| 106 | threadId: ThreadId.make("gated-thread"), |
| 107 | providerSessionId: ProviderSessionId.make("gated-session"), |
| 108 | modelSelection: { instanceId: instance.instanceId, model: "test-model" }, |
| 109 | runtimePolicy: { runtimeMode: "full-access", interactionMode: "default", cwd: "/workspace" }, |
| 110 | }) |
| 111 | .pipe(Effect.exit); |
| 112 | }); |
| 113 | |
| 114 | it.effect("refuses a session only when a fresh probe still rates the version broken", () => |
| 115 | Effect.gen(function* () { |
| 116 | const opened = { ...personalAdapter, openSession: () => Effect.succeed({} as never) }; |
| 117 | const refused = yield* openOn(makeInstance(personalId, opened, "0.100.0")); |
| 118 | assert.include(JSON.stringify(refused), "0.100.0 does not work with this T3 Code release"); |
| 119 | // Updated since the last probe. |
| 120 | assert.isTrue( |
| 121 | Exit.isSuccess(yield* openOn(makeInstance(personalId, opened, "0.100.0", "0.200.0"))), |
| 122 | ); |
| 123 | // An adapter that rates the handshake version is not judged on the probe. |
| 124 | const handshake = { ...opened, checksVersionAtHandshake: true }; |
| 125 | assert.isTrue(Exit.isSuccess(yield* openOn(makeInstance(personalId, handshake, "0.100.0")))); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | it.effect( |
| 130 | "blocks a new session while another instance changes their shared provider credentials", |
| 131 | () => |
| 132 | Effect.gen(function* () { |
| 133 | const unused = () => Effect.die("unused auth operation"); |
| 134 | const auth: ProviderAuthController = { |
| 135 | credentialBinding: { owner: "provider", key: "shared-cli" }, |
| 136 | isChangingCredentials: Effect.succeed(false), |
| 137 | start: unused, |
| 138 | complete: unused, |
| 139 | cancel: unused, |
| 140 | logout: unused, |
| 141 | subscribe: () => Stream.empty, |
| 142 | }; |
| 143 | const related = [ |
| 144 | { ...instances[0], auth }, |
| 145 | { ...instances[1], auth: { ...auth, isChangingCredentials: Effect.succeed(true) } }, |
| 146 | ]; |
| 147 | const registry = yield* Effect.service( |
| 148 | ProviderAdapterRegistry.ProviderAdapterRegistryV2, |
| 149 | ).pipe( |
| 150 | Effect.provide( |
| 151 | layerRegistry.pipe( |
| 152 | Layer.provide( |
| 153 | Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ |
| 154 | getInstance: (id) => |
| 155 | Effect.succeed(related.find((instance) => instance.instanceId === id)), |
| 156 | listInstances: Effect.succeed(related), |
| 157 | }), |
| 158 | ), |
| 159 | ), |
| 160 | ), |
| 161 | ); |
| 162 | const adapter = yield* registry.get(personalId); |
| 163 | const error = yield* adapter |
| 164 | .openSession({ |
| 165 | threadId: ThreadId.make("new-thread"), |
| 166 | providerSessionId: ProviderSessionId.make("new-session"), |
| 167 | modelSelection: { instanceId: personalId, model: "test-model" }, |
| 168 | runtimePolicy: { |
| 169 | runtimeMode: "full-access", |
| 170 | interactionMode: "default", |
| 171 | cwd: "/workspace", |
| 172 | }, |
| 173 | }) |
| 174 | .pipe(Effect.flip); |
| 175 | assert.instanceOf(error, ProviderAdapter.ProviderAdapterOpenSessionError); |
| 176 | assert.instanceOf(error.cause, ProviderSetupError); |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | it.effect("interrupts admitted session startup when a shared peer signs out", () => |
| 181 | Effect.gen(function* () { |
| 182 | const entered = yield* Deferred.make<void>(); |
| 183 | const stopped = yield* Deferred.make<void>(); |
| 184 | const binding = { owner: "provider" as const, key: "shared-cli" }; |
| 185 | const auth = yield* ProviderAuthFlow.make({ |
| 186 | instanceId: personalId, |
| 187 | credentialBinding: binding, |
| 188 | methods: Effect.succeed([]), |
| 189 | authenticate: () => Effect.void, |
| 190 | logout: Effect.void, |
| 191 | }); |
| 192 | const peerAuth = yield* ProviderAuthFlow.make({ |
| 193 | instanceId: workId, |
| 194 | credentialBinding: binding, |
| 195 | methods: Effect.succeed([]), |
| 196 | authenticate: () => Effect.void, |
| 197 | logout: Effect.void, |
| 198 | }); |
| 199 | const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = { |
| 200 | ...workAdapter, |
| 201 | openSession: () => |
| 202 | Effect.gen(function* () { |
| 203 | yield* Deferred.succeed(entered, undefined); |
| 204 | return yield* Effect.never; |
| 205 | }).pipe(Effect.ensuring(Deferred.succeed(stopped, undefined))), |
| 206 | }; |
| 207 | const related = [ |
| 208 | { ...instances[0], auth }, |
| 209 | { ...instances[1], auth: peerAuth, orchestrationAdapter: adapter }, |
| 210 | ]; |
| 211 | const registry = yield* Effect.service(ProviderAdapterRegistry.ProviderAdapterRegistryV2).pipe( |
| 212 | Effect.provide( |
| 213 | layerRegistry.pipe( |
| 214 | Layer.provide( |
| 215 | Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ |
| 216 | getInstance: (id) => |
| 217 | Effect.succeed(related.find((instance) => instance.instanceId === id)), |
| 218 | listInstances: Effect.succeed(related), |
| 219 | }), |
| 220 | ), |
| 221 | ), |
| 222 | ), |
| 223 | ); |
| 224 | const guarded = yield* registry.get(workId); |
| 225 | const startup = yield* guarded |
| 226 | .openSession({ |
| 227 | threadId: ThreadId.make("shared-startup"), |
| 228 | providerSessionId: ProviderSessionId.make("shared-session"), |
| 229 | modelSelection: { instanceId: workId, model: "test-model" }, |
| 230 | runtimePolicy: { |
| 231 | runtimeMode: "full-access", |
| 232 | interactionMode: "default", |
| 233 | cwd: "/workspace", |
| 234 | }, |
| 235 | }) |
| 236 | .pipe(Effect.forkChild); |
| 237 | yield* Deferred.await(entered); |
| 238 | yield* auth.logout(Effect.void); |
| 239 | yield* Deferred.await(stopped); |
| 240 | assert.isTrue(Exit.isFailure(yield* Fiber.await(startup))); |
| 241 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 242 | ); |