Frankly, I'd say that the very idea that composers of the late baroque had a conceptual apparatus centered around our modern diatonic scales is something approaching an urban legend, promoted by modern theory textbooks that prefer to spend a semester working on "diatonic" theory without the complexities of chromatic notes.
Actual historical practicing composers learned to use chromatic notes from their very first lessons. Look at Mozart's earliest minuets and pieces composed at the age of five, and the first thing you'll see is a cadence to V including a raised scale degree 4 in the middle. That's literally how music of the late baroque and classical periods worked. You can see it in simple exercises learned by beginning keyboardists in the late baroque, such as the rule of the octave, which contained harmonizations for each degree of the bass scale going up and down. Such basic harmonizations inevitably contained at least one chromatic note (the raised 4) and frequently contained more. These were the very simplest patterns that young keyboardists and musicians would have learned about harmony. Very soon after learning the raised 4 to lead toward a dominant, composers would learn a lowered 7 to lead toward the subdominant key (as in the first example in the question). They'd likely learn these inflections even in simple two-part counterpoint before even understanding any detailed theory of chords.
I know this will probably sound controversial to most people reading this, but diatonicism for Bach is a myth. Theory textbooks have often struggled for the past century or more to find even a phrase or two of a Bach chorale that they can use for the first semester of "diatonic theory." It's exceptionally rare to see more than a few phrases of Bach's music in a row that stick to our "major scale," and the minor scale is always fluctuating, realistically containing at least 10 of the 12 chromatic notes in the octave as "standard" notes (including lowered and raised versions of scale degrees 6 and 7, as well as the raised 4 to lead to a strong dominant; the raised version of the third scale degree also is a frequent turn toward subdominant, and flat-2 is found as a common Neapolitan, so minor is really about all the notes of the chromatic scale by the time of Bach).
That's not to say that composers of the baroque didn't learn the abstractions we now think of as "major scales" and what we'd now call "melodic minor," but those were one of many simple patterns of exercises that beginning keyboardists would learn to harmonize with. They'd rapidly branch out into chromatic versions, which is how little Mozart would know to use the #4 scale degree to make a strong middle cadence.
In sum, the question shouldn't really be "what's the theory behind Bach's deviations from the scale." Instead, we should be shocked and surprised when Bach maintains total diatonicism for any extended amount of time, as that was not the norm for him or for most composers in the late baroque. I don't think composers of the time assumed diatonicism as a norm at all.