Skip to content

Conversation

@MorganaFuture
Copy link

Several service methods (BalanceFile, BuildFile, GetFileContents, SegmentFileID, FlattenBatches) were mutating files stored in the in-memory repository because FindFile() returns a direct pointer to the stored object.

This caused data corruption where:

  • BalanceFile modified the original file's batches and ID, then stored the same pointer under a new key, leaving two keys pointing to one mutated object
  • Other methods called Create() on repository files, modifying their control totals

The fix adds a cloneFile() helper that creates a deep copy via JSON serialization before any mutations. This ensures the original files in the repository remain unchanged.

Added regression tests to verify original files are preserved after each operation

Several service methods (BalanceFile, BuildFile, GetFileContents,
SegmentFileID, FlattenBatches) were mutating files stored in the
in-memory repository because FindFile() returns a direct pointer
to the stored object.

This caused data corruption where:
- BalanceFile modified the original file's batches and ID, then stored
  the same pointer under a new key, leaving two keys pointing to one
  mutated object
- Other methods called Create() on repository files, modifying their
  control totals

The fix adds a cloneFile() helper that creates a deep copy via JSON
serialization before any mutations. This ensures the original files
in the repository remain unchanged.

Added regression tests to verify original files are preserved after
each operation
@gemini-code-assist
Copy link

Summary of Changes

Hello @MorganaFuture, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a significant data integrity problem within the server's ACH file processing logic. Previously, various service methods were directly modifying file objects retrieved from the repository, causing unintended side effects and corrupting stored data. The core solution involves introducing a deep cloning mechanism for these files, ensuring that any operation performed on a file is done on a copy, thereby safeguarding the original data in the repository from mutation. This change enhances the robustness and reliability of file handling.

Highlights

  • Data Corruption Fix: Addressed a critical issue where several service methods (BalanceFile, BuildFile, GetFileContents, SegmentFileID, FlattenBatches) were inadvertently mutating ach.File objects directly from the in-memory repository, leading to data corruption.
  • Immutability via Deep Cloning: Implemented a cloneFile() helper function that creates a deep copy of an ach.File object using JSON serialization before any modifications. This ensures that the original files stored in the repository remain unchanged and immutable.
  • Regression Tests: Introduced new regression tests (TestBalanceFile_OriginalUnchanged, TestBuildFile_OriginalUnchanged, TestSegmentFileID_OriginalUnchanged, TestFlattenBatches_OriginalUnchanged) to verify that original files are consistently preserved after these operations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a critical data corruption bug by preventing service methods from mutating files in the in-memory repository. The introduction of a cloneFile helper to create deep copies of ach.File objects before modification is a solid approach. The implementation of cloneFile using JSON serialization is well-reasoned and correctly handles validation options. The accompanying regression tests are excellent and provide strong confidence in the fix for the modified methods.

However, I've identified a similar potential mutation issue in the MergeFiles method, which was not modified in this PR. It appears to retrieve files from the repository and use them directly without cloning, which could lead to the same data corruption this PR aims to solve. I strongly recommend applying the same cloning pattern to MergeFiles within this pull request to ensure all mutation paths are covered.

Additionally, there are minor inconsistencies in error handling in the modified methods that could be improved for better debuggability and consistency, which I've pointed out in specific comments.

Comment on lines +132 to +134
if err != nil {
return nil, fmt.Errorf("build file: error cloning file %s: %v", id, err)
}

Choose a reason for hiding this comment

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

medium

For better error inspection and consistency with other parts of the code, it's preferable to use %w for error wrapping instead of %v. This allows for unwrapping the error chain using errors.Is or errors.As.

Suggested change
if err != nil {
return nil, fmt.Errorf("build file: error cloning file %s: %v", id, err)
}
if err != nil {
return nil, fmt.Errorf("build file: error cloning file %s: %w", id, err)
}

Comment on lines +152 to +154
if err != nil {
return nil, fmt.Errorf("problem cloning file %s: %v", id, err)
}

Choose a reason for hiding this comment

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

medium

Similar to other parts of the code, using %w for error wrapping is preferred over %v for better error handling capabilities.

Suggested change
if err != nil {
return nil, fmt.Errorf("problem cloning file %s: %v", id, err)
}
if err != nil {
return nil, fmt.Errorf("problem cloning file %s: %w", id, err)
}

Comment on lines +227 to +229
if err != nil {
return nil, err
}

Choose a reason for hiding this comment

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

medium

The error returned from cloneFile is not wrapped with any context. For consistency with the other methods modified in this PR and for better debuggability, it's a good practice to wrap this error. The other functions add context like which operation failed and for which file ID.

Suggested change
if err != nil {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("balance file: failed to clone file %s: %w", fileID, err)
}

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.

1 participant