Skip to content

Commit 1c30c3f

Browse files
authored
A tab signed out elsewhere stops asking, and goes to sign-in (#2816)
* A tab signed out elsewhere stops asking, and goes to sign-in A browser hides the 401 that refuses a WebSocket upgrade, so the conversation events socket only saw it close and reconnected for ever, backing off to 30 seconds. The read of a computer's wheel took OpenBot's 401 for a missed read and asked again every second or two. One person's tab, signed out since the afternoon, did both for hours and never told them (Cloud alert: conversation-events 401 at 25%). - A socket refused before it opens asks once who is signed in (getPerson, the gate's own question), then no more than every 30 seconds. Nobody: it stops, and opens again only once somebody is signed in. - The wheel's read stops on OpenBot's own 401, as it does for a Bot that is gone. A proxy's 401 is still only a missed read. - Either way the app is told (SignedOutWatch), and the signed-in gate runs again: the sign-in screen, or Desktop's and the embed's own renewal. - The socket names the app in an optional `client` query parameter, so a refused socket is logged with the app that asked. * Allow the signed-out tab test's one wait that proves nothing more is asked * Prove the signed-out tab stops over two minutes of the page's clock, not a fixed wait
1 parent 12e804d commit 1c30c3f

11 files changed

Lines changed: 580 additions & 22 deletions

‎api/accepted-live-breaking-changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Once the change is on the base branch, the check no longer reports it and its li
2424
- 2026-11-26 `add /components/schemas/ConversationWork/properties/kind/x-extensible-enum`: the same change, its open list.
2525
- 2026-11-26 `remove /components/schemas/ConversationActivity/properties/work/items/properties/kind/enum`: the same change, where the message's work is resolved.
2626
- 2026-11-26 `add /components/schemas/ConversationActivity/properties/work/items/properties/kind/x-extensible-enum`: the same change, its open list there.
27+
- 2026-12-02 `add /channels/conversationEvents/bindings/ws/query/properties/client`: the socket gains a second optional query parameter, `client`, naming the app that opens it as the OpenBot-Client header does. The server only writes it into its log when it refuses the socket; a socket opened without it is answered the same, so no client loses anything.

‎api/asyncapi.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ channels:
129129
can name the person's own turn (ConversationWork);
130130
data-changed, the socket is sent dataChanged; and
131131
artifact-activity, the socket is sent artifactActivity.
132+
client:
133+
type: string
134+
maxLength: 200
135+
description: >-
136+
The app that opens the socket, as the OpenBot-Client header
137+
names it on a request (`<kind>/<version>`, such as
138+
`web/1.2.8`), since a browser can't set a header on a
139+
WebSocket. Optional, and used only to name the app in the
140+
server's log when it refuses the socket: nothing is refused
141+
for it, and a socket opened without it is answered the same.
132142
bindingVersion: 0.1.0
133143
messages:
134144
conversationActivity:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useRouter } from "@tanstack/react-router";
2+
import { useEffect } from "react";
3+
import { onSignedOutFound } from "@/lib/auth/session-check";
4+
5+
/**
6+
* Found out, while the app is open, that nobody is signed in any more (lib/auth/session-check.ts):
7+
* the signed-in gate runs again (`_authed`), and does what it does on a load. The sign-in screen,
8+
* bringing the person back to this page; inside OpenBot Desktop, Desktop's own renewal; embedded,
9+
* the host's. Draws nothing.
10+
*/
11+
export function SignedOutWatch() {
12+
const router = useRouter();
13+
useEffect(
14+
() =>
15+
onSignedOutFound(() => {
16+
void router.invalidate();
17+
}),
18+
[router],
19+
);
20+
return null;
21+
}

