From d284b6a3924d93a38a51903e417d76336be5a504 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Wed, 23 Sep 2026 20:05:00 -0700 Subject: [PATCH 1/5] test: cover fast paste for mixed plain and HTML clipboard --- tests/test_plain_text_paste_worker.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_plain_text_paste_worker.py b/tests/test_plain_text_paste_worker.py index 2e88c0d5e9ff..611e30de34c2 100644 --- a/tests/test_plain_text_paste_worker.py +++ b/tests/test_plain_text_paste_worker.py @@ -92,6 +92,13 @@ def test_unicode_multiline_and_repeat(self): print(json.dumps(dict(helper_startup_ms=ms, directory=directory_index, repetition=repetition, size=Path(HELPER).stat().st_size)), flush=True) + def test_plain_text_with_html_uses_fast_path(self): + text = "hello\n日本語 🦀\n" + board = self.board(text=text, extra={"public.html": "

rich

"}) + result = self.result(self.directory(board)) + self.assertEqual(result["textPayload"]["destination"], {"terminal": {}}) + self.assertEqual((self.workdirs[-1] / "text-payload.txt").read_bytes(), text.encode()) + def test_rich_images_and_auxiliary_urls_delegate_without_provider_read(self): for flavor in ["public.html", "public.rtf", "com.apple.flat-rtfd", "public.png", "public.tiff", "public.jpeg", "public.file-url", "public.url", From a9c54baa8e34d2a1d52c3cc525938e61b173d6c9 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Wed, 23 Sep 2026 20:05:19 -0700 Subject: [PATCH 2/5] fix: keep mixed rich text paste on the fast plain-text path --- workers/cmux-paste-text/main.m | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/workers/cmux-paste-text/main.m b/workers/cmux-paste-text/main.m index b01dfb315c36..af4f5a5d1478 100644 --- a/workers/cmux-paste-text/main.m +++ b/workers/cmux-paste-text/main.m @@ -121,10 +121,7 @@ static BOOL isPlainTextType(NSString *typeIdentifier) { } static BOOL hasDisallowedType(NSString *typeIdentifier) { - if ([typeIdentifier isEqualToString:NSPasteboardTypeHTML] || - [typeIdentifier isEqualToString:NSPasteboardTypeRTF] || - [typeIdentifier isEqualToString:NSPasteboardTypeRTFD] || - [typeIdentifier isEqualToString:NSPasteboardTypeFileURL] || + if ([typeIdentifier isEqualToString:NSPasteboardTypeFileURL] || [typeIdentifier isEqualToString:NSPasteboardTypeURL] || [typeIdentifier isEqualToString:@"NSFilenamesPboardType"] || [typeIdentifier isEqualToString:@"com.apple.pasteboard.promised-file-url"] || @@ -190,13 +187,12 @@ static int runWorker(NSArray *arguments) { NSArray *types = pasteboard.types ?: @[]; BOOL hasPlainText = NO; + BOOL hasDisallowedPayload = NO; for (NSPasteboardType type in types) { - if (hasDisallowedType(type)) { - return kIneligibleStatus; - } hasPlainText = hasPlainText || isPlainTextType(type); + hasDisallowedPayload = hasDisallowedPayload || hasDisallowedType(type); } - if (!hasPlainText) { + if (!hasPlainText || hasDisallowedPayload) { return kIneligibleStatus; } From 5d8258b19b01f0a890b1eed6dd49613cf8ae4622 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 24 Sep 2026 13:41:55 -0700 Subject: [PATCH 3/5] test: preserve rich paste fallback and text fidelity --- tests/test_plain_text_paste_worker.py | 42 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/test_plain_text_paste_worker.py b/tests/test_plain_text_paste_worker.py index 611e30de34c2..a2de9e35993c 100644 --- a/tests/test_plain_text_paste_worker.py +++ b/tests/test_plain_text_paste_worker.py @@ -33,8 +33,8 @@ def tearDown(self): def board(self, text="hello\n日本語 🦀 e\u0301\r\n", extra=None, behavior=None): identity = str(uuid.uuid4()) - config = dict(name="cmux-paste-test-" + identity, text=text, - representations={"public.utf8-plain-text": text, **(extra or {})}, + config = dict(name="cmux-paste-test-" + identity, text=text or "", + representations={**({"public.utf8-plain-text": text} if text is not None else {}), **(extra or {})}, ready=str(self.root / (identity + "-ready.json")), requested=str(self.root / (identity + "-requested"))) if behavior: @@ -92,15 +92,39 @@ def test_unicode_multiline_and_repeat(self): print(json.dumps(dict(helper_startup_ms=ms, directory=directory_index, repetition=repetition, size=Path(HELPER).stat().st_size)), flush=True) - def test_plain_text_with_html_uses_fast_path(self): - text = "hello\n日本語 🦀\n" - board = self.board(text=text, extra={"public.html": "

rich

"}) - result = self.result(self.directory(board)) - self.assertEqual(result["textPayload"]["destination"], {"terminal": {}}) - self.assertEqual((self.workdirs[-1] / "text-payload.txt").read_bytes(), text.encode()) + def test_plain_text_with_rich_flavors_uses_fast_path(self): + for flavor in ["public.html", "public.rtf"]: + for text in ["hello\n日本語 🦀 e\u0301\r\n", "Question?", "文本\n" * 100_000]: + with self.subTest(flavor=flavor, bytes=len(text.encode())): + board = self.board(text=text, extra={flavor: "unused rich text"}) + path = self.directory(board) + result = self.result(path) + self.assertEqual(result["textPayload"]["destination"], {"terminal": {}}) + self.assertEqual((path / "text-payload.txt").read_bytes(), text.encode()) + + def test_rich_only_and_empty_plain_text_delegate(self): + for flavor in ["public.html", "public.rtf", "com.apple.flat-rtfd"]: + for text in [None, ""]: + with self.subTest(flavor=flavor, text=text): + board = self.board(text=text, extra={flavor: "rich fallback"}) + self.result(self.directory(board), expected=73) + + def test_lossy_plain_text_with_rich_flavors_delegates(self): + for flavor in ["public.html", "public.rtf"]: + for text in ["??", "text\ufffd", "日本語??"]: + with self.subTest(flavor=flavor, text=text): + board = self.board(text=text, extra={flavor: "rich fallback"}) + self.result(self.directory(board), expected=73) + + def test_loss_markers_without_rich_text_remain_literal(self): + text = "??\ufffd" + board = self.board(text=text) + path = self.directory(board) + self.result(path) + self.assertEqual((path / "text-payload.txt").read_bytes(), text.encode()) def test_rich_images_and_auxiliary_urls_delegate_without_provider_read(self): - for flavor in ["public.html", "public.rtf", "com.apple.flat-rtfd", "public.png", + for flavor in ["com.apple.flat-rtfd", "public.png", "public.tiff", "public.jpeg", "public.file-url", "public.url", "NSFilenamesPboardType", "com.apple.pasteboard.promised-file-url"]: with self.subTest(flavor=flavor): From f3f43ed7d470801f99827f2c49ae7f2288e9f01a Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 24 Sep 2026 13:42:51 -0700 Subject: [PATCH 4/5] fix: retain rich text fallback for lossy paste data --- workers/cmux-paste-text/main.m | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/workers/cmux-paste-text/main.m b/workers/cmux-paste-text/main.m index af4f5a5d1478..d08105a4ce7b 100644 --- a/workers/cmux-paste-text/main.m +++ b/workers/cmux-paste-text/main.m @@ -121,6 +121,8 @@ static BOOL isPlainTextType(NSString *typeIdentifier) { } static BOOL hasDisallowedType(NSString *typeIdentifier) { + // RTFD may carry attachments even alongside plain text. Keep its existing + // rich-text/image selection policy in the full worker. if ([typeIdentifier isEqualToString:NSPasteboardTypeFileURL] || [typeIdentifier isEqualToString:NSPasteboardTypeURL] || [typeIdentifier isEqualToString:@"NSFilenamesPboardType"] || @@ -187,12 +189,16 @@ static int runWorker(NSArray *arguments) { NSArray *types = pasteboard.types ?: @[]; BOOL hasPlainText = NO; - BOOL hasDisallowedPayload = NO; + BOOL hasRichText = [types containsObject:NSPasteboardTypeHTML] || + [types containsObject:NSPasteboardTypeRTF] || + [types containsObject:NSPasteboardTypeRTFD]; for (NSPasteboardType type in types) { + if (hasDisallowedType(type)) { + return kIneligibleStatus; + } hasPlainText = hasPlainText || isPlainTextType(type); - hasDisallowedPayload = hasDisallowedPayload || hasDisallowedType(type); } - if (!hasPlainText || hasDisallowedPayload) { + if (!hasPlainText) { return kIneligibleStatus; } @@ -217,10 +223,25 @@ static int runWorker(NSArray *arguments) { return 0; } if (text == nil) { + if (hasRichText) { + return kIneligibleStatus; + } writeResponse(workingDirectory, NO); return 0; } + // Match PasteboardTextFidelity.shouldInspectRichTextForPlainTextLoss: + // the full worker can recover characters lost by the plain-text exporter. + if (hasRichText) { + NSUInteger questionMarks = 0; + for (NSUInteger index = 0; index < text.length; index++) { + unichar character = [text characterAtIndex:index]; + if (character == 0xFFFD || (character == '?' && ++questionMarks >= 2)) { + return kIneligibleStatus; + } + } + } + NSData *payload = [text dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; if (payload == nil || payload.length > kMaximumTextByteCount) { return 74; From f6df46e0835351bdde6ceda598594ac76e45d455 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 24 Sep 2026 13:44:04 -0700 Subject: [PATCH 5/5] fix: retain rich text fallback for lossy paste data --- tests/test_plain_text_paste_worker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_plain_text_paste_worker.py b/tests/test_plain_text_paste_worker.py index a2de9e35993c..477a089a26c8 100644 --- a/tests/test_plain_text_paste_worker.py +++ b/tests/test_plain_text_paste_worker.py @@ -124,7 +124,7 @@ def test_loss_markers_without_rich_text_remain_literal(self): self.assertEqual((path / "text-payload.txt").read_bytes(), text.encode()) def test_rich_images_and_auxiliary_urls_delegate_without_provider_read(self): - for flavor in ["com.apple.flat-rtfd", "public.png", + for flavor in ["public.png", "public.tiff", "public.jpeg", "public.file-url", "public.url", "NSFilenamesPboardType", "com.apple.pasteboard.promised-file-url"]: with self.subTest(flavor=flavor):