Skip to content

PD-85: fix memory leak by capping checkpoint file size and sharing dirty file contents - #1772

Merged
svarlamov merged 1 commit into
mainfrom
devin/pd85-memory-leak-sanitized
Jul 9, 2026
Merged

svarlamov merged 1 commit into
mainfrom
devin/pd85-memory-leak-sanitized

Conversation

@Siddhant-K-code

@Siddhant-K-code Siddhant-K-code commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

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 whole Strings.

Problem

The checkpoint path was making multiple full copies of every file’s contents:

  • build_checkpoint_files read each file into String.
  • resolve_checkpoint_request cloned that String 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 (with all contents) into each concurrent task.
  • read_current_file_content returned a freshly cloned String.

For large files this multiplied memory by the number of concurrent tasks and checkpoints.

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)

# 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)
git-ai config set max_checkpoint_file_size_bytes 1024

# Set up a demo repo
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)

# 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.

Link to Devin session: https://app.devin.ai/sessions/014c14b4562d4a07847ad3073f355bc3
Requested by: @Siddhant-K-code

Siddhant-K-code commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@Siddhant-K-code
Siddhant-K-code marked this pull request as ready for review July 9, 2026 18:26

@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: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

…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
svarlamov force-pushed the devin/pd85-memory-leak-sanitized branch from 61b2f55 to 002c7ce Compare July 9, 2026 19:20
@svarlamov
svarlamov merged commit b22eeab into main Jul 9, 2026
36 checks passed
@svarlamov
svarlamov deleted the devin/pd85-memory-leak-sanitized branch July 9, 2026 23:36
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.

2 participants