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.

86.4 KB
Raw Blame
// 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 assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";

import { readSrc, registerBundlerResolver } from "./helpers/kit.ts";

registerBundlerResolver();

const THREAD = readSrc("components/assistant-ui/thread.tsx");
const CHAT_ADAPTER = readSrc("features/chat/api/chat-adapter.ts");
const AUTO_CONTINUE_RUN_KEEPER = readSrc("features/chat/utils/auto-continue-run-keeper.ts");

const {
  AUTO_CONTINUE_CONTINUED_TTL_MS,
  AUTO_CONTINUE_LEASE_KEY,
  AUTO_CONTINUE_LEASE_RENEW_MS,
  AUTO_CONTINUE_LEASE_TTL_MS,
  AUTO_CONTINUE_LIMIT,
  autoContinueCount,
  createAutoContinueLeaseKeeper,
  createAutoContinueTab,
  budgetImpliesTruncation,
  hasRenderableContent,
  incompleteLabel,
  incompleteReasonAfterError,
  incompleteRemedy,
  isContinuableContent,
  isProviderReportedReason,
  isRestart,
  joinContinuation,
  modeAllowsContinuation,
  noteRunStartedThisSession,
  providerCompactionContinuationFields,
  readContinuationRequest,
  readIncompleteInfo,
  readTextThoughtSignature,
  resolveIncompleteReason,
  restoredAssistantStatus,
  claimAutoContinue,
  recordAutoContinue,
  rejectsAssistantPrefill,
  resetAutoContinue,
  runStartedThisSession,
  wasAutoContinued,
  shouldAutoContinue,
  shouldAutoContinueMessage,
  resumesExactly,
  stripContinuationOverlap,
} = await import("../src/features/chat/utils/continuation.ts");

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";

const PANE = "pane-1";

test("a token-exact continuation is appended verbatim", () => {
  assert.equal(
    joinContinuation(PARTIAL, " cover it with a damp cloth."),
    `${PARTIAL} cover it with a damp cloth.`,
  );
});

test("a repeated tail is dropped instead of stuttering", () => {
  const continuation = "warm the bowl and cover it with a damp cloth.";
  assert.equal(
    joinContinuation(PARTIAL, continuation),
    "There are three steps to proofing dough properly. First, warm the bowl and cover it with a damp cloth.",
  );
});

test("a short coincidental overlap is left alone", () => {
  const continuation = "and then wait.";
  assert.equal(
    stripContinuationOverlap("...warm the bowl and", continuation),
    continuation,
  );
});

test("a provider that ignores prefill and restarts replaces the partial", () => {
  const restart = `${PARTIAL} cover it with a damp cloth. Second, wait.`;
  assert.ok(isRestart(PARTIAL, restart));
  assert.equal(joinContinuation(PARTIAL, restart), restart);
});

test("mid-stream the restart check is deferred", () => {
  const restart = `${PARTIAL} cover it`;
  assert.equal(
    joinContinuation(PARTIAL, restart, { streaming: true }),
    `${PARTIAL}${restart.slice(PARTIAL.length)}`,
  );
});

test("a live yield is repaired like the terminal one, so Stop saves clean text", () => {
  // assistant-ui drops yields after abort, so the last live yield is what persists on Stop.
  assert.equal(
    joinContinuation(PARTIAL, "warm the bowl and cover it with"),
    `${PARTIAL} cover it with`,
  );

  const long = `${PARTIAL} ${Array.from(
    { length: 12 },
    (_, i) => `Step ${i} is to knead it for ${i} minutes without adding flour.`,
  ).join(" ")}`;
  const restart = `${long} Second, shape the loaf.`;
  assert.ok(long.length > 400);
  assert.equal(joinContinuation(long, restart), restart);
  assert.equal(
    joinContinuation(long, restart, { streaming: true }),
    `${long}${restart}`,
  );
});

test("a partial too short to judge is never treated as a restart", () => {
  assert.equal(isRestart("Sure!", "Sure! Here is the answer."), false);
  assert.equal(
    joinContinuation("Sure!", " Here is the answer."),
    "Sure! Here is the answer.",
  );
});

test("an empty partial yields the continuation alone", () => {
  assert.equal(joinContinuation("", "anything"), "anything");
});

test("an exhausted budget only implies truncation on the MLX route", () => {
  const capped = { maxTokens: 256, completionTokens: 256 };
  assert.equal(budgetImpliesTruncation({ isMlx: true, ...capped }), true);
  // Other providers report "length" themselves; only this one needs the inference.
  assert.equal(
    budgetImpliesTruncation({
      isMlx: false,
      maxTokens: 256,
      completionTokens: 260,
    }),
    false,
  );
  assert.equal(budgetImpliesTruncation({ isMlx: false, ...capped }), false);
  assert.equal(
    budgetImpliesTruncation({
      isMlx: true,
      maxTokens: 256,
      completionTokens: undefined,
    }),
    false,
  );
  assert.equal(
    budgetImpliesTruncation({
      isMlx: true,
      maxTokens: 256,
      completionTokens: 255,
    }),
    false,
  );
});

test("incomplete metadata round-trips and unknown reasons are ignored", () => {
  assert.deepEqual(
    readIncompleteInfo({ custom: { incomplete: { reason: "length" } } }),
    { reason: "length" },
  );
  assert.equal(
    readIncompleteInfo({ custom: { incomplete: { reason: "banana" } } }),
    null,
  );
  assert.equal(readIncompleteInfo(undefined), null);
  assert.equal(readIncompleteInfo({}), null);
});

test("every stop reason has a label", () => {
  assert.equal(incompleteLabel("length"), "Response hit the Max Tokens limit");
  assert.equal(incompleteLabel("cancelled"), "Response stopped");
  assert.equal(incompleteLabel("interrupted"), "Response interrupted");
  assert.equal(
    incompleteLabel("context_window"),
    "Response filled the model's context window",
  );
  assert.equal(
    incompleteLabel("empty"),
    "The model returned an empty response",
  );
});

test("text-like parts have to carry weight for a turn to count as rendered", () => {
  assert.equal(hasRenderableContent([]), false);
  assert.equal(hasRenderableContent([{ type: "text", text: "" }]), false);
  assert.equal(hasRenderableContent([{ type: "text", text: " \n\t" }]), false);
  assert.equal(hasRenderableContent([{ type: "text", text: "hi" }]), true);
  assert.equal(hasRenderableContent([{ type: "reasoning", text: "" }]), false);
  assert.equal(
    hasRenderableContent([{ type: "reasoning", text: "thought" }]),
    true,
  );
  // A turn that only called a tool or drew an image still answered.
  assert.equal(hasRenderableContent([{ type: "tool-call" }]), true);
  assert.equal(hasRenderableContent([{ type: "image" }]), true);
  assert.equal(
    hasRenderableContent([{ type: "text", text: "" }, { type: "source" }]),
    true,
  );
});

test("an empty turn is reported, and offers a retry rather than a resume", () => {
  assert.equal(incompleteRemedy("empty"), "Try again, or pick a different model");
  assert.equal(incompleteRemedy("cancelled"), null);
  assert.equal(incompleteRemedy("length"), null);
  assert.deepEqual(
    readIncompleteInfo({ custom: { incomplete: { reason: "empty" } } }),
    { reason: "empty" },
  );
});

test("a reloaded empty turn keeps its reason instead of reading as a Stop", () => {
  const metadata = { custom: { incomplete: { reason: "empty" as const } } };
  const status = restoredAssistantStatus(metadata);
  const stamped = readIncompleteInfo(metadata);
  assert.notEqual(status.type === "incomplete" && status.reason, "cancelled");

  const cancelled = status.type === "incomplete" && status.reason === "cancelled";
  const reason =
    cancelled && !isProviderReportedReason(stamped?.reason)
      ? "cancelled"
      : stamped?.reason;
  assert.equal(reason, "empty");
  assert.notEqual(incompleteRemedy(reason!), null);

  assert.equal(resolveIncompleteReason("cancelled", false), "cancelled");
});

