Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion apps/server/src/provider/Layers/PiProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Queue from "effect/Queue";
import * as Sink from "effect/Sink";
import { TestClock } from "effect/testing";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";

Expand Down Expand Up @@ -44,14 +48,98 @@ function piProbeSpawner(version: string) {
});
}

function delayedPiProbeSpawner(startupDelayMs: number, started: Deferred.Deferred<void>) {
return ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
if (args.includes("--version")) return processHandle({ stdout: "pi 0.99.1\n" });
assert.include(args, "--no-session");
assert.notInclude(args, "--no-extensions");
const responses = yield* Queue.unbounded<Uint8Array>();
return ChildProcessSpawner.makeHandle({
...processHandle({}),
exitCode: Effect.never,
stdin: Sink.forEach((chunk: Uint8Array) =>
Effect.gen(function* () {
const request = JSON.parse(new TextDecoder().decode(chunk)) as {
id: string;
type: string;
};
if (request.type === "get_state") {
yield* Deferred.succeed(started, undefined);
yield* Effect.sleep(startupDelayMs);
}
const data =
request.type === "get_available_models"
? { models: [{ provider: "extension-provider", id: "custom-model" }] }
: request.type === "get_commands"
? { commands: [] }
: {};
yield* Queue.offer(
responses,
encoder.encode(
`${JSON.stringify({ type: "response", id: request.id, success: true, data })}\n`,
),
);
}),
),
stdout: Stream.fromQueue(responses),
});
}),
);
}

const settings = {
enabled: true,
binaryPath: "pi",
binaryPath: "pi-test",
launchArgs: "",
customModels: [],
} as const;

describe("PiProvider", () => {
it.effect("discovers extension models when RPC startup takes more than 15 seconds", () =>
Effect.gen(function* () {
const started = yield* Deferred.make<void>();
const probe = yield* checkPiProviderStatus(settings).pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
delayedPiProbeSpawner(20_000, started),
),
Effect.forkChild,
);
yield* Deferred.await(started);
yield* TestClock.adjust("20 seconds");
const snapshot = yield* Fiber.join(probe);
assert.equal(snapshot.auth.status, "authenticated");
assert.deepEqual(
snapshot.models.map((model) => model.slug),
["default", "extension-provider/custom-model"],
);
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect("still falls back when RPC startup exceeds the discovery deadline", () =>
Effect.gen(function* () {
const started = yield* Deferred.make<void>();
const probe = yield* checkPiProviderStatus(settings).pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
delayedPiProbeSpawner(60_000, started),
),
Effect.forkChild,
);
yield* Deferred.await(started);
yield* TestClock.adjust("31 seconds");
const snapshot = yield* Fiber.join(probe);
assert.equal(snapshot.status, "ready");
assert.equal(snapshot.auth.status, "unknown");
assert.deepEqual(
snapshot.models.map((model) => model.slug),
["default"],
);
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect("requires the first published Pi version with entries and settlement hooks", () =>
Effect.gen(function* () {
const snapshot = yield* checkPiProviderStatus(settings).pipe(
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/Layers/PiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const PI_PRESENTATION = {
} as const;

const VERSION_PROBE_TIMEOUT_MS = 4_000;
const PI_RPC_DISCOVERY_TIMEOUT_MS = 15_000;
const PI_RPC_DISCOVERY_TIMEOUT_MS = 30_000;
/**
* get_entries arrived in 0.80.3 and agent_settled landed in source at 0.80.4.
* Version 0.80.5 was the first published package containing both hooks. T3
Expand Down
Loading