Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

14.2 KB
Raw Blame
#!/usr/bin/env bash
# Cut a desktop release: work out the next version, tag it, push the tag. CI does everything else —
# ../.github/workflows/release-desktop.yml builds macOS, Linux and Windows, publishes to GCS, and creates the
# GitHub Release. The tag IS the version; CI never bumps on its own. Tags are "vX.Y.Z_desktop" — the
# "_desktop" suffix is stripped by release-desktop.yml before anything treats it as a version; it
# exists only so this tag never also triggers the CLI's "vX.Y.Z_cli" or the backend's "vX.Y.Z_backend"
# release, now that all three live in one repo.
#
# Usage (from the repo root, `make release-desktop ARGS=...` runs the same thing):
#   bash scripts/release-desktop.sh                    # bump the patch and release
#   bash scripts/release-desktop.sh --dry-run          # print everything, tag nothing, push nothing
#   bash scripts/release-desktop.sh --minor            # bump the MINOR version, per the usual semver
#                                                      # convention
#   bash scripts/release-desktop.sh 1.1.0              # release an explicit version
#   bash scripts/release-desktop.sh --notes-file f.md  # hand-written release notes instead of the
#                                                      # generated commit list
#   bash scripts/release-desktop.sh --wait             # finish when this tag is live and verified
#   bash scripts/release-desktop.sh --prepare          # package the pushed PR head during final checks; no publication
#   bash scripts/release-desktop.sh --allow-older      # a rollback: older code under a higher version
#
# THE NEXT VERSION COMES FROM TWO SOURCES, AND BOTH MATTER. Git tags alone are not enough: this repo
# had one tag (v1.0.52) while the live manifest was already serving 1.0.61, because
# scripts/upload-desktop.sh bumps from the manifest and tags nothing. Bumping from tags there would
# have produced 1.0.53 — a version LOWER than what users already run, which every app refuses to
# install (semverGt in lib/update/desktop_updater.dart) while the release still looks successful. So
# the current version is max(highest git tag, highest version in the manifest), across ALL of the
# manifest's desktop-* keys, not just the macOS one.
#
# Requires: git, curl, python3, push access to origin; gh for --wait. No GCS credentials — the manifest is read
# over public HTTPS, exactly as scripts/upload-desktop.sh reads it.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

# --- config (all overridable via env) ---
GCS_BUCKET="${GCS_BUCKET:-s3-autonomous-upgrade-3}"
GCS_PUBLIC_BASE_URL="${GCS_PUBLIC_BASE_URL:-https://storage.googleapis.com/${GCS_BUCKET}}"
METADATA_PATH="${METADATA_PATH:-harness/desktop/metadata.json}"
META_URL="${META_URL:-${GCS_PUBLIC_BASE_URL%/}/${METADATA_PATH#/}}"
BRANCH="${BRANCH:-}"

DRY_RUN=0
WAIT=0
PREPARE=0
DO_MINOR=0
ALLOW_NO_MANIFEST=0
ALLOW_OLDER=0
NEW_VER=""
NOTES_FILE=""
URGENT_SET=0
URGENT=""

want_notes_file=0
want_urgent=0
for arg in "$@"; do
  if [ "$want_notes_file" -eq 1 ]; then NOTES_FILE="$arg"; want_notes_file=0; continue; fi
  if [ "$want_urgent" -eq 1 ]; then URGENT="$arg"; want_urgent=0; continue; fi
  case "$arg" in
    --urgent) URGENT_SET=1; want_urgent=1 ;;
    --dry-run) DRY_RUN=1 ;;
    --wait) WAIT=1 ;;
    --prepare) PREPARE=1 ;;
    --minor) DO_MINOR=1 ;;
    --allow-no-manifest) ALLOW_NO_MANIFEST=1 ;;
    --allow-older) ALLOW_OLDER=1 ;;
    --notes-file) want_notes_file=1 ;;
    --notes-file=*) NOTES_FILE="${arg#*=}" ;;
    -h|--help) sed -n '2,26p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;;
    -*) echo "ERROR unknown flag: $arg" >&2; exit 1 ;;
    *) NEW_VER="$arg" ;;
  esac
