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
harnessd: nothing pasted into a pane acts as a keystroke, on any tmux. (#799)
Dee996f3d097e5f
diff --git a/cli/e2e/content.e2e.ts b/cli/e2e/content.e2e.tsindex 146262de6..9c9c91733 100644--- a/cli/e2e/content.e2e.ts+++ b/cli/e2e/content.e2e.ts@@ -7,9 +7,12 @@* in it may act as keystrokes: in particular the paste's own end marker (ESC [201~), which would end* the paste early and turn the rest into typing, and a carriage return inside it.*- * Measured (tmux 3.7c): tmux itself defangs control characters inside a bracketed paste (the engine- * receives ESC as the two characters `^[`), and that is what keeps the end marker from acting; the- * daemon pastes the message as it came. This test is the guard for a tmux that does not.+ * Measured: tmux 3.7c defangs control characters inside a bracketed paste itself (the engine receives+ * ESC as the two characters `^[`), but 3.2a and 3.3a, what Ubuntu 22.04 and Debian 12 ship, pass ESC+ * through raw: there the end marker ended the paste after `hello` and the engine ran `!exit` as typed+ * input. The daemon now makes every control but tab and newline visible before it pastes+ * (lib/pasteText.ts), so a message reads the same on every tmux. This test holds that on whichever tmux+ * is first on PATH.*/import { execFileSync } from 'node:child_process'import { mkdirSync } from 'node:fs'diff --git a/cli/src/lib/pasteText.spec.ts b/cli/src/lib/pasteText.spec.tsnew file mode 100644index 000000000..01493f174--- /dev/null+++ b/cli/src/lib/pasteText.spec.ts@@ -0,0 +1,47 @@+import { describe, expect, it } from 'vitest'+import { neutralizePasteControls } from './pasteText.js'++const ESC = '\x1b'++describe('what a bracketed paste may carry', () => {+ it('shows the paste\'s own markers as text, so a message cannot end its paste and type the rest', () => {+ // The message that made the engine run `!exit` on tmux 3.2a and 3.3a (e2e/content.e2e.ts).+ expect(neutralizePasteControls(`hello${ESC}[201~\r!exit\r`)).toBe('hello^[[201~\n!exit\n')+ expect(neutralizePasteControls(`${ESC}[200~typed?${ESC}[201~`)).toBe('^[[200~typed?^[[201~')+ expect(neutralizePasteControls(`before${ESC}[2J${ESC}[Hafter`)).toBe('before^[[2J^[[Hafter')+ })++ it('makes every control but tab and newline visible, in caret notation, as tmux 3.7c does', () => {+ for (let code = 0; code < 0x20; code++) {+ if (code === 0x09 || code === 0x0a || code === 0x0d) continue+ expect(neutralizePasteControls(`a${String.fromCharCode(code)}b`), `0x${code.toString(16)}`)+ .toBe(`a^${String.fromCharCode(code + 0x40)}b`)+ }+ expect(neutralizePasteControls('a\x7fb')).toBe('a^?b')+ expect(neutralizePasteControls('a\tb\nc')).toBe('a\tb\nc')+ })++ it('takes a carriage return, alone or before a newline, as one line break', () => {+ expect(neutralizePasteControls('one\r\ntwo\rthree\nfour')).toBe('one\ntwo\nthree\nfour')+ })++ it('shows an 8-bit control as the ESC and letter a terminal may read it as', () => {+ expect(neutralizePasteControls('\u009b31mred')).toBe('^[[31mred')+ expect(neutralizePasteControls('\u009d0;title\u0007')).toBe('^[]0;title^G')+ })++ it('leaves text as people write it exactly as it is', () => {+ for (const text of [+ 'a family 👨👩👧👦 and a flag 🇻🇳, joined',+ 'שלום עולם and مرحبا بالعالم, right to left',+ 'tabs\tbetween\twords and `backticks` $(echo not run) ; | & > <',+ 'combining: é ä and a zero-widthspace',+ 'a caret and a bracket typed by hand: ^[ is two characters',+ ]) expect(neutralizePasteControls(text)).toBe(text)+ })++ it('changes nothing the second time', () => {+ const once = neutralizePasteControls(`x${ESC}[201~\r\n\u009by\x00`)+ expect(neutralizePasteControls(once)).toBe(once)+ })+})diff --git a/cli/src/lib/pasteText.ts b/cli/src/lib/pasteText.tsnew file mode 100644index 000000000..0e5f4af9f--- /dev/null+++ b/cli/src/lib/pasteText.ts@@ -0,0 +1,29 @@+/**+ * What a bracketed paste may carry into a pane: the text as written, with every control character but+ * tab and newline made visible, so that nothing in it acts as a keystroke.+ *+ * A message is pasted into the engine's pane between ESC [200~ and ESC [201~ (`paste-buffer -p`), and+ * the engine takes what is between them as text. A message carrying ESC [201~ itself ends the paste+ * there, and everything after it is typed. Measured on tmux 3.2a and 3.3a, what Ubuntu 22.04 and+ * Debian 12 ship: `hello ESC[201~ CR !exit CR` ended the paste after `hello`, and the engine ran+ * `!exit` as typed input (e2e/content.e2e.ts). In Claude Code a typed `!` line is a shell command, and+ * anything that can send an agent a message can send that, other agents included. tmux 3.7c defangs+ * controls inside a bracketed paste itself (ESC reaches the engine as `^[`), which is why the same+ * message was one prompt there. The daemon now does the same, whatever tmux does.+ *+ * In tmux 3.7c's form: caret notation, `^[` for ESC, `^?` for DEL, so a message reads the same on every+ * tmux. A carriage return, alone or before a newline, is a line break: one newline each, which tmux+ * pastes as the carriage return a pasted line ends with. The 8-bit C1 controls, which a terminal may+ * read as ESC and a letter (U+009B is ESC [), are shown as that pair.+ */+export function neutralizePasteControls(text: string): string {+ return text+ .replace(/\r\n?/g, '\n')+ // eslint-disable-next-line no-control-regex+ .replace(/[\x00-\x08\x0b-\x1f\x7f\u0080-\u009f]/g, (control) => {+ const code = control.charCodeAt(0)+ if (code === 0x7f) return '^?'+ if (code >= 0x80) return `^[${String.fromCharCode(code - 0x40)}`+ return `^${String.fromCharCode(code + 0x40)}`+ })+}diff --git a/cli/src/lib/tmux.spec.ts b/cli/src/lib/tmux.spec.tsindex 2ab2afff5..5682ad4fa 100644--- a/cli/src/lib/tmux.spec.ts+++ b/cli/src/lib/tmux.spec.ts@@ -614,4 +614,38 @@ if [ "$1" = "paste-buffer" ] && [ "$TMUX_SUBMIT_FAIL" = "1" ]; then exit 2; firmSync(dir, { recursive: true, force: true })}})++ it('pastes a message or a clipboard with its control characters made visible, and types a literal as given', async () => {+ // A message carrying the paste's end marker ended its paste on tmux 3.2a and 3.3a, and what followed+ // was typed (e2e/content.e2e.ts): a buffer tmux pastes bracketed holds no control but tab and newline.+ const dir = mkdtempSync(join(tmpdir(), 'harness-tmux-paste-'))+ const fakeTmux = join(dir, 'tmux')+ writeFileSync(fakeTmux, `#!/bin/sh+if [ "$1" = "load-buffer" ]; then+ n=$(cat "$TMUX_PASTE_DIR/count" 2>/dev/null || echo 0); n=$((n + 1)); echo "$n" > "$TMUX_PASTE_DIR/count"+ cat > "$TMUX_PASTE_DIR/buffer-$n"+fi+`)+ chmodSync(fakeTmux, 0o700)+ const previous = { path: process.env.PATH, dir: process.env.TMUX_PASTE_DIR }+ const hostile = 'hello\x1b[201~\r!exit\r'+ try {+ process.env.PATH = `${dir}:${previous.path ?? ''}`+ process.env.TMUX_PASTE_DIR = dir+ expect(await sendToTmux('%7', hostile)).toBe(true)+ expect(await pasteRawIntoTmux('%7', hostile)).toBe(true)+ expect(await sendLiteralToTmux('%7', 'filter\x1b')).toBe(true)+ const buffer = (n: number) => readFileSync(join(dir, `buffer-${n}`), 'utf8')+ // The message loses its trailing carriage return before the paste, as before, then its controls.+ expect(buffer(1)).toBe('hello^[[201~\n!exit')+ expect(buffer(2)).toBe('hello^[[201~\n!exit\n')+ expect(buffer(3)).toBe('filter\x1b')+ } finally {+ if (previous.path === undefined) delete process.env.PATH+ else process.env.PATH = previous.path+ if (previous.dir === undefined) delete process.env.TMUX_PASTE_DIR+ else process.env.TMUX_PASTE_DIR = previous.dir+ rmSync(dir, { recursive: true, force: true })+ }+ })})diff --git a/cli/src/lib/tmux.ts b/cli/src/lib/tmux.tsindex 338872b2c..a3184bcef 100644--- a/cli/src/lib/tmux.ts+++ b/cli/src/lib/tmux.ts@@ -17,6 +17,7 @@ import {import { BYPASS_PERMISSION_FLAGS, PERMISSION_MODES, permissionModeApproves } from './engineLaunch.js'import { psEnv } from './childLocale.js'import { nativeProcessImages } from './nativeProcessImages.js'+import { neutralizePasteControls } from './pasteText.js'export { captureTmuxPane, tmuxCaptureArgs } from './tmuxCapture.js'function cleanPaneTitle(title: string): string | null {@@ -1217,10 +1218,12 @@ function tmuxDeleteBuffer(name: string): Promise<void> {})}-/** Paste text from a uniquely named stdin-loaded buffer so its bytes never enter argv or errors. */+/** Paste text from a uniquely named stdin-loaded buffer so its bytes never enter argv or errors. A+ * bracketed paste carries text and nothing else (pasteText.ts), not even its own end marker; an+ * unbracketed one is typing, and is typed as it was given. */async function tmuxPasteText(pane: string, content: string, bracketed: boolean): Promise<boolean> {const bufferName = `machinemsg-${process.pid}-${++injectBufferSequence}`- if (!(await tmuxLoadBuffer(bufferName, content))) {+ if (!(await tmuxLoadBuffer(bufferName, bracketed ? neutralizePasteControls(content) : content))) {await tmuxDeleteBuffer(bufferName)console.error(`[tmux] load-buffer for ${pane} failed`)return false@@ -1251,7 +1254,7 @@ async function tmuxPasteText(pane: string, content: string, bracketed: boolean):export function sendToTmux(pane: string, text: string): Promise<boolean> {const content = text.replace(/[\r\n]+$/, '') // strip trailing newlines so the submit Enter isn't doubledreturn (async () => {- const needsSettle = content.length > INJECT_FASTPATH_MAXLEN || content.includes('\n')+ const needsSettle = content.length > INJECT_FASTPATH_MAXLEN || /[\r\n]/.test(content)if (!(await tmuxPasteText(pane, content, true))) return falseif (needsSettle) await sleep(Math.min(1500, INJECT_PASTE_DELAY_BASE_MS + Math.floor(content.length / 60)))return tmuxEnter(pane)