Skip to content

[webkitscmpy] Invalidate cache when branches move backward - #76262

Closed
JonWBedard wants to merge 1 commit into
WebKit:mainfrom
JonWBedard:eng/webkitscmpy-Invalidate-cache-when-branches-move-backward
Closed

JonWBedard wants to merge 1 commit into
WebKit:mainfrom
JonWBedard:eng/webkitscmpy-Invalidate-cache-when-branches-move-backward

Conversation

@JonWBedard

@JonWBedard JonWBedard commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

d2b36d4

[webkitscmpy] Invalidate cache when branches move backward
https://bugs.webkit.org/show_bug.cgi?id=326753
rdar://189460513

Reviewed by NOBODY (OOPS!).

Handle case where a branch move backward. Particularly common when contributors
stage a change on main and then move that change to a PR branch.

* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py:
(Git.Cache.load):
(Git.Cache._fill):
(Git.Cache.populate):
(Git.Cache.clear):
(Git):
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/git_unittest.py:

d2b36d4

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win ❌ 🛠 ios-apple
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests ❌ 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ❌ 🧪 api-mac ✅ 🧪 api-wpe ✅ 🛠 vision-apple
✅ 🧪 webkitpy ✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-debug
✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🧪 mac-AS-debug-wk2 ✅ 🛠 gtk
✅ 🧪 services ✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

https://bugs.webkit.org/show_bug.cgi?id=326753
rdar://189460513

Reviewed by NOBODY (OOPS!).

Handle case where a branch move backward. Particularly common when contributors
stage a change on main and then move that change to a PR branch.

* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py:
(Git.Cache.load):
(Git.Cache._fill):
(Git.Cache.populate):
(Git.Cache.clear):
(Git):
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/git_unittest.py:
@JonWBedard JonWBedard self-assigned this Oct 8, 2026
@JonWBedard JonWBedard added the Tools / Tests Tools in the Tools directory, build issues, test infrastructure, and bugs in test cases label Oct 8, 2026

@gsnedders gsnedders left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think one walk can tell whether commits have left the default branch: it can't distinguish a rewind from local main simply being ahead of or behind origin/main, which happens all the time in the normal git-webkit workflow (more in the inline comments). origin/main doesn't rewind, so the real case is git-webkit pr: it moves the commit to eng/…, then runs git branch -f main remotes/origin/main, leaving the commit on no default-branch ref:

if did_local_branch_diverge and run([
repository.executable(), 'branch', '-f',
branch_point.branch,
'remotes/{}/{}'.format(source_remote, branch_point.branch),
], cwd=repository.root_path).returncode:

So I think detection needs to ask about every default-branch ref: local main plus each of source_remotes()'s main, by explicit ref name rather than a refs/remotes/*/main wildcard. A single git rev-list can list exactly the cached commits that are on none of them, so only those need removing (more in the inline comment on _fill(None)).

Even then, this only takes effect when something repopulates main. commit() only populates the cache on a miss, and a stale N@main is a hit, so straight after git-webkit pr a lookup still says N@main until another branch gets populated (see the thread on #76103).

I've put tests up as a PR against this branch: JonWBedard#2. Three pass without this PR and fail with it (quoted inline), and one covers the git-webkit pr case, failing without it and passing with it.


self._fill(self.repo.default_branch)
for branch in self._ordered_commits.keys():
for branch in list(self._ordered_commits.keys()):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're copying this… so we can remove items from it during iteration.

Nit: I might be tempted to add a comment to explain this, especially given the indirection via d.

Though I do wonder if we should actually be looking at the union of keys: self._ordered_commits.keys() | self._ordered_revisions.keys() | self._last_populated.keys() (which would also handle the avoiding copying!).

self._fill(branch)
except BaseException:
pass
except (IOError, OSError, ValueError, KeyError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IOError = OSError since Python 3.3, so you don't need both.

The tuple also misses some damaged-but-valid-JSON cases the old BaseException caught: IndexError from _fill when revisions[branch] is shorter than hashes[branch], and TypeError/AttributeError when an entry isn't the expected type. Those now escape Cache.__init__, so local.Git(path) raises instead of falling back to populating:

webkitscmpy.test.git_unittest.TestGit.test_cache_file_revisions_shorter_than_hashes erred:
  ...
    File "Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py", line 113, in _fill
      if self._ordered_revisions[branch][index]:
  IndexError: list index out of range

Exception is probably right here.

except BaseException:
pass
except (IOError, OSError, ValueError, KeyError):
self.repo.log("Failed to load identifier cache from '{}'".format(self.path))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd be tempted to split this up into smaller try/except blocks, with more specific error messages?

We have two largely separate things in the try block:

  1. Loading self._ordered_commits and self._ordered_revisions from the JSON file.
  2. Making sure everything is consistent and refilling the cache.

It probably makes sense to treat these two separately?

Whichever way it's split, the failure path should also reset _ordered_commits, _ordered_revisions and both identifier maps, rather than leaving whatever was half-loaded.

Comment on lines +88 to +90
for branch in self._ordered_commits.keys():
if branch != default_branch:
self._fill(branch)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is default_branch the only special case, or should we do all production branches first, maybe linked to priority order so that we build older versions first (thinking especially of safari-7625.1-branch before safari-7625.1.2-branch).

I guess currently we don't do this in the rest of the code, so that's probably not a change for now, and this code is fine?

return
# Only bail early if the branch hasn't moved. If our tip is a cached commit which isn't the last
# one we know of, the branch was moved backwards (reset, rebase) and the cache must be truncated.
if intersected and len(hashes) <= 1 and self._ordered_commits[branch] and self._ordered_commits[branch][-1] == hashes[0]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is where a walk stopping at a cached commit that isn't the cached tip becomes a truncation. That also happens when nothing has been rewound:

  • Ahead: you've committed on main but not yet run git-webkit pr. Populating any other branch walks origin/main first, which stops short of the unpushed commit, so it's dropped. The populate(main) straight after is skipped because _last_populated['main'] was just set, so it's only added back by a later lookup:

    if not is_default_branch:
    self.populate(branch=self.repo.default_branch, remote=self.repo.default_remote)
    self.populate(branch=self.repo.default_branch)

  • Behind: after a git fetch that doesn't update local main, walking local main drops every commit that's only on origin/main, though they're still on the default branch:

    webkitscmpy.test.git_unittest.TestGit.test_cache_local_default_branch_behind_remote failed:
      ...
      AssertionError: None != '5@main'
    

Lookups stay correct, since commit() falls back to git on a miss, but each drop costs a full rebuild (see my comment on _fill(None)). I think the decision to truncate needs to come from all the default-branch refs, not this one walk.

self._branch = None
self.cache = self.Cache(self) if self.root_path and cached else None
self.default_remote = 'origin'
self.cache = self.Cache(self) if self.root_path and cached else None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be worth making explicit why this reorder had to happen.

def _fill(self, branch=None):
default_branch = self.repo.default_branch

# Without a branch, rebuild identifiers for every branch from scratch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feels entirely too aggressive — we shouldn't need to drop everything?

Only the commits that are no longer on any default-branch ref need dropping, and git can list exactly those, starting from the cached main tip (self._ordered_commits[default_branch][-1], before populate() changes it):

git rev-list --ignore-missing <cached main tip> --not refs/heads/main refs/remotes/origin/main …

with one refs/remotes/<remote>/main for each of source_remotes(). --ignore-missing covers remotes without a main. Every cached main entry is an ancestor of that tip, so this covers the whole list, but it only walks the part no ref has. That's empty when local main is just ahead of or behind origin/main, and is only the moved commit after git-webkit pr. It takes about 10–15 ms on a full WebKit checkout, and only needs running when populate()'s walk stops short of the cached tip.

Then only those hashes need removing from _hash_to_identifiers and _revisions_to_identifiers, plus re-_filling any branch whose branch point is among them. Everything before them keeps its identifier.

As it is, with a WebKit-sized main (~323k) this takes about 0.5 s, against microseconds when only the tail needs filling, and it's reachable from to_identifier/to_hash on a miss. In the ahead case (see my comment on the early return in populate()) it happens on every lookup that populates another branch, recomputing all of main when nothing on it has changed:

webkitscmpy.test.git_unittest.TestGit.test_cache_local_default_branch_ahead_of_remote failed:
  ...
  AssertionError: 8 != 13

(that's the identifiers computed beyond loading the cache: 2 for the branch's own commits, against 7).

@JonWBedard

Copy link
Copy Markdown
Member Author

Closing in favor of #77838, which is a different way of solving this problem.

@JonWBedard JonWBedard closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Tools / Tests Tools in the Tools directory, build issues, test infrastructure, and bugs in test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants