Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,50 @@ step; each entry says what to use instead.

### Fixed

- **A timed-out `execute_python` stops the code and what it started.** The
timeout killed only the process the executor started (the shell, or
bubblewrap's monitor), so the code ran on: on Linux the sandbox's init,
in a session of its own, outlived bubblewrap, and on macOS a process the
code forked outlived `sandbox-exec`. The command now runs in a session of
its own, and a timeout, a KeyboardInterrupt while it waits (in a caller
on the main thread), or, in the CLI, a hangup or SIGTERM kills its whole
process group and, on Linux, each child of it that left the group
(bubblewrap's sandbox init, whose death ends every process in the
sandbox); a signal the app ignores (under `nohup`) still changes nothing.
A normal exit waits for a running call, as before (to its timeout), and
then nothing it started is left; a run that ends on its own takes with it
what it left in its group. Outside a pid namespace (on macOS, and on Linux
without the sandbox), code that calls `setsid()` can outlive them (on
Linux the walk only catches a direct child of a live group member).

- **`execute_python` runs inside bubblewrap on Linux.** It never did: every
call was refused with "it did not start (bubblewrap, exit status 1): bwrap:
Can't create file at ~/.bash_profile: Read-only file system" (or another
protected file the home lacks: most homes lack some of `~/.bashrc`,
`~/.bash_profile`, `~/.zshrc`, `~/.profile`, …, and `/System`, a macOS
path in the same list, is on no Linux host), or, with `os_sandbox_strict`
off, run without the sandbox. The bubblewrap backend bound `/dev/null`
over each protected path that did not exist, and bwrap must make that
mount point itself, which it cannot do below the read-only root; seen
first on GitHub's ubuntu-26.04 runner, the first image with `bwrap` on
it, and the same with every bwrap version. A missing protected path the
read-only root covers is now left to it (the code still cannot create
it). One the code could create (a project's `.git` before `git init`, its
`.{app_name}` before the app makes it) is held by a placeholder, an empty
folder bound read-only, which the wrap makes and removes once the last run
holding it is over (`OSSandboxResult.release`, which the executor calls):
bwrap would have left an empty file there, and an empty `.git` file breaks
git ("invalid gitfile format"). A run never removes a folder it did not
make: a protected folder that is there and empty (an empty `.git`) is bound
read-only, and left as it was after a run that ended on its own (after a
timeout, or a run a signal ended, the run's hidden hold file stays in it).
The code runs as the sandbox's pid 1
(`--as-pid-1`), so a run that returned has left no process behind; one that
timed out, or that a signal ended, is not released, and its placeholder
stays, holding the run's hidden hold file and protected, for later runs to
hold. A placeholder that
cannot be made where the code could still create the path fails the wrap.

- **A model call a tool makes reports its usage.** `generate_simple`
(web_fetch's and the knowledge base's summaries, the memory proposals)
reported no `LLM_USAGE` on ADK or Deep Agents, so the status bar's totals
Expand Down
6 changes: 3 additions & 3 deletions CLAUDE.md

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,19 @@ The `/sandbox` CLI command lists and resets sandbox sessions.
`execute_python` runs its code only inside the OS sandbox: it writes only to
the working directory (and `os_sandbox_writable_paths`), reads no credential
store, no `~/.{app_name}` and not the project's `.env`, and has no network
unless `os_sandbox_allow_network` is on. On a host with no sandbox backend
unless `os_sandbox_allow_network` is on. On Linux, bubblewrap keeps the code
from creating a protected path that does not exist in a folder it may write
(the project's `.git` before `git init`) with a placeholder: a folder there
while the code runs and removed once it is over, but kept after a run that
timed out or that a signal ended (holding only that run's hidden hold file,
and still protected). An `os_sandbox_writable_paths` entry that holds your
home directory puts such folders at the shell start-up files it lacks
(`~/.bash_profile`, `~/.zshrc`, …), and a folder at `~/.bash_profile` makes a
login bash skip `~/.profile`. A timeout stops the code and the processes it
started; outside a pid namespace (on macOS, and on Linux without the
sandbox), code that calls `setsid()` can outlive it (on Linux only a direct
child of a live group member is caught).
On a host with no sandbox backend
(Windows has none), when the sandbox cannot be set up, or when the sandbox tool
fails as it starts (on Ubuntu 24.04 and later, `bwrap` needs unprivileged user
namespaces, which AppArmor restricts by default), the call is refused with an
Expand Down
58 changes: 57 additions & 1 deletion src/agentic_cli/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
import shlex
import shutil
import signal
import sys
import threading
import warnings
Expand Down Expand Up @@ -161,6 +162,57 @@ def sink(line: str) -> None:
_NOT_KEPT_LEFT = "not kept (session_store is memory)"


#: Whether ``_stop_runs_on_signals`` installed its handlers in this process.
_runs_stop_on_signals = False


def _stop_runs_on_signals() -> None:
"""Have a hangup (the terminal closed) or a SIGTERM kill the code
``execute_python`` runs before the app goes.

Each run is in a session of its own (``tools/executor.py::_run_command``),
where the signal the terminal sends the app's process group does not
reach it. Each handler kills the live runs, then does what the signal did
before: it calls the handler it replaced, or, where that was the default,
restores the default and sends the signal again, so the app still dies
of it. A signal the app ignores (under ``nohup``) gets no handler: the
app goes on, and so do its runs, each to its timeout. Installed once per
process, from the main thread (where a handler can be set); SIGINT is
left alone.
"""
global _runs_stop_on_signals
if _runs_stop_on_signals or threading.current_thread() is not threading.main_thread():
return
from agentic_cli.tools.executor import _kill_live_runs

for name in ("SIGHUP", "SIGTERM"):
signum = getattr(signal, name, None)
if signum is None:
continue
previous = signal.getsignal(signum)
if previous != signal.SIG_IGN:
signal.signal(signum, _killing_runs_first(_kill_live_runs, previous))
_runs_stop_on_signals = True


def _killing_runs_first(kill_runs, previous):
"""A handler that calls ``kill_runs``, then, whatever that did, does what
``previous`` (a handler, ``SIG_DFL``, or None for one set outside Python)
did."""

def handler(signum, frame):
try:
kill_runs()
finally:
if callable(previous):
previous(signum, frame)
else:
signal.signal(signum, signal.SIG_DFL)
os.kill(os.getpid(), signum)

return handler


def _passed_over_line(passed_over: list[tuple[str, int]], *, continuing: str | None) -> str:
"""What ``--continue`` says of the newer conversations other processes have
open: each by its id and holder, past three a count."""
Expand Down Expand Up @@ -2174,10 +2226,14 @@ async def run(self) -> None:

It installs the filters that log what ADK would print over the
prompt (``workflow/adk/log_filters.py``) first: an app that is built
but never run, as a library user's, keeps Python's own handling.
but never run, as a library user's, keeps Python's own handling. It
installs there, likewise, the SIGHUP and SIGTERM handlers that kill
the code ``execute_python`` runs before the app goes
(``_stop_runs_on_signals``).
"""
logger.info("repl_starting")
install_log_filters()
_stop_runs_on_signals()

job_monitor = self._new_job_monitor()
self._job_monitor = job_monitor # told when a conversation is deleted
Expand Down
4 changes: 2 additions & 2 deletions src/agentic_cli/tools/exec_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _protect_memory_folders(policy: "OSSandboxPolicy", settings: Any) -> None:
the read-only mount.
* A folder that exists is denied for both backends; one that could not be
made is denied by seatbelt alone (``deny_write_missing_paths``):
bubblewrap binds only what exists, and a bind over a missing path
leaves a stray file where the store later makes its folder.
bubblewrap covers a missing path with a placeholder folder, which
could not be made there either (the run would be refused).
"""
workspace = getattr(settings, "workspace_dir", None)
if workspace is None:
Expand Down
Loading
Loading