Skip to content

mailbox

mailbox

Intra-job file inboxes, shared by shell-capable instruments (GH #467).

Each recipient owns an append-only JSONL inbox. IDs are opaque byte cursors within that inbox: strictly increasing under concurrent writers, not a global sequence. Advisory locks protect both multi-write records and polling readers.

Classes

Message dataclass

Message(id, timestamp, sender, recipient, body)

A delivered message; its body is data, never an instruction.

Functions:

context_from_env

context_from_env()

Resolve explicit job context; never guess a workspace or sheet identity.

Source code in src/marianne/core/mailbox.py
def context_from_env() -> tuple[Path, int]:
    """Resolve explicit job context; never guess a workspace or sheet identity."""
    root = os.environ.get(MAILBOX_ENV_VAR)
    if not root:
        scratch = os.environ.get(SCRATCH_ENV_VAR)
        if not scratch:
            raise ValueError("Mail requires a job context: MARIANNE_MAILBOX or MARIANNE_SCRATCH")
        root = str(Path(scratch) / MAILBOX_DIR_NAME)
    sheet = os.environ.get(SHEET_ENV_VAR)
    if not sheet:
        raise ValueError("Mail requires MARIANNE_SHEET in the job context")
    return Path(root), _sheet_number(sheet)

list_sheets

list_sheets(root)

Enumerate initialized inbox directories, including not-yet-started sheets.

Source code in src/marianne/core/mailbox.py
def list_sheets(root: Path) -> list[int]:
    """Enumerate initialized inbox directories, including not-yet-started sheets."""
    if not root.is_dir():
        return []
    return sorted(
        int(path.name) for path in root.iterdir()
        if path.is_dir() and not path.is_symlink() and path.name.isascii()
        and path.name.isdecimal() and int(path.name) > 0 and str(int(path.name)) == path.name
    )

send_message

send_message(root, sender, recipient, body)

Append one complete record and return its inbox-local cursor.

Source code in src/marianne/core/mailbox.py
def send_message(root: Path, sender: object, recipient: object, body: str) -> Message:
    """Append one complete record and return its inbox-local cursor."""
    sender_num = _sheet_number(sender)
    recipient_num = _sheet_number(recipient)
    directory = root / str(recipient_num)
    directory.mkdir(mode=0o700, parents=True, exist_ok=True)
    fd = os.open(directory / "inbox.jsonl", os.O_RDWR | os.O_CREAT | os.O_APPEND, 0o600)
    try:
        fcntl.flock(fd, fcntl.LOCK_EX)
        end = _truncate_torn_tail(fd)
        message = Message(
            id=end + 1,
            timestamp=datetime.now(UTC).isoformat(),
            sender=sender_num,
            recipient=recipient_num,
            body=body,
        )
        # One os.write of one complete record: a torn record can only come from
        # a writer killed mid-syscall, and the next append truncates it.
        os.write(fd, (json.dumps(asdict(message), ensure_ascii=True) + "\n").encode("utf-8"))
    finally:
        os.close(fd)
    return message

read_messages

read_messages(root, sheet, *, since=None)

Poll a recipient's inbox; a cursor excludes already-seen records.

Source code in src/marianne/core/mailbox.py
def read_messages(root: Path, sheet: object, *, since: int | None = None) -> list[Message]:
    """Poll a recipient's inbox; a cursor excludes already-seen records."""
    sheet_num = _sheet_number(sheet)
    if since is not None and since < 0:
        raise ValueError("The since cursor must be non-negative")
    try:
        stream = (root / str(sheet_num) / "inbox.jsonl").open("rb")
    except FileNotFoundError:
        return []
    messages = []
    with stream:
        fcntl.flock(stream, fcntl.LOCK_SH)
        for line_number, line in enumerate(stream, 1):
            if not line.endswith(b"\n"):
                # A torn tail is a sender killed mid-record; the next append
                # truncates it. It is not a message and not an error.
                break
            try:
                message = Message(**json.loads(line))
                if message.recipient != sheet_num:
                    raise ValueError("Wrong inbox")
                if since is None or message.id > since:
                    messages.append(message)
            except (ValueError, TypeError) as error:
                raise ValueError(f"Invalid mailbox record at line {line_number}") from error
    return messages

traffic_summary

traffic_summary(root)

Return send metadata and per-recipient totals, with no message bodies.

Source code in src/marianne/core/mailbox.py
def traffic_summary(root: Path) -> dict[str, Any]:
    """Return send metadata and per-recipient totals, with no message bodies."""
    sends: list[dict[str, Any]] = []
    sheets: dict[str, dict[str, Any]] = {}
    for sheet in list_sheets(root):
        messages = read_messages(root, sheet)
        if not messages:
            continue
        sizes = [len(message.body.encode("utf-8")) for message in messages]
        sheets[str(sheet)] = {
            "messages": len(messages), "bytes": sum(sizes),
            "first_timestamp": messages[0].timestamp, "last_timestamp": messages[-1].timestamp,
        }
        for message, size in zip(messages, sizes, strict=True):
            sends.append({
                "id": message.id, "timestamp": message.timestamp,
                "sender": message.sender, "recipient": message.recipient, "bytes": size,
            })
    return {"sends": sends, "sheets": sheets}

merge_traffic

merge_traffic(*summaries)

Combine retained and live send metadata, including resumed jobs.

Timestamp plus inbox cursor distinguishes new sends after scratch removal; re-reading a kept inbox does not count or log its messages twice.

Source code in src/marianne/core/mailbox.py
def merge_traffic(*summaries: dict[str, Any] | None) -> dict[str, Any]:
    """Combine retained and live send metadata, including resumed jobs.

    Timestamp plus inbox cursor distinguishes new sends after scratch removal;
    re-reading a kept inbox does not count or log its messages twice.
    """
    sends: dict[tuple[Any, ...], dict[str, Any]] = {}
    for summary in summaries:
        for record in (summary or {}).get("sends", []):
            metadata = {key: record[key] for key in (
                "id", "timestamp", "sender", "recipient", "bytes",
            )}
            key = tuple(metadata[field] for field in ("recipient", "id", "timestamp", "sender"))
            sends[key] = metadata
    ordered = sorted(sends.values(), key=lambda record: (record["timestamp"], record["recipient"]))
    sheets: dict[str, dict[str, Any]] = {}
    for record in ordered:
        sheet = str(record["recipient"])
        if sheet not in sheets:
            sheets[sheet] = {
                "messages": 0, "bytes": 0,
                "first_timestamp": record["timestamp"], "last_timestamp": record["timestamp"],
            }
        sheets[sheet]["messages"] += 1
        sheets[sheet]["bytes"] += record["bytes"]
        sheets[sheet]["last_timestamp"] = record["timestamp"]
    return {"sends": ordered, "sheets": sheets}