Skip to content

bug: Git Pull never detects upstream changes on working branches #195

Closed

Description

@AndriiPasternak31

Bug Description

When an agent is created from a GitHub template and Git sync is initialized, the Pull button never detects or fetches upstream changes pushed to the template repository's main branch. Clicking Pull always reports "Already up to date" even when new commits exist on main.

This affects all agents that use Trinity's working branch pattern (trinity/<agent-name>/<instance-id>).

How to Reproduce

  1. Add a GitHub template (e.g., https://github.com/user/my-agent/tree/main)
  2. Create an agent from that template
  3. Initialize Git sync — Trinity creates a working branch trinity/my-agent/a702560e
  4. Push new commits to main on the template's GitHub repo (e.g., update skills)
  5. Open agent detail → Git tab
  6. Observe: Pull button shows no commits behind, clicking Pull says "Already up to date"
  7. Refresh button also shows 0 behind

Root Cause Analysis

Three independent bugs combine to make this completely broken:

Bug 1: git fetch --dry-run never updates remote refs (status endpoint)

File: docker/base-image/agent_server/routers/git.py — get_git_status() (line ~83)

The status endpoint uses git fetch --dry-run to check for remote changes, but --dry-run does not update remote tracking refs — it only prints what would be fetched. The subsequent git rev-list --left-right --count origin/{branch}...HEAD compares against stale refs from the initial clone, so behind is always 0.

# BUG: --dry-run doesn't update refs, so ahead/behind is always stale
fetch_result = subprocess.run(
    ["git", "fetch", "--dry-run"],   # ← should be ["git", "fetch", "origin"]
    ...
)

Impact: UI never highlights the Pull button blue or shows "(N)" commit count.

Bug 2: Pull compares against the working branch, not main

File: docker/base-image/agent_server/routers/git.py — pull_from_github() (line ~587)

The pull endpoint uses current_branch (e.g., trinity/my-agent/a702560e) for all remote operations:

# BUG: Checks against origin/trinity/my-agent/xxx — nobody pushes there
behind_result = subprocess.run(
    ["git", "rev-list", "--count", f"HEAD..origin/{current_branch}"],
    ...
)
# commits_behind is always 0 → returns "Already up to date"

Even though the endpoint does a real git fetch origin first (which would update origin/main), it then checks for commits behind on origin/trinity/... — a branch that only Trinity pushes to, never the user.

All three pull strategies (clean, stash_reapply, force_reset) have the same issue — they all pull from origin/{current_branch}.

Bug 3: Template clone deletes .git directory

File: src/backend/services/template_service.py — clone_github_repo() (line ~240)

# Remove .git directory to prevent accidental pushes from container
git_dir = dest_path / ".git"
if git_dir.exists():
    shutil.rmtree(git_dir)

When an agent is created from a template, the .git directory is intentionally removed. Later, initialize_git_in_container() creates a fresh git repo (git init → git add . → git commit → git push --force). This overwrites the template repo's history with a single "Initial commit" and creates a working branch.

This is by design for the push flow, but it means the working branch has no relationship to the template's main branch commit history, making cross-branch pulls more complex.

Key Files

File Lines Role
docker/base-image/agent_server/routers/git.py ~83-96 Status endpoint — git fetch --dry-run bug
docker/base-image/agent_server/routers/git.py ~587-635 Pull clean strategy — compares against wrong branch
docker/base-image/agent_server/routers/git.py ~522-585 Pull stash_reapply — same wrong branch
docker/base-image/agent_server/routers/git.py ~494-520 Pull force_reset — same wrong branch
src/backend/services/template_service.py ~240-243 Template clone removes .git
src/backend/services/git_service.py ~334-344 Git init creates fresh repo + working branch

Proposed Fix

Fix 1: Replace --dry-run with real fetch in status endpoint

# Before (broken)
["git", "fetch", "--dry-run"]

# After (correct)
["git", "fetch", "origin"]

Fix 2: Detect working branches and pull from main

When the current branch starts with trinity/, determine the upstream source branch (main) and use it for all remote comparisons and pull operations:

pull_branch = current_branch
if current_branch.startswith("trinity/"):
    # Check if origin/main exists
    check_main = subprocess.run(
        ["git", "rev-parse", "--verify", "origin/main"], ...
    )
    if check_main.returncode == 0:
        pull_branch = "main"

Then use pull_branch instead of current_branch in:

  • git rev-list --count HEAD..origin/{pull_branch} (behind check)
  • git pull --rebase origin {pull_branch} (actual pull)
  • git reset --hard origin/{pull_branch} (force reset)

Apply the same logic in the status endpoint's ahead/behind calculation.

Fix 3 (optional, future): Preserve template git history

Instead of deleting .git and reinitializing, keep the clone's git history and create the working branch from the template's main. This would make pulls cleaner since the branches share common ancestry.

Impact

  • All template-based agents cannot receive upstream updates via Pull
  • No data loss — the Push direction works fine (pushes to the working branch)
  • Workaround: SSH into the agent container and manually run git pull origin main

Environment

  • Branch shown in UI: trinity/alpaca-vybe-live/a702560e
  • Template repo: AndriiPasternak31/alpaca-vybe-live (main branch)
  • Last commit in agent: 378ff43 ("Trinity sync: 2026-03-25 11:48:19")
  • Changes pushed to main on GitHub are invisible to the agent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions