Skip to content

fix(tool): break glob mtime ties by path - #809

Merged
Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/glob-deterministic-order
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/glob-deterministic-order

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What

glob sorts matches newest-first with no tiebreak:

files.sort((a, b) => b.mtime - a.mtime)

Array.prototype.sort is stable, so files with an identical mtime keep ripgrep's traversal order — which is filesystem enumeration order. The tool's own description (glob.txt:3) promises "Returns matching file paths sorted by modification time".

Why it matters

A checkout, a build, a clone, or a script that writes files in one burst gives every file the same mtime. That isn't an edge case; it's what a fresh clone looks like.

Then the ordering is decided by the filesystem, so two identical glob calls over an unchanged tree can return the same files in a different sequence. The model diffs two "same" results, sees files apparently appearing and vanishing, and either re-runs the search or reports a change that never happened.

Verified on this machine with four files pinned to one mtime: the pre-fix order was filesystem order (bravo, alpha, delta, charlie).

Verification

test/tool/glob.test.ts did not exist — test/tool/ had grep, ls-tree, and read, but nothing for glob. Fails before, passes after:

(fail) glob > orders equal-mtime files deterministically, newest first
(pass) glob > still orders strictly newer files first
 1 pass  1 fail
(pass) glob > orders equal-mtime files deterministically, newest first
(pass) glob > still orders strictly newer files first
 2 pass
 0 fail

The first test pins the mtime explicitly with fs.utimes and asserts new Set(mtimes).size === 1, so it cannot silently degrade into testing the ordinary path on a filesystem with coarse timestamps. It also runs the tool twice and requires identical output, which is the property that was actually missing. The second test uses genuinely distinct mtimes so the primary newest-first behaviour stays pinned.

The expected order is derived from the same comparator the fix uses rather than hardcoded, so the test states the rule instead of a locale's idea of alphabetical order.

The change

-    files.sort((a, b) => b.mtime - a.mtime)
+    // One checkout or one write burst gives every match the same mtime, so
+    // without a tiebreak the order came from filesystem enumeration and two
+    // identical calls could return the same files in a different sequence.
+    files.sort((a, b) => b.mtime - a.mtime || a.path.localeCompare(b.path))

bun run typecheck clean; touched files are Prettier-clean (checked on LF-normalized copies — this checkout has core.autocrlf=true, which makes Prettier flag every file in the repo).

Related, deliberately not changed here

The 100-file cap is applied during traversal, before the sort, so a truncated result is an arbitrary filesystem-order prefix rather than the 100 most recently modified matches. That is a truthful truncated: true; choosing between "sort before slicing" (memory cost) and "say so in the note" is a design call rather than a bug fix, so it is left alone.

Fixes #808

@vercel

vercel Bot commented Sep 28, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

The comparator had no tiebreak, so files sharing an mtime kept ripgrep traversal order and two identical searches could return the same files in a different sequence. The tool documents the list as sorted by modification time; equal timestamps now fall back to the path.
@ishaan1124
Ishaan Gangwani (ishaan1124) merged commit 34b2a9a into synthetic-sciences:main Sep 29, 2026
8 of 9 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.

glob returns a non-deterministic order for files with the same modification time

2 participants