Skip to content

fix: checkpoints inside modules are overwritten#2843

Closed
fgvieira wants to merge 3 commits intosnakemake:mainfrom
fgvieira:module_checkpoints
Closed

fix: checkpoints inside modules are overwritten#2843
fgvieira wants to merge 3 commits intosnakemake:mainfrom
fgvieira:module_checkpoints

Conversation

@fgvieira
Copy link
Copy Markdown
Contributor

@fgvieira fgvieira commented Apr 24, 2024

Description

When including two modules with checkpoints (or the same module twice with different prefixes), only the checkpoint of the last loaded module is used.

In the example here, the checkpoint somestep is run twice the module test_b (under prefix b) while it should be run one for each prefix (as exemplified under expected-results).

Assuming unrestricted shared filesystem usage.
Building DAG of jobs...
Using shell: /usr/bin/bash
Provided cores: 1 (use --cores to define parallelism)
Rules claiming more threads will be scaled down.
Job stats:
job                 count
----------------  -------
all                     1
test_a_aggregate        1
test_b_aggregate        1
test_b_somestep         2
total                   5

Select jobs to execute...
Execute 1 jobs...

[Wed Apr 24 13:36:30 2024]
localcheckpoint test_b_somestep:
    output: b/somestep/a.txt
    jobid: 2
    reason: Missing output files: <TBD>
    wildcards: sample=a
    resources: tmpdir=/tmp
DAG of jobs will be updated after completion.

[Wed Apr 24 13:36:31 2024]
Finished job 2.
1 of 5 steps (20%) done
Select jobs to execute...
Execute 1 jobs...

[Wed Apr 24 13:36:31 2024]
localrule test_a_alt_intermediate:
    output: a/alt/a.txt
    jobid: 8
    reason: Missing output files: a/alt/a.txt
    wildcards: sample=a
    resources: tmpdir=/tmp

[Wed Apr 24 13:36:31 2024]
Finished job 8.
2 of 6 steps (33%) done
Select jobs to execute...
Execute 1 jobs...

[Wed Apr 24 13:36:31 2024]
localcheckpoint test_b_somestep:
    output: b/somestep/b.txt
    jobid: 4
    reason: Missing output files: <TBD>
    wildcards: sample=b
    resources: tmpdir=/tmp
DAG of jobs will be updated after completion.
[...]

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • Tests
    • Enhanced test validations with additional assertions ensuring functions produce the expected results.
    • Introduced a new helper utility that returns a consistent preset value to support reliable testing.
    • Added an expected output file for improved test verification.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@fgvieira fgvieira added bug Something isn't working help wanted Extra attention is needed labels Apr 24, 2024
@fgvieira fgvieira changed the title fix: checkpoints inside modules are oerwritten (only the last is used) fix: checkpoints inside modules are overwritten Apr 24, 2024
@johanneskoester johanneskoester self-assigned this Apr 29, 2024
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Nov 13, 2024

📝 Walkthrough

Walkthrough

This pull request adds assertions to a test Snakefile and introduces a new function in a module-specific Snakefile. The assertions check that the function, defined in the module file and returning a constant value of 15, behaves as expected when invoked from two test contexts. In addition, a new expected results file is added, providing a reference output for validation.

Changes

File Path Changes Summary
tests/test_module_checkpoint/Snakefile Added two assertions validating that some_func() returns 15 in both test_a and test_b modules.
tests/test_module_checkpoint/module-test/Snakefile Introduced a new function some_func() that returns a constant value of 15.
tests/test_module_checkpoint/expected-results/a/…/a.txt Created a new file containing the line alt_a.

Sequence Diagram(s)

sequenceDiagram
    participant Runner as Test Runner
    participant Test as Test Snakefile
    participant Module as Module-Test Snakefile

    Runner->>Test: Execute test for test_a
    Test->>Module: Invoke some_func()
    Module-->>Test: Return 15
    Test->>Runner: Assert value equals 15

    Runner->>Test: Execute test for test_b
    Test->>Module: Invoke some_func()
    Module-->>Test: Return 15
    Test->>Runner: Assert value equals 15
Loading

Possibly related PRs

Suggested reviewers

  • johanneskoester

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b62f8c and 5c707c4.

📒 Files selected for processing (2)
  • tests/test_module_checkpoint/Snakefile (1 hunks)
  • tests/test_module_checkpoint/module-test/Snakefile (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/test_module_checkpoint/Snakefile
  • tests/test_module_checkpoint/module-test/Snakefile
⏰ Context from checks skipped due to timeout of 90000ms (31)
  • GitHub Check: tests (10, windows-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py311)
  • GitHub Check: tests (9, windows-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py311)
  • GitHub Check: tests (8, windows-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py311)
  • GitHub Check: tests (7, windows-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py311)
  • GitHub Check: tests (6, windows-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py311)
  • GitHub Check: tests (5, windows-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py311)
  • GitHub Check: tests (4, windows-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py311)
  • GitHub Check: tests (3, windows-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py311)
  • GitHub Check: tests (2, windows-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py311)
  • GitHub Check: tests (1, windows-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py311)
  • GitHub Check: apidocs

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
tests/test_module_checkpoint/Snakefile (1)

1-36: Well-structured test design for module checkpoint verification

The test setup effectively demonstrates the checkpoint bug by:

  1. Using identical modules with different prefixes and configs
  2. Ensuring independent execution paths through unique prefixes
  3. Verifying consistent behavior through assertions

This design should successfully validate that checkpoints maintain independence across module instances.

tests/test_module_checkpoint/module-test/Snakefile (2)

3-4: Add docstring to clarify function purpose

The function's purpose in testing module functionality should be documented for better maintainability.

 def some_func():
+    """Helper function that returns 15, used to verify module loading and execution."""
     return 15

7-11: Consider adding output verification

While the workflow structure is good for testing module checkpoint behavior, consider adding output verification to ensure the correct paths are being used.

Add a verification step to the aggregate rule:

 rule aggregate:
     input:
         aggregate_input
     output:
         "aggregated/{sample}.txt"
+    run:
+        # Verify input path matches expected module prefix
+        expected_prefix = "post" if wildcards.sample == "a" else "alt"
+        input_path = str(input[0])
+        assert input_path.startswith(expected_prefix), f"Unexpected input path: {input_path}"
+        shell("touch {output}")
-    shell:
-        "touch {output}"

Also applies to: 42-48

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 76d5329 and 2b47be2.

📒 Files selected for processing (6)
  • tests/test_module_checkpoint/Snakefile (1 hunks)
  • tests/test_module_checkpoint/expected-results/a/alt/a.txt (1 hunks)
  • tests/test_module_checkpoint/expected-results/a/somestep/a.txt (1 hunks)
  • tests/test_module_checkpoint/expected-results/b/alt/b.txt (1 hunks)
  • tests/test_module_checkpoint/expected-results/b/somestep/b.txt (1 hunks)
  • tests/test_module_checkpoint/module-test/Snakefile (1 hunks)
✅ Files skipped from review due to trivial changes (4)
  • tests/test_module_checkpoint/expected-results/a/alt/a.txt
  • tests/test_module_checkpoint/expected-results/a/somestep/a.txt
  • tests/test_module_checkpoint/expected-results/b/alt/b.txt
  • tests/test_module_checkpoint/expected-results/b/somestep/b.txt
🔇 Additional comments (5)
tests/test_module_checkpoint/Snakefile (4)

1-1: LGTM: Shell configuration is appropriate

Setting bash as the shell executable ensures consistent behavior across different environments.


35-36: Verify the function definition and its expected behavior

The assertions check for consistent behavior of some_func() across modules. Let's verify its implementation.

#!/bin/bash
# Description: Verify some_func() definition and implementation
# Expected: Find function definition that returns 15

echo "Searching for some_func definition:"
rg "def\s+some_func" -A 5 "tests/test_module_checkpoint/module-test/Snakefile"

28-32: Verify the existence of referenced aggregate rules

The all rule references test_a_aggregate and test_b_aggregate. Let's verify these rules exist in the module.

#!/bin/bash
# Description: Verify referenced rules exist in the module
# Expected: Find 'aggregate' rule definition in the module Snakefile

echo "Searching for aggregate rule definition:"
rg "rule\s+aggregate:" "tests/test_module_checkpoint/module-test/Snakefile"

4-25: Verify the module Snakefile and its checkpoint implementation

The module configuration looks correct, but we should verify the referenced Snakefile and its checkpoint implementation.

tests/test_module_checkpoint/module-test/Snakefile (1)

12-18: Validate config value before writing to file

The checkpoint writes the raw config value to a file without validation. This could cause issues if the config contains special characters or is not a string.

Consider adding config validation:

 checkpoint somestep:
     output:
         "somestep/{sample}.txt"
+    run:
+        # Ensure config is string-safe
+        config_str = str(config).replace('\n', ' ').strip()
+        with open(output[0], 'w') as f:
+            f.write(config_str)
-    shell:
-        # simulate some output vale
-        "echo {config} > {output}"

@johanneskoester
Copy link
Copy Markdown
Contributor

@fgvieira shouldn't the testcase fail?

@johanneskoester
Copy link
Copy Markdown
Contributor

ping @fgvieira

@fgvieira
Copy link
Copy Markdown
Contributor Author

fgvieira commented Jan 7, 2025

Sorry for the late reply. The test does not fail because both modules have the same rules (since it is the same Snakefile being added twice) and rule test_b_somestep ends up being called twice. As I see it, each module should call its own rules, so the dryrun should look like:

job                 count
----------------  -------
all                     1
test_a_aggregate        1
test_b_aggregate        1
test_a_somestep         1
test_b_somestep         1
total                   5

@Hocnonsense
Copy link
Copy Markdown
Contributor

@fgvieira Hello, may I push some commits based on your code?

@fgvieira
Copy link
Copy Markdown
Contributor Author

Sure! 😄

@sonarqubecloud
Copy link
Copy Markdown

@Hocnonsense Hocnonsense moved this from In progress to In review in Snakemake Hackathon March 2025 Mar 12, 2025
@Hocnonsense
Copy link
Copy Markdown
Contributor

@fgvieira @matthewfeickert Hello, do you think this solution #3359 accepable? I just don't know how to merge that to this PR

johanneskoester added a commit that referenced this pull request Mar 14, 2025
update of #2843, may merge to there

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

- **New Features**
- Introduced a modular workflow with distinct processing stages and
clear output checkpoints.
  - Added new modules and rules for enhanced workflow capabilities.
  
- **Tests**
- Expanded the test suite with additional module checkpoint coverage for
enhanced workflow integrity.

- **Refactor**
- Optimized checkpoint management to improve modularity and stability in
workflow execution.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: fgvieira <1151762+fgvieira@users.noreply.github.com>
Co-authored-by: Johannes Koester <johannes.koester@uni-due.de>
@fgvieira
Copy link
Copy Markdown
Contributor Author

@Hocnonsense seems to be working. Thanks!
I guess there is no need for this PR then.

@fgvieira fgvieira closed this Mar 14, 2025
@github-project-automation github-project-automation bot moved this from In review to Done in Snakemake Hackathon March 2025 Mar 14, 2025
@fgvieira fgvieira deleted the module_checkpoints branch March 14, 2025 15:15
@Hocnonsense
Copy link
Copy Markdown
Contributor

Cheers! Happy that I can help!

kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
update of snakemake#2843, may merge to there

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

- **New Features**
- Introduced a modular workflow with distinct processing stages and
clear output checkpoints.
  - Added new modules and rules for enhanced workflow capabilities.
  
- **Tests**
- Expanded the test suite with additional module checkpoint coverage for
enhanced workflow integrity.

- **Refactor**
- Optimized checkpoint management to improve modularity and stability in
workflow execution.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: fgvieira <1151762+fgvieira@users.noreply.github.com>
Co-authored-by: Johannes Koester <johannes.koester@uni-due.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working help wanted Extra attention is needed

Projects

Development

Successfully merging this pull request may close these issues.

3 participants