Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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 <dir> 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.
Expand Down
7 changes: 4 additions & 3 deletions client/cli/session_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
2 changes: 2 additions & 0 deletions client/harness/attach_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
60 changes: 56 additions & 4 deletions client/harness/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import os
import threading

from harness import target_policy

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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


Expand All @@ -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 + "/")), "")

Expand Down
19 changes: 19 additions & 0 deletions client/tests/test_session_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading