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: stop opening a saved chat from re-running a Max Tokens reply (#11875)

Michael Han8a738c1a8f76
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 43efec3ebd7..440fb3122c6 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -300,6 +300,7 @@ import {
hasRenderableContent,
incompleteLabel,
type IncompleteReason,
+ noteRunStartedThisSession,
readIncompleteInfo,
resolveIncompleteReason,
readContinuationRequest,
@@ -8370,6 +8371,8 @@ export function createOpenAIStreamAdapter(
} satisfies ChatModelAdapter;
return {
async *run(args) {
+ // Only runs started here may auto-continue a Max Tokens cut.
+ noteRunStartedThisSession(args.unstable_assistantMessageId);
invalidateMinPRecoveries();
const preStreamThreadIds = preStreamRunThreadIdsForAdapter(
args.unstable_threadId,
diff --git a/studio/frontend/src/features/chat/utils/continuation.ts b/studio/frontend/src/features/chat/utils/continuation.ts
index 34bedcf9c42..36f2a1592f9 100644
--- a/studio/frontend/src/features/chat/utils/continuation.ts
+++ b/studio/frontend/src/features/chat/utils/continuation.ts
@@ -1030,6 +1030,26 @@ export function createAutoContinueLeaseKeeper({
};
}
+/** Assistant messages whose run this page started. Only these auto-continue: `spent` resets on
+ * reload, so a saved cut would otherwise re-run every time its chat is opened. */
+const startedThisSession = new Set<string>();
+
+/** Called by the chat adapter as each run starts, continuations included. */
+export function noteRunStartedThisSession(
+ messageId: string | null | undefined,
+): void {
+ if (messageId) {
+ startedThisSession.add(messageId);
+ }
+}
+
+/** Whether this page started the run that produced `messageId`. */
+export function runStartedThisSession(
+ messageId: string | null | undefined,
+): boolean {
+ return Boolean(messageId) && startedThisSession.has(messageId as string);
+}
+
/** Whether THIS message is the one to continue automatically. `shouldAutoContinue` answers about
* the turn and keeps saying yes after a message has been claimed, since the budget is per
* turn while the claim is per message, so rendering off the turn's answer alone showed a
@@ -1040,7 +1060,7 @@ export function shouldAutoContinueMessage(
key: string | null | undefined,
options: Parameters<typeof shouldAutoContinue>[2] = {},
): boolean {
- if (wasAutoContinued(messageId)) {
+ if (!runStartedThisSession(messageId) || wasAutoContinued(messageId)) {
return false;
}
return shouldAutoContinue(reason, key, options);
@@ -1052,6 +1072,7 @@ export function shouldAutoContinueMessage(
export function resetAutoContinue(key?: string): void {
if (key === undefined) {
spent.clear();
+ startedThisSession.clear();
tab.reset();
} else {
spent.delete(key);
diff --git a/studio/frontend/tests/chat-continuation.test.ts b/studio/frontend/tests/chat-continuation.test.ts
index eb50cb78418..a3a9351e218 100644
--- a/studio/frontend/tests/chat-continuation.test.ts
+++ b/studio/frontend/tests/chat-continuation.test.ts
@@ -31,6 +31,7 @@ const {
isRestart,
joinContinuation,
modeAllowsContinuation,
+ noteRunStartedThisSession,
readContinuationRequest,
readIncompleteInfo,
readTextThoughtSignature,
@@ -40,6 +41,7 @@ const {
recordAutoContinue,
rejectsAssistantPrefill,
resetAutoContinue,
+ runStartedThisSession,
wasAutoContinued,
shouldAutoContinue,
shouldAutoContinueMessage,
@@ -733,6 +735,7 @@ test("a claim is reported and cleared by a full reset", async () => {
test("a message already claimed stops reporting itself as continuing", async () => {
resetAutoContinue();
+ noteRunStartedThisSession("m1");
// The turn that fires it: nothing has claimed the message yet.
assert.equal(shouldAutoContinueMessage("m1", "length", "parent-1"), true);
await claimAutoContinue("m1", PANE);
@@ -750,11 +753,14 @@ test("a claim on one message does not silence another", async () => {
await claimAutoContinue("m1", PANE);
// The next round of the same turn is a new message with budget left, and continues.
recordAutoContinue("parent-1");
+ noteRunStartedThisSession("m2");
assert.equal(shouldAutoContinueMessage("m2", "length", "parent-1"), true);
});
test("a claimed message still honours the gates the turn itself fails", () => {
resetAutoContinue();
+ noteRunStartedThisSession("m1");
+ noteRunStartedThisSession("m2");
// Nothing about the claim resurrects a cut that was never automatic in the first place.
assert.equal(shouldAutoContinueMessage("m1", "cancelled", "parent-1"), false);
assert.equal(
@@ -763,6 +769,52 @@ test("a claimed message still honours the gates the turn itself fails", () => {
);
});
+// --- history ---------------------------------------------------------------------------
+// Opening a saved chat must not auto-continue its Max Tokens cut.
+
+test("a Max Tokens cut loaded from history is left to the Continue button", () => {
+ resetAutoContinue();
+ assert.equal(runStartedThisSession("saved-reply"), false);
+ assert.equal(shouldAutoContinueMessage("saved-reply", "length", "parent-1"), false);
+ // The manual button still offers it.
+ assert.equal(shouldAutoContinue("length", "parent-1"), true);
+ assert.equal(autoContinueCount("parent-1"), 0);
+});
+
+test("a Max Tokens cut from a run this page started still continues on its own", () => {
+ resetAutoContinue();
+ noteRunStartedThisSession("live-reply");
+ assert.equal(shouldAutoContinueMessage("live-reply", "length", "parent-1"), true);
+ // Each round is a new sibling, noted as it starts.
+ recordAutoContinue("parent-1");
+ noteRunStartedThisSession("round-2");
+ assert.equal(shouldAutoContinueMessage("round-2", "length", "parent-1"), true);
+});
+
+test("a missing message id never counts as started", () => {
+ resetAutoContinue();
+ noteRunStartedThisSession(undefined);
+ noteRunStartedThisSession(null);
+ noteRunStartedThisSession("");
+ assert.equal(runStartedThisSession(undefined), false);
+ assert.equal(runStartedThisSession(""), false);
+});
+
+test("a full reset forgets which runs this page started", () => {
+ noteRunStartedThisSession("live-reply");
+ resetAutoContinue();
+ assert.equal(runStartedThisSession("live-reply"), false);
+});
+
+test("every adapter run records its assistant message before it starts", () => {
+ const start = CHAT_ADAPTER.indexOf(" return {\n async *run(args) {");
+ assert.ok(start >= 0);
+ const wrapper = CHAT_ADAPTER.slice(start);
+ const noted = wrapper.indexOf("noteRunStartedThisSession(args.unstable_assistantMessageId)");
+ const delegated = wrapper.indexOf("yield* adapter.run(args)");
+ assert.ok(noted >= 0 && delegated >= 0 && noted < delegated);
+});
+
// --- cross-tab claim ------------------------------------------------------------------
// The module claim above is per TAB: each one loads its own copy of the module with its
// own empty set. Open the same saved thread twice with a `length` reply last and both