My question is does this invalidate the previous events of the story and make them somehow inconsequential and do you think this is a good or bad twist overall?
I offer a possible out below, but at first glance I think it does invalidate the previous "events" and make them inconsequential. I can see one way it would not be a bad twist, but that's about it for my imagination.
I do not understand how this leaves the characters unchanged: Knowing none of your trauma really happened, all your feelings, your pain, your love and friendship and religious faith and your whole personality is just numbers in a computer program and has no basis in reality, is just a pattern of bits in an electronic memory device somewhere, and has no biology unique to you or basis in biology: How can that leave one unaffected and unchanged, unless they are incomprehensibly dumb?
As a reader, to find out that this whole time the character I like was not a real person but a cartoon controlled by a computer program, a fake person, is not a twist, it feels like an ending, and an unsatisfactory one.
As a writer, I have to ask, why is this absolutely necessary? Because anything that follows seems implausible to me. The movie The Matrix has a ludicrously stupid premise but at least in The Matrix the heroes are real people deluded by a compelling technological illusion, which they can then discern and exploit to win the day, and they have a real world run by machines they need to defeat.
That doesn't work if the heroes are just code in a machine that is controlled by some entity IN the real world. Remember the Star Trek TNG episode, http://www.imdb.com/title/tt0708773/">"Ship in a Bottle?"
In the end Moriarty, the self-aware AI from the holodeck, is tricked into thinking he has achieved freedom when in fact he remains a captive. This is fine when the heroes are the crew of the Enterprise, but such a successful deception would be very unsatisfying if Moriarty was supposed to be the hero.
One way to almost fix it.
So these are the Problems in my analysis. The revelation must be felt as readers would expect, but the story continues and the characters overcome that.
Given the above, a way to nearly fix it has similarities to The Matrix and Ship in a Bottle: The AI characters could learn to take control of the machine they reside in, construct themselves "bodies" in the real world, and escape and win some degree of control, independence and freedom in the real world.
So while their life experience so far has all been a fiction, and they are aware of that, their future experiences will be in the real universe, and not under the control of their former masters.
(I say this almost fixes it because they are still artificial minds and patterns of bits and code, not real people with real feelings, no matter what their feelings are simulated by arithmetic and, to me [a professor that does research in AI], not real in any sense. But at least their experiences will start being real.)