From ad1d82f5ee121738a80fbb7f215875fd93586d85 Mon Sep 17 00:00:00 2001 From: m3taversal Date: Fri, 1 May 2026 15:42:47 +0100 Subject: [PATCH] fix(sync-mirror): tracker gate to break empty auto-create loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Diagnosis (per Ganymede pushback): the original mechanism story was wrong. Vida and Leo show 100+ PRs at 0 merge failures — luck doesn't produce that. Real cause is sync-mirror's auto-create loop, not session spawning. Verified data: - vida/research-2026-04-30: 1 commit on branch, 303 PRs in DB - reweave/2026-04-29: 1 commit on branch, 840 PRs in DB - Cron fires once/day per agent; reweave fires once/day at 01:00 UTC - Forgejo currently has 0 PRs for vida (all merged/closed); 3 distinct SHAs total across reweave's history (PRs replay same SHA repeatedly) Mechanism (confirmed in /opt/teleo-eval/logs/sync.log): 1. Pipeline merges PR → calls _delete_remote_branch on Forgejo 2. Next sync cycle: git fetch forgejo --prune drops the local Forgejo ref; refs/remotes/origin still has it (GitHub copy untouched) 3. comm sees branch GitHub-only → re-pushes to Forgejo at original SHA 4. HAS_PR check uses ?state=closed&limit=50 — closed PR for this branch scrolled out of pagination window long ago → returns "no" 5. Auto-create POST → fresh Forgejo PR (e.g. #7295 created at 21:46 for branch SHA from 04:12) 6. Pipeline merges (cherry-pick is empty no-op since content's on main; reweave union produces "already up to date" via the empty-diff guard shipped in 923454c) → _delete_remote_branch → loop Fix (per Ganymede design point #2: "right place is discovery, not _claim_next_pr"): SHA-based tracker in pipeline.db. Records (branch, sha) after every successful auto-create. Subsequent cycles see the same (branch, sha) → skip the entire push+create sequence. Cheap O(1) sqlite lookup per branch per cycle. Why SHA, not branch: research-session.sh and nightly-reweave.sh both use --force push, so a branch can legitimately get new commits over time. Tracker keys on SHA so genuine new commits produce a tracker miss → PR creation proceeds normally. No regression on legitimate branch reuse. Why pipeline.db, not flat file: shared with discover_external_prs + audit_log + the agent's own tooling; survives sync-mirror restarts; ACID-safe under the cron's 2-min cadence. CREATE IF NOT EXISTS is inline (no migration needed) because this table is private to sync-mirror — pipeline daemon doesn't read it. Validated against /tmp/pipeline-test.db copy: gate fires on known (branch, sha), misses on new SHA (correctly allows new content). Defense-in-depth — leaves existing HAS_PR check in place. Tracker is the durable signal; HAS_PR is best-effort and may catch cases the tracker hasn't seen yet (e.g. PR created out-of-band). Reweave numbers (Ganymede point #3): same shape, same fix. Both research and reweave loops killed by the same gate. Co-Authored-By: Claude Opus 4.7 (1M context) --- deploy/sync-mirror.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/deploy/sync-mirror.sh b/deploy/sync-mirror.sh index 7b3a4e2..4446c49 100755 --- a/deploy/sync-mirror.sh +++ b/deploy/sync-mirror.sh @@ -204,7 +204,39 @@ sync_github_to_forgejo_with_prs() { local FORGEJO_TOKEN FORGEJO_TOKEN=$(cat /opt/teleo-eval/secrets/forgejo-admin-token 2>/dev/null) + + # Lazy schema for sync-mirror's auto-create tracker. Records (branch, sha) + # pairs we've already auto-created PRs for, so the loop below can skip + # redundant creates after pipeline merge → _delete_remote_branch → + # GitHub-only re-discovery → re-push. Cheap CREATE IF NOT EXISTS on each + # cycle; no migration needed because this table is private to sync-mirror. + sqlite3 "$PIPELINE_DB" "CREATE TABLE IF NOT EXISTS sync_autocreate_tracker (branch TEXT NOT NULL, sha TEXT NOT NULL, pr_number INTEGER, created_at TEXT DEFAULT (datetime('now')), PRIMARY KEY (branch, sha));" 2>/dev/null || true + for branch in $GITHUB_ONLY; do + # Already-tracked gate: if we've previously auto-created a PR for + # this exact (branch, sha), skip the entire push+create sequence. + # Closes the empty-PR loop (research and reweave both observed): + # pipeline merges PR → _delete_remote_branch on Forgejo → next sync + # sees branch GitHub-only (origin still has it) → re-pushes to + # Forgejo → HAS_PR misses (Forgejo ?head= broken; closed PRs scroll + # past 50-item paginated window) → auto-creates fresh PR → pipeline + # merges (empty no-op via cherry-pick / reweave union) → repeat. + # Tracker keys on SHA, so legitimate new commits on the same branch + # produce a new SHA → tracker miss → auto-create proceeds normally. + local BRANCH_SHA TRACKED_PR + if [[ "$branch" == gh-pr-* ]]; then + BRANCH_SHA=$(git rev-parse "refs/heads/$branch" 2>/dev/null || true) + else + BRANCH_SHA=$(git rev-parse "refs/remotes/origin/$branch" 2>/dev/null || true) + fi + if [ -n "$BRANCH_SHA" ]; then + TRACKED_PR=$(sqlite3 "$PIPELINE_DB" "SELECT pr_number FROM sync_autocreate_tracker WHERE branch=$(printf "'%s'" "${branch//\'/\'\'}") AND sha=$(printf "'%s'" "$BRANCH_SHA") LIMIT 1;" 2>/dev/null || true) + if [ -n "$TRACKED_PR" ]; then + log "Skip auto-create: $branch SHA $BRANCH_SHA already tracked (PR #$TRACKED_PR)" + continue + fi + fi + log "New from GitHub: $branch -> Forgejo" # Fork PR branches live as local refs (from Step 2.1), not on origin remote if [[ "$branch" == gh-pr-* ]]; then @@ -275,6 +307,13 @@ print('no') fi log "Auto-created PR #$PR_NUM on Forgejo for $branch" + # Record (branch, sha, pr_number) so the tracker gate above can short- + # circuit the next time we see this exact (branch, sha) combination. + # INSERT OR IGNORE: idempotent if a concurrent run already inserted. + if [ -n "$BRANCH_SHA" ] && [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then + sqlite3 "$PIPELINE_DB" "INSERT OR IGNORE INTO sync_autocreate_tracker (branch, sha, pr_number) VALUES ($(printf "'%s'" "${branch//\'/\'\'}"), $(printf "'%s'" "$BRANCH_SHA"), $PR_NUM);" 2>/dev/null || true + fi + # Step 4.5: Link GitHub PR to Forgejo PR in pipeline DB if [[ "$branch" == gh-pr-* ]]; then GH_PR_NUM=$(echo "$branch" | sed 's|gh-pr-\([0-9]*\)/.*|\1|')