Skip to content

Commit 94395e2

Browse files
committed
fix(server): stop restoring stale Pi models
1 parent fed41fa commit 94395e2

4 files changed

Lines changed: 368 additions & 7 deletions

File tree

‎apps/server/src/provider/Layers/PiProvider.test.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, describe, it } from "@effect/vitest";
3+
import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
4+
import * as Cause from "effect/Cause";
35
import * as Effect from "effect/Effect";
6+
import * as Queue from "effect/Queue";
7+
import * as Schema from "effect/Schema";
48
import * as Sink from "effect/Sink";
59
import * as Stream from "effect/Stream";
610
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
711

812
import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./PiProvider.ts";
13+
import { mergeProviderSnapshot } from "./ProviderRegistry.ts";
914

1015
const encoder = new TextEncoder();
16+
const decodeRpcRequest = Schema.decodeSync(
17+
Schema.fromJsonString(Schema.Struct({ id: Schema.String, type: Schema.String })),
18+
);
19+
const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
1120

1221
function processHandle(input: {
1322
readonly stdout?: string;
@@ -44,6 +53,67 @@ function piProbeSpawner(version: string) {
4453
});
4554
}
4655

56+
const piDiscoverySpawner = (inventory: unknown) =>
57+
Effect.gen(function* () {
58+
const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
59+
let stdinBuffer = "";
60+
const stdin = Sink.forEach((chunk: Uint8Array) =>
61+
Effect.gen(function* () {
62+
stdinBuffer += new TextDecoder().decode(chunk);
63+
while (true) {
64+
const newline = stdinBuffer.indexOf("\n");
65+
if (newline === -1) return;
66+
const line = stdinBuffer.slice(0, newline);
67+
stdinBuffer = stdinBuffer.slice(newline + 1);
68+
if (line.length === 0) continue;
69+
const request = decodeRpcRequest(line);
70+
const data =
71+
request.type === "get_available_models"
72+
? inventory
73+
: request.type === "get_commands"
74+
? { commands: [] }
75+
: {};
76+
yield* Queue.offer(
77+
stdout,
78+
encoder.encode(
79+
`${encodeJsonLine({ type: "response", id: request.id, command: request.type, success: true, data })}\n`,
80+
),
81+
);
82+
}
83+
}),
84+
);
85+
return ChildProcessSpawner.make((command) => {
86+
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
87+
return Effect.succeed(
88+
args.includes("--version")
89+
? processHandle({ stdout: "pi 0.84.3\n" })
90+
: ChildProcessSpawner.makeHandle({
91+
...processHandle({}),
92+
exitCode: Effect.never,
93+
isRunning: Effect.succeed(true),
94+
stdin,
95+
stdout: Stream.fromQueue(stdout),
96+
}),
97+
);
98+
});
99+
});
100+
101+
const piInstance = {
102+
instanceId: ProviderInstanceId.make("pi-personal"),
103+
driver: ProviderDriverKind.make("pi"),
104+
};
105+
106+
const previousModels = [
107+
{ slug: "default", name: "Pi default", isCustom: false, capabilities: null },
108+
{ slug: "openrouter/openai/gpt-5", name: "GPT-5", isCustom: false, capabilities: null },
109+
{
110+
slug: "anthropic/claude-sonnet-4",
111+
name: "Claude Sonnet 4",
112+
isCustom: false,
113+
capabilities: null,
114+
},
115+
] as const satisfies ServerProvider["models"];
116+
47117
const settings = {
48118
enabled: true,
49119
binaryPath: "pi",
@@ -75,6 +145,80 @@ describe("PiProvider", () => {
75145
["default"],
76146
);
77147
assert.include(snapshot.message ?? "", "could not refresh its models and commands");
148+
const nextProvider = { ...snapshot, ...piInstance };
149+
assert.deepEqual(
150+
mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map(
151+
(model) => model.slug,
152+
),
153+
previousModels.map((model) => model.slug),
154+
);
155+
}).pipe(Effect.provide(NodeServices.layer)),
156+
);
157+
158+
it.effect("removes logged-out OpenRouter models after successful Pi discovery", () =>
159+
Effect.gen(function* () {
160+
const spawner = yield* piDiscoverySpawner({
161+
models: [{ provider: "anthropic", id: "claude-sonnet-4" }],
162+
});
163+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
164+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
165+
);
166+
assert.equal(snapshot.status, "ready");
167+
assert.equal(snapshot.auth.status, "authenticated");
168+
const nextProvider = { ...snapshot, ...piInstance };
169+
assert.deepEqual(
170+
mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map(
171+
(model) => model.slug,
172+
),
173+
["default", "anthropic/claude-sonnet-4"],
174+
);
175+
}).pipe(Effect.provide(NodeServices.layer)),
176+
);
177+
178+
it.effect("clears discovered models when successful Pi discovery returns no usable models", () =>
179+
Effect.gen(function* () {
180+
const spawner = yield* piDiscoverySpawner({ models: [] });
181+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
182+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
183+
);
184+
assert.equal(snapshot.status, "warning");
185+
assert.equal(snapshot.auth.status, "unauthenticated");
186+
const nextProvider = { ...snapshot, ...piInstance };
187+
assert.deepEqual(
188+
mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map(
189+
(model) => model.slug,
190+
),
191+
["default"],
192+
);
193+
}).pipe(Effect.provide(NodeServices.layer)),
194+
);
195+
196+
it.effect.each([
197+
["missing models", {}],
198+
["non-array models", { models: {} }],
199+
["missing model provider", { models: [{ id: "claude-sonnet-4" }] }],
200+
["missing model id", { models: [{ provider: "anthropic" }] }],
201+
["empty model provider", { models: [{ provider: "", id: "claude-sonnet-4" }] }],
202+
["empty model id", { models: [{ provider: "anthropic", id: "" }] }],
203+
[
204+
"mixed valid and invalid models",
205+
{ models: [{ provider: "anthropic", id: "claude-sonnet-4" }, { provider: "openrouter" }] },
206+
],
207+
] as const)("retains cached models when Pi discovery returns %s", (_label, inventory) =>
208+
Effect.gen(function* () {
209+
const spawner = yield* piDiscoverySpawner(inventory);
210+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
211+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
212+
);
213+
const nextProvider = { ...snapshot, ...piInstance };
214+
assert.deepEqual(
215+
mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map(
216+
(model) => model.slug,
217+
),
218+
previousModels.map((model) => model.slug),
219+
);
220+
assert.equal(snapshot.status, "ready");
221+
assert.equal(snapshot.auth.status, "unknown");
78222
}).pipe(Effect.provide(NodeServices.layer)),
79223
);
80224
});

