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.

36.9 KB
Raw Blame
import 'dart:async';
import 'dart:convert';

import 'package:harness/shared/theme/app_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:harness/terminal/terminal_text.dart';

import '../core/harness_file_store.dart';
import '../core/local_key_value_store.dart';
import '../core/test_run.dart';
import '../shared/theme/app_theme.dart' as grid;
import '../shared/theme/app_type.dart';
import '../widgets/box_chrome.dart';
import '../widgets/desktop_chrome.dart';
import '../widgets/terminal_prompt.dart';
import 'app_keymap.dart';
import 'app_shortcuts.dart';
import 'keymap.dart';
import 'keymap_commands.dart';
import 'keymap_host.dart';
import 'keymap_keyboard.dart';

class KeyboardLesson {
  const KeyboardLesson(
    this.command,
    this.label,
    this.group,
    this.context,
    this.bindings,
  );
  final String command, label, group;
  final KeymapContext context;
  final List<KeyBinding> bindings;
  String get id =>
      '${context.name}:$command:${bindings.map((b) => b.sequence).join('|')}';
  String get keys => bindings
      .map((binding) => boxKeyLabel(describeKeyBinding(binding)))
      .join(' / ');
  String get result => switch (command) {
    'swarm.new' => '[work]  [new tab]\nFind a harness, or create one',
    'agent.open' => '# Projects\nChoose a project to see its harnesses.',
    'agent.add' => '[harness 1] │ [harness 2]\nBoth harnesses share this tab.',
    'agent.new' => 'Codex · This Mac · payments\nHarness anything (optional)\nNew harness is selected. Return starts with these settings; Tab reaches each control. Model, approvals, and Codex profile are below the message.',
    'pane.zoom' => '[harness 2 — full workspace]\nPress the same key to restore the other panes.',
    'pane.close' => '[harness 1]\nClosing the last view saves idle work to disk. Working sessions ask when to stop.',
    'swarm.close' => '[previous tab]\nIdle sessions are saved and closed. Working sessions ask when to stop.',
    'navigation.commands' => '>rename\nRename Harness\nRename Tab',
    'terminal.find' => 'find > timeout\n1/3 matches in this terminal’s output',
    'picker.complete' => 'project  ~/work/payments\nTab completes the current argument; Enter accepts it.',
    'picker.complete_back' =>
      'project  ~/work/\nShift-Tab walks path candidates backward.',
    'picker.cancel' => '> ready\nEscape steps back from a nested chooser, then returns to the terminal.',
    'creation.agent' => 'agent    Claude Code\nFilter the agents, then Enter to choose. Escape returns to the launch menu.',
    'creation.project' => 'repo on This Mac\nresearch    website    payments\nNew project / Open folder / Clone GitHub repository\nType to filter repos. Up/Down selects; Enter chooses; Escape goes back.',
    'creation.project_new' => 'name     payments processing\nCreate payments-processing\nThis Mac:~/harnesses/payments-processing',
    'creation.project_existing' => 'Open folder\nChoose a folder on the selected machine. Cancelling returns to the Repo list.',
    'creation.project_repository' => 'repo     https://github.com/openai/codex\nChoose the repository, then launch to clone it and start the agent.',
    'creation.project_machine' => 'machine  Office\nChoose where the project lives. Folders are scoped to that machine.',
    'creation.project_browse' => 'Browse folders\nChoose a folder on the selected machine; cancelling returns to the previous chooser.',
    'creation.project_recent_1' ||
    'creation.project_recent_2' ||
    'creation.project_recent_3' ||
    'creation.project_recent_4' ||
    'creation.project_recent_5' ||
    'creation.project_recent_6' ||
    'creation.project_recent_7' ||
    'creation.project_recent_8' ||
    'creation.project_recent_9' => 'Repo selected\nThe composer now uses this folder on the selected machine.',
    'creation.task' => 'Harness anything\nEnter starts the harness. Shift-Enter adds a line; Escape keeps the draft.',
    'creation.options' =>
      'Model\nChoose a model for this harness. For Terminal, choose a repo.',
    'picker.more_options' => 'New harness\nOpen the composer from search. In the composer, choose a model; Terminal opens the Repo list.',
    'picker.toggle_preview' =>
      'results │ preview\nToggle again to hide the preview.',
    'agent.stop' => 'Stop Harness?\n> Cancel    Stop\nStopping ends the running harness; its saved conversation is kept. Switching tabs keeps work running.',
    'agent.restart' => 'Restart Harness\nRestarts the harness in the same pane and tries to resume its conversation.',
    'agent.fork' =>
      'Fork Harness\nChoose a name and first task. The source stays open.',
    'agent.clone' => 'Clone Harness\nAnother of this one opens beside it: same folder and settings, fresh conversation.',
    'app.store' =>
      '[Harness Store]\nFind a harness for the kind of work you want to do.',
    'pane.focus_left' ||
    'pane.focus_right' ||
    'pane.focus_above' ||
    'pane.focus_below' =>
      '$label\nThe caret moves to the neighboring pane. Start typing there.',
    'pane.resize' => 'resize > arrows  adjust    shift-arrows  larger steps\nTab changes divider. Escape returns to the terminal.',
    'pane.layout' => 'layout > columns    rows    grid\nKeep pressing the key to cycle layouts.',
    'edit.word' => 'task     fix the \nCtrl-W removes the previous word.',
    'edit.line' =>
      'task     \nCtrl-U removes text back to the start of this line.',
    'edit.restore' => 'task     fix the tests\nCtrl-Y restores the last text removed with Ctrl-W/U.',
    'edit.newline' => 'task     fix the tests\n         keep the patch small',
    'edit.chat_newline' => 'message  first line\n         second line\nEnter sends; Shift-Enter or Option-Enter adds a line.',
    'edit.backspace' =>
      'task     tes\nCtrl-H removes the character before the cursor.',
    'edit.delete' =>
      'task     tests\nCtrl-D removes the character after the cursor.',
    _ =>
      '$label\n${context.isPicker ? 'The focused prompt handles this key.' : 'The workspace handles this key; ordinary typing stays in the terminal.'}',
  };
}

/// The Swarm chat box writes this line itself, so it is no keymap command: no binding to move,
/// and nothing for the workspace to take (`SuperTabView` composer key handler).
final _chatBoxNewlineLesson = KeyboardLesson(
  'edit.chat_newline',
  'New line in a chat message',
  'Swarm chat box',
  KeymapContext.swarmChat,
  [
    for (final key in ['shift+enter', 'alt+enter'])
      KeyBinding(
        keys: [KeyStroke.parse(key)],
        command: 'edit.chat_newline',
        context: KeymapContext.swarmChat,
      ),
  ],
);

/// The catalog and live bindings are the source of truth, including aliases,
/// user sequences, unbindings, and terminal-only overrides.
List<KeyboardLesson> keyboardLessons(AppKeymap keymap) {
  const essentials = [
    'swarm.new',
    'agent.open',
    'agent.new',
    'pane.zoom',
    'navigation.commands',
    'terminal.find',
    'keyboard.help',
  ];
  const excluded = {
    'keyboard.practice',
    'keyboard.quick_start',
    'keyboard.pause_guide',
    'app.debug',
  };
  final result = <KeyboardLesson>[];
  for (final command in harnessCommands) {
    if (command.hidden ||
        excluded.contains(command.id) ||
        !harnessCommandActive(command.id)) {
      continue;
    }
    final context = command.context;
    final bindings = keymap.bindings(command.id, context: context).toList();
    if (command.id.startsWith('pane.focus_') &&
        RegExp(r'_[1-9]$').hasMatch(command.id) &&
        bindings.isEmpty &&
        keymap.bindings(command.id, context: KeymapContext.terminal).isEmpty) {
      continue;
    }
    final group = context.isPicker
        ? 'Search & creation'
        : essentials.contains(command.id)
        ? 'Essentials'
        : command.group.label;
    result.add(
      KeyboardLesson(command.id, command.label, group, context, bindings),
    );
    if (context == KeymapContext.workspace) {
      final terminal = keymap
          .bindings(command.id, context: KeymapContext.terminal)
          .toList();
      if (terminal.map((b) => b.sequence).join('|') !=
          bindings.map((b) => b.sequence).join('|')) {
        result.add(
          KeyboardLesson(
            command.id,
            command.label,
            'Agent input overrides',
            KeymapContext.terminal,
            terminal,
          ),
        );
      }
    }
  }
  for (final (id, label, key) in [
    ('word', 'Erase the previous word', 'ctrl+w'),
    ('line', 'Erase to the start of the line', 'ctrl+u'),
    ('restore', 'Restore erased text', 'ctrl+y'),
    ('newline', 'Add a task line', 'alt+enter'),
    ('backspace', 'Erase the previous character', 'ctrl+h'),
    ('delete', 'Erase the next character', 'ctrl+d'),
  ]) {
    if (keymap.current.match(KeymapContext.picker, [
      KeyStroke.parse(key),
    ]).matched) {
      continue;
    }
    result.add(
      KeyboardLesson(
        'edit.$id',
        label,
        'Prompt editing',
        KeymapContext.picker,
        [
          KeyBinding(
            keys: [KeyStroke.parse(key)],
            command: 'edit.$id',
            context: KeymapContext.picker,
          ),
        ],
      ),
    );
  }
  result.add(_chatBoxNewlineLesson);
  return [
    for (final id in essentials)
      ...result.where((lesson) => lesson.command == id),
    ...result.where((lesson) => !essentials.contains(lesson.command)),
  ];
}

