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: keep tracking a new chat's upload when its id commits late (#11738)

Daniel Han288b1a3044e0
diff --git a/studio/frontend/src/features/rag/components/use-rag-documents.ts b/studio/frontend/src/features/rag/components/use-rag-documents.ts
index cdb57e10ce7..0c289cd0b31 100644
--- a/studio/frontend/src/features/rag/components/use-rag-documents.ts
+++ b/studio/frontend/src/features/rag/components/use-rag-documents.ts
@@ -125,6 +125,9 @@ export function useRagDocuments(
// True while upload() runs, so the scope-change effect can tell a real switch
// from lazy thread materialization mid-upload (which must not reset).
const uploadInFlightRef = useRef(false);
+ // The scope an upload begun with none resolved to. Leaving the null scope keeps its jobs only
+ // when this is where it lands; landing anywhere else is a navigation.
+ const materializedKeyRef = useRef<string | null>(null);
const uploadGenerationRef = useRef(0);
const activeUploadsRef = useRef(new Set<object>());
useEffect(
@@ -373,7 +376,17 @@ export function useRagDocuments(
const jobs = trackedJobs.current;
const prev = prevScopeKeyRef.current;
prevScopeKeyRef.current = scopeKey;
- if (prev !== null && prev !== scopeKey) {
+ const materialized = materializedKeyRef.current;
+ if (scopeKey !== null) materializedKeyRef.current = null;
+ // Leaving the null scope for a chat other than the one its upload materialized: the cleanup
+ // below kept that upload's jobs, so drop them here as any other switch would.
+ const leftForAnotherChat =
+ prev === null &&
+ scopeKey !== null &&
+ !uploadInFlightRef.current &&
+ (jobs.size > 0 || materialized !== null) &&
+ materialized !== scopeKey;
+ if ((prev !== null && prev !== scopeKey) || leftForAnotherChat) {
for (const controller of jobs.values()) controller.abort();
jobs.clear();
sigByDocId.current.clear();
@@ -409,8 +422,11 @@ export function useRagDocuments(
}
return () => {
// Preserve in-flight tracking when cleanup is the materialization flip,
- // not a real switch/unmount.
- if (uploadInFlightRef.current) return;
+ // not a real switch/unmount. Leaving no scope may be that flip even once
+ // the upload has finished, since React can commit the new id after the POST
+ // returned, so the next setup decides: it keeps the jobs only for the scope
+ // the upload materialized. An unmount aborts in the unmount effect.
+ if (uploadInFlightRef.current || scopeKey === null) return;
for (const controller of jobs.values()) controller.abort();
jobs.clear();
};
@@ -660,6 +676,11 @@ export function useRagDocuments(
setDocuments((rows) => rows.filter((row) => !tempIds.has(row.id)));
return;
}
+ // Whenever the hook itself has no scope yet, passed in or materialized alike: the job this
+ // starts may be running before React commits the scope it belongs to.
+ if (liveKey === null) {
+ materializedKeyRef.current = resolvedKey;
+ }
for (const { tempId, item } of fresh) {
if (generation !== uploadGenerationRef.current) return;
diff --git a/studio/frontend/tests/rag-upload-lifecycle.test.ts b/studio/frontend/tests/rag-upload-lifecycle.test.ts
index ee8d00dbebf..ce1f53cc2b1 100644
--- a/studio/frontend/tests/rag-upload-lifecycle.test.ts
+++ b/studio/frontend/tests/rag-upload-lifecycle.test.ts
@@ -36,8 +36,9 @@ const flush = () => new Promise<void>((resolve) => setImmediate(resolve));
function harness(
options: {
filename?: string;
- events?: () => AsyncGenerator<JobEvent>;
+ events?: (jobId: string, signal?: AbortSignal) => AsyncGenerator<JobEvent>;
getJob?: () => Promise<IndexJob>;
+ list?: () => Promise<RagDocument[]>;
} = {},
) {
const slots: unknown[] = [];
@@ -51,7 +52,7 @@ function harness(
jobId: "job",
filename: options.filename ?? "report.pdf",
};
- const lister = async () => [];
+ const lister = options.list ?? (async () => []);
let scope: RagDocumentScope | null = { type: "thread", threadId: "thread" };
const react = {
useRef(value: unknown) {
@@ -302,3 +303,148 @@ test("an upload begun without a scope stops at the chat the user left", async ()
app.dispose();
}
});
+
+test("a new chat's id committed after its upload finished keeps tracking the job", async () => {
+ const indexed = deferred<void>();
+ let done = false;
+ let signal: AbortSignal | undefined;
+ const app = harness({
+ list: async () => [
+ {
+ id: "doc",
+ threadId: "thread",
+ filename: "report.pdf",
+ status: done ? "completed" : "running",
+ managed: false,
+ numChunks: done ? 10 : 0,
+ },
+ ],
+ events: async function* (_jobId, streamSignal) {
+ signal = streamSignal;
+ yield { type: "progress", progress: 0.4, stage: "captioning" };
+ await new Promise<void>((resolve, reject) => {
+ indexed.promise.then(resolve);
+ streamSignal?.addEventListener("abort", () =>
+ reject(new DOMException("Fetch is aborted", "AbortError")),
+ );
+ });
+ done = true;
+ yield { type: "complete", num_chunks: 10 };
+ },
+ });
+ try {
+ app.setScope(null);
+ let hook = app.render();
+ await flush();
+ // The chat materializes, and React is late to commit its id: the POST has
+ // returned and tracking started before the scope change reaches the hook.
+ await hook.upload([report()], async () => ({
+ type: "thread",
+ threadId: "thread",
+ }));
+ await flush();
+ assert.equal(app.render().uploading, false);
+ assert.equal(signal?.aborted, false, "the job was never tracked");
+ app.setScope({ type: "thread", threadId: "thread" });
+ hook = app.render();
+ await flush();
+ assert.equal(
+ signal?.aborted,
+ false,
+ "the scope arriving for the new chat aborted its own upload's job",
+ );
+ indexed.resolve();
+ await flush();
+ await flush();
+ hook = app.render();
+ assert.equal(hook.documents[0]?.status, "completed");
+ assert.equal(hook.hasIndexing, false);
+ assert.deepEqual(app.errors, []);
+ } finally {
+ app.dispose();
+ }
+});
+
+test("leaving a new chat for another one after its upload finished drops the job", async () => {
+ let signal: AbortSignal | undefined;
+ const app = harness({
+ events: async function* (_jobId, streamSignal) {
+ signal = streamSignal;
+ yield { type: "progress", progress: 0.4, stage: "captioning" };
+ await new Promise<void>((_resolve, reject) => {
+ streamSignal?.addEventListener("abort", () =>
+ reject(new DOMException("Fetch is aborted", "AbortError")),
+ );
+ });
+ },
+ });
+ try {
+ app.setScope(null);
+ let hook = app.render();
+ await flush();
+ await hook.upload([report()], async () => ({
+ type: "thread",
+ threadId: "thread",
+ }));
+ await flush();
+ assert.equal(signal?.aborted, false, "the job was never tracked");
+ // Before the new chat's id commits, the user opens a different chat.
+ app.setScope({ type: "thread", threadId: "other" });
+ hook = app.render();
+ await flush();
+ await flush();
+ hook = app.render();
+ assert.equal(
+ signal?.aborted,
+ true,
+ "the new chat's job kept streaming into the chat the user opened",
+ );
+ assert.deepEqual(hook.documents, []);
+ assert.equal(hook.hasIndexing, false);
+ assert.deepEqual(app.errors, []);
+ } finally {
+ app.dispose();
+ }
+});
+
+test("a scope passed to an unscoped hook keeps its job when that scope commits late", async () => {
+ const indexed = deferred<void>();
+ let signal: AbortSignal | undefined;
+ const app = harness({
+ events: async function* (_jobId, streamSignal) {
+ signal = streamSignal;
+ yield { type: "progress", progress: 0.4, stage: "captioning" };
+ await new Promise<void>((resolve, reject) => {
+ indexed.promise.then(resolve);
+ streamSignal?.addEventListener("abort", () =>
+ reject(new DOMException("Fetch is aborted", "AbortError")),
+ );
+ });
+ yield { type: "complete", num_chunks: 10 };
+ },
+ });
+ try {
+ app.setScope(null);
+ let hook = app.render();
+ await flush();
+ // The composer hands its scope over directly, since the hook's own is still null on the
+ // render that starts the upload.
+ await hook.upload([report()], { type: "thread", threadId: "thread" });
+ await flush();
+ assert.equal(signal?.aborted, false, "the job was never tracked");
+ app.setScope({ type: "thread", threadId: "thread" });
+ hook = app.render();
+ await flush();
+ assert.equal(
+ signal?.aborted,
+ false,
+ "the passed scope committing late aborted its own upload's job",
+ );
+ indexed.resolve();
+ await flush();
+ await flush();
+ assert.deepEqual(app.errors, []);
+ } finally {
+ app.dispose();
+ }
+});