Skip to content

Commit f1e891f

Browse files
committed
fix(tui): reload auth store ownership after login
1 parent c6506e3 commit f1e891f

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎src/agents/auth-profiles/path-resolve.shared-store.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ describe("shared auth store path resolution", () => {
7272
});
7373
});
7474

75+
it("reloads ownership after an explicit out-of-process auth mutation", async () => {
76+
const env = makeStateEnv();
77+
const {
78+
reloadSharedAuthStoreOwnership,
79+
resolveSharedAuthStoreOwnership,
80+
resolveSharedAuthStorePath,
81+
} = await import("./path-resolve.js");
82+
83+
expect(resolveSharedAuthStoreOwnership(env)).toEqual({ location: "legacy-main" });
84+
writeConfigMachineState("auth.sharedStore", { location: "state-db" }, { env });
85+
expect(resolveSharedAuthStoreOwnership(env)).toEqual({ location: "legacy-main" });
86+
87+
expect(reloadSharedAuthStoreOwnership(env)).toEqual({ location: "state-db" });
88+
expect(resolveSharedAuthStorePath(env)).toBe(resolveOpenClawStateSqlitePath(env));
89+
});
90+
7591
it("resolves the relocated store to the canonical shared state database", async () => {
7692
const env = makeStateEnv();
7793
writeConfigMachineState("auth.sharedStore", { location: "state-db" }, { env });

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ export function noteCommittedSharedAuthStoreOwnership(
8787
sharedAuthStoreOwnershipByDatabasePath.set(databasePath, ownership);
8888
}
8989

90+
/** Reload shared auth ownership after an explicit out-of-process auth mutation. */
91+
export function reloadSharedAuthStoreOwnership(
92+
env: NodeJS.ProcessEnv = process.env,
93+
): SharedAuthStoreOwnership {
94+
const databasePath = path.resolve(resolveOpenClawStateSqlitePath(env));
95+
const ownership = parseSharedAuthStoreOwnership(
96+
readConfigMachineState<unknown>(SHARED_AUTH_STORE_STATE_KEY, { env, path: databasePath }),
97+
);
98+
sharedAuthStoreOwnershipByDatabasePath.set(databasePath, ownership);
99+
return ownership;
100+
}
101+
90102
/** Resolve the canonical shared auth database path. */
91103
export function resolveSharedAuthStorePath(env: NodeJS.ProcessEnv = process.env): string {
92104
if (resolveSharedAuthStoreOwnership(env).location === "state-db") {

‎src/tui/tui-pty-local.e2e.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
} from "../../test/helpers/openclaw-test-instance.js";
1414
import { isProcessAlive, waitForPidFile } from "../../test/helpers/process-wait.js";
1515
import { createDeferred } from "../../test/helpers/promise.js";
16-
import { loadPersistedAuthProfileStore } from "../agents/auth-profiles/persisted.js";
16+
import { reloadSharedAuthStoreOwnership } from "../agents/auth-profiles/path-resolve.js";
17+
import { loadAuthProfileStoreForRuntime } from "../agents/auth-profiles/store.js";
1718
import { resolveAgentModelPrimaryValue } from "../config/model-input.js";
1819
import type { ModelProviderConfig } from "../config/types.models.js";
1920
import type { OpenClawConfig } from "../config/types.openclaw.js";
2021
import { connectGatewayClient } from "../gateway/test-helpers.e2e.js";
2122
import { runExec } from "../process/exec.js";
23+
import { withEnv } from "../test-utils/env.js";
2224
import { killPidIfAlive } from "../test-utils/process-tree.js";
2325
import { sleep } from "../utils/sleep.js";
2426
import { GatewayChatClient } from "./gateway-chat.js";
@@ -1587,7 +1589,13 @@ export default {
15871589
const agentDir = path.join(fixture.stateDir, "agents", "main", "agent");
15881590
const sqlitePath = path.join(agentDir, "openclaw-agent.sqlite");
15891591
expect(await stat(sqlitePath).then((entry) => entry.isFile())).toBe(true);
1590-
const store = loadPersistedAuthProfileStore(agentDir);
1592+
const store = withEnv({ OPENCLAW_STATE_DIR: fixture.stateDir }, () => {
1593+
reloadSharedAuthStoreOwnership();
1594+
return loadAuthProfileStoreForRuntime(agentDir, {
1595+
readOnly: true,
1596+
syncExternalCli: false,
1597+
});
1598+
});
15911599
const profile = store?.profiles[profileId];
15921600
expect(profile?.type === "api_key").toBe(true);
15931601
expect(profile?.provider === providerId).toBe(true);

‎src/tui/tui.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
resolveSessionAgentId,
1919
tryResolveDefaultAgentId,
2020
} from "../agents/agent-scope.js";
21+
import { reloadSharedAuthStoreOwnership } from "../agents/auth-profiles/path-resolve.js";
2122
import { normalizeThinkLevel } from "../auto-reply/thinking.shared.js";
2223
import { formatCliCommand } from "../cli/command-format.js";
2324
import { getRuntimeConfig, type OpenClawConfig } from "../config/config.js";
@@ -1404,6 +1405,7 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
14041405
const outcome = `argv=${commandArgv} exitCode=${String(result.exitCode)} signal=${String(result.signal)}`;
14051406
if (result.exitCode === 0 && !result.signal) {
14061407
tuiAuthLog.info(`auth child finished: ${outcome}`);
1408+
reloadSharedAuthStoreOwnership();
14071409
} else {
14081410
tuiAuthLog.error(`auth child failed: ${outcome}`);
14091411
}

0 commit comments

Comments
 (0)