fix(macos-container): forward TERM env var in container exec --tty
Without TERM, Claude Code inside the container cannot determine which modifier-key protocol to enable (modifyOtherKeys / kitty). The inner PTY session has no terminal-type context, so Shift+Enter and Enter produce identical byte sequences (\r), making them indistinguishable. Pass the host TERM via --env TERM=<value> on every container exec --interactive --tty call, falling back to xterm-256color when TERM is not set on the host. Non-TTY exec paths are unaffected. Closes #245
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Callable, cast
|
from typing import Callable, cast
|
||||||
|
|
||||||
@@ -47,6 +48,11 @@ class MacosContainerBottle(Bottle):
|
|||||||
cmd = ["container", "exec"]
|
cmd = ["container", "exec"]
|
||||||
if tty:
|
if tty:
|
||||||
cmd.extend(["--interactive", "--tty"])
|
cmd.extend(["--interactive", "--tty"])
|
||||||
|
# Forward TERM so Claude Code can enable modifier-key protocols
|
||||||
|
# (e.g. modifyOtherKeys). Without it the inner PTY session has no
|
||||||
|
# TERM and Shift+Enter is indistinguishable from plain Enter.
|
||||||
|
term = os.environ.get("TERM", "xterm-256color")
|
||||||
|
cmd.extend(["--env", f"TERM={term}"])
|
||||||
if self.agent_workdir and self.agent_workdir != "/home/node":
|
if self.agent_workdir and self.agent_workdir != "/home/node":
|
||||||
cmd.extend(["--workdir", self.agent_workdir])
|
cmd.extend(["--workdir", self.agent_workdir])
|
||||||
cmd.extend([self.name, self.agent_command, *full_argv])
|
cmd.extend([self.name, self.agent_command, *full_argv])
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from bot_bottle.backend.macos_container import bottle as bottle_mod
|
||||||
from bot_bottle.backend.macos_container.bottle import MacosContainerBottle
|
from bot_bottle.backend.macos_container.bottle import MacosContainerBottle
|
||||||
|
|
||||||
|
|
||||||
@@ -16,12 +17,15 @@ class TestMacosContainerBottle(unittest.TestCase):
|
|||||||
None,
|
None,
|
||||||
agent_command="codex",
|
agent_command="codex",
|
||||||
)
|
)
|
||||||
|
with patch.dict(bottle_mod.os.environ, {"TERM": "xterm-256color"}, clear=False):
|
||||||
|
argv = bottle.agent_argv(["run"])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[
|
[
|
||||||
"container", "exec", "--interactive", "--tty",
|
"container", "exec", "--interactive", "--tty",
|
||||||
|
"--env", "TERM=xterm-256color",
|
||||||
"bot-bottle-dev-abc", "codex", "run",
|
"bot-bottle-dev-abc", "codex", "run",
|
||||||
],
|
],
|
||||||
bottle.agent_argv(["run"]),
|
argv,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_agent_argv_includes_workdir(self):
|
def test_agent_argv_includes_workdir(self):
|
||||||
@@ -31,15 +35,38 @@ class TestMacosContainerBottle(unittest.TestCase):
|
|||||||
None,
|
None,
|
||||||
agent_workdir="/home/node/workspace",
|
agent_workdir="/home/node/workspace",
|
||||||
)
|
)
|
||||||
|
with patch.dict(bottle_mod.os.environ, {"TERM": "xterm-256color"}, clear=False):
|
||||||
|
argv = bottle.agent_argv([])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[
|
[
|
||||||
"container", "exec", "--interactive", "--tty",
|
"container", "exec", "--interactive", "--tty",
|
||||||
|
"--env", "TERM=xterm-256color",
|
||||||
"--workdir", "/home/node/workspace",
|
"--workdir", "/home/node/workspace",
|
||||||
"bot-bottle-dev-abc", "claude",
|
"bot-bottle-dev-abc", "claude",
|
||||||
],
|
],
|
||||||
bottle.agent_argv([]),
|
argv,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_agent_argv_uses_host_term_value(self):
|
||||||
|
bottle = MacosContainerBottle("bot-bottle-dev-abc", lambda: None, None)
|
||||||
|
with patch.dict(bottle_mod.os.environ, {"TERM": "screen-256color"}, clear=False):
|
||||||
|
argv = bottle.agent_argv([])
|
||||||
|
self.assertIn("--env", argv)
|
||||||
|
self.assertIn("TERM=screen-256color", argv)
|
||||||
|
|
||||||
|
def test_agent_argv_term_falls_back_to_xterm_256color(self):
|
||||||
|
bottle = MacosContainerBottle("bot-bottle-dev-abc", lambda: None, None)
|
||||||
|
env_without_term = {k: v for k, v in bottle_mod.os.environ.items() if k != "TERM"}
|
||||||
|
with patch.object(bottle_mod.os, "environ", env_without_term):
|
||||||
|
argv = bottle.agent_argv([])
|
||||||
|
self.assertIn("TERM=xterm-256color", argv)
|
||||||
|
|
||||||
|
def test_agent_argv_no_tty_omits_term(self):
|
||||||
|
bottle = MacosContainerBottle("bot-bottle-dev-abc", lambda: None, None)
|
||||||
|
argv = bottle.agent_argv([], tty=False)
|
||||||
|
self.assertNotIn("--tty", argv)
|
||||||
|
self.assertNotIn("--env", argv)
|
||||||
|
|
||||||
def test_exec_pipes_script_to_shell(self):
|
def test_exec_pipes_script_to_shell(self):
|
||||||
bottle = MacosContainerBottle("bot-bottle-dev-abc", lambda: None, None)
|
bottle = MacosContainerBottle("bot-bottle-dev-abc", lambda: None, None)
|
||||||
with patch("bot_bottle.backend.macos_container.bottle.subprocess.run") as run:
|
with patch("bot_bottle.backend.macos_container.bottle.subprocess.run") as run:
|
||||||
|
|||||||
Reference in New Issue
Block a user