Skip to content

output_hub

output_hub

Live per-sheet output streaming (#352 increment 3).

Instrument drain chunks → line-buffered redaction → bounded per-sheet ring → fan-out to IPC subscribers (job.output.stream → mzt watch).

Design (4-model lab, ~/lab-archives/2026-06-11-streaming-352): - Redaction happens ONCE, at entry, on COMPLETE lines — chunk-boundary splits are the #1 redaction hazard, so partial lines are buffered until their newline arrives (or a size flush forces them out). - Each (job, sheet) holds a ~256KB drop-oldest ring. Ephemeral by design: a conductor restart loses the ring; SheetState.stdout_tail remains the persisted evidence. - Subscribers get bounded queues with drop-on-backpressure — a slow mzt watch reader must NEVER block or freeze the conductor. Dropped output is surfaced as a [dropped ...] marker, not hidden. - The writer callback is synchronous and runs on the daemon loop (the drain awaits between chunks), so no locking is needed.

Classes

OutputSubscriber

OutputSubscriber()

One mzt watch client: a bounded queue + drop accounting.

Source code in src/marianne/daemon/output_hub.py
def __init__(self) -> None:
    self.queue: asyncio.Queue[str] = asyncio.Queue(
        maxsize=SUBSCRIBER_QUEUE_SIZE
    )
    self.dropped = 0
Methods:
offer
offer(line)

Enqueue without ever blocking; count drops on backpressure.

Source code in src/marianne/daemon/output_hub.py
def offer(self, line: str) -> None:
    """Enqueue without ever blocking; count drops on backpressure."""
    if self.dropped:
        # Try to surface the marker first so ordering reads naturally.
        try:
            self.queue.put_nowait(f"[dropped {self.dropped} lines]")
            self.dropped = 0
        except asyncio.QueueFull:
            self.dropped += 1
            return
    try:
        self.queue.put_nowait(line)
    except asyncio.QueueFull:
        self.dropped += 1

SheetOutputBuffer

SheetOutputBuffer(capacity_bytes=DEFAULT_RING_CAPACITY)

Line assembly + redaction + drop-oldest ring for one (job, sheet).

Source code in src/marianne/daemon/output_hub.py
def __init__(self, capacity_bytes: int = DEFAULT_RING_CAPACITY) -> None:
    self._capacity = capacity_bytes
    self._lines: deque[str] = deque()
    self._size = 0
    self._dropped_lines = 0
    # Per-stream partial-line buffers (stdout and stderr interleave).
    self._partials: dict[str, bytes] = {}
Methods:
feed
feed(stream, chunk)

Absorb a raw chunk; return the COMPLETE lines it produced.

Lines are decoded (errors="replace"), redacted once, stderr-tagged, and appended to the ring before being returned for fan-out.

Source code in src/marianne/daemon/output_hub.py
def feed(self, stream: str, chunk: bytes) -> list[str]:
    """Absorb a raw chunk; return the COMPLETE lines it produced.

    Lines are decoded (errors="replace"), redacted once, stderr-tagged,
    and appended to the ring before being returned for fan-out.
    """
    data = self._partials.get(stream, b"") + chunk
    *complete, rest = data.split(b"\n")
    if len(rest) > MAX_PARTIAL_LINE:
        complete.append(rest)
        rest = b""
    self._partials[stream] = rest

    out: list[str] = []
    for raw in complete:
        text = raw.decode("utf-8", errors="replace")
        # Carriage-return redraws (progress bars, TUIs) leave the last
        # segment as the visible line; earlier segments were overwritten.
        if "\r" in text:
            text = text.rsplit("\r", 1)[-1]
        text = strip_terminal_controls(text)
        text = redact_credentials(text)
        if stream == "stderr":
            text = f"[stderr] {text}"
        self._append(text)
        out.append(text)
    return out
flush
flush()

Flush any unterminated partial lines (end of execution).

Source code in src/marianne/daemon/output_hub.py
def flush(self) -> list[str]:
    """Flush any unterminated partial lines (end of execution)."""
    out: list[str] = []
    for stream, rest in list(self._partials.items()):
        if rest:
            out.extend(self.feed(stream, b"\n"))
    self._partials.clear()
    return out
snapshot
snapshot()

The retained lines, prefixed with an eviction marker if any.

Source code in src/marianne/daemon/output_hub.py
def snapshot(self) -> list[str]:
    """The retained lines, prefixed with an eviction marker if any."""
    lines = list(self._lines)
    if self._dropped_lines:
        lines.insert(0, f"[dropped {self._dropped_lines} earlier lines]")
    return lines

OutputStreamHub

OutputStreamHub(ring_capacity=DEFAULT_RING_CAPACITY)

Daemon-wide registry of per-sheet output buffers and subscribers.

Source code in src/marianne/daemon/output_hub.py
def __init__(self, ring_capacity: int = DEFAULT_RING_CAPACITY) -> None:
    self._ring_capacity = ring_capacity
    self._buffers: dict[tuple[str, int], SheetOutputBuffer] = {}
    self._subscribers: dict[tuple[str, int], set[OutputSubscriber]] = {}
    # Job-wide subscribers receive every sheet's lines, sheet-tagged.
    self._job_subscribers: dict[str, set[OutputSubscriber]] = {}
Methods:
make_writer
make_writer(job_id, sheet_num)

A per-dispatch chunk sink for the instrument's drain loop.

Fail-open: streaming is observability — an error here must never break the execution it observes.

Source code in src/marianne/daemon/output_hub.py
def make_writer(
    self, job_id: str, sheet_num: int
) -> Callable[[str, bytes], None]:
    """A per-dispatch chunk sink for the instrument's drain loop.

    Fail-open: streaming is observability — an error here must never
    break the execution it observes.
    """
    key = (job_id, sheet_num)

    def write(stream: str, chunk: bytes) -> None:
        try:
            buffer = self._buffers.get(key)
            if buffer is None:
                buffer = SheetOutputBuffer(self._ring_capacity)
                self._buffers[key] = buffer
            for line in buffer.feed(stream, chunk):
                self._fan_out(key, line)
        except Exception:  # noqa: BLE001 — fail-open: a watcher must never break a sheet
            # WARNING: watchers lose this chunk (GH #308).
            _logger.warning(
                "output_hub.write_failed",
            extra={"job_id": job_id, "sheet_num": sheet_num},
            exc_info=True,
            )

    return write
flush
flush(job_id, sheet_num)

Flush partial lines at end of execution. Fail-open.

Source code in src/marianne/daemon/output_hub.py
def flush(self, job_id: str, sheet_num: int) -> None:
    """Flush partial lines at end of execution. Fail-open."""
    try:
        buffer = self._buffers.get((job_id, sheet_num))
        if buffer is None:
            return
        for line in buffer.flush():
            self._fan_out((job_id, sheet_num), line)
    except Exception:  # noqa: BLE001 — fail-open: a watcher must never break a sheet
        # WARNING: watchers lose the final partial line (GH #308).
        _logger.warning(
            "output_hub.flush_failed",
            extra={"job_id": job_id, "sheet_num": sheet_num},
            exc_info=True,
        )
snapshot
snapshot(job_id, sheet_num=None)

Retained lines for one sheet, or all sheets (tagged) for a job.

Source code in src/marianne/daemon/output_hub.py
def snapshot(self, job_id: str, sheet_num: int | None = None) -> list[str]:
    """Retained lines for one sheet, or all sheets (tagged) for a job."""
    if sheet_num is not None:
        buffer = self._buffers.get((job_id, sheet_num))
        return buffer.snapshot() if buffer else []
    lines: list[str] = []
    for num in self.sheets_for_job(job_id):
        buffer = self._buffers[(job_id, num)]
        lines.extend(f"[s{num}] {line}" for line in buffer.snapshot())
    return lines
subscribe
subscribe(job_id, sheet_num=None)

Subscribe to one sheet, or (sheet_num=None) the whole job — job-wide lines arrive tagged [s<sheet>] ....

Source code in src/marianne/daemon/output_hub.py
def subscribe(
    self, job_id: str, sheet_num: int | None = None
) -> OutputSubscriber:
    """Subscribe to one sheet, or (sheet_num=None) the whole job —
    job-wide lines arrive tagged ``[s<sheet>] ...``."""
    sub = OutputSubscriber()
    if sheet_num is None:
        self._job_subscribers.setdefault(job_id, set()).add(sub)
    else:
        self._subscribers.setdefault((job_id, sheet_num), set()).add(sub)
    return sub
clear_job
clear_job(job_id)

Drop a finished job's rings (memory bound). Subscribers keep their already-queued lines; new output cannot arrive anyway.

Source code in src/marianne/daemon/output_hub.py
def clear_job(self, job_id: str) -> None:
    """Drop a finished job's rings (memory bound). Subscribers keep
    their already-queued lines; new output cannot arrive anyway."""
    for key in [k for k in self._buffers if k[0] == job_id]:
        del self._buffers[key]
    for key in [k for k in self._subscribers if k[0] == job_id]:
        del self._subscribers[key]
    self._job_subscribers.pop(job_id, None)

Functions:

strip_terminal_controls

strip_terminal_controls(text)

Remove escape sequences and every control character except tab.

Sequence forms are stripped first (so their payload bytes go with them), then an allowlist drops any remaining Unicode Cc character other than : C0, DEL and the C1 range a terminal may honour as introducers.

Source code in src/marianne/daemon/output_hub.py
def strip_terminal_controls(text: str) -> str:
    """Remove escape sequences and every control character except tab.

    Sequence forms are stripped first (so their payload bytes go with them),
    then an allowlist drops any remaining Unicode ``Cc`` character other than
    ``\t``: C0, DEL and the C1 range a terminal may honour as introducers.
    """
    text = _CONTROL_RE.sub("", text)
    return "".join(
        ch for ch in text if ch == "\t" or unicodedata.category(ch) != "Cc"
    )