Repository navigation
PD-85: fix memory leak by capping checkpoint file size and sharing dirty file contents - #1772
Merged
Merged
Conversation
Contributor
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Siddhant-K-code
marked this pull request as ready for review
July 9, 2026 18:26
…rty file contents
## Problem
The checkpoint pipeline was cloning whole file contents multiple times as `String`:
- `build_checkpoint_files` read every targeted file into a `String`.
- `resolve_checkpoint_request` cloned those `String`s into `dirty_files`.
- `execute_resolved_checkpoint` cloned `dirty_files` into the working log.
- `save_current_file_states` cloned each content again for hashing and blob writes.
- `get_checkpoint_entries` cloned the entire `PersistedWorkingLog` (including all contents) into every concurrent task.
- `read_current_file_content` cloned the content for callers.
For large files this amplified memory use by the number of concurrent checkpoints and tasks.
## Changes
1. **Per-file size limit** (`max_checkpoint_file_size_bytes`, default 3 MB)
- Added to `Config` / `FileConfig` / `ConfigPatch` and the `git-ai config` CLI.
- `build_checkpoint_files` now skips files larger than the limit before reading them.
- `resolve_checkpoint_request` drops oversized dirty-file contents and skips binary files.
- `execute_pre/post_file_edit` and `execute_known_human_edit` also enforce the limit on override content.
2. **Shared, cheap clones** (`Arc<str>`)
- `ResolvedCheckpointExecution.dirty_files` → `HashMap<String, Arc<str>>`.
- `PersistedWorkingLog.dirty_files` → `Option<HashMap<String, Arc<str>>>`.
- `get_checkpoint_entry_for_file`, `get_previous_content_from_head`, `get_checkpoint_entries` and `save_current_file_states` now hold/operate on `Arc<str>`.
- `resolve_checkpoint_request` uses `std::mem::take` to move `CheckpointFile.content` into `Arc<str>` without copying.
- `execute_resolved_checkpoint` uses `std::mem::take` to move the map into the working log.
- Hashing and blob writes use `content.as_bytes()` instead of copying into a new `String`.
3. **Test infrastructure**
- Added two `TestRepo` integration tests in `tests/integration/checkpoint_size.rs`:
- `test_checkpoint_skips_oversized_files` — lowers the limit to 64 bytes, writes one small and one oversized file, and asserts only the small file is checkpointed.
- `test_checkpoint_saves_normal_files_under_limit` — lowers the limit to 1 KB, writes a normal file, and asserts it is checkpointed.
- `config_cli_coverage` now covers the new config key.
- `background_agent` detection now ignores `/opt/.devin` when the test harness marker (`GIT_AI_TEST_DB_PATH`) is set, so the Devin VM environment is not misidentified as the active agent during tests.
## How to validate (real user flow)
```bash
# Build the debug binary
cargo build
# Or install git-ai system-wide so the `git-ai` CLI is on PATH
# task dev # recommended for local dev
# Configure a 1 KB per-file checkpoint limit (optional)
# default is 3 MB, so this is just for demo purposes
git-ai config set max_checkpoint_file_size_bytes 1024
# Set up a demo repo and make it git-ai-aware
mkdir -p /tmp/pd85-demo && cd /tmp/pd85-demo
git init
git config user.name "Demo User"
git config user.email "[email protected]"
echo "small line" > small.txt
dd if=/dev/zero of=large.txt bs=1024 count=5 # 5 KB, over the limit
# Fire an AI checkpoint on both files
# The oversized file should be skipped, the small file should be checkpointed
git-ai checkpoint mock_ai small.txt large.txt
# Commit and check blame
git add small.txt large.txt
git commit -m "demo commit"
git-ai blame small.txt
```
## How to validate (test suite)
```bash
# build and lint
cargo build
cargo clippy --all-targets -- -D warnings
cargo fmt -- --check
# unit tests
cargo test --lib
# targeted new tests
cargo test --test integration "checkpoint_size::"
# full integration suite (passes on this branch with git >= 2.38)
cargo test --test integration
```
I ran the full suite locally after upgrading git to 2.54.0 and saw `3196 passed; 0 failed; 85 ignored`.
## Linear
Fixes PD-85.
svarlamov
force-pushed
the
devin/pd85-memory-leak-sanitized
branch
from
July 9, 2026 19:20
61b2f55 to
002c7ce
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
This PR fixes the memory-amplification leak in the checkpoint pipeline by (1) capping the size of files that are read into the checkpoint flow and (2) sharing file contents via
Arc<str>instead of repeatedly cloning wholeStrings.Problem
The checkpoint path was making multiple full copies of every file’s contents:
build_checkpoint_filesread each file intoString.resolve_checkpoint_requestcloned thatStringintodirty_files.execute_resolved_checkpointcloneddirty_filesinto the working log.save_current_file_statescloned each content again for hashing and blob writes.get_checkpoint_entriescloned the entirePersistedWorkingLog(with all contents) into each concurrent task.read_current_file_contentreturned a freshly clonedString.For large files this multiplied memory by the number of concurrent tasks and checkpoints.
Changes
Per-file size limit (
max_checkpoint_file_size_bytes, default 3 MB)Config/FileConfig/ConfigPatchand thegit-ai configCLI.build_checkpoint_filesnow skips files larger than the limit before reading them.resolve_checkpoint_requestdrops oversized dirty-file contents and skips binary files.execute_pre/post_file_editandexecute_known_human_editalso enforce the limit on override content.Shared, cheap clones (
Arc<str>)ResolvedCheckpointExecution.dirty_files→HashMap<String, Arc<str>>.PersistedWorkingLog.dirty_files→Option<HashMap<String, Arc<str>>>.get_checkpoint_entry_for_file,get_previous_content_from_head,get_checkpoint_entriesandsave_current_file_statesnow hold/operate onArc<str>.resolve_checkpoint_requestusesstd::mem::taketo moveCheckpointFile.contentintoArc<str>without copying.execute_resolved_checkpointusesstd::mem::taketo move the map into the working log.content.as_bytes()instead of copying into a newString.Test infrastructure
TestRepointegration tests intests/integration/checkpoint_size.rs:test_checkpoint_skips_oversized_files— lowers the limit to 64 bytes, writes one small and one oversized file, and asserts only the small file is checkpointed.test_checkpoint_saves_normal_files_under_limit— lowers the limit to 1 KB, writes a normal file, and asserts it is checkpointed.config_cli_coveragenow covers the new config key.background_agentdetection now ignores/opt/.devinwhen the test harness marker (GIT_AI_TEST_DB_PATH) is set, so the Devin VM environment is not misidentified as the active agent during tests.How to validate (real user flow)
How to validate (test suite)
I ran the full suite locally after upgrading git to 2.54.0 and saw
3196 passed; 0 failed; 85 ignored.Linear
Fixes PD-85.
Link to Devin session: https://app.devin.ai/sessions/014c14b4562d4a07847ad3073f355bc3
Requested by: @Siddhant-K-code