Skip to content

feat(tes/data/crypt4gh): add a teardown executor - #73

Merged
uniqueg merged 5 commits into
devfrom
feat/67-crypt4gh-teardown
Nov 12, 2026
Merged

uniqueg merged 5 commits into
devfrom
feat/67-crypt4gh-teardown

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds a teardown executor to tes.data.crypt4gh that removes the decrypted data after the task's own executors.

  • Teardown executor (setting teardown, on by default). It is added after the task's own executors and runs the decryption image's script with --clear <volume>. This removes everything in the plugin's volume but keeps the volume itself, which may be a mount point. That covers the decrypted files, temporary files of a stopped decryption, and files that the task's executors derived from the decrypted files there (e.g. an index written next to its input). The volume is reserved for the plugin: no input, output or volume of the task may be in it, so nothing there is a result. Symbolic links in it are removed, not followed, and --clear refuses relative paths, / and a directory that is itself a link. Missing or already empty volumes are fine, so a restarted executor runs again. If an entry can't be removed, the executor exits with status 1.
  • teardown_on_error (opt-in, off by default). Sets ignore_error: true on the task's own executors, but not on the decryption or teardown executor, so that the teardown also runs after one of them fails.
  • Invariants from feat(tes/data/crypt4gh): add the Crypt4GH plugin #66 are kept:
    • A task that the plugin already rewrote is recognised as before (exact decryption executor first, volume once). On a second pass it gets no second decryption executor and no second teardown.
    • Exact copies of the teardown executor among the task's own executors are moved to the end, so it runs once, after all of them. Executors that differ from it in any field are kept.
    • Tasks left to the app, and tasks without Crypt4GH inputs, are untouched.
    • Path checks run before the rewrite, unchanged.
  • Image: decrypt.py gets --clear DIRECTORY, which can't be combined with --key/--file. image/smoke.sh now also runs the teardown twice and checks three things: the volume is empty (including a derived file in a subdirectory and a temporary file), a link out of the volume was not followed, and the volume and a file outside it remain.
  • Docs: README (what it does, example, new section "The teardown executor" with the TES semantics and backend limits, settings table, keys and decrypted data, changes from the middleware) and CHANGELOG.

TES semantics and backends (why teardown_on_error is opt-in)

  • TES 1.1, tesExecutor.ignore_error: by default, execution stops at the first executor that fails. With ignore_error: true, the runner records the exit code and continues with the next executor. So by default the teardown runs only if all of the task's own executors succeed.
  • TESK (tesk-api): accepts only ignore_error: false and refuses true with 400 Bad Request. With teardown_on_error on, every Crypt4GH task sent to TESK would be refused.
  • Funnel (calypr/funnel, internal/worker/worker.go): an executor error is cleared when that executor has ignore_error, so a task whose own executor failed ends COMPLETE if the teardown succeeds. Later executors also run after a failure.
  • When backends remove the data otherwise:
    • Funnel removes the worker's work directory unless LeaveWorkDir is set.
    • TESK deletes the task's PVC, unless KEEP_FAILED_PVC keeps the PVCs of tasks that don't complete.
    • The README lists these as of the sources in November 2026.

Because turning it on changes how tasks run and how they are reported, and breaks TESK, teardown_on_error stays off by default. The README documents the limits.

Verification

  • uv run ruff check, ruff format --check, ast-grep scan, python -m scripts.packages mypy plugins/tes/data/crypt4gh: passed.
  • coverage run --source plugins/tes/data/crypt4gh/src -m pytest plugins/tes/data/crypt4gh + coverage report: 129 passed, 100% (Python 3.13, FOCA 0.22.0).
  • coverage run --source scripts,docs -m pytest tests: passed, 100%.
  • mkdocs build --strict (docs group): passed.
  • uv build --all-packages --no-create-gitignore + python -m scripts.checks dist dist: passed.
  • pre-commit run --all-files: passed.
  • docker build -f image/Dockerfile … + image/smoke.sh: passed, including the new teardown checks.
  • Two review rounds, one for correctness and one for maintainability; findings are fixed except those listed below. The main change from round 1: the teardown now empties the reserved volume instead of removing the decrypted files by name, so derived plaintext next to the decrypted files is removed too. Round 2 found only lows: --clear now refuses a directory that is a link, and some wording was fixed.
  • CI: all checks passed on 37d8bc2 ("CI passed", including the smoke test and the test matrix).
  • Sourcery (4 threads): 2 fixed in 37d8bc2, 2 answered (see below):
    • Fixed: directories in the volume are now removed entry by entry, so an entry that can't be removed no longer stops the removal of its siblings.
    • Fixed: the test of teardown: false compares the whole list of executors.
    • After the fix: 130 tests passed, 100%, and the smoke test passed.

Declined review findings

  • Dropping only a trailing copy of the teardown executor, instead of moving all exact copies to the end, is declined. A copy left in the middle would remove the decrypted data before the task's later executors run. The README says that exact copies are moved.
  • Skipping ignore_error for a teardown executor left from a pass with other settings (teardown off, teardown_on_error on) is declined. The README already says to chain gateways with this plugin only with the same settings.
  • Expressing the --clear vs --key/--file rule with an argparse mutually exclusive group is declined. argparse can't express "either --clear, or both --key and --file", so the checks stay explicit and tested.
  • Sourcery's request that the decryption executor empty the volume when its own cleanup fails is declined. That is the decryption executor's behaviour from feat(tes/data/crypt4gh): add the Crypt4GH plugin #66, and this PR doesn't change it. It logs each file it can't remove, the task fails, and the README says the files stay until the backend removes the task's working directory.
  • Sourcery's request that the teardown list the volume again until it is empty is declined. TES runs executors one after another, each in its own container, so nothing of an earlier executor writes to the volume while the teardown runs.

Open questions

None.

Closes #67

🤖 Generated with Claude Code

https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV

uniqueg and others added 4 commits November 12, 2026 03:29
Add the option --remove to the decryption script, for the teardown
executor: it removes each decrypted file and the temporary files left
next to it by a stopped decryption. Missing files are fine, so that a
restarted executor runs again; files that can't be removed are logged
and give exit status 1. --remove doesn't go with --key or --file.

The image's smoke test runs the teardown twice and checks that it
removes the decrypted and temporary files and keeps other files.

Refs #67

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
Add the teardown executor after the task's own executors, which removes
the decrypted files (setting teardown, on by default). Copies of it
among the task's own executors are dropped, so that it runs once, at
the end, also when a task passes the plugin twice.

The opt-in setting teardown_on_error sets ignore_error on the task's
own executors, so that the teardown runs after they fail. It is off by
default: it lets later executors run after a failure, Funnel then
reports such tasks as COMPLETE, and TESK refuses ignore_error true.
The README documents these limits and when backends remove the
decrypted files otherwise.

Closes #67

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
The teardown executor now runs the script with --clear VOLUME instead
of --remove for each decrypted file: it removes everything in the
volume, but keeps the volume itself. The volume is reserved for the
plugin, so this also removes files that the task's executors derived
from the decrypted files there (e.g. an index next to its input) and
temporary files of a stopped decryption, without matching names.
Symbolic links are removed, not followed; --clear refuses relative
paths and /.

From the review: README on moved teardown copies and backend sources,
a test for near-copies of the teardown executor, the full argparse
error, split changelog entries and smoke-test messages.

Refs #67

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
--clear now refuses a directory that is itself a symbolic link, so that
its target is never emptied; links inside it were already removed, not
followed. Wording of the summaries, docstrings and help now says that
the teardown empties the volume, not only the decrypted files.

Refs #67

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
@sourcery-ai

sourcery-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add the teardown settings to the plugin configuration, with teardown enabled by default and teardown-on-error disabled.

  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/__init__.py L77-78

API

Add the script’s --clear DIRECTORY mode and its argument validation alongside the existing decryption command-line interface.

  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py L25-32, L221-228, L247-274 (+3 small edits)

Implementation

Validate that teardown-on-error is used only with teardown, and pass both settings into task rewriting.

  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/__init__.py L123-136, L225-226

Implement safe, repeatable removal of the reserved volume’s contents and dispatch --clear separately from decryption.

  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py L159-212 (+3 small edits)

Construct the teardown executor and update rewriting to deduplicate it at the end while optionally setting ignore_error on task executors.

  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/task.py L248-264, L315-333, L340-344 (+1 small edits)

Documentation

Explain teardown behavior, backend error semantics, configuration, and the decrypted-data lifecycle, and update the changelog and plugin descriptions.

  • plugins/tes/data/crypt4gh/CHANGELOG.md L18-23
  • plugins/tes/data/crypt4gh/README.md L75-80, L123-171, L240-246 (+5 small edits)
  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/__init__.py L9-10, L55-61, L153-155
  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py L5-8 (+3 small edits)
  • plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/task.py L6-9

Tests

Extend plugin rewriting, configuration, script, and executor tests to cover teardown ordering, cleanup safety, error behavior, and idempotence.

  • plugins/tes/data/crypt4gh/tests/test_crypt4gh.py L73-81, L272-345, L720-725 (+6 small edits)
  • plugins/tes/data/crypt4gh/tests/test_decrypt.py L520-762, L768-772 (+2 small edits)

Update the image smoke test to verify repeatable teardown, removal of derived and temporary files, and preservation of the mount and external files.

  • plugins/tes/data/crypt4gh/image/smoke.sh L7-11, L63-80

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@read-the-docs-community

read-the-docs-community Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 foca-plugins | 🛠️ Build #35830825 | 📁 Comparing 37d8bc2 against latest (acbfb0c)

  🔍 Preview build  

1 file changed
± catalogue/index.html

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (acbfb0c) to head (37d8bc2).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev       #73   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          826       886   +60     
  Branches       111       125   +14     
=========================================
+ Hits           826       886   +60     
Flag Coverage Δ
foca-plugin-tes-data-crypt4gh 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 12, 2026 05:13
@sourcery-ai

sourcery-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 37d8bc2

