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(follow-main): never kill a writing git, heal a stale lock, stop bundle line-ending noise (#807)

Ruben Lütke4f6e9daf7944
diff --git a/.gitattributes b/.gitattributes
index 5c208b0b9e..946d433036 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -63,3 +63,9 @@ install/tuf/*.json text eol=lf
# language statistics. Contributors never commit it; the maintainer rebuilds
# the bundle from HEAD's sources (see CONTRIBUTING.md, "Opening your PR").
dodaios/ui/web/dist/** linguist-generated=true
+
+# The bundle is committed byte for byte as `npm run build` writes it (LF). Left
+# to `core.autocrlf=true`, a Windows checkout writes CRLF copies, so every
+# rebuild makes hundreds of files look modified (line endings only) and that
+# noise blocks the fast-forward of anyone following main. No eol conversion.
+dodaios/ui/web/dist/** -text
diff --git a/docs/agent-done-gate.md b/docs/agent-done-gate.md
index 1d28957dc3..e6e4d0af7f 100644
--- a/docs/agent-done-gate.md
+++ b/docs/agent-done-gate.md
@@ -13,7 +13,7 @@ sent back to work with one imperative next step.
| D2 | every local commit of the branch is on the remote |
| D3 | the branch's pull request is merged into `main` |
| D4 | the `main_workflows` are green on `main` for the merge commit (or for a later `main` commit that contains it) |
-| D5 | the local `main` checkout contains the merge commit; the gate fast-forwards it, and a checkout with local edits is left alone and noted |
+| D5 | the local `main` checkout contains the merge commit; the gate fast-forwards it (never killed on a short timer), and a checkout with local edits, or with an `index.lock` (another git command is writing it), is left alone and noted |
## Verdicts
diff --git a/dodaios/core/main_follow.py b/dodaios/core/main_follow.py
index f48279d518..1de10bb82c 100644
--- a/dodaios/core/main_follow.py
+++ b/dodaios/core/main_follow.py
@@ -12,6 +12,14 @@ that nudge, for a person who opted in (``[agentic_ide] follow_main``):
incoming commits would overwrite, is left alone and the reason reported.
* The built frontend bundle (``dodaios/ui/web/dist``) is this follower's to
rebuild, so local copies of it never block the fast-forward.
+* A git command that writes the checkout (``merge``, ``checkout``, ``clean``) is
+ never killed on a short timer: killed mid-write it leaves ``.git/index.lock``
+ behind and a work tree half way to the new commit. When that happens anyway
+ (a crash, a power cut, another tool's timeout), the follower recovers by
+ itself: a lock older than :data:`STALE_LOCK_S` that no running git process
+ can hold is removed, and files a killed fast-forward already brought to
+ ``origin/main``'s content are put back so the fast-forward can run again. A
+ file with any other content is a person's edit and is never touched.
* A change under the frontend source rebuilds the bundle; open windows reload
themselves once the new bundle is on disk (``lib/bundleWatch.ts``).
* A change to the Python code needs a restart. It happens when nothing would be
@@ -32,7 +40,7 @@ import subprocess
import sys
import threading
import time
-from collections.abc import Callable
+from collections.abc import Callable, Iterable
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any
@@ -45,7 +53,13 @@ from dodaios.core.process_utils import NO_WINDOW_CREATIONFLAGS
INTERVAL_S = 60.0
#: The first look waits this long, so it never competes with the app's boot.
FIRST_DELAY_S = 45.0
+#: Reads and fetches: one given up on leaves nothing half written.
_GIT_TIMEOUT_S = 120.0
+#: Commands that write the work tree and the index. On Windows a fast-forward that
+#: rewrites thousands of bundle files (a virus scanner looking at each) can take
+#: minutes; killed, it leaves a stale index.lock and a half-updated checkout, so it
+#: gets all the time it needs.
+GIT_WRITE_TIMEOUT_S = 30 * 60.0
_BUILD_TIMEOUT_S = 20 * 60.0
_INSTALL_TIMEOUT_S = 15 * 60.0
@@ -89,29 +103,66 @@ class FollowStatus:
def _run(
- args: list[str], cwd: Path, *, timeout: float = _GIT_TIMEOUT_S
+ args: list[str],
+ cwd: Path,
+ *,
+ timeout: float = _GIT_TIMEOUT_S,
+ read_only: bool = False,
+ stdin: str | None = None,
) -> subprocess.CompletedProcess[str]:
+ """Run ``args``; a timeout or a program that cannot start comes back as a failure."""
env = dict(os.environ)
env["GIT_TERMINAL_PROMPT"] = "0"
env["GCM_INTERACTIVE"] = "never"
- return subprocess.run(
- args,
- cwd=str(cwd),
- capture_output=True,
- text=True,
- encoding="utf-8",
- errors="replace",
- timeout=timeout,
- check=False,
- env=env,
- creationflags=NO_WINDOW_CREATIONFLAGS,
- )
+ if read_only:
+ # A read never takes the index lock (``git status`` would refresh the index
+ # under it), so a read that is given up on can never leave one behind.
+ env["GIT_OPTIONAL_LOCKS"] = "0"
+ try:
+ return subprocess.run(
+ args,
+ cwd=str(cwd),
+ input=stdin,
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ errors="replace",
+ timeout=timeout,
+ check=False,
+ env=env,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+ except subprocess.TimeoutExpired:
+ logger.warning("Follow main: {} did not finish within {:.0f} s.", args[:4], timeout)
+ return subprocess.CompletedProcess(args, 124, "", f"timed out after {timeout:.0f} s")
+ except OSError as exc:
+ logger.warning("Follow main: {} could not run: {}", args[:4], exc)
+ return subprocess.CompletedProcess(args, 127, "", str(exc))
def _git(
- args: list[str], root: Path, *, timeout: float = _GIT_TIMEOUT_S
+ args: list[str], root: Path, *, stdin: str | None = None
+) -> subprocess.CompletedProcess[str]:
+ """A git command that only reads (or fetches): short timeout, no optional locks."""
+ return _run(
+ ["git", "-c", "core.quotePath=false", *args],
+ root,
+ timeout=_GIT_TIMEOUT_S,
+ read_only=True,
+ stdin=stdin,
+ )
+
+
+def _git_write(
+ args: list[str], root: Path, *, stdin: str | None = None
) -> subprocess.CompletedProcess[str]:
- return _run(["git", "-c", "core.quotePath=false", *args], root, timeout=timeout)
+ """A git command that writes the work tree or the index: never killed on a short timer."""
+ return _run(
+ ["git", "-c", "core.quotePath=false", *args],
+ root,
+ timeout=GIT_WRITE_TIMEOUT_S,
+ stdin=stdin,
+ )
def _last_line(result: subprocess.CompletedProcess[str]) -> str:
@@ -121,20 +172,103 @@ def _last_line(result: subprocess.CompletedProcess[str]) -> str:
return lines[-1] if lines else f"exit code {result.returncode}"
-def _overwritten(result: subprocess.CompletedProcess[str]) -> list[str]:
- """The files git names when local edits would be overwritten by a merge."""
- files: list[str] = []
- collecting = False
+def _blocking(result: subprocess.CompletedProcess[str]) -> tuple[list[str], list[str]]:
+ """(tracked files with local changes, untracked files) that a refused merge names."""
+ tracked: list[str] = []
+ untracked: list[str] = []
+ into: list[str] | None = None
for line in (result.stderr or "").splitlines():
- if "would be overwritten" in line or "untracked working tree files" in line:
- collecting = True
+ if "untracked working tree files" in line:
+ into = untracked
continue
- if collecting:
+ if "would be overwritten" in line:
+ into = tracked
+ continue
+ if into is not None:
if line.startswith(("\t", " ")) and line.strip():
- files.append(line.strip())
+ into.append(line.strip())
elif line.strip():
- collecting = False
- return files
+ into = None
+ return tracked, untracked
+
+
+def _overwritten(result: subprocess.CompletedProcess[str]) -> list[str]:
+ """The files git names when local edits would be overwritten by a merge."""
+ tracked, untracked = _blocking(result)
+ return tracked + untracked
+
+
+@dataclass(frozen=True, slots=True)
+class GitProcess:
+ """A running process whose name starts with ``git``; ``None`` = could not be read."""
+
+ name: str
+ cwd: str | None
+ cmdline: list[str] | None
+
+
+def list_git_processes() -> list[GitProcess] | None:
+ """Every running git process, or ``None`` when that cannot be found out."""
+ try:
+ import psutil
+ except ImportError: # no psutil: cannot tell, keep the lock
+ return None
+ found: list[GitProcess] = []
+ try:
+ for proc in psutil.process_iter(["name"]):
+ name = str(proc.info.get("name") or "")
+ if not name.lower().startswith("git"):
+ continue
+ cwd: str | None = None
+ cmdline: list[str] | None = None
+ try:
+ cwd = proc.cwd()
+ except psutil.NoSuchProcess: # ended meanwhile: holds nothing
+ continue
+ except (psutil.Error, OSError): # unreadable cwd: cmdline still checked
+ pass
+ try:
+ cmdline = list(proc.cmdline())
+ except psutil.NoSuchProcess: # ended meanwhile: holds nothing
+ continue
+ except (psutil.Error, OSError): # unreadable cmdline: matched by cwd
+ pass
+ found.append(GitProcess(name, cwd, cmdline))
+ except (psutil.Error, OSError): # unreadable table: keep the lock
+ return None
+ return found
+
+
+def _inside(path: str, root: Path) -> bool:
+ try:
+ resolved = os.path.normcase(os.path.abspath(path))
+ top = os.path.normcase(os.path.abspath(root))
+ except (OSError, ValueError): # unresolvable: not inside
+ return False
+ return resolved == top or resolved.startswith(top.rstrip("\\/") + os.sep)
+
+
+def git_works_in(root: Path, processes: Iterable[GitProcess] | None) -> bool | None:
+ """Is a git process working in ``root``? ``None`` when that cannot be told.
+
+ A process counts when its working folder is inside ``root`` or its command line
+ names ``-C <a folder inside root>``. One whose working folder cannot be read
+ might be working here, so it makes the answer unknown unless another process
+ already answers yes.
+ """
+ if processes is None:
+ return None
+ unknown = False
+ for proc in processes:
+ args = proc.cmdline or []
+ for flag, value in zip(args, args[1:], strict=False):
+ if flag == "-C" and _inside(os.path.join(proc.cwd or "", value), root):
+ return True
+ if proc.cwd is None:
+ unknown = True
+ elif _inside(proc.cwd, root):
+ return True
+ return None if unknown else False
def package_root() -> Path:
@@ -154,8 +288,10 @@ class MainFollower:
restart: Callable[[], bool],
npm: str | None = None,
python: str | None = None,
+ processes: Callable[[], Iterable[GitProcess] | None] = list_git_processes,
) -> None:
self.root = root
+ self._processes = processes
self._enabled = enabled
self._idle = idle
self._restart = restart
@@ -265,7 +401,11 @@ class MainFollower:
return Path(git_dir) / "index.lock" if git_dir else None
def _locked(self, status: FollowStatus) -> bool:
- """Is the checkout's index locked? Says so, with its age, instead of failing later."""
+ """Is the checkout's index locked? Says so, with its age, instead of failing later.
+
+ A lock older than :data:`STALE_LOCK_S` that no running git process can be
+ holding was left by a killed command; it is removed here, without a click.
+ """
lock = self._index_lock()
if lock is None or not lock.exists():
status.lock_since = 0.0
@@ -277,6 +417,23 @@ class MainFollower:
return False
status.lock_since = since
status.lock_stale = time.time() - since > STALE_LOCK_S
+ if status.lock_stale and git_works_in(self.root, self._processes()) is False:
+ try:
+ lock.unlink()
+ except FileNotFoundError: # gone already: someone else removed it, which is the goal
+ pass
+ except OSError as exc:
+ logger.warning("Follow main: the stale git lock could not be removed: {}", exc)
+ else:
+ logger.info(
+ "Follow main: removed a stale git lock ({:.0f} min old; no git process "
+ "works in this checkout).",
+ (time.time() - since) / 60,
+ )
+ if not lock.exists():
+ status.lock_since = 0.0
+ status.lock_stale = False
+ return False
self._note(
"A git lock left by a stopped command blocks the update (.git/index.lock)."
if status.lock_stale
@@ -315,9 +472,11 @@ class MainFollower:
try:
if any(path.startswith(f"{DIST}/") for path in changed):
# The bundle is rebuilt below; a local copy of it must not block the update.
- _git(["checkout", "--", DIST], self.root)
- _git(["clean", "-fdq", "--", DIST], self.root)
- merged = _git(["merge", "--ff-only", "--quiet", "origin/main"], self.root)
+ _git_write(["checkout", "--", DIST], self.root)
+ _git_write(["clean", "-fdq", "--", DIST], self.root)
+ merged = _git_write(["merge", "--ff-only", "--quiet", "origin/main"], self.root)
+ if merged.returncode != 0 and self._recover_interrupted(merged):
+ merged = _git_write(["merge", "--ff-only", "--quiet", "origin/main"], self.root)
if merged.returncode != 0:
status.blocked_by = _overwritten(merged)
shown = ", ".join(status.blocked_by[:5]) or _last_line(merged)
@@ -345,6 +504,73 @@ class MainFollower:
finally:
status.busy = ""
+ def _recover_interrupted(self, merged: subprocess.CompletedProcess[str]) -> bool:
+ """Put back what a killed fast-forward left half done. True = merge again.
+
+ A fast-forward killed mid-write leaves files at the new commit's content
+ while ``HEAD`` and the index are still at the old one: they look like local
+ edits, new files look untracked, and every later fast-forward is refused. A
+ listed file whose content equals its ``origin/main`` blob is such a leftover
+ and loses nothing when it is put back: the merge writes that content again.
+ If any listed file holds anything else, it is a person's edit and nothing at
+ all is touched.
+ """
+ tracked, untracked = _blocking(merged)
+ listed = tracked + untracked
+ if not listed or not self._all_at_remote(listed):
+ return False
+ if tracked:
+ restored = _git_write(
+ [
+ "--literal-pathspecs",
+ "checkout",
+ "--pathspec-from-file=-",
+ "--pathspec-file-nul",
+ "HEAD",
+ ],
+ self.root,
+ stdin="\0".join(tracked) + "\0",
+ )
+ if restored.returncode != 0:
+ logger.warning(
+ "Follow main: putting back an interrupted update failed: {}",
+ _last_line(restored),
+ )
+ return False
+ for rel in untracked:
+ try:
+ (self.root / rel).unlink()
+ except FileNotFoundError: # gone already: nothing left to remove
+ pass
+ except OSError as exc:
+ logger.warning("Follow main: could not remove the leftover {}: {}", rel, exc)
+ return False
+ logger.info(
+ "Follow main: {} file(s) an interrupted update had already brought to "
+ "origin/main were put back; updating again.",
+ len(listed),
+ )
+ return True
+
+ def _all_at_remote(self, paths: list[str]) -> bool:
+ """Does every one of ``paths`` hold exactly its content at ``origin/main``?"""
+ local = _git(["hash-object", "--stdin-paths"], self.root, stdin="\n".join(paths) + "\n")
+ remote = _git(
+ ["cat-file", "--batch-check=%(objectname) %(objecttype)"],
+ self.root,
+ stdin="".join(f"origin/main:{path}\n" for path in paths),
+ )
+ if local.returncode != 0 or remote.returncode != 0:
+ return False
+ hashes = local.stdout.split()
+ blobs = [line.split() for line in remote.stdout.splitlines()]
+ if len(hashes) != len(paths) or len(blobs) != len(paths):
+ return False
+ return all(
+ len(blob) == 2 and blob[1] == "blob" and blob[0] == sha
+ for sha, blob in zip(hashes, blobs, strict=True)
+ )
+
def _build(self, *, npm_install: bool) -> bool:
npm = self._npm or shutil.which("npm")
if not npm:
diff --git a/scripts/agent_done_gate.py b/scripts/agent_done_gate.py
index abc7bc2f96..7c69fc3113 100644
--- a/scripts/agent_done_gate.py
+++ b/scripts/agent_done_gate.py
@@ -63,7 +63,14 @@ if str(REPO_ROOT) not in sys.path:
from scripts.ci import merge_freeze # noqa: E402
-Runner = Callable[[list[str], "Path | None"], subprocess.CompletedProcess]
+#: ``run(cmd, cwd)`` or ``run(cmd, cwd, timeout=seconds)``.
+Runner = Callable[..., subprocess.CompletedProcess]
+#: Commands that only read (or call GitHub) are given up on after this long.
+READ_TIMEOUT_S = 120.0
+#: A git command that writes a work tree is never killed on a short timer: killed
+#: mid-write, it leaves ``index.lock`` behind and a checkout half way to the new
+#: commit, which then blocks every later update until someone cleans up by hand.
+GIT_WRITE_TIMEOUT_S = 30 * 60.0
MAY_STOP = ("DONE", "NONE", "HOLD", "STUCK")
MUST_CONTINUE = ("WAIT", "FIX")
@@ -111,7 +118,9 @@ class GateError(Exception):
# --------------------------------------------------------------------------
-def default_runner(cmd: list[str], cwd: Path | None = None) -> subprocess.CompletedProcess:
+def default_runner(
+ cmd: list[str], cwd: Path | None = None, *, timeout: float = READ_TIMEOUT_S
+) -> subprocess.CompletedProcess:
try:
return subprocess.run( # noqa: S603
cmd,
@@ -121,7 +130,7 @@ def default_runner(cmd: list[str], cwd: Path | None = None) -> subprocess.Comple
encoding="utf-8",
errors="replace",
check=False,
- timeout=120,
+ timeout=timeout,
)
except (OSError, subprocess.TimeoutExpired) as exc:
return subprocess.CompletedProcess(cmd, 127, "", str(exc))
@@ -137,8 +146,12 @@ class Ctx:
common_dir: Path | None = None
_freeze: tuple[float, object] | None = None
- def git(self, *args: str, cwd: Path | None = None) -> subprocess.CompletedProcess:
- return self.run(["git", *args], cwd or self.cwd)
+ def git(
+ self, *args: str, cwd: Path | None = None, timeout: float | None = None
+ ) -> subprocess.CompletedProcess:
+ if timeout is None:
+ return self.run(["git", *args], cwd or self.cwd)
+ return self.run(["git", *args], cwd or self.cwd, timeout=timeout)
def git_out(self, *args: str, cwd: Path | None = None) -> str | None:
proc = self.git(*args, cwd=cwd)
@@ -446,6 +459,21 @@ def main_ci_state(ctx: Ctx, sha: str) -> tuple[str, str]:
return "success", ""
+def index_lock(checkout: Path) -> Path | None:
+ """The ``index.lock`` of the work tree at ``checkout`` (a main or a linked worktree)."""
+ dot_git = checkout / ".git"
+ if dot_git.is_dir():
+ return dot_git / "index.lock"
+ try:
+ text = dot_git.read_text(encoding="utf-8").strip()
+ except OSError:
+ return None
+ if not text.startswith("gitdir:"):
+ return None
+ git_dir = Path(text[len("gitdir:") :].strip())
+ return (git_dir if git_dir.is_absolute() else checkout / git_dir) / "index.lock"
+
+
def sync_local_main(ctx: Ctx, sha: str) -> tuple[bool, str]:
"""D5: make the local main contain ``sha``. Never stashes, never resets."""
base = ctx.base
@@ -462,7 +490,22 @@ def sync_local_main(ctx: Ctx, sha: str) -> tuple[bool, str]:
if checkout is None:
ctx.git("fetch", "--quiet", "origin", f"{base}:{base}")
else:
- ctx.git("merge", "--ff-only", "--quiet", f"origin/{base}", cwd=checkout)
+ lock = index_lock(checkout)
+ if lock is not None and lock.exists():
+ # Another git command (the app following main, a person's pull) is
+ # writing that checkout, or one was killed: either way, not ours to touch.
+ return False, (
+ f"the local {base} at {checkout} is being updated by another git command; "
+ f"left as it is"
+ )
+ ctx.git(
+ "merge",
+ "--ff-only",
+ "--quiet",
+ f"origin/{base}",
+ cwd=checkout,
+ timeout=GIT_WRITE_TIMEOUT_S,
+ )
if is_ancestor(ctx, sha, f"refs/heads/{base}"):
return True, f"fast-forwarded the local {base}"
where = f" at {checkout}" if checkout else ""
diff --git a/tests/unit/ci/test_agent_done_gate.py b/tests/unit/ci/test_agent_done_gate.py
index 2c3e3d7030..d569559a14 100644
--- a/tests/unit/ci/test_agent_done_gate.py
+++ b/tests/unit/ci/test_agent_done_gate.py
@@ -40,6 +40,7 @@ class FakeWorld:
self.ff_works = True
self.is_repo = True
self.commands: list[list[str]] = []
+ self.timeouts: list[tuple[list[str], float | None]] = []
def ok(self, cmd: list[str], out: object = "") -> subprocess.CompletedProcess:
text = out if isinstance(out, str) else json.dumps(out)
@@ -48,8 +49,11 @@ class FakeWorld:
def fail(self, cmd: list[str]) -> subprocess.CompletedProcess:
return subprocess.CompletedProcess(cmd, 1, "", "no")
- def __call__(self, cmd: list[str], cwd: Path | None = None) -> subprocess.CompletedProcess:
+ def __call__(
+ self, cmd: list[str], cwd: Path | None = None, timeout: float | None = None
+ ) -> subprocess.CompletedProcess:
self.commands.append(cmd)
+ self.timeouts.append((cmd, timeout))
if cmd[0] == "git":
return self.git(cmd, cmd[1:], cwd)
return self.gh(cmd, cmd[1:])
@@ -409,6 +413,42 @@ def test_a_local_main_with_edits_is_left_alone_and_noted(world: FakeWorld) -> No
assert "left untouched" in verdict.notes[0]
+def test_the_fast_forward_of_local_main_is_never_killed_on_a_short_timer(
+ world: FakeWorld,
+) -> None:
+ world.branch("claude/x")
+ world.pr("claude/x", state="MERGED", mergeCommit={"oid": MERGE_SHA})
+ world.main_runs = [main_run(MERGE_SHA, "success")]
+ assert run_gate(world, ["claude/x"]).verdict == "DONE"
+ merges = [t for cmd, t in world.timeouts if cmd[1:3] == ["merge", "--ff-only"]]
+ assert merges == [gate.GIT_WRITE_TIMEOUT_S] and gate.GIT_WRITE_TIMEOUT_S >= 30 * 60
+
+
+def test_a_local_main_another_git_command_is_updating_is_left_as_it_is(
+ world: FakeWorld,
+) -> None:
+ world.branch("claude/x")
+ world.pr("claude/x", state="MERGED", mergeCommit={"oid": MERGE_SHA})
+ world.main_runs = [main_run(MERGE_SHA, "success")]
+ (world.top / ".git" / "index.lock").write_bytes(b"")
+ verdict = run_gate(world, ["claude/x"])
+ assert verdict.verdict == "DONE" and verdict.checks["D5"] is False
+ assert "being updated by another git command" in verdict.notes[0]
+ assert not any(cmd[1:2] == ["merge"] for cmd in world.commands)
+
+
+def test_the_index_lock_of_a_linked_worktree_is_found_through_its_git_file(
+ tmp_path: Path,
+) -> None:
+ tree = tmp_path / "tree"
+ tree.mkdir()
+ (tree / ".git").write_text("gitdir: ../repo/.git/worktrees/tree\n", encoding="utf-8")
+ assert gate.index_lock(tree) == tree / "../repo/.git/worktrees/tree" / "index.lock"
+ (tmp_path / "plain" / ".git").mkdir(parents=True)
+ assert gate.index_lock(tmp_path / "plain") == tmp_path / "plain" / ".git" / "index.lock"
+ assert gate.index_lock(tmp_path / "nothing") is None
+
+
def test_the_worst_branch_decides_for_the_session(world: FakeWorld) -> None:
world.branch("claude/a")
world.pr("claude/a", state="MERGED", mergeCommit={"oid": MERGE_SHA})
diff --git a/tests/unit/ci/test_check_dist_consistency.py b/tests/unit/ci/test_check_dist_consistency.py
index 442d7b424a..b936f93025 100644
--- a/tests/unit/ci/test_check_dist_consistency.py
+++ b/tests/unit/ci/test_check_dist_consistency.py
@@ -92,3 +92,17 @@ def test_staged_mode_blocks_added_orphans_and_prune_untracks_them(
assert A + "Lazy-EEEEEEEE.js" not in staged
assert (tmp_path / A / "Lazy-EEEEEEEE.js").is_file() # disk untouched
assert gate.main(["--staged"]) == 0
+
+
[email protected](shutil.which("git") is None, reason="git is not installed")
+def test_the_bundle_is_checked_out_byte_for_byte_without_eol_conversion() -> None:
+ """With core.autocrlf=true a converted checkout makes every rebuild look modified."""
+ repo = Path(__file__).resolve().parents[3]
+ result = subprocess.run(
+ ["git", "check-attr", "text", "--", "dodaios/ui/web/dist/index.html"],
+ cwd=repo,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ assert result.stdout.strip() == "dodaios/ui/web/dist/index.html: text: unset"
diff --git a/tests/unit/core/test_main_follow.py b/tests/unit/core/test_main_follow.py
index 5d2ba791c2..099860f637 100644
--- a/tests/unit/core/test_main_follow.py
+++ b/tests/unit/core/test_main_follow.py
@@ -2,14 +2,18 @@
from __future__ import annotations
+import os
import shutil
import subprocess
+import sys
+import time
+from collections.abc import Callable, Iterable
from pathlib import Path
import pytest
from dodaios.core import main_follow
-from dodaios.core.main_follow import DIST, MainFollower
+from dodaios.core.main_follow import DIST, GitProcess, MainFollower, git_works_in
pytestmark = pytest.mark.skipif(shutil.which("git") is None, reason="git is not installed")
@@ -21,6 +25,8 @@ def _identity(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
monkeypatch.setenv("GIT_COMMITTER_NAME", "Test")
monkeypatch.setenv("GIT_COMMITTER_EMAIL", "[email protected]")
monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
+ # Long temp folders on Windows overflow MAX_PATH inside a bare repo's objects.
+ (tmp_path / "gitconfig").write_text("[core]\n\tlongpaths = true\n", encoding="utf-8")
monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(tmp_path / "gitconfig"))
@@ -68,9 +74,21 @@ class Recorder:
self.restarts = 0
self.builds: list[bool] = []
- def follower(self, root: Path, *, enabled: bool = True) -> MainFollower:
+ def follower(
+ self,
+ root: Path,
+ *,
+ enabled: bool = True,
+ processes: Callable[[], Iterable[GitProcess] | None] = lambda: None,
+ ) -> MainFollower:
+ # By default the running processes cannot be told, so no lock is ever
+ # removed on its own; the tests about that pass a lister of their own.
follower = MainFollower(
- root, enabled=lambda: enabled, idle=lambda: self.idle, restart=self._restart
+ root,
+ enabled=lambda: enabled,
+ idle=lambda: self.idle,
+ restart=self._restart,
+ processes=processes,
)
def build(*, npm_install: bool) -> bool:
@@ -205,9 +223,6 @@ def test_python_outside_the_app_needs_no_restart(setup: tuple[Path, Path]) -> No
def test_a_git_lock_waits_and_only_an_old_one_is_offered_for_removal(
setup: tuple[Path, Path],
) -> None:
- import os
- import time
-
app, other = setup
_land(other, {"jarvis_extra.txt": "x\n"}, "chore: extra")
lock = app / ".git" / "index.lock"
@@ -229,3 +244,163 @@ def test_a_git_lock_waits_and_only_an_old_one_is_offered_for_removal(
status = follower.tick()
assert (app / "jarvis_extra.txt").exists()
assert status.lock_since == 0 and status.reason == ""
+
+
+# ------------------------------------------------------------------ killed git commands
+
+
+def _age(path: Path) -> None:
+ old = time.time() - main_follow.STALE_LOCK_S - 60
+ os.utime(path, (old, old))
+
+
+def test_writing_git_commands_get_the_long_timeout_and_reads_take_no_lock(
+ setup: tuple[Path, Path], monkeypatch: pytest.MonkeyPatch
+) -> None:
+ app, other = setup
+ _write(app, f"{DIST}/index.html", "<html>local build</html>\n")
+ _land(other, {f"{DIST}/index.html": "<html>v2</html>\n"}, "build(ui): bundle")
+ calls: list[tuple[list[str], float, bool]] = []
+ real_run = main_follow._run
+
+ def recording(args: list[str], cwd: Path, **kwargs: object) -> subprocess.CompletedProcess:
+ timeout = kwargs.get("timeout", main_follow._GIT_TIMEOUT_S)
+ calls.append((args, float(timeout), bool(kwargs.get("read_only", False)))) # type: ignore[arg-type]
+ return real_run(args, cwd, **kwargs) # type: ignore[arg-type]
+
+ monkeypatch.setattr(main_follow, "_run", recording)
+ status = Recorder().follower(app).tick()
+ assert status.reason == "" and status.head == status.remote
+
+ def verb(args: list[str]) -> str:
+ return args[3] if args[:3] == ["git", "-c", "core.quotePath=false"] else args[0]
+
+ writes = {verb(args) for args, _, read_only in calls if not read_only}
+ assert writes == {"checkout", "clean", "merge"}
+ for args, timeout, read_only in calls:
+ if verb(args) in writes:
+ assert timeout == main_follow.GIT_WRITE_TIMEOUT_S >= 30 * 60, args
+ assert not read_only
+ else:
+ assert read_only and timeout <= 120, args
+
+
+def test_a_timeout_or_a_missing_program_is_a_failed_result_not_an_exception(
+ tmp_path: Path,
+) -> None:
+ slow = main_follow._run(
+ [sys.executable, "-c", "import time; time.sleep(30)"], tmp_path, timeout=0.5
+ )
+ assert slow.returncode != 0 and "timed out" in slow.stderr
+ missing = main_follow._run(["dodaios-no-such-program-xyz"], tmp_path)
+ assert missing.returncode != 0
+
+
+def test_which_git_processes_count_as_working_in_the_checkout(tmp_path: Path) -> None:
+ root = tmp_path / "app"
+ elsewhere = str(tmp_path / "other")
+ assert git_works_in(root, []) is False
+ assert git_works_in(root, [GitProcess("git.exe", elsewhere, ["git", "status"])]) is False
+ assert git_works_in(root, [GitProcess("git.exe", str(root / "sub"), ["git"])]) is True
+ assert git_works_in(root, [GitProcess("git", elsewhere, ["git", "-C", str(root)])]) is True
+ # Nothing can be read about it: it might be working here.
+ assert git_works_in(root, [GitProcess("git.exe", None, None)]) is None
+ assert git_works_in(root, [GitProcess("git.exe", None, ["git", "status"])]) is None
+ assert git_works_in(root, None) is None
+ # One that is known to work here answers, whatever else is unreadable.
+ both = [GitProcess("git.exe", None, None), GitProcess("git.exe", str(root), ["git"])]
+ assert git_works_in(root, both) is True
+
+
+def test_a_stale_lock_no_git_process_holds_is_removed_by_itself(
+ setup: tuple[Path, Path],
+) -> None:
+ app, other = setup
+ _land(other, {"jarvis_extra.txt": "x\n"}, "chore: extra")
+ lock = app / ".git" / "index.lock"
+ lock.write_bytes(b"")
+ _age(lock)
+ status = Recorder().follower(app, processes=lambda: []).tick()
+ assert not lock.exists() and (app / "jarvis_extra.txt").exists()
+ assert status.lock_since == 0 and not status.lock_stale and status.reason == ""
+
+
+def test_a_young_lock_is_never_removed_even_with_no_git_running(
+ setup: tuple[Path, Path],
+) -> None:
+ app, other = setup
+ _land(other, {"jarvis_extra.txt": "x\n"}, "chore: extra")
+ lock = app / ".git" / "index.lock"
+ lock.write_bytes(b"")
+ status = Recorder().follower(app, processes=lambda: []).tick()
+ assert lock.exists() and not (app / "jarvis_extra.txt").exists()
+ assert "Another git command" in status.reason
+
+
+ "seen",
+ [
+ pytest.param(lambda root: [GitProcess("git.exe", str(root), ["git", "merge"])], id="cwd"),
+ pytest.param(
+ lambda root: [GitProcess("git.exe", str(root.parent), ["git", "-C", str(root)])],
+ id="dash-C",
+ ),
+ pytest.param(lambda root: [GitProcess("git.exe", None, None)], id="unreadable"),
+ pytest.param(lambda root: None, id="cannot-list"),
+ ],
+)
+def test_a_stale_lock_stays_while_a_git_process_may_work_in_the_checkout(
+ setup: tuple[Path, Path], seen: Callable[[Path], list[GitProcess] | None]
+) -> None:
+ app, other = setup
+ _land(other, {"jarvis_extra.txt": "x\n"}, "chore: extra")
+ lock = app / ".git" / "index.lock"
+ lock.write_bytes(b"")
+ _age(lock)
+ status = Recorder().follower(app, processes=lambda: seen(app)).tick()
+ assert lock.exists() and not (app / "jarvis_extra.txt").exists()
+ assert status.lock_stale and "stopped command" in status.reason
+
+
+def _interrupted(app: Path, other: Path, readme: str) -> None:
+ """origin/main moves on; a killed fast-forward already wrote some of its files."""
+ _land(
+ other,
+ {
+ "README.md": "new readme\n",
+ "dodaios/core/new.py": "N = 1\n",
+ "dodaios/core/a.py": "A = 2\n",
+ },
+ "feat: three files",
+ )
+ _write(app, "README.md", readme)
+ _write(app, "dodaios/core/new.py", "N = 1\n")
+ lock = app / ".git" / "index.lock"
+ lock.write_bytes(b"")
+ _age(lock)
+
+
+def test_an_interrupted_fast_forward_is_finished_when_its_leftovers_match_origin(
+ setup: tuple[Path, Path],
+) -> None:
+ app, other = setup
+ _interrupted(app, other, readme="new readme\n")
+ status = Recorder().follower(app, processes=lambda: []).tick()
+ assert _git(app, "rev-parse", "HEAD") == _git(app, "rev-parse", "origin/main")
+ assert status.reason == "" and status.blocked_by == []
+ assert (app / "dodaios/core/a.py").read_text(encoding="utf-8") == "A = 2\n"
+ assert _git(app, "status", "--porcelain") == ""
+
+
+def test_an_interrupted_fast_forward_with_one_real_edit_is_left_alone(
+ setup: tuple[Path, Path],
+) -> None:
+ app, other = setup
+ _interrupted(app, other, readme="my own notes\n")
+ head = _git(app, "rev-parse", "HEAD")
+ status = Recorder().follower(app, processes=lambda: []).tick()
+ assert _git(app, "rev-parse", "HEAD") == head
+ assert sorted(status.blocked_by) == ["README.md", "dodaios/core/new.py"]
+ assert (app / "README.md").read_text(encoding="utf-8") == "my own notes\n"
+ assert (app / "dodaios/core/new.py").read_text(encoding="utf-8") == "N = 1\n"
+ assert (app / "dodaios/core/a.py").read_text(encoding="utf-8") == "A = 1\n"