test("the adapter marks a finish that rendered nothing", () => {
  const ending = CHAT_ADAPTER.slice(
    CHAT_ADAPTER.indexOf("const finalContent = ["),
  );
  const reason = ending.slice(0, ending.indexOf("yield {"));
  assert.match(reason, /hasRenderableContent\(finalContent\) \? null : "empty"/);
  assert.match(ending, /yield \{\s*content: finalContent,/);
});

test("a reply cut mid-quote is reported, and offers a retry rather than a resume", () => {
  assert.equal(incompleteLabel("quote_cut"), "This response may have ended early");
  assert.match(incompleteRemedy("quote_cut") ?? "", /may have emitted a special token/);
  assert.match(incompleteRemedy("quote_cut") ?? "", /< \|im_end\|>/);
  assert.deepEqual(
    readIncompleteInfo({ custom: { incomplete: { reason: "quote_cut" } } }),
    { reason: "quote_cut" },
  );
  assert.equal(shouldAutoContinue("quote_cut", "parent-1"), false);
  assert.deepEqual(
    restoredAssistantStatus({ custom: { incomplete: { reason: "quote_cut" } } }),
    { type: "incomplete", reason: "length" },
  );
});

test("the adapter stamps a cut the backend reported on a clean finish", () => {
  assert.match(
    CHAT_ADAPTER,
    /if \(chunk\.quote_cut\) \{\s*quoteCut = true;\s*continue;\s*\}/,
  );
  const ending = CHAT_ADAPTER.slice(
    CHAT_ADAPTER.indexOf("const finalIncompleteReason ="),
  );
  const reason = ending.slice(0, ending.indexOf("yield {"));
  assert.match(
    reason,
    /resolveIncompleteReason\(incompleteReason, contextWindowExceeded\) \?\?\s*\(quoteCut \? "quote_cut" : null\) \?\?/,
  );
});

test("the provider's own reason outranks every reason the client infers", () => {
  assert.equal(resolveIncompleteReason("length", true), "context_window");
  assert.equal(resolveIncompleteReason(null, true), "context_window");
  assert.equal(resolveIncompleteReason("cancelled", true), "context_window");
  assert.equal(resolveIncompleteReason("interrupted", true), "context_window");
  assert.equal(resolveIncompleteReason("length", false), "length");
  assert.equal(resolveIncompleteReason("cancelled", false), "cancelled");
  assert.equal(resolveIncompleteReason("interrupted", false), "interrupted");
  assert.equal(resolveIncompleteReason(null, false), null);
});

test("a provider-reported reason is the one a cancelled status cannot overrule", () => {
  assert.equal(isProviderReportedReason("context_window"), true);
  assert.equal(isProviderReportedReason("length"), false);
  assert.equal(isProviderReportedReason("cancelled"), false);
  assert.equal(isProviderReportedReason("interrupted"), false);
  assert.equal(isProviderReportedReason(null), false);
  assert.equal(isProviderReportedReason(undefined), false);
});

test("a window-exhausted turn is stamped apart from a Max Tokens cut", () => {
  resetAutoContinue();
  assert.equal(resolveIncompleteReason("length", false), "length");
  const reason = resolveIncompleteReason("length", true);
  assert.equal(reason, "context_window");
  assert.deepEqual(readIncompleteInfo({ custom: { incomplete: { reason } } }), {
    reason: "context_window",
  });
  assert.deepEqual(restoredAssistantStatus({ custom: { incomplete: { reason } } }), {
    type: "incomplete",
    reason: "length",
  });
});

test("a window-exhausted turn is never resumed automatically", () => {
  resetAutoContinue();
  assert.equal(
    shouldAutoContinue(resolveIncompleteReason("length", true), "parent-1"),
    false,
  );
  assert.equal(
    shouldAutoContinueMessage(
      "m1",
      resolveIncompleteReason("length", true),
      "parent-1",
    ),
    false,
  );
  assert.equal(autoContinueCount("parent-1"), 0);
});

test("the adapter latches the backend window-exhaustion event", () => {
  const adapter = readFileSync(
    new URL("../src/features/chat/api/chat-adapter.ts", import.meta.url),
    "utf8",
  );
  assert.match(
    adapter,
    /toolEvent\.type === "context_window_exceeded"[\s\S]{0,120}contextWindowExceeded = true/,
    "the backend event is no longer latched",
  );
  assert.match(
    adapter,
    /resolveIncompleteReason\(\s*incompleteReason,\s*contextWindowExceeded,?\s*\)/,
    "the latched signal no longer reaches the stamped reason",
  );
  assert.match(
    adapter,
    /incomplete: finalIncompleteReason\s*\?\s*\{ reason: finalIncompleteReason \}/,
    "the resolved reason no longer reaches the persisted metadata",
  );
  assert.match(
    adapter,
    /reason: resolveIncompleteReason\([\s\S]{0,400}contextWindowExceeded,\s*\)/,
    "the error path decides a reason without asking what the provider reported",
  );
  assert.match(
    adapter,
    /incomplete: \{\s*reason: resolveIncompleteReason\(\s*(?:\/\/[^\n]*\n\s*)*incompleteReason \?\?\s*\(\s*generationDecision === "durable" \?\s*"cancelled"\s*:\s*"interrupted"\),\s*contextWindowExceeded,\s*\)/,
    "an abort saves a bare cancelled again, losing the gate that names a walk-away and what the provider reported",
  );
  const handler = adapter.slice(
    adapter.indexOf('toolEvent.type === "context_window_exceeded"'),
    adapter.indexOf('toolEvent.type === "tool_output"'),
  );
  assert.ok(handler.length > 0, "the handler moved; this assertion reads nothing");
  assert.match(
    handler,
    /yield \{[\s\S]*custom: liveCustom\(\),/,
    "the latched signal is no longer published when it arrives",
  );
  assert.doesNotMatch(
    handler,
    /\.length > 0/,
    "publishing the reason depends on renderable content again",
  );
});

test("only the cut no continuation can undo carries a way out", () => {
  assert.equal(incompleteRemedy("length"), null);
  assert.equal(incompleteRemedy("cancelled"), null);
  assert.equal(incompleteRemedy("interrupted"), null);
  assert.equal(
    incompleteRemedy("context_window"),
    "Start a new chat, or shorten this one, to keep going",
  );
});

test("a tool-using turn that fills the window is the case the bar must not miss", () => {
  const content = [
    { type: "tool-call", toolName: "web_search", toolCallId: "t1", args: {} },
    { type: "text", text: "Based on those results, the three main causes are, first, the" },
  ];
  assert.equal(isContinuableContent(content), false);
  const reason = resolveIncompleteReason("length", true);
  assert.equal(reason, "context_window");
  assert.notEqual(incompleteRemedy(reason!), null);
});

test("the bar offers the way out in place of a Continue that cannot help", () => {
  const thread = readFileSync(
    new URL("../src/components/assistant-ui/thread.tsx", import.meta.url),
    "utf8",
  );
  assert.match(
    thread,
    /const remedy = reason \? incompleteRemedy\(reason\) : null/,
    "the bar no longer asks whether resuming can help",
  );
  assert.match(
    thread,
    /if \(!reason \|\| \(!remedy && !resumable\)\) \{\n\s*return null;/,
    "the way out is gated on the turn being resumable again",
  );
  assert.match(
    thread,
    /cancelled && !isProviderReportedReason\(stamped\?\.reason\)/,
    "a cancelled status overrules the provider's own reason again",
  );
  assert.match(
    thread,
    /\{incompleteLabel\(reason\)\}\.\{remedy \? ` \$\{remedy\}\.` : ""\}/,
    "the way out is no longer rendered beside the reason",
  );
  assert.match(
    thread,
    /\{remedy \? null : \([\s\S]{0,700}<span>Resume<\/span>\n\s*<\/Button>/,
    "the resume button is offered again for a cut it cannot help",
  );
});

test("a continuation request is read only when it carries text", () => {
  assert.deepEqual(
    readContinuationRequest({
      custom: { unslothContinuation: { partial: "half an answer" } },
    }),
    { partial: "half an answer" },
  );
  assert.equal(
    readContinuationRequest({
      custom: { unslothContinuation: { partial: "" } },
    }),
    null,
  );
  assert.equal(readContinuationRequest({}), null);
  assert.equal(readContinuationRequest(undefined), null);
});

test("a continuation carries a complete provider compaction tuple", () => {
  const fields = {
    providerCompaction: {
      type: "compaction",
      content: "summary",
      encrypted_content: "opaque",
    },
    providerCompactionAfterToolCalls: 0,
    providerCompactionProviderType: "anthropic",
    providerCompactionModelId: "claude-opus-4-7",
    providerCompactionConnectionKey: "v1:connection-a",
  };
  assert.deepEqual(
    providerCompactionContinuationFields({ custom: fields }),
    fields,
  );
  assert.deepEqual(
    readContinuationRequest({
      custom: {
        unslothContinuation: { partial: "half an answer", ...fields },
      },
    }),
    { partial: "half an answer", ...fields },
  );
  assert.deepEqual(
    readContinuationRequest({
      custom: {
        unslothContinuation: {
          partial: "half an answer",
          ...fields,
          providerCompactionModelId: undefined,
        },
      },
    }),
    { partial: "half an answer" },
  );
  assert.match(
    THREAD,
    /\.\.\.providerCompactionContinuationFields\(metadata\)/,
  );
  assert.match(
    CHAT_ADAPTER,
    /const continuationCompaction = continuation[\s\S]*providerCompactionForTarget\([\s\S]*content: continuationCompaction/,
  );
});

test("a turn that called a tool cannot be continued", () => {
  assert.equal(
    isContinuableContent([
      { type: "text", text: "Looking that up." },
      { type: "tool-call", toolCallId: "c1", toolName: "web_search" },
    ]),
    false,
  );
});

test("text and reasoning parts are continuable, empty text is not", () => {
  assert.equal(
    isContinuableContent([
      { type: "reasoning", text: "hmm" },
      { type: "text", text: "The answer is" },
    ]),
    true,
  );
  assert.equal(
    isContinuableContent([{ type: "reasoning", text: "hmm" }]),
    false,
  );
  assert.equal(isContinuableContent([{ type: "text", text: "" }]), false);
  assert.equal(isContinuableContent(undefined), false);
});

test("providers that reject a trailing assistant turn get the instruction path", () => {
  // Anthropic, Gemini and Mistral each reject or cannot express a trailing assistant prefill.
  assert.equal(rejectsAssistantPrefill("anthropic"), true);
  assert.equal(rejectsAssistantPrefill("gemini"), true);
  assert.equal(rejectsAssistantPrefill("mistral"), true);
  assert.equal(rejectsAssistantPrefill("openai"), false);
  assert.equal(rejectsAssistantPrefill(undefined), false);
});

test("modes that answer from scratch do not offer Continue", () => {
  const plain = {
    fromAudioInput: false,
    audioOutputModel: false,
  };
  assert.equal(modeAllowsContinuation(plain), true);
  assert.equal(
    modeAllowsContinuation({ ...plain, fromAudioInput: true }),
    false,
  );
  assert.equal(
    modeAllowsContinuation({ ...plain, audioOutputModel: true }),
    false,
  );
});

test("the overlap repair can eat a legitimate repeat, so local output skips it", () => {
  // Local backends resume at the exact token boundary, so only external providers get repair.
  const partial = "Ranking them, the clear winner is the second result";
  const continuation = "the second result held up best under load.";
  assert.equal(
    stripContinuationOverlap(partial, continuation),
    " held up best under load.",
  );
  assert.equal(
    `${partial} ${continuation}`,
    "Ranking them, the clear winner is the second result the second result held up best under load.",
  );
});

test("a continuation carries the Gemini signature of the turn it resumes", () => {
  assert.equal(
    readTextThoughtSignature([
      { type: "text", text: "first", _google_thought_signature: "SIG-A" },
      { type: "text", text: "second", _google_thought_signature: "SIG-B" },
    ]),
    "SIG-B",
  );
  assert.equal(
    readTextThoughtSignature([{ type: "reasoning", text: "hmm" }]),
    undefined,
  );
  assert.equal(readTextThoughtSignature([{ type: "text", text: "x" }]), undefined);
  assert.equal(readTextThoughtSignature(undefined), undefined);

  assert.deepEqual(
    readContinuationRequest({
      custom: { unslothContinuation: { partial: "half", thoughtSignature: "SIG" } },
    }),
    { partial: "half", thoughtSignature: "SIG" },
  );
  assert.deepEqual(
    readContinuationRequest({
      custom: {
        unslothContinuation: {
          partial: "half",
          answerParts: [
            { text: "half" },
            { text: "", thoughtSignature: "SIG-END" },
          ],
        },
      },
    }),
    {
      partial: "half",
      answerParts: [
        { text: "half" },
        { text: "", thoughtSignature: "SIG-END" },
      ],
    },
  );
  // An unsigned turn stays unsigned rather than gaining an empty key.
  assert.deepEqual(
    readContinuationRequest({
      custom: { unslothContinuation: { partial: "half" } },
    }),
    { partial: "half" },
  );
});

test("only the servers sent the flags resume exactly", () => {
  for (const providerType of ["vllm", "llama_cpp"]) {
    assert.equal(resumesExactly(providerType), true, providerType);
  }
  for (const providerType of [
    "ollama",
    "custom",
    "openai",
    "anthropic",
    undefined,
  ]) {
    assert.equal(resumesExactly(providerType), false, String(providerType));
  }
});

test("citations appended for display do not block Continue", () => {
  assert.equal(
    isContinuableContent([
      { type: "text", text: "The answer begins" },
      { type: "source", sourceType: "url", id: "s1", url: "https://x" },
    ]),
    true,
  );
});

test("a Max Tokens cut resumes on its own", () => {
  resetAutoContinue();
  assert.equal(shouldAutoContinue("length", "parent-1"), true);
});

test("pressing Stop is never undone by an automatic resume", () => {
  resetAutoContinue();
  assert.equal(shouldAutoContinue("cancelled", "parent-1"), false);
});

test("a dropped connection still asks, rather than retrying silently", () => {
  resetAutoContinue();
  assert.equal(shouldAutoContinue("interrupted", "parent-1"), false);
});

test("automatic resumes are bounded, then the bar comes back", () => {
  resetAutoContinue();
  for (let round = 0; round < AUTO_CONTINUE_LIMIT; round += 1) {
    assert.equal(shouldAutoContinue("length", "parent-1"), true);
    recordAutoContinue("parent-1");
  }
  assert.equal(autoContinueCount("parent-1"), AUTO_CONTINUE_LIMIT);
  assert.equal(shouldAutoContinue("length", "parent-1"), false);
});

test("the budget is per turn, so a later turn is not punished for an earlier one", () => {
  resetAutoContinue();
  for (let round = 0; round < AUTO_CONTINUE_LIMIT; round += 1) {
    recordAutoContinue("parent-1");
  }
  assert.equal(shouldAutoContinue("length", "parent-1"), false);
  assert.equal(shouldAutoContinue("length", "parent-2"), true);
});

test("the count is keyed on the parent, which every round of one turn shares", () => {
  resetAutoContinue();
  // Continuations are siblings with new ids, so the budget is keyed on the parent.
  recordAutoContinue("parent-1");
  recordAutoContinue("parent-1");
  assert.equal(autoContinueCount("parent-1"), 2);
});

test("a turn with no parent is never resumed automatically", () => {
  resetAutoContinue();
  assert.equal(shouldAutoContinue("length", null), false);
});

test("a turn whose own fit was refused is never resumed automatically", () => {
  resetAutoContinue();
  assert.equal(
    shouldAutoContinue("length", "parent-1", { fits: false }),
    false,
  );
});

test("a turn whose fit only missed the reply reserve is not resumed either", () => {
  resetAutoContinue();
  assert.equal(
    shouldAutoContinue("length", "parent-1", { fits: false }),
    false,
  );
});

test("a partial that already fills the budget is not resumed", () => {
  resetAutoContinue();
  assert.equal(
    shouldAutoContinue("length", "parent-1", {
      partialTokens: 3648,
      promptTarget: 3648,
    }),
    false,
  );
  assert.equal(
    shouldAutoContinue("length", "parent-1", {
      partialTokens: 400,
      promptTarget: 3648,
    }),
    true,
  );
});

test("an unknown fit does not block resuming", () => {
  resetAutoContinue();
  assert.equal(shouldAutoContinue("length", "parent-1", {}), true);
  assert.equal(
    shouldAutoContinue("length", "parent-1", { fits: true }),
    true,
  );
});

test("a message is claimed for automatic continuation exactly once", async () => {
  resetAutoContinue();
  assert.equal(await claimAutoContinue("m1", PANE), "started");
  assert.equal(await claimAutoContinue("m1", PANE), "skipped");
  assert.equal(await claimAutoContinue("m1", PANE), "skipped");
});

test("the claim survives a remount, which a component ref did not", async () => {
  resetAutoContinue();
  assert.equal(await claimAutoContinue("m1", PANE), "started");
  assert.equal(shouldAutoContinue("length", "parent-1"), true);
  assert.equal(await claimAutoContinue("m1", PANE), "skipped");
});

test("claims are tracked per message", async () => {
  resetAutoContinue();
  assert.equal(await claimAutoContinue("m1", PANE), "started");
  assert.equal(await claimAutoContinue("m2", PANE), "started");
  assert.equal(await claimAutoContinue("m1", PANE), "skipped");
});

test("a missing message id is refused rather than claimed", async () => {
  resetAutoContinue();
  assert.equal(await claimAutoContinue(null, PANE), "skipped");
  assert.equal(await claimAutoContinue(undefined, PANE), "skipped");
  assert.equal(await claimAutoContinue("", PANE), "skipped");
});

test("a claim is reported and cleared by a full reset", async () => {
  resetAutoContinue();
  assert.equal(wasAutoContinued("m1"), false);
  await claimAutoContinue("m1", PANE);
  assert.equal(wasAutoContinued("m1"), true);
  resetAutoContinue();
  assert.equal(wasAutoContinued("m1"), false);
  assert.equal(await claimAutoContinue("m1", PANE), "started");
});

test("a message already claimed stops reporting itself as continuing", async () => {
  resetAutoContinue();
  noteRunStartedThisSession("m1");
  assert.equal(shouldAutoContinueMessage("m1", "length", "parent-1"), true);
  await claimAutoContinue("m1", PANE);
  recordAutoContinue("parent-1");

  assert.equal(shouldAutoContinue("length", "parent-1"), true);
  assert.equal(shouldAutoContinueMessage("m1", "length", "parent-1"), false);
});

test("a claim on one message does not silence another", async () => {
  resetAutoContinue();
  await claimAutoContinue("m1", PANE);
  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");
  assert.equal(shouldAutoContinueMessage("m1", "cancelled", "parent-1"), false);
  assert.equal(
    shouldAutoContinueMessage("m2", "length", "parent-1", { fits: false }),
    false,
  );
});

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);
  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);
  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: each tab has its own module scope, so two tabs could both claim one message.

function storageFake(
  onSet?: (store: Map<string, string>) => void,
  onGet?: (store: Map<string, string>) => void,
) {
  const store = new Map<string, string>();
  return {
    store,
    storage: {
      getItem: (key: string) => {
        const value = store.get(key) ?? null;
        // After the value is taken, so the caller keeps a stale snapshot; that staleness is the bug.
        onGet?.(store);
        return value;
      },
      setItem: (key: string, value: string) => {
        store.set(key, value);
        onSet?.(store);
      },
      removeItem: (key: string) => {
        store.delete(key);
      },
    },
  };
}

function startedARun(outcome: unknown): boolean {
  return outcome === "started" || outcome === true;
}

function lockManagerFake() {
  const tails = new Map<string, Promise<unknown>>();
  return {
    request<T>(name: string, callback: () => T | Promise<T>): Promise<T> {
      const tail = tails.get(name) ?? Promise.resolve();
      const run = tail.then(() => callback());
      tails.set(
        name,
        run.then(
          () => undefined,
          () => undefined,
        ),
      );
      return run;
    },
  };
}

test("a second tab with the same thread open does not start its own continuation", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const first = createAutoContinueTab({ storage, locks });
  const second = createAutoContinueTab({ storage, locks });
  const now = 1_000;

  assert.equal(await first.claim("m1", { now }), "started");
  assert.equal(await second.claim("m1", { now }), "held-elsewhere");
  assert.equal(second.claimed("m1", { now }), true);
});

test("a lease covers only the message it was taken for", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const first = createAutoContinueTab({ storage, locks });
  const second = createAutoContinueTab({ storage, locks });
  const now = 1_000;

  assert.equal(await first.claim("m1", { now }), "started");
  assert.equal(await second.claim("m2", { now }), "started");
});

test("two tabs claiming at once leave exactly one winner", async () => {
  // Write-then-read-back is not a compare-and-swap; only the lock serializes the two tabs.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const first = createAutoContinueTab({ storage, locks });
  const second = createAutoContinueTab({ storage, locks });
  const now = 1_000;

  const outcomes = await Promise.all([
    first.claim("m1", { now }),
    second.claim("m1", { now }),
  ]);
  assert.equal(outcomes.filter(startedARun).length, 1);
  assert.equal(
    outcomes.filter((outcome) => outcome === "held-elsewhere").length,
    1,
  );
});

test("a tab that reads the slot free does not win it behind another tab's back", async () => {
  const now = 1_000;
  let intruder: (() => unknown) | null = null;
  let secondOutcome: unknown;
  let reads = 0;
  const { storage } = storageFake(undefined, () => {
    reads += 1;
    if (reads !== 2) {
      return;
    }
    const run = intruder;
    intruder = null;
    if (run) {
      secondOutcome = run();
    }
  });
  const locks = lockManagerFake();
  const first = createAutoContinueTab({ storage, locks });
  const second = createAutoContinueTab({ storage, locks });

  intruder = () => second.claim("m1", { now });
  const outcomes = [await first.claim("m1", { now }), await secondOutcome];
  assert.equal(
    outcomes.filter(startedARun).length,
    1,
    "exactly one tab may start the run",
  );
});

test("a claim that loses says so, rather than just failing", async () => {
  // `held-elsewhere` and `skipped` need opposite UI, so they cannot both be a bare false.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const winner = createAutoContinueTab({ storage, locks });
  const loser = createAutoContinueTab({ storage, locks });
  const now = 1_000;

  assert.equal(startedARun(await winner.claim("m1", { now })), true);
  const lost = await loser.claim("m1", { now });
  const replay = await winner.claim("m1", { now });
  assert.notEqual(lost, replay);
  assert.equal(lost, "held-elsewhere");
  assert.equal(replay, "skipped");
  const inFlight = winner.claim("m2", { now });
  assert.equal(await winner.claim("m2", { now }), "skipped");
  assert.equal(await inFlight, "started");
});

test("two tabs writing in the same tick leave exactly one winner", async () => {
  // No lock manager (older browsers, webviews): only write-then-read-back remains.
  const now = 1_000;
  let interleave = true;
  const { storage } = storageFake((store) => {
    if (!interleave) {
      return;
    }
    interleave = false;
    store.set(
      AUTO_CONTINUE_LEASE_KEY,
      JSON.stringify({
        m1: { token: "other-tab", expires: now + AUTO_CONTINUE_LEASE_TTL_MS },
      }),
    );
  });
  const first = createAutoContinueTab({ storage, locks: null });

  assert.equal(await first.claim("m1", { now }), "held-elsewhere");
  assert.equal(
    await first.claim("m1", { now: now + AUTO_CONTINUE_LEASE_TTL_MS + 1 }),
    "started",
  );
});

test("a browser with no lock manager still continues", async () => {
  const { storage } = storageFake();
  const only = createAutoContinueTab({ storage, locks: null });
  assert.equal(await only.claim("m1"), "started");
  assert.equal(await only.claim("m1"), "skipped");
});

test("a lock manager that refuses the request does not block the claim", async () => {
  const { storage } = storageFake();
  const angryLocks = {
    request<T>(): Promise<T> {
      return Promise.reject(new Error("NotSupportedError"));
    },
  };
  const tab = createAutoContinueTab({ storage, locks: angryLocks });
  assert.equal(await tab.claim("m1"), "started");
  assert.equal(
    await createAutoContinueTab({ storage }).claim("m1"),
    "held-elsewhere",
  );
});

test("a tab with no storage keeps the claim it always had", async () => {
  // No storage seam: falls back to module scope and must never refuse the single tab.
  const only = createAutoContinueTab({ storage: null });
  assert.equal(await only.claim("m1"), "started");
  assert.equal(await only.claim("m1"), "skipped");
  assert.equal(only.claimed("m1"), true);
});

test("storage that throws is no worse than no storage", async () => {
  const angry = {
    getItem(): string | null {
      throw new Error("SecurityError");
    },
    setItem(): void {
      throw new Error("QuotaExceededError");
    },
    removeItem(): void {
      throw new Error("SecurityError");
    },
  };
  const tab = createAutoContinueTab({ storage: angry });
  assert.equal(await tab.claim("m1"), "started");
  assert.equal(await tab.claim("m1"), "skipped");
  assert.equal(
    await createAutoContinueTab({ storage: angry }).claim("m1"),
    "started",
  );
});

test("a lease lapses, so a tab that died mid-run does not wedge the message", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const winner = createAutoContinueTab({ storage, locks });
  const later = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(await winner.claim("m1", { now: start }), "started");
  assert.equal(
    await later.claim("m1", { now: start + AUTO_CONTINUE_LEASE_TTL_MS - 1 }),
    "held-elsewhere",
  );
  assert.equal(
    await later.claim("m1", { now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1 }),
    "started",
  );
});

test("a running continuation keeps its lease past the TTL", async () => {
  // The holder renews while its run is live, so the TTL only measures silence.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const running = createAutoContinueTab({ storage, locks });
  const other = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(
    startedARun(await running.claim("m1", { now: start, holder: PANE })),
    true,
  );
  for (let tick = 1; tick <= 3; tick += 1) {
    await running.renew("m1", PANE, {
      now: start + tick * AUTO_CONTINUE_LEASE_RENEW_MS,
    });
  }
  const past =
    start + AUTO_CONTINUE_LEASE_TTL_MS + AUTO_CONTINUE_LEASE_RENEW_MS;
  assert.equal(await other.claim("m1", { now: past }), "held-elsewhere");
});

test("a finished run leaves the message continued, not free again", async () => {
  // Released on any terminal state so the full TTL means only a crash; marked done, not handed back.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const running = createAutoContinueTab({ storage, locks });
  const other = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(
    await running.claim("m1", { now: start, holder: PANE }),
    "started",
  );
  await running.release("m1", PANE, { now: start });
  assert.equal(
    await other.claim("m1", { now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1 }),
    "held-elsewhere",
  );
  assert.equal(
    await other.claim("m1", {
      now: start + AUTO_CONTINUE_CONTINUED_TTL_MS - 1,
    }),
    "held-elsewhere",
  );
  await running.renew("m1", PANE, { now: start + AUTO_CONTINUE_LEASE_TTL_MS });
  assert.equal(
    await createAutoContinueTab({ storage, locks }).claim("m1", {
      now: start + AUTO_CONTINUE_LEASE_TTL_MS + 2,
    }),
    "held-elsewhere",
  );
  assert.equal(
    await createAutoContinueTab({ storage, locks }).claim("m1", {
      now: start + AUTO_CONTINUE_CONTINUED_TTL_MS + 1,
    }),
    "started",
  );
});

test("a stale tab cannot take a message back once it has been continued", async () => {
  // A stale tab records nothing locally after a refusal, so storage alone must prevent a second run.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const winner = createAutoContinueTab({ storage, locks });
  const stale = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(await winner.claim("m1", { now: start, holder: PANE }), "started");
  assert.equal(await stale.claim("m1", { now: start + 1 }), "held-elsewhere");
  await winner.release("m1", PANE, { now: start + 60_000 });
  assert.equal(
    stale.claimed("m1", { now: start + 60_000 + AUTO_CONTINUE_LEASE_TTL_MS }),
    true,
  );
  assert.equal(
    await stale.claim("m1", {
      now: start + 60_000 + AUTO_CONTINUE_LEASE_TTL_MS,
    }),
    "held-elsewhere",
  );
});

test("a tab that died mid-run still hands the message back", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const killed = createAutoContinueTab({ storage, locks });
  const survivor = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(await killed.claim("m1", { now: start, holder: PANE }), "started");
  assert.equal(
    await survivor.claim("m1", { now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1 }),
    "started",
  );
});

/**
 * React runs child effects before parent ones, so the next claim lands before the keeper
 * sees the run end; `release` must name only the message whose run ended.
 */
async function sequentialRounds(
  locks: ReturnType<typeof lockManagerFake> | null,
) {
  const { storage } = storageFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(
    startedARun(await tab.claim("round-1", { now: start, holder: PANE })),
    true,
  );
  assert.equal(
    startedARun(await tab.claim("round-2", { now: start, holder: PANE })),
    true,
  );
  await tab.release("round-1", PANE, { now: start });

  for (let tick = 1; tick <= 3; tick += 1) {
    await tab.renew("round-2", PANE, {
      now: start + tick * AUTO_CONTINUE_LEASE_RENEW_MS,
    });
  }
  const past =
    start + AUTO_CONTINUE_LEASE_TTL_MS + AUTO_CONTINUE_LEASE_RENEW_MS;
  assert.equal(
    await otherTab.claim("round-2", { now: past }),
    "held-elsewhere",
    "the round that just started keeps its lease",
  );
  assert.equal(
    await otherTab.claim("round-1", {
      now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1,
    }),
    "held-elsewhere",
  );
}

test("the next round keeps its lease when the last one is released", async () => {
  await sequentialRounds(null);
});

test("the next round keeps its lease with a lock manager in the way", async () => {
  await sequentialRounds(lockManagerFake());
});

function runSignalFake(running: Set<string>) {
  const listeners = new Set<() => void>();
  return {
    signal: {
      isRunning: (threadId: string) => running.has(threadId),
      subscribe: (onChange: () => void) => {
        listeners.add(onChange);
        return () => listeners.delete(onChange);
      },
    },
    change: () => {
      for (const listener of [...listeners]) {
        listener();
      }
    },
  };
}

test("a run on a background thread keeps its lease while the user reads another chat", async () => {
  // Switching chats does not stop a generation, so the lease must not follow the selected thread.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const start = 1_000;
  let clock = start;
  const pending: Promise<void>[] = [];

  const running = new Set<string>();
  const runs = runSignalFake(running);
  const keeper = createAutoContinueLeaseKeeper({
    signal: runs.signal,
    renew: (messageId, holder, now) => {
      pending.push(tab.renew(messageId, holder, { now }));
    },
    release: (messageId, holder, now) => {
      pending.push(tab.release(messageId, holder, { now }));
    },
    now: () => clock,
  });

  assert.equal(
    startedARun(await tab.claim("a-m1", { now: start, holder: "thread-A" })),
    true,
  );
  keeper.hold("a-m1", "thread-A");
  running.add("thread-A");
  runs.change();

  for (let tick = 1; tick <= 5; tick += 1) {
    clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
    keeper.tick();
  }
  await Promise.all(pending);

  const past =
    start + AUTO_CONTINUE_LEASE_TTL_MS + AUTO_CONTINUE_LEASE_RENEW_MS;
  assert.equal(
    await otherTab.claim("a-m1", { now: past }),
    "held-elsewhere",
    "the background run keeps the message it is still writing",
  );

  clock = past;
  running.delete("thread-A");
  runs.change();
  await Promise.all(pending);
  assert.equal(
    await otherTab.claim("a-m1", {
      now: past + AUTO_CONTINUE_LEASE_TTL_MS + 1,
    }),
    "held-elsewhere",
    "the message was continued, so nobody continues it again",
  );
  assert.equal(keeper.held(), 0);
});

test("a hold waits for its own run, not the one already in flight", async () => {
  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 keeper = createAutoContinueLeaseKeeper({
    signal: runs.signal,
    renew: (messageId, holder, now) => {
      pending.push(tab.renew(messageId, holder, { now }));
    },
    release: (messageId, holder, now) => {
      pending.push(tab.release(messageId, holder, { now }));
    },
    now: () => clock,
  });

  await tab.claim("round-2", { now: start, holder: "thread-A" });
  keeper.hold("round-2", "thread-A");

  running.delete("thread-A");
  runs.change();
  running.add("thread-A");
  runs.change();
  for (let tick = 1; tick <= 5; tick += 1) {
    clock = start + tick * AUTO_CONTINUE_LEASE_RENEW_MS;
    keeper.tick();
  }
  await Promise.all(pending);
  const past =
    start + AUTO_CONTINUE_LEASE_TTL_MS + AUTO_CONTINUE_LEASE_RENEW_MS;
  assert.equal(await otherTab.claim("round-2", { now: past }), "held-elsewhere");
});

test("a hold keeps its lease while its run is still in preflight", async () => {
  // Preflight (settings pairing, waitForModelReady) is unbounded, so the hold has no arming deadline.
  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 keeper = createAutoContinueLeaseKeeper({
    signal: runs.signal,
    renew: (messageId, holder, now) => {
      pending.push(tab.renew(messageId, holder, { now }));
    },
    release: () => {
      assert.fail("a hold that never armed has nothing to give back");
    },
    now: () => clock,
  });

  await tab.claim("a-m1", { now: start, holder: "thread-A" });
  keeper.hold("a-m1", "thread-A");

  for (let tick = 1; tick <= 8; 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("a-m1", { now: clock }),
    "held-elsewhere",
    "nobody else may take a message this tab is still about to continue",
  );

  running.add("thread-A");
  runs.change();
  clock += AUTO_CONTINUE_LEASE_RENEW_MS;
  keeper.tick();
  await Promise.all(pending);
  assert.equal(
    await otherTab.claim("a-m1", {
      now: clock + AUTO_CONTINUE_LEASE_TTL_MS - 1,
    }),
    "held-elsewhere",
  );
});

test("one compare pane finishing does not release the other pane's lease", async () => {
  // Compare mode runs two runtimes in one tab; release must only drop the finished pane's lease.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const compareTab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const base = "pane-base";
  const lora = "pane-lora";
  const start = 1_000;

  assert.equal(
    startedARun(
      await compareTab.claim("base-m1", { now: start, holder: base }),
    ),
    true,
  );
  assert.equal(
    startedARun(
      await compareTab.claim("lora-m1", { now: start, holder: lora }),
    ),
    true,
  );

  await compareTab.release("base-m1", base, { now: start });

  for (let tick = 1; tick <= 3; tick += 1) {
    await compareTab.renew("lora-m1", lora, {
      now: start + tick * AUTO_CONTINUE_LEASE_RENEW_MS,
    });
  }
  const past =
    start + AUTO_CONTINUE_LEASE_TTL_MS + AUTO_CONTINUE_LEASE_RENEW_MS;
  assert.equal(
    await otherTab.claim("lora-m1", { now: past }),
    "held-elsewhere",
    "the still-running pane keeps its message",
  );
  assert.equal(
    await otherTab.claim("base-m1", {
      now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1,
    }),
    "held-elsewhere",
  );
});

test("a renewal touches this tab's leases and nobody else's", async () => {
  const { storage, store } = storageFake();
  const locks = lockManagerFake();
  const mine = createAutoContinueTab({ storage, locks });
  const theirs = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  await mine.claim("m1", { now: start, holder: PANE });
  await theirs.claim("m2", { now: start, holder: PANE });
  await mine.renew("m1", PANE, { now: start + AUTO_CONTINUE_LEASE_RENEW_MS });

  const leases = JSON.parse(store.get(AUTO_CONTINUE_LEASE_KEY) ?? "{}");
  assert.equal(
    leases.m1.expires,
    start + AUTO_CONTINUE_LEASE_RENEW_MS + AUTO_CONTINUE_LEASE_TTL_MS,
  );
  assert.equal(leases.m2.expires, start + AUTO_CONTINUE_LEASE_TTL_MS);
});

test("lapsed leases are pruned rather than accumulating", async () => {
  const { storage, store } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  await tab.claim("m1", { now: start });
  await createAutoContinueTab({ storage, locks }).claim("m2", {
    now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1,
  });
  const leases = JSON.parse(store.get(AUTO_CONTINUE_LEASE_KEY) ?? "{}");
  assert.deepEqual(Object.keys(leases), ["m2"]);
});

test("reloading one tab does not fire a second continuation", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const before = createAutoContinueTab({ storage, locks });
  const start = 1_000;
  assert.equal(await before.claim("m1", { now: start }), "started");

  const after = createAutoContinueTab({ storage, locks });
  assert.equal(
    await after.claim("m1", { now: start + 5_000 }),
    "held-elsewhere",
  );
  assert.equal(after.claimed("m1", { now: start + 5_000 }), true);
});

test("a reset gives back this tab's lease and leaves other tabs alone", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const mine = createAutoContinueTab({ storage, locks });
  const theirs = createAutoContinueTab({ storage, locks });
  const now = 1_000;

  assert.equal(await mine.claim("m1", { now }), "started");
  mine.reset();
  assert.equal(await mine.claim("m1", { now }), "started");

  assert.equal(await theirs.claim("m2", { now }), "started");
  mine.reset();
  assert.equal(
    await createAutoContinueTab({ storage, locks }).claim("m2", { now }),
    "held-elsewhere",
  );
});

test("a stored lease survives a full reset by the module, being another tab's", async () => {
  const { storage, store } = storageFake();
  const locks = lockManagerFake();
  await createAutoContinueTab({ storage, locks }).claim("m1", { now: 1_000 });
  resetAutoContinue();
  assert.equal(
    await createAutoContinueTab({ storage, locks }).claim("m1", { now: 1_000 }),
    "held-elsewhere",
  );
  assert.equal(store.has(AUTO_CONTINUE_LEASE_KEY), true);
});

test("a malformed lease record is ignored rather than blocking", async () => {
  const { storage, store } = storageFake();
  store.set(AUTO_CONTINUE_LEASE_KEY, "not json at all");
  assert.equal(await createAutoContinueTab({ storage }).claim("m1"), "started");

  const { storage: other, store: otherStore } = storageFake();
  otherStore.set(AUTO_CONTINUE_LEASE_KEY, JSON.stringify({ m1: { token: 7 } }));
  assert.equal(
    await createAutoContinueTab({ storage: other }).claim("m1"),
    "started",
  );
});

test("a hold whose run never starts holds its lease for the life of the tab", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const running = new Set<string>();
  const pending: Promise<unknown>[] = [];
  const start = 1_000;
  let clock = start;
  const keeper = createAutoContinueLeaseKeeper({
    signal: {
      isRunning: (threadId: string) => running.has(threadId),
      subscribe: () => () => {},
    },
    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,
  });

  assert.equal(
    await tab.claim("m1", { now: start, holder: "thread-A" }),
    "started",
  );
  keeper.hold("m1", "thread-A");
  for (
    let day = 1;
    day <= (3 * 86_400_000) / AUTO_CONTINUE_LEASE_RENEW_MS;
    day += 1
  ) {
    clock = start + day * AUTO_CONTINUE_LEASE_RENEW_MS;
    keeper.tick();
  }
  await Promise.all(pending);

  assert.equal(keeper.held(), 1, "nothing drops a pending hold, by design");
  assert.equal(
    await otherTab.claim("m1", { now: clock + AUTO_CONTINUE_LEASE_TTL_MS - 1 }),
    "held-elsewhere",
    "three days on, every other tab is still refused this message",
  );
});

test("a claim whose run was never issued is left to lapse, not held", async () => {
  // The claim resolves after the render and aui.thread() follows selection, so the bar must check
  // the message still exists before holding. Pinned at the source since there is no renderer.
  const claimed = THREAD.indexOf(
    'claimAutoContinue(messageId, runThreadId ?? "")',
  );
  assert.notEqual(claimed, -1, "the claim moved; this test needs rewriting");
  const branch = THREAD.slice(
    claimed,
    THREAD.indexOf("held-elsewhere", claimed),
  );

  const guard = branch.search(/messages\.some\(/);
  const hold = branch.indexOf("holdAutoContinueRun(");
  const record = branch.indexOf("recordAutoContinue(");
  const run = branch.indexOf("startContinuation()");
  assert.notEqual(
    guard,
    -1,
    "nothing checks the message is still there before holding",
  );
  assert.notEqual(hold, -1, "the hold is gone; this test needs rewriting");
  assert.ok(
    guard < hold && guard < record && guard < run,
    "the message has to be confirmed present before the lease is held or the round spent",
  );
  const between = branch.slice(guard, hold);
  assert.match(
    between,
    /\breturn;/,
    "the guard has to stop before the hold, not fall through",
  );
  assert.doesNotMatch(
    between,
    /setTimeout|setInterval/,
    "a deadline here is the arming timeout coming back, which lapses live continuations",
  );
});

test("a losing claim does not follow the row onto the next branch", () => {
  // Rows mount by index, so a lost-claim flag must be keyed by message, not a bare boolean.
  const rows = readSrc("components/assistant-ui/progressive-messages.tsx");
  assert.match(
    rows,
    /<AuiProvider key=\{index\} value=\{gate\.row\(index\)\}>\s*<MessageByIndexProvider index=\{index\}>/,
    "rows are no longer keyed by index; this test needs rewriting",
  );

  const start = THREAD.indexOf("const ContinueMessageBarForLastMessage");
  assert.notEqual(start, -1, "the bar moved; this test needs rewriting");
  const component = THREAD.slice(
    start,
    THREAD.indexOf("const WebSearchToolUIConfirmable", start),
  );
  const state =
    /const \[(\w+), (set\w+)\] = useState<string \| null>\(null\)/.exec(
      component,
    );
  assert.ok(
    state,
    "the losing answer is still a bare flag, so it outlives the message it was decided for",
  );
  assert.match(
    component,
    new RegExp(`claimHeldElsewhere =\\s*${state[1]} === messageId`),
    "the flag has to be re-answered against the message on screen now",
  );
  assert.match(
    component,
    new RegExp(`${state[2]}\\(messageId\\)`),
    "the message that lost is what gets remembered",
  );
});

test("a claim taken for a run that was never issued is given back", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const start = 1_000;

  assert.equal(
    await tab.claim("m1", { now: start, holder: "thread-A" }),
    "started",
  );
  tab.forget("m1");
  assert.equal(
    await otherTab.claim("m1", { now: start + 1 }),
    "held-elsewhere",
    "the rollback must not hand the message to a second tab",
  );
  assert.equal(
    await tab.claim("m1", {
      now: start + AUTO_CONTINUE_LEASE_TTL_MS + 1,
      holder: "thread-A",
    }),
    "started",
    "a claim that started nothing may not wedge the message for the life of the tab",
  );
});

