Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ export function loadConfig(): OcxConfig {
warnDegradedCredentialGroups(parsed);
return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed));
}
// Only object-shaped configs are repairable. Spreading another JSON value
// into defaults can manufacture a valid config and bypass the invalid-file
// backup.
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
warnAndBackupInvalidConfig(configPath, result.error);
return getDefaultConfig();
}
// Schema validation failed — merge defaults into the raw object instead of
// discarding it entirely, so pool accounts and providers survive a missing
// field like defaultProvider.
Expand Down
21 changes: 21 additions & 0 deletions tests/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,27 @@ describe("opencodex config defaults", () => {
}
});

test.each([
["number", "123"],
["boolean", "true"],
["string", JSON.stringify("not-an-object")],
["array", "[]"],
["null", "null"],
])("backs up a top-level %s instead of repairing it", (_kind, raw) => {
writeConfig(raw);
const errorSpy = spyOn(console, "error").mockImplementation(() => {});

try {
expect(loadConfig()).toEqual(getDefaultConfig());
const backups = backupNames();
expect(backups).toHaveLength(1);
expect(readFileSync(join(testDir, backups[0]), "utf-8")).toBe(raw);
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Could not load opencodex config"));
} finally {
errorSpy.mockRestore();
}
});

test("repairs structurally incomplete config by merging defaults instead of rejecting", () => {
writeConfig({ port: 10100 });
const errorSpy = spyOn(console, "error").mockImplementation(() => {});
Expand Down
Loading