




















@@ -104,6 +104,10 @@ describe("ensureAuthProfileStore", () => {
104104restoreEnvValue("PI_CODING_AGENT_DIR", params.previousPiAgentDir);
105105}
106106107+function resolveDefaultMainAgentDir(stateDir: string): string {
108+return path.join(stateDir, "agents", "main", "agent");
109+}
110+107111function expectApiKeyProfile(
108112profile: AuthProfileCredential,
109113): Extract<AuthProfileCredential, { type: "api_key" }> {
@@ -167,17 +171,14 @@ describe("ensureAuthProfileStore", () => {
167171168172it("merges main auth profiles into agent store and keeps agent overrides", () => {
169173const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-merge-"));
170-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
171-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
174+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
172175try {
173-const mainDir = path.join(root, "main-agent");
176+process.env.OPENCLAW_STATE_DIR = root;
177+const mainDir = resolveDefaultMainAgentDir(root);
174178const agentDir = path.join(root, "agent-x");
175179fs.mkdirSync(mainDir, { recursive: true });
176180fs.mkdirSync(agentDir, { recursive: true });
177181178-process.env.OPENCLAW_AGENT_DIR = mainDir;
179-process.env.PI_CODING_AGENT_DIR = mainDir;
180-181182const mainStore = {
182183version: AUTH_STORE_VERSION,
183184profiles: {
@@ -227,24 +228,21 @@ describe("ensureAuthProfileStore", () => {
227228key: "agent-key",
228229});
229230} finally {
230-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
231+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
231232fs.rmSync(root, { recursive: true, force: true });
232233}
233234});
234235235236it("uses the main agent's newer OAuth profile when an agent still has a stale default profile", () => {
236237const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-"));
237-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
238-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
238+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
239239try {
240-const mainDir = path.join(root, "main-agent");
240+process.env.OPENCLAW_STATE_DIR = root;
241+const mainDir = resolveDefaultMainAgentDir(root);
241242const agentDir = path.join(root, "agent-x");
242243fs.mkdirSync(mainDir, { recursive: true });
243244fs.mkdirSync(agentDir, { recursive: true });
244245245-process.env.OPENCLAW_AGENT_DIR = mainDir;
246-process.env.PI_CODING_AGENT_DIR = mainDir;
247-248246const freshProfileId = "openai-codex:user@example.com";
249247const staleProfileId = "openai-codex:default";
250248saveAuthProfileStore(
@@ -325,24 +323,21 @@ describe("ensureAuthProfileStore", () => {
325323) as { profiles: Record<string, unknown> };
326324expect(persistedAgentStore.profiles[staleProfileId]).toBeDefined();
327325} finally {
328-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
326+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
329327fs.rmSync(root, { recursive: true, force: true });
330328}
331329});
332330333331it("keeps a newer agent replacement credential while repairing stale default references", () => {
334332const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-newer-agent-"));
335-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
336-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
333+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
337334try {
338-const mainDir = path.join(root, "main-agent");
335+process.env.OPENCLAW_STATE_DIR = root;
336+const mainDir = resolveDefaultMainAgentDir(root);
339337const agentDir = path.join(root, "agent-x");
340338fs.mkdirSync(mainDir, { recursive: true });
341339fs.mkdirSync(agentDir, { recursive: true });
342340343-process.env.OPENCLAW_AGENT_DIR = mainDir;
344-process.env.PI_CODING_AGENT_DIR = mainDir;
345-346341const freshProfileId = "openai-codex:user@example.com";
347342const staleProfileId = "openai-codex:default";
348343saveAuthProfileStore(
@@ -408,24 +403,21 @@ describe("ensureAuthProfileStore", () => {
408403expect(store.order?.["openai-codex"]).toEqual([freshProfileId]);
409404expect(store.lastGood?.["openai-codex"]).toBe(freshProfileId);
410405} finally {
411-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
406+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
412407fs.rmSync(root, { recursive: true, force: true });
413408}
414409});
415410416411it("preserves a valid main default OAuth profile while replacing a stale agent override", () => {
417412const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-base-default-"));
418-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
419-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
413+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
420414try {
421-const mainDir = path.join(root, "main-agent");
415+process.env.OPENCLAW_STATE_DIR = root;
416+const mainDir = resolveDefaultMainAgentDir(root);
422417const agentDir = path.join(root, "agent-x");
423418fs.mkdirSync(mainDir, { recursive: true });
424419fs.mkdirSync(agentDir, { recursive: true });
425420426-process.env.OPENCLAW_AGENT_DIR = mainDir;
427-process.env.PI_CODING_AGENT_DIR = mainDir;
428-429421const freshProfileId = "openai-codex:user@example.com";
430422const defaultProfileId = "openai-codex:default";
431423saveAuthProfileStore(
@@ -497,24 +489,21 @@ describe("ensureAuthProfileStore", () => {
497489lastUsed: 123,
498490});
499491} finally {
500-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
492+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
501493fs.rmSync(root, { recursive: true, force: true });
502494}
503495});
504496505497it("keeps a stale default OAuth profile when the main profile belongs to a different identity", () => {
506498const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-drift-mismatch-"));
507-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
508-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
499+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
509500try {
510-const mainDir = path.join(root, "main-agent");
501+process.env.OPENCLAW_STATE_DIR = root;
502+const mainDir = resolveDefaultMainAgentDir(root);
511503const agentDir = path.join(root, "agent-x");
512504fs.mkdirSync(mainDir, { recursive: true });
513505fs.mkdirSync(agentDir, { recursive: true });
514506515-process.env.OPENCLAW_AGENT_DIR = mainDir;
516-process.env.PI_CODING_AGENT_DIR = mainDir;
517-518507const freshProfileId = "openai-codex:user@example.com";
519508const staleProfileId = "openai-codex:default";
520509saveAuthProfileStore(
@@ -568,24 +557,21 @@ describe("ensureAuthProfileStore", () => {
568557expect(store.order?.["openai-codex"]).toEqual([staleProfileId]);
569558expect(store.lastGood?.["openai-codex"]).toBe(staleProfileId);
570559} finally {
571-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
560+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
572561fs.rmSync(root, { recursive: true, force: true });
573562}
574563});
575564576565it("rewrites invalidated per-agent Codex order to the main agent's healthy relogin profile", () => {
577566const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-codex-relogin-"));
578-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
579-const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
567+const previousStateDir = process.env.OPENCLAW_STATE_DIR;
580568try {
581-const mainDir = path.join(root, "main-agent");
569+process.env.OPENCLAW_STATE_DIR = root;
570+const mainDir = resolveDefaultMainAgentDir(root);
582571const agentDir = path.join(root, "agent-x");
583572fs.mkdirSync(mainDir, { recursive: true });
584573fs.mkdirSync(agentDir, { recursive: true });
585574586-process.env.OPENCLAW_AGENT_DIR = mainDir;
587-process.env.PI_CODING_AGENT_DIR = mainDir;
588-589575const now = Date.now();
590576const healthyProfileId = "openai-codex:bunsthedev@gmail.com";
591577const staleProfileId = "openai-codex:val@viewdue.ai";
@@ -656,7 +642,7 @@ describe("ensureAuthProfileStore", () => {
656642expect(store.lastGood?.["openai-codex"]).toBe(healthyProfileId);
657643expect(store.usageStats?.[staleProfileId]).toBeUndefined();
658644} finally {
659-restoreAgentDirEnv({ previousAgentDir, previousPiAgentDir });
645+restoreEnvValue("OPENCLAW_STATE_DIR", previousStateDir);
660646fs.rmSync(root, { recursive: true, force: true });
661647}
662648});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。