done
if [ "$PREPARE" -eq 1 ]; then
  [ "$WAIT" -eq 0 ] || { echo 'ERROR --prepare starts a candidate; use --wait when releasing after merge' >&2; exit 1; }
  BRANCH="${BRANCH:-$(git branch --show-current)}"
  [ -n "$BRANCH" ] || { echo 'ERROR --prepare needs a pushed branch' >&2; exit 1; }
else
  BRANCH="${BRANCH:-main}"
fi
if [ "$WAIT" -eq 1 ] || { [ "$PREPARE" -eq 1 ] && [ "$DRY_RUN" -eq 0 ]; }; then
  command -v gh >/dev/null 2>&1 || { echo 'ERROR --wait and --prepare require gh' >&2; exit 1; }
fi
[ "$want_notes_file" -eq 0 ] || { echo "ERROR --notes-file needs a path" >&2; exit 1; }

# --- the release window: refused with RELEASE_WINDOW outside 09:00-18:00 Asia/Ho_Chi_Minh, naming the next allowed
# time (scripts/release-window.py). A P0 passes --urgent "<reason>"; the reason goes on the tag.
# A --prepare candidate publishes nothing, so it is not a release and runs at any hour.
source "$ROOT/../scripts/release-window.sh"
[ "$PREPARE" -eq 1 ] || release_window_guard desktop

