#!/bin/bash
# Publishes the Felix FV memo plan: each lane's cache key, and whether an
# earlier run of this PR already passed that lane with the same inputs.
# felix-build and the lanes act on this plan rather than querying the cache
# themselves, so that they all act on one decision.

set -euo pipefail

# The lanes trust the memo to skip only what already passed, and a bug there
# would show up as nothing but green lanes.
.semaphore/vms/test-batch-memo.sh

plan=felix-fv-plan
mkdir -p "$plan"
: > "$plan/lanes"

# Calculate the scope of the memoization cache: the prefix of every key.  It
# is per PR, so a run reuses only passes from earlier runs of its own PR, and
# empty (memoization off) for anything else.
scope=""
if [ "${CI_GIT_REF_TYPE:-}" = PR ] && [ "${FELIX_FV_MEMO:-on}" = on ]; then
  scope="felix-fv-memo/pr-${CI_GIT_PR_NUMBER}"
fi
echo "$scope" > "$plan/scope"

if [ -n "$scope" ]; then
  # Memoization enabled: calculate one key per lane, hashing every file the
  # lane executes, so the key changes whenever the lane's result could.
  GOFLAGS=-buildvcs=false go run ./hack/cmd/felix-fv-memo \
    .argoci/ciworkflow.yaml .argoci/modules/20-felix.yaml > "$plan/keys"
else
  # Memoization disabled: with no keys, every lane runs in full.
  : > "$plan/keys"
fi

# A lane hits if an earlier run stored a pass of the whole lane under its key.
# A lane that misses still looks up each of its batches when it runs.
hits=0
while read -r group class key; do
  state=miss
  if artifact cache has_key "$scope/$key/lane" >/dev/null; then
    state=hit
    hits=$((hits + 1))
  fi
  echo "$group $class $key $state" >> "$plan/lanes"
done < "$plan/keys"

# felix-build builds what every lane's VMs run, so it can be skipped only when
# no lane needs to run a batch.
lanes=$(wc -l < "$plan/lanes")
build=run
if [ "$lanes" -gt 0 ] && [ "$hits" -eq "$lanes" ]; then
  build=skip
fi
echo "$build" > "$plan/build"

echo "Cache scope: ${scope:-none (not a PR, or disabled)}"
echo "Lanes (group class key state):"
sed 's/^/  /' "$plan/lanes"
# One line per run, for mining the logs.
echo "FV-MEMO-PLAN felix-build=${build} lanes=${lanes} lanes-hit=${hits}" \
  "pr=${CI_GIT_PR_NUMBER:-none} workflow=${CI_WORKFLOW_NAME:-unknown}"

artifact push workflow "$plan"
