From f7f37dffe35ec9dc77e21a01c32f63f76aa5ee80 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 2 Dec 2026 13:07:18 -0700 Subject: [PATCH] Session start reads the store's old rules before the project file _move and _apply write the project file first and remove or commit the store copy second, so a reader that never waits must read the store copy first: it then either sees the lines there or the project file is already complete. Reading the project file first could miss both (fault 6). Adds a deterministic test that runs a whole move between the two reads. Co-Authored-By: Claude Opus 5.5 --- sisi/rules.py | 5 ++++- tests/test_core.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/sisi/rules.py b/sisi/rules.py index f8694f5..69ccd92 100644 --- a/sisi/rules.py +++ b/sisi/rules.py @@ -210,8 +210,11 @@ def scope_lines(scope, cwd=None, migrate=True): if migrate: return _migrate(cwd)[0] dest, old = path(PROJECT, cwd), _legacy(cwd) + # The store copy first, the project file second: `_move` writes the project file before it removes the store + # copy, so a no-lock reader either reads the copy before it goes or knows the project file is already complete. + ls = lines(old) raw = store.regular(dest, True)[1] if dest and not os.path.islink(os.path.dirname(dest)) else None - return list(dict.fromkeys((_parse(raw.decode("utf-8", "replace")) if raw is not None else []) + lines(old))) + return list(dict.fromkeys(ls + ((_parse(raw.decode("utf-8", "replace"))) if raw is not None else []))) _in_store = lambda p: p.startswith(os.path.join(store.DATA, "")) diff --git a/tests/test_core.py b/tests/test_core.py index 9e8aafa..22bfe4b 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -922,6 +922,26 @@ class Core(unittest.TestCase): self.assertEqual(len([l for l in got if learned in l]), 1, (n, rep, got)) self.assertFalse(os.path.exists(old), (n, rep)) + def test_a_rules_read_between_a_moves_two_writes_still_sees_the_old_rules(self): + """A session start that does not wait reads while another start moves the old rules into `.sisi/rules.md`; + the reader sits between its two reads while the move commits, so it must still see every line.""" + from sisi import rules, store + p = self.project("mid-move") + row = "- Old store rule read while the move commits [owner 2026-11-20]" + old, dest = self.legacy(p, row + "\n"), os.path.join(p, ".sisi", "rules.md") + fired, orig = [], store.regular + def regular(path, load=False): + out = orig(path, load) + if load and os.path.realpath(path) == os.path.realpath(dest) and not fired: + fired.append(1) # the reader has read one copy: the whole move commits before it reads the other + self.assertEqual(rules._move(dest, old), "") + return out + with mock.patch.object(store, "regular", regular): + got = self.in_process(lambda: rules.scope_lines(rules.PROJECT, p, migrate=False))[0] + self.assertIn(row, got) + self.assertIn(row, self.read(dest)) # the injected move really did complete + self.assertFalse(os.path.exists(old)) + def test_old_rules_land_in_the_main_checkout_from_a_linked_worktree(self): tracked = "- Checked in rule from the main branch [owner 2026-11-20]\n" self.mkdir(self.proj, ".sisi")