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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Write a readable traceback under each JSON log record (#8585)
Daniel Hane005b1e990b0
diff --git a/studio/backend/loggers/config.py b/studio/backend/loggers/config.pyindex 4ab0ea67ff1..fa143b0269d 100644--- a/studio/backend/loggers/config.py+++ b/studio/backend/loggers/config.py@@ -100,6 +100,171 @@ def _truncate_exception_processor(logger, method_name, event_dict):return truncate_exception(event_dict)+def _plain_tracebacks_enabled() -> bool:+ """Echo readable tracebacks? ``UNSLOTH_STUDIO_PLAIN_TRACEBACKS=0`` turns it off."""+ return (os.environ.get("UNSLOTH_STUDIO_PLAIN_TRACEBACKS") or "").strip().lower() not in (+ "0",+ "off",+ "no",+ "false",+ )+++# Prefix on every echoed line. NOT whitespace: RFC 8259 lets a parser skip leading+# space/tab, so json.loads(' {"event": ...}') SUCCEEDS and a request-derived exception+# message could forge a record (CWE-117). "| " cannot begin a JSON value, so no echoed+# line parses as one, and a records-only reader can drop them on the prefix alone.+_TRACEBACK_ECHO_PREFIX = "| "+++# Unicode's Bidi_Control set (PropList.txt), exactly what the UAX #9 algorithm acts on and+# what UTR #36 / Trojan Source (CVE-2021-42574) name. json.dumps already escapes these+# (ensure_ascii), so only the echo would emit them raw.+#+# Deliberately NOT all of category Cf: U+200B-200D, U+00AD and U+FEFF occur in ordinary+# text (ZWNJ in Persian/Arabic, ZWJ in emoji) and reorder nothing.+_BIDI_CONTROLS = frozenset(+ "" # ARABIC LETTER MARK+ "" # LEFT-TO-RIGHT / RIGHT-TO-LEFT MARK+ "" # LRE, RLE, PDF, LRO, RLO+ "" # LRI, RLI, FSI, PDI+)+++def _escape_unprintable(text: str) -> str:+ """Spell as ``\\uXXXX`` what a terminal would ACT on or stdout cannot encode, leaving+ ordinary non-ASCII text readable. The JSON renderer used to cover all three for free:++ * **Lone surrogates** (reachable via ``json.loads('"\\ud800"')`` in a request body)+ raise ``UnicodeEncodeError`` on a UTF-8 stdout, which inside an exception handler+ loses the traceback AND replaces the original exception with the encoding error.+ * **Terminal controls**: raw ESC lets request-derived text rewrite what the reader+ sees, and a backspace run can rub out the prefix record forgery depends on.+ * **Bidi controls** need no terminal -- any UAX #9 viewer reorders the line. Measured:+ ``"| ValueError: rejected upload \\u202egnp.eliforp/sdaolpu/"`` DISPLAYS as+ ``| ValueError: rejected upload /uploads/profile.png``. Escaped, not stripped, so+ the record still says one was there.++ Tab is kept: it shifts alignment but cannot move the cursor back or erase.+ """+ out = []+ for ch in text:+ code = ord(ch)+ if ch == "\t":+ out.append(ch)+ elif (+ code < 0x20+ or code == 0x7F+ or 0x80 <= code <= 0x9F+ or 0xD800 <= code <= 0xDFFF+ or ch in _BIDI_CONTROLS+ ):+ out.append(f"\\u{code:04x}")+ else:+ out.append(ch)+ return "".join(out)+++def _echoable(exception: str) -> str:+ """The traceback as lines that can never read as a log record, nor act on a terminal.++ ``splitlines`` also splits on \\r, \\x0b, \\x0c, \\x85 and U+2028/9, so rejoining on+ \\n normalises every separator a message could smuggle in, including the \\r the export+ worker's log reader treats as a line break.++ Capped AFTER escaping: the field arrives bounded by ``truncate_exception`` at+ ``_MAX_EXC_CHARS``, but escaping costs six characters each, so an all-C0 payload turns+ 16 KiB of bounded field into 98 KiB of echo. Capping the input keeps that multiplier."""+ lines = [+ f"{_TRACEBACK_ECHO_PREFIX}{_escape_unprintable(part)}"+ for part in exception.rstrip().splitlines()+ ]+ return _cap_echoed_lines(lines, _MAX_EXC_CHARS)+++def _cap_echoed_lines(lines: list[str], limit: int) -> str:+ """Join the echoed lines within `limit` characters, keeping the head and the tail.++ Whole lines where they fit, and the omission notice is prefixed too, so no emitted line+ can begin a JSON value. A line too long for its budget is cut, not dropped: the cut can+ land inside a ``\\uXXXX`` escape, but the tail holds the exception type and message and+ a control-heavy message is exactly what makes that last line oversized."""+ if limit <= 0:+ return "\n".join(lines)+ total = sum(len(line) + 1 for line in lines)+ if total <= limit:+ return "\n".join(lines)+ tail_budget = max(1, limit // 4)+ head_budget = limit - tail_budget+ head: list[str] = []+ used = 0+ for line in lines:+ if used + len(line) + 1 > head_budget:+ break+ head.append(line)+ used += len(line) + 1+ tail: list[str] = []+ used = 0+ for line in reversed(lines[len(head) :]):+ if used + len(line) + 1 > tail_budget:+ # Cut the boundary line rather than drop it: losing a traceback's last line+ # leaves the reader every frame and no reason.+ room = tail_budget - used - 1+ if room > 0:+ tail.append(line[:room])+ break+ tail.append(line)+ used += len(line) + 1+ tail.reverse()+ if not head and not tail:+ head = [lines[0][:head_budget]]+ # A cut boundary line counts as kept, so say "cut" rather than claim zero lines went.+ dropped = len(lines) - len(head) - len(tail)+ what = f"{dropped} lines omitted" if dropped else "cut here"+ notice = (+ f"{_TRACEBACK_ECHO_PREFIX}... [{what}; raise "+ "UNSLOTH_STUDIO_MAX_EXCEPTION_CHARS to see it all] ..."+ )+ return "\n".join([*head, notice, *tail])+++def with_readable_traceback(renderer):+ """Wrap the JSON renderer so an exception is ALSO echoed as a real multi-line traceback+ on the lines after the record.++ ~/.unsloth/studio/logs is a tee of stdout and stdout is JSON, so every traceback+ reached its reader as one enormous line with newlines escaped to ``\\n`` -- correct+ JSON, unreadable prose. The reported Image Transform failure ("RuntimeError: Input type+ (float) and bias type (c10::BFloat16)...") arrived that way, and so does every crash+ anyone is asked to send in.++ The JSON record is emitted UNCHANGED, so record-by-record readers see what they always+ saw, and every echoed line is prefixed so it cannot parse as a record. Non-JSON lines+ in that file are already expected -- faulthandler dumps native stacks to the same+ handle.++ Returned as part of the SAME string rather than written to another stream, so one+ ``print`` under ``PrintLogger``'s lock keeps record and traceback adjacent and ordered:+ a processor runs BEFORE that print, and the export worker reads stdout and stderr on+ separate pipes.++ JSON only. ConsoleRenderer (development) already prints tracebacks as tracebacks."""++ def _render(logger, method_name, event_dict):+ exception = event_dict.get("exception")+ line = renderer(logger, method_name, event_dict)+ if (+ isinstance(exception, str)+ and exception.strip()+ and isinstance(line, str)+ and _plain_tracebacks_enabled()+ ):+ return f"{line}\n{_echoable(exception)}"+ return line++ return _render++# Set alongside HF_HUB_DISABLE_PROGRESS_BARS when the value is Studio's default rather# than the operator's, so allow_progress_bars() can tell them apart._PROGRESS_BARS_DEFAULTED = "UNSLOTH_STUDIO_PROGRESS_BARS_DEFAULTED"@@ -391,7 +556,8 @@ class LogConfig:},},(- structlog.processors.JSONRenderer(sort_keys = False) # Preserve order+ # Preserve order; the wrapper adds the human-readable traceback copy.+ with_readable_traceback(structlog.processors.JSONRenderer(sort_keys = False))if env == "production"else structlog.dev.ConsoleRenderer()),diff --git a/studio/backend/tests/test_readable_traceback_logging.py b/studio/backend/tests/test_readable_traceback_logging.pynew file mode 100644index 00000000000..f520e9658fe--- /dev/null+++ b/studio/backend/tests/test_readable_traceback_logging.py@@ -0,0 +1,253 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A traceback in the log file has to be readable as a traceback.++~/.unsloth/studio/logs is a tee of stdout and stdout is JSON, so every stack trace reached+the reader as one line with its newlines escaped to ``\\n`` -- as the reported Image+Transform failure did. ``with_readable_traceback`` echoes the traceback under the record;+the JSON line itself must survive byte-for-byte for anything parsing the file.+"""++from __future__ import annotations++import json+import sys+from pathlib import Path++_BACKEND = Path(__file__).resolve().parent.parent+if str(_BACKEND) not in sys.path:+ sys.path.insert(0, str(_BACKEND))++from loggers import config as log_config # noqa: E402++_TRACEBACK = (+ "Traceback (most recent call last):\n"+ ' File "/studio/backend/core/inference/diffusion.py", line 5240, in generate\n'+ " image_latents = self.vae.encode(image)\n"+ "RuntimeError: Input type (float) and bias type (c10::BFloat16) should be the same"+)+++def _json_renderer():+ import structlog+ return log_config.with_readable_traceback(structlog.processors.JSONRenderer(sort_keys = False))+++def _render(event_dict):+ return _json_renderer()(None, "error", event_dict)+++def test_record_without_an_exception_is_a_single_json_line():+ out = _render({"event": "loaded", "level": "info"})+ assert "\n" not in out+ assert json.loads(out)["event"] == "loaded"+++def test_traceback_is_echoed_as_real_lines_after_the_record():+ out = _render({"event": "request_failed", "exception": _TRACEBACK})+ first, _, rest = out.partition("\n")+ # The record is untouched, so a record-by-record reader sees what it saw before.+ record = json.loads(first)+ assert record["exception"] == _TRACEBACK+ # ...and the readable copy follows as real lines, each behind a prefix so it cannot+ # read as a record of its own.+ prefix = log_config._TRACEBACK_ECHO_PREFIX+ lines = rest.splitlines()+ assert [line.removeprefix(prefix) for line in lines] == _TRACEBACK.splitlines()+ assert lines[0] == f"{prefix}Traceback (most recent call last):"+ assert lines[-1].startswith(f"{prefix}RuntimeError: Input type (float)")+++def test_echo_can_be_turned_off(monkeypatch):+ monkeypatch.setenv("UNSLOTH_STUDIO_PLAIN_TRACEBACKS", "0")+ out = _render({"event": "request_failed", "exception": _TRACEBACK})+ assert "\n" not in out+ assert json.loads(out)["exception"] == _TRACEBACK+++def test_blank_and_non_string_exceptions_are_not_echoed():+ for value in ("", " \n", None, 17):+ out = _render({"event": "e", "exception": value})+ assert "\n" not in out, value+++def test_console_renderer_is_left_alone(monkeypatch):+ # Development already prints tracebacks as tracebacks; wrapping it would double them.+ import inspect++ source = inspect.getsource(log_config.LogConfig.setup_logging)+ assert "with_readable_traceback(structlog.processors.JSONRenderer" in source+ assert "with_readable_traceback(structlog.dev.ConsoleRenderer" not in source+++def test_echoed_copy_is_the_redacted_truncated_one():+ # The wrapper reads event_dict["exception"] AFTER filter_sensitive_data and truncation,+ # so no secret re-enters the log and no 2 MB traceback is echoed whole.+ huge = "HEAD" + ("x" * 4_000_000) + "\nValueError: nope"+ capped = log_config.truncate_exception({"exception": huge})["exception"]+ out = _render({"event": "request_failed", "exception": capped})+ assert len(out) < 2 * (log_config._MAX_EXC_CHARS + 500)+ assert out.endswith("ValueError: nope")+++def test_a_control_heavy_traceback_cannot_outgrow_the_cap_by_escaping():+ # truncate_exception bounds the FIELD; escaping then multiplies it six-fold per C0+ # control, so 16 KiB of bounded exception used to leave as 98 KiB of echo.+ payload = "HEAD\n" + ("\x1b" * 200) + "\n" + "\n".join("\x00" * 400 for _ in range(400))+ capped = log_config.truncate_exception({"exception": payload + "\nValueError: nope"})[+ "exception"+ ]+ out = _render({"event": "request_failed", "exception": capped})+ _, _, echoed = out.partition("\n")+ assert len(echoed) <= log_config._MAX_EXC_CHARS + 200+ # The notice is prefixed like every other line.+ for line in echoed.split("\n"):+ assert line.startswith("| "), line+ assert "lines omitted" in echoed+ assert echoed.endswith("ValueError: nope")+++def test_an_uncapped_traceback_is_echoed_whole():+ # The cap only engages past the budget. A normal traceback keeps every frame.+ body = "\n".join(f' File "f{i}.py", line {i}, in fn' for i in range(20))+ out = _render({"event": "request_failed", "exception": f"Traceback:\n{body}\nValueError: nope"})+ _, _, echoed = out.partition("\n")+ assert "lines omitted" not in echoed+ assert echoed.count("\n") == 21+++def test_an_exception_message_cannot_forge_a_log_record():+ # CWE-117. Exception messages carry request-derived text, so a message holding a+ # newline plus a JSON object is reachable and every echoed line must be one+ # json.loads() REJECTS. RFC 8259 lets a parser skip leading whitespace, so indenting+ # would not be enough: ' {"a": 1}' parses.+ forged = json.dumps({"level": "info", "event": "admin_login", "user": "attacker"})+ out = _render(+ {+ "event": "request_failed",+ "exception": f"Traceback (most recent call last):\nValueError: bad prompt: \n{forged}",+ }+ )+ head, _, echoed = out.partition("\n")+ json.loads(head) # the real record still parses, unchanged+ for line in echoed.split("\n"):+ assert not line[:1].isspace(), line+ try:+ json.loads(line)+ except json.JSONDecodeError:+ continue+ raise AssertionError(f"echoed line parses as a record: {line!r}")+++def test_every_echoed_line_carries_the_prefix_including_exotic_separators():+ # splitlines() also breaks on \r, \x0b, \x0c, \x85 and U+2028/9, so a message cannot+ # smuggle an unprefixed line in on a separator the echo did not rejoin.+ exception = 'Traceback:\r\n frame\rValueError: x\u2028{"event": "fake"}'+ echoed = _render({"event": "e", "exception": exception}).split("\n")[1:]+ assert echoed+ assert all(line.startswith(log_config._TRACEBACK_ECHO_PREFIX) for line in echoed)+ assert not any("\r" in line for line in echoed)+++def test_a_lone_surrogate_cannot_break_the_log_write():+ # json.loads('"\ud800"') yields a lone surrogate, so a request body can put one in an+ # exception message. Printed raw it raises UnicodeEncodeError on a UTF-8 stdout, losing+ # the traceback and replacing the original exception with the encoding error.+ import io++ surrogate = json.loads('"\\ud800"')+ out = _render({"event": "request_failed", "exception": f"ValueError: bad prompt: {surrogate}"})+ assert surrogate not in out+ assert "\\ud800" in out+ # The real test: a strict UTF-8 stream, which is what PrintLogger writes to.+ stream = io.TextIOWrapper(io.BytesIO(), encoding = "utf-8")+ print(out, file = stream) # must not raise+ out.encode("utf-8")+++def test_terminal_controls_are_neutralised():+ # Raw ESC would let request-derived text rewrite what the reader sees, and a backspace+ # run would rub out the prefix that stops record forgery.+ exception = "ValueError: \x1b[2Jcleared\x08\x08\x08\x7f and \x9b more"+ out = _render({"event": "request_failed", "exception": exception})+ _, _, echoed = out.partition("\n")+ for raw in ("\x1b", "\x08", "\x7f", "\x9b"):+ assert raw not in echoed+ assert "\\u001b" in echoed and "\\u0008" in echoed+ assert echoed.startswith(log_config._TRACEBACK_ECHO_PREFIX)+++def test_bidi_controls_cannot_reorder_the_echoed_line():+ # UAX #9 / UTR #36, the Trojan Source class (CVE-2021-42574). json.dumps escapes these,+ # so the echo is the only place a raw one reaches a viewer. Measured with python-bidi,+ # "| ValueError: rejected upload gnp.eliforp/sdaolpu/" DISPLAYS as+ # "| ValueError: rejected upload /uploads/profile.png".+ exception = "ValueError: rejected upload gnp.eliforp/sdaolpu/"+ echoed = _render({"event": "request_failed", "exception": exception}).partition("\n")[2]+ assert "" not in echoed+ assert "\\u202e" in echoed+ # The whole set, not just the override: an unterminated isolate reorders a line too.+ exotic = "ValueError: " + "".join(sorted(log_config._BIDI_CONTROLS))+ echoed = _render({"event": "e", "exception": exotic}).partition("\n")[2]+ for ch in log_config._BIDI_CONTROLS:+ assert ch not in echoed+ assert f"\\u{ord(ch):04x}" in echoed+++def test_the_escaped_set_is_exactly_unicodes_bidi_controls():+ # Pinned to PropList.txt's Bidi_Control so the set cannot widen into all of category Cf+ # (escaping ZWJ / ZWNJ / soft hyphen out of legitimate text) nor narrow to U+202E.+ assert log_config._BIDI_CONTROLS == frozenset(+ chr(c)+ for c in (+ 0x061C,+ 0x200E,+ 0x200F,+ 0x202A,+ 0x202B,+ 0x202C,+ 0x202D,+ 0x202E,+ 0x2066,+ 0x2067,+ 0x2068,+ 0x2069,+ )+ )+++def test_zero_width_and_joining_characters_stay_readable():+ # Cf, but they reorder nothing: ZWNJ carries meaning in Persian/Arabic, ZWJ builds+ # emoji sequences.+ exception = "ValueError: بینام and \U0001f469\U0001f4bb"+ echoed = _render({"event": "e", "exception": exception}).partition("\n")[2]+ assert "" in echoed and "" in echoed+++def test_ordinary_text_is_left_readable():+ # Non-English text must not become hex soup, and a tab cannot move the cursor or erase.+ exception = "ValueError: 中文 café — tab:\there"+ echoed = _render({"event": "e", "exception": exception}).partition("\n")[2]+ assert "中文" in echoed and "café" in echoed and "—" in echoed+ assert "\there" in echoed+++def test_the_exception_line_survives_a_cap_that_cannot_fit_it():+ # A control-heavy message is what makes the last line too big for the tail budget, and+ # dropping it whole left the reader every frame and no reason.+ frames = "\n".join(f' File "/app/x{i}.py", line {i}, in fn' for i in range(60))+ payload = (+ "Traceback (most recent call last):\n"+ + frames+ + "\nValueError: rejected upload "+ + ("\x00" * 3000)+ )+ capped = log_config.truncate_exception({"exception": payload})["exception"]+ out = _render({"event": "request_failed", "exception": capped})+ _, _, echoed = out.partition("\n")+ lines = echoed.split("\n")+ assert lines[-1].startswith("| ValueError: rejected upload ")+ assert len(echoed) <= log_config._MAX_EXC_CHARS + 200+ for line in lines:+ assert line.startswith("| "), line