Skip to content

Commit 4d4297c

Browse files
authored
fix(server): stop restoring stale Pi models (pingdotgg#18498)
1 parent e0de586 commit 4d4297c

4 files changed

Lines changed: 332 additions & 5 deletions

File tree

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,204 @@ it.layer(
11141114
assert.deepStrictEqual(afterFailure.models, [authoritativeProvider.models[0]!]);
11151115
});
11161116

1117+
describe("Pi model inventories", () => {
1118+
const defaultModel = {
1119+
slug: "default",
1120+
name: "Pi default",
1121+
isCustom: false,
1122+
capabilities: null,
1123+
} as const;
1124+
const currentModel = {
1125+
slug: "openai/gpt-6.1-sol",
1126+
name: "GPT-6.1-Sol",
1127+
isCustom: false,
1128+
capabilities: null,
1129+
} as const;
1130+
const removedModel = {
1131+
slug: "openrouter/google/gemini-2.5-pro",
1132+
name: "Google: Gemini 2.5 Pro",
1133+
isCustom: false,
1134+
capabilities: null,
1135+
} as const;
1136+
const customModel = {
1137+
slug: "custom-model",
1138+
name: "Custom model",
1139+
isCustom: true,
1140+
capabilities: null,
1141+
} as const;
1142+
const cachedProvider = {
1143+
instanceId: ProviderInstanceId.make("pi-personal"),
1144+
driver: ProviderDriverKind.make("pi"),
1145+
status: "ready",
1146+
enabled: true,
1147+
installed: true,
1148+
auth: { status: "authenticated", type: "pi" },
1149+
checkedAt: "2026-10-03T00:00:00.000Z",
1150+
version: "1.0.0",
1151+
models: [
1152+
defaultModel,
1153+
currentModel,
1154+
removedModel,
1155+
{ ...customModel, slug: "removed-custom" },
1156+
],
1157+
slashCommands: [],
1158+
skills: [],
1159+
} satisfies ServerProvider;
1160+
const refreshedProvider = {
1161+
...cachedProvider,
1162+
checkedAt: "2026-10-03T00:01:00.000Z",
1163+
models: [defaultModel, currentModel, customModel],
1164+
} satisfies ServerProvider;
1165+
const pendingProvider = {
1166+
...cachedProvider,
1167+
status: "warning",
1168+
version: null,
1169+
auth: { status: "unknown" },
1170+
models: [defaultModel, customModel],
1171+
} satisfies ServerProvider;
1172+
const failedProvider = {
1173+
...pendingProvider,
1174+
checkedAt: "2026-10-03T00:02:00.000Z",
1175+
status: "ready",
1176+
version: "1.0.0",
1177+
message: "Pi is available, but T3 Code could not refresh its models and commands.",
1178+
} satisfies ServerProvider;
1179+
1180+
it("drops disconnected Pi upstream models after successful discovery", () => {
1181+
assert.deepStrictEqual(
1182+
ProviderRegistry.mergeProviderSnapshot(cachedProvider, refreshedProvider).models,
1183+
refreshedProvider.models,
1184+
);
1185+
});
1186+
1187+
it("clears discovered Pi models when successful discovery finds no usable models", () => {
1188+
const signedOutProvider = {
1189+
...refreshedProvider,
1190+
status: "warning",
1191+
auth: { status: "unauthenticated", type: "pi" },
1192+
models: [defaultModel, customModel],
1193+
} satisfies ServerProvider;
1194+
1195+
assert.deepStrictEqual(
1196+
ProviderRegistry.mergeProviderSnapshot(cachedProvider, signedOutProvider).models,
1197+
signedOutProvider.models,
1198+
);
1199+
});
1200+
1201+
it("retains Pi inventories during incomplete probes without restoring removed custom models", () => {
1202+
const incompleteProviders = [
1203+
pendingProvider,
1204+
{ ...pendingProvider, installed: false },
1205+
failedProvider,
1206+
{ ...failedProvider, status: "error" },
1207+
] satisfies ReadonlyArray<ServerProvider>;
1208+
1209+
for (const provider of incompleteProviders) {
1210+
assert.deepStrictEqual(
1211+
ProviderRegistry.mergeProviderSnapshot(cachedProvider, provider).models,
1212+
[defaultModel, customModel, currentModel, removedModel],
1213+
);
1214+
}
1215+
});
1216+
1217+
it("does not restore disconnected Pi models after a later discovery failure", () => {
1218+
const afterRemoval = ProviderRegistry.mergeProviderSnapshot(
1219+
cachedProvider,
1220+
refreshedProvider,
1221+
);
1222+
assert.deepStrictEqual(
1223+
ProviderRegistry.mergeProviderSnapshot(afterRemoval, failedProvider).models,
1224+
[defaultModel, customModel, currentModel],
1225+
);
1226+
});
1227+
1228+
it.effect(
1229+
"persists Pi inventory removals across failed refreshes and registry restarts",
1230+
() =>
1231+
Effect.gen(function* () {
1232+
const config = yield* ServerConfig.ServerConfig;
1233+
const filePath = yield* resolveProviderStatusCachePath({
1234+
cacheDir: config.providerStatusCacheDir,
1235+
instanceId: cachedProvider.instanceId,
1236+
});
1237+
yield* writeProviderStatusCache({ filePath, provider: cachedProvider });
1238+
const nextProvider = yield* Ref.make<ServerProvider>(refreshedProvider);
1239+
const instance = {
1240+
instanceId: cachedProvider.instanceId,
1241+
driverKind: cachedProvider.driver,
1242+
continuationIdentity: {
1243+
driverKind: cachedProvider.driver,
1244+
continuationKey: "pi:instance:pi-personal",
1245+
},
1246+
displayName: undefined,
1247+
enabled: true,
1248+
snapshot: {
1249+
resolveMaintenance: () =>
1250+
Effect.succeed(
1251+
makeManualOnlyProviderMaintenanceCapabilities({
1252+
provider: cachedProvider.driver,
1253+
packageName: null,
1254+
}),
1255+
),
1256+
getSnapshot: Effect.succeed(pendingProvider),
1257+
refresh: Ref.get(nextProvider),
1258+
streamChanges: Stream.empty,
1259+
applyUsageLimits: () => Effect.void,
1260+
},
1261+
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
1262+
textGeneration: {} as ProviderInstance["textGeneration"],
1263+
} satisfies ProviderInstance;
1264+
const layerInstanceRegistry = Layer.succeed(
1265+
ProviderInstanceRegistry.ProviderInstanceRegistry,
1266+
{
1267+
getInstance: (id) =>
1268+
Effect.succeed(id === instance.instanceId ? instance : undefined),
1269+
listInstances: Effect.succeed([instance]),
1270+
listUnavailable: Effect.succeed([]),
1271+
streamChanges: Stream.empty,
1272+
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), PubSub.subscribe),
1273+
},
1274+
);
1275+
const retainedModels = [defaultModel, customModel, currentModel];
1276+
1277+
for (const restarted of [false, true]) {
1278+
yield* Effect.gen(function* () {
1279+
const registry = yield* ProviderRegistry.ProviderRegistry;
1280+
assert.deepStrictEqual(
1281+
(yield* registry.getProviders)[0]?.models,
1282+
restarted
1283+
? retainedModels
1284+
: [defaultModel, customModel, currentModel, removedModel],
1285+
);
1286+
1287+
yield* registry.refreshInstance(instance.instanceId);
1288+
assert.deepStrictEqual(
1289+
(yield* readProviderStatusCache(filePath))?.models,
1290+
restarted ? retainedModels : refreshedProvider.models,
1291+
);
1292+
1293+
yield* Ref.set(nextProvider, failedProvider);
1294+
const afterFailure = yield* registry.refreshInstance(instance.instanceId);
1295+
assert.deepStrictEqual(afterFailure[0]?.models, retainedModels);
1296+
assert.deepStrictEqual(
1297+
(yield* readProviderStatusCache(filePath))?.models,
1298+
retainedModels,
1299+
);
1300+
}).pipe(
1301+
Effect.provide(ProviderRegistry.layer.pipe(Layer.provide(layerInstanceRegistry))),
1302+
Effect.scoped,
1303+
);
1304+
}
1305+
}).pipe(
1306+
Effect.provide(
1307+
ServerConfig.layerTest(process.cwd(), {
1308+
prefix: "t3-pi-model-cache-",
1309+
}).pipe(Layer.provideMerge(NodeServices.layer)),
1310+
),
1311+
),
1312+
);
1313+
});
1314+
11171315
describe("Codex model inventories", () => {
11181316
const cachedProvider = {
11191317
instanceId: ProviderInstanceId.make("codex-personal"),

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ export function upsertProviderWorkspaceSnapshot(
144144
}
145145

146146
const shouldRetainMissingProviderModels = (provider: ServerProvider): boolean => {
147+
if (provider.driver === ProviderDriverKind.make("pi")) {
148+
// Pi reports known auth only after RPC inventory discovery completes.
149+
// An empty inventory is warning/unauthenticated; failed or interactive
150+
// discovery stays ready/unknown and must keep the last known models.
151+
return !(
152+
provider.installed &&
153+
(provider.status === "ready" || provider.status === "warning") &&
154+
provider.auth.status !== "unknown"
155+
);
156+
}
157+
147158
if (provider.driver === ProviderDriverKind.make("acpRegistry")) {
148159
// ACP Registry discovery probes return the agent's complete inventory, so
149160
// a completed probe (ready and authenticated) replaces the model list —

‎packages/provider-pi/src/server/status.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, describe, it } from "@effect/vitest";
3+
import * as Cause from "effect/Cause";
34
import * as Effect from "effect/Effect";
5+
import * as Queue from "effect/Queue";
6+
import * as Schema from "effect/Schema";
47
import * as Sink from "effect/Sink";
58
import * as Stream from "effect/Stream";
69
import * as ChildProcess from "effect/process/ChildProcess";
@@ -9,6 +12,10 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
912
import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./status.ts";
1013

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

1320
function processHandle(input: {
1421
readonly stdout?: string;
@@ -45,6 +52,51 @@ function piProbeSpawner(version: string) {
4552
});
4653
}
4754

55+
const piDiscoverySpawner = (inventory: unknown) =>
56+
Effect.gen(function* () {
57+
const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
58+
let stdinBuffer = "";
59+
const stdin = Sink.forEach((chunk: Uint8Array) =>
60+
Effect.gen(function* () {
61+
stdinBuffer += new TextDecoder().decode(chunk);
62+
while (true) {
63+
const newline = stdinBuffer.indexOf("\n");
64+
if (newline === -1) return;
65+
const line = stdinBuffer.slice(0, newline);
66+
stdinBuffer = stdinBuffer.slice(newline + 1);
67+
if (line.length === 0) continue;
68+
const request = decodeRpcRequest(line);
69+
const data =
70+
request.type === "get_available_models"
71+
? inventory
72+
: request.type === "get_commands"
73+
? { commands: [] }
74+
: {};
75+
yield* Queue.offer(
76+
stdout,
77+
encoder.encode(
78+
`${encodeJsonLine({ type: "response", id: request.id, command: request.type, success: true, data })}\n`,
79+
),
80+
);
81+
}
82+
}),
83+
);
84+
return ChildProcessSpawner.make((command) => {
85+
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
86+
return Effect.succeed(
87+
args.includes("--version")
88+
? processHandle({ stdout: "pi 0.84.3\n" })
89+
: ChildProcessSpawner.makeHandle({
90+
...processHandle({}),
91+
exitCode: Effect.never,
92+
isRunning: Effect.succeed(true),
93+
stdin,
94+
stdout: Stream.fromQueue(stdout),
95+
}),
96+
);
97+
});
98+
});
99+
48100
const settings = {
49101
enabled: true,
50102
binaryPath: "pi",
@@ -78,4 +130,59 @@ describe("PiProvider", () => {
78130
assert.include(snapshot.message ?? "", "could not refresh its models and commands");
79131
}).pipe(Effect.provide(NodeServices.layer)),
80132
);
133+
134+
it.effect("reports only the models returned by successful Pi discovery", () =>
135+
Effect.gen(function* () {
136+
const spawner = yield* piDiscoverySpawner({
137+
models: [{ provider: "anthropic", id: "claude-sonnet-4" }],
138+
});
139+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
140+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
141+
);
142+
assert.equal(snapshot.status, "ready");
143+
assert.equal(snapshot.auth.status, "authenticated");
144+
assert.deepEqual(
145+
snapshot.models.map((model) => model.slug),
146+
["default", "anthropic/claude-sonnet-4"],
147+
);
148+
assert.equal(snapshot.models[1]?.subProvider, "anthropic");
149+
}).pipe(Effect.provide(NodeServices.layer)),
150+
);
151+
152+
it.effect("reports a known unauthenticated inventory when Pi returns no usable models", () =>
153+
Effect.gen(function* () {
154+
const spawner = yield* piDiscoverySpawner({ models: [] });
155+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
156+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
157+
);
158+
assert.equal(snapshot.status, "warning");
159+
assert.equal(snapshot.auth.status, "unauthenticated");
160+
assert.deepEqual(
161+
snapshot.models.map((model) => model.slug),
162+
["default"],
163+
);
164+
}).pipe(Effect.provide(NodeServices.layer)),
165+
);
166+
167+
it.effect.each([
168+
["missing models", {}],
169+
["non-array models", { models: {} }],
170+
["missing model provider", { models: [{ id: "claude-sonnet-4" }] }],
171+
["missing model id", { models: [{ provider: "anthropic" }] }],
172+
["empty model provider", { models: [{ provider: "", id: "claude-sonnet-4" }] }],
173+
["empty model id", { models: [{ provider: "anthropic", id: "" }] }],
174+
[
175+
"mixed valid and invalid models",
176+
{ models: [{ provider: "anthropic", id: "claude-sonnet-4" }, { provider: "openrouter" }] },
177+
],
178+
] as const)("reports unknown authentication when Pi discovery returns %s", (_label, inventory) =>
179+
Effect.gen(function* () {
180+
const spawner = yield* piDiscoverySpawner(inventory);
181+
const snapshot = yield* checkPiProviderStatus(settings).pipe(
182+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
183+
);
184+
assert.equal(snapshot.status, "ready");
185+
assert.equal(snapshot.auth.status, "unknown");
186+
}).pipe(Effect.provide(NodeServices.layer)),
187+
);
81188
});

0 commit comments

Comments
 (0)