Diffable document format: findings on fidelity, identity, and options
Problem
Design files that live in git next to the code need a format git can diff and merge. OpenPencil saves only .fig, a ZIP of binary Kiwi, and #173 tracks .pen as a save target. The roadmap asks for both a first-class .pen save target and Git-friendly structured node-tree diffs (packages/docs/development/roadmap.md, lines ~82 and ~95). This issue collects findings on which format could be diffable without losing Figma fidelity, so the choice can be made with evidence. It compares options and recommends; the decision is the maintainers'.
Context: #727 / #751 export Storybook stories from a document, and #761 keeps each design file next to its component's stories. That is only reviewable in git if the design file is text.
Findings
Probes ran on a .fig written by OpenPencil (component with auto layout, variable-bound fill, text, instance, ellipse, vector network, image fill, second page), decoding and re-encoding with the file's embedded schema.
- The format does not decide fidelity; the SceneGraph conversion does. Decoding the Kiwi
NodeChange[]message to JSON with sorted keys (bytes as hashed blob refs) and encoding it back with the file's own schema gave byte-identical output (1,741 B), in any key order. A plain import → export through SceneGraph instead changed a text node'slineHeightand the pluginData order, which is #578 / #610 at small scale. Any format written from SceneGraph, including today's.figwriter, own JSON, or.pen, inherits that drift. - Node identity is unstable for OpenPencil-authored nodes, and this blocks every diffable option. Nodes never imported from
.figget GUIDs and fractional positions from a traversal counter at save time (generateId()inpackages/scene-graph/src/index.ts:84;export-node.ts:977). Inserting one node at the top of a page renumbered every later node's GUID and position, so every record changed. Imported nodes are stable becausesource.id/orderKeypersist. #750 reports related renumbering of component-property and variable-mode IDs. Persisting IDs and order keys at creation is the first step whichever format is chosen. - Diffs are small once identity is stable. Moving one node by 10 px changed one line in one node record. A second round trip is idempotent. Noise comes from derived data (
derivedTextData,textData, layout versions, fill/stroke geometry blobs,derivedSymbolData), index-based blob references (vectorNetworkBlobindexes intomessage.blobs[]), andmeta.json.createdAt, which is the only non-deterministic part of a written.figtoday. - Semantic divergence (#756). fig-json is faithful to Figma's semantics. An OpenPencil-only edit such as a descendant resize is stored as a Figma-shaped
symbolOverridesclaim that is inert in Figma and looks like a real override in a diff. OpenPencil already has an extension namespace,pluginData[pluginID="open-pencil"](packages/fig/src/node-change/plugin-data.ts:17-25). Writing such edits there instead would stop the inert claim, let export list what Figma won't keep, and make divergence visible in review, especially if the canonical form expands those JSON-string values into objects. An own-JSON format is faithful to OpenPencil's semantics, but the Figma incompatibility then only shows at.figexport. - Parity numbers.
fig.kiwihas 605 definitions, andNodeChangehas 590 fields. The codec models about 118 and preserves 100 raw (FIGMA_RAW_NODE_FIELD_KEYS,packages/fig/src/node-change/convert.ts:1017). Every other field is dropped once a document goes through SceneGraph.packages/docs/reference/file-format.mdstill says "194 definitions / ~390 fields". - Bugs found on the way: #765 (colour variable bindings authored in OpenPencil don't reach
.figascolorVar) and #766 (duplicate order key when inserting among imported siblings).
Options compared
| Option | Figma fidelity | Parity | Diffable | Mergeable | Cost |
|---|---|---|---|---|---|
a. .fig as-is |
Reference; the writer drifts after edits (#578) | Full | No | No | none |
| b. fig-json: canonical JSON of the Kiwi message, the schema stored once, blobs by content hash | Same as .fig; byte-identical re-encode tested |
All 590 fields, including those SceneGraph drops | Good once IDs are stable and derived data sits in sidecars | Good with a file per page or top-level frame | Low–medium: the codec and schema exist |
c. .pen (Pencil.dev) |
Lossy: no rich text runs, prototyping, plugin data, vector networks (SVG paths), component property types | Poor for Figma metadata | Good | Medium | Medium: a writer is needed (#173) |
| d. Own SceneGraph JSON | Same drift as today's writer until #610 | SceneGraph plus the 100 raw fields | Good once IDs persist | Good | Medium–high: schema versioning and migrations, rules for Maps and bytes |
| e. Figma REST JSON | Lossy, derived | Read model | OK | n/a | Not viable: no write API |
Prior art: Penpot's v3 .penpot export (ZIP of per-page and per-shape JSON plus a binary objects/ folder), tldraw's .tldr (records plus a schema with per-record migrations), and Excalidraw (inline base64 images, the pattern to avoid for git).
Proposed solution (a recommendation, not a decision)
Stable identity first: persist
source.idandorderKeywhen a node is created, and keep property and mode IDs stable (#750). This is a prerequisite for any diffable option.Then a fig-json directory as the git-facing format:
- the schema stored once;
NodeChangeJSON per page or per top-level frame, with sorted keys;- blobs as files named by content hash, with index references rewritten;
- derived data in sidecar files that reviewers can mark
-diff; open-pencilplugin data expanded into objects;- no timestamps.
It is the only diffable option shown to be as faithful as
.fig, and it builds on the existing Kiwi code..penas an interop save and export target (#173), not as the fidelity container..figstays the default save and the interop format. Making it byte-reproducible means fixingcreatedAtand zip entry times.
What would change this:
- If #756 settles on OpenPencil deliberately diverging from Figma (option 2 applied broadly), own SceneGraph JSON becomes the primary format and fig-json a derived review artifact.
- If #610 makes SceneGraph round trips lossless, own JSON's readability wins.
- If people or agents will hand-edit the file, fig-json's Figma field names and GUID-path overrides are a poor fit.
Open questions
- Store derived data or regenerate it on load? Storing keeps Figma fidelity. Regenerating keeps diffs clean but depends on OpenPencil's text and layout matching Figma's, and
reader-reopen.mdon #646 shows Figma recomputes hug sizes anyway. - File granularity for very large documents (material3 has ~158k nodes): per page, per top-level frame, or per node. This needs a measurement.
- Does Figma write NaN or Infinity floats? JSON can't represent them without a tagged encoding.
Affected area
.fig import/export
References
#173, #756, #750, #578, #610, #641, #495, #502, #727, #751, #761, #765, #766. Probes and the full write-up are available on request.
Source: open-pencil/open-pencil