container: Set AGENT_CONTAINER=1 for in-container detection

Tools and skills running inside the container need to know they are not
on a bare host: the install-in-container skill hard-gates on this env
var before using passwordless sudo to install packages. Env vars are
fixed at create time, so containers created before the marker existed
keep running without it and detection fails forever. Recreate those
containers on startup, mirroring the existing image-mismatch check.
This commit is contained in:
Jeena 2026-09-27 03:10:54 +00:00
parent cc5c2b14a8
commit aff70f009e

View file

@ -167,6 +167,12 @@ class AgentContainer:
)
self.remove_container()
if self.container_exists() and not self._container_env_matches():
logger.info(
"Recreating container: it predates the AGENT_CONTAINER env marker"
)
self.remove_container()
if not self.container_exists():
self.create_container()
@ -390,6 +396,10 @@ class AgentContainer:
"create",
"--name",
self.container_name,
# Lets tools/scripts running inside detect that they are in
# an agent container (rather than a bare host).
"--env",
"AGENT_CONTAINER=1",
"--network",
"host",
"--user",
@ -423,6 +433,26 @@ class AgentContainer:
).stdout.strip()
return bool(container_image) and container_image == current_image
def _container_env_matches(self) -> bool:
"""Whether the existing container was created with the env vars the
current script sets at create time. Containers predating the
AGENT_CONTAINER marker keep running without it, so in-container
detection (see the install-in-container skill) fails. Comparing
against Config.Env catches that."""
env = subprocess.run(
[
"docker",
"container",
"inspect",
"-f",
"{{.Config.Env}}",
self.container_name,
],
capture_output=True,
text=True,
).stdout
return "AGENT_CONTAINER=1" in env
def start_container(self) -> bool:
subprocess.run(
["docker", "start", self.container_name],