#7486·agents

Tagger.add()/remove() can break the outcome invariant that success()/fail() maintain

Author: feiiiiii5Created Sep 26, 2026Updated Sep 26, 2026

Bug Description

Tagger keeps the session outcome inside the same tag dict that add() writes to, but only success() and fail() maintain the mutual exclusion. add() and remove() write the dict directly, so the two public paths into the reserved lk. namespace disagree about the outcome.

success() and fail() both start by popping the opposite outcome tag (observability.py:58, :69), and outcome then reads that dict back (:102-108). A tag written through add() skips that step:

python
>>> t = Tagger()
>>> t.fail(reason="booking failed")
>>> t.add("lk.success")
>>> sorted(t.tags)
['lk.fail', 'lk.success']
>>> t.outcome, t.outcome_reason
('success', 'booking failed')

outcome checks lk.success first, so the session is reported as a success while session.tags still carries lk.fail (telemetry/traces.py:1302) and the reason field still holds the failure text (traces.py:1363-1365). Because the severity is derived from outcome (:1372-1373), the job is also reported at SeverityNumber.UNSPECIFIED instead of ERROR, so a failed job is not flagged as an error in LiveKit Cloud.

remove() has the mirror problem: it clears the tag but not the reason, so success("done") followed by remove("lk.success") leaves outcome is None with outcome_reason == "done".

This is reachable rather than theoretical because add() is already an established writer into the reserved namespace: the framework itself calls ctx.tagger.add(f"lk.amd:{category}", ...) at voice/amd/detector.py:552, and _evaluation writes lk.judge.* through the same dict (:120-131). Nothing in add()'s docstring says lk.* is off limits, so application code following that pattern can hit it.

Expected Behavior

Either outcome tag is present at most once, and outcome_reason is only set while an outcome tag is. outcome should not be able to disagree with tags.

Reproduction Steps

No LiveKit Cloud account or running session is needed — Tagger is standalone:

python
import importlib.util, pathlib, sys

p = pathlib.Path("livekit/agents/observability.py")  # any checkout
spec = importlib.util.spec_from_file_location("obs", p)
m = importlib.util.module_from_spec(spec)
sys.modules["obs"] = m
spec.loader.exec_module(m)

t = m.Tagger()
t.fail(reason="booking failed")
t.add("lk.success")
print(sorted(t.tags), t.outcome, t.outcome_reason)
# ['lk.fail', 'lk.success'] success booking failed

There is no test covering the invariant — tests/test_recording.py only uses a mocked tagger.

Environment Details

Operating System: macOS Models Used: n/a (observability only, no model involved) Package Versions: livekit-agents at 57b3227a7 (main)

Proposed Solution

Two consistent options, and which one you prefer is your call:

  1. Keep the dict, enforce the invariant in the mutators. In add(), route the reserved keys through success() / fail() (or refuse them), and in remove(), clear _outcome_reason when the removed tag was the outcome tag. Smallest diff, no change to the read path.
  2. Store the outcome in its own field. _outcome: str | None plus _tags for custom tags only, and derive the lk.success / lk.fail tag when building the report. This removes the possibility entirely, at the cost of touching outcome, tags, _evaluation and the report builder.

I lean towards 1, since traces.py and the Cloud payload both read the tags, and 2 changes the shape of what gets uploaded.

Additional Context

voice/amd/detector.py:552 and _evaluation both write lk.* through add()-style paths, so option 1 also gives those a single place that owns the reserved keys.