Skip to content

Stop app-started sessions from forking an old agent conversation - #284

Closed
pstayets wants to merge 1 commit into
TeoSlayer:mainfrom
pstayets:fix/daemon-agent-conversation-env
Closed

pstayets wants to merge 1 commit into
TeoSlayer:mainfrom
pstayets:fix/daemon-agent-conversation-env

Conversation

@pstayets

Copy link
Copy Markdown
Contributor

What changes for the user?

Bug: on a machine where shell was first run from inside Claude Code, starting claude from the app reopens that old Claude conversation, as a fork, instead of starting a new one. It happens every time, until the daemon restarts. We hit this on a real account: two app-started "new sessions" in a row both opened a fork of the Claude Code conversation that had run shell list earlier.

Cause:

  1. Any shell command starts the daemon when none is running (ensureDaemon), and startDetachedDaemon gives it os.Environ().
  2. Inside Claude Code's Bash tool, that environment includes CLAUDECODE, CLAUDE_CODE_CHILD_SESSION=1 and CLAUDE_CODE_SESSION_ID.
  3. The daemon passes its whole environment to every session the browser asks for (agent_loop.go, launch.Env = os.Environ()).
  4. For each one, prepareCommandLaunch sees a bare claude "inside" a Claude conversation and rewrites it to claude --resume <id> --fork-session.

OpenCode has the same path, including its "continue the latest session" fallback.

Fix: three independent guards, so one regression can't bring it back.

Where Change
startDetachedDaemon (Unix and Windows) The daemon starts without any agent harness's identity variables.
agent_loop.go, browser-started sessions Launched without them too. This also covers a daemon an older release already started with them.
main.go, handoff decision handoffAllowed(): a session the browser asked for (SHELL_ONLINE_SESSION_ORIGIN set) never hands off.

Typing shell claude inside Claude Code still forks the conversation, as the handoff feature intends. The installed service was never affected, because it passes only its allowlist of variables through.

harnessIdentityVariables is the single list. A test checks it covers every adapter's detection and strip variables, so a new harness adapter can't silently reopen this.

Verification

  • New tests in handoff_test.go:
    • TestBrowserStartedSessionNeverForksAnAgentConversation is the regression test. The pre-fix call site (prepareCommandLaunch(..., !foreground)) turns a browser-started claude into claude --resume 123e4567-… --fork-session, and it no longer does. A typed shell claude still hands off, and a foreground launch doesn't.
    • TestDetachedEnvironmentDropsAgentConversationIdentity
    • TestHarnessIdentityVariablesCoverEveryAdapter
  • With real binaries, from inside a Claude Code conversation, with a sandboxed config (remote_start: true) and runtime directory: shell list started a daemon.
    • Old binary: the daemon inherited 3 Claude conversation variables.
    • Fixed binary: it inherited 0.
  • go vet ./... and gofmt are clean. go test -race ./... passes: all 9 packages.

Access and privacy

This narrows what the daemon and browser-started sessions inherit. No change to authorization, remote-start consent, encryption or stored content.

  • No real share links, credentials, private terminal content or deployment secrets are included.
  • The changelog is updated.

Rollout

Needs a CLI release. On an affected machine, the fix takes effect once the new release's daemon replaces the running one, via shell service install, a reboot, or signing in again (restartDaemon). The second guard also protects sessions launched by a new daemon even if its own environment were somehow contaminated.

Until then, the workaround is to stop the daemon and start it again from a normal terminal, not from inside an agent.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ScihqCEjAhjFfkvfSR3ZXN

Any shell command starts the daemon when none is running, and
startDetachedDaemon gave it os.Environ(). Run from inside Claude Code, that
carried CLAUDE_CODE_CHILD_SESSION and CLAUDE_CODE_SESSION_ID into the daemon,
and from there into every session the browser asked for. prepareCommandLaunch
saw a bare `claude` inside a Claude conversation and rewrote it to
`claude --resume <id> --fork-session`, so starting Claude from the app
reopened the conversation the daemon happened to be born in, every time,
until the daemon restarted. OpenCode had the same path, including its
"continue the latest session" fallback.

Three independent guards, so one regression cannot bring it back:

- The daemon starts without any agent harness's identity variables.
- Sessions the daemon launches for the browser start without them too, which
  also covers a daemon an older release already started with them.
- A session the browser asked for (SHELL_ONLINE_SESSION_ORIGIN set) never
  hands off. Typing `shell claude` inside Claude Code still does.

harnessIdentityVariables is the one list; a test checks it covers every
adapter's detection and strip variables.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ScihqCEjAhjFfkvfSR3ZXN
@pstayets

Copy link
Copy Markdown
Contributor Author

Combined into #285, with the other open fixes and the two flaky CI checks fixed.

@pstayets pstayets closed this Sep 29, 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.

2 participants