Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

refactor(studio): derive notebook diff entries (#49109)

Charis2e68f2bf6ec7
diff --git a/apps/studio/data/content/notebooks/notebook-operations.test.ts b/apps/studio/data/content/notebooks/notebook-operations.test.ts
index 3fafc6f7431..e77b1d6b56b 100644
--- a/apps/studio/data/content/notebooks/notebook-operations.test.ts
+++ b/apps/studio/data/content/notebooks/notebook-operations.test.ts
@@ -1,6 +1,10 @@
import { describe, expect, it } from 'vitest'
-import { applyNotebookOperations, type NotebookOperation } from './notebook-operations'
+import {
+ applyNotebookOperations,
+ deriveNotebookDiff,
+ type NotebookOperation,
+} from './notebook-operations'
import type { NotebookWire } from './notebook-schema'
const NOTEBOOK: NotebookWire = {
@@ -256,4 +260,262 @@ describe('applyNotebookOperations', () => {
expect(result).toEqual({ success: false, error: { _tag: 'empty_result' } })
})
+
+ it('anchors an insert on a cell that an earlier operation replaced', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'replace_cell', cell_id: 'cell-2', cell: NEW_MARKDOWN_CELL },
+ { _tag: 'insert_cell', after_cell_id: 'cell-2', cell: { _tag: 'markdown_cell', text: 'x' } },
+ ]
+
+ const result = applyNotebookOperations(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ notebook: {
+ schema_version: 1,
+ cells: [
+ NOTEBOOK.cells[0],
+ NEW_MARKDOWN_CELL,
+ { _tag: 'markdown_cell', text: 'x' },
+ NOTEBOOK.cells[2],
+ ],
+ },
+ })
+ })
+
+ it('anchors a move on a cell that an earlier operation replaced', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'replace_cell', cell_id: 'cell-2', cell: NEW_MARKDOWN_CELL },
+ { _tag: 'move_cell', cell_id: 'cell-3', after_cell_id: 'cell-2' },
+ ]
+
+ const result = applyNotebookOperations(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ notebook: {
+ schema_version: 1,
+ cells: [NOTEBOOK.cells[0], NEW_MARKDOWN_CELL, NOTEBOOK.cells[2]],
+ },
+ })
+ })
+
+ it('still rejects an operation targeting a cell that an earlier operation replaced', () => {
+ // A replaced cell can be anchored on, but not targeted — the conflict pre-pass rejects
+ // the batch before any operation runs.
+ const ops: NotebookOperation[] = [
+ { _tag: 'replace_cell', cell_id: 'cell-2', cell: NEW_MARKDOWN_CELL },
+ { _tag: 'delete_cell', cell_id: 'cell-2' },
+ ]
+
+ const result = applyNotebookOperations(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: false,
+ error: { _tag: 'conflicting_operations', cell_id: 'cell-2' },
+ })
+ })
+})
+
+describe('deriveNotebookDiff', () => {
+ it('annotates every cell as unchanged when there are no operations', () => {
+ const result = deriveNotebookDiff(NOTEBOOK, [])
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[0] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[1] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ ],
+ })
+ })
+
+ it('annotates an inserted cell as added in its resulting position', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'insert_cell', after_cell_id: 'cell-1', cell: NEW_MARKDOWN_CELL },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[0] },
+ { _tag: 'added', cell: NEW_MARKDOWN_CELL, operationIndex: 0 },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[1] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ ],
+ })
+ })
+
+ it('annotates a replaced cell in place, keeping both sides', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'replace_cell', cell_id: 'cell-2', cell: NEW_MARKDOWN_CELL },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[0] },
+ {
+ _tag: 'replaced',
+ before: NOTEBOOK.cells[1],
+ after: NEW_MARKDOWN_CELL,
+ operationIndex: 0,
+ },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ ],
+ })
+ })
+
+ it('keeps a deleted cell in the position it used to hold', () => {
+ const ops: NotebookOperation[] = [{ _tag: 'delete_cell', cell_id: 'cell-2' }]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[0] },
+ { _tag: 'removed', cell: NOTEBOOK.cells[1], operationIndex: 0 },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ ],
+ })
+ })
+
+ it('annotates a moved cell with the position it started from', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'move_cell', cell_id: 'cell-1', after_cell_id: 'cell-3' },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[1] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ { _tag: 'moved', cell: NOTEBOOK.cells[0], fromIndex: 0, operationIndex: 0 },
+ ],
+ })
+ })
+
+ it('reports fromIndex against the original notebook, not the shifted working order', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'delete_cell', cell_id: 'cell-1' },
+ { _tag: 'move_cell', cell_id: 'cell-3', after_cell_id: 'start' },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'moved', cell: NOTEBOOK.cells[2], fromIndex: 2, operationIndex: 1 },
+ { _tag: 'removed', cell: NOTEBOOK.cells[0], operationIndex: 0 },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[1] },
+ ],
+ })
+ })
+
+ it('downgrades moves that cancel out to unchanged', () => {
+ // Same operations as the applyNotebookOperations case that lands back on the original
+ // order: nothing actually moved, so nothing should be badged as moved.
+ const ops: NotebookOperation[] = [
+ { _tag: 'move_cell', cell_id: 'cell-1', after_cell_id: 'cell-2' },
+ { _tag: 'move_cell', cell_id: 'cell-2', after_cell_id: 'cell-1' },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result).toEqual({
+ success: true,
+ entries: [
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[0] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[1] },
+ { _tag: 'unchanged', cell: NOTEBOOK.cells[2] },
+ ],
+ })
+ })
+
+ it('keeps reporting moves that do change the order', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'move_cell', cell_id: 'cell-1', after_cell_id: 'cell-3' },
+ { _tag: 'move_cell', cell_id: 'cell-2', after_cell_id: 'cell-1' },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result.success).toBe(true)
+ if (!result.success) return
+ expect(result.entries.map((entry) => entry._tag)).toEqual(['unchanged', 'moved', 'moved'])
+ })
+
+ it('leaves removed entries out of the way of inserts anchored at the same cell', () => {
+ const ops: NotebookOperation[] = [
+ { _tag: 'delete_cell', cell_id: 'cell-2' },
+ {
+ _tag: 'insert_cell',
+ after_cell_id: 'cell-1',
+ cell: { _tag: 'markdown_cell', text: '1st' },
+ },
+ {
+ _tag: 'insert_cell',
+ after_cell_id: 'cell-1',
+ cell: { _tag: 'markdown_cell', text: '2nd' },
+ },
+ ]
+
+ const result = deriveNotebookDiff(NOTEBOOK, ops)
+
+ expect(result.success).toBe(true)
+ if (!result.success) return
+ expect(result.entries.map((entry) => entry._tag)).toEqual([
+ 'unchanged',
+ 'added',
+ 'added',
+ 'removed',
+ 'unchanged',
+ ])
+ // The removed entry sits in the middle of the entry list but must not affect the order
+ // the surviving cells end up in.
+ expect(applyNotebookOperations(NOTEBOOK, ops)).toEqual({
+ success: true,
+ notebook: {
+ schema_version: 1,
+ cells: [
+ NOTEBOOK.cells[0],
+ { _tag: 'markdown_cell', text: '1st' },
+ { _tag: 'markdown_cell', text: '2nd' },
+ NOTEBOOK.cells[2],
+ ],
+ },
+ })
+ })
+
+ it('reports the same errors as applyNotebookOperations', () => {
+ expect(deriveNotebookDiff(NOTEBOOK, [{ _tag: 'delete_cell', cell_id: 'missing' }])).toEqual({
+ success: false,
+ error: { _tag: 'unknown_cell_id', cell_id: 'missing' },
+ })
+ expect(
+ deriveNotebookDiff(NOTEBOOK, [
+ { _tag: 'delete_cell', cell_id: 'cell-2' },
+ { _tag: 'replace_cell', cell_id: 'cell-2', cell: NEW_MARKDOWN_CELL },
+ ])
+ ).toEqual({
+ success: false,
+ error: { _tag: 'conflicting_operations', cell_id: 'cell-2' },
+ })
+ expect(
+ deriveNotebookDiff(NOTEBOOK, [
+ { _tag: 'delete_cell', cell_id: 'cell-1' },
+ { _tag: 'delete_cell', cell_id: 'cell-2' },
+ { _tag: 'delete_cell', cell_id: 'cell-3' },
+ ])
+ ).toEqual({ success: false, error: { _tag: 'empty_result' } })
+ })
})
diff --git a/apps/studio/data/content/notebooks/notebook-operations.ts b/apps/studio/data/content/notebooks/notebook-operations.ts
index 8ff279ac980..2e4391e07dd 100644
--- a/apps/studio/data/content/notebooks/notebook-operations.ts
+++ b/apps/studio/data/content/notebooks/notebook-operations.ts
@@ -67,6 +67,22 @@ export type ApplyNotebookOperationsResult =
| { success: true; notebook: NotebookOperationsResult }
| { success: false; error: NotebookOperationError }
+export type NotebookCellDiffEntry =
+ | { _tag: 'unchanged'; cell: CellWire }
+ | { _tag: 'added'; cell: AgentCell; operationIndex: number }
+ | { _tag: 'removed'; cell: CellWire; operationIndex: number }
+ | { _tag: 'replaced'; before: CellWire; after: AgentCell; operationIndex: number }
+ | {
+ _tag: 'moved'
+ cell: CellWire
+ fromIndex: number
+ operationIndex: number
+ }
+
+export type DeriveNotebookDiffResult =
+ | { success: true; entries: NotebookCellDiffEntry[] }
+ | { success: false; error: NotebookOperationError }
+
export function describeNotebookOperationError(error: NotebookOperationError): string {
switch (error._tag) {
case 'unknown_cell_id':
@@ -89,10 +105,67 @@ function targetCellId(operation: NotebookOperation): string | undefined {
}
}
-export function applyNotebookOperations(
+function findAnchorIndex(entries: NotebookCellDiffEntry[], cellId: string): number {
+ return entries.findIndex((entry) => {
+ switch (entry._tag) {
+ case 'unchanged':
+ case 'moved':
+ return entry.cell.id === cellId
+ case 'replaced':
+ return entry.before.id === cellId
+ case 'added':
+ case 'removed':
+ return false
+ }
+ })
+}
+
+function findTargetCell(
+ entries: NotebookCellDiffEntry[],
+ cellId: string
+): { index: number; cell: CellWire } | undefined {
+ for (let index = 0; index < entries.length; index++) {
+ const entry = entries[index]
+ if (entry._tag !== 'unchanged' && entry._tag !== 'moved') continue
+ if (entry.cell.id === cellId) return { index, cell: entry.cell }
+ }
+ return undefined
+}
+
+function downgradeNoOpMoves(
+ entries: NotebookCellDiffEntry[],
+ notebook: NotebookWire
+): NotebookCellDiffEntry[] {
+ if (!entries.some((entry) => entry._tag === 'moved')) return entries
+
+ const finalOrder = entries.flatMap((entry) =>
+ entry._tag === 'unchanged' || entry._tag === 'moved' ? [entry.cell.id] : []
+ )
+ const survivingIds = new Set(finalOrder)
+ const originalOrder = notebook.cells
+ .map((cell) => cell.id)
+ .filter((cellId) => survivingIds.has(cellId))
+
+ const hasSamePredecessors = (cellId: string) => {
+ const finalPredecessors = new Set(finalOrder.slice(0, finalOrder.indexOf(cellId)))
+ const originalPredecessors = originalOrder.slice(0, originalOrder.indexOf(cellId))
+ return (
+ originalPredecessors.length === finalPredecessors.size &&
+ originalPredecessors.every((predecessor) => finalPredecessors.has(predecessor))
+ )
+ }
+
+ return entries.map((entry) =>
+ entry._tag === 'moved' && hasSamePredecessors(entry.cell.id)
+ ? { _tag: 'unchanged', cell: entry.cell }
+ : entry
+ )
+}
+
+export function deriveNotebookDiff(
notebook: NotebookWire,
operations: NotebookOperation[]
-): ApplyNotebookOperationsResult {
+): DeriveNotebookDiffResult {
const targetedIds = new Set<string>()
for (const operation of operations) {
const cellId = targetCellId(operation)
@@ -104,53 +177,66 @@ export function applyNotebookOperations(
targetedIds.add(cellId)
}
- const cells: OperationResultCell[] = [...notebook.cells]
- const indexOfCellId = (cellId: string) =>
- cells.findIndex((cell) => 'id' in cell && cell.id === cellId)
+ const originalIndexById = new Map(notebook.cells.map((cell, index) => [cell.id, index]))
+ const entries: NotebookCellDiffEntry[] = notebook.cells.map((cell) => ({
+ _tag: 'unchanged',
+ cell,
+ }))
const insertedAfter = new Map<string, number>()
const insertAfter = (
anchor: string,
- item: OperationResultCell
+ entry: NotebookCellDiffEntry
): NotebookOperationError | undefined => {
- const anchorIndex = anchor === CELL_ANCHOR_START ? -1 : indexOfCellId(anchor)
+ const anchorIndex = anchor === CELL_ANCHOR_START ? -1 : findAnchorIndex(entries, anchor)
if (anchor !== CELL_ANCHOR_START && anchorIndex === -1) {
return { _tag: 'unknown_cell_id', cell_id: anchor }
}
const offset = insertedAfter.get(anchor) ?? 0
- cells.splice(anchorIndex + 1 + offset, 0, item)
+ entries.splice(anchorIndex + 1 + offset, 0, entry)
insertedAfter.set(anchor, offset + 1)
return undefined
}
- for (const operation of operations) {
+ for (let operationIndex = 0; operationIndex < operations.length; operationIndex++) {
+ const operation = operations[operationIndex]
+
switch (operation._tag) {
case 'insert_cell': {
- const error = insertAfter(operation.after_cell_id, operation.cell)
+ const error = insertAfter(operation.after_cell_id, {
+ _tag: 'added',
+ cell: operation.cell,
+ operationIndex,
+ })
if (error) return { success: false, error }
break
}
case 'replace_cell': {
- const index = indexOfCellId(operation.cell_id)
- if (index === -1) {
+ const found = findTargetCell(entries, operation.cell_id)
+ if (found === undefined) {
return {
success: false,
error: { _tag: 'unknown_cell_id', cell_id: operation.cell_id },
}
}
- cells[index] = operation.cell
+ entries[found.index] = {
+ _tag: 'replaced',
+ before: found.cell,
+ after: operation.cell,
+ operationIndex,
+ }
break
}
case 'delete_cell': {
- const index = indexOfCellId(operation.cell_id)
- if (index === -1) {
+ const found = findTargetCell(entries, operation.cell_id)
+ if (found === undefined) {
return {
success: false,
error: { _tag: 'unknown_cell_id', cell_id: operation.cell_id },
}
}
- cells.splice(index, 1)
+ entries[found.index] = { _tag: 'removed', cell: found.cell, operationIndex }
break
}
case 'move_cell': {
@@ -161,28 +247,61 @@ export function applyNotebookOperations(
}
}
- const fromIndex = indexOfCellId(operation.cell_id)
- if (fromIndex === -1) {
+ const found = findTargetCell(entries, operation.cell_id)
+ if (found === undefined) {
return {
success: false,
error: { _tag: 'unknown_cell_id', cell_id: operation.cell_id },
}
}
- const [movedCell] = cells.splice(fromIndex, 1)
- const error = insertAfter(operation.after_cell_id, movedCell)
+ entries.splice(found.index, 1)
+ const error = insertAfter(operation.after_cell_id, {
+ _tag: 'moved',
+ cell: found.cell,
+ fromIndex: originalIndexById.get(found.cell.id) ?? found.index,
+ operationIndex,
+ })
if (error) return { success: false, error }
break
}
}
}
- if (cells.length === 0) {
+ if (!entries.some((entry) => entry._tag !== 'removed')) {
return { success: false, error: { _tag: 'empty_result' } }
}
+ return { success: true, entries: downgradeNoOpMoves(entries, notebook) }
+}
+
+function resultingCells(entries: NotebookCellDiffEntry[]): OperationResultCell[] {
+ return entries.flatMap((entry) => {
+ switch (entry._tag) {
+ case 'unchanged':
+ case 'moved':
+ case 'added':
+ return [entry.cell]
+ case 'replaced':
+ return [entry.after]
+ case 'removed':
+ return []
+ }
+ })
+}
+
+export function applyNotebookOperations(
+ notebook: NotebookWire,
+ operations: NotebookOperation[]
+): ApplyNotebookOperationsResult {
+ const result = deriveNotebookDiff(notebook, operations)
+ if (!result.success) return result
+
return {
success: true,
- notebook: { schema_version: notebook.schema_version, cells },
+ notebook: {
+ schema_version: notebook.schema_version,
+ cells: resultingCells(result.entries),
+ },
}
}