Ctrl K
ComunidadGuías…

tests/test_agent_loop_tool_output_truncation.py

Rama dev · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
"""Tool-output display truncation uses _truncate with an indicator.

Previously agent_loop sliced tool output to a hard character limit ([:2000]
or [:4000]) with no signal to the UI that data was lost.  Now it delegates to
tool_utils._truncate which caps at MAX_OUTPUT_CHARS (10 000) and appends
a ``... (truncated, N chars total)`` suffix so the frontend can show a
truncation indicator in the tool bubble.
"""
from src.tool_utils import _truncate, MAX_OUTPUT_CHARS
from src.tool_execution import format_tool_result


def test_short_output_unchanged():
    """Outputs within the limit pass through verbatim."""
    text = "hello world"
    assert _truncate(text) == text


def test_long_output_truncated_with_indicator():
    """Outputs exceeding MAX_OUTPUT_CHARS are truncated with a suffix."""
    text = "x" * (MAX_OUTPUT_CHARS + 500)
    result = _truncate(text)
    assert len(result) > MAX_OUTPUT_CHARS  # includes suffix
    assert result.startswith("x" * MAX_OUTPUT_CHARS)
    assert "truncated" in result
    assert str(len(text)) in result  # original length reported


def test_exact_limit_unchanged():
    """An output exactly at the limit is not truncated."""
    text = "a" * MAX_OUTPUT_CHARS
    assert _truncate(text) == text


def test_default_limit_matches_constant():
    """_truncate default limit equals MAX_OUTPUT_CHARS (10 000)."""
    assert MAX_OUTPUT_CHARS == 10_000
    text = "y" * 10_001
    result = _truncate(text)
    assert "truncated" in result


def test_empty_string():
    assert _truncate("") == ""


def test_external_bridge_result_is_bounded_before_model_replay():
    text = "package output\n" * MAX_OUTPUT_CHARS

    rendered = format_tool_result(
        "bash: install dependencies",
        {"output": text, "exit_code": 0},
    )

    assert len(rendered) < MAX_OUTPUT_CHARS + 100
    assert "truncated" in rendered


def test_format_tool_result_does_not_serialize_image_payloads_as_json():
    rendered = format_tool_result(
        "read media",
        {
            "output": "frames extracted",
            "exit_code": 0,
            "images": [{"mimeType": "image/png", "data": "base64-frame-data"}],
        },
    )

    assert "frames extracted" in rendered
    assert "base64-frame-data" not in rendered