Skip to content

Commit a0aaedd

Browse files
ezexeclaude
andauthored
p4-phi 0.11.174: a racy walk of the tree is not kept, so a folder added in the clock's same tick is seen on Windows; a commit told another process moved HEAD runs again (#255)
Co-authored-by: Anthropics Connoisseur of Hallucinations <noreply@anthropic.com>
1 parent 717fc3f commit a0aaedd

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎lbiza/plugins/p4-phi/.claude-plugin/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "p4-phi",
3-
"version": "0.11.173",
3+
"version": "0.11.174",
44
"description": "What linked Claude Code sessions hand each other.",
55
"author": {
66
"name": "ezexe"

‎lbiza/plugins/p4-phi/scripts/phi.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3821,6 +3821,10 @@ def remove_main(argv):
38213821

38223822
COMMIT_TRIES = 100 # repeats of a git call whose output names index.lock
38233823
COMMIT_WAIT = 0.1 # seconds between them
3824+
# What git says when another process holds a lock it needs: the index's, or a ref's, as two commits at once race for
3825+
# HEAD and the later one is told "cannot lock ref 'HEAD': is at <new> but expected <old>" (seen on macOS CI,
3826+
# 2026-11-24); a repeat of `git commit --only` builds on the new HEAD.
3827+
GIT_LOCKED = re.compile(r"index\.lock|cannot lock ref|\.lock': File exists", re.I)
38243828

38253829

38263830
def git_again(args, cwd, tries=COMMIT_TRIES, wait=COMMIT_WAIT):
@@ -3829,7 +3833,7 @@ def git_again(args, cwd, tries=COMMIT_TRIES, wait=COMMIT_WAIT):
38293833
for k in range(tries + 1):
38303834
ran = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True, encoding="utf-8",
38313835
errors="replace")
3832-
if ran.returncode == 0 or "index.lock" not in ran.stdout + ran.stderr:
3836+
if ran.returncode == 0 or not GIT_LOCKED.search(ran.stdout + ran.stderr):
38333837
return ran, False
38343838
if k < tries:
38353839
time.sleep(wait)

‎lbiza/plugins/p4-phi/scripts/relay.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def _rows(path):
133133
NODE_PATH_MAX = 180 # the longest folder path claim makes, so the files under it stay within Windows' 260
134134
TREE = "tree" # <root>/phi/tree/<ancestor ids...>/<id>: a node's folder, nested by parent
135135
_TREE = {} # by the root store: [the folders it listed with their times], and {id: (rel, node.json)}
136+
RACY_NS = 2_000_000_000 # a folder time this close to a walk is no proof the folder is unchanged since
136137
_MIGRATING = set() # the roots this process is migrating, so a read inside the migration does not start it
137138
_TMP = re.compile(r"\.tmp-\d+-[0-9a-f]{8}\Z") # a folder a claim is still making
138139

@@ -194,8 +195,17 @@ def _tree(home):
194195
if key not in _MIGRATING and os.path.isdir(old) and _old_stores(old):
195196
migrate_nested(home)
196197
head = (_stamp(os.path.join(home, "phi")), _stamp(old))
198+
walked_at = time.time_ns()
197199
walk = _walk(home)
198-
_TREE[key] = (head, [(d, _stamp(d)) for d in walk[1]], walk[0])
200+
stamps = [(d, _stamp(d)) for d in walk[1]]
201+
# A folder whose time is within RACY_NS of the walk may change again in the same tick of the file system's
202+
# clock, which on Windows moves in steps of about 16 ms, and the change would leave its time as it was: such a
203+
# walk is not kept, so the next call walks again, as git treats a racily clean index entry.
204+
racy = any(m is not None and m >= walked_at - RACY_NS for m in [*head, *(m for _, m in stamps)])
205+
if not racy:
206+
_TREE[key] = (head, stamps, walk[0])
207+
else:
208+
_TREE.pop(key, None)
199209
return walk[0]
200210

201211

‎lbiza/tests/p4-phi/test_phi.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,20 @@ def test_it_refuses_a_path_outside_the_checkout_and_one_under_dot_claude(self):
29032903
self.assertEqual(self.git("rev-parse", "HEAD"), before)
29042904
self.assertEqual(self.commit("m").returncode, 2, "no path")
29052905

2906+
def test_a_commit_told_that_another_process_moved_head_is_run_again(self):
2907+
"""Seen on macOS CI, 2026-11-24: of two commits at once the later was told "cannot lock ref 'HEAD': is at
2908+
<new> but expected <old>" and gave up; git_again runs it again, as it does for index.lock."""
2909+
from unittest import mock
2910+
told = [subprocess.CompletedProcess([], 128, "", "fatal: cannot lock ref 'HEAD': is at 1 but expected 2\n"),
2911+
subprocess.CompletedProcess([], 0, "ok", "")]
2912+
with mock.patch.object(phi.subprocess, "run", side_effect=told) as run, mock.patch.object(phi.time, "sleep"):
2913+
ran, locked = phi.git_again(["commit"], ".", tries=3, wait=0)
2914+
self.assertEqual((ran.returncode, locked, run.call_count), (0, False, 2))
2915+
with mock.patch.object(phi.subprocess, "run",
2916+
return_value=subprocess.CompletedProcess([], 1, "", "nothing to commit")) as run:
2917+
self.assertEqual(phi.git_again(["commit"], ".", tries=3, wait=0)[0].returncode, 1)
2918+
self.assertEqual(run.call_count, 1, "another failure is not repeated")
2919+
29062920
def test_two_processes_started_at_once_on_two_paths_both_commit_each_holding_its_own(self):
29072921
for k in range(4):
29082922
self.put("a.txt", f"a{k}\n")

‎lbiza/tests/p4-phi/test_relay.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,17 @@ def test_the_cache_sees_a_folder_added_under_the_tree_by_another_process(self):
20642064
write(os.path.join(folder, "w9", relay.NODE), json.dumps({"id": "w9", "parent": "kid"}))
20652065
self.assertEqual(sorted(relay.nodes(self.home)), ["kid", "w9"], "no forget() needed")
20662066

2067+
def test_a_folder_added_in_the_same_tick_of_the_clock_as_the_walk_is_seen(self):
2068+
"""Seen on Windows CI, 2026-11-24: a folder made right after a walk left its parent's time as it was, since
2069+
the file system's clock moves in steps; a walk whose folders are that fresh is not kept."""
2070+
folder = relay.claim(self.home, "kid", "session", "top")
2071+
with mock.patch.object(relay, "_stamp", lambda path: 1 if path else None): # every time the same, as in one tick
2072+
with mock.patch.object(relay.time, "time_ns", lambda: 1 + relay.RACY_NS // 2):
2073+
self.assertEqual(sorted(relay.nodes(self.home)), ["kid"])
2074+
os.makedirs(os.path.join(folder, "w9"))
2075+
write(os.path.join(folder, "w9", relay.NODE), json.dumps({"id": "w9", "parent": "kid"}))
2076+
self.assertEqual(sorted(relay.nodes(self.home)), ["kid", "w9"], "a racy walk is walked again")
2077+
20672078
def test_two_processes_that_claim_at_once_get_two_folders_for_two_ids_and_one_for_one(self):
20682079
code = ("import sys; sys.path.insert(0, sys.argv[1]); import relay; "
20692080
"print(relay.claim(sys.argv[2], sys.argv[3], 'session', 'top'))")

0 commit comments

Comments
 (0)