test("the bar rolls its claim back when it issues no run", () => {
  const claimed = THREAD.indexOf(
    'claimAutoContinue(messageId, runThreadId ?? "")',
  );
  assert.notEqual(claimed, -1, "the claim moved; this test needs rewriting");
  const branch = THREAD.slice(
    claimed,
    THREAD.indexOf("held-elsewhere", claimed),
  );
  const guard = branch.search(/messages\.some\(/);
  const hold = branch.indexOf("holdAutoContinueRun(");
  const between = branch.slice(guard, hold);
  assert.match(
    between,
    /forgetAutoContinue\(messageId\)/,
    "the claim survives a run that was never issued, and skips every later one",
  );
});

test("a hold is settled when its run fails before it ever starts", async () => {
  // A preflight failure never reaches runningByThreadId, so only the failure signal ends the hold.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const running = new Set<string>();
  const runs = runSignalFake(running);
  const pending: Promise<unknown>[] = [];
  const start = 1_000;
  let clock = start;
  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 never started wrote nothing, so nothing may be marked continued",
      );
    },
    now: () => clock,
  });

  assert.equal(
    await tab.claim("m1", { now: start, holder: "thread-A" }),
    "started",
  );
  keeper.hold("m1", "thread-A");
  clock = start + AUTO_CONTINUE_LEASE_RENEW_MS;
  keeper.tick();
  assert.equal(keeper.held(), 1, "a slow preflight is still a live hold");

  keeper.failed("thread-A");
  assert.equal(
    keeper.held(),
    0,
    "nothing renews a run that failed on its way out",
  );

  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",
    "the lease lapses, so another tab can recover a message that was never continued",
  );
});

