Skip to content

Commit 6d33511

Browse files
authored
fix(tui): reload auth store ownership after login (#130349)
* fix(tui): reload auth store ownership after login * fix(tui): refresh auth runtime after login
1 parent 7781b90 commit 6d33511

4 files changed

Lines changed: 55 additions & 4 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: 19 additions & 4 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";
@@ -59,6 +61,7 @@ type MockModelBehavior = {
5961
type MockModelRequest = {
6062
method: string;
6163
path: string;
64+
authorization?: string;
6265
body: Record<string, unknown>;
6366
};
6467

@@ -346,7 +349,12 @@ async function startRoutedMockModelServer(
346349
const body = await readJsonRequest(req);
347350
if (url.pathname === "/v1/responses" || url.pathname === "/responses") {
348351
const modelId = typeof body.model === "string" ? body.model : "";
349-
const request = { method: req.method, path: url.pathname, body };
352+
const request = {
353+
method: req.method,
354+
path: url.pathname,
355+
authorization: req.headers.authorization,
356+
body,
357+
};
350358
const behavior = behaviors[modelId];
351359
if (!behavior) {
352360
rejectedRequests.push(request);
@@ -1469,7 +1477,7 @@ describe("TUI PTY real backends", () => {
14691477
"authenticates a manifest-discovered provider and resumes the unchanged local model",
14701478
async ({ onTestFinished }) => {
14711479
const pluginId = "t05-local-auth-fixture";
1472-
const providerId = "t05-local-auth-provider";
1480+
const providerId = "tui-pty-mock";
14731481
const profileId = `${providerId}:default`;
14741482
const sentinel = `t05-${randomUUID()}`;
14751483
const expectedDigest = createHash("sha256").update(sentinel).digest("hex");
@@ -1587,7 +1595,13 @@ export default {
15871595
const agentDir = path.join(fixture.stateDir, "agents", "main", "agent");
15881596
const sqlitePath = path.join(agentDir, "openclaw-agent.sqlite");
15891597
expect(await stat(sqlitePath).then((entry) => entry.isFile())).toBe(true);
1590-
const store = loadPersistedAuthProfileStore(agentDir);
1598+
const store = withEnv({ OPENCLAW_STATE_DIR: fixture.stateDir }, () => {
1599+
reloadSharedAuthStoreOwnership();
1600+
return loadAuthProfileStoreForRuntime(agentDir, {
1601+
readOnly: true,
1602+
syncExternalCli: false,
1603+
});
1604+
});
15911605
const profile = store?.profiles[profileId];
15921606
expect(profile?.type === "api_key").toBe(true);
15931607
expect(profile?.provider === providerId).toBe(true);
@@ -1608,6 +1622,7 @@ export default {
16081622
onTimeout: () => new Error("post-auth prompt did not reach the mock provider"),
16091623
});
16101624
expect(fixture.mockModel.requests()[0]?.body.model).toBe("gpt-5.5");
1625+
expect(fixture.mockModel.requests()[0]?.authorization).toBe(`Bearer ${sentinel}`);
16111626
await fixture.run.waitForOutput("LOCAL_AUTH_RESPONSE", LOCAL_OUTPUT_TIMEOUT_MS);
16121627

16131628
await fixture.run.write("/exit\r", { delay: false });

‎src/tui/tui.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
resolveSessionAgentId,
1919
tryResolveDefaultAgentId,
2020
} from "../agents/agent-scope.js";
21+
import { reloadSharedAuthStoreOwnership } from "../agents/auth-profiles/path-resolve.js";
22+
import { clearRuntimeAuthProfileStoreSnapshots } from "../agents/auth-profiles/runtime-snapshots.js";
2123
import { normalizeThinkLevel } from "../auto-reply/thinking.shared.js";
2224
import { formatCliCommand } from "../cli/command-format.js";
2325
import { getRuntimeConfig, type OpenClawConfig } from "../config/config.js";
@@ -1404,6 +1406,12 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
14041406
const outcome = `argv=${commandArgv} exitCode=${String(result.exitCode)} signal=${String(result.signal)}`;
14051407
if (result.exitCode === 0 && !result.signal) {
14061408
tuiAuthLog.info(`auth child finished: ${outcome}`);
1409+
if (!codexBin) {
1410+
reloadSharedAuthStoreOwnership();
1411+
// The auth child persisted outside this process. Invalidate the
1412+
// published generation so retained local runtimes rebuild from it.
1413+
clearRuntimeAuthProfileStoreSnapshots();
1414+
}
14071415
} else {
14081416
tuiAuthLog.error(`auth child failed: ${outcome}`);
14091417
}

0 commit comments

Comments
 (0)