fix: Bug - Sending files and attachment issues (cross-transport auto-download race)

Re-queue attachment auto-downloads when the chat message arrives, since
file-announce (WebRTC) can beat chat-message (websocket) and the announce-time
pass gives up on an unknown room. Gate stalled-download resets on chunk-progress
staleness so an active transfer is never cancelled mid-stream, which deadlocked
the retry against the sender's active-transfer dedupe.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-12 21:26:59 +02:00
co-authored by Cursor
parent 0078c320a5
commit 497033aff0
7 changed files with 118 additions and 16 deletions
@@ -1,20 +1,52 @@
import { shouldResetStalledAttachmentDownload } from './attachment-autodownload.rules';
import { ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS, shouldResetStalledAttachmentDownload } from './attachment-autodownload.rules';
const NOW_MS = 1_750_000_000_000;
describe('attachment autodownload rules', () => {
it('resets stalled partial downloads that are no longer actively transferring', () => {
it('does not reset an actively transferring download with recent chunk progress', () => {
// Regression: the pending-request marker is deleted on the first received
// chunk, so an in-flight multi-chunk transfer has receivedBytes > 0 and no
// pending request - it must NOT be treated as stalled while chunks flow.
expect(shouldResetStalledAttachmentDownload({
available: false,
receivedBytes: 128
}, false)).toBe(true);
receivedBytes: 128,
lastUpdateMs: NOW_MS - 100
}, false, NOW_MS)).toBe(false);
});
it('resets partial downloads with no progress past the stall threshold', () => {
expect(shouldResetStalledAttachmentDownload({
available: false,
receivedBytes: 128,
lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1
}, false, NOW_MS)).toBe(true);
});
it('treats partial downloads without a progress timestamp as stalled', () => {
expect(shouldResetStalledAttachmentDownload({
available: false,
receivedBytes: 128
}, true)).toBe(false);
}, false, NOW_MS)).toBe(true);
});
it('never resets downloads with a pending request or already available', () => {
expect(shouldResetStalledAttachmentDownload({
available: false,
receivedBytes: 128,
lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1
}, true, NOW_MS)).toBe(false);
expect(shouldResetStalledAttachmentDownload({
available: true,
receivedBytes: 128
}, false)).toBe(false);
receivedBytes: 128,
lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1
}, false, NOW_MS)).toBe(false);
});
it('never resets downloads that have not received any bytes', () => {
expect(shouldResetStalledAttachmentDownload({
available: false,
receivedBytes: 0
}, false, NOW_MS)).toBe(false);
});
});