Files
josh-sync/docs/adr/005-git-trailer-loop-prevention.md

2.6 KiB

ADR-005: Git Trailer for Loop Prevention

Status: Accepted Date: 2026-01

Context

Bidirectional sync creates an infinite loop risk: forward sync pushes commit A to the subrepo, reverse sync sees commit A as "new" and creates a PR back to the monorepo, forward sync sees the merged PR as "new" and pushes again, etc.

Alternatives considered

  1. SHA tracking only: Compare SHAs to skip already-synced content. Breaks when josh-proxy rewrites SHAs (which it always does for filtered views). The monorepo commit SHA and the filtered/subrepo commit SHA are never the same.

  2. Commit message prefix: Add [sync] to bot commit messages. Fragile — humans might use the same prefix. Requires string matching on message content.

  3. Git trailer: A structured key-value pair in the commit message body (after a blank line), following the git interpret-trailers convention. Format: Key: value. Machine-parseable, unlikely to be used by humans, and supported by git log --grep.

Decision

All bot commits include a git trailer with a configurable key (default: Josh-Sync-Origin). Reverse sync filters out commits containing this trailer so bot-generated forward-sync commits are not proposed back to the monorepo.

Format

Sync from monorepo 2026-02-12T10:30:00Z

Josh-Sync-Origin: forward/main/2026-02-12T10:30:00Z

The trailer value encodes: direction, branch, and timestamp. This aids debugging but is not parsed by the loop filter — only the trailer key presence matters.

Filtering

  • Reverse sync: git log --invert-grep --grep="^${BOT_TRAILER}:" excludes all commits with the trailer
  • Forward sync: the composite action still runs even when HEAD has the trailer; forward sync uses tree comparison, sync state, and merge checks to decide whether to push

Configuration

The trailer key is set in .josh-sync.yml under bot.trailer. This allows multiple josh-sync instances (with different bots) to operate on the same repos without interfering.

Consequences

Positive:

  • Reliable loop prevention — trailer is part of the immutable commit object
  • Configurable key avoids conflicts between multiple sync bots
  • Human-readable — git log shows the trailer in commit messages
  • Repositories can act as middle hops, receiving a sync commit from one relationship and forwarding relevant changes to another

Negative:

  • Commits with manually-added trailers matching the key would be incorrectly filtered
  • Trailer must be in the commit body (after blank line), not the subject line
  • Squash-and-merge on PRs may lose the trailer if the platform doesn't preserve commit message body