‎apps/server/src/provider/Layers/PiProvider.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from "../../orchestration-v2/Adapters/piT3McpInjection.ts";
3333
import {
3434
makePiRpcConnection,
35+
PiRpcError,
3536
piRecordField as recordField,
3637
piRecordString as recordString,
3738
} from "../../orchestration-v2/Adapters/PiRpc.ts";
@@ -103,15 +104,23 @@ function piModelsFromSettings(
103104
function parseDiscoveredModels(
104105
data: unknown,
105106
defaultThinkingLevel: unknown,
106-
): ReadonlyArray<ServerProviderModel> {
107+
): Result.Result<ReadonlyArray<ServerProviderModel>, PiRpcError> {
107108
const models = recordField(data, "models");
108-
if (!Array.isArray(models)) return [];
109+
if (!Array.isArray(models)) {
110+
return Result.fail(
111+
new PiRpcError({ operation: "get_available_models", detail: "Invalid model inventory" }),
112+
);
113+
}
109114
const seen = new Set<string>();
110115
const parsed: Array<ServerProviderModel> = [];
111116
for (const model of models) {
112117
const provider = recordString(model, "provider");
113118
const id = recordString(model, "id");
114-
if (provider === undefined || id === undefined) continue;
119+
if (provider === undefined || id === undefined || provider.trim() === "" || id.trim() === "") {
120+
return Result.fail(
121+
new PiRpcError({ operation: "get_available_models", detail: "Invalid model identity" }),
122+
);
123+
}
115124
const slug = `${provider}/${id}`;
116125
if (seen.has(slug)) continue;
117126
seen.add(slug);
@@ -122,7 +131,7 @@ function parseDiscoveredModels(
122131
capabilities: thinkingCapabilitiesForPiModel(model, defaultThinkingLevel),
123132
});
124133
}
125-
return parsed;
134+
return Result.succeed(parsed);
126135
}
127136

128137
const discoverPiViaRpc = (
@@ -155,9 +164,8 @@ const discoverPiViaRpc = (
155164
const commandsData = yield* connection
156165
.request({ type: "get_commands" })
157166
.pipe(Effect.orElseSucceed(() => undefined));
158-
const discoveredModels = parseDiscoveredModels(
159-
modelsData,
160-
recordString(stateData, "thinkingLevel"),
167+
const discoveredModels = yield* Effect.fromResult(
168+
parseDiscoveredModels(modelsData, recordString(stateData, "thinkingLevel")),
161169
);
162170
const { slashCommands, skills } = parsePiDiscoveredCommands(commandsData);
163171
return {

0 commit comments

Comments
 (0)