Skip to content

fix(git): invalidate settled diff cache when a file's ctime advances - #47639

Merged
nwparker merged 1 commit into
stablyai:mainfrom
0718lol:fix/diff-cache-preserved-mtime
Nov 23, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
0718lol:fix/diff-cache-preserved-mtime

Conversation

@0718lol

@0718lol 0718lol commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Some tools rewrite a file in place and then put its old "last modified" time back. When the new content has the same length, Orca's diff cache thought nothing had changed and kept showing the old diff. Orca now also checks the file's "status changed" time (ctime), which the operating system always updates on a write, so the cache notices the change.

What Changed

  • Before: a working-tree file's diff-cache stamp was mtime:size:inode. An in-place rewrite that kept size, inode and mtime (touch -r, rsync --times, some editors and formatters) gave the same stamp, so the settled diff cache returned the old diff until something else invalidated it.
  • After: the stamp is mtime:ctime:size:inode. The racy-write guard (no caching within 2s of a write) now uses the later of mtime and ctime, so a freshly rewritten file stays out of the cache until its timestamps settle.
  • Mechanism: readWorkingTreeComponent in input-stamps/worktree-diff-stamp.ts reads ctimeMs from the stat result it already has. StampComponent.mtimeMs / WorktreeDiffStamp.newestMtimeMs are renamed to timestampMs / newestTimestampMs because they can now hold ctime. Ref and config stamps (readFileStampComponent, the commit-pair cache) still use mtime only; only the field names changed there.

Why

Two different file versions produced identical stamps, so the cache could serve stale diffs indefinitely. ctime cannot be set from user space with utimes, so it catches timestamp-preserving rewrites. The alternative, hashing file contents, would add reads on every diff.

Linked Issue

Fixes #47631

Visual Proof

N/A for screenshots. The bug is stale data from the main-process diff cache, not a rendering change. Before/after from the production diff reader on a real Git fixture (macOS): same-size rewrite with the mtime restored. Inode and size stay the same, and ctime advances.

                                 main (before)   this PR (after)
first read (diff content)        "yyyy\n"        "yyyy\n"
git numstat after rewrite        2  1            2  1
second read (diff content)       "yyyy\n" stale  "a\nbc\n" fresh
second read (line stats)         +1 -1 stale     +2 -1 fresh

Testing

  • Reproduced on macOS with real Git: a probe test rewrote a file in place, kept its size, inode and mtime, then read the diff twice. On main the second read returned the stale diff. With this change it matches Git.
  • Added worktree-diff-stamp.test.ts: two stats with the same mtime, size and inode and different ctimes must produce different stamps, and newestTimestampMs must equal the newer ctime. It fails on main (identical stamps) and passes here.
  • vitest run src/main/git (17 files, 92 tests) passes. oxfmt, oxlint, anti-slop, code-quality native plugins, ast-grep and the changed-code quality gate are clean on the touched files.
  • Platforms: macOS. Linux, Windows and remote SSH hosts use the same Node fs.stat field (ctimeMs), which Node fills on every platform (on Windows it is the NTFS change time).

Performance

There are no new syscalls or subprocesses. The same async stat call supplies ctimeMs, and the change only adds one more number to each file's stamp string. This runs once per file on diff reads, not on a polling path. I benchmarked readHostFilesStamp against main (ms, 1 file / 512 files). The differences are within run-to-run noise:

         1 file p50/p95     512 files p50/p95
main     0.189 / 0.261      1.771 / 2.415
branch   0.202 / 0.268      1.913 / 2.795

Trade-off: right after a timestamp-preserving rewrite, the file stays out of the cache for the existing 2s margin, where it was previously cached with stale content.

Risk

Level: Low. On filesystems that update ctime when only metadata changes (chmod, chown, link count), the cache misses more often. That costs a normal Git read and never returns wrong data. The stamp lives only in memory, so mixed-version and remote compatibility is not affected. To back it out, revert this commit.

Review

Original fix by @0718lol in #47639. Authorship is kept on the squashed commit.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Working-tree stamps now include ctime in their identity and use the later of ctime and mtime as their timestamp. Stamp freshness and clock-skew checks use the newest timestamp. Commit-pair scope checks also use timestamps. A test verifies that a ctime change produces a different stamp when size, inode, and mtime stay the same.

Severity of issue fixed: Medium

Merge Risk: Low · up to bc3ae