Future<void> showKeyboardPractice(
  BuildContext context, {
  AppKeymap? keymap,
  KeyboardLesson? initialLesson,
}) => showTerminalPrompt<void>(
  context,
  keymap: keymap,
  builder: (context) => KeyboardPractice(
    keymap: keymap ?? KeymapTheme.of(context, listen: false),
    initialLesson: initialLesson,
    storage: kUnderTest ? null : HarnessFileStore.shared,
  ),
);

class KeyboardPractice extends StatefulWidget {
  const KeyboardPractice({
    super.key,
    this.keymap,
    this.storage,
    this.initialLesson,
  });
  final KeyboardLesson? initialLesson;
  final AppKeymap? keymap;
  final LocalKeyValueStore? storage;
  @override
  State<KeyboardPractice> createState() => _KeyboardPracticeState();
}

class _KeyboardPracticeState extends State<KeyboardPractice> {
  static const _storageKey = 'keyboard_practice_v1';
  final _fallback = AppKeymap();
  AppKeymap get map => widget.keymap ?? _fallback;
  final _filter = TextEditingController();
  final _answer = TextEditingController();
  final _filterFocus = FocusNode(debugLabel: 'Practice search');
  final _exerciseFocus = FocusNode(debugLabel: 'Practice shortcut');
  final _answerFocus = FocusNode(debugLabel: 'Practice command');
  final _scroll = ScrollController();
  final _detailScroll = ScrollController();
  final _done = <String>{};
  KeyboardLesson? _lesson;
  bool _matched = false;
  bool _loaded = false;
  String? _feedback;
  int _cursor = 0;
  Future<void> _saving = Future.value();

  List<KeyboardLesson> get lessons => keyboardLessons(map);
  List<KeyboardLesson> get filtered {
    final query = _filter.text.trim().toLowerCase();
    return lessons
        .where(
          (l) =>
              '${l.label} ${l.group} ${l.keys}'.toLowerCase().contains(query),
        )
        .toList();
  }

  @override
  void initState() {
    super.initState();
    map.addListener(_keysChanged);
    _lesson = widget.initialLesson;
    if (_lesson != null) _requestFocus();
    unawaited(_load());
  }

  Future<void> _load() async {
    try {
      final raw = await widget.storage?.read(_storageKey);
      if (!mounted || raw == null) return;
      final data = jsonDecode(raw);
      if (data is List) setState(() => _done.addAll(data.whereType<String>()));
    } catch (_) {
      // Practice still works when local preferences are unavailable.
    } finally {
      if (mounted) {
        _loaded = true;
        if (_done.isNotEmpty) _save();
      }
    }
  }

  void _save() {
    if (!_loaded) return;
    final saved = jsonEncode(_done.toList());
    _saving = _saving.then((_) async {
      try {
        await widget.storage?.write(_storageKey, saved);
      } catch (_) {}
    });
  }

  String _hint(String command) {
    final bindings = map.bindings(command, context: KeymapContext.picker);
    final binding =
        bindings.where((b) => b.custom).firstOrNull ?? bindings.firstOrNull;
    return binding == null ? 'click' : boxKeyLabel(describeKeyBinding(binding));
  }

