Skip to content

Commit 9017fa2

Browse files
fix(doctor): persist normalized agent roster
1 parent ee9a658 commit 9017fa2

5 files changed

Lines changed: 63 additions & 3 deletions

‎src/commands/doctor-config-flow.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,9 +1627,29 @@ describe("doctor config flow", () => {
16271627
});
16281628

16291629
expect(result.shouldWriteConfig).toBe(true);
1630+
expect(result.explicitSetPaths).toEqual([["agents", "entries"]]);
16301631
expect(result.cfg.agents?.entries).toEqual({
16311632
main: { default: true, workspace: "/tmp/migrated-main" },
16321633
});
1634+
expect(terminalNoteMock).toHaveBeenCalledWith(
1635+
"Prepared agents.entries with exactly one explicit default agent for persistence.",
1636+
"Doctor changes",
1637+
);
1638+
expect(terminalNoteMock).not.toHaveBeenCalledWith(
1639+
expect.stringContaining("Persisted agents.entries"),
1640+
expect.anything(),
1641+
);
1642+
});
1643+
1644+
it("drops roster write intent when a preview repair is declined", async () => {
1645+
const result = await runDoctorConfigWithInput({
1646+
config: { agents: { entries: { main: { default: true } } } },
1647+
parsedConfig: {},
1648+
run: loadAndMaybeMigrateDoctorConfig,
1649+
});
1650+
1651+
expect(result.shouldWriteConfig).toBe(false);
1652+
expect(result.explicitSetPaths).toBeUndefined();
16331653
});
16341654

16351655
it("removes a legacy list when Doctor persists keyed roster entries", async () => {
@@ -1777,6 +1797,7 @@ describe("doctor config flow", () => {
17771797
});
17781798

17791799
expect(result.shouldWriteConfig).toBe(false);
1800+
expect(result.explicitSetPaths).toBeUndefined();
17801801
expect(result.cfg.agents?.entries).toEqual({ ops: { default: true } });
17811802
});
17821803

