fix(crdt): route CollectionSetDelta out of GetDelta instead of dropping it#4726
fix(crdt): route CollectionSetDelta out of GetDelta instead of dropping it#4726SAY-5 wants to merge 1 commit intosourcenetwork:developfrom
Conversation
…ng it internal/core/crdt/ipld_union.go declares a keyed IPLD union with seven arms, one of which is CollectionSetDelta. CRDT.GetDelta() has two identical case arms for c.CollectionDelta != nil, so the second one is unreachable and a CRDT block carrying a CollectionSetDelta falls off the end of the switch and returns nil. GetPriority and the IPLD schema both handle CollectionSetDelta correctly, so the drop is specific to GetDelta. Change the second case to check c.CollectionSetDelta != nil, matching the sibling GetPriority dispatch. Any downstream merge that relied on GetDelta() returning the set-delta (rather than nil) now works as the schema intended. Fixes sourcenetwork#4720 Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR corrects a copy-paste bug in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Assessment against linked issues
Suggested reviewers
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/core/crdt/ipld_union.go (1)
62-80: Consider adding a regression unit test.Per the PR objectives, a unit test that constructs
CRDT{CollectionSetDelta: &CollectionSetDelta{...}}and assertsGetDelta()returns the non-nil*CollectionSetDeltawould guard against this class of copy-paste regression. A table-driven test covering all seven variants would also catch any similar fallthrough in the future.Additionally, enabling a linter rule such as
govet's duplicate-case detection orgolangci-lint'sdupl/gocriticdupBranchBodycould catch identical switch conditions automatically.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/core/crdt/ipld_union.go` around lines 62 - 80, Add a regression unit test for CRDT.GetDelta: create a table-driven test that iterates over each variant (LWWDelta, DocCompositeDelta, CounterDelta, CollectionDelta, CollectionSetDelta, CollectionDefinitionDelta, FieldDefinitionDelta), construct a CRDT with only that field set (e.g. CRDT{CollectionSetDelta: &CollectionSetDelta{...}}), call GetDelta() and assert the returned Delta equals the non-nil concrete pointer for that variant (use pointer equality or type assertion as appropriate); include at least one explicit test case for CollectionSetDelta to prevent copy-paste regressions and enable a linter rule such as govet/duplicate-case or golangci-lint gocritic/dupBranchBody to catch identical switch branches automatically.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@internal/core/crdt/ipld_union.go`:
- Around line 62-80: Add a regression unit test for CRDT.GetDelta: create a
table-driven test that iterates over each variant (LWWDelta, DocCompositeDelta,
CounterDelta, CollectionDelta, CollectionSetDelta, CollectionDefinitionDelta,
FieldDefinitionDelta), construct a CRDT with only that field set (e.g.
CRDT{CollectionSetDelta: &CollectionSetDelta{...}}), call GetDelta() and assert
the returned Delta equals the non-nil concrete pointer for that variant (use
pointer equality or type assertion as appropriate); include at least one
explicit test case for CollectionSetDelta to prevent copy-paste regressions and
enable a linter rule such as govet/duplicate-case or golangci-lint
gocritic/dupBranchBody to catch identical switch branches automatically.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e6f7dc43-99ba-4154-bff9-3cafcb795c6b
📒 Files selected for processing (1)
internal/core/crdt/ipld_union.go
📜 Review details
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-03-27T08:24:16.083Z
Learnt from: islamaliev
Repo: sourcenetwork/defradb PR: 4661
File: http/middleware.go:93-93
Timestamp: 2026-03-27T08:24:16.083Z
Learning: When handling `client.ErrNotAuthorizedToPerformOperation` in the defradb repository, treat it as an unauthenticated/missing-identity condition (not insufficient permissions). Map it to HTTP 401 Unauthorized rather than 403 Forbidden; this mapping is intentional and consistent with prior behavior (pre-PR `#4661`).
Applied to files:
internal/core/crdt/ipld_union.go
🔇 Additional comments (1)
internal/core/crdt/ipld_union.go (1)
72-73: Correct fix for the copy-paste bug.The updated case correctly dispatches
CollectionSetDeltaand matches the handling inNewCRDT(Line 33),GetPriority(Line 93), and the IPLD schema (Line 55). Previously this branch was unreachable due to the duplicatec.CollectionDelta != nilcondition, causingGetDelta()to returnnilfor CRDT blocks carrying only aCollectionSetDeltaand silently skipping merges atinternal/core/block/store.go(around Line 201).
Problem
internal/core/crdt/ipld_union.godeclares a keyed IPLD union whosearms are
LWWDelta,DocCompositeDelta,CounterDelta,CollectionDelta,CollectionSetDelta,CollectionDefinitionDelta,FieldDefinitionDelta.CRDT.GetDelta()dispatches on whichever field is non-nil. Lines 70 and 72both check the same field:
The second arm is unreachable, so a
CRDTcarrying onlyCollectionSetDeltafalls through every case and returns
nil. Any downstream code that callsGetDelta()on such a block observes a nilDeltaand silently skips ormis-handles the merge.
The sibling
GetPriority()at lines 93-94 already dispatches onc.CollectionSetDelta != nil, so the IPLD schema and the priority pathare consistent — only
GetDeltais broken.Fix
Check
CollectionSetDeltain the second arm, matching the schema andGetPriority.Fixes #4720