Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(git): polled reads of changed files no longer rewrite the index (#649)

Ruben Lütkefae11cc9db43
diff --git a/jarvis/agentic_ide/git_changes.py b/jarvis/agentic_ide/git_changes.py
index 16e1e6032a..d7b7a7d52d 100644
--- a/jarvis/agentic_ide/git_changes.py
+++ b/jarvis/agentic_ide/git_changes.py
@@ -18,6 +18,7 @@ that has git at all, and every call is bounded:
from __future__ import annotations
+import os
import re
import subprocess
from dataclasses import dataclass, field
@@ -28,6 +29,10 @@ from loguru import logger
from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
_GIT_TIMEOUT_S = 8.0
+#: Put before a `git diff` that prints counts or a patch: it then leaves the index
+#: alone instead of refreshing it (which takes the index lock). Not for --raw,
+#: --name-status or --name-only, where it would list files that were only touched.
+NO_INDEX_REFRESH = ["-c", "diff.autoRefreshIndex=false"]
#: Most changed files reported in one answer.
MAX_CHANGED_FILES = 500
#: Most diff lines returned for one file; the rest is summarised by ``truncated``.
@@ -92,10 +97,17 @@ def _git(
``stdin`` is fed to git when given; otherwise git reads nothing, so a
command that would wait for input can never hang the caller.
"""
+ env = dict(os.environ)
+ # These reads are polled while people and agents run their own git in
+ # the same checkout. A status that refreshes the index takes its lock:
+ # their command then fails, and a read killed at the timeout leaves the
+ # lock behind for good.
+ env["GIT_OPTIONAL_LOCKS"] = "0"
try:
return subprocess.run(
["git", "-c", "core.quotePath=false", *args],
cwd=str(cwd),
+ env=env,
input=stdin if stdin is not None else "",
capture_output=True,
text=True,
@@ -194,7 +206,7 @@ def workspace_changes(folder: str | Path, only: set[str] | None = None) -> Works
)
counts: dict[str, tuple[int | None, int | None]] = {}
- numstat_args = ["diff", "--numstat", "-z", "--no-renames"]
+ numstat_args = [*NO_INDEX_REFRESH, "diff", "--numstat", "-z", "--no-renames"]
if _has_head(root):
numstat_args.append("HEAD")
numstat = _git([*numstat_args, "--", "."], root)
@@ -388,7 +400,7 @@ def file_diff(folder: str | Path, path: str) -> FileDiff:
if word == "untracked":
return untracked_diff(root, rel)
- args = ["diff", "--no-color", "--no-ext-diff", "--no-renames", "-U3"]
+ args = [*NO_INDEX_REFRESH, "diff", "--no-color", "--no-ext-diff", "--no-renames", "-U3"]
if _has_head(root):
args.append("HEAD")
diff = _git([*args, "--", rel], root)
diff --git a/jarvis/agentic_ide/git_ops.py b/jarvis/agentic_ide/git_ops.py
index 504d65f9a6..b24a2c02d8 100644
--- a/jarvis/agentic_ide/git_ops.py
+++ b/jarvis/agentic_ide/git_ops.py
@@ -383,11 +383,18 @@ def inspect(folder: str | Path) -> RepoInfo:
info.is_repo = True
info.root = str(root)
- status = _run(["status", "--porcelain=v2", "--branch", "--untracked-files=normal"], root)
+ # No optional locks: this is read while agents run their own git here.
+ status = _run(
+ ["--no-optional-locks", "status", "--porcelain=v2", "--branch",
+ "--untracked-files=normal"],
+ root,
+ )
if status.returncode == 0:
_parse_status(status.stdout, info)
if not info.unborn:
- shortstat = _run(["diff", "--shortstat", "HEAD"], root)
+ shortstat = _run(
+ ["-c", "diff.autoRefreshIndex=false", "diff", "--shortstat", "HEAD"], root
+ )
if shortstat.returncode == 0:
plus = re.search(r"(\d+) insertion", shortstat.stdout)
minus = re.search(r"(\d+) deletion", shortstat.stdout)
diff --git a/tests/unit/agentic_ide/test_git_changes.py b/tests/unit/agentic_ide/test_git_changes.py
index a83a275176..2c9604ee51 100644
--- a/tests/unit/agentic_ide/test_git_changes.py
+++ b/tests/unit/agentic_ide/test_git_changes.py
@@ -143,3 +143,32 @@ def test_a_plain_folder_is_reported_unavailable_not_raised(tmp_path: Path) -> No
assert not changes.available
assert changes.reason
+
+
+def test_reading_changes_never_rewrites_the_index(repo: Path) -> None:
+ """A polled read must not take the index lock from someone's own git command."""
+ import os
+
+ from jarvis.agentic_ide import git_ops
+
+ tracked = next(path for path in repo.iterdir() if path.is_file())
+ index = repo / ".git" / "index"
+
+ def touch() -> bytes:
+ # Same content, newer timestamp: a plain `git status` now refreshes the index.
+ stamp = tracked.stat().st_mtime + 30
+ os.utime(tracked, (stamp, stamp))
+ return index.read_bytes()
+
+ before = touch()
+ changes = git_changes.workspace_changes(repo)
+ assert changes.available and git_ops.inspect(repo).is_repo
+ git_changes.file_diff(repo, tracked.name)
+ assert index.read_bytes() == before
+ # ... and a file that was only touched is still not listed as changed.
+ assert tracked.name not in [entry.path for entry in changes.files]
+
+ # The premise holds: git's own status does rewrite it.
+ before = touch()
+ _git(repo, "status")
+ assert index.read_bytes() != before