diff --git a/ui/src/events/EventsPage.vue b/ui/src/events/EventsPage.vue index e5e3b7399..ced4942b1 100644 --- a/ui/src/events/EventsPage.vue +++ b/ui/src/events/EventsPage.vue @@ -73,13 +73,20 @@ watch(active, (tab) => show(tab), { immediate: true }); // and the counting live in counts.ts, so the strip, the chips and the test all // read one source rather than three. const counts = ref({}); +// A counts read bumps the generation; one that settles after a newer read +// started is dropped, so the previous workspace's counts never overwrite the +// current one. +let countsGeneration = 0; async function loadCounts() { + const gen = ++countsGeneration; const workspace = events.workspace; - counts.value = await loadEventCounts({ + const loaded = await loadEventCounts({ captures: api.captures, mappings: () => api.mappings(workspace), bindings: () => api.bindings(workspace), }); + if (gen !== countsGeneration) return; + counts.value = loaded; } useLiveResource(null, () => void loadCounts(), 1000); diff --git a/ui/src/tasks/TaskLogs.vue b/ui/src/tasks/TaskLogs.vue index 0a208ed58..b6a25c3c1 100644 --- a/ui/src/tasks/TaskLogs.vue +++ b/ui/src/tasks/TaskLogs.vue @@ -77,7 +77,12 @@ const pageKey = computed( () => `${props.taskId}|${resolvedAttempt.value}|${props.filters.level}|${props.filters.stage}`, ); +// A page fetch bumps the generation; one that settles after a newer fetch or +// an attempt/filter change is dropped, so an older page never lands under the +// current attempt or filter. +let pageGeneration = 0; watch(pageKey, () => { + pageGeneration++; page.value = null; newerPages.value = []; pageError.value = false; @@ -95,10 +100,10 @@ const canOlder = computed( const canNewer = computed(() => canPage.value && newerPages.value.length > 0); /** Read the page ending just before cursor; zero reads the tail. */ -async function fetchPage(before: number): Promise { +async function fetchPage(before: number, gen: number): Promise { if (props.taskId == null) return null; paging.value = true; - pageError.value = false; + if (gen === pageGeneration) pageError.value = false; try { return await api.taskLogs(String(props.taskId), { attempt: resolvedAttempt.value || undefined, @@ -108,7 +113,7 @@ async function fetchPage(before: number): Promise { before: before || undefined, }); } catch { - pageError.value = true; + if (gen === pageGeneration) pageError.value = true; return null; } finally { paging.value = false; @@ -116,10 +121,12 @@ async function fetchPage(before: number): Promise { } async function loadOlder(): Promise { + const gen = ++pageGeneration; const shown = current.value; const before = shown?.cursor ?? 0; if (!shown?.more_available || before <= 0) return; - const older = await fetchPage(before); + const older = await fetchPage(before, gen); + if (gen !== pageGeneration) return; if (!older) return; await router.replace({ hash: "" }); newerPages.value.push({ before: page.value?.before ?? 0, state: shown }); diff --git a/ui/src/tasks/TasksPage.vue b/ui/src/tasks/TasksPage.vue index b8df718cf..935580b64 100644 --- a/ui/src/tasks/TasksPage.vue +++ b/ui/src/tasks/TasksPage.vue @@ -100,16 +100,24 @@ const state = computed<"loading" | "error" | "empty" | "no-match">(() => { const PAGE = 100; +// Every read bumps the generation; a response that settles after a newer read +// started is dropped, so the previous filter's rows or next cursor never land +// under the current filter. +let loadGeneration = 0; + // load re-reads the board from the top, as many rows as are showing, so a live // update or a row action does not collapse pages the operator loaded. async function load() { + const current = ++loadGeneration; error.value = null; const limit = Math.min(Math.max(tasks.value?.length ?? 0, PAGE), 500); try { const page = await api.taskPage({ status: status.value || undefined, limit }); + if (current !== loadGeneration) return; tasks.value = page.tasks; next.value = page.next; } catch (err) { + if (current !== loadGeneration) return; error.value = String((err as Error).message || err); tasks.value = null; } @@ -117,6 +125,7 @@ async function load() { async function loadMore() { if (!next.value || loadingMore.value) return; + const current = ++loadGeneration; loadingMore.value = true; try { const page = await api.taskPage({ @@ -124,9 +133,11 @@ async function loadMore() { limit: PAGE, cursor: next.value, }); + if (current !== loadGeneration) return; tasks.value = [...(tasks.value ?? []), ...page.tasks]; next.value = page.next; } catch (err) { + if (current !== loadGeneration) return; error.value = String((err as Error).message || err); } finally { loadingMore.value = false; diff --git a/ui/src/tasks/use-task-run.ts b/ui/src/tasks/use-task-run.ts index 669d3c3e6..384d04e3b 100644 --- a/ui/src/tasks/use-task-run.ts +++ b/ui/src/tasks/use-task-run.ts @@ -164,6 +164,13 @@ export function provideTaskRun(id: ComputedRef): TaskRun { debugKey.value == null ? undefined : debug.value.get(debugKey.value), ); + // Each keyed read gets a generation number; a response or timeout that + // settles after a newer read for the same key started is dropped, so a late + // null never overwrites a newer successful response. dropCaches bumps the + // global counter, which stales every in-flight read at once. + let cacheGeneration = 0; + const loadGenerations = new Map(); + function keyedLoad( cacheKey: string, fetcher: () => Promise, @@ -171,6 +178,8 @@ export function provideTaskRun(id: ComputedRef): TaskRun { { force = false, keep = false }: { force?: boolean; keep?: boolean } = {}, ): void { if (cache.value.has(cacheKey) && cache.value.get(cacheKey) !== null && !force) return; + const current = ++cacheGeneration; + loadGenerations.set(cacheKey, current); // keep leaves a loaded payload on screen while it is re-read, so a live // refresh replaces it in place instead of flashing the loading state. if (!keep || cache.value.get(cacheKey) == null) cache.value = new Map(cache.value).set(cacheKey, undefined); @@ -184,9 +193,11 @@ export function provideTaskRun(id: ComputedRef): TaskRun { }); Promise.race([fetcher(), timeout]) .then((res) => { + if (loadGenerations.get(cacheKey) !== current) return; cache.value = new Map(cache.value).set(cacheKey, res); }) .catch(() => { + if (loadGenerations.get(cacheKey) !== current) return; cache.value = new Map(cache.value).set(cacheKey, null); }); } @@ -272,6 +283,10 @@ export function provideTaskRun(id: ComputedRef): TaskRun { /** Drop every cache so a manual refresh cannot show a stale capture or log * beside fresh attempt history. */ function dropCaches(): void { + // Invalidate in-flight reads too: a response that lands after the drop + // would otherwise write its stale payload into the fresh caches. + cacheGeneration++; + loadGenerations.clear(); logs.value = new Map(); changes.value = new Map(); debug.value = new Map();