Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(e2e): harden HCC readiness cleanup proof
Alfredo Lopez9bd2aa5ad8ce
diff --git a/scripts/e2e/_lib/hcc-watch-recovery-fixture.sh b/scripts/e2e/_lib/hcc-watch-recovery-fixture.shindex 0cc7a407f..4bb8f891d 100755--- a/scripts/e2e/_lib/hcc-watch-recovery-fixture.sh+++ b/scripts/e2e/_lib/hcc-watch-recovery-fixture.sh@@ -2,9 +2,21 @@# Safety helpers shared by disruptive HCC fault-injection fixtures.+profile_env_value() {+ local key=$1 file=$2+ awk -v key="$key" '+ index($0, key "=") == 1 {+ print substr($0, length(key) + 2)+ exit+ }+ ' "$file"+}+require_branch_owned_hcc_gate() {local marker_namespace="${1:-control-plane}"- local actual_worktree_id actual_head worktree_dirty+ local actual_worktree_id actual_head actual_cluster_fingerprint actual_gate+ local worktree_dirty expected_short_head expected_branch profile_env+ local pre_gate_state_root cluster_fingerprint_file expected_cluster_fingerprintis_branch_scoped_e2e_context "$E2E_KUBECONTEXT" ||die "fault injection requires a generated branch-scoped context, got '${E2E_KUBECONTEXT}'"@@ -19,16 +31,55 @@ require_branch_owned_hcc_gate() {printf '%s' "$HCC_BRANCH_GATE_REPO_ROOT" | shasum | awk '{print $1}')"HCC_BRANCH_GATE_EXPECTED_HEAD="$(git -C "$HCC_BRANCH_GATE_REPO_ROOT" rev-parse HEAD)"+ expected_short_head="$(git -C "$HCC_BRANCH_GATE_REPO_ROOT" rev-parse --short=8 HEAD)"+ expected_branch="$(git -C "$HCC_BRANCH_GATE_REPO_ROOT" branch --show-current)"+ [ -n "$expected_branch" ] ||+ die "branch-owned runtime proof requires a named branch, not detached HEAD"+ [ "${MINIKUBE_PROFILE:-}" = "$E2E_KUBECONTEXT" ] ||+ die "MINIKUBE_PROFILE must explicitly select context ${E2E_KUBECONTEXT}, got ${MINIKUBE_PROFILE:-missing}"++ profile_env="${E2E_BRANCH_PROFILE_ENV:-${HOME}/.cache/clerum/minikube-profiles/${E2E_KUBECONTEXT}/profile.env}"+ [ -r "$profile_env" ] ||+ die "branch-profile helper evidence is missing at ${profile_env}; run a state-writing branch-profile helper action from this worktree"+ [ "$(profile_env_value PROFILE "$profile_env")" = "$E2E_KUBECONTEXT" ] ||+ die "branch-profile helper output does not select context ${E2E_KUBECONTEXT}"+ [ "$(profile_env_value REPO_DIR "$profile_env")" = "$HCC_BRANCH_GATE_REPO_ROOT" ] ||+ die "branch-profile helper output belongs to a different worktree"+ [ "$(profile_env_value BRANCH "$profile_env")" = "$expected_branch" ] ||+ die "branch-profile helper output belongs to a different branch"+ [ "$(profile_env_value SHA_SHORT "$profile_env")" = "$expected_short_head" ] ||+ die "branch-profile helper output belongs to a different HEAD"+ [ "$(profile_env_value DIRTY "$profile_env")" = false ] ||+ die "branch-profile helper recorded a dirty worktree; refresh it after committing"++ pre_gate_state_root="${E2E_PRE_GATE_STATE_ROOT:-${TMPDIR:-/tmp}/clerum-pre-gate-sync}"+ cluster_fingerprint_file="${pre_gate_state_root}/${HCC_BRANCH_GATE_EXPECTED_WORKTREE_ID}/cluster.sha"+ [ -r "$cluster_fingerprint_file" ] ||+ die "pre-gate fingerprint evidence is missing at ${cluster_fingerprint_file}"+ expected_cluster_fingerprint="$(sed -n '1p' "$cluster_fingerprint_file")"+ [[ "$expected_cluster_fingerprint" =~ ^[0-9a-f]{40}$ ]] ||+ die "pre-gate fingerprint evidence is malformed"+ [ -n "${E2E_EXPECTED_PRE_GATE_GATE:-}" ] ||+ die "E2E_EXPECTED_PRE_GATE_GATE must name the gate recorded by the branch-owned pre-gate sync"+HCC_BRANCH_GATE_SYNC_MARKER="$(kctl get configmap clerum-pre-gate-sync-state -n "$marker_namespace" -o json 2>/dev/null)" || die "cluster has no readable pre-gate sync marker"actual_worktree_id="$(jq -r '.data.worktreeId // ""' <<<"$HCC_BRANCH_GATE_SYNC_MARKER")"actual_head="$(jq -r '.data.gitHead // ""' <<<"$HCC_BRANCH_GATE_SYNC_MARKER")"+ actual_cluster_fingerprint="$(+ jq -r '.data.clusterFingerprint // ""' <<<"$HCC_BRANCH_GATE_SYNC_MARKER"+ )"+ actual_gate="$(jq -r '.data.gate // ""' <<<"$HCC_BRANCH_GATE_SYNC_MARKER")"[ "$actual_worktree_id" = "$HCC_BRANCH_GATE_EXPECTED_WORKTREE_ID" ] ||die "cluster ownership marker does not match this worktree; run minikube-pre-gate-sync first"[ "$actual_head" = "$HCC_BRANCH_GATE_EXPECTED_HEAD" ] ||die "cluster HEAD marker ${actual_head:-missing} does not match ${HCC_BRANCH_GATE_EXPECTED_HEAD}"+ [ "$actual_cluster_fingerprint" = "$expected_cluster_fingerprint" ] ||+ die "cluster fingerprint marker does not match this worktree's last pre-gate sync"+ [ "$actual_gate" = "$E2E_EXPECTED_PRE_GATE_GATE" ] ||+ die "cluster gate marker ${actual_gate:-missing} does not match expected ${E2E_EXPECTED_PRE_GATE_GATE}"}create_hcc_api_proxy() {diff --git a/scripts/e2e/e2e-hcc-mcp-context-readiness.sh b/scripts/e2e/e2e-hcc-mcp-context-readiness.shindex 3de5c5b64..de9e26c7c 100755--- a/scripts/e2e/e2e-hcc-mcp-context-readiness.sh+++ b/scripts/e2e/e2e-hcc-mcp-context-readiness.sh@@ -19,6 +19,9 @@ source "${SCRIPT_DIR}/e2e-lib.sh"# shellcheck source=scripts/e2e/_lib/hcc-watch-recovery-lock.sh# shellcheck disable=SC1091source "${SCRIPT_DIR}/_lib/hcc-watch-recovery-lock.sh"+# shellcheck source=scripts/e2e/_lib/hcc-watch-recovery-fixture.sh+# shellcheck disable=SC1091+source "${SCRIPT_DIR}/_lib/hcc-watch-recovery-fixture.sh"[ -n "$E2E_KUBECONTEXT" ] || {echo "KUBECONTEXT or E2E_K8S_CONTEXT must select an explicit branch-scoped minikube context." >&2@@ -71,6 +74,7 @@ UPSTREAM_DNS_IP=""NEW_HCC_POD=""HCC_UID=""HCC_RESTARTS=""+HCC_PORT=""HCC_MUTATED=0DNS_BLOCKER_CREATED=0MCP_CREATED=0@@ -102,26 +106,18 @@ die() {wait_until() {local timeout=$1 description=$2shift 2- local elapsed=0- while [ "$elapsed" -lt "$timeout" ]; do+ local deadline now+ deadline=$(( $(date +%s) + timeout ))+ while :; do"$@" && return 0+ now="$(date +%s)"+ [ "$now" -lt "$deadline" ] || breaksleep 1- elapsed=$((elapsed + 1))doneecho "Timed out after ${timeout}s waiting for ${description}" >&2return 1}-profile_env_value() {- local key=$1 file=$2- awk -v key="$key" '- index($0, key "=") == 1 {- print substr($0, length(key) + 2)- exit- }- ' "$file"-}-running_hcc_pod() {local rowsrows="$(kctl get pods -n "$HCC_NS" -l "app=${HCC_DEPLOY}" \@@ -131,6 +127,13 @@ running_hcc_pod() {awk -F '\t' '$1 != "" && $4 == "" { print $1; exit }' <<<"$rows"}+hcc_pods_absent() {+ local pods+ pods="$(kctl get pods -n "$HCC_NS" -l "app=${HCC_DEPLOY}" -o name 2>/dev/null)" ||+ return 1+ [ -z "$pods" ]+}+hcc_log_contains() {local marker=$1 logs[ -n "$NEW_HCC_POD" ] || return 1@@ -348,6 +351,11 @@ fixture_resources_absent() {fixture_mcp_runtime_absent && context_policies_absent}+fixture_inputs_absent() {+ resource_absent context "$CONTEXT_NAME" "$MCP_NS" &&+ resource_absent mcpserver "$MCP_NAME" "$MCP_NS"+}+baseline_policies_exist() {kctl get networkpolicy "deny-all-${MCP_NS}" -n "$MCP_NS" >/dev/null 2>&1 &&kctl get networkpolicy "allow-dns-egress-${MCP_NS}" -n "$MCP_NS" >/dev/null 2>&1 &&@@ -381,7 +389,11 @@ const http = require('http');const expected = process.argv[1];function get(path) {return new Promise((resolve, reject) => {- const request = http.get({host: '127.0.0.1', port: 8081, path}, response => {+ const request = http.get({+ host: '127.0.0.1',+ port: Number(process.env.HCC_E2E_PORT),+ path+ }, response => {let body = '';response.setEncoding('utf8');response.on('data', chunk => { body += chunk; });@@ -413,7 +425,7 @@ function get(path) {NODE)"kctl exec "pod/${NEW_HCC_POD}" -n "$HCC_NS" -c host-context-controller -- \- node -e "$probe_script" "$MCP_NAME"+ env "HCC_E2E_PORT=${HCC_PORT}" node -e "$probe_script" "$MCP_NAME"}probe_hcc_final_context() {@@ -424,7 +436,11 @@ const context = process.argv[1];const expected = process.argv[2];function get(path) {return new Promise((resolve, reject) => {- const request = http.get({host: '127.0.0.1', port: 8081, path}, response => {+ const request = http.get({+ host: '127.0.0.1',+ port: Number(process.env.HCC_E2E_PORT),+ path+ }, response => {let body = '';response.setEncoding('utf8');response.on('data', chunk => { body += chunk; });@@ -456,6 +472,7 @@ function get(path) {NODE)"kctl exec "pod/${NEW_HCC_POD}" -n "$HCC_NS" -c host-context-controller -- \+ env "HCC_E2E_PORT=${HCC_PORT}" \node -e "$probe_script" "$CONTEXT_NAME" "$MCP_NAME"}@@ -463,10 +480,11 @@ probe_hcc_ready_pod() {local podpod="$(ready_pod_name "$HCC_NS" "app=${HCC_DEPLOY}")" || return 1kctl exec "pod/${pod}" -n "$HCC_NS" -c host-context-controller -- \+ env "HCC_E2E_PORT=${HCC_PORT}" \node -e 'const http = require("node:http");const request = http.get(- {host: "127.0.0.1", port: 8081, path: "/ready"},+ {host: "127.0.0.1", port: Number(process.env.HCC_E2E_PORT), path: "/ready"},response => {let body = "";response.setEncoding("utf8");@@ -577,40 +595,57 @@ EOF}cleanup() {- local status=$? cleanup_failed=0 restore_ok=1 restore_patch+ local status=$? cleanup_failed=0 restore_ok=1 fixture_inputs_removed=1 restore_patchtrap - EXITset +eif [ "$HCC_MUTATED" = 1 ]; thenkctl scale deployment "$HCC_DEPLOY" -n "$HCC_NS" --replicas=0 >/dev/null 2>&1 ||restore_ok=0- kctl wait pod -n "$HCC_NS" -l "app=${HCC_DEPLOY}" --for=delete \- --timeout=120s >/dev/null 2>&1 || restore_ok=0+ if [ "$restore_ok" = 1 ]; then+ wait_until 120 "HCC pods to stop before fixture cleanup" \+ hcc_pods_absent >/dev/null 2>&1 || restore_ok=0+ fifiif [ "$CONTEXT_CREATED" = 1 ]; thenkctl delete context "$CONTEXT_NAME" -n "$MCP_NS" \--ignore-not-found --wait=true --timeout=60s >/dev/null 2>&1 ||- cleanup_failed=1+ {+ cleanup_failed=1+ fixture_inputs_removed=0+ }fiif [ "$MCP_CREATED" = 1 ]; thenkctl delete mcpserver "$MCP_NAME" -n "$MCP_NS" \--ignore-not-found --wait=true --timeout=60s >/dev/null 2>&1 ||- cleanup_failed=1+ {+ cleanup_failed=1+ fixture_inputs_removed=0+ }+ fi+ if ! wait_until 60 "gate-owned Context and McpServer inputs to disappear" \+ fixture_inputs_absent >/dev/null 2>&1; then+ cleanup_failed=1+ fixture_inputs_removed=0fiif [ "$HCC_MUTATED" = 1 ]; then- restore_patch="$(jq -cn --arg dnsPolicy "$ORIGINAL_DNS_POLICY" \- '{spec:{template:{spec:{dnsPolicy:$dnsPolicy,dnsConfig:null}}}}')"- kctl patch deployment "$HCC_DEPLOY" -n "$HCC_NS" --type=strategic \- -p "$restore_patch" >/dev/null 2>&1 || restore_ok=0- kctl scale deployment "$HCC_DEPLOY" -n "$HCC_NS" \- --replicas="${ORIGINAL_REPLICAS:-1}" >/dev/null 2>&1 || restore_ok=0- kctl rollout status deployment "$HCC_DEPLOY" -n "$HCC_NS" \- --timeout=240s >/dev/null 2>&1 || restore_ok=0- if [ "$restore_ok" = 1 ]; then- wait_until 60 "restored HCC DNS, replicas, Ready pod, and /ready endpoint" \- hcc_restore_is_verified >/dev/null 2>&1 || restore_ok=0+ if [ "$fixture_inputs_removed" = 1 ] && [ "$restore_ok" = 1 ]; then+ restore_patch="$(jq -cn --arg dnsPolicy "$ORIGINAL_DNS_POLICY" \+ '{spec:{template:{spec:{dnsPolicy:$dnsPolicy,dnsConfig:null}}}}')"+ kctl patch deployment "$HCC_DEPLOY" -n "$HCC_NS" --type=strategic \+ -p "$restore_patch" >/dev/null 2>&1 || restore_ok=0+ kctl scale deployment "$HCC_DEPLOY" -n "$HCC_NS" \+ --replicas="${ORIGINAL_REPLICAS:-1}" >/dev/null 2>&1 || restore_ok=0+ kctl rollout status deployment "$HCC_DEPLOY" -n "$HCC_NS" \+ --timeout=240s >/dev/null 2>&1 || restore_ok=0+ if [ "$restore_ok" = 1 ]; then+ wait_until 60 "restored HCC DNS, replicas, Ready pod, and /ready endpoint" \+ hcc_restore_is_verified >/dev/null 2>&1 || restore_ok=0+ fi+ else+ restore_ok=0fifi@@ -638,63 +673,19 @@ cleanup() {[ "$restore_ok" = 1 ] || print_repair_instructions[ "$status" = 0 ] && status=1fi+ if [ "$status" = 0 ] && [ "$cleanup_failed" = 0 ] && [ "$restore_ok" = 1 ]; then+ header "HCC McpServer/Context readiness gate passed"+ echo "$blocked_probe"+ echo "$post_context_probe"+ echo "$final_probe"+ fiexit "$status"}trap cleanup EXITheader "HCC readiness during initial McpServer and Context convergence"-repo_root="$(git -C "${SCRIPT_DIR}/../.." rev-parse --show-toplevel)"-[ -z "$(git -C "$repo_root" status --porcelain --untracked-files=normal)" ] ||- die "worktree has uncommitted changes; commit and pre-gate sync before runtime proof"-expected_worktree_id="$(printf '%s' "$repo_root" | shasum | awk '{print $1}')"-expected_head="$(git -C "$repo_root" rev-parse HEAD)"-expected_short_head="$(git -C "$repo_root" rev-parse --short=8 HEAD)"-expected_branch="$(git -C "$repo_root" branch --show-current)"-[ -n "$expected_branch" ] ||- die "branch-owned runtime proof requires a named branch, not detached HEAD"-[ -z "${MINIKUBE_PROFILE:-}" ] || [ "$MINIKUBE_PROFILE" = "$E2E_KUBECONTEXT" ] ||- die "MINIKUBE_PROFILE ${MINIKUBE_PROFILE} disagrees with context ${E2E_KUBECONTEXT}"--profile_env="${E2E_BRANCH_PROFILE_ENV:-${HOME}/.cache/clerum/minikube-profiles/${E2E_KUBECONTEXT}/profile.env}"-[ -r "$profile_env" ] ||- die "branch-profile helper evidence is missing at ${profile_env}; run a state-writing branch-profile helper action from this worktree"-[ "$(profile_env_value PROFILE "$profile_env")" = "$E2E_KUBECONTEXT" ] ||- die "branch-profile helper output does not select context ${E2E_KUBECONTEXT}"-[ "$(profile_env_value REPO_DIR "$profile_env")" = "$repo_root" ] ||- die "branch-profile helper output belongs to a different worktree"-[ "$(profile_env_value BRANCH "$profile_env")" = "$expected_branch" ] ||- die "branch-profile helper output belongs to a different branch"-[ "$(profile_env_value SHA_SHORT "$profile_env")" = "$expected_short_head" ] ||- die "branch-profile helper output belongs to a different HEAD"-[ "$(profile_env_value DIRTY "$profile_env")" = false ] ||- die "branch-profile helper recorded a dirty worktree; refresh it after committing"--pre_gate_state_root="${E2E_PRE_GATE_STATE_ROOT:-${TMPDIR:-/tmp}/clerum-pre-gate-sync}"-cluster_fingerprint_file="${pre_gate_state_root}/${expected_worktree_id}/cluster.sha"-[ -r "$cluster_fingerprint_file" ] ||- die "pre-gate fingerprint evidence is missing at ${cluster_fingerprint_file}"-expected_cluster_fingerprint="$(sed -n '1p' "$cluster_fingerprint_file")"-printf '%s\n' "$expected_cluster_fingerprint" | grep -Eq '^[0-9a-f]{40}$' ||- die "pre-gate fingerprint evidence is malformed"-[ -n "${E2E_EXPECTED_PRE_GATE_GATE:-}" ] ||- die "E2E_EXPECTED_PRE_GATE_GATE must name the gate recorded by the branch-owned pre-gate sync"--sync_marker="$(kctl get configmap clerum-pre-gate-sync-state -n "$HCC_NS" -o json \- 2>/dev/null)" ||- die "cluster has no readable pre-gate sync marker"-actual_worktree_id="$(jq -r '.data.worktreeId // ""' <<<"$sync_marker")"-actual_head="$(jq -r '.data.gitHead // ""' <<<"$sync_marker")"-actual_cluster_fingerprint="$(jq -r '.data.clusterFingerprint // ""' <<<"$sync_marker")"-actual_gate="$(jq -r '.data.gate // ""' <<<"$sync_marker")"-[ "$actual_worktree_id" = "$expected_worktree_id" ] ||- die "cluster ownership marker does not match this worktree"-[ "$actual_head" = "$expected_head" ] ||- die "cluster HEAD marker ${actual_head:-missing} does not match ${expected_head}"-[ "$actual_cluster_fingerprint" = "$expected_cluster_fingerprint" ] ||- die "cluster fingerprint marker does not match this worktree's last pre-gate sync"-[ "$actual_gate" = "$E2E_EXPECTED_PRE_GATE_GATE" ] ||- die "cluster gate marker ${actual_gate:-missing} does not match expected ${E2E_EXPECTED_PRE_GATE_GATE}"+require_branch_owned_hcc_gate "$HCC_NS"kctl get nodes -o json |jq -e 'any(.items[]; .metadata.labels["minikube.k8s.io/name"] != null)' >/dev/null ||die "target context is not a minikube cluster"@@ -720,6 +711,17 @@ ORIGINAL_DNS_CONFIG="$(kctl get deployment "$HCC_DEPLOY" -n "$HCC_NS" -o json |HCC_IMAGE="$(kctl get deployment "$HCC_DEPLOY" -n "$HCC_NS" \-o jsonpath='{.spec.template.spec.containers[?(@.name=="host-context-controller")].image}')"[ -n "$HCC_IMAGE" ] || die "could not resolve the running HCC image"+HCC_PORT="$(kctl get deployment "$HCC_DEPLOY" -n "$HCC_NS" -o json | jq -r '+ .spec.template.spec.containers[]? |+ select(.name == "host-context-controller") as $container |+ (+ [$container.env[]? | select(.name == "CONTEXT_MAPPER_PORT") | .value][0] //+ [$container.ports[]? | select(.name == "http") | .containerPort][0] //+ empty+ )+')"+[[ "$HCC_PORT" =~ ^[0-9]+$ ]] && [ "$HCC_PORT" -ge 1 ] && [ "$HCC_PORT" -le 65535 ] ||+ die "could not resolve a valid HCC HTTP port from the Deployment"UPSTREAM_DNS_IP="$(kctl get service kube-dns -n kube-system -o jsonpath='{.spec.clusterIP}')"[ -n "$UPSTREAM_DNS_IP" ] || die "could not resolve kube-system/kube-dns ClusterIP"running_dns_pods="$(kctl get pods -n kube-system -l k8s-app=kube-dns \@@ -922,8 +924,8 @@ ok "isolated DNS hold proxy is ready and forwards non-fixture UDP/TCP queries toHCC_MUTATED=1kctl scale deployment "$HCC_DEPLOY" -n "$HCC_NS" --replicas=0 >/dev/null-kctl wait pod -n "$HCC_NS" -l "app=${HCC_DEPLOY}" --for=delete \- --timeout=120s >/dev/null || die "existing HCC pod did not stop"+wait_until 120 "existing HCC pods to stop" hcc_pods_absent ||+ die "existing HCC pod did not stop"CONTEXT_CREATED=1kctl apply -f - >/dev/null <<EOF@@ -1103,7 +1105,4 @@ ok "released MCP convergence created the real runtime and external-egress policyok "Context ingress, mcp-host egress, and rpc-proxy egress policies reflect the latest watch state"ok "HCC stayed Ready on the same pod throughout blocked and released convergence"-header "HCC McpServer/Context readiness gate passed"-echo "$blocked_probe"-echo "$post_context_probe"-echo "$final_probe"+header "HCC McpServer/Context assertions complete; restoring branch-owned runtime"diff --git a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh b/scripts/e2e/e2e-hcc-readiness-bootstrap.shindex d806b9956..4e5614886 100755--- a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh+++ b/scripts/e2e/e2e-hcc-readiness-bootstrap.sh@@ -100,16 +100,6 @@ wait_until() {return 1}-profile_env_value() {- local key=$1 file=$2- awk -v key="$key" '- index($0, key "=") == 1 {- print substr($0, length(key) + 2)- exit- }- ' "$file"-}-running_hcc_pod() {local rowsrows="$(kctl get pods -n "$HCC_NS" -l "app=${HCC_DEPLOY}" \@@ -442,46 +432,6 @@ trap cleanup EXITheader "HCC readiness during initial Host fleet reconciliation"require_branch_owned_hcc_gate "$HCC_NS"-repo_root="$HCC_BRANCH_GATE_REPO_ROOT"-expected_worktree_id="$HCC_BRANCH_GATE_EXPECTED_WORKTREE_ID"-expected_short_head="$(git -C "$repo_root" rev-parse --short=8 HEAD)"-expected_branch="$(git -C "$repo_root" branch --show-current)"-[ -n "$expected_branch" ] ||- die "branch-owned runtime proof requires a named branch, not detached HEAD"-[ -z "${MINIKUBE_PROFILE:-}" ] || [ "$MINIKUBE_PROFILE" = "$E2E_KUBECONTEXT" ] ||- die "MINIKUBE_PROFILE ${MINIKUBE_PROFILE} disagrees with context ${E2E_KUBECONTEXT}"--profile_env="${E2E_BRANCH_PROFILE_ENV:-${HOME}/.cache/clerum/minikube-profiles/${E2E_KUBECONTEXT}/profile.env}"-[ -r "$profile_env" ] ||- die "branch-profile helper evidence is missing at ${profile_env}; run a state-writing branch-profile helper action from this worktree"-[ "$(profile_env_value PROFILE "$profile_env")" = "$E2E_KUBECONTEXT" ] ||- die "branch-profile helper output does not select context ${E2E_KUBECONTEXT}"-[ "$(profile_env_value REPO_DIR "$profile_env")" = "$repo_root" ] ||- die "branch-profile helper output belongs to a different worktree"-[ "$(profile_env_value BRANCH "$profile_env")" = "$expected_branch" ] ||- die "branch-profile helper output belongs to a different branch"-[ "$(profile_env_value SHA_SHORT "$profile_env")" = "$expected_short_head" ] ||- die "branch-profile helper output belongs to a different HEAD"-[ "$(profile_env_value DIRTY "$profile_env")" = false ] ||- die "branch-profile helper recorded a dirty worktree; refresh it after committing"--pre_gate_state_root="${E2E_PRE_GATE_STATE_ROOT:-${TMPDIR:-/tmp}/clerum-pre-gate-sync}"-cluster_fingerprint_file="${pre_gate_state_root}/${expected_worktree_id}/cluster.sha"-[ -r "$cluster_fingerprint_file" ] ||- die "pre-gate fingerprint evidence is missing at ${cluster_fingerprint_file}"-expected_cluster_fingerprint="$(sed -n '1p' "$cluster_fingerprint_file")"-printf '%s\n' "$expected_cluster_fingerprint" | grep -Eq '^[0-9a-f]{40}$' ||- die "pre-gate fingerprint evidence is malformed"-[ -n "${E2E_EXPECTED_PRE_GATE_GATE:-}" ] ||- die "E2E_EXPECTED_PRE_GATE_GATE must name the gate recorded by the branch-owned pre-gate sync"--sync_marker="$HCC_BRANCH_GATE_SYNC_MARKER"-actual_cluster_fingerprint="$(jq -r '.data.clusterFingerprint // ""' <<<"$sync_marker")"-actual_gate="$(jq -r '.data.gate // ""' <<<"$sync_marker")"-[ "$actual_cluster_fingerprint" = "$expected_cluster_fingerprint" ] ||- die "cluster fingerprint marker does not match this worktree's last pre-gate sync"-[ "$actual_gate" = "$E2E_EXPECTED_PRE_GATE_GATE" ] ||- die "cluster gate marker ${actual_gate:-missing} does not match expected ${E2E_EXPECTED_PRE_GATE_GATE}"ok "branch helper, profile, exact HEAD/fingerprint/gate verified"kctl get nodes -o json |diff --git a/scripts/tests/test-hcc-watch-recovery-gate.sh b/scripts/tests/test-hcc-watch-recovery-gate.shindex c5f5bb8a3..1f6350138 100755--- a/scripts/tests/test-hcc-watch-recovery-gate.sh+++ b/scripts/tests/test-hcc-watch-recovery-gate.sh@@ -13,8 +13,9 @@ LOG_HELPER="${SCRIPT_DIR}/_lib/hcc-watch-recovery-logs.sh"FIXTURE_HELPER="${SCRIPT_DIR}/_lib/hcc-watch-recovery-fixture.sh"MOCK_STATE_FILE="$(mktemp "${TMPDIR:-/tmp}/hcc-lock-test.XXXXXX")"MOCK_LOG_FILE="$(mktemp "${TMPDIR:-/tmp}/hcc-log-test.XXXXXX")"+MOCK_PROFILE_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/hcc-profile-test.XXXXXX")"rm -f "$MOCK_STATE_FILE"-trap 'rm -f "$MOCK_STATE_FILE" "$MOCK_LOG_FILE"' EXIT+trap 'rm -f "$MOCK_STATE_FILE" "$MOCK_LOG_FILE"; rm -rf "$MOCK_PROFILE_ROOT"' EXITpass() { echo "PASS: $1"; }fail() { echo "FAIL: $1"; FAIL=1; }@@ -30,14 +31,13 @@ done# Literal source-code assertions.# shellcheck disable=SC2016if grep -Fq 'source "${SCRIPT_DIR}/_lib/hcc-watch-recovery-fixture.sh"' "$READINESS_GATE" &&- grep -Fq 'require_branch_owned_hcc_gate "$HCC_NS"' "$READINESS_GATE" &&- grep -Fq 'sync_marker="$HCC_BRANCH_GATE_SYNC_MARKER"' "$READINESS_GATE" &&- ! grep -Fq 'sync_marker="$(kctl get configmap' "$READINESS_GATE" &&- grep -Fq "actual_cluster_fingerprint=\"\$(jq -r '.data.clusterFingerprint" "$READINESS_GATE" &&- grep -Fq "actual_gate=\"\$(jq -r '.data.gate" "$READINESS_GATE"; then- pass "readiness gate reuses one shared branch-owned ownership, HEAD, fingerprint, and gate snapshot"+ [ "$(grep -Fc 'require_branch_owned_hcc_gate "$HCC_NS"' "$READINESS_GATE")" = 1 ] &&+ ! grep -Fq 'HCC_BRANCH_GATE_SYNC_MARKER' "$READINESS_GATE" &&+ ! grep -Fq 'cluster_fingerprint_file=' "$READINESS_GATE" &&+ ! grep -Fq 'profile_env=' "$READINESS_GATE"; then+ pass "readiness gate delegates ownership, HEAD, profile, fingerprint, and gate proof once"else- fail "readiness gate duplicates, bypasses, or splits the branch-owned marker snapshot"+ fail "readiness gate duplicates, bypasses, or splits the shared branch-owned proof"fifixture_gate_function="$(sed -n '/^require_branch_owned_hcc_gate() {$/,/^}$/p' "$FIXTURE_HELPER")"@@ -56,6 +56,132 @@ elsefail "shared branch gate can split ownership and HEAD across marker reads"fi+# Literal source-code assertions.+# shellcheck disable=SC2016+if [[ "$fixture_gate_function" == *'expected_branch="$(git -C "$HCC_BRANCH_GATE_REPO_ROOT" branch --show-current)"'* ]] &&+ [[ "$fixture_gate_function" == *'[ -n "$expected_branch" ]'* ]] &&+ [[ "$fixture_gate_function" == *'[ "${MINIKUBE_PROFILE:-}" = "$E2E_KUBECONTEXT" ]'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env="${E2E_BRANCH_PROFILE_ENV:-'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env_value PROFILE'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env_value REPO_DIR'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env_value BRANCH'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env_value SHA_SHORT'* ]] &&+ [[ "$fixture_gate_function" == *'profile_env_value DIRTY'* ]] &&+ [[ "$fixture_gate_function" == *'cluster_fingerprint_file='* ]] &&+ [[ "$fixture_gate_function" == *'.data.clusterFingerprint'* ]] &&+ [[ "$fixture_gate_function" == *'.data.gate'* ]] &&+ [[ "$fixture_gate_function" == *'E2E_EXPECTED_PRE_GATE_GATE'* ]]; then+ pass "shared branch gate owns the complete profile, fingerprint, and expected-gate proof"+else+ fail "shared branch gate omits a required profile, fingerprint, or expected-gate boundary"+fi++for gate in "${GATES[@]}"; do+ marker_reads="$(grep -Fc 'kctl get configmap clerum-pre-gate-sync-state' "$gate")"+ if [ "$marker_reads" = 0 ] &&+ ! grep -Fq 'profile_env=' "$gate" &&+ ! grep -Fq 'cluster_fingerprint_file=' "$gate" &&+ grep -Fq 'require_branch_owned_hcc_gate' "$gate"; then+ pass "$(basename "$gate") delegates its branch-owned proof to the shared authority"+ else+ fail "$(basename "$gate") duplicates or bypasses the shared branch-owned proof"+ fi+done++run_branch_gate_case() (+ local mode=$1+ local repo_root="${MOCK_PROFILE_ROOT}/repo"+ local context="clerum-codex-profile-test-1234abcd"+ local head="1111111111111111111111111111111111111111"+ local short_head="11111111"+ local branch="codex/profile-test"+ local fingerprint="2222222222222222222222222222222222222222"+ local marker_fingerprint="$fingerprint"+ local expected_gate="full"+ local marker_gate="$expected_gate"+ local minikube_profile="$context"+ local profile="$context"+ local profile_repo="$repo_root"+ local profile_branch="$branch"+ local profile_sha="$short_head"+ local profile_dirty=false+ local mock_worktree_dirty=""+ local worktree_id+ worktree_id="$(printf '%s' "$repo_root" | shasum | awk '{print $1}')"++ case "$mode" in+ success) ;;+ detached) branch="" ;;+ minikube-profile-missing) minikube_profile="" ;;+ minikube-profile) minikube_profile="clerum-other-profile-1234abcd" ;;+ profile-name) profile="clerum-other-profile-1234abcd" ;;+ profile-repo) profile_repo="${repo_root}-other" ;;+ profile-branch) profile_branch="codex/other" ;;+ profile-sha) profile_sha="aaaaaaaa" ;;+ profile-dirty) profile_dirty=true ;;+ dirty-worktree) mock_worktree_dirty=" M changed.ts" ;;+ fingerprint) marker_fingerprint="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ;;+ gate) marker_gate="targeted" ;;+ *) exit 98 ;;+ esac++ mkdir -p "${MOCK_PROFILE_ROOT}/state/${worktree_id}"+ printf '%s\n' "$fingerprint" >"${MOCK_PROFILE_ROOT}/state/${worktree_id}/cluster.sha"+ printf '%s\n' \+ "PROFILE=${profile}" \+ "REPO_DIR=${profile_repo}" \+ "BRANCH=${profile_branch}" \+ "SHA_SHORT=${profile_sha}" \+ "DIRTY=${profile_dirty}" >"${MOCK_PROFILE_ROOT}/profile.env"++ E2E_KUBECONTEXT="$context"+ MINIKUBE_PROFILE="$minikube_profile"+ E2E_BRANCH_PROFILE_ENV="${MOCK_PROFILE_ROOT}/profile.env"+ E2E_PRE_GATE_STATE_ROOT="${MOCK_PROFILE_ROOT}/state"+ E2E_EXPECTED_PRE_GATE_GATE="$expected_gate"+ HCC_NS=control-plane+ SCRIPT_DIR="${ROOT}/scripts/e2e"+ MOCK_MARKER="$(+ jq -cn --arg worktreeId "$worktree_id" --arg gitHead "$head" \+ --arg clusterFingerprint "$marker_fingerprint" --arg gate "$marker_gate" \+ '{data:{worktreeId:$worktreeId,gitHead:$gitHead,+ clusterFingerprint:$clusterFingerprint,gate:$gate}}'+ )"++ is_branch_scoped_e2e_context() { return 0; }+ die() { exit 97; }+ git() {+ local command=$3+ shift 3+ case "$command $*" in+ "rev-parse --show-toplevel") printf '%s\n' "$repo_root" ;;+ "status --porcelain --untracked-files=normal") printf '%s\n' "$mock_worktree_dirty" ;;+ "rev-parse HEAD") printf '%s\n' "$head" ;;+ "rev-parse --short=8 HEAD") printf '%s\n' "$short_head" ;;+ "branch --show-current") printf '%s\n' "$branch" ;;+ *) return 96 ;;+ esac+ }+ kctl() { printf '%s\n' "$MOCK_MARKER"; }+ # shellcheck source=scripts/e2e/_lib/hcc-watch-recovery-fixture.sh+ source "$FIXTURE_HELPER"+ require_branch_owned_hcc_gate "$HCC_NS"+)++if run_branch_gate_case success; then+ pass "shared branch gate accepts matching branch-profile evidence"+else+ fail "shared branch gate rejects matching branch-profile evidence"+fi+for case_name in detached minikube-profile-missing minikube-profile profile-name profile-repo profile-branch \+ profile-sha profile-dirty dirty-worktree fingerprint gate; do+ if run_branch_gate_case "$case_name" >/dev/null 2>&1; then+ fail "shared branch gate accepted mismatched ${case_name} evidence"+ else+ pass "shared branch gate rejects mismatched ${case_name} evidence"+ fi+done+# shellcheck source=scripts/e2e/_lib/hcc-watch-recovery-logs.shsource "$LOG_HELPER"HCC_LOG_BUFFER="$MOCK_LOG_FILE"@@ -286,6 +412,43 @@ elsefail "readiness gate can announce a final pass before cleanup and ownership release finish"fi+mcp_cleanup_body="$(sed -n '/^cleanup() {$/,/^}$/p' "$MCP_READINESS_GATE")"+# shellcheck disable=SC2016+mcp_input_absence_line="$(+ grep -nF 'fixture_inputs_absent' <<<"$mcp_cleanup_body" | head -1 | cut -d: -f1+)"+# shellcheck disable=SC2016+mcp_restore_line="$(+ grep -nF 'restore_patch="$(jq -cn' <<<"$mcp_cleanup_body" | head -1 | cut -d: -f1+)"+if [[ "$mcp_cleanup_body" == *'fixture_inputs_removed=0'* ]] &&+ [[ "$mcp_cleanup_body" == *'if [ "$fixture_inputs_removed" = 1 ] && [ "$restore_ok" = 1 ]; then'* ]] &&+ [ -n "$mcp_input_absence_line" ] &&+ [ -n "$mcp_restore_line" ] &&+ [ "$mcp_input_absence_line" -lt "$mcp_restore_line" ]; then+ pass "MCP readiness cleanup keeps HCC stopped until fixture input deletion is verified"+else+ fail "MCP readiness cleanup can restart HCC after an unverified Context or McpServer deletion"+fi++mcp_finalize_line="$(+ grep -nF 'finalize_hcc_watch_gate_lock "$cleanup_failed" "$restore_ok"' \+ <<<"$mcp_cleanup_body" | head -1 | cut -d: -f1+)"+mcp_pass_line="$(+ grep -nF 'header "HCC McpServer/Context readiness gate passed"' \+ <<<"$mcp_cleanup_body" | head -1 | cut -d: -f1+)"+if [ "$(grep -Fc 'header "HCC McpServer/Context readiness gate passed"' \+ "$MCP_READINESS_GATE")" = 1 ] &&+ [ -n "$mcp_finalize_line" ] &&+ [ -n "$mcp_pass_line" ] &&+ [ "$mcp_finalize_line" -lt "$mcp_pass_line" ]; then+ pass "MCP readiness gate announces PASS only after cleanup and lock finalization"+else+ fail "MCP readiness gate can announce PASS before cleanup and lock finalization"+fi+wait_until_body="$(sed -n '/^wait_until() {$/,/^}$/p' "$READINESS_GATE")"# Literal source-code assertion.# shellcheck disable=SC2016@@ -298,6 +461,28 @@ elsefail "readiness gate retains a command-latency, zero-pod wait, or hard-coded-port edge"fi+mcp_wait_until_body="$(sed -n '/^wait_until() {$/,/^}$/p' "$MCP_READINESS_GATE")"+mcp_port_probe_count="$(grep -Fc 'env "HCC_E2E_PORT=${HCC_PORT}"' "$MCP_READINESS_GATE")"+mcp_pod_absence_count="$(grep -Fc 'hcc_pods_absent' "$MCP_READINESS_GATE")"+# Literal source-code assertions.+# shellcheck disable=SC2016+if grep -Fq 'source "${SCRIPT_DIR}/_lib/hcc-watch-recovery-fixture.sh"' \+ "$MCP_READINESS_GATE" &&+ [ "$(grep -Fc 'require_branch_owned_hcc_gate "$HCC_NS"' "$MCP_READINESS_GATE")" = 1 ] &&+ ! grep -Fq 'HCC_BRANCH_GATE_SYNC_MARKER' "$MCP_READINESS_GATE" &&+ ! grep -Fq 'cluster_fingerprint_file=' "$MCP_READINESS_GATE" &&+ ! grep -Fq 'profile_env=' "$MCP_READINESS_GATE" &&+ [[ "$mcp_wait_until_body" == *'deadline=$(( $(date +%s) + timeout ))'* ]] &&+ grep -Fq 'hcc_pods_absent' "$MCP_READINESS_GATE" &&+ ! grep -Fq -- '--for=delete' "$MCP_READINESS_GATE" &&+ ! grep -Fq 'port: 8081' "$MCP_READINESS_GATE" &&+ [ "$mcp_port_probe_count" = 3 ] &&+ [ "$mcp_pod_absence_count" = 3 ]; then+ pass "MCP readiness gate shares exact-head ownership and uses bounded portable runtime probes"+else+ fail "MCP readiness gate duplicates ownership or retains a deadline, pod-absence, or port edge"+fi+for gate in "${GATES[@]}"; doacquire_line="$(grep -nF 'acquire_hcc_watch_gate_lock' "$gate" | tail -1 | cut -d: -f1)"case "$gate" in