Skip to content

Commit bf9a04e

Browse files
authored
fix(auth): relogin repairs stale profile order after upgrade
Closes #135140 Reported by @mattcbianco. ## What Problem This Solves Fixes an issue where users who signed in to OpenAI again after shared auth-store relocation could still get `Explicit auth order for openai has no usable profiles.` before a model request started. ## Why This Change Was Made Login credentials and per-agent profile order have separate SQLite owners after relocation. Profile promotion now reads the effective credential view before it updates the agent-local order, and it preserves the inherited profile ID when it saves that order. This keeps explicit order fail-closed behavior unchanged. It does not add a dispatch fallback or copy shared OAuth credentials into the agent database. ## User Impact A successful OpenAI relogin now places the new usable profile before stale entries, so the next `openai/*` model call can start with the login that just succeeded. ## Evidence - Current-main baseline: `2e3f734017b9b1a5a32ba11d844d1fa7a5141de9`. - Before: Codex reported a healthy ChatGPT login and OpenClaw listed a healthy OAuth profile, but the selected Codex runtime had no effective profile. Real Luna and Sol turns stopped before provider dispatch with `Explicit auth order for openai has no usable profiles.` - After: OpenClaw device login stored the fresh OAuth profile, agent-local order became `[fresh, stale]`, and status reported a usable Codex runtime route. - Live exact-head result: a real `openclaw agent --local` Luna turn returned exactly `ISSUE_135140_EXACT_HEAD_GREEN_2` through provider `openai`. - Shipped CLI regression: `models auth login --provider openai --agent main` failed on the baseline because local order stayed absent; passed after the fix with `[fresh, stale]`. - Focused CLI, owner, and sibling tests: 131 passed across login promotion, auth upsert/rollback, shared-store relocation, and runtime auth planning. - Oxfmt, Oxlint, and `git diff --check`: passed. - Autoreview P0: clean. Production delta: `+4/-5` (net `-1`). Test delta: `+108/-0`. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 704f610 commit bf9a04e

2 files changed

Lines changed: 112 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export async function setAuthProfileOrder(params: {
125125
});
126126
}
127127

128-
/** Promotes one auth profile to the front of a provider order. */
128+
/** Promotes across shared-credential/local-order owners; otherwise relogin leaves stale order. */
129129
export async function promoteAuthProfileInOrder(params: {
130130
agentDir?: string;
131131
provider: string;
@@ -134,13 +134,12 @@ export async function promoteAuthProfileInOrder(params: {
134134
createFromOrder?: string[];
135135
}): Promise<AuthProfileStore | null> {
136136
const providerKey = resolveProviderIdForAuth(params.provider);
137+
const effectiveStore = ensureAuthProfileStoreForLocalUpdate(params.agentDir);
137138
return await updateAuthProfileStoreWithLock({
138139
agentDir: params.agentDir,
139-
...(params.createFromOrder
140-
? { saveOptions: { preserveOrderProfileIds: params.createFromOrder } }
141-
: {}),
140+
saveOptions: { preserveOrderProfileIds: [params.profileId, ...(params.createFromOrder ?? [])] },
142141
updater: (store) => {
143-
const profile = store.profiles[params.profileId];
142+
const profile = store.profiles[params.profileId] ?? effectiveStore.profiles[params.profileId];
144143
if (!profile || resolveProviderIdForAuth(profile.provider) !== providerKey) {
145144
return false;
146145
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Exercises the shipped models auth login command across shared credential and local order owners.
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { loadPersistedAuthProfileStore } from "../agents/auth-profiles/persisted.js";
4+
import { loadAuthProfileStoreForRuntime } from "../agents/auth-profiles/store.js";
5+
import type { ProviderPlugin } from "../plugins/types.js";
6+
import { writeConfigMachineState } from "../state/config-machine-state.js";
7+
import { runRegisteredCli } from "../test-utils/command-runner.js";
8+
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
9+
import { registerModelsCli } from "./models-cli.js";
10+
11+
const FRESH_PROFILE_ID = "openai:fresh-login";
12+
const STALE_PROFILE_ID = "openai:stale-login";
13+
14+
const mocks = vi.hoisted(() => ({
15+
callGateway: vi.fn(async () => ({})),
16+
runAuth: vi.fn(async () => ({
17+
profiles: [
18+
{
19+
profileId: "openai:fresh-login",
20+
credential: {
21+
type: "oauth" as const,
22+
provider: "openai" as const,
23+
access: "fresh-access",
24+
refresh: "fresh-refresh",
25+
expires: Date.now() + 60_000,
26+
},
27+
},
28+
],
29+
})),
30+
}));
31+
32+
vi.mock("../gateway/call.js", () => ({ callGateway: mocks.callGateway }));
33+
vi.mock("../plugins/setup-registry.js", () => ({
34+
resolvePluginSetupProviderCore: () => undefined,
35+
resolvePluginSetupRegistry: () => ({ providers: [] }),
36+
}));
37+
vi.mock("../plugins/providers.runtime.js", () => ({
38+
resolvePluginProvidersCore: () =>
39+
[
40+
{
41+
id: "openai",
42+
label: "OpenAI",
43+
auth: [
44+
{
45+
id: "oauth",
46+
label: "OAuth",
47+
kind: "oauth",
48+
run: mocks.runAuth,
49+
},
50+
],
51+
},
52+
] satisfies ProviderPlugin[],
53+
}));
54+
55+
function makeStdinInteractive(): () => void {
56+
const stdin = process.stdin as NodeJS.ReadStream & { isTTY?: boolean };
57+
const descriptor = Object.getOwnPropertyDescriptor(stdin, "isTTY");
58+
Object.defineProperty(stdin, "isTTY", { configurable: true, get: () => true });
59+
return () => {
60+
if (descriptor) {
61+
Object.defineProperty(stdin, "isTTY", descriptor);
62+
} else {
63+
delete stdin.isTTY;
64+
}
65+
};
66+
}
67+
68+
describe("models auth login owner integration", () => {
69+
let restoreStdin: (() => void) | undefined;
70+
71+
afterEach(() => {
72+
restoreStdin?.();
73+
restoreStdin = undefined;
74+
vi.clearAllMocks();
75+
});
76+
77+
it("promotes a relocated shared login through the shipped CLI command", async () => {
78+
await withOpenClawTestState(
79+
{ label: "models-auth-login-owner", scenario: "minimal" },
80+
async (state) => {
81+
await state.writeConfig({
82+
agents: { list: [{ id: "main" }] },
83+
auth: { order: { openai: [STALE_PROFILE_ID] } },
84+
});
85+
writeConfigMachineState("auth.sharedStore", { location: "state-db" }, { env: state.env });
86+
restoreStdin = makeStdinInteractive();
87+
88+
await runRegisteredCli({
89+
register: registerModelsCli,
90+
argv: ["models", "auth", "login", "--provider", "openai", "--agent", "main"],
91+
});
92+
93+
expect(mocks.runAuth).toHaveBeenCalledOnce();
94+
expect(loadPersistedAuthProfileStore()?.profiles[FRESH_PROFILE_ID]).toMatchObject({
95+
type: "oauth",
96+
provider: "openai",
97+
});
98+
expect(
99+
loadPersistedAuthProfileStore(state.agentDir())?.profiles[FRESH_PROFILE_ID],
100+
).toBeUndefined();
101+
expect(loadAuthProfileStoreForRuntime(state.agentDir()).order?.openai).toEqual([
102+
FRESH_PROFILE_ID,
103+
STALE_PROFILE_ID,
104+
]);
105+
},
106+
);
107+
});
108+
});

0 commit comments

Comments
 (0)