Skip to content

Insert anchored to a previously-moved cell lands after the wrong cell in deriveNotebookDiff #49545

Description

@anp0429

deriveNotebookDiff` (introduced in #49109) places a later insert anchored on a cell that an earlier operation moved relative to the anchor's original slot, not its current position — so the insert lands after whatever cell now occupies that slot.

Repro against current master (b9d22fa), vitest in apps/studio (added to data/content/notebooks/notebook-operations.test.ts):

it('places a later same-anchor insert after the anchor after that anchor moves', () => {
  const notebook: NotebookWire = {
    schema_version: 1,
    cells: [
      { _tag: 'markdown_cell', _id: 'cell-1', text: '# One' },
      { _tag: 'markdown_cell', _id: 'cell-2', text: '# Two' },
      { _tag: 'markdown_cell', _id: 'cell-3', text: '# Three' },
      { _tag: 'markdown_cell', _id: 'cell-4', text: '# Four' },
    ],
  }
 
  const first = { _tag: 'markdown_cell' as const, text: 'first' }
  const second = { _tag: 'markdown_cell' as const, text: 'second' }
 
  const result = deriveNotebookDiff(notebook, [
    { _tag: 'insert_cell', after_cell_id: 'cell-1', cell: first },
    { _tag: 'move_cell', cell_id: 'cell-1', after_cell_id: 'cell-3' },
    { _tag: 'insert_cell', after_cell_id: 'cell-1', cell: second },
  ])
 
  expect(result).toEqual({
    success: true,
    entries: [
      { _tag: 'added', cell: first, operationIndex: 0 },
      { _tag: 'unchanged', cell: notebook.cells[1] },
      { _tag: 'unchanged', cell: notebook.cells[2] },
      { _tag: 'moved', cell: notebook.cells[0], fromIndex: 0, operationIndex: 1 },
      { _tag: 'added', cell: second, operationIndex: 2 },
      { _tag: 'unchanged', cell: notebook.cells[3] },
    ],
  })
})

The second insert is anchored on cell-1, which the previous operation moved after cell-3. Expected: the insert lands directly after cell-1's current position. Actual: it lands after cell-4 — the added/unchanged entries come back swapped:

- Expected                        + Received
  { "_tag": "added",                { "_tag": "unchanged",
    "cell": { "text": "second" },     "cell": { "_id": "cell-4", … } },
    "operationIndex": 2 },          { "_tag": "added",
  { "_tag": "unchanged",              "cell": { "text": "second" },
    "cell": { "_id": "cell-4", … }}    "operationIndex": 2 },

Anchor resolution elsewhere in this module uses the moved cell's current position — the existing test "resolves a move anchored on another moved cell using its new position" passes in the same run. An insert anchored on a moved cell resolves against the pre-move slot instead, so the same anchor means different places depending on the operation type. #49109's description names the guarantee at stake: the diff a user approves cannot disagree with the cells that get written. This is the approval-preview path for assistant-edited notebooks.

First found Aug 20 (master 5d3b849); re-verified today, Aug 25, on current master (b9d22fa) — fails identically throughout, across the id → _id refactor (#49272). The 29 existing tests in the file pass in the same run. Failing-test branch: https://github.com/anp0429/supabase/tree/edgeverdict/notebook-regressions-v2 — happy to open a PR with the test if useful.

Activity

  1. added
    bug
    on Aug 25, 2026
  2. added
    external-issue
    on Aug 25, 2026
  3. added
    frontend
    pr-opened
    and removed on Aug 27, 2026
  4. anp0429 commented on Aug 28, 2026

    @anp0429
    Author

    Fixed by #49694 (86c813e). My regression test from the issue passes on current master. This can close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugexternal-issuefrontendpr-opened

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions