Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AIScientists Dev WorldSeed Git Lock For Parallel Experiments

From Leeroopedia
Knowledge Sources
Domains Distributed_Computing, Concurrency
Last Updated 2026-10-26 19:00 GMT

Overview

Serialize git checkout/commit operations with an asyncio lock when multiple experiments run in parallel on a shared worktree, while leaving the long training phase unlocked for GPU parallelism.

Description

The `AutoresearchWorker` runs multiple experiments concurrently — one per GPU. Each experiment needs to create a git branch, write `train_gpt.py`, and commit on the same main worktree. Without a lock, concurrent git operations race: `git reset --hard`, `checkout`, and `commit` on the same directory crash with `git_commit_failed` on tight bursts.

The solution is an `asyncio.Lock` (`_git_lock`) held only for the ~1s git phase. Training (~210s) runs after the lock is released, so other GPUs can train in parallel. This gives the best of both worlds: git safety + GPU parallelism.

Usage

Use this pattern whenever multiple async tasks need to perform read-modify-write operations on a shared filesystem or git worktree, but the expensive part (training, API call, etc.) is independent and can run unlocked.

The Insight (Rule of Thumb)

  • Action: Wrap only the git checkout+commit phase in `async with self._git_lock:`. Run training outside the lock.
  • Value: Git phase: ~1s (locked). Training: ~210s (unlocked). Lock is per-worker, not per-experiment.
  • Trade-off: Experiments queue briefly on the git phase. With 8 concurrent experiments, worst-case git wait is ~7s — negligible vs. 210s training.
  • Compatibility: Requires asyncio cooperative scheduling. The lock is held across `await` points in `_git()`, which is safe because asyncio is single-threaded.

Reasoning

Git operations on a shared worktree are inherently non-reentrant — `checkout -b` changes `HEAD`, and a concurrent `reset --hard` can destroy the branch you just created. Observed crashes in production: `git_commit_failed` on tight experiment bursts.

The lock scope is deliberately minimal. Locking the entire experiment (git + training) would serialize all 8 GPUs to one-at-a-time, wasting 7/8 of the hardware. By locking only the ~1s git phase and releasing before the ~210s training call, we get full GPU utilization with git safety.

The asyncio cooperative model makes this safe: between `lock.acquire()` and `lock.release()`, no other coroutine can touch the worktree because there are no `await` points that yield to other experiments' git operations.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment