Skip to content

Commit c84c8fe

Browse files
committed
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JQwxbaTVGEEt267uyT6ufm
1 parent 14353fe commit c84c8fe

4 files changed

Lines changed: 164 additions & 23 deletions

File tree

‎.changeset/gantt-paged-ceiling-read.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Metadata-protocol: none
1010

1111
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.
1212

13-
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.
13+
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.

‎ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,21 @@ describe('ObjectGantt.rowCeiling-7210', () => {
386386
),
387387
);
388388

389-
// The walk read one probe row past the ceiling — that is what makes the
390-
// cut a fact about the rows in hand rather than a guess — and asked no
391-
// single read for more than one step (objectstack: a hosted maximum page
392-
// size refuses a `$top` of 10,001).
389+
// The first read came back full, so the set was walked from the top
390+
// under a unique order. The walk read one probe row past the ceiling —
391+
// that is what makes the cut a fact about the rows in hand rather than a
392+
// guess — and asked no single read for more than one step (a hosted
393+
// maximum page size refuses a `$top` of 10,001).
393394
const wanted = nonGridRowCeilingQuery(GANTT_ROW_CEILING).$top;
394-
const tops = calls.map((params) => params.$top);
395+
expect(calls[0].$top).toBe(GANTT_FETCH_STEP);
396+
const walk = calls.slice(1);
397+
const tops = walk.map((params) => params.$top);
395398
expect(tops.reduce((sum, top) => sum + top, 0)).toBe(wanted);
396399
for (const top of tops) expect(top).toBeLessThanOrEqual(GANTT_FETCH_STEP);
397-
expect(calls.map((params) => params.$skip ?? 0)).toEqual(
400+
expect(walk.map((params) => params.$skip ?? 0)).toEqual(
398401
tops.map((_, i) => tops.slice(0, i).reduce((sum, top) => sum + top, 0)),
399402
);
403+
for (const params of walk) expect(params.$orderby).toEqual({ id: 'asc' });
400404

401405
// ⭐ The half the ruling cares about: the truncation is NOT silent.
402406
const note = await screen.findByRole('note');
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* The gantt's stepped read (`fetchGanttRows`): no single read asks for more
11+
* than `GANTT_FETCH_STEP` rows, a set that fits one step is one read answered
12+
* as is, and a set walked in steps is read under a unique order and kept once
13+
* per `id`, so offset steps over tied sort keys can neither repeat nor drop a
14+
* row.
15+
*/
16+
17+
import { describe, it, expect, vi } from 'vitest';
18+
import { GANTT_ROW_CEILING, applyNonGridRowCeiling } from '@object-ui/core';
19+
import { GANTT_FETCH_STEP, fetchGanttRows, withIdTieBreak } from './ganttRowWalk';
20+
21+
type Row = { id: string; stage: string };
22+
23+
function rows(n: number): Row[] {
24+
return Array.from({ length: n }, (_, i) => ({ id: String(i + 1), stage: i % 2 ? 'b' : 'a' }));
25+
}
26+
27+
/** Honours `$top` / `$skip`; sorts by `$orderby` only when it names `id`. */
28+
function adapter(store: Row[]) {
29+
return vi.fn(async (params: Record<string, any>) => {
30+
const ordered = params.$orderby && 'id' in params.$orderby
31+
? [...store].sort((x, y) => Number(x.id) - Number(y.id))
32+
: store;
33+
const skip = params.$skip ?? 0;
34+
return { data: ordered.slice(skip, skip + params.$top), total: store.length };
35+
});
36+
}
37+
38+
describe('fetchGanttRows', () => {
39+
it('a set that fits one step is ONE read, answered as is, with the authored order untouched', async () => {
40+
const find = adapter(rows(7));
41+
const answer = await fetchGanttRows(find, { $orderby: { stage: 'desc' } });
42+
expect(find).toHaveBeenCalledTimes(1);
43+
expect(find.mock.calls[0][0]).toEqual({ $orderby: { stage: 'desc' }, $top: GANTT_FETCH_STEP });
44+
expect(answer).toEqual({ data: rows(7), total: 7 });
45+
});
46+
47+
it('never asks one read for more than a step, and stops one row past the ceiling', async () => {
48+
const find = adapter(rows(GANTT_ROW_CEILING + 300));
49+
const answer = await fetchGanttRows(find, {});
50+
for (const [params] of find.mock.calls) expect(params.$top).toBeLessThanOrEqual(GANTT_FETCH_STEP);
51+
const capped = applyNonGridRowCeiling(answer, GANTT_ROW_CEILING);
52+
expect(capped.rows).toHaveLength(GANTT_ROW_CEILING);
53+
expect(capped.truncated).toBe(true);
54+
expect(capped.total).toBe(GANTT_ROW_CEILING + 300);
55+
});
56+
57+
it('walks under a unique order: the authored keys, then `id` in the last key’s direction', async () => {
58+
const find = adapter(rows(GANTT_FETCH_STEP + 5));
59+
await fetchGanttRows(find, { $orderby: { stage: 'desc' } });
60+
const walk = find.mock.calls.slice(1).map(([params]) => params);
61+
expect(walk.length).toBeGreaterThan(0);
62+
for (const params of walk) expect(params.$orderby).toEqual({ stage: 'desc', id: 'desc' });
63+
expect(walk[0].$skip).toBeUndefined();
64+
});
65+
66+
it('keeps a row once when tied keys move it across a step boundary', async () => {
67+
const store = rows(GANTT_FETCH_STEP + 10);
68+
// An adapter that breaks ties differently on each read: every step answers
69+
// the row just before its window again, as an unstable order can.
70+
const find = vi.fn(async (params: Record<string, any>) => {
71+
const skip = params.$skip ?? 0;
72+
const from = Math.max(0, skip - 1);
73+
return { data: store.slice(from, from + params.$top), total: store.length };
74+
});
75+
const answer = (await fetchGanttRows(find, {})) as { data: Row[] };
76+
const ids = answer.data.map((row) => row.id);
77+
expect(new Set(ids).size).toBe(ids.length);
78+
});
79+
80+
it('stops asking once a newer reload owns the screen', async () => {
81+
const find = adapter(rows(GANTT_ROW_CEILING + 300));
82+
let current = true;
83+
find.mockImplementation(async (params: Record<string, any>) => {
84+
if (find.mock.calls.length === 2) current = false;
85+
const skip = params.$skip ?? 0;
86+
const store = rows(GANTT_ROW_CEILING + 300);
87+
return { data: store.slice(skip, skip + params.$top), total: store.length };
88+
});
89+
await fetchGanttRows(find, {}, () => current);
90+
expect(find).toHaveBeenCalledTimes(2);
91+
});
92+
});
93+
94+
describe('withIdTieBreak', () => {
95+
it('appends `id` ascending when nothing was authored', () => {
96+
expect(withIdTieBreak(undefined)).toEqual({ id: 'asc' });
97+
});
98+
it('leaves an order that already names `id` alone', () => {
99+
expect(withIdTieBreak({ id: 'desc', stage: 'asc' })).toEqual({ id: 'desc', stage: 'asc' });
100+
});
101+
});

‎ui/packages/plugin-gantt/src/ganttRowWalk.ts‎

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ export const GANTT_FETCH_STEP: number = nonGridRowCeilingQuery().$top;
3636
* Read the gantt's filtered result set up to {@link GANTT_ROW_CEILING} plus
3737
* the probe row, in steps of at most {@link GANTT_FETCH_STEP} rows.
3838
*
39-
* `$top` is the step (or the rows still wanted, at the end), `$skip` moves by
40-
* the rows that came back, and the walk stops at the first short answer or one
41-
* row past the ceiling, so `applyNonGridRowCeiling(result, GANTT_ROW_CEILING)`
42-
* still reads truncation from the rows in hand. A platform read that carries
43-
* `limit` or `offset` is a partition of the result set (the drivers append the
44-
* `id` tie-breaker), so the steps neither repeat nor skip a row.
39+
* The first read is the query as authored, `$top` the step. When it answers
40+
* short (the whole set fits one step, the common case) or longer than the step
41+
* (an adapter that ignores `$top`, which the ceiling then slices), it settles
42+
* the set and is returned AS IS, envelope and all: one read, exactly as before.
4543
*
46-
* A first answer that already settles it is returned AS IS, envelope and all:
47-
* a short one (the whole set fits one step, the common case) or one longer than
48-
* the step (an adapter that ignores `$top`, which the ceiling then slices).
49-
* Only a walk of several steps is re-wrapped, as `{ data, total? }`, with the
50-
* newest `total` an answer reported.
44+
* Only a FULL first answer means more rows exist, and then the set is walked
45+
* from the top under a UNIQUE order: the authored `$orderby` with `id` appended
46+
* as the tie-breaker, in the last key's direction (`asc` when there is none).
47+
* Offset steps over an order with ties may repeat one row and skip another on
48+
* an adapter that does not break ties itself (the platform's drivers append the
49+
* same `id` key to a paged read, so there it changes nothing). The first read
50+
* is not reused, because it was not taken under that order. Rows are kept once
51+
* by `id` (`_id`); a row with neither is kept as it comes. `$top` is the step
52+
* (or the rows still wanted, at the end), `$skip` moves by the rows that came
53+
* back, and the walk stops at the first short answer or one row past the
54+
* ceiling, so `applyNonGridRowCeiling(result, GANTT_ROW_CEILING)` still reads
55+
* truncation from the rows in hand. The walk's answer is `{ data, total? }`,
56+
* with the newest `total` a read reported.
5157
*
5258
* `isCurrent` is asked after every answer: when a newer reload owns the screen
5359
* the walk stops asking, and its caller discards what it has.
@@ -60,22 +66,52 @@ export async function fetchGanttRows(
6066
const wanted = GANTT_ROW_CEILING + 1;
6167
const firstTop = Math.min(GANTT_FETCH_STEP, wanted);
6268
const first = await find({ ...params, $top: firstTop });
63-
const firstRows = extractRecords(first);
64-
if (firstRows.length !== firstTop || !isCurrent()) return first;
69+
if (extractRecords(first).length !== firstTop || !isCurrent()) return first;
6570

66-
const data: unknown[] = [...firstRows];
71+
const ordered = { ...params, $orderby: withIdTieBreak(params.$orderby) };
72+
const data: unknown[] = [];
73+
const seen = new Set<string>();
74+
let read = 0;
6775
let total = reportedTotal(first);
6876
while (data.length < wanted) {
6977
const top = Math.min(GANTT_FETCH_STEP, wanted - data.length);
70-
const result = await find({ ...params, $top: top, $skip: data.length });
78+
const result = await find({ ...ordered, $top: top, ...(read > 0 ? { $skip: read } : {}) });
7179
const rows = extractRecords(result);
7280
total = reportedTotal(result) ?? total;
73-
data.push(...rows);
81+
read += rows.length;
82+
for (const row of rows) {
83+
const id = rowId(row);
84+
if (id !== undefined) {
85+
if (seen.has(id)) continue;
86+
seen.add(id);
87+
}
88+
data.push(row);
89+
}
7490
if (rows.length < top || !isCurrent()) break;
7591
}
7692
return total !== undefined ? { data, total } : { data };
7793
}
7894

95+
/**
96+
* `$orderby` with `id` appended as the last key, in the last authored key's
97+
* direction (`asc` when nothing was authored). An order that already names
98+
* `id` is returned unchanged.
99+
*/
100+
export function withIdTieBreak(orderby: unknown): Record<string, 'asc' | 'desc'> {
101+
const keys = orderby && typeof orderby === 'object' && !Array.isArray(orderby)
102+
? (orderby as Record<string, 'asc' | 'desc'>)
103+
: {};
104+
if ('id' in keys) return { ...keys };
105+
const directions = Object.values(keys);
106+
return { ...keys, id: directions.length > 0 ? directions[directions.length - 1] : 'asc' };
107+
}
108+
109+
function rowId(row: unknown): string | undefined {
110+
if (!row || typeof row !== 'object') return undefined;
111+
const id = (row as { id?: unknown; _id?: unknown }).id ?? (row as { _id?: unknown })._id;
112+
return id === undefined || id === null ? undefined : String(id);
113+
}
114+
79115
function reportedTotal(result: unknown): number | undefined {
80116
const total = result && typeof result === 'object' ? (result as { total?: unknown }).total : undefined;
81117
return typeof total === 'number' ? total : undefined;

0 commit comments

Comments
 (0)