Keep asynchronous clipboard files with their original composer

This commit is contained in:
2026-11-08 00:10:34 +00:00
parent d9a99457cf
commit b08d458c4b
3 changed files with 60 additions and 6 deletions
@@ -248,7 +248,8 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
_seenAccepted = widget.composer.composerSendsAccepted;
_syncDraft();
}
if (oldWidget.paste != widget.paste ||
if (oldWidget.composer != widget.composer ||
oldWidget.paste != widget.paste ||
oldWidget.pasteTarget != widget.pasteTarget) {
_stopListeningForPaste();
_listenForPaste();
@@ -257,10 +258,17 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
void _listenForPaste() {
if (!_paste.isSupported) return;
final owner = widget.composer;
_stopPaste = _paste.listen(
onFiles: _onPastedFiles,
// File decoding is asynchronous. Keep an accepted paste on its original
// chat even if it becomes inactive, but never carry it to a replacement.
onFiles: (files) {
if (mounted && identical(widget.composer, owner)) _onPastedFiles(files);
},
canAccept: _canAcceptPaste,
onError: _onPasteError,
onError: (error) {
if (mounted && identical(widget.composer, owner)) _onPasteError(error);
},
onText: _onPastedText,
canAcceptText: _canAcceptTextPaste,
);
+6
View File
@@ -11,3 +11,9 @@ Verification:
- Full app browser fixture evidence: `/tmp/review/unfocused-paste-final/` and `/tmp/unfocused-paste-capture.json`. Uses in-process fixtures, synthetic events and private Chrome only. Synthetic paste validates routing/default-prevention; it does not claim trusted OS clipboard integration or native-device verification.
Harness limitation: importing full app widget dependencies into Flutter's Chrome test runner collides with SDK `host.dart.js` naming and hangs at loading. That attempt was interrupted and is not counted as passing. Browser bridge tests and a release-mode full app fixture avoid that test-server issue. No real chats or device clipboard were used. Work preserved for pipeline continuation; no commit, deployment or shared-service restart performed here.
Manager review: an accepted asynchronous file paste belongs to the original chat,
including when quick-switch makes it inactive while bytes decode. Listener
callbacks capture composer identity so decoded files/errors cannot attach to a
replacement composer that reuses the State; changing composer rebinds the listener.
Regressions cover both retained inactive ownership and stale-completion rejection.
@@ -297,9 +297,7 @@ void main() {
expect(fake.canAcceptText!(), isFalse);
});
testWidgets('paste is only taken by the active composer', (
tester,
) async {
testWidgets('paste is only taken by the active composer', (tester) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
@@ -312,6 +310,48 @@ void main() {
expect(fake.canAccept!(), isTrue);
});
testWidgets('accepted file paste stays with its original inactive chat', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
expect(fake.canAccept!(), isTrue);
final decodingFinished = fake.onFiles!;
await pumpWith(tester, paste: fake.source, pasteTarget: false);
decodingFinished([
_file('original.txt', [1]),
]);
await tester.pump();
expect(find.text('original.txt'), findsOneWidget);
});
testWidgets('late file decoding never attaches to a replacement composer', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
final decodingFinished = fake.onFiles!;
composer = ChatComposerController(
gateway,
'replacement',
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op/replacement',
);
controllers.add(composer);
await pumpWith(tester, paste: fake.source);
decodingFinished([
_file('wrong-chat.txt', [1]),
]);
await tester.pump();
expect(find.text('wrong-chat.txt'), findsNothing);
expect(fake.canAccept!(), isTrue);
fake.paste([
_file('replacement.txt', [2]),
]);
await tester.pump();
expect(find.text('replacement.txt'), findsOneWidget);
});
testWidgets('a paste during an in-flight prompt adds nothing', (
tester,
) async {