Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(app): an automatic sign-out no longer forces the built-in server down under live runs

ogsamrate52467d068d0
diff --git a/apps/app/src/app/lib/den-session-events.ts b/apps/app/src/app/lib/den-session-events.ts
index a0039893..d0b4c658 100644
--- a/apps/app/src/app/lib/den-session-events.ts
+++ b/apps/app/src/app/lib/den-session-events.ts
@@ -10,6 +10,13 @@ export type DenSessionUpdatedDetail = {
user?: DenUser | null;
email?: string | null;
message?: string | null;
+ /**
+ * Set only when a person explicitly signed out or changed the control
+ * plane. An automatic sign-out (token expiry, revoked session) leaves it
+ * unset, so consumers must not force disruptive work such as restarting
+ * the built-in server while runs are live.
+ */
+ userInitiated?: boolean;
};
export function dispatchDenSessionUpdated(detail: DenSessionUpdatedDetail) {
diff --git a/apps/app/src/app/lib/den.ts b/apps/app/src/app/lib/den.ts
index 1ab4c962..25f588b5 100644
--- a/apps/app/src/app/lib/den.ts
+++ b/apps/app/src/app/lib/den.ts
@@ -1657,7 +1657,12 @@ export function writeDenSettings(
});
}
-export function clearDenSession(options?: { includeBaseUrls?: boolean }) {
+/**
+ * Pass `userInitiated: true` only from an explicit user action (sign-out
+ * button, control-plane change). Automatic sign-outs (expired or revoked
+ * token) omit it so listeners defer disruptive restarts until runs are idle.
+ */
+export function clearDenSession(options?: { includeBaseUrls?: boolean; userInitiated?: boolean }) {
if (typeof window === "undefined") {
return;
}
@@ -1695,6 +1700,7 @@ export function clearDenSession(options?: { includeBaseUrls?: boolean }) {
dispatchDenSessionUpdated({
status: "signed_out",
baseUrl: readDenSettings().baseUrl,
+ ...(options?.userInitiated === true ? { userInitiated: true } : {}),
});
}
diff --git a/apps/app/src/react-app/domains/cloud/forced-signin-page.tsx b/apps/app/src/react-app/domains/cloud/forced-signin-page.tsx
index 130ab541..08955c56 100644
--- a/apps/app/src/react-app/domains/cloud/forced-signin-page.tsx
+++ b/apps/app/src/react-app/domains/cloud/forced-signin-page.tsx
@@ -218,7 +218,7 @@ export function ForcedSigninPage({ developerMode }: ForcedSigninPageProps) {
setBaseUrlError(null);
setBaseUrl(persisted.baseUrl);
setBaseUrlDraft(persisted.baseUrl);
- clearDenSession({ includeBaseUrls: false });
+ clearDenSession({ includeBaseUrls: false, userInitiated: true });
setAuthError(null);
setStatusMessage(t("den.status_base_url_updated"));
void desktopConfig.refresh();
diff --git a/apps/app/src/react-app/domains/cloud/join-organization-dialog.tsx b/apps/app/src/react-app/domains/cloud/join-organization-dialog.tsx
index 2e708ecf..b7ff8a4d 100644
--- a/apps/app/src/react-app/domains/cloud/join-organization-dialog.tsx
+++ b/apps/app/src/react-app/domains/cloud/join-organization-dialog.tsx
@@ -186,7 +186,7 @@ export function JoinOrganizationDialog({
setBusy(false);
return;
}
- clearDenSession({ includeBaseUrls: false });
+ clearDenSession({ includeBaseUrls: false, userInitiated: true });
platform.openLink(invite.url);
setStatus({ phase: "invite-opened", host: invite.host });
setBusy(false);
@@ -201,7 +201,7 @@ export function JoinOrganizationDialog({
setError(t("join_org.error_invalid"));
return true;
}
- clearDenSession({ includeBaseUrls: false });
+ clearDenSession({ includeBaseUrls: false, userInitiated: true });
setStatus({ phase: "server-saved", host: hostFromUrl(persisted.baseUrl) });
return true;
}, []);
diff --git a/apps/app/src/react-app/domains/cloud/omnirush-web-access-gate.tsx b/apps/app/src/react-app/domains/cloud/omnirush-web-access-gate.tsx
index fa1181fb..8cda5f1e 100644
--- a/apps/app/src/react-app/domains/cloud/omnirush-web-access-gate.tsx
+++ b/apps/app/src/react-app/domains/cloud/omnirush-web-access-gate.tsx
@@ -201,7 +201,7 @@ export function OmniRushWebAccessGate({ children }: { children: ReactNode }) {
if (authToken) {
void createDenClient({ baseUrl: settings.baseUrl, token: authToken }).signOut().catch(() => undefined);
}
- clearDenSession();
+ clearDenSession({ userInitiated: true });
void denAuth.refresh();
};
diff --git a/apps/app/src/react-app/domains/connections/provider-auth/store.ts b/apps/app/src/react-app/domains/connections/provider-auth/store.ts
index 4fd50344..86157fd2 100644
--- a/apps/app/src/react-app/domains/connections/provider-auth/store.ts
+++ b/apps/app/src/react-app/domains/connections/provider-auth/store.ts
@@ -2378,11 +2378,14 @@ export function createProviderAuthStore(options: CreateProviderAuthStoreOptions)
// but a running OpenCode child retains its spawn environment.
// Explicit desktop sign-out must replace that process so an
// account-scoped provider cannot remain connected in the UI.
+ // An automatic sign-out (token expiry, revoked session) is not
+ // user-initiated: the restart guard then defers until no run
+ // is live instead of ending every running session.
if (isDesktopRuntime()) {
await engineRestart({
reason: "account_signed_out",
source: "provider-auth",
- userInitiated: true,
+ ...(detail.userInitiated === true ? { userInitiated: true } : {}),
}).catch(() => undefined);
}
})();
diff --git a/apps/app/src/react-app/domains/session/sidebar/account-status-menu.tsx b/apps/app/src/react-app/domains/session/sidebar/account-status-menu.tsx
index 8407e69a..dc4a54f2 100644
--- a/apps/app/src/react-app/domains/session/sidebar/account-status-menu.tsx
+++ b/apps/app/src/react-app/domains/session/sidebar/account-status-menu.tsx
@@ -331,7 +331,7 @@ export function AccountStatusMenu(props: AccountStatusMenuProps) {
.signOut()
.catch(() => undefined);
}
- clearDenSession();
+ clearDenSession({ userInitiated: true });
void denAuth.refresh();
};
diff --git a/apps/app/src/react-app/domains/settings/cloud/use-den-session.tsx b/apps/app/src/react-app/domains/settings/cloud/use-den-session.tsx
index e610e5fa..74a21f5e 100644
--- a/apps/app/src/react-app/domains/settings/cloud/use-den-session.tsx
+++ b/apps/app/src/react-app/domains/settings/cloud/use-den-session.tsx
@@ -170,12 +170,12 @@ export function useDenSession({
const clearSignedInState = React.useCallback(
(
message?: string | null,
- options?: { includeBaseUrls?: boolean },
+ options?: { includeBaseUrls?: boolean; userInitiated?: boolean },
) => {
const includeBaseUrls = options?.includeBaseUrls ?? !developerMode;
const previousSettings = readDenSettings();
return runBeforeSignedOut(onBeforeSignedOut, previousSettings).then(() => {
- clearDenSession({ includeBaseUrls });
+ clearDenSession({ includeBaseUrls, userInitiated: options?.userInitiated === true });
if (includeBaseUrls) {
setBaseUrl(DEFAULT_DEN_BASE_URL);
setBaseUrlDraft(DEFAULT_DEN_BASE_URL);
@@ -273,7 +273,7 @@ export function useDenSession({
setBaseUrl(persisted.baseUrl);
setBaseUrlDraft(persisted.baseUrl);
- await clearSignedInState(t("den.status_base_url_updated"), { includeBaseUrls: false });
+ await clearSignedInState(t("den.status_base_url_updated"), { includeBaseUrls: false, userInitiated: true });
} catch (error) {
setBaseUrlError(error instanceof Error ? error.message : t("den.error_base_url"));
} finally {
@@ -294,7 +294,7 @@ export function useDenSession({
setBaseUrlError(null);
setBaseUrl(persisted.baseUrl);
setBaseUrlDraft(persisted.baseUrl);
- await clearSignedInState(t("den.status_base_url_updated"), { includeBaseUrls: false });
+ await clearSignedInState(t("den.status_base_url_updated"), { includeBaseUrls: false, userInitiated: true });
} catch (error) {
setBaseUrlError(error instanceof Error ? error.message : t("den.error_base_url"));
} finally {
@@ -326,7 +326,7 @@ export function useDenSession({
);
setBaseUrl(resolved.baseUrl);
setBaseUrlDraft(resolved.baseUrl);
- await clearSignedInState(t("den.status_server_config_cleared"), { includeBaseUrls: false });
+ await clearSignedInState(t("den.status_server_config_cleared"), { includeBaseUrls: false, userInitiated: true });
} catch (error) {
setBaseUrlError(error instanceof Error ? error.message : t("den.error_base_url"));
} finally {
@@ -538,7 +538,7 @@ export function useDenSession({
if (authToken.trim()) {
await client.signOut();
}
- await clearSignedInState(t("den.status_signed_out"));
+ await clearSignedInState(t("den.status_signed_out"), { userInitiated: true });
} catch (error) {
setAuthError(
error instanceof DenApiError
diff --git a/apps/app/src/react-app/shell/cloud-workspace-overlay.tsx b/apps/app/src/react-app/shell/cloud-workspace-overlay.tsx
index 4609bbc8..3f1e37d9 100644
--- a/apps/app/src/react-app/shell/cloud-workspace-overlay.tsx
+++ b/apps/app/src/react-app/shell/cloud-workspace-overlay.tsx
@@ -224,7 +224,7 @@ export function CloudWorkspaceStatusProvider(props: { children: ReactNode }) {
if (authToken) {
void denClient.signOut().catch(() => undefined);
}
- clearDenSession();
+ clearDenSession({ userInitiated: true });
void denAuth.refresh();
}, [authToken, denAuth, denClient]);
diff --git a/apps/app/tests/den-session-offline-resilience.test.ts b/apps/app/tests/den-session-offline-resilience.test.ts
index b886b6f3..ac847a74 100644
--- a/apps/app/tests/den-session-offline-resilience.test.ts
+++ b/apps/app/tests/den-session-offline-resilience.test.ts
@@ -380,4 +380,28 @@ describe("explicit sign-out", () => {
) as CustomEvent<{ status?: string }> | undefined;
expect(sessionUpdated?.detail.status).toBe("signed_out");
});
+
+ test("marks the signed_out notification user-initiated only when the caller says so", () => {
+ const dispatched: CustomEvent<{ status?: string; userInitiated?: boolean }>[] = [];
+ Object.defineProperty(globalThis, "window", {
+ configurable: true,
+ value: {
+ localStorage: memoryStorage(),
+ dispatchEvent: (event: Event) => {
+ if (event.type === "omnirush-den-session-updated") {
+ dispatched.push(event as CustomEvent<{ status?: string; userInitiated?: boolean }>);
+ }
+ return true;
+ },
+ },
+ });
+
+ // The automatic expiry path (den-auth-provider) calls it bare.
+ clearDenSession();
+ clearDenSession({ includeBaseUrls: false });
+ clearDenSession({ userInitiated: true });
+
+ expect(dispatched.map((event) => event.detail.status)).toEqual(["signed_out", "signed_out", "signed_out"]);
+ expect(dispatched.map((event) => event.detail.userInitiated)).toEqual([undefined, undefined, true]);
+ });
});
diff --git a/apps/app/tests/join-organization-input.test.ts b/apps/app/tests/join-organization-input.test.ts
index 61795f88..f3f7c4da 100644
--- a/apps/app/tests/join-organization-input.test.ts
+++ b/apps/app/tests/join-organization-input.test.ts
@@ -73,7 +73,7 @@ describe("welcome one-field contract", () => {
expect(source).toContain("platform.openLink(invite.url)");
expect(source).toContain('setStatus({ phase: "invite-opened", host: invite.host });');
expect(source).toContain('setStatus({ phase: "server-saved", host: hostFromUrl(persisted.baseUrl) });');
- expect(source).toContain('clearDenSession({ includeBaseUrls: false });');
+ expect(source).toContain('clearDenSession({ includeBaseUrls: false, userInitiated: true });');
});
test("the welcome page has one join door and no separate server-URL affordance", () => {
diff --git a/apps/app/tests/session-provider-auth-server-sync.test.ts b/apps/app/tests/session-provider-auth-server-sync.test.ts
index de4eb169..005b619e 100644
--- a/apps/app/tests/session-provider-auth-server-sync.test.ts
+++ b/apps/app/tests/session-provider-auth-server-sync.test.ts
@@ -491,4 +491,52 @@ describe("session-route cloud provider sync wiring", () => {
// The legacy renderer-side reconciliation still runs for remote workspaces.
expect(requests.some((request) => request.url === "https://den.example/api/den/v1/llm-providers")).toBe(true);
});
+
+ // An automatic sign-out (token expiry or a revoked session found by the
+ // background getSession refresh) must not force the built-in server down
+ // under live runs: only an explicit sign-out passes userInitiated, which
+ // bypasses the desktop restart guard's busy-session deferral.
+ async function signOutEngineRestarts(clear: () => void) {
+ const storage = installWindow();
+ installCloudSession(storage);
+ const desktopCalls: Array<{ command: string; args: unknown[] }> = [];
+ (window as unknown as { __OMNIRUSH_ELECTRON__: unknown }).__OMNIRUSH_ELECTRON__ = {
+ invokeDesktop: async (command: string, ...args: unknown[]) => {
+ desktopCalls.push({ command, args });
+ return undefined;
+ },
+ };
+ const requests: RecordedRequest[] = [];
+ installFetchMock(requests);
+ const store = createSessionRouteStore({
+ endpoint: makeEndpoint({ origin: LOCAL_SERVER_ORIGIN, isRemote: false }),
+ hostToken: "host-token-live",
+ });
+
+ store.start();
+ clear();
+ const restarts = () => desktopCalls.filter((call) => call.command === "engineRestart");
+ for (let attempt = 0; attempt < 50 && restarts().length === 0; attempt += 1) {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ }
+ store.dispose();
+ expect(
+ requests.filter((request) => request.method === "DELETE" && new URL(request.url).pathname === "/den-session"),
+ ).toHaveLength(1);
+ return restarts().map((call) => call.args[0] as Record<string, unknown>);
+ }
+
+ test("an automatic sign-out restarts the engine without userInitiated so live runs defer it", async () => {
+ const restarts = await signOutEngineRestarts(() => clearDenSession());
+
+ expect(restarts).toHaveLength(1);
+ expect(restarts[0]).toMatchObject({ reason: "account_signed_out", source: "provider-auth" });
+ expect(restarts[0]?.userInitiated).toBeUndefined();
+ });
+
+ test("an explicit sign-out restarts the engine as user-initiated", async () => {
+ const restarts = await signOutEngineRestarts(() => clearDenSession({ userInitiated: true }));
+
+ expect(restarts).toEqual([{ reason: "account_signed_out", source: "provider-auth", userInitiated: true }]);
+ });
});