Skip to content

Commit a1a0754

Browse files
committed
fix(doctor): scope auth refresh by agent dir
1 parent 1e32871 commit a1a0754

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/commands/doctor-auth.profile-health.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const authProfileMocks = vi.hoisted(() => ({
2020
>(() => {
2121
throw new Error("unexpected auth profile load");
2222
}),
23-
hasAnyAuthProfileStoreSource: vi.fn(() => false),
23+
hasAnyAuthProfileStoreSource: vi.fn((_agentDir?: string) => false),
2424
resolveApiKeyForProfile: vi.fn(),
2525
resolveProfileUnusableUntilForDisplay: vi.fn(),
2626
}));
@@ -87,6 +87,32 @@ describe("noteAuthProfileHealth", () => {
8787
expect(authProfileMocks.ensureAuthProfileStore).not.toHaveBeenCalled();
8888
});
8989

90+
it("checks the configured default agent auth store source", async () => {
91+
const defaultDir = path.join(tempDir, "custom-default");
92+
authProfileMocks.hasAnyAuthProfileStoreSource.mockImplementation(
93+
(agentDir) => agentDir === defaultDir,
94+
);
95+
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
96+
version: 1,
97+
profiles: {},
98+
});
99+
100+
await noteAuthProfileHealth({
101+
cfg: {
102+
agents: {
103+
list: [{ id: "main", default: true, agentDir: defaultDir }],
104+
},
105+
} as OpenClawConfig,
106+
prompter: {} as DoctorPrompter,
107+
allowKeychainPrompt: false,
108+
});
109+
110+
expect(authProfileMocks.hasAnyAuthProfileStoreSource).toHaveBeenCalledWith(defaultDir);
111+
expect(authProfileMocks.ensureAuthProfileStore).toHaveBeenCalledWith(defaultDir, {
112+
allowKeychainPrompt: false,
113+
});
114+
});
115+
90116
it("labels model auth diagnostics by agent when multiple agent auth stores are checked", async () => {
91117
const now = 1_700_000_000_000;
92118
vi.spyOn(Date, "now").mockReturnValue(now);
@@ -129,4 +155,41 @@ describe("noteAuthProfileHealth", () => {
129155
"Model auth (agent: coder)",
130156
);
131157
});
158+
159+
it("passes the target agent dir when refreshing OAuth profiles", async () => {
160+
const now = 1_700_000_000_000;
161+
vi.spyOn(Date, "now").mockReturnValue(now);
162+
const coderDir = path.join(tempDir, "coder-agent");
163+
writeAuthStore(coderDir);
164+
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(false);
165+
authProfileMocks.ensureAuthProfileStore.mockImplementation((agentDir) => {
166+
if (agentDir === coderDir) {
167+
return expiredStore("openai-codex:coder", now - 60_000);
168+
}
169+
return { version: 1, profiles: {} };
170+
});
171+
authProfileMocks.resolveApiKeyForProfile.mockResolvedValue("token");
172+
173+
await noteAuthProfileHealth({
174+
cfg: {
175+
agents: {
176+
list: [
177+
{ id: "main", default: true, agentDir: path.join(tempDir, "main-agent") },
178+
{ id: "coder", agentDir: coderDir },
179+
],
180+
},
181+
} as OpenClawConfig,
182+
prompter: {
183+
confirmAutoFix: vi.fn(async () => true),
184+
} as unknown as DoctorPrompter,
185+
allowKeychainPrompt: false,
186+
});
187+
188+
expect(authProfileMocks.resolveApiKeyForProfile).toHaveBeenCalledWith(
189+
expect.objectContaining({
190+
agentDir: coderDir,
191+
profileId: "openai-codex:coder",
192+
}),
193+
);
194+
});
132195
});

src/commands/doctor-auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ async function noteAuthProfileHealthForTarget(params: {
334334
cfg: params.cfg,
335335
store,
336336
profileId: profile.profileId,
337+
agentDir: params.target.agentDir,
337338
});
338339
} catch (err) {
339340
const message = formatErrorMessage(err);
@@ -388,7 +389,7 @@ export async function noteAuthProfileHealth(params: {
388389
const targets = listAuthProfileHealthTargets(params.cfg);
389390
const activeTargets = targets.filter((target) =>
390391
target.isDefault
391-
? hasAnyAuthProfileStoreSource() || configuredProfiles
392+
? hasAnyAuthProfileStoreSource(target.agentDir) || configuredProfiles
392393
: hasLocalAuthProfileStoreSource(target.agentDir),
393394
);
394395
if (activeTargets.length === 0) {

0 commit comments

Comments
 (0)