Sub topic 8
Using recent edits as context
This chapter assumes you know how a completion request is built from the prefix and suffix (the text before and after the cursor), and that you can read a diff. It covers edit history: the record of what you just changed, and why a model can use it to guess what you will change next.
Why the current file is not enough
A prefix and suffix show the code as it is now. They don't show how it got there. Take this snapshot:
def area(width, height):
return width * height
def describe():
return area(w=2, h=3)The call site is wrong, but nothing in the snapshot says why. The model sees two valid-looking functions and one broken call. If it also sees that you just renamed w and h to width and height in the definition, the fix is clear: apply the same rename at the call site.
Recent edits give the model intent. They answer "what is this person in the middle of doing?"
What an edit history contains
An editor keeps a short log of changes while you work. Each entry usually holds:
- The file path.
- The range that changed.
- The text before the change and the text after it.
- A timestamp or an ordering, so recent edits can be told apart from old ones.
The editor doesn't send everything. It selects a window of the most recent edits, often the last few edits in the current file and sometimes in files you just had open. It sends them in the prompt, usually as diffs, because a diff states the change compactly.
Merging keystrokes into edits
Typing width produces five separate keystroke events. Sending five events wastes tokens and hides the meaning. Editors merge adjacent changes into one logical edit, and start a new one when the cursor jumps to a different place or you pause.
The sketch below merges changes on the same line into one edit.
from dataclasses import dataclass
@dataclass
class Edit:
line: int
before: str
after: str
def merge_edits(events: list[Edit]) -> list[Edit]:
merged: list[Edit] = []
for event in events:
if merged and merged[-1].line == event.line:
merged[-1].after = event.after
else:
merged.append(Edit(event.line, event.before, event.after))
return merged
events = [
Edit(0, "def area(w, h):", "def area(wi, h):"),
Edit(0, "def area(wi, h):", "def area(width, h):"),
Edit(0, "def area(width, h):", "def area(width, height):"),
]
print(merge_edits(events))The output is one edit from def area(w, h): to def area(width, height):. The model sees the finished rename, not the three steps.
Where history enters the prompt
Each stage has a cost. A longer window gives more signal but adds latency and tokens. The edit history also competes with the prefix, suffix and related files for the same context budget.
Choosing what to keep
Not every edit helps. These rules are common:
- Prefer newer edits over older ones.
- Drop edits that were undone. An edit and its reversal cancel out.
- Drop very large edits, such as a pasted file or a formatter run. They add tokens and little intent.
- Cap the total count and size of the history.
Common errors
- Stale history. Edits from ten minutes ago on an unrelated task mislead the model. Use a time or count limit.
- Raw keystrokes. Unmerged events fill the window with noise.
- No file context. A diff alone lacks the surrounding code. Send both.
Completions use history lightly, to improve text at the cursor. Next edit suggestions depend on it, because the whole task is to predict the following change from the previous ones. The next chapter builds on that.
Exercise
You renamed a function parameter, then pasted a 2,000-line file into another tab. Which of these edits should usually stay in the history sent to the model, and why?
Show answer
Keep the parameter rename. It is small, recent and shows clear intent. Drop or heavily trim the paste, because it adds many tokens and says little about what you will edit next.