Repository navigation
Description
This came out of a claude-code audit session chasing a real-workflow report (fluree CLI 4.1.6, local .fluree project, macOS): a branch replace-update on an @json property merged back to main, and after the merge main held both the old and the new value on the same subject+predicate — with a sh:maxCount 1 shape on that property, so the ledger was silently in a state its own shapes reject (the SHACL half is #1782). Everything below was reproduced hermetically against 4.1.6 and traced in source at main (1aae89018).
The root cause: rdf:JSON literals are verbatim strings, so "the same JSON" is two distinct facts
At ingest, @json values are stored as whatever bytes the writer happened to serialize — fluree-db-transact/src/parse/jsonld.rs:1732-1745 takes a string @value verbatim and otherwise runs plain serde_json::to_string(val), with no canonicalization. So [{"name":"alpha","qty":1}] and [{"qty":1,"name":"alpha"}] are two different FlakeValue::Json strings, two different RDF terms, two different facts. A quick probe confirms storage is verbatim end-to-end (key order survives both novelty and indexed reads):
$ fluree insert -l demo -e '... "ex:items": {"@value": [{"zeta": 1, "alpha": {"y": 2, "x": 3}}], "@type": "@json"} ...'
$ fluree query ... # novelty read
['[{"zeta":1,"alpha":{"y":2,"x":3}}]'
$ fluree index demo && fluree query ... # indexed read — identical, still verbatim
JSON-LD 1.1 is explicit that this isn't how @json literals are supposed to work: in the Object-to-RDF transformation the lexical form of a JSON literal MUST be the canonical form per the JSON Canonicalization Scheme (RFC 8785). We skip that, and the identity of an rdf:JSON literal becomes an accident of the writer's serializer. It's the same disease class as #1319 (same number, two terms via xsd:long vs xsd:integer) — a value that users reasonably treat as one thing is several distinct terms internally, and every equality-driven mechanism downstream (retraction matching, upsert replace, merge conflict handling, sh:maxCount counting) quietly operates on the wrong unit.
How that becomes the merge duplicate
Minimal repro (throwaway dir, fluree init; full script at the bottom):
maint1:ex:config/ex:items= A, written as[{"name": "alpha", "qty": 1}].fluree branch create feat.- On
feat: JSON-LD update{where/delete ?old, insert B}— the standard replace shape.?oldbinds A as stored, so the branch commit is[retract A-bytes, assert B]. Branch queries correctly show only B. - On
main: some writer touches the node and re-serializes the same logical value with different key order —[{"qty": 1, "name": "alpha"}](call it A′). Because of the verbatim identity above, that'sretract A, assert A′— a different fact now holds the value. fluree branch merge feat→
Merged 'feat' into 'main' (t=3, 1 commits copied, 1 conflicts).
$ fluree query -l demo -e 'SELECT ?o WHERE { <.../config> <.../items> ?o }'
"value": "[{\"name\":\"beta\",\"qty\":2}]",
"value": "[{\"qty\":1,\"name\":\"alpha\"}]",
Both values, at HEAD, from a plain query — exactly the field report. The mechanics: prepare_merge detects the (s, p, g) conflict (fluree-db-api/src/merge.rs:494-498, both sides touched the key), and the default take-both strategy (ConflictStrategy in fluree-db-api/src/rebase.rs:29-42; apply_two_way_strategy at rebase.rs:752-785 passes the source flakes through untouched) stages the branch's retract A-bytes + assert B onto main. The retract matches nothing — main's current value is A′, a different term — so it's a no-op, B lands, and A′ survives. To any human (or any JSON-aware tool) the result reads as "the replace lost and we duplicated the value," even though every individual mechanism did what its own equality told it to.
Worth saying plainly what this is not, because the control matrix was extensive: with a plain string literal the identical flow is correct (strings have no serialization variance to exploit); with byte-stable @json the merge is correct in every combination we tried — no-rebase, rebase, target-indexed-before-merge, target re-asserting the same bytes at higher t, target A→C→A cycles, post-merge incremental reindex, post-merge full rebuild. The retract cancels fine whenever the bytes agree. The failure needs exactly (a) @json's verbatim identity and (b) any second writer serializing the same value differently — which in practice is any app that round-trips JSON through its own serializer (sorted-key vs insertion-order alone does it).
Fix directions
I think the real fix is at ingest: canonicalize @json lexical forms per RFC 8785 at jsonld.rs:1732-1745 (both the pre-serialized-string arm and the serde_json::to_string arm), which is also just what JSON-LD 1.1 says to do. Two things we'd want to be deliberate about:
- The migration hazard is this very bug. Existing ledgers hold non-canonical stored forms; if we canonicalize new writes only, a new
delete ?oldbinds the stored (non-canonical) form and retracts fine — but a literal-value delete (user supplies the JSON) would canonicalize and then miss the old stored bytes. We'd probably want equality/lookup for rdf:JSON to compare canonicalized (or reindex-time re-canonicalization with an index-format bump), not just a write-side change. - RFC 8785 number serialization (ES6
Number::toString) is the fiddly part of a conformant JCS; worth pulling a vetted implementation rather than hand-rolling.
Separately — and it stands even once identity is fixed — a merge that resolves a conflict by silently materializing a sh:maxCount-violating state gets no pushback from validation at all today; that's #1782 (it reproduces with byte-identical values and an in-ledger shape). The same audit also turned up a third, independent defect in the branch machinery — transplanted flakes keep branch-local t, corrupting time-travel reads — filed as #1783.
Full repro script (fluree 4.1.6, local mode)
#!/bin/bash
set -u
mkdir -p /tmp/json-merge-repro && cd /tmp/json-merge-repro
fluree init >/dev/null 2>&1; fluree create demo >/dev/null 2>&1
CTX='{"ex": "http://example.org/"}'
# t1: A with key order (name, qty)
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:config", "ex:items": {"@value": [{"name": "alpha", "qty": 1}], "@type": "@json"}}'
fluree branch create feat -l demo
# feat t2: replace A -> B (binds ?old = A as stored)
fluree update -l demo:feat -e '{"@context": {"ex": "http://example.org/"}, "where": [{"@id": "ex:config", "ex:items": "?old"}], "delete": [{"@id": "ex:config", "ex:items": "?old"}], "insert": [{"@id": "ex:config", "ex:items": {"@value": [{"name": "beta", "qty": 2}], "@type": "@json"}}]}'
# main t2: rewrite the SAME logical value, different key order (qty, name)
fluree update -l demo -e '{"@context": {"ex": "http://example.org/"}, "where": [{"@id": "ex:config", "ex:items": "?old"}], "delete": [{"@id": "ex:config", "ex:items": "?old"}], "insert": [{"@id": "ex:config", "ex:items": {"@value": [{"qty": 1, "name": "alpha"}], "@type": "@json"}}]}'
fluree branch merge feat -l demo
# => Merged 'feat' into 'main' (t=3, 1 commits copied, 1 conflicts).
fluree query -l demo -e 'SELECT ?o WHERE { <http://example.org/config> <http://example.org/items> ?o }' --format json
# => TWO rows: [{"name":"beta","qty":2}] AND [{"qty":1,"name":"alpha"}]Versions: fluree CLI 4.1.6 (local mode, macOS); source refs against main @ 1aae89018.
Activity