‎app/src/lib/auth/session-check.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { QueryClient } from "@tanstack/react-query";
2+
import { currentUserQueryOptions } from "./queries";
3+
4+
/**
5+
* What OpenBot said when asked again who is signed in: somebody, nobody, or no answer (the network,
6+
* a 5xx, a page that is not OpenBot's), which is never read as signed out (lib/auth/signed-out.ts).
7+
*/
8+
export type SessionAnswer = "signed-in" | "signed-out" | "unknown";
9+
10+
/*
11+
* A SIGNED-OUT TAB MUST STOP ASKING. Some of what the app keeps open never sees a 401 it can read: a
12+
* browser hides the status of a WebSocket upgrade it was refused, and only tells the page the socket
13+
* closed. A tab signed out elsewhere (in another tab, by an admin, by the session running out) went
14+
* on reconnecting every 30 seconds and reading a computer's wheel every second or two, for hours,
15+
* and never told the person (Cloud alert, 2 Dec 2026: conversation-events refused 401 at 25%).
16+
*
17+
* So what was refused that way asks here, once, who is signed in, with the app's own question
18+
* (`getPerson`, the one the `_authed` gate asks). Nobody: whatever asked stops, and the app is told
19+
* (`onSignedOutFound`), which runs the gate again (`SignedOutWatch`): the sign-in screen, or inside
20+
* OpenBot Desktop and embedded, their own ways of signing in again.
21+
*/
22+
const listeners = new Set<() => void>();
23+
let asking: Promise<SessionAnswer> | undefined;
24+
25+
/**
26+
* Ask OpenBot again whether anybody is signed in, past the cached answer. One question at a time,
27+
* however many sockets and reads ask together. Nobody signed in is told to `onSignedOutFound`.
28+
*/
29+
export function askWhoIsSignedIn(
30+
queryClient: QueryClient,
31+
): Promise<SessionAnswer> {
32+
if (asking) return asking;
33+
const question = queryClient
34+
.fetchQuery({ ...currentUserQueryOptions(), staleTime: 0 })
35+
.then(
36+
(user): SessionAnswer => (user ? "signed-in" : "signed-out"),
37+
(): SessionAnswer => "unknown",
38+
)
39+
.then((answer) => {
40+
if (answer === "signed-out")
41+
for (const listener of [...listeners]) listener();
42+
return answer;
43+
})
44+
.finally(() => {
45+
if (asking === question) asking = undefined;
46+
});
47+
asking = question;
48+
return question;
49+
}
50+
51+
/** Hear that OpenBot answered nobody is signed in. Returns how to stop hearing. */
52+
export function onSignedOutFound(listener: () => void): () => void {
53+
listeners.add(listener);
54+
return () => {
55+
listeners.delete(listener);
56+
};
57+
}
58+
59+
/** Whether the cached answer to who is signed in is somebody: a sign-in again, after nobody. */
60+
export function signedInNow(queryClient: QueryClient): boolean {
61+
return Boolean(queryClient.getQueryData(currentUserQueryOptions().queryKey));
62+
}

