Skip to content

fix(memory): bound attribution-recovery diff to the immediate parent (PD-23 / #1677) - #1699

Merged
svarlamov merged 5 commits into
mainfrom
fix/recovery-diff-memory-june-30
Jun 30, 2026
Merged

svarlamov merged 5 commits into
mainfrom
fix/recovery-diff-memory-june-30

Conversation

@svarlamov

@svarlamov svarlamov commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Problem

The git-ai daemon could consume very large memory during fast-forward/update-ref style paths, especially git pull-driven ref updates.

Root Cause

On a fast-forward update-ref, the daemon can call post-commit finalization with base_commit = old, where old is the branch tip before the ref moved. That value is still the correct working-log base, but it is not necessarily the immediate parent of the finalized commit.

Several post-commit diff consumers then used that raw parent_sha as a Git diff base:

old_tip..new_tip

When old_tip is many commits behind new_tip, that materializes the whole pulled range instead of only the finalized commit. That can spike memory, delay or fail attribution recovery, recover against irrelevant pulled files, and inflate stats/unknown percentages.

Fix

Resolve the finalized commit’s immediate first parent for post-commit diff parsing, while keeping the supplied parent_sha for working-log lookup and cleanup.

The bounded diff base is now used by:

  • attribution recovery hunk discovery
  • no-hooks background-agent fill
  • post-commit stats cost estimation
  • post-commit stats hunk computation
  • committed metric base commit attribution

Normal commit and amend paths are unchanged because their supplied parent is already the immediate parent.

Test Coverage

Removed the heavy Linux RSS repro and replaced it with a targeted TestRepo regression:

  • builds an old_tip -> pulled_early_1 -> pulled_early_2 -> final_tip fast-forward range
  • seeds a working log at old_tip
  • drives the daemon update-ref old_tip -> final_tip path directly
  • verifies recovery still sees final_tip.txt
  • verifies earlier pulled files are not attributed to the local recovery session

Validation

task fmt
task test TEST_FILTER=test_fast_forward_update_ref_bounds_recovery_to_new_tip_parent
task test TEST_FILTER=post_commit
task lint

Closes #1677.


Open in Devin Review

PD-23 / GH #1677: the git-ai daemon could consume 20GB+ RSS during a
`git pull`. On a fast-forward `update-ref`, the daemon calls
`post_commit_from_working_log(Some(old), new)` where `old` is the old
branch tip from *before* the pull. Attribution recovery
(`recovery_committed_hunks`) then ran `diff_added_lines(old, new, None)`
with no pathspec, buffering the entire `git diff -U0 old..new` output
plus one u32 per added line into memory. When the pull fast-forwards
across a large range that is the whole history delta — gigabytes.

This path is new since v1.5.13: the same unbounded diff previously ran
only inside the background-agent block (Devin/Codex-Cloud), never for
ordinary users. `recover_attribution` (default true, added in #1654)
made it run for everyone.

Fix: resolve the finalized commit's immediate first parent and diff
against that instead of the caller's `parent_sha`. The normal commit and
amend paths already pass the immediate parent, so they are unchanged;
only the fast-forward `update-ref` range is capped to a single commit.
This also removes a latent cross-author misattribution (recovery could
attribute remotely-authored pulled lines to the local user on a
timestamp coincidence).

Repro (tests/integration/recovery_diff_memory_repro.rs): over a ~270MB
fast-forward range the full-range diff grows peak RSS ~293MB while the
immediate-parent diff grows ~1MB (275x). Plus a unit regression test
asserting recovery sees only the finalized commit's files.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
devin-ai-integration[bot]

This comment was marked as resolved.

svarlamov and others added 2 commits June 30, 2026 16:45
Address Devin review on #1699:

1. The no-hooks background-agent `fill_unattributed_lines` path ran the
   same unbounded `diff_added_lines(parent_sha, commit_sha, None)` with
   the raw far-behind `parent_sha`. Devin/Codex-Cloud agents can be
   active during a pull, so this had the identical 20GB blow-up. Bound it
   to the immediate parent via the shared `immediate_parent_diff_base`.

2. The RSS repro read VmHWM (process-wide, monotonic) and the harness
   runs tests in parallel, so a concurrent test could corrupt the
   reading. Mark it `#[serial_test::serial]` and measure VmHWM *growth*
   caused by the full-range diff (after the bounded diff establishes the
   prior peak), which is robust to any peak inherited from earlier tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
devin-ai-integration[bot]

This comment was marked as resolved.

svarlamov and others added 2 commits June 30, 2026 22:41
…awns

The previous bounding helper resolved the immediate-parent OID with
`find_commit` + `parent_count` + `parent(0)` — ~3 extra git spawns on
*every* post-commit, at 3 call sites (recovery, bg-agent fill, stats).
Each spawn makes the hot path slower.

We never actually need the parent OID: the immediate first parent is the
rev-expression `<commit_sha>^`, which git resolves for free inside the
`git diff` / `git diff-tree` spawn that already runs. So
`single_commit_diff_base` is now a pure string builder — zero added
spawns, zero object lookups:
  - `parent_sha == "initial"` (root commit) -> empty tree (unchanged)
  - otherwise -> `<commit_sha>^` (== `^1`, first parent)

This is semantically identical to the prior `parent(0)` resolution
(first-parent diff for merges; equals `parent_sha` on the normal/amend
paths) and to the far-behind FF `update-ref` case it bounds.

Also keep `record_commit_metrics`' `base_commit_sha` as the original
`parent_sha` rather than the `<commit>^` diff rev-expression, so the
stored metric attribute stays a real OID.

Verified by test_fast_forward_update_ref_bounds_recovery_to_new_tip_parent
plus the pull_rebase_ff / background_agent / session_event suites.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The bounded diff base has two branches: `<commit>^` for commits with a
parent, and the empty-tree SHA when `parent_sha == "initial"` (root
commit, no `^`). The existing FF regression test only exercises the
former. Add a session-event recovery test whose uncheckpointed AI
content lands in the repo's *first* commit, so post-commit runs with
`parent_sha == "initial"` and must fall back to the empty tree. Guards
against a regression that would `git diff <firstcommit>^` and silently
recover nothing.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Open in Devin Review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 Main authorship computation still uses raw parent_sha for committed hunks in the fast-forward path

The to_authorship_log_and_initial_working_log_with_precomputed_diff call at src/authorship/post_commit.rs:319-327 still passes raw parent_sha (which is the old branch tip in the daemon fast-forward path). Inside that function, when precomputed_parent_diff is None, collect_committed_hunks at src/authorship/virtual_attribution.rs:2161 diffs the full old_tip..new_head range. This is partially mitigated by pathspec filtering (only checkpoint-touched files are diffed), but for files that were both checkpointed locally AND modified by intermediate pulled commits, the committed hunks would span the entire range rather than just the single finalized commit. This is a pre-existing issue not introduced by this PR, but worth noting since the PR fixes the same class of problem in other call sites and the inconsistency could cause subtle attribution inaccuracies.

(Refers to lines 319-327)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@svarlamov
svarlamov merged commit 647a39a into main Jun 30, 2026
24 checks passed
@svarlamov
svarlamov deleted the fix/recovery-diff-memory-june-30 branch June 30, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: git-ai can consume >20 GB memory during git pull

1 participant