Skip to content

A re-insert of a compact list with a gap in its positions duplicates a member #2180

Closed

Description

@aaj3f

Claude caught this while re-checking #2159's follow-up commits after merge. Introduced in de65d869f (merged with #2159), and unchanged on main at 9420db4ca.

What happens

encode in fluree-db-transact/src/lists.rs restates a list when its values match the compact list its owner already holds (lists.rs:287):

Some(held) if same_members(held, &list.members) => out.extend(list.into_compact()),

same_members compares values, datatypes and languages, but not positions, and into_compact() writes fresh positions 0..n-1. So a held compact list whose stored positions have a gap still matches a re-insert of its values, and the restate asserts members at positions the held list doesn't use.

A gap is easy to get: a JSON-LD delete of one member leaves the others where they were ([1, 2, 3] minus 2 is stored as 1@0, 3@2).

Repro

  1. Insert {"@id": "ex:s", "ex:items": {"@list": [1, 2, 3]}}.
  2. Delete member 2 (a where with ["filter", "(= ?o 2)"], then delete).
  3. Insert {"@id": "ex:s", "ex:items": {"@list": [1, 3]}}.

The list then reads [1, 3, 3] through rdf_list::read_lists, through a JSON-LD query, and through SPARQL's rdf:rest*/rdf:first alike. One update that deletes 2 and inserts {"@list": [1, 3]} lands in the same place, since compact_lists_held leaves out the members the same transaction retracts. With no gap, the re-insert restates [1, 3] as intended.

Before de65d869f this arm stored the second list as a chain, so the duplicate comes in with that commit (the 4.2.4 CLI also gives [1, 3, 3], through the old position merge).

A fix that works

Restating at the held positions:

Some(held) if same_members(held, &list.members) => out.extend(
    held.iter().zip(list.members).map(|(h, mut m)| {
        m.m = h.m.clone();
        m
    }),
),

With it, both sequences read [1, 3], and the lists unit tests, it_list_identity and it_sparql_lists stay green. a_list_restating_the_compact_one_restates_its_members restates a list stored at 0 and 1, so a case with a gap would pin this.

This is the position merge #2159 set out to fix, coming back through the restate path because compact storage names no list or cell. #2171 is the structural fix for that on next, but main ships this (and so would v4.2.5, #2145), so I think the point fix belongs on main now.

Activity

  1. added
    bug
    area:transact
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:transactbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions