"""Launch-time journals belong in Library/Logs, outside protected Documents/Downloads. launchd opens these before the application starts, so an application's existing folder grant cannot authorize xpcproxy to open a journal inside the checkout. This does not move evidence or change access to the application's data directory. """ import os import stat from pathlib import Path def launchd_log_path(name: str) -> Path: if Path(name).name != name or not name.endswith(".log"): raise ValueError("A single journal filename is required") return Path.home() / "Library/Logs/NODE.DC/MissionCore" / name def prepare_launchd_log(path: Path) -> None: if path != launchd_log_path(path.name): raise ValueError("Unexpected launch journal directory") path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) info = path.parent.lstat() if path.parent.resolve() != path.parent or info.st_uid != os.getuid(): raise ValueError("Launch journal directory must be owned and canonical") descriptor = os.open(path, os.O_CREAT | os.O_APPEND | os.O_WRONLY | os.O_NOFOLLOW, 0o600) try: info = os.fstat(descriptor) if not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid(): raise ValueError("Launch journal must be an owned regular file") os.fchmod(descriptor, 0o600) finally: os.close(descriptor) def bounded_launchd_output(document: dict, repository: Path, *, uv_path=None) -> dict: """Preserve a declared command behind a single bounded output owner. uv is the accepted Mac privacy execution boundary for this checkout. Its bootstrap output is discarded; child output and tracebacks go to the owned journal. launchd still owns the full process group and the original policy. """ result = dict(document) arguments = list(result["ProgramArguments"]) if "k1link.rotating_process" in arguments: service_command(result) # Validate the existing owner instead of nesting wrappers. result["StandardOutPath"] = result["StandardErrorPath"] = "/dev/null" return result journal = result.get("StandardErrorPath") or result.get("StandardOutPath") if not journal or journal == "/dev/null": raise ValueError("An explicit service journal is required") path = Path(journal) if not path.is_absolute() or path.suffix != ".log": raise ValueError("Invalid service journal") # Legacy output is kept intact. Fresh managed generations alone rotate. managed = path.with_name(path.stem + ".managed.log") result["ProgramArguments"] = [ str(uv_path or "/opt/homebrew/bin/uv"), "run", "--no-sync", "--project", str(repository), "python", "-m", "k1link.rotating_process", "--log", str(managed), "--", *arguments, ] result["StandardOutPath"] = result["StandardErrorPath"] = "/dev/null" return result def service_command(document: dict) -> list[str]: arguments = list(document["ProgramArguments"]) if "k1link.rotating_process" in arguments: index = arguments.index("k1link.rotating_process") if arguments[index + 1 : index + 2] != ["--log"] or arguments[index + 3 : index + 4] != [ "--" ]: raise ValueError("Invalid managed journal command") return arguments[index + 4 :] return arguments def service_log_path(document: dict) -> Path: arguments = document["ProgramArguments"] if "k1link.rotating_process" in arguments: return Path(arguments[arguments.index("k1link.rotating_process") + 2]) return Path(document["StandardErrorPath"])