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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ gets its address there at the next `live stop`.
| omp | Not pre-approved: its own approval settings apply | As omp decides |
| agy | Not pre-approved: its own approval settings apply | As agy decides; started with `--csrf_token` like `live agy` |

When Codex asks to review hooks that are new or changed, spawn answers "Continue
without trusting": those hooks stay off for that agent and nothing gets trusted.
Trust them yourself in an interactive `codex` to have spawned agents run them.

### Attaching to an agent

```bash
Expand Down
3 changes: 2 additions & 1 deletion one_conv/live/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ def spawn_command(harness, harness_args, cwd, brief, brief_file, mode, tmux_sess
# Recorded before registering, so an agent stuck on a prompt can still be stopped by its tmux name.
row = spawn.Spawned(name, harness, cwd, "", sender.address if sender else "", time.time(), sorted(before))
spawn.record(row)
session = spawn.wait_for(adapter.list_sessions, before, cwd, timeout, lambda: spawn.tmux_taken(name))
session = spawn.wait_for(adapter.list_sessions, before, cwd, timeout, lambda: spawn.tmux_taken(name),
on_poll=spawn.prompt_answerer(name, harness))
if session is None:
state = "is still running" if spawn.tmux_taken(name) else "exited"
raise click.ClickException(
Expand Down
24 changes: 23 additions & 1 deletion one_conv/live/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
when none is set) and the working directory trusted. Its bypass-permissions warning is left for
the person to accept once on that machine.

Codex asks before running hooks that are new or changed since they were last trusted. Spawn
answers "Continue without trusting": those hooks stay off for the agent and nothing is trusted.
Codex's only flag for it, `--dangerously-bypass-hook-trust`, would run hooks nobody vetted.

Every spawned agent is recorded in `spawned.json` under the live folder, keyed by its tmux session,
so `live stop` can end it later and refuses to touch sessions it did not start.
"""
Expand All @@ -33,6 +37,9 @@
TOOLS = ("send_message", "set_name", "list_live_sessions", "read_conversation", "search_conversations",
"pack_conversations", "find_conversations")

# Per harness: text of a start-up prompt that blocks registration, and the keys that pick its safe choice.
STARTUP_PROMPTS = {"codex": ("Continue without trusting", "3")}

MODES = ("ask", "auto", "yolo")
# Per-harness flags for each mode; `ask` adds none. omp has no reviewed auto mode.
MODE_FLAGS: dict[str, dict[str, list[str]]] = {
Expand Down Expand Up @@ -150,13 +157,27 @@ def screen(name: str, lines: int = 15) -> str:
return "\n".join([line for line in output.splitlines() if line.strip()][-lines:])


def prompt_answerer(name: str, harness: str) -> Callable[[], None]:
"""Answers, once, the start-up prompt `harness` shows in tmux session `name` before it registers."""
prompt = STARTUP_PROMPTS.get(harness)
answered = False

def answer() -> None:
nonlocal answered
if prompt and not answered and prompt[0] in screen(name, lines=40):
_tmux("send-keys", "-t", f"={name}:", prompt[1])
answered = True
return answer


def _same_dir(left: str, right: str) -> bool:
return bool(left) and os.path.realpath(left) == os.path.realpath(right)


def wait_for(list_sessions: Callable[[], list[LiveSession]], before: set[str], cwd: str, timeout: float,
alive: Callable[[], bool], poll: float = 1.0, sleep: Callable[[float], None] = time.sleep,
clock: Callable[[], float] = time.monotonic) -> LiveSession | None:
clock: Callable[[], float] = time.monotonic,
on_poll: Callable[[], None] = lambda: None) -> LiveSession | None:
"""The first session that was not running before and works in `cwd`; None at timeout or once the pane is gone."""
deadline = clock() + timeout
while True:
Expand All @@ -165,6 +186,7 @@ def wait_for(list_sessions: Callable[[], list[LiveSession]], before: set[str], c
return session
if clock() >= deadline or not alive():
return None
on_poll()
sleep(poll)


Expand Down
49 changes: 49 additions & 0 deletions tests/test_live_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,55 @@ def test_wait_stops_once_the_pane_is_gone(tmp_path):
assert spawn.wait_for(lambda: [], set(), str(tmp_path), 60, lambda: False, sleep=lambda s: None) is None


def test_wait_polls_while_the_agent_has_not_registered(tmp_path):
polls = []
clock = iter([0, 0, 10]).__next__
spawn.wait_for(lambda: [], set(), str(tmp_path), 5, lambda: True, sleep=lambda s: None, clock=clock,
on_poll=lambda: polls.append(1))
assert polls == [1]


CODEX_HOOKS_SCREEN = """ Hooks need review
1 hook is new or changed.
Hooks can run outside the sandbox after you trust them.
› 1. Review hooks
2. Trust all and continue
3. Continue without trusting (hooks won't run)
enter confirm · esc skip"""


@pytest.fixture
def keys(monkeypatch):
"""Records the keys sent to tmux sessions."""
sent = []
monkeypatch.setattr(spawn, "_tmux", lambda *args: sent.append(args) or subprocess.CompletedProcess(args, 0, "", ""))
return sent


def test_codex_hooks_prompt_is_answered_without_trusting(monkeypatch, keys):
monkeypatch.setattr(spawn, "screen", lambda name, lines=15: CODEX_HOOKS_SCREEN)
spawn.prompt_answerer("codex-w", "codex")()
assert keys == [("send-keys", "-t", "=codex-w:", "3")]


def test_codex_hooks_prompt_is_answered_once(monkeypatch, keys):
monkeypatch.setattr(spawn, "screen", lambda name, lines=15: CODEX_HOOKS_SCREEN)
answer = spawn.prompt_answerer("codex-w", "codex")
answer()
answer()
assert len(keys) == 1


@pytest.mark.parametrize(("harness", "shown"), [
("codex", "› Ask Codex to do anything"),
("omp", CODEX_HOOKS_SCREEN),
])
def test_no_keys_without_a_known_prompt(monkeypatch, keys, harness, shown):
monkeypatch.setattr(spawn, "screen", lambda name, lines=15: shown)
spawn.prompt_answerer("agent-w", harness)()
assert keys == []


def test_tmux_start_runs_the_command_without_a_shell(monkeypatch):
calls = []
monkeypatch.setattr(subprocess, "run", lambda argv, **kw: calls.append(argv) or subprocess.CompletedProcess(argv, 0, "", ""))
Expand Down
Loading