Manuscript Offprint

arXiv:2609.35475

Spontaneous Context Restoration: How Language Models Recover from Corrupted Inputs

Pranjal Garg, Jacob Beck

Abstract

Language models sometimes produce correct outputs even when their inputs are corrupted by deletion, replacement, or misspelling. We study the internal processes accompanying this behavior, which we call context restoration, in controlled attention-only transformers and five pretrained LLMs (1B–32B parameters) across arithmetic, reading comprehension, and multiple-choice reasoning tasks. In the attention-only transformers, restoration emerges spontaneously despite training exclusively on clean sequences, without corruption training or an explicit denoising objective. We find that context restoration follows a two-phase process: early layers localize effects associated with repair at corrupted positions, while later layers accumulate these effects at uncorrupted positions through the residual stream and ultimately concentrate them at the output position. Repair outcome is predictable from hidden states: cosine alignment with the clean state is highly predictive in attention-only models, while linear probes recover additional information in pretrained LLMs. A linear probe using only the corrupted prompt's first-block hidden state predicts failure with mean ROC-AUC 0.78. This enables failure triage under matched or even partially shifted deployment conditions and may reduce unnecessary verification or computation. Failed examples also show substantially greater nonlinearity along corruption directions. Moderate-corruption finetuning increases corruption tolerance while simultaneously reducing displacement-normalized linearization error, associating improved robustness with a more nearly linear response to corruption.

Introduction

When an input is corrupted, a key word is deleted, a number in a sequence is replaced, or a sentence is interrupted by irrelevant text, a language model sometimes behaves as if it had seen the uncorrupted input. It may still produce the same answer as it would have produced from the original prompt. Whether accuracy survives the perturbation, measured at the output alone, does not capture what is happening here: the model must preserve the task-relevant computation despite receiving damaged input, and matching outputs alone do not show whether it did. This creates an oversight problem: when robustness fails, the failure may be silent, so a deployed system needs a way to identify inputs at elevated risk before acting on the model's output. This matters in practice because real inputs are often affected by OCR errors, speech-transcription errors, formatting artifacts, tool-call failures, copy-paste noise, and adversarial edits (Singh, Singh, and Vatsal 2024; Wang et al. 2024; McCoy, Pavlick, and Linzen 2019; Petroni et al. 2020; Zhou, Li, and Wang 2024). Understanding how models preserve correct behavior under such corruption can help distinguish deployed systems that are genuinely robust from those that will fail silently under distributional shift and inform training procedures that promote reliable recovery (Wang, Wang, and Yang 2022). A mechanism that estimates failure risk before generation could support selective clarification or verification rather than treating every corrupted input as unreliable.

We call this phenomenon spontaneous context restoration: the preservation of correct task-relevant computation under label-preserving corruption of input content. In our attention-only transformer models, this behavior emerges despite the model being trained exclusively on clean sequences and never trained to recover from corrupted inputs. We trace where corrupted information first matters inside the network, how effects associated with repair move across positions, and what geometric properties of internal representations separate examples that restore from examples that fail. We study controlled attention-only transformers (4- and 10-layer) trained on arithmetic sequences alongside five pretrained instruction-tuned LLMs spanning 1B–32B parameters, evaluated on ARC-Challenge (Clark et al. 2018), factual QA (Rajpurkar et al. 2016), and CommonsenseQA (Talmor et al. 2019) under four corruption modes (dropout, replacement, spelling, distractor injection).

Our central finding is that behaviorally context restoration follows a two-phase spatial process shared between architectures and tasks: early layers localize the influence of replacing corrupted-run activations with their clean counterparts at corrupted positions, while with depth this effect shifts toward clean context positions and ultimately concentrates at the prediction position. Ablating attention heads leaves repair largely intact, indicating that it draws on the accumulated residual stream rather than any single specialized head. The repair outcome is predicted by the geometry of the residual stream: in controlled attention-only transformer models, the cosine similarity between the corrupted and the clean states reaches AUC 0.80–0.95 at fixed corruption severity, while in pretrained models linear probes recover features invisible to directional metrics alone. Using only the corrupted prompt's first-block hidden state, a linear probe achieves a mean AUC of 0.78 across models, tasks, corruption modes, and corruption levels. Flagging the 10% of inputs assigned the highest failure risk yields a mean 2.6×2.6 \times recall lift over random selection, allowing selective clarification or verification before full generation. A linearization analysis (i.e. how closely a first-order Taylor approximation predicts the true effect of corruption on the output) shows that failed examples exhibit greater nonlinearity along corruption directions than repaired examples. Consistent with this account, moderate-corruption finetuning in the controlled model reduces curvature along corruption directions and the corruption level tolerated at 50% accuracy increases from 40% to 90% without measurable loss of clean accuracy.

Figure

Figure 1: Overview of spontaneous context restoration. Patching effects shift across positions with depth, hidden states distinguish repaired from failed inputs, and early representations support failure triage before generation.

Our contributions are: (1) we show that context restoration emerges spontaneously in models trained only on clean sequences and characterize its two-phase spatial organization across attention-only and pretrained transformers, localizing where corrupted information matters and how effects associated with repair shift to the prediction position; (2) we show that repair outcome is predictable from residual-stream geometry and linearly decodable from the corrupted prompt's early hidden state before output generation, enabling selective verification of inputs most likely to fail rather than treating all corrupted inputs as unreliable; (3) we show that failed examples exhibit greater nonlinearity along corruption directions, and that corruption finetuning jointly increases robustness and reduces linearization error.

References (40)