"""Where the job registry and its audit log live (Issue #15, Issue #57). Standard library only, on purpose: create/resume's control-plane binding, status.sh and lib.sh resolve these paths through ``_delegate_py_bin``, whose last fallback is the PATH's ``python3``, which may not have paho-mqtt. ``mqtt_common`` re-exports every name here, so existing callers are unchanged. """ from __future__ import annotations import os from typing import Optional DEFAULT_REGISTRY_DIR = ".mam/jobs" def _workspace_mam_dir(leaf: str) -> Optional[str]: """`$WORKSPACE_ROOT/.mam/` when lib.sh has told us which workspace we serve; None otherwise.""" root = os.environ.get("WORKSPACE_ROOT") if root and root.strip() and os.path.isdir(root): return os.path.join(root, ".mam", leaf) return None def default_registry_dir() -> str: """Registry root, resolved at call time (Issue #15). ``MAM_JOBS_DIR`` wins; otherwise the workspace lib.sh exported as ``WORKSPACE_ROOT``; otherwise the historical cwd-relative ``.mam/jobs``. Anchoring on the workspace is what keeps a test sandbox (or any caller whose cwd is not the workspace) from registering jobs into whichever ``.mam/jobs`` happens to sit under the current directory - the pytest suite used to leave hundreds of onboarding records in the real registry. """ env = os.environ.get("MAM_JOBS_DIR") if env and env.strip(): return env return _workspace_mam_dir("jobs") or DEFAULT_REGISTRY_DIR def sibling_logs_dir(registry_dir: str) -> str: """The audit-log root that pairs with ``registry_dir``: the ``delegate_job_logs`` next to it. Pure; reads no environment. Issue #57. The registry is taken by its real path: a registry directory that is itself a symlink pairs with the audit log next to its target, which is what the wrapper (``pwd -P``) and the control-plane binding (``realpath``) already see, so submit and logs agree (Mac review R2). A relative registry resolves against the cwd, as registry.py opens it. """ return os.path.join(os.path.dirname(os.path.realpath(registry_dir)), "delegate_job_logs") def get_logs_dir(registry_dir: Optional[str] = None) -> str: """Audit-log root, resolved at call time (B-9). ``DELEGATE_JOB_LOGS_DIR`` when set: it means the audit log was moved to a place that cannot be derived from the registry. Otherwise the audit log follows the registry (Issue #57): the ``delegate_job_logs`` next to ``registry_dir``, or next to ``default_registry_dir()`` when the caller does not know its registry. The default registry is ``$WORKSPACE_ROOT/.mam/jobs`` (Issue #15), otherwise ``/.mam/jobs``, so the usual roots are unchanged. Resolved per call rather than at import so a chdir after import cannot strand the audit trail in the old tree — the same reason ``DEFAULT_REGISTRY_DIR`` stays a relative string. """ env = os.environ.get("DELEGATE_JOB_LOGS_DIR") if env and env.strip(): return env return sibling_logs_dir(registry_dir or default_registry_dir())