testsAndMisc/python_pkg/phone_lease/owner.py
Krzysztof kuhy Rudnicki 24503e9197
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
phone_lease: fair FIFO queue; drop fix_waifu2x
Finish the queue left uncommitted on 2026-11-17: callers that find a
scope taken get a ticket and are granted in arrival order, dead
sessions are reaped at once. The CLI keeps exit 3 for a held scope.
fix_waifu2x.sh patched the CUDA waifu2x build, now uninstalled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWV2U9cEtTMTf7iQWin
2026-11-21 11:50:32 +01:00

57 lines
2.1 KiB
Python

"""Which Claude session is calling: the identity every lease is held under."""
from __future__ import annotations
import os
from pathlib import Path
# Set this to name the owner explicitly (CI, a script without a claude parent).
OWNER_ENV = "PHONE_LEASE_OWNER"
def owner_id(pid: int | None = None, environ: dict[str, str] | None = None) -> str:
"""Identify the calling session.
``PHONE_LEASE_OWNER`` wins. Otherwise walk up from ``pid`` (this process
by default) to the nearest ancestor whose ``comm`` is ``claude`` -- one
per session -- and fall back to this process's own pid when there is none.
"""
env = os.environ if environ is None else environ
explicit = env.get(OWNER_ENV)
if explicit:
return explicit
current = os.getpid() if pid is None else pid
origin = current
while current > 1:
try:
comm = Path(f"/proc/{current}/comm").read_text(encoding="utf-8").strip()
stat = Path(f"/proc/{current}/stat").read_text(encoding="utf-8")
except OSError:
break
if comm == "claude":
return f"claude:{current}"
# Field 4 of /proc/<pid>/stat is the parent pid; the comm field before
# it may contain spaces, so split after its closing paren.
current = int(stat.rsplit(")", 1)[1].split()[1])
return f"pid:{origin}"
def owner_alive(owner: str, *, proc_root: Path = Path("/proc")) -> bool:
"""Whether ``owner``'s session still runs, so its leases and tickets stay.
``claude:<pid>`` lives while that pid is a ``claude`` process (a reused pid
running anything else counts as dead); ``<tool>:<pid>`` lives while the pid
exists. ``pid:<pid>`` is the CLI's own short-lived process, and explicit
names (``PHONE_LEASE_OWNER``) carry no pid, so both always count as alive.
"""
kind, _, pid = owner.partition(":")
if not pid.isdigit() or kind == "pid":
return True
comm = proc_root / pid / "comm"
if kind != "claude":
return comm.parent.exists()
try:
return comm.read_text(encoding="utf-8").strip() == "claude"
except OSError:
return False