You need to stop thinking scales (especially the pentatonic) and start thinking chords. Melody loves chord tones, so you need to spend some time seeing chords for what they really are: selections of notes that occupy the whole fretboard, not just those little chord diagrams. Once you can do that, you can use those notes (for each chord as it comes and goes in the progression) as your main melody notes. When you think along those lines, you never have to worry about outside chords or key changes ... you're always just working around the 'chord of the moment' as I call it, always on track. Of course, you need to know the progression and you need to think ahead a bit, but that's the way I've done it for decades now.