Skip to content

TypeScript returns stored merge rewrites with their keys in alphabetical order, so entity merges and graph.diff output differ from Python #525

Backlog

Description

@saurav-shakya

Summary

Merge records read back from the store list each rewrite's keys in a different order in TypeScript than in Python, so the CLI entity merges, the MCP entity_merges tool, and the entities_merged / entities_unmerged lists of graph.diff print different JSON for the same graph. Results and keys are a wire format both SDKs share.

Reproduce

Embedded Kuzu, current main, one database for both CLIs:

export SELFGRAPH_STORE=kuzu SELFGRAPH_URI=$(mktemp -d)/db
node typescript/dist/cli.js entity create --name Ann --type Person
node typescript/dist/cli.js insert text "Bob knows Ann." --resolver default
selfgraph entity merges person:ann > py.json
node typescript/dist/cli.js entity merges person:ann > ts.json
diff py.json ts.json

Each entry of rewrites:

  • Python: kind, id, original, rewritten, existed, dropped, evidence (the MergeRewrite field order).
  • TypeScript: dropped, evidence, existed, id, kind, original, rewritten.

It does not matter which SDK wrote the merge; a fresh insert result is in model order in both, so only reads diverge.

The evidence records inside a rewrite (kept for a relation a merge dropped as a self-loop) come back the same way: TypeScript prints chunk_id, confidence, end_offset, quote, source_id, source_version, start_offset, Python the Evidence field order.

Expected

The same key order in both SDKs, the model's field order.

Root cause

typescript/src/resolution.ts, decodeMerge, rebuilds rewrites with { ...rw, dropped: ..., evidence: rw.evidence ?? [] }. That spreads the stored JSON, whose keys are sorted, instead of building a MergeRewrite in its field order.

Proposed fix

Build each rewrite, and each evidence record in it, explicitly in field order in decodeMerge, and add the key order of a merges read to the conformance pair.

Not a duplicate of

I would like to work on this.

Activity

  1. added
    bug
    approved-for-pr
    area: parity
    area: cli-mcp
    P3
    effort: S
    on Nov 26, 2026
  2. arrry-hydraDB commented on Nov 26, 2026

    @arrry-hydraDB
    Collaborator

    /approved

    Confirmed from the code path: both SDKs store each rewrite with sorted keys, but on read Python coerces the stored dicts back into MergeRewrite (model field order) while TypeScript decodeMerge spreads the stored object (...rw), keeping the alphabetical key order. So entity merges, the MCP entity_merges tool, and the entities_merged / entities_unmerged lists of graph.diff print different JSON for the same merge, and the nested evidence records diverge the same way.

    Scope: TypeScript only, since Python already emits the model field order. Build each rewrite, and each evidence record inside it, explicitly in field order in decodeMerge, and add a merges read to the conformance pair so the key order is checked from now on.

    Priority P3, effort S: no wrong values or data loss, but merge reads are a wire format both SDKs share.

  3. github-actions commented on Nov 26, 2026

    @github-actions

    Approved by @arrry-hydraDB. @saurav-shakya or an assignee of this issue can now open a PR that links it with Closes #525 (max 10 open PRs at a time). Anyone else can comment /take on this issue to be assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3approved-for-prarea: cli-mcparea: paritybugeffort: S

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions