Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

6.6 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Bounded log-tail reads that seek from the end, so cost ignores file size; output is redacted."""

from __future__ import annotations

import base64
import json
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional

from utils.log_redaction import redact_log_text

BLOCK_BYTES = 65_536
DEFAULT_TAIL_LINES = 1_000
MAX_TAIL_LINES = 2_000  # == MAX_LINES_PER_RESPONSE: a larger ?lines= was silently capped
# /api is not gzipped, so this is what goes on the wire on first paint.
MAX_TAIL_BYTES = 1_048_576
MAX_APPEND_BYTES = 524_288
MAX_LINE_BYTES = 32_768
MAX_LINES_PER_RESPONSE = 2_000

_CURSOR_PREFIX = "c1."


@dataclass
class ReadResult:
    lines: list[str] = field(default_factory = list)
    cursor: Optional[str] = None
    reset: bool = False
    reset_reason: Optional[str] = None
    dropped_bytes: int = 0
    truncated_head: bool = False
    more_pending: bool = False
    size_bytes: int = 0


def _file_key(stat: os.stat_result, name: str) -> str:
    # st_ctime_ns changes on append on Linux; st_ino can be 0 on Windows, so name helps.
    return f"{name}|{stat.st_dev}|{stat.st_ino}"


def encode_cursor(key: str, offset: int) -> str:
    raw = json.dumps({"k": key, "o": int(offset)}, separators = (",", ":")).encode("utf-8")
    return _CURSOR_PREFIX + base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")


def decode_cursor(cursor: Optional[str]) -> Optional[tuple[str, int]]:
    """None if unusable; a foreign cursor gets a fresh tail, not an error, so polls stay quiet."""
    if not cursor or not isinstance(cursor, str) or not cursor.startswith(_CURSOR_PREFIX):
        return None
    body = cursor[len(_CURSOR_PREFIX) :]
    try:
        padded = body + "=" * (-len(body) % 4)
        payload = json.loads(base64.urlsafe_b64decode(padded.encode("ascii")).decode("utf-8"))
        key = payload["k"]
        offset = int(payload["o"])
    except Exception:
        return None
    if not isinstance(key, str) or offset < 0:
        return None
    return key, offset


def _split_lines(data: bytes, *, drop_partial_head: bool) -> tuple[list[str], bool]:
    truncated_head = False
    if drop_partial_head:
        first = data.find(b"\n")
        remainder = b"" if first == -1 else data[first + 1 :]
        if not remainder:
            # Window inside one huge record: keep its tail rather than render an empty pane.
            body = data if first == -1 else data[:first]
            remainder = body[-MAX_LINE_BYTES:]
        data = remainder
        truncated_head = True
    text = data.decode("utf-8", errors = "replace")
    raw = text.split("\n")
    if raw and raw[-1] == "":
        raw.pop()
    lines: list[str] = []
    for line in raw:
        line = line.rstrip("\r")
        # An enormous line is split rather than dropped, so nothing is lost.
        while len(line) > MAX_LINE_BYTES:
            lines.append(line[:MAX_LINE_BYTES])
            line = line[MAX_LINE_BYTES:]
        lines.append(line)
    return lines, truncated_head


def _redact(lines: list[str]) -> list[str]:
    return [redact_log_text(line) for line in lines]


def read_tail(path: Path, max_lines: int = DEFAULT_TAIL_LINES) -> ReadResult:
    max_lines = max(1, min(int(max_lines), MAX_TAIL_LINES))
    stat = path.stat()
    size = stat.st_size
    result = ReadResult(size_bytes = size)
    result.cursor = encode_cursor(_file_key(stat, path.name), size)
    result.reset = True
    if size == 0:
        return result

    chunks: list[bytes] = []
    pos = size
    newlines = 0
    scanned = 0
    with open(path, "rb") as handle:
        while pos > 0 and newlines <= max_lines and scanned < MAX_TAIL_BYTES:
            step = min(BLOCK_BYTES, pos, MAX_TAIL_BYTES - scanned)
            pos -= step
            handle.seek(pos)
            block = handle.read(step)
            if not block:
                break
            chunks.insert(0, block)
            newlines += block.count(b"\n")
            scanned += len(block)

    data = b"".join(chunks)
    lines, truncated = _split_lines(data, drop_partial_head = pos > 0)
    result.truncated_head = truncated
    if len(lines) > max_lines:
        lines = lines[-max_lines:]
        result.truncated_head = True
    result.lines = _redact(lines[-MAX_LINES_PER_RESPONSE:])
    return result


def read_since(
    path: Path,
    cursor: Optional[str],
    max_lines: int = DEFAULT_TAIL_LINES,
) -> ReadResult:
    """Appended lines only, or a fresh tail when the cursor cannot apply."""
    decoded = decode_cursor(cursor)
    if decoded is None:
        result = read_tail(path, max_lines)
        result.reset_reason = "initial" if not cursor else "cursor_stale"
        return result

    key, offset = decoded
    stat = path.stat()
    current_key = _file_key(stat, path.name)
    size = stat.st_size

    if current_key != key:
        result = read_tail(path, max_lines)
        result.reset_reason = "rotated"
        return result
    if offset > size:
        # Reopened in "w" mode, or truncated underneath us.
        result = read_tail(path, max_lines)
        result.reset_reason = "truncated"
        return result

    result = ReadResult(size_bytes = size)
    if offset == size:
        result.cursor = encode_cursor(current_key, offset)
        return result

    start = offset
    pending = size - offset
    if pending > MAX_APPEND_BYTES:
        start = size - MAX_APPEND_BYTES
        result.dropped_bytes = start - offset
    with open(path, "rb") as handle:
        handle.seek(start)
        data = handle.read(size - start)

    # Stop at the last newline and leave the cursor before the partial line
    last_newline = data.rfind(b"\n")
    if last_newline == -1:
        if len(data) < MAX_LINE_BYTES:
            result.cursor = encode_cursor(current_key, start)
            return result
        consumed = len(data)
        body = data
    else:
        consumed = last_newline + 1
        body = data[:consumed]

    # Cap by bytes before decoding so the cursor stops exactly where the response stops.
    newline_count = body.count(b"\n")
    if newline_count > MAX_LINES_PER_RESPONSE:
        cut = -1
        for _ in range(MAX_LINES_PER_RESPONSE):
            cut = body.find(b"\n", cut + 1)
        consumed = cut + 1
        body = body[:consumed]
        result.more_pending = True

    lines, truncated = _split_lines(body, drop_partial_head = result.dropped_bytes > 0)
    result.truncated_head = truncated
    result.lines = _redact(lines)
    result.cursor = encode_cursor(current_key, start + consumed)
    return result