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

address review: migration 033 heals widgets/grid drift

Adam Eivy5bc4bc06c21b
diff --git a/scripts/migrations/033-seed-quick-image-widget.js b/scripts/migrations/033-seed-quick-image-widget.js
index d85b237296..f8fcdfab60 100644
--- a/scripts/migrations/033-seed-quick-image-widget.js
+++ b/scripts/migrations/033-seed-quick-image-widget.js
@@ -51,19 +51,34 @@ function pickGridEntry(grid, layoutId) {
function applyToLayout(layout) {
if (!layout || typeof layout !== 'object') return false;
if (!Array.isArray(layout.widgets)) return false;
- if (layout.widgets.includes(WIDGET_ID)) return false;
- layout.widgets = [...layout.widgets, WIDGET_ID];
- // Always insert a grid entry, even for legacy/corrupted layouts whose
- // `grid` is missing or non-array. Without this, the widget would be
- // enabled but unplaced — the client's `synthesizeGrid` auto-flow CAN
- // recover this at render time, but the on-disk state would be
- // inconsistent (widgets includes quick-image, grid has no entry for
- // it) and a subsequent "Save Arrangement" would persist the gap.
- // Initialize grid to [] before picking so pickGridEntry's reduce
- // starts from a clean slate.
+
+ let changed = false;
+ // 1) Insert into widgets if absent.
+ if (!layout.widgets.includes(WIDGET_ID)) {
+ layout.widgets = [...layout.widgets, WIDGET_ID];
+ changed = true;
+ }
+ // 2) Heal the grid entry independently. The previous early-return on
+ // widgets.includes() skipped the realistic legacy/corrupted case where
+ // the widget id was present in `widgets` but missing from `grid` —
+ // either because an older migration only seeded one side, or because
+ // a `widgets`-only edit landed without an arrange-and-save pass. We
+ // detect that here and add the placement entry, so the on-disk state
+ // is always self-consistent. Treat non-array grid as [] (same shape
+ // the client's `synthesizeGrid` would auto-create at render time).
const existingGrid = Array.isArray(layout.grid) ? layout.grid : [];
- layout.grid = [...existingGrid, pickGridEntry(existingGrid, layout.id)];
- return true;
+ const hasGridEntry = existingGrid.some((it) => it?.id === WIDGET_ID);
+ if (!hasGridEntry) {
+ layout.grid = [...existingGrid, pickGridEntry(existingGrid, layout.id)];
+ changed = true;
+ } else if (!Array.isArray(layout.grid)) {
+ // Defensive: widget had an entry in a non-array grid (impossible by
+ // the some() check above unless the grid is itself weird) — normalize
+ // to the existingGrid we just synthesized.
+ layout.grid = existingGrid;
+ changed = true;
+ }
+ return changed;
}
export default {
diff --git a/scripts/migrations/033-seed-quick-image-widget.test.js b/scripts/migrations/033-seed-quick-image-widget.test.js
index 39d7671c35..854b40ce57 100644
--- a/scripts/migrations/033-seed-quick-image-widget.test.js
+++ b/scripts/migrations/033-seed-quick-image-widget.test.js
@@ -87,6 +87,34 @@ describe('migration 033 — seed quick-image widget into default layout', () =>
expect(result.reason).toBe('unreadable');
});
+ it('heals legacy state where widget id is in widgets[] but missing from grid[]', async () => {
+ // Regression: an earlier shape of migration 033 (or any out-of-band
+ // 'widgets'-only edit) could leave the widget id in widgets[] without
+ // the corresponding placement entry in grid[]. Without the heal, the
+ // widget renders only via client-side synthesizeGrid auto-flow, and
+ // any subsequent "Save Arrangement" persists the gap. The migration
+ // should add the missing grid entry on the next run.
+ writeJson(layoutsPath, {
+ activeLayoutId: 'default',
+ layouts: [
+ {
+ id: 'default',
+ name: 'Everything',
+ builtIn: true,
+ widgets: ['quick-brain', 'quick-image'],
+ grid: [{ id: 'quick-brain', x: 0, y: 0, w: 3, h: 2 }],
+ },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.updated).toBe(1);
+ const after = readJson(layoutsPath);
+ const entry = after.layouts[0].grid.find((g) => g.id === 'quick-image');
+ expect(entry).toBeDefined();
+ // widgets[] should not duplicate the id (still exactly one occurrence).
+ expect(after.layouts[0].widgets.filter((w) => w === 'quick-image')).toHaveLength(1);
+ });
+
it('appends below existing items when the preferred slot is occupied by user rearrangement', async () => {
writeJson(layoutsPath, {
activeLayoutId: 'default',