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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
refactor(mobile): drop first-run environment provisioning
dee7efb7b5712af
diff --git a/mobile/lib/analytics/analytics_events.dart b/mobile/lib/analytics/analytics_events.dartindex 135a07629..e04a81582 100644--- a/mobile/lib/analytics/analytics_events.dart+++ b/mobile/lib/analytics/analytics_events.dart@@ -52,12 +52,6 @@ extension AnalyticsEvents on Analytics {/// The user signed out.void signedOut() => track('signed_out');- /// First-run provisioning finished. [ready] is false when a required step- /// could not be completed — which says how much of a fresh Mac this app can- /// actually set up on its own, otherwise only visible in a support thread.- void environmentPrepared({required bool ready}) =>- track('environment_prepared', params: {'ready': ready});-// --- Agents -------------------------------------------------------------//// How many people open the New agent dialog, how many of those finish it,diff --git a/mobile/lib/app_shell.dart b/mobile/lib/app_shell.dartindex 5547683c4..808334974 100644--- a/mobile/lib/app_shell.dart+++ b/mobile/lib/app_shell.dart@@ -231,11 +231,6 @@ class RootShell extends ConsumerWidget {// and again on success.AppStatus.bootstrapping =>app.signingIn ? signedOutScreen(app) : bootScreen(app),- // Provisioning a computer for the harness CLI — a state only a desktop build enters (see- // above). Named so the switch stays exhaustive, and drawn as the boot screen should it- // ever be reached.- AppStatus.checkingEnvironment ||- AppStatus.preparingEnvironment => bootScreen(app),AppStatus.unauthenticated => signedOutScreen(app),AppStatus.authenticated => authenticatedScreen(app),},diff --git a/mobile/lib/bootstrap/environment_provisioner.dart b/mobile/lib/bootstrap/environment_provisioner.dartdeleted file mode 100644index 6cf3c33cb..000000000--- a/mobile/lib/bootstrap/environment_provisioner.dart+++ /dev/null@@ -1,1613 +0,0 @@-import '../core/host_platform.dart';--import 'dart:async';-import 'dart:convert';-import 'dart:io';--import '../core/harness_cli_runner.dart';--/// The CLI-only installer contract for callers that already own host setup.-/// Desktop verifies system tools, the active Linux clipboard helper and tmux-/// before reaching this command, then performs its own complete verification-/// again after Harness lands.-const String kHarnessDesktopInstallCommand =- 'curl -fsSL https://cdn.autonomous.ai/harness/cli/install.sh | '- '/bin/sh -s -- --desktop';--const int _linuxClockSyncFailureExitCode = 31;-const int _linuxAptUpdateFailureExitCode = 32;-const int _linuxAptInstallFailureExitCode = 33;-const String _linuxClockRepairCommand =- 'if command -v chronyc >/dev/null 2>&1; then '- 'sudo chronyc makestep; else sudo timedatectl set-ntp true && '- 'sudo systemctl restart systemd-timesyncd; fi';--enum EnvironmentStep {- clipboard,- harness,- tmux;-- /// Harness Desktop is only ready when every command in this list works.- bool get isRequired => true;-}--enum EnvironmentSetupPhase {- preflight,- review,- chooseMethod,- installing,- waitingForTerminal,- verifying,- ready,- failed,-}--enum EnvironmentSetupMode { automatic, manual }--/// Identifies which host setup owns a visible Terminal handoff. Linux can wait-/// on base/clipboard packages even when tmux itself is already ready, so a-/// resumed failure must not always be attributed to the tmux row.-enum EnvironmentTerminalSetup { linuxHost, tmux }--class EnvironmentFailure {- final EnvironmentStep? step;- final String title;- final String detail;- final String? command;- final int? exitCode;-- const EnvironmentFailure({- this.step,- required this.title,- required this.detail,- this.command,- this.exitCode,- });-}--enum EnvironmentStepStatus {- pending,- running,- ready,- needsTerminal,- failed,-- /// This host has no native clipboard to prepare (for example, headless- /// Linux). Unlike [unavailable], this is a satisfied, non-blocking state.- notApplicable,-- /// The host does not support this dependency. All current steps are required,- /// so this status remains blocking.- unavailable,-}--class EnvironmentReadiness {- final Map<EnvironmentStep, EnvironmentStepStatus> steps;- final String? message;- final List<String> output;- final EnvironmentSetupPhase phase;- final EnvironmentSetupMode? mode;- final EnvironmentFailure? failure;- final String? terminalLogPath;- final String? terminalResultPath;- final EnvironmentTerminalSetup? terminalSetup;- final bool systemReady;- final bool? homebrewReady;- final bool? tmuxBinaryReady;- final List<String> missingLinuxPackages;-- const EnvironmentReadiness({- required this.steps,- this.message,- this.output = const [],- this.phase = EnvironmentSetupPhase.preflight,- this.mode,- this.failure,- this.terminalLogPath,- this.terminalResultPath,- this.terminalSetup,- this.systemReady = false,- this.homebrewReady,- this.tmuxBinaryReady,- this.missingLinuxPackages = const [],- });-- factory EnvironmentReadiness.initial() => EnvironmentReadiness(- steps: {- for (final step in EnvironmentStep.values)- step: EnvironmentStepStatus.pending,- },- );-- bool get isReady =>- systemReady &&- steps.values.every(- (status) =>- status == EnvironmentStepStatus.ready ||- status == EnvironmentStepStatus.notApplicable,- );-- bool get needsTerminal =>- phase == EnvironmentSetupPhase.waitingForTerminal ||- steps.values.any(- (status) => status == EnvironmentStepStatus.needsTerminal,- );-- EnvironmentReadiness copyWith({- Map<EnvironmentStep, EnvironmentStepStatus>? steps,- String? message,- List<String>? output,- EnvironmentSetupPhase? phase,- EnvironmentSetupMode? mode,- EnvironmentFailure? failure,- String? terminalLogPath,- String? terminalResultPath,- EnvironmentTerminalSetup? terminalSetup,- bool? systemReady,- bool? homebrewReady,- bool? tmuxBinaryReady,- List<String>? missingLinuxPackages,- bool clearFailure = false,- bool clearTerminalHandoff = false,- }) => EnvironmentReadiness(- steps: steps ?? this.steps,- message: message ?? this.message,- output: output ?? this.output,- phase: phase ?? this.phase,- mode: mode ?? this.mode,- failure: clearFailure ? null : failure ?? this.failure,- terminalLogPath: clearTerminalHandoff- ? null- : terminalLogPath ?? this.terminalLogPath,- terminalResultPath: clearTerminalHandoff- ? null- : terminalResultPath ?? this.terminalResultPath,- terminalSetup: clearTerminalHandoff- ? null- : terminalSetup ?? this.terminalSetup,- systemReady: systemReady ?? this.systemReady,- homebrewReady: homebrewReady ?? this.homebrewReady,- tmuxBinaryReady: tmuxBinaryReady ?? this.tmuxBinaryReady,- missingLinuxPackages: missingLinuxPackages ?? this.missingLinuxPackages,- );-}--typedef ProcessRunner = Future<ProcessResult> Function(- String executable,- List<String> arguments, {- Map<String, String>? environment,-});--typedef TerminalLauncher = Future<void> Function(String scriptPath);--typedef ProcessStarter = Future<Process> Function(- String executable,- List<String> arguments, {- Map<String, String>? environment,-});--/// The progress callback `ensureReady` builds for itself, named so the per-host paths below can be-/// separate methods rather than one very long function.-typedef EmitStep = void Function({- EnvironmentStep? step,- EnvironmentStepStatus? status,- String? message,- String? output,-});--Future<ProcessResult> _defaultRun(- String executable,- List<String> arguments, {- Map<String, String>? environment,-}) => Process.run(executable, arguments, environment: environment);--Future<Process> _defaultStart(- String executable,- List<String> arguments, {- Map<String, String>? environment,-}) => Process.start(executable, arguments, environment: environment);--/// macOS opens a real Terminal.app window. Linux has no single canonical-/// terminal, so this tries the Debian/Ubuntu `update-alternatives` target-/// first, then the two most common emulators, and gives up only if none of-/// them exist on the box.-Future<void> _defaultOpenTerminal(String scriptPath) async {- if (Platform.isMacOS) {- await Process.start('/usr/bin/open', ['-a', 'Terminal', scriptPath]);- return;- }- final candidates = <List<String>>[- ['x-terminal-emulator', '-e', scriptPath],- ['gnome-terminal', '--', scriptPath],- ['xterm', '-e', scriptPath],- ];- for (final candidate in candidates) {- try {- await Process.start(candidate.first, candidate.skip(1).toList());- return;- } on ProcessException {- continue;- }- }- throw StateError(- 'Could not find a terminal emulator to launch (tried x-terminal-emulator, '- 'gnome-terminal, xterm)',- );-}--/// Prepares the only system dependencies required by the desktop transport.-///-/// Node lives beneath [harnessHome] rather than in Homebrew/nvm/PATH. The-/// CLI launcher is written against that exact binary, so launching Harness-/// from Finder and from Terminal has identical runtime behavior.-class EnvironmentProvisioner {- final Directory harnessHome;- final ProcessRunner _run;- final ProcessStarter? _start;- final TerminalLauncher _openTerminal;- final bool _isMacOS;- final bool _isLinux;- final bool _isWindows;- final bool _forceMissingAppleDeveloperTools;- final Map<String, String> _platformEnvironment;-- EnvironmentProvisioner({- Directory? harnessHome,- ProcessRunner? run,- ProcessStarter? start,- TerminalLauncher? openTerminal,- bool? isMacOS,- bool? isLinux,- bool? isWindows,- bool? forceMissingAppleDeveloperTools,- Map<String, String>? platformEnvironment,- }) : harnessHome = harnessHome ?? Directory(_defaultHarnessHome()),- _run = run ?? _defaultRun,- _start = start ?? (run == null ? _defaultStart : null),- _openTerminal = openTerminal ?? _defaultOpenTerminal,- _isMacOS = isMacOS ?? Platform.isMacOS,- _isLinux = isLinux ?? Platform.isLinux,- _isWindows = isWindows ?? Platform.isWindows,- _platformEnvironment = platformEnvironment ?? Platform.environment,- _forceMissingAppleDeveloperTools =- forceMissingAppleDeveloperTools ??- const bool.fromEnvironment(- 'HARNESS_DESKTOP_FORCE_MISSING_APPLE_DEVELOPER_TOOLS',- );-- /// HOME, then USERPROFILE. Windows sets only the latter, so a launch from Explorer used to throw- /// out of this constructor before a single frame could render.- static String? userHome() {- final home = Platform.environment['HOME'];- if (home != null && home.isNotEmpty) return home;- final profile = Platform.environment['USERPROFILE'];- if (profile != null && profile.isNotEmpty) return profile;- return containerHome;- }-- static String _defaultHarnessHome() {- final home = userHome();- if (home == null) {- throw StateError('Could not resolve the current user home directory');- }- return '$home${Platform.pathSeparator}.harness';- }-- /// Checks the complete environment, and mutates it only when [install] is explicitly true.- Future<EnvironmentReadiness> ensureReady({- required void Function(EnvironmentReadiness value) onProgress,- EnvironmentReadiness? resumeFrom,- bool install = true,- EnvironmentSetupMode? mode,- }) async {- final retryingFailedSetup =- install && resumeFrom?.phase == EnvironmentSetupPhase.failed;- var state = (resumeFrom ?? EnvironmentReadiness.initial()).copyWith(- phase: install- ? EnvironmentSetupPhase.installing- : EnvironmentSetupPhase.preflight,- mode: mode,- clearFailure: true,- clearTerminalHandoff: retryingFailedSetup,- );- void emit({- EnvironmentStep? step,- EnvironmentStepStatus? status,- String? message,- String? output,- EnvironmentSetupPhase? phase,- EnvironmentFailure? failure,- String? terminalLogPath,- String? terminalResultPath,- EnvironmentTerminalSetup? terminalSetup,- bool? systemReady,- bool? homebrewReady,- bool? tmuxBinaryReady,- List<String>? missingLinuxPackages,- }) {- final next = Map<EnvironmentStep, EnvironmentStepStatus>.from(- state.steps,- );- if (step != null && status != null) next[step] = status;- final lines = [...state.output];- if (output != null && output.trim().isNotEmpty) {- if (output.startsWith('Terminal log:\n')) {- lines.removeWhere((line) => line.startsWith('Terminal log:\n'));- }- lines.add(output.trim());- if (lines.length > 200) lines.removeRange(0, lines.length - 200);- }- state = EnvironmentReadiness(- steps: next,- message: message ?? state.message,- output: lines,- phase: phase ?? state.phase,- mode: mode ?? state.mode,- failure: failure ?? state.failure,- terminalLogPath: terminalLogPath ?? state.terminalLogPath,- terminalResultPath: terminalResultPath ?? state.terminalResultPath,- terminalSetup: terminalSetup ?? state.terminalSetup,- systemReady: systemReady ?? state.systemReady,- homebrewReady: homebrewReady ?? state.homebrewReady,- tmuxBinaryReady: tmuxBinaryReady ?? state.tmuxBinaryReady,- missingLinuxPackages:- missingLinuxPackages ?? state.missingLinuxPackages,- );- onProgress(state);- }-- final previousTerminalLog = state.terminalLogPath;- var previousTerminalLogText = '';- EnvironmentTerminalSetup? completedTerminalSetup;- if (previousTerminalLog != null) {- try {- final text = await File(previousTerminalLog).readAsString();- previousTerminalLogText = text;- final snapshot = 'Terminal log:\n${text.trim()}';- if (text.trim().isNotEmpty && !state.output.contains(snapshot)) {- emit(output: snapshot);- }- } on FileSystemException {- // The terminal may not have created its log yet.- }- }- final previousTerminalResult = state.terminalResultPath;- var terminalResultPending = false;- if (previousTerminalResult != null) {- if (!await File(previousTerminalResult).exists()) {- terminalResultPending = true;- // `terminal.exit` is a handoff hint, not the source of truth. Some- // Linux terminal emulators keep the launched shell/window alive after- // apt has already finished, and an interrupted EXIT trap can omit the- // file entirely. Continue into the read-only command probes below so- // the 5-second poll (and the user's Recheck button) can observe that- // the host is actually ready instead of waiting forever for a file a- // cold app launch does not need either.- emit(- message: state.message ?? 'Complete the visible prompts in Terminal.',- phase: EnvironmentSetupPhase.waitingForTerminal,- );- }- try {- final exitCode = int.tryParse(- (await File(previousTerminalResult).readAsString()).trim(),- );- if (exitCode != null && exitCode != 0) {- final linuxHost =- state.terminalSetup == EnvironmentTerminalSetup.linuxHost;- final clipboardFailed =- linuxHost &&- state.steps[EnvironmentStep.clipboard] ==- EnvironmentStepStatus.needsTerminal;- final tmuxFailed =- state.steps[EnvironmentStep.tmux] ==- EnvironmentStepStatus.needsTerminal;- if (clipboardFailed) {- emit(- step: EnvironmentStep.clipboard,- status: EnvironmentStepStatus.failed,- );- }- if (tmuxFailed) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.failed,- );- }- final classifiedFailure = linuxHost- ? _classifiedLinuxPackageFailure(- exitCode,- previousTerminalLogText,- )- : null;- emit(- step: linuxHost ? null : EnvironmentStep.tmux,- status: linuxHost ? null : EnvironmentStepStatus.failed,- message: 'The Terminal setup exited with code $exitCode.',- phase: EnvironmentSetupPhase.failed,- failure: EnvironmentFailure(- step: !linuxHost- ? EnvironmentStep.tmux- : !state.systemReady- ? null- : clipboardFailed- ? EnvironmentStep.clipboard- : tmuxFailed- ? EnvironmentStep.tmux- : null,- title:- classifiedFailure?.title ??- 'System package installation failed',- detail:- classifiedFailure?.detail ??- 'Terminal exited with code $exitCode. Review the complete log below.',- command:- classifiedFailure?.command ??- (linuxHost- ? await _linuxHostManualCommand()- : _manualCommandFor(EnvironmentStep.tmux)),- exitCode: exitCode,- ),- );- return state;- }- if (exitCode == 0) completedTerminalSetup = state.terminalSetup;- } on FileSystemException {- // The result can disappear between exists() and readAsString(). The- // live dependency probes below remain the authoritative fallback.- }- }- onProgress(state);-- if (!_isMacOS && !_isLinux) {- if (_isWindows) {- await _verifyWindows(emit);- return state;- }- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.failed,- message: 'Automatic environment setup is currently available on macOS and Linux only.',- phase: EnvironmentSetupPhase.failed,- );- return state;- }-- try {- var linuxMissingBasePackages = <String>[];- String? linuxMissingClipboardPackage;- EnvironmentFailure? systemFailure;- if (_isMacOS) {- systemFailure = await _systemPreflightFailure();- } else {- if (!await _hasWritableHome()) {- systemFailure = const EnvironmentFailure(- title: 'Home directory is not writable',- detail: 'Harness needs to write ~/.harness and ~/.local/bin.',- );- } else {- linuxMissingBasePackages = await _missingLinuxBasePackages();- linuxMissingClipboardPackage = await _missingLinuxClipboardPackage();- }- }- if (systemFailure != null) {- emit(- message: systemFailure.detail,- phase: EnvironmentSetupPhase.failed,- failure: systemFailure,- );- return state;- }- var systemReady = _isMacOS- ? await _hasAppleDeveloperTools()- : linuxMissingBasePackages.isEmpty;- emit(- systemReady: systemReady,- missingLinuxPackages: _isLinux- ? <String>[- ...linuxMissingBasePackages,- ?linuxMissingClipboardPackage,- ]- : const [],- output: systemReady- ? '✓ required system tools · writable home'- : _isMacOS- ? '✗ Apple developer tools · xcrun --find clang'- : '✗ missing Linux base packages · ${linuxMissingBasePackages.join(', ')}',- );-- final clipboardApplicable = _isLinux && _linuxClipboardCommand() != null;- var clipboardReady =- !clipboardApplicable || linuxMissingClipboardPackage == null;- emit(- step: EnvironmentStep.clipboard,- status: clipboardApplicable- ? clipboardReady- ? EnvironmentStepStatus.ready- : EnvironmentStepStatus.failed- : EnvironmentStepStatus.notApplicable,- message: clipboardApplicable- ? clipboardReady- ? 'Native image clipboard is ready.'- : '$linuxMissingClipboardPackage is required for native image paste.'- : _isLinux- ? 'Native image clipboard is not applicable on a headless Linux host.'- : null,- output: clipboardApplicable- ? clipboardReady- ? '✓ native image clipboard · ${_linuxClipboardCommand()}'- : '✗ native image clipboard · $linuxMissingClipboardPackage'- : _isLinux- ? '– native image clipboard N/A (headless)'- : null,- );-- final homebrewReady = !_isMacOS || await _hasHomebrew();- var tmuxBinaryReady = await _hasTmux();- var tmuxReady =- tmuxBinaryReady && (!_isMacOS || (homebrewReady && systemReady));- emit(- step: EnvironmentStep.tmux,- status: tmuxReady- ? EnvironmentStepStatus.ready- : EnvironmentStepStatus.failed,- message: tmuxReady- ? (_isMacOS ? 'Homebrew and tmux are ready.' : 'tmux is ready.')- : _isMacOS && !systemReady- ? 'Apple developer tools are required.'- : _isMacOS && !homebrewReady- ? 'Homebrew is required.'- : 'tmux is required.',- output: tmuxReady- ? (_isMacOS ? '✓ Homebrew · tmux --version' : '✓ tmux --version')- : _isMacOS && !systemReady- ? '✗ Apple developer tools · xcrun --find clang'- : _isMacOS && !homebrewReady- ? '✗ Homebrew · brew --version'- : '✗ tmux --version',- homebrewReady: _isMacOS ? homebrewReady : null,- tmuxBinaryReady: tmuxBinaryReady,- );-- final harnessReady = await _hasHarness();- emit(- step: EnvironmentStep.harness,- status: harnessReady- ? EnvironmentStepStatus.ready- : EnvironmentStepStatus.failed,- message: harnessReady- ? 'Harness CLI and managed Node are ready.'- : 'Harness CLI or its managed Node runtime is missing.',- output: harnessReady- ? '✓ managed Node >= 20 · harness version'- : '✗ managed Node >= 20 · harness version',- );-- if (state.isReady) {- emit(- message: 'All required tools passed verification.',- phase: EnvironmentSetupPhase.ready,- );- return state;- }-- if (!install) {- final linuxMissingPackages = <String>[- ...linuxMissingBasePackages,- ?linuxMissingClipboardPackage,- ];- if (terminalResultPending &&- (!systemReady || !clipboardReady || !tmuxReady)) {- emit(- message:- state.message ?? 'Complete the visible prompts in Terminal.',- phase: EnvironmentSetupPhase.waitingForTerminal,- );- return state;- }- if (completedTerminalSetup == EnvironmentTerminalSetup.linuxHost &&- (linuxMissingPackages.isNotEmpty || !tmuxBinaryReady)) {- emit(- message: 'The Linux host dependency install finished, but verification still found missing packages.',- phase: EnvironmentSetupPhase.failed,- failure: EnvironmentFailure(- step: !systemReady- ? null- : linuxMissingClipboardPackage != null- ? EnvironmentStep.clipboard- : EnvironmentStep.tmux,- title: 'Host dependency verification failed',- detail:- 'Still missing: ${[...linuxMissingPackages, if (!tmuxBinaryReady) 'tmux'].join(', ')}. Review the Terminal log, then retry when ready.',- command: await _linuxHostManualCommand(),- ),- );- return state;- }- emit(- message: _isLinux && linuxMissingPackages.isNotEmpty- ? 'Linux host packages required: ${linuxMissingPackages.join(', ')}.'- : 'Review what Harness will install before continuing.',- phase: EnvironmentSetupPhase.review,- );- return state;- }-- // Strict dependency order: one host-package transaction (base tools,- // tmux and the active clipboard helper) -> managed Node/Harness.- if (_isLinux && (!systemReady || !clipboardReady || !tmuxBinaryReady)) {- var packages = <String>{- ...linuxMissingBasePackages,- ?linuxMissingClipboardPackage,- if (!tmuxBinaryReady) 'tmux',- }.toList();- if (!await _hasAptGet()) {- final command = await _linuxHostManualCommand();- emit(- message: 'Automatic Linux package installation supports apt-based distributions only.',- phase: EnvironmentSetupPhase.failed,- failure: EnvironmentFailure(- title: 'Unsupported Linux package manager',- detail:- 'Install ${packages.join(', ')} with this distribution\'s package manager, then recheck.',- command: command,- ),- );- return state;- }-- ProcessResult? backgroundInstall;- if (await _canInstallAptUnattended()) {- if (linuxMissingClipboardPackage != null) {- emit(- step: EnvironmentStep.clipboard,- status: EnvironmentStepStatus.running,- );- }- emit(- step: tmuxBinaryReady ? null : EnvironmentStep.tmux,- status: tmuxBinaryReady ? null : EnvironmentStepStatus.running,- message: 'Installing Linux system packages…',- );- try {- backgroundInstall = await _shellStreaming(- _linuxAptInstallCommand(packages, nonInteractiveSudo: true),- onOutput: (line) => emit(- message:- line.contains('Enabling automatic time synchronization')- ? 'Synchronizing the system clock before retrying Ubuntu packages…'- : null,- output: line,- ),- );- } catch (error) {- emit(output: 'Background Linux package install failed: $error');- }-- linuxMissingBasePackages = await _missingLinuxBasePackages();- linuxMissingClipboardPackage = await _missingLinuxClipboardPackage();- systemReady = linuxMissingBasePackages.isEmpty;- clipboardReady =- !clipboardApplicable || linuxMissingClipboardPackage == null;- tmuxBinaryReady = await _hasTmux();- tmuxReady = tmuxBinaryReady;- emit(- step: EnvironmentStep.clipboard,- status: clipboardApplicable- ? clipboardReady- ? EnvironmentStepStatus.ready- : EnvironmentStepStatus.failed- : EnvironmentStepStatus.notApplicable,- missingLinuxPackages: <String>[- ...linuxMissingBasePackages,- ?linuxMissingClipboardPackage,- ],- tmuxBinaryReady: tmuxBinaryReady,- );- final classifiedBackgroundFailure = backgroundInstall == null- ? null- : _classifiedLinuxPackageFailure(- backgroundInstall.exitCode,- _resultText(backgroundInstall),- );- if (classifiedBackgroundFailure != null) {- if (!tmuxReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.failed,- );- }- emit(- message: classifiedBackgroundFailure.detail,- phase: EnvironmentSetupPhase.failed,- systemReady: systemReady,- failure: EnvironmentFailure(- step: !systemReady- ? null- : !clipboardReady- ? EnvironmentStep.clipboard- : !tmuxReady- ? EnvironmentStep.tmux- : null,- title: classifiedBackgroundFailure.title,- detail: classifiedBackgroundFailure.detail,- command: classifiedBackgroundFailure.command,- exitCode: classifiedBackgroundFailure.exitCode,- ),- );- return state;- }- if (systemReady && clipboardReady && tmuxReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.ready,- message: 'Linux system packages and tmux are ready.',- output: '✓ Linux host dependencies installed and verified',- systemReady: true,- tmuxBinaryReady: true,- missingLinuxPackages: const [],- );- } else {- if (backgroundInstall != null) {- emit(- output: backgroundInstall.exitCode == 0- ? 'apt finished, but Linux host dependencies did not pass verification.'- : 'Background apt install exited ${backgroundInstall.exitCode}: '- '${_resultText(backgroundInstall)}',- );- }- packages = <String>{- ...linuxMissingBasePackages,- ?linuxMissingClipboardPackage,- if (!tmuxBinaryReady) 'tmux',- }.toList();- }- }-- if (!systemReady || !clipboardReady || !tmuxReady) {- final terminal = await _launchLinuxHostSetup(packages);- if (linuxMissingClipboardPackage != null) {- emit(- step: EnvironmentStep.clipboard,- status: EnvironmentStepStatus.needsTerminal,- );- }- if (!tmuxReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.needsTerminal,- );- }- emit(- message: 'Complete the visible Linux package prompts in Terminal. Harness never sees your password.',- output: backgroundInstall == null- ? 'Terminal opened to install Linux host dependencies.'- : 'Background install was incomplete; Terminal opened to finish Linux host dependencies.',- phase: EnvironmentSetupPhase.waitingForTerminal,- terminalLogPath: terminal.log.path,- terminalResultPath: terminal.result.path,- terminalSetup: EnvironmentTerminalSetup.linuxHost,- systemReady: systemReady,- );- return state;- }- }-- if (_isMacOS && systemReady && homebrewReady && !tmuxBinaryReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.running,- message: 'Installing tmux via Homebrew…',- );- ProcessResult? installResult;- try {- installResult = await _shellStreaming(- 'brew install tmux',- onOutput: (line) => emit(output: line),- );- } catch (error) {- emit(output: 'Background tmux install failed: $error');- }- tmuxReady = installResult?.exitCode == 0 && await _hasTmux();- if (tmuxReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.ready,- message: 'Homebrew and tmux are ready.',- output: '✓ tmux installed via Homebrew',- homebrewReady: true,- tmuxBinaryReady: true,- );- } else {- if (installResult != null) {- emit(- output: installResult.exitCode == 0- ? 'Homebrew finished, but tmux did not pass verification.'- : 'Background tmux install exited ${installResult.exitCode}: '- '${_resultText(installResult)}',- );- }- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.running,- message: 'tmux needs attention in Terminal…',- );- final terminal = await _launchTmuxSetup();- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.needsTerminal,- message: 'Complete the visible Homebrew prompts in Terminal. Harness never sees your password.',- output: 'Background install failed; Terminal opened to retry tmux.',- phase: EnvironmentSetupPhase.waitingForTerminal,- terminalLogPath: terminal.log.path,- terminalResultPath: terminal.result.path,- terminalSetup: EnvironmentTerminalSetup.tmux,- );- return state;- }- } else if (!tmuxReady) {- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.running,- message: 'Preparing tmux in a secure terminal…',- );- final terminal = await _launchTmuxSetup();- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.needsTerminal,- message: 'Complete any password or macOS prompts in Terminal. Harness never sees your password.',- output: _isMacOS- ? 'Terminal opened to install Homebrew and tmux.'- : 'Terminal opened to install tmux.',- phase: EnvironmentSetupPhase.waitingForTerminal,- terminalLogPath: terminal.log.path,- terminalResultPath: terminal.result.path,- terminalSetup: EnvironmentTerminalSetup.tmux,- );- return state;- }-- if (!harnessReady) {- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.running,- message: 'Installing managed Node and Harness CLI…',- );- await _ensureHarness((line) => emit(output: line));- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.ready,- output: '✓ Harness CLI ready',- );- }-- emit(- message: 'Verifying every required command…',- phase: EnvironmentSetupPhase.verifying,- );- if (_isLinux) {- final missingBase = await _missingLinuxBasePackages();- final missingClipboard = await _missingLinuxClipboardPackage();- final writableHome = await _hasWritableHome();- if (missingBase.isNotEmpty ||- missingClipboard != null ||- !writableHome) {- if (missingClipboard != null) {- emit(- step: EnvironmentStep.clipboard,- status: EnvironmentStepStatus.failed,- );- }- emit(- message:- 'Linux system dependencies did not pass final verification.',- phase: EnvironmentSetupPhase.failed,- systemReady: writableHome && missingBase.isEmpty,- failure: EnvironmentFailure(- title: 'Linux system verification failed',- detail: !writableHome- ? 'The home directory is not writable.'- : 'Still missing packages: ${[...missingBase, ?missingClipboard].join(', ')}',- command: await _linuxHostManualCommand(),- ),- );- return state;- }- }- final finalChecks = <EnvironmentStep, Future<bool> Function()>{- EnvironmentStep.tmux: _isTmuxEnvironmentReady,- EnvironmentStep.harness: _hasHarness,- };- for (final entry in finalChecks.entries) {- if (!await entry.value()) {- emit(step: entry.key, status: EnvironmentStepStatus.failed);- throw StateError(- '${entry.key.name} did not pass final version verification.',- );- }- }- emit(message: 'Environment ready.', phase: EnvironmentSetupPhase.ready);- return state;- } catch (error) {- final failed =- state.steps.entries- .where((entry) => entry.value == EnvironmentStepStatus.running)- .map((entry) => entry.key)- .firstOrNull ??- state.steps.entries- .where((entry) => entry.value == EnvironmentStepStatus.failed)- .map((entry) => entry.key)- .firstOrNull;- emit(- step: failed,- status: EnvironmentStepStatus.failed,- message: 'Environment setup failed: $error',- phase: EnvironmentSetupPhase.failed,- failure: EnvironmentFailure(- step: failed,- title: 'Setup could not finish',- detail: '$error',- command: failed == null ? null : _manualCommandFor(failed),- ),- );- return state;- }- }-- /// Windows gets a VERIFY pass rather than the install pass above.- ///- /// Every installer this class drives is a POSIX shell script — `install.sh` piped into `/bin/sh`,- /// a `zsh`/`bash` login shell for each probe, `brew`, `apt-get` — and none of it exists here. So- /// instead of refusing to boot a Windows box that is in fact provisioned, this checks what is- /// actually on it and names the command for whatever is missing. Windows packaging remains out- /// of scope for this release.- Future<void> _verifyWindows(EmitStep emit) async {- emit(- step: EnvironmentStep.clipboard,- status: EnvironmentStepStatus.notApplicable,- );- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.running,- message: 'Checking the Harness CLI…',- );- var harnessReady = false;- try {- final runner = HarnessCliRunner(- harnessHome: harnessHome,- runProcess: _run,- );- final status = await runner.run(['auth', 'status', '--json']);- harnessReady =- status.exitCode == 0 && (status.stdout as String).trim().isNotEmpty;- } on ProcessException {- harnessReady = false;- } on StateError {- // No managed node/cli.js pair recorded under ~/.harness yet.- harnessReady = false;- }- if (!harnessReady) {- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.failed,- message:- 'The Harness CLI is not installed for this user. There is no Windows installer yet — '- 'from a checkout of the CLI run: npm install && npm run bundle && '- 'bash scripts/install-cli.sh — then click Recheck.',- );- return;- }- emit(- step: EnvironmentStep.harness,- status: EnvironmentStepStatus.ready,- output: 'Harness CLI ready',- );-- emit(- step: EnvironmentStep.tmux,- status: EnvironmentStepStatus.unavailable,- output:- 'tmux does not exist on Windows — terminals come from Herdr instead.',- );- }-- Future<EnvironmentFailure?> _systemPreflightFailure() async {- final tools = _isMacOS- ? 'command -v sh zsh bash curl tar sed awk shasum >/dev/null'- : 'command -v sh bash curl tar sed awk sha256sum >/dev/null';- final base = await _shell(tools);- if (base.exitCode != 0) {- return EnvironmentFailure(- title: 'Required system tools are missing',- detail: _resultText(base).isEmpty- ? 'Harness needs curl, tar, sed, awk, checksum tools and a POSIX shell.'- : _resultText(base),- command: _isMacOS- ? 'xcode-select --install'- : 'sudo apt-get install -y bash curl tar sed gawk coreutils',- );- }- if (!await _hasWritableHome()) {- return const EnvironmentFailure(- title: 'Home directory is not writable',- detail: 'Harness needs to write ~/.harness and ~/.local/bin.',- );- }- return null;- }-- Future<bool> _hasWritableHome() async {- final writable = await _shell('test -w "\$HOME"');- return writable.exitCode == 0;- }-- /// Base packages the desktop needs before it can hand host setup to the- /// CLI-only installer. Native clipboard support is reported separately so- /// the UI can explain it without creating a second install transaction.- Future<List<String>> _missingLinuxBasePackages() async {- if (!_isLinux) return const [];- const commandPackages = <String, String>{- 'sh': 'dash',- 'bash': 'bash',- 'curl': 'curl',- 'tar': 'tar',- 'sed': 'sed',- 'awk': 'gawk',- 'sha256sum': 'coreutils',- };- final missing = <String>[];- for (final entry in commandPackages.entries) {- final probe = await _shell('command -v ${entry.key} >/dev/null 2>&1');- if (probe.exitCode != 0) missing.add(entry.value);- }- return missing.toSet().toList();- }-- /// Clipboard selection intentionally matches osClipboard.ts: Wayland wins- /// when both display variables exist, X11 is the fallback, and a headless- /// machine has no native clipboard requirement to satisfy.- Future<String?> _missingLinuxClipboardPackage() async {- if (!_isLinux) return null;- final clipboardCommand = _linuxClipboardCommand();- final clipboardPackage = _linuxClipboardPackage();- if (clipboardCommand != null && clipboardPackage != null) {- final probe = await _shell(- 'command -v $clipboardCommand >/dev/null 2>&1',- );- if (probe.exitCode != 0) return clipboardPackage;- }- return null;- }-- Future<List<String>> _missingLinuxHostPackages() async {- final packages = <String>{...await _missingLinuxBasePackages()};- final clipboard = await _missingLinuxClipboardPackage();- if (clipboard != null) packages.add(clipboard);- return packages.toList();- }-- String? _linuxClipboardCommand() {- if (!_isLinux) return null;- if ((_platformEnvironment['WAYLAND_DISPLAY'] ?? '').isNotEmpty) {- return 'wl-copy';- }- if ((_platformEnvironment['DISPLAY'] ?? '').isNotEmpty) return 'xclip';- return null;- }-- String? _linuxClipboardPackage() => switch (_linuxClipboardCommand()) {- 'wl-copy' => 'wl-clipboard',- 'xclip' => 'xclip',- _ => null,- };-- Future<bool> _hasAptGet() async {- final result = await _shell('command -v apt-get >/dev/null 2>&1');- return result.exitCode == 0;- }-- Future<bool> _canInstallAptUnattended() async {- final user = await _shell('id -u');- if (user.exitCode == 0 && '${user.stdout}'.trim() == '0') return true;- final sudo = await _shell(- 'command -v sudo >/dev/null 2>&1 && sudo -n true >/dev/null 2>&1',- );- return sudo.exitCode == 0;- }-- bool _isLinuxClockSkewOutput(String output) {- final normalized = output.toLowerCase();- return normalized.contains('is not valid yet') ||- normalized.contains('certificate is not yet valid');- }-- EnvironmentFailure? _classifiedLinuxPackageFailure(- int exitCode,- String output,- ) {- if (exitCode == _linuxClockSyncFailureExitCode ||- _isLinuxClockSkewOutput(output)) {- return const EnvironmentFailure(- title: 'System clock could not be synchronized',- detail: 'Ubuntu reports that repository metadata is in the future relative to this computer. Enable automatic time synchronization, confirm the clock is correct, then retry.',- command: _linuxClockRepairCommand,- exitCode: _linuxClockSyncFailureExitCode,- );- }- if (exitCode == _linuxAptUpdateFailureExitCode) {- return const EnvironmentFailure(- title: 'Package repository refresh failed',- detail: 'Ubuntu could not refresh its package indexes, so Harness stopped instead of retrying with stale package data.',- command: 'sudo apt-get update',- exitCode: _linuxAptUpdateFailureExitCode,- );- }- return null;- }-- String _linuxAptInstallCommand(- List<String> packages, {- required bool nonInteractiveSudo,- }) {- final names = packages.toSet().join(' ');- final sudoFlag = nonInteractiveSudo ? '-n ' : '';- return '''set -o pipefail-export LC_ALL=C-run_as_root() {- if [ "\$(id -u)" -eq 0 ]; then- "\$@"- elif command -v sudo >/dev/null 2>&1; then- sudo $sudoFlag"\$@"- else- echo 'Installing Linux system packages needs root access, but sudo is unavailable.' >&2- return 126- fi-}-apt_as_root() {- run_as_root env DEBIAN_FRONTEND=noninteractive apt-get "\$@"-}-repair_system_clock() {- if command -v chronyc >/dev/null 2>&1; then- echo 'Repository metadata is ahead of this computer. Enabling automatic time synchronization with chrony…'- run_as_root chronyc online >/dev/null 2>&1 || true- run_as_root chronyc burst 4/4 >/dev/null 2>&1 || true- clock_attempt=0- while :; do- chrony_tracking="\$(chronyc tracking 2>/dev/null || true)"- if printf '%s\n' "\$chrony_tracking" | grep -Eq 'Leap status[[:space:]]*:[[:space:]]*Normal'; then- if run_as_root chronyc makestep; then- echo 'System clock stepped with chrony.'- return 0- fi- echo 'chrony has a synchronized source but could not step the system clock.' >&2- break- fi- clock_attempt=\$((clock_attempt + 1))- if [ "\$clock_attempt" -ge 15 ]; then- echo 'chrony did not obtain a synchronized source within 30 seconds.' >&2- break- fi- sleep 2- done- fi- if ! command -v timedatectl >/dev/null 2>&1; then- echo 'System clock is behind repository metadata, but no usable time synchronization service was found.' >&2- return $_linuxClockSyncFailureExitCode- fi- echo 'Repository metadata is ahead of this computer. Enabling automatic time synchronization…'- run_as_root timedatectl set-ntp true || return $_linuxClockSyncFailureExitCode- if command -v systemctl >/dev/null 2>&1; then- run_as_root systemctl restart systemd-timesyncd >/dev/null 2>&1 || true- fi- clock_attempt=0- while [ "\$(timedatectl show -p NTPSynchronized --value 2>/dev/null || true)" != 'yes' ]; do- clock_attempt=\$((clock_attempt + 1))- if [ "\$clock_attempt" -ge 15 ]; then- echo 'Automatic time synchronization did not become ready within 30 seconds.' >&2- return $_linuxClockSyncFailureExitCode- fi- sleep 2- done- echo 'System clock synchronized.'-}-refresh_package_indexes() {- apt_update_log="\${TMPDIR:-/tmp}/harness-apt-update-\$\$.log"- if apt_as_root update 2>&1 | tee "\$apt_update_log"; then- rm -f "\$apt_update_log"- return 0- fi- if grep -Eiq 'is not valid yet|certificate is not yet valid' "\$apt_update_log"; then- rm -f "\$apt_update_log"- if ! repair_system_clock; then- echo 'System clock could not be synchronized automatically.' >&2- return $_linuxClockSyncFailureExitCode- fi- if ! apt_as_root update; then- echo 'Package repository refresh still failed after clock synchronization.' >&2- return $_linuxAptUpdateFailureExitCode- fi- return 0- fi- rm -f "\$apt_update_log"- echo 'Package repository refresh failed. Harness will not retry with stale package indexes.' >&2- return $_linuxAptUpdateFailureExitCode-}-install_linux_packages() {- apt_as_root install -y $names && return 0- echo 'Refreshing package indexes before retrying…'- refresh_package_indexes || return \$?- if ! apt_as_root install -y $names; then- echo 'Package installation still failed after a successful repository refresh.' >&2- return $_linuxAptInstallFailureExitCode- fi-}-if install_linux_packages; then- :-else- install_status=\$?- exit "\$install_status"-fi''';- }-- Future<String> _linuxHostManualCommand() async {- final packages = <String>{- ...await _missingLinuxHostPackages(),- if (!await _hasTmux()) 'tmux',- }.join(' ');- if (packages.isEmpty) return 'Recheck Linux system dependencies.';- if (!await _hasAptGet()) {- return 'Install with your distribution package manager: $packages';- }- return 'sudo apt-get install -y $packages';- }-- Future<bool> _hasHarness() async {- final currentNode = File('${harnessHome.path}/runtime/current-node');- if (!await currentNode.exists()) return false;- final nodePath = (await currentNode.readAsString()).trim();- if (nodePath.isEmpty || !File(nodePath).existsSync()) return false;- final runtimeRoot = '${harnessHome.absolute.path}/runtime/';- if (!File(nodePath).absolute.path.startsWith(runtimeRoot)) return false;- final node = await _run(nodePath, ['--version']);- if (node.exitCode != 0) return false;- final match = RegExp(r'^v?(\d+)').firstMatch('${node.stdout}'.trim());- if (match == null || int.parse(match.group(1)!) < 20) return false;- try {- final cli = File('${harnessHome.path}/cli/cli.js');- if (!await cli.exists()) return false;- final version = await _run(nodePath, [cli.path, 'version']);- return version.exitCode == 0;- } on ProcessException {- return false;- } on StateError {- return false;- }- }-- Future<void> _ensureHarness(void Function(String line) onOutput) async {- final runner = HarnessCliRunner(harnessHome: harnessHome, runProcess: _run);- if (await _hasHarness()) return;- // No interpreter is named here. install.sh provisions the same- // checksum-verified Node under `~/.harness/runtime` when the computer has- // none, records it in `current-node`, and bakes its absolute path into the- // `~/.local/bin/harness` launcher — which is what makes a Finder launch,- // where PATH is launchd's bare `/usr/bin:/bin:/usr/sbin:/sbin`, still find- // a Node. Doing it there rather than here keeps ONE implementation of that,- // shared with everyone who installs the CLI from a terminal.- final install = await _shellStreaming(- // Served off the CDN-fronted public bucket (autonomous-code: apps/web/scripts/cli-install.sh,- // published with `make upload-cli-install-sh`), not by the web app. The old web-app URL,- // https://harness.autonomous.ai/cli/install.sh, still redirects here, but pointing at the CDN- // URL directly avoids that extra hop.- // A stale URL is worse here than anywhere else: a 404 piped into bash still exits 0 (measured),- // so the `install.exitCode != 0` check below would pass and the failure would only surface as- // the confusing "CLI did not start after installation" a few lines further down.- 'set -e; $kHarnessDesktopInstallCommand',- onOutput: onOutput,- );- if (install.exitCode != 0) {- throw StateError(- 'Harness installer exited ${install.exitCode}: ${_resultText(install)}',- );- }- final verified = await runner.run(['version']);- if (verified.exitCode != 0 || !await _hasHarness()) {- throw StateError(- 'Harness CLI verification exited ${verified.exitCode}: ${_resultText(verified)}',- );- }- }-- Future<({File log, File result})> _launchLinuxHostSetup(- List<String> packages,- ) => _launchTerminalSetup(linuxPackages: packages);-- Future<({File log, File result})> _launchTmuxSetup() =>- _launchTerminalSetup();-- Future<({File log, File result})> _launchTerminalSetup({- List<String> linuxPackages = const [],- }) async {- // Package-manager installs can ask for a password. Always hand them to a- // visible OS terminal instead of guessing whether this particular run will- // prompt in a headless Process.run child.- final script = await _writeTerminalBootstrapScript(- linuxPackages: linuxPackages,- );- await _openTerminal(script.path);- return (- log: File('${script.parent.path}/terminal.log'),- result: File('${script.parent.path}/terminal.exit'),- );- }-- Future<bool> _hasTmux() async {- final result = await _shell('command -v tmux >/dev/null && tmux -V');- return result.exitCode == 0;- }-- Future<bool> _hasHomebrew() async {- final result = await _shell('command -v brew >/dev/null && brew --version');- return result.exitCode == 0;- }-- Future<bool> _isTmuxEnvironmentReady() async {- if (_isMacOS && !await _hasAppleDeveloperTools()) return false;- if (_isMacOS && !await _hasHomebrew()) return false;- return _hasTmux();- }-- /// `xcode-select -p` only proves that a path was selected. It also succeeds- /// for an incomplete or moved Command Line Tools directory. Resolve a tool- /// through xcrun so pre-flight reflects whether Homebrew can actually use- /// the selected Apple toolchain.- Future<bool> _hasAppleDeveloperTools() async {- if (_forceMissingAppleDeveloperTools) return false;- final result = await _shell(- 'command -v /usr/bin/xcrun >/dev/null 2>&1 && '- '/usr/bin/xcrun --find clang >/dev/null 2>&1',- );- return result.exitCode == 0;- }-- Future<File> _writeTerminalBootstrapScript({- List<String> linuxPackages = const [],- }) async {- final directory = Directory(- '${harnessHome.path}/desktop-app-v2/setup-runs/${DateTime.now().millisecondsSinceEpoch}',- );- await directory.create(recursive: true);- await _run('/bin/chmod', ['700', directory.path]);- if (_isMacOS) {- final script = File('${directory.path}/install-tmux.command');- await script.writeAsString('''#!/bin/zsh-set -e-umask 077-LOG_FILE="${directory.path}/terminal.log"-RESULT_FILE="${directory.path}/terminal.exit"-exec > >(tee -a "\$LOG_FILE") 2>&1-finish() {- status=\$?- printf '%s\\n' "\$status" > "\$RESULT_FILE"- if [ "\$status" -ne 0 ]; then- echo- echo 'System setup failed. Review the error above, then return to Harness.'- read -r '?Press Enter to close this window…' || true- fi- return "\$status"-}-trap finish EXIT-apple_developer_tools_ready() {- /usr/bin/xcrun --find clang >/dev/null 2>&1-}-if ! apple_developer_tools_ready; then- if [ -x /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild ]; then- echo 'Xcode is installed but is not the active developer directory.'- echo 'macOS may ask for your password to select it.'- sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer- else- echo 'Apple developer tools are missing. Installing Command Line Tools; finish the macOS dialog to continue.'- xcode-select --install || true- fi- attempts=0- until apple_developer_tools_ready; do- attempts=\$((attempts + 1))- if [ "\$attempts" -ge 200 ]; then- echo 'Apple developer tools did not become ready within 10 minutes.' >&2- echo 'Finish the macOS installer, then retry setup.' >&2- exit 12- fi- sleep 3- done-fi-if ! command -v brew >/dev/null 2>&1; then- echo 'Installing Homebrew (macOS may ask for your password)…'- /bin/bash -c "\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"-fi-eval "\$(/opt/homebrew/bin/brew shellenv 2>/dev/null || /usr/local/bin/brew shellenv)"-if ! command -v tmux >/dev/null 2>&1; then- brew install tmux-fi-echo 'tmux is ready. Return to Harness.'-''', flush: true);- await _run('/bin/chmod', ['700', script.path]);- return script;- }- final packageList = linuxPackages.isEmpty- ? const ['tmux']- : linuxPackages.toSet().toList();- final packages = packageList.join(' ');- const packageCommands = <String, String>{- 'dash': 'sh',- 'bash': 'bash',- 'curl': 'curl',- 'tar': 'tar',- 'sed': 'sed',- 'gawk': 'awk',- 'coreutils': 'sha256sum',- 'tmux': 'tmux',- 'xclip': 'xclip',- 'wl-clipboard': 'wl-copy',- };- final verification = packageList- .map((package) => packageCommands[package])- .whereType<String>()- .map(- (command) => command == 'tmux'- ? 'command -v tmux >/dev/null 2>&1 && tmux -V'- : 'command -v $command >/dev/null 2>&1',- )- .join('\n');- final aptInstallCommand = _linuxAptInstallCommand(- packageList,- nonInteractiveSudo: false,- );- final script = File('${directory.path}/install-linux-dependencies.sh');- await script.writeAsString('''#!/bin/bash-set -eu-umask 077-LOG_FILE="${directory.path}/terminal.log"-RESULT_FILE="${directory.path}/terminal.exit"-exec > >(tee -a "\$LOG_FILE") 2>&1--finish() {- status=\$?- printf '%s\\n' "\$status" > "\$RESULT_FILE"- if [ "\$status" -ne 0 ]; then- echo- echo 'Linux package installation failed. Review the error above, then try again.'- read -r -p 'Press Enter to close this window…' || true- fi- return "\$status"-}-trap finish EXIT--if command -v apt-get >/dev/null 2>&1; then- echo 'Installing Linux host dependencies (you may be asked for your password)…'- $aptInstallCommand- $verification- echo 'Installed packages: $packages'- echo 'Linux host dependencies are ready. This window will close automatically in 5 seconds.'- sleep 5-else- echo 'Automatic Linux package installation only supports apt-based distributions (Ubuntu/Debian).'- echo 'Install these packages with your distribution package manager: $packages'- exit 1-fi-''', flush: true);- await _run('/bin/chmod', ['700', script.path]);- return script;- }-- Future<ProcessResult> _shell(- String command, {- Map<String, String>? environment,- Duration? timeout,- }) {- final run = _run(_isMacOS ? '/bin/zsh' : '/bin/bash', [- '-l',- '-c',- // The Homebrew prefixes are named rather than trusted to be on PATH:- // `-l` is a LOGIN shell but not an interactive one, so it reads- // `~/.zprofile` and never `~/.zshrc`- // — which is where `brew shellenv` sits on plenty of machines. A Finder- // launch then starts from launchd's bare `/usr/bin:/bin:/usr/sbin:/sbin`- // and this probe reports tmux missing on a computer that has it, then- // "installs" it through whichever `brew` it can see. Measured: an Apple- // Silicon Mac with both Homebrews picked up Intel `/usr/local/bin/brew`- // and built openssl@3 from source under Rosetta, holding the boot open- // on "Checking tmux…" for as long as that took. Apple Silicon first, so- // a machine with both never installs through the Intel one. The CLI- // closed the same gap in `lib/tmuxOnPath.ts`.- 'export PATH="\$HOME/.local/bin${_isMacOS ? ':/opt/homebrew/bin:/usr/local/bin' : ''}:\$PATH"; $command',- ], environment: environment);- if (timeout == null) return run;- // The child keeps running — Process.run gives us no handle to kill. That is- // the intent: a slow install finishes in the background and the next launch- // finds it, while this one stops waiting.- return run.timeout(- timeout,- onTimeout: () => ProcessResult(0, 124, '', 'timed out after $timeout'),- );- }-- Future<ProcessResult> _shellStreaming(- String command, {- required void Function(String line) onOutput,- Duration? timeout,- }) async {- final shell = _isMacOS ? '/bin/zsh' : '/bin/bash';- final wrapped =- 'export PATH="\$HOME/.local/bin${_isMacOS ? ':/opt/homebrew/bin:/usr/local/bin' : ''}:\$PATH"; $command';- if (_start == null) {- final result = await _shell(command, timeout: timeout);- for (final line in '${result.stdout}\n${result.stderr}'.split('\n')) {- if (line.trim().isNotEmpty) onOutput(line);- }- return result;- }-- final process = await _start(shell, ['-l', '-c', wrapped]);- final stdoutLines = <String>[];- final stderrLines = <String>[];- final stdoutSubscription = process.stdout- .transform(utf8.decoder)- .transform(const LineSplitter())- .listen((line) {- stdoutLines.add(line);- onOutput(line);- });- final stderrSubscription = process.stderr- .transform(utf8.decoder)- .transform(const LineSplitter())- .listen((line) {- stderrLines.add(line);- onOutput(line);- });- final stdoutDone = stdoutSubscription.asFuture<void>();- final stderrDone = stderrSubscription.asFuture<void>();- var timedOut = false;- final exit = timeout == null- ? await process.exitCode- : await process.exitCode.timeout(- timeout,- onTimeout: () {- timedOut = true;- process.kill();- return 124;- },- );- if (timedOut) {- await stdoutSubscription.cancel();- await stderrSubscription.cancel();- } else {- await Future.wait([stdoutDone, stderrDone]);- }- return ProcessResult(- process.pid,- exit,- stdoutLines.join('\n'),- stderrLines.join('\n'),- );- }-- String _manualCommandFor(EnvironmentStep step) => switch (step) {- EnvironmentStep.clipboard =>- 'sudo apt-get install -y ${_linuxClipboardPackage() ?? 'xclip or wl-clipboard'}',- EnvironmentStep.tmux =>- _isMacOS- ? 'brew install tmux && tmux -V'- : 'sudo apt-get install -y tmux && tmux -V',- EnvironmentStep.harness =>- '$kHarnessDesktopInstallCommand && harness version',- };-- String _resultText(ProcessResult result) {- final text = '${result.stderr}\n${result.stdout}'.trim();- return text.length <= 700 ? text : text.substring(text.length - 700);- }-}--extension on Iterable<EnvironmentStep> {- EnvironmentStep? get firstOrNull => isEmpty ? null : first;-}diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dartindex daf5e2db0..1e8377713 100644--- a/mobile/lib/state/app_state.dart+++ b/mobile/lib/state/app_state.dart@@ -17,7 +17,6 @@ import '../auth/peer_link_client.dart';import '../auth/sign_in_client.dart';import '../auth/cli_link.dart';import '../auth/cli_login.dart';-import '../bootstrap/environment_provisioner.dart';import '../core/viewer_mode.dart';import '../core/config.dart';import '../core/agent_preference.dart';@@ -65,13 +64,7 @@ import '../usage/remote_usage.dart';import '../usage/usage_accounts.dart';import '../phone/phone_name_store.dart';-enum AppStatus {- bootstrapping,- checkingEnvironment,- preparingEnvironment,- unauthenticated,- authenticated,-}+enum AppStatus { bootstrapping, unauthenticated, authenticated }enum AgentLoadStatus { idle, needsLink, loading, loaded, error }@@ -349,7 +342,6 @@ class AppNotifier extends ChangeNotifier {/// default. Touch it earlier and it freezes the wrong `localCliBaseUrl`.late final LocalCliDiscovery _discovery =localCliDiscovery ?? LocalCliDiscovery(config: config);- final EnvironmentProvisioner? environmentProvisioner;final DesktopUpdater? desktopUpdater;@visibleForTestingfinal WsConn Function(String machineId)? connectionForTest;@@ -478,23 +470,6 @@ class AppNotifier extends ChangeNotifier {// Shown on the pre-navigation `bootstrapping` screen while [_finishBootstrapSignedIn] waits on the// local daemon — null the rest of the time, including once [status] flips to `authenticated`.String? _bootStatusMessage;- EnvironmentReadiness environmentReadiness = EnvironmentReadiness.initial();- bool _environmentSetupInFlight = false;- // Polls a step stuck in needsTerminal/failed every 5s (see `_scheduleEnvironmentRecheck`) so a user- // who fixes it by hand in their own terminal doesn't have to remember to click Recheck. A one-shot- // Timer that reschedules itself rather than `Timer.periodic`, so a slow recheck can't overlap with- // the next tick.- Timer? _environmentRecheckTimer;-- /// Whether a stuck step is being auto-polled right now — drives the "Checking automatically…"- /// caption on the desktop's `EnvironmentSetupScreen` alongside its Recheck button.- bool get environmentRecheckPending => _environmentRecheckTimer != null;-- /// Whether a provisioning run (initial or a per-step recheck) is in flight — lets the setup- /// screen disable its Recheck/Start over buttons and show a spinner instead of a second click- /// racing the first.- bool get environmentSetupInFlight => _environmentSetupInFlight;-// The daemon's own advertised local-ws endpoint — the dial target for EVERY machine's data plane// now, not just this computer's own one (see src/lib/remoteRelay.ts in the harness CLI repo: a// foreign machineId is relayed to backend transparently, so the app never dials backend directly).@@ -1069,7 +1044,6 @@ class AppNotifier extends ChangeNotifier {required AuthSession authSession,ConfigStore? configStore,this.localCliDiscovery,- this.environmentProvisioner,this.desktopUpdater,this.connectionForTest,SignInClient? cliLogin,@@ -1468,25 +1442,20 @@ class AppNotifier extends ChangeNotifier {api = _newApiClient();_skippedDesktopUpdateVersion = _store.skippedDesktopUpdateVersion;}- // A viewer installs nothing and is updated by its store: provisioning and the updater both- // serve a computer that runs the harness CLI. The updater is not merely useless there — it- // throws on an architecture it has no channel for (`ios_arm64`), from inside bootstrap.- if (viewer == null) {- _startUpdateChecking();- final environmentReady = await _prepareEnvironment();- if (!environmentReady) return;- }- await _continueAfterEnvironmentReady();+ // A viewer is updated by its store: the updater serves a computer that runs the harness CLI.+ // It is not merely useless there — it throws on an architecture it has no channel for+ // (`ios_arm64`), from inside bootstrap.+ if (viewer == null) _startUpdateChecking();+ await _checkSignIn();} catch (error, stack) {debugPrint('bootstrap: fallback to login after error: $error\n$stack');currentUser = null;status = AppStatus.unauthenticated;notifyListeners();} finally {- // A `finally` rather than a call per exit path: bootstrap resolves four- // ways (environment not ready, signed out, signed in, thrown) and the- // launch happened in all four. `retryEnvironmentSetup` re-enters here,- // which is why the event itself is once-per-launch.+ // A `finally` rather than a call per exit path: bootstrap resolves three+ // ways (signed out, signed in, thrown) and the launch happened in all+ // three._trackAppOpened();}}@@ -1507,99 +1476,13 @@ class AppNotifier extends ChangeNotifier {if (signedIn) _armFirstMessage('launch');}- /// Runs before we invoke a single Harness subcommand. A fresh mac used to- /// fail here with a generic Sign in error because `harness` and its Node- /// runtime were absent; provisioning now makes that a visible, recoverable- /// first-run phase instead.- Future<bool> _prepareEnvironment() async {- // A viewer has nothing to provision. The provisioner looks for a shell, the- // POSIX tools, tmux and a managed Node runtime under `~/.harness` — all of- // which exist to host the local `harness` CLI, which a viewer build does- // not have and does not want. Running it on a phone reported every step- // missing and parked the app on a setup screen whose two actions, Retry- // and Switch to Manual, could not succeed either. Treated as ready so- // bootstrap goes on to ask about sign-in, which a viewer can answer.- if (viewer != null) return true;- if (_environmentSetupInFlight) return false;- _environmentSetupInFlight = true;- status = AppStatus.checkingEnvironment;- environmentReadiness = EnvironmentReadiness.initial();- notifyListeners();- try {- // Always read-only here. Installation starts only after explicit confirmation in the wizard.- final result = await _runProvisioner(install: false);- if (!result.isReady) {- status = AppStatus.preparingEnvironment;- notifyListeners();- return false;- }- _cancelEnvironmentRecheckTimer();- return true;- } finally {- _environmentSetupInFlight = false;- }- }-- /// Shared with [_prepareEnvironment]: runs the provisioner, updates- /// [environmentReadiness] as it streams progress, and reports the outcome.- Future<EnvironmentReadiness> _runProvisioner({- EnvironmentReadiness? resumeFrom,- bool install = false,- EnvironmentSetupMode? mode,- bool quiet = false,- }) async {- final provisioner = environmentProvisioner ?? EnvironmentProvisioner();- final result = await provisioner.ensureReady(- onProgress: (value) {- if (quiet &&- value.phase != EnvironmentSetupPhase.ready &&- value.phase != EnvironmentSetupPhase.failed) {- // A 5-second Terminal poll must not repaint the wizard through- // preflight -> review -> waiting. Keep the stable handoff surface- // and only stream its diagnostics until there is a real terminal- // outcome or installation can continue.- environmentReadiness = environmentReadiness.copyWith(- output: value.output,- terminalLogPath: value.terminalLogPath,- terminalResultPath: value.terminalResultPath,- terminalSetup: value.terminalSetup,- systemReady: value.systemReady,- );- } else {- environmentReadiness = value;- }- // A successful probe belongs to the quiet pre-flight surface, never- // the installation wizard. This also prevents the setup screen from- // flashing its own ready phase for one frame after an install/recheck.- if (value.isReady) status = AppStatus.checkingEnvironment;- notifyListeners();- },- resumeFrom: resumeFrom,- install: install,- mode: mode,- );- environmentReadiness = result;- if (!quiet ||- result.isReady ||- result.phase == EnvironmentSetupPhase.failed) {- analytics.environmentPrepared(ready: result.isReady);- }- return result;- }-- /// What `bootstrap()` does right after the environment is confirmed ready — pulled out so- /// [recheckEnvironmentStep] can reach the same destination without repeating `bootstrap()`'s config- /// load and update-check startup, which already ran on the launch that got stuck here.- Future<void> _continueAfterEnvironmentReady() async {+ /// Whether this device is signed in, and everything behind the login screen when it is — what+ /// `bootstrap()` does once its config is loaded.+ Future<void> _checkSignIn() async {final revision = _authRevision;if (!_authWorkCurrent(revision)) return;- _cancelEnvironmentRecheckTimer();- // A viewer skipped the preflight (see [_prepareEnvironment]), so there is no- // preflight screen to hold while the sign-in is checked — it stays on the- // boot spinner instead.- status = viewer == null- ? AppStatus.checkingEnvironment- : AppStatus.bootstrapping;+ // The boot spinner holds while the sign-in is checked.+ status = AppStatus.bootstrapping;notifyListeners();// Auth now lives entirely with the local `harness` CLI — it owns the SSO session on disk and// refreshes it itself. This app never reads, stores, or refreshes a token of its own; it just@@ -1616,188 +1499,14 @@ class AppNotifier extends ChangeNotifier {notifyListeners();return;}- status = AppStatus.bootstrapping;- notifyListeners();await _finishBootstrapSignedIn();} catch (error, stack) {if (!_authWorkCurrent(revision)) return;- debugPrint(- 'continueAfterEnvironmentReady: fallback to login after error: '- '$error\n$stack',- );- currentUser = null;- status = AppStatus.unauthenticated;- notifyListeners();- }- }-- void showEnvironmentReview() {- environmentReadiness = environmentReadiness.copyWith(- phase: EnvironmentSetupPhase.review,- );- notifyListeners();- }-- void showEnvironmentMethodChoice() {- environmentReadiness = environmentReadiness.copyWith(- phase: EnvironmentSetupPhase.chooseMethod,- );- notifyListeners();- }-- void selectEnvironmentSetupMode(EnvironmentSetupMode mode) {- environmentReadiness = environmentReadiness.copyWith(mode: mode);- notifyListeners();- }-- Future<void> startEnvironmentSetup() async {- if (_environmentSetupInFlight) return;- _cancelEnvironmentRecheckTimer();- final mode = environmentReadiness.mode ?? EnvironmentSetupMode.automatic;- if (mode == EnvironmentSetupMode.manual) {- notifyListeners();- return;- }- _environmentSetupInFlight = true;- notifyListeners();- try {- final result = await _runProvisioner(- resumeFrom: environmentReadiness,- install: true,- mode: mode,- );- if (!result.isReady) {- _scheduleEnvironmentRecheck();- return;- }- await _continueAfterEnvironmentReady();- } finally {- _environmentSetupInFlight = false;- notifyListeners();- }- }-- Future<void> continueAfterEnvironmentSetup() async {- if (!environmentReadiness.isReady) return;- await _continueAfterEnvironmentReady();- }-- /// A manual repair always returns to a read-only probe.- Future<void> retryEnvironmentSetup() async {- if (_environmentSetupInFlight) return;- _environmentSetupInFlight = true;- notifyListeners();- try {- final result = await _runProvisioner(- install: false,- mode: environmentReadiness.mode,- );- if (result.isReady) await _continueAfterEnvironmentReady();- } finally {- _environmentSetupInFlight = false;- notifyListeners();- }- }-- /// Rechecks a single stuck step (`failed`/`needsTerminal`) without re-running steps already- /// `ready` — the user fixed it by hand (see `environment_step_guidance.dart`'s command) and this- /// confirms it, then falls through to whatever step comes next, exactly like a fresh `bootstrap()`- /// would have. [step] identifies which row's Recheck button was pressed; the provisioner itself- /// decides what to (re-)attempt from the current [environmentReadiness], so an already-resolved- /// step is never disturbed regardless of which row triggered this.- Future<void> recheckEnvironmentStep(EnvironmentStep step) async {- if (_environmentSetupInFlight) return;- _cancelEnvironmentRecheckTimer();- _environmentSetupInFlight = true;- notifyListeners();- try {- final visibleBeforeProbe = environmentReadiness;- final mode = environmentReadiness.mode;- var result = await _runProvisioner(- resumeFrom: environmentReadiness,- install: false,- mode: mode,- quiet:- mode == EnvironmentSetupMode.automatic &&- environmentReadiness.phase ==- EnvironmentSetupPhase.waitingForTerminal,- );- if (!result.isReady &&- mode == EnvironmentSetupMode.automatic &&- result.phase != EnvironmentSetupPhase.waitingForTerminal &&- result.systemReady &&- result.steps[EnvironmentStep.tmux] == EnvironmentStepStatus.ready &&- (result.steps[EnvironmentStep.clipboard] ==- EnvironmentStepStatus.ready ||- result.steps[EnvironmentStep.clipboard] ==- EnvironmentStepStatus.notApplicable)) {- result = await _runProvisioner(- resumeFrom: result,- install: true,- mode: mode,- );- }- if (!result.isReady) {- if (mode == EnvironmentSetupMode.automatic &&- result.phase != EnvironmentSetupPhase.failed) {- environmentReadiness = visibleBeforeProbe.copyWith(- phase: EnvironmentSetupPhase.waitingForTerminal,- output: result.output,- terminalLogPath: result.terminalLogPath,- terminalResultPath: result.terminalResultPath,- terminalSetup: result.terminalSetup,- systemReady: result.systemReady,- );- }- _scheduleEnvironmentRecheck();- return;- }- await _continueAfterEnvironmentReady();- } catch (error, stack) {- debugPrint(- 'recheckEnvironmentStep: fallback to login after error: $error\n$stack',- );+ debugPrint('checkSignIn: fallback to login after error: $error\n$stack');currentUser = null;status = AppStatus.unauthenticated;- } finally {- _environmentSetupInFlight = false;notifyListeners();- _trackAppOpened();- }- }-- /// Polls the currently-stuck required step every 5s (see `_environmentRecheckTimer`'s doc) so- /// fixing it in another window and forgetting to click Recheck still moves the app forward.- /// A no-op unless automatic setup is waiting on the real Terminal window.- void _scheduleEnvironmentRecheck() {- _environmentRecheckTimer?.cancel();- if (environmentReadiness.phase !=- EnvironmentSetupPhase.waitingForTerminal ||- environmentReadiness.mode != EnvironmentSetupMode.automatic) {- return;}- EnvironmentStep? stuck;- for (final entry in environmentReadiness.steps.entries) {- if (!entry.key.isRequired) continue;- if (entry.value == EnvironmentStepStatus.needsTerminal ||- entry.value == EnvironmentStepStatus.failed) {- stuck = entry.key;- break;- }- }- if (stuck == null && environmentReadiness.terminalSetup == null) return;- // Base system setup has no EnvironmentStep row of its own. The callback- // argument is only a UI trigger; the provisioner rechecks the complete- // environment and uses terminalSetup to attribute any failure.- final step = stuck ?? EnvironmentStep.tmux;- _environmentRecheckTimer = Timer(const Duration(seconds: 5), () {- unawaited(recheckEnvironmentStep(step));- });- }-- void _cancelEnvironmentRecheckTimer() {- _environmentRecheckTimer?.cancel();- _environmentRecheckTimer = null;}/// Both `bootstrap()` (already signed in) and `login()` (just finished signing in) land here once@@ -6931,7 +6640,6 @@ class AppNotifier extends ChangeNotifier {_closedHistory.clear();_daemonSupervisionTimer?.cancel();_updateCheckTimer?.cancel();- _environmentRecheckTimer?.cancel();_stopAllOfflineRetries();_stopAllLinkRetries();_stopAllAgentSyncTimers();diff --git a/mobile/test/app_state_sign_in_test.dart b/mobile/test/app_state_sign_in_test.dartindex e7e64c51e..d43798a06 100644--- a/mobile/test/app_state_sign_in_test.dart+++ b/mobile/test/app_state_sign_in_test.dart@@ -26,7 +26,7 @@ void main() {expect(rig.app.currentUser, isNull);});- test('signed in: machines and profile, with no environment step', () async {+ test('signed in: machines and profile, with no step between', () async {final rig = viewerApp();addTearDown(rig.app.dispose);rig.api.onMachines = () async => [remoteMachine('m')];@@ -37,9 +37,8 @@ void main() {await settle();expect(rig.app.status, AppStatus.authenticated);- // A viewer installs nothing: provisioning is never shown.- expect(seen, isNot(contains(AppStatus.checkingEnvironment)));- expect(seen, isNot(contains(AppStatus.preparingEnvironment)));+ // A viewer installs nothing: the boot spinner, then the app.+ expect(seen.toSet(), {AppStatus.bootstrapping, AppStatus.authenticated});expect(rig.app.machines.map((m) => m.machineId), ['m']);expect(rig.app.stateOf('m'), isNotNull);expect(rig.app.machinesLoading, isFalse);