Skip to content

fix(search): make last_n_commits mean N commits, not N first-parents - #530

Merged
HananKavitzAmat merged 3 commits into
chunkhound:mainfrom
tharunsridhar:fix/last-n-commits-512
Nov 18, 2026
Merged

HananKavitzAmat merged 3 commits into
chunkhound:mainfrom
tharunsridhar:fix/last-n-commits-512

Conversation

@tharunsridhar

Copy link
Copy Markdown
Contributor

Fixes #512

last_n_commits=N was turned into HEAD~N..HEAD. ~N follows first parents only, so in a history with merge commits the window covered more than N commits, and once N exceeded the first-parent depth git failed with a raw bad revision error, leaving the older commits unreachable.

N is now resolved against the commits git actually reports (rev-list --parents) and the diff starts at the parent of the Nth commit.

  • When the window reaches the root commit, the repository's empty tree is diffed against HEAD so the root commit is included. Works for SHA-1 and SHA-256 repositories.
  • A value above the reachable history is clamped to the full history with one warning.
  • The diff is still a single git invocation.
  • The MCP tool descriptions and the CLI help now say what the parameter means.

Tests

tests/unit/test_last_n_commits_range.py (7 tests) builds real git repositories, including a merge history where the old HEAD~5..HEAD range fails, plus the root-commit, SHA-256 and clamping cases. tests/integration/test_deep_research_with_diff.py is adjusted for the new resolution path. 59 tests across the neighbouring suites pass.

Note on running them: the repo-root chunkhound_native stub shadows the compiled extension under pytest's default import mode, so I ran with --import-mode=importlib.

@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

Summary: The PR fixes the HEAD~N failure on merge history and the root-commit case. It still doesn't return exactly N commits on merge history, because the commit ordering is wrong. ####

Strengths

  • It counts commits with rev-list, so the old "bad revision" crash is gone. It also handles the root commit and SHA-256 repos via _empty_tree_oid, which already existed.
  • It makes one diff call, warns once when N is clamped, and uses the same wording in the CLI help, the MCP tool descriptions and the docstrings.
  • The tests use real git repos: linear history, merge history, SHA-1 and SHA-256, clamping and an empty repo. They check behaviour, not internals, which fits TESTING_PHILOSOPHY.

Issues

Major

  • runner.py:~170, rev-list uses default date order. git rev-list --max-count=N sorts by commit date, not by graph topology. The oldest commit in that list gives X^..HEAD, which is then a different set from the N commits.
    • I reproduced this on a repo with main (a, b, m1) and a feature branch (f1, f2), merged into main at M. With N=3, rev-list returned {M, f2, m1}. The oldest of those is m1, so the range became m1^..HEAD. That range covers 4 commits (M, f2, m1, f1) and includes f1,ted window.
    • The clamp warning and the docs both say "N commits", so this is a contract mismatch. Branches with clock skew
      would make it worse.
    • The merge test only checks that f1, f2 and m3 appear. It never checks that nothing extra is included, so it
      doesn't catch this.
    • Fix options: use --topo-order, or compute the diff as "tree at HEAD versus the tree before the window" and
      document that it is a tree diff. Whichev assert the exact file set.
  • runner.py:~185, shallow clones. In a shallow clone the boundary commit appears parentless. The code treats it as the
    root and diffs against the empty tree. Thae shallow boundary into "additions", and itis not a clamp to the real history. Detect it with git rev-parse --is-shallow-repository, or at least document it.

Minor

  • runner.py, _rev_list_parents. It copies thrror and wait boilerplate that_empty_tree_oid already has. The two also differ: this one has no asyncio.shield, and _empty_tree_oid does. Pull the
    shared parts into one helper such as run
  • runner.py, error message. It says "counts first-parent commits back from HEAD", which is the opposite of what the PR
    now does. That line should be removed or c
  • common_arguments.py:189. The help string is one very long line. Wrap it to match the file's line-length limit and
    the surrounding style.
  • tools.py:~594, services=None default. _resolve_commit_range takes services: DatabaseServices | None = None and
    raises if it's None when last_n_commits ismeter, since both callers pass it.
  • tools.py, local import. The function-level import of resolve_last_n_commits_range is probably unnecessary, because
    core.git_diff is already imported lazily es a deliberate circular-import guard, add acomment saying so.
  • tools.py docs. The tool descriptions mentiut a loguru warning on the server is notvisible to the MCP caller. Say that the result is clamped silently from the caller's side, or include a note in the
    response.
  • Tests. The two integration tests now patch resolve_last_n_commits_range to return HEAD~5..HEAD. That is the old
    buggy semantics, and it hides the wiring. ter. I checked that the return_value=Nonepatches of _resolve_commit_range in the other tests still work, since patching an async function gives an AsyncMock.

Reusability

  • Extract a shared _run_git_capture(cwd, *are used by both _empty_tree_oid and_rev_list_parents.
  • Add --topo-order to the rev-list call. A c-list --max-count=N HEAD combined with gitmerge-base/^@ exclusion, so the diff base is derived from commits that are not in the window.
    Decision: REQUEST CHANGES. The N-commits contract is still wrong on branchy history, and the tests don't catch it. Fix the ordering, add an exact-set assertion, anrest is minor cleanup.

@tharunsridhar

Copy link
Copy Markdown
Contributor Author

CI note: the only red jobs on this PR are rust-checks (macos-latest) and rust-checks (windows-latest). I don't think this change causes them.

  • Both fail at "Run Rust unit tests". The binary builds, then dies at startup before any test runs. macOS reports dyld: Library not loaded: @rpath/libduckdb.dylib. Ubuntu showed the same failure with libduckdb.so: cannot open shared object file on the earlier attempt I checked.
  • This PR only changes Python (runner.py, tools.py, common_arguments.py and tests). It has no Rust, Cargo.* or CI changes.
  • main fails the same way. Its CI runs on 11-13 and 11-15 failed too, while 11-12 passed. Nothing in src/, Cargo.* or .github changed between the last good run and the first bad one. I only read the 11-13 run's logs, not the 11-15 run's, so I haven't confirmed that run failed at the same step.
  • All Python, site, watchman and native-wheel jobs pass.

My guess is a stale build or DuckDB cache in Actions, since the macOS log shows a cache hit on the Rust build artifacts. I can't confirm that. If a maintainer can clear the Actions caches and re-run, that should show whether it's the cause. Happy to rebase and re-push once it's sorted.

@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

The issue with rust is unrelated to your PR. It is being handled.
#529

@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

@tharunsridhar The rust-checks issue is resolved in main. Please pull the fix.

last_n_commits was converted to HEAD~N..HEAD. ~N walks first parents, so
with merge commits it covered more than N commits, and past the
first-parent depth git failed with a raw 'bad revision' error, leaving the
older commits unreachable.

Resolve N against the commits git reports (rev-list --parents) and diff
from the Nth commit's parent. When the window reaches the root commit, diff
the repository's empty tree against HEAD so it is included, in SHA-1 and
SHA-256 repositories. A value above the reachable history is clamped to the
full history with one warning. The diff itself stays a single git
invocation. Tool descriptions and CLI help state the semantics.

Fixes #512
…shallow clones

Address review of #530:

- rev-list now uses --topo-order, so the window no longer depends on commit
  dates (clock skew could pick a different set of commits).
- The base is derived from the window's boundary commits: a single boundary
  gives an exact N-commit range; several boundaries (merged branches) use
  their common ancestor, which can include extra commits from those branches.
  The docstring says so.
- A parentless commit in a shallow clone is the edge of the fetched history,
  not the root. Diff from it with a warning instead of reporting its whole
  snapshot as additions.
- Share the git subprocess handling between the empty-tree lookup, rev-list,
  rev-parse and merge-base through _run_git_capture, which also gives
  rev-list the shielded cleanup the empty-tree lookup had.
- Drop the stale "first-parent" wording, wrap the CLI help, document the
  local import, make services a required argument, and state that a value
  above the history covers the full history.
- Tests assert exact file sets for the merge, two-boundary, skewed-date and
  shallow cases, and the integration tests no longer patch in HEAD~5..HEAD.

Refs #512
@tharunsridhar
tharunsridhar force-pushed the fix/last-n-commits-512 branch 3 times, most recently from 265942f to b2c09a3 Compare November 17, 2026 13:10
@tharunsridhar

Copy link
Copy Markdown
Contributor Author

Thanks, I pulled the fix. I rebased this branch and #531 onto the latest main (b171ede) and force-pushed both.

I also ran the tests locally on the rebased branch: cargo test passes (135 passed, 0 failed) and the Python tests for this change pass (10 of 10).

CI hasn't started on the new commits (no checks are reported). If it needs approval to run for a fork PR, could you approve the workflow run when you get a chance? Thanks.

@tharunsridhar

Copy link
Copy Markdown
Contributor Author

CI has run on this PR now and rust-checks passes on Ubuntu, macOS and Windows. Thanks for approving it.

Two macOS jobs show as cancelled rather than failed: tests (macos-latest, 3.14, 3) and site-build-validation (macos-latest). They never ran any steps, so it looks like a runner cancellation and not something in this change. Could you re-run them when you get a chance?

Also, #531 (same author) still has no checks and looks like it is waiting for a workflow approval. Could you approve it as well? Thanks.

@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

@tharunsridhar Thanks.
We are expiriencing github actions slowness.
I will rerun this in PR once the slowness is resolved.

@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

looks good

@HananKavitzAmat
HananKavitzAmat added this pull request to the merge queue Nov 18, 2026
Merged via the queue into chunkhound:main with commit d9ba078 Nov 18, 2026
126 of 134 checks passed
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.

fix(search): make last_n_commits mean N commits, not N first-parents

2 participants