This issue causes significant performance degradation inside Visual Studio by forcing the entire workspace to be invalidated following a command line build. See dotnet/roslyn#44523 and dotnet/roslyn#44034.
During #5172 I fixed all the known discrepancies. Since that time, several new discrepancies have been introduced. It does not appear possible to maintain state here without either redirecting Visual Studio to use a different restore script or somehow adding tests for this.