Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(importers): await Pi config fallback reads

isword76c766deb401
diff --git a/apps/desktop/electron/main/importers/model-config.ts b/apps/desktop/electron/main/importers/model-config.ts
index e1d21283e..7528723d3 100644
--- a/apps/desktop/electron/main/importers/model-config.ts
+++ b/apps/desktop/electron/main/importers/model-config.ts
@@ -35,6 +35,14 @@ async function readJson(filePath: string): Promise<unknown | null> {
return parseJsonDocument(text);
}
+async function readFirstJson(...filePaths: string[]): Promise<unknown | null> {
+ for (const filePath of filePaths) {
+ const value = await readJson(filePath);
+ if (value != null) return value;
+ }
+ return null;
+}
+
export async function scanModelConfigs(
options: ModelConfigScanOptions = {},
): Promise<ModelConfigImportDraft[]> {
@@ -89,15 +97,21 @@ async function scanPi(
const agentDir = path.join(home, ".pi", "agent");
const fallbackDir = path.join(home, ".pi");
const [models, auth, modelsStore] = await Promise.all([
- readJson(path.join(agentDir, "models.json")) ??
- readJson(path.join(fallbackDir, "models.json")),
- readJson(path.join(agentDir, "auth.json")) ??
- readJson(path.join(fallbackDir, "auth.json")),
+ readFirstJson(
+ path.join(agentDir, "models.json"),
+ path.join(fallbackDir, "models.json"),
+ ),
+ readFirstJson(
+ path.join(agentDir, "auth.json"),
+ path.join(fallbackDir, "auth.json"),
+ ),
// Pi caches the resolved catalog (including built-in providers) beside
// models.json. It is the only place an auth-only provider's base URL and
// model limits are available.
- readJson(path.join(agentDir, "models-store.json")) ??
- readJson(path.join(fallbackDir, "models-store.json")),
+ readFirstJson(
+ path.join(agentDir, "models-store.json"),
+ path.join(fallbackDir, "models-store.json"),
+ ),
]);
const drafts = parsePiModelConfig(models, env);
const authConfig = parsePiAuthModelConfig(auth, modelsStore, env);
diff --git a/apps/desktop/test/model-config-import.test.mjs b/apps/desktop/test/model-config-import.test.mjs
index 4cf34a48e..39f34c8c1 100644
--- a/apps/desktop/test/model-config-import.test.mjs
+++ b/apps/desktop/test/model-config-import.test.mjs
@@ -303,3 +303,62 @@ test("scanModelConfigs imports Pi auth-only providers and fills unresolved model
),
);
});
+
+// Regression: readPi originally used `promise ?? fallbackPromise`, which never
+// awaited the left side and therefore never read the documented ~/.pi fallback.
+test("scanModelConfigs reads Pi fallback files directly under ~/.pi", async () => {
+ const home = await mkdtemp(join(tmpdir(), "pi-fallback-import-"));
+ const configDir = join(home, ".pi");
+ await mkdir(configDir, { recursive: true });
+
+ await writeFile(
+ join(configDir, "models.json"),
+ JSON.stringify({
+ providers: {
+ legacy: {
+ baseUrl: "https://legacy.example/v1",
+ api: "openai-completions",
+ apiKey: "env:LEGACY_KEY",
+ models: [{ id: "legacy-model" }],
+ },
+ },
+ }),
+ );
+ await writeFile(
+ join(configDir, "auth.json"),
+ JSON.stringify({
+ legacy: { type: "api_key", key: "sk-legacy" },
+ "auth-only": { type: "api_key", key: "sk-auth-only" },
+ }),
+ );
+ await writeFile(
+ join(configDir, "models-store.json"),
+ JSON.stringify({
+ "auth-only": {
+ models: [
+ {
+ id: "auth-model",
+ api: "openai-completions",
+ baseUrl: "https://auth.example/v1",
+ },
+ ],
+ },
+ }),
+ );
+
+ const drafts = (await scanModelConfigs({ homeDir: home, env: {} })).filter(
+ (draft) => draft.source === "pi",
+ );
+ assert.deepEqual(
+ drafts.map((draft) => draft.externalId).sort(),
+ ["auth-only", "legacy"],
+ );
+ assert.equal(
+ drafts.find((draft) => draft.externalId === "legacy")?.secretValue,
+ "sk-legacy",
+ );
+ assert.equal(
+ drafts.find((draft) => draft.externalId === "auth-only")?.secretValue,
+ "sk-auth-only",
+ );
+});