‎app/src/lib/channels/use-channel-events.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
resyncArtifacts,
1414
} from "@/lib/artifacts/activity";
1515
import { HELD_OUT_NOW, holdOut, SSO_REQUIRED_CLOSE } from "@/lib/auth/held-out";
16-
import { CAPABILITIES } from "@/lib/client";
16+
import { askWhoIsSignedIn, signedInNow } from "@/lib/auth/session-check";
17+
import { CAPABILITIES, CLIENT_NAME } from "@/lib/client";
1718
import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries";
1819
import { embedSocketRenewal } from "@/lib/embed/session";
1920
import {
@@ -664,12 +665,23 @@ export function recoveryReadDelayMs(random: number = Math.random()): number {
664665
/**
665666
* The socket names the newer messages this app understands in its query, as every request does in
666667
* its OpenBot-Capabilities header (`lib/client.ts`): a browser can't set a header on a WebSocket.
668+
* It names this app too (`client`), as the OpenBot-Client header does, so a socket OpenBot refuses
669+
* is logged with the app that asked.
667670
*/
668671
function socketUrl() {
669-
const query = new URLSearchParams({ [CAPABILITIES_QUERY]: CAPABILITIES });
672+
const query = new URLSearchParams({
673+
[CAPABILITIES_QUERY]: CAPABILITIES,
674+
client: CLIENT_NAME,
675+
});
670676
return `${buildSocketUrl(apiSocketPath("/conversation-events"))}?${query}`;
671677
}
672678

679+
/**
680+
* How seldom a socket that goes on being refused before it opens asks again who is signed in, once
681+
* OpenBot said somebody is: as seldom as the slowest reconnect.
682+
*/
683+
const ASK_AGAIN_MS = MAX_RETRY_MS;
684+
673685
export function useChannelEvents() {
674686
const queryClient = useQueryClient();
675687
const router = useRouter();
@@ -697,13 +709,53 @@ export function useChannelEvents() {
697709
let stopped = false;
698710
// Embedded, a socket refused for want of a session renews it once and opens again at once.
699711
const renewal = embedSocketRenewal();
712+
/** When this socket last asked who is signed in, after being refused before it opened. */
713+
let askedAt: number | undefined;
714+
/** Waiting for somebody to be signed in again, after OpenBot said nobody is. */
715+
let stopWaiting: (() => void) | undefined;
700716

701717
const retryLater = () => {
702718
if (stopped) return;
703719
retryTimer = window.setTimeout(connect, reconnectDelayMs(attempts));
704720
attempts += 1;
705721
};
706722

723+
/*
724+
* Nobody signed in: not reconnected. The app runs its gate again (`SignedOutWatch`), which takes
725+
* the tab to signing in, and this screen goes with it. Should the gate keep the tab (OpenBot
726+
* Desktop renewed its sign-in), the socket opens again once somebody is signed in.
727+
*/
728+
const waitForSignIn = () => {
729+
if (stopped || stopWaiting) return;
730+
const stop = queryClient.getQueryCache().subscribe(() => {
731+
if (!signedInNow(queryClient)) return;
732+
stop();
733+
stopWaiting = undefined;
734+
attempts = 0;
735+
connect();
736+
});
737+
stopWaiting = stop;
738+
};
739+
740+
/**
741+
* Refused before it opened. A browser never shows the page why, so OpenBot is asked who is
742+
* signed in (`askWhoIsSignedIn`): on the first refusal since the socket was last open, and then
743+
* no more often than {@link ASK_AGAIN_MS}. Nobody: wait for a sign-in; anything else: as before.
744+
*/
745+
const refusedBeforeOpening = () => {
746+
const now = Date.now();
747+
if (askedAt !== undefined && now - askedAt < ASK_AGAIN_MS) {
748+
retryLater();
749+
return;
750+
}
751+
askedAt = now;
752+
askWhoIsSignedIn(queryClient).then((answer) => {
753+
if (stopped) return;
754+
if (answer === "signed-out") waitForSignIn();
755+
else retryLater();
756+
}, retryLater);
757+
};
758+
707759
/** Read the lists, as nothing patched while the socket was not heard can be trusted. */
708760
const readLists = () => {
709761
// An artifact open on screen reads its comments and viewers again, as the roster does.
@@ -741,6 +793,7 @@ export function useChannelEvents() {
741793
opened = true;
742794
renewal.opened();
743795
attempts = 0;
796+
askedAt = undefined;
744797
/*
745798
* A turn's end said while the socket was down was never heard, so what it said before is
746799
* let go of: the read below is the best word there is. This tab's own turns stay held.
@@ -876,8 +929,8 @@ export function useChannelEvents() {
876929
renewal.closedBeforeOpening().then((renewed) => {
877930
if (stopped) return;
878931
if (renewed) connect();
879-
else retryLater();
880-
}, retryLater);
932+
else refusedBeforeOpening();
933+
}, refusedBeforeOpening);
881934
return;
882935
}
883936
retryLater();
@@ -888,6 +941,7 @@ export function useChannelEvents() {
888941

889942
return () => {
890943
stopped = true;
944+
stopWaiting?.();
891945
if (retryTimer !== undefined) window.clearTimeout(retryTimer);
892946
if (readTimer !== undefined) window.clearTimeout(readTimer);
893947
// Cleared first: the close below must not schedule a reconnect for a screen that is gone.

‎app/src/lib/computers/control.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
takeComputerControl,
99
} from "@/lib/api/generated/computers/computers";
1010
import { idempotencyKey, organizationId } from "@/lib/api/request";
11+
import { refusedAsSignedOut } from "@/lib/auth/signed-out";
1112
import { ClientError } from "@/lib/client";
1213
import type { Failure } from "@/lib/load-failure";
1314

@@ -103,30 +104,38 @@ export function readControl(
103104
/** A read of the wheel that answered "not found": the Bot is not one this person can use. */
104105
export const BOT_GONE = "gone";
105106

107+
/** A read of the wheel that OpenBot refused with its own 401: nobody is signed in. */
108+
export const SIGNED_OUT = "signed-out";
109+
106110
/**
107-
* Who is driving, for the poll: as `readControl`, and saying when the Bot itself is not there.
111+
* Who is driving, for the poll: as `readControl`, and saying when the Bot itself is not there, or
112+
* when nobody is signed in.
108113
*
109114
* The API answers the read 404 when the Bot does not exist or is not this person's any more (a Bot
110-
* shared with a team they were taken out of). That is an answer and not a hiccup: asking again a
111-
* second later gets it again, for as long as the screen stays open.
115+
* shared with a team they were taken out of), and 401 when the person was signed out (elsewhere, by
116+
* an admin, or their session ran out). Both are answers and not hiccups: asking again a second later
117+
* gets them again, for as long as the screen stays open. Only OpenBot's own 401 is signed out
118+
* (`refusedAsSignedOut`): a proxy's, or a hosted sign-in that needs renewing, is no answer.
112119
*/
113120
export async function readControlOrGone(
114121
computerId: string,
115122
signal?: AbortSignal,
116-
): Promise<ControlState | typeof BOT_GONE | null> {
117-
let gone = false;
123+
): Promise<ControlState | typeof BOT_GONE | typeof SIGNED_OUT | null> {
124+
let refusal: typeof BOT_GONE | typeof SIGNED_OUT | undefined;
118125
const control = await controlFrom(
119126
getComputerControl(
120127
organizationId(),
121128
computerId,
122129
undefined,
123130
signal ? { signal } : {},
124131
).catch((error: unknown) => {
125-
gone = error instanceof ClientError && error.status === 404;
132+
if (error instanceof ClientError && error.status === 404)
133+
refusal = BOT_GONE;
134+
else if (refusedAsSignedOut(error)) refusal = SIGNED_OUT;
126135
throw error;
127136
}),
128137
);
129-
return gone ? BOT_GONE : control;
138+
return refusal ?? control;
130139
}
131140

132141
export async function takeControl(computerId: string, requestId?: string) {

‎app/src/lib/computers/use-control.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useQuery } from "@tanstack/react-query";
22
import { useSyncExternalStore } from "react";
33
import { currentUserQueryOptions } from "@/lib/auth/queries";
4+
import { askWhoIsSignedIn } from "@/lib/auth/session-check";
45
import {
56
type WindowAttention,
67
watchWindowAttention,
@@ -14,6 +15,7 @@ import {
1415
cancelControl,
1516
readControlOrGone,
1617
releaseControl,
18+
SIGNED_OUT,
1719
takeControl,
1820
} from "./control";
1921
import { nextReadIn, STEADY_READS } from "./poll-pace";
@@ -47,7 +49,7 @@ function createControlStore(computerId: string): ControlStore {
4749
let failures = 0;
4850
/** Reads in a row that answered what the one before did, which slows the next (`nextReadIn`). */
4951
let unchanged = 0;
50-
/** The last read answered that the Bot is not this person's to use. See `refresh`. */
52+
/** The last read answered that the Bot is not this person's to use, or that nobody is signed in. See `read`. */
5153
let gone = false;
5254
let stopWatching: (() => void) | undefined;
5355
const listeners = new Set<() => void>();
@@ -111,14 +113,22 @@ function createControlStore(computerId: string): ControlStore {
111113
* the read is not asked again every second (it was, 55 times in one capture of an open
112114
* conversation). Coming back to the tab asks once more, in case the Bot is theirs again.
113115
*/
114-
gone = answer === BOT_GONE;
116+
/*
117+
* Nobody is signed in any more: signed out elsewhere, by an admin, or the session ran out. The
118+
* same as a Bot that is gone, the read stops (it went on every second or two for hours, in a tab
119+
* signed out since the afternoon), and the app is asked to find out who is signed in, which
120+
* takes the tab to signing in again (`askWhoIsSignedIn`).
121+
*/
122+
gone = answer === BOT_GONE || answer === SIGNED_OUT;
115123
if (gone) {
116124
failures = 0;
125+
if (answer === SIGNED_OUT) void askWhoIsSignedIn(queryClient);
117126
if (snapshot.control || snapshot.problem)
118127
publish({ ...snapshot, control: null, problem: null });
119128
return;
120129
}
121-
const control = answer === BOT_GONE ? null : answer;
130+
const control =
131+
answer === BOT_GONE || answer === SIGNED_OUT ? null : answer;
122132
/*
123133
* One read that did not come back is not news. It used to blank the control state at once, which
124134
* took the wheel off a person mid-sentence: their keyboard stopped being forwarded and every key

‎app/src/routes/_authed.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ReactNode } from "react";
44
import { BotLookProvider } from "../components/agents/bot-look";
55
import { BeingDeletedGate } from "../components/auth/being-deleted-page";
66
import { HeldOutGate } from "../components/auth/company-sign-in-page";
7+
import { SignedOutWatch } from "../components/auth/signed-out-watch";
78
import { WebPushKeeper } from "../components/delivery/web-push-keeper";
89
import { OpenFromApp } from "../components/desktop/open-from-app";
910
import { TeamFeaturesDialog } from "../components/edition/team-features-dialog";
@@ -32,6 +33,9 @@ import {
3233
import { signInReturnPath } from "../lib/auth/resume";
3334
import { fittedReturnPath } from "../lib/auth/return-address";
3435
import { forgetSignedIn, rememberSignedIn } from "../lib/auth/was-signed-in";
36+
import { CopilotProvider } from "../lib/copilot/provider";
37+
import { whenConnectionBack } from "../lib/desktop/connection-lost";
38+
import { noteUnreachable } from "../lib/desktop/reachability";
3539
import { embedConfig } from "../lib/embed/config";
3640
import {
3741
EMBED_REFUSED_DEFAULT_MESSAGE,
@@ -41,9 +45,6 @@ import {
4145
SIGNED_OUT,
4246
withoutEmbedRenewal,
4347
} from "../lib/embed/session";
44-
import { CopilotProvider } from "../lib/copilot/provider";
45-
import { whenConnectionBack } from "../lib/desktop/connection-lost";
46-
import { noteUnreachable } from "../lib/desktop/reachability";
4748
import { ReportedErrorComponent } from "../lib/error-reporting";
4849
import { AppHotkeys } from "../lib/hotkeys/app-hotkeys";
4950
import { standingInWhileLoadingAgain } from "../lib/load-again";
@@ -342,6 +343,8 @@ function AuthedApp() {
342343
*/}
343344
<SkinBotLook>
344345
<AppHotkeys />
346+
{/* Nobody signed in any more, found out by a socket or a read refused: the gate again, so the tab signs in. */}
347+
<SignedOutWatch />
345348
{/* This browser's push subscription, saved again in each organization OpenBot starts in. */}
346349
<WebPushKeeper />
347350
{/* For an admin: requests for apps kept counted, and a notice for a new one while OpenBot is out of sight. */}

0 commit comments

Comments
 (0)