Skip to content

An in-flight turn is lost from the UI if you navigate away before the agent binds its session #31

Open

Description

@crocsarecool

Symptom

Send a message to a bot, then switch to another bot (or leave the page) while the turn is still running. Come back and the chat is empty — the "Start a conversation" empty state, no prompt, no reply, and the bot reads "Idle" in the sidebar. It looks like navigating away killed the run.

It is intermittent: leave a few seconds later and it reattaches correctly.

What it is not

The run is not killed. GET /events only unsubscribes its listener when the client disconnects (src/server.ts:394-396), and session cleanup is disabled (src/server-common.ts:347). I confirmed this: drop the SSE mid-run and the turn continues to completion, and a page reload afterwards shows the full answer. Nothing is lost on disk. What breaks is the client's ability to find the run again.

Root cause

The reattach path and the thread's history are both gated on thread.sdkSessionId, which is not set until the agent reports its session id — and that can take many seconds.

  1. Binding is late. For Claude Code the thread is bound when the SDK emits system/init (src/start-claude-code.ts:129-135 → bindSession, src/bot-store.ts:290). In a small repo that is ~2-6s; in a repo that loads MCP servers I measured 30s+ before sdkSessionId appeared. Codex binds on its own thread_id event (src/start-codex.ts:291), so it has the same window.

  2. Rejoin is skipped entirely when it is null. ui/app/components/chat.tsx:722 — if (thread.sdkSessionId) guards the whole rejoin block. Null means no /status, no /permissions, no /events. In the repro below the client issues zero session requests on return.

  3. History comes back empty too. src/bot-routes.ts:166 returns [] when sdkSessionId is null, so even the user's own just-sent message disappears. That is why it reads as "wiped" rather than "stalled".

  4. Nothing ever retries. The rejoin lives only in the [thread?.id] effect in chat.tsx, and refreshThreads runs only on bot switch or turn end (ui/app/components/app-shell.tsx:358,708). Sitting on the thread, nothing polls — the view stayed blank for 60s in my test, well past the point where the turn had already finished. Recovery requires switching away and back again, or a reload.

The reason it is intermittent: switching bots refetches the thread list, so if binding landed while you were away, you recover. If you return inside the binding window, you do not.

Reproduction

Deterministic against @gitbot-hq/[email protected] (Playwright, isolated GITBOT_DATA_DIR):

  1. Create a claude-code bot and a fresh thread (no prior turn, so sdkSessionId is null).
  2. Send hi.
  3. After ~800ms, click another bot in the sidebar.
  4. Return to the first bot ~4s later.

Result: empty chat, bot shows "Idle", and it stays that way indefinitely while the agent runs to completion in the background. Reloading the page afterwards shows the whole conversation intact.

Timing from the run (send at t=0):

t+0.0s   POST /chat            -> {"sessionId":"c56a49a1-..."}
t+0.0s   GET  /events?sessionId=c56a49a1-...
t+0.8s   [switch to other bot]
t+4.9s   [switch back]         -> GET /threads  (sdkSessionId still null)
                                  no /status, no /events — rejoin skipped
t+6.5s   server binds thread   -> sdkSessionId=b803fdf2-...   (1s too late)
t+6..66s view stays "Start a conversation"; turn completes unseen

Second bug, same root cause

src/server.ts:259 derives existingId from thread.sdkSessionId, and the "one turn per thread" guard (src/server.ts:306-310) only fires if that resolves to an existing store. During the binding window it resolves to nothing, so the guard is bypassed. Two POST /chat calls 1s apart on one thread gave me two concurrent agent runs with separate sessions:

first  -> {"sessionId":"c289ddab-ebaa-468c-a6c2-9ce9924cccab"}
second -> {"sessionId":"33b9c976-79d5-41ce-9e9f-e46984dc0e27"}

Both race to bindSession (first writer wins, bot-store.ts:292), and the loser's work is orphaned in a transcript the thread will never point at. This is reachable from the UI: the blank chat invites you to retype your message.

Proposed fix

The rejoin endpoints do not actually need the SDK's id — /events, /status, /permissions, /config and /abort all resolve a session by either gitbotId or sdkSessionId (src/server.ts:366-367 and friends). And gitbotId exists the instant POST /chat returns. So the missing handle is available from the first moment; it just is not exposed on the thread.

Suggested approach, smallest version that also fixes the concurrency hole:

  1. Derive a live session id per thread rather than persisting one. sessions is exported (src/server-common.ts:259) and every store carries threadId (:242). Have GET /threads and GET /threads/:id include activeSessionId = the gitbotId of the running session for that thread, if any. No change to threads.json, nothing to clean up, and it is correct after a server restart (no session, no id).
  2. Client rejoins on activeSessionId ?? sdkSessionId in chat.tsx:722. This covers both reported paths, including a full page reload, since the truth lives in server memory rather than client state.
  3. Close the guard hole in src/server.ts by looking up a running session by threadId before the existingId lookup, so a second POST /chat during the binding window 409s like it does everywhere else.

Deliberately kept out of scope above, but worth deciding on:

  • History while unbound. bot-routes.ts:166 can keep returning [] — once rejoin works the live stream carries the turn. But note the client currently discards replayed assistant/tool_use events during catch-up (chat.tsx:980,986) on the assumption history already has them, which is only true once the transcript is on disk. If rejoin is fixed without touching this, a mid-turn rejoin on an unbound thread will show activity but not the text already produced.
  • Alternative, client-only: have app-shell remember the sessionId that postChat returned, keyed by thread. Smaller diff, but it dies on page reload, so it only fixes half of the report.

Happy to put up a PR for whichever shape you prefer — or if the binding contract should change instead (bind the thread at POST /chat time with the gitbot id, and treat the SDK id purely as a transcript pointer), that is arguably the cleaner model and I am glad to do it that way.

Notes

  • Verified on @gitbot-hq/[email protected], macOS, claude-code agent.
  • Only tested with claude-code. Codex binds on thread_id (start-codex.ts:291) so it should have the same window; OpenCode binds in start-opencode.ts:157-164 and I could not test it (CLI not installed here).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions