Sub topic 14
Common Failure Modes and Reviewing Suggestions Safely
This chapter assumes you know how inline completions and next edit suggestions (NES) are produced. That covers prefix and suffix context, fill-in-the-middle prompting, debouncing, post-processing, and the use of recent edit history. It also assumes you can read Python or TypeScript and a unified diff. Here you will learn how these features fail, how to spot each failure quickly, and how to review a suggestion before you accept it.
Why suggestions fail
A completion model predicts likely text from the context it is given. It does not run your program, read your tests, or check that the names it uses exist. A suggestion can look fluent and still be wrong. Failures fall into four groups:
- Content errors: the text is wrong or unsafe.
- Context errors: the model did not see what it needed, or saw stale information.
- Pipeline errors: the text was fine but arrived late, in the wrong place, or was cut badly.
- Policy errors: the suggestion or the request breaks a privacy or licensing rule.
Knowing the group tells you where to look. A content error calls for careful review. A pipeline error often calls for a setting change or a bug report.
Content errors
Hallucinated APIs and names
The model may call a function that does not exist, use an argument that a library dropped, or invent an import. This is most common with newer libraries, internal packages, and rarely used APIs. The code reads naturally, so you notice it only when it fails to run.
Signs to look for:
- An import you do not recognize.
- A method name that is almost right, such as
list.addin Python instead oflist.append. - A keyword argument that the library docs do not list.
Run the type checker or linter right after accepting. Editor squiggles are a cheap test for invented names.
Plausible but wrong logic
The model copies the shape of nearby code. That helps with repeated patterns and hurts when the new case differs slightly. Typical errors are off-by-one bounds, a swapped comparison, a copied variable that should have changed, and missing handling of an empty input.
def last_n(items, n):
# A suggestion that looks right but fails when n is 0:
return items[-n:]
print(last_n([1, 2, 3], 2)) # [2, 3]
print(last_n([1, 2, 3], 0)) # [1, 2, 3], not []The slice items[-0:] equals items[0:], so the whole list comes back. A reviewer who checks only the usual case misses this. Test the edge values: zero, one, empty, and the maximum.
Insecure patterns
Models reproduce common code from their training data, including bad habits. Watch for:
- SQL built by string formatting instead of parameters.
- Disabled TLS verification, such as
verify=False. - Hard-coded secrets or tokens in examples.
- Weak hashing for passwords.
- Shell commands built from user input.
A suggestion that makes a test pass is not therefore safe. Review security-sensitive code with the same care you would give a colleague's pull request.
Stale or deprecated code
Training data has a cutoff. The model may suggest an API that was current years ago and is now deprecated or removed. Deprecation warnings at runtime and in the linter are the usual signal.
Context errors
Missing or truncated context
The request has a token budget. When the file is long, the assembler cuts the prefix and suffix near the cursor. The model may then not see the helper defined 2,000 lines up, or the type declared in another file. It guesses a signature instead, and the guess is often wrong.
Ways to help:
- Keep the relevant definition near the cursor, or in an open tab if your editor uses open files as context.
- Write a short comment or a type annotation that names what you want.
- Keep files and functions small enough that the needed context fits.
Stale edit history
Next edit suggestions depend on your recent edits. If you made an edit, then undid it, then edited something unrelated, the model may still predict a change based on the first edit. The result is a suggestion that follows a rename you abandoned.
The diff below shows this case. The developer renamed w and h to width and height, reverted the rename, and the model still proposes updating the call site.
--- a/geometry.py
+++ b/geometry.py
@@ -1,6 +1,6 @@
def area(w, h):
return w * h
def describe():
- return area(w=2, h=3)
+ return area(width=2, height=3)Applying this would break the call, because area again takes w and h. The fix is to check every NES hunk against the current definition, not against the edit you remember making.
Wrong edit location
An NES must pick a line to edit. It can pick the wrong one, for example when two functions have similar bodies. The diff then touches code you did not intend to change. Look at the line numbers and the surrounding lines in the preview, not only at the changed text.
Pipeline errors
Latency and stale results
If a suggestion arrives after you have moved on, showing it is worse than showing nothing. Editors cancel the request when the cursor moves, but a slow backend can still return a result for an old position. Good clients check that the document version and cursor position still match before displaying. Poor ones show ghost text that no longer fits.
Signs of a latency problem:
- Ghost text that appears one or two characters behind where you are typing.
- Suggestions that flicker and disappear.
- A noticeable delay before every suggestion.
Bad trimming and bracket problems
Post-processing removes text that duplicates what follows the cursor. When it fails, you get doubled closing brackets or quotes, because editors often auto-insert them. The overlap trimmer from earlier in this tutorial is a simplified version of that step.
def trim_overlap(completion: str, suffix: str) -> str:
"""Drop the end of the completion if it repeats the start of the text after the cursor."""
for n in range(min(len(completion), len(suffix)), 0, -1):
if completion.endswith(suffix[:n]):
return completion[:-n]
return completion
# Works when the editor already inserted ")":
print(repr(trim_overlap("foo(a, b)", ")\n"))) # 'foo(a, b'
# Fails to trim when the overlap is not at the very end of the completion:
print(repr(trim_overlap("foo(a, b) # call", ")\n"))) # 'foo(a, b) # call'The simple version only compares the end of the completion with the start of the suffix. A real implementation must handle whitespace, indentation, and multi-line overlaps. If you see doubled brackets often in one language, the editor integration for that language is the likely cause.
Indentation and formatting drift
In Python, a wrong indent changes meaning. A completion may continue at the wrong level, which silently moves a statement out of a loop or an if. Check the indentation of every accepted block, and run the formatter.
Repetition and runaway output
A model can loop and repeat the same line many times, or produce a very long block that you did not want. Filters usually cap length and detect repeats. If you see long repeated output, reject it and report it. Do not trim it by hand and keep it.
Policy errors
Sensitive data in the request
The request contains your prefix, suffix, and often other open files. If those hold secrets or personal data, they leave your machine. Check your tool's settings for:
- Which files and languages are excluded from completion.
- Whether your prompts and accepted code are retained, and for how long.
- Whether they are used to train models.
- Whether your organization can set these centrally.
Add .env files and credential stores to the exclusion list.
Licensing and memorized code
Models can reproduce training text word for word, especially short well-known snippets. Some tools offer a filter that blocks suggestions matching public code, and some show a reference to the source. Turn the filter on if your organization has licensing rules, and treat any long verbatim block with suspicion.
A review routine
Review a suggestion in the time it takes to read it, then decide. This routine works for both ghost text and NES diffs.
- Read all of it. Do not accept on the first line. For long suggestions, accept one word or line at a time, then read the rest.
- Check every name: imports, functions, arguments, fields. Hover or jump to the definition if you are unsure.
- Check the edges: empty, zero, null, the largest value, and error paths.
- For an NES, check the location and compare against the current code, not your memory of it.
- Look for security patterns in the list above.
- Run the formatter, linter, type checker, and tests.
- Read the final diff before committing, as you would for hand-written code.
Partial acceptance helps here. Taking a suggestion by word or by line lets you keep the safe part and rewrite the doubtful part yourself.
| Failure | Typical sign | First response |
|---|---|---|
| Hallucinated API | Unknown import or method | Check docs, run type checker |
| Edge-case bug | Passes the common case only | Test boundary values |
| Insecure pattern | String-built SQL, disabled TLS | Rewrite with the safe API |
| Truncated context | Wrong signature guessed | Move definition closer or annotate |
| Stale edit history | NES reverses an abandoned change | Compare against current code |
| Wrong NES location | Diff touches a similar function | Check line numbers in preview |
| Late result | Ghost text trails the cursor | Check latency and client version |
| Bad trimming | Doubled brackets | Reject, report for that language |
| Verbatim code | Long exact block | Enable match filter, check license |
Exercises
Exercise
A suggestion for def tail(items, n): return items[-n:] passes your test with n = 2. Which input should you also try, and what goes wrong?
Show answer
Try n = 0. The slice items[-0:] is the same as items[0:], so the function returns the whole list instead of an empty list.
Exercise
You rename a function parameter, undo the rename, and then an NES proposes changing a call site to use the new parameter name. Which failure group is this, and how do you decide whether to accept?
Show answer
It is a context error caused by stale edit history. Compare the proposed hunk with the current function definition, see that the old parameter name is still in use, and reject the suggestion.
Exercise
Name three settings or practices that reduce the privacy and licensing risk of completions.
Show answer
Any three of: exclude secret files and sensitive languages from completion, check the retention and training policy for prompts and accepted code, enable the filter that blocks matches to public code, and apply central organization policy where the tool supports it.