DEV Community

Robin
Robin

Posted on

Merge Concurrent Agent Patches by Base Commit and Hunk Ownership

Two coding agents start from commit A. One finishes quickly and merges as B. The second finishes later with a patch that was valid against A.

Applying the second patch because it arrived last makes completion timing part of repository correctness. The merge protocol needs explicit inputs.

Patch envelope

{
  "patch_id": "p-204",
  "base_commit": "4f2c1d...",
  "paths": ["src/parser.ts", "test/parser.test.ts"],
  "owned_hunks": [
    {"path":"src/parser.ts","start":88,"end":112,"symbol":"parseHeader"}
  ],
  "required_checks": ["npm test -- parser"],
  "diff_sha256": "..."
}
Enter fullscreen mode Exit fullscreen mode

The base commit and diff digest bind review to exact bytes. Path and hunk ownership are conflict hints, not proof that independent patches compose semantically.

Deterministic decision flow

receive patch
  -> verify digest and base exists
  -> compare with changes since base
  -> reject overlapping ownership
  -> attempt rebase in isolated worktree
  -> run union of required checks
  -> emit candidate commit or conflict record
Enter fullscreen mode Exit fullscreen mode

Do not choose a winner by task start time, completion time, agent identity, or retry count. Those fields are operational metadata, not merge semantics.

Three conflict classes

Conflict Example Action
textual same lines changed reject to resolution queue
structural function moved while edited require semantic review
behavioral separate files alter one invariant tests or owner rule must detect

Git catches much of the first class. It cannot reliably infer the third. Repository-owned tests and critical-path ownership remain part of the protocol.

Reproducible fixture

Create a disposable repository with commit A and three branches:

  1. patch P1 changes parseHeader;
  2. patch P2 changes the same hunk from A;
  3. patch P3 changes an independent formatter and test.

Merge P1. Feed P2 and P3 to the coordinator in both arrival orders. Assert:

  • P2 always produces the same conflict record;
  • P3 rebases to the same tree content;
  • required checks run after rebase, not only on A;
  • rerunning an accepted patch ID is idempotent;
  • changing diff bytes without changing the envelope digest is rejected.

Record Git version, base and result commits, command output, and tree hashes. A deterministic protocol should let another machine reproduce the same decision from the same repository state.

Limitations

Hunk ownership becomes stale when code moves. Symbol extraction differs by language. Passing tests cannot prove two patches preserve every product invariant. These are reasons to expose conflict evidence and keep a human resolution path—not reasons to let “last finisher wins” become the hidden policy.

Top comments (0)