Skip to content

TaskHasher folds the evaluated task.ext.* value, putting task.cpus back in the task hash #7663

Closed

Description

@jorgee

Summary

TaskHasher deliberately hashes the un-interpolated script: source so that referencing a resource directive does not invalidate the cache — the comment says so outright (TaskHasher.groovy:60-63):

add source code of script: or exec: block

  • this allows task script to reference directives like task.cpus without invalidating the cache

It then folds in the fully evaluated value of every task.ext.* the script references, which puts task.cpus straight back into the key. The two halves of the same hash disagree about whether resource allocation counts.

Mechanism

TaskHasher.compute()                                  TaskHasher.groovy:90-93
  └─ def vars = getTaskGlobalVars(); keys.add(vars.entrySet())
       └─ getTaskGlobalVars()                         :159-164
            ├─ task.getGlobalVars(binding)
            └─ getTaskExtensionDirectiveVars()        :166-178
                 for( key : task.getVariableNames() )
                     if( !key.startsWith('task.ext.') ) continue
                     final value = taskConfig.eval(key.substring(5))
                     result.put(key, value)           // ← the EVALUATED value
                          └─ TaskConfig.eval()        TaskConfig.groovy:153-155
                               └─ LazyMap.getValue() → resolve()   :827-830
                                    // runs the closure against the task context

The nf-core idiom that trips it is common. nf-core/rnaseq, conf/modules/trimgalore.config:

ext.args = {
    def preset_args_map = ["--fastqc_args": "'-t ${task.cpus}'"]
    ...
}

resolve() evaluates that, so the string folded into the hash is --fastqc_args '-t 8' on one compute environment and --fastqc_args '-t 3' on another.

Evidence

Two runs of nf-core/rnaseq pinned to the same commit, same container, same inputs, on compute environments that allocate different CPUs. The lineage TaskRun records for the same task (TRIMGALORE, same sample) differ in exactly one hash-relevant field:

globalVars, run A (task.cpus=3)
  { "task.ext.args": "--fastqc_args '-t 3'", "task.ext.prefix": "H1_REP1_trimmed" }

globalVars, run B (task.cpus=8)
  { "task.ext.args": "--fastqc_args '-t 8'", "task.ext.prefix": "H1_REP1_trimmed" }

codeChecksum   identical      ← the un-interpolated source
container      identical
input          identical
script         differs        ← the RENDERED command; not a hash input, by design

--fastqc_args '-t 8' versus '-t 3' changes a thread count. trim_galore produces the same result either way, so by the hash's own definition of a match — same script, same inputs, same container — this is a false invalidation.

Impact

Harmless for -resume: the same run has the same compute environment and therefore the same task.cpus. It becomes systematic for a cache shared across compute environments, of the kind the SPIs in #7638 enable.

It also cascades. The invalidated process re-executes and its outputs are not byte-reproducible (trim_galore gzips, and gzip embeds mtime), so a content-derived identity sees new inputs downstream and those tasks miss too — even where their own script and CPU count match. In the run above, a downstream SALMON_QUANT with an identical script and identical task.cpus on both sides still missed.

The cascade is not inherent, which is what makes the cost worth quoting: BEDTOOLS_GENOMECOV also re-executed, but its output is deterministic, so both of its consumers (UCSC_BEDCLIP, UCSC_BEDGRAPHTOBIGWIG, 7 tasks each) hit anyway. One hash term in one upstream process accounted for roughly 250 task re-executions out of ~300.

Possible fixes

  1. Normalize resource directives during the ext.* evaluation. Evaluate against a context whose cpus, memory, time, disk and accelerator resolve to a sentinel. Removes the compute-environment dependency and leaves every other ext input intact. Smallest change, contained in getTaskExtensionDirectiveVars().

  2. Fold the unevaluated expression instead of its value, symmetric with how task.source is already treated. More consistent with the stated intent, but a closure that reads params or meta would then collide across genuinely different configurations, so those inputs would have to be folded separately — more design work.

Happy to prepare a PR for (1) if that direction looks right.

Activity

  1. bentsherman commented on Sep 22, 2026

    @bentsherman
    Member

    I don't see the issue here. If the user puts something into ext then they accept that it will contribute to the task hash

  2. jorgee commented on Sep 22, 2026

    @jorgee
    ContributorAuthor

    Ok, fine with me. The fix seems quite complicated. I wanted to raise it; I got it when re-executing the same pipeline with optimized values in the platform. It changed the CPUs and memory, and some nf-core/rnaseq tasks were not cached because the ext.args contained CPU values.

  3. awgymer commented on Nov 23, 2026

    @awgymer

    Given this is a somewhat common idiom in nf-core I am wondering if there is some way a user could give a set of keys not to include in the hash? Or a cache setting that allows the user to simply not include ext at all?

  4. bentsherman commented on Nov 23, 2026

    @bentsherman
    Member

    I think it's better to maintain a simple contract when it comes to the task hash. If you supply a directive like task.cpus through an input, whether a process input or ext, then you accept that it will affect the task hash. If you want to avoid that then you should not supply directives as inputs in the first place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions