Skip to content

fix: deduplicate session messages + improve session lifecycle#805

Open
raymainfunc wants to merge 10 commits intoslopus:mainfrom
raymainfunc:fix/dedup-session-messages
Open

fix: deduplicate session messages + improve session lifecycle#805
raymainfunc wants to merge 10 commits intoslopus:mainfrom
raymainfunc:fix/dedup-session-messages

Conversation

@raymainfunc
Copy link

Summary

  • Fix duplicate messages in web client by sharing localId between modern and legacy format sends, so server dedup drops the second copy
  • Make sendSessionDeath async with server ack (2s timeout fallback) for reliable session cleanup
  • Add SIGHUP handler for graceful cleanup on terminal disconnect
  • Bump max messages per batch from 100 to 1000
  • Fix Dockerfile: copy patches/ dir needed by postinstall script
  • Plus: Gemini 3.1 Pro support, legacy output compat, various bug fixes

Test plan

  • Verified DB stores one message per localId (no duplicates)
  • Docker image builds successfully with patches/ fix
  • CLI type-checks and builds cleanly
  • Server container starts and serves requests

🤖 Generated with Claude Code
via Happy

DxTa and others added 10 commits February 28, 2026 18:01
…#120)

When the daemon spawns a session, it previously passed its full
process.env to the child process. If the daemon's shell had
ANTHROPIC_API_KEY set, Claude Code would use API key authentication
instead of its native OAuth login (Max plan), causing unexpected
billing.

Now, auth-related env vars (ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN,
CLAUDE_CODE_OAUTH_TOKEN) are stripped from the inherited environment
unless the selected profile explicitly sets them. This allows Claude
Code to fall back to its native OAuth authentication for Max plan users.

Includes comprehensive unit tests for env var stripping behavior.
Strip ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, and CLAUDE_CODE_OAUTH_TOKEN from
inherited environment when spawning Claude in local mode, unless explicitly set by
configuration. This prevents unintended authentication using credentials from the
parent process, extending the security fix from the daemon path to local execution.

Changes:
- claudeLocal.ts: Add baseEnv filtering logic (lines 241-258) that strips auth vars
  unless explicitly provided in claudeEnvVars
- claudeLocal.test.ts: Add 5 comprehensive test cases covering stripping behavior,
  explicit overrides, empty configs, and audit logging

All 15 tests pass (10 existing + 5 new). Follows secure-by-default principle and
matches implementation pattern from daemon fix (commit a5d18fc).

Related to issue slopus#120: prevent credential inheritance in Claude spawning
- Update Gemini model list to 3.1 Pro Preview + 3.0 Flash Preview
- Set default Gemini model to gemini-3.1-pro-preview
- Add --model, --dangerously-skip-permissions, --resume and initial
  prompt support to `happy gemini` for headless/automated dispatch
- Fix nested session CLAUDECODE env var leak in spawn helpers

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>
The mobile app was showing stale gemini-2.5-pro models because Gemini
sessions never pushed their available models to the server metadata.
This adds session.updateMetadata() calls on startup and model change,
so the app dynamically receives gemini-3.1-pro-preview and
gemini-3.0-flash-preview. Also removes all gemini-2.5 references.

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>
Two bugs caused the main loop to freeze permanently after user abort:

1. MessageQueue2.reset() set this.waiter = null WITHOUT resolving the
   pending promise. If the main loop was in waitForMessages(), the
   promise would never resolve and the loop hung forever.

2. sendPrompt() and waitForResponseComplete() were not interruptible
   by the abort signal. When handleAbort() fired, the main loop stayed
   stuck on these awaits while the Gemini backend continued processing
   tool calls indefinitely. New messages queued up but were never consumed.

Fixes:
- reset() now properly resolves the waiter with false
- Added withAbort() helper that races promises against the abort signal
- Wrapped sendPrompt/waitForResponseComplete with withAbort so they
  reject immediately with AbortError when abort is triggered

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>
… env stripping util

Mobile clients on Play Store (v5, 2025-12-22) predate the session-protocol migration
(commit 54d0031, 2026-02-13) and cannot parse the modern `role:'session'` wire format.
This caused Claude assistant responses (e.g. 'pong') to be silently dropped.

## Mobile compatibility (happy-cli + happy-app)

- CLI emits a legacy `output` mirror for every Claude assistant message:
  - `role: 'agent'`, `content.type: 'output'`, `legacyCompat: true`
  - UUID is preserved from original body or synthesised via randomUUID() if absent,
    since pre-migration parsers drop assistant output without a uuid.
- Session-protocol agent envelopes also dual-send a legacy-wrapped copy
  (`role: 'agent'`, `content.type: 'session'`) alongside the modern envelope.
- Mobile parser (`typesRaw.ts`) drops records tagged `legacyCompat: true` to prevent
  duplicate rendering on newer builds.
- Removed dead `rawAgentContentInputSchema`/`RawAgentContentInput` type.
- Collapsed duplicate codex and ACP `message|reasoning` branches.
- Test coverage: 25/25 CLI tests, 62/62 mobile parser tests pass.

## Auth env stripping refactor (happy-cli)

- Extracted `buildEnvWithStrippedAuthVars` and `deleteAuthVarsFromProcessEnv` into
  `src/utils/stripAuthEnvVars.ts` (with tests in `claudeRemote.test.ts`).
- Replaced inline stripping loops in claudeLocal, claudeRemote, and daemon run with
  the shared utility, eliminating code duplication.
- Changed `shx` devDependency to caret range.
…without body

Synthetic error assistant messages from the Claude SDK may have body.message == null
(isApiErrorMessage: true path). Only emit the legacy compat mirror when message is
present to avoid sending malformed output records to older mobile clients.
- Fix duplicate messages in web client by sharing localId between
  modern and legacy format sends, so server dedup drops the second copy
- Make sendSessionDeath async with server ack (2s timeout fallback)
- Add SIGHUP handler for graceful cleanup
- Bump max messages per batch from 100 to 1000
- Fix Dockerfile: copy patches/ dir needed by postinstall script

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>
The `extractSDKMetadataAsync` callback created a throwaway
`ApiSessionClient` (which opens a socket) just to update metadata,
while the main session client created another socket 1s later.
Both connected to the same session, causing the server to broadcast
every update to both connections — resulting in duplicate messages.

Fix: move session client creation before the async metadata extractor
so it reuses the same client instance.

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>
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.

2 participants