The best way to control Claude Code, Codex and more from any computer or phone

main
ts 512 lines 19.4 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, describe, it } from "@effect/vitest";
3 import * as Cause from "effect/Cause";
4 import * as Effect from "effect/Effect";
5 import * as Deferred from "effect/Deferred";
6 import * as Fiber from "effect/Fiber";
7 import * as TestClock from "effect/testing/TestClock";
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 ChildProcess from "effect/process/ChildProcess";
13 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
14
15 import { checkPiProviderStatus } from "./status.ts";
16
17 const encoder = new TextEncoder();
18 const decodeRpcRequest = Schema.decodeSync(
19 Schema.fromJsonString(Schema.Struct({ id: Schema.String, type: Schema.String })),
20 );
21 const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
22 const STARTUP_DIALOG = `${encodeJsonLine({
23 type: "extension_ui_request",
24 id: "ui-1",
25 method: "select",
26 title: "Allow project MCP server “github”?",
27 options: ["Don't allow", "Allow"],
28 })}\n`;
29
30 function processHandle(input: {
31 readonly stdout?: string;
32 readonly stderr?: string;
33 readonly exitCode?: number;
34 /** Holds stdout and the exit back until it completes, like a CLI slow to answer. */
35 readonly answered?: Effect.Effect<void>;
36 }) {
37 const answered = input.answered ?? Effect.void;
38 const bytes = (value: string | undefined) =>
39 value === undefined || value.length === 0
40 ? Stream.empty
41 : Stream.succeed(encoder.encode(value));
42 return ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(900_000_001),
44 exitCode: Effect.as(answered, ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
45 isRunning: Effect.succeed(false),
46 kill: () => Effect.void,
47 unref: Effect.succeed(Effect.void),
48 stdin: Sink.drain,
49 stdout: Stream.unwrap(Effect.as(answered, bytes(input.stdout))),
50 stderr: bytes(input.stderr),
51 all: Stream.empty,
52 getInputFd: () => Sink.drain,
53 getOutputFd: () => Stream.empty,
54 });
55 }
56
57 function piProbeSpawner(version: string) {
58 return ChildProcessSpawner.make((command) => {
59 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
60 return Effect.succeed(
61 args.includes("--version")
62 ? processHandle({ stdout: `pi ${version}\n` })
63 : processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
64 );
65 });
66 }
67
68 const piRpcProcess = (
69 inventory: unknown,
70 onModelRequest: Effect.Effect<void> = Effect.void,
71 state: unknown = {},
72 ) =>
73 Effect.gen(function* () {
74 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
75 let stdinBuffer = "";
76 const stdin = Sink.forEach((chunk: Uint8Array) =>
77 Effect.gen(function* () {
78 stdinBuffer += new TextDecoder().decode(chunk);
79 while (true) {
80 const newline = stdinBuffer.indexOf("\n");
81 if (newline === -1) return;
82 const line = stdinBuffer.slice(0, newline);
83 stdinBuffer = stdinBuffer.slice(newline + 1);
84 if (line.length === 0) continue;
85 const request = decodeRpcRequest(line);
86 if (request.type === "get_available_models") {
87 yield* onModelRequest;
88 if (inventory === null) continue;
89 }
90 const data =
91 request.type === "get_available_models"
92 ? inventory
93 : request.type === "get_commands"
94 ? { commands: [] }
95 : request.type === "get_state"
96 ? state
97 : {};
98 yield* Queue.offer(
99 stdout,
100 encoder.encode(
101 `${encodeJsonLine({ type: "response", id: request.id, command: request.type, success: true, data })}\n`,
102 ),
103 );
104 }
105 }),
106 );
107 return ChildProcessSpawner.makeHandle({
108 ...processHandle({}),
109 exitCode: Effect.never,
110 isRunning: Effect.succeed(true),
111 stdin,
112 stdout: Stream.fromQueue(stdout),
113 });
114 });
115
116 /** Makes the extension-free launch exit before answering, like launch args naming an extension provider. */
117 const exitsWithoutExtensions = Symbol("exitsWithoutExtensions");
118
119 // Text generation lists models with --no-extensions, so its inventory can differ.
120 const piDiscoverySpawner = (
121 inventory: unknown,
122 extensionFreeInventory: unknown = inventory,
123 onExtensionFreeModelRequest: Effect.Effect<void> = Effect.void,
124 options: {
125 readonly version?: string;
126 readonly state?: unknown;
127 readonly authCheck?: string | undefined;
128 /** Without `--no-approve`, print a project-trust dialog and exit 0 as Pi 1.x does. */
129 readonly startupDialog?: boolean;
130 /** What `--no-approve` lists once project-local extensions are skipped. */
131 readonly declinedProjectInventory?: unknown;
132 } = {},
133 ) =>
134 Effect.succeed(
135 ChildProcessSpawner.make((command) => {
136 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
137 if (args.includes("--version")) {
138 return Effect.succeed(processHandle({ stdout: `pi ${options.version ?? "0.84.3"}\n` }));
139 }
140 if (args[0] === "auth") {
141 const expected = ["auth", "check", "--provider", "openai", "--json", "--no-refresh"];
142 return options.authCheck === undefined || args.join(" ") !== expected.join(" ")
143 ? Effect.die(`Unexpected pi ${args.join(" ")}`)
144 : Effect.succeed(processHandle({ stdout: options.authCheck, exitCode: 1 }));
145 }
146 if (
147 options.startupDialog === true &&
148 !args.includes("--no-extensions") &&
149 !args.includes("--no-approve")
150 ) {
151 return Effect.succeed(processHandle({ stdout: STARTUP_DIALOG }));
152 }
153 if (args.includes("--no-extensions") && extensionFreeInventory === exitsWithoutExtensions) {
154 return Effect.succeed(
155 processHandle({ stderr: 'Error: Unknown provider "demo".', exitCode: 1 }),
156 );
157 }
158 return piRpcProcess(
159 args.includes("--no-extensions")
160 ? extensionFreeInventory
161 : args.includes("--no-approve") && options.declinedProjectInventory !== undefined
162 ? options.declinedProjectInventory
163 : inventory,
164 args.includes("--no-extensions") ? onExtensionFreeModelRequest : Effect.void,
165 options.state,
166 );
167 }),
168 );
169
170 const settings = {
171 enabled: true,
172 binaryPath: "pi",
173 launchArgs: "",
174 customModels: [],
175 } as const;
176
177 const BROKEN_ADVISORY = {
178 status: "broken",
179 message: null,
180 recommendedVersion: null,
181 recommendedRange: ">=1.0.0",
182 } as const;
183
184 describe("PiProvider", () => {
185 it.effect("reports a version the policy rates broken as an error", () =>
186 Effect.gen(function* () {
187 const snapshot = yield* checkPiProviderStatus(settings, undefined, undefined, () =>
188 Effect.succeed(BROKEN_ADVISORY),
189 ).pipe(
190 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.84.2")),
191 );
192 assert.equal(snapshot.status, "error");
193 assert.equal(snapshot.version, "0.84.2");
194 assert.include(snapshot.message ?? "", "Pi 0.84.2 does not work");
195 }).pipe(Effect.provide(NodeServices.layer)),
196 );
197
198 it.effect("keeps compatible Pi selectable when optional discovery fails", () =>
199 Effect.gen(function* () {
200 const snapshot = yield* checkPiProviderStatus(settings).pipe(
201 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, piProbeSpawner("0.84.3")),
202 );
203 assert.equal(snapshot.status, "ready");
204 assert.equal(snapshot.auth.status, "unknown");
205 assert.deepEqual(
206 snapshot.models.map((model) => model.slug),
207 ["default"],
208 );
209 assert.include(snapshot.message ?? "", "could not refresh its models and commands");
210 }).pipe(Effect.provide(NodeServices.layer)),
211 );
212
213 it.effect.each([
214 {
215 label: "answers after 6 s",
216 versionDelay: "6 seconds",
217 status: "ready",
218 version: "0.84.3",
219 message: "could not refresh its models and commands",
220 probesAfterVersion: true,
221 },
222 {
223 label: "never answers",
224 versionDelay: "Infinity",
225 status: "error",
226 version: null,
227 message: "Pi CLI is installed but timed out while running `pi --version`.",
228 probesAfterVersion: false,
229 },
230 ] as const)("gives `pi --version` 10 s when it $label", (row) =>
231 Effect.gen(function* () {
232 const spawned: Array<string> = [];
233 const spawner = ChildProcessSpawner.make((command) => {
234 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
235 spawned.push(args.join(" "));
236 return Effect.succeed(
237 args.includes("--version")
238 ? processHandle({ stdout: "pi 0.84.3\n", answered: Effect.sleep(row.versionDelay) })
239 : processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
240 );
241 });
242 const fiber = yield* checkPiProviderStatus(settings).pipe(
243 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
244 Effect.forkChild,
245 );
246 yield* TestClock.adjust("10 seconds");
247 const snapshot = yield* Fiber.join(fiber);
248 assert.equal(snapshot.status, row.status);
249 assert.equal(snapshot.version, row.version);
250 assert.include(snapshot.message ?? "", row.message);
251 assert.equal(spawned.length > 1, row.probesAfterVersion);
252 }).pipe(Effect.provide(NodeServices.layer)),
253 );
254
255 it.effect("reports a known unauthenticated inventory when Pi returns no usable models", () =>
256 Effect.gen(function* () {
257 const spawner = yield* piDiscoverySpawner({ models: [] });
258 const snapshot = yield* checkPiProviderStatus(settings).pipe(
259 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
260 );
261 assert.equal(snapshot.status, "warning");
262 assert.equal(snapshot.auth.status, "unauthenticated");
263 assert.deepEqual(
264 snapshot.models.map((model) => model.slug),
265 ["default"],
266 );
267 }).pipe(Effect.provide(NodeServices.layer)),
268 );
269
270 it.effect.each([
271 {
272 label: "a pinned model without credentials",
273 version: "1.4.0",
274 activeModel: { provider: "openai", id: "gpt-5" },
275 authCheck: '{"status":"not_ready","provider":"openai","reason":"credentials_not_configured"}',
276 message: "Pi default (openai/gpt-5) has no credentials configured for openai.",
277 },
278 {
279 label: "a pinned model with a runtime --api-key",
280 version: "1.4.0",
281 launchArgs: "--provider openai --model gpt-5 --api-key fake-key",
282 activeModel: { provider: "openai", id: "gpt-5" },
283 authCheck: undefined,
284 message: "Pi default (openai/gpt-5) uses openai, which has no available models in Pi.",
285 },
286 {
287 label: "a pinned model whose provider auth check cannot find",
288 version: "1.4.0",
289 activeModel: { provider: "openai", id: "gpt-5" },
290 authCheck: '{"status":"not_ready","provider":"openai","reason":"provider_not_found"}',
291 message: "Pi default (openai/gpt-5) uses openai, which has no available models in Pi.",
292 },
293 {
294 label: "an uncatalogued model id of a provider with credentials",
295 version: "1.4.0",
296 activeModel: { provider: "anthropic", id: "claude-next" },
297 authCheck: undefined,
298 message: undefined,
299 },
300 {
301 label: "an extension model Pi lists as available",
302 version: "1.4.0",
303 activeModel: { provider: "router", id: "auto" },
304 authCheck: undefined,
305 message: undefined,
306 },
307 ])("diagnoses Pi's active model readiness for $label", (row) =>
308 Effect.gen(function* () {
309 const spawner = yield* piDiscoverySpawner(
310 {
311 models: [
312 { provider: "anthropic", id: "claude-sonnet-4" },
313 { provider: "router", id: "auto" },
314 ],
315 },
316 undefined,
317 Effect.void,
318 { version: row.version, state: { model: row.activeModel }, authCheck: row.authCheck },
319 );
320 const snapshot = yield* checkPiProviderStatus({
321 ...settings,
322 launchArgs: row.launchArgs ?? "",
323 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
324 assert.equal(snapshot.status, "ready");
325 assert.equal(snapshot.auth.status, "authenticated");
326 if (row.message === undefined) assert.isUndefined(snapshot.message);
327 else assert.include(snapshot.message ?? "", row.message);
328 }).pipe(Effect.provide(NodeServices.layer)),
329 );
330
331 it.effect("marks models that only load with Pi extensions as unusable for text generation", () =>
332 Effect.gen(function* () {
333 const spawner = yield* piDiscoverySpawner(
334 {
335 models: [
336 { provider: "anthropic", id: "claude-sonnet-4" },
337 { provider: "demo", id: "demo-model" },
338 ],
339 },
340 { models: [{ provider: "anthropic", id: "claude-sonnet-4" }] },
341 );
342 const snapshot = yield* checkPiProviderStatus(settings).pipe(
343 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
344 );
345 assert.deepEqual(
346 snapshot.models.map((model) => [model.slug, model.supportsTextGeneration]),
347 [
348 ["default", undefined],
349 ["anthropic/claude-sonnet-4", undefined],
350 ["demo/demo-model", false],
351 ],
352 );
353 assert.equal(snapshot.supportsTextGeneration, undefined);
354 }).pipe(Effect.provide(NodeServices.layer)),
355 );
356
357 it.effect.each([
358 { label: "exits before answering", extensionFreeInventory: exitsWithoutExtensions },
359 { label: "lists no models", extensionFreeInventory: { models: [] } },
360 ])("makes Pi unusable for text generation when Pi without extensions $label", (row) =>
361 Effect.gen(function* () {
362 const spawner = yield* piDiscoverySpawner(
363 { models: [{ provider: "demo", id: "extension-model" }] },
364 row.extensionFreeInventory,
365 );
366 const snapshot = yield* checkPiProviderStatus({
367 ...settings,
368 launchArgs: "--provider demo --model extension-model",
369 customModels: ["local/custom-alias"],
370 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
371 assert.equal(snapshot.status, "ready");
372 assert.equal(snapshot.auth.status, "authenticated");
373 // Pi default and custom models still run in chat, so the picker keeps them.
374 assert.deepEqual(
375 snapshot.models.map((model) => model.slug),
376 ["default", "demo/extension-model", "local/custom-alias"],
377 );
378 assert.equal(snapshot.supportsTextGeneration, false);
379 }).pipe(Effect.provide(NodeServices.layer)),
380 );
381
382 it.effect("preserves normal discovery when the extension-free model listing times out", () =>
383 Effect.gen(function* () {
384 const modelRequested = yield* Deferred.make<void>();
385 const spawner = yield* piDiscoverySpawner(
386 { models: [{ provider: "demo", id: "demo-model" }] },
387 null,
388 Deferred.succeed(modelRequested, undefined).pipe(Effect.asVoid),
389 );
390 const fiber = yield* checkPiProviderStatus(settings).pipe(
391 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
392 Effect.forkChild,
393 );
394 yield* Deferred.await(modelRequested);
395 yield* TestClock.adjust("5 seconds");
396 const snapshot = yield* Fiber.join(fiber);
397 assert.equal(snapshot.status, "ready");
398 assert.equal(snapshot.auth.status, "authenticated");
399 assert.deepEqual(
400 snapshot.models.map((model) => [model.slug, model.supportsTextGeneration]),
401 [
402 ["default", undefined],
403 ["demo/demo-model", undefined],
404 ],
405 );
406 assert.equal(snapshot.supportsTextGeneration, undefined);
407 assert.include(
408 snapshot.slashCommands.map((command) => command.name),
409 "compact",
410 );
411 }).pipe(Effect.provide(NodeServices.layer)),
412 );
413
414 it.effect("advertises a model's service tiers beside its thinking levels", () =>
415 Effect.gen(function* () {
416 const spawner = yield* piDiscoverySpawner({
417 models: [
418 {
419 provider: "columbia-codex",
420 id: "gpt-6.1-sol",
421 reasoning: true,
422 serviceTiers: [
423 { id: "priority", name: "Fast", description: "2x speed, increased usage" },
424 { id: "ultrafast", name: "Ultrafast" },
425 ],
426 },
427 { provider: "anthropic", id: "claude-sonnet-4", reasoning: true },
428 ],
429 });
430 const snapshot = yield* checkPiProviderStatus(settings).pipe(
431 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
432 );
433 const descriptors = (slug: string) =>
434 snapshot.models.find((model) => model.slug === slug)?.capabilities?.optionDescriptors ?? [];
435 assert.deepEqual(
436 descriptors("columbia-codex/gpt-6.1-sol").map((descriptor) => descriptor.id),
437 ["thinking", "serviceTier"],
438 );
439 assert.deepEqual(descriptors("columbia-codex/gpt-6.1-sol")[1], {
440 id: "serviceTier",
441 label: "Service Tier",
442 type: "select",
443 options: [
444 { id: "default", label: "Standard", isDefault: true },
445 { id: "priority", label: "Fast", description: "2x speed, increased usage" },
446 { id: "ultrafast", label: "Ultrafast" },
447 ],
448 currentValue: "default",
449 });
450 assert.deepEqual(
451 descriptors("anthropic/claude-sonnet-4").map((descriptor) => descriptor.id),
452 ["thinking"],
453 );
454 }).pipe(Effect.provide(NodeServices.layer)),
455 );
456
457 it.effect.each([
458 { launchArgs: "", startupDialog: true, models: ["default", "anthropic/claude-opus"] },
459 { launchArgs: "--approve", startupDialog: true, models: ["default"] },
460 {
461 launchArgs: "",
462 startupDialog: false,
463 models: ["default", "anthropic/claude-opus", "project/local-model"],
464 },
465 ])(
466 "keeps project trust unless a startup dialog blocks discovery (dialog: $startupDialog, launch args '$launchArgs')",
467 (row) =>
468 Effect.gen(function* () {
469 const spawner = yield* piDiscoverySpawner(
470 {
471 models: [
472 { provider: "anthropic", id: "claude-opus" },
473 { provider: "project", id: "local-model" },
474 ],
475 },
476 undefined,
477 undefined,
478 {
479 startupDialog: row.startupDialog,
480 declinedProjectInventory: { models: [{ provider: "anthropic", id: "claude-opus" }] },
481 },
482 );
483 const snapshot = yield* checkPiProviderStatus({
484 ...settings,
485 launchArgs: row.launchArgs,
486 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
487 assert.deepEqual(
488 snapshot.models.map((model) => model.slug),
489 row.models,
490 );
491 }).pipe(Effect.provide(NodeServices.layer)),
492 );
493
494 // regression: #18498 a malformed model inventory must not restore stale Pi models
495 it.effect.each([
496 ["missing models", {}],
497 ["missing model id", { models: [{ provider: "anthropic" }] }],
498 [
499 "mixed valid and invalid models",
500 { models: [{ provider: "anthropic", id: "claude-sonnet-4" }, { provider: "openrouter" }] },
501 ],
502 ] as const)("reports unknown authentication when Pi discovery returns %s", (_label, inventory) =>
503 Effect.gen(function* () {
504 const spawner = yield* piDiscoverySpawner(inventory);
505 const snapshot = yield* checkPiProviderStatus(settings).pipe(
506 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
507 );
508 assert.equal(snapshot.status, "ready");
509 assert.equal(snapshot.auth.status, "unknown");
510 }).pipe(Effect.provide(NodeServices.layer)),
511 );
512 });