Skip to content

fix: ignore cosmetic key-order drift in model comparisons#172

Merged
angeloashmore merged 5 commits intomainfrom
aa/fix-cosmetic-model-drift
May 2, 2026
Merged

fix: ignore cosmetic key-order drift in model comparisons#172
angeloashmore merged 5 commits intomainfrom
aa/fix-cosmetic-model-drift

Conversation

@angeloashmore
Copy link
Copy Markdown
Member

@angeloashmore angeloashmore commented May 2, 2026

Resolves: #170

Description

prismic push and prismic pull write model JSON in different key orders, so after a successful push, prismic status reports types and slices as "Differ" indefinitely — and the git-dirty safety check then blocks the next push until the user commits a cosmetic-only pull.

Root cause: diffArrays compares with JSON.stringify, which is key-order sensitive. The API returns top-level model keys, top-level slice keys, and slice variation keys in a different order than we author them, but those keys have no semantic meaning.

This change normalizes models before diffing — sort keys by default, but preserve insertion order inside containers where order is meaningful in the editor (json for tab order and field order within tabs, and primary/items/fields for field order within variations and groups). Disk content is untouched.

Checklist

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

How to QA

  1. In a fresh project, create a type and add a few fields:
    • prismic type create "Announcement"
    • prismic field add uid --to-type announcement
    • prismic field add text message --to-type announcement --label "Message"
  2. git add . && git commit -m "add announcement type"
  3. prismic push → reports the type as pushed.
  4. prismic status → should report "Already up to date." (previously reported "Differ").
  5. prismic pull → should report "Already up to date." (previously rewrote the local file with reordered keys).
  6. Reorder a field with prismic field reorder and confirm prismic status still detects the change.

View in Codesmith
Need help on this PR? Tag @codesmith with what you need.

  • Let Codesmith autofix CI failures and bot reviews

Note

Medium Risk
Changes the equality logic used by prismic push/pull/status to decide whether models differ; an overly-aggressive canonicalization could cause real schema changes to be missed or reduce visibility of differences.

Overview
Model diffs are now key-order insensitive. diffArrays gains an optional equals comparator, and push, pull, and status pass a comparator that compares canonicalizeModel() output rather than raw JSON.stringify.

Adds canonicalizeModel() in models.ts to normalize custom type and slice models (including per-variation objects) by sorting object keys before comparison, reducing cosmetic-only churn and repeated “Differ” status after successful syncs.

Reviewed by Cursor Bugbot for commit 99a29dc. Bugbot is set up for automated code reviews on this repo. Configure here.

The Prismic API returns model JSON with metadata keys in a different
order than we write them locally. This caused push/pull/status to flag
purely cosmetic differences as updates, blocking subsequent pushes via
the dirty-tree check until the user committed a no-op pull.

Canonicalize models before diffing: sort keys by default, but preserve
insertion order inside containers where it carries meaning (tab order
under \`json\`, field order inside tabs and inside variation
\`primary\`/\`items\`/\`fields\`).

Fixes #170

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread src/commands/push.ts Outdated
angeloashmore and others added 2 commits May 2, 2026 00:57
Replace the orderedDepth integer with three named conditions:
isFieldMap, isTabMap, isTabObject. Mirrors the model shape directly
(\`json\` is a tab map; tab values are field maps; \`primary\`/\`items\`/
\`fields\` are field maps).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replace the recursive walker with two flat sorts: model's top-level
keys, and each variation's top-level keys. The API only reorders these
metadata keys; everything inside (\`json\`, \`primary\`, etc.) is left
untouched, preserving the editor-significant order naturally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6e0c2f1. Configure here.

Comment thread src/commands/pull.ts Outdated
angeloashmore and others added 2 commits May 2, 2026 01:14
\`diffArrays\` returns items from its source array, so canonicalizing
the inputs caused the key-sorted copies to flow through to the
\`insertCustomType\`/\`updateSlice\`/etc. write paths in push and pull.

Add an optional \`equals\` option to \`diffArrays\` and pass the
canonicalize-then-stringify comparator from each call site. Originals
are passed in unchanged, so writes get the user-authored objects.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@angeloashmore angeloashmore merged commit 5a96016 into main May 2, 2026
13 checks passed
@angeloashmore angeloashmore deleted the aa/fix-cosmetic-model-drift branch May 2, 2026 01:40
@github-actions github-actions Bot mentioned this pull request May 2, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prismic push and prismic pull write JSON in different key orders, causing perpetual cosmetic drift

1 participant