  void _keysChanged() {
    final current = _lesson;
    setState(() {
      _lesson = current == null
          ? null
          : lessons
                .where(
                  (l) =>
                      l.command == current.command &&
                      l.context == current.context,
                )
                .firstOrNull;
      _matched = false;
      _feedback = null;
    });
    _requestFocus();
  }

  void _requestFocus() => WidgetsBinding.instance.addPostFrameCallback((_) {
    if (mounted) {
      if (_detailScroll.hasClients) _detailScroll.jumpTo(0);
      (_lesson == null
              ? _filterFocus
              : _lesson!.bindings.isEmpty && !_matched
              ? _answerFocus
              : _exerciseFocus)
          .requestFocus();
    }
  });

  void _open(KeyboardLesson lesson) {
    setState(() {
      _lesson = lesson;
      _matched = false;
      _feedback = null;
      _answer.clear();
    });
    _requestFocus();
  }

  void _openSelected() {
    final rows = filtered;
    if (rows.isNotEmpty) _open(rows[_cursor.clamp(0, rows.length - 1)]);
  }

  void _move(int delta) {
    final rows = filtered;
    if (rows.isEmpty) return;
    setState(() => _cursor = (_cursor + delta).clamp(0, rows.length - 1));
    if (_scroll.hasClients) {
      final height = 54.0 * appTextScaleOf(context);
      final top = _cursor * height, bottom = top + height;
      final at = _scroll.offset, view = _scroll.position.viewportDimension;
      if (top < at || bottom > at + view) {
        _scroll.jumpTo(
          (top < at ? top : bottom - view).clamp(
            0.0,
            _scroll.position.maxScrollExtent,
          ),
        );
      }
    }
  }

  void _back() {
    if (_lesson == null) {
      Navigator.of(context).pop();
    } else {
      setState(() {
        _lesson = null;
        _feedback = null;
      });
      _requestFocus();
    }
  }

  void _next() {
    final rows = filtered;
    final at = rows.indexWhere((l) => l.id == _lesson?.id);
    if (at >= 0 && at + 1 < rows.length) {
      _cursor = at + 1;
      _open(rows[at + 1]);
    } else {
      _back();
    }
  }

  void _read(int direction, {bool byLine = false}) {
    if (!_detailScroll.hasClients) return;
    final position = _detailScroll.position;
    _detailScroll.jumpTo(
      (position.pixels +
              direction *
                  (byLine
                      ? terminalCellSizeOf(context).height
                      : position.viewportDimension * .8))
          .clamp(0.0, position.maxScrollExtent),
    );
  }

  void _record(String id) {
    if (_lesson == null) return;
    if ((_matched || id != _lesson!.command) &&
        {
          'picker.page_down',
          'picker.page_up',
          'picker.preview_down',
          'picker.preview_up',
          'picker.preview_page_down',
          'picker.preview_page_up',
        }.contains(id)) {
      _read(
        id.endsWith('_down') ? 1 : -1,
        byLine: id == 'picker.preview_down' || id == 'picker.preview_up',
      );
      return;
    }
    if (id == 'picker.accept' &&
        !_exerciseFocus.hasPrimaryFocus &&
        !_answerFocus.hasFocus) {
      activatePromptControl();
      return;
    }
    if (_matched) {
      if (id == 'picker.accept') _next();
      if (id == 'picker.cancel') _back();
      return;
    }
    if (id != _lesson!.command) {
      if (id == 'picker.cancel') {
        _back();
        return;
      }
      if (id == 'picker.complete' || id == 'picker.complete_back') {
        id == 'picker.complete'
            ? FocusManager.instance.primaryFocus?.nextFocus()
            : FocusManager.instance.primaryFocus?.previousFocus();
        return;
      }
      setState(
        () => _feedback =
            'That is ${harnessCommandById[id]?.label ?? id}. Try ${_lesson!.keys}.',
      );
      return;
    }
    setState(() {
      _matched = true;
      _feedback = null;
      _done.add(_lesson!.id);
    });
    _save();
    _requestFocus();
  }

  void _submitName() {
    if (_answer.text.trim().toLowerCase() == _lesson!.label.toLowerCase()) {
      _record(_lesson!.command);
    } else {
      setState(() => _feedback = 'Type ${_lesson!.label}, then press Enter.');
    }
  }

