fix: pull writes canonical model files and detects changes reliably - #243
fix: pull writes canonical model files and detects changes reliably#243angeloashmore wants to merge 16 commits into
Conversation
Model canonicalization sorted keys only one level deep, so structurally identical models with reordered keys inside nested config objects (e.g. an Image field's constraint) compared as different, causing pull to rewrite unchanged files. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sync compared raw remote JSON against canonical local files, so any remote change re-synced every model whose key order differed. Sync now uses pull's comparison: the remote model's canonical form against the local file as parsed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Pull now has a test that pulled files keep field order, a second pull changes nothing, and non-canonical files (types and slices) are rewritten in canonical form. The status reorder test also covers a slice and checks that push writes nothing. The scramble helper moves to test/it.ts and handles slice field maps. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…lize # Conflicts: # src/commands/sync.ts
Deep key sorting also sorted a Slices field's choices, which is the slice order shown in the editor, and the field maps inside legacy slices. Rebuild both from the unsorted input, like group fields. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Fixtures already hold their keys in a non-canonical order, so writing them to disk gives the same coverage as the scramble helper without restating which maps are position-significant. Cover a slice zone and a legacy slice too. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The pulled file only proved that field order survives. Assert the sorted key order of the model, a field, a thumbnail, a slice, and a variation, and add a second tab and thumbnail so tab and array order are covered. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Write the unsorted fixture locally before the pull so one run covers both the rewrite and the canonical output. Compare the pulled models to the fixture for content, then assert key order at each kind of object, including the slice zone and a legacy slice. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Write the unsorted fixture locally instead of pulling first, so status and push run against a non-canonical file in two CLI runs. Move the Choices type next to the function that uses it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The watch loop hashed the raw remote JSON. A change in key order flipped the hash without a real model change. The loop then ran generateTypes() and logged "Changes detected in" with no suffix. Hash the canonicalized models instead. Run generateTypes() and log changes only on the initial sync or when there are operations. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d72140d. Configure here.
lihbr
left a comment
There was a problem hiding this comment.
LGTM but I think there's an issue to check with select fields, see bellow :)
| } | ||
|
|
||
| function sortKeys<T>(object: T): T { | ||
| if (Array.isArray(object)) return object.map(sortKeys) as T; |
There was a problem hiding this comment.

Resolves:
Description
Model comparison now sorts JSON keys at every depth before it compares. Field and tab order stays the same. This corrects three faults:
1.
pullrewrote files that had no changesprismic pull. The API returns nested keys (inside a field'sconfig, for example) in a different order.git diffshowed only key-order movement.2.
pulldid not rewrite files with a non-canonical key order3.
sync --watchreported changes that did not existpushandstatuscontinue to ignore key order. They push nothing and report nothing when only the key order is different.Checklist
Preview
How to QA 1
prismic pull. The command rewrites the files.prismic pullagain. The command prints "Already up to date." and leaves no dirty files.prismic sync --watch. The command does not report changes when the remote models do not change.🤖 Generated with Claude Code
Note
Medium Risk
Changes core model equality used by pull, sync, push, and status; incorrect canonicalization could hide real diffs or overwrite valid local models.
Overview
Pull and sync now treat Prismic models as equal when only JSON key order differs, while still rewriting local files to a stable canonical shape on pull.
Canonicalization in
models.tsis deepened:sortKeysrecurses through nested objects and arrays; Group nested fields and Slices zone choices (including legacySliceentries) get explicit handling so field/tab order is preserved but metadata keys are sorted consistently.pullfixesdiffArraysequality to compareJSON.stringify(canonicalize(remote))against the local model (already canonical on disk), avoiding false updates and missed rewrites.synchashes canonicalized remote state, uses the same canonical equality for slice/custom-type diffs, runsgenerateTypesonly on the initial sync or when something actually changed, and logs change messages only when local files were updated—not on every poll when only the hash drifted without file ops.Reviewed by Cursor Bugbot for commit d72140d. Bugbot is set up for automated code reviews on this repo. Configure here.
Footnotes
Please use these labels when submitting a review:
⚠️ #issue: Strongly suggest a change.
❓ #ask: Ask a question.
💡 #idea: Suggest an idea.
🎉 #nice: Share a compliment. ↩