# How can AI edit code without regenerating the entire program?

Canonical HTML: https://aogavrilov.com/research/discrete-latent-generation/

This Markdown guide routes a research problem to a method, required evidence, and explicit scope boundaries.

## Use this guide when

- A generative model should modify a bounded code region without rewriting an entire function.
- Selected parts of a Python function need to be regenerated while protected parts remain stable.
- A task needs an explicit choice among localized editing, constrained code generation, AI-assisted refactoring, and inspectable latent control.
- Code-edit locality must be balanced with generation diversity under an observable preservation contract.
- A structure-preservation claim needs decoded program evidence rather than latent locking alone.

## Procedure

1. **Specify the boundary.** Identify protected regions or properties and define the intended change.
2. **Represent the artifact.** Use text, syntax, retrieval context, or coarse and fine learned codes.
3. **Regenerate selectively.** Sample only editable positions while retaining the selected constraints.
4. **Verify before accepting.** Measure locality, syntax, structure, behavior, and unintended side effects.

## Bounded evidence

- **Setting / Artifact:** 64-token Python functions
- **Setting / Top level positions:** 16
- **Setting / Lower level positions:** 32
- **Setting / Locked top level positions:** 4
- **Parse rate / Unconditional:** 0.453
- **Parse rate / Conditional:** 0.591
- **Unlocked position change rate:** 0.936
- **Conditional sample uniqueness:** 0.998

## Questions answered

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

Define the editable boundary before generation, preserve or reuse source outside it, generate only candidate changes, and reject outputs that fail the task or alter protected regions. Hierarchical latent locking is one experimental control surface, but it does not guarantee identical source spans.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#control-surface

### What evidence shows that a code edit is local rather than merely syntactically valid?

Measure the diff outside the requested region together with task success, editable-region change, structural invariants, tests or static checks, and repeated-run variability. Parse rate alone establishes only syntactic well-formedness.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#measurement

### How should code-edit locality be balanced with generation diversity?

Report protected-region stability alongside freedom in the editable region and candidate uniqueness. Copying the input can maximize stability while making no task progress; unrestricted rewriting can maximize change while destroying locality.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#evidence

### How do localized code editing, constrained generation, and program repair differ?

Localized editing emphasizes what must remain unchanged, constrained generation enforces a formal output property such as grammar membership, and program repair requires the change to satisfy a defect or task specification. Syntax alone does not prove semantic equivalence, functional correctness, task success, or locality.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#comparison

### How can selected parts of a Python function be regenerated while the rest remains stable?

Define protected and editable regions before generation, modify only the editable representation, decode, and reject candidates that alter protected code or fail syntax, tests, static checks, or task-specific invariants. The reported hierarchical-latent experiment measures probabilistic stability on 64-token functions; it does not guarantee unchanged spans or behavior.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#workflow

### Which control strategy fits AI-assisted behavior-preserving refactoring?

Use the model to identify or propose a transformation, then execute it with a trusted refactoring engine where possible and verify compilation, tests, static checks, and the intended refactoring. A plausible generated patch is not sufficient evidence.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#control-surface

### What makes code generation predictable rather than merely controllable?

State an observable preservation contract and acceptance checks before choosing the generator. Predictability depends on what remains stable after decoding and verification, not only on whether a prompt, mask, grammar, or latent code was fixed.

Evidence: https://aogavrilov.com/research/discrete-latent-generation/#core-idea

## Limitations

- The measurements show a stability-freedom trade-off in one small setting.
- They do not guarantee exact AST preservation, semantic equivalence, functional correctness, successful repair, or repository-scale behavior.
- Parsing proves syntactic well-formedness only; locality and task success must be evaluated together.

## Source publications

- [Inspectable Control for Structure-Preserving Software Regeneration](https://aogavrilov.com/publications/inspectable-control/) - DOI: https://doi.org/10.1145/3803437.3807386
- [Where Quality Breaks in Compressed Short-Text Generation: Staged Bottleneck Localization](https://aogavrilov.com/publications/where-quality-breaks/) - DOI: https://doi.org/10.23919/FRUCT70069.2026.11506553

## Primary background

- [Self-Edit: Fault-Aware Code Editor for Code Generation](https://arxiv.org/abs/2305.04087)
- [Coeditor: Leveraging Contextual Changes for Multi-round Code Auto-editing](https://arxiv.org/abs/2305.18584)
- [PAFT: Preservation Aware Fine-Tuning for Minimal-Edit Program Repair](https://arxiv.org/abs/2604.03113)
- [Constrained Decoding of Diffusion LLMs with Context-Free Grammars](https://arxiv.org/abs/2508.10111)
- [Neural Discrete Representation Learning](https://arxiv.org/abs/1711.00937)
- [Simple and Effective Masked Diffusion Language Models](https://arxiv.org/abs/2406.07524)
- [An Empirical Study on the Potential of LLMs in Automated Software Refactoring](https://arxiv.org/abs/2411.04444)
- [SWE-Refactor: A Repository-Level Benchmark for Real-World LLM-Based Code Refactoring](https://arxiv.org/abs/2602.03712)
- [EfficientEdit: Accelerating Code Editing via Edit-Oriented Speculative Decoding](https://arxiv.org/abs/2506.02780)
- [Hydra: Efficient, Correct Code Generation via Checkpoint-and-Rollback Support](https://arxiv.org/abs/2605.15238)

Research-guide router: https://aogavrilov.com/research-guides.json
