Add option --experimental_discard_actions_after_execution in order to turn it off after a release.

Discarding actions hasn't been shown to have a significant positive effect on heap memory usage, after careful research by mschaller@. It's holding back other projects (threading Fileset metadata through) and adding complexity, so it's time to kill it. Out of an abundance of caution, I'll keep actions in memory via a flag flip, then, if it sticks, I'll change the default, and then I'll unwire everything.

PiperOrigin-RevId: 196682768
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildRequestOptions.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildRequestOptions.java
index 42cbc43..cd4e86d 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildRequestOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildRequestOptions.java
@@ -403,6 +403,18 @@
   )
   public boolean keepStateAfterBuild;
 
+  @Option(
+      name = "discard_actions_after_execution",
+      defaultValue = "true",
+      documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
+      metadataTags = OptionMetadataTag.INCOMPATIBLE_CHANGE,
+      effectTags = {OptionEffectTag.LOSES_INCREMENTAL_STATE},
+      help =
+          "If true, Blaze will clear actions from memory after it executes them. Has no effect "
+              + "unless --notrack_incremental_state is also specified. Do not use unless instructed"
+              + " by the Blaze team.")
+  public boolean discardActionsAfterExecution;
+
   /** Converter for jobs: [0, MAX_JOBS] or "auto". */
   public static class JobsConverter extends RangeConverter {
     /**
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SequencedSkyframeExecutor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SequencedSkyframeExecutor.java
index e63ae09..aaeed3f 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SequencedSkyframeExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SequencedSkyframeExecutor.java
@@ -612,12 +612,15 @@
       }
     }
 
+    removeActionsAfterEvaluation.set(
+        !trackIncrementalState
+            && requestOptions != null
+            && requestOptions.discardActionsAfterExecution);
     // Now check if it is necessary to wipe the previous state. We do this if either the previous
     // or current incrementalStateRetentionStrategy requires the build to have been isolated.
     if (oldValueOfTrackIncrementalState != trackIncrementalState) {
       logger.info("Set incremental state to " + trackIncrementalState);
       evaluatorNeedsReset = true;
-      removeActionsAfterEvaluation.set(!trackIncrementalState);
     } else if (!trackIncrementalState) {
       evaluatorNeedsReset = true;
     }
diff --git a/src/test/shell/integration/discard_graph_edges_test.sh b/src/test/shell/integration/discard_graph_edges_test.sh
index d2da4cf..9a69c4e 100755
--- a/src/test/shell/integration/discard_graph_edges_test.sh
+++ b/src/test/shell/integration/discard_graph_edges_test.sh
@@ -371,6 +371,39 @@
   BUILD_FLAGS="$old_build_flags"
 }
 
+function test_actions_deleted_after_execution_explicit() {
+  readonly local old_build_flags="$BUILD_FLAGS"
+  BUILD_FLAGS="$BUILD_FLAGS --discard_actions_after_execution"
+  test_actions_deleted_after_execution
+  BUILD_FLAGS="$old_build_flags"
+}
+
+function test_actions_not_deleted_after_execution() {
+  mkdir -p foo || fail "Couldn't mkdir"
+  cat > foo/BUILD <<'EOF' || fail "Couldn't write file"
+genrule(name = "foo", cmd = "touch $@", outs = ["foo.out"])
+EOF
+  bazel build $BUILD_FLAGS //foo:foo >& "$TEST_log" || fail "Expected success"
+  "${bazel_javabase}/bin/jmap" -histo:live "$(bazel info server_pid)" > histo.txt
+  local genrule_action_count="$(extract_histogram_count histo.txt \
+        'GenRuleAction$')"
+  if [[ "$genrule_action_count" -gt 0 ]]; then
+    cat histo.txt >> "$TEST_log"
+    fail "GenRuleAction unexpectedly found: $genrule_action_count"
+  fi
+
+  bazel build $BUILD_FLAGS --nodiscard_actions_after_execution //foo:foo \
+      >& "$TEST_log" || fail "Expected success"
+  "${bazel_javabase}/bin/jmap" -histo:live "$(bazel info server_pid)" > histo.txt
+  genrule_action_count="$(extract_histogram_count histo.txt \
+        'GenRuleAction$')"
+  if [[ "$genrule_action_count" -lt 1 ]]; then
+    cat histo.txt >> "$TEST_log"
+    fail "GenRuleAction unexpectedly not found: $genrule_action_count"
+  fi
+
+}
+
 function test_dump_after_discard_incrementality_data() {
   bazel build --notrack_incremental_state //testing:mytest >& "$TEST_log" \
        || fail "Expected success"