As the Vashta Nerada close in from one side and Proper Dave's animated suit moves in from the other, Song opens a hole in the wall with her squareness gun and she, the Doctor, Anita, Lux, and Other Dave escape just in time. The Girl is watching their exploits on TV, despite the fact her father can't see anything and thinks she's hallucinating.
The Girl switches channels to a channel showing Donna being taken to a countryside convalescence home, CAL. Dr. Moon meets with Donna in her room and explains that he's been treating her for two years. She doesn't seem to remember anything and he suggests they go down to the river and walk. They instantly appear there, momentarily confusing Donna. They meet another patient, Lee MacIvoy, who invites Donna fishing. Instantly (from Donna's perspective) they are on a fishing date the next day, and then seconds later getting married, and seconds after that having two children. Dr. Moon visits Donna at her home and they chat, and Donna talks about the time-jumps. Moon starts to reassure her but suddenly derezzes out and is replaced with an image of the Doctor, who calls out to her and complains about signal interference. He disappears as Dr. Moon reappears and tells her to forget seeing the Doctor and she immediately does.
The Doctor, Song, and the others taken refuge in an exterior room with sunlight coming in through the roof, but the sun is setting and time is running out. The Doctor uses his sonic screwdriver to detect the Vashta Nerada, which are starting to approach, but a signal interferes with his devices. He's angry at Donna's disappearance and Song says he has to trust her as his future companion. When he refuses, she whispers one word in his ear and he immediately accepts her. Realizing the signal interference is important, he notes that something new must be causing it as it was present earlier. They look up to see the planet's moon and Lux explains that it is a doctor moon that acts as a firewall and anti-virus program for the Library's computer core. The Doctor accidentally summons an image of Donna from his sonic screwdriver due to signal bleed over, but she disappears before he can say more than a few words. Anita speaks up, saying that she has two shadows. As everyone else dons their helmets, the Doctor tints her visor in the hope it will confuse the Vashta Nerada into believing some of them are already in the suit so they don't need to force entry. Proper Dave's animated corpse arrives and they're forced to retreat into the Library.
The Girl is watching everything on the television and switches to Donna, who is at her hoe with Lee and her two children, Ella and Josh. Donna glances outside and sees a black-robed woman in the street. When she says she's tired she finds herself instantly in her bedroom with Lee. She hears the door open downstairs and Lee goes to look. Donna glances outside and sees the woman in black walking away. Lee returns with a note saying "The world is wrong" and asking Donna to go to the park the next day. The Girl says Donna shouldn't go but Donna instantly finds herself at the park the next day with Ella and Josh. She sits next to the woman in black on a park bench, who points out the time discrepancy and that they've met before in the Library. Donna starts to remember and recognizes the voice as Miss Evangelista's.
The Doctor tries to talk to the Vashta Nerada through Proper Dave's telepathic link and Other Dave stays behind to protect him while Song and the others go ahead. The creatures finally manages to speak through the link and explain that the Library is their home. The Doctor realizes that they hatched from spores contained within the book covers. He wonders why the creatures are talking but realizes it's a distraction: they've killed Other Dave and animated his suit as well. The two suits move on the Doctor, who activates a trap door and drops through to the girders below.
The woman in black tells Donna that she called her to the playground for a reason, to force Donna to see something she's been programmed not to see. Donna remembers that Evangelista died in the Library, and Evangelista explains that she has no children: they're projected images. She tells Donna to concentrate and look hard, and Donna sees that all of the children playing are copies of Josh and Ella. Recoiling in shock, Donna pulls the woman in black's veil aside to reveal Evangelista's face: distorted and twisted.
Song and the others take refuge in a room and when Anita wonders what the problem is between Song and the Doctor, Song explains that the current Doctor is not the one she knows: "her" Doctor could send armies running and open the TARDIS doors with a snap of his fingers. The Doctor arrives, having eluded the Daves, and says it's not possible to open a TARDIS door that way. A worried Anita wonders how long she has left to live and asks the Doctor what the one word Song told him was because she could use a reassuring word. The Doctor says it was his secret name and Anita assures him it's safe with her. The Doctor picks up on the word "safe," realizing the message concerning the survivors said they were "saved," not "safe."
The twisted Evangelista figure explains to Donna that her face is distorted due to transcription errors and that the computer "saved" her. The Girl screams at the television, saying Evangelista can't give away the secret.
The Doctor checks the computer records and determines that a hundred years ago when the Library inhabitants disappeared, there was an enormous surge. The computer used the teleporters to save everyone in its hard drive.
Evangelista explains that her face is distorted because of transcription errors because she is a data ghost, not a real person teleported into the system the way that Donna and the other Library staff and visitors were. Donna's body was converted into an energy pattern and her brainwaves stored in the computer and given a virtual reality life inside a dream. When Donna wonders whose dream it is, Evangelista says it is CAL's dream. The Girl changes channels to try and stop Evangelista from saying more, and Donna's daughter Ella bumps her knee. Donna goes to her but Evangelista says she has to ignore them, that they're not real. The Girl screams at Evangelista at the television screen, and when her dad interferes she yells at him and points the remote at him, making him vanish. Shocked and angry, she throws down the remote. In the Library, lights start glowing red and the computer system announces that auto-destruct has been engaged for 20 minutes.
Donna goes home with her children but everything glows red. Dr. Moon appears to the Girl at her home and says that she saved everyone... but then forgot. She refuses to listen and shuts him off. In the real world, the Doctor loses the computer connection. Lux, realizing what's going on, says they have to get to the computer core to save CAL.
Donna puts her children to bed but they worry that they sometimes seem to disappear when she's not looking at them. She says she'll always be there to look at them, but when she turns away for a second, they disappear. She collapses to her knees, crying.
With time running out, the Doctor, Song, Anita, and Lux get to the central computer core. They can hear the Girl crying for help and the Doctor determines the computer is in sleep mode. He tries to reactivate it and Lux activates a Node with the face of the Girl. He explains that the Girl is CAL: Charlotte Abigail Lux, his grandfather's youngest daughter. She died generations ago but the Lux family created the Library and put her mind inside of it. Charlotte liked to read so they made sure she had all the books she could ever want and a virtual reality to enjoy them in. They also created the doctor moon to watch over her. However, then the Vashta Nerada emerged from the books and attacked the Library residents, Charlotte teleported them all into herself. The extra data overwhelmed her own memory systems, causing her to forget. She also lacks the memory to teleport them back out now that she does remember.
The Doctor works to use his own memory to boost the computer systems and rescue everyone. Anita asks what will happen to the Vashta Nerada and the Doctor says they can roam as much as they want. He turns to her and de-tints her helmet, revealing a skeleton. He knew that the Vashta Nerada had consumed her some time ago and were using her corpse and the telepathic link to monitor him. They prepare to kill him but he tells them to look him up in the Library database, which contain the sum total of human knowledge. They consider the Doctor's history... then agree to give hi one day to recover the survivors and leave the planet.
Song realizes what the Doctor has in mind, and that it will kill him. She knocks him out and handcuffs him to the wall, then hooks herself into the memory enhancement circuits. She explains that her future Doctor knew when the time arose for her to go back and accomplish this mission in "his" past, and gave her the enhanced sonic screwdriver. The Doctor says he has to take the risk but she warns he can't change history by dying before she met him. She activates the circuitry and boost the teleport circuits. In the virtual world, Lee comes to find Donna but he starts to fade away. As she's teleported out, she promises to find him no matter what.
The survivors start reappearing throughout the Library. Donna tries to find Lee without success, and wonders if he was real or part of the virtual reality. She and the Doctor go back to the TARDIS... just as the real Lee is teleported out and catches a brief glimpse of her before he can catch up. On their way back to the TARDIS, the Doctor considers Song's diary of his future and wonders if they should check it for "spoilers." Donna is equally concerned since Song didn't know her in the future, but they both agree to go on into the future uninformed. The Doctor leaves the diary and Song's sonic screwdriver there and prepares to leave… and then realizes there was a reason his future self gave Song the device. Opening it, he finds a telepathic circuit inside. He runs to the computer core and plugs the sonic screwdriver into the computer.
Inside the virtual reality, Song appears… alive and well. A restored Charlotte and Dr. Moon greet her, and the Daves, Anita, and a now-whole Evangelista greet her. All of their data ghosts have been restored to full sentience and given a place to live.
The Doctor goes to the TARDIS, considers it for a moment… and then opens the doors with a snap of his fingers. As he and Donna depart, in the virtual reality Song tucks her new children Josh and Ella into bed, and notes that when the Doctor is involved, sometimes… sometimes everyone lives.