Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/drained-turn-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-server": patch
---

A question, tool approval, location or mask request that a backend raises after its turn's pending requests were drained (for example when the host's turn limit ran out before the question reached the bridge) is now refused at once with the drain's reason, instead of waiting forever and holding the session's turn open. A refused question is recorded as closed, so a late answer is told the question ended.
20 changes: 20 additions & 0 deletions packages/ui-server/src/ws/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ export async function makeBridge(
);
resolve(decision);
};
// A request raised after the turn's drain is expired the way the
// drain expires one, or it would never settle (#3553).
const drained = coordinator.drainedReason(turn, turnId);
if (drained !== undefined) {
coordinator.recordApprovalOutcome(req.toolUseId, { outcome: "expired", turnId, sessionId: turn.sessionId, reason: drained });
recorded({ behavior: "deny", message: drained });
return;
}
if (!coordinator.openApproval(req.toolUseId, { turn, turnId, request: req, resolve: recorded })) {
resolve({ behavior: "deny", message: "Duplicate tool-approval id" });
return;
Expand Down Expand Up @@ -514,6 +522,8 @@ export async function makeBridge(
: {}),
askUser: (requestId, questions) => {
return new Promise<AskUserResult>((resolve, reject) => {
const drained = coordinator.refuseDrainedAsk({ turn, turnId, requestId });
if (drained !== undefined) return reject(new Error(drained));
if (!coordinator.openAsk("ask_user", { turn, turnId, requestId, questions, resolve, reject })) {
reject(new Error("Duplicate ask-user request id"));
return;
Expand All @@ -532,6 +542,8 @@ export async function makeBridge(
},
askUserList: (requestId, request) => {
return new Promise<AskUserListResult>((resolve, reject) => {
const drained = coordinator.refuseDrainedAsk({ turn, turnId, requestId });
if (drained !== undefined) return reject(new Error(drained));
// One id space across all ask kinds: a single `ask_user_cancel`
// dismisses any of them, so an id pending as one must not open as the other.
if (!coordinator.openAsk("ask_user_list", { turn, turnId, requestId, request, resolve, reject })) {
Expand All @@ -556,6 +568,8 @@ export async function makeBridge(
return Promise.reject(new Error("Duplicate ask-user request id"));
}
return new Promise<AskUserRankResult>((resolve, reject) => {
const drained = coordinator.refuseDrainedAsk({ turn, turnId, requestId });
if (drained !== undefined) return reject(new Error(drained));
coordinator.openAsk("ask_user_rank", { turn, turnId, requestId, request, resolve, reject });
host.sendToClients(withTurnScope({ type: "ask_user_rank_request", requestId, ...request }, turn, turnId));
host.sendToClients(withTurnScope({ type: "status", status: "tool_executing", detail: "Waiting for your input" }, turn, turnId));
Expand All @@ -568,6 +582,8 @@ export async function makeBridge(
return Promise.reject(new Error("Duplicate ask-user request id"));
}
return new Promise<AskUserFormResult>((resolve, reject) => {
const drained = coordinator.refuseDrainedAsk({ turn, turnId, requestId });
if (drained !== undefined) return reject(new Error(drained));
coordinator.openAsk("ask_user_form", { turn, turnId, requestId, request: validated, resolve, reject });
host.sendToClients(withTurnScope({ type: "ask_user_form_request", requestId, ...validated }, turn, turnId));
host.sendToClients(withTurnScope({ type: "status", status: "tool_executing", detail: "Waiting for your input" }, turn, turnId));
Expand All @@ -581,6 +597,8 @@ export async function makeBridge(
}
const requestId = coordinator.nextLocationRequestId();
return new Promise<LocationFix>((resolve, reject) => {
const drained = coordinator.drainedReason(turn, turnId);
if (drained !== undefined) return reject(new Error(drained));
if (!coordinator.openLocation(requestId, { turn, turnId, resolve, reject })) {
reject(new Error("Duplicate location request id"));
return;
Expand All @@ -605,6 +623,8 @@ export async function makeBridge(
}
const requestId = coordinator.nextMaskRequestId();
return new Promise<Uint8Array>((resolve, reject) => {
const drained = coordinator.drainedReason(turn, turnId);
if (drained !== undefined) return reject(new Error(drained));
if (!coordinator.openMask(requestId, { turn, turnId, resolve, reject })) {
reject(new Error("Duplicate mask request id"));
return;
Expand Down
29 changes: 29 additions & 0 deletions packages/ui-server/src/ws/turns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ export class TurnCoordinator {
readonly #handoffs = new Map<string, PendingHandoff>();
readonly #handoffPreparations = new Map<string, HandoffPreparation>();
readonly #askOutcomes = new Map<string, AskOutcome>();
/** The last drain of each turn's interactive requests, by the turn id it ended (#3553). */
readonly #drained = new WeakMap<RunningTurn, { turnId: string; reason: string }>();
readonly #authorizationRegistry = new Map<AuthorizationContext, number>();

constructor(options: TurnCoordinatorOptions = {}) {
Expand Down Expand Up @@ -996,8 +998,35 @@ export class TurnCoordinator {
this.#onApprovalSettled?.(toolUseId, record);
}

/**
* Why this turn's interactive requests were drained, when they were for
* this turn id. A drain runs once, so a request a backend raises after it
* (a turn limit that ran out before the question reached the bridge) would
* never settle and would park startTurn until restart (#3553). Openers
* refuse with this reason instead.
*/
drainedReason(turn: RunningTurn, turnId: string): string | undefined {
const drained = this.#drained.get(turn);
return drained?.turnId === turnId ? drained.reason : undefined;
}

/**
* Refuse an ask raised after its turn's drain: record it closed as ended,
* as the drain would have, and return the drain's reason. Undefined when
* the turn's requests were not drained and the ask may open.
*/
refuseDrainedAsk(p: { turn: RunningTurn; turnId: string; requestId: string }): string | undefined {
const reason = this.drainedReason(p.turn, p.turnId);
if (reason !== undefined) this.closeAsk(p, "ended");
return reason;
}

/** Resolve/reject every pending interactive request belonging to one turn. */
drainPendingForTurn(turn: RunningTurn, reason: string): void {
// A drain of an aborted turn ends its interactive requests; every such
// caller aborts first. The first one's reason is the one kept.
if (turn.abortController?.signal.aborted && this.#drained.get(turn)?.turnId !== turn.turnId)
this.#drained.set(turn, { turnId: turn.turnId, reason });
for (const [id, p] of this.#pendingApprovals) {
if (p.turn !== turn) continue;
p.resolve({ behavior: "deny", message: reason });
Expand Down
Loading
Loading