Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

os: bind graphical services to the Harness screen (#936)

Deeff76b8bb74a2
diff --git a/os/root/usr/lib/harness-os/session b/os/root/usr/lib/harness-os/session
index 3351de6ad..17107c1d0 100644
--- a/os/root/usr/lib/harness-os/session
+++ b/os/root/usr/lib/harness-os/session
@@ -34,14 +34,18 @@ compositor_config=/usr/share/harness-os/labwc
if [ -f /etc/harness-live ]; then
compositor_config=/usr/share/harness-os/labwc-install
fi
-if /usr/lib/harness-os/labwc -C "$compositor_config" > "$HOME/.local/state/harness-os/display.log" 2>&1; then
+display_status=0
+/usr/lib/harness-os/labwc -C "$compositor_config" > "$HOME/.local/state/harness-os/display.log" 2>&1 || display_status=$?
+# Also clean up when the compositor exits without running its shutdown hook.
+# The graphical target owns portals and screen services, never the agent runtime.
+systemctl --user stop harness-os.target || true
+if [ "$display_status" -eq 0 ]; then
exit 0
fi
restore_tty
trap - EXIT
# A missing/broken GPU driver must not strand the programmer at a blank screen.
# Stay in hn on the Linux console; an agent can install drivers and arrange a reboot.
-systemctl --user stop harness-os.target || true
if [ -f /etc/harness-live ]; then
exec sudo /usr/bin/python3 /usr/lib/harness-os/install.py --boot
fi
diff --git a/os/root/usr/lib/systemd/user/harness-os.target b/os/root/usr/lib/systemd/user/harness-os.target
index 96856b3b9..10ecf5b21 100644
--- a/os/root/usr/lib/systemd/user/harness-os.target
+++ b/os/root/usr/lib/systemd/user/harness-os.target
@@ -1,4 +1,7 @@
[Unit]
Description=The hn screen
+BindsTo=graphical-session.target
+# Portals can keep the standard target needed after our screen stops.
+PropagatesStopTo=graphical-session.target
Wants=hn-screen.service harness-install.service harness-idle.service harness-update.timer harness-gpu-check.timer
After=graphical-session-pre.target
diff --git a/os/tests/fedora_package_vm.py b/os/tests/fedora_package_vm.py
index 1f29d1fc6..d3e3a59e7 100644
--- a/os/tests/fedora_package_vm.py
+++ b/os/tests/fedora_package_vm.py
@@ -20,6 +20,7 @@ from arm_boot import digest
from arm_session import ROOT, SessionVM, failure_evidence, fixture_identity
from arm_update_vm import UserSession, update_identity
from fast_update_vm import exercise
+from graphical_session_vm import exercise as exercise_graphical_services
from fedora_package_lifecycle import identity
from session_vm import put
@@ -143,6 +144,7 @@ print(json.dumps({key:identity(value) for key,value in pids.items()},sort_keys=T
machine.keys('ret')
machine.wait_user('pgrep -u 1000 -x opencode >/dev/null', 60)
machine.frame('02-packaged-agent', ['OpenCode', 'Ask anything'], 90)
+ result['graphical_services'] = exercise_graphical_services(machine)
machine.command('rpm -V harness-os-session')
runtime = json.loads(machine.read_file('/usr/share/harness-os/runtime.json'))
assert runtime == final['runtime'], 'Boot did not use the RPM runtime identity'
diff --git a/os/tests/graphical_session_vm.py b/os/tests/graphical_session_vm.py
new file mode 100644
index 000000000..7ca68e691
--- /dev/null
+++ b/os/tests/graphical_session_vm.py
@@ -0,0 +1,45 @@
+"""Native VM regression: graphical services follow the screen, agents do not."""
+import json
+
+
+def processes(machine):
+ text, _ = machine.user("""python3 - <<'PY'
+import json, subprocess
+from pathlib import Path
+agent = subprocess.check_output(['pgrep', '-u', '1000', '-x', 'opencode'], text=True).split()
+daemon = subprocess.check_output(['systemctl', '--user', 'show', 'harness-daemon.service',
+ '--property=MainPID', '--value'], text=True).strip()
+assert agent and int(daemon) > 0
+print(json.dumps({pid: (Path('/proc') / pid / 'stat').read_text().rsplit(')', 1)[1].split()[19]
+ for pid in [*agent, daemon]}, sort_keys=True))
+PY""", timeout=15)
+ return json.loads(next(line for line in text.splitlines() if line.startswith('{')))
+
+
+def portal(machine):
+ machine.user('systemctl --user is-active --quiet harness-os.target graphical-session.target', timeout=15)
+ text, _ = machine.user('busctl --user --no-pager --timeout=10 get-property '
+ 'org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop '
+ 'org.freedesktop.portal.ScreenCast version', timeout=15)
+ assert any(line.startswith('u ') and int(line.split()[1]) > 0 for line in text.splitlines()), text
+ return text
+
+
+def exercise(machine):
+ before = processes(machine)
+ portal(machine)
+ machine.user('systemctl --user stop harness-os.target', timeout=30)
+ units = ['harness-os.target', 'graphical-session.target', 'xdg-desktop-portal.service',
+ 'xdg-desktop-portal-wlr.service']
+ machine.wait_user(' && '.join('test "$(systemctl --user is-active ' + unit + ')" = inactive'
+ for unit in units), 20)
+ screen, _ = machine.user('systemctl --user --no-pager show hn-screen.service '
+ '-p ActiveState -p MainPID -p Result', timeout=15)
+ assert 'MainPID=0' in screen, 'The old screen is still running'
+ assert processes(machine) == before, 'Stopping graphical services interrupted agent work'
+ machine.user('systemctl --user start harness-os.target', timeout=45)
+ portal(machine)
+ assert processes(machine) == before, 'Reconnecting the screen replaced agent work'
+ return {'status': 'passed', 'process_start_ticks': before, 'screen_stop': screen,
+ 'checks': ['Actual portal DBus activation', 'Screen and portals stop together',
+ 'Screen and portal reconnect with the same agent and daemon processes']}
diff --git a/os/tests/test_fedora_package.py b/os/tests/test_fedora_package.py
index e6e338db2..0ae36437f 100644
--- a/os/tests/test_fedora_package.py
+++ b/os/tests/test_fedora_package.py
@@ -55,7 +55,7 @@ class FedoraPayload(unittest.TestCase):
self.assertEqual((destination / 'usr/lib/harness/harness-tui').read_bytes(),
(self.runtime / 'harness-tui').read_bytes())
session = (destination / 'usr/lib/harness-os/session').read_text()
- self.assertIn('if labwc -C ', session)
+ self.assertIn('\nlabwc -C ', session)
self.assertNotIn('/usr/lib/harness-os/labwc -C', session)
self.assertNotIn('usr/lib/harness-os/labwc', result['files'])
@@ -76,6 +76,9 @@ class FedoraPayload(unittest.TestCase):
self.assertIn('W-u', (destination / 'usr/share/harness-os/labwc/rc.xml').read_text())
self.assertNotIn('W-i', (destination / 'usr/share/harness-os/labwc/rc.xml').read_text())
self.assertNotIn('harness-install', (destination / 'usr/lib/systemd/user/harness-os.target').read_text())
+ self.assertEqual((destination / 'usr/lib/systemd/user/harness-os.target').read_text(),
+ (ROOT / 'os/root/usr/lib/systemd/user/harness-os.target').read_text()
+ .replace(' harness-install.service', '').replace(' harness-gpu-check.timer', ''))
guide = (destination / 'usr/share/harness-os/AGENTS.md').read_text()
self.assertIn('Fedora', guide)
self.assertNotIn('sudo pacman', guide)
diff --git a/os/tests/test_graphical_session.py b/os/tests/test_graphical_session.py
new file mode 100644
index 000000000..a551c0b9d
--- /dev/null
+++ b/os/tests/test_graphical_session.py
@@ -0,0 +1,68 @@
+"""Session exit must release graphical services without ending agent work."""
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+
+ROOT = Path(__file__).resolve().parents[2]
+SESSION = ROOT / 'os/root/usr/lib/harness-os/session'
+
+
+class GraphicalSession(unittest.TestCase):
+ def exercise(self, display_status, stop_status=0):
+ with tempfile.TemporaryDirectory() as temporary:
+ home = Path(temporary)
+ commands = home / '.local/bin'
+ commands.mkdir(parents=True)
+ # Harness uses merged-/usr Linux; macOS keeps mkdir in /bin.
+ (commands / 'mkdir').symlink_to(shutil.which('mkdir'))
+ (home / 'projects').mkdir()
+ for name, body in {
+ 'labwc': 'echo display >> "$EVENTS"; exit "$DISPLAY_STATUS"',
+ 'systemctl': 'echo "systemctl $*" >> "$EVENTS"; '
+ 'if [ "$2" = stop ]; then exit "$STOP_STATUS"; fi',
+ 'python3': 'exit 0',
+ 'virtio-2d': 'exit 1',
+ 'stty': 'exit 1',
+ 'hn': 'echo terminal >> "$EVENTS"',
+ # End only the fixture fallback loop after its first terminal.
+ 'sleep': 'exit 71',
+ }.items():
+ path = commands / name
+ path.write_text('#!/bin/sh\n' + body + '\n')
+ path.chmod(0o755)
+ script = SESSION.read_text().replace('/usr/lib/harness-os/', str(commands) + '/')
+ script = script.replace('/usr/bin/python3', str(commands / 'python3'))
+ script = script.replace('/usr/bin/hn', str(commands / 'hn'))
+ script = script.replace('/etc/harness-live', str(home / 'not-live'))
+ events = home / 'events'
+ result = subprocess.run(['/bin/sh'], input=script, text=True, capture_output=True,
+ env=dict(os.environ, HOME=str(home), XDG_RUNTIME_DIR=str(home),
+ EVENTS=str(events), DISPLAY_STATUS=str(display_status),
+ STOP_STATUS=str(stop_status)), timeout=15)
+ self.assertTrue(events.exists(), f'{result.returncode}: {result.stderr}')
+ return result, events.read_text().splitlines()
+
+ def test_success_stops_graphical_services_without_stopping_agents(self):
+ result, events = self.exercise(0)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(events, ['display', 'systemctl --user stop harness-os.target'])
+
+ def test_compositor_failure_cleans_up_before_console_fallback(self):
+ result, events = self.exercise(139)
+ self.assertEqual(result.returncode, 71, result.stderr)
+ self.assertEqual(events, ['display', 'systemctl --user stop harness-os.target',
+ 'systemctl --user start harness-daemon.service', 'terminal'])
+ self.assertIn('continuing in hn on the console', result.stdout)
+
+ def test_unavailable_user_manager_does_not_prevent_fallback(self):
+ result, events = self.exercise(1, stop_status=1)
+ self.assertEqual(result.returncode, 71, result.stderr)
+ self.assertEqual(events[-1], 'terminal')
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/os/tools/fedora_payload.py b/os/tools/fedora_payload.py
index 8e388a2e3..6bf7f222f 100644
--- a/os/tools/fedora_payload.py
+++ b/os/tools/fedora_payload.py
@@ -55,7 +55,7 @@ def stage(source, runtime, destination, commit, runtime_commit, agent=None):
*['usr/lib/harness-os/' + name for name in
['session', 'session-settings.py', 'runtime-path', 'wait-runtime', 'virtio-2d', 'open-wifi', 'open-updates', 'screen-action', 'screenshot', 'lock', 'files']],
*['usr/lib/systemd/user/' + name for name in
- ['hn-screen.service', 'harness-daemon.service', 'harness-idle.service',
+ ['harness-os.target', 'hn-screen.service', 'harness-daemon.service', 'harness-idle.service',
'harness-update.service', 'harness-update.timer']],
]
for name in paths:
@@ -90,10 +90,8 @@ def stage(source, runtime, destination, commit, runtime_commit, agent=None):
'package_source_commit': commit, 'runtime_source_commit': runtime_commit,
'tui_reference': 'tui/README.md', 'tui_reference_source_commit': commit,
}, indent=2) + '\n')
- (destination / 'usr/lib/systemd/user/harness-os.target').write_text(
- '[Unit]\nDescription=The hn screen\n'
- 'Wants=hn-screen.service harness-idle.service harness-update.timer\n'
- 'After=graphical-session-pre.target\n')
+ target = destination / 'usr/lib/systemd/user/harness-os.target'
+ target.write_text(target.read_text().replace(' harness-install.service', '').replace(' harness-gpu-check.timer', ''))
config = destination / 'usr/share/harness-os/labwc/rc.xml'
tree = ET.parse(config)
keyboard = tree.getroot().find('keyboard')