#67 feat(tes/data/crypt4gh): add a teardown executor · 3 of 3 met

# Requirement Status Notes
1 Add an optional teardown executor after the task's own executors that removes the decrypted files; enable it by default if TES semantics allow it. ✅ Met The teardown setting defaults to true, and the executor that clears the volume is appended after the task's own executors.
2 Run teardown after a task executor fails where supported, using ignore_error on the task's executors and documenting backend limits. ✅ Met teardown_on_error opts in to setting ignore_error: true on the task's own executors; the README documents TES and backend limitations.
3 Add the setting, README and changelog updates, and tests. ✅ Met The change adds both settings, documents teardown behavior and backend limits, updates the changelog, and adds executor and cleanup tests.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/task.py" line_range="342" />
<code_context>
-    task["volumes"] = [*items(task, "volumes"), volume]
+        if ignore_errors:
+            executor["ignore_error"] = True
+    task["executors"] = [decryption, *own, *([removal] if teardown else [])]
+    if not again:
+        task["volumes"] = [*items(task, "volumes"), volume]
</code_context>
<issue_to_address>
**Failed decryption skips teardown**

When decryption fails and removal of a written target or temporary file also fails, `remove` only logs an `OSError` and continues, so a failed decryption can leave plaintext or temporary files behind; the decryption executor then fails before the final teardown runs, including with `teardown_on_error`, and the files remain until backend cleanup.

Ensure failed-decryption cleanup retries or clears the volume before returning failure, and report cleanup failures explicitly.
</issue_to_address>

### Comment 2
<location path="plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py" line_range="192" />
<code_context>
+after them that empties that volume again. The successor of the Crypt4GH
+middleware of proTES.
 <!-- --8<-- [end:summary] -->

 | | |
</code_context>
<issue_to_address>
**Files remain after teardown succeeds**

When a task executor leaves a process running that writes into the volume during teardown, `clear` uses one initial directory snapshot and returns `done` without checking again, so files created after the snapshot remain in the volume while teardown reports success, leaving decrypted or derived data on disk.

Have `clear` recheck the directory before reporting success and continue clearing until it is empty.

Also at `plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py:170`.
</issue_to_address>

### Comment 3
<location path="plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py" line_range="182" />
<code_context>
+            # Links are removed, never followed
+            if entry.is_dir() and not entry.is_symlink():
+                shutil.rmtree(entry)
+            else:
+                entry.unlink()
+        except FileNotFoundError:
</code_context>
<issue_to_address>
**Files remain in partly cleared directories**

When a nested entry in a directory being cleared cannot be removed, `shutil.rmtree(entry)` stops at the first nested removal error, and `clear` catches the error only after that traversal has stopped, leaving removable siblings in the subtree and plaintext in the volume while teardown exits nonzero.

Change the directory-removal logic to continue attempting to remove the remaining descendants after an individual removal error.
</issue_to_address>

### Comment 4
<location path="plugins/tes/data/crypt4gh/tests/test_crypt4gh.py" line_range="278" />
<code_context>
+    request = make_inbound(make_task())
+    plugin.on_request(request, make_context())
+    executors = request.payload["executors"]
+    assert len(executors) == 2
+    assert executors[0] == decryption_executor(
+        ("/in/reads.fq.c4gh", "/vol/crypt4gh/reads.fq")
</code_context>
<issue_to_address>
**Disabled teardown can drop task work**

When the `teardown=False` path emits the decryption executor and teardown executor instead of preserving the task's executor, `test_on_request_can_leave_out_teardown` at lines 278 and 282 checks only the executor count and that the second executor lacks `ignore_error`; an output containing the decryption executor followed by the teardown executor therefore passes even though it drops the task's executor and adds teardown when disabled.

Assert that the second executor equals the original task executor, or compare the complete expected executor list.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 4 findings to address first, and after the task’s executors finish, the teardown script removes every entry in the configured volume, including files those executors derived from decrypted inputs. If that volume contains data the task needs to retain, a successful teardown leaves no failure signal and reverting the change will not restore the deleted files.

Blocking findings: plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/task.py:342, plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py:192, plugins/tes/data/crypt4gh/src/foca_plugins/tes/data/crypt4gh/decrypt.py:182, plugins/tes/data/crypt4gh/tests/test_crypt4gh.py:278


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

The teardown removed directories in the volume with shutil.rmtree,
which stops at the first entry it can't remove and leaves the rest of
that directory. It now removes directories entry by entry and goes on
after errors, so only the entries that can't be removed stay; links
are still removed, not followed.

The test of the setting teardown off now compares the whole list of
executors.

Refs #67

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
@uniqueg
uniqueg merged commit 712780d into dev Nov 12, 2026
88 of 90 checks passed
@uniqueg
uniqueg deleted the feat/67-crypt4gh-teardown branch November 12, 2026 05:34
uniqueg added a commit that referenced this pull request Nov 12, 2026
Brings in the teardown executor (#73). The changelog keeps the entries
of both: the teardown entries and the published image.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
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.

feat(tes/data/crypt4gh): add a teardown executor

1 participant