Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(chat): keep a line-ending backslash when the ⌘M view types a message

Paul Robertson24578b70625e
diff --git a/CLAUDE.md b/CLAUDE.md
index a2bfbe969..217c0295c 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -4444,7 +4444,12 @@ command-bearing opens; this does not add a human-confirm dialog or change mobile
the dialog check `sendChatPrompt` makes is seconds old by then, and an Enter into a dialog that
opened meanwhile would ANSWER it — the text stays in the composer and the caller hears
`pasted-not-submitted`. The panel ignores a second Enter while a send is in flight (the draft is
- cleared only once it lands). **"The text arrived" is read from the COMPOSER, never counted
+ cleared only once it lands). **A backslash is never typed right before a line break or the Enter**
+ (MEASURED, 2.1.319): the composer reads `\` + Enter and `\` + M-Enter alike as "new line" and eats
+ the backslash, so a `!` command's continuation lines ran as separate commands and a message ending
+ in `\` was never sent. A line's trailing backslashes are typed after its line break (Left, the
+ run, Right; `typedRecords`), and a message ending in one is submitted with Left then Enter
+ (`cursorLeftFirst`). **"The text arrived" is read from the COMPOSER, never counted
across the screen** (`TypedSurface.draftOf` → `readClaudeDraft`, on a styled capture so the dim
placeholder is not draft text): the draft must end with the message's tail and differ from the
draft before typing. Typing also clears the placeholder and shortens the status line ("· ← for
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 8d197ef6e..486d41193 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -141,7 +141,7 @@ import {
pasteBufferName,
runPasteDelivery
} from './tmux-naming'
-import { localTypedArgs, localTypedEnv, typeThenSubmitWhenSettled } from './typed-input'
+import { endsWithBackslash, localTypedArgs, localTypedEnv, typedLines, typeThenSubmitWhenSettled } from './typed-input'
import { encodeSendKeysHex } from './tmux-control'
import {
ZELLIJ_NESTING_ENV,
@@ -6699,6 +6699,8 @@ export class PtyManager {
const live = this.liveSessionForPersistKey(persistKey)
if (live?.nativeWindowsPane || this.isZellij(persistKey, live)) return null
const sshRemote = live?.sshRemote
+ // A message ending in `\`: Enter right after it would be read as a line break (typed-input.ts).
+ const leftFirst = endsWithBackslash(typedLines(text))
let type: (stdin: string) => Promise<unknown>
let submit: () => Promise<unknown>
if (sshRemote) {
@@ -6706,14 +6708,15 @@ export class PtyManager {
if (!ssh) return false
type = (stdin) =>
runWithStdin(ssh, remoteTypedArgs(sshRemote.conn, sshRemote.controlPath, target, pasteBufferName()), stdin)
- submit = () => runAsync(ssh, remoteTmuxEnterArgs(sshRemote.conn, sshRemote.controlPath, target))
+ submit = () =>
+ runAsync(ssh, remoteTmuxEnterArgs(sshRemote.conn, sshRemote.controlPath, target, { cursorLeftFirst: leftFirst }))
} else if (live?.sessionHost || !this.tmuxPath) {
return null
} else {
const tmuxPath = this.tmuxPath
type = (stdin) =>
runTypedScript(localTypedArgs(TMUX_SOCKET, target, pasteBufferName()), stdin, localTypedEnv(tmuxPath))
- submit = () => runAsync(tmuxPath, localTmuxEnterArgs(TMUX_SOCKET, target))
+ submit = () => runAsync(tmuxPath, localTmuxEnterArgs(TMUX_SOCKET, target, { cursorLeftFirst: leftFirst }))
}
const ok = async (run: () => Promise<unknown>): Promise<boolean> => {
try {
diff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.ts
index 647cbb404..919159360 100644
--- a/src/core/remote-ssh/control-master.test.ts
+++ b/src/core/remote-ssh/control-master.test.ts
@@ -253,6 +253,12 @@ describe('remoteTmuxEnterArgs', () => {
`${TP}tmux -L ${RMT_TMUX_SOCKET} send-keys -t '=nt-x:' Enter`
])
})
+
+ it('steps the cursor left first for a typed message ending in a backslash', () => {
+ const args = remoteTmuxEnterArgs(conn, '/s.sock', 'nt-x', { cursorLeftFirst: true })
+
+ expect(args[args.length - 1]).toBe(`${TP}tmux -L ${RMT_TMUX_SOCKET} send-keys -t '=nt-x:' Left Enter`)
+ })
})
describe('remoteCapturePaneArgs', () => {
diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.ts
index 6e2ce1624..d9196889f 100644
--- a/src/core/remote-ssh/control-master.ts
+++ b/src/core/remote-ssh/control-master.ts
@@ -403,13 +403,16 @@ export function remoteTypedArgs(
export function remoteTmuxEnterArgs(
conn: SshConnection,
controlPath: string,
- sessionId: string
+ sessionId: string,
+ opts?: { cursorLeftFirst?: boolean }
): string[] {
assertPasteTarget(sessionId)
+ // `cursorLeftFirst`: a typed message ending in `\` (core/typed-input.ts `endsWithBackslash`).
+ const keys = opts?.cursorLeftFirst === true ? 'Left Enter' : 'Enter'
return childArgs(
conn,
controlPath,
- tmuxCmd(`tmux -L ${RMT_TMUX_SOCKET} send-keys -t ${posixQuote(exactPaneTarget(sessionId))} Enter`)
+ tmuxCmd(`tmux -L ${RMT_TMUX_SOCKET} send-keys -t ${posixQuote(exactPaneTarget(sessionId))} ${keys}`)
)
}
diff --git a/src/core/tmux-naming.test.ts b/src/core/tmux-naming.test.ts
index 3e0d56a03..9c067af9e 100644
--- a/src/core/tmux-naming.test.ts
+++ b/src/core/tmux-naming.test.ts
@@ -34,6 +34,12 @@ describe('localTmuxEnterArgs', () => {
])
expect(localTmuxEnterArgs(TMUX_SOCKET, 'nt-x')).not.toContain('-l')
})
+
+ it('steps the cursor left first for a typed message ending in a backslash', () => {
+ const args = localTmuxEnterArgs('sock', 'nt-x', { cursorLeftFirst: true })
+
+ expect(args.slice(-2)).toEqual(['Left', 'Enter'])
+ })
})
/**
diff --git a/src/core/tmux-naming.ts b/src/core/tmux-naming.ts
index 815ba571c..e1a4a55ab 100644
--- a/src/core/tmux-naming.ts
+++ b/src/core/tmux-naming.ts
@@ -89,9 +89,11 @@ export function exactSessionTarget(session: string): string {
* nothing: the rule is "every splice is checked at the splice", and the two builders that had no
* buffer of their own were the two that quietly opted out of it.
*/
-export function localTmuxEnterArgs(socket: string, target: string): string[] {
+export function localTmuxEnterArgs(socket: string, target: string, opts?: { cursorLeftFirst?: boolean }): string[] {
assertPasteTarget(target)
- return ['-L', socket, 'send-keys', '-t', exactPaneTarget(target), 'Enter']
+ // `cursorLeftFirst`: a typed message ending in `\` (core/typed-input.ts `endsWithBackslash`).
+ const keys = opts?.cursorLeftFirst === true ? ['Left', 'Enter'] : ['Enter']
+ return ['-L', socket, 'send-keys', '-t', exactPaneTarget(target), ...keys]
}
/**
diff --git a/src/core/typed-input.realtmux.test.ts b/src/core/typed-input.realtmux.test.ts
index a4b3e3e19..5467f771e 100644
--- a/src/core/typed-input.realtmux.test.ts
+++ b/src/core/typed-input.realtmux.test.ts
@@ -17,6 +17,9 @@ import { makeTmuxTmpdir } from './tmux-test-socket'
const ESC = '\x1b'
/** M-Enter as the pane receives it: the "new line, don't submit" key. */
const NEWLINE_KEY = `${ESC}\r`
+/** The arrow keys as a pane not in application-cursor mode receives them. */
+const LEFT = `${ESC}[D`
+const RIGHT = `${ESC}[C`
/** A private socket — never `TMUX_SOCKET`/`nodeterm-rmt`, which carry live user sessions. */
const SOCKET = `nt-typed-test-${process.pid}`
const CONN = { host: 'h.example.com', user: 'deploy', port: 2222, identityFile: '/k/id' }
@@ -123,6 +126,17 @@ describe.skipIf(TMUX === null || process.platform === 'win32')('typed delivery t
)
})
+ it('types a line\'s trailing backslashes after the line break, so the composer cannot eat them', () => {
+ const session = `nt-ty-bslash-${leg}`
+ const out = recorderPane(session)
+
+ type(leg, session, 'cd . \\\npopd \\\\\nlast \\')
+
+ expect(drain(session, out)).toBe(
+ `cd . ${NEWLINE_KEY}${LEFT}\\${RIGHT}popd ${NEWLINE_KEY}${LEFT}\\\\${RIGHT}last \\`
+ )
+ })
+
it('delivers what tmux argv parsing would mangle, byte for byte', () => {
const session = `nt-ty-argv-${leg}`
const out = recorderPane(session)
diff --git a/src/core/typed-input.test.ts b/src/core/typed-input.test.ts
index 73d06bcdb..d9356c4f4 100644
--- a/src/core/typed-input.test.ts
+++ b/src/core/typed-input.test.ts
@@ -7,7 +7,9 @@ import {
typedInputScript,
typedLines,
typedStdin,
- type TypedSurface
+ type TypedSurface,
+ typedRecords,
+ endsWithBackslash
} from './typed-input'
import { readClaudeDraft } from '../shared/agents/claude-screen'
import composer from '../shared/agents/__fixtures__/claude/composer-capture.json'
@@ -31,8 +33,41 @@ describe('typedLines', () => {
})
describe('typedStdin', () => {
- it('terminates every line, so the script reads the last one too', () => {
- expect(typedStdin(['a', '', 'b'])).toBe('a\n\nb\n')
+ it('gives every line a carry line and terminates both, so the script reads the last one too', () => {
+ expect(typedStdin(['a', '', 'b'])).toBe('\na\n\n\n\nb\n')
+ })
+
+ it('carries a line\'s trailing backslashes onto the next line', () => {
+ expect(typedStdin(['cd . \\', 'popd'])).toBe('\ncd . \n\\\npopd\n')
+ })
+})
+
+describe('typedRecords', () => {
+ it('moves each trailing run of backslashes past the line break, whole', () => {
+ const records = typedRecords(['a \\', 'b \\\\', '\\', 'end'])
+
+ expect(records).toEqual([
+ { carry: '', text: 'a ' },
+ { carry: '\\', text: 'b ' },
+ { carry: '\\\\', text: '' },
+ { carry: '\\', text: 'end' }
+ ])
+ })
+
+ it('keeps the last line whole, and backslashes that do not end a line', () => {
+ expect(typedRecords(['a\\b', 'tail \\'])).toEqual([
+ { carry: '', text: 'a\\b' },
+ { carry: '', text: 'tail \\' }
+ ])
+ })
+})
+
+describe('endsWithBackslash', () => {
+ it('asks only about the last line', () => {
+ expect(endsWithBackslash(['tail \\'])).toBe(true)
+ expect(endsWithBackslash(['a \\', 'b'])).toBe(false)
+ expect(endsWithBackslash(['a\\b'])).toBe(false)
+ expect(endsWithBackslash([])).toBe(false)
})
})
@@ -86,7 +121,7 @@ describe('typeThenSubmitWhenSettled', () => {
const result = await typeThenSubmitWhenSettled('hello there', surface, noWait)
expect(result).toBe(true)
- expect(surface.typed).toEqual(['hello there\n'])
+ expect(surface.typed).toEqual(['\nhello there\n'])
expect(surface.submit).toHaveBeenCalledTimes(1)
})
diff --git a/src/core/typed-input.ts b/src/core/typed-input.ts
index 06106d3d4..cf084d75e 100644
--- a/src/core/typed-input.ts
+++ b/src/core/typed-input.ts
@@ -26,6 +26,14 @@ import { ENVELOPE_SETTLE_POLLS, ENVELOPE_SETTLE_POLL_MS, type SettleOptions } fr
* same burst as the text is read by Claude Code as a line break, and the message sits unsent.
* - Tabs become spaces: a Tab keystroke is a shortcut to Claude Code's composer and the character
* vanishes (measured).
+ * - A backslash is never the last character typed before a line break or the submitting Enter.
+ * MEASURED on Claude Code 2.1.319: the composer treats `\` + Enter as "new line" and eats the
+ * backslash, and does the same for `\` + M-Enter, so `cd . \` / `popd` arrived as `cd .` /
+ * `popd` (a `!` command's continuation lines ran as separate commands), and a message ending in
+ * `\` was not sent at all. So a line's trailing backslashes are typed AFTER the line break, with
+ * the cursor stepped back onto the line (Left, the backslashes, Right), and a message whose last
+ * line ends in `\` is submitted with Left then Enter. Both measured to keep every backslash; a
+ * backslash anywhere else in a line is untouched by the composer.
*
* tmux backends only (local, SSH, Server Edition). The Windows backends keep the paste path: how
* Claude Code on Windows reads M-Enter has not been measured.
@@ -67,13 +75,21 @@ export function typedInputScript(tmux: string, socket: string, target: string, b
return [
// Leave copy mode first, like the paste path: keys typed into copy mode drive IT, not the app.
`${t} if-shell -F -t ${q} '#{pane_in_mode}' 'send-keys -t ${pane} -X cancel' || exit 1`,
+ // Two stdin lines per line of text (`typedStdin`): the backslashes carried over from the line
+ // before, typed after the line break with the cursor stepped back onto that line, then the text.
+ 'paste() {',
+ ` printf '%s' "$1" | ${t} load-buffer -b ${buffer} - || exit 1`,
+ ` ${t} paste-buffer -d -r -b ${buffer} -t ${q} || { ${t} delete-buffer -b ${buffer} 2>/dev/null; exit 1; }`,
+ '}',
'first=1',
- 'while IFS= read -r line; do',
+ 'while IFS= read -r carry && IFS= read -r line; do',
` if [ "$first" = 1 ]; then first=0; else ${t} send-keys -t ${q} M-Enter || exit 1; fi`,
- ' if [ -n "$line" ]; then',
- ` printf '%s' "$line" | ${t} load-buffer -b ${buffer} - || exit 1`,
- ` ${t} paste-buffer -d -r -b ${buffer} -t ${q} || { ${t} delete-buffer -b ${buffer} 2>/dev/null; exit 1; }`,
+ ' if [ -n "$carry" ]; then',
+ ` ${t} send-keys -t ${q} Left || exit 1`,
+ ' paste "$carry"',
+ ` ${t} send-keys -t ${q} Right || exit 1`,
' fi',
+ ' if [ -n "$line" ]; then paste "$line"; fi',
'done'
].join('\n')
}
@@ -96,9 +112,42 @@ export function localTypedEnv(tmuxPath: string, base: NodeJS.ProcessEnv = proces
return { ...base, [TYPED_TMUX_ENV]: tmuxPath }
}
-/** What stdin the script reads: one line per line, each LF-terminated so the last is read too. */
+/** The backslashes ending a line (`\\+$`), or ''. */
+export function trailingBackslashes(line: string): string {
+ let i = line.length
+ while (i > 0 && line[i - 1] === '\\') i--
+ return line.slice(i)
+}
+
+/**
+ * What the script types for each line: `carry`, the backslashes moved off the end of the line BEFORE
+ * this one (typed after the line break, the cursor stepped back onto that line), then `text`. Every
+ * line but the last gives up its trailing backslashes this way; the last keeps them (its Enter is
+ * sent with the cursor stepped back instead, `endsWithBackslash`).
+ */
+export function typedRecords(lines: readonly string[]): Array<{ carry: string; text: string }> {
+ let carry = ''
+ return lines.map((line, i) => {
+ const last = i === lines.length - 1
+ const tail = last ? '' : trailingBackslashes(line)
+ const record = { carry, text: line.slice(0, line.length - tail.length) }
+ carry = tail
+ return record
+ })
+}
+
+/** Whether the last line ends in a backslash: its submitting Enter then goes with the cursor stepped
+ * back one place, or the composer would take the Enter as a line break and eat the backslash. */
+export function endsWithBackslash(lines: readonly string[]): boolean {
+ return lines.length > 0 && trailingBackslashes(lines[lines.length - 1]) !== ''
+}
+
+/** What stdin the script reads: two LF-terminated lines per line of text, its `carry` (only ever
+ * backslashes, or empty) and its `text` (`typedRecords`). */
export function typedStdin(lines: readonly string[]): string {
- return `${lines.join('\n')}\n`
+ return typedRecords(lines)
+ .map((r) => `${r.carry}\n${r.text}\n`)
+ .join('')
}
export interface TypedSurface {