Skip to content

fix(happy-cli): suppress the done push while background tasks are pending - #1863

Closed
tako3ball wants to merge 1 commit into
slopus:mainfrom
tako3ball:fix/suppress-done-push-background-tasks
Closed

tako3ball wants to merge 1 commit into
slopus:mainfrom
tako3ball:fix/suppress-done-push-background-tasks

Conversation

@tako3ball

Copy link
Copy Markdown

Problem

When a Claude turn dispatches subagents (or background shells), the CLI emits an SDK result for the turn while that background work is still running. claudeRemoteLauncher treats every result as a session end and fires the done session notification — so a single user-visible task produces one mobile push per subagent completion, and the last push can arrive before the final answer is even composed.

Fix

Track live background work at the claudeRemote level from the SDK's background_tasks_changed system messages (type: 'system', subtype: 'background_tasks_changed'):

  • Each message carries the full replacement set of tasks (the SDK recommends swapping the set per payload rather than pairing start/end edges), so the flag is replaced wholesale on every message.
  • Tasks with ambient: true are CLI housekeeping and do not count as user-visible work.
  • The flag is passed through onReady(status, hasPendingBackgroundTasks); the launcher holds the done push while status !== 'failed' && !pending && queue empty && hasPendingBackgroundTasks.

The turn that wakes on task completion sends the real final push, so behavior for ordinary turns is unchanged.

Compatibility

  • Fail-open: the guard uses message.tasks?.some(...) ?? false, so wire messages from older CLI builds (no tasks field) behave exactly as before.
  • The /clear early-return path calls onReady() with no args → push behavior unchanged.
  • failed turns still notify immediately (the status !== 'failed' check short-circuits first).
  • The flag is a local per-claudeRemote invocation, so it resets to empty on every CLI process (re)start — matching the SDK's per-process level semantics.
  • No API or wire-schema changes.

Tests

pnpm exec vitest run --project unit — 2 touched test files, 17 tests passing (rebased on current main):

  • claudeRemote.test.ts (12): flag true while tasks in flight · replacement set empties the flag · ambient-only ignored · ambient coexisting with real work keeps the flag · flag persists across two results (multi-turn).
  • claudeRemoteLauncher.test.ts: holds the done push while pending and sends it once tasks drain; closeClaudeSessionTurn still called with 'completed'.

review: L2 fresh-context reviewer + real-data verification subagent passed (no BLOCKER/MAJOR; 4 findings applied: launcher-side gating test, multi-turn persistence test, optional-chain fail-open guard, ambient-mix test)
todo: none — task tracking for this change lives in the author's separate ~/.claude setup repository, outside this upstream contribution

🤖 Generated with Claude Code

…ding

A turn that ends while subagents or background shells are still running is a pause, not a session end. The done push should land when the turn that wakes on task completion finishes, not on every intermediate result.

Track live background work from the SDK's background_tasks_changed system messages — each payload is a full replacement set, and ambient tasks (CLI housekeeping) are excluded. The launcher holds the done notification while non-ambient tasks are in flight. Fail-open for wire messages from older CLI builds, and the /clear and failed paths are unchanged.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
@tako3ball

Copy link
Copy Markdown
Author

Closing as this approach is no longer needed on our side — we are going with a client-agnostic Claude Code hook instead. Thanks for maintaining happy!

@tako3ball tako3ball closed this Oct 6, 2026
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.

1 participant