Repository navigation
Description
Problem
When git blame stops at a commit classified as a refactor, trace_lines asks RefactorOrigins.find where the refactor took the line from:
findnormalizes the line's text and looks it up among all rows that refactor removed.- It returns the first match, preferring the same path.
- It doesn't check that the matched row is the one the refactor replaced with this line.
So when a refactor rewrites one statement into text that some other row it touched used to have, the hop lands on that other row. Blot then reports the other row's history.
In the reproduction below:
- A fix adds
session="current"to a secondmake(...)call. - A feature adds
session='current'to the first call. - A formatting commit normalizes the first call's quotes and re-indents the second call.
After step 3 the first call's row reads session="current",, which is also the text of the second call's removed row. The hop goes there:
blot whyexplains the first call by the second call's fix;- editing only the first call gives a severity-2
removed-fixwarning.
git blame itself stops at the formatting commit for that line, so the cross-call step is Blot's hop, not git.
Affected code
src/blot/trace.py on main (5845a10):
RefactorOrigins._loadindexes the rows a refactor removed as normalized text →[(old path, old line)]. Where the replacing row sits on the new side isn't recorded.RefactorOrigins.findlooks up by text only; the blamed line number isn't an input. It returns(same_path or candidates or [None])[0]: the first same-path match, or else the first match in any path.trace_linescallsfindfor every line blamed on a refactor, then blames the parent at the returned row.
Reproduction
Needs only Git and Blot, uses no network, and runs in about a second. It doesn't need Black.
#!/bin/sh
# Blot's exact refactor hop attributes one call's argument to another call's fix.
# Usage: sh repro-exact-hop.sh [PYTHON] (PYTHON must be able to `import blot`; default python3)
set -eu
py=${1:-python3}
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null GIT_AUTHOR_DATE=2026-01-01T00:00:00Z GIT_COMMITTER_DATE=2026-01-01T00:00:00Z
cd "$work"
git init -q -b main .
git config user.email test@example.com
git config user.name Test
save() { cat > ctx.py; git add ctx.py; git commit -qm "$1"; }
save "Add calls" <<'EOF'
make()
if ready:
make()
EOF
save "Fix crash when session is unset in second call (fixes #1)" <<'EOF'
make()
if ready:
make(
session="current",
)
EOF
save "Use current session in first call" <<'EOF'
make(
session='current',
)
if ready:
make(
session="current",
)
EOF
# Formatting only: normalize quotes in the first call, re-indent the second.
save "Reformat with black" <<'EOF'
make(
session="current",
)
if ready:
make(
session="current",
)
EOF
git log --format='%h %s'
echo '--- git blame for line 2 (first call)'
git blame -w -M -C -s -L 2,2 HEAD -- ctx.py
# Change only the first call's value.
sed -i.bak '2s/"current"/"other"/' ctx.py && rm ctx.py.bak
git add ctx.py
echo '--- blot why ctx.py:2'
"$py" -m blot why ctx.py:2 2>/dev/null
echo '--- blot check'
"$py" -m blot check 2>/dev/null || trueActual output
On main (5845a10). The output is byte-for-byte the same on #58's head (f6b8358).
4e4e44a Reformat with black
7a28390 Use current session in first call
ffdeb4b Fix crash when session is unset in second call (fixes #1)
15c947b Add calls
--- git blame for line 2 (first call)
4e4e44ad 2) session="current",
--- blot why ctx.py:2
ctx.py:2
| session="current",
explained by ffdeb4bc 2026-01-01 Fix crash when session is unset in second call (fixes #1) [bug fix] #1
traced past 4e4e44ad 2026-01-01 Reformat with black [refactor]
--- blot check
blot: 1 warning
! removed-fix ctx.py:2
You're removing or changing a line that a bug fix added.
Check that the bug it fixed can't come back.
ffdeb4b "Fix crash when session is unset in second call (fixes #1)" (2026-01-01, fixes #1)
traced past refactor 4e4e44a "Reformat with black"
find returns ('ctx.py', 6) for line 2. That row is the second call's session="current", from before the re-indent. Blot blames the parent there and lands on line 4 of the fix commit.
Expected behaviour
Blot shouldn't explain the first call's argument by the second call's fix:
blot why ctx.py:2should stop atReformat with blackor reachUse current session in first call.blot checkshould give noremoved-fixwarning for this edit.
When the refactor's diff doesn't identify which row was replaced, stopping at the refactor, as Blot already does when the hop finds nothing, is an acceptable result.
Not covered here: git blame -M
Replace current with current_session in the script (20 or more letters and digits) and git blame -M itself credits line 2 to the fix (fadbea4b 2)). Blot reports that with no hop: explained by fadbea4b ..., no traced past line, and the same warning.
That is git's move detection, a different mechanism. Changing find wouldn't affect it, and this issue doesn't ask for it.
Relationship to #58
#58 adds a trailing-comma fallback that runs only after find misses, and it declines ambiguous candidates. It doesn't change find, _load or the exact-match path. The reproduction gives identical output on main and on #58's head.
It came up while testing #58: the index-only variant of the comma fallback considered there inherited the same weakness. It predates #58 and is independent of it.
Known limitations of this report
- The histories are constructed. The formatting commit makes the kinds of edits Black makes (quote normalization, re-indenting two-space code), but I didn't run Black to confirm it would produce exactly this file.
- I haven't measured how often this happens in real repositories, and nothing here establishes that. I also haven't checked whether any labelled replay warning depends on a wrong exact hop.
- Only the same-file case was reproduced. The fallback in
findto a match in another path was not exercised.
Suggested acceptance criteria
- Regression tests for ambiguous exact matches:
- this history, where the hop must not land on another statement's row;
- a history where several removed rows share the line's normalized text.
- Valid historical attribution preserved: the existing test suite passes, with any changed expectations individually justified, including fix(trace): follow Black's trailing commas through refactors #58's trace tests if it has merged by then, and
blot benchstays 33/33. - Useful benchmark warnings preserved:
- the labelled replays on the five reference repos lose no
usefulwarning; - new or moved warnings get labels;
bench/regressions.py runrecall remains at least 12/71, with any improvement independently validated.
- the labelled replays on the five reference repos lose no
- Performance measured on the exact-hop path:
- before/after timings for
blot check(still under ~500 ms),blot mapon Flasksrc/flask/app.py, andblot whyon a line that hops through a refactor; - any extra Git calls per hop.
- before/after timings for
- No unrelated scope: only the exact hop changes. The comma fallback, refactor classification and blame options, including the
-Mcase above, stay as they are.
Activity
Sandro850 commentedon Nov 29, 2026
As discussed in #58, I've documented the pre-existing exact-hop weakness with a reproducible Git history. I'd like to investigate and work on this issue once #58 is merged, if that works for you.
Sym-jay commentedon Nov 29, 2026
Thanks Sandro, great write-up. Yes, it's yours (#58 is merged now). One thing before you go deep: could you first check how often this actually happens on the five reference repos, e.g. count exact hops where the matched row isn't the one the refactor replaced? If it never touches a labelled warning, let's keep the fix as small as possible. That hop is under almost every traced warning.
Sandro850 commentedon Nov 29, 2026
I completed the incidence study across the five pinned reference repositories, using the same 500-commit replay configuration as #58.
Exact-hop incidence
All 146 distinct successful exact hops were assessed.
For example, Flask's only matching removed row for an
@click.option(opener belonged to--all-methods, whereas the blamed row belonged to--eager-loading.Labelled warning impact
None of the 126 labelled replay findings depended on a confirmed incorrect exact hop.
Stopping all 24 confirmed incorrect hops preserved:
Rerouting to verified predecessors, where identifiable, produced the same replay results.
Of the 110 labelled removed-fix reporting rows:
Historical regression impact
One on-target Django regression finding depended on an incorrect exact hop.
Stopping that hop moved the warning from line 169 to line 459. Following the verified predecessor preserved line 169.
All 137 pairs retained their scoring:
The instrumentation also reproduced the original trace results across all 2,900 replay and recall batches.
Recommendation
I agree that a minimal correction is justified.
The attribution errors are observable in real repository histories, but none affected the labelled replay findings in this corpus.
I'll focus on rejecting demonstrably unrelated matches, preserving legitimate moves and avoiding unnecessary Git subprocesses on the hot path.
I'll also retain the Django case as a regression test and measure the performance impact before proposing a replacement.
These measurements describe the pinned corpus, not the prevalence of the defect across arbitrary repositories.
No production changes have been made yet.
Sandro850 commentedon Nov 29, 2026
I independently reviewed the incidence study and explored
a small correction using only the refactor diff that Blot
already loads.
The preferred approach corroborates exact matches using
hunk positions and neighbouring rows, rather than relying
on normalized text alone.
The Django regression also retains its original reporting
location.
There is one design question before implementation.
In #58, test_exact_origin_precedes_the_comma_fallback
explicitly expects a cross-file exact match to take
priority over a potential same-file comma origin.
The new rule would reject that cross-file match when
the destination line is an in-place replacement rather
than an insertion, and allow the existing comma fallback
to evaluate the same-file origin.
A more conservative variant preserves the existing test,
but leaves one confirmed replay error, one recall error,
and additional cross-file misattributions observed in
a whole-file Django trace.
Would you be comfortable changing that test's expectation
while preserving priority for corroborated exact matches?
The preferred prototype adds approximately 82 lines of
production code across trace.py and diff.py, with no new
Git subprocesses, AST parsing or blob reads.
Normal Flask timings are comparable to baseline. The
worst observed case, a 15 MB Django reformat, increases
index loading time by 25%, check time by 23%, and cache
memory to 2.2 times baseline.
It also declines three previously correct replay hops.
None affected the labelled findings.
I'd prefer the corroborated-match rule, but wanted to
confirm the contract change and these trade-offs before
implementing it.
No production changes have been made.