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.

fix(wsl): paste Windows screenshots into agents, open viewers in the browser (#107, #108)

bubu-dudu082701974850
diff --git a/cli/src/lib/osClipboard.spec.ts b/cli/src/lib/osClipboard.spec.ts
new file mode 100644
index 000000000..cde9902f7
--- /dev/null
+++ b/cli/src/lib/osClipboard.spec.ts
@@ -0,0 +1,13 @@
+import { describe, expect, it } from 'vitest'
+import { isWsl } from './osClipboard.js'
+
+describe('isWsl', () => {
+ it('knows WSL by the distro WSL names, or by its kernel when that is unset', () => {
+ expect(isWsl({ WSL_DISTRO_NAME: 'Ubuntu' }, '6.8.0-45-generic')).toBe(true)
+ expect(isWsl({}, '5.15.167.4-microsoft-standard-WSL2')).toBe(true)
+ })
+
+ it('leaves a plain Linux machine alone', () => {
+ expect(isWsl({}, '6.8.0-45-generic')).toBe(false)
+ })
+})
diff --git a/cli/src/lib/osClipboard.ts b/cli/src/lib/osClipboard.ts
index 7921589df..1d6351fe7 100644
--- a/cli/src/lib/osClipboard.ts
+++ b/cli/src/lib/osClipboard.ts
@@ -7,6 +7,7 @@
* needed; no OS-level keystroke synthesis is involved.
*/
import { execFile, spawn } from 'node:child_process'
+import { release } from 'node:os'
import { binaryOnPath } from './binaryOnPath.js'
export type OsClipboardImageResult =
@@ -74,11 +75,22 @@ function writeMacClipboard(pngPath: string): Promise<OsClipboardImageResult> {
})
}
+/** Running inside Windows Subsystem for Linux: WSL sets `WSL_DISTRO_NAME` for every process it
+ * starts, and its kernels name themselves `…-microsoft-standard-WSL2` for one that was not. */
+export function isWsl(env: NodeJS.ProcessEnv = process.env, kernel: string = release()): boolean {
+ return !!env.WSL_DISTRO_NAME || /microsoft/i.test(kernel)
+}
+
/** Linux: which clipboard tool applies depends on the ACTIVE session, not the distro, so this is
* checked per-call rather than once at startup — `$WAYLAND_DISPLAY`/`$DISPLAY` reflect the
* session the daemon is currently running under. Neither set means no display server at all
* (a genuinely headless box), which no clipboard tool can fix. */
async function writeLinuxClipboard(pngBytes: Uint8Array): Promise<OsClipboardImageResult> {
+ // Under WSL the Linux clipboard is WSLg's mirror of the Windows one, and WSLg syncs an image back
+ // from Windows as BMP only (microsoft/wslg#833) — a PNG written here is replaced before the engine
+ // reads it, and Codex/Claude Code then find no image at all (openharness#107). The path fallback
+ // is the one that works there: both engines attach an image whose path is pasted.
+ if (isWsl()) return { state: 'unavailable', reason: 'WSL: WSLg replaces a PNG on the clipboard with BMP' }
if (process.env.WAYLAND_DISPLAY) {
if (!binaryOnPath('wl-copy')) {
return { state: 'unavailable', reason: 'wl-copy not found (install the wl-clipboard package)' }
diff --git a/desktop/lib/clipboard/native_clipboard.dart b/desktop/lib/clipboard/native_clipboard.dart
index 02fa3a257..5a7ad78d4 100644
--- a/desktop/lib/clipboard/native_clipboard.dart
+++ b/desktop/lib/clipboard/native_clipboard.dart
@@ -1,3 +1,7 @@
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import '../core/runtime_platform.dart';
@@ -23,11 +27,50 @@ class NativeClipboard {
/// today's text-paste behavior either way.
static Future<Uint8List?> readImagePng() async {
if (!RuntimePlatform.isMacOS && !RuntimePlatform.isLinux) return null;
+ Uint8List? bytes;
try {
- final bytes = await _channel.invokeMethod<Uint8List>('readImagePng');
- return bytes;
+ bytes = await _channel.invokeMethod<Uint8List>('readImagePng');
} on MissingPluginException {
- return null;
+ bytes = null;
+ } catch (_) {
+ bytes = null;
+ }
+ if ((bytes == null || bytes.isEmpty) && RuntimePlatform.isWsl) {
+ return readWindowsImagePng();
+ }
+ return bytes;
+ }
+
+ /// Under WSL, the WINDOWS clipboard's image as PNG, read through
+ /// `powershell.exe` (WSL interop). WSLg mirrors a Windows screenshot into
+ /// the Linux clipboard as BMP only, when it mirrors it at all
+ /// (microsoft/wslg#833, #236), so the GTK read above can come back empty
+ /// with a picture plainly on the Windows clipboard (openharness#107).
+ /// Null when there is no image, no interop, or it takes over five seconds.
+ @visibleForTesting
+ static Future<Uint8List?> Function() readWindowsImagePng =
+ _readWindowsImagePng;
+
+ static Future<Uint8List?> _readWindowsImagePng() async {
+ const script =
+ r'Add-Type -AssemblyName System.Windows.Forms,System.Drawing;'
+ r'$i=[System.Windows.Forms.Clipboard]::GetImage();'
+ r'if($i -eq $null){exit 1};'
+ r'$m=New-Object System.IO.MemoryStream;'
+ r'$i.Save($m,[System.Drawing.Imaging.ImageFormat]::Png);'
+ r'[Convert]::ToBase64String($m.ToArray())';
+ try {
+ final result = await Process.run('powershell.exe', [
+ '-NoProfile',
+ '-NonInteractive',
+ '-STA',
+ '-Command',
+ script,
+ ]).timeout(const Duration(seconds: 5));
+ if (result.exitCode != 0) return null;
+ final encoded = (result.stdout as String).trim();
+ if (encoded.isEmpty) return null;
+ return base64Decode(encoded);
} catch (_) {
return null;
}
diff --git a/desktop/lib/core/open_in_browser.dart b/desktop/lib/core/open_in_browser.dart
new file mode 100644
index 000000000..41c2f323e
--- /dev/null
+++ b/desktop/lib/core/open_in_browser.dart
@@ -0,0 +1,37 @@
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+import 'package:url_launcher/url_launcher.dart';
+
+import 'runtime_platform.dart';
+
+/// Opens an http(s) [url] in the person's browser. True when a browser was
+/// asked to; anything else (another scheme, nothing to open it with) is false.
+///
+/// Under WSL that is the WINDOWS browser, through `explorer.exe` (WSL
+/// interop): `url_launcher` would run the distro's `xdg-open`, and a WSL
+/// distro rarely has a browser of its own. A viewer on `127.0.0.1` inside WSL
+/// is still reachable from Windows through WSL's localhost forwarding, which
+/// is on by default. `explorer.exe` exits non-zero even when it opened the
+/// page, so starting it is the success signal there.
+Future<bool> openInBrowser(Uri url) =>
+ (url.scheme == 'http' || url.scheme == 'https') && url.host.isNotEmpty
+ ? browserOpener(url)
+ : Future.value(false);
+
+@visibleForTesting
+Future<bool> Function(Uri url) browserOpener = _open;
+
+Future<bool> _open(Uri url) async {
+ if (RuntimePlatform.isWsl) {
+ try {
+ await Process.start('explorer.exe', [
+ url.toString(),
+ ], mode: ProcessStartMode.detached);
+ return true;
+ } catch (_) {
+ // No interop (disabled in wsl.conf): fall through to the distro's own.
+ }
+ }
+ return launchUrl(url, mode: LaunchMode.externalApplication);
+}
diff --git a/desktop/lib/core/runtime_platform.dart b/desktop/lib/core/runtime_platform.dart
index e2696217a..788c70bd3 100644
--- a/desktop/lib/core/runtime_platform.dart
+++ b/desktop/lib/core/runtime_platform.dart
@@ -7,6 +7,15 @@ import 'package:flutter/foundation.dart';
abstract final class RuntimePlatform {
static bool get isMacOS => !kIsWeb && io.Platform.isMacOS;
static bool get isLinux => !kIsWeb && io.Platform.isLinux;
+
+ /// The Linux build running inside Windows Subsystem for Linux (WSLg). WSL
+ /// sets `WSL_DISTRO_NAME` for what it starts, and its kernels name
+ /// themselves `…-microsoft-standard-WSL2`. Not a supported platform — only
+ /// the places where WSLg behaves unlike a Linux desktop read this.
+ static bool get isWsl =>
+ isLinux &&
+ (environment['WSL_DISTRO_NAME'] != null ||
+ operatingSystemVersion.toLowerCase().contains('microsoft'));
static bool get isWindows => !kIsWeb && io.Platform.isWindows;
static bool get isIOS => !kIsWeb && io.Platform.isIOS;
static bool get isAndroid => !kIsWeb && io.Platform.isAndroid;
diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dart
index 3f0106174..16b815ff3 100644
--- a/desktop/lib/widgets/terminal_panel.dart
+++ b/desktop/lib/widgets/terminal_panel.dart
@@ -15,6 +15,7 @@ import 'package:xterm/xterm.dart';
import '../clipboard/native_clipboard.dart';
import '../core/models.dart';
+import '../core/runtime_platform.dart';
import '../state/app_state.dart';
import '../state/model_start_watch.dart';
@@ -1741,8 +1742,13 @@ class _TerminalPanelState extends State<TerminalPanel>
// quoted-insert, so the agent image-paste fallback would change its mode.
if (isTerminalEngine(target.engineId)) return;
final machine = widget.notifier.stateOf(target.machineId);
+ // A local engine reads its own clipboard on Ctrl-V — except under WSL,
+ // where WSLg leaves a Windows screenshot there as BMP or not at all, and
+ // Codex/Claude Code find no image (openharness#107). There the app reads
+ // the image itself and hands it to the daemon like a remote paste; the
+ // daemon, knowing it is on WSL too, pastes the saved file's path.
if (machine != null &&
- !machine.isLocalMachine &&
+ (!machine.isLocalMachine || RuntimePlatform.isWsl) &&
machine.terminalImagePasteAvailable) {
final imageBytes = await NativeClipboard.readImagePng();
if (!stillOwnsPaste()) return;
diff --git a/desktop/lib/widgets/web_pane_panel.dart b/desktop/lib/widgets/web_pane_panel.dart
index 6ebb499bb..71de024cd 100644
--- a/desktop/lib/widgets/web_pane_panel.dart
+++ b/desktop/lib/widgets/web_pane_panel.dart
@@ -4,6 +4,7 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
+import '../core/open_in_browser.dart';
import '../core/runtime_platform.dart';
import '../core/models.dart' show AgentVerdict;
import '../core/test_run.dart';
@@ -212,6 +213,15 @@ class _WebPanePanelState extends State<WebPanePanel> {
.catchError((_) {});
}
+ Future<void> _openInBrowser(Uri page) async {
+ if (await openInBrowser(page) || !mounted) return;
+ ScaffoldMessenger.maybeOf(context)?.showSnackBar(
+ const SnackBar(
+ content: Text('Could not open a browser. Copy the address instead.'),
+ ),
+ );
+ }
+
void _reload() {
if (_remote case final remote?) {
remote.reload();
@@ -352,11 +362,24 @@ class _WebPanePanelState extends State<WebPanePanel> {
final controller = _controller;
final url = widget.pane.url;
if (controller == null) {
+ // No embedded webview on this platform (it ships for macOS only — see
+ // [webviewAvailable]), so the page it would have shown opens in the
+ // browser instead of sitting here as text (openharness#108).
+ final page = url == null ? null : Uri.tryParse(url);
return _Notice(
key: const ValueKey('web-pane-placeholder'),
icon: LucideIcons.globe,
title: 'Viewer',
- detail: url ?? 'No viewer yet.',
+ detail: page == null
+ ? 'No viewer yet.'
+ : 'This viewer opens in your browser on this platform.\n$url',
+ action: page == null
+ ? null
+ : TextButton(
+ key: const ValueKey('web-pane-open-in-browser'),
+ onPressed: () => _openInBrowser(page),
+ child: const Text('Open in browser'),
+ ),
);
}
return Stack(
diff --git a/desktop/test/open_in_browser_test.dart b/desktop/test/open_in_browser_test.dart
new file mode 100644
index 000000000..4013aada5
--- /dev/null
+++ b/desktop/test/open_in_browser_test.dart
@@ -0,0 +1,32 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/open_in_browser.dart';
+
+void main() {
+ test('only an http(s) page with a host is handed to a browser', () async {
+ final opened = <Uri>[];
+ final previous = browserOpener;
+ browserOpener = (url) async {
+ opened.add(url);
+ return true;
+ };
+ addTearDown(() => browserOpener = previous);
+
+ expect(
+ await openInBrowser(Uri.parse('http://127.0.0.1:4179/?file=a')),
+ isTrue,
+ );
+ expect(await openInBrowser(Uri.parse('https://excalidraw.com')), isTrue);
+ for (final refused in [
+ 'file:///etc/passwd',
+ 'javascript:alert(1)',
+ 'ms-settings:privacy',
+ 'http:///no-host',
+ ]) {
+ expect(await openInBrowser(Uri.parse(refused)), isFalse, reason: refused);
+ }
+ expect(opened.map((u) => u.toString()), [
+ 'http://127.0.0.1:4179/?file=a',
+ 'https://excalidraw.com',
+ ]);
+ });
+}
diff --git a/desktop/test/web_pane_test.dart b/desktop/test/web_pane_test.dart
index 55b87c6fc..b982177c2 100644
--- a/desktop/test/web_pane_test.dart
+++ b/desktop/test/web_pane_test.dart
@@ -5,6 +5,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/open_in_browser.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/state/pane_arrangement.dart';
import 'package:harness/state/terminal_pane.dart';
@@ -324,7 +325,19 @@ void main() {
expect(WebPanePanel.webviewAvailable, isFalse);
expect(find.byType(WebPanePanel), findsOneWidget);
expect(find.byKey(const ValueKey('web-pane-placeholder')), findsOneWidget);
- expect(find.text('http://127.0.0.1:4179/'), findsOneWidget);
+ expect(find.textContaining('http://127.0.0.1:4179/'), findsOneWidget);
+ // With no embedded webview the page opens in the browser instead
+ // (openharness#108 — a Linux desktop, or WSLg, has none today).
+ final opened = <Uri>[];
+ final previousOpener = browserOpener;
+ browserOpener = (url) async {
+ opened.add(url);
+ return true;
+ };
+ addTearDown(() => browserOpener = previousOpener);
+ await tester.tap(find.byKey(const ValueKey('web-pane-open-in-browser')));
+ await tester.pump();
+ expect(opened, [Uri.parse('http://127.0.0.1:4179/')]);
// Its own close control, and no way to end an agent from it.
expect(find.byTooltip('Close viewer'), findsOneWidget);
// Stop remains a command, not a pane-header control. Viewer visibility