← Back to Pull requests

Count every duplicate group in the dedupe summary #5941

Open
unknown opened this pull request with 631 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 29, 2026 13:14 UTC
Author

Closes #5548

Stacks on #5618, which rewrites the same group body. Its branch lives on my fork, so this PR targets main and, until #5618 merges, its diff also shows #5618's four commits; only 2847d6b91 belongs to this PR.
Fork pre-flight: jaysbeekay/sure#1036 (pull/1036) (same branch, same commit 2847d6b91).

Problem

rake securities:deduplicate ends with "Total duplicate sets: N", where N comes from the outer duplicates local. The ticker + MIC loop reassigns that local to each group's own duplicates (duplicates = securities[1..]), since a block shares its enclosing scope's locals, so the summary prints the duplicate count of whichever group ran last. "Found N sets" is printed before the loop and is right.

Resulting behaviour

  • The group body uses its own group_duplicates local at the assignment and every reader: the Duplicates line, the holdings, trades, prices, lots and reference counts, and Security::Merger.merge_group!. The outer list keeps its uses in "Found N sets", the each and the summary.

  • The summary shows the number of groups. Nothing else in the output or the merge changes.
  • Not fixed here

    Pre-existing, left for follow-ups:

  • dry_run = args[:dry_run].present? treats "false" as a dry run, so securities:deduplicate[false] changes nothing.

  • The summary leaves out the first pass, duplicates without a MIC: its totals count only the ticker + MIC groups.
  • Validation

  • Red first on a6f8cbcfc (#5618's head): three groups of 2, 2 and 5 securities in a dry run (invoke("true")) printed Total duplicate sets: 1, the last group processed (pluck with group has no order), and the test expects /^ Total duplicate sets: 3$/.

  • A live run (bare invoke) merges each group into its earliest security: its holding, trade and price re-point and only the others are deleted, and the summary reads 3 sets, 3 holdings, 3 trades, 3 prices.

  • Boundary check, not coverage of the fix: with no groups the summary prints Total duplicate sets: 0, which also passes before the fix, since the loop never runs.

  • Mutations, each caught: the outer local reassigned again; the holdings and trades counts reading the outer list; merge_group! given the outer list; the Duplicates line reading it.

  • Rubocop and Brakeman clean. Full suite on ab631eaad (upstream main merged in): 19,799 runs, 0 failures, 8 errors, all ReleaseNotesWorkflowTest needing node, which the test image lacks.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Rewrites both security-dedupe tasks to merge each duplicate group through Security::Merger, and fixes the dedupe summary so "Total duplicate sets" counts every ticker + MIC group instead of echoing the last group's duplicate count.

  • Each group merges in one transaction: holdings, lots, splits, tags, trades, and prices move with the duplicate, which is then deleted; previously a duplicate's lots were cascade-deleted.

  • A colliding holding keeps the more recently written row, and moved holdings keep their original write time.

  • Accounts touched by a merge are synced once, after the last group, and also when the task stops partway; failed sync dispatches are logged and captured in the debug log.

  • The task output prints the merged counts (holdings moved/removed, trades, lots, references) and the collision rule.

  • Security#move_taggings_to! is removed; its behavior is now covered in the merger tests.
  • Closes #5548.

    <sup>Written for commit ab631eaad4542531746f82c051edb18fe5d712b1. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/5941?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Improved duplicate security cleanup by consolidating related holdings, lots, trades, prices, references, and tags into the retained security.
    When duplicate securities have conflicting holdings, the most recently updated holding is retained.
    Affected accounts are refreshed after merges, with repeated refreshes avoided when multiple duplicate groups affect the same account. Refreshes continue independently if one fails, including when cleanup is interrupted.
    Improvements
    Cleanup summaries now include counts for lots and references alongside other merged records.
    * Duplicate groups are merged atomically, preventing partial changes within a group.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 631 lines (+473 / -158) across 7 files Reviewer: jaysbeekay Review duration: 8729m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 631 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #5941"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.9% · Safe to merge
    26.9%