Skip to content

History listings disagree with the plain rebuild after a set and an add, and never carry replicated chains #4332

Open

Description

@kriszyp

History listings (getHistory, getHistoryOfRecord, read_audit_log) carry an image from patch to patch through HistoryReader (#3504). They are meant to return what the plain rebuild (entry.getValue(store, true, time)) returns, but two of their carry rules disagree with it.

A numeric string set before an add. historyCanCarry accepts any patch whose operations are known, but a later add changes how the plain rebuild types an earlier value. Its reverse walk subtracts the add and returns a number. Reproduced on main:

await T.put({ id: 'a', n: '5', name: 'base' });
await T.patch('a', { name: 'first' });
await T.patch('a', { n: '10' });
await T.patch('a', { n: { __op__: 'add', value: 1 } });
await T.patch('a', { name: 'last' });
// plain rebuild of the third entry: { n: 10, name: 'first' }
// getHistory() and getHistoryOfRecord('a'): { n: '10', name: 'first' }

The listing returns the string that was written; the plain rebuild returns the number its reversal produces. Either may be the right answer, but the two paths should agree.

Replicated chains never carry. HistoryReader.read follows a patch from the previous image only when (entry.previousNodeId ?? 0) === image.origin. previousNodeId is never encoded (see the comment in resources/Table.ts above const headNodeId), so the check passes only for origin 0. A record written by another node rebuilds every patch from its current version, which makes listings quadratic again for replicated data.

The subscription replay reader added for #3032 (ReplayReader) handles both cases differently. It carries only chains whose patches set fields (historyCanCarry's plainOnly), and it follows when entry.nodeId matches the image's origin. Moving the listing reader onto the same rules, or sharing one carry implementation between the two, removes both gaps along with the duplicated logic.

Review-Deferral: d538299566747aea severity=minor category=maintainability

🤖 Generated by Claude (Anthropic); posted via @kriszyp.

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

    No labels
    No labels

    Type

    Fields

    Priority

    P3

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions