Skip to content

Exact refactor hop matches removed rows by text alone and can cite another statement's fix #59

Todo

Description

@Sandro850

Problem

When git blame stops at a commit classified as a refactor, trace_lines asks RefactorOrigins.find where the refactor took the line from:

  • find normalizes 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:

  1. A fix adds session="current" to a second make(...) call.
  2. A feature adds session='current' to the first call.
  3. 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 why explains the first call by the second call's fix;
  • editing only the first call gives a severity-2 removed-fix warning.

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._load indexes 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.find looks 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_lines calls find for 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 || true

Actual 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:2 should stop at Reformat with black or reach Use current session in first call.
  • blot check should give no removed-fix warning 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 find to 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 bench stays 33/33.
  • Useful benchmark warnings preserved:
    • the labelled replays on the five reference repos lose no useful warning;
    • new or moved warnings get labels;
    • bench/regressions.py run recall remains at least 12/71, with any improvement independently validated.
  • Performance measured on the exact-hop path:
    • before/after timings for blot check (still under ~500 ms), blot map on Flask src/flask/app.py, and blot why on a line that hops through a refactor;
    • any extra Git calls per hop.
  • No unrelated scope: only the exact hop changes. The comma fallback, refactor classification and blame options, including the -M case above, stay as they are.

Activity

  1. Sandro850 commented on Nov 29, 2026

    @Sandro850
    ContributorAuthor

    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.

  2. Sym-jay commented on Nov 29, 2026

    @Sym-jay
    Owner

    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.

  3. added
    bug
    on Nov 29, 2026
  4. Sandro850 commented on Nov 29, 2026

    @Sandro850
    ContributorAuthor

    I completed the incidence study across the five pinned reference repositories, using the same 500-commit replay configuration as #58.

    Exact-hop incidence

    Repository Exact lookups Distinct successful hops Correct Incorrect Indeterminate
    Flask 576 78 64 13 1
    Requests 73 3 3 0 0
    Express 31 1 0 1 0
    Django 212 11 9 2 0
    React 1091 53 36 8 9
    Total 1983 146 112 24 10

    All 146 distinct successful exact hops were assessed.

    • 24/146 (16.4%) selected an incorrect historical row.
    • 10 remained indeterminate.
    • Counting all indeterminate cases as incorrect gives a conservative upper bound of 34/146 (23.3%) within this corpus.
    • 16 confirmed errors involved multiple candidates, while 8 involved a unique candidate.

    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:

    • All 70 useful warnings.
    • All 56 noise warnings.
    • Their reporting locations and historical citations.

    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:

    • Overall recall: 12/71.
    • Within-model recall: 12/27.
    • Scripted benchmark: 33/33.

    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.

  5. Sandro850 commented on Nov 29, 2026

    @Sandro850
    ContributorAuthor

    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.

    Result Proposed approach
    Confirmed replay errors corrected 24/24
    Correct replay hops preserved 109/112
    Historical regression errors corrected 8/8
    Labelled findings changed 0/126
    Recall Unchanged
    Scripted benchmark 33/33
    Additional Git subprocesses 0

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions