diff --git a/CHANGELOG.md b/CHANGELOG.md index c1c5619f..d142a057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable user-visible changes are recorded here. Versions follow [Semantic Ve ## Unreleased +### Fixed + +- Starting `claude` (or `opencode`) from the app no longer reopens an old + agent conversation. Any `shell` command starts the background daemon when + none is running, and it passed on its whole environment. Run from inside + Claude Code, that included the conversation's id, so every session the app + then asked for was treated as a handoff and forked that conversation instead + of starting fresh, until the daemon restarted. The daemon and the sessions it + starts no longer carry an agent conversation's identity, and a session the + app asks for never hands off. Typing `shell claude` inside Claude Code still + forks the conversation, as before. Machines already affected are fixed once + the new release's daemon replaces the running one (`shell service install`, + a restart, or signing in again). + ## [0.23.3] — 2026-09-25 ### Fixed diff --git a/cmd/shell/agent_loop.go b/cmd/shell/agent_loop.go index 485126dd..e95caf24 100644 --- a/cmd/shell/agent_loop.go +++ b/cmd/shell/agent_loop.go @@ -247,7 +247,9 @@ func performAgentCommand( // not silently weaken E2EE or widen file sharing on the wrapper itself. arguments := append([]string{"--"}, browserCommandArguments(command.Command)...) launch := exec.CommandContext(ctx, self, arguments...) - launch.Env = os.Environ() + // Never an agent conversation's identity, even from a daemon an older + // release started with one: the browser asked for a new program. + launch.Env = detachedEnvironment(os.Environ()) if command.Name != "" { // The launched shell reads this when it publishes the session, so // the name chosen in the browser survives to the session list. diff --git a/cmd/shell/daemon_unix.go b/cmd/shell/daemon_unix.go index 371c4736..956ec9df 100644 --- a/cmd/shell/daemon_unix.go +++ b/cmd/shell/daemon_unix.go @@ -134,7 +134,7 @@ func startDetachedDaemon(self string) { defer null.Close() command := exec.Command(self, "daemon") - command.Env = os.Environ() + command.Env = detachedEnvironment(os.Environ()) command.Stdin = null command.Stdout = null command.Stderr = null diff --git a/cmd/shell/daemon_windows.go b/cmd/shell/daemon_windows.go index 1ae0bb83..85d26e1f 100644 --- a/cmd/shell/daemon_windows.go +++ b/cmd/shell/daemon_windows.go @@ -102,7 +102,7 @@ func startDetachedDaemon(self string) { defer null.Close() command := exec.Command(self, "daemon") - command.Env = os.Environ() + command.Env = detachedEnvironment(os.Environ()) command.Stdin = null command.Stdout = null command.Stderr = null diff --git a/cmd/shell/handoff.go b/cmd/shell/handoff.go index 1ef90ed7..a37a027a 100644 --- a/cmd/shell/handoff.go +++ b/cmd/shell/handoff.go @@ -83,6 +83,43 @@ func prepareCommandLaunch(arguments, environment []string, allowHandoff bool) co return launch } +// harnessIdentityVariables are the variables an agent harness sets to tell a +// tool subprocess which conversation it belongs to. They are right for the one +// command the agent ran, and wrong for anything that outlives it. +// +// The daemon is the case that matters: any shell command starts it when none +// is running, so a first command typed inside Claude Code used to leave a +// daemon carrying that conversation's id. Every session the browser then asked +// for inherited it, read as a handoff, and forked that old conversation instead +// of starting the program that was asked for, until the daemon next restarted. +// +// Keep this a superset of every adapter's detection and strip lists; +// TestHarnessIdentityVariablesCoverEveryAdapter checks. +var harnessIdentityVariables = []string{ + "CLAUDECODE", + "CLAUDE_CODE_CHILD_SESSION", + "CLAUDE_CODE_SESSION_ID", + "CLAUDE_CODE_BRIDGE_SESSION_ID", + "CLAUDE_CODE_REMOTE_SESSION_ID", + "OPENCODE", + "OPENCODE_PID", + "OPENCODE_SESSION_ID", + "AGENT", +} + +// detachedEnvironment is the environment for a process that outlives the +// command that started it: this one, minus any agent conversation's identity. +func detachedEnvironment(environment []string) []string { + return removeEnvironmentVariables(environment, harnessIdentityVariables...) +} + +// handoffAllowed reports whether a launch may fork the agent conversation it +// was started from. Only a background launch someone typed may. A session the +// browser asked for never does: it asked for the program, started fresh. +func handoffAllowed(foreground bool, environment []string) bool { + return !foreground && environmentValue(environment, sessionOriginEnvironment) == "" +} + func environmentValue(environment []string, name string) string { prefix := name + "=" for _, entry := range environment { diff --git a/cmd/shell/handoff_test.go b/cmd/shell/handoff_test.go index f6b3ebaa..c255ceab 100644 --- a/cmd/shell/handoff_test.go +++ b/cmd/shell/handoff_test.go @@ -233,3 +233,70 @@ func TestOpencodeAdapterFallsBackToContinueWhenUnresolvable(t *testing.T) { t.Fatalf("arguments = %#v, want %#v", launch.Arguments, wantArguments) } } + +func TestHarnessIdentityVariablesCoverEveryAdapter(t *testing.T) { + listed := map[string]bool{} + for _, name := range harnessIdentityVariables { + listed[name] = true + } + for _, adapter := range harnessAdapters { + // With a session id, every adapter rewrites without touching disk. + _, stripped, _ := adapter.Rewrite(adapter.Binary, testClaudeSessionID) + for _, name := range stripped { + if !listed[name] { + t.Errorf("%s strips %s, which harnessIdentityVariables does not list", adapter.Binary, name) + } + } + } + for _, name := range []string{"CLAUDE_CODE_CHILD_SESSION", "CLAUDE_CODE_SESSION_ID", "OPENCODE", "OPENCODE_SESSION_ID"} { + if !listed[name] { + t.Errorf("harnessIdentityVariables does not list detection variable %s", name) + } + } +} + +func TestDetachedEnvironmentDropsAgentConversationIdentity(t *testing.T) { + got := detachedEnvironment([]string{ + "PATH=/usr/bin", + "CLAUDECODE=1", + "CLAUDE_CODE_CHILD_SESSION=1", + "CLAUDE_CODE_SESSION_ID=" + testClaudeSessionID, + "OPENCODE=1", + "OPENCODE_SESSION_ID=" + testOpencodeSessionID, + "HOME=/home/someone", + "SHELL_ONLINE_CONFIG=/tmp/config.json", + }) + want := []string{"PATH=/usr/bin", "HOME=/home/someone", "SHELL_ONLINE_CONFIG=/tmp/config.json"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("detachedEnvironment = %#v, want %#v", got, want) + } +} + +// Regression: a daemon first started from inside a Claude Code conversation +// passed that conversation's id to every browser-started session, so starting +// `claude` from the app forked that old conversation again, every time. +func TestBrowserStartedSessionNeverForksAnAgentConversation(t *testing.T) { + inherited := []string{ + "CLAUDECODE=1", + "CLAUDE_CODE_CHILD_SESSION=1", + "CLAUDE_CODE_SESSION_ID=" + testClaudeSessionID, + sessionOriginEnvironment + "=cmd_123", + } + // Even if the variables reach the session, the browser's request is not a handoff. + launch := prepareCommandLaunch([]string{"claude"}, inherited, handoffAllowed(false, inherited)) + if launch.Handoff != "" || !reflect.DeepEqual(launch.Arguments, []string{"claude"}) { + t.Fatalf("browser-started claude handed off: %#v", launch) + } + // And they no longer reach it: the daemon and its sessions start without them. + if value := environmentValue(detachedEnvironment(inherited), "CLAUDE_CODE_SESSION_ID"); value != "" { + t.Fatalf("detached environment kept the conversation id %q", value) + } + // Typed inside Claude Code, `shell claude` still forks the conversation, as before. + typed := inherited[:3] + if launch := prepareCommandLaunch([]string{"claude"}, typed, handoffAllowed(false, typed)); launch.Handoff != claudeConversationHandoff { + t.Fatalf("typed shell claude no longer hands off: %#v", launch) + } + if handoffAllowed(true, typed) { + t.Fatal("a foreground launch must not hand off") + } +} diff --git a/cmd/shell/main.go b/cmd/shell/main.go index 23ebb2bb..e9b7d583 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -160,7 +160,7 @@ func run(arguments []string, stdout, stderr io.Writer) int { if len(command) == 0 { command = []string{defaultShellCommand()} } - launch := prepareCommandLaunch(command, os.Environ(), !*foreground) + launch := prepareCommandLaunch(command, os.Environ(), handoffAllowed(*foreground, os.Environ())) command = launch.Arguments fileService, err := openSharedFileService(*shareFiles, *filesRoot) if err != nil {