Skip to content

Answer from the notch card can be delivered to the wrong session when several sessions are waiting #308

Closed

Description

@halindrome

Summary

With several sessions waiting at once, answering the question/permission card in the notch sometimes delivers the answer to a different session than the one the card was showing. I hit this with two Claude Code sessions running in sibling directories (gitops-ansible and liverpool-cleanup): the card showed one project, I picked an option (and typed custom text), and the other session received the answer.

It is intermittent and I have not found a deterministic repro, but the code has a structural race that explains it.

Root cause (as far as I can tell)

The card's identity and the card's answer target come from two different places, and nothing ties them together:

  • NotchPanelView renders case .questionCard(let sid) — the project name / cwd / mascot shown to the user come from appState.sessions[sid].
  • The question text and options come from appState.pendingQuestion, i.e. questionQueue.first.
  • AppState.answerQuestion(_:) / answerQuestionMulti(_:) / skipQuestion() (and likewise approvePermission / denyPermission on permissionQueue) do an unconditional queue.removeFirst() and resume that continuation.

So the surface's sid is never compared against the head of the queue at the moment of submission. Any mutation of the queue head between "card rendered" and "user clicks" silently re-targets the answer. Ways the head can change while the card is up:

  • handlePeerDisconnect(sessionId:) → drainQuestions/drainPermissions(forSession:) removes the head entry when that session's socket drops (e.g. the user answered in the terminal, or the CLI exited). showNextPending() then only re-points surface if shouldAutoOpenQuestionSurface / shouldAutoOpenPendingSurface allows it — Smart Suppress can decline, leaving the stale .questionCard(sid) on screen with a new head behind it.
  • showNextPending() reorders permissionQueue (remove(at: idx) + insert(at: 0)), and the tool-use cache path removes entries by index.
  • A multi-question AskUserQuestion wizard collects answers over several seconds of user interaction; the queue head can be drained mid-wizard, after which answerQuestionMulti posts the collected answers to whatever is now first.

The mismatch is not just "wrong recipient" — for AskUserQuestion the response is built as updatedInput from pending.event.toolInput, so answers can be zipped onto a different question's option list by position.

Suggested fix

Make the answer target explicit rather than positional. Two pieces:

  1. Carry an identity into the surface — .questionCard(requestId:) / .approvalCard(requestId:) (or at minimum pass sid + the request's toolUseId) — and have answerQuestion / approvePermission / denyPermission / skipQuestion take that id, look it up in the queue, and resume that entry instead of removeFirst().
  2. If the id is no longer in the queue (drained, disconnected, answered in terminal), drop the answer and re-render rather than falling through to the head. A visible "this prompt is no longer waiting" beats a silent misroute.

A cheaper stopgap that would catch most of it: in the answer path, guard pending.event.sessionId == surfaceSessionId else { return }.

Environment

  • CodeIsland 1.0.31, macOS 15.x (Darwin 25.5.0), Apple Silicon
  • Agent: Claude Code (2 concurrent sessions, separate terminal tabs, sibling repos)
  • Code references verified against main @ 9e3a1eb (v1.0.31) — the unconditional removeFirst() is still present there.
  • Searched open/closed issues and open PRs; I did not find a duplicate. 修复(OMP):终端原生 Ask 与 CodeIsland 并行回答 #264 touches the adjacent "terminal answered it first" path (OMP-specific, still open) but does not address which queue entry the card resolves.

Activity

  1. wxtsky commented on Aug 15, 2026

    @wxtsky
    Owner

    Merged as 5b4d9f7 on main. Answers now resolve the queued request belonging to the card's session instead of queue.removeFirst(), and the cards render pendingPermission(forSession:) / pendingQuestion(forSession:) so display and resolution can't disagree. The three review rounds on your fork were worth it — thanks for pinning each guard by mutating the production path it names.

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