  KeyEventResult _key(FocusNode node, KeyEvent event) {
    if (event is! KeyDownEvent) {
      return event is KeyRepeatEvent
          ? KeyEventResult.handled
          : KeyEventResult.ignored;
    }
    final stroke = keyStrokeForEvent(event);
    if (stroke == null) return KeyEventResult.ignored;
    final lesson = _lesson!;
    if (!_matched &&
        lesson.command.startsWith('edit.') &&
        lesson.bindings.any((b) => b.keys.single == stroke)) {
      _record(lesson.command);
      return KeyEventResult.handled;
    }
    if (stroke == const KeyStroke('escape')) {
      _back();
      return KeyEventResult.handled;
    }
    if (stroke == const KeyStroke('pagedown') ||
        stroke == const KeyStroke('pageup')) {
      _read(stroke == const KeyStroke('pagedown') ? 1 : -1);
      return KeyEventResult.handled;
    }
    if (stroke == const KeyStroke('enter')) {
      if (!_exerciseFocus.hasPrimaryFocus && !_answerFocus.hasFocus) {
        return KeyEventResult.ignored;
      }
      if (_matched) {
        _next();
      } else if (lesson.bindings.isEmpty) {
        _submitName();
      }
      return KeyEventResult.handled;
    }
    return KeyEventResult.ignored;
  }

  @override
  void dispose() {
    map.removeListener(_keysChanged);
    _fallback.dispose();
    _filter.dispose();
    _answer.dispose();
    _filterFocus.dispose();
    _exerciseFocus.dispose();
    _answerFocus.dispose();
    _scroll.dispose();
    _detailScroll.dispose();
    super.dispose();
  }

