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
import 'dart:math';
import 'package:xterm/xterm.dart';
/// How far up from the cursor a prompt's own text can reach. A sentence
/// dictated into the prompt wraps, and its first line is as much the prompt as
/// the line the cursor ended on.
const _maxPromptRows = 6;
/// The rows at the foot of the screen that count whatever the cursor says.
const _footRows = 3;
/// A line a TUI draws around its input rather than a line anybody typed.
final _rule = RegExp(r'^[─━═╭╮╰╯┌┐└┘\-]+$');
/// Whether a tap on buffer row [row] is aimed at the program's prompt — on a
/// phone, the one tap that should raise the keyboard. A tap anywhere else on the
/// terminal is somebody reading.
///
/// **The cursor says where the prompt is.** Both agent TUIs park the REAL
/// terminal cursor on their input line — Codex on `› `, Claude Code on `❯ ` —
/// as measured in live tmux panes (`#{cursor_y}` against `capture-pane`), and
/// Claude Code keeps it there while it is hidden, on the trust dialog's
/// selection for one. So the prompt is the block of text the cursor sits in,
/// walked up to its first line, and everything under it: the footer, the mode
/// line, the empty screen below a session that has barely started.
///
/// One row of slack above that, because a terminal row is ~16pt and a thumb is
/// not.
///
/// ⚠️ The bottom [_footRows] rows always count. Every agent TUI keeps its input
/// at the foot of the screen, and a program that leaves its cursor on the last
/// row — under its input rather than on it — must not leave the keyboard, which
/// on the phone only a tap raises, out of reach.
///
/// [row] is an absolute buffer row, scrollback included: what
/// `RenderTerminal.getCellOffset` reports. A tap on history scrolled far above
/// the prompt is therefore never a prompt tap.
///
/// ⚠️ **A dialog drawn in the prompt's place counts whole** ([wholeDialog]).
/// Claude Code hides its prompt under a dialog and parks the cursor UNDER the
/// dialog's footer — measured on a live pane with an MCP server's
/// `Accept Decline` open (openharness#1272): the cursor sat on the blank row
/// below `Esc to cancel · ↑/↓ to navigate`, so the dialog's own rows were
/// "reading", the rows left to tap were under the mic, and the phone had no
/// way at all to press the keys the dialog asked for. See [_dialogTop].
///
/// False where the host answers the dialog's rows itself (a tap on an answer's
/// band): a tap beside them is somebody reading the question, and a keyboard
/// rising over the answers would hide the very thing they were about to tap.
bool isPromptTap(Buffer buffer, int row, {bool wholeDialog = true}) {
final top = _promptTop(buffer, buffer.absoluteCursorY) - 1;
final edge = min(top, buffer.height - _footRows);
final dialog = wholeDialog ? _dialogTop(buffer) : null;
return row >= (dialog == null ? edge : min(edge, dialog));
}
/// A dialog's footer: what a key does, as Claude Code words its hints —
/// `Esc to cancel`, `↑/↓ to navigate`, `Enter to select`.
final _dialogFooter = RegExp(
r'\b(?:esc|enter|tab|space)\s+to\s+\w|[←↑→↓](?:\s*/\s*[←↑→↓])?\s+to\s+\w',
caseSensitive: false,
);
/// The rule over a dialog the program drew where its prompt was, or null when
/// the foot of the screen is a prompt — or anything a footer does not mark as a
/// dialog.
///
/// Walked up from the foot of the screen: no row on the way may start a prompt
/// (`❯ `, `› `), a footer must be passed, and the first rule at or above the
/// cursor is the dialog's top. Claude Code draws every dialog that way — a
/// full-width rule, the dialog, its footer — and Codex draws no rule, so a
/// Codex pane keeps exactly the zone it had.
int? _dialogTop(Buffer buffer) {
final lines = buffer.lines;
final screenTop = max(0, buffer.height - buffer.viewHeight);
final cursor = min(buffer.absoluteCursorY, buffer.height - 1);
var footer = false;
for (var row = buffer.height - 1; row >= screenTop; row--) {
final line = lines[row];
if (_promptGlyphColumn(line) != null) return null;
if (_dialogFooter.hasMatch(line.getText())) footer = true;
if (row <= cursor && _isRuleRow(line)) return footer ? row : null;
}
return null;
}
int _promptTop(Buffer buffer, int cursorRow) {
var top = cursorRow;
while (top > 0 &&
cursorRow - top < _maxPromptRows - 1 &&
_isTyped(buffer.lines[top]) &&
_isTyped(buffer.lines[top - 1])) {
top--;
}
return top;
}
bool _isTyped(BufferLine line) {
final text = line.getText().trim();
return text.isNotEmpty && !_rule.hasMatch(text);
}
/// How many rows a prompt's typed text can run to — a long paste included.
const _maxInputRows = 200;
/// OpenCode's boxed composer, read at its cursor, before its blank row and
/// model footer. A cursor at the start means the grey placeholder is not text.
/// The first-arrival starter is a sentence; the TUI wraps it at word boundaries.
String? openCodePromptText(Buffer buffer) {
final cursor = buffer.absoluteCursorY;
if (cursor < 0 || cursor >= buffer.lines.length) return null;
final current = buffer.lines[cursor].getText();
final side = current.indexOf('┃');
if (side < 0 || buffer.cursorX <= side + 3) return null;
String? row(int y) {
if (y < 0 || y >= buffer.lines.length) return null;
final text = buffer.lines[y].getText();
if (text.indexOf('┃') != side || text.length <= side + 3) return null;
final content = text.substring(side + 3).trimRight();
return content.isEmpty ? null : content;
}
var top = cursor;
while (top > 0 && cursor - top < _maxInputRows && row(top - 1) != null) {
top--;
}
final parts = <String>[];
for (var y = top; y < buffer.lines.length && y - top < _maxInputRows; y++) {
final content = row(y);
if (content == null) break;
parts.add(content);
}
return parts.isEmpty ? null : parts.join(' ');
}
/// What is typed into [engine]'s prompt, on one line: OpenCode's box ([openCodePromptText]), or
/// the `❯`/`›` prompt Claude Code and Codex draw ([promptInputRange]), its wrapped rows joined.
/// Null when nothing is typed, only the placeholder shows, or the prompt cannot be read.
String? typedPromptText(Buffer buffer, String? engine) {
if (engine == 'opencode') return openCodePromptText(buffer);
final range = promptInputRange(buffer);
if (range == null) return null;
final text = buffer.getText(range).replaceAll(_spaces, ' ').trim();
return text.isEmpty ? null : text;
}
final _spaces = RegExp(r'\s+');
/// Whether [text] read off a prompt holds what a TUI drew in place of something pasted — Claude
/// Code's `[Pasted text #1 +12 lines]` and `[Image #1]`, Codex's `[Pasted Content 1200 chars]`. What
/// was pasted is only in the program: typed again from the screen, it would be the marker.
bool holdsPasteMarker(String text) => _pasteMarker.hasMatch(text);
final _pasteMarker = RegExp(
r'\[(?:Pasted text #\d+|Image #\d+|Pasted Content \d+)',
);
/// Whether the cursor is in [engine]'s prompt: on Claude Code's or Codex's `❯`/`›` line or a row
/// its text wrapped onto, or inside OpenCode's box — where all three keep the terminal's own cursor,
/// shown, at the end of what is typed (measured in tmux on 2026-10-23: Claude Code 2.1.309, Codex
/// 0.163.0, OpenCode 1.19.0). False for any other engine.
bool cursorInPrompt(Buffer buffer, String? engine) {
final cursor = buffer.absoluteCursorY;
if (cursor < 0 || cursor >= buffer.lines.length) return false;
if (engine == 'opencode') {
final side = buffer.lines[cursor].getText().indexOf('┃');
return side >= 0 && buffer.cursorX > side;
}
if (engine != 'claude' && engine != 'codex') return false;
final line = buffer.lines[cursor];
if (_promptGlyphColumn(line) case final glyph?) {
// A question's answer the cursor sits on (`❯ 1. Yes`) is a dialog, not the prompt.
return !_answerRow.hasMatch(line.getText(glyph + 1).trimLeft());
}
// A row the typed text wrapped onto.
return promptInputRange(buffer) != null;
}
/// Whether the cursor sits on a Claude Code or Codex prompt with nothing typed in it — its glyph
/// alone, or its grey placeholder: the TUI is drawn and waiting for its first words.
///
/// Not a question's answer the cursor sits on (`❯ 1. Yes, proceed`): that is a dialog.
bool emptyPromptAtCursor(Buffer buffer) {
final cursor = buffer.absoluteCursorY;
if (cursor < 0 || cursor >= buffer.lines.length) return false;
final line = buffer.lines[cursor];
final glyph = _promptGlyphColumn(line);
return glyph != null &&
!_answerRow.hasMatch(line.getText(glyph + 1).trimLeft()) &&
promptInputRange(buffer) == null;
}
/// What an agent's prompt starts with, in column 0 or just inside a box's side: Claude Code's `❯`
/// (`>` in older builds), its `!` shell mode and `#` memory mode, and Codex's `›`. A shell themed
/// with a `❯` prompt reads the same way, and rightly: what follows it is what was typed.
const _promptGlyphs = {0x276F, 0x3E, 0x21, 0x23, 0x203A};
/// The side of the box older Claude Code draws its prompt inside: `│ > text │`.
const _boxSide = 0x2502;
/// An open question's answer, which the cursor sits on too: `❯ 1. Yes`. Not a prompt.
final _answerRow = RegExp(r'^\d+[.)]\s');
/// Where the text typed into the program's prompt is — from just past `❯ ` to its last character —
/// or null when nothing is typed there: an empty prompt, one showing only its grey placeholder, an
/// open question, or a prompt this cannot read (a plain shell's `%`, which cannot be told apart from
/// what was typed after it).
///
/// Found from the cursor, as [isPromptTap] finds the prompt: walked up to the row with the prompt's
/// glyph, then down through the rows the text wrapped onto, to the box's rule or a blank row.
BufferRangeLine? promptInputRange(Buffer buffer) {
final lines = buffer.lines;
final cursorRow = buffer.absoluteCursorY;
if (cursorRow < 0 || cursorRow >= lines.length) return null;
int? top;
int? glyph;
for (
var row = cursorRow;
row >= 0 && cursorRow - row < _maxInputRows;
row--
) {
final line = lines[row];
if (!_isTyped(line)) return null;
glyph = _promptGlyphColumn(line);
if (glyph != null) {
top = row;
break;
}
}
if (top == null || glyph == null) return null;
if (_answerRow.hasMatch(lines[top].getText(glyph + 1).trimLeft())) {
return null;
}
var last = top;
for (
var row = top + 1;
row < lines.length && row - top < _maxInputRows;
row++
) {
final line = lines[row];
if (!_isTyped(line) || _promptGlyphColumn(line) != null) break;
last = row;
}
final start = CellOffset(glyph + 2, top);
final end = CellOffset(_textEnd(lines[last]), last);
if (!start.isBefore(end)) return null;
final range = BufferRangeLine(start, end);
return _isPlaceholder(buffer, range) ? null : range;
}
/// How many screen rows the prompt the cursor is in can take, at most: from its first row — the
/// one with the prompt's glyph, or the one under the rule a TUI draws over its input — to the foot
/// of the screen. The whole screen when neither is above the cursor.
///
/// ⚠️ **An overcount, by design.** It counts the footer under the prompt too, and a row the prompt
/// is not on. Its one use is how many times to kill a line ([TerminalSession.clearPrompt]), and a
/// kill too many does nothing, where one too few leaves text behind.
int promptRowsBound(Buffer buffer) {
final lines = buffer.lines;
final screenTop = max(0, buffer.height - buffer.viewHeight);
var top = screenTop;
for (
var row = min(buffer.absoluteCursorY, buffer.height - 1);
row >= screenTop;
row--
) {
final line = lines[row];
if (_promptGlyphColumn(line) != null) {
top = row;
break;
}
if (_isRuleRow(line)) {
top = row + 1;
break;
}
}
return max(1, buffer.height - top);
}
/// A rule a TUI draws across the screen, from column 0 — not a line of dashes typed into the
/// prompt, which is indented under its glyph.
bool _isRuleRow(BufferLine line) {
if (line.length == 0) return false;
final lead = line.getCodePoint(0);
if (lead == 0 || lead == 0x20) return false;
return _rule.hasMatch(line.getText().trim());
}
/// The column of [line]'s prompt glyph — 0, or 2 inside a box's side — or null when the line does
/// not start a prompt. Always followed by a blank, which is what keeps a `#` or `>` the person
/// typed at the start of a wrapped row from reading as a new prompt: those rows are indented.
int? _promptGlyphColumn(BufferLine line) {
int at(int column) => column < line.length ? line.getCodePoint(column) : 0;
bool blank(int column) => at(column) == 0 || at(column) == 0x20;
if (_promptGlyphs.contains(at(0)) && blank(1)) return 0;
if (at(0) == _boxSide &&
blank(1) &&
_promptGlyphs.contains(at(2)) &&
blank(3)) {
return 2;
}
return null;
}
/// Where [line]'s text ends, short of a box's closing side and the padding before it.
int _textEnd(BufferLine line) {
var end = line.getTrimmedLength();
if (end > 0 && line.getCodePoint(end - 1) == _boxSide) {
end--;
while (end > 0 &&
(line.getCodePoint(end - 1) == 0 ||
line.getCodePoint(end - 1) == 0x20)) {
end--;
}
}
return end;
}
/// Whether [range] holds the prompt's placeholder rather than anything typed: faint text, as both
/// TUIs draw it (`Try "fix lint errors"`), all of it but the cell the TUI's own cursor is drawn on.
bool _isPlaceholder(Buffer buffer, BufferRangeLine range) {
var cells = 0;
var faint = 0;
for (final segment in range.toSegments()) {
final line = buffer.lines[segment.line];
final from = segment.start ?? 0;
final to = (segment.end ?? line.length).clamp(0, line.length);
for (var x = from; x < to; x++) {
final codePoint = line.getCodePoint(x);
if (codePoint == 0 || codePoint == 0x20) continue;
cells++;
if (line.getAttributes(x) & CellFlags.faint != 0) faint++;
}
}
return faint > 0 && faint >= cells - 1;
}