Skip to content

fix(questions): stop background subagents denying the main thread's question (#340) - #345

Merged
wxtsky merged 1 commit into
wxtsky:mainfrom
halindrome:fix/question-drain-agent-scope
Sep 23, 2026
Merged

wxtsky merged 1 commit into
wxtsky:mainfrom
halindrome:fix/question-drain-agent-scope

Conversation

@halindrome

Copy link
Copy Markdown
Contributor

Fixes #340.

Problem

Background subagents report under their parent's session_id. CodeIsland's per-session drains treated a subagent's events as the parent's, so while the main thread's AskUserQuestion card was on screen, the question was denied. Claude Code shows this as Permission denied by hook, and the agent then falls back to asking in plain text.

Seen in the unified log: a question asked at 13:54:19 was denied 1.3 s later by a background agent's SubagentStop:

permission deny reason=drainQuestions(wasWaiting-blanket-drain-event=SubagentStop) session=aef16fa9-… tool=AskUserQuestion

A long-running background agent sends events continuously, so every question in that session gets denied. That matches the "always denied" report in #340.

Every drain path had the same blind spot:

Path Trigger from a subagent
handleEvent wasWaiting sweep its PostToolUse, SubagentStop, …
handlePermissionRequest its own PermissionRequest
handleAskUserQuestion / handleQuestion its own question
handlePeerDisconnect its hook socket dropping (e.g. Esc on its prompt)

Fix

  • drainQuestions / drainPermissions take an event filter, and each of the paths above passes an agent_id match. Requests stay mutually exclusive within one agent, as before.
  • monitorPeerDisconnect carries the agent_id of the connection. The session stays waiting if another agent's request is still queued.
  • Answering in the terminal still clears the card, because the main thread's own PostToolUse(AskUserQuestion) matches its question.

This is the question-side counterpart of #147, which made the same change for permissions.

Tests

  • testSubagentActivityDoesNotDenyMainThreadQuestion fails on main and passes with this change.
  • testSubagentRequestsAndDisconnectsDoNotDenyMainThreadQuestion covers the permission-request, question, and disconnect paths.
  • Full suite: 935 tests. The one failure, AppStateGitBranchTests.testBranchRefreshesOnStopAfterSwitch, times out under full-suite load on unmodified main too, and passes when run alone.

🤖 Generated with Claude Code

…g agent

Background subagents report under their parent's session_id. Every
per-session drain treated them as the parent, so while the main thread's
AskUserQuestion card was up, a subagent's PostToolUse or SubagentStop
(the wasWaiting sweep), its own PermissionRequest or question, or its hook
socket dropping denied the question. Claude reports that as "Permission
denied by hook" and falls back to asking in plain text (wxtsky#340).

Each drain now takes an event filter, and every caller reacting to one
agent's activity passes an agent_id match. Peer disconnects carry the
agent_id of the connection that dropped, and leave the session waiting
when another agent's request is still queued. Terminal answers still
clear the card: the main thread's own PostToolUse matches its question.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wxtsky

wxtsky commented Sep 23, 2026

Copy link
Copy Markdown
Owner

Merged (1cecfc5) — thank you. Tracing the deny to drainQuestions(wasWaiting-blanket-drain-event=SubagentStop) from the unified log is exactly the evidence #340 was missing, and scoping every drain path (sweep, new request, question, peer disconnect) to the requesting agent_id is the right shape — it's the question-side twin of #147. The disconnect path keeping the session in its waiting state while another agent's request is still queued is a nice touch. Ships in the next release.

wxtsky added a commit that referenced this pull request Sep 23, 2026
…333)

With two sessions waiting on AskUserQuestion, answering the first card and
then the second sent the second session the first answer - the report's
"是否立即触发构建? → openURL 传裸路径".

Root cause: QuestionBar kept its wizard state (current question index,
collected answers, selection, typed text) in @State, and nothing tied that
state to the request it was collected for. The card slot renders
`case .questionCard(sid)` -> `QuestionBar(...)` with no `.id`, so when
answering session A's card makes showNextPending() promote session B
(surface .questionCard(A) -> .questionCard(B) in one transaction), SwiftUI
keeps the same view and hands B the old @State. advance() never cleared
collectedAnswers after the final submit, so B's answer was appended behind
A's and B's card submitted [a1, b1]. answerQuestionMulti maps answers onto
items by position, so B's only question got a1. The #310 session routing
was right throughout: the right continuation received the wrong payload.
The state has been unkeyed since the wizard was introduced, so this is not
a regression of #310/#320/#345.

- QuestionRequest gets a per-request `id`, and the card is `.id(q.id)`:
  each request gets a fresh view and fresh state.
- The wizard state moves into QuestionWizardState, bound to that id.
  Recording for a different request starts over, and the final answer is
  not retained, so a retried submit cannot accumulate either.
- answerQuestionMulti refuses an answer set that is not exactly one answer
  per question of the target request (count and question text) instead of
  mapping it by position, so a future view-state bug leaves the card
  waiting rather than answering another session's question.

Tests reproduce the report end to end (two sessions, one wizard state
carried across the card swap; on the old logic the second session gets
"是否立即触发构建(含后续飞书通知)?: openURL 传裸路径"), and pin the
wizard reducer, per-request card identity and the model-side refusal.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

AskUserQuestion is always denied by the PreToolUse hook, while every other tool passes (CodeIsland 1.0.33, Claude Code 2.1.261)

2 participants