test("a failure on a thread leaves a hold whose run is already streaming alone", async () => {
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const running = new Set<string>();
  const runs = runSignalFake(running);
  const released: string[] = [];
  const pending: Promise<unknown>[] = [];
  const start = 1_000;
  let clock = start;
  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: () => clock,
  });

  await tab.claim("m1", { now: start, holder: "thread-A" });
  keeper.hold("m1", "thread-A");
  running.add("thread-A");
  runs.change();
  clock = start + AUTO_CONTINUE_LEASE_RENEW_MS;
  keeper.tick();

  keeper.failed("thread-A");
  assert.equal(keeper.held(), 1, "an armed hold is its run's to give back");
  assert.deepEqual(released, []);
  running.delete("thread-A");
  runs.change();
  await Promise.all(pending);
  assert.deepEqual(
    released,
    ["m1"],
    "and the thread going idle is what gives it back",
  );
  assert.equal(keeper.held(), 0);
});

test("a turn that fails before any text is still saved as interrupted", () => {
  const failure = CHAT_ADAPTER.slice(
    CHAT_ADAPTER.indexOf("const partialContent = buildAssistantContent(partialText);"),
  );
  const saved = failure.slice(0, failure.indexOf("throw err;"));
  assert.doesNotMatch(saved, /if \(partialContent\.length > 0\)/);
  assert.match(saved, /yield \{\s*content: partialContent,[\s\S]*incomplete: \{/);
});

test("the keeper is wired to the failure the adapter already reports", () => {
  // The adapter wrapper announces every adapter.run throw per thread; pinned at both ends.
  const wrapper = CHAT_ADAPTER.slice(CHAT_ADAPTER.indexOf("yield* adapter.run(args)"));
  assert.match(
    wrapper,
    /catch \(error\) \{[\s\S]*notifyPromptQueueRunFailed\(/,
    "the adapter no longer reports a failed run per thread; this test needs rewriting",
  );

  assert.match(
    AUTO_CONTINUE_RUN_KEEPER,
    /PROMPT_QUEUE_RUN_FAILED_EVENT/,
    "nothing settles a hold whose run failed before it started",
  );
  assert.match(
    AUTO_CONTINUE_RUN_KEEPER,
    /keeper\.failed\(/,
    "the failure has to reach the keeper",
  );
  assert.doesNotMatch(
    AUTO_CONTINUE_RUN_KEEPER,
    /setTimeout\(/,
    "a deadline here is the arming timeout coming back, which lapses live continuations",
  );
});

function issuedRunFake() {
  const settlers = new Set<() => void>();
  return {
    issued: {
      whenSettled: (onSettled: () => void) => {
        settlers.add(onSettled);
      },
    },
    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 emits no failure notice and no running flag; only the run promise 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 () => {
  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 before the runtime announces the run ending.
  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",
  );

  await tab.claim("round-2", { now: clock, holder: "thread-A" });
  keeper.hold("round-2", "thread-A");
  keeper.settleOn("round-2", "thread-A", round2.issued);

  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 () => {
  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);

  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 is already busy cannot tell its own run apart, so it is renewed.
  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);

  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 () => {
  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);

  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 multiple owners, so a key can stay busy after this hold's run settles.
  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);

  running.add("thread-A");
  runs.change();

  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 () => {
  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"]);

  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", () => {
  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", () => {
  // A non-thenable run means no signal: keep renewing, never release 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",
  );

  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 () => {
  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 holding startRun's return value, so it must hand it to the keeper.
  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 kind counts as a deadline; the renewal interval is named so it cannot be confused.
  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}`,
      );
    }
  }
});

function ownedRunSignalFake() {
  const running = new Map<string, { owner: () => void }[]>();
  const listeners = new Set<() => void>();
  const announce = () => {
    for (const listener of [...listeners]) {
      listener();
    }
  };
  return {
    signal: {
      isRunning: (threadId: string) => {
        const owners = running.get(threadId);
        return Boolean(owners?.length) && !isImageGateRunOnly(owners);
      },
      subscribe: (onChange: () => void) => {
        listeners.add(onChange);
        return () => listeners.delete(onChange);
      },
    },
    start: (threadId: string, owner: () => void) => {
      running.set(threadId, [...(running.get(threadId) ?? []), { owner }]);
      announce();
    },
    end: (threadId: string, owner: () => void) => {
      const rest = (running.get(threadId) ?? []).filter(
        (entry) => entry.owner !== owner,
      );
      if (rest.length > 0) {
        running.set(threadId, rest);
      } else {
        running.delete(threadId);
      }
      announce();
    },
  };
}

test("a continuation refused by the image gate is not recorded as continued", async () => {
  // The image gate pulses the running flag without issuing a request; it must not mark done.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const runs = ownedRunSignalFake();
  const pending: Promise<unknown>[] = [];
  const start = 1_000;
  let clock = start;
  const keeper = createAutoContinueLeaseKeeper({
    signal: runs.signal,
    renew: (messageId: string, holder: string, now: number) => {
      pending.push(tab.renew(messageId, holder, { now }));
    },
    release: () => {
      assert.fail(
        "the gate issued no request, so nothing may be marked continued",
      );
    },
    now: () => clock,
  });

  assert.equal(
    await tab.claim("m1", { now: start, holder: "thread-A" }),
    "started",
  );
  keeper.hold("m1", "thread-A");

  const gateOwner = createImageGateRunOwner();
  runs.start("thread-A", gateOwner);
  assert.equal(
    keeper.held(),
    1,
    "a pulse that stands for a refusal does not arm the hold",
  );
  runs.end("thread-A", gateOwner);
  assert.equal(keeper.held(), 1, "and so cannot settle it either");

  keeper.failed("thread-A");
  assert.equal(keeper.held(), 0);

  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",
    "the message was never continued, so it comes back rather than reading done for a day",
  );
});

test("a real run on the thread the gate refused still owns its lease", async () => {
  // Compare mode shares "__default"; a gate pulse beside a real run must not read as idle.
  const { storage } = storageFake();
  const locks = lockManagerFake();
  const tab = createAutoContinueTab({ storage, locks });
  const otherTab = createAutoContinueTab({ storage, locks });
  const runs = ownedRunSignalFake();
  const released: string[] = [];
  const pending: Promise<unknown>[] = [];
  const start = 1_000;
  let clock = start;
  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: () => clock,
  });

  await tab.claim("m1", { now: start, holder: "__default" });
  keeper.hold("m1", "__default");

  const streamingRun = () => {};
  runs.start("__default", streamingRun);
  clock = start + AUTO_CONTINUE_LEASE_RENEW_MS;
  keeper.tick();

  const gateOwner = createImageGateRunOwner();
  runs.start("__default", gateOwner);
  runs.end("__default", gateOwner);
  assert.deepEqual(released, [], "the run beside it is still generating");
  assert.equal(keeper.held(), 1);

  runs.end("__default", streamingRun);
  await Promise.all(pending);
  assert.deepEqual(released, ["m1"]);
  assert.equal(
    await otherTab.claim("m1", {
      now: clock + AUTO_CONTINUE_LEASE_TTL_MS + 1,
    }),
    "held-elsewhere",
    "this one really was continued",
  );
});

test("only the gate's own tokens are read as a refusal", () => {
  const gateOwner = createImageGateRunOwner();
  const runOwner = () => {};
  assert.equal(isImageGateRunOnly([{ owner: gateOwner }]), true);
  assert.equal(isImageGateRunOnly([{ owner: runOwner }]), false);
  assert.equal(
    isImageGateRunOnly([{ owner: gateOwner }, { owner: runOwner }]),
    false,
  );
  assert.equal(isImageGateRunOnly([]), false, "an ownerless flag is a run");
  assert.equal(isImageGateRunOnly(undefined), false);
  assert.notEqual(
    createImageGateRunOwner(),
    gateOwner,
    "one token per pulse, or two gates on a shared key clear each other",
  );
});

test("the gate's pulse is tagged where it is fired and read where it matters", () => {
  const gate = CHAT_ADAPTER.slice(CHAT_ADAPTER.indexOf("const blockAttachmentRun ="));
  assert.match(
    CHAT_ADAPTER.slice(CHAT_ADAPTER.indexOf("const imageGateReason =")),
    /if \(imageGateReason\) \{\n\s*blockAttachmentRun\(imageGateReason\);/,
  );
  assert.match(
    gate,
    /const gateOwner = createImageGateRunOwner\(\)/,
    "an anonymous owner is indistinguishable from a run that really started",
  );
  assert.match(
    gate.slice(gate.indexOf("const gateOwner")),
    /setThreadRunning\([\s\S]*owner: gateOwner[\s\S]*setThreadRunning\([\s\S]*owner: gateOwner/,
    "the pulse compare mode waits on is still fired under that token",
  );

  assert.match(
    AUTO_CONTINUE_RUN_KEEPER,
    /isImageGateRunOnly\(state\.runOwnerByThreadId\[threadId\]\)/,
    "the keeper is arming holds on a request the gate refused to send",
  );
  assert.doesNotMatch(
    AUTO_CONTINUE_RUN_KEEPER,
    /setTimeout\(/,
    "a deadline here is the arming timeout coming back, which lapses live continuations",
  );
});

// Streaming publishes must never drop the partial: Stop persists the last streamed yield,
// so the restart check must stay off while streaming.

const { createContinuationMerger } = await import(
  "../src/features/chat/utils/continuation.ts"
);


const REASONING =
  "Okay, so the user is asking about how to structure the migration. " +
  "Let me think through this carefully step by step before answering. " +
  "First I need to consider what the existing schema looks like, and " +
  "whether an online migration is even possible given the constraints. ";

function replay(partial: string, tail: string): string[] {
  const merge = createContinuationMerger(partial, true);
  const published: string[] = [];
  let cumulative = partial;
  published.push(merge(cumulative));
  for (const character of tail) {
    cumulative += character;
    published.push(merge(cumulative));
  }
  return published;
}

test("a restart mid-stream never discards the partial", () => {
  const partial = REASONING.repeat(6);
  const published = replay(partial, `${REASONING}and so on. `.repeat(2));

  for (const value of published) {
    assert.equal(
      value.startsWith(partial),
      true,
      "a streamed publish dropped the partial, so Stop would save only the restart",
    );
  }
});

test("no streamed publish withholds generated text", () => {
  const partial = REASONING.repeat(6);
  const tail = "The second consideration is throughput, which matters here. ";
  const published = replay(partial, tail);
  const last = published[published.length - 1];

  assert.equal(last, `${partial}${tail}`, "the new text must be present in the live value");
  assert.equal(
    published[10].length > partial.length,
    true,
    "text generated early must appear early, not wait for a settle point",
  );
});

test("what Stop would save mid-stream carries the overlap repair", () => {
  const partial = REASONING.repeat(6);
  const repeated = partial.slice(-60);
  const published = replay(partial, `${repeated}and then it continues onward. `);
  const saved = published[published.length - 1];

  assert.equal(
    saved.includes(`${repeated}${repeated}`),
    false,
    "the repeated tail survived into the value Stop persists",
  );
  assert.equal(saved.startsWith(partial), true, "the partial itself must be intact");
});

test("the join can shift by the overlap, and never by more", () => {
  const partial = REASONING.repeat(6);
  const published = replay(
    partial,
    `${partial.slice(-60)}and then it continues onward from there. `,
  );

  let worst = 0;
  for (let i = 1; i < published.length; i += 1) {
    worst = Math.max(worst, published[i - 1].length - published[i].length);
  }
  assert.equal(worst <= 400, true, `the join shifted by ${worst}, beyond MAX_OVERLAP`);
});

test("the final merge still collapses a genuine restart", () => {
  const partial = REASONING.repeat(6);
  const restart = `${REASONING}and so on. `;
  assert.equal(
    createContinuationMerger(partial, true)(partial + restart, { final: true }),
    restart,
    "a restart is collapsed once the turn is complete and the evidence is in",
  );
});

test("a short partial with a whitespace-led restart is not collapsed mid-stream", () => {
  const partial = "Sure, here is a plan for the migration you asked me about.";
  const merge = createContinuationMerger(partial, true);
  const restart = `\n${partial}`;

  assert.equal(
    merge(`${partial}${restart}`).startsWith(partial),
    true,
    "the partial must survive a whitespace-led restart while streaming",
  );
});

test("a merger with repair off is the identity, streaming or final", () => {
  const partial = REASONING.repeat(2);
  const merge = createContinuationMerger(partial, false);
  const full = `${partial}${REASONING}`;
  assert.equal(merge(full), full);
  assert.equal(merge(full, { final: true }), full);
});

const ERROR_PATH_REASON =
  /incompleteReasonAfterError\(\s*incompleteReason,\s*err instanceof GenerationLengthError\s*\?\s*lengthIncompleteReason\(err\.stopCause\)/;

test("a length error refines the length the terminal chunk latched", () => {
  assert.equal(
    incompleteReasonAfterError("length", "context_window"),
    "context_window",
  );
  assert.equal(incompleteReasonAfterError("length", "length"), "length");
  assert.equal(
    incompleteReasonAfterError("cancelled", "context_window"),
    "cancelled",
  );
  assert.equal(
    incompleteReasonAfterError(null, "context_window"),
    "context_window",
  );
  assert.equal(incompleteReasonAfterError(null, "interrupted"), "interrupted");
  assert.equal(incompleteReasonAfterError("length", "interrupted"), "length");
});

test("the adapter's error path asks the length error which limit it was", () => {
  assert.match(CHAT_ADAPTER, ERROR_PATH_REASON);
});