‎src/commands/doctor-config-flow.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
162162
pendingChanges: false,
163163
fixHints: [],
164164
};
165+
const explicitSetPaths: string[][] = [];
165166
let shouldRepairCronCodexModelRefsAfterConfigWrite = false;
166167
const doctorFixCommand = formatCliCommand("openclaw doctor --fix");
167168
const applyConfigMutation = (
@@ -213,11 +214,14 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
213214
entries: migratedEntries as NonNullable<OpenClawConfig["agents"]>["entries"],
214215
},
215216
},
216-
changes: ["Persisted agents.entries with exactly one explicit default agent."],
217+
changes: ["Prepared agents.entries with exactly one explicit default agent for persistence."],
217218
};
218219
applyConfigMutation(rosterRepair, {
219220
fixHint: `Run "${doctorFixCommand}" to persist the explicit agent roster.`,
220221
});
222+
// Read-time normalization already exposes this roster in the runtime shape.
223+
// Preserve doctor's write intent so the atomic writer does not restore the authored omission.
224+
explicitSetPaths.push(["agents", "entries"]);
221225
}
222226
applyConfigMutation(materializeDefaultAgentRoles(state.candidate), {
223227
fixHint: `Run "${doctorFixCommand}" to persist explicit ambient agent targets.`,
@@ -503,6 +507,7 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
503507
sourceConfigValid: snapshot.valid,
504508
...(sourceLastTouchedVersion ? { sourceLastTouchedVersion } : {}),
505509
...(legacyMigrationPartiallyValid ? { skipPluginValidationOnWrite: true } : {}),
510+
...(finalized.shouldWriteConfig && explicitSetPaths.length > 0 ? { explicitSetPaths } : {}),
506511
...(singleTopLevelIncludeWrite ? { skipWizardMetadataForIncludeWrite: true } : {}),
507512
...(shouldRepairCronCodexModelRefsAfterConfigWrite
508513
? { shouldRepairCronCodexModelRefsAfterConfigWrite: true }

‎src/flows/doctor-health-contribution-runners.config.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ export async function runWriteConfigHealth(
3333
const { replaceConfigFile } = await import("../config/config.js");
3434
const { logConfigUpdated } = await import("../config/logging.js");
3535
const { shortenHomePath } = await import("../utils.js");
36+
const configResultWritePending =
37+
ctx.configResult.shouldWriteConfig === true && ctx.configResultWriteCommitted !== true;
3638
const shouldWriteConfig =
37-
(ctx.configResult.shouldWriteConfig && ctx.configResultWriteCommitted !== true) ||
38-
JSON.stringify(ctx.cfg) !== JSON.stringify(ctx.cfgForPersistence);
39+
configResultWritePending || JSON.stringify(ctx.cfg) !== JSON.stringify(ctx.cfgForPersistence);
3940
if (shouldWriteConfig) {
4041
const updateDoctorRun = isUpdateDoctorRun(ctx.env ?? process.env);
4142
if (ctx.configResult.skipWizardMetadataForIncludeWrite !== true) {
@@ -58,6 +59,9 @@ export async function runWriteConfigHealth(
5859
allowConfigSizeDrop: ctx.configResult.shouldWriteConfig === true || updateDoctorRun,
5960
skipPluginValidation:
6061
ctx.configResult.skipPluginValidationOnWrite === true || updateDoctorRun,
62+
...(configResultWritePending && ctx.configResult.explicitSetPaths
63+
? { explicitSetPaths: ctx.configResult.explicitSetPaths }
64+
: {}),
6165
preservedLegacyRootKeys: ctx.configResult.preservedLegacyRootKeys,
6266
...(legacyParentVersionOverride
6367
? { lastTouchedVersionOverride: legacyParentVersionOverride }

‎src/flows/doctor-health-contribution-types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type DoctorConfigResult = {
1515
sourceConfigValid?: boolean;
1616
sourceLastTouchedVersion?: string;
1717
skipPluginValidationOnWrite?: boolean;
18+
explicitSetPaths?: readonly (readonly string[])[];
1819
skipWizardMetadataForIncludeWrite?: boolean;
1920
preservedLegacyRootKeys?: readonly string[];
2021
shouldRepairCronCodexModelRefsAfterConfigWrite?: boolean;

‎src/flows/doctor-health-contributions.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,35 @@ describe("doctor health contributions", () => {
36463646
);
36473647
});
36483648

3649+
it("forwards explicit paths only for the pending doctor migration write", async () => {
3650+
const ctx = buildWriteConfigCtx({});
3651+
ctx.configResult.explicitSetPaths = [["agents", "entries"]];
3652+
3653+
await writeConfigContribution.run(ctx);
3654+
3655+
expect(mocks.replaceConfigFile).toHaveBeenNthCalledWith(
3656+
1,
3657+
expect.objectContaining({
3658+
writeOptions: expect.objectContaining({
3659+
explicitSetPaths: [["agents", "entries"]],
3660+
}),
3661+
}),
3662+
);
3663+
3664+
ctx.cfg = { gateway: { mode: "remote" } };
3665+
await writeConfigContribution.run(ctx);
3666+
3667+
expect(mocks.replaceConfigFile).toHaveBeenCalledTimes(2);
3668+
expect(mocks.replaceConfigFile).toHaveBeenNthCalledWith(
3669+
2,
3670+
expect.objectContaining({
3671+
writeOptions: expect.not.objectContaining({
3672+
explicitSetPaths: expect.anything(),
3673+
}),
3674+
}),
3675+
);
3676+
});
3677+
36493678
it("points update-time config rewrites at the pre-update backup", async () => {
36503679
vi.mocked(fs.existsSync).mockImplementation((value) => String(value).endsWith(".pre-update"));
36513680
const ctx = buildWriteConfigCtx({

0 commit comments

Comments
 (0)