Repository navigation
fix(memory): bound attribution-recovery diff to the immediate parent (PD-23 / #1677) - #1699
Conversation
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]>
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]>
…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]>
There was a problem hiding this comment.
🚩 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)
Was this helpful? React with 👍 or 👎 to provide feedback.
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 withbase_commit = old, whereoldis 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_shaas a Git diff base:When
old_tipis many commits behindnew_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_shafor working-log lookup and cleanup.The bounded diff base is now used by:
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
TestReporegression:old_tip -> pulled_early_1 -> pulled_early_2 -> final_tipfast-forward rangeold_tipupdate-ref old_tip -> final_tippath directlyfinal_tip.txtValidation
Closes #1677.