Skip to content

refactor(chat): decouple visible session from live socket runtime in AgentDetail#152

Open
Congregalis wants to merge 2 commits intodataelement:mainfrom
Congregalis:codex/followup-session-lifecycle
Open

refactor(chat): decouple visible session from live socket runtime in AgentDetail#152
Congregalis wants to merge 2 commits intodataelement:mainfrom
Congregalis:codex/followup-session-lifecycle

Conversation

@Congregalis
Copy link
Contributor

Summary

This PR is a follow-up to the session-switching bugfix work (#144 / #151).
It separates UI visibility from per-session chat runtime in AgentDetail.tsx, so switching sessions/agents no longer relies on a single global socket lifecycle.

Problem

AgentDetail previously treated:

  • the currently visible session
  • and the currently running conversation socket

as the same lifecycle.

That coupling made session switching fragile and caused issues such as:

  • stale responses leaking across sessions,
  • inconsistent streaming/waiting behavior on session transitions,
  • race conditions when rapidly switching sessions.

What changed

1) Per-session runtime management

  • Introduced runtime keying by agentId:sessionId.
  • Replaced single-socket assumptions with per-session socket/runtime tracking.

2) Session message load race protection

  • Added AbortController + sequence checks in selectSession().
  • Prevents earlier (stale) session-message requests from overwriting newer selection state.

3) Unified session-switch behavior

  • createNewSession() now reuses session selection flow instead of ad-hoc state resets.
  • Session deletion now also cleans corresponding runtime/socket state.

4) Active-session scoped send/stop actions

  • Send and abort now target the active session runtime socket explicitly.
  • Avoids sending/stopping on the wrong socket.

5) Preserve “thinking/waiting” state across switches

  • Added per-session UI state cache for isWaiting / isStreaming.
  • If user switches away and returns while response is still pending, the “thinking” indicator is restored.

Scope / Non-goals

This PR focuses on stabilizing in-page session/agent switching behavior in AgentDetail.
It does not introduce a full background execution/job model across page reloads.

Validation

Manual checks performed:

  1. Session A streaming -> switch to session/agent B -> switch back to A: messages remain intact.
  2. If A is still pending, returning to A restores waiting/streaming visual state.
  3. Rapid session switches do not allow stale message fetch responses to overwrite current selection.

Files changed

  • frontend/src/pages/AgentDetail.tsx

Checklist

  • Tested locally
  • No unrelated changes included

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