Skip to content

The agent that grows with you

Merge origin/main into fix/pkg10-creds-b

Teknium9d817db0c4e2
diff --cc tools/environments/docker.py
index e8d9b16dd81,5fdf38e4e9a..e8eb7bca62c
--- a/tools/environments/docker.py
+++ b/tools/environments/docker.py
@@@ -209,8 -206,7 +211,9 @@@ def reap_orphan_containers
age = (now - finished_at).total_seconds()
if age < max_age_seconds:
continue
+ strip_sandbox_privileges(docker, cid) # its teardown never ran; owners stay (it may restart)
+ if _is_idle_workspace_container(docker, cid):
+ continue
# No -f: a sibling may have restarted the container between the ps snapshot
# and now (FinishedAt still reports the previous exit), and the daemon refuses
# a plain rm on a running container, which is the atomic recheck this sweep needs.
@@@ -1327,25 -1314,9 +1337,33 @@@ class DockerEnvironment(BaseEnvironment
if not self._persistent:
self._remove_bind_dirs()
+ def stop_idle(self) -> None:
+ """Idle reclaim of a per-workspace persistent container (#140665): ``docker stop``, never
+ ``rm``. Its writable layer and ``/root`` sandbox stay, and the next use in that workspace
+ re-attaches through :meth:`_attach_existing_container`, which ``docker start``s a stopped
+ container. Without this, persistent mode left one RUNNING container per workspace a user
- ever opened (each with its own ``container_memory``) until the process exited."""
++ ever opened (each with its own ``container_memory``) until the process exited. Like a
++ persist-mode cleanup, the host user may touch what it left before it is reattached, so its
++ sandbox privileges are stripped once it has stopped."""
+ container_id = self._container_id
+ if not container_id:
+ return
+ docker_exe, log_id = self._docker_exe, container_id[:12]
+
+ def _do_stop() -> None:
+ try:
+ subprocess.run([docker_exe, "stop", "-t", "10", container_id], capture_output=True,
+ timeout=30, stdin=subprocess.DEVNULL, check=False)
+ except (subprocess.TimeoutExpired, OSError) as e:
+ logger.warning("docker stop %s (idle workspace) failed: %s", log_id, e)
++ strip_sandbox_privileges(docker_exe, container_id)
+
- t = threading.Thread(target=_do_stop, daemon=True, name=f"hermes-idle-stop-{log_id}")
++ self._start_teardown(_do_stop, log_id)
++ self._container_id = None
++
+ def _start_teardown(self, work, log_id: str) -> None:
+ """Run *work* on a daemon thread the atexit drain joins."""
+ t = threading.Thread(target=work, daemon=True, name=f"hermes-cleanup-{log_id}")
with _TEARDOWN_LOCK:
_TEARDOWN_THREADS.add(t)
t.start()