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.

Harness OS updates find the new screen whichever hn server answers

bitcoinbuilder811b738e482a
diff --git a/os/live_update.py b/os/live_update.py
index 0ccd48ee0..8b682436e 100644
--- a/os/live_update.py
+++ b/os/live_update.py
@@ -535,6 +535,15 @@ def restart(cli_changed):
run('systemctl', '--user', 'is-active', '--quiet', 'hn-screen.service')
+def in_screen(pid):
+ """Whether this client runs in hn-screen.service: the OS screen, not SSH or this command."""
+ try:
+ return any(line.split(':', 2)[-1].endswith('/hn-screen.service')
+ for line in (PROC / str(pid) / 'cgroup').read_text().splitlines())
+ except OSError:
+ return False
+
+
def capture_view():
"""Remember the OS screen, without selecting an unrelated SSH client."""
try:
@@ -542,15 +551,7 @@ def capture_view():
'#{client_pid}\t#{session_id}\t#{window_id}\t#{pane_id}\t#{client_tty}', timeout=3)
for row in rows.splitlines():
parts = row.split('\t')
- if len(parts) != 5 or not parts[0].isdigit():
- continue
- groups = Path('/proc') / parts[0] / 'cgroup'
- try:
- belongs = any(line.split(':', 2)[-1].endswith('/hn-screen.service')
- for line in groups.read_text().splitlines())
- except OSError:
- continue
- if not belongs:
+ if len(parts) != 5 or not parts[0].isdigit() or not in_screen(parts[0]):
continue
if all(re.fullmatch(pattern, value) for pattern, value in
zip([r'\$\d+', r'@\d+', r'%\d+', r'/dev/pts/\d+'], parts[1:])):
@@ -584,17 +585,20 @@ def restore_view(view):
def screen_ready(target):
- """Wait for an attached client executing the selected binary, not just foot."""
+ """Wait for the screen's own client executing the selected binary, not just foot."""
deadline = time.monotonic() + 30
observed = {}
while time.monotonic() < deadline:
try:
- # list-clients deliberately includes the backing tmux connections.
- # hn-list-clients identifies hn's own rendering clients instead.
- clients = run('/usr/bin/hn', 'hn-list-clients', '-F', '#{client_pid}', timeout=3)
+ # hn-list-clients names hn's rendering clients, but has no rows when an hn started
+ # elsewhere first (over SSH, before anyone logged in at the screen): the screen's client
+ # then serves its own socket, and every update was judged failed and rolled back
+ # (2026-10-12, 2026-10-13). list-clients names it either way, beside the backing tmux
+ # connections and this command's own client; the screen's cgroup tells them apart.
+ clients = run('/usr/bin/hn', 'list-clients', '-F', '#{client_pid}', timeout=3)
for pid in clients.splitlines():
- if pid.isdigit():
- executable = Path('/proc') / pid / 'exe'
+ if pid.isdigit() and in_screen(pid):
+ executable = PROC / pid / 'exe'
observed[pid] = str(executable.resolve())
if executable.samefile(target / 'harness-tui'):
write(STATE / 'screen.json', {'pid': int(pid), 'executable': observed[pid], 'target': str(target)})
diff --git a/os/tests/test_live_update.py b/os/tests/test_live_update.py
index 3e838b23a..488bcf420 100644
--- a/os/tests/test_live_update.py
+++ b/os/tests/test_live_update.py
@@ -971,6 +971,13 @@ class RuntimeSupervision(unittest.TestCase):
with self.subTest(after=after), self.assertRaisesRegex(ValueError, 'not running under harness-daemon.service'):
self.restart([(None, None), after])
+ def test_the_unit_bounds_a_stop_and_kills_only_its_main_process(self):
+ # The restart above waits for the old core to stop; systemd's default 90 s held an update
+ # for all of it (2026-10-13). A kill must still spare the agents in tmux.
+ unit = (Path(__file__).parents[1] / 'root/usr/lib/systemd/user/harness-daemon.service').read_text().splitlines()
+ self.assertIn('TimeoutStopSec=10', unit)
+ self.assertIn('KillMode=process', unit)
+
def test_an_hn_only_change_leaves_the_daemon_alone(self):
self.assertEqual(self.restart([], cli_changed=False), self.SCREEN)
@@ -989,5 +996,69 @@ class RuntimeSupervision(unittest.TestCase):
self.assertEqual(ask(odd, ''), (None, None))
+class ScreenReadiness(unittest.TestCase):
+ """The new screen is the hn-screen.service client running the selected binary."""
+
+ SCREEN = '0::/user.slice/user-1000.slice/[email protected]/app.slice/hn-screen.service\n'
+ ELSEWHERE = '0::/user.slice/user-1000.slice/session-28.scope\n'
+
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(self.tmp.cleanup)
+ root = Path(self.tmp.name)
+ self.proc, self.state = root / 'proc', root / 'state'
+ self.state.mkdir()
+ self.new, self.old = root / 'new', root / 'old'
+ for build in (self.new, self.old):
+ build.mkdir()
+ (build / 'harness-tui').write_bytes(b'\x7fELF')
+ for patcher in (patch.object(update, 'PROC', self.proc), patch.object(update, 'STATE', self.state),
+ patch.object(update.time, 'sleep')):
+ patcher.start()
+ self.addCleanup(patcher.stop)
+ clock = iter(range(0, 1000, 5))
+ patcher = patch.object(update.time, 'monotonic', side_effect=lambda: next(clock))
+ patcher.start()
+ self.addCleanup(patcher.stop)
+
+ def client(self, pid, cgroup, build):
+ (self.proc / str(pid)).mkdir(parents=True)
+ (self.proc / str(pid) / 'cgroup').write_text(cgroup)
+ (self.proc / str(pid) / 'exe').symlink_to(build / 'harness-tui')
+
+ def ready(self, listed):
+ with patch.object(update, 'run', return_value=listed) as run:
+ update.screen_ready(self.new)
+ run.assert_called_with('/usr/bin/hn', 'list-clients', '-F', '#{client_pid}', timeout=3)
+ return update.read(self.state / 'screen.json')
+
+ def test_the_screens_client_on_the_new_binary_is_ready(self):
+ # This command's own hn client runs the new binary too; it is not the screen.
+ self.client(7479, '0::/user.slice/user-1000.slice/[email protected]/app.slice/run-u31.service\n', self.new)
+ self.client(5559, self.SCREEN, self.new)
+ self.assertEqual(self.ready('7479\n5559\n')['pid'], 5559)
+
+ def test_ready_when_an_hn_started_over_ssh_holds_the_server(self):
+ # hn-list-clients had no rows here (2026-10-13): the screen's client served itself.
+ self.client(3576, self.SCREEN, self.new)
+ self.assertEqual(self.ready('3576\n')['pid'], 3576)
+
+ def test_never_ready_on_a_client_outside_the_screen_or_the_old_binary(self):
+ self.client(880, self.ELSEWHERE, self.new)
+ self.client(5559, self.SCREEN, self.old)
+ with self.assertRaisesRegex(ValueError, 'did not become ready'):
+ self.ready('880\n5559\nnot-a-pid\n')
+ failed = update.read(self.state / 'screen.json')
+ self.assertEqual(failed['status'], 'failed')
+ self.assertEqual(failed['observed'], {'5559': str((self.old / 'harness-tui').resolve())})
+
+ def test_a_view_is_captured_only_from_the_screens_client(self):
+ self.client(880, self.ELSEWHERE, self.new)
+ self.client(5559, self.SCREEN, self.new)
+ rows = '880\t$9\t@9\t%9\t/dev/pts/0\n5559\t$1\t@2\t%3\t/dev/pts/2\n'
+ with patch.object(update, 'run', return_value=rows):
+ self.assertEqual(update.capture_view(), dict(session='$1', window='@2', pane='%3', tty='/dev/pts/2'))
+
+
if __name__ == '__main__':
unittest.main()