From cda0a76b7e6dfaa7886bb77fa660b471086103ff Mon Sep 17 00:00:00 2001 From: Markus Ecker Date: Fri, 27 Nov 2026 16:53:00 +0100 Subject: [PATCH 1/4] Answer a Bot that stops its own run to ask before it uses a tool --- CHANGELOG.md | 10 ++ .../approvals/bot-approval-notice.tsx | 146 ++++++++++++++++++ app/src/components/channels/channel-chat.tsx | 17 ++ app/src/lib/copilot/bot-raised-interrupts.ts | 90 +++++++++++ app/tests/bot-raised-interrupts.test.ts | 93 +++++++++++ 5 files changed, 356 insertions(+) create mode 100644 app/src/components/approvals/bot-approval-notice.tsx create mode 100644 app/src/lib/copilot/bot-raised-interrupts.ts create mode 100644 app/tests/bot-raised-interrupts.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b5822ccde5..0059e3ed38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,16 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged. ## Unreleased +- **A Bot that stops to ask before it uses a tool can be answered.** A Bot connected by its address + can pause its own run to ask, as a harness does before it runs one of its own tools. The + conversation now shows what the Bot wants to use and with what, with Allow and Don't allow; a wait + that is not a yes or no can be let go. Before, nothing showed and the conversation was stuck: + every later message was refused until the wait was answered. + +- **A single-user OpenBot in a container tells its browser about the deployment.** It refused the + page's first question about the deployment on every page, so the release check and attachment + limits were missing. The page now asks once more as the browser that was let in. + **Before upgrading.** - **Migrations run at start: six, and all are expands.** The database moves from diff --git a/app/src/components/approvals/bot-approval-notice.tsx b/app/src/components/approvals/bot-approval-notice.tsx new file mode 100644 index 0000000000..052d016e8f --- /dev/null +++ b/app/src/components/approvals/bot-approval-notice.tsx @@ -0,0 +1,146 @@ +import type { Message } from "@ag-ui/core"; +import { useEffect, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { + answersFor, + asksForApproval, + botRaisedInterrupts, + type HeldInterrupt, + type InterruptAnswer, + interruptedCall, +} from "@/lib/copilot/bot-raised-interrupts"; +import { type Failure, failureOf, failureSaid } from "@/lib/load-failure"; + +type InterruptedAgent = { + pendingInterrupts: HeldInterrupt[]; + messages: readonly Message[]; + isRunning: boolean; + subscribe(subscriber: { + onRunInitialized?: () => void; + onRunFinalized?: () => void; + onRunFailed?: () => void; + }): { unsubscribe(): void }; +}; + +/** + * What a Bot stopped its own run to ask, with the person's answer to it. + * + * Only for a wait the Bot raised itself; OpenBot's own approvals have their cards + * (`bot-raised-interrupts.ts` says which is which). The answer is sent as the next run's `resume`, + * which is how AG-UI carries one. + */ +export function BotApprovalNotice({ + agent, + botName, + ready, + run, +}: { + agent: InterruptedAgent; + botName: string; + ready: boolean; + run: (resume: InterruptAnswer[]) => Promise; +}) { + const [, redraw] = useState(0); + const [busy, setBusy] = useState(false); + const [problem, setProblem] = useState(null); + // The agent's interrupts change when a run ends or starts, which is not a render of its own. + useEffect(() => { + const again = () => redraw((count) => count + 1); + const watching = agent.subscribe({ + onRunInitialized: again, + onRunFinalized: again, + onRunFailed: again, + }); + return () => watching.unsubscribe(); + }, [agent]); + + const waiting = ready ? botRaisedInterrupts(agent.pendingInterrupts) : []; + if (waiting.length === 0 || agent.isRunning) return null; + const approvals = waiting.filter(asksForApproval); + + const answer = async (approved: boolean) => { + if (busy) return; + setBusy(true); + setProblem(null); + try { + await run(answersFor(agent.pendingInterrupts, approved)); + } catch (error) { + setProblem(failureOf(error, "Couldn't send your answer. Try again.")); + } finally { + setBusy(false); + } + }; + + return ( +
+ {approvals.length > 0 ? ( +

{botName} asks before it goes on

+ ) : ( +

+ {botName} is waiting for an answer this app can't give +

+ )} + {waiting.map((interrupt) => { + const call = interruptedCall(interrupt, agent.messages); + // What the Bot asks, in its own words. Not an error's: nothing here failed. + const asked = interrupt.message; + return ( +
+ {/* + * The call itself when the conversation has it: its name and what it was given, as the + * person would check them. A harness words its question for a terminal (`Approve + * "shell"? Input: {...}`), so its words are only for a wait that names no call. + */} + {call ? ( + <> +

+ Use {call.name} +

+

+ {call.args} +

+ + ) : asked ? ( +

{asked}

+ ) : null} +
+ ); + })} + {problem ? ( +

+ {failureSaid(problem)} +

+ ) : null} +
+ {approvals.length > 0 ? ( + <> + + + + ) : ( + + )} +
+
+ ); +} diff --git a/app/src/components/channels/channel-chat.tsx b/app/src/components/channels/channel-chat.tsx index a0a396a7a1..cf2efc7718 100644 --- a/app/src/components/channels/channel-chat.tsx +++ b/app/src/components/channels/channel-chat.tsx @@ -15,6 +15,7 @@ import { useSyncExternalStore, } from "react"; import { BotOrb } from "@/components/agents/bot-orb"; +import { BotApprovalNotice } from "@/components/approvals/bot-approval-notice"; import { ConversationAllowances } from "@/components/approvals/conversation-allowances"; import { answerMessage, @@ -3050,6 +3051,22 @@ export function ChannelChat({ {channel.active ? ( ) : null} + {/* What the Bot stopped its own run to ask; the answer is the next run's `resume`. */} + { + setRunsInFlight((count) => count + 1); + try { + await whileObserved(agent, () => + copilotkit.runAgent({ agent, resume }), + ); + } finally { + setRunsInFlight((count) => count - 1); + } + }} + /> ; +}; + +/** One answer to an interrupt, as a run's `resume` carries it. */ +export type InterruptAnswer = { + interruptId: string; + status: "resolved" | "cancelled"; + payload?: unknown; +}; + +/** + * The interrupts a Bot raised itself, which only this app can answer. + * + * MOST WAITS ARE THE SERVER'S. An approval, a question or a sign-in OpenBot asks for is answered on + * its own card and carried on by the server (`server-owned-interrupts.ts`). A Bot connected by its + * address can also stop a run on its own: a harness that asks before it runs one of its own tools, a + * graph's `interrupt()`. The server passes that through untouched, and passes its answer back + * (`endpointResume` in the server's copilot.ts), so the run waits on whoever holds the conversation. + * Left unanswered, the conversation is stuck: the agent refuses every later run ("pending + * interrupt(s) not addressed by resume"). + */ +export function botRaisedInterrupts( + pending: readonly T[], +): T[] { + return pending.filter((interrupt) => !serverOwnedInterrupt(interrupt)); +} + +/** + * Whether an interrupt asks yes or no: its answer is an object whose one required field is the + * boolean `approved`. That is what an approval asked by a Bot's own harness looks like on AG-UI; + * anything else has an answer this app cannot draw a form for, and can only be let go. + */ +export function asksForApproval(interrupt: HeldInterrupt): boolean { + const schema = interrupt.responseSchema; + if (!schema || typeof schema !== "object") return false; + const { properties, required } = schema as { + properties?: Record; + required?: unknown; + }; + return ( + properties?.approved?.type === "boolean" && + Array.isArray(required) && + required.length === 1 && + required[0] === "approved" + ); +} + +/** The call an interrupt is about, from the conversation: its name and its arguments as sent. */ +export function interruptedCall( + interrupt: HeldInterrupt, + messages: readonly Message[], +): { name: string; args: string } | undefined { + if (!interrupt.toolCallId) return undefined; + for (const message of messages) { + if (message.role !== "assistant") continue; + const call = message.toolCalls?.find( + (candidate) => candidate.id === interrupt.toolCallId, + ); + if (call) + return { name: call.function.name, args: call.function.arguments }; + } + return undefined; +} + +/** + * The `resume` that answers every interrupt the agent holds, given the person's decision on those + * the Bot raised. A run must address all of them at once, so one decision covers each approval the + * Bot asked for in that step; an interrupt that is not a yes or no is cancelled, which lets the + * conversation go on. + */ +export function answersFor( + pending: readonly HeldInterrupt[], + approved: boolean, +): InterruptAnswer[] { + return botRaisedInterrupts(pending).map((interrupt) => + asksForApproval(interrupt) + ? { interruptId: interrupt.id, status: "resolved", payload: { approved } } + : { interruptId: interrupt.id, status: "cancelled" }, + ); +} diff --git a/app/tests/bot-raised-interrupts.test.ts b/app/tests/bot-raised-interrupts.test.ts new file mode 100644 index 0000000000..5396c15e63 --- /dev/null +++ b/app/tests/bot-raised-interrupts.test.ts @@ -0,0 +1,93 @@ +import { describe, expect, test } from "bun:test"; +import type { Message } from "@ag-ui/core"; +import { OPENBOT_WAITING_METADATA } from "@openbot/shared/waiting"; +import { + answersFor, + asksForApproval, + botRaisedInterrupts, + interruptedCall, +} from "@/lib/copilot/bot-raised-interrupts"; + +const yesOrNo = { + type: "object", + properties: { approved: { type: "boolean" } }, + required: ["approved"], +}; +/** What a Strands harness sends when it asks before running one of its own tools. */ +const fromTheBot = { + id: "int-1", + reason: "tool_call", + toolCallId: "call-1", + responseSchema: yesOrNo, +}; +const fromTheServer = { + id: "call-2", + reason: "human_input", + metadata: { [OPENBOT_WAITING_METADATA]: { requestId: "r1" } }, +}; +const aQuestion = { + id: "int-3", + reason: "input_required", + responseSchema: { type: "object", properties: { city: { type: "string" } } }, +}; + +describe("a wait the Bot raised itself", () => { + test("is told apart from one the server answers", () => { + expect(botRaisedInterrupts([fromTheBot, fromTheServer])).toEqual([ + fromTheBot, + ]); + }); + + test("asks yes or no when its one required answer is the boolean `approved`", () => { + expect(asksForApproval(fromTheBot)).toBe(true); + expect(asksForApproval(aQuestion)).toBe(false); + expect(asksForApproval({ id: "bare" })).toBe(false); + expect( + asksForApproval({ + id: "more", + responseSchema: { ...yesOrNo, required: ["approved", "reason"] }, + }), + ).toBe(false); + }); + + test("names the call it is about, from the conversation", () => { + const messages: Message[] = [ + { id: "u", role: "user", content: "run it" }, + { + id: "a", + role: "assistant", + toolCalls: [ + { + id: "call-1", + type: "function", + function: { name: "shell", arguments: '{"command":"ls"}' }, + }, + ], + }, + ]; + expect(interruptedCall(fromTheBot, messages)).toEqual({ + name: "shell", + args: '{"command":"ls"}', + }); + expect(interruptedCall(aQuestion, messages)).toBeUndefined(); + }); + + test("is answered with the decision, and the server's waits are left to the server", () => { + expect(answersFor([fromTheBot, fromTheServer], true)).toEqual([ + { interruptId: "int-1", status: "resolved", payload: { approved: true } }, + ]); + expect(answersFor([fromTheBot], false)).toEqual([ + { + interruptId: "int-1", + status: "resolved", + payload: { approved: false }, + }, + ]); + }); + + test("is cancelled when its answer is not a yes or no, so the conversation can go on", () => { + expect(answersFor([aQuestion], true)).toEqual([ + { interruptId: "int-3", status: "cancelled" }, + ]); + }); +}); From f328c251ba7d09043371ca82b2053ce199fe8150 Mon Sep 17 00:00:00 2001 From: Markus Ecker Date: Fri, 27 Nov 2026 16:53:00 +0100 Subject: [PATCH 2/4] Ask a single-user OpenBot about its deployment again as the browser it let in --- app/src/lib/deployment/queries.ts | 42 +++++++++++++++++--------- app/tests/second-organization.test.tsx | 26 ++++++++++++++++ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/app/src/lib/deployment/queries.ts b/app/src/lib/deployment/queries.ts index 4b003776b9..0cb5618144 100644 --- a/app/src/lib/deployment/queries.ts +++ b/app/src/lib/deployment/queries.ts @@ -126,6 +126,31 @@ function showsOpenBotOnThisComputer(): Promise { * told "not found" and drew the router's error in its place. Told "not found" in the organization * it knows (signed out since, or no longer in it), it is asked the way it is before signing in. */ +/** + * `deployment` at `default`, asked as nobody: without the page's cookies. + * + * ONE OPENBOT REFUSES NOBODY: a single-user one in a container, which answers only a browser that + * came in through its entry address, and knows that browser by a cookie (the server's + * request-trust.ts). Asked without cookies it said "permission denied", on every page, to the very + * browser it had let in: no deployment facts, so no release check and no attachment limits, and a + * failed request in the console each time. That refusal is about the browser, not about a person, + * so the page asks once more with its cookies. Any other OpenBot answers the first ask. + */ +async function asNobody(options: { + signal?: AbortSignal; + fallback: string; +}): Promise { + try { + return await getDeployment(DEPLOYMENT_ORGANIZATION, { + ...options, + credentials: "omit", + }); + } catch (error) { + if (!(error instanceof ClientError) || error.status !== 403) throw error; + return getDeployment(DEPLOYMENT_ORGANIZATION, options); + } +} + export async function readDeployment(options: { signal?: AbortSignal; fallback: string; @@ -137,17 +162,10 @@ export async function readDeployment(options: { } catch (error) { if (!(error instanceof ClientError) || error.status !== 404) throw error; if (await showsOpenBotOnThisComputer()) throw error; - return getDeployment(DEPLOYMENT_ORGANIZATION, { - ...options, - credentials: "omit", - }); + return asNobody(options); } } - if (!(await showsOpenBotOnThisComputer())) - return getDeployment(DEPLOYMENT_ORGANIZATION, { - ...options, - credentials: "omit", - }); + if (!(await showsOpenBotOnThisComputer())) return asNobody(options); try { return await getDeployment(DEPLOYMENT_ORGANIZATION, options); } catch (error) { @@ -156,11 +174,7 @@ export async function readDeployment(options: { .organizations[0]; // In no organization (removed from the last one): what this OpenBot offers anybody, asked as nobody, so the // sign-in screen and the page for somebody in no organization can still offer a way back in. - if (!own) - return getDeployment(DEPLOYMENT_ORGANIZATION, { - ...options, - credentials: "omit", - }); + if (!own) return asNobody(options); return getDeployment(own.id, options); } } diff --git a/app/tests/second-organization.test.tsx b/app/tests/second-organization.test.tsx index b8c5d19d77..c3a46dafc2 100644 --- a/app/tests/second-organization.test.tsx +++ b/app/tests/second-organization.test.tsx @@ -63,6 +63,12 @@ const DEPLOYMENT_ADMINS_ONLY = { "Only an admin of the default organization can do this for now, because it applies to every organization here. Ask one of them.", retryable: false, }; +/** What a single-user OpenBot with an install token says to a request that does not carry it. */ +const INSTALL_TOKEN_REFUSAL = { + ...DEPLOYMENT_ADMINS_ONLY, + detail: + "This OpenBot only answers its own app. Open it from the OpenBot app, or use an API key.", +}; const NOT_FOUND = { type: "https://open.bot/errors/organization-not-found", title: "Organization not found.", @@ -354,6 +360,26 @@ test("signed in outside `default`, the app's first load learns what OpenBot offe ]); }); +test("an OpenBot that answers only the browser it let in is asked again with that browser's cookies", async () => { + // A single-user OpenBot in a container knows its one browser by a cookie, and refuses a request without it: the + // deployment asked as nobody, on every page. Refused that way, it is asked once more as the page. + setCurrentOrganization(undefined); + answer = (path, asNobody) => + path === "/api/v2/organizations/default/deployment" && !asNobody + ? Response.json(FACTS) + : problem(INSTALL_TOKEN_REFUSAL); + const capabilities = await freshClient().fetchQuery( + deploymentCapabilitiesQueryOptions(), + ); + expect(capabilities).toMatchObject({ generativeUi: true }); + expect(sent.map((request) => request.path)).toEqual([ + "/api/v2/organizations/default/deployment", + "/api/v2/organizations/default/deployment", + ]); + // The first ask, without cookies, is the one refused; nothing else is. + expect(refused).toHaveLength(1); +}); + test("once the organization is known, what OpenBot offers is asked there, and nobody signed in asks at `default`", async () => { setCurrentOrganization(HARBOR.id); answer = (path) => From 95500d95a8133498ed2fc67547bd4eceda9ea547 Mon Sep 17 00:00:00 2001 From: Markus Ecker Date: Thu, 3 Dec 2026 14:53:25 +0100 Subject: [PATCH 3/4] Take the release note out of CHANGELOG.md: it goes in the pull request's description --- CHANGELOG.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dce3b15f6e..20b215ff8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,16 +8,6 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged. ## Unreleased -- **A Bot that stops to ask before it uses a tool can be answered.** A Bot connected by its address - can pause its own run to ask, as a harness does before it runs one of its own tools. The - conversation now shows what the Bot wants to use and with what, with Allow and Don't allow; a wait - that is not a yes or no can be let go. Before, nothing showed and the conversation was stuck: - every later message was refused until the wait was answered. - -- **A single-user OpenBot in a container tells its browser about the deployment.** It refused the - page's first question about the deployment on every page, so the release check and attachment - limits were missing. The page now asks once more as the browser that was let in. - ## 1.2.9 **Before upgrading.** From a97deb75660f59a8994848b8938d480337e62208 Mon Sep 17 00:00:00 2001 From: Markus Ecker Date: Fri, 4 Dec 2026 11:33:42 +0100 Subject: [PATCH 4/4] Answering a Bot's own wait: sent beside a wait of OpenBot's, let go once its time is over, and tracked as a turn - A wait OpenBot holds beside the Bot's is released on the agent before the answer is sent, as before any message: with both replayed, Allow sent nothing. - A wait whose time is over can only be let go. The card says so and offers that, and a click checks again. - The answer runs as a turn like any other, so what the Bot goes on to say reaches the roster and a failed run is said under the transcript; one that never left the browser is said on the card. --- .../approvals/bot-approval-notice.tsx | 26 +- app/src/components/channels/channel-chat.tsx | 92 ++++- app/src/lib/copilot/bot-raised-interrupts.ts | 27 +- app/tests/bot-approval-resume.test.tsx | 339 ++++++++++++++++++ app/tests/bot-raised-interrupts.test.ts | 21 ++ 5 files changed, 479 insertions(+), 26 deletions(-) create mode 100644 app/tests/bot-approval-resume.test.tsx diff --git a/app/src/components/approvals/bot-approval-notice.tsx b/app/src/components/approvals/bot-approval-notice.tsx index 052d016e8f..3a9dd675c2 100644 --- a/app/src/components/approvals/bot-approval-notice.tsx +++ b/app/src/components/approvals/bot-approval-notice.tsx @@ -5,6 +5,7 @@ import { answersFor, asksForApproval, botRaisedInterrupts, + expired, type HeldInterrupt, type InterruptAnswer, interruptedCall, @@ -54,16 +55,31 @@ export function BotApprovalNotice({ return () => watching.unsubscribe(); }, [agent]); + // Set when a click found the Bot no longer takes the decision, so the card says so from then on. + const [lapsed, setLapsed] = useState(false); + const waiting = ready ? botRaisedInterrupts(agent.pendingInterrupts) : []; if (waiting.length === 0 || agent.isRunning) return null; const approvals = waiting.filter(asksForApproval); + /* + * A decision the Bot still takes. One whose time is over can only be let go: the AG-UI client + * refuses an answer to it before anything is sent, so Allow did nothing and the conversation was + * stuck. Read when the card draws and again at the click, which may come much later. + */ + const open = lapsed ? [] : approvals.filter((asked) => !expired(asked)); const answer = async (approved: boolean) => { if (busy) return; + const now = new Date(); + // Allowing what the Bot no longer waits on would be sent as letting it go: said first instead. + if (approved && approvals.some((asked) => expired(asked, now))) { + setLapsed(true); + return; + } setBusy(true); setProblem(null); try { - await run(answersFor(agent.pendingInterrupts, approved)); + await run(answersFor(agent.pendingInterrupts, approved, now)); } catch (error) { setProblem(failureOf(error, "Couldn't send your answer. Try again.")); } finally { @@ -77,8 +93,12 @@ export function BotApprovalNotice({ data-bot-approval role="status" > - {approvals.length > 0 ? ( + {open.length > 0 ? (

{botName} asks before it goes on

+ ) : approvals.length > 0 ? ( +

+ {botName} waited too long for this answer and no longer takes it +

) : (

{botName} is waiting for an answer this app can't give @@ -116,7 +136,7 @@ export function BotApprovalNotice({

) : null}
- {approvals.length > 0 ? ( + {open.length > 0 ? ( <>