Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(ship): strip control bytes in _fail before escaping markup (#1872)

Alexisc476f5d07624
diff --git a/changelog.d/0.76.0/1872.fixed.md b/changelog.d/0.76.0/1872.fixed.md
new file mode 100644
index 00000000..2ddf9a37
--- /dev/null
+++ b/changelog.d/0.76.0/1872.fixed.md
@@ -0,0 +1 @@
+- **`soup ship` error messages no longer pass control bytes to the terminal (#1869 by @bernalalexis-try in #1872).** `_fail` escaped Rich markup but let ESC and C1 bytes through, including in exception text forwarded from rendering a Hub model's chat template. It now strips control characters before escaping.
diff --git a/src/soup_cli/commands/ship.py b/src/soup_cli/commands/ship.py
index 9695a0fd..4b3adbc8 100644
--- a/src/soup_cli/commands/ship.py
+++ b/src/soup_cli/commands/ship.py
@@ -126,7 +126,7 @@ _MAX_BENCHMARK_NAME_CHARS = 256
def _fail(message: str, code: int) -> NoReturn:
"""Print a friendly red error and raise ``typer.Exit(code)``."""
- console.print(f"[red]Error:[/] {escape(message)}")
+ console.print(f"[red]Error:[/] {escape(for_terminal(message))}")
raise typer.Exit(code=code)
diff --git a/tests/test_ship_fail_terminal.py b/tests/test_ship_fail_terminal.py
new file mode 100644
index 00000000..42cb14f1
--- /dev/null
+++ b/tests/test_ship_fail_terminal.py
@@ -0,0 +1,41 @@
+"""``ship._fail`` strips control bytes as well as escaping Rich markup (#1869).
+
+``_verdict_live`` forwards an exception raised while rendering a Hub model's chat
+template, which is text the user did not write. ``escape`` alone left a raw ESC
+or C1 byte to reach the terminal.
+"""
+
+from __future__ import annotations
+
+from io import StringIO
+
+import pytest
+import typer
+from rich.console import Console
+
+from soup_cli.commands import ship
+
+
+def test_fail_strips_control_bytes_and_keeps_markup_literal(monkeypatch):
+ out = StringIO()
+ monkeypatch.setattr(ship, "console", Console(file=out, color_system=None, width=200))
+
+ with pytest.raises(typer.Exit) as info:
+ ship._fail("a" + chr(0x1B) + "b" + chr(0x9B) + "c [red]d", 3)
+
+ assert info.value.exit_code == 3
+ text = out.getvalue()
+ assert chr(0x1B) not in text
+ assert chr(0x9B) not in text
+ assert text == "Error: abc [red]d\n"
+
+
+def test_fail_strips_before_escaping(monkeypatch):
+ """Strip THEN escape: a stripped ESC must not leave a live [tag] behind a backslash."""
+ out = StringIO()
+ monkeypatch.setattr(ship, "console", Console(file=out, color_system=None, width=200))
+
+ with pytest.raises(typer.Exit):
+ ship._fail("\\" + chr(0x1B) + "[red]x", 3)
+
+ assert out.getvalue() == "Error: \\[red]x\n"