From 2e6c038ff839aa85c8a3f5913ea4ad44059401f4 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Thu, 19 Nov 2026 13:27:58 +0100 Subject: [PATCH 1/2] Keep a user's corpora in the state directory when the registry is shipped In a container the registry is part of the image and the session's user cannot write it. Opening SPEAR on a tree the image does not know offered to register its sub-trees, then died on the write. What a session adds now goes to projects.json in the state directory, which is read along with the shipped registry; the shipped entry wins on a name. An install whose state directory is the install itself behaves as before. --- client/cli/corpus_registry.py | 57 ++++++++++++++++--- .../test_corpus_behaviour_is_declared.py | 36 ++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/client/cli/corpus_registry.py b/client/cli/corpus_registry.py index e3e7bee..2b10ab5 100644 --- a/client/cli/corpus_registry.py +++ b/client/cli/corpus_registry.py @@ -72,17 +72,42 @@ def _warn_once(message): print(message, file=sys.stderr) +def _read_registry(path): + try: + with open(path) as f: + raw = json.load(f) + except (OSError, json.JSONDecodeError): + return {} + + return raw if isinstance(raw, dict) else {} + + +def user_projects_file(): + """Where the user's own corpora are kept when the registry is not theirs + to write: a container ships it in the image, and what a session registers + there lands in the state directory instead. None when the state directory + is the install itself.""" + from cli import chat_settings + + if os.path.realpath(chat_settings.STATE_DIR) == os.path.realpath(APP_DIR): + return None + + return os.path.join(chat_settings.STATE_DIR, "projects.json") + + def load_projects(): """Return {name: {"path","kind", ...}}. Tolerates the legacy {name: path} format (treated as generic). Extra keys (exclude, include_build — see reindex_options) are PRESERVED: rebuilding specs from path/kind alone erased them at the first save_projects().""" - try: - with open(PROJECTS_FILE) as f: - raw = json.load(f) - except (OSError, json.JSONDecodeError): - raw = {} + raw = _read_registry(PROJECTS_FILE) + own = user_projects_file() + + # The shipped registry wins on a name: the user's file only adds. + + if own: + raw = {**_read_registry(own), **raw} projects = {} @@ -122,9 +147,25 @@ def save_projects(projects): with open(PROJECTS_FILE, "w") as f: json.dump(projects, f, indent=2) except OSError as exc: - # The registry is shipped configuration, not session state: in a - # container it lives in the image and is not the user's to change. - # Say where it has to be done instead of raising a stack trace. + # The registry is shipped configuration: in a container it lives in + # the image and is not the user's to change. What the session adds to + # it goes to the user's own file; without one, say where it has to be + # done instead of raising a stack trace. + + own = user_projects_file() + shipped = _read_registry(PROJECTS_FILE) + + if own: + try: + os.makedirs(os.path.dirname(own), exist_ok=True) + + with open(own, "w") as f: + json.dump({name: spec for name, spec in projects.items() + if name not in shipped}, f, indent=2) + + return + except OSError: + pass raise SystemExit( f"cannot write the corpus registry ({PROJECTS_FILE}): {exc}\n" diff --git a/client/tests/test_corpus_behaviour_is_declared.py b/client/tests/test_corpus_behaviour_is_declared.py index 1c4e75f..e63a251 100644 --- a/client/tests/test_corpus_behaviour_is_declared.py +++ b/client/tests/test_corpus_behaviour_is_declared.py @@ -68,6 +68,42 @@ def register(self, **spec): return spec +class AShippedRegistryTakesTheUsersAdditionsElsewhere(Registry): + def setUp(self): + super().setUp() + from cli import chat_settings + + self.addCleanup(setattr, chat_settings, "STATE_DIR", chat_settings.STATE_DIR) + chat_settings.STATE_DIR = f"{self.root}/state" + + with open(corpus_registry.PROJECTS_FILE, "w") as f: + json.dump({"shipped": {"path": f"{self.root}/tree"}}, f) + + os.chmod(corpus_registry.PROJECTS_FILE, 0o444) + + def test_a_new_corpus_goes_to_the_state_directory(self): + projects = corpus_registry.load_projects() + projects["mine"] = {"path": f"{self.root}/tree", "kind": "generic"} + corpus_registry.save_projects(projects) + + with open(f"{self.root}/state/projects.json") as f: + self.assertEqual(list(json.load(f)), ["mine"]) + + with open(corpus_registry.PROJECTS_FILE) as f: + self.assertEqual(list(json.load(f)), ["shipped"]) + + self.assertEqual(sorted(corpus_registry.load_projects()), ["mine", "shipped"]) + + def test_the_shipped_entry_wins_on_its_name(self): + os.makedirs(f"{self.root}/state") + + with open(f"{self.root}/state/projects.json", "w") as f: + json.dump({"shipped": {"path": "/elsewhere"}}, f) + + self.assertEqual(corpus_registry.load_projects()["shipped"]["path"], + f"{self.root}/tree") + + class TheIndexerIsDeclared(Registry): def test_generic_by_default(self): self.register(kind="generic") From 6ea2f79c75ed45e542ddc0746bf2f3000a65f51c Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Thu, 19 Nov 2026 13:33:17 +0100 Subject: [PATCH 2/2] Keep the read-only registry test on the install that has no other place The test asserts what happens when nothing can be written. With a state directory there is now somewhere to write, and the shared test state kept the corpus for the tests that followed. --- client/tests/test_indexing.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/tests/test_indexing.py b/client/tests/test_indexing.py index 1dd5d42..bebb902 100644 --- a/client/tests/test_indexing.py +++ b/client/tests/test_indexing.py @@ -472,6 +472,13 @@ def test_scan_takes_min_as_a_value_not_as_a_directory(self): def test_a_read_only_registry_does_not_kill_the_session(self): """save_projects raises SystemExit there — right for a CLI, fatal in a chat loop, which would lose the conversation to a typo.""" + + # An install that is its own state directory: nowhere else to write. + + self.addCleanup(setattr, self.corpus_registry, "user_projects_file", + self.corpus_registry.user_projects_file) + self.corpus_registry.user_projects_file = lambda: None + os.chmod(self.corpus_registry.PROJECTS_FILE, 0o444) os.chmod(self.root, 0o555) self.addCleanup(os.chmod, self.root, 0o755)