Tagger.add()/remove() can break the outcome invariant that success()/fail() maintain
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:
>>> 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:
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 failedThere 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:
- Keep the dict, enforce the invariant in the mutators. In
add(), route the reserved keys throughsuccess()/fail()(or refuse them), and inremove(), clear_outcome_reasonwhen the removed tag was the outcome tag. Smallest diff, no change to the read path. - Store the outcome in its own field.
_outcome: str | Noneplus_tagsfor custom tags only, and derive thelk.success/lk.failtag when building the report. This removes the possibility entirely, at the cost of touchingoutcome,tags,_evaluationand 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.
Source: livekit/agents