@@ -253,8 +253,19 @@ export function watchForCancel(abortController: AbortController): () => void {
253253
254254 // ─── Paste handling ───────────────────────────────────────────────────────────
255255
256-const PASTE_SENTINEL = "\x00PASTE\x00";
257-const PASTE_SENTINEL_LEN = PASTE_SENTINEL.length;
256+const PASTE_TOKEN_START = 0xe000;
257+const PASTE_TOKEN_END = 0xf8ff;
258+
259+function createPasteToken(
260+ buf: string,
261+ pasteTokens: ReadonlyMap<string, string>,
262+): string {
263+ for (let code = PASTE_TOKEN_START; code <= PASTE_TOKEN_END; code++) {
264+ const token = String.fromCharCode(code);
265+ if (!buf.includes(token) && !pasteTokens.has(token)) return token;
266+ }
267+ throw new Error("Too many pasted chunks in a single prompt");
268+}
258269
259270 export function pasteLabel(text: string): string {
260271 const lines = text.split("\n");
@@ -265,6 +276,81 @@ export function pasteLabel(text: string): string {
265276 return `[pasted: "${preview}"${more}]`;
266277 }
267278
279+export function renderInputBuffer(
280+ buf: string,
281+ pasteTokens: ReadonlyMap<string, string>,
282+): string {
283+ let out = "";
284+ for (let i = 0; i < buf.length; i++) {
285+ const ch = buf[i] ?? "";
286+ const pasted = pasteTokens.get(ch);
287+ out += pasted ? pasteLabel(pasted) : ch;
288+ }
289+ return out;
290+}
291+
292+export function expandInputBuffer(
293+ buf: string,
294+ pasteTokens: ReadonlyMap<string, string>,
295+): string {
296+ let out = "";
297+ for (let i = 0; i < buf.length; i++) {
298+ const ch = buf[i] ?? "";
299+ out += pasteTokens.get(ch) ?? ch;
300+ }
301+ return out;
302+}
303+
304+export function pruneInputPasteTokens(
305+ pasteTokens: ReadonlyMap<string, string>,
306+ ...buffers: readonly string[]
307+): Map<string, string> {
308+ const referenced = buffers.join("");
309+ const next = new Map<string, string>();
310+ for (const [token, text] of pasteTokens) {
311+ if (referenced.includes(token)) next.set(token, text);
312+ }
313+ return next;
314+}
315+
316+function getVisualCursor(
317+ buf: string,
318+ cursor: number,
319+ pasteTokens: ReadonlyMap<string, string>,
320+): number {
321+ let visual = 0;
322+ for (let i = 0; i < Math.min(cursor, buf.length); i++) {
323+ const ch = buf[i] ?? "";
324+ const pasted = pasteTokens.get(ch);
325+ visual += pasted ? pasteLabel(pasted).length : 1;
326+ }
327+ return visual;
328+}
329+
330+export function buildPromptDisplay(
331+ text: string,
332+ cursor: number,
333+ maxLen: number,
334+): { display: string; cursor: number } {
335+ const clampedCursor = Math.max(0, Math.min(cursor, text.length));
336+ if (maxLen <= 0) return { display: "", cursor: 0 };
337+ if (text.length <= maxLen) return { display: text, cursor: clampedCursor };
338+
339+ let start = Math.max(0, clampedCursor - maxLen);
340+ const end = Math.min(text.length, start + maxLen);
341+ if (end - start < maxLen) start = Math.max(0, end - maxLen);
342+
343+ let display = text.slice(start, end);
344+ if (start > 0 && display.length > 0) display = `…${display.slice(1)}`;
345+ if (end < text.length && display.length > 0)
346+ display = `${display.slice(0, -1)}…`;
347+
348+ return {
349+ display,
350+ cursor: Math.min(clampedCursor - start, display.length),
351+ };
352+}
353+
268354 // ─── Main readline function ───────────────────────────────────────────────────
269355
270356 const PROMPT = c.green("▶ ");
@@ -289,9 +375,26 @@ export async function readline(opts: {
289375 let savedInput = ""; // saved current line when navigating history
290376 let searchMode = false;
291377 let searchQuery = "";
292- let pasteBuffer: string | null = null; // full text of a pending paste
378+ const pasteTokens = new Map<string, string>();
293379 const imageAttachments: ImageAttachment[] = []; // images collected during this prompt
294380
381+ function prunePasteTokens(): void {
382+ const next = pruneInputPasteTokens(pasteTokens, buf, savedInput);
383+ pasteTokens.clear();
384+ for (const [token, text] of next) pasteTokens.set(token, text);
385+ }
386+
387+ function insertText(text: string): void {
388+ buf = buf.slice(0, cursor) + text + buf.slice(cursor);
389+ cursor += text.length;
390+ }
391+
392+ function insertPasteToken(text: string): void {
393+ const token = createPasteToken(buf, pasteTokens);
394+ pasteTokens.set(token, text);
395+ insertText(token);
396+ }
397+
295398 terminal.setRawMode(true);
296399 process.stdin.resume();
297400 process.stdout.write(BPASTE_ENABLE);
@@ -300,26 +403,13 @@ export async function readline(opts: {
300403
301404 function renderPrompt(): void {
302405 const cols = process.stdout.columns ?? 80;
303- // Replace paste sentinel and image labels with styled placeholders for display
304- const visualBuf = (
305- pasteBuffer
306- ? buf.replace(PASTE_SENTINEL, c.dim(pasteLabel(pasteBuffer)))
307- : buf
308- ).replace(/\[image: [^\]]+\]/g, (m) => c.dim(c.cyan(m)));
309-
310- // For cursor positioning we need the visual length without the sentinel substitution
311- const visualCursor = pasteBuffer
312- ? (() => {
313- const sentinelPos = buf.indexOf(PASTE_SENTINEL);
314- if (sentinelPos === -1 || cursor <= sentinelPos) return cursor;
315- // cursor is after the sentinel: shift by (label len - sentinel len)
316- return cursor - PASTE_SENTINEL_LEN + pasteLabel(pasteBuffer).length;
317- })()
318- : cursor;
319- const display =
320- visualBuf.length > cols - PROMPT_RAW_LEN - 2
321- ? `…${visualBuf.slice(-(cols - PROMPT_RAW_LEN - 3))}`
322- : visualBuf;
406+ const visualBuf = renderInputBuffer(buf, pasteTokens);
407+ const visualCursor = getVisualCursor(buf, cursor, pasteTokens);
408+ const { display, cursor: displayCursor } = buildPromptDisplay(
409+ visualBuf,
410+ visualCursor,
411+ Math.max(1, cols - PROMPT_RAW_LEN - 2),
412+ );
323413
324414 const prompt = opts.planMode
325415 ? PROMPT_PLAN
@@ -328,7 +418,7 @@ export async function readline(opts: {
328418 : PROMPT;
329419
330420 process.stdout.write(
331- `${CLEAR_LINE}${prompt}${display}${CSI}${PROMPT_RAW_LEN + visualCursor + 1}G`,
421+ `${CLEAR_LINE}${prompt}${display}${CSI}${PROMPT_RAW_LEN + displayCursor + 1}G`,
332422 );
333423 }
334424
@@ -343,6 +433,7 @@ export async function readline(opts: {
343433 buf = savedInput;
344434 }
345435 cursor = buf.length;
436+ prunePasteTokens();
346437 renderPrompt();
347438 }
348439
@@ -404,15 +495,12 @@ export async function readline(opts: {
404495 const imageResult = await tryExtractImageFromPaste(pasted, cwd);
405496 if (imageResult) {
406497 imageAttachments.push(imageResult.attachment);
407- buf = buf.slice(0, cursor) + imageResult.label + buf.slice(cursor);
408- cursor += imageResult.label.length;
498+ insertText(imageResult.label);
409499 renderPrompt();
410500 continue;
411501 }
412502
413- pasteBuffer = pasted;
414- buf = buf.slice(0, cursor) + PASTE_SENTINEL + buf.slice(cursor);
415- cursor += PASTE_SENTINEL_LEN;
503+ insertPasteToken(pasted);
416504 renderPrompt();
417505 continue;
418506 }
@@ -473,7 +561,7 @@ export async function readline(opts: {
473561 while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
474562 while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
475563 buf = buf.slice(0, cursor) + buf.slice(end);
476- if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
564+ prunePasteTokens();
477565 renderPrompt();
478566 continue;
479567 }
@@ -512,7 +600,7 @@ export async function readline(opts: {
512600 while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
513601 while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
514602 buf = buf.slice(0, cursor) + buf.slice(end);
515- if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
603+ prunePasteTokens();
516604 renderPrompt();
517605 continue;
518606 }
@@ -520,14 +608,14 @@ export async function readline(opts: {
520608 if (raw === CTRL_U) {
521609 buf = buf.slice(cursor);
522610 cursor = 0;
523- if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
611+ prunePasteTokens();
524612 renderPrompt();
525613 continue;
526614 }
527615
528616 if (raw === CTRL_K) {
529617 buf = buf.slice(0, cursor);
530- if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
618+ prunePasteTokens();
531619 renderPrompt();
532620 continue;
533621 }
@@ -549,7 +637,7 @@ export async function readline(opts: {
549637 if (cursor > 0) {
550638 buf = buf.slice(0, cursor - 1) + buf.slice(cursor);
551639 cursor--;
552- if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
640+ prunePasteTokens();
553641 renderPrompt();
554642 }
555643 continue;
@@ -580,11 +668,8 @@ export async function readline(opts: {
580668
581669 // ── Enter — submit ────────────────────────────────────────────────────
582670 if (raw === ENTER || raw === NEWLINE) {
583- // Expand paste sentinel before trimming/submitting
584- const expanded = pasteBuffer
585- ? buf.replace(PASTE_SENTINEL, pasteBuffer)
586- : buf;
587- pasteBuffer = null;
671+ const expanded = expandInputBuffer(buf, pasteTokens);
672+ pasteTokens.clear();
588673 const text = expanded.trim();
589674 process.stdout.write("\n");
590675 buf = "";
@@ -625,35 +710,32 @@ export async function readline(opts: {
625710 };
626711 }
627712
628- // ── Paste detection ───────────────────────────────────────────────────
629- // A chunk with more than one character that isn't an escape sequence is
630- // almost certainly a clipboard paste. Capture it and show a placeholder.
713+ // ── Multi-char chunks ──────────────────────────────────────────────────
714+ // Bracketed paste is handled above. Other multi-char chunks are usually
715+ // coalesced typing or a non-bracketed paste from a terminal that does not
716+ // support bracketed paste.
631717 if (raw.length > 1) {
632- // Strip trailing newline that some terminals append on paste
633- const pasted = raw.replace(/\r?\n$/, "");
634-
635- // Check if this is an image (data URL or image file path)
636- const imageResult = await tryExtractImageFromPaste(pasted, cwd);
718+ const chunk = raw.replace(/\r?\n$/, "");
719+ const imageResult = await tryExtractImageFromPaste(chunk, cwd);
637720 if (imageResult) {
638721 imageAttachments.push(imageResult.attachment);
639- // Insert the plain label into buf (no sentinel — nothing to expand at submit)
640- buf = buf.slice(0, cursor) + imageResult.label + buf.slice(cursor);
641- cursor += imageResult.label.length;
722+ insertText(imageResult.label);
642723 renderPrompt();
643724 continue;
644725 }
645726
646- pasteBuffer = pasted;
647- buf = buf.slice(0, cursor) + PASTE_SENTINEL + buf.slice(cursor);
648- cursor += PASTE_SENTINEL_LEN;
727+ if (chunk.includes("\n") || chunk.includes("\r")) {
728+ insertPasteToken(chunk);
729+ } else {
730+ insertText(chunk);
731+ }
649732 renderPrompt();
650733 continue;
651734 }
652735
653736 // ── Printable characters ──────────────────────────────────────────────
654737 if (raw >= " " || raw === "\t") {
655- buf = buf.slice(0, cursor) + raw + buf.slice(cursor);
656- cursor++;
738+ insertText(raw);
657739 renderPrompt();
658740 }
659741 }