Skip to content

Commit 1b05b0f

Browse files
authored
fix(account-switcher): support async utility-process connection proxy (ilysenko#1710)
* fix(account-switcher): support async utility-process connection proxy * fix(account-switcher): remove obsolete clearAuthTokenCache fallback * fix(account-switcher): remove obsolete native probe
1 parent 2fd3d3d commit 1b05b0f

4 files changed

Lines changed: 25 additions & 145 deletions

File tree

‎linux-features/account-switcher/README.md‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,6 @@ mkdir -p "$test_tmp"
8989
TMPDIR="$test_tmp" node --test linux-features/account-switcher/test.js
9090
```
9191

92-
Set `CODEX_ACCOUNT_SWITCHER_NATIVE_APP` to an extracted official application
93-
root and `CODEX_ACCOUNT_SWITCHER_ASAR_CLI` to `@electron/asar/bin/asar.mjs` to also
94-
run the native Owl regression test. This requires a graphical session and an
95-
unlocked Secret Service provider. It generates a disposable probe, uses synthetic
96-
credentials, verifies key persistence and vault decryption in fresh switcher
97-
instances, and exercises **Add account** with the official connection class's
98-
request/subscription methods. An initially disconnected hosted connection must
99-
remain untouched, and the probe rejects any attempts to query global cloud work.
100-
The probe uses a synthetic transport and intercepts the browser launch, then
101-
cancels OAuth; it does not log in to real accounts. It exits only its own process
102-
and clears only its own test key.
103-
Do not set a long build `TMPDIR` when launching a desktop app: Chromium's socket
104-
path must fit the Unix-domain socket limit. Use `TMPDIR="$XDG_RUNTIME_DIR"` for
105-
interactive launches instead.
106-
10792
Set `CODEX_ACCOUNT_SWITCHER_CLI` to the extracted official `resources/codex` to
10893
also check credential-file decoding and the idle-check protocol in a disposable,
10994
unauthenticated app server. This does not test OAuth with real accounts.

‎linux-features/account-switcher/native-probe.js‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎linux-features/account-switcher/runtime.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
214214
async function assertIdle() {
215215
stage = "checking backend connections";
216216
for (const client of clients()) {
217-
if (client.getPendingRequestCount() !== 0) throw new SwitcherError("Wait for pending requests and active tasks to finish before switching accounts.");
217+
if (client.hostConfig.id === "durable" && client.getConnectionState() === "disconnected") continue;
218+
if (await client.getPendingRequestCount() !== 0) throw new SwitcherError("Wait for pending requests and active tasks to finish before switching accounts.");
218219
// Durable tasks run independently of this desktop connection. An
219220
// account-wide catalog includes work in other apps/profiles, which a
220221
// local credential change neither restarts nor cancels. Only in-flight
@@ -261,7 +262,6 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
261262
stage = "reconnecting the account";
262263
const info = identity(text);
263264
writePrivate(authPath, text);
264-
client.clearAuthTokenCache();
265265
await client.restart({ intent: "reconnect" });
266266
await rpc(client, "account/read", { refreshToken: false });
267267
if (!await principalMatches(client, info, PRINCIPAL_SETTLE_ATTEMPTS)) {
@@ -295,7 +295,7 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
295295
if (message.method !== "account/login/completed") return;
296296
if (loginId == null) { early = message.params; return; }
297297
if (message.params?.loginId === loginId) resolveLogin(message.params);
298-
});
298+
}, { methods: ["account/login/completed"] });
299299
const controller = new AbortController();
300300
try {
301301
const login = await rpc(client, "account/login/start", { type: "chatgpt", appBrand: "chatgpt", useHostedLoginSuccessPage: true });
@@ -311,7 +311,6 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
311311
dialog.showMessageBox({ type: "info", title: "ChatGPT Community", message: "Sign in to another ChatGPT account in your browser.", detail: "Your current login is remembered. Cancel returns to it.", buttons: ["Cancel"], cancelId: 0, signal: controller.signal }).then(() => null),
312312
]);
313313
if (!result?.success) return;
314-
client.clearAuthTokenCache();
315314
remember(accounts, await readCurrent(client, PRINCIPAL_SETTLE_ATTEMPTS));
316315
completed = true;
317316
} catch (error) {

‎linux-features/account-switcher/test.js‎

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ function fixture(t, options = {}) {
6060
const client = {
6161
hostConfig: { id: "local", kind: "local" },
6262
ensureReady: async () => { ready = true; calls.push("ensureReady"); },
63-
getPendingRequestCount: () => options.pending ?? 0,
63+
getConnectionState: () => ready ? "connected" : "disconnected",
64+
getPendingRequestCount: async () => options.pending ?? 0,
6465
getAuthenticatedPrincipal: async () => ({ accountId: `account-${active}`, userId: `user-${active}` }),
65-
clearAuthTokenCache: () => calls.push("clearCache"),
6666
restart: async () => {
6767
calls.push("restart");
6868
const name = JSON.parse(fs.readFileSync(authPath)).tokens.account_id.slice(8);
@@ -322,7 +322,9 @@ for (const action of ["add", "switch"]) {
322322
const f = fixture(t);
323323
const reads = [];
324324
f.clients.push({
325-
hostConfig: { id: "durable", kind: "local" }, getPendingRequestCount: () => pending,
325+
hostConfig: { id: "durable", kind: "local" },
326+
getConnectionState: () => "connected",
327+
getPendingRequestCount: async () => pending,
326328
sendAppServerRequest: async method => {
327329
reads.push(method);
328330
// A catalog would contain a permanently active task from another
@@ -348,6 +350,23 @@ for (const action of ["add", "switch"]) {
348350
}
349351
}
350352

353+
test("disconnected durable host is not opened during idle checks", async t => {
354+
for (const action of ["switch", "add"]) {
355+
const f = fixture(t, action === "add" ? { login: "second" } : {});
356+
let durableOpened = false;
357+
f.clients.push({
358+
hostConfig: { id: "durable", kind: "local" },
359+
getConnectionState: () => "disconnected",
360+
getPendingRequestCount: async () => { durableOpened = true; return 0; },
361+
});
362+
if (action === "switch") { await f.open(3); f.seedSecond(); }
363+
await f.open(1);
364+
assert.equal(durableOpened, false, `disconnected durable host must not be opened during ${action}`);
365+
assert.equal(f.dialogs.filter(dialog => dialog.type === "error").length, 0);
366+
assert.equal(fs.readFileSync(f.authPath, "utf8"), credentials("second"));
367+
}
368+
});
369+
351370
test("login setup errors name the phase without exposing upstream error details", async t => {
352371
const f = fixture(t);
353372
f.client.registerInternalNotificationHandler = () => { throw new Error("private-error-token"); };
@@ -648,40 +667,3 @@ test("official main and profile dropdown chunks patch uniquely and remain valid
648667
}
649668
}
650669
});
651-
652-
test("official Owl reaches OAuth and reopens an encrypted vault without querying independent cloud work", {
653-
skip: !process.env.CODEX_ACCOUNT_SWITCHER_NATIVE_APP || !process.env.CODEX_ACCOUNT_SWITCHER_ASAR_CLI,
654-
timeout: 30_000,
655-
}, t => {
656-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "account-native-"));
657-
const app = process.env.CODEX_ACCOUNT_SWITCHER_NATIVE_APP;
658-
const home = path.join(dir, "home"), profile = path.join(dir, "profile");
659-
const probe = path.join(dir, "app"), source = path.join(dir, "source");
660-
fs.mkdirSync(path.join(probe, "resources"), { recursive: true });
661-
fs.mkdirSync(source);
662-
t.after(() => {
663-
if (fs.existsSync(home)) {
664-
const id = crypto.createHash("sha256").update(fs.realpathSync(home)).digest("hex");
665-
try { execFileSync("secret-tool", ["clear", "application", "codex-desktop", "feature", "account-switcher", "profile", id], { stdio: "ignore", timeout: 5_000 }); } catch {}
666-
}
667-
fs.rmSync(dir, { recursive: true, force: true });
668-
});
669-
fs.copyFileSync(path.join(app, "ChatGPT"), path.join(probe, "ChatGPT"));
670-
fs.chmodSync(path.join(probe, "ChatGPT"), 0o755);
671-
for (const name of fs.readdirSync(app)) {
672-
if (["ChatGPT", "resources", "start.sh", ".codex-linux"].includes(name)) continue;
673-
fs.symlinkSync(path.join(app, name), path.join(probe, name));
674-
}
675-
for (const name of ["owl-app.ini", "owl-electron-app.json"]) fs.copyFileSync(path.join(app, "resources", name), path.join(probe, "resources", name));
676-
fs.copyFileSync(path.join(__dirname, "native-probe.js"), path.join(source, "probe.js"));
677-
fs.writeFileSync(path.join(source, "package.json"), JSON.stringify({ name: "account-native-probe", version: "1.0.0", main: "probe.js" }));
678-
execFileSync(process.execPath, [process.env.CODEX_ACCOUNT_SWITCHER_ASAR_CLI, "pack", source, path.join(probe, "resources/app.asar")], { stdio: "pipe", timeout: 5_000 });
679-
const result = path.join(dir, "result.json");
680-
execFileSync(path.join(probe, "ChatGPT"), [`--user-data-dir=${profile}`], {
681-
env: { ...process.env, TMPDIR: process.env.XDG_RUNTIME_DIR || "/tmp", CODEX_HOME: home, CODEX_ELECTRON_USER_DATA_PATH: profile,
682-
ACCOUNT_SWITCHER_PROBE_RUNTIME: process.env.CODEX_ACCOUNT_SWITCHER_PROBE_RUNTIME || path.join(__dirname, "runtime.js"), ACCOUNT_SWITCHER_PROBE_RESULT: result,
683-
ACCOUNT_SWITCHER_PROBE_NATIVE_APP: app },
684-
stdio: "pipe", timeout: 20_000,
685-
});
686-
assert.deepEqual(JSON.parse(fs.readFileSync(result)), { menus: 3, encryptedVault: true, errors: [], openedLogin: 1, readyChecks: 4, cloudUntouched: true });
687-
});

0 commit comments

Comments
 (0)