Is there any legal protection for actions that later become illegal? For example, lets say that being a software developer is a legal occupation right now. But 2 years from now, it becomes illegal to develop software. Can you be prosecuted for work that you've done in the past that has only recently become illegal? Are there any legal protections / immunity currently in place to prevent this type of situation?
This is known as a retroactive or ex post facto law. Such laws are explicitly forbidden by the US Constitution (Wikipedia reference), and are generally frowned on in jurisdictions where the rule of law applies, partly because it is difficult to prove criminal intent when your action was not at the time criminal.
The legal issues are too black and white to have any meaningful impact on an ethics discussion. The IP belongs to A and B can't use it. A's not planning to build a product around the patent is irrelevant to the law and I do not see it bring up any ethical issue, but some people who do not understand patent law might. There might be ethics issues if A hired Josh with the specific plan to fire him as soon as the application was filed and intentionally misled him about these plans. Another issue might be Josh's duty to not disclose A's confidential information (the application does not become public right away), and Josh's duty to not draw B into developing an infringing product.
If you want to protect yourself or any property interests you may have in this circumstance you have to talk to a lawyer. You cannot get (or trust) legal advice from the internet. I will, however, make the following personal observations: I only provide a W-9 to people who are paying me money, and who request it as a condition of paying me. I don't know of a legal requirement to supply it after the fact. However, not providing it to someone who did pay you more than $600 in a tax year could certainly make it difficult for them to comply with their tax filing obligations. I do not sign any agreements, assignments, or contracts, without what I consider to be fair consideration. E.g., if on leaving a job (as has happened) I am asked by a former employer to sign something that I am not already obligated to sign due to some prior contract, then I negotiate what is commonly called a "severance package." They pay me and/or extend benefits, and I sign something that limits their liability.
Yes and no. [note, the following is all written about US law. In other jurisdictions laws are, of course, different (though usually not drastically so.)] In the US there are (at least) three different bodies of law that might apply to code: copyright, patents, trade secrets. Copyright covers original expression. Anything you write is automatically, immediately protected under copyright. The copyright applies to the code itself, and anything "derived" from that code. It's up to the courts to decide exactly what "derived" means. One case that's long been viewed as a landmark in this area is Gates Rubber v. Bando Chemicals. The Court of Appeals for the tenth Circuit decision includes a section titled: "The Test for Determining Whether the Copyright of a Computer Program Has Been Infringed." Note that you can register a copyright, and that can be worthwhile, such as helping recover some damages you can't otherwise. Patents are quite different from copyrights. Where a copyright covers expression of an idea, a patent covers a specific invention. Rather than being awarded automatically, a patent has to be applied for, and awarded only after the patent office has determined that there's no relevant prior art to prevent it from being awarded. A patent, however, covers things like somebody else independently discovering/inventing what's covered by the patent. A trade secret could (at least theoretically) apply to some process or procedure embodied in the code. A trade secret mostly applies to a situation where (for example) you're trying to form an alliance with some other company, and in the process tell them things you don't tell the general public. If you've identified the fact that what you're telling them is a trade secret, and they then tell a competitor (or the general public, etc.) or more generally use that information in any way other than the originally intended purpose, it could constitute a trade secret violation. As a side-note: patents and copyright fall under federal law, so they're basically uniform nation-wide. Trade secrets mostly fall under state law, so the exact details vary by state. Absent a reason to believe otherwise, I'd guess your interest here is primarily in copyright infringement. The key here would be showing that one piece of code was derived from the other. That is, it specifically would not apply in a case where there were only a limited number of ways of doing something, so anybody who wanted to do that had to use one of those ways. Since this would not indicate actual derivation, it would not indicate copyright violation.
As you have agreed, by contract, not to reverse engineer the product, technically it would be a "breach of contract" to do so, assuming such terms are enforceable where you live (or wherever the EULA selects as the choice of forum).
It does mean that you cannot reuse any parts of the source code, even small simple ones. You would have to rebuild the code from scratch. There is a significant chance that the code would be "substantially similar" to the code that you were hired to build, also that if anybody else were to write a bubble sort or 24-to-am/pm conversion routine, it would look the same, where even variable names (which should describe function) are the same or very similar. In case of an infringement lawsuit, you would have to defend yourself by showing that there are only a few ways to code a given function. Copyright protects only the "expression", not the abstract idea. A linked list is an idea, which can't be protected by copyright; same with recursion, pointers, stacks, object-oriented programming... Anything that involves copy and paste is infringement. If you re-read the copyrighted code and then try to reconstruct it, you probably crossed the infringement line. If you remember the problems and solutions and accidentally write somewhat similar code, that is probably on the safe side. From the perspective of the programmer not wanting to always reinvent the wheel, it would be most useful to make a distinction in the contract between "the essentials of the customer's program" versus "incidental utility work". The difficulty will arise in saying specifically what is essential vs. incidental. For instance, I know that if I were to hire you to develop a speech-recognition system, low-level audio-acquisition and encoding would not be essential to my purpose, whereas DSP parsing routines would be the center of my interest. The programmer would then want to retain recycling rights to all non-essential code.
So-called AI software does not enjoy a special legal status (at present: one never knows what new law might be added). The question of whether any software can be distributed "safely" or "responsibly" is also not a legal issue. Nor is "true sentience" a relevant consideration, and nothing is guaranteed. When you distribute software of any kind, there is an implied warranty that the product is "fit", and if software kills you, you may be able to sue the creator for negligence. A software creator may then want to disclaim liability, by saying "WARNING: THIS PROGRAM MAY KILL YOU. OCP IS NOT LIABLE FOR ANY INJURIES ARISING FROM USE OF THIS PRODUCT". This may or may not actually remove liability. In the UK "liability for negligence occasioning death or personal injury cannot be excluded", so such a disclaimer will not prevent a suit against the manufacturer. In the US, the issue is determined at the level of the state – here is a summary of the law in the states. Probably the primary question would be whether such a disclaimer is an unconscionable term, and the second question is whether the act constituted gross negligence (not simply "negligence"). Mississippi exceptionally does not allow disclaimers, but even then, it does allow disclaiming liability when it comes to computer hardware and software. A software disclaimer is not inherently unconscionable, though perhaps some specific disclaimer would be found to be. Courts typically disfavor disclaimers in the case of gross negligence, and again determining what constitutes "gross negligence" is determined on a state by state basis. If the act shows "reckless indifference to the rights of others" and "failure to use even slight care or conduct that is so careless as to show com­plete disregard for the rights and safety of others", then the act might be grossly negligent.
It is illegal to make copies of copyrighted materials without license. In the case of software, obviously it will be illegal to make copies by copying and installing the software without a license, but we are not talking about that. If I have a legitimate license of say Photoshop, and I start the application, parts or all of the code will be loaded into the RAM of my computer, which is a copy. According to copyright law, it is legal for me to make that copy. You are allowed to copy legitimately owned software into RAM to execute it. If your copy of Photoshop is illegal, and you start the application, the copy that is made into RAM is again copyright infringement. Having read the software license for the software that you get when you buy a Mac, it seems that if you steal my computer and just start the operating system, you are committing copyright infringement, and it seems that if you buy such a stolen computer and just start the operating system, you are committing copyright infringement as well, because the license that I received when I purchased the computer covers anyone using it with my permission, and covers anyone who legally buys the computer from me, but doesn't cover a thief. Now does this affect the work that you did? No, you have the full copyright on your work. Copyright law doesn't require that your tools are all used legitimately.