| ... | @@ -0,0 +1,219 @@ |
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import { ProviderInstanceId, type PiSettings } from "@t3tools/contracts"; |
| 4 | import * as Deferred from "effect/Deferred"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Fiber from "effect/Fiber"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Queue from "effect/Queue"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as Sink from "effect/Sink"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import * as TestClock from "effect/testing/TestClock"; |
| 13 | import { HttpClient } from "effect/http"; |
| 14 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 15 | |
| 16 | import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts"; |
| 17 | import * as ServerConfig from "../../config.ts"; |
| 18 | import * as IdAllocator from "../../orchestration-v2/IdAllocator.ts"; |
| 19 | import * as ServerSettings from "../../serverSettings.ts"; |
| 20 | import { PiDriver } from "./PiDriver.ts"; |
| 21 | |
| 22 | const layerTest = Layer.mergeAll( |
| 23 | ServerConfig.layerTest("/machine", { prefix: "t3-pi-driver-" }), |
| 24 | IdAllocator.layer, |
| 25 | ServerSettings.layerTest({ enableProviderUpdateChecks: false }), |
| 26 | Layer.mock(BackgroundPolicy.BackgroundPolicy)({ |
| 27 | shouldRunScopeWork: () => Effect.succeed(false), |
| 28 | }), |
| 29 | Layer.succeed( |
| 30 | HttpClient.HttpClient, |
| 31 | HttpClient.make(() => Effect.die("Unexpected HTTP")), |
| 32 | ), |
| 33 | ).pipe(Layer.provideMerge(NodeServices.layer)); |
| 34 | |
| 35 | const decodeRequest = Schema.decodeSync( |
| 36 | Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)), |
| 37 | ); |
| 38 | const encoder = new TextEncoder(); |
| 39 | const personalSkill = { |
| 40 | name: "skill:personal", |
| 41 | source: "skill", |
| 42 | sourceInfo: { scope: "user", path: "/home/.pi/agent/skills/personal/SKILL.md" }, |
| 43 | }; |
| 44 | |
| 45 | // Respond through the real stdio transport, with a distinct command catalog for each cwd. |
| 46 | const makePiSpawner = Effect.gen(function* () { |
| 47 | const pendingCommand = yield* Deferred.make<void>(); |
| 48 | const launches: Array<ChildProcess.StandardCommand> = []; |
| 49 | const spawner = ChildProcessSpawner.make((command) => |
| 50 | Effect.gen(function* () { |
| 51 | assert.isTrue(ChildProcess.isStandardCommand(command)); |
| 52 | if (!ChildProcess.isStandardCommand(command)) return yield* Effect.die("Unexpected pipeline"); |
| 53 | launches.push(command); |
| 54 | const version = command.args.includes("--version"); |
| 55 | const stdout = yield* Queue.unbounded<Uint8Array>(); |
| 56 | const cwd = command.options.cwd; |
| 57 | return ChildProcessSpawner.makeHandle({ |
| 58 | // Outside the valid PID range, so transport cleanup cannot signal a real process. |
| 59 | pid: ChildProcessSpawner.ProcessId(999_999_999), |
| 60 | exitCode: version ? Effect.succeed(ChildProcessSpawner.ExitCode(0)) : Effect.never, |
| 61 | isRunning: Effect.succeed(!version), |
| 62 | kill: () => Effect.void, |
| 63 | unref: Effect.succeed(Effect.void), |
| 64 | stdin: Sink.forEach((chunk: Uint8Array) => { |
| 65 | const request = decodeRequest(new TextDecoder().decode(chunk).trim()); |
| 66 | if (cwd === "/pending" && request.type === "get_commands") { |
| 67 | return Deferred.succeed(pendingCommand, undefined).pipe(Effect.asVoid); |
| 68 | } |
| 69 | const failed = cwd === "/failed" && request.type === "get_commands"; |
| 70 | const data = |
| 71 | request.type === "get_commands" |
| 72 | ? { |
| 73 | commands: [ |
| 74 | personalSkill, |
| 75 | ...(cwd === "/machine" |
| 76 | ? [] |
| 77 | : [ |
| 78 | { |
| 79 | name: `skill:${cwd?.slice(1)}`, |
| 80 | source: "skill", |
| 81 | sourceInfo: { |
| 82 | scope: "project", |
| 83 | path: `${cwd}/.agents/skills/SKILL.md`, |
| 84 | }, |
| 85 | }, |
| 86 | { name: `prompt-${cwd?.slice(1)}`, source: "prompt" }, |
| 87 | ]), |
| 88 | ], |
| 89 | } |
| 90 | : request.type === "get_available_models" |
| 91 | ? { models: [{ provider: "test", id: "model" }] } |
| 92 | : {}; |
| 93 | return Queue.offer( |
| 94 | stdout, |
| 95 | encoder.encode( |
| 96 | `${JSON.stringify({ type: "response", id: request.id, success: !failed, data, ...(failed ? { error: "commands unavailable" } : {}) })}\n`, |
| 97 | ), |
| 98 | ).pipe(Effect.asVoid); |
| 99 | }), |
| 100 | stdout: version ? Stream.succeed(encoder.encode("pi 1.0.2\n")) : Stream.fromQueue(stdout), |
| 101 | stderr: Stream.empty, |
| 102 | all: Stream.empty, |
| 103 | getInputFd: () => Sink.drain, |
| 104 | getOutputFd: () => Stream.empty, |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | return { spawner, launches, pendingCommand }; |
| 109 | }); |
| 110 | |
| 111 | const create = (config: Partial<PiSettings> = {}, enabled = true) => |
| 112 | PiDriver.create({ |
| 113 | instanceId: ProviderInstanceId.make("pi-workspace-test"), |
| 114 | displayName: "My Pi", |
| 115 | accentColor: "#abcdef", |
| 116 | environment: [{ name: "PI_CODING_AGENT_DIR", value: "/isolated-pi", sensitive: false }], |
| 117 | enabled, |
| 118 | config: { ...PiDriver.defaultConfig(), binaryPath: "custom-pi", ...config }, |
| 119 | }); |
| 120 | |
| 121 | it.layer(layerTest)("PiDriver workspace discovery", (it) => { |
| 122 | it.effect("keeps each workspace's skills and commands separate from the machine catalog", () => |
| 123 | Effect.gen(function* () { |
| 124 | const { spawner, launches } = yield* makePiSpawner; |
| 125 | const instance = yield* create({ launchArgs: '--approve --skill "extra skill"' }).pipe( |
| 126 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 127 | ); |
| 128 | yield* instance.snapshot.refresh; |
| 129 | assert.isDefined(instance.snapshotForCwd); |
| 130 | const [first, second] = yield* Effect.all( |
| 131 | [instance.snapshotForCwd!("/first"), instance.snapshotForCwd!("/second")], |
| 132 | { concurrency: "unbounded" }, |
| 133 | ); |
| 134 | assert.deepEqual( |
| 135 | first.skills.map((skill) => skill.name), |
| 136 | ["personal", "first"], |
| 137 | ); |
| 138 | assert.deepEqual( |
| 139 | second.skills.map((skill) => skill.name), |
| 140 | ["personal", "second"], |
| 141 | ); |
| 142 | assert.deepEqual( |
| 143 | first.slashCommands.map((command) => command.name), |
| 144 | ["compact", "prompt-first"], |
| 145 | ); |
| 146 | const machine = yield* instance.snapshot.getSnapshot; |
| 147 | assert.deepEqual( |
| 148 | machine.skills.map((skill) => skill.name), |
| 149 | ["personal"], |
| 150 | ); |
| 151 | assert.deepEqual( |
| 152 | machine.slashCommands.map((command) => command.name), |
| 153 | ["compact"], |
| 154 | ); |
| 155 | assert.equal(first.instanceId, instance.instanceId); |
| 156 | assert.equal(first.displayName, "My Pi"); |
| 157 | assert.equal(first.accentColor, "#abcdef"); |
| 158 | assert.deepEqual(first.models, machine.models); |
| 159 | const workspaceLaunch = launches.find((launch) => launch.options.cwd === "/first"); |
| 160 | assert.isDefined(workspaceLaunch); |
| 161 | assert.equal(workspaceLaunch!.command, "custom-pi"); |
| 162 | assert.includeMembers( |
| 163 | [...workspaceLaunch!.args], |
| 164 | ["--approve", "--skill", "extra skill", "--no-session"], |
| 165 | ); |
| 166 | assert.equal(workspaceLaunch!.options.env?.PI_CODING_AGENT_DIR, "/isolated-pi"); |
| 167 | }).pipe(Effect.scoped), |
| 168 | ); |
| 169 | |
| 170 | it.effect("does not run a disabled provider's workspace probe", () => |
| 171 | Effect.gen(function* () { |
| 172 | const instance = yield* create({}, false).pipe( |
| 173 | Effect.provideService( |
| 174 | ChildProcessSpawner.ChildProcessSpawner, |
| 175 | ChildProcessSpawner.make(() => Effect.die("Disabled Pi must not spawn")), |
| 176 | ), |
| 177 | ); |
| 178 | assert.isDefined(instance.snapshotForCwd); |
| 179 | const workspace = yield* instance.snapshotForCwd!("/first"); |
| 180 | assert.isFalse(workspace.enabled); |
| 181 | assert.deepEqual(workspace.skills, []); |
| 182 | }).pipe(Effect.scoped), |
| 183 | ); |
| 184 | |
| 185 | it.effect( |
| 186 | "fails command discovery instead of returning an empty successful workspace catalog", |
| 187 | () => |
| 188 | Effect.gen(function* () { |
| 189 | const { spawner } = yield* makePiSpawner; |
| 190 | const instance = yield* create().pipe( |
| 191 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 192 | ); |
| 193 | yield* instance.snapshot.refresh; |
| 194 | assert.isDefined(instance.snapshotForCwd); |
| 195 | const error = yield* Effect.flip(instance.snapshotForCwd!("/failed")); |
| 196 | assert.equal(error._tag, "ProviderDriverError"); |
| 197 | assert.equal(error.instanceId, instance.instanceId); |
| 198 | assert.deepEqual( |
| 199 | (yield* instance.snapshot.getSnapshot).skills.map((skill) => skill.name), |
| 200 | ["personal"], |
| 201 | ); |
| 202 | }).pipe(Effect.scoped), |
| 203 | ); |
| 204 | |
| 205 | it.effect("times out workspace discovery that needs interactive input", () => |
| 206 | Effect.gen(function* () { |
| 207 | const { spawner, pendingCommand } = yield* makePiSpawner; |
| 208 | const instance = yield* create().pipe( |
| 209 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 210 | ); |
| 211 | const probe = yield* instance.snapshotForCwd!("/pending").pipe(Effect.flip, Effect.forkChild); |
| 212 | yield* Deferred.await(pendingCommand); |
| 213 | yield* TestClock.adjust("15 seconds"); |
| 214 | const error = yield* Fiber.join(probe); |
| 215 | assert.equal(error._tag, "ProviderDriverError"); |
| 216 | assert.include(error.detail, "workspace commands"); |
| 217 | }).pipe(Effect.scoped), |
| 218 | ); |
| 219 | }); |