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.

Keep the last browser window focused when returning from Harness (#800)

Dee8278900cbdb6
diff --git a/os/root/usr/share/harness-os/labwc/rc.xml b/os/root/usr/share/harness-os/labwc/rc.xml
index 905d27484..f9cecac4c 100644
--- a/os/root/usr/share/harness-os/labwc/rc.xml
+++ b/os/root/usr/share/harness-os/labwc/rc.xml
@@ -11,7 +11,13 @@
<action name="If">
<query identifier="chromium*" />
<then><action name="ForEach"><query identifier="hn" /><then><action name="Raise" /><action name="Focus" /></then></action></then>
- <else><action name="ForEach"><query identifier="chromium*" /><then><action name="Raise" /><action name="Focus" /></then><none><action name="Execute" command="hn-browser" /></none></action></else>
+ <else>
+ <!-- ForEach visits front-to-back. Reverse once without changing focus,
+ then raise/focus back to the original order: the browser window
+ the user left must stay on top, even with several windows open. -->
+ <action name="ForEach"><query identifier="chromium*" /><then><action name="Raise" /></then></action>
+ <action name="ForEach"><query identifier="chromium*" /><then><action name="Raise" /><action name="Focus" /></then><none><action name="Execute" command="hn-browser" /></none></action>
+ </else>
</action>
</keybind>
<keybind key="W-Return"><action name="ForEach"><query identifier="hn" /><then><action name="Raise" /><action name="Focus" /></then></action></keybind>
diff --git a/os/tests/browser_guest.py b/os/tests/browser_guest.py
index 2de26a6e6..6c1df6687 100644
--- a/os/tests/browser_guest.py
+++ b/os/tests/browser_guest.py
@@ -11,6 +11,7 @@ label{display:block;margin-bottom:8px}input{font-size:24px;width:70%}</style>
<h1>Harness browser check</h1><label for="input">Keyboard</label>
<input id="input" autofocus autocomplete="off">
<script>
+document.title += ' ' + location.pathname;
const input = document.querySelector('input');
let previous = '';
function report() {
@@ -55,6 +56,11 @@ class Handler(BaseHTTPRequestHandler):
output.write(json.dumps(data) + '\n')
(ROOT / 'state.json.tmp').write_text(json.dumps(data))
(ROOT / 'state.json.tmp').replace(ROOT / 'state.json')
+ states_path = ROOT / 'states.json'
+ states = json.loads(states_path.read_text()) if states_path.exists() else {}
+ states[data['path']] = data
+ (ROOT / 'states.json.tmp').write_text(json.dumps(states))
+ (ROOT / 'states.json.tmp').replace(states_path)
self.send_response(204)
self.end_headers()
diff --git a/os/tests/browser_vm.py b/os/tests/browser_vm.py
index 03cc02541..cdd68bef0 100644
--- a/os/tests/browser_vm.py
+++ b/os/tests/browser_vm.py
@@ -70,15 +70,18 @@ def new_tab_ready(vm, name):
def state(vm, *, path=None, value=None, ready=False, timeout=10):
- expression = 'import json; d=json.load(open("/tmp/harness-browser-probe/state.json")); '
+ filename = 'states.json' if path is not None else 'state.json'
+ expression = 'import json; d=json.load(open("/tmp/harness-browser-probe/' + filename + '")); '
if path is not None:
+ expression += 'd=d[' + repr(path) + ']; '
expression += 'assert d["path"] == ' + repr(path) + '; '
if value is not None:
expression += 'assert d["input"] == ' + repr(value) + '; '
if ready:
expression += 'assert d["focused"] and d["inputFocused"] and d["outerWidth"] > 0; '
wait_command(vm, 'python3 -c ' + shlex.quote(expression), timeout)
- return json.loads(vm.read_file('/tmp/harness-browser-probe/state.json'))
+ data = json.loads(vm.read_file('/tmp/harness-browser-probe/' + filename))
+ return data[path] if path is not None else data
def page_fills_display(vm, name):
@@ -164,6 +167,10 @@ def check_browser(vm, result):
focused(vm, 'chromium', 'explicit-url-browser')
state(vm, path='/second')
page_fills_display(vm, 'explicit-url-full-display')
+ vm.click_word('first-window-input', 'Keyboard')
+ state(vm, path='/second', ready=True)
+ vm.type_probe('window-one')
+ state(vm, path='/second', value='window-one')
result['checks'].append('An explicit hn-browser URL raises the already running browser from behind Harness')
vm.keys('ctrl', 'n')
new_tab_ready(vm, 'new-browser-window')
@@ -172,6 +179,20 @@ def check_browser(vm, result):
vm.keys('ret')
state(vm, path='/new-window')
page_fills_display(vm, 'new-window-full-display')
+ vm.click_word('second-window-input', 'Keyboard')
+ state(vm, path='/new-window', ready=True)
+ vm.type_probe('window-two')
+ state(vm, path='/new-window', value='window-two')
+ vm.keys('meta_l', 'b')
+ focused(vm, 'hn', 'two-window-terminal')
+ terminal('with-two-browser-windows')
+ vm.keys('meta_l', 'b')
+ focused(vm, 'chromium', 'two-window-return', title='Harness browser check /new-window - Chromium')
+ state(vm, path='/new-window', value='window-two', ready=True)
+ vm.type_probe('-returned')
+ state(vm, path='/new-window', value='window-two-returned')
+ state(vm, path='/second', value='window-one')
+ result['checks'].append('With two browser windows open, Super+b returns to the window the user left and typing leaves the other window unchanged')
vm.keys('ctrl', 'shift', 'w')
focused(vm, 'chromium', 'original-browser-restored')
vm.keys('ctrl', 'shift', 'w')
@@ -263,7 +284,7 @@ def main():
raise
finally:
if vm.shell_ready:
- for name in ['events.jsonl', 'state.json', 'terminal-input']:
+ for name in ['events.jsonl', 'state.json', 'states.json', 'terminal-input']:
try:
(folder / name).write_bytes(vm.read_file('/tmp/harness-browser-probe/' + name))
except Exception: