Skip to content

Commit 6405a59

Browse files
authored
fix(doctor): stop reporting phantom shared auth migrations (#124929)
1 parent e95300f commit 6405a59

5 files changed

Lines changed: 105 additions & 6 deletions

‎src/commands/doctor-auth.hints.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Doctor auth hint tests cover OAuth refresh failure formatting and auth repair guidance.
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
4+
import { resolveSharedMainAuthAgentDir } from "../agents/auth-profiles/shared-main-dir.js";
5+
import { writePersistedAuthProfileStoreRaw } from "../agents/auth-profiles/sqlite.js";
46
import type { OpenClawConfig } from "../config/types.openclaw.js";
57
import { writeConfigMachineState } from "../state/config-machine-state.js";
68
import {
@@ -68,11 +70,31 @@ describe("doctor auth hints", () => {
6870
);
6971
});
7072

71-
it("reports the legacy shared auth owner with the migration command", () => {
72-
noteSharedAuthStoreStatus({
73+
it("does not report a legacy shared auth owner without stored credentials", () => {
74+
const env = {
7375
...process.env,
7476
OPENCLAW_STATE_DIR: tempDirs.make("openclaw-doctor-shared-auth-"),
75-
});
77+
};
78+
noteSharedAuthStoreStatus(env);
79+
80+
expect(mocks.note).not.toHaveBeenCalled();
81+
});
82+
83+
it("reports the legacy shared auth owner with stored credentials", () => {
84+
const env = {
85+
...process.env,
86+
OPENCLAW_STATE_DIR: tempDirs.make("openclaw-doctor-shared-auth-"),
87+
};
88+
writePersistedAuthProfileStoreRaw(
89+
{
90+
version: 1,
91+
profiles: {
92+
"openai:default": { type: "api_key", provider: "openai", key: "test-key" },
93+
},
94+
},
95+
resolveSharedMainAuthAgentDir(env),
96+
);
97+
noteSharedAuthStoreStatus(env);
7698

7799
expect(mocks.note).toHaveBeenCalledWith(
78100
expect.stringContaining("openclaw doctor --fix"),

‎src/commands/doctor-auth.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
resolveAuthStorePathForDisplay,
3535
resolveSharedAuthStoreOwnership,
3636
} from "../agents/auth-profiles/path-resolve.js";
37+
import { inspectPersistedSharedAuthProfileStoreRaw } from "../agents/auth-profiles/sqlite.js";
3738
import { buildProviderAuthRecoveryHint } from "../agents/provider-auth-recovery-hint.js";
3839
import type { OpenClawConfig } from "../config/types.openclaw.js";
3940
import type { HealthFinding } from "../flows/health-checks.js";
@@ -54,7 +55,10 @@ const DOCTOR_REAUTH_PROVIDER_ALIASES: Readonly<Record<string, string>> = {
5455

5556
/** Surface the one-time relocation while the legacy shared owner is still active. */
5657
export function noteSharedAuthStoreStatus(env: NodeJS.ProcessEnv = process.env): void {
57-
if (resolveSharedAuthStoreOwnership(env).location !== "legacy-main") {
58+
if (
59+
resolveSharedAuthStoreOwnership(env).location !== "legacy-main" ||
60+
inspectPersistedSharedAuthProfileStoreRaw(env).status !== "readable"
61+
) {
5862
return;
5963
}
6064
note(

‎src/commands/doctor-state-migrations.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,37 @@ describe("doctor legacy state migrations", () => {
952952

953953
expect(result.warnings).toEqual([]);
954954
expect(result.changes).toContain("Relocated shared auth profiles into shared SQLite state.");
955+
expect(result.notices).toContain(
956+
"The main agent no longer owns shared credentials and can now be deleted.",
957+
);
955958
expect(readPersistedSharedAuthProfileStoreRaw(env)).toEqual(store);
956959
expect(readPersistedAuthProfileStoreRaw(mainAgentDir)).toBeNull();
957960
});
958961

962+
it("records fresh shared auth ownership without reporting a relocation", async () => {
963+
const stateDir = makeDoctorStateDir();
964+
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
965+
966+
const result = await autoMigrateLegacyState({
967+
cfg: {},
968+
env,
969+
doctorOnlyStateMigrations: true,
970+
});
971+
972+
expect(result.changes).not.toContain(
973+
"Relocated shared auth profiles into shared SQLite state.",
974+
);
975+
expect(result.notices ?? []).not.toContain(
976+
"The main agent no longer owns shared credentials and can now be deleted.",
977+
);
978+
const database = openOpenClawStateDatabase({ env }).db;
979+
expect(
980+
database
981+
.prepare("SELECT value_json FROM config_machine_state WHERE state_key = 'auth.sharedStore'")
982+
.get(),
983+
).toEqual({ value_json: JSON.stringify({ location: "state-db" }) });
984+
});
985+
959986
it("removes stale transcript paths left by a shipped legacy migration", async () => {
960987
const root = makeDoctorStateDir();
961988
const legacyDir = path.join(root, "sessions");

‎src/infra/state-migrations.shared-auth-store.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,49 @@ describe("shared auth store relocation", () => {
7777
};
7878
}
7979

80+
async function createEmptyFixture(createSourceDatabase: boolean) {
81+
const stateDir = tempDirs.make("openclaw-shared-auth-empty-");
82+
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
83+
vi.stubEnv("OPENCLAW_AGENT_DIR", "");
84+
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir, OPENCLAW_AGENT_DIR: undefined };
85+
const [paths, ownership, sqlite, migration] = await Promise.all([
86+
import("../agents/auth-profiles/shared-main-dir.js"),
87+
import("../agents/auth-profiles/path-resolve.js"),
88+
import("../agents/auth-profiles/sqlite.js"),
89+
import("./state-migrations.shared-auth-store.js"),
90+
]);
91+
const mainAgentDir = paths.resolveSharedMainAuthAgentDir(env);
92+
const sourcePath = sqlite.resolveAuthProfileDatabasePath(mainAgentDir);
93+
if (createSourceDatabase) {
94+
sqlite.writePersistedAuthProfileStoreRaw({ version: 1, profiles: {} }, mainAgentDir);
95+
sqlite.deletePersistedAuthProfileStoreRaw(mainAgentDir);
96+
}
97+
return { env, stateDir, sourcePath, ownership, migration };
98+
}
99+
100+
it.each([
101+
{ label: "fresh profile", createSourceDatabase: false },
102+
{ label: "legacy profile with an empty source database", createSourceDatabase: true },
103+
])("records ownership without reporting relocation for a $label", async (testCase) => {
104+
const fixture = await createEmptyFixture(testCase.createSourceDatabase);
105+
expect(fs.existsSync(fixture.sourcePath)).toBe(testCase.createSourceDatabase);
106+
107+
const detected = fixture.migration.detectSharedAuthStoreMigration({
108+
stateDir: fixture.stateDir,
109+
doctorOnlyStateMigrations: true,
110+
});
111+
expect(detected.hasLegacy).toBe(true);
112+
expect(
113+
await fixture.migration.migrateSharedAuthStore({
114+
detected,
115+
stateDir: fixture.stateDir,
116+
}),
117+
).toEqual({ changes: [], warnings: [] });
118+
expect(fixture.ownership.resolveSharedAuthStoreOwnership(fixture.env)).toEqual({
119+
location: "state-db",
120+
});
121+
});
122+
80123
it("moves exact rows, preserves every effective agent store, and records receipts", async () => {
81124
const fixture = await createFixture();
82125
const effectiveBytes = (agentDir: string) => {

‎src/infra/state-migrations.shared-auth-store.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ export async function migrateSharedAuthStore(params: {
599599
now,
600600
});
601601
const sourceCleaned = cleanupSourceRows({ env, sourcePath: params.detected.sourcePath });
602+
const relocatedRows = rows.store !== null || rows.state !== null || sourceCleaned;
602603
finalizeMigration({
603604
env,
604605
sourcePath: params.detected.sourcePath,
@@ -608,13 +609,15 @@ export async function migrateSharedAuthStore(params: {
608609
});
609610
return {
610611
changes: [
611-
...(ownershipFlipped ? ["Relocated shared auth profiles into shared SQLite state."] : []),
612+
...(ownershipFlipped && relocatedRows
613+
? ["Relocated shared auth profiles into shared SQLite state."]
614+
: []),
612615
...(sourceCleaned && !ownershipFlipped
613616
? ["Completed legacy shared auth row cleanup."]
614617
: []),
615618
],
616619
warnings: [],
617-
...(ownershipFlipped
620+
...(ownershipFlipped && rows.store !== null
618621
? {
619622
notices: ["The main agent no longer owns shared credentials and can now be deleted."],
620623
}

0 commit comments

Comments
 (0)