Skip to content

Commit c6c4d53

Browse files
committed
fix(doctor): report shared auth health without a default agent
1 parent 2a920ab commit c6c4d53

4 files changed

Lines changed: 470 additions & 197 deletions

File tree

‎docs/cli/doctor.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ openclaw doctor --lint --skip core/doctor/skills-readiness
241241

242242
`--only` and `--skip` accept full check ids and may be repeated. If an `--only` id is not registered, no check runs for that id; use `checksRun`/`checksSkipped` in the output to confirm a focused gate selects the checks you expect.
243243

244+
To check model credentials, run `openclaw doctor --lint --only core/doctor/auth-profiles --json`.
245+
This opt-in check inspects shared credentials and each configured agent's local
246+
auth store, including fleets without a default agent. Shared credential problems
247+
are reported once; agent-specific cooldowns remain attributed to their local store.
248+
244249
## Post-upgrade mode
245250

246251
`openclaw doctor --post-upgrade` runs plugin compatibility probes for chaining after a build or upgrade. Findings go to stdout; exit code is 1 if any finding has `level: "error"`. Add `--json` for a machine-readable envelope (`{ probesRun, findings }`), suitable for CI, the community `fork-upgrade` skill, and other post-upgrade smoke tooling. If the installed plugin index is missing or malformed, JSON mode still emits the envelope with a `plugin.index_unavailable` error finding.

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

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths
1212
import type { DoctorPrompter } from "./doctor-prompter.js";
1313

