[BUG] Web UI: a session can disappear from the list while the model is still thinking (ghost session)
Quick Summary
In the Web UI, a freshly created session can silently disappear from the session list while the model is still thinking, leaving what looks like a "ghost session": the chat you are looking at is no longer present in the history dropdown, and there is no way to get back to it. This report documents how sessions are currently listed/created and points at the two code paths that can produce the ghost.
Environment & Tools
- PicoClaw Version: v0.3.1 (git
2cf030d2) - Go Version: go1.25.11
- AI Model & Provider: any (the issue is in session listing/creation, not the model)
- Operating System: Linux amd64
- Channels: Web UI (
picochannel)
Steps to Reproduce
Intermittent, but the pattern is:
- Open the Web UI and start a new chat (or send the first message of a new session).
- The turn starts — the "thinking" indicator shows and the model is working.
- Open the session history dropdown (
SessionHistoryMenu) while the turn is still running, and/or let the turn finish/error. - The session you are currently in is missing from the list (or never appears), even though you are looking at its transcript. It looks like a ghost session.
❌ Actual Behavior
The active session is not present in GET /api/sessions, so the history dropdown does not show it. The user is stuck in a session that "does not exist" according to the list.
✅ Expected Behavior
Any session that is currently open in the UI must be listed in the history — including one whose first turn is still in progress or that ended in an error. A session should never be invisible while the user is actively reading/writing it.
Root-cause analysis (current session handling)
Verified against main (web/frontend + web/backend + pkg/session). Two independent mechanisms can hide a session:
1. Empty sessions are filtered out of the list.
web/backend/api/session.go → handleListSessions skips any session for which isEmptySession(sess) is true:
func isEmptySession(sess sessionFile) bool {
return len(sess.Messages) == 0 && strings.TrimSpace(sess.Summary) == ""
}A brand-new session whose first turn has not yet produced a persisted message (still thinking, or the turn failed before the assistant message was written) has zero messages on disk → it is dropped from the list. The frontend then shows the empty state (chat.noHistory) even though the user is inside that session. This is the most likely cause of the "ghost".
2. The list is only refreshed when the dropdown opens, and it races with in-progress writes.
web/frontend/src/components/chat/chat-page.tsx calls loadSessions(true) only on SessionHistoryMenu onOpenChange (i.e. when the user opens the dropdown). There is no refresh on message.send, on typing.stop, or when a turn completes. So if the list is fetched before the first message is flushed to disk (or while a write is in flight), the session is absent — and it stays absent until the user reopens the dropdown. Combined with (1), a session that is still empty at fetch time simply never shows.
Why the active session is not special-cased: the frontend generates the session id client-side (features/chat/state.ts → generateSessionId(), stored in localStorage as picoclaw:last-session-id) and opens the WebSocket with ?session_id=<uuid>. The backend has no notion of "the currently open session", so it cannot guarantee the active session is always listed.
Note on message counts: buildSessionListItem counts only visible messages (visibleSessionMessages, thoughts excluded). A session whose only content so far is a reasoning/thought block can therefore report message_count: 0 even though it is not technically empty — worth confirming whether that contributes to the perception of a ghost.
Proposed fix / diagnostics
- Backend: in
handleListSessions, do not drop the session that is currently active. Simplest robust option: stop filtering empty sessions out of the list entirely (or only hide them when they are also not the active session), and/or expose anis_emptyflag so the client can decide. Include sessions that only have a thought/tool-call message in the count. - Frontend: refresh the session list after
message.sendand ontyping.stop/turn completion, not only on dropdown open, so a just-created session appears as soon as it has content. Optionally optimistically insert the active session into the list. - Diagnostics wanted from maintainers: confirm the intended semantics of
isEmptySessionand whether a session with an in-flight first turn is expected to be listed. If the ghost is instead caused by the JSONL write not being visible to the list reader (a read/write race inpkg/session/jsonl_backend.go), that is a second, separate fix.
Additional Context
- Related prior issues (all auto-closed as
stale): #2972 (Web UI "message chaos", new session attached old history) and #2621 (session context lost, duplicate default session). Neither covers the "active session disappears from the list while thinking" case. - This is the same area as the session-list improvements requested in #3406; the ghost is a correctness bug that should be fixed before/alongside that UX work.
Source: sipeed/picoclaw