Skip to content

agent_session: add SessionClosed event - #1705

Closed
u9g wants to merge 2 commits into
mainfrom
agent-session-closed-event
Closed

agent_session: add SessionClosed event#1705
u9g wants to merge 2 commits into
mainfrom
agent-session-closed-event

Conversation

@u9g

@u9g u9g commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Problem

The agent side of the session protocol can only emit observations (AgentSessionEvent) and answer requests. It has no way to say the call is over.

That leaves a driver unable to distinguish three states that look identical from outside:

  • the agent hung up deliberately
  • the agent crashed
  • the agent is still thinking, or TTS is still flushing during a graceful drain

There is no transport-level fallback for the room case either. RoomSessionTransport iterates a channel that only closes when the local side closes it, so when the agent leaves the room the driver's channel simply goes quiet — no EOF. (TCP does surface EOF.) Detecting the hang-up means watching RTC participant events out of band, separately from the session protocol.

The gap shows up concretely in text-driven simulations. RunInputResponse carries items or an error and nothing else, so a turn arriving after the agent hung up is reported as a failed request rather than as a call that ended.

Change

Adds AgentSessionEvent.SessionClosed, carrying a SessionCloseReason that mirrors the CloseReason both SDKs already have (livekit/agents voice/events.py, livekit/agents-js voice/events.ts), plus an optional error string for the error close.

Additive: new enum, new nested message, new oneof field 24. No existing field changes.

Note: SC_TASK_COMPLETED exists in the Python CloseReason but not in the agents-js one — encoding the union here, but flagging it as an existing parity gap rather than something this PR resolves.

Follow-ups

  • livekit/agents and livekit/agents-js: emit it from the session close path.
  • Simulation driver: treat it as a call ended by the agent rather than an unanswered turn.

u9g and others added 2 commits August 6, 2026 09:44
The agent side of the session protocol can only emit observations and answer
requests; it has no way to report that the call is over. A driver reading a
closed session sees the agent go quiet, which is indistinguishable from it
stalling or crashing, and on the room transport there is no EOF to fall back
on - the byte-stream channel only closes locally.

This matters for an agent that hangs up on its own, which is ordinary in
telephony and increasingly common in voice agents that end a call after a
farewell.
@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3ca621f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
github.com/livekit/protocol Patch
@livekit/protocol Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@u9g u9g closed this Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant