fix: preserve custom provider smoke config

This commit is contained in:
2026-08-05 01:35:42 +02:00
parent 174f854b96
commit 6b828288e3
2 changed files with 176 additions and 4 deletions
+80
View File
@@ -24,6 +24,83 @@ function rpcChild(onCommand: (command: any, emit: (message: unknown) => void) =>
return child;
}
// Catches an isolated smoke agent that copies auth.json but drops the selected custom
// provider/model from models.json, causing set_model to fail before the real request.
test("provider smoke reaches the selected custom provider from an isolated models.json", async () => {
let isolatedAgentDir: string | undefined;
let providerRequests = 0;
const child = rpcChild((command, emit) => {
if (command.type === "set_model") {
const models = JSON.parse(readFileSync(`${isolatedAgentDir}/models.json`, "utf8"));
const selectedProvider = models.providers?.[command.provider];
const selectedModel = selectedProvider?.models?.find(
(candidate: { id?: unknown }) => candidate.id === command.modelId,
);
emit({ type: "response", id: command.id, success: Boolean(selectedModel) });
}
if (command.type === "set_thinking_level") {
emit({ type: "response", id: command.id, success: true });
}
if (command.type === "prompt") {
providerRequests++;
emit({
type: "message_end",
message: { role: "assistant", stopReason: "stop", content: "must-not-be-returned" },
});
emit({
type: "agent_end",
messages: [{ role: "assistant", stopReason: "stop", content: "must-not-be-returned" }],
});
}
});
const smoke = createPiProviderSmoke(loadConfig({ PI_BIN: "/usr/local/bin/pi" }), {
spawnFn: (_command, _args, options) => {
isolatedAgentDir = options.env.PI_CODING_AGENT_DIR;
expect(readdirSync(isolatedAgentDir)).toEqual(["auth.json", "models.json"]);
expect(JSON.parse(readFileSync(`${isolatedAgentDir}/models.json`, "utf8"))).toEqual({
providers: {
"custom-openai": {
baseUrl: "https://selected.invalid/v1",
api: "openai-completions",
models: [{ id: "selected-model", name: "Selected model", reasoning: true }],
},
},
});
return child;
},
authProviders: () => new Set(["custom-openai"]),
readAuthStore: () => JSON.stringify({
"custom-openai": { type: "api_key", key: "test-only" },
unrelated: { type: "api_key", key: "must-not-enter-isolated-context" },
}),
readModelsStore: () => JSON.stringify({
providers: {
"custom-openai": {
baseUrl: "https://selected.invalid/v1",
api: "openai-completions",
models: [
{ id: "selected-model", name: "Selected model", reasoning: true },
{ id: "unrelated-model", name: "Must not enter isolated context" },
],
},
unrelated: {
baseUrl: "https://unrelated.invalid/v1",
api: "openai-completions",
apiKey: "!must-not-run",
models: [{ id: "unrelated-model" }],
},
},
}),
});
await expect(smoke({
provider: "custom-openai", model: "selected-model", reasoning: "medium", timeoutMs: 750,
})).resolves.toBeUndefined();
expect(providerRequests).toBe(1);
expect(child.kill).toHaveBeenCalledOnce();
expect(isolatedAgentDir && existsSync(dirname(isolatedAgentDir))).toBe(false);
});
// Catches a provider smoke process that runs from the trusted harness or leaves Pi tools,
// extensions, skills, context files, templates, themes, or session persistence enabled.
test("provider smoke makes one configured request from an isolated no-capability Pi process", async () => {
@@ -71,6 +148,7 @@ test("provider smoke makes one configured request from an isolated no-capability
zai: { type: "api_key", key: "test-only" },
deepseek: { type: "api_key", key: "must-not-enter-isolated-context" },
}),
readModelsStore: () => undefined,
});
await expect(smoke({
@@ -144,6 +222,7 @@ test.each(unexpectedToolEvents)("provider smoke fails closed on an unexpected $n
spawnFn: () => child,
authProviders: () => new Set(["zai"]),
readAuthStore: () => '{"zai":{"type":"api_key","key":"test-only"}}',
readModelsStore: () => undefined,
});
await expect(smoke({
@@ -174,6 +253,7 @@ test("provider smoke rejects a failed model turn with a stable non-secret error"
spawnFn: () => child,
authProviders: () => new Set(["zai"]),
readAuthStore: () => '{"zai":{"type":"api_key","key":"test-only"}}',
readModelsStore: () => undefined,
});
let caught: unknown;