Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: give back the continuation lease when the user stops the preflight (#9560)

Daniel Han9ca293042e02
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 446d1f3e6c0..45808544887 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -163,7 +163,10 @@ import {
recordAutoContinue,
shouldAutoContinueMessage,
} from "@/features/chat/utils/continuation";
-import { holdAutoContinueRun } from "@/features/chat/utils/auto-continue-run-keeper";
+import {
+ holdAutoContinueRun,
+ watchAutoContinueRun,
+} from "@/features/chat/utils/auto-continue-run-keeper";
import { McpComposerButton } from "@/features/chat/mcp-composer-button";
import { pickerAcceptForTextBasenames } from "@/features/chat/text-attachment-accept";
import {
@@ -7162,15 +7165,16 @@ const ContinueMessageBarForLastMessage: FC = () => {
return index > 0 ? thread.messages[index - 1].id : null;
});
- const startContinuation = useCallback(() => {
+ // Hands the started run back, untyped: the only handle identified with THIS run.
+ const startContinuation = useCallback((): unknown => {
const messages = aui.thread().getState().messages;
const index = messages.findIndex((message) => message.id === messageId);
if (index < 0) {
- return;
+ return undefined;
}
// Sibling of the truncated turn, so the branch picker can still reach the partial.
const parent = index > 0 ? messages[index - 1].id : null;
- aui.thread().startRun({
+ return aui.thread().startRun({
parentId: parent,
runConfig: {
custom: {
@@ -7285,7 +7289,8 @@ const ContinueMessageBarForLastMessage: FC = () => {
// Recorded BEFORE the run, so a round that produces nothing still spends its
// budget instead of re-firing this effect forever.
recordAutoContinue(parentId);
- startContinuation();
+ // The run's own promise is what ends the hold if this preflight is stopped.
+ watchAutoContinueRun(messageId, runThreadId, startContinuation());
return;
}
// `skipped` is this tab's own duplicate call, where the run is coming from the
diff --git a/studio/frontend/src/features/chat/utils/auto-continue-issued-run.ts b/studio/frontend/src/features/chat/utils/auto-continue-issued-run.ts
new file mode 100644
index 00000000000..50a6be4ea0d
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/auto-continue-issued-run.ts
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import type { AutoContinueIssuedRun } from "./continuation";
+
+/** `startRun` is DECLARED `void` but returns the roundtrip's promise, so it arrives untyped and
+ * the shape is checked rather than assumed. Not thenable means no signal, so the hold is renewed
+ * as any unarmed hold is, never released early. A rejection settles it too: it says the run is
+ * not coming, not whether the lease may be given back. */
+export function issuedRunFrom(
+ started: unknown,
+): AutoContinueIssuedRun | undefined {
+ if (
+ started === null ||
+ (typeof started !== "object" && typeof started !== "function") ||
+ typeof (started as { then?: unknown }).then !== "function"
+ ) {
+ return undefined;
+ }
+ const run = started as PromiseLike<unknown>;
+ return {
+ whenSettled: (onSettled) => {
+ run.then(onSettled, onSettled);
+ },
+ };
+}
diff --git a/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts b/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
index b1cb76cc124..a66c8a264c9 100644
--- a/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
+++ b/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
@@ -8,6 +8,7 @@
* survives navigation. */
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
+import { issuedRunFrom } from "./auto-continue-issued-run";
import {
AUTO_CONTINUE_LEASE_RENEW_MS,
createAutoContinueLeaseKeeper,
@@ -90,3 +91,16 @@ export function holdAutoContinueRun(
}
timer ??= setInterval(tick, AUTO_CONTINUE_LEASE_RENEW_MS);
}
+
+/** The only thing that ends a hold whose preflight the user STOPPED: that run raises no failure
+ * and never reached the stream flag, so without it the hold renewed until the tab closed. */
+export function watchAutoContinueRun(
+ messageId: string,
+ threadId: string | undefined,
+ started: unknown,
+): void {
+ if (!threadId) {
+ return;
+ }
+ keeper.settleOn(messageId, threadId, issuedRunFrom(started));
+}
diff --git a/studio/frontend/src/features/chat/utils/continuation.ts b/studio/frontend/src/features/chat/utils/continuation.ts
index 8550bcafcab..34bedcf9c42 100644
--- a/studio/frontend/src/features/chat/utils/continuation.ts
+++ b/studio/frontend/src/features/chat/utils/continuation.ts
@@ -862,12 +862,21 @@ export type AutoContinueRunSignal = {
subscribe(onChange: () => void): () => void;
};
+/** The run a hold was taken for. `AutoContinueRunSignal` answers off the STREAM, so it is silent
+ * for a preflight the user STOPPED; this is pending for the whole preflight, so a merely slow run
+ * settles nothing. It must be the run's OWN promise: the next round is claimed while the previous
+ * is still winding down, so a thread-wide notice would lapse the successor's lease mid-preflight. */
+export type AutoContinueIssuedRun = {
+ whenSettled(onSettled: () => void): void;
+};
+
/** Holds the lease of each continuation this tab is running, for as long as its own run runs. A
* hold is (message, thread) and arms when THAT thread starts generating; a hold taken while
* the thread is busy waits to see it idle first, or it would arm on its predecessor's run. A
* hold whose run has not appeared yet is renewed, never timed out: the preflight has no bound
* (settings pairing, then waiting while a large local GGUF loads), and a fixed arming timeout
- * dropped the hold under a run that had since started streaming. */
+ * dropped the hold under a run that had since started streaming. An unarmed hold is discarded
+ * when its own run settles or preflight fails; a pending run keeps its renewals. */
export function createAutoContinueLeaseKeeper({
signal,
renew = (messageId, holder, now) => tab.renew(messageId, holder, { now }),
@@ -880,6 +889,11 @@ export function createAutoContinueLeaseKeeper({
now?: () => number;
}): {
hold: (messageId: string, threadId: string) => void;
+ settleOn: (
+ messageId: string,
+ threadId: string,
+ issued: AutoContinueIssuedRun | undefined,
+ ) => void;
observe: () => void;
failed: (threadId: string) => void;
tick: () => void;
@@ -891,8 +905,13 @@ export function createAutoContinueLeaseKeeper({
threadId: string;
/** Seen idle since the hold was taken, so the next run to start is this hold's own. */
idle: boolean;
+ /** The key was free when the hold was taken, so a true reading of it is this hold's own
+ * run and never somebody else's. Never reassigned, unlike `idle`. */
+ ownsTheKey: boolean;
/** That run has started. Only an armed hold is ever released. */
armed: boolean;
+ /** That run has ended, per its own promise. Nothing running after it is that run. */
+ settled: boolean;
};
const holds = new Map<string, Hold>();
let unsubscribe: (() => void) | null = null;
@@ -904,6 +923,17 @@ export function createAutoContinueLeaseKeeper({
function observe(): void {
const at = now();
for (const [id, hold] of [...holds]) {
+ if (hold.settled && !hold.armed && hold.ownsTheKey) {
+ // Its own run is over and the stream never began: Stop during preflight. Discarded, not
+ // released, so the lease lapses on its own TTL and no `done` marker claims a message that
+ // produced not one token. Ahead of the running check so nothing on the thread now can arm
+ // it, and only while UNARMED, since the key carries a LIST of owners and dropping an armed
+ // hold costs a continuation that did stream its marker. `ownsTheKey` likewise: a hold taken
+ // on an already-busy key cannot arm off its own run, so one that streamed throughout is
+ // indistinguishable from one that was stopped. Undecidable, so renewed.
+ holds.delete(id);
+ continue;
+ }
if (signal.isRunning(hold.threadId)) {
// Only a run that started after this hold was taken can be its own.
hold.armed ||= hold.idle;
@@ -916,7 +946,7 @@ export function createAutoContinueLeaseKeeper({
release(hold.messageId, hold.threadId, at);
continue;
}
- // Not armed yet, so its run is still in preflight, which has no upper bound. Kept and renewed
+ // Not armed and not settled, so its run is still in preflight, which has no upper bound. Kept and renewed
// rather than timed out: dropping it stopped the renewals while the run was on its way, and
// the lease then lapsed under a live continuation.
}
@@ -939,12 +969,31 @@ export function createAutoContinueLeaseKeeper({
threadId,
// Claimed while the thread is between runs, the ordinary case: the bar only fires on a reply that has finished.
idle: !signal.isRunning(threadId),
+ ownsTheKey: !signal.isRunning(threadId),
armed: false,
+ settled: false,
});
unsubscribe ??= signal.subscribe(observe);
},
+ /** Tie an existing hold to the run just issued for it. Separate from `hold` because the hold
+ * is taken on the line BEFORE the run starts, so the promise does not exist yet. */
+ settleOn(messageId, threadId, issued) {
+ if (!issued || !messageId || !threadId) {
+ return;
+ }
+ const hold = holds.get(key(messageId, threadId));
+ if (!hold) {
+ return;
+ }
+ issued.whenSettled(() => {
+ // The captured hold, never a fresh lookup: the same key is claimed again as soon as
+ // the next round hits Max Tokens, and this run must not settle that round's preflight.
+ hold.settled = true;
+ observe();
+ });
+ },
observe,
- /** `threadId`'s run failed on its way out, before it ever reached the run signal. The one thing
+ /** `threadId`'s run failed on its way out, before it ever reached the run signal. A signal
* that can end a hold which never armed, and a fact rather than a deadline: the adapter
* threw, so the run is over. Armed holds are left alone, since the thread going idle settles
* them with the `done` marker. This one only discards, so the lease lapses on its own TTL. */
diff --git a/studio/frontend/tests/chat-continuation-run-wiring.test.ts b/studio/frontend/tests/chat-continuation-run-wiring.test.ts
new file mode 100644
index 00000000000..8ac8645d3df
--- /dev/null
+++ b/studio/frontend/tests/chat-continuation-run-wiring.test.ts
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/**
+ * `watchAutoContinueRun` against the real module-scope keeper. `chat-continuation.test.ts` proves
+ * the keeper DECIDES correctly once handed a run; this proves the run reaches it, which a
+ * source-text match cannot: emptying this function's body kept all of those cases green.
+ *
+ * Its own file because `auto-continue-run-keeper.ts` reads `runningByThreadId` off the chat runtime
+ * store and so needs the store stub resolver. The observable is the renewal timer, which the keeper
+ * clears as soon as nothing is held, so "the timer stopped" is "the hold was given up" with no clock
+ * to advance. Replaced outright rather than mocked: the keeper captured `Date.now` as a default
+ * argument at import, so mocking `Date` desynchronises its clock from the lease's and renewals
+ * silently stop landing, passing this test for the wrong reason.
+ */
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ installLocalStorageFake,
+ registerStoreStubResolver,
+} from "./helpers/kit.ts";
+
+registerStoreStubResolver();
+const { store } = installLocalStorageFake();
+
+const { AUTO_CONTINUE_LEASE_KEY, claimAutoContinue } = await import(
+ "../src/features/chat/utils/continuation.ts"
+);
+
+const { holdAutoContinueRun, watchAutoContinueRun } = await import(
+ "../src/features/chat/utils/auto-continue-run-keeper.ts"
+);
+
+const { useChatRuntimeStore } = await import(
+ "../src/features/chat/stores/chat-runtime-store.ts"
+);
+
+/** This tab's stored lease for `messageId`, or null once it is gone. */
+function lease(messageId: string): { expires?: number; done?: boolean } | null {
+ const raw = store.get(AUTO_CONTINUE_LEASE_KEY);
+ if (!raw) {
+ return null;
+ }
+ return (
+ (JSON.parse(raw) as Record<string, { expires?: number; done?: boolean }>)[
+ messageId
+ ] ?? null
+ );
+}
+
+/** Let the renewal's own promise land: the keeper drops it, so draining is the only way. */
+async function settleWrites(): Promise<void> {
+ for (let turn = 0; turn < 25; turn += 1) {
+ await new Promise((resolve) => setImmediate(resolve));
+ }
+}
+
+test("the run the bar started reaches the keeper, so stopping it gives the lease back", async (t) => {
+ useChatRuntimeStore.setState({ runningByThreadId: {} });
+
+ // The renewal interval, captured rather than scheduled, so its ticks are this test's to drive.
+ const realSetInterval = globalThis.setInterval;
+ const realClearInterval = globalThis.clearInterval;
+ const renewalId = Symbol("renewal-interval");
+ // Collected, not a `let`: narrowing ignores a write inside a callback, so the handler would
+ // type as `never` and not call.
+ const renewalTicks: (() => void)[] = [];
+ let renewalStopped = false;
+ globalThis.setInterval = ((handler: () => void) => {
+ renewalTicks.push(handler);
+ return renewalId;
+ }) as unknown as typeof globalThis.setInterval;
+ globalThis.clearInterval = ((id: unknown) => {
+ if (id === renewalId) {
+ renewalStopped = true;
+ }
+ }) as unknown as typeof globalThis.clearInterval;
+ t.after(() => {
+ globalThis.setInterval = realSetInterval;
+ globalThis.clearInterval = realClearInterval;
+ });
+
+ assert.equal(
+ await claimAutoContinue("m1", "thread-A"),
+ "started",
+ "this tab has to own the message before it can hold its lease",
+ );
+
+ // Idle, the ordinary case, so the hold owns the key and a stopped preflight is decidable.
+ holdAutoContinueRun("m1", "thread-A");
+ assert.equal(
+ renewalTicks.length,
+ 1,
+ "holding a run has to start renewing its lease, exactly once",
+ );
+ const renewalTick = renewalTicks[0];
+
+ // A real promise, because that is what `startRun` hands back and `issuedRunFrom` must accept.
+ let stopTheRun: (() => void) | undefined;
+ const startedRun = new Promise<void>((resolve) => {
+ stopTheRun = resolve;
+ });
+ watchAutoContinueRun("m1", "thread-A", startedRun);
+
+ // Renewed throughout a long preflight, which is why arming has no deadline.
+ for (let renewal = 0; renewal < 3; renewal += 1) {
+ renewalTick();
+ await settleWrites();
+ }
+ assert.equal(
+ renewalStopped,
+ false,
+ "the hold was given up while its run was still starting",
+ );
+ assert.ok(lease("m1"), "the lease is still held while its run is starting");
+
+ // Stop. The stream flag never moved and no failure is announced, and those were the keeper's
+ // only two signals before this change, so without the run itself nothing reports anything.
+ stopTheRun?.();
+ await startedRun;
+ await settleWrites();
+
+ // Still open and still ticking, but its own run is over, so the keeper stops.
+ renewalTick();
+ await settleWrites();
+
+ assert.equal(
+ renewalStopped,
+ true,
+ "the stopped preflight kept its hold, so it renews the lease for the life of the tab",
+ );
+ assert.equal(
+ lease("m1")?.done,
+ undefined,
+ "a message that streamed not one token was marked continued",
+ );
+});
diff --git a/studio/frontend/tests/chat-continuation.test.ts b/studio/frontend/tests/chat-continuation.test.ts
index 810c952ccf0..b1a779c488c 100644
--- a/studio/frontend/tests/chat-continuation.test.ts
+++ b/studio/frontend/tests/chat-continuation.test.ts
@@ -51,6 +51,10 @@ const { createImageGateRunOwner, isImageGateRunOnly } = await import(
"../src/features/chat/utils/image-input-support.ts"
);
+const { issuedRunFrom } = await import(
+ "../src/features/chat/utils/auto-continue-issued-run.ts"
+);
+
const PARTIAL =
"There are three steps to proofing dough properly. First, warm the bowl and";
@@ -1921,6 +1925,587 @@ test("the keeper is wired to the failure the adapter already reports", () => {
);
});
+/**
+ * The run a hold was taken for, as the keeper sees it: something that settles, once.
+ * `runSignalFake` above is the STREAM, true only once tokens are on their way; this is the RUN,
+ * pending for the whole preflight and settling however that one run ends.
+ */
+function issuedRunFake() {
+ const settlers = new Set<() => void>();
+ return {
+ issued: {
+ whenSettled: (onSettled: () => void) => {
+ settlers.add(onSettled);
+ },
+ },
+ /** That run ended. */
+ settle: () => {
+ for (const onSettled of [...settlers]) {
+ onSettled();
+ }
+ },
+ };
+}
+
+test("a preflight the user stopped gives up its hold instead of keeping it for the tab", async () => {
+ // Stop during preflight. The adapter wrapper skips its per-thread failure notice ON PURPOSE
+ // (the abort was asked for, not a fault) and `runningByThreadId` never moved, since no token
+ // was ever on its way, so the hold had nothing to arm on and nothing to settle on and renewed
+ // its lease for the life of the tab. The run's own promise is the one thing that knows.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ let clock = start;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: () => {
+ assert.fail(
+ "a run that streamed nothing has continued nothing to record",
+ );
+ },
+ now: () => clock,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ for (let tick = 1; tick <= 4; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ assert.equal(keeper.held(), 1, "a preflight in progress keeps its hold");
+
+ issued.settle();
+ assert.equal(
+ keeper.held(),
+ 0,
+ "an aborted preflight strands its hold, which then renews the lease forever",
+ );
+ await Promise.all(pending);
+
+ const lapsed = clock + AUTO_CONTINUE_LEASE_TTL_MS + 1;
+ clock = lapsed;
+ keeper.tick();
+ await Promise.all(pending);
+ assert.equal(
+ await otherTab.claim("m1", { now: lapsed }),
+ "started",
+ "nothing renews it, so the lease lapses and the message comes back",
+ );
+});
+
+test("a hold whose run is merely slow is never settled by the clock", async () => {
+ // The preflight has no upper bound (settings pairing, then `waitForModelReady` polling while
+ // a large local GGUF loads) and a pending promise settles nothing.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ let clock = start;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => clock,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ for (let tick = 1; tick <= 20; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ await Promise.all(pending);
+ assert.equal(keeper.held(), 1, "the hold is still expecting its run");
+ assert.equal(
+ await otherTab.claim("m1", { now: clock }),
+ "held-elsewhere",
+ "nobody else may take a message this tab is still about to continue",
+ );
+
+ running.add("thread-A");
+ runs.change();
+ running.delete("thread-A");
+ runs.change();
+ await Promise.all(pending);
+ assert.equal(keeper.held(), 0, "its own run ending is what gives it back");
+});
+
+test("the run that ends is the one the hold was taken for, not the round before it", async () => {
+ // The adapter clears `runningByThreadId` from its own `finally`, strictly before the runtime
+ // announces that run ending, so anything per-thread would read the predecessor's ending as
+ // the successor's and lapse the lease under a live continuation.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ let clock = start;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const predecessor = issuedRunFake();
+ const round2 = issuedRunFake();
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => clock,
+ });
+
+ await tab.claim("round-1", { now: start, holder: "thread-A" });
+ keeper.hold("round-1", "thread-A");
+ keeper.settleOn("round-1", "thread-A", predecessor.issued);
+ running.add("thread-A");
+ runs.change();
+ running.delete("thread-A");
+ runs.change();
+ await Promise.all(pending);
+ assert.equal(
+ keeper.held(),
+ 0,
+ "round one is settled by its own stream ending",
+ );
+
+ // Round two is claimed and issued while round one is STILL unwinding.
+ await tab.claim("round-2", { now: clock, holder: "thread-A" });
+ keeper.hold("round-2", "thread-A");
+ keeper.settleOn("round-2", "thread-A", round2.issued);
+
+ // Round one's run finally reports itself over. It is not round two's.
+ predecessor.settle();
+ assert.equal(
+ keeper.held(),
+ 1,
+ "the predecessor ending settled the successor's hold mid-preflight",
+ );
+ for (let tick = 1; tick <= 4; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ await Promise.all(pending);
+ assert.equal(
+ await otherTab.claim("round-2", { now: clock }),
+ "held-elsewhere",
+ "and its lease is still renewed while its own run is on its way",
+ );
+
+ round2.settle();
+ assert.equal(keeper.held(), 0);
+});
+
+test("a settled hold is read before the thread is, not after it", async () => {
+ // The only pass where the settled check being ahead of the arming check is the whole answer:
+ // the hold owns an idle key, its own run ended without streaming, and the key reads busy by
+ // the time the keeper looks. Below the arming check that pass arms the hold on somebody
+ // else's run, whose end then writes a `done` marker for a message that produced not one
+ // token. An invariant of the keeper rather than a user-reachable sequence -- the signal it is
+ // wired to notifies on every store write -- but the keeper takes that signal as a parameter
+ // and promises nothing about delivery.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const released: string[] = [];
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ released.push(messageId);
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => start,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ // The key turns busy without the keeper having been told, then this hold's own run settles.
+ running.add("thread-A");
+ issued.settle();
+ assert.equal(
+ keeper.held(),
+ 0,
+ "a stopped preflight armed on whatever was on the thread when it was read",
+ );
+
+ running.delete("thread-A");
+ runs.change();
+ await Promise.all(pending);
+ assert.deepEqual(
+ released,
+ [],
+ "a message that streamed nothing was marked continued",
+ );
+});
+
+test("a hold on a key that was already busy is left alone, not guessed at", async () => {
+ // A hold taken while the key already reads busy -- `scheduleGenerationRecovery` follows a
+ // durable run on it -- has not seen the thread idle, so nothing it reads there is its own
+ // run and unarmed stops meaning "streamed nothing". The bar reaches this on its own: its
+ // `!isRunning` gate reads the SELECTED BRANCH, not `runningByThreadId`. Undecidable, so the
+ // hold is renewed; discarding it would hand a continuation that may well have streamed to
+ // the next tab to pay for again.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ let clock = start;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>(["thread-A"]);
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => clock,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ // Its own run ends while the other owner is still going: which of the two ways is unknowable.
+ issued.settle();
+ assert.equal(keeper.held(), 1, "an undecidable hold was dropped anyway");
+
+ for (let tick = 1; tick <= 12; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ await Promise.all(pending);
+ assert.equal(
+ await otherTab.claim("m1", { now: clock }),
+ "held-elsewhere",
+ "a tab that may have continued this message does not hand it back",
+ );
+});
+
+test("a continuation that streamed under a second owner keeps its marker", async () => {
+ // The regression the guard above exists for: the key is busy when the hold is taken so it
+ // never arms, yet its own run streams the whole way through. Dropped there, the message
+ // reads as never continued and another tab pays for the same continuation again.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ let clock = start;
+ const pending: Promise<void>[] = [];
+ // A durable run followed from storage, holding the key before the continuation begins.
+ const running = new Set<string>(["thread-A"]);
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => clock,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ // It streams to the end, but the other owner keeps the key true so the flag never moves.
+ runs.change();
+ for (let tick = 1; tick <= 3; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ issued.settle();
+ await Promise.all(pending);
+
+ for (let tick = 4; tick <= 16; tick += 1) {
+ clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
+ keeper.tick();
+ }
+ await Promise.all(pending);
+ assert.equal(
+ await otherTab.claim("m1", { now: clock }),
+ "held-elsewhere",
+ "a second tab was offered a continuation this tab had already streamed",
+ );
+});
+
+test("a second owner on the key does not cost an armed hold its marker", async () => {
+ // `runningByThreadId` has a LIST of owners behind it and the adapter clears only its own, so
+ // a durable run being followed keeps the key busy after this hold's stream is over and its
+ // run settles with the thread still reading busy. It streamed, so it is owed its `done`
+ // marker; discarding it there hands the message back for another tab to pay for again.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const otherTab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const issued = issuedRunFake();
+ const released: string[] = [];
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ released.push(messageId);
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => start,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", issued.issued);
+
+ // Its own run streams, and the recovery follower takes the same key while it does.
+ running.add("thread-A");
+ runs.change();
+
+ // Its stream ends and its promise settles, but the follower still holds the key.
+ issued.settle();
+ assert.equal(keeper.held(), 1, "the hold was dropped on somebody else's run");
+
+ running.delete("thread-A");
+ runs.change();
+ await Promise.all(pending);
+ assert.deepEqual(released, ["m1"]);
+ assert.equal(
+ await otherTab.claim("m1", { now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1 }),
+ "held-elsewhere",
+ "a continuation that streamed is not offered to another tab",
+ );
+});
+
+test("a run that ends after its hold is gone reaches nothing", async () => {
+ // By the time a promise settles its hold may have been given back or the key claimed again:
+ // the callback is scoped to one hold, not to the message or the thread.
+ const { storage } = storageFake();
+ const tab = createAutoContinueTab({ storage, locks: null });
+ const start = 1_000;
+ const pending: Promise<void>[] = [];
+ const running = new Set<string>();
+ const runs = runSignalFake(running);
+ const first = issuedRunFake();
+ const released: string[] = [];
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: (messageId: string, holder: string, now: number) => {
+ pending.push(tab.renew(messageId, holder, { now }));
+ },
+ release: (messageId: string, holder: string, now: number) => {
+ released.push(messageId);
+ pending.push(tab.release(messageId, holder, { now }));
+ },
+ now: () => start,
+ });
+
+ await tab.claim("m1", { now: start, holder: "thread-A" });
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", first.issued);
+ running.add("thread-A");
+ runs.change();
+ running.delete("thread-A");
+ runs.change();
+ await Promise.all(pending);
+ assert.deepEqual(released, ["m1"]);
+
+ // The same message is claimed again for the next round, and only THEN does the first run's
+ // promise settle.
+ keeper.hold("m1", "thread-A");
+ first.settle();
+ assert.equal(
+ keeper.held(),
+ 1,
+ "an older round's run settled a hold that is not its own",
+ );
+});
+
+test("settling a hold that was never taken does nothing", () => {
+ // `settleOn` runs a line after `hold`, and the hold may have been refused: a thread with no
+ // remote id yet is not safe to watch, so nothing is held for it.
+ const runs = runSignalFake(new Set<string>());
+ const issued = issuedRunFake();
+ let attached = 0;
+ const keeper = createAutoContinueLeaseKeeper({
+ signal: runs.signal,
+ renew: () => {},
+ release: () => {
+ assert.fail("nothing was ever held");
+ },
+ now: () => 1_000,
+ });
+ keeper.settleOn("m1", "thread-A", {
+ whenSettled: () => {
+ attached += 1;
+ },
+ });
+ assert.equal(attached, 0, "no hold, so the run is not watched at all");
+ assert.equal(keeper.held(), 0);
+
+ keeper.hold("m1", "thread-A");
+ keeper.settleOn("m1", "thread-A", undefined);
+ issued.settle();
+ assert.equal(keeper.held(), 1, "no signal is not the same as an ended run");
+});
+
+test("only what the runtime actually hands back is treated as the run", () => {
+ // Not thenable is assistant-ui no longer handing the run back, and the honest answer is no
+ // signal: the hold is kept and renewed as before this fix, never released early.
+ for (const notARun of [undefined, null, 0, "", "pending", true, {}, []]) {
+ assert.equal(
+ issuedRunFrom(notARun),
+ undefined,
+ `${JSON.stringify(notARun) ?? "undefined"} is not a run`,
+ );
+ }
+
+ assert.notEqual(
+ issuedRunFrom(Promise.resolve("done")),
+ undefined,
+ "a promise is a run",
+ );
+
+ // A thenable, which is all the contract asks for. The handler is parked on an object rather
+ // than a local so it survives narrowing to `never`.
+ const parked: { settle?: (ok: boolean) => void } = {};
+ const thenable = {
+ then: (onOk: () => void, onErr: () => void) => {
+ parked.settle = (ok: boolean) => (ok ? onOk() : onErr());
+ },
+ };
+ const custom = issuedRunFrom(thenable);
+ assert.notEqual(custom, undefined, "a thenable is a run");
+ let customSettled = 0;
+ custom?.whenSettled(() => {
+ customSettled += 1;
+ });
+ assert.equal(customSettled, 0, "and it is pending until it is not");
+ parked.settle?.(false);
+ assert.equal(
+ customSettled,
+ 1,
+ "a run that ended by throwing has still ended",
+ );
+});
+
+test("a rejected run settles its hold rather than escaping", async () => {
+ // Neither arm says whether the lease may be given back, only that the run is not coming.
+ // An unobserved rejection here would also be an unhandled one.
+ const settled: string[] = [];
+ const rejected = issuedRunFrom(Promise.reject(new Error("model refused")));
+ rejected?.whenSettled(() => settled.push("rejected"));
+ const fulfilled = issuedRunFrom(Promise.resolve());
+ fulfilled?.whenSettled(() => settled.push("resolved"));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ assert.deepEqual(
+ settled.sort(),
+ ["rejected", "resolved"],
+ "both outcomes are an ended run",
+ );
+});
+
+test("the abort case is wired to the run, not to a clock", () => {
+ // The bar is the last place that has the run in hand -- the keeper lives in module scope and
+ // the runtime is only reachable through a hook -- so `startRun`'s own return value has to be
+ // handed over there. Pinned at both ends, since neither side is exercised by a unit test,
+ // and re-pinned against a deadline because a deadline here is the arming timeout coming back.
+ const bar = readFileSync(
+ new URL("../src/components/assistant-ui/thread.tsx", import.meta.url),
+ "utf8",
+ );
+ assert.match(
+ bar,
+ /return aui\.thread\(\)\.startRun\(/,
+ "the continuation bar no longer hands its started run back",
+ );
+ assert.match(
+ bar,
+ /watchAutoContinueRun\(\s*messageId,\s*runThreadId,\s*startContinuation\(\),?\s*\)/,
+ "the started run no longer reaches the keeper",
+ );
+
+ const probe = readFileSync(
+ new URL(
+ "../src/features/chat/utils/auto-continue-issued-run.ts",
+ import.meta.url,
+ ),
+ "utf8",
+ );
+ assert.match(
+ probe,
+ /typeof \(started as \{ then\?: unknown \}\)\.then !== "function"/,
+ "the declared type is void, so the shape has to be checked rather than assumed",
+ );
+ const wiredKeeper = readFileSync(
+ new URL(
+ "../src/features/chat/utils/auto-continue-run-keeper.ts",
+ import.meta.url,
+ ),
+ "utf8",
+ );
+ // Every timer, not just `setTimeout(`: a deadline spelled `setInterval` counts its own elapsed
+ // time and passed unnoticed. The renewal interval is NAMED, so a second one cannot pose as it.
+ const TIMERS =
+ /\b(?:setTimeout|setInterval|setImmediate|queueMicrotask|requestIdleCallback|requestAnimationFrame)\s*\(/g;
+ for (const [name, source, allowed] of [
+ ["auto-continue-issued-run.ts", probe, []],
+ [
+ "auto-continue-run-keeper.ts",
+ wiredKeeper,
+ ["setInterval(tick, AUTO_CONTINUE_LEASE_RENEW_MS)"],
+ ],
+ ] as const) {
+ assert.deepEqual(
+ source.match(TIMERS) ?? [],
+ allowed.map((call) => `${call.slice(0, call.indexOf("("))}(`),
+ `${name} decides on facts, not on elapsed time`,
+ );
+ for (const call of allowed) {
+ assert.ok(
+ source.includes(call),
+ `${name}'s only timer must be ${call}`,
+ );
+ }
+ }
+});
+
/**
* The same field the keeper reads in the app, owners and all.
*