Skip to content

perf(unsaved): merge only the squash pairs that can pass - #721

Draft
blooop wants to merge 5 commits into
fix/ls-unpushed-check-quadraticfrom
perf/squash-rule-cost
Draft

blooop wants to merge 5 commits into
fix/ls-unpushed-check-quadraticfrom
perf/squash-rule-cost

Conversation

@blooop

@blooop blooop commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

The squash rule in dl --ls, dl rm and wf reap now merges only the pairs that can pass. On a kinisi_ros clone, the rule fell from about 19 s to about 5 s, with the same answer.

Stacked on #720 — one copy-rule question per branch. Merge that PR first.

In short

The squash rule finds a branch that GitHub squashed into a remote ref. It asks one question of each pair: does the merge of this point (a commit on the branch) into this ref give the ref's tree? holds_the_change_of makes the final check.

Before this PR, the rule merged every point into every ref first, in one merge-tree --stdin per branch. On a kinisi_ros clone, one --ls row made 824 merges. Each merge took 20 ms to 240 ms, most of it in rename detection. That was 16 s to 18 s of git, and no pair could pass.

Now two cheap git calls come before the merges. They find the pairs where holds_the_change_of must answer false, and the rule does not merge those pairs. If either call fails, or prints output in a shape the parser does not know, the rule merges every pair, as before.

Evidence

The bench is a cp -a copy of a real kinisi_ros workspace. It times account_of, the call that runs the squash rule. Release build, same host, same copy:

  • Before: 19.6 s and 18.2 s (load near 20); 23.3 s and 23.9 s (load near 65).
  • After: 3.7 s and 6.1 s (load near 20); 8.0 s and 5.8 s (load near 65).
  • The holds output is the same byte for byte in every run: md5 7277c0862ec816463c716559923ac65c.

A debug run with a wrapper that times each git call:

  • Before: 20.0 s, 111 git calls, 6 merge-tree --stdin calls that took 16.1 s.
  • After: 7.0 s, 116 git calls, 0 merge-tree --stdin calls, 5 diff-tree calls that took 1.95 s, 6 rev-parse calls for the bases.

What changes

Asked::worth_merging runs before trees_of_clean_merges. It uses two new git calls:

  1. Git::merge_bases_of: one rev-parse <ref>...<point> for all pairs in one spawn. For a symmetric range, rev-parse prints the point, the ref's commit, and each merge base with a ^ before it. These are the bases that merge-base --all gives. I checked that the two agree on all 443 different pairs of the bench copy.
  2. Git::paths_changed: one diff-tree --stdin -r --no-renames --name-status -z --always. Each input line is <commit> <base>, so git lists the paths that each side changed since the merge base.

The rule does not merge a pair when one of these is true:

  • The pair has no merge base, or two or more. holds_the_change_of says false for these.
  • The merge base is the ref's commit. Then the ref changed nothing, and holds_the_change_of says false.
  • The point changes a path that the ref did not change (changes_a_path_the_ref_kept). The merge then brings in the point's version of that path, so the merged tree is not the ref's tree.

One exception to the path check: a path that the point added, under a directory where the ref deleted a file. With merge.directoryRenames=true, git can move that file into the renamed directory. The rule leaves that pair to the merge.

A cache keyed by (point, ref commit) keeps each answer across branches. Two branches on one commit ask the same pairs. The merge budget (MERGE_BUDGET) counts every pair, as before, so the same branches fit.

Review of the skip conditions

The squash rule stops dl rm and wf reap from deleting commits. So a skipped pair must be a pair that the merge would reject. I checked each condition against git 2.56.1:

  • A point that renames a file into a directory that the ref renamed, so that the merge would put the file back at its old path. Git refuses that merge: CONFLICT (implicit dir rename): Existing file/dir at D2/name in the way. The skip is safe there.
  • A point that adds a file under a directory that the ref renamed, when the ref holds the moved file. With merge.directoryRenames=true this merge is clean and passes. The exception above keeps this pair. A real-git test pins it.

I removed no skip condition.

