Skip to content

Commit 2777d64

Browse files
author
scotthuang
committed
fix(doctor): keep migrated agents on their existing workspace
1 parent 3289e08 commit 2777d64

3 files changed

Lines changed: 93 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ export async function loadAndMaybeMigrateDoctorConfig(params: {
238238
const includeOwnsRoster = configIncludeOwnsAgentRoster(snapshot);
239239
if (snapshot.exists && rosterMigrationNeeded && !includeOwnsRoster) {
240240
// Runtime roster normalization is read-only; doctor --fix owns persistence.
241-
const migrated = migratePersistedImplicitMainRoster(state.candidate).config as OpenClawConfig;
241+
// Persist the legacy owner's workspace in doctor's canonical candidate. The writer may run
242+
// again after health repairs, when the retired owner marker is no longer available to recover it.
243+
const migrated = migratePersistedImplicitMainRoster(state.candidate, {
244+
materializeWorkspace: true,
245+
}).config as OpenClawConfig;
242246
const migratedRoster = readAgentRosterProperty(migrated);
243247
const migratedEntries = migratedRoster?.kind === "entries" ? migratedRoster.value : undefined;
244248
const { list: _legacyList, ...candidateAgents } = migrated.agents ?? {};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import path from "node:path";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { readConfigFileSnapshot } from "../config/config.js";
4+
import { withEnvOverride, withTempHome, writeOpenClawConfig } from "../config/test-helpers.js";
5+
import {
6+
runInitialConfigWriteHealth,
7+
runWriteConfigHealth,
8+
} from "../flows/doctor-health-contribution-runners.config.js";
9+
import type { DoctorHealthFlowContext } from "../flows/doctor-health-contribution-types.js";
10+
import type { RuntimeEnv } from "../runtime.js";
11+
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
12+
import { loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow.js";
13+
import { createDoctorPrompter, type DoctorOptions } from "./doctor-prompter.js";
14+
15+
describe("Doctor workspace persistence", () => {
16+
afterEach(() => {
17+
closeOpenClawStateDatabaseForTest();
18+
});
19+
20+
it("keeps the legacy owner on the shared workspace across later health writes", async () => {
21+
await withTempHome(async (home) => {
22+
await withEnvOverride({ OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1" }, async () => {
23+
const workspace = path.join(home, "shared-workspace");
24+
const configPath = await writeOpenClawConfig(home, {
25+
agents: {
26+
defaults: { workspace },
27+
entries: {
28+
main: { default: true },
29+
cursor: { workspace },
30+
},
31+
},
32+
gateway: { mode: "local" },
33+
});
34+
const runtime: RuntimeEnv = {
35+
error: vi.fn(),
36+
exit: vi.fn(),
37+
log: vi.fn(),
38+
};
39+
const options: DoctorOptions = { nonInteractive: true, repair: true };
40+
const prompter = createDoctorPrompter({ runtime, options });
41+
const configResult = await loadAndMaybeMigrateDoctorConfig({
42+
options,
43+
confirm: (params) => prompter.confirm(params),
44+
runtime,
45+
prompter,
46+
});
47+
const ctx: DoctorHealthFlowContext = {
48+
runtime,
49+
options,
50+
prompter,
51+
configResult,
52+
cfg: configResult.cfg,
53+
cfgForPersistence: structuredClone(configResult.cfg),
54+
sourceConfigValid: configResult.sourceConfigValid ?? true,
55+
configPath,
56+
stateDirExistedAtStart: true,
57+
...(configResult.runWithPluginMetadataSnapshot
58+
? { runWithPluginMetadataSnapshot: configResult.runWithPluginMetadataSnapshot }
59+
: {}),
60+
...(configResult.invalidatePluginMetadataSnapshot
61+
? { invalidatePluginMetadataSnapshot: configResult.invalidatePluginMetadataSnapshot }
62+
: {}),
63+
};
64+
65+
await runInitialConfigWriteHealth(ctx);
66+
expect((await readConfigFileSnapshot()).config.agents?.entries?.main?.workspace).toBe(
67+
workspace,
68+
);
69+
70+
ctx.cfg = {
71+
...ctx.cfg,
72+
gateway: { ...ctx.cfg.gateway, bind: "lan" },
73+
};
74+
await runWriteConfigHealth(ctx);
75+
76+
const snapshot = await readConfigFileSnapshot();
77+
expect(snapshot.valid).toBe(true);
78+
expect(snapshot.config.agents?.ownership).toBe("explicit");
79+
expect(snapshot.config.agents?.entries?.main?.workspace).toBe(workspace);
80+
});
81+
});
82+
});
83+
});

‎src/config/legacy.roster.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ type MigrationResult = {
1515
retainedLegacyDefaultAgentId?: string;
1616
};
1717

18-
export function migratePersistedImplicitMainRoster(raw: unknown): MigrationResult {
18+
export function migratePersistedImplicitMainRoster(
19+
raw: unknown,
20+
options: { materializeWorkspace?: boolean } = {},
21+
): MigrationResult {
1922
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
2023
return { config: raw, changed: false, diagnostics: [] };
2124
}
@@ -124,6 +127,7 @@ export function migratePersistedImplicitMainRoster(raw: unknown): MigrationResul
124127
const materialized = materializeLegacyDefaultAgentRoles(
125128
nextRoot as OpenClawConfig,
126129
legacyDefaultAgentId,
130+
options,
127131
);
128132
nextRoot = materialized.config as Record<string, unknown>;
129133
insertedPaths = materialized.insertedPaths;

0 commit comments

Comments
 (0)