Skip to content

Tolerate unknown IChange $type via PeekThenConcreteChangeConverter#80

Open
hahn-kev-bot wants to merge 5 commits into
mainfrom
feat/unknown-change-type-converter
Open

Tolerate unknown IChange $type via PeekThenConcreteChangeConverter#80
hahn-kev-bot wants to merge 5 commits into
mainfrom
feat/unknown-change-type-converter

Conversation

@hahn-kev-bot

@hahn-kev-bot hahn-kev-bot commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Replace STJ JsonPolymorphic on IChange with PeekThenConcreteChangeConverter: known $type values deserialize via cached concrete JsonTypeInfo; unknown values become OpaqueChange (raw JSON preserved for round-trip).
  • Emit serialize-only synthetic $type on registered concrete change types from CrdtConfig, and skip snapshot apply for changes that do not support NewEntity (opaque/unknown creates).
  • Add unit coverage for happy-path, opaque, and $type-first requirements.

Test plan

  • dotnet test src/SIL.Harmony.Tests --filter ChangeConverterTests
  • dotnet test src/SIL.Harmony.Tests (full suite)
  • Confirm sync/round-trip of a commit containing a known change plus an unregistered $type keeps the unknown payload and does not throw
  • Confirm older clients skip applying opaque creates without dropping the change from history

Summary by CodeRabbit

  • New Features
    • Added support for preserving and round-tripping unrecognized change types without losing their original data.
    • Known and unknown changes can now be safely deserialized from mixed commits.
  • Bug Fixes
    • Unsupported changes no longer interrupt snapshot processing; they remain in history while snapshot creation is skipped.
    • Enforced consistent JSON formatting with the change type discriminator appearing first.

hahn-kev and others added 3 commits July 10, 2026 14:19
Co-authored-by: Cursor <cursoragent@cursor.com>
Unknown $type values will be held as OpaqueChange once the converter owns discrimination.

Co-authored-by: Cursor <cursoragent@cursor.com>
CrdtConfig owns IChange discrimination with synthetic $type on write; SnapshotWorker skips opaque creates.

Co-authored-by: Cursor <cursoragent@cursor.com>
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR replaces interface-level polymorphic JSON handling with a custom converter, adds OpaqueChange for unknown discriminators, preserves unknown payloads during serialization, skips unsupported opaque entity creation, and adds coverage for known, unknown, mixed, and invalid JSON shapes.

Changes

Change serialization and replay

Layer / File(s) Summary
Discriminator mapping and serializer metadata
src/SIL.Harmony/Changes/Change.cs, src/SIL.Harmony/CrdtConfig.cs, src/SIL.Harmony/Changes/PeekThenConcreteChangeConverter.cs
CrdtConfig builds discriminator maps and injects synthetic $type properties, while IChange delegates polymorphic handling to the custom converter.
Known and opaque change conversion
src/SIL.Harmony/Changes/PeekThenConcreteChangeConverter.cs, src/SIL.Harmony/Changes/OpaqueChange.cs
The converter validates $type ordering, deserializes registered types, stores unknown payloads in OpaqueChange, and reserializes preserved JSON.
Replay behavior and converter tests
src/SIL.Harmony/SnapshotWorker.cs, src/SIL.Harmony.Tests/ChangeConverterTests.cs
Snapshot application skips unsupported new-entity changes, and tests cover round-tripping, unknown types, mixed commits, preservation, and discriminator ordering.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CommitJson
  participant CrdtConfig
  participant PeekThenConcreteChangeConverter
  participant SnapshotWorker
  CommitJson->>CrdtConfig: Create serializer options and discriminator maps
  CommitJson->>PeekThenConcreteChangeConverter: Deserialize change JSON
  PeekThenConcreteChangeConverter->>PeekThenConcreteChangeConverter: Match first $type discriminator
  PeekThenConcreteChangeConverter->>CommitJson: Return known concrete change
  PeekThenConcreteChangeConverter->>CommitJson: Return OpaqueChange with raw JSON
  SnapshotWorker->>OpaqueChange: Check SupportsNewEntity()
  SnapshotWorker->>SnapshotWorker: Skip unsupported opaque creation
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main change: unknown IChange $type values are handled by PeekThenConcreteChangeConverter.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/unknown-change-type-converter

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/SIL.Harmony/CrdtConfig.cs`:
- Around line 49-65: Update BuildChangeDiscriminatorMaps to construct the
reverse discriminator map in a local dictionary alongside knownChanges, populate
both fully within the loop, then assign the completed map to
_changeTypeDiscriminators immediately before returning. Do not publish the field
until all entries have been validated and added.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 00791495-bffd-4c2a-baae-784dde3c8328

📥 Commits

Reviewing files that changed from the base of the PR and between 01efe25 and 2d008f7.

📒 Files selected for processing (8)
  • .gitignore
  • AGENTS.md
  • src/SIL.Harmony.Tests/ChangeConverterTests.cs
  • src/SIL.Harmony/Changes/Change.cs
  • src/SIL.Harmony/Changes/OpaqueChange.cs
  • src/SIL.Harmony/Changes/PeekThenConcreteChangeConverter.cs
  • src/SIL.Harmony/CrdtConfig.cs
  • src/SIL.Harmony/SnapshotWorker.cs

Comment thread src/SIL.Harmony/CrdtConfig.cs
hahn-kev and others added 2 commits July 16, 2026 10:22
_changeTypeDiscriminators was assigned before its loop finished populating
it, so a concurrent JsonTypeModifier callback (invoked lazily by STJ on
arbitrary threads) could observe a partially-built dictionary. Build into a
local and assign the field once, so readers only ever see null or a complete
map.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@myieye myieye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just some improvement ideas/suggestions, but the feature looks solid

if (prevSnapshot is null)
{
// create brand new entity - this will (and should) throw if the change doesn't support NewEntity
if (!commitChange.Change.SupportsNewEntity())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd opt to keep throwing if it's not an opaque change.
It's an unexpected scenario that has exposed my user errors before.

return new OpaqueChange
{
TypeName = typeName,
EntityId = element.TryGetProperty("EntityId", out var id) && id.ValueKind == JsonValueKind.String

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
EntityId = element.TryGetProperty("EntityId", out var id) && id.ValueKind == JsonValueKind.String
EntityId = element.TryGetProperty(nameof(IChange.EntityId), out var id) && id.ValueKind == JsonValueKind.String

};
}

private bool TryFindKnown(ref Utf8JsonReader reader, out int index, out string? unknownTypeName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm guessing there's no way to somehow return/out a ref to the known-type instead of passing out an index and doing the lookup a second time?

Comment on lines +67 to +68
_changeTypeDiscriminators = discriminators;
return knownChanges;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's no very pretty that that're one side-effect and one return value.
You can leave it if you want or you could make more Lazy<> fields for one or both dictionaries.


// IChange polymorphism is owned by PeekThenConcreteChangeConverter — do not set PolymorphismOptions.
if (_changeTypeDiscriminators is not null
&& typeInfo.Kind == JsonTypeInfoKind.Object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't the original condition still ok and more meaningful?

Suggested change
&& typeInfo.Kind == JsonTypeInfoKind.Object
&& typeInfo.Type == typeof(IChange)

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.

3 participants