# Same rules as next_desktop_version()/bump_minor_version() in scripts/upload-desktop.sh:55-86, which
# is where this convention is defined. Note the rollover: .99 goes to the next MINOR at .1, not .0.
next_patch_version() {
  local current="$1" major minor patch
  if [[ ! "$current" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
    echo "ERROR version '$current' must look like X.Y.Z" >&2
    return 1
  fi
  major=$((10#${BASH_REMATCH[1]}))
  minor=$((10#${BASH_REMATCH[2]}))
  patch=$((10#${BASH_REMATCH[3]}))
  if (( patch >= 99 )); then
    printf '%d.%d.1\n' "$major" "$((minor + 1))"
  else
    printf '%d.%d.%d\n' "$major" "$minor" "$((patch + 1))"
  fi
}

next_minor_version() {
  local current="$1" major minor
  if [[ ! "$current" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
    echo "ERROR version '$current' must look like X.Y.Z" >&2
    return 1
  fi
  major=$((10#${BASH_REMATCH[1]}))
  minor=$((10#${BASH_REMATCH[2]}))
  printf '%d.%d.1\n' "$major" "$((minor + 1))"
}

# Strictly greater, comparing X.Y.Z numerically — the same test the app applies before it will install
# anything (semverGt, lib/update/desktop_updater.dart:73).
version_gt() {
  [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -1)" = "$1" ]
}

# --- preflight: tooling ---
for tool in git curl python3; do
  command -v "$tool" >/dev/null 2>&1 || { echo "ERROR $tool not found — it is needed to cut a release" >&2; exit 1; }
done

# --- preflight: the tag must capture exactly what was tested, and CI must be able to fetch it ---
git rev-parse --git-dir >/dev/null 2>&1 || { echo "ERROR not a git repository: $ROOT" >&2; exit 1; }
git fetch --tags --quiet origin

if [ -n "$(git status --porcelain)" ]; then
  echo "ERROR working tree is dirty — a release tag must capture the tested source exactly:" >&2
  git status --short | sed 's/^/        /' >&2
  exit 1
fi

HEAD_SHA="$(git rev-parse HEAD)"
if ! git merge-base --is-ancestor "$HEAD_SHA" "origin/$BRANCH" 2>/dev/null; then
  echo "ERROR HEAD is not on origin/$BRANCH — push the commit first, or CI will build a commit nobody else has." >&2
  exit 1
fi
if [ "$PREPARE" -eq 1 ] && [ "$HEAD_SHA" != "$(git rev-parse "origin/$BRANCH")" ]; then
  echo 'ERROR --prepare must run at the pushed branch tip so CI packages exactly this source' >&2
  exit 1
fi

# --- current version, source 1: the highest release tag in git ---
# The brace group keeps `set -e` from killing the script when grep legitimately matches nothing.
# Only "vX.Y.Z_desktop" tags count — the CLI's and backend's tags share this repo. Strip "v" and
# "_desktop" so TAG_VER is a bare X.Y.Z for the comparisons below.
TAG_VER="$(git tag -l 'v*_desktop' \
  | { grep -E '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)_desktop$' || true; } \
  | sed -E 's/^v//; s/_desktop$//' \
  | sort -V | tail -1)"
LAST_TAG=""
[ -z "$TAG_VER" ] || LAST_TAG="v${TAG_VER}_desktop"
TAG_VER="${TAG_VER:-0.0.0}"

# --- current version, source 2: the highest version actually being served ---
# Every desktop-* key, because each publisher only ever wrote its own: a linux-only publish would be
# invisible if we read desktop-macos alone, and stepping over it is how a downgrade gets released.
GCS_READ="$(curl -fsSL --connect-timeout 10 --max-time 30 "$META_URL" 2>/dev/null | python3 -c '
import json, re, sys
try:
    data = json.load(sys.stdin)
except Exception:
    sys.exit(0)
if not isinstance(data, dict):
    sys.exit(0)
best, holder = None, ""
for key, entry in data.items():
    if not key.startswith("desktop-") or not isinstance(entry, dict):
        continue
    version = entry.get("version")
    if not isinstance(version, str):
        continue
    m = re.match(r"^(\d+)\.(\d+)\.(\d+)$", version.strip())
    if not m:
        continue
    parsed = tuple(int(g) for g in m.groups())
    if best is None or parsed > best:
        best, holder = parsed, key
if best is not None:
    print("%d.%d.%d %s" % (best + (holder,)))
' 2>/dev/null || true)"

GCS_VER="${GCS_READ%% *}"
GCS_KEY="${GCS_READ#* }"
if [ -z "$GCS_VER" ]; then
  if [ "$ALLOW_NO_MANIFEST" -eq 1 ]; then
    echo ">> WARNING could not read $META_URL — continuing on git tags alone (--allow-no-manifest)" >&2
    GCS_VER="0.0.0"
    GCS_KEY="(unavailable)"
  else
    echo "ERROR could not read the live manifest at $META_URL" >&2
    echo "      It is the only record of what users are actually running, and releasing without it" >&2
    echo "      risks publishing a version LOWER than the one already being served." >&2
    echo "      Re-run with --allow-no-manifest only if the bucket is genuinely down." >&2
    exit 1
  fi
fi

# --- preflight: this release must contain the one before it ---
# HEAD on origin/$BRANCH is not enough: a checkout nobody updated is on it too, and a tag cut there ships
# older code under a newer version, which every app installs. v1.2.36_desktop was cut that way on
# October 1, 2026, twenty commits behind v1.2.35_desktop, and v0.3.82_cli on October 12.
# Checked against the highest tag and against the tag of the version being served: they differ when a
# tag's release never published. A rollback is the one release that means to do this. Not for --prepare,
# which publishes nothing: its candidate is a PR's head, and the release itself is cut after the merge.
contains_release() {
  local tag="$1" sha
  git rev-parse -q --verify "refs/tags/$tag" >/dev/null || return 0
  sha="$(git rev-list -n1 "$tag")"
  git merge-base --is-ancestor "$sha" "$HEAD_SHA" 2>/dev/null && return 0
  if [ "$ALLOW_OLDER" -eq 1 ]; then
    echo ">> WARNING HEAD (${HEAD_SHA:0:12}) does not contain $tag (${sha:0:12}) — releasing older code (--allow-older)" >&2
    return 0
  fi
  echo "ERROR HEAD (${HEAD_SHA:0:12}) does not contain $tag (${sha:0:12})." >&2
  echo "      This release would ship older code under a newer version, taking back" >&2
  echo "      $(git rev-list --count "$HEAD_SHA..$sha") commit(s) that $tag already shipped." >&2
  echo "      Update this checkout first: git fetch origin && git checkout --detach origin/$BRANCH" >&2
  echo "      Re-run with --allow-older only to roll back on purpose." >&2
  exit 1
}
if [ "$PREPARE" -eq 0 ]; then
  [ -z "$LAST_TAG" ] || contains_release "$LAST_TAG"
  contains_release "v${GCS_VER}_desktop"
fi

# --- the next version ---
CUR="$TAG_VER"
version_gt "$GCS_VER" "$CUR" && CUR="$GCS_VER"

if [ -n "$NEW_VER" ]; then
  VER="$NEW_VER"
elif [ "$DO_MINOR" -eq 1 ]; then
  VER="$(next_minor_version "$CUR")"
else
  VER="$(next_patch_version "$CUR")"
fi

if [[ ! "$VER" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "ERROR version '$VER' must look like X.Y.Z — release.yml rejects anything else" >&2
  exit 1
fi

TAG="v${VER}_desktop"

# The last gate against a release nobody can install.
if ! version_gt "$VER" "$GCS_VER"; then
  echo "ERROR $VER is not higher than $GCS_VER, which is already published ($GCS_KEY)." >&2
  echo "      Every running app would refuse it and the release would still look successful." >&2
  exit 1
fi

if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
  echo "ERROR tag $TAG already exists. Re-tagging a released version breaks the tag<->build mapping." >&2
  exit 1
fi

# --- release notes: the tag message IS the GitHub Release body ---
# release.yml's github-release job hard-fails on a lightweight tag or an empty message — and it runs
# AFTER the manifest is already live, so a missing message means a half-done release: users are
# downloading a build that has no Release page. Refuse to tag without notes rather than find out then.
NOTES="$(mktemp)"
trap 'rm -f "${NOTES:-}"' EXIT
if [ -n "$NOTES_FILE" ]; then
  [ -f "$NOTES_FILE" ] || { echo "ERROR notes file not found: $NOTES_FILE" >&2; exit 1; }
  cat "$NOTES_FILE" > "$NOTES"
else
  {
    printf 'Harness Desktop %s\n\n' "$VER"
    if [ -n "$LAST_TAG" ]; then
      git log --no-merges --pretty='- %s' "${LAST_TAG}..HEAD"
    else
      git log --no-merges --pretty='- %s' -20
    fi
  } > "$NOTES"
fi
[ -s "$NOTES" ] || { echo "ERROR release notes are empty — CI would fail after publishing" >&2; exit 1; }

# --- summary (shared by dry runs and real ones) ---
echo ""
echo "  last git tag       ${LAST_TAG:-(none)} ($TAG_VER)"
echo "  live on GCS        $GCS_VER (highest of the desktop-* keys: $GCS_KEY)"
if version_gt "$GCS_VER" "$TAG_VER"; then
  echo "  NOTE               the manifest is ahead of git — versions were published without a tag."
  echo "                     Bumping from tags alone here would publish a downgrade."
fi
echo "  releasing          $VER   (tag $TAG on ${HEAD_SHA:0:12})"
if [ "$DO_MINOR" -eq 1 ] || [ "${VER%%.*}" != "${CUR%%.*}" ] || [ "$(echo "$VER" | cut -d. -f2)" != "$(echo "$CUR" | cut -d. -f2)" ]; then
  echo "  NOTE               this is a major/minor version bump."
fi
echo ""
echo "  release notes:"
sed 's/^/      /' "$NOTES"
echo ""

if [ "$DRY_RUN" -eq 1 ]; then
  echo "  dry run — nothing tagged, nothing pushed."
  exit 0
fi

if [ "$PREPARE" -eq 1 ]; then
  gh workflow run release-desktop.yml --ref "$BRANCH" -f version="$VER" -f prepare_only=true
  echo "  Candidate $VER started on $HEAD_SHA. No tag or product manifest was published."
  echo "  Continue validation/review, merge, then release $VER normally; matching packages are reused automatically."
  exit 0
fi

# --- tag and push ---
[ -z "$URGENT_TAG_LINE" ] || printf '\n%s\n' "$URGENT_TAG_LINE" >> "$NOTES"
git tag -a "$TAG" --cleanup=verbatim -F "$NOTES"
git push origin "$TAG"

echo ""
echo "  pushed $TAG → CI builds macOS, Linux and Windows, publishes to GCS, and cuts the GitHub Release."
echo "  watch (from repo root): python3 scripts/watch-desktop-release.py $TAG --sha $HEAD_SHA"
echo ""
if [ "$WAIT" -eq 1 ]; then
  python3 "$ROOT/../scripts/watch-desktop-release.py" "$TAG" --sha "$HEAD_SHA"
fi