107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
"""Lab projects folded into a worker project — ``/data/project-merges.json``,
|
|
``{"<lab slug>": "<worker project slug>"}``.
|
|
|
|
A repo can live twice: a checkout under ``~/projects`` on the lab and one on a
|
|
worker. When the worker's is the real one (the Orus monorepo: the lab clone
|
|
went stale once work moved to the Orus MacBook, 2026-09-28), every
|
|
conversation attributed to the lab slug is attributed to the worker project
|
|
instead, and the lab card leaves the Projects page — one project, its whole
|
|
history (Gabriel's call, 2026-11-06). Applied last, after every other rule
|
|
(``projects/workers.py:conversation_projects``), so a profile's home project
|
|
or a meta stamp naming the lab slug follows it too.
|
|
|
|
Owner-only: ``GET``/``PUT /api/project-merges``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import threading
|
|
import time
|
|
|
|
from core.config import PROJECT_MERGES_PATH
|
|
|
|
_lock = threading.Lock()
|
|
# (checked_monotonic, path, mtime, data)
|
|
_cache: tuple[float, str, float, dict[str, str]] | None = None
|
|
_STAT_TTL_S = 1.0
|
|
|
|
|
|
def _path() -> str:
|
|
return os.environ.get("PROJECT_MERGES_PATH") or PROJECT_MERGES_PATH
|
|
|
|
|
|
def load() -> dict[str, str]:
|
|
"""``{lab slug: worker project slug}`` — re-read when the file changes
|
|
(stat rate-limited to once per second)."""
|
|
global _cache
|
|
p = _path()
|
|
now = time.monotonic()
|
|
with _lock:
|
|
if _cache and _cache[1] == p and now - _cache[0] < _STAT_TTL_S:
|
|
return _cache[3]
|
|
try:
|
|
mtime = os.stat(p).st_mtime
|
|
except OSError:
|
|
with _lock:
|
|
_cache = (now, p, 0.0, {})
|
|
return {}
|
|
with _lock:
|
|
if _cache and _cache[1] == p and _cache[2] == mtime:
|
|
_cache = (now, p, mtime, _cache[3])
|
|
return _cache[3]
|
|
try:
|
|
with open(p, encoding="utf-8") as f:
|
|
raw = json.load(f)
|
|
except (OSError, ValueError):
|
|
raw = {}
|
|
data = {str(k): str(v) for k, v in (raw or {}).items()
|
|
if isinstance(k, str) and isinstance(v, str) and k and v and k != v}
|
|
_cache = (now, p, mtime, data)
|
|
return data
|
|
|
|
|
|
def version() -> float:
|
|
p = _path()
|
|
now = time.monotonic()
|
|
with _lock:
|
|
if _cache and _cache[1] == p and now - _cache[0] < _STAT_TTL_S:
|
|
return _cache[2]
|
|
try:
|
|
return os.stat(p).st_mtime
|
|
except OSError:
|
|
return 0.0
|
|
|
|
|
|
def save(data: dict[str, str]) -> dict[str, str]:
|
|
global _cache
|
|
p = _path()
|
|
clean = {k: v for k, v in data.items() if k and v and k != v}
|
|
tmp = p + ".tmp"
|
|
with _lock:
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
|
json.dump(clean, f, indent=2, sort_keys=True)
|
|
f.write("\n")
|
|
os.replace(tmp, p)
|
|
_cache = None
|
|
return clean
|
|
|
|
|
|
def apply(slugs: list[str], merges: dict[str, str] | None = None) -> list[str]:
|
|
"""``slugs`` with each merged lab slug replaced by its worker project,
|
|
order kept, duplicates dropped."""
|
|
m = load() if merges is None else merges
|
|
out: list[str] = []
|
|
for s in slugs:
|
|
s = m.get(s, s)
|
|
if s not in out:
|
|
out.append(s)
|
|
return out
|
|
|
|
|
|
def merged_from(slug: str, merges: dict[str, str] | None = None) -> list[str]:
|
|
"""The lab slugs folded into ``slug``."""
|
|
m = load() if merges is None else merges
|
|
return sorted(k for k, v in m.items() if v == slug)
|