Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: clamp migration 269's ranking inputs to the read path's bounds (#4133)

Adam Eivy3cbb8f75804b
diff --git a/scripts/migrations/269-dashboard-grid-order.js b/scripts/migrations/269-dashboard-grid-order.js
index 33f9ca0cac..a0208b9604 100644
--- a/scripts/migrations/269-dashboard-grid-order.js
+++ b/scripts/migrations/269-dashboard-grid-order.js
@@ -23,11 +23,22 @@
* disk say what the renderer means.
*/
+import {
+ GRID_COLS,
+ GRID_ORDER_MAX,
+ GRID_LEGACY_Y_MAX,
+} from '../../server/services/dashboardLayouts.js';
import { readLayoutsDoc, writeLayoutsDoc } from './_lib.js';
const LABEL = 'migration 269';
const intOr = (v, fallback) => (Number.isFinite(v) ? Math.floor(v) : fallback);
+// Bounds come from the service (the migration-030 convention) rather than
+// being mirrored, so the two can't drift. The read path clamps BEFORE it
+// ranks: two entries at y 300 and y 500 both land on the ceiling there and
+// are separated by column instead, so ranking the raw values here would order
+// them differently than the server already does.
+const clamp = (v, max) => Math.max(0, Math.min(max, intOr(v, 0)));
// Already-converted entries carry `order` and no `y`. Both halves matter: a
// hand-merged file can carry `order` alongside a stale `y`, and that still
@@ -53,11 +64,11 @@ function toOrderedGrid(grid) {
g,
idx,
rank: anyOrder
- ? (Number.isFinite(g.order) ? Math.floor(g.order) : Number.MAX_SAFE_INTEGER)
- : intOr(g.y, 0),
+ ? (Number.isFinite(g.order) ? clamp(g.order, GRID_ORDER_MAX) : Number.MAX_SAFE_INTEGER)
+ : clamp(g.y, GRID_LEGACY_Y_MAX),
// Ties in legacy grids are side-by-side cells: column decides. Ties in
// the new shape are corrupt data: file order decides.
- tie: anyOrder ? idx : intOr(g.x, 0),
+ tie: anyOrder ? idx : clamp(g.x, GRID_COLS - 1),
}))
.sort((a, b) => a.rank - b.rank || a.tie - b.tie || a.idx - b.idx)
.map(({ g }, order) => {
diff --git a/scripts/migrations/269-dashboard-grid-order.test.js b/scripts/migrations/269-dashboard-grid-order.test.js
index c982182b7d..d72ee9f854 100644
--- a/scripts/migrations/269-dashboard-grid-order.test.js
+++ b/scripts/migrations/269-dashboard-grid-order.test.js
@@ -150,6 +150,28 @@ describe('migration 269 — convert dashboard grids to the order-only shape', ()
.toEqual([{ id: 'alpha', x: 2, w: 4, order: 0, h: 7 }]);
});
+ // The read path clamps `y` to GRID_LEGACY_Y_MAX (200) before it ranks, so
+ // two out-of-range entries land on the same ceiling and are separated by
+ // column. Ranking the raw values here would order them the other way and
+ // the conversion would change a layout the server was already serving.
+ it('clamps an out-of-range legacy y the way the read path does', async () => {
+ writeJson(layoutsPath, {
+ activeLayoutId: 'default',
+ layouts: [{
+ id: 'default', name: 'Everything', builtIn: true, widgets: ['alpha', 'beta'],
+ grid: [
+ { id: 'alpha', x: 0, y: 500, w: 6, h: 2 },
+ { id: 'beta', x: 6, y: 300, w: 6, h: 2 },
+ ],
+ }],
+ });
+
+ expect((await migration.up({ rootDir })).updated).toBe(1);
+ // Both clamp to y 200, so the tie falls to column and alpha (x 0) leads.
+ // Ranking the raw values would have put beta (y 300) first.
+ expect(readJson(layoutsPath).layouts[0].grid.map((g) => g.id)).toEqual(['alpha', 'beta']);
+ });
+
it('converts every layout in the file, and leaves empty grids alone', async () => {
writeJson(layoutsPath, {
activeLayoutId: 'focus',