  InputDecoration _fieldDecoration(String hint, {bool search = false}) =>
      InputDecoration(
        hintText: hint,
        hintStyle: DesktopChrome.control(color: DesktopChrome.hint),
        prefixIcon: search
            ? Icon(AppIcons.search, size: 18, color: DesktopChrome.hint)
            : null,
        filled: true,
        fillColor: DesktopChrome.field,
        isDense: true,
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 12,
          vertical: 10,
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(DesktopChrome.controlRadius),
          borderSide: BorderSide(
            color: MediaQuery.highContrastOf(context)
                ? DesktopChrome.muted
                : DesktopChrome.rim,
            width: 1.5,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(DesktopChrome.controlRadius),
          borderSide: BorderSide(color: DesktopChrome.rim),
        ),
      );

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    TerminalFontScope.watch(context);
    final lesson = _lesson;
    final rows = filtered;
    final completedCount = lessons.where((l) => _done.contains(l.id)).length;
    final lessonCount = lessons.length;
    final body = DesktopChrome(
      child: DesktopDialogSurface(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              LayoutBuilder(
                builder: (context, constraints) => Wrap(
                  alignment: WrapAlignment.spaceBetween,
                  spacing: 16,
                  runSpacing: 4,
                  children: [
                    Semantics(
                      header: true,
                      child: Text(
                        'Keyboard practice',
                        style: DesktopChrome.heading(),
                      ),
                    ),
                    Semantics(
                      label:
                          '$completedCount of $lessonCount shortcuts practiced',
                      excludeSemantics: true,
                      child: Text(
                        constraints.maxWidth < 560 * appTextScaleOf(context)
                            ? '$completedCount of $lessonCount'
                            : '$completedCount/$lessonCount practiced',
                        style: DesktopChrome.metadata(),
                      ),
                    ),
                  ],
                ),
              ),
              if (lesson == null) ...[
                const SizedBox(height: 8),
                Text(
                  'Try shortcuts in a scratch workspace. Your agents stay as they are.',
                  style: DesktopChrome.metadata(),
                ),
              ],
              const SizedBox(height: 16),
              if (lesson == null) ...[
                TextField(
                  key: const ValueKey('practice-filter'),
                  controller: _filter,
                  focusNode: _filterFocus,
                  autofocus: true,
                  style: DesktopChrome.control(),
                  decoration: _fieldDecoration(
                    'Find a shortcut or a group',
                    search: true,
                  ),
                  onChanged: (_) => setState(() => _cursor = 0),
                ),
                const SizedBox(height: 12),
                Flexible(
                  child: rows.isEmpty
                      ? Padding(
                          padding: const EdgeInsets.symmetric(vertical: 16),
                          child: Text(
                            'No matching shortcuts.',
                            style: DesktopChrome.text(
                              color: DesktopChrome.muted,
                            ),
                          ),
                        )
                      : ListView.builder(
                          controller: _scroll,
                          shrinkWrap: true,
                          itemExtent: 54.0 * appTextScaleOf(context),
                          itemCount: rows.length,
                          itemBuilder: (context, i) {
                            final row = rows[i];
                            final selected =
                                i == _cursor.clamp(0, rows.length - 1);
                            final practiced = _done.contains(row.id);
                            return MergeSemantics(
                              child: Semantics(
                                key: ValueKey(
                                  'practice-${row.context.name}-${row.command}',
                                ),
                                button: true,
                                selected: selected,
                                label:
                                    '${row.label}, ${row.bindings.isEmpty ? 'command search' : row.keys}',
                                value: practiced
                                    ? 'Practiced'
                                    : 'Not practiced',
                                child: TextButton(
                                  onPressed: () => _open(row),
                                  style:
                                      TextButton.styleFrom(
                                        alignment: Alignment.centerLeft,
                                        padding: const EdgeInsets.symmetric(
                                          horizontal: 12,
                                          vertical: 5,
                                        ),
                                        backgroundColor: selected
                                            ? DesktopChrome.selection
                                            : Colors.transparent,
                                        shape: RoundedRectangleBorder(
                                          borderRadius: BorderRadius.circular(
                                            DesktopChrome.controlRadius,
                                          ),
                                        ),
                                        tapTargetSize:
                                            MaterialTapTargetSize.shrinkWrap,
                                        splashFactory: NoSplash.splashFactory,
                                      ).copyWith(
                                        side: WidgetStateProperty.resolveWith(
                                          (states) => BorderSide(
                                            width: 1.5,
                                            color:
                                                states.contains(
                                                  WidgetState.focused,
                                                )
                                                ? DesktopChrome.accent
                                                : Colors.transparent,
                                          ),
                                        ),
                                        overlayColor:
                                            WidgetStateProperty.resolveWith(
                                              (states) => DesktopChrome
                                                  .foreground
                                                  .withValues(
                                                    alpha:
                                                        states.contains(
                                                          WidgetState.pressed,
                                                        )
                                                        ? .12
                                                        : states.contains(
                                                            WidgetState.hovered,
                                                          )
                                                        ? .05
                                                        : 0,
                                                  ),
                                            ),
                                      ),
                                  child: ExcludeSemantics(
                                    child: Row(
                                      children: [
                                        Icon(
                                          practiced
                                              ? AppIcons.circleCheck
                                              : AppIcons.circle,
                                          size: 18,
                                          color: practiced
                                              ? DesktopChrome.accent
                                              : DesktopChrome.muted,
                                        ),
                                        const SizedBox(width: 12),
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text(
                                                row.label,
                                                maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                                style: DesktopChrome.control(),
                                              ),
                                              const SizedBox(height: 4),
                                              Text(
                                                '${row.group} · ${row.bindings.isEmpty ? 'command search' : row.keys}',
                                                maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                                style: DesktopChrome.metadata(),
                                              ),
                                            ],
                                          ),
                                        ),
                                        const SizedBox(width: 8),
                                        Icon(
                                          AppIcons.chevronRight,
                                          size: 18,
                                          color: DesktopChrome.muted,
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            );
                          },
                        ),
                ),
              ] else
                Flexible(
                  child: Scrollbar(
                    controller: _detailScroll,
                    thumbVisibility: true,
                    child: SingleChildScrollView(
                      controller: _detailScroll,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Text(lesson.group, style: DesktopChrome.metadata()),
                          const SizedBox(height: 4),
                          Semantics(
                            header: true,
                            child: Text(
                              lesson.label,
                              style: AppType.title(
                                color: DesktopChrome.foreground,
                              ),
                            ),
                          ),
                          const SizedBox(height: 16),
                          if (_matched)
                            Row(
                              children: [
                                Icon(
                                  AppIcons.circleCheck,
                                  color: DesktopChrome.accent,
                                  size: 20,
                                ),
                                const SizedBox(width: 8),
                                Expanded(
                                  child: Text(
                                    'Shortcut practiced',
                                    key: const ValueKey('practice-completed'),
                                    style: DesktopChrome.control(medium: true),
                                  ),
                                ),
                              ],
                            )
                          else
                            Text(
                              lesson.bindings.isEmpty
                                  ? 'No shortcut assigned. Find it by name in command search.'
                                  : 'Press ${lesson.keys}',
                              style: DesktopChrome.text(),
                            ),
                          if (!_matched && lesson.bindings.length > 1)
                            Padding(
                              padding: const EdgeInsets.only(top: 6),
                              child: Text(
                                'Try any of these bindings.',
                                style: DesktopChrome.metadata(),
                              ),
                            ),
                          if (!_matched && lesson.bindings.isEmpty)
                            Padding(
                              padding: const EdgeInsets.only(top: 12),
                              child: TextField(
                                key: const ValueKey('practice-command'),
                                controller: _answer,
                                focusNode: _answerFocus,
                                style: DesktopChrome.control(),
                                decoration: _fieldDecoration(lesson.label),
                              ),
                            ),
                          const SizedBox(height: 16),
                          Container(
                            padding: const EdgeInsets.all(14),
                            // This is terminal content, with its existing mono
                            // type and scratch output, inside desktop chrome.
                            decoration: BoxDecoration(
                              color: grid.AppPalette.swarmField,
                              borderRadius: BorderRadius.circular(8),
                            ),
                            child: Text(
                              _matched ? lesson.result : 'scratch workspace\n\n[agent 1] │ [agent 2]\n\n> ready',
                              key: const ValueKey('practice-preview'),
                              style: boxMonoStyle(),
                            ),
                          ),
                          const SizedBox(height: 12),
                          Text(
                            'A scratch workspace. Your agents stay as they are.',
                            style: DesktopChrome.metadata(),
                          ),
                          const SizedBox(height: 4),
                          Text(
                            'Page Up / Page Down to scroll',
                            style: DesktopChrome.metadata(),
                          ),
                          if (_feedback != null)
                            Padding(
                              padding: const EdgeInsets.only(top: 12),
                              child: Text(
                                _feedback!,
                                style: DesktopChrome.metadata(),
                              ),
                            ),
                          if (_matched)
                            Padding(
                              padding: const EdgeInsets.only(top: 12),
                              child: Semantics(
                                liveRegion: true,
                                child: Text(
                                  'Shortcut practiced. Enter continues.',
                                  style: DesktopChrome.metadata(),
                                ),
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ),
              const SizedBox(height: 12),
              Wrap(
                spacing: 16,
                runSpacing: 8,
                crossAxisAlignment: WrapCrossAlignment.center,
                children: [
                  if (lesson == null)
                    Text(
                      '${_hint('picker.accept')} Practice · ${_hint('picker.previous')}/${_hint('picker.next')} Browse',
                      style: DesktopChrome.metadata(),
                    ),
                  if (lesson != null)
                    DesktopPill(
                      onPressed: _next,
                      label: _matched ? 'Next shortcut' : 'Skip',
                    ),
                  DesktopPill(
                    key: const ValueKey('practice-back'),
                    onPressed: _back,
                    label: lesson == null ? 'Close' : 'All shortcuts',
                    tooltip: lesson == null ? _hint('picker.cancel') : 'Esc',
                    quiet: true,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    final content = lesson == null
        ? TerminalPromptKeys(
            cancel: _back,
            inputFocus: _filterFocus,
            accept: () => _filterFocus.hasFocus
                ? _openSelected()
                : activatePromptControl(),
            next: () => _move(1),
            previous: () => _move(-1),
            child: body,
          )
        : KeymapRegion(
            contextKind: lesson.context,
            actions: {
              for (final command in harnessCommands)
                command.id: () {
                  if (!_matched &&
                      lesson.bindings.isEmpty &&
                      command.id == 'picker.accept') {
                    _submitName();
                  } else {
                    _record(command.id);
                  }
                },
            },
            child: Focus(
              focusNode: _exerciseFocus,
              autofocus: true,
              onKeyEvent: _key,
              child: body,
            ),
          );
    return KeymapProvider(
      keymap: map,
      child: KeymapHost(
        keymap: map,
        enabled: () => false,
        actions: const {},
        child: Dialog(
          key: const ValueKey('keyboard-practice'),
          backgroundColor: Colors.transparent,
          elevation: 0,
          shape: DesktopChrome.bareDialogShape,
          insetPadding: const EdgeInsets.all(16),
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 780, maxHeight: 620),
            child: content,
          ),
        ),
      ),
    );
  }
}