From 14353fe2fb16ccfc983464bf706d052d6f53d138 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:23:09 +0000 Subject: [PATCH 1/2] wip: gantt walks to its row ceiling in steps under a host's maximum page size --- .changeset/gantt-paged-ceiling-read.md | 13 +++ .../core/src/utils/non-grid-row-ceiling.ts | 14 ++- .../ObjectGantt.elementDataSource.test.tsx | 4 +- .../src/ObjectGantt.harness.test.tsx | 95 ++++++++++++++++--- ui/packages/plugin-gantt/src/ObjectGantt.tsx | 37 ++++---- ui/packages/plugin-gantt/src/ganttRowWalk.ts | 82 ++++++++++++++++ 6 files changed, 211 insertions(+), 34 deletions(-) create mode 100644 .changeset/gantt-paged-ceiling-read.md create mode 100644 ui/packages/plugin-gantt/src/ganttRowWalk.ts diff --git a/.changeset/gantt-paged-ceiling-read.md b/.changeset/gantt-paged-ceiling-read.md new file mode 100644 index 00000000000..573b1bc4ea5 --- /dev/null +++ b/.changeset/gantt-paged-ceiling-read.md @@ -0,0 +1,13 @@ +--- +'@object-ui/plugin-gantt': patch +'@object-ui/core': patch +--- + +fix(gantt): a gantt opens on a host that sets a maximum page size, instead of showing the refusal + +Clause-②: no +Metadata-protocol: none + +The gantt read its whole filtered result set, up to its 10,000-row ceiling, as ONE read of 10,001 rows (the ceiling plus the probe row that makes a cut detectable). A host that sets a maximum page size (`OS_DATA_MAX_PAGE_SIZE`; the hosted plane sets 5,000) refuses any read over it before running the query, so on every such host the gantt drew "Query parameter 'top' asks for 10001 rows; this deployment's maximum page size is 5000…" in place of the chart, for an empty object as much as a full one. + +The gantt now walks to its ceiling in steps of at most 2,001 rows, the one read the calendar, map, tree and timesheet already send: `$skip` moves by the rows that came back, and the walk stops at the first short answer or one row past the ceiling. The ceiling, the footnote that names a cut, and the rule that an authored `pagination.pageSize` or `limit` never reaches the read are unchanged; a result set that fits one step is still one read. diff --git a/ui/packages/core/src/utils/non-grid-row-ceiling.ts b/ui/packages/core/src/utils/non-grid-row-ceiling.ts index 09743ab4c51..72daf2385e2 100644 --- a/ui/packages/core/src/utils/non-grid-row-ceiling.ts +++ b/ui/packages/core/src/utils/non-grid-row-ceiling.ts @@ -135,6 +135,13 @@ export const NON_GRID_ROW_CEILING = 2000; * value. Past it (10,500 rows) the probe row cuts at 10,000 and * `NonGridRowCeilingNote` shows, as below the shared ceiling. * + * ⛔ A STOP CONDITION, NOT A REQUEST SIZE. The gantt reaches this ceiling in + * steps no larger than the shared ceiling's one read (`fetchGanttRows`, + * `@object-ui/plugin-gantt`). Sent as one `$top` of 10,001 it was refused + * outright by any host whose maximum page size is smaller + * (`OS_DATA_MAX_PAGE_SIZE`; the hosted plane sets 5,000), on an empty object + * as on a full one, and the refusal drew in place of the chart. + * * ⚠️ As with the table above: SHARED-BOX seconds and a reading taken once, * when objectstack#32421 landed; nothing re-derives it. The DOM counts are * the environment-independent half. @@ -173,8 +180,11 @@ export type NonGridRowCeiling = typeof NON_GRID_ROW_CEILING | typeof GANTT_ROW_C * A fresh object per call, so a caller that mutates the query it built cannot * change what the next caller sends. * - * `ceiling` defaults to the shared {@link NON_GRID_ROW_CEILING}; only the - * gantt passes {@link GANTT_ROW_CEILING}. A caller passes the SAME ceiling to + * `ceiling` defaults to the shared {@link NON_GRID_ROW_CEILING}, and every + * view that spreads this query passes no other: ⛔ it is ONE read, and + * {@link GANTT_ROW_CEILING} plus its probe row (10,001) is over the maximum + * page size a host may set, so the gantt walks to its ceiling in steps instead + * (see that constant). A caller passes the SAME ceiling to * {@link applyNonGridRowCeiling}, or the probe row is sliced at the wrong * place. */ diff --git a/ui/packages/plugin-gantt/src/ObjectGantt.elementDataSource.test.tsx b/ui/packages/plugin-gantt/src/ObjectGantt.elementDataSource.test.tsx index 4b3dde19c97..a53e58db3e8 100644 --- a/ui/packages/plugin-gantt/src/ObjectGantt.elementDataSource.test.tsx +++ b/ui/packages/plugin-gantt/src/ObjectGantt.elementDataSource.test.tsx @@ -31,7 +31,7 @@ import { render, waitFor } from '@testing-library/react'; import React from 'react'; import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; import { I18nProvider } from '@object-ui/i18n'; -import { GANTT_ROW_CEILING, nonGridRowCeilingQuery } from '@object-ui/core'; +import { GANTT_FETCH_STEP } from './ganttRowWalk'; vi.mock('sonner', () => ({ toast: { error: vi.fn() } })); @@ -164,7 +164,7 @@ describe('object-gantt — dataSource: { object, view } (objectstack#7121)', () // Not an oversight — `limit` is unmapped because there is no read site. The // assertion is here so a future mapping addition has to be deliberate. expect(params.$top).not.toBe(3); - expect(params.$top).toBe(nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top); + expect(params.$top).toBe(GANTT_FETCH_STEP); expect(params.options).toBeUndefined(); }); diff --git a/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx b/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx index 9884bc4bcad..9fb07ce4ea1 100644 --- a/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx +++ b/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx @@ -51,6 +51,7 @@ import { I18nProvider } from '@object-ui/i18n'; import type { DataSource, DeclaredNode, ObjectGanttBlockNode } from '@object-ui/types'; import { safeValidateSchema } from '@object-ui/types/zod'; import { ObjectGantt } from './ObjectGantt'; +import { GANTT_FETCH_STEP } from './ganttRowWalk'; // Module scope, not a hook: this import IS the registration (AGENTS.md // test-discipline section). import './index'; @@ -322,14 +323,25 @@ describe('ObjectGantt.rowCeiling-7210', () => { let calls: Array> = []; - /** An adapter that HONOURS `$top`, the way a real one does. */ - function makeDataSource(storeSize: number) { + /** + * An adapter that HONOURS `$top` and `$skip`, the way a real one does. With + * `maxPageSize` it also refuses a `$top` over it, before reading anything, + * the way a host with `OS_DATA_MAX_PAGE_SIZE` set does (hosted: 5,000). + */ + function makeDataSource(storeSize: number, maxPageSize?: number) { const store = makeRows(storeSize); return { find: vi.fn(async (_resource: string, params: any) => { calls.push({ ...(params ?? {}) }); const top = typeof params?.$top === 'number' ? params.$top : store.length; - return { data: store.slice(0, top), total: store.length }; + if (maxPageSize !== undefined && top > maxPageSize) { + throw new Error( + `Query parameter 'top' asks for ${top} rows; this deployment's maximum page size is ${maxPageSize}. ` + + `The query was not run. Ask for at most ${maxPageSize} rows and page with offset.`, + ); + } + const skip = typeof params?.$skip === 'number' ? params.$skip : 0; + return { data: store.slice(skip, skip + top), total: store.length }; }), findOne: vi.fn(), create: vi.fn(), @@ -374,12 +386,17 @@ describe('ObjectGantt.rowCeiling-7210', () => { ), ); - // The fetch asked for one probe row past the ceiling — that is what makes - // the cut a fact about the rows in hand rather than a guess. - expect(calls.length).toBeGreaterThan(0); - for (const params of calls) { - expect(params.$top).toBe(nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top); - } + // The walk read one probe row past the ceiling — that is what makes the + // cut a fact about the rows in hand rather than a guess — and asked no + // single read for more than one step (objectstack: a hosted maximum page + // size refuses a `$top` of 10,001). + const wanted = nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top; + const tops = calls.map((params) => params.$top); + expect(tops.reduce((sum, top) => sum + top, 0)).toBe(wanted); + for (const top of tops) expect(top).toBeLessThanOrEqual(GANTT_FETCH_STEP); + expect(calls.map((params) => params.$skip ?? 0)).toEqual( + tops.map((_, i) => tops.slice(0, i).reduce((sum, top) => sum + top, 0)), + ); // ⭐ The half the ruling cares about: the truncation is NOT silent. const note = await screen.findByRole('note'); @@ -410,11 +427,60 @@ describe('ObjectGantt.rowCeiling-7210', () => { ); expect(calls.length).toBeGreaterThan(0); for (const params of calls) { - expect(params.$top).toBe(GANTT_ROW_CEILING + 1); + expect(params.$top).toBe(GANTT_FETCH_STEP); } expect(screen.queryByRole('note')).toBeNull(); }); + /** + * objectstack — the hosted refusal. A host with a maximum page size (the + * hosted plane sets `OS_DATA_MAX_PAGE_SIZE` to 5,000) refuses a `$top` + * over it before running the query, so a single read of the ceiling plus + * its probe row (10,001) put the refusal in place of the chart, on an + * EMPTY object as on a full one. + * + * REVERSE VERIFICATION: sending `...nonGridRowCeilingQuery(GANTT_ROW_CEILING)` + * as one read again turns all three of these red at the drawn count, with + * the refusal on screen. + */ + describe('a host with a maximum page size of 5,000', () => { + const HOSTED_MAX_PAGE_SIZE = 5000; + + it('draws an empty object instead of the refusal', async () => { + render(); + + await waitFor(() => + expect(screen.getByTestId('gantt-view').getAttribute('data-task-count')).toBe('0'), + ); + expect(screen.queryByText(/maximum page size/)).toBeNull(); + expect(calls).toHaveLength(1); + }); + + it('draws a set larger than one host page, every row', async () => { + const rows = HOSTED_MAX_PAGE_SIZE + 1234; + render(); + + await waitFor(() => + expect(screen.getByTestId('gantt-view').getAttribute('data-task-count')).toBe(String(rows)), + ); + expect(screen.queryByRole('note')).toBeNull(); + for (const params of calls) expect(params.$top).toBeLessThanOrEqual(HOSTED_MAX_PAGE_SIZE); + }); + + it('past the gantt ceiling: draws the ceiling and still says so', async () => { + render(); + + await waitFor(() => + expect(screen.getByTestId('gantt-view').getAttribute('data-task-count')).toBe( + String(GANTT_ROW_CEILING), + ), + ); + const note = await screen.findByRole('note'); + expect(note.textContent).toContain(String(GANTT_ROW_CEILING)); + expect(note.textContent).toContain(String(TOTAL_ROWS)); + }); + }); + /** * Ruling B keeps ruling a′'s "not authorable": an authored * `pagination.pageSize` (or `limit`) does not reach the gantt's query in @@ -432,7 +498,7 @@ describe('ObjectGantt.rowCeiling-7210', () => { expect(screen.getByTestId('gantt-view').getAttribute('data-task-count')).toBe('4999'), ); for (const params of calls) { - expect(params.$top).toBe(GANTT_ROW_CEILING + 1); + expect(params.$top).toBe(GANTT_FETCH_STEP); } expect(screen.queryByRole('note')).toBeNull(); unmount(); @@ -641,10 +707,11 @@ describe('ObjectGantt.hostDataProp-7210', () => { await waitFor(() => expect(calls.length).toBeGreaterThan(0)); for (const params of calls) { - // Not `pagination.pageSize` (2), and not absent either: the platform - // ceiling, one probe row wide so the cut is detectable. + // Not `pagination.pageSize` (2), and not absent either: the walk's + // first step toward the platform ceiling. Five rows answer it short, + // so it is the only read. expect(params.$top).not.toBe(2); - expect(params.$top).toBe(nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top); + expect(params.$top).toBe(GANTT_FETCH_STEP); expect(params.$skip).toBeUndefined(); } }); diff --git a/ui/packages/plugin-gantt/src/ObjectGantt.tsx b/ui/packages/plugin-gantt/src/ObjectGantt.tsx index 24cf7bda203..241392af2bc 100644 --- a/ui/packages/plugin-gantt/src/ObjectGantt.tsx +++ b/ui/packages/plugin-gantt/src/ObjectGantt.tsx @@ -76,7 +76,6 @@ import { resolveAffordance, type SchemaLike, applyNonGridRowCeiling, - nonGridRowCeilingQuery, GANTT_ROW_CEILING, type NonGridCeilingResult, isRealCalendarDate, @@ -97,6 +96,7 @@ import { } from '@object-ui/fields'; import { GanttView, type GanttTask, type GanttDependency, type GanttLinkType, type GanttTaskType } from './GanttView'; import { invertFrom, invertTo, makeTzShift } from './tzShift'; +import { fetchGanttRows } from './ganttRowWalk'; import { ResourceWorkload } from './ResourceWorkload'; import { QuickFilterBar, type QuickFilterField, type QuickFilterOption } from './QuickFilterBar'; import type { WorkingCalendar } from './scheduling'; @@ -1070,22 +1070,27 @@ export const ObjectGantt: React.FC = ({ const expand = !perms?.isLoaded || !resource ? expandable : expandable.filter((f) => perms.checkField(resource, f, 'read')); - const result = await adapter.find(resource, { + // The gantt's own ceiling, `GANTT_ROW_CEILING` (objectstack#32421, + // ruling B), not the shared `NON_GRID_ROW_CEILING` the + // non-virtualised tree prices (objectui#7210, ruling a′). The gantt + // still fetches the whole FILTERED result set — a truthful + // `min(start) → max(end)` range and the group rollups need all of it, + // which is why paging this at `pagination.pageSize` was rejected — but + // "the whole result set" stops at a number instead of at whatever the + // table happens to hold. The walk reads one probe row past the ceiling, + // which is what makes the cut DETECTABLE; `applyNonGridRowCeiling` + // slices it back off at the SAME ceiling. ⛔ Not authorable: an + // authored `limit` / `pagination.pageSize` still cannot reach this + // query, and neither does a datasource's `external.rowCap`, by both + // rulings. + // + // ⛔ The ceiling is a stop condition, not a request size: the walk asks + // in `GANTT_FETCH_STEP` steps, because a host with a maximum page size + // (hosted: 5,000) refuses one `$top` of 10,001 outright, empty object + // or not. See `./ganttRowWalk`. + const result = await fetchGanttRows((params) => adapter.find(resource, params), { $filter: queryFilter, $orderby: convertSortToQueryParams(schema.sort), - // The gantt's own ceiling, `GANTT_ROW_CEILING` (objectstack#32421, - // ruling B), not the shared `NON_GRID_ROW_CEILING` the - // non-virtualised tree prices (objectui#7210, ruling a′). The gantt - // still fetches the whole FILTERED result set — a truthful - // `min(start) → max(end)` range and the group rollups need all of it, - // which is why paging this at `pagination.pageSize` was rejected — but - // "the whole result set" stops at a number instead of at whatever the - // table happens to hold. One probe row past the ceiling is what makes - // the cut DETECTABLE; `applyNonGridRowCeiling` slices it back off at - // the SAME ceiling. ⛔ Not authorable: an authored `limit` / - // `pagination.pageSize` still cannot reach this query, and neither - // does a datasource's `external.rowCap`, by both rulings. - ...nonGridRowCeilingQuery(GANTT_ROW_CEILING), ...(expand.length > 0 ? { $expand: expand } : {}), // objectui#10250 — the term and its field narrowing, sent together // or not at all. See `searchTerm` above. @@ -1095,7 +1100,7 @@ export const ObjectGantt: React.FC = ({ ...(searchFields ? { $searchFields: searchFields } : {}), } : {}), - }); + }, isCurrent); const capped = applyNonGridRowCeiling(result, GANTT_ROW_CEILING); if (isCurrent()) { setData(capped.rows); diff --git a/ui/packages/plugin-gantt/src/ganttRowWalk.ts b/ui/packages/plugin-gantt/src/ganttRowWalk.ts new file mode 100644 index 00000000000..da386eea470 --- /dev/null +++ b/ui/packages/plugin-gantt/src/ganttRowWalk.ts @@ -0,0 +1,82 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { + extractRecords, + nonGridRowCeilingQuery, + GANTT_ROW_CEILING, +} from '@object-ui/core'; + +/** + * The most rows ONE gantt read asks for: the shared non-grid ceiling plus its + * probe row (2,001), the same single request the calendar, map, tree and + * timesheet send. + * + * The gantt's own ceiling ({@link GANTT_ROW_CEILING}, 10,000, objectstack#32421 + * ruling B) is a stop condition, not a request size. It used to be sent as one + * `$top` of 10,001, and a host that sets a maximum page size refuses any read + * over it before running the query (`OS_DATA_MAX_PAGE_SIZE`, 400 + * `MAX_RECORDS_EXCEEDED`, never clamped). The hosted plane sets 5,000, so every + * hosted gantt showed that refusal in place of the chart, an EMPTY one too: the + * refusal is decided by the `$top` asked for, not by the rows the object holds. + * + * So the gantt walks its result set in steps of this size, and asks no single + * read for more than the other non-grid views already ask for. A deployment + * whose maximum page size is below this refuses all five views alike; the + * gantt is no longer the one view that needs a larger page than the rest. + */ +export const GANTT_FETCH_STEP: number = nonGridRowCeilingQuery().$top; + +/** + * Read the gantt's filtered result set up to {@link GANTT_ROW_CEILING} plus + * the probe row, in steps of at most {@link GANTT_FETCH_STEP} rows. + * + * `$top` is the step (or the rows still wanted, at the end), `$skip` moves by + * the rows that came back, and the walk stops at the first short answer or one + * row past the ceiling, so `applyNonGridRowCeiling(result, GANTT_ROW_CEILING)` + * still reads truncation from the rows in hand. A platform read that carries + * `limit` or `offset` is a partition of the result set (the drivers append the + * `id` tie-breaker), so the steps neither repeat nor skip a row. + * + * A first answer that already settles it is returned AS IS, envelope and all: + * a short one (the whole set fits one step, the common case) or one longer than + * the step (an adapter that ignores `$top`, which the ceiling then slices). + * Only a walk of several steps is re-wrapped, as `{ data, total? }`, with the + * newest `total` an answer reported. + * + * `isCurrent` is asked after every answer: when a newer reload owns the screen + * the walk stops asking, and its caller discards what it has. + */ +export async function fetchGanttRows( + find: (params: Record) => Promise, + params: Record, + isCurrent: () => boolean = () => true, +): Promise { + const wanted = GANTT_ROW_CEILING + 1; + const firstTop = Math.min(GANTT_FETCH_STEP, wanted); + const first = await find({ ...params, $top: firstTop }); + const firstRows = extractRecords(first); + if (firstRows.length !== firstTop || !isCurrent()) return first; + + const data: unknown[] = [...firstRows]; + let total = reportedTotal(first); + while (data.length < wanted) { + const top = Math.min(GANTT_FETCH_STEP, wanted - data.length); + const result = await find({ ...params, $top: top, $skip: data.length }); + const rows = extractRecords(result); + total = reportedTotal(result) ?? total; + data.push(...rows); + if (rows.length < top || !isCurrent()) break; + } + return total !== undefined ? { data, total } : { data }; +} + +function reportedTotal(result: unknown): number | undefined { + const total = result && typeof result === 'object' ? (result as { total?: unknown }).total : undefined; + return typeof total === 'number' ? total : undefined; +} From c84c8fece1f82fc21ca1c7c2faee63e49dabd3df Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:38:16 +0000 Subject: [PATCH 2/2] fix(gantt): walk a multi-step gantt read under a unique order and keep each row once A full first read is re-walked from the top with id appended to the authored $orderby (last key's direction), and rows are kept once by id, so offset steps over tied sort keys cannot repeat or drop a row on an adapter that does not break ties itself. A set that fits one read is still one read, as authored. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01JQwxbaTVGEEt267uyT6ufm --- .changeset/gantt-paged-ceiling-read.md | 2 +- .../src/ObjectGantt.harness.test.tsx | 16 +-- .../plugin-gantt/src/ganttRowWalk.test.ts | 101 ++++++++++++++++++ ui/packages/plugin-gantt/src/ganttRowWalk.ts | 68 +++++++++--- 4 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 ui/packages/plugin-gantt/src/ganttRowWalk.test.ts diff --git a/.changeset/gantt-paged-ceiling-read.md b/.changeset/gantt-paged-ceiling-read.md index 573b1bc4ea5..2b698f3fc54 100644 --- a/.changeset/gantt-paged-ceiling-read.md +++ b/.changeset/gantt-paged-ceiling-read.md @@ -10,4 +10,4 @@ Metadata-protocol: none The gantt read its whole filtered result set, up to its 10,000-row ceiling, as ONE read of 10,001 rows (the ceiling plus the probe row that makes a cut detectable). A host that sets a maximum page size (`OS_DATA_MAX_PAGE_SIZE`; the hosted plane sets 5,000) refuses any read over it before running the query, so on every such host the gantt drew "Query parameter 'top' asks for 10001 rows; this deployment's maximum page size is 5000…" in place of the chart, for an empty object as much as a full one. -The gantt now walks to its ceiling in steps of at most 2,001 rows, the one read the calendar, map, tree and timesheet already send: `$skip` moves by the rows that came back, and the walk stops at the first short answer or one row past the ceiling. The ceiling, the footnote that names a cut, and the rule that an authored `pagination.pageSize` or `limit` never reaches the read are unchanged; a result set that fits one step is still one read. +The gantt now asks no single read for more than 2,001 rows, the one read the calendar, map, tree and timesheet already send. A result set that fits that read is one read, as before. A larger one is walked from the top in steps of that size under a unique order (the authored sort with `id` appended as the tie-breaker) and kept once per row, so a step can neither repeat nor skip a row; the walk stops at the first short answer or one row past the ceiling. The ceiling, the footnote that names a cut, and the rule that an authored `pagination.pageSize` or `limit` never reaches the read are unchanged. diff --git a/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx b/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx index 9fb07ce4ea1..ef3fd0fcc43 100644 --- a/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx +++ b/ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx @@ -386,17 +386,21 @@ describe('ObjectGantt.rowCeiling-7210', () => { ), ); - // The walk read one probe row past the ceiling — that is what makes the - // cut a fact about the rows in hand rather than a guess — and asked no - // single read for more than one step (objectstack: a hosted maximum page - // size refuses a `$top` of 10,001). + // The first read came back full, so the set was walked from the top + // under a unique order. The walk read one probe row past the ceiling — + // that is what makes the cut a fact about the rows in hand rather than a + // guess — and asked no single read for more than one step (a hosted + // maximum page size refuses a `$top` of 10,001). const wanted = nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top; - const tops = calls.map((params) => params.$top); + expect(calls[0].$top).toBe(GANTT_FETCH_STEP); + const walk = calls.slice(1); + const tops = walk.map((params) => params.$top); expect(tops.reduce((sum, top) => sum + top, 0)).toBe(wanted); for (const top of tops) expect(top).toBeLessThanOrEqual(GANTT_FETCH_STEP); - expect(calls.map((params) => params.$skip ?? 0)).toEqual( + expect(walk.map((params) => params.$skip ?? 0)).toEqual( tops.map((_, i) => tops.slice(0, i).reduce((sum, top) => sum + top, 0)), ); + for (const params of walk) expect(params.$orderby).toEqual({ id: 'asc' }); // ⭐ The half the ruling cares about: the truncation is NOT silent. const note = await screen.findByRole('note'); diff --git a/ui/packages/plugin-gantt/src/ganttRowWalk.test.ts b/ui/packages/plugin-gantt/src/ganttRowWalk.test.ts new file mode 100644 index 00000000000..7b4256ed827 --- /dev/null +++ b/ui/packages/plugin-gantt/src/ganttRowWalk.test.ts @@ -0,0 +1,101 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * The gantt's stepped read (`fetchGanttRows`): no single read asks for more + * than `GANTT_FETCH_STEP` rows, a set that fits one step is one read answered + * as is, and a set walked in steps is read under a unique order and kept once + * per `id`, so offset steps over tied sort keys can neither repeat nor drop a + * row. + */ + +import { describe, it, expect, vi } from 'vitest'; +import { GANTT_ROW_CEILING, applyNonGridRowCeiling } from '@object-ui/core'; +import { GANTT_FETCH_STEP, fetchGanttRows, withIdTieBreak } from './ganttRowWalk'; + +type Row = { id: string; stage: string }; + +function rows(n: number): Row[] { + return Array.from({ length: n }, (_, i) => ({ id: String(i + 1), stage: i % 2 ? 'b' : 'a' })); +} + +/** Honours `$top` / `$skip`; sorts by `$orderby` only when it names `id`. */ +function adapter(store: Row[]) { + return vi.fn(async (params: Record) => { + const ordered = params.$orderby && 'id' in params.$orderby + ? [...store].sort((x, y) => Number(x.id) - Number(y.id)) + : store; + const skip = params.$skip ?? 0; + return { data: ordered.slice(skip, skip + params.$top), total: store.length }; + }); +} + +describe('fetchGanttRows', () => { + it('a set that fits one step is ONE read, answered as is, with the authored order untouched', async () => { + const find = adapter(rows(7)); + const answer = await fetchGanttRows(find, { $orderby: { stage: 'desc' } }); + expect(find).toHaveBeenCalledTimes(1); + expect(find.mock.calls[0][0]).toEqual({ $orderby: { stage: 'desc' }, $top: GANTT_FETCH_STEP }); + expect(answer).toEqual({ data: rows(7), total: 7 }); + }); + + it('never asks one read for more than a step, and stops one row past the ceiling', async () => { + const find = adapter(rows(GANTT_ROW_CEILING + 300)); + const answer = await fetchGanttRows(find, {}); + for (const [params] of find.mock.calls) expect(params.$top).toBeLessThanOrEqual(GANTT_FETCH_STEP); + const capped = applyNonGridRowCeiling(answer, GANTT_ROW_CEILING); + expect(capped.rows).toHaveLength(GANTT_ROW_CEILING); + expect(capped.truncated).toBe(true); + expect(capped.total).toBe(GANTT_ROW_CEILING + 300); + }); + + it('walks under a unique order: the authored keys, then `id` in the last key’s direction', async () => { + const find = adapter(rows(GANTT_FETCH_STEP + 5)); + await fetchGanttRows(find, { $orderby: { stage: 'desc' } }); + const walk = find.mock.calls.slice(1).map(([params]) => params); + expect(walk.length).toBeGreaterThan(0); + for (const params of walk) expect(params.$orderby).toEqual({ stage: 'desc', id: 'desc' }); + expect(walk[0].$skip).toBeUndefined(); + }); + + it('keeps a row once when tied keys move it across a step boundary', async () => { + const store = rows(GANTT_FETCH_STEP + 10); + // An adapter that breaks ties differently on each read: every step answers + // the row just before its window again, as an unstable order can. + const find = vi.fn(async (params: Record) => { + const skip = params.$skip ?? 0; + const from = Math.max(0, skip - 1); + return { data: store.slice(from, from + params.$top), total: store.length }; + }); + const answer = (await fetchGanttRows(find, {})) as { data: Row[] }; + const ids = answer.data.map((row) => row.id); + expect(new Set(ids).size).toBe(ids.length); + }); + + it('stops asking once a newer reload owns the screen', async () => { + const find = adapter(rows(GANTT_ROW_CEILING + 300)); + let current = true; + find.mockImplementation(async (params: Record) => { + if (find.mock.calls.length === 2) current = false; + const skip = params.$skip ?? 0; + const store = rows(GANTT_ROW_CEILING + 300); + return { data: store.slice(skip, skip + params.$top), total: store.length }; + }); + await fetchGanttRows(find, {}, () => current); + expect(find).toHaveBeenCalledTimes(2); + }); +}); + +describe('withIdTieBreak', () => { + it('appends `id` ascending when nothing was authored', () => { + expect(withIdTieBreak(undefined)).toEqual({ id: 'asc' }); + }); + it('leaves an order that already names `id` alone', () => { + expect(withIdTieBreak({ id: 'desc', stage: 'asc' })).toEqual({ id: 'desc', stage: 'asc' }); + }); +}); diff --git a/ui/packages/plugin-gantt/src/ganttRowWalk.ts b/ui/packages/plugin-gantt/src/ganttRowWalk.ts index da386eea470..fe2704e9a26 100644 --- a/ui/packages/plugin-gantt/src/ganttRowWalk.ts +++ b/ui/packages/plugin-gantt/src/ganttRowWalk.ts @@ -36,18 +36,24 @@ export const GANTT_FETCH_STEP: number = nonGridRowCeilingQuery().$top; * Read the gantt's filtered result set up to {@link GANTT_ROW_CEILING} plus * the probe row, in steps of at most {@link GANTT_FETCH_STEP} rows. * - * `$top` is the step (or the rows still wanted, at the end), `$skip` moves by - * the rows that came back, and the walk stops at the first short answer or one - * row past the ceiling, so `applyNonGridRowCeiling(result, GANTT_ROW_CEILING)` - * still reads truncation from the rows in hand. A platform read that carries - * `limit` or `offset` is a partition of the result set (the drivers append the - * `id` tie-breaker), so the steps neither repeat nor skip a row. + * The first read is the query as authored, `$top` the step. When it answers + * short (the whole set fits one step, the common case) or longer than the step + * (an adapter that ignores `$top`, which the ceiling then slices), it settles + * the set and is returned AS IS, envelope and all: one read, exactly as before. * - * A first answer that already settles it is returned AS IS, envelope and all: - * a short one (the whole set fits one step, the common case) or one longer than - * the step (an adapter that ignores `$top`, which the ceiling then slices). - * Only a walk of several steps is re-wrapped, as `{ data, total? }`, with the - * newest `total` an answer reported. + * Only a FULL first answer means more rows exist, and then the set is walked + * from the top under a UNIQUE order: the authored `$orderby` with `id` appended + * as the tie-breaker, in the last key's direction (`asc` when there is none). + * Offset steps over an order with ties may repeat one row and skip another on + * an adapter that does not break ties itself (the platform's drivers append the + * same `id` key to a paged read, so there it changes nothing). The first read + * is not reused, because it was not taken under that order. Rows are kept once + * by `id` (`_id`); a row with neither is kept as it comes. `$top` is the step + * (or the rows still wanted, at the end), `$skip` moves by the rows that came + * back, and the walk stops at the first short answer or one row past the + * ceiling, so `applyNonGridRowCeiling(result, GANTT_ROW_CEILING)` still reads + * truncation from the rows in hand. The walk's answer is `{ data, total? }`, + * with the newest `total` a read reported. * * `isCurrent` is asked after every answer: when a newer reload owns the screen * the walk stops asking, and its caller discards what it has. @@ -60,22 +66,52 @@ export async function fetchGanttRows( const wanted = GANTT_ROW_CEILING + 1; const firstTop = Math.min(GANTT_FETCH_STEP, wanted); const first = await find({ ...params, $top: firstTop }); - const firstRows = extractRecords(first); - if (firstRows.length !== firstTop || !isCurrent()) return first; + if (extractRecords(first).length !== firstTop || !isCurrent()) return first; - const data: unknown[] = [...firstRows]; + const ordered = { ...params, $orderby: withIdTieBreak(params.$orderby) }; + const data: unknown[] = []; + const seen = new Set(); + let read = 0; let total = reportedTotal(first); while (data.length < wanted) { const top = Math.min(GANTT_FETCH_STEP, wanted - data.length); - const result = await find({ ...params, $top: top, $skip: data.length }); + const result = await find({ ...ordered, $top: top, ...(read > 0 ? { $skip: read } : {}) }); const rows = extractRecords(result); total = reportedTotal(result) ?? total; - data.push(...rows); + read += rows.length; + for (const row of rows) { + const id = rowId(row); + if (id !== undefined) { + if (seen.has(id)) continue; + seen.add(id); + } + data.push(row); + } if (rows.length < top || !isCurrent()) break; } return total !== undefined ? { data, total } : { data }; } +/** + * `$orderby` with `id` appended as the last key, in the last authored key's + * direction (`asc` when nothing was authored). An order that already names + * `id` is returned unchanged. + */ +export function withIdTieBreak(orderby: unknown): Record { + const keys = orderby && typeof orderby === 'object' && !Array.isArray(orderby) + ? (orderby as Record) + : {}; + if ('id' in keys) return { ...keys }; + const directions = Object.values(keys); + return { ...keys, id: directions.length > 0 ? directions[directions.length - 1] : 'asc' }; +} + +function rowId(row: unknown): string | undefined { + if (!row || typeof row !== 'object') return undefined; + const id = (row as { id?: unknown; _id?: unknown }).id ?? (row as { _id?: unknown })._id; + return id === undefined || id === null ? undefined : String(id); +} + function reportedTotal(result: unknown): number | undefined { const total = result && typeof result === 'object' ? (result as { total?: unknown }).total : undefined; return typeof total === 'number' ? total : undefined;