1414
const authProfileMocks = vi.hoisted(() => ({
15-
ensureAuthProfileStore: vi.fn<
15+
loadAuthProfileStoreForRuntime: vi.fn<
1616
(
1717
agentDir?: string,
18-
options?: { allowKeychainPrompt?: boolean; readOnly?: boolean },
18+
options?: { allowKeychainPrompt?: boolean; readOnly?: boolean; inheritedAuthDir?: string },
1919
) => AuthProfileStore
2020
>(() => {
2121
throw new Error("unexpected auth profile load");
@@ -28,7 +28,7 @@ const authProfileMocks = vi.hoisted(() => ({
2828

2929
vi.mock("../agents/auth-profiles.js", async (importOriginal) => ({
3030
...(await importOriginal<typeof import("../agents/auth-profiles.js")>()),
31-
ensureAuthProfileStore: authProfileMocks.ensureAuthProfileStore,
31+
loadAuthProfileStoreForRuntime: authProfileMocks.loadAuthProfileStoreForRuntime,
3232
hasAnyAuthProfileStoreSource: authProfileMocks.hasAnyAuthProfileStoreSource,
3333
hasLocalAuthProfileStoreSource: authProfileMocks.hasLocalAuthProfileStoreSource,
3434
resolveApiKeyForProfile: authProfileMocks.resolveApiKeyForProfile,
@@ -52,7 +52,7 @@ describe("noteAuthProfileHealth", () => {
5252
beforeEach(() => {
5353
tempDir = tempDirs.make("openclaw-doctor-auth-");
5454
vi.stubEnv("OPENCLAW_STATE_DIR", tempDir);
55-
authProfileMocks.ensureAuthProfileStore.mockReset();
55+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReset();
5656
authProfileMocks.hasAnyAuthProfileStoreSource.mockReset();
5757
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(false);
5858
authProfileMocks.hasLocalAuthProfileStoreSource.mockReset();
@@ -97,8 +97,10 @@ describe("noteAuthProfileHealth", () => {
9797
vi.spyOn(Date, "now").mockReturnValue(now);
9898
const mainDir = path.join(tempDir, "main-agent");
9999
writeAuthStore(mainDir);
100-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
101-
authProfileMocks.ensureAuthProfileStore.mockReturnValue(
100+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
101+
(agentDir) => agentDir !== undefined,
102+
);
103+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue(
102104
expiredStore("openai:default", now - 60_000),
103105
);
104106

@@ -129,7 +131,7 @@ describe("noteAuthProfileHealth", () => {
129131
writeConfigMachineState("auth.sharedStore", { location: "state-db" });
130132
writePersistedAuthProfileStoreRaw({ version: 1, profiles: {} });
131133
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
132-
authProfileMocks.ensureAuthProfileStore.mockReturnValue(
134+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue(
133135
expiredStore("openai:default", now - 60_000),
134136
);
135137

@@ -150,8 +152,10 @@ describe("noteAuthProfileHealth", () => {
150152
const now = 1_700_000_000_000;
151153
vi.spyOn(Date, "now").mockReturnValue(now);
152154
const mainDir = path.join(tempDir, "main-agent");
153-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
154-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
155+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
156+
(agentDir) => agentDir !== undefined,
157+
);
158+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
155159
version: 1,
156160
profiles: {
157161
"anthropic:static-cli": {
@@ -186,8 +190,10 @@ describe("noteAuthProfileHealth", () => {
186190
const now = 1_700_000_000_000;
187191
vi.spyOn(Date, "now").mockReturnValue(now);
188192
const mainDir = path.join(tempDir, "main-agent");
189-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
190-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
193+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
194+
(agentDir) => agentDir !== undefined,
195+
);
196+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
191197
version: 1,
192198
profiles: {
193199
"anthropic:custom-cli": {
@@ -219,9 +225,11 @@ describe("noteAuthProfileHealth", () => {
219225
vi.spyOn(Date, "now").mockReturnValue(now);
220226
const mainDir = path.join(tempDir, "main-agent");
221227
writeAuthStore(mainDir);
222-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
228+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
229+
(agentDir) => agentDir !== undefined,
230+
);
223231
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
224-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
232+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
225233
version: 1,
226234
profiles: {},
227235
usageStats: {
@@ -263,9 +271,11 @@ describe("noteAuthProfileHealth", () => {
263271
const now = 1_700_000_000_000;
264272
vi.spyOn(Date, "now").mockReturnValue(now);
265273
const mainDir = path.join(tempDir, "main-agent");
266-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
274+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
275+
(agentDir) => agentDir !== undefined,
276+
);
267277
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
268-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
278+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
269279
version: 1,
270280
profiles: {
271281
"openai:disabled": { type: "api_key", provider: "openai", key: "secret" },
@@ -292,9 +302,11 @@ describe("noteAuthProfileHealth", () => {
292302
const now = 1_700_000_000_000;
293303
vi.spyOn(Date, "now").mockReturnValue(now);
294304
const mainDir = path.join(tempDir, "main-agent");
295-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
305+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
306+
(agentDir) => agentDir !== undefined,
307+
);
296308
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
297-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
309+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
298310
version: 1,
299311
profiles: {
300312
"openai:expired": {
@@ -332,9 +344,11 @@ describe("noteAuthProfileHealth", () => {
332344
const now = 1_700_000_000_000;
333345
vi.spyOn(Date, "now").mockReturnValue(now);
334346
const mainDir = path.join(tempDir, "main-agent");
335-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
347+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
348+
(agentDir) => agentDir !== undefined,
349+
);
336350
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
337-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
351+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
338352
...expiredStore("openai:expired", now - 60_000),
339353
usageStats: { "openai:expired": { cooldownUntil: now + 5 * 60_000 } },
340354
});
@@ -355,9 +369,11 @@ describe("noteAuthProfileHealth", () => {
355369
const now = 1_700_000_000_000;
356370
vi.spyOn(Date, "now").mockReturnValue(now);
357371
const mainDir = path.join(tempDir, "main-agent");
358-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
372+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
373+
(agentDir) => agentDir !== undefined,
374+
);
359375
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
360-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
376+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
361377
version: 1,
362378
profiles: {
363379
"google-gemini-cli:legacy": {
@@ -395,9 +411,11 @@ describe("noteAuthProfileHealth", () => {
395411
const now = 1_700_000_000_000;
396412
vi.spyOn(Date, "now").mockReturnValue(now);
397413
const mainDir = path.join(tempDir, "main-agent");
398-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
414+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
415+
(agentDir) => agentDir !== undefined,
416+
);
399417
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
400-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
418+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
401419
version: 1,
402420
profiles: {},
403421
usageStats: { "openai:cooldown": { cooldownUntil: now + 5 * 60_000 } },
@@ -417,8 +435,10 @@ describe("noteAuthProfileHealth", () => {
417435
it("maps malformed API-key auth profiles to structured findings", async () => {
418436
const mainDir = path.join(tempDir, "main-agent");
419437
writeAuthStore(mainDir);
420-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
421-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
438+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
439+
(agentDir) => agentDir !== undefined,
440+
);
441+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
422442
version: 1,
423443
profiles: {
424444
"zai:default": {
@@ -455,9 +475,10 @@ describe("noteAuthProfileHealth", () => {
455475
vi.spyOn(Date, "now").mockReturnValue(now);
456476
const mainDir = path.join(tempDir, "main-agent");
457477
const coderDir = path.join(tempDir, "coder-agent");
458-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
459-
authProfileMocks.hasLocalAuthProfileStoreSource.mockReturnValue(true);
460-
authProfileMocks.ensureAuthProfileStore.mockImplementation((agentDir) => {
478+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
479+
(agentDir) => agentDir !== undefined,
480+
);
481+
authProfileMocks.loadAuthProfileStoreForRuntime.mockImplementation((agentDir) => {
461482
if (agentDir === mainDir) {
462483
return expiredStore("openai:main", now - 60_000);
463484
}
@@ -494,15 +515,15 @@ describe("noteAuthProfileHealth", () => {
494515
});
495516

496517
expect(authProfileMocks.hasAnyAuthProfileStoreSource).toHaveBeenCalledOnce();
497-
expect(authProfileMocks.ensureAuthProfileStore).not.toHaveBeenCalled();
518+
expect(authProfileMocks.loadAuthProfileStoreForRuntime).not.toHaveBeenCalled();
498519
});
499520

500521
it("checks the configured default agent auth store source", async () => {
501522
const defaultDir = path.join(tempDir, "custom-default");
502-
authProfileMocks.hasAnyAuthProfileStoreSource.mockImplementation(
523+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
503524
(agentDir) => agentDir === defaultDir,
504525
);
505-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
526+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
506527
version: 1,
507528
profiles: {},
508529
});
@@ -517,9 +538,11 @@ describe("noteAuthProfileHealth", () => {
517538
allowKeychainPrompt: false,
518539
});
519540

520-
expect(authProfileMocks.hasAnyAuthProfileStoreSource).toHaveBeenCalledWith(defaultDir);
521-
expect(authProfileMocks.ensureAuthProfileStore).toHaveBeenCalledWith(defaultDir, {
541+
expect(authProfileMocks.hasLocalAuthProfileStoreSource).toHaveBeenCalledWith(defaultDir);
542+
expect(authProfileMocks.loadAuthProfileStoreForRuntime).toHaveBeenCalledWith(defaultDir, {
543+
inheritedAuthDir: defaultDir,
522544
allowKeychainPrompt: false,
545+
readOnly: undefined,
523546
});
524547
});
525548

@@ -530,9 +553,10 @@ describe("noteAuthProfileHealth", () => {
530553
const coderDir = path.join(tempDir, "coder-agent");
531554
writeAuthStore(mainDir);
532555
writeAuthStore(coderDir);
533-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
534-
authProfileMocks.hasLocalAuthProfileStoreSource.mockReturnValue(true);
535-
authProfileMocks.ensureAuthProfileStore.mockImplementation((agentDir) => {
556+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
557+
(agentDir) => agentDir !== undefined,
558+
);
559+
authProfileMocks.loadAuthProfileStoreForRuntime.mockImplementation((agentDir) => {
536560
if (agentDir === mainDir) {
537561
return expiredStore("openai-codex:main", now - 60_000);
538562
}
@@ -561,9 +585,9 @@ describe("noteAuthProfileHealth", () => {
561585
expect(modelAuthCalls).toHaveLength(1);
562586
const body = String(modelAuthCalls[0]?.[0]);
563587
expect(body).toContain("openai-codex:coder");
564-
expect(body).toContain("(agents: coder)");
588+
expect(body).toContain("(stores: Agent coder)");
565589
expect(body).toContain("openai-codex:main");
566-
expect(body).toContain("(agents: main)");
590+
expect(body).toContain("(stores: Agent main)");
567591
});
568592

569593
it("deduplicates model auth diagnostics shared by every agent", async () => {
@@ -573,9 +597,10 @@ describe("noteAuthProfileHealth", () => {
573597
const coderDir = path.join(tempDir, "coder-agent");
574598
writeAuthStore(mainDir);
575599
writeAuthStore(coderDir);
576-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
577-
authProfileMocks.hasLocalAuthProfileStoreSource.mockReturnValue(true);
578-
authProfileMocks.ensureAuthProfileStore.mockReturnValue(
600+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
601+
(agentDir) => agentDir !== undefined,
602+
);
603+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue(
579604
expiredStore("openai-codex:shared", now - 60_000),
580605
);
581606

@@ -598,17 +623,19 @@ describe("noteAuthProfileHealth", () => {
598623
expect(modelAuthCalls).toHaveLength(1);
599624
const body = String(modelAuthCalls[0]?.[0]);
600625
expect(body.match(/openai-codex:shared/g)).toHaveLength(1);
601-
expect(body).not.toContain("(agents:");
626+
expect(body).not.toContain("(stores:");
602627
});
603628

604629
it("offers credential repair while the same profile is cooling down", async () => {
605630
const now = 1_700_000_000_000;
606631
vi.spyOn(Date, "now").mockReturnValue(now);
607632
const mainDir = path.join(tempDir, "main-agent");
608633
writeAuthStore(mainDir);
609-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
634+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
635+
(agentDir) => agentDir !== undefined,
636+
);
610637
authProfileMocks.resolveProfileUnusableUntilForDisplay.mockReturnValue(now + 5 * 60_000);
611-
authProfileMocks.ensureAuthProfileStore.mockReturnValue({
638+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue({
612639
...expiredStore("openai-codex:expired", now - 60_000),
613640
usageStats: { "openai-codex:expired": { cooldownUntil: now + 5 * 60_000 } },
614641
});
@@ -638,11 +665,10 @@ describe("noteAuthProfileHealth", () => {
638665
vi.spyOn(Date, "now").mockReturnValue(now);
639666
const mainDir = path.join(tempDir, "main-agent");
640667
const coderDir = path.join(tempDir, "coder-agent");
641-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
642668
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
643669
(agentDir) => agentDir === mainDir,
644670
);
645-
authProfileMocks.ensureAuthProfileStore.mockImplementation((agentDir) => {
671+
authProfileMocks.loadAuthProfileStoreForRuntime.mockImplementation((agentDir) => {
646672
if (agentDir === mainDir) {
647673
return expiredStore("openai-codex:main", now - 60_000);
648674
}
@@ -665,9 +691,11 @@ describe("noteAuthProfileHealth", () => {
665691
});
666692

667693
expect(authProfileMocks.hasLocalAuthProfileStoreSource).toHaveBeenCalledWith(coderDir);
668-
expect(authProfileMocks.ensureAuthProfileStore).toHaveBeenCalledOnce();
669-
expect(authProfileMocks.ensureAuthProfileStore).toHaveBeenCalledWith(mainDir, {
694+
expect(authProfileMocks.loadAuthProfileStoreForRuntime).toHaveBeenCalledOnce();
695+
expect(authProfileMocks.loadAuthProfileStoreForRuntime).toHaveBeenCalledWith(mainDir, {
696+
inheritedAuthDir: mainDir,
670697
allowKeychainPrompt: false,
698+
readOnly: undefined,
671699
});
672700
expect(noteMock).toHaveBeenCalledWith(
673701
expect.stringContaining("openai-codex:main"),
@@ -678,8 +706,10 @@ describe("noteAuthProfileHealth", () => {
678706
it("prints malformed API-key profile diagnostics", async () => {
679707
const agentDir = path.join(tempDir, "main-agent");
680708
writeAuthStore(agentDir);
681-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
682-
authProfileMocks.ensureAuthProfileStore.mockImplementation(
709+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
710+
(candidateDir) => candidateDir !== undefined,
711+
);
712+
authProfileMocks.loadAuthProfileStoreForRuntime.mockImplementation(
683713
(receivedAgentDir): AuthProfileStore => {
684714
if (receivedAgentDir === agentDir) {
685715
return {
@@ -724,7 +754,7 @@ describe("noteAuthProfileHealth", () => {
724754
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
725755
(agentDir) => agentDir === coderDir,
726756
);
727-
authProfileMocks.ensureAuthProfileStore.mockImplementation((agentDir) => {
757+
authProfileMocks.loadAuthProfileStoreForRuntime.mockImplementation((agentDir) => {
728758
if (agentDir === coderDir) {
729759
return expiredStore("openai-codex:coder", now + 60 * 60_000);
730760
}
@@ -783,8 +813,10 @@ describe("noteAuthProfileHealth", () => {
783813
const now = 1_700_000_000_000;
784814
vi.spyOn(Date, "now").mockReturnValue(now);
785815
const agentDir = path.join(tempDir, "main-agent");
786-
authProfileMocks.hasAnyAuthProfileStoreSource.mockReturnValue(true);
787-
authProfileMocks.ensureAuthProfileStore.mockReturnValue(
816+
authProfileMocks.hasLocalAuthProfileStoreSource.mockImplementation(
817+
(candidateDir) => candidateDir !== undefined,
818+
);
819+
authProfileMocks.loadAuthProfileStoreForRuntime.mockReturnValue(
788820
expiredStore(profileId, now - 60_000),
789821
);
790822
authProfileMocks.resolveApiKeyForProfile.mockRejectedValue(new Error(message));

0 commit comments

Comments
 (0)