Skip to content

fix(session-tab): KeepAlive race capture + optimistic user-message echo (#789) - #790

Merged
vybe merged 1 commit into
devfrom
fix/789-session-tab-keepalive-race
May 11, 2026
Merged

vybe merged 1 commit into
devfrom
fix/789-session-tab-keepalive-race

Conversation

@webmixgamer

Copy link
Copy Markdown
Contributor

Summary

Two follow-up fixes for #782's KeepAlive reattach work, both on the same SessionPanel code path.

  • Capture agentName and sid before the await in onActivated — fixes the TOCTOU race against Vue prop reactivity. Without it, polling captured the new agent's name with the previous session's id and 404'd ~60 times per incident against GET /api/agents/<new>/sessions/<old-sid> until MAX_ATTEMPTS hit ~22 min later. User-visible symptom: stuck spinner + blocked input after navigating away from a mid-turn session and back.
  • Restore optimistic user-message insert (without rollback) — fix(session-tab): reattach to in-flight turn on KeepAlive activation (#759) #782 dropped the optimistic insert on the assumption that turns are fast (the store comment said "~100-300 ms perceived latency"). For 30s+ Bash/sleep prompts the user stared at a bare spinner for the entire turn. The backend persists the user row at turn start, so an optimistic row + no rollback is canonical — loadSession on success overwrites with the same content (no flicker), and on error the row is intentionally kept (server-side failures occur after persist; network failures keep the user's text in view rather than silently disappearing).

Changes

  • src/frontend/src/components/SessionPanel.vue — capture pattern in onActivated.
  • src/frontend/src/stores/sessions.js — optimistic-insert in sendMessage; comment updated to describe the no-rollback rationale.
  • docs/memory/feature-flows/session-tab.md — line 51 corrected (the doc previously claimed "optimistic insert + rollback on failure" which was wrong both before and after fix(session-tab): reattach to in-flight turn on KeepAlive activation (#759) #782).
  • tests/unit/test_session_panel_race_capture.py — 4 new AST/regex guards (capture-before-await; no re-read of props.agentName after capture; optimistic-mutation-before-axios-post; [...existing, ...] spread pattern). Same convention as test_session_inflight_sentinel.py.

Test plan

  • pytest tests/unit/test_session_panel_race_capture.py -v — 4 passed locally.
  • Live repro on testfix + temporary testfix-2, with console.log instrumentation that captured agentName flipping from testfix to testfix-2 across the await boundary in the broken code.
  • Post-fix repro: no 404 polling errors in console, no stuck spinner on cross-agent navigation, user message echoes immediately on send, bug(session-tab): shows 'failed to send' on return navigation while execution is still running #759 cross-agent bleed scenario still works end-to-end.
  • Reviewer: try the recipe in the issue with the optimistic-insert restored (testfix → send long prompt → NavBar Agents → another agent → back; expect: user message visible immediately, spinner clears with assistant reply, no stuck states).

Fixes #789

🤖 Generated with Claude Code

… user-message echo

Two follow-up fixes for #782's KeepAlive reattach work, both on the
same SessionPanel code path. Filed and tested as #789.

1. TOCTOU race in onActivated (SessionPanel.vue)
   AgentDetail's route watcher calls loadAgent() async, so `agent.value`
   (and therefore `SessionPanel.props.agentName`) can update mid-await.
   Reading `props.agentName` after `await sessionsStore.loadSession(...)`
   paired the previous agent's session id with the new agent's name;
   startPolling captured the cross-agent pair and 404'd ~60 times per
   incident against `GET /api/agents/<new>/sessions/<old-sid>` until
   MAX_ATTEMPTS hit ~22 min later. Surface: stuck spinner + blocked
   input on the original agent after switching away and back.
   Fix: capture `agentName` and `sid` into locals before the await;
   pass the captured pair to startPolling.

2. Missing optimistic user-message insert (stores/sessions.js)
   PR #782 dropped the optimistic insert from sendMessage on the
   assumption that turns are fast ("~100-300 ms perceived latency").
   For a 30s+ Bash/sleep prompt the user stares at a bare spinner for
   the full turn duration. The Chat tab does this correctly at
   ChatPanel.vue:612.
   Fix: optimistic insert at the start of sendMessage without rollback
   on error. Backend persists the user row at turn start so loadSession
   on success overwrites the optimistic row with the canonical one
   (same content, no flicker). On error the row is intentionally kept
   in view — server-side failures occur after the user row is
   persisted, and for the rare network failure where the POST never
   reaches the server, keeping the row visible is still better than
   silent disappearance.

Tests: tests/unit/test_session_panel_race_capture.py — 4 AST/regex
guards pinning (a) `const agentName = props.agentName` captured before
first await in onActivated, (b) no re-read of props.agentName after
the capture, (c) `messagesBySession[sessionId] = [...]` precedes the
axios.post in sendMessage, (d) the optimistic insert spreads existing
messages rather than replacing. Same convention as
test_session_inflight_sentinel.py.

Docs: feature-flows/session-tab.md line 51 corrected — "optimistic
insert + rollback on failure" was wrong both before #782 (which
removed it entirely) and after #789 (which restores it without
rollback).

Verified live against testfix + testfix-2:
- 404 polling errors gone.
- Cross-agent stuck-spinner repro gone.
- User message echoes immediately on send.
- #759 cross-agent bleed scenario still works end-to-end.

Fixes #789

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@vybe
vybe merged commit 0c7c2c3 into dev May 11, 2026
12 checks passed
@webmixgamer
webmixgamer deleted the fix/789-session-tab-keepalive-race branch May 11, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority-p2 Important type-bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants