Claude Code 2.1.310 runs Agent sub-agents in the background by default.
When one ends while the main agent is mid-turn, Claude Code tells its
model with a <task-notification> in a {role: "system"} message after the
tool results (several that end together share one text block), or as a
text block of the results' message. On the Claude subscription path
saidWithResults drops every system message that isn't the user's own
mid-turn words (#2060, c4f4aaaa9), and agentNotice matches
<task-notification> too, so the run's Claude Code got only the tool
results. Its model never heard the sub-agent finished and kept waiting.
withTaskNotices now appends each such notification to the last tool
result inside <system-reminder>, where Claude Code itself puts one when
a notification lands in a tool result. saidWithResults strips them
first; token counts and hooks' notices are still dropped, and the
user's mid-turn words still go through as theirs. The other two shapes
already got through: a notification after the main agent's reply (a new
turn) and one inside a tool_result string (what Sonnet 4.5 gets).
Captured with a real Claude Code 2.1.310 against a local fake upstream
in a sandbox HOME (no real account): the system message after the
results, the shared text block, and the notice beside the user's
mid-turn message.
Tests (internal/gateway/claude_task_notice_test.go):
TestClaudeTaskNotificationsGoOnWithTheResults (4 cases),
TestClaudeTaskNotificationBesideTheUsersMessage (5 cases: the user's
mid-turn message and the notification in one block, in both orders, in
a system message and in a reminder after the results, with and without
Claude Code's "IMPORTANT: ... address the user's message" line),
TestWithTaskNoticesLeavesOtherResults, TestClaudeTurnToldTheTaskNotification.
Broken by hand (call site passes the bare results, no stripping): all 9
cases of the first two fail, e.g. "the run was never told SUBRESULT-A
finished: call-1 was answered \"slept\"". Stripping alone turned off:
the two user-first cases fail, the notification written to Claude Code
as the user's words.
Run at this head, rebased on origin/main 83234fe92:
- gofmt -l internal: clean; go build ./...; go vet ./internal/gateway on
darwin, GOOS=linux and GOOS=windows: ok
- go test -race on the new tests and the #2060 mid-turn/hook tests: ok
- go test -run 'TestClaude|Context' ./internal/gateway: ok (64.5s)
- guard packages internal/proc, internal/testenv: ok
Full gateway suite (-count=1 -timeout 45m, alone on the box): ok in
819.6s at 1dd279f69 (base e219f699c), with the first version of the
BesideTheUsersMessage test; the widened test was run on its own above
(-race -count=3 too). Not run again after the rebase onto 83234fe92
(gateway/gui context parts, no overlap with this file) because three
other sessions' suites were running; CI runs it at this head.
Not tried: a real Claude account; the other candidates for the report
(a lost stream tail, concurrent streams ending together) with a
concurrent fake upstream, since the captures show content being dropped,
not a cut stream; the Windows box; go test ./... beyond gateway and the guard packages.