Skip to content

Commit 81ace37

Browse files
authored
fix(auth): stop advising a retry when the auth store needs migration (#127306)
Writing an auth profile on an install whose credentials still live in the retired JSON store printed two contradictory lines: the real migration error, followed by a generic "the auth store lock may be busy. Wait a moment and retry." An operator reading the last line retries forever, because nothing is locked and the write can never succeed until Doctor migrates the store. updateAuthProfileStoreWithLock wrapped its transaction in a blanket catch that logged a warn and returned null, erasing the failure's identity. Callers only saw null, so the sole guess available to them was lock contention. Read paths already let AuthProfileMigrationRequiredError and AuthProfileStoreUnreadableError through; only the write path swallowed them. Rethrow those two credential-boundary errors at the producer and keep null for genuine write failures, which is what the lock hint was always describing. All three sibling call sites (upsertAuthProfileWithLockOrThrow, models auth logout, models auth order set) funnel through that null, so they are fixed by the one change. Onboard now prints only the migration line, and `openclaw doctor --fix` followed by the same onboard command succeeds.
1 parent 43c54af commit 81ace37

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎src/agents/auth-profiles/source-check.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ describe("hasAuthProfileStoreSourceForProvider", () => {
7979
writePersistedAuthProfileStoreRaw(unreadableStore, agentDir);
8080
const updater = vi.fn(() => true);
8181

82-
await expect(updateAuthProfileStoreWithLock({ agentDir, updater })).resolves.toBeNull();
82+
// The unreadable store names its own remediation; a null return would make
83+
// callers report generic lock contention instead.
84+
await expect(updateAuthProfileStoreWithLock({ agentDir, updater })).rejects.toThrow(
85+
"is unreadable; run openclaw doctor --fix",
86+
);
8387
expect(updater).not.toHaveBeenCalled();
8488
expect(readPersistedAuthProfileStoreRaw(agentDir)).toEqual(unreadableStore);
8589
});

‎src/agents/auth-profiles/store.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ function mergeRuntimeExternalProfileState(params: {
862862
return merged;
863863
}
864864

865-
/** Apply an auth store update inside the SQLite write lock. */
865+
/** Apply an auth store update inside the SQLite write lock; null on write failure, rethrows credential-boundary errors. */
866866
export async function updateAuthProfileStoreWithLock(params: {
867867
agentDir?: string;
868868
sharedStoreWrite?: boolean;
@@ -896,6 +896,14 @@ export async function updateAuthProfileStoreWithLock(params: {
896896
{ sharedStoreWrite: params.sharedStoreWrite, stateDir: params.stateDir },
897897
);
898898
} catch (error) {
899+
// Credential-boundary failures name their own remediation; collapsing them
900+
// into `null` makes callers report a lock-contention retry that never clears.
901+
const isCredentialBoundaryFailure =
902+
error instanceof AuthProfileMigrationRequiredError ||
903+
error instanceof AuthProfileStoreUnreadableError;
904+
if (isCredentialBoundaryFailure) {
905+
throw error;
906+
}
899907
const message = error instanceof Error ? error.message : String(error);
900908
authProfilesLog.warn(`auth profile store update failed: ${message}`, {
901909
agentDir,

‎src/agents/auth-profiles/upsert-with-lock.sqlite.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../../state/openclaw-agent-db.js";
99
import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js";
1010
import { withEnvAsync } from "../../test-utils/env.js";
11+
import { clearAuthProfileMigrationDiagnostics } from "./legacy-source-diagnostic.js";
1112
import { loadPersistedAuthProfileStore } from "./persisted.js";
1213
import {
1314
inspectPersistedAuthProfileStateRaw,
@@ -16,10 +17,14 @@ import {
1617
} from "./sqlite.js";
1718
import { saveAuthProfileStore, updateAuthProfileStoreWithLock } from "./store.js";
1819
import type { ApiKeyCredential } from "./types.js";
19-
import { persistAuthProfileBatch } from "./upsert-with-lock.js";
20+
import { persistAuthProfileBatch, upsertAuthProfileWithLockOrThrow } from "./upsert-with-lock.js";
2021

2122
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
2223

24+
afterEach(() => {
25+
clearAuthProfileMigrationDiagnostics();
26+
});
27+
2328
function apiKey(key: string): ApiKeyCredential {
2429
return { type: "api_key", provider: "openai", key };
2530
}
@@ -165,6 +170,30 @@ describe("auth profile batch persistence", () => {
165170
});
166171
});
167172

173+
it("reports the migration remediation instead of lock-contention retry advice", async () => {
174+
await withAgentDir(async (agentDir) => {
175+
// Credentials still living in the retired JSON store: the write can never
176+
// succeed, so retry advice would loop the operator forever.
177+
fs.writeFileSync(
178+
path.join(agentDir, "auth-profiles.json"),
179+
JSON.stringify({
180+
version: 1,
181+
profiles: { "openai:legacy": apiKey("sk-json-era") },
182+
}),
183+
);
184+
185+
const failure = await upsertAuthProfileWithLockOrThrow({
186+
agentDir,
187+
profileId: "openai:new",
188+
credential: apiKey("sk-new"),
189+
}).catch((error: unknown) => error);
190+
191+
expect(String(failure)).toContain("requires legacy credential migration");
192+
expect(String(failure)).toContain("openclaw doctor --fix");
193+
expect(String(failure)).not.toContain("lock may be busy");
194+
});
195+
});
196+
168197
it("leaves no partial profile batch when the SQLite state write fails", async () => {
169198
await withAgentDir(async (agentDir) => {
170199
const database = openOpenClawAgentDatabase({

0 commit comments

Comments
 (0)