How to determine if configuration-cache is enabled?

Is there a programmatic way to determine if a run is being stored-to or read-from the configuration cache? I would expect such information to live in StartParameter but I can’t find it there.

You can do a serialization hack to detect that your state has been restored from disk, but if there’s a “real” way to do it that would be better.

None that is officially supported.
Here is an issue you can thumbs-up that requests it: Public API for deciding whether config caching is enabled · Issue #14375 · gradle/gradle · GitHub.
In the meantime you could use an internal class, but that’s fragile of course:

(gradle.startParameter as StartParameterInternal).configurationCache.get()

Thanks @Vampire. I ended up going this way:

class MyTask {
  transient Object flag = new Object();

  boolean isRunningUnderConfigCache() { return flag == null; }

This method only works for runs that are serialized from the cache, it doesn’t work for the first run.