# Localized code modification with generative models

Canonical HTML: https://aogavrilov.com/research-notes/localized-code-modification-generative-models/
Document language: en
Source publication: https://aogavrilov.com/publications/inspectable-control/
Source full-text HTML: https://aogavrilov.com/publications/inspectable-control/full-text/
DOI: https://doi.org/10.1145/3803437.3807386

How to prevent unnecessary whole-function rewriting while preserving enough freedom for a generative model to make the requested code change.

## Direct answer

### How can a generative model modify code without rewriting the entire function?

Define protected and editable regions before generation, reuse or constrain the protected representation, generate only candidate changes, and reject outputs that alter protected code or fail task-specific checks. Locality and task success must be measured together.

## Why the distinction matters

Localized code modification is an editing problem, not merely a shorter code-generation prompt. The input already contains an artifact worth preserving, so the method needs an explicit boundary between the region allowed to change and the properties that must remain stable.

The boundary may be a source span, syntax node, API signature, test behavior, dependency contract, or learned latent position. These choices are not interchangeable: each protects a different observable property and requires a matching verification step.

## Practical procedure

1. **State the preservation contract.** Identify the editable region and the exact text, structure, interface, or behavior that must remain unchanged.
2. **Choose the narrowest useful control surface.** Reuse unchanged source spans, use infilling or edit-oriented decoding, apply formal constraints, or lock selected latent positions according to the required property.
3. **Generate only where change is allowed.** Keep enough freedom inside the editable region to solve the task; copying the entire input is local but makes no progress.
4. **Verify locality and success together.** Reject candidates that alter protected regions, fail parsing or compilation, violate structural invariants, or do not satisfy the requested change.

## Evidence to require

- Outside-region diff or another direct protected-region stability measure.
- Task success inside the editable region.
- Parsing, compilation, tests, static checks, or task-specific invariants as applicable.
- Editable-region change rate so copying is not mistaken for control.
- Candidate uniqueness and repeated-run variability so locality is not confused with mode collapse.

## What the linked study reports

- The linked experiment compresses 64-token Python functions into hierarchical discrete positions and regenerates selected latent positions under partial constraints.
- Locking four top-level codes raised parse rate from 0.453 to 0.591 while unlocked positions changed at rate 0.936 and conditional samples remained 0.998 unique.
- Those measurements expose a stability-freedom trade-off above the token level; they do not prove exact source-span, AST, semantic, or behavioral preservation.

## Scope boundary

- A locked latent code is not automatically an AST node, protected source span, or formal invariant.
- Parse rate establishes syntactic well-formedness, not functional correctness or successful repair.
- The reported evidence comes from short preprocessed Python functions and does not establish repository-scale behavior.

Complete research guide: https://aogavrilov.com/projects/discrete-latent-generation/#control-surface

## Primary and nearby sources

- [Inspectable Control for Structure-Preserving Software Regeneration](https://aogavrilov.com/publications/inspectable-control/): Primary paper and bounded hierarchical-latent experiment.
- [EfficientEdit: Accelerating Code Editing via Edit-Oriented Speculative Decoding](https://arxiv.org/abs/2506.02780): Edit-oriented decoding that reuses unchanged source regions.
- [PAFT: Preservation Aware Fine-Tuning for Minimal-Edit Program Repair](https://arxiv.org/abs/2604.03113): Makes preservation and minimal change explicit in program-repair training.

This maintained note summarizes existing evidence and adds no experimental result beyond the cited sources.
