Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion linux-features/account-switcher/native-probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ electron.app.whenReady().then(async () => {
Object.assign(client, {
options: { hostId: id, hostConfig: { id, kind: "local" }, transport: { kind: id === "local" ? "stdio" : "websocket" } },
connection: id === "local" ? {} : null,
connectionState: id === "local" ? "connected" : "disconnected",
internalNotificationHandlers: new Set(), internalResponseHandlers: new Map(),
clientRequestQueue: { size: 0, updatePeakPendingRequestCount() {} },
backendAuth: { beginAccountChange() {}, finishAccountChange() {} },
configuration: { handleSuccessfulRequest() {} },
getAuthenticatedPrincipal: async () => ({ accountId: "native-probe", userId: "native-probe" }),
ensureReady: async () => { readyChecks++; client.connection = {}; },
clearAuthTokenCache() {}, restart: async () => {},
restart: async () => {},
messageDelivery: { sendMessage: request => {
if (!client.connection) throw Error("Codex app-server is not available");
// Global cloud work must not be read, cancelled or reconnected by a
Expand Down
7 changes: 3 additions & 4 deletions linux-features/account-switcher/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
async function assertIdle() {
stage = "checking backend connections";
for (const client of clients()) {
if (client.getPendingRequestCount() !== 0) throw new SwitcherError("Wait for pending requests and active tasks to finish before switching accounts.");
if (client.hostConfig.id === "durable" && client.getConnectionState() === "disconnected") continue;
if (await client.getPendingRequestCount() !== 0) throw new SwitcherError("Wait for pending requests and active tasks to finish before switching accounts.");
// Durable tasks run independently of this desktop connection. An
// account-wide catalog includes work in other apps/profiles, which a
// local credential change neither restarts nor cancels. Only in-flight
Expand Down Expand Up @@ -261,7 +262,6 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
stage = "reconnecting the account";
const info = identity(text);
writePrivate(authPath, text);
client.clearAuthTokenCache();
await client.restart({ intent: "reconnect" });
await rpc(client, "account/read", { refreshToken: false });
if (!await principalMatches(client, info, PRINCIPAL_SETTLE_ATTEMPTS)) {
Expand Down Expand Up @@ -295,7 +295,7 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
if (message.method !== "account/login/completed") return;
if (loginId == null) { early = message.params; return; }
if (message.params?.loginId === loginId) resolveLogin(message.params);
});
}, { methods: ["account/login/completed"] });
const controller = new AbortController();
try {
const login = await rpc(client, "account/login/start", { type: "chatgpt", appBrand: "chatgpt", useHostedLoginSuccessPage: true });
Expand All @@ -311,7 +311,6 @@ function createAccountSwitcher({ electron, clients, home, runSecretTool }) {
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),
]);
if (!result?.success) return;
client.clearAuthTokenCache();
remember(accounts, await readCurrent(client, PRINCIPAL_SETTLE_ATTEMPTS));
completed = true;
} catch (error) {
Expand Down
25 changes: 22 additions & 3 deletions linux-features/account-switcher/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function fixture(t, options = {}) {
const client = {
hostConfig: { id: "local", kind: "local" },
ensureReady: async () => { ready = true; calls.push("ensureReady"); },
getPendingRequestCount: () => options.pending ?? 0,
getConnectionState: () => ready ? "connected" : "disconnected",
getPendingRequestCount: async () => options.pending ?? 0,
getAuthenticatedPrincipal: async () => ({ accountId: `account-${active}`, userId: `user-${active}` }),
clearAuthTokenCache: () => calls.push("clearCache"),
restart: async () => {
calls.push("restart");
const name = JSON.parse(fs.readFileSync(authPath)).tokens.account_id.slice(8);
Expand Down Expand Up @@ -322,7 +322,9 @@ for (const action of ["add", "switch"]) {
const f = fixture(t);
const reads = [];
f.clients.push({
hostConfig: { id: "durable", kind: "local" }, getPendingRequestCount: () => pending,
hostConfig: { id: "durable", kind: "local" },
getConnectionState: () => "connected",
getPendingRequestCount: async () => pending,
sendAppServerRequest: async method => {
reads.push(method);
// A catalog would contain a permanently active task from another
Expand All @@ -348,6 +350,23 @@ for (const action of ["add", "switch"]) {
}
}

test("disconnected durable host is not opened during idle checks", async t => {
for (const action of ["switch", "add"]) {
const f = fixture(t, action === "add" ? { login: "second" } : {});
let durableOpened = false;
f.clients.push({
hostConfig: { id: "durable", kind: "local" },
getConnectionState: () => "disconnected",
getPendingRequestCount: async () => { durableOpened = true; return 0; },
});
if (action === "switch") { await f.open(3); f.seedSecond(); }
await f.open(1);
assert.equal(durableOpened, false, `disconnected durable host must not be opened during ${action}`);
assert.equal(f.dialogs.filter(dialog => dialog.type === "error").length, 0);
assert.equal(fs.readFileSync(f.authPath, "utf8"), credentials("second"));
}
});

test("login setup errors name the phase without exposing upstream error details", async t => {
const f = fixture(t);
f.client.registerInternalNotificationHandler = () => { throw new Error("private-error-token"); };
Expand Down