viva-workspace 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- viva_workspace-0.1.0/LICENSE +21 -0
- viva_workspace-0.1.0/PKG-INFO +91 -0
- viva_workspace-0.1.0/README.md +64 -0
- viva_workspace-0.1.0/pyproject.toml +43 -0
- viva_workspace-0.1.0/setup.cfg +4 -0
- viva_workspace-0.1.0/tests/test_naming.py +29 -0
- viva_workspace-0.1.0/tests/test_outcomes.py +71 -0
- viva_workspace-0.1.0/tests/test_paths.py +143 -0
- viva_workspace-0.1.0/viva_workspace/__init__.py +43 -0
- viva_workspace-0.1.0/viva_workspace/naming.py +53 -0
- viva_workspace-0.1.0/viva_workspace/outcomes.py +57 -0
- viva_workspace-0.1.0/viva_workspace/paths.py +307 -0
- viva_workspace-0.1.0/viva_workspace.egg-info/PKG-INFO +91 -0
- viva_workspace-0.1.0/viva_workspace.egg-info/SOURCES.txt +15 -0
- viva_workspace-0.1.0/viva_workspace.egg-info/dependency_links.txt +1 -0
- viva_workspace-0.1.0/viva_workspace.egg-info/requires.txt +5 -0
- viva_workspace-0.1.0/viva_workspace.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eran Agmon / The Vivarium Collective
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: viva-workspace
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Shared source of truth for viva workspace-layout logic (layout map, root-walker, study resolver, canonical outcomes, package naming).
|
|
5
|
+
Author-email: Eran Agmon <agmon.eran@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/vivarium-collective/viva-workspace
|
|
8
|
+
Project-URL: Repository, https://github.com/vivarium-collective/viva-workspace
|
|
9
|
+
Project-URL: Issues, https://github.com/vivarium-collective/viva-workspace/issues
|
|
10
|
+
Keywords: vivarium,workspace,process-bigraph,study,layout
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pyyaml>=6
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# viva-workspace
|
|
29
|
+
|
|
30
|
+
The single **source of truth for workspace-layout logic** shared by
|
|
31
|
+
`vivarium-workbench`, `viva-superpowers`, and `v2ecoli`.
|
|
32
|
+
|
|
33
|
+
Before this package those three repos each reimplemented the same concepts —
|
|
34
|
+
~12 study-directory resolvers, ~6 root-walkers, and a hand-synced *vendored* copy
|
|
35
|
+
of `workspace_paths.py` — with three subtly conflicting semantics. This package
|
|
36
|
+
consolidates them into one dependency-light module so the layout is defined once.
|
|
37
|
+
|
|
38
|
+
## Why dependency-light
|
|
39
|
+
|
|
40
|
+
`vivarium-workbench` is deliberately **AI-free** (all AI lives in
|
|
41
|
+
`viva-superpowers`). To be safe for the workbench to depend on, this package uses
|
|
42
|
+
**stdlib + `pyyaml` only** — no `process_bigraph` / `bigraph_schema`, no web
|
|
43
|
+
framework, no AI deps.
|
|
44
|
+
|
|
45
|
+
## Public API
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from viva_workspace import (
|
|
49
|
+
WorkspacePaths, # load workspace.yaml (mtime-memoized) + derived dirs
|
|
50
|
+
find_workspace_root, # the one root-walker (walk up for workspace.yaml)
|
|
51
|
+
study_dir, # ONE layout-map-aware study resolver (nested + flat)
|
|
52
|
+
canonical_outcomes, # the LOCKED canonical-run outcome selection
|
|
53
|
+
package_slug, # canonical viva_<slug> (with pbg_<slug> compat shim)
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Layout map
|
|
58
|
+
|
|
59
|
+
`WorkspacePaths` reads an optional `layout:` map from `workspace.yaml`. A missing
|
|
60
|
+
key falls back to the flat default (`studies` -> `studies/`); a workspace with no
|
|
61
|
+
`layout:` block keeps the classic flat layout. To nest research dirs:
|
|
62
|
+
|
|
63
|
+
```yaml
|
|
64
|
+
layout:
|
|
65
|
+
studies: workspace/studies
|
|
66
|
+
investigations: workspace/investigations
|
|
67
|
+
reports: workspace/reports
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`study_dir(root_or_paths, slug)` honors that map for both nested
|
|
71
|
+
(`workspace/studies/<slug>`) and flat (`studies/<slug>`) layouts with no hardcoded
|
|
72
|
+
literal, searching flat-first then nested, and returning the canonical write
|
|
73
|
+
location for a not-yet-created study (pass `must_exist=True` to require existence).
|
|
74
|
+
|
|
75
|
+
### Canonical outcomes
|
|
76
|
+
|
|
77
|
+
`canonical_outcomes(study_spec)` returns exactly ONE run's `outcomes`: the run
|
|
78
|
+
flagged `canonical: true` if any (last wins), else the newest **completed** run by
|
|
79
|
+
timestamp, else the last run. It is *not* a last-run-wins merge — an older
|
|
80
|
+
`canonical: true` run correctly overrides a newer completed one.
|
|
81
|
+
|
|
82
|
+
### Package naming
|
|
83
|
+
|
|
84
|
+
`package_slug(name)` returns the canonical `viva_<slug>`. During the pbg -> viva
|
|
85
|
+
migration window, `resolve_package_dir(root, name)` returns an existing
|
|
86
|
+
`pbg_<slug>/` directory if the canonical `viva_<slug>/` is absent.
|
|
87
|
+
|
|
88
|
+
## Status
|
|
89
|
+
|
|
90
|
+
Local-only. Not yet published; consumers still ship their own copies until this is
|
|
91
|
+
released and they migrate.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# viva-workspace
|
|
2
|
+
|
|
3
|
+
The single **source of truth for workspace-layout logic** shared by
|
|
4
|
+
`vivarium-workbench`, `viva-superpowers`, and `v2ecoli`.
|
|
5
|
+
|
|
6
|
+
Before this package those three repos each reimplemented the same concepts —
|
|
7
|
+
~12 study-directory resolvers, ~6 root-walkers, and a hand-synced *vendored* copy
|
|
8
|
+
of `workspace_paths.py` — with three subtly conflicting semantics. This package
|
|
9
|
+
consolidates them into one dependency-light module so the layout is defined once.
|
|
10
|
+
|
|
11
|
+
## Why dependency-light
|
|
12
|
+
|
|
13
|
+
`vivarium-workbench` is deliberately **AI-free** (all AI lives in
|
|
14
|
+
`viva-superpowers`). To be safe for the workbench to depend on, this package uses
|
|
15
|
+
**stdlib + `pyyaml` only** — no `process_bigraph` / `bigraph_schema`, no web
|
|
16
|
+
framework, no AI deps.
|
|
17
|
+
|
|
18
|
+
## Public API
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from viva_workspace import (
|
|
22
|
+
WorkspacePaths, # load workspace.yaml (mtime-memoized) + derived dirs
|
|
23
|
+
find_workspace_root, # the one root-walker (walk up for workspace.yaml)
|
|
24
|
+
study_dir, # ONE layout-map-aware study resolver (nested + flat)
|
|
25
|
+
canonical_outcomes, # the LOCKED canonical-run outcome selection
|
|
26
|
+
package_slug, # canonical viva_<slug> (with pbg_<slug> compat shim)
|
|
27
|
+
)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Layout map
|
|
31
|
+
|
|
32
|
+
`WorkspacePaths` reads an optional `layout:` map from `workspace.yaml`. A missing
|
|
33
|
+
key falls back to the flat default (`studies` -> `studies/`); a workspace with no
|
|
34
|
+
`layout:` block keeps the classic flat layout. To nest research dirs:
|
|
35
|
+
|
|
36
|
+
```yaml
|
|
37
|
+
layout:
|
|
38
|
+
studies: workspace/studies
|
|
39
|
+
investigations: workspace/investigations
|
|
40
|
+
reports: workspace/reports
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`study_dir(root_or_paths, slug)` honors that map for both nested
|
|
44
|
+
(`workspace/studies/<slug>`) and flat (`studies/<slug>`) layouts with no hardcoded
|
|
45
|
+
literal, searching flat-first then nested, and returning the canonical write
|
|
46
|
+
location for a not-yet-created study (pass `must_exist=True` to require existence).
|
|
47
|
+
|
|
48
|
+
### Canonical outcomes
|
|
49
|
+
|
|
50
|
+
`canonical_outcomes(study_spec)` returns exactly ONE run's `outcomes`: the run
|
|
51
|
+
flagged `canonical: true` if any (last wins), else the newest **completed** run by
|
|
52
|
+
timestamp, else the last run. It is *not* a last-run-wins merge — an older
|
|
53
|
+
`canonical: true` run correctly overrides a newer completed one.
|
|
54
|
+
|
|
55
|
+
### Package naming
|
|
56
|
+
|
|
57
|
+
`package_slug(name)` returns the canonical `viva_<slug>`. During the pbg -> viva
|
|
58
|
+
migration window, `resolve_package_dir(root, name)` returns an existing
|
|
59
|
+
`pbg_<slug>/` directory if the canonical `viva_<slug>/` is absent.
|
|
60
|
+
|
|
61
|
+
## Status
|
|
62
|
+
|
|
63
|
+
Local-only. Not yet published; consumers still ship their own copies until this is
|
|
64
|
+
released and they migrate.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "viva-workspace"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Shared source of truth for viva workspace-layout logic (layout map, root-walker, study resolver, canonical outcomes, package naming)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Eran Agmon", email = "agmon.eran@gmail.com" }]
|
|
13
|
+
keywords = ["vivarium", "workspace", "process-bigraph", "study", "layout"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Scientific/Engineering",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"pyyaml>=6",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/vivarium-collective/viva-workspace"
|
|
30
|
+
Repository = "https://github.com/vivarium-collective/viva-workspace"
|
|
31
|
+
Issues = "https://github.com/vivarium-collective/viva-workspace/issues"
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = [
|
|
35
|
+
"pytest>=7",
|
|
36
|
+
"ruff>=0.5",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
include = ["viva_workspace*"]
|
|
41
|
+
|
|
42
|
+
[tool.ruff.lint]
|
|
43
|
+
select = ["F", "E722"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Tests for package naming and the viva_/pbg_ compat shim."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from viva_workspace import legacy_package_slug, package_slug, resolve_package_dir
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_canonical_viva_prefix():
|
|
8
|
+
assert package_slug("my-workspace") == "viva_my_workspace"
|
|
9
|
+
assert package_slug(None) == "viva_workspace"
|
|
10
|
+
assert package_slug("") == "viva_workspace"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_legacy_slug():
|
|
14
|
+
assert legacy_package_slug("my-workspace") == "pbg_my_workspace"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_resolve_prefers_canonical(tmp_path):
|
|
18
|
+
(tmp_path / "viva_demo").mkdir()
|
|
19
|
+
(tmp_path / "pbg_demo").mkdir()
|
|
20
|
+
assert resolve_package_dir(tmp_path, "demo") == tmp_path / "viva_demo"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_resolve_falls_back_to_legacy_pbg(tmp_path):
|
|
24
|
+
(tmp_path / "pbg_demo").mkdir()
|
|
25
|
+
assert resolve_package_dir(tmp_path, "demo") == tmp_path / "pbg_demo"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_resolve_defaults_to_canonical_when_neither_exists(tmp_path):
|
|
29
|
+
assert resolve_package_dir(tmp_path, "demo") == tmp_path / "viva_demo"
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Tests for canonical-run outcome selection."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from viva_workspace import canonical_outcomes, canonical_run
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_canonical_flag_beats_newer_completed():
|
|
8
|
+
spec = {
|
|
9
|
+
"runs": [
|
|
10
|
+
{
|
|
11
|
+
"name": "old-canonical",
|
|
12
|
+
"status": "completed",
|
|
13
|
+
"timestamp": "2024-01-01",
|
|
14
|
+
"canonical": True,
|
|
15
|
+
"outcomes": {"PASS": True},
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"name": "newer-completed",
|
|
19
|
+
"status": "completed",
|
|
20
|
+
"timestamp": "2025-01-01",
|
|
21
|
+
"outcomes": {"PASS": False},
|
|
22
|
+
},
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
assert canonical_run(spec)["name"] == "old-canonical"
|
|
26
|
+
assert canonical_outcomes(spec) == {"PASS": True}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_newest_completed_when_no_flag():
|
|
30
|
+
spec = {
|
|
31
|
+
"runs": [
|
|
32
|
+
{"name": "a", "status": "completed", "timestamp": "2024-01-01",
|
|
33
|
+
"outcomes": {"v": 1}},
|
|
34
|
+
{"name": "b", "status": "completed", "timestamp": "2025-06-01",
|
|
35
|
+
"outcomes": {"v": 2}},
|
|
36
|
+
{"name": "c", "status": "running", "timestamp": "2025-12-01",
|
|
37
|
+
"outcomes": {"v": 3}},
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
assert canonical_run(spec)["name"] == "b"
|
|
41
|
+
assert canonical_outcomes(spec) == {"v": 2}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_last_canonical_flag_wins():
|
|
45
|
+
spec = {
|
|
46
|
+
"runs": [
|
|
47
|
+
{"name": "c1", "canonical": True, "outcomes": {"n": 1}},
|
|
48
|
+
{"name": "c2", "canonical": True, "outcomes": {"n": 2}},
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
assert canonical_run(spec)["name"] == "c2"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_falls_back_to_last_run_when_none_completed():
|
|
55
|
+
spec = {"runs": [
|
|
56
|
+
{"name": "a", "status": "running", "outcomes": {"x": 1}},
|
|
57
|
+
{"name": "b", "status": "queued", "outcomes": {"x": 2}},
|
|
58
|
+
]}
|
|
59
|
+
assert canonical_run(spec)["name"] == "b"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_empty():
|
|
63
|
+
assert canonical_run({"runs": []}) is None
|
|
64
|
+
assert canonical_outcomes({}) == {}
|
|
65
|
+
assert canonical_outcomes(None) == {}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_accepts_bare_runs_list():
|
|
69
|
+
runs = [{"name": "a", "status": "done", "timestamp": "2025-01-01",
|
|
70
|
+
"outcomes": {"ok": True}}]
|
|
71
|
+
assert canonical_outcomes(runs) == {"ok": True}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Tests for the layout map, root-walker, study_dir, and mtime memo."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from viva_workspace import WorkspacePaths, find_workspace_root, study_dir
|
|
9
|
+
from viva_workspace import paths as paths_mod
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture(autouse=True)
|
|
13
|
+
def _clear_cache():
|
|
14
|
+
paths_mod._clear_load_cache()
|
|
15
|
+
yield
|
|
16
|
+
paths_mod._clear_load_cache()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _write(p, text=""):
|
|
20
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
21
|
+
p.write_text(text, encoding="utf-8")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# --- study_dir: flat vs nested via the layout map --------------------------
|
|
25
|
+
|
|
26
|
+
def test_study_dir_flat_layout(tmp_path):
|
|
27
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
28
|
+
_write(tmp_path / "studies" / "s1" / "study.yaml", "name: s1\n")
|
|
29
|
+
d = study_dir(tmp_path, "s1")
|
|
30
|
+
assert d == (tmp_path / "studies" / "s1").resolve()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_study_dir_nested_layout_via_map(tmp_path):
|
|
34
|
+
_write(tmp_path / "workspace.yaml", "layout:\n studies: workspace/studies\n")
|
|
35
|
+
_write(tmp_path / "workspace" / "studies" / "s1" / "study.yaml", "name: s1\n")
|
|
36
|
+
d = study_dir(tmp_path, "s1")
|
|
37
|
+
assert d == (tmp_path / "workspace" / "studies" / "s1").resolve()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_study_dir_nested_under_investigation(tmp_path):
|
|
41
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
42
|
+
_write(
|
|
43
|
+
tmp_path / "investigations" / "inv1" / "studies" / "s2" / "study.yaml",
|
|
44
|
+
"name: s2\n",
|
|
45
|
+
)
|
|
46
|
+
d = study_dir(tmp_path, "s2")
|
|
47
|
+
assert d == (tmp_path / "investigations" / "inv1" / "studies" / "s2").resolve()
|
|
48
|
+
# owner derived from nested layout
|
|
49
|
+
wp = WorkspacePaths.load(tmp_path)
|
|
50
|
+
assert wp.study_owner("s2") == "inv1"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_study_dir_missing_returns_canonical_write_location(tmp_path):
|
|
54
|
+
_write(tmp_path / "workspace.yaml", "layout:\n studies: workspace/studies\n")
|
|
55
|
+
d = study_dir(tmp_path, "newstudy")
|
|
56
|
+
assert d == (tmp_path / "workspace" / "studies" / "newstudy").resolve()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_study_dir_must_exist_raises(tmp_path):
|
|
60
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
61
|
+
with pytest.raises(FileNotFoundError):
|
|
62
|
+
study_dir(tmp_path, "nope", must_exist=True)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_study_dir_accepts_workspacepaths(tmp_path):
|
|
66
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
67
|
+
_write(tmp_path / "studies" / "s1" / "study.yaml", "name: s1\n")
|
|
68
|
+
wp = WorkspacePaths.load(tmp_path)
|
|
69
|
+
assert study_dir(wp, "s1") == (tmp_path / "studies" / "s1").resolve()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_flat_wins_over_nested_on_collision(tmp_path):
|
|
73
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
74
|
+
_write(tmp_path / "studies" / "dup" / "study.yaml", "name: dup\n")
|
|
75
|
+
_write(
|
|
76
|
+
tmp_path / "investigations" / "inv1" / "studies" / "dup" / "study.yaml",
|
|
77
|
+
"name: dup\n",
|
|
78
|
+
)
|
|
79
|
+
assert study_dir(tmp_path, "dup") == (tmp_path / "studies" / "dup").resolve()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# --- find_workspace_root ---------------------------------------------------
|
|
83
|
+
|
|
84
|
+
def test_root_walk_from_nested_dir(tmp_path):
|
|
85
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
86
|
+
deep = tmp_path / "investigations" / "inv1" / "studies" / "s1"
|
|
87
|
+
deep.mkdir(parents=True)
|
|
88
|
+
assert find_workspace_root(deep) == tmp_path.resolve()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_root_walk_not_found(tmp_path):
|
|
92
|
+
with pytest.raises(FileNotFoundError):
|
|
93
|
+
find_workspace_root(tmp_path)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_root_walk_default_cwd(tmp_path, monkeypatch):
|
|
97
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
98
|
+
sub = tmp_path / "sub"
|
|
99
|
+
sub.mkdir()
|
|
100
|
+
monkeypatch.chdir(sub)
|
|
101
|
+
assert find_workspace_root() == tmp_path.resolve()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# --- layout defaults / drift reconciliation --------------------------------
|
|
105
|
+
|
|
106
|
+
def test_layout_defaults_include_reconciled_keys():
|
|
107
|
+
wp = WorkspacePaths.from_config("/tmp/x", {})
|
|
108
|
+
# union of workbench + superpowers vendored keys
|
|
109
|
+
for key in ("studies", "reports", "pbg", "notes", "experiments"):
|
|
110
|
+
assert wp.dir(key) is not None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_package_dir_from_name_and_package_path(tmp_path):
|
|
114
|
+
wp = WorkspacePaths.from_config(tmp_path, {"name": "my-thing"})
|
|
115
|
+
assert wp.package.name == "viva_my_thing"
|
|
116
|
+
wp2 = WorkspacePaths.from_config(tmp_path, {"package_path": "custom_pkg"})
|
|
117
|
+
assert wp2.package.name == "custom_pkg"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# --- mtime memoization -----------------------------------------------------
|
|
121
|
+
|
|
122
|
+
def test_mtime_memo_no_reparse_when_unchanged(tmp_path):
|
|
123
|
+
_write(tmp_path / "workspace.yaml", "name: demo\n")
|
|
124
|
+
start = paths_mod._parse_count
|
|
125
|
+
WorkspacePaths.load(tmp_path)
|
|
126
|
+
after_first = paths_mod._parse_count
|
|
127
|
+
assert after_first == start + 1
|
|
128
|
+
for _ in range(5):
|
|
129
|
+
WorkspacePaths.load(tmp_path)
|
|
130
|
+
assert paths_mod._parse_count == after_first # no re-parse
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_mtime_memo_reparse_on_change(tmp_path):
|
|
134
|
+
wf = tmp_path / "workspace.yaml"
|
|
135
|
+
_write(wf, "name: demo\n")
|
|
136
|
+
WorkspacePaths.load(tmp_path)
|
|
137
|
+
base = paths_mod._parse_count
|
|
138
|
+
# bump mtime to a distinct value and change content
|
|
139
|
+
_write(wf, "layout:\n studies: workspace/studies\n")
|
|
140
|
+
os.utime(wf, ns=(base + 10**9, base + 10**9))
|
|
141
|
+
wp = WorkspacePaths.load(tmp_path)
|
|
142
|
+
assert paths_mod._parse_count == base + 1 # re-parsed
|
|
143
|
+
assert wp.rel("studies") == "workspace/studies"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""viva-workspace — the shared source of truth for workspace-layout logic.
|
|
2
|
+
|
|
3
|
+
One dependency-light (stdlib + pyyaml) package consolidating the workspace-layout
|
|
4
|
+
helpers that vivarium-workbench, viva-superpowers and v2ecoli each reimplemented:
|
|
5
|
+
the ``layout:`` map + derived directories (:class:`WorkspacePaths`), the
|
|
6
|
+
root-walker (:func:`find_workspace_root`), the single layout-aware study
|
|
7
|
+
resolver (:func:`study_dir`), canonical-run outcome selection
|
|
8
|
+
(:func:`canonical_outcomes`) and package naming (:func:`package_slug`).
|
|
9
|
+
|
|
10
|
+
Deliberately free of AI / process_bigraph / web-framework deps so the AI-free
|
|
11
|
+
workbench can depend on it safely.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from .naming import (
|
|
16
|
+
legacy_package_slug,
|
|
17
|
+
package_slug,
|
|
18
|
+
resolve_package_dir,
|
|
19
|
+
)
|
|
20
|
+
from .outcomes import canonical_outcomes, canonical_run
|
|
21
|
+
from .paths import (
|
|
22
|
+
LAYOUT_DEFAULTS,
|
|
23
|
+
LAYOUT_KEYS,
|
|
24
|
+
WorkspacePaths,
|
|
25
|
+
find_workspace_root,
|
|
26
|
+
study_dir,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
__version__ = "0.1.0"
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"__version__",
|
|
33
|
+
"WorkspacePaths",
|
|
34
|
+
"LAYOUT_DEFAULTS",
|
|
35
|
+
"LAYOUT_KEYS",
|
|
36
|
+
"find_workspace_root",
|
|
37
|
+
"study_dir",
|
|
38
|
+
"canonical_outcomes",
|
|
39
|
+
"canonical_run",
|
|
40
|
+
"package_slug",
|
|
41
|
+
"legacy_package_slug",
|
|
42
|
+
"resolve_package_dir",
|
|
43
|
+
]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Workspace Python-package naming — the LOCKED ``viva_<slug>`` decision plus a
|
|
2
|
+
``pbg_<slug>`` legacy compat shim.
|
|
3
|
+
|
|
4
|
+
Consolidates the ``package_slug`` helper that both
|
|
5
|
+
``vivarium_workbench/lib/workspace_paths.py`` and
|
|
6
|
+
``viva_superpowers/workspace_paths.py`` defined identically as
|
|
7
|
+
``f"pbg_{slug}"``. As part of the pbg -> viva rebrand the canonical prefix is
|
|
8
|
+
now ``viva_``; :func:`package_slug` returns that. Existing workspaces still ship
|
|
9
|
+
a ``pbg_<slug>/`` package directory, so :func:`resolve_package_dir` recognizes an
|
|
10
|
+
on-disk ``pbg_<slug>`` during the migration window instead of silently pointing
|
|
11
|
+
at a non-existent ``viva_<slug>``.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Optional, Union
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _slugify(name: Optional[str]) -> str:
|
|
20
|
+
return (name or "workspace").replace("-", "_")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def package_slug(name: Optional[str]) -> str:
|
|
24
|
+
"""Canonical Python package directory name for a workspace named ``name``.
|
|
25
|
+
|
|
26
|
+
LOCKED decision: standardize on ``viva_<slug>`` (was ``pbg_<slug>`` before the
|
|
27
|
+
rebrand). ``None``/empty -> ``viva_workspace``; hyphens become underscores.
|
|
28
|
+
"""
|
|
29
|
+
return f"viva_{_slugify(name)}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def legacy_package_slug(name: Optional[str]) -> str:
|
|
33
|
+
"""The pre-rebrand ``pbg_<slug>`` package name, for compat recognition."""
|
|
34
|
+
return f"pbg_{_slugify(name)}"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def resolve_package_dir(root: Union[Path, str], name: Optional[str]) -> Path:
|
|
38
|
+
"""Absolute package directory for a workspace, honoring the migration window.
|
|
39
|
+
|
|
40
|
+
Returns ``<root>/viva_<slug>`` (canonical) UNLESS that directory is absent and
|
|
41
|
+
a legacy ``<root>/pbg_<slug>`` directory exists on disk, in which case the
|
|
42
|
+
existing ``pbg_<slug>`` path is returned so a not-yet-migrated workspace keeps
|
|
43
|
+
resolving to its real package. Once ``viva_<slug>`` exists it always wins,
|
|
44
|
+
even if a stale ``pbg_<slug>`` also lingers.
|
|
45
|
+
"""
|
|
46
|
+
root = Path(root)
|
|
47
|
+
canonical = root / package_slug(name)
|
|
48
|
+
if canonical.is_dir():
|
|
49
|
+
return canonical
|
|
50
|
+
legacy = root / legacy_package_slug(name)
|
|
51
|
+
if legacy.is_dir():
|
|
52
|
+
return legacy
|
|
53
|
+
return canonical
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Canonical-run outcome selection — the LOCKED semantics for which of a study's
|
|
2
|
+
recorded runs is authoritative.
|
|
3
|
+
|
|
4
|
+
Ported verbatim (minus the reconciliation/DB-writing machinery, which stays in
|
|
5
|
+
vivarium-workbench / viva-superpowers) from
|
|
6
|
+
``viva_superpowers/study_outcomes.py::canonical_run`` / ``canonical_outcomes``.
|
|
7
|
+
|
|
8
|
+
The rule, in priority order:
|
|
9
|
+
|
|
10
|
+
1. A run explicitly flagged ``canonical: true`` wins (last such run, if several).
|
|
11
|
+
2. Otherwise the newest COMPLETED run by ``timestamp``.
|
|
12
|
+
3. Otherwise the last run in the list.
|
|
13
|
+
4. Otherwise ``None`` / ``{}``.
|
|
14
|
+
|
|
15
|
+
This is deliberately NOT a last-run-wins merge across runs: :func:`canonical_outcomes`
|
|
16
|
+
returns exactly ONE run's ``outcomes`` dict, so an older ``canonical: true`` run
|
|
17
|
+
correctly overrides a newer completed run.
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import Optional, Union
|
|
22
|
+
|
|
23
|
+
# Status strings that count as a completed run.
|
|
24
|
+
_COMPLETE = {"complete", "completed", "ran", "done"}
|
|
25
|
+
|
|
26
|
+
# A study spec (dict with a "runs" list) or a bare runs list.
|
|
27
|
+
SpecOrRuns = Union[dict, list, None]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _runs_of(spec_or_runs: SpecOrRuns) -> list[dict]:
|
|
31
|
+
if isinstance(spec_or_runs, list):
|
|
32
|
+
runs = spec_or_runs
|
|
33
|
+
else:
|
|
34
|
+
runs = (spec_or_runs or {}).get("runs") or []
|
|
35
|
+
return [r for r in runs if isinstance(r, dict)]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def canonical_run(spec_or_runs: SpecOrRuns) -> Optional[dict]:
|
|
39
|
+
"""The run whose outcomes are authoritative: an explicit ``canonical: true``
|
|
40
|
+
(last one wins), else the newest completed run by ``timestamp``, else the last
|
|
41
|
+
run, else ``None``."""
|
|
42
|
+
runs = _runs_of(spec_or_runs)
|
|
43
|
+
if not runs:
|
|
44
|
+
return None
|
|
45
|
+
flagged = [r for r in runs if r.get("canonical") is True]
|
|
46
|
+
if flagged:
|
|
47
|
+
return flagged[-1]
|
|
48
|
+
completed = [r for r in runs if str(r.get("status", "")).lower() in _COMPLETE]
|
|
49
|
+
if completed:
|
|
50
|
+
return max(completed, key=lambda r: r.get("timestamp") or "")
|
|
51
|
+
return runs[-1]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def canonical_outcomes(spec_or_runs: SpecOrRuns) -> dict:
|
|
55
|
+
"""The canonical run's ``outcomes`` dict (empty if none)."""
|
|
56
|
+
run = canonical_run(spec_or_runs)
|
|
57
|
+
return (run or {}).get("outcomes") or {}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"""Canonical resolution of a workspace's directory layout.
|
|
2
|
+
|
|
3
|
+
Single source of truth for the ``layout:`` map in ``workspace.yaml`` and the
|
|
4
|
+
derived well-known directories (``studies/``, ``investigations/``, ``reports/``,
|
|
5
|
+
the Python package, ...). Consolidated from three divergent implementations that
|
|
6
|
+
this package replaces:
|
|
7
|
+
|
|
8
|
+
* ``vivarium-workbench`` ``vivarium_workbench/lib/workspace_paths.py`` — the
|
|
9
|
+
fullest ``WorkspacePaths`` (mtime-memoized ``load``, layout map, ~231 lines).
|
|
10
|
+
This is the canonical base.
|
|
11
|
+
* ``viva-superpowers`` ``viva_superpowers/workspace_paths.py`` — a hand-synced
|
|
12
|
+
vendored copy. Its ``LAYOUT_DEFAULTS`` had drifted to include ``notes`` and
|
|
13
|
+
``experiments`` keys not present in the workbench copy, and it dropped the
|
|
14
|
+
mtime memo. Reconciled here by taking the UNION of layout keys and keeping the
|
|
15
|
+
memoized ``load``.
|
|
16
|
+
* ``v2ecoli`` — the layout-map-aware ``_studies_root()`` /
|
|
17
|
+
``study_dir`` in ``v2ecoli/composites/ecoli_structural.py`` (reads
|
|
18
|
+
``layout.studies``) and the hardcoded ``ws/"workspace"/"studies"/slug``
|
|
19
|
+
literals in ``v2ecoli/library/parquet_viz.py`` + ``workflow/*``. Those
|
|
20
|
+
hardcoded literals are exactly what the layout-map-driven :func:`study_dir`
|
|
21
|
+
below replaces.
|
|
22
|
+
|
|
23
|
+
Backward compatibility: a key left out of ``layout:`` falls back to the
|
|
24
|
+
conventional flat name (``studies`` -> ``studies/``). A workspace with no
|
|
25
|
+
``layout:`` block at all therefore keeps the classic flat layout, so existing
|
|
26
|
+
workspaces are unaffected.
|
|
27
|
+
|
|
28
|
+
Example ``workspace.yaml`` nesting research dirs under ``workspace/``::
|
|
29
|
+
|
|
30
|
+
layout:
|
|
31
|
+
studies: workspace/studies
|
|
32
|
+
investigations: workspace/investigations
|
|
33
|
+
composites: workspace/composites
|
|
34
|
+
references: workspace/references
|
|
35
|
+
datasets: workspace/datasets
|
|
36
|
+
reports: workspace/reports
|
|
37
|
+
pbg: workspace/.pbg
|
|
38
|
+
"""
|
|
39
|
+
from __future__ import annotations
|
|
40
|
+
|
|
41
|
+
from dataclasses import dataclass
|
|
42
|
+
from pathlib import Path
|
|
43
|
+
from typing import Iterator, Mapping, Optional, Union
|
|
44
|
+
|
|
45
|
+
import yaml
|
|
46
|
+
|
|
47
|
+
from .naming import package_slug
|
|
48
|
+
|
|
49
|
+
# The canonical flat layout — the single source of truth for directory names.
|
|
50
|
+
# Keys are logical names used throughout the codebase; values are the default
|
|
51
|
+
# workspace-root-relative paths. The Python package (`package`) is special: it
|
|
52
|
+
# derives from `package_path`/`name` in workspace.yaml, so it has no fixed
|
|
53
|
+
# default here.
|
|
54
|
+
#
|
|
55
|
+
# Drift reconciliation: `notes` and `experiments` came from the viva-superpowers
|
|
56
|
+
# vendored copy; the rest match vivarium-workbench. The union is safe because an
|
|
57
|
+
# unused key simply resolves to a directory nothing reads.
|
|
58
|
+
LAYOUT_DEFAULTS: dict[str, str] = {
|
|
59
|
+
"studies": "studies",
|
|
60
|
+
"investigations": "investigations",
|
|
61
|
+
"composites": "composites",
|
|
62
|
+
"references": "references",
|
|
63
|
+
"datasets": "datasets",
|
|
64
|
+
"notes": "notes",
|
|
65
|
+
"experiments": "experiments",
|
|
66
|
+
"reports": "reports",
|
|
67
|
+
"pbg": ".pbg",
|
|
68
|
+
"scripts": "scripts",
|
|
69
|
+
"tests": "tests",
|
|
70
|
+
"docs": "docs",
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
# Logical names a workspace may override via `layout:` (`package` is normally
|
|
74
|
+
# set through `package_path`, but may also be relocated via `layout`).
|
|
75
|
+
LAYOUT_KEYS = tuple(LAYOUT_DEFAULTS) + ("package",)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# mtime-keyed memo for ``WorkspacePaths.load``. ``load`` was called ~126 times
|
|
79
|
+
# across the dashboard, each re-``stat``ing, re-reading and re-parsing
|
|
80
|
+
# ``workspace.yaml``. The layout rarely changes within a process lifetime, so we
|
|
81
|
+
# cache the resolved ``WorkspacePaths`` keyed by resolved root, tagged with the
|
|
82
|
+
# ``workspace.yaml`` mtime (ns). A cached entry is reused only while that mtime
|
|
83
|
+
# is unchanged; any edit (or create/delete) changes the tag and forces a
|
|
84
|
+
# re-parse, so the cache stays correct. Not locked: a benign race just re-parses.
|
|
85
|
+
# ``_parse_count`` is a test hook incremented on each real parse.
|
|
86
|
+
_LOAD_CACHE: dict[str, tuple[Optional[int], "WorkspacePaths"]] = {}
|
|
87
|
+
_parse_count = 0
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _clear_load_cache() -> None:
|
|
91
|
+
"""Drop the ``WorkspacePaths.load`` memo (test/utility helper)."""
|
|
92
|
+
_LOAD_CACHE.clear()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def find_workspace_root(start: Union[Path, str, None] = None) -> Path:
|
|
96
|
+
"""Walk up from ``start`` (default CWD) to the nearest ancestor holding a
|
|
97
|
+
``workspace.yaml``; return that directory.
|
|
98
|
+
|
|
99
|
+
The single root-walker consolidating ``vivarium_workbench.lib._root``'s
|
|
100
|
+
CWD-fallback loop and the copy-pasted ``_find_workspace_root`` in
|
|
101
|
+
``v2ecoli/library/parquet_viz.py`` (and siblings). Raises ``FileNotFoundError``
|
|
102
|
+
if no workspace root is found.
|
|
103
|
+
"""
|
|
104
|
+
here = Path(start or Path.cwd()).resolve()
|
|
105
|
+
for candidate in [here, *here.parents]:
|
|
106
|
+
if (candidate / "workspace.yaml").is_file():
|
|
107
|
+
return candidate
|
|
108
|
+
raise FileNotFoundError(
|
|
109
|
+
f"no workspace.yaml found walking up from {here}"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@dataclass(frozen=True)
|
|
114
|
+
class WorkspacePaths:
|
|
115
|
+
"""Resolved directory layout for a single workspace.
|
|
116
|
+
|
|
117
|
+
Construct via :meth:`load` (reads ``workspace.yaml``) or :meth:`from_config`
|
|
118
|
+
(caller supplies the parsed dict). Access directories by attribute
|
|
119
|
+
(``wp.studies``) or by name (``wp.dir("studies")``). Subpaths are formed by
|
|
120
|
+
joining onto the result, e.g. ``wp.pbg / "schemas"`` or
|
|
121
|
+
``wp.reports / "figures" / study``.
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
root: Path
|
|
125
|
+
_layout: Mapping[str, str]
|
|
126
|
+
|
|
127
|
+
@classmethod
|
|
128
|
+
def from_config(
|
|
129
|
+
cls, root: Union[Path, str], config: Optional[Mapping] = None
|
|
130
|
+
) -> "WorkspacePaths":
|
|
131
|
+
config = dict(config or {})
|
|
132
|
+
layout = dict(LAYOUT_DEFAULTS)
|
|
133
|
+
# Package directory: explicit package_path wins, else derive from name.
|
|
134
|
+
layout["package"] = config.get("package_path") or package_slug(config.get("name"))
|
|
135
|
+
# Apply explicit per-directory overrides.
|
|
136
|
+
overrides = config.get("layout") or {}
|
|
137
|
+
for key, value in overrides.items():
|
|
138
|
+
if key in LAYOUT_KEYS and isinstance(value, str) and value:
|
|
139
|
+
layout[key] = value
|
|
140
|
+
return cls(Path(root).resolve(), layout)
|
|
141
|
+
|
|
142
|
+
@classmethod
|
|
143
|
+
def load(cls, root: Union[Path, str]) -> "WorkspacePaths":
|
|
144
|
+
"""Resolve layout from ``<root>/workspace.yaml`` (empty if missing).
|
|
145
|
+
|
|
146
|
+
Memoized by resolved root + ``workspace.yaml`` mtime: a repeated load
|
|
147
|
+
with an unchanged file returns the cached instance without re-parsing;
|
|
148
|
+
a changed (or newly created/deleted) file invalidates the entry.
|
|
149
|
+
"""
|
|
150
|
+
root = Path(root)
|
|
151
|
+
resolved = str(root.resolve())
|
|
152
|
+
wf = root / "workspace.yaml"
|
|
153
|
+
try:
|
|
154
|
+
mtime: Optional[int] = wf.stat().st_mtime_ns
|
|
155
|
+
except OSError:
|
|
156
|
+
# Missing/unreadable workspace.yaml -> empty config. Tag None so a
|
|
157
|
+
# later creation (mtime becomes an int) invalidates the entry.
|
|
158
|
+
mtime = None
|
|
159
|
+
cached = _LOAD_CACHE.get(resolved)
|
|
160
|
+
if cached is not None and cached[0] == mtime:
|
|
161
|
+
return cached[1]
|
|
162
|
+
config: dict = {}
|
|
163
|
+
if mtime is not None:
|
|
164
|
+
config = yaml.safe_load(wf.read_text(encoding="utf-8")) or {}
|
|
165
|
+
global _parse_count
|
|
166
|
+
_parse_count += 1
|
|
167
|
+
wp = cls.from_config(root, config)
|
|
168
|
+
_LOAD_CACHE[resolved] = (mtime, wp)
|
|
169
|
+
return wp
|
|
170
|
+
|
|
171
|
+
def dir(self, name: str) -> Path:
|
|
172
|
+
"""Absolute path to the directory registered under logical ``name``."""
|
|
173
|
+
if name not in self._layout:
|
|
174
|
+
raise KeyError(f"unknown workspace directory: {name!r}")
|
|
175
|
+
return self.root / self._layout[name]
|
|
176
|
+
|
|
177
|
+
def rel(self, name: str) -> str:
|
|
178
|
+
"""Workspace-root-relative path string for logical ``name``."""
|
|
179
|
+
return self._layout[name]
|
|
180
|
+
|
|
181
|
+
# Convenience accessors -------------------------------------------------
|
|
182
|
+
@property
|
|
183
|
+
def studies(self) -> Path: return self.dir("studies")
|
|
184
|
+
@property
|
|
185
|
+
def investigations(self) -> Path: return self.dir("investigations")
|
|
186
|
+
@property
|
|
187
|
+
def composites(self) -> Path: return self.dir("composites")
|
|
188
|
+
@property
|
|
189
|
+
def references(self) -> Path: return self.dir("references")
|
|
190
|
+
@property
|
|
191
|
+
def datasets(self) -> Path: return self.dir("datasets")
|
|
192
|
+
@property
|
|
193
|
+
def notes(self) -> Path: return self.dir("notes")
|
|
194
|
+
@property
|
|
195
|
+
def experiments(self) -> Path: return self.dir("experiments")
|
|
196
|
+
@property
|
|
197
|
+
def reports(self) -> Path: return self.dir("reports")
|
|
198
|
+
@property
|
|
199
|
+
def pbg(self) -> Path: return self.dir("pbg")
|
|
200
|
+
@property
|
|
201
|
+
def scripts(self) -> Path: return self.dir("scripts")
|
|
202
|
+
@property
|
|
203
|
+
def tests(self) -> Path: return self.dir("tests")
|
|
204
|
+
@property
|
|
205
|
+
def docs(self) -> Path: return self.dir("docs")
|
|
206
|
+
@property
|
|
207
|
+
def package(self) -> Path: return self.dir("package")
|
|
208
|
+
|
|
209
|
+
# Study resolution (investigation-centric structure) --------------------
|
|
210
|
+
def iter_study_dirs(self) -> Iterator[Path]:
|
|
211
|
+
"""Yield every study dir. Flat ``studies/<slug>/`` FIRST (so a top-level
|
|
212
|
+
study wins on a slug collision and is never shadowed by a nested copy),
|
|
213
|
+
then nested ``investigations/<inv>/studies/<slug>/`` for any slug not
|
|
214
|
+
already yielded. A dir is a study iff it holds ``study.yaml``. Both roots
|
|
215
|
+
are resolved via the layout map (never hardcoded literals)."""
|
|
216
|
+
seen: set[str] = set()
|
|
217
|
+
flat = self.dir("studies")
|
|
218
|
+
if flat.is_dir():
|
|
219
|
+
for s in sorted(p for p in flat.iterdir() if p.is_dir()):
|
|
220
|
+
if (s / "study.yaml").is_file() and s.name not in seen:
|
|
221
|
+
seen.add(s.name)
|
|
222
|
+
yield s
|
|
223
|
+
inv_root = self.dir("investigations")
|
|
224
|
+
if inv_root.is_dir():
|
|
225
|
+
for inv in sorted(p for p in inv_root.iterdir() if p.is_dir()):
|
|
226
|
+
sroot = inv / "studies"
|
|
227
|
+
if sroot.is_dir():
|
|
228
|
+
for s in sorted(p for p in sroot.iterdir() if p.is_dir()):
|
|
229
|
+
if (s / "study.yaml").is_file() and s.name not in seen:
|
|
230
|
+
seen.add(s.name)
|
|
231
|
+
yield s
|
|
232
|
+
|
|
233
|
+
def study_dir(self, slug: str, must_exist: bool = False) -> Path:
|
|
234
|
+
"""Resolve a study by slug, honoring the layout map.
|
|
235
|
+
|
|
236
|
+
Searches existing studies flat-first then nested (see
|
|
237
|
+
:meth:`iter_study_dirs`) and returns the found dir. If the slug does not
|
|
238
|
+
exist yet and ``must_exist`` is False (default), returns the CANONICAL
|
|
239
|
+
location it would occupy — ``self.studies / slug`` — so callers writing a
|
|
240
|
+
new study land in the layout-map-driven location. With ``must_exist=True``
|
|
241
|
+
an absent slug raises ``FileNotFoundError`` (the old workbench semantics).
|
|
242
|
+
|
|
243
|
+
This unifies two divergent behaviours: the workbench/superpowers resolver
|
|
244
|
+
was a lookup that RAISED when absent, while v2ecoli's ``study_dir`` /
|
|
245
|
+
``_studies_root`` DERIVED an output path (``layout.studies/<slug>``) for a
|
|
246
|
+
study that may not exist yet. ``must_exist`` selects between them.
|
|
247
|
+
"""
|
|
248
|
+
for s in self.iter_study_dirs():
|
|
249
|
+
if s.name == slug:
|
|
250
|
+
return s
|
|
251
|
+
if must_exist:
|
|
252
|
+
raise FileNotFoundError(f"study {slug!r} not found under {self.root}")
|
|
253
|
+
return self.studies / slug
|
|
254
|
+
|
|
255
|
+
def report_dir(self, inv_slug: str) -> Path:
|
|
256
|
+
"""Per-investigation report/publication dir: investigations/<slug>/reports/."""
|
|
257
|
+
return self.dir("investigations") / inv_slug / "reports"
|
|
258
|
+
|
|
259
|
+
def inputs_dir(self, inv_slug: str) -> Path:
|
|
260
|
+
"""investigations/<inv_slug>/inputs (per-investigation owned inputs)."""
|
|
261
|
+
return self.dir("investigations") / inv_slug / "inputs"
|
|
262
|
+
|
|
263
|
+
def study_owner(self, slug: str) -> Optional[str]:
|
|
264
|
+
"""Owning investigation slug for a study: nested layout wins, else the
|
|
265
|
+
study.yaml ``investigation:`` back-ref, else None.
|
|
266
|
+
|
|
267
|
+
(The workbench's extra forward-list fallback scanned each
|
|
268
|
+
``investigation.yaml``'s ``studies:`` list via
|
|
269
|
+
``investigation_member_slugs`` — dropped here to keep this package free of
|
|
270
|
+
the vivarium-workbench dependency. Consumers needing it can layer it on.)
|
|
271
|
+
"""
|
|
272
|
+
try:
|
|
273
|
+
d = self.study_dir(slug, must_exist=True)
|
|
274
|
+
except FileNotFoundError:
|
|
275
|
+
return None
|
|
276
|
+
try:
|
|
277
|
+
parts = d.relative_to(self.dir("investigations")).parts
|
|
278
|
+
if len(parts) >= 3 and parts[1] == "studies":
|
|
279
|
+
return parts[0]
|
|
280
|
+
except ValueError:
|
|
281
|
+
pass
|
|
282
|
+
sy = d / "study.yaml"
|
|
283
|
+
if sy.is_file():
|
|
284
|
+
data = yaml.safe_load(sy.read_text(encoding="utf-8")) or {}
|
|
285
|
+
return data.get("investigation")
|
|
286
|
+
return None
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def study_dir(
|
|
290
|
+
root_or_paths: Union["WorkspacePaths", Path, str],
|
|
291
|
+
slug: str,
|
|
292
|
+
must_exist: bool = False,
|
|
293
|
+
) -> Path:
|
|
294
|
+
"""Layout-map-aware study directory resolver — the ONE resolver replacing the
|
|
295
|
+
~12 study-dir resolvers scattered across the three repos (including the
|
|
296
|
+
hardcoded ``ws/"workspace"/"studies"/slug`` literals in v2ecoli).
|
|
297
|
+
|
|
298
|
+
Accepts either a :class:`WorkspacePaths` or a workspace root (Path/str; loaded
|
|
299
|
+
via :meth:`WorkspacePaths.load`). Delegates to :meth:`WorkspacePaths.study_dir`,
|
|
300
|
+
so it honors ``layout.studies`` (nested ``workspace/studies`` vs legacy flat
|
|
301
|
+
``studies``) with no hardcoded literal, searches nested + flat, and — unless
|
|
302
|
+
``must_exist`` — returns the canonical write location for a not-yet-created
|
|
303
|
+
study.
|
|
304
|
+
"""
|
|
305
|
+
paths = root_or_paths if isinstance(root_or_paths, WorkspacePaths) \
|
|
306
|
+
else WorkspacePaths.load(root_or_paths)
|
|
307
|
+
return paths.study_dir(slug, must_exist=must_exist)
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: viva-workspace
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Shared source of truth for viva workspace-layout logic (layout map, root-walker, study resolver, canonical outcomes, package naming).
|
|
5
|
+
Author-email: Eran Agmon <agmon.eran@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/vivarium-collective/viva-workspace
|
|
8
|
+
Project-URL: Repository, https://github.com/vivarium-collective/viva-workspace
|
|
9
|
+
Project-URL: Issues, https://github.com/vivarium-collective/viva-workspace/issues
|
|
10
|
+
Keywords: vivarium,workspace,process-bigraph,study,layout
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pyyaml>=6
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# viva-workspace
|
|
29
|
+
|
|
30
|
+
The single **source of truth for workspace-layout logic** shared by
|
|
31
|
+
`vivarium-workbench`, `viva-superpowers`, and `v2ecoli`.
|
|
32
|
+
|
|
33
|
+
Before this package those three repos each reimplemented the same concepts —
|
|
34
|
+
~12 study-directory resolvers, ~6 root-walkers, and a hand-synced *vendored* copy
|
|
35
|
+
of `workspace_paths.py` — with three subtly conflicting semantics. This package
|
|
36
|
+
consolidates them into one dependency-light module so the layout is defined once.
|
|
37
|
+
|
|
38
|
+
## Why dependency-light
|
|
39
|
+
|
|
40
|
+
`vivarium-workbench` is deliberately **AI-free** (all AI lives in
|
|
41
|
+
`viva-superpowers`). To be safe for the workbench to depend on, this package uses
|
|
42
|
+
**stdlib + `pyyaml` only** — no `process_bigraph` / `bigraph_schema`, no web
|
|
43
|
+
framework, no AI deps.
|
|
44
|
+
|
|
45
|
+
## Public API
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from viva_workspace import (
|
|
49
|
+
WorkspacePaths, # load workspace.yaml (mtime-memoized) + derived dirs
|
|
50
|
+
find_workspace_root, # the one root-walker (walk up for workspace.yaml)
|
|
51
|
+
study_dir, # ONE layout-map-aware study resolver (nested + flat)
|
|
52
|
+
canonical_outcomes, # the LOCKED canonical-run outcome selection
|
|
53
|
+
package_slug, # canonical viva_<slug> (with pbg_<slug> compat shim)
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Layout map
|
|
58
|
+
|
|
59
|
+
`WorkspacePaths` reads an optional `layout:` map from `workspace.yaml`. A missing
|
|
60
|
+
key falls back to the flat default (`studies` -> `studies/`); a workspace with no
|
|
61
|
+
`layout:` block keeps the classic flat layout. To nest research dirs:
|
|
62
|
+
|
|
63
|
+
```yaml
|
|
64
|
+
layout:
|
|
65
|
+
studies: workspace/studies
|
|
66
|
+
investigations: workspace/investigations
|
|
67
|
+
reports: workspace/reports
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`study_dir(root_or_paths, slug)` honors that map for both nested
|
|
71
|
+
(`workspace/studies/<slug>`) and flat (`studies/<slug>`) layouts with no hardcoded
|
|
72
|
+
literal, searching flat-first then nested, and returning the canonical write
|
|
73
|
+
location for a not-yet-created study (pass `must_exist=True` to require existence).
|
|
74
|
+
|
|
75
|
+
### Canonical outcomes
|
|
76
|
+
|
|
77
|
+
`canonical_outcomes(study_spec)` returns exactly ONE run's `outcomes`: the run
|
|
78
|
+
flagged `canonical: true` if any (last wins), else the newest **completed** run by
|
|
79
|
+
timestamp, else the last run. It is *not* a last-run-wins merge — an older
|
|
80
|
+
`canonical: true` run correctly overrides a newer completed one.
|
|
81
|
+
|
|
82
|
+
### Package naming
|
|
83
|
+
|
|
84
|
+
`package_slug(name)` returns the canonical `viva_<slug>`. During the pbg -> viva
|
|
85
|
+
migration window, `resolve_package_dir(root, name)` returns an existing
|
|
86
|
+
`pbg_<slug>/` directory if the canonical `viva_<slug>/` is absent.
|
|
87
|
+
|
|
88
|
+
## Status
|
|
89
|
+
|
|
90
|
+
Local-only. Not yet published; consumers still ship their own copies until this is
|
|
91
|
+
released and they migrate.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
tests/test_naming.py
|
|
5
|
+
tests/test_outcomes.py
|
|
6
|
+
tests/test_paths.py
|
|
7
|
+
viva_workspace/__init__.py
|
|
8
|
+
viva_workspace/naming.py
|
|
9
|
+
viva_workspace/outcomes.py
|
|
10
|
+
viva_workspace/paths.py
|
|
11
|
+
viva_workspace.egg-info/PKG-INFO
|
|
12
|
+
viva_workspace.egg-info/SOURCES.txt
|
|
13
|
+
viva_workspace.egg-info/dependency_links.txt
|
|
14
|
+
viva_workspace.egg-info/requires.txt
|
|
15
|
+
viva_workspace.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
viva_workspace
|