Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

7.4 KB
Raw Blame
r"""Protocol-label parsing for streaming LLM responses.

The agentic engine drives LLM calls with a ``\`\`LABEL\`\`+content`` protocol:
prompts require one allowed label, double-backtick-wrapped, on the first line
of every reply, then the rest of the content. The parser detects that label
up front, tolerates a few provider/model formatting slips, and routes the
post-label stream accordingly.

Label sets are caller-supplied: the research block loop uses
``(THINK, TOOL, APPEND, FINISH)``, its report sub-phases take a single
terminal label each (``(OUTLINE,)``, ``(INTRO,)``, ``(SECTION,)``, ...), and
the question and PageIndex loops use ``(THINK, TOOL, FINISH)``.

Chat is **not** one of those callers, and neither is anything else running on
the chat loop (mastery, reading, ...). Chat carried this protocol until
``46093e5e`` (2026-06-11) and now drives its rounds through native tool
calling instead: a round that carries ``tool_calls`` is a tool round, a round
without them finishes the turn, and no label is ever asked of the model.
Reasoning the model writes into the *content* channel is recognised there
only when tagged ``<think>``, which ``InlineThinkFilter`` in
``deeptutor.agents.loop.agent_loop`` splits off at streaming time.
"""

from __future__ import annotations

import re

LABEL_UNKNOWN = "UNKNOWN"
LABEL_PROBE_MAX_CHARS = 64

_INVISIBLE_PREFIX_CHARS = "​‌‍"
_LABEL_SEPARATOR_CHARS = "\n\r \t::-–—"


def strip_label_probe_prefix(buffer: str) -> str:
    """Trim leading whitespace and zero-width chars before label probing."""
    stripped = str(buffer or "")
    previous = None
    while stripped != previous:
        previous = stripped
        stripped = stripped.lstrip().lstrip(_INVISIBLE_PREFIX_CHARS)
    return stripped


def classify_label(
    buffer: str,
    *,
    allowed_labels: tuple[str, ...],
    final: bool = False,
) -> tuple[str, str] | None:
    r"""Inspect a content buffer for a leading ``\`\`LABEL\`\``` prefix.

    Returns ``(label, after_text)`` once an allowed label is detected after any
    leading whitespace — caller routes ``after_text`` and all subsequent chunks
    accordingly.

    Returns ``None`` while the buffer is too short or still a partial prefix
    match. The caller keeps buffering and tries again on the next chunk, and
    must fall back to :data:`LABEL_UNKNOWN` once the buffer exceeds
    :data:`LABEL_PROBE_MAX_CHARS` without a match.

    Accepts the wrapped form (``\`\`LABEL\`\``` preferred, with common
    one-/three-backtick variants tolerated) and a bare fallback (``LABEL``
    followed by a separator) — some models drop or alter the backticks on
    one-shot prompts and the bare form is unambiguous as long as the
    protocol labels are all-uppercase tokens. The wrapped form may be
    followed immediately by body text because Markdown inline-code styling
    visually separates the label even when the raw stream has no whitespace
    (for example ``\`\`FINISH\`\`你好``).

    ``final=True`` means the caller knows no more chunks are coming, so an
    exact bare label such as ``FINISH`` can be accepted even without a
    trailing separator.
    """
    stripped = strip_label_probe_prefix(buffer)
    for label in allowed_labels:
        wrapped = re.match(
            rf"^(?P<ticks>`+)\s*{re.escape(label)}\s*(?P=ticks)(?P<after>.*)$",
            stripped,
            flags=re.DOTALL,
        )
        if wrapped is not None:
            after = wrapped.group("after")
            if after and after[0] == "`":
                # Avoid accepting an over-closed / still-streaming wrapper
                # such as ``FINISH``` and leaking the extra backtick into
                # the routed body. A non-backtick tail is real body text,
                # even when the model forgot the separator after the label.
                continue
            # Eat the separating newline / spaces / punctuation after the
            # label so the body / reasoning text doesn't start with stray
            # blank lines or a locale-specific colon.
            return label, after.lstrip(_LABEL_SEPARATOR_CHARS)
        # Bare-label fallback: only when the label is followed by a clear
        # separator so we don't false-positive on a body that happens to
        # start with a token like ``FINISHED``. An empty tail (label
        # exactly matches buffer) is ambiguous while streaming — keep
        # buffering until the next chunk reveals a separator or a
        # continuation char. At stream end (``final=True``), accept it.
        if stripped.startswith(label):
            tail = stripped[len(label) :]
            if tail and tail[0] in _LABEL_SEPARATOR_CHARS:
                return label, tail.lstrip(_LABEL_SEPARATOR_CHARS)
            if final and not tail:
                return label, ""
    return None


def recover_finished_label(
    buffer: str,
    *,
    allowed_labels: tuple[str, ...],
) -> tuple[str, str] | None:
    r"""Re-read a *finished* reply for a recoverable protocol-label slip.

    The streaming probe (:func:`classify_label` without ``final=True``) must
    stay strict: an unclosed ``\`\`\`SECTION`` fence or an over-wide closer
    such as ``\`\`FINISH\`\`\``` cannot be resolved while another chunk may
    still arrive. Once the caller knows the stream is complete, accept those
    fences around an *exact* allowed label and return ``(label, after_text)``.

    Does not invent a label from body structure — report heading recovery
    belongs to the report writer, which knows the section number it asked for.
    """
    parsed = classify_label(buffer, allowed_labels=allowed_labels, final=True)
    if parsed is not None:
        return parsed

    stripped = strip_label_probe_prefix(buffer)
    boundary = "`" + _LABEL_SEPARATOR_CHARS
    for label in allowed_labels:
        match = re.match(
            rf"^(?P<ticks>`+)\s*{re.escape(label)}(?P<after>.*)$",
            stripped,
            flags=re.DOTALL,
        )
        if match is None:
            continue
        after = match.group("after")
        # ``SECTIONAL`` must not match ``SECTION``; require end, a separator,
        # or a backtick immediately after the token.
        if after and after[0] not in boundary:
            continue
        # Drop a same-line closing fence of any length (unclosed = none;
        # over-wide / mismatched = extra ticks). A code fence on the *next*
        # line is body text and must stay.
        after = re.sub(r"^[ \t]*`+", "", after, count=1)
        return label, after.lstrip(_LABEL_SEPARATOR_CHARS)
    return None


def find_inline_labels(text: str, *, allowed_labels: tuple[str, ...]) -> list[str]:
    """Return labels that appear inside post-label body text.

    The protocol requires exactly one label per reply (on the first line).
    A second label found at the start of a later body line is a violation
    worth flagging. Mentions inside prose such as "next I should use
    ``TOOL``" are not action labels and must not trigger repair loops.
    """
    if not allowed_labels:
        return []
    pattern = "|".join(re.escape(label) for label in allowed_labels)
    raw = str(text or "")
    separators = re.escape(_LABEL_SEPARATOR_CHARS)
    wrapped = [
        match.group("label")
        for match in re.finditer(
            rf"(?m)^[^\S\r\n]*(?P<ticks>`+)\s*(?P<label>{pattern})\s*(?P=ticks)(?=$|[{separators}])",
            raw,
        )
    ]
    bare = re.findall(rf"(?m)^[^\S\r\n]*({pattern})(?=$|[{separators}])", raw)
    return [*wrapped, *bare]