mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:18:10 +01:00
fix(deploy): a week-old main still passes "are you on main?" (#285)
Production was rolled back today and self-healed before anyone looked.
CI shipped main at 09:47 and verified it live at 09:48 — the deploy log
says "live build is fe9395b950 — the commit this deploy shipped". Ten
minutes later /opt/fleetcrown/app was overwritten with a tree ~25
commits old, and for the next ten minutes production served the
previous day's code, including a health endpoint too old to report
which commit it was running. The next unrelated deploy quietly put main
back.
check-deploy-ref.sh was supposed to have ended this class, and it
cannot: it asks "are you on main?", which every commit that ever landed
on main answers yes to, forever. A checkout sitting on last week's main
walks straight through it. The build-ref marker added in #279 makes the
damage visible after the fact, which is how this was found, but nothing
made it impossible.
So gate on the direction of the move instead of the name of the branch:
the commit currently live must be an ANCESTOR of the commit being
shipped, i.e. a deploy may only ever add history. An older commit, or a
sideways jump onto unrelated history, is refused with the two subjects
printed and the fix spelled out. ALLOW_ROLLBACK=1 is the override, so a
deliberate rollback is one word, not a workaround.
Undecidable cases warn and allow on purpose — no marker on the box, a
SHA this checkout has not fetched. This gate must never be the reason a
box that is already broken cannot be repaired.
The gate is its own script rather than a branch inside deploy-hetzner.sh
so it can be tested without deploying anything: ten cases over throwaway
git repos, covering the exact regression, the fast-forward it must not
slow down, the override, and every undecidable path.
Co-authored-by: t <t@t.t>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+211
-1
No files matched your search
+2
-1
@@ -19,7 +19,7 @@
|
||||
"lint": "eslint src/ home/",
|
||||
"check:design": "bash scripts/check-design-system.sh",
|
||||
"check:desktop": "bash scripts/check-desktop.sh",
|
||||
"verify": "tsc --noEmit && npm run lint && npm run check:design && npm run check:desktop && npm run test:unit && npm run test:home && npm run test:deploy-ref-gate && npm run test:ops",
|
||||
"verify": "tsc --noEmit && npm run lint && npm run check:design && npm run check:desktop && npm run test:unit && npm run test:home && npm run test:deploy-ref-gate && npm run test:not-behind-gate && npm run test:ops",
|
||||
"smoke": "bash scripts/smoke.sh",
|
||||
"test:home": "bash scripts/test-home.sh",
|
||||
"test:ops": "bash scripts/hetzner/test-host-alerts.sh && bash scripts/hetzner/test-disk-gc.sh",
|
||||
@@ -42,6 +42,7 @@
|
||||
"test:notify-close": "npx tsx scripts/test/notify-close.ts",
|
||||
"test:fact-budget": "npx tsx scripts/test/fact-budget.ts",
|
||||
"test:deploy-ref-gate": "bash scripts/test/deploy-ref-gate.sh",
|
||||
"test:not-behind-gate": "bash scripts/test/not-behind-gate.sh",
|
||||
"test:approval-cues": "npx tsx scripts/test/approval-cues.ts",
|
||||
"test:groq-error": "npx tsx scripts/test/groq-error.ts",
|
||||
"test:answering-prompt": "npx tsx scripts/test/answering-prompt.ts",
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
# check-not-behind.sh — refuse a deploy that would move production BACKWARDS.
|
||||
#
|
||||
# Usage: check-not-behind.sh <live_sha> <shipping_sha> [repo_dir]
|
||||
# exit 0 → safe to ship (fast-forward, redeploy, or undecidable-but-allowed)
|
||||
# exit 1 → refuse
|
||||
#
|
||||
# The sibling gate check-deploy-ref.sh asks "are you on main?". That is a
|
||||
# different question, and it is why this one exists: every commit that ever
|
||||
# landed on main passes the branch check forever, so a deploy run from a
|
||||
# checkout sitting on a WEEK-OLD main sails straight through it and overwrites
|
||||
# production with old code.
|
||||
#
|
||||
# That happened on 2026-08-15. CI shipped main and verified it live at 09:48;
|
||||
# ten minutes later a hand-run deploy replaced it with a tree ~25 commits old,
|
||||
# and for the next ten minutes production served code from the day before —
|
||||
# including a health endpoint too old to report which commit it was running,
|
||||
# which is what made the regression hard to see. It has happened before (see
|
||||
# src/app/api/health/route.ts).
|
||||
#
|
||||
# The rule: the commit currently live must be an ANCESTOR of the commit being
|
||||
# shipped. Then the deploy only ever adds history. Anything else — an older
|
||||
# commit, or a sideways jump to an unrelated branch — drops whatever is live
|
||||
# and is refused. ALLOW_ROLLBACK=1 is the deliberate override.
|
||||
#
|
||||
# Undecidable cases (no marker on the box, unresolvable SHA, a commit this
|
||||
# checkout has never fetched) WARN and allow: this gate must never be the reason
|
||||
# a box that is already broken cannot be repaired.
|
||||
set -uo pipefail
|
||||
|
||||
LIVE="${1:-}"
|
||||
SHIPPING="${2:-}"
|
||||
REPO="${3:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
|
||||
|
||||
short() { printf '%.12s' "$1"; }
|
||||
subject() { git -C "$REPO" log -1 --format=%s "$1" 2>/dev/null; }
|
||||
|
||||
if [ "${ALLOW_ROLLBACK:-}" = "1" ]; then
|
||||
echo " ⚠ ALLOW_ROLLBACK=1 — skipping the not-behind check (deliberate rollback)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -z "$LIVE" ]; then
|
||||
echo " ⚠ box reports no build-ref — nothing to compare against, shipping anyway"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -z "$SHIPPING" ]; then
|
||||
echo " ⚠ cannot resolve the commit being shipped — skipping the not-behind check"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! git -C "$REPO" cat-file -e "${LIVE}^{commit}" 2>/dev/null; then
|
||||
echo " ⚠ live build $(short "$LIVE") is unknown to this checkout — run \`git fetch\` to make this check meaningful; shipping anyway"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! git -C "$REPO" cat-file -e "${SHIPPING}^{commit}" 2>/dev/null; then
|
||||
echo " ⚠ shipping commit $(short "$SHIPPING") is not a commit in this checkout — skipping the not-behind check"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$LIVE" = "$SHIPPING" ]; then
|
||||
echo " ✓ not-behind: redeploying the commit already live ($(short "$LIVE"))"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if git -C "$REPO" merge-base --is-ancestor "$LIVE" "$SHIPPING" 2>/dev/null; then
|
||||
echo " ✓ not-behind: fast-forward $(short "$LIVE") → $(short "$SHIPPING")"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
{
|
||||
echo "✗ REFUSING TO DEPLOY — this would move production BACKWARDS."
|
||||
echo " live on box : $(short "$LIVE") $(subject "$LIVE")"
|
||||
echo " this deploy : $(short "$SHIPPING") $(subject "$SHIPPING")"
|
||||
echo " The live commit is not an ancestor of what you are shipping, so this"
|
||||
echo " ship would drop everything on the box that is not in your tree."
|
||||
echo " Fix: git fetch && git rebase origin/main (then redeploy)"
|
||||
echo " Or: ALLOW_ROLLBACK=1 <your deploy command> if a rollback is what you mean."
|
||||
} >&2
|
||||
exit 1
|
||||
@@ -179,6 +179,25 @@ if [ ! -d "$STANDALONE/.next/static" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ── Regression guard: never ship a commit that is BEHIND what is already live ──
|
||||
#
|
||||
# This script ships whatever the working tree is on, and the box accepts it
|
||||
# without question — so a deploy run from a stale checkout silently rolls
|
||||
# production back. It has done so repeatedly (see the comment in
|
||||
# src/app/api/health/route.ts). Most recently on 2026-08-15: CI shipped and
|
||||
# verified main at 09:48, and ten minutes later a hand-run deploy replaced it
|
||||
# with a tree ~25 commits old. The build-ref marker made that *visible* — it is
|
||||
# how this was found — but visibility after the fact is not prevention.
|
||||
#
|
||||
# The box's marker is the SSOT for what is running. If that commit is an
|
||||
# ancestor of what we are about to ship, this is a fast-forward and safe.
|
||||
# Anything else moves production backwards, or sideways onto an unrelated
|
||||
# branch, and is refused. ALLOW_ROLLBACK=1 is the deliberate override for a
|
||||
# genuine rollback.
|
||||
SHIPPING_SHA="$(git -C "$PROJECT_DIR" rev-parse "${REF:-HEAD}" 2>/dev/null || echo "")"
|
||||
LIVE_REF_NOW="$(ssh "$HOST" "cat '$APP_DIR/.build-ref' 2>/dev/null" 2>/dev/null | tr -d '[:space:]')"
|
||||
bash "$PROJECT_DIR/scripts/ci/check-not-behind.sh" "$LIVE_REF_NOW" "$SHIPPING_SHA" "$PROJECT_DIR" || exit 1
|
||||
|
||||
# Snapshot the current box build for one-command rollback (fix: in-place rsync
|
||||
# left no way back from a broken restart). Excludes backups/ so we don't copy
|
||||
# the dump store every deploy; .env + launch.sh ARE snapshotted so a rollback
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for the backwards-deploy gate (scripts/ci/check-not-behind.sh).
|
||||
#
|
||||
# The gate exists because check-deploy-ref.sh cannot catch this: it asks "are
|
||||
# you on main?", and a commit from last week's main answers yes forever. On
|
||||
# 2026-08-15 a deploy from a stale checkout overwrote a CI-verified production
|
||||
# with a tree ~25 commits old, ten minutes after the pipeline had confirmed the
|
||||
# right build was live.
|
||||
#
|
||||
# Builds throwaway repos in a temp dir; touches nothing real, hits no network.
|
||||
# Run: npm run test:not-behind-gate
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
GATE="$SCRIPT_DIR/../ci/check-not-behind.sh"
|
||||
TMP="$(mktemp -d)"
|
||||
# An empty TMP would make the fixture path absolute-from-root and still satisfy
|
||||
# a "$TMP"/* match, so the containment check below is only meaningful once this
|
||||
# holds. Same reasoning as deploy-ref-gate.sh.
|
||||
[ -n "$TMP" ] && [ -d "$TMP" ] || { echo " ✗ mktemp -d produced no usable dir" >&2; exit 1; }
|
||||
trap 'rm -rf "$TMP"' EXIT
|
||||
|
||||
PASSED=0
|
||||
fail() { echo " ✗ $1" >&2; exit 1; }
|
||||
ok() { PASSED=$((PASSED + 1)); echo " ✓ $1"; }
|
||||
|
||||
REPO="$TMP/repo"
|
||||
case "$REPO" in "$TMP"/*) ;; *) fail "refusing: fixture '$REPO' is not under '$TMP'" ;; esac
|
||||
|
||||
git init -q "$REPO"
|
||||
git -C "$REPO" config user.email t@example.com
|
||||
git -C "$REPO" config user.name t
|
||||
# The fixture must not inherit the machine's global hooks: a commit here is a
|
||||
# test fixture, not a change worth scanning, and a slow or failing global hook
|
||||
# would make this suite fail for reasons that have nothing to do with the gate.
|
||||
git -C "$REPO" config core.hooksPath /dev/null
|
||||
|
||||
commit() { # message -> echoes sha
|
||||
echo "$1" > "$REPO/file.txt"
|
||||
git -C "$REPO" add file.txt
|
||||
git -C "$REPO" commit -q -m "$1"
|
||||
git -C "$REPO" rev-parse HEAD
|
||||
}
|
||||
|
||||
OLD="$(commit old)"
|
||||
MID="$(commit mid)"
|
||||
NEW="$(commit new)"
|
||||
|
||||
# A genuinely unrelated line of history — the "sideways" case.
|
||||
git -C "$REPO" checkout -q --orphan other
|
||||
git -C "$REPO" rm -rq --cached . 2>/dev/null || true
|
||||
OTHER="$(commit other-root)"
|
||||
git -C "$REPO" checkout -q --detach "$NEW"
|
||||
|
||||
run() { bash "$GATE" "$1" "$2" "$REPO" >/dev/null 2>&1; }
|
||||
|
||||
# ── the case that actually happened ────────────────────────────────────────
|
||||
if run "$NEW" "$OLD"; then
|
||||
fail "shipping an OLDER commit over a newer live build was allowed — this is the bug"
|
||||
fi
|
||||
ok "refuses a deploy that would move production backwards"
|
||||
|
||||
# ── the normal case must stay fast ─────────────────────────────────────────
|
||||
run "$OLD" "$NEW" || fail "a fast-forward deploy was refused"
|
||||
ok "allows a fast-forward (live is an ancestor of what ships)"
|
||||
|
||||
run "$OLD" "$MID" || fail "fast-forward to an intermediate commit was refused"
|
||||
ok "allows a fast-forward that does not go all the way to the tip"
|
||||
|
||||
run "$NEW" "$NEW" || fail "redeploying the live commit was refused"
|
||||
ok "allows redeploying the exact commit already live"
|
||||
|
||||
# ── sideways is not a fast-forward ─────────────────────────────────────────
|
||||
if run "$NEW" "$OTHER"; then
|
||||
fail "shipping an unrelated history over the live build was allowed"
|
||||
fi
|
||||
ok "refuses a sideways jump to unrelated history"
|
||||
|
||||
# ── the override must work, or the gate blocks real rollbacks ──────────────
|
||||
ALLOW_ROLLBACK=1 run "$NEW" "$OLD" || fail "ALLOW_ROLLBACK=1 did not permit a deliberate rollback"
|
||||
ok "ALLOW_ROLLBACK=1 permits a deliberate rollback"
|
||||
|
||||
# ── undecidable cases must never block repairing a broken box ──────────────
|
||||
run "" "$NEW" || fail "a box with no build-ref was blocked from deploying"
|
||||
ok "allows shipping when the box reports no build-ref (nothing to compare)"
|
||||
|
||||
run "0000000000000000000000000000000000000000" "$NEW" \
|
||||
|| fail "an unknown live commit blocked the deploy"
|
||||
ok "allows shipping when the live commit is unknown to this checkout"
|
||||
|
||||
run "$OLD" "" || fail "an unresolvable shipping sha blocked the deploy"
|
||||
ok "allows shipping when the shipping commit cannot be resolved"
|
||||
|
||||
# ── the refusal has to be usable, not just correct ─────────────────────────
|
||||
out="$(bash "$GATE" "$NEW" "$OLD" "$REPO" 2>&1)"
|
||||
case "$out" in
|
||||
*BACKWARDS*) ;;
|
||||
*) fail "refusal message does not say what went wrong" ;;
|
||||
esac
|
||||
case "$out" in
|
||||
*ALLOW_ROLLBACK*) ;;
|
||||
*) fail "refusal message does not tell the operator how to override" ;;
|
||||
esac
|
||||
ok "refusal names the problem and the way out"
|
||||
|
||||
echo ""
|
||||
echo "$PASSED passed"
|
||||
Reference in new issue
Block a user