fix(highway): force full video upload when server fast-paths a stale resource (#145)

Group/c2c video resources expire server-side. When the bot re-sends a
video whose md5 the server already cached, the NTV2 OIDB returns no uKey
for the main file (fast-upload hit) and SnowLuma trusted it — the send
"succeeds" but the receiver sees 资源已过期, because the reused object had
expired. Confirmed by the reporter: same MP4 fast-paths and breaks, but
mutating one byte (new md5) forces a full upload and plays fine.

Add a per-sub `forceFullOnFastPath` flag on MediaSubFileUpload. When a
flagged sub-file (video main) carries real bytes but the server fast-paths
it, runNtv2Upload re-issues the OIDB request with tryFastUploadCompleted
disabled (proton drops a `false` bool, which the server reads as "no
fast-upload"), forcing a fresh resource + full PUT. The thumb stays
fast-path-OK so a cached thumb never re-pushes the main video; forwarding
paths carry no bytes so they never trigger the retry.

Also fixes the observability gap the reporter hit: the main file being
silently skipped on a fast-path now logs explicitly, and so does the
forced-retry, instead of only the thumb PUT showing up.

Note: the retry relies on the server treating an absent
tryFastUploadCompleted as "no fast-upload" — needs real-device
confirmation that the forced round actually returns a uKey.
This commit is contained in:
DrSmoothl committed 2026-06-25 20:52:11 +08:00
1 parent 78b0e1a124
commit 3eba2a677c
4 files changed
+247 -73

No files matched your search

@@ -283,6 +283,113 @@ describe('pipeline — runNtv2Upload', () => {
}))).rejects.toThrow(/image fast-upload not available/);
});
// ─────────── forceFullOnFastPath (video stale-resource fallback, #145) ───────────
// Bridge whose sendRawPacket returns a different OIDB response per call,
// so we can model "round 1 fast-paths the main file, round 2 demands the
// bytes after we disable fast-upload".
function makeSequencedBridge(responses: Buffer[]) {
let call = 0;
const sendRawPacket = vi.fn<(cmd: string, body: Uint8Array) => Promise<SendPacketResult>>(
async () => {
const responseData = responses[Math.min(call, responses.length - 1)]!;
call += 1;
return { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
},
);
return { sendRawPacket };
}
it('forceFullOnFastPath: re-requests with fast-upload disabled when the flagged main file is fast-pathed', async () => {
const bridge = makeSequencedBridge([
// Round 1: server fast-paths the main file (no uKey) but demands nothing.
encodeOidbResponse({ upload: { msgInfo: { msgInfoBody: [], extBizInfo: {} } /* no uKey */ } }),
// Round 2 (fast-upload disabled): server now hands back a uKey.
encodeOidbResponse({
upload: {
uKey: 'fresh-ukey',
ipv4s: [{ ipv4: '1.2.3.4', port: 80 }],
msgInfo: { msgInfoBody: [], extBizInfo: {} },
},
}),
]);
const upload = await runNtv2Upload(baseParams({
bridge,
label: 'video',
uploads: [{
source: 'top',
cmdId: 1003,
bytes: new Uint8Array([1, 2, 3]),
md5: new Uint8Array(16),
sha1: new Uint8Array(20),
forceFullOnFastPath: true,
}],
}));
// Two OIDB requests went out; the second omits tryFastUploadCompleted
// (proton drops `false`), which the server reads as "no fast-upload".
expect(bridge.sendRawPacket).toHaveBeenCalledTimes(2);
const firstReq: any = protobuf_decode<OidbBase<NTV2UploadRichMediaReq>>(bridge.sendRawPacket.mock.calls[0]![1] as Uint8Array);
const secondReq: any = protobuf_decode<OidbBase<NTV2UploadRichMediaReq>>(bridge.sendRawPacket.mock.calls[1]![1] as Uint8Array);
expect(firstReq.body.upload.tryFastUploadCompleted).toBe(true);
expect(secondReq.body.upload.tryFastUploadCompleted ?? false).toBe(false);
// The forced full upload PUT ran, and the returned upload is round 2's.
expect(highway.uploadHighwayHttp).toHaveBeenCalledOnce();
expect(upload.uKey).toBe('fresh-ukey');
});
it('forceFullOnFastPath: no retry when the flagged file gets a uKey on the first try', async () => {
const bridge = makeBridge({
responseData: encodeOidbResponse({
upload: { uKey: 'ukey-1', msgInfo: { msgInfoBody: [], extBizInfo: {} } },
}),
});
await runNtv2Upload(baseParams({
bridge,
uploads: [{
source: 'top', cmdId: 1003, bytes: new Uint8Array([1, 2, 3]),
md5: new Uint8Array(16), sha1: new Uint8Array(20), forceFullOnFastPath: true,
}],
}));
expect(bridge.sendRawPacket).toHaveBeenCalledOnce();
expect(highway.uploadHighwayHttp).toHaveBeenCalledOnce();
});
it('forceFullOnFastPath: no retry for a fast-pathed forward (no bytes to upload)', async () => {
const bridge = makeBridge({
responseData: encodeOidbResponse({ upload: { msgInfo: { msgInfoBody: [], extBizInfo: {} } } }),
});
await runNtv2Upload(baseParams({
bridge,
uploads: [{
source: 'top', cmdId: 1003, bytes: new Uint8Array(0),
md5: new Uint8Array(16), sha1: new Uint8Array(20), forceFullOnFastPath: true,
}],
}));
// bytes.length === 0 → nothing to distrust; the fast-path stands.
expect(bridge.sendRawPacket).toHaveBeenCalledOnce();
expect(highway.uploadHighwayHttp).not.toHaveBeenCalled();
});
it('a fast-pathed sub-file WITHOUT the flag (e.g. thumb) does not trigger a retry', async () => {
const bridge = makeBridge({
responseData: encodeOidbResponse({
// Main fully uploaded (has uKey); thumb fast-pathed (no subFileInfos).
upload: { uKey: 'main-ukey', msgInfo: { msgInfoBody: [], extBizInfo: {} } },
}),
});
await runNtv2Upload(baseParams({
bridge,
uploads: [
{ source: 'top', cmdId: 1001, bytes: new Uint8Array([1]), md5: new Uint8Array(16), sha1: new Uint8Array(20), forceFullOnFastPath: true },
{ source: 0, cmdId: 1002, bytes: new Uint8Array([2]), md5: new Uint8Array(16), sha1: new Uint8Array(20) /* no flag */ },
],
}));
expect(bridge.sendRawPacket).toHaveBeenCalledOnce();
});
it('silently skips a sub-file with empty bytes when no fastOnlyError is configured', async () => {
// This mirrors the video-thumb path where bytes are always synthesised
// (FALLBACK_THUMB) so we don't expect the check to fire even if the
@@ -80,6 +80,16 @@ describe('video-upload', () => {
expect(uploads[1]!.fastOnlyError).toBeUndefined();
});
it('main file distrusts a server fast-path (forceFullOnFastPath); thumb does not (#145)', async () => {
// Group/c2c video resources expire server-side: a fast-path hit can
// reference a stale object the receiver shows as "资源已过期", so the
// main file forces a fresh full upload. The thumb stays fast-path-OK.
await uploadVideoMsgInfo({} as any, true, 12345, FINGERPRINT);
const uploads = vi.mocked(pipeline.runNtv2Upload).mock.calls[0]![0].uploads;
expect(uploads[0]!.forceFullOnFastPath).toBe(true);
expect(uploads[1]!.forceFullOnFastPath).toBeUndefined();
});
it('uploadInfo carries TWO entries (main mp4 + thumb jpg) with subFileType 0 and 100', async () => {
await uploadVideoMsgInfo({} as any, true, 12345, FINGERPRINT);
const uploadInfo = vi.mocked(pipeline.runNtv2Upload).mock.calls[0]![0].uploadInfo;