Tests

  • pair_bases_in and paths_changed_in: parser tests for one, two and no bases, SHA-256 names, paths with a space and a newline, empty diffs, and every malformed shape (each reads as no answer).
  • The pinned argv of both new git calls, and no spawn for an empty list.
  • a_point_that_changes_a_path_the_ref_kept_cannot_pass: the path check, with the directory-rename exception at each depth.
  • the_squash_rule_merges_no_pair_that_cannot_pass: counts merges. Each skip condition makes 0 merges. A refusal or an unreadable answer from either git call makes the 2 merges of before, and clears the commit.
  • real_git_answers_rule_out_pairs_before_any_merge: the parsers against real git output.
  • a_squash_that_renamed_the_directory_the_branch_added_to_is_found: real git, merge.directoryRenames=true.
  • the_squash_rule_makes_no_more_merges_than_its_budget: now also asserts one rev-parse of the bases per merged branch.

I removed the directory-rename exception for one run: two tests failed. I turned the skip off for one run: two tests failed.

Local results on this branch:

  • cargo test --locked --workspace --no-fail-fast: all pass except closing_the_terminal_mid_up_removes_the_token_file_and_kills_the_up in dl/tests/interrupt. That test failed only in full-suite runs while the host load average was 20 to 80. It passed 3 of 3 times alone on this branch, and alone on the base branch. It does not touch the squash rule.
  • cargo clippy --locked --all-targets -- -D warnings: pass.
  • cargo fmt --check: pass.
  • prek on the changed files: pass.

🤖 Generated with Claude Code

Summary by Sourcery

Pre-filter squash candidates so only pairs that could pass are merged, substantially reducing unsaved-change checks without changing their results.

Bug Fixes:

  • Prevent the squash rule from performing merges for point/ref pairs that cannot possibly pass, while preserving the previous fallback behavior when Git answers are unavailable or unparseable.

Enhancements:

  • Add merge-base and changed-path analysis with caching to reduce unnecessary squash-rule work and retain support for directory-rename cases.

Tests:

  • Add parser, Git invocation, skip-condition, failure-fallback, budget, and real-Git coverage for pre-filtering squash pairs.

The squash rule merged every point of a branch into every remote ref it
compares with. On a kinisi_ros clone that was 824 merges for one row of
`dl --ls`, 16 s to 18 s of git, and none of them could pass.

Before the merges, one `rev-parse <ref>...<point>` per batch reads each
pair's merge bases, and one `diff-tree --stdin` reads the paths each side
changed since its base. A pair is not merged when holds_the_change_of
must say no: no single merge base, a ref that changed nothing since the
base, or a point that changes a path the ref left alone. A path the point
added under a directory the ref deleted a file from is left to the merge,
because merge.directoryRenames=true can move it. Answers are cached by
(point, ref commit) across branches.

Any refusal or output in another shape merges every pair, as before. The
budget is charged as before, so the answer does not change.
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add the Git result types that carry merge-base and changed-path data for squash-pair screening.

  • rust/devlaunch-core/src/clients/git.rs L2429-2452

API

Add batched Git interfaces to read symmetric-range merge bases and changed paths before squash merges.

  • rust/devlaunch-core/src/clients/git.rs L1136-1207

Implementation

Parse and validate the batched Git outputs so malformed or incomplete answers cannot incorrectly rule out a pair.

  • rust/devlaunch-core/src/clients/git.rs L2453-2518

Screen and cache squash pairs before merging, retaining pairs whenever pre-screening cannot prove they will fail and accounting for directory renames.

  • rust/devlaunch-core/src/domain/workspace_state.rs L935-939, L1199-1204, L1234-1343 (+4 small edits)

Documentation

Document the squash-rule merge reduction and its measured performance improvement.

  • CHANGELOG.md L18-24

Tests

Test Git invocation, parsers, empty-input behavior, and validation of batched merge-base and changed-path output.

  • rust/devlaunch-core/src/clients/git/tests.rs L1727-1898

Test squash-pair screening, safe fallbacks, merge budgeting, path and directory-rename behavior, and real-Git results.

  • rust/devlaunch-core/src/domain/workspace_state/tests.rs L1317-1319, L1365-1372, L1419-1674

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.79518% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.80%. Comparing base (3b0acc1) to head (f5b8f53).

