Skip to content

symfonic.capabilities.tools.results.service

service

The result-policy service — applying decisions to a transcript (T3.1.3).

Four invariants this service exists to hold, each one previously a comment in the middle of a react-node helper:

  • the call id is preserved verbatim. A rewritten result whose id changed is an immediate provider 400; a result with no id at all is never rewritten, because there is no id to preserve.
  • the tool_use side is never touched. Editing it is also a 400, and it sits inside the cached prefix — rewriting it shifts bytes and busts the cache for everything after it.
  • nothing is mutated. The input list and its messages are left alone, so the checkpoint stays lossless and only the wire view is compact.
  • stubs are deterministic. They come from the ledger handle, so a compacted prefix is byte-identical across iterations.

The transcript adapter is injected: constructing a provider message type is wire-format work, and a capability may not import one.

ResultPolicyService

ResultPolicyService(policy: ResultPolicy, *, ledger: Any, transcript: Transcript)

Rewrite the wire view of a transcript according to the policy.

Source code in src/symfonic/capabilities/tools/results/service.py
def __init__(
    self, policy: ResultPolicy, *, ledger: Any, transcript: Transcript,
) -> None:
    self._policy = policy
    self._ledger = ledger
    self._transcript = transcript
    self._last: tuple[ResultRecord, ...] = ()

last_decisions property

last_decisions: tuple[ResultRecord, ...]

What the most recent :meth:apply decided, per result.

apply async

apply(messages: Sequence[Any]) -> list[Any]

Return the wire view of messages.

Returns the argument itself (identity, not a copy) when nothing can change — which is every iteration of every deployment that leaves compaction off.

Source code in src/symfonic/capabilities/tools/results/service.py
async def apply(self, messages: Sequence[Any]) -> list[Any]:
    """Return the wire view of ``messages``.

    Returns the argument itself (identity, not a copy) when nothing
    can change — which is every iteration of every deployment that
    leaves compaction off.
    """
    self._last = ()
    settings = self._policy.settings
    if not settings.enabled:
        return list(messages) if not isinstance(messages, list) else messages

    transcript = self._transcript
    result_indices = [
        i for i, m in enumerate(messages) if transcript.is_tool_result(m)
    ]
    if not result_indices:
        return list(messages) if not isinstance(messages, list) else messages

    policy = self._policy
    if settings.offload_enabled and settings.turn_boundary_index is None:
        policy = policy.for_settings(
            settings.with_turn_boundary(self._derive_boundary(messages))
        )

    protected = set(
        result_indices[-settings.keep_last_n :] if settings.keep_last_n > 0 else []
    )
    call_meta = self._call_metadata(messages)

    rewritten: list[Any] = []
    records: list[ResultRecord] = []
    for index, message in enumerate(messages):
        if not transcript.is_tool_result(message):
            rewritten.append(message)
            continue
        content = transcript.content_text(message)
        decision = policy.decide(
            index=index,
            size_chars=len(content),
            protected=index in protected,
            viewable=transcript.is_viewable(message),
        )
        call_id = transcript.call_id(message)
        # The result message's own ``name`` wins when LangChain
        # populated it; otherwise the name comes from the call that
        # asked. Neither present means the pair is unattributable,
        # and the placeholder says so rather than reading as unnamed.
        name = (
            transcript.tool_name(message)
            or call_meta.get(call_id, ("<unknown_tool>", None))[0]
        )
        if not decision.rewrites or not call_id:
            rewritten.append(message)
            records.append(
                ResultRecord(call_id, name, len(content), decision, applied=False)
            )
            continue
        stub = await self._stub_for(call_id, name, call_meta, content)
        if stub is None:
            rewritten.append(message)
            records.append(
                ResultRecord(call_id, name, len(content), decision, applied=False)
            )
            continue
        rewritten.append(transcript.restub(message, stub, name))
        records.append(
            ResultRecord(call_id, name, len(content), decision, applied=True)
        )
    self._last = tuple(records)
    return rewritten

Transcript

Bases: Protocol

The narrow slice of a message transcript the service reads.