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(desktop): recover a conflicted frontend index at boot

rubenluetke10-beepbbb072092e7d
diff --git a/jarvis/ui/web/fast_bootstrap.py b/jarvis/ui/web/fast_bootstrap.py
index f4f59e29a2..2e77645424 100644
--- a/jarvis/ui/web/fast_bootstrap.py
+++ b/jarvis/ui/web/fast_bootstrap.py
@@ -29,7 +29,7 @@ from contextlib import suppress
from pathlib import Path
from typing import Any
-from .spa_build import build_is_complete, holding_page_html
+from .spa_build import build_is_complete, holding_page_html, recover_conflicted_index
from .surface_security import SurfaceSecurity
# The built React frontend lives next to this module (jarvis/ui/web/dist),
@@ -333,17 +333,26 @@ class FastBootstrap:
target = self._resolve_static_file(scope.get("path", "/"))
if target is None:
return False
- if target.name == "index.html" and not build_is_complete(target, self._dist_dir):
+ incomplete_index = target.name == "index.html" and not build_is_complete(
+ target, self._dist_dir
+ )
+ recovered = None
+ if incomplete_index:
+ recovered = recover_conflicted_index(target, self._dist_dir)
+ if incomplete_index and recovered is None:
# A rebuild is between deleting dist/assets and writing the new
# ones. Handing out this index.html gives the window a boot splash
# whose entry bundle 404s — no React, no self-healing, and a
# twenty-second wait before anything notices (see spa_build.py).
await self._rebuilding(scope, send)
return True
- try:
- data = await asyncio.to_thread(target.read_bytes)
- except OSError:
- return False
+ if recovered is not None:
+ data = recovered.encode("utf-8")
+ else:
+ try:
+ data = await asyncio.to_thread(target.read_bytes)
+ except OSError:
+ return False
ctype = mimetypes.guess_type(target.name)[0] or "application/octet-stream"
headers = [
(b"content-type", ctype.encode("latin-1")),
diff --git a/jarvis/ui/web/server.py b/jarvis/ui/web/server.py
index 46b8796fa1..90f3f683af 100644
--- a/jarvis/ui/web/server.py
+++ b/jarvis/ui/web/server.py
@@ -56,7 +56,7 @@ from .schema import (
WSWelcome,
event_to_ws_envelope,
)
-from .spa_build import build_is_complete, holding_page_html
+from .spa_build import build_is_complete, holding_page_html, recover_conflicted_index
from .surface_security import SurfaceSecurity, set_browser_login_required
from .wallpapers import register_wallpaper_routes
@@ -2214,6 +2214,15 @@ class WebServer:
"Pragma": "no-cache",
},
)
+ recovered = recover_conflicted_index(INDEX_FILE, DIST_DIR)
+ if recovered is not None:
+ return HTMLResponse(
+ content=recovered,
+ headers={
+ "Cache-Control": "no-store, max-age=0",
+ "Pragma": "no-cache",
+ },
+ )
return self._spa_placeholder_response()
@staticmethod
diff --git a/jarvis/ui/web/spa_build.py b/jarvis/ui/web/spa_build.py
index e5a6145917..e63d4543c4 100644
--- a/jarvis/ui/web/spa_build.py
+++ b/jarvis/ui/web/spa_build.py
@@ -38,6 +38,7 @@ from pathlib import Path
__all__ = [
"referenced_assets",
"build_is_complete",
+ "recover_conflicted_index",
"holding_page_html",
"HOLDING_ATTRIBUTE",
"HOLDING_MARKER",
@@ -88,6 +89,12 @@ def build_is_complete(index_file: Path, dist_dir: Path) -> bool:
html = index_file.read_text(encoding="utf-8", errors="replace")
except OSError:
return False
+ if any(line.startswith("<<<<<<< ") for line in html.splitlines()):
+ return False
+ return _assets_exist(html, dist_dir)
+
+
+def _assets_exist(html: str, dist_dir: Path) -> bool:
for ref in referenced_assets(html):
candidate = dist_dir / ref.lstrip("/")
try:
@@ -98,6 +105,68 @@ def build_is_complete(index_file: Path, dist_dir: Path) -> bool:
return True
+def recover_conflicted_index(index_file: Path, dist_dir: Path) -> str | None:
+ """Serve an intact side of a Git-conflicted build without editing the merge.
+
+ ``dist/`` is tracked, so a merge can put conflict markers into index.html
+ while leaving one side's hashed assets on disk. The normal completeness
+ check then sees assets from both sides and holds the desktop on Updating
+ forever. Prefer the local side, then the incoming side, only when its entry
+ assets exist. The unresolved file remains available for the merge owner.
+ """
+ try:
+ lines = index_file.read_text(encoding="utf-8", errors="replace").splitlines(
+ keepends=True
+ )
+ except OSError:
+ return None
+ if not any(line.startswith("<<<<<<< ") for line in lines):
+ return None
+
+ variants = [[], []]
+ cursor = 0
+ conflicts = 0
+ while cursor < len(lines):
+ line = lines[cursor]
+ if not line.startswith("<<<<<<< "):
+ for variant in variants:
+ variant.append(line)
+ cursor += 1
+ continue
+
+ conflicts += 1
+ cursor += 1
+ sides = [[], []]
+ side = 0
+ while cursor < len(lines) and not lines[cursor].startswith(">>>>>>> "):
+ line = lines[cursor]
+ if line.startswith("||||||| "):
+ side = -1 # Ignore the diff3 base version.
+ elif line.rstrip("\r\n") == "=======":
+ side = 1
+ elif side >= 0:
+ sides[side].append(line)
+ cursor += 1
+ if cursor == len(lines) or side != 1:
+ return None
+ for variant, content in zip(variants, sides, strict=True):
+ variant.extend(content)
+ cursor += 1
+
+ if not conflicts:
+ return None
+ for variant in variants:
+ html = "".join(variant)
+ if (
+ html.lstrip("\ufeff\r\n ").lower().startswith("<!doctype html>")
+ and "</html>" in html
+ and referenced_assets(html)
+ and _assets_exist(html, dist_dir)
+ ):
+ return html
+ return None
+
+
def holding_page_html() -> str:
"""The page a window gets while a rebuild is between two states on disk.
diff --git a/tests/unit/ui/web/test_spa_build_completeness.py b/tests/unit/ui/web/test_spa_build_completeness.py
index 908c83dde6..e1744bd5dd 100644
--- a/tests/unit/ui/web/test_spa_build_completeness.py
+++ b/tests/unit/ui/web/test_spa_build_completeness.py
@@ -30,6 +30,7 @@ from jarvis.ui.web.spa_build import (
HOLDING_MARKER,
build_is_complete,
holding_page_html,
+ recover_conflicted_index,
referenced_assets,
)
@@ -40,6 +41,20 @@ _INDEX = (
'</head><body><div id="root"></div></body></html>'
)
+_CONFLICTED_INDEX = (
+ "<!doctype html><html><head>\n"
+ "<<<<<<< HEAD\n"
+ '<script type="module" src="/assets/index-abc123.js"></script>\n'
+ '<link rel="stylesheet" href="/assets/index-def456.css">\n'
+ "||||||| base\n"
+ '<script type="module" src="/assets/index-old.js"></script>\n'
+ "=======\n"
+ '<script type="module" src="/assets/index-incoming.js"></script>\n'
+ '<link rel="stylesheet" href="/assets/index-incoming.css">\n'
+ ">>>>>>> origin/main\n"
+ '</head><body><div id="root"></div></body></html>'
+)
+
def _dist(tmp_path: Path, *, assets: bool = True, index: bool = True) -> Path:
dist = tmp_path / "dist"
@@ -92,6 +107,37 @@ class TestBuildIsComplete:
(dist / "index.html").write_text("<html><body>hi</body></html>", encoding="utf-8")
assert build_is_complete(dist / "index.html", dist) is True
+ def test_a_merge_conflict_is_never_served_as_html(self, tmp_path: Path) -> None:
+ dist = _dist(tmp_path)
+ (dist / "index.html").write_text(_CONFLICTED_INDEX, encoding="utf-8")
+ for name in ("index-incoming.js", "index-incoming.css"):
+ (dist / "assets" / name).write_text("", encoding="utf-8")
+ assert build_is_complete(dist / "index.html", dist) is False
+ recovered = recover_conflicted_index(dist / "index.html", dist)
+ assert recovered is not None
+ assert "index-abc123.js" in recovered
+ assert "index-incoming.js" not in recovered
+ assert "<<<<<<<" not in recovered
+
+ def test_a_merge_can_use_the_incoming_side_when_local_assets_are_gone(
+ self, tmp_path: Path
+ ) -> None:
+ dist = _dist(tmp_path, assets=False)
+ (dist / "index.html").write_text(_CONFLICTED_INDEX, encoding="utf-8")
+ for name in ("index-incoming.js", "index-incoming.css"):
+ (dist / "assets" / name).write_text("", encoding="utf-8")
+ recovered = recover_conflicted_index(dist / "index.html", dist)
+ assert recovered is not None
+ assert "index-incoming.js" in recovered
+ assert "<<<<<<<" not in recovered
+
+ def test_an_unrecoverable_conflict_keeps_the_holding_page(
+ self, tmp_path: Path
+ ) -> None:
+ dist = _dist(tmp_path, assets=False)
+ (dist / "index.html").write_text(_CONFLICTED_INDEX, encoding="utf-8")
+ assert recover_conflicted_index(dist / "index.html", dist) is None
+
class TestHoldingPage:
def test_it_carries_the_marker_a_waiting_window_looks_for(self) -> None:
@@ -175,6 +221,16 @@ class TestFastBootstrapServesTheHoldingPage:
# And crucially NOT the document whose script tag would 404.
assert b"index-abc123.js" not in body
+ @pytest.mark.asyncio
+ async def test_a_conflicted_build_serves_the_complete_side(self, tmp_path: Path) -> None:
+ dist = _dist(tmp_path)
+ (dist / "index.html").write_text(_CONFLICTED_INDEX, encoding="utf-8")
+ sent = await _drive(FastBootstrap(dist_dir=dist)._asgi, _get())
+ body = b"".join(m.get("body", b"") for m in sent if m["type"] == "http.response.body")
+ assert b"index-abc123.js" in body
+ assert b"<<<<<<<" not in body
+ assert HOLDING_MARKER.encode() not in body
+
class TestTheRealAppServesTheHoldingPage:
"""The same rule on the route a running window actually hits.
@@ -215,6 +271,19 @@ class TestTheRealAppServesTheHoldingPage:
assert HOLDING_MARKER.encode() in response.body
assert b"index-abc123.js" not in response.body
+ def test_a_conflicted_build_serves_the_complete_side(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ from fastapi.responses import HTMLResponse
+
+ dist = _dist(tmp_path)
+ (dist / "index.html").write_text(_CONFLICTED_INDEX, encoding="utf-8")
+ response = self._response(monkeypatch, dist)
+ assert isinstance(response, HTMLResponse)
+ assert b"index-abc123.js" in response.body
+ assert b"<<<<<<<" not in response.body
+ assert HOLDING_MARKER.encode() not in response.body
+
def test_a_missing_build_still_gets_the_holding_page(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None: