Skip to content

Commit 210c635

Browse files
Support fork-backed runtime update remotes
1 parent 0bbece3 commit 210c635

5 files changed

Lines changed: 152 additions & 11 deletions

File tree

‎config/ulysses/runtime-management.json‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,19 @@
102102
{
103103
"path": ".env",
104104
"mode": "0600",
105-
"content": "# Camofox browser environment\nCAMOFOX_PORT=9377\nCAMOUFOX_INSTALL_DIR=${HOME}/.cache/camoufox\nCAMOFOX_CRASH_REPORT_ENABLED=false\n"
105+
"content": "# Camofox browser environment\nCAMOFOX_PORT=9377\nCAMOUFOX_INSTALL_DIR=${HOME}/.cache/camoufox\nCAMOFOX_CRASH_REPORT_ENABLED=false\nBROWSER_IDLE_TIMEOUT_MS=0\n"
106106
},
107107
{
108108
"path": "start.sh",
109109
"mode": "0755",
110110
"content": "#!/usr/bin/env bash\nset -Eeuo pipefail\nROOT=\"$(cd -- \"$(dirname -- \"${BASH_SOURCE[0]}\")\" && pwd -P)\"\ncd \"$ROOT\"\nif [[ -f .env ]]; then\n set -a\n source .env\n set +a\nfi\nexec bun start\n"
111111
}
112112
],
113-
"launch": ["bash", "start.sh"],
113+
"launch": ["env", "BROWSER_IDLE_TIMEOUT_MS=0", "bash", "start.sh"],
114114
"tmux_session": "diogenes-camofox-browser",
115115
"git_update": true,
116116
"source_url": "https://github.com/jo-inc/camofox-browser",
117+
"source_remote": "upstream",
117118
"source_branch": "master",
118119
"build_script": "build",
119120
"bun_install_mode": "foreign_lock",
@@ -188,6 +189,7 @@
188189
"env_files": [],
189190
"git_update": true,
190191
"source_url": "https://github.com/ildunari/context-mode.git",
192+
"source_remote": "ildunari",
191193
"source_branch": "feat/hermes-native-support",
192194
"build_script": "build",
193195
"bun_install_mode": "frozen",
@@ -216,6 +218,7 @@
216218
"env_files": [],
217219
"git_update": true,
218220
"source_url": "https://github.com/redf0x1/camofox-mcp.git",
221+
"source_remote": "upstream",
219222
"source_branch": "main",
220223
"build_script": "build",
221224
"bun_install_mode": "frozen",
@@ -346,6 +349,7 @@
346349
"tmux_session": "diogenes-hermes-workspace",
347350
"git_update": true,
348351
"source_url": "https://github.com/outsourc-e/hermes-workspace.git",
352+
"source_remote": "upstream",
349353
"source_branch": "main",
350354
"build_script": "build",
351355
"bun_install_mode": "pnpm_lock",

‎src/diogenes_git_sync.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
r"^https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(?:\.git)?$"
1616
)
1717
BRANCH_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$")
18+
REMOTE_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
1819

1920

2021
def _run(argv: list[str], root: Path) -> subprocess.CompletedProcess[str]:
@@ -38,7 +39,7 @@ def _value(root: Path, *args: str) -> str:
3839
return result.stdout.strip()
3940

4041

41-
def sync(root: Path, source: str, branch: str) -> str:
42+
def sync(root: Path, source: str, branch: str, remote_name: str = "origin") -> str:
4243
root = root.expanduser()
4344
if not root.is_absolute():
4445
raise RuntimeError("Git checkout path must be absolute")
@@ -52,14 +53,18 @@ def sync(root: Path, source: str, branch: str) -> str:
5253
or ".." in branch.split("/")
5354
):
5455
raise RuntimeError("Declared Git branch is invalid")
56+
if not REMOTE_RE.fullmatch(remote_name):
57+
raise RuntimeError("Declared Git remote is invalid")
5558
current_branch = _value(root, "branch", "--show-current")
5659
if current_branch != branch:
5760
raise RuntimeError(
5861
f"Checkout is on {current_branch or 'detached HEAD'}, expected {branch}"
5962
)
60-
origin = _value(root, "remote", "get-url", "origin")
61-
if canonical_git_remote(origin) != canonical_git_remote(source):
62-
raise RuntimeError("Checkout origin does not match the declared source")
63+
remote_url = _value(root, "remote", "get-url", remote_name)
64+
if canonical_git_remote(remote_url) != canonical_git_remote(source):
65+
raise RuntimeError(
66+
f"Checkout remote {remote_name} does not match the declared source"
67+
)
6368
dirty = _value(root, "status", "--porcelain", "--untracked-files=no")
6469
if dirty:
6570
raise RuntimeError(
@@ -85,7 +90,7 @@ def sync(root: Path, source: str, branch: str) -> str:
8590
if before.lower() == remote_commit:
8691
return f"Current at {before[:7]}. Nothing to do."
8792
fetched = _run(
88-
["git", "-C", str(root), "fetch", "--prune", "origin", branch],
93+
["git", "-C", str(root), "fetch", "--prune", remote_name, branch],
8994
root,
9095
)
9196
if fetched.returncode:
@@ -97,7 +102,7 @@ def sync(root: Path, source: str, branch: str) -> str:
97102
str(root),
98103
"merge",
99104
"--ff-only",
100-
f"origin/{branch}",
105+
f"{remote_name}/{branch}",
101106
],
102107
root,
103108
)
@@ -114,10 +119,11 @@ def main() -> int:
114119
parser = argparse.ArgumentParser()
115120
parser.add_argument("--root", type=Path, required=True)
116121
parser.add_argument("--source", required=True)
122+
parser.add_argument("--remote", default="origin")
117123
parser.add_argument("--branch", required=True)
118124
args = parser.parse_args()
119125
try:
120-
message = sync(args.root, args.source, args.branch)
126+
message = sync(args.root, args.source, args.branch, args.remote)
121127
except (OSError, RuntimeError, subprocess.SubprocessError) as exc:
122128
print(f"Update blocked: {exc}")
123129
return 1

‎src/ulysses_runtime_management.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,14 @@ def load_runtime_management(
289289
if raw.get("git_update"):
290290
source_url = str(raw.get("source_url") or "")
291291
source_branch = str(raw.get("source_branch") or "")
292+
source_remote = str(raw.get("source_remote") or "origin")
292293
if (
293294
not re.fullmatch(r"https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(?:\.git)?", source_url)
294295
or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._/-]{0,127}", source_branch)
295296
or ".." in source_branch.split("/")
297+
or not re.fullmatch(
298+
r"[A-Za-z0-9][A-Za-z0-9._-]{0,63}", source_remote
299+
)
296300
):
297301
raise RuntimeJobError(
298302
f"{runtime_id} Git update source is not pinned safely"
@@ -304,6 +308,8 @@ def load_runtime_management(
304308
repository_root=repo.resolve(),
305309
)
306310
item = dict(raw)
311+
if raw.get("git_update"):
312+
item["source_remote"] = source_remote
307313
item["resource_kind"] = resource_kind
308314
dependency_section = str(
309315
raw.get("dependency_section") or "Other"
@@ -766,18 +772,25 @@ def _git(root: Path) -> dict[str, Any]:
766772
"branch": None,
767773
"commit": None,
768774
"origin": None,
775+
"remotes": {},
769776
}
770777

771778
def value(*args: str) -> str:
772779
result = _run(["git", "-C", str(root), *args])
773780
return result.stdout.strip() if result.returncode == 0 else ""
774781

782+
remotes = {
783+
remote: value("remote", "get-url", remote)
784+
for remote in value("remote").splitlines()
785+
if remote
786+
}
775787
return {
776788
"present": True,
777789
"dirty": bool(value("status", "--porcelain", "--untracked-files=no")),
778790
"branch": value("branch", "--show-current") or None,
779791
"commit": value("rev-parse", "HEAD") or None,
780-
"origin": value("remote", "get-url", "origin") or None,
792+
"origin": remotes.get("origin") or None,
793+
"remotes": remotes,
781794
}
782795

783796

@@ -786,12 +799,17 @@ def _canonical_git_url(value: object) -> str:
786799

787800

788801
def _git_contract_matches(item: dict[str, Any], git: dict[str, Any]) -> bool:
802+
source_remote = str(item.get("source_remote") or "origin")
803+
remotes = git.get("remotes") if isinstance(git.get("remotes"), dict) else {}
804+
source_remote_url = remotes.get(source_remote)
805+
if source_remote == "origin" and not source_remote_url:
806+
source_remote_url = git.get("origin")
789807
return bool(
790808
item.get("git_update")
791809
and git.get("present")
792810
and not git.get("dirty")
793811
and git.get("branch") == item.get("source_branch")
794-
and _canonical_git_url(git.get("origin"))
812+
and _canonical_git_url(source_remote_url)
795813
== _canonical_git_url(item.get("source_url"))
796814
)
797815

@@ -810,6 +828,7 @@ def _git_remote_status(
810828
result = dict(git)
811829
result.update(
812830
{
831+
"source_remote": str(item.get("source_remote") or "origin"),
813832
"remote_commit": None,
814833
"ahead": None,
815834
"behind": None,
@@ -1093,6 +1112,8 @@ def _git_sync_step(
10931112
str(item["root"]),
10941113
"--source",
10951114
str(item["source_url"]),
1115+
"--remote",
1116+
str(item.get("source_remote") or "origin"),
10961117
"--branch",
10971118
str(item["source_branch"]),
10981119
],
@@ -2354,6 +2375,8 @@ def _steps(
23542375
"argv": [
23552376
"git",
23562377
"clone",
2378+
"--origin",
2379+
str(item.get("source_remote") or "origin"),
23572380
"--branch",
23582381
str(item["source_branch"]),
23592382
"--single-branch",

‎tests/test_diogenes_git_sync.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from __future__ import annotations
2+
3+
import re
4+
import subprocess
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
import src.diogenes_git_sync as git_sync
10+
11+
12+
def _git(root: Path, *args: str) -> str:
13+
result = subprocess.run(
14+
["git", "-C", str(root), *args],
15+
capture_output=True,
16+
check=True,
17+
text=True,
18+
)
19+
return result.stdout.strip()
20+
21+
22+
def test_sync_uses_declared_named_remote(
23+
tmp_path: Path,
24+
monkeypatch: pytest.MonkeyPatch,
25+
) -> None:
26+
remote = tmp_path / "upstream.git"
27+
source = tmp_path / "source"
28+
checkout = tmp_path / "checkout"
29+
subprocess.run(["git", "init", "--bare", str(remote)], check=True)
30+
subprocess.run(["git", "init", "-b", "main", str(source)], check=True)
31+
_git(source, "config", "user.name", "Test")
32+
_git(source, "config", "user.email", "[email protected]")
33+
(source / "value.txt").write_text("one\n", encoding="utf-8")
34+
_git(source, "add", "value.txt")
35+
_git(source, "commit", "-m", "one")
36+
_git(source, "remote", "add", "upstream", str(remote))
37+
_git(source, "push", "-u", "upstream", "main")
38+
subprocess.run(
39+
[
40+
"git",
41+
"clone",
42+
"--origin",
43+
"upstream",
44+
"--branch",
45+
"main",
46+
str(remote),
47+
str(checkout),
48+
],
49+
check=True,
50+
)
51+
52+
(source / "value.txt").write_text("two\n", encoding="utf-8")
53+
_git(source, "add", "value.txt")
54+
_git(source, "commit", "-m", "two")
55+
_git(source, "push", "upstream", "main")
56+
57+
monkeypatch.setattr(git_sync, "SOURCE_RE", re.compile(r".+"))
58+
message = git_sync.sync(checkout, str(remote), "main", "upstream")
59+
60+
assert message.startswith("Updated ")
61+
assert (checkout / "value.txt").read_text(encoding="utf-8") == "two\n"
62+
assert _git(checkout, "remote", "get-url", "upstream") == str(remote)

‎tests/test_ulysses_runtime_management.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,52 @@ def fake_run(
734734
assert "--no-write-fetch-head" not in flattened
735735

736736

737+
def test_named_source_remote_drives_contract_and_sync_step(tmp_path: Path) -> None:
738+
source = "https://github.com/example/runtime.git"
739+
item = {
740+
"root": tmp_path,
741+
"git_update": True,
742+
"source_url": source,
743+
"source_remote": "upstream",
744+
"source_branch": "main",
745+
}
746+
git = {
747+
"present": True,
748+
"dirty": False,
749+
"branch": "main",
750+
"commit": "1" * 40,
751+
"origin": "https://github.com/example/fork.git",
752+
"remotes": {
753+
"fork": "https://github.com/example/fork.git",
754+
"upstream": source,
755+
},
756+
}
757+
758+
assert manager._git_contract_matches(item, git) is True
759+
step = manager._git_sync_step(item)
760+
remote_index = step["argv"].index("--remote")
761+
assert step["argv"][remote_index + 1] == "upstream"
762+
763+
764+
def test_fork_backed_catalog_declares_source_remotes(tmp_path: Path) -> None:
765+
items = {
766+
item["id"]: item
767+
for item in manager.load_runtime_management(
768+
home=tmp_path,
769+
services_root=tmp_path / "services",
770+
)
771+
}
772+
773+
assert items["camofox.browser"]["source_remote"] == "upstream"
774+
assert items["camofox.mcp"]["source_remote"] == "upstream"
775+
assert items["hermes.workspace"]["source_remote"] == "upstream"
776+
assert items["context.mode.mcp"]["source_remote"] == "ildunari"
777+
assert items["camofox.browser"]["launch"][:2] == [
778+
"env",
779+
"BROWSER_IDLE_TIMEOUT_MS=0",
780+
]
781+
782+
737783
def test_port_only_process_at_a_missing_root_is_unmanaged(
738784
monkeypatch: pytest.MonkeyPatch,
739785
tmp_path: Path,

0 commit comments

Comments
 (0)