AI-native design editor. Open-source Figma alternative.
fix(tools): guard checkpoint identities and share input schemas
Danila Poyarkova91647b8521d
diff --git a/packages/core/src/tools/analyze/diff.ts b/packages/core/src/tools/analyze/diff.tsindex 9a4f7253a..1dee4019e 100644--- a/packages/core/src/tools/analyze/diff.ts+++ b/packages/core/src/tools/analyze/diff.ts@@ -6,7 +6,7 @@ import type { SceneNode } from '@open-pencil/scene-graph'import { colorToHex, parseColor } from '#core/color'import type { FigmaAPI } from '#core/figma-api'-import { toolNumber, nodeIdInput } from '#core/tools/input'+import { toolNumber, nodeIdInput, nodeComparisonInput } from '#core/tools/input'import { defineTool } from '#core/tools/schema'function serializePaintProps(raw: SceneNode, lines: string[]): void {@@ -128,8 +128,7 @@ export const diffCreate = defineTool({execution: { kind: 'sync', mutation: 'none' },exposure: { webmcp: false },input: v.object({- from: v.pipe(v.string(), v.description('Source node ID')),- to: v.pipe(v.string(), v.description('Target node ID')),+ ...nodeComparisonInput.entries,depth: v.optional(toolNumber(v.pipe(v.number(), v.description('Max tree depth (default: 10)'))))}),execute: (figma, args) => {diff --git a/packages/core/src/tools/input.ts b/packages/core/src/tools/input.tsindex 4d3d1b6d3..ff428f144 100644--- a/packages/core/src/tools/input.ts+++ b/packages/core/src/tools/input.ts@@ -2,6 +2,17 @@ import * as v from 'valibot'export const nodeIdInput = v.pipe(v.string(), v.description('Node ID'))export const nodeInput = v.object({ id: nodeIdInput })+export const nodeComparisonInput = v.object({+ from: v.pipe(v.string(), v.description('Source node ID')),+ to: v.pipe(v.string(), v.description('Target node ID'))+})++export function nodeTraversalInput(depthDescription: string) {+ return v.object({+ id: nodeIdInput,+ depth: v.optional(toolNumber(v.pipe(v.number(), v.description(depthDescription))))+ })+}export const positionInputs = {x: toolNumber(v.pipe(v.number(), v.description('X position'))),y: toolNumber(v.pipe(v.number(), v.description('Y position')))diff --git a/packages/core/src/tools/read/jsx.ts b/packages/core/src/tools/read/jsx.tsindex 4622df03f..e4fa06377 100644--- a/packages/core/src/tools/read/jsx.ts+++ b/packages/core/src/tools/read/jsx.ts@@ -2,7 +2,7 @@ import { createTwoFilesPatch } from 'diff'import * as v from 'valibot'import { sceneNodeToJSX } from '#core/io/formats/jsx'-import { nodeIdInput } from '#core/tools/input'+import { nodeIdInput, nodeComparisonInput } from '#core/tools/input'import { defineTool } from '#core/tools/schema'const MAX_JSX_LENGTH = 12_000@@ -46,10 +46,7 @@ export const diffJSX = defineTool({'Structural diff between two nodes in JSX format. Shows added/removed children, changed props.',execution: { kind: 'sync', mutation: 'none' },exposure: { webmcp: false },- input: v.object({- from: v.pipe(v.string(), v.description('Source node ID')),- to: v.pipe(v.string(), v.description('Target node ID'))- }),+ input: nodeComparisonInput,execute: (figma, { from, to }) => {const fromNode = figma.getNodeById(from)if (!fromNode) return { error: `Node "${from}" not found` }diff --git a/packages/core/src/tools/structure/basic.ts b/packages/core/src/tools/structure/basic.tsindex 225003d0a..b41db6c31 100644--- a/packages/core/src/tools/structure/basic.ts+++ b/packages/core/src/tools/structure/basic.ts@@ -60,8 +60,7 @@ export const nodeBounds = defineTool({input: nodeInput,execute: (figma, { id }) => {const node = figma.getNodeById(id)- if (!node) return { error: `Node "${id}" not found` }- return { id, bounds: node.absoluteBoundingBox }+ return node ? { id, bounds: node.absoluteBoundingBox } : nodeNotFound(id)}})diff --git a/packages/core/src/tools/structure/tree.ts b/packages/core/src/tools/structure/tree.tsindex a74785f2d..c0a5c3e3f 100644--- a/packages/core/src/tools/structure/tree.ts+++ b/packages/core/src/tools/structure/tree.ts@@ -1,7 +1,5 @@-import * as v from 'valibot'-import type { FigmaNodeProxy } from '#core/figma-api'-import { toolNumber, nodeIdInput, nodeInput } from '#core/tools/input'+import { nodeTraversalInput, nodeInput } from '#core/tools/input'import { defineTool, getRawNodeOrError, nodeNotFound, nodeSummary } from '#core/tools/schema'export const nodeAncestors = defineTool({@@ -9,10 +7,7 @@ export const nodeAncestors = defineTool({description: 'Get the ancestor chain from a node to the page root.',execution: { kind: 'sync', mutation: 'none' },exposure: { webmcp: false },- input: v.object({- id: nodeIdInput,- depth: v.optional(toolNumber(v.pipe(v.number(), v.description('Max depth to traverse'))))- }),+ input: nodeTraversalInput('Max depth to traverse'),execute: (figma, args) => {const node = figma.getNodeById(args.id)if (!node) return { error: `Node "${args.id}" not found` }@@ -46,12 +41,7 @@ export const nodeTree = defineTool({description: 'Get a node tree with types and hierarchy.',execution: { kind: 'sync', mutation: 'none' },exposure: { webmcp: false },- input: v.object({- id: nodeIdInput,- depth: v.optional(- toolNumber(v.pipe(v.number(), v.description('Max depth (default: unlimited)')))- )- }),+ input: nodeTraversalInput('Max depth (default: unlimited)'),execute: (figma, args) => {const node = figma.getNodeById(args.id)if (!node) return { error: `Node "${args.id}" not found` }diff --git a/packages/scene-graph/src/checkpoint.ts b/packages/scene-graph/src/checkpoint.tsindex 2b2b89a81..4e39eb67e 100644--- a/packages/scene-graph/src/checkpoint.ts+++ b/packages/scene-graph/src/checkpoint.ts@@ -32,6 +32,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {const after = graph.nodes.get(id)if (!after ||+ after !== nodeRefs.get(id) ||before.id !== after.id ||before.type !== after.type ||before.parentId !== after.parentId ||@@ -45,6 +46,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {const after = graph.variables.get(id)if (!after ||+ after !== variableRefs.get(id) ||before.id !== after.id ||before.collectionId !== after.collectionId ||before.type !== after.typediff --git a/tests/engine/editor/history/atomic-tools.test.ts b/tests/engine/editor/history/atomic-tools.test.tsindex 4dadfd320..9e4045580 100644--- a/tests/engine/editor/history/atomic-tools.test.ts+++ b/tests/engine/editor/history/atomic-tools.test.ts@@ -134,6 +134,31 @@ describe('atomic agent tools', () => {}})+ test.each(['node', 'variable'] as const)('rejects replacement of a captured %s', (kind) => {+ const { graph, figma, editor, undo, rectangle, tool } = setup()+ const collection = graph.createCollection('Test')+ const variable = graph.createVariable('Test', 'STRING', collection.id, '')+ const node = graph.getNode(rectangle.id)+ if (!node) throw new Error('Missing node')+ expect(() =>+ executeAtomicTool(+ editor,+ figma,+ {+ ...tool('set_opacity'),+ execute: () => {+ if (kind === 'node') graph.nodes.set(node.id, structuredClone(node))+ else graph.variables.set(variable.id, structuredClone(variable))+ }+ },+ {}+ )+ ).toThrow('Atomic tools must not change')+ expect(graph.getNode(node.id)).toBe(node)+ expect(graph.variables.get(variable.id)).toBe(variable)+ expect(undo.canUndo).toBe(false)+ })+test('rejects and restores instance index corruption even on reported success', () => {const { graph, figma, editor, undo, tool } = setup()const component = figma.createComponent()