Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

ci: add a scheduled push watch for foreign or oversized pushes (#5498)

Francesco Bonacci77fd07f96e93
diff --git a/.github/push-watch-orphan-branches.txt b/.github/push-watch-orphan-branches.txt
new file mode 100644
index 0000000000..85160ad2c9
--- /dev/null
+++ b/.github/push-watch-orphan-branches.txt
@@ -0,0 +1,5 @@
+# Branches that are expected to have no common ancestor with main.
+# Maintained by automation (state and ledger branches); the push watch
+# (.github/scripts/push_watch.py) skips the foreign-history check for them.
+hyprland-plugin-compat
+image-doctor-ledger
diff --git a/.github/scripts/push_watch.py b/.github/scripts/push_watch.py
new file mode 100644
index 0000000000..6316184812
--- /dev/null
+++ b/.github/scripts/push_watch.py
@@ -0,0 +1,166 @@
+#!/usr/bin/env python3
+"""Flag suspicious pushes to this repository from its Events feed.
+
+Why a scheduled job: a push workflow runs the workflow files of the pushed
+commit. A branch with someone else's history brings its own workflows and never
+runs ours, so the check has to run from the default branch on a schedule.
+
+What it flags (non-default branches only):
+ * foreign history: the pushed head has no common ancestor with main. The
+ 13 Oct 2026 push of trycua/cloud history to this repo looked like this;
+ * a very large push: more than --max-commits commits in one push, unless the
+ pusher is a bot (release automation).
+
+A finding fails the run (annotations + job summary) so the failure reaches the
+usual notifications. It prints refs, SHAs and pushers, never commit content.
+
+Usage:
+ push_watch.py --repo trycua/cua --since-last-run push-watch.yml
+ push_watch.py --repo trycua/cua --minutes 180 # local run, needs gh auth
+"""
+
+from __future__ import annotations
+
+import argparse
+import datetime as dt
+import json
+import os
+import re
+import subprocess
+import sys
+
+ZERO = "0" * 40
+DEFAULT_MINUTES = 30
+OVERLAP_MINUTES = 3
+MAX_LOOKBACK_MINUTES = 6 * 60
+
+
+def gh_api(path: str) -> tuple[int, object]:
+ """GET through the gh CLI. Returns (http status, parsed body or None)."""
+ proc = subprocess.run(["gh", "api", path], capture_output=True, text=True)
+ if proc.returncode == 0:
+ return 200, json.loads(proc.stdout or "null")
+ match = re.search(r"HTTP (\d{3})", proc.stderr)
+ return (int(match.group(1)) if match else 0), None
+
+
+def is_bot(login: str) -> bool:
+ return login.endswith("[bot]")
+
+
+def classify(event: dict, repo: str, max_commits: int, compare, orphans: frozenset = frozenset()) -> list[str]:
+ """Return the reasons a PushEvent is suspicious (empty when it is fine).
+
+ `compare(base, head)` returns (status, body) like the compare API. `orphans`
+ names branches that are expected to have no common ancestor with main
+ (bot-maintained state branches).
+ """
+ payload = event.get("payload", {})
+ ref = payload.get("ref", "")
+ head = payload.get("head", "")
+ before = payload.get("before", ZERO)
+ if not ref.startswith("refs/heads/") or ref == "refs/heads/main" or not head:
+ return []
+
+ actor = event.get("actor", {}).get("login", "unknown")
+ status, vs_main = compare("main", head)
+ if status != 200:
+ if ref.removeprefix("refs/heads/") in orphans:
+ return []
+ return [
+ f"{ref} (head {head[:10]}) pushed by {actor} has no common ancestor with "
+ f"main, or its commits cannot be read (HTTP {status}). Treat it as foreign history."
+ ]
+
+ commits = vs_main.get("ahead_by", 0)
+ if before != ZERO:
+ inc_status, incremental = compare(before, head)
+ if inc_status == 200:
+ commits = incremental.get("total_commits", commits)
+
+ if commits > max_commits and not is_bot(actor):
+ return [
+ f"{ref} (head {head[:10]}) pushed by {actor} carries {commits} commits in one "
+ f"push (limit {max_commits})."
+ ]
+ return []
+
+
+def parse_time(value: str) -> dt.datetime:
+ return dt.datetime.fromisoformat(value.replace("Z", "+00:00"))
+
+
+def window_start(args, now: dt.datetime) -> dt.datetime:
+ minutes = args.minutes
+ if minutes is None and args.since_last_run:
+ status, runs = gh_api(
+ f"repos/{args.repo}/actions/workflows/{args.since_last_run}/runs?per_page=5"
+ )
+ current = os.environ.get("GITHUB_RUN_ID", "")
+ if status == 200:
+ for run in runs.get("workflow_runs", []):
+ if str(run["id"]) != current:
+ gap = (now - parse_time(run["created_at"])).total_seconds() / 60
+ minutes = min(gap + OVERLAP_MINUTES, MAX_LOOKBACK_MINUTES)
+ break
+ return now - dt.timedelta(minutes=minutes or DEFAULT_MINUTES)
+
+
+def recent_push_events(repo: str, since: dt.datetime) -> list[dict]:
+ events: list[dict] = []
+ for page in (1, 2, 3):
+ status, body = gh_api(f"repos/{repo}/events?per_page=100&page={page}")
+ if status != 200 or not body:
+ break
+ events.extend(
+ e for e in body if e.get("type") == "PushEvent" and parse_time(e["created_at"]) >= since
+ )
+ return events
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--repo", required=True)
+ parser.add_argument("--max-commits", type=int, default=300)
+ parser.add_argument("--minutes", type=float, help="look back this many minutes")
+ parser.add_argument("--since-last-run", metavar="WORKFLOW_FILE")
+ parser.add_argument(
+ "--orphan-branches",
+ metavar="FILE",
+ help="file with one branch name per line that may have no common ancestor with main",
+ )
+ args = parser.parse_args()
+
+ now = dt.datetime.now(dt.timezone.utc)
+ since = window_start(args, now)
+
+ def compare(base: str, head: str):
+ return gh_api(f"repos/{args.repo}/compare/{base}...{head}")
+
+ orphans = frozenset()
+ if args.orphan_branches:
+ with open(args.orphan_branches, encoding="utf-8") as fh:
+ orphans = frozenset(
+ line.strip() for line in fh if line.strip() and not line.startswith("#")
+ )
+
+ events = recent_push_events(args.repo, since)
+ findings = [
+ reason
+ for event in events
+ for reason in classify(event, args.repo, args.max_commits, compare, orphans)
+ ]
+
+ print(f"push-watch: {len(events)} push events since {since:%Y-%m-%d %H:%M}Z, {len(findings)} suspicious")
+ summary = os.environ.get("GITHUB_STEP_SUMMARY")
+ if summary:
+ with open(summary, "a", encoding="utf-8") as fh:
+ fh.write(f"### Push watch\n\n{len(events)} push events checked, {len(findings)} suspicious.\n\n")
+ fh.writelines(f"- {reason}\n" for reason in findings)
+ for reason in findings:
+ print(f"::error title=Suspicious push::{reason}")
+ return 1 if findings else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/.github/scripts/tests/test_push_watch.py b/.github/scripts/tests/test_push_watch.py
new file mode 100644
index 0000000000..759fb6b26f
--- /dev/null
+++ b/.github/scripts/tests/test_push_watch.py
@@ -0,0 +1,76 @@
+"""Behavior of push_watch.classify with a fake compare API."""
+
+from __future__ import annotations
+
+import importlib.util
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[3]
+spec = importlib.util.spec_from_file_location("push_watch", ROOT / ".github/scripts/push_watch.py")
+push_watch = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(push_watch)
+
+HEAD = "a" * 40
+BEFORE = "b" * 40
+ZERO = "0" * 40
+
+
+def event(ref="refs/heads/feat/x", actor="f-trycua", before=ZERO, head=HEAD):
+ return {"actor": {"login": actor}, "payload": {"ref": ref, "before": before, "head": head}}
+
+
+def fake_compare(results):
+ def compare(base, head):
+ return results[(base, head)]
+
+ return compare
+
+
+def test_normal_branch_push_is_fine():
+ compare = fake_compare({("main", HEAD): (200, {"ahead_by": 3})})
+ assert push_watch.classify(event(), "trycua/cua", 300, compare) == []
+
+
+def test_foreign_history_is_flagged():
+ compare = fake_compare({("main", HEAD): (404, None)})
+ reasons = push_watch.classify(event(), "trycua/cua", 300, compare)
+ assert len(reasons) == 1
+ assert "no common ancestor" in reasons[0]
+ assert "f-trycua" in reasons[0]
+
+
+def test_large_new_branch_is_flagged():
+ compare = fake_compare({("main", HEAD): (200, {"ahead_by": 10555})})
+ reasons = push_watch.classify(event(), "trycua/cua", 300, compare)
+ assert len(reasons) == 1 and "10555 commits" in reasons[0]
+
+
+def test_large_update_uses_the_incremental_count():
+ compare = fake_compare(
+ {
+ ("main", HEAD): (200, {"ahead_by": 900}),
+ (BEFORE, HEAD): (200, {"total_commits": 2}),
+ }
+ )
+ assert push_watch.classify(event(before=BEFORE), "trycua/cua", 300, compare) == []
+
+
+def test_bots_may_push_many_commits_but_not_foreign_history():
+ big = fake_compare({("main", HEAD): (200, {"ahead_by": 900})})
+ assert push_watch.classify(event(actor="cua-release-bot[bot]"), "trycua/cua", 300, big) == []
+ foreign = fake_compare({("main", HEAD): (404, None)})
+ assert push_watch.classify(event(actor="cua-release-bot[bot]"), "trycua/cua", 300, foreign)
+
+
+def test_main_tags_and_deletes_are_ignored():
+ never = fake_compare({})
+ assert push_watch.classify(event(ref="refs/heads/main"), "trycua/cua", 300, never) == []
+ assert push_watch.classify(event(ref="refs/tags/v1"), "trycua/cua", 300, never) == []
+ assert push_watch.classify(event(head=""), "trycua/cua", 300, never) == []
+
+
+def test_allowlisted_orphan_branch_is_not_flagged():
+ foreign = fake_compare({("main", HEAD): (404, None)})
+ ev = event(ref="refs/heads/image-doctor-ledger", actor="cua-image-doctor")
+ assert push_watch.classify(ev, "trycua/cua", 300, foreign, frozenset({"image-doctor-ledger"})) == []
+ assert push_watch.classify(ev, "trycua/cua", 300, foreign)
diff --git a/.github/workflows/security-push-watch.yml b/.github/workflows/security-push-watch.yml
new file mode 100644
index 0000000000..0b6432d095
--- /dev/null
+++ b/.github/workflows/security-push-watch.yml
@@ -0,0 +1,40 @@
+name: "Security: push watch"
+
+# Flags pushes that look like someone else's history (no common ancestor with
+# main) or an unusually large push, from the public Events feed. It runs on a
+# schedule from the default branch on purpose: a push workflow runs the workflow
+# files of the pushed commit, so a branch carrying another repository's history
+# would never run this check. A finding fails the run, which notifies the
+# workflow's owner. See .github/scripts/push_watch.py.
+
+on:
+ schedule:
+ - cron: "*/15 * * * *"
+ workflow_dispatch:
+
+permissions:
+ contents: read
+ actions: read
+
+concurrency:
+ group: security-push-watch
+ cancel-in-progress: false
+
+jobs:
+ watch:
+ name: Check recent pushes
+ if: github.repository == 'trycua/cua'
+ runs-on: ubuntu-latest
+ timeout-minutes: 10
+ steps:
+ - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
+ with:
+ sparse-checkout: .github
+ - name: Check pushes since the last run
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: >
+ python3 .github/scripts/push_watch.py
+ --repo "$GITHUB_REPOSITORY"
+ --since-last-run security-push-watch.yml
+ --orphan-branches .github/push-watch-orphan-branches.txt