Files with missing lines Patch % Lines
rust/devlaunch-core/src/clients/git.rs 98.94% 1 Missing ⚠️
rust/devlaunch-core/src/domain/workspace_state.rs 98.59% 1 Missing ⚠️
Additional details and impacted files
Flag Coverage Δ
python 42.98% <ø> (ø)
rust 95.00% <98.79%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
shipped code (rust) 95.00% <98.79%> (+0.02%) ⬆️
harness and tooling (python) 42.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blooop blooop left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

This was generated by AI during review.

Review of #721 at b4771fc1, diff against its base fix/ls-unpushed-check-quadratic (merge base 2b8f8036) only.

Safety question

The squash rule guards dl rm and wf reap. A skip in worth_merging can only remove a pair from the merge batch, and the final answer still comes from holds_the_change_of on the pairs that are left, in the same order. So a skip cannot make a false "saved". The worst case for a wrong skip is a missed squash (a false "unsaved"), which is the safe direction. I tried to find even that case:

  • No base / two or more bases, and base == ref commit. holds_the_change_of returns false for each of these before it merges (merge-base --all gives the same bases as rev-parse <ref>...<point>; a base equal to the ref commit implies other_tree == base_tree). No pair is lost.
  • The point changes a path the ref left alone. In a merge, a change on one side only comes through as that side has it. Rename detection on the ref side starts and ends only at paths the ref touched, and directory-rename moves only act on paths the point added. The exception keeps those.
  • Real git, by hand (git 2.56.1): root-level "directory rename" (the ref moves every top-level file into sub/ and adds sub/n.txt; the point adds n.txt). Git does not rename the top-level directory, so the merge keeps n.txt and the tree differs from the ref. The skip is correct. Rename-to-self (t6423 12i shape) gives CONFLICT (implicit dir rename), so the merge fails and the skip is correct.
  • Real git, randomized: 1,350 random (base, point, ref) repos. Each mixed adds, edits, deletes, file renames, directory renames (also to and from the top level), and mode flips. The ref was often a squash of the point plus more changes. merge.directoryRenames was set to true, conflict or false. 689 pairs passed the merge-tree and tree check. The PR's skip predicate (re-implemented from changes_a_path_the_ref_kept, with the same diff-tree -r --no-renames input) skipped none of them.
  • Submodules, binary files, case-only renames, merge.renames=false and rename limits change no path that the point alone touches. They can only add conflicts, which the skip does not depend on. If diff.ignoreSubmodules=all hides a gitlink, it hides it on both sides, so it can only stop a skip.
  • A refusal or output that the parser cannot read gives true for every pair it did not answer. The cache stores only answers that are sure.

I found no counterexample.

Standards

  • Stale doc (minor). The doc comment on Asked::passed_at still says "Every point is merged into every remote ref the clone has in one Git::trees_of_clean_merges". After this PR, only the pairs that worth_merging keeps are merged. Update that sentence.
  • Duplicated Code (nit). paths_changed copies the tempfile-as-stdin block from trees_of_clean_merges (NamedTempFile::new().ok().filter(write), then SpawnSpec ... with_stdin_file). A small helper would hold both. This is not blocking.
  • Data Clumps (nit). (String, String) as the could_pass key, and (&str, &str) as a diff, are positional pairs. The doc comments say which side is which, so this is acceptable.
  • The names (worth_merging, changes_a_path_the_ref_kept, directories_above, PairBases, Change) say what they mean. The parsers fail closed (to "merge everything"), and tests cover each malformed shape.
  • PR body: it gives the why, the evidence, each skip condition and its safety argument, and it matches the code at this head.
  • Local checks at b4771fc1: cargo test --locked --workspace passed (all 39 test binaries ok). cargo clippy --locked --all-targets -- -D warnings passed.

Spec

No spec available. The PR closes no issue, and its commits reference none. I did not infer requirements from the code.

Verdict

Approve. No finding blocks this PR. The stale passed_at doc is worth a one-line fix before merge, or in a follow-up.

This branch has not been deployed

No deployments
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.

1 participant