This fixes stale unstaged diffs after in-place rewrites that preserve size and modification time. Because change time now also invalidates the cache, diffs for recently modified files may be recomputed more often. Check that the extra recomputation is acceptable before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Issue #47631 requires that a second unstaged diff read after an in-place rewrite that preserves size, inode, and mtime returns the new contents. At head bc3ae09, readWorkingTreeComponent in `worktr…
Out of Scope Changes check Passed All changes support the #47631 fix. In worktree-diff-stamp.ts, the renames mtimeMs to timestampMs and newestMtimeMs to newestTimestampMs carry the ctime-aware value through `WorktreeDiffStam…
Title check Passed The title names the main change: the settled diff cache is invalidated when a file's ctime advances. The changeset adds ctime to working-tree file stamps and renames the timestamp fields to match, so …
Description check Passed The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #47631), Visual Proof (N/A with reason and a before/after table), Testing, Performance, Ris…
Full details: Linked Issues check

Explanation

Issue #47631 requires that a second unstaged diff read after an in-place rewrite that preserves size, inode, and mtime returns the new contents. At head bc3ae09, readWorkingTreeComponent in worktree-diff-stamp.ts builds the stamp text as ${mtimeMs}:${ctimeMs}:${size}:${inode}, so a ctime change changes the stamp value and forces a cache miss. Its timestampMs is Math.max(mtimeMs, ctimeMs), so canProveUnchangedByStamp also refuses reuse within the two-second margin. The same stat call supplies both values, so no filesystem reads are added. The regression test in worktree-diff-stamp.test.ts mocks stat with equal mtime, size, and inode and different ctime, then asserts that the stamp value differs. I read the test source. I did not run it, so its pass or fail status relies on the PR summary. The non-coding reproduction steps add no requirements.

Full details: Out of Scope Changes check

Explanation

All changes support the #47631 fix. In worktree-diff-stamp.ts, the renames mtimeMs to timestampMs and newestMtimeMs to newestTimestampMs carry the ctime-aware value through WorktreeDiffStamp, canProveUnchangedByStamp, and isDiffStampClockSkewed. requireMtimeMs becomes requireTimestampMs for the same reason and also checks ctime. readFileStampComponent keeps mtime and size only, so its behavior does not change. In commit-pair-cache.ts, shallow and packed-refs components keep timestampMs equal to mtimeMs, so commit-pair behavior is unchanged. The test file and the settled-diff-cache.ts documentation changes are supporting changes. The diff adds no commands, UI, agent, or integration changes.

Full details: Title check

Explanation

The title names the main change: the settled diff cache is invalidated when a file's ctime advances. The changeset adds ctime to working-tree file stamps and renames the timestamp fields to match, so the title is related to the main change. It follows the conventional type(scope) form and is 68 characters long.

Full details: Description check

Explanation

The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #47631), Visual Proof (N/A with reason and a before/after table), Testing, Performance, Risk with level, Review, Agent skill boundary, Notes and Checklist. The Performance section gives a concrete reason and benchmark data. The AI Disclosure section is empty, but the template marks it as optional for StablyAI team members, and the author's identity is not stated. The final pnpm checklist item is unchecked, and the description says the full lint, typecheck, test and build suites were not run locally.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b505ee0e-84ca-4a03-ba3d-d864fa1e1d73
Commits

Reviewing files that changed from the base of the PR and between 4269320 and bc3ae09.

Files selected for processing: 2

  • src/main/git/source-control/input-stamps/worktree-diff-stamp.ts
  • src/main/git/source-control/settled-diff-cache.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Working-tree file stamps now include ctime alongside mtime, size and
inode, and the racy-write guard uses the later of the two timestamps.
A timestamp-preserving in-place rewrite (same size, inode and mtime)
previously produced an identical stamp, so the settled diff cache served
the old diff. The same stat call already reports ctime, so no extra I/O.

Fixes stablyai#47631

Co-authored-by: Neil <[email protected]>
@nwparker
nwparker force-pushed the fix/diff-cache-preserved-mtime branch from 4269320 to bc3ae09 Compare November 23, 2026 12:36
@nwparker nwparker changed the title fix(git): invalidate diff cache when file change time advances fix(git): invalidate settled diff cache when a file's ctime advances Nov 23, 2026
@nwparker
nwparker merged commit 4ca2cae into stablyai:main Nov 23, 2026
55 of 56 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.

[Bug]: Unstaged diff stays stale after an in-place edit preserves file size and mtime

2 participants