diff --git a/CHANGELOG b/CHANGELOG index 7413b16..c95361e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,10 @@ Unreleased + • The start inventory of the declared trees runs beside the session's + startup instead of before it: every registered corpus is walked, and + spear-chat took over a minute to show its prompt, --help included. A + tree inside another one is walked once. + • /cd moves the session's corpus -- index, history, memories, rules -- to a directory inside the launch tree, so /reindex right after rebuilds that tree. The tools stay in the launch directory. diff --git a/client/cli/session_workspace.py b/client/cli/session_workspace.py index 17a9b7c..ba54278 100644 --- a/client/cli/session_workspace.py +++ b/client/cli/session_workspace.py @@ -158,10 +158,11 @@ def set_project(spec): COMMAND_POLICY.bind_workspace(WORKSPACE) # What the trees hold now is what the session did not create: only the - # rest is its to clean up (provenance.py). + # rest is its to clean up (provenance.py). Taken beside the startup: the + # declared trees are every registered corpus. - provenance.capture(WORKSPACE.roots) - attach_control.pin_pristine() + provenance.capture(WORKSPACE.roots, background=True, + then=attach_control.pin_pristine) PROJECT_KIND = spec["kind"] diff --git a/client/harness/attach_control.py b/client/harness/attach_control.py index dab5e9f..ad8b146 100644 --- a/client/harness/attach_control.py +++ b/client/harness/attach_control.py @@ -398,6 +398,8 @@ def refresh(recipe: str, started: float) -> tuple[str, str, persistence_check.Ve def _verify(root: str, tree: str, pf: str) -> persistence_check.Verification: + provenance.ready() + def failed(reason: str) -> persistence_check.Verification: return persistence_check.Verification(False, reason) diff --git a/client/harness/provenance.py b/client/harness/provenance.py index 8b0fdcd..f9aca23 100644 --- a/client/harness/provenance.py +++ b/client/harness/provenance.py @@ -17,6 +17,7 @@ from __future__ import annotations import os +import threading from harness import target_policy @@ -34,6 +35,19 @@ # inventory, and the ones whose walk completed: only those hold created objects. _asked: tuple[str, ...] = () _complete: tuple[str, ...] = () +# The walk in progress. Every registered corpus is a declared tree, and their +# walk takes long enough to hold the prompt back, so a session runs it beside +# its startup. +_pending: "threading.Thread | None" = None + + +def ready() -> None: + """Wait for the start inventory, and what was run after it.""" + global _pending + + if _pending is not None and _pending is not threading.current_thread(): + _pending.join() + _pending = None def _key(path: str) -> int: @@ -83,21 +97,53 @@ def _walk(root: str): return paths, inodes, names -def capture(roots) -> None: - """Take the start inventory of `roots`, once per set of roots.""" - global _asked, _complete +def capture(roots, background: bool = False, then=None) -> None: + """Take the start inventory of `roots`, once per set of roots. + + In the background, readers of the inventory wait for it, and for + `then`, run after it on the same thread. An object made while the walk + runs may be taken for one of the start: refused to the session's + cleanup, never handed to it. + """ + global _asked, _pending roots = tuple(sorted(os.path.realpath(str(root)) for root in roots)) if roots == _asked: return + ready() + _asked = roots + def run(): + _inventory(roots) + + if then is not None: + then() + + if not background: + run() + + return + + _pending = threading.Thread(target=run, daemon=True, name="provenance-inventory") + _pending.start() + + +def _inventory(roots) -> None: + global _complete + _paths.clear() _inodes.clear() _names.clear() complete = [] for root in roots: + # Under a root already walked in full: its objects are in already. + + if any(root.startswith(done + "/") for done in complete): + complete.append(root) + continue + found = _walk(root) if found is not None: @@ -106,13 +152,15 @@ def capture(roots) -> None: _names.extend(found[2]) complete.append(root) - _asked, _complete = roots, tuple(complete) + _complete = tuple(complete) def forget() -> None: """Start over: no inventory, nothing counts as created.""" global _asked, _complete + ready() + _paths.clear() _inodes.clear() _names.clear() @@ -121,6 +169,8 @@ def forget() -> None: def start_paths() -> list[str]: """Every object of the inventoried trees at the start, by name.""" + ready() + return _names @@ -130,6 +180,8 @@ def known(path: str) -> bool: def _inside(path: str) -> str: + ready() + return next((root for root in _complete if path == root or path.startswith(root + "/")), "") diff --git a/client/tests/test_session_cleanup.py b/client/tests/test_session_cleanup.py index d1faeb6..fa309df 100644 --- a/client/tests/test_session_cleanup.py +++ b/client/tests/test_session_cleanup.py @@ -46,6 +46,25 @@ def test_a_new_object_is_created_and_a_start_one_is_not(self): self.assertFalse(provenance.created(f"{self.root}/a.txt")) self.assertFalse(provenance.created(f"{self.root}/build")) + def test_a_background_walk_is_waited_for_and_runs_what_follows(self): + ran = [] + provenance.forget() + provenance.capture([self.root], background=True, then=lambda: ran.append(1)) + provenance.ready() + Path(self.root, "new.tmp").write_text("n\n") + + self.assertEqual(ran, [1]) + self.assertTrue(provenance.created(f"{self.root}/new.tmp")) + self.assertFalse(provenance.created(f"{self.root}/a.txt")) + + def test_a_tree_inside_another_is_inventoried_once(self): + provenance.forget() + provenance.capture([self.root, f"{self.root}/build"]) + + self.assertTrue(provenance.known(f"{self.root}/build/x/old.c")) + self.assertEqual(len(provenance.start_paths()), + len(set(provenance.start_paths()))) + def test_recreating_a_start_path_does_not_make_it_the_sessions(self): os.remove(f"{self.root}/a.txt") Path(self.root, "a.txt").write_text("again\n")