From f8b7ffe516e74af624ce8f84550c2a3479be852d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=88=E7=A7=8B?= <49514152+yiqiui@users.noreply.github.com> Date: Mon, 5 Oct 2026 23:57:50 +0800 Subject: [PATCH] fix(grid): reconcile an exact total when a page lands beyond it Closes #10968 --- apps/desktop/src/components/grid/DataGrid.vue | 17 ++++++++++ .../DataGridTotalRowCountRefresh.spec.ts | 16 ++++++++++ .../__tests__/dataGridPagination.spec.ts | 32 ++++++++++++++++++- .../src/lib/dataGrid/dataGridPagination.ts | 28 ++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index 32b2b1ed6e..34e3b79c9c 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -230,6 +230,7 @@ import { ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS, elasticsearchCursorPageJumpRequestCount, hasCompleteLocalDataGridResult, + reconcileDataGridExactTotalWithObservedPage, resolveDataGridPaginationTotal, showDataGridRerunTotalCountAction, type DataGridInexactTotalRowCountMode, @@ -3462,6 +3463,22 @@ const serverKnownTotalRowCount = computed(() => (typeof manualTotalRowCount.valu const displayedTotalRowCount = computed(() => serverKnownTotalRowCount.value ?? inferredBackendTotalRowCount.value); const totalRowCountIsExact = computed(() => typeof manualTotalRowCount.value === "number" || props.totalRowCountIsExact !== false); const totalRowCountLabelKey = computed(() => dataGridTotalRowCountLabelKey(totalRowCountIsExact.value, props.inexactTotalRowCountMode)); +// The COUNT behind an exact total and the query serving a page are two +// separate snapshots: rows can land in between (a table being written to), and +// an agent result session serves the snapshot it was opened with. A page that +// lands with rows past the exact total must not render row indexes beyond the +// claimed end of the result (#10968) — adopt the observed extent instead. +watch( + () => [props.loading, props.pageOffset, props.result.rows.length, props.result.appended_from_row_count] as const, + ([loading, offset, rowCount, appendedFromRowCount]) => { + if (loading || isInfiniteScrollPaginating.value || appendedFromRowCount !== undefined) return; + const exactTotal = serverKnownTotalRowCount.value; + if (!totalRowCountIsExact.value || typeof offset !== "number" || typeof exactTotal !== "number") return; + const reconciled = reconcileDataGridExactTotalWithObservedPage({ offset, rowCount, exactTotal }); + if (reconciled !== undefined) manualTotalRowCount.value = reconciled; + }, + { flush: "post" }, +); // A backend can expose an exact display total while deliberately restricting // offset pagination to a smaller safe range. const paginationTotalRowCount = computed(() => diff --git a/apps/desktop/src/components/grid/__tests__/DataGridTotalRowCountRefresh.spec.ts b/apps/desktop/src/components/grid/__tests__/DataGridTotalRowCountRefresh.spec.ts index d1191f542d..4723742857 100644 --- a/apps/desktop/src/components/grid/__tests__/DataGridTotalRowCountRefresh.spec.ts +++ b/apps/desktop/src/components/grid/__tests__/DataGridTotalRowCountRefresh.spec.ts @@ -117,6 +117,22 @@ describe("DataGrid manual count freshness", () => { expect(totalText(host)).toContain("250"); }); + it("reconciles a stale exact total when a page lands beyond it (#10968)", async () => { + const { host, state } = mountGrid(); + await settle(); + clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline"))); + await settle(); + expect(totalText(host)).toContain("250"); + + // The COUNT said 250, but the served snapshot holds more: the counted last + // page (offset 200, page size 100) comes back full, so row indexes reach + // 300 — adopt the observed extent instead of showing rows past the total. + state.pageOffset = 200; + state.result = pageResult(); + await settle(); + expect(totalText(host)).toContain("300"); + }); + it("does not let an old manual count override a refreshed automatic count", async () => { let resolveCount!: (total: number) => void; const countTotalRows = vi.fn(() => new Promise((resolve) => (resolveCount = resolve))); diff --git a/apps/desktop/src/lib/dataGrid/__tests__/dataGridPagination.spec.ts b/apps/desktop/src/lib/dataGrid/__tests__/dataGridPagination.spec.ts index d8c94a59b6..f9b151be0d 100644 --- a/apps/desktop/src/lib/dataGrid/__tests__/dataGridPagination.spec.ts +++ b/apps/desktop/src/lib/dataGrid/__tests__/dataGridPagination.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS, elasticsearchCursorPageJumpRequestCount, hasCompleteLocalDataGridResult } from "@/lib/dataGrid/dataGridPagination"; +import { ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS, elasticsearchCursorPageJumpRequestCount, hasCompleteLocalDataGridResult, reconcileDataGridExactTotalWithObservedPage } from "@/lib/dataGrid/dataGridPagination"; describe("Elasticsearch cursor page jumps", () => { it("counts only missing forward cursors", () => { @@ -54,3 +54,33 @@ describe("complete local DataGrid results", () => { ).toBe(false); }); }); + +describe("reconciling an exact total with an observed page (#10968)", () => { + it("adopts the observed extent when a page lands past the counted total", () => { + // Issue screenshot: COUNT said 2892, but the counted last page (offset 2800, + // 200 rows) came back full from a snapshot holding more rows. + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 200, exactTotal: 2892 })).toBe(3000); + }); + + it("keeps the counted total for pages that fit inside it", () => { + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 92, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 0, rowCount: 200, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 400, rowCount: 200, exactTotal: 2892 })).toBeUndefined(); + }); + + it("keeps the counted total when the page comes back short at its end", () => { + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 92, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 0, exactTotal: 2892 })).toBeUndefined(); + }); + + it("extends a zero or missing total as soon as rows are on screen", () => { + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 0, rowCount: 5, exactTotal: 0 })).toBe(5); + }); + + it("ignores malformed offsets, rows, and totals", () => { + expect(reconcileDataGridExactTotalWithObservedPage({ offset: -1, rowCount: 200, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: -1, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: Number.NaN, rowCount: 200, exactTotal: 2892 })).toBeUndefined(); + expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 200, exactTotal: Number.NaN })).toBeUndefined(); + }); +}); diff --git a/apps/desktop/src/lib/dataGrid/dataGridPagination.ts b/apps/desktop/src/lib/dataGrid/dataGridPagination.ts index 64ccec211d..49bfa27b0e 100644 --- a/apps/desktop/src/lib/dataGrid/dataGridPagination.ts +++ b/apps/desktop/src/lib/dataGrid/dataGridPagination.ts @@ -73,6 +73,34 @@ export function resolveDataGridPaginationTotal(options: { paginationTotalRowCoun return Math.min(total, options.maxRows); } +export interface ReconcileDataGridExactTotalOptions { + offset: number; + rowCount: number; + exactTotal: number; +} + +/** + * Total to display after a page landed with rows beyond the exact counted + * total, or `undefined` when the counted total still describes the page. + * + * The COUNT that produced the exact total and the query that served the page + * are two separate snapshots: rows can land between them (a table being + * written to), and an agent result session serves the snapshot it was opened + * with even after newer rows were counted. Rendering that page against the + * stale total shows row indexes past the claimed end of the result (#10968) — + * adopt the observed extent instead, so the displayed total always covers + * every row the grid actually shows. + */ +export function reconcileDataGridExactTotalWithObservedPage(options: ReconcileDataGridExactTotalOptions): number | undefined { + const { offset, rowCount, exactTotal } = options; + if (!Number.isSafeInteger(offset) || offset < 0) return undefined; + if (!Number.isSafeInteger(rowCount) || rowCount <= 0) return undefined; + if (!Number.isSafeInteger(exactTotal) || exactTotal < 0) return undefined; + const observedExtent = offset + rowCount; + if (observedExtent <= exactTotal) return undefined; + return observedExtent; +} + export function hasCompleteLocalDataGridResult(options: CompleteLocalDataGridResultOptions): boolean { if (!options.isResultsContext || options.truncated === true || options.hasMore === true) return false; if (options.pageLimit === undefined) return true;