diff --git a/app/app.py b/app/app.py index 66d72239..dd8bf3e2 100644 --- a/app/app.py +++ b/app/app.py @@ -33,7 +33,7 @@ from web.core import app from web import shared from web import filters, runs, traces, compare, builder, catalogue, agents, settings, workspaces, execute, judge, feasibility, reconcile, judge_tasks, onboarding -from nullius import execution # noqa: E402 - recorded runs, picked up again at start +from nullius import execution, workspace # noqa: E402 - recorded runs, picked up again at start; the workspace left open def _port(value: str) -> int: @@ -80,7 +80,12 @@ def main(argv=None) -> None: for r in ws.roots: print(f" {r}") elif not unmatched: - print("No job path given — the web page will ask for one, or offer a workspace.") + ws = workspace.last_open(shared.WS_HOME) + if ws: + shared.open_workspace(ws) + print(f"Workspace {ws.name!r} ({ws.dir}) reopened") + else: + print("No job path given — the web page will ask for one, or offer a workspace.") # Runs started from the builder before this start: the ones still going # are followed again (they run detached on their target), the rest listed. back = execution.restore() diff --git a/app/nullius/workspace.py b/app/nullius/workspace.py index 190af5be..5f982f57 100644 --- a/app/nullius/workspace.py +++ b/app/nullius/workspace.py @@ -5,7 +5,7 @@ and so the expensive part — parsing a 375-trial job, converting a foreign run tree — happens once per change rather than once per page. - //workspace.json name, label, roots, created, opened + //workspace.json name, label, roots, created, opened, closed //imported/ foreign run trees converted to job dirs //jobs/.json a parsed Job, keyed by the job dir's signature @@ -83,6 +83,7 @@ class Workspace: opened: str dir: str job_roots: list = field(default_factory=list) # after imports: what parse walks + closed: bool = False # closed by the user: a restart leaves it closed @property def imports_dir(self) -> str: @@ -156,6 +157,12 @@ def list_all(home_dir: str) -> list: return sorted(found, key=lambda w: w.opened, reverse=True) +def last_open(home_dir: str) -> Workspace | None: + """The workspace opened last, unless the user closed it.""" + last = next(iter(list_all(home_dir)), None) + return None if last is None or last.closed else last + + def find_by_roots(home_dir: str, roots, besides: str = "") -> Workspace | None: """The workspace already pointing at exactly these roots, if any; the most recently opened. ``besides``: a folder whose roots a workspace may @@ -213,6 +220,7 @@ def rename(home_dir: str, ws: Workspace, label: str) -> Workspace: def touch(ws: Workspace) -> None: ws.opened = _now() + ws.closed = False save(ws) diff --git a/app/tests/field_contract.json b/app/tests/field_contract.json index a1abfd36..4fdc1778 100644 --- a/app/tests/field_contract.json +++ b/app/tests/field_contract.json @@ -511,6 +511,7 @@ }, "workspace": { "Workspace": [ + "closed", "created", "dir", "imports_dir", diff --git a/app/tests/test_workspace.py b/app/tests/test_workspace.py index eedbb3f3..e0b7dab4 100644 --- a/app/tests/test_workspace.py +++ b/app/tests/test_workspace.py @@ -170,6 +170,19 @@ def test_touch_orders_the_list(self): workspace.touch(a) self.assertEqual([w.name for w in workspace.list_all(self.home)][0], "a") + def test_last_open_is_the_latest_unless_the_user_closed_it(self): + self.assertIsNone(workspace.last_open(self.home)) + a = workspace.create(self.home, ["/r/a"], label="a") + a.opened = "2000-01-01T00:00:00+00:00" + workspace.save(a) + b = workspace.create(self.home, ["/r/b"], label="b") + self.assertEqual(workspace.last_open(self.home).name, "b") + b.closed = True + workspace.save(b) + self.assertIsNone(workspace.last_open(self.home)) # not the one opened before it either + workspace.touch(b) + self.assertEqual(workspace.last_open(self.home).name, "b") + class Cache(unittest.TestCase): def setUp(self): diff --git a/app/ui_tests/test_run_routes.py b/app/ui_tests/test_run_routes.py index ac07f336..cfcd817f 100644 --- a/app/ui_tests/test_run_routes.py +++ b/app/ui_tests/test_run_routes.py @@ -347,6 +347,44 @@ def test_the_page_carries_the_token_the_stop_button_sends(self): self.assertEqual(self.client.post(f"/run/{r.json['id']}/stop", headers={"Origin": "http://localhost", "X-CSRF-Token": ""}).status_code, 403) +class ReopenAtStart(unittest.TestCase): + """A start with no job path given reopens the workspace left open.""" + def setUp(self): + self.stack = ExitStack(); self.addCleanup(self.stack.close) + self.tmp = Path(tempfile.mkdtemp(prefix="nullius-reopen-")); self.addCleanup(shutil.rmtree, self.tmp, True) + self.home = str(self.tmp / "ws") + self.stack.enter_context(patch.dict(os.environ, {"RUNS_DIR": str(self.tmp / "runs"), "JOB_ROOTS": ""})) + self.stack.enter_context(patch.object(web.shared, "WS_HOME", self.home)) + self.stack.enter_context(patch.dict(web.shared.STATE, {"workspace": None, "roots": [], "label": "", "job_roots": []})) + self.stack.enter_context(patch.object(web.app, "run", lambda **kw: None)) + self.root = self.tmp / "a" + self.root.mkdir() + + def restart(self, argv=()): + web.shared.STATE.update({"workspace": None, "roots": [], "label": "", "job_roots": []}) + web.main(list(argv)) + return web.shared.STATE["workspace"] + + def test_no_workspace_shows_the_first_steps(self): + self.assertIsNone(self.restart()) + self.assertEqual(workspace.list_all(self.home), []) + + def test_the_workspace_left_open_opens_again(self): + ws = workspace.create(self.home, [str(self.root)], label="witty pelican") + web.shared.open_workspace(ws) + self.assertEqual(self.restart().name, ws.name) + + def test_a_workspace_whose_roots_are_gone_opens_as_the_open_action_does(self): + ws = workspace.create(self.home, [str(self.root)], label="witty pelican") + web.shared.open_workspace(ws) + self.root.rmdir() + self.assertEqual(self.restart().roots, [str(self.root)]) + + def test_job_paths_given_win(self): + workspace.create(self.home, ["/elsewhere"], label="witty pelican") + self.assertEqual(self.restart([str(self.root)]).roots, [str(self.root)]) + + class ResumeIntoWorkspace(unittest.TestCase): """A restart while a run started from the builder goes on: the run's job goes back into the workspace it started in.""" def setUp(self): @@ -383,28 +421,44 @@ def setUp(self): ex = execution.EXECUTIONS[r.json["id"]] ex._save = lambda: None # the app stops: the old thread writes no record again execution.EXECUTIONS.clear() - web.shared.close_workspace() def restart(self): + web.shared.STATE.update({"workspace": None, "roots": [], "label": "", "job_roots": []}) # the app stops web.main([]) back = list(execution.EXECUTIONS.values()) self.addCleanup(lambda: [e.stop() for e in back]) return web.shared.STATE["workspace"] - def test_a_restart_puts_the_run_back_into_its_workspace(self): - again = self.restart() - self.assertEqual(again.name, self.ws.name) - self.assertEqual([w.name for w in workspace.list_all(self.home)], [self.ws.name]) # no new workspace - self.assertIn(self.jobs, again.roots) + def test_a_restart_reopens_the_open_workspace_and_puts_the_run_back_into_its_own(self): + self.ws.opened = "2000-01-01T00:00:00+00:00" + workspace.save(self.ws) + other = workspace.create(self.home, [str(self.tmp / "a")], label="quiet otter") + web.shared.open_workspace(other) + self.assertEqual(self.restart().name, other.name) # the run does not open its workspace + self.assertEqual(len(workspace.list_all(self.home)), 2) # no new workspace + self.assertIn(self.jobs, workspace.load(self.home, self.ws.name).roots) + self.assertNotIn(self.jobs, workspace.load(self.home, other.name).roots) self.assertEqual(json.loads(self.record.read_text())["workspace"], self.ws.name) + def test_a_restart_leaves_a_closed_workspace_closed(self): + r = self.client.post("/change-source", data={"csrf": self.csrf}); self.addCleanup(r.close) + self.assertEqual(r.status_code, 302) + self.assertIsNone(self.restart()) + self.assertIn(self.jobs, workspace.load(self.home, self.ws.name).roots) + + def test_an_old_record_goes_into_the_reopened_workspace(self): + rec = json.loads(self.record.read_text()); rec.pop("workspace", None); self.record.write_text(json.dumps(rec)) + self.assertEqual(self.restart().name, self.ws.name) + self.assertEqual(len(workspace.list_all(self.home)), 1) + def test_a_run_whose_workspace_is_gone_gets_one_as_before(self): workspace.delete(self.home, self.ws.name) again = self.restart() self.assertEqual(again.roots, [self.jobs]) - def test_an_old_record_without_its_workspace_gets_one_as_before(self): + def test_an_old_record_with_no_workspace_open_gets_one_as_before(self): rec = json.loads(self.record.read_text()); rec.pop("workspace", None); self.record.write_text(json.dumps(rec)) + web.shared.close_workspace() again = self.restart() self.assertNotEqual(again.name, self.ws.name) self.assertEqual(again.roots, [self.jobs]) diff --git a/app/web/shared.py b/app/web/shared.py index 3d72ffd2..b6fe2332 100644 --- a/app/web/shared.py +++ b/app/web/shared.py @@ -314,6 +314,10 @@ def open_workspace(ws, refresh=False): def close_workspace(): + ws = STATE["workspace"] + if ws is not None: + ws.closed = True + workspace.save(ws) STATE.update({"workspace": None, "roots": [], "label": "", "job_roots": []}) @@ -322,8 +326,8 @@ def adopt_roots(roots, label="", new=False, into=""): Without a label, roots that already have a workspace reopen it — so a restart with the same argv lands in the same place, cache and all. - ``into``: the workspace to add them to while it exists, opened only - when none is open. + ``into``: the workspace to add them to while it exists, left closed + unless it is open already. """ ws = STATE["workspace"] if into and not (ws and ws.name == into): @@ -331,7 +335,7 @@ def adopt_roots(roots, label="", new=False, into=""): if ws and not new: ws.roots = list(ws.roots) + [r for r in roots if r not in ws.roots] workspace.save(ws) - if STATE["workspace"] in (None, ws): + if STATE["workspace"] is ws: open_workspace(ws) return ws ws = None if label else workspace.find_by_roots(WS_HOME, roots, besides=execution.runs_root()) diff --git a/docs/builder.md b/docs/builder.md index 4dba8b3b..6108515d 100644 --- a/docs/builder.md +++ b/docs/builder.md @@ -107,7 +107,7 @@ a declared model list keeps those published choices. | Docker or Podman | The engine must answer `ps` (`docker ps`, or `podman ps` for the standard's container backend), or the run stops before it starts | | Stop | Ends the current part (exit 130), records `stopped`; the next part never starts | | Delete launch | On the launch in Runs, *By launch*, once every run of it has ended; asks first. The job page links to it. Deletes each run's `//` (a shard per machine included), every job of the launch, their judgements in every workspace, and each run's copy on its host when ticked. A run still going is refused: stop it first. A launch with jobs from elsewhere has no Delete | -| Restart of Nullius | `execution.restore` reattaches from `runs//run.json`; the job goes back into the workspace it started in, while that workspace exists | +| Restart of Nullius | `execution.restore` reattaches from `runs//run.json`; the job goes back into the workspace it started in, while that workspace exists, without opening it. The workspace open before the restart opens again | | Cannot run from the page | Generated tasks and custom agents are not supported by Run | | Refused | Reads *refused* on the runs page, with why; the job was never written | | Files | `//`: script, log, `run.json`, `jobs/`; default `runs/` under the user data directory (`nullius paths`) | diff --git a/docs/concepts.md b/docs/concepts.md index f1bb1560..4e232066 100644 --- a/docs/concepts.md +++ b/docs/concepts.md @@ -67,7 +67,7 @@ Importing results needs no runner. ## Workspace -The roots you opened, the imports converted and the judgements saved; the same roots reopen the same workspace. +The roots you opened, the imports converted and the judgements saved; the same roots reopen the same workspace. A start with no root given reopens the workspace left open; a closed one stays closed.