"""Bounded, agent-neutral terminal reply for the device (never a session history)."""
from .speech import redact_credentials

DEVICE_FINAL_MAX_BYTES = 2048


def device_final_text(value: str) -> str:
    # Redact before truncating so a credential crossing the boundary stays hidden.
    value = redact_credentials(value)
    value = ''.join(c for c in value.replace('\r\n', '\n') if c in '\n\t' or ord(c) >= 32)
    encoded = value.encode('utf-8')
    if len(encoded) <= DEVICE_FINAL_MAX_BYTES:
        return value
    return encoded[:DEVICE_FINAL_MAX_BYTES - 3].decode('utf-8', errors='ignore') + '…'
