monkeybot-cli 0.13.2__tar.gz → 0.13.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/PKG-INFO +1 -1
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/pyproject.toml +1 -1
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/skills/monkeybot/SKILL.md +1 -1
- monkeybot_cli-0.13.3/spikes/chat_gateway_orphan_port.py +399 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/chat.py +9 -15
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/run_cmd.py +11 -21
- monkeybot_cli-0.13.3/src/monkeybot_cli/process_tree.py +175 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/gateway_manager.py +11 -14
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/talk_ui.py +3 -4
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/monkeybot.example.yaml +0 -1
- monkeybot_cli-0.13.3/tests/test_gateway_teardown.py +348 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/uv.lock +12 -12
- monkeybot_cli-0.13.2/skills/monkeybot/references/config-sections.md +0 -169
- monkeybot_cli-0.13.2/src/monkeybot_cli/process_tree.py +0 -44
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/.gitignore +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/__init__.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_file_index.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_local_shell.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_renderer.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_session.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_status_bar.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_theme.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_tool_display.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_tui.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/chat_tui_widgets.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/__init__.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/doctor.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/loop.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/mcp_login.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/new.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/refresh.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/talk.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/trace.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/commands/validate.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/compat.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/config_resolve.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/exit_commands.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/extras_catalog.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/gateway_health.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/main.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/opensandbox_lifecycle.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/output.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/overrides.txt +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/providers.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/__init__.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/audio_io.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/client.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/push_to_talk.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/session.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/session_controller.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/realtime/wire_encode.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/runtime_python.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/AGENT.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/Dockerfile +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/__init__.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/browser/SKILL.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/command_allowlist.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/dockerignore +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/env.example +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/goal/SKILL.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/image-generator/SKILL.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/image-generator/generate_image.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/loop/SKILL.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/mcp.json +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/opensandbox.docker.toml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/otel-collector.example.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/permissions.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/templates/coding/AGENT.md +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/templates/coding/command_allowlist.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/templates/coding/monkeybot.overrides.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/scaffold_defaults/templates/coding/permissions.yaml +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/session_controller.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/src/monkeybot_cli/terminal_markdown.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_continue.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_e2e.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_errors.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_file_index.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_grounding.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_local_shell.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_renderer_parity.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_session.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_status_bar.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_theme.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_tool_display.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_chat_tui.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_cli.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_config_resolve.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_doctor.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_extras_catalog.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_gateway_health.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_opensandbox_lifecycle.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_realtime_session_controller.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_run_cmd.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_runtime_python.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_scaffold.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_terminal_markdown.py +0 -0
- {monkeybot_cli-0.13.2 → monkeybot_cli-0.13.3}/tests/test_trace_cmd.py +0 -0
|
@@ -152,7 +152,7 @@ One successful turn confirms Tier 1.
|
|
|
152
152
|
|
|
153
153
|
## Tier 2 — Customize (on demand)
|
|
154
154
|
|
|
155
|
-
Only reach for these when the interview surfaces a need. Each maps to a `monkeybot.yaml` section. **For
|
|
155
|
+
Only reach for these when the interview surfaces a need. Each maps to a `monkeybot.yaml` section. **For every option, its allowed values, default, and `validate`/`doctor` check id, read [docs/config.md](https://github.com/human-plus-machine/monkeybot/blob/develop/docs/config.md).** Don't inline that reference here. Offline, the scaffolded `monkeybot_config/monkeybot.example.yaml` lists the same keys with shorter comments.
|
|
156
156
|
|
|
157
157
|
Decision → config map:
|
|
158
158
|
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"""Spike: does `monkeybot chat` leave a gateway grandchild holding the port?
|
|
2
|
+
|
|
3
|
+
The direct child is a shim, the same shape as ``uv run python``. On SIGTERM the
|
|
4
|
+
shim exits without signaling its child. The grandchild binds ``PORT`` and is
|
|
5
|
+
the process that would make the next ``monkeybot chat`` fail with
|
|
6
|
+
"port already in use".
|
|
7
|
+
|
|
8
|
+
This drives the real ``run_chat`` teardown:
|
|
9
|
+
|
|
10
|
+
- ``/bye`` and a normal session return go through ``run_chat``'s ``finally``
|
|
11
|
+
- Ctrl-C is a ``KeyboardInterrupt`` from the session, same ``finally``
|
|
12
|
+
- startup failure is the health-check miss path, which returns before that
|
|
13
|
+
``finally``
|
|
14
|
+
|
|
15
|
+
Exit 0 means every case released the port. Exit 1 means at least one orphan
|
|
16
|
+
still holds it. Exit 2 means the scenario never armed (setup error).
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import argparse
|
|
22
|
+
import os
|
|
23
|
+
import signal
|
|
24
|
+
import socket
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import tempfile
|
|
28
|
+
import textwrap
|
|
29
|
+
import time
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
from typing import Literal
|
|
32
|
+
from unittest.mock import patch
|
|
33
|
+
|
|
34
|
+
from monkeybot_cli.commands.chat import run_chat
|
|
35
|
+
from monkeybot_cli.runtime_python import RuntimePython
|
|
36
|
+
|
|
37
|
+
CaseKind = Literal["bye", "interrupt", "startup"]
|
|
38
|
+
ChildSignal = Literal["die", "ignore"]
|
|
39
|
+
|
|
40
|
+
_GRANDCHILD = textwrap.dedent(
|
|
41
|
+
"""\
|
|
42
|
+
import json, os, signal
|
|
43
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
44
|
+
from pathlib import Path
|
|
45
|
+
|
|
46
|
+
if os.environ.get("SPIKE_IGNORE_SIGTERM") == "1":
|
|
47
|
+
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
|
48
|
+
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
49
|
+
|
|
50
|
+
class Handler(BaseHTTPRequestHandler):
|
|
51
|
+
def do_GET(self) -> None:
|
|
52
|
+
if self.path.split("?", 1)[0] != "/health":
|
|
53
|
+
self.send_response(404)
|
|
54
|
+
self.end_headers()
|
|
55
|
+
return
|
|
56
|
+
body = json.dumps({"ok": True}).encode()
|
|
57
|
+
self.send_response(200)
|
|
58
|
+
self.send_header("Content-Type", "application/json")
|
|
59
|
+
self.send_header("Content-Length", str(len(body)))
|
|
60
|
+
self.end_headers()
|
|
61
|
+
self.wfile.write(body)
|
|
62
|
+
|
|
63
|
+
def log_message(self, fmt: str, *args: object) -> None:
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
# Pid is recorded before accept() so a failed bind cannot look like a leak.
|
|
67
|
+
Path(os.environ["SPIKE_PIDFILE"]).write_text(str(os.getpid()))
|
|
68
|
+
ThreadingHTTPServer(("127.0.0.1", int(os.environ["PORT"])), Handler).serve_forever()
|
|
69
|
+
"""
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
_SHIM = textwrap.dedent(
|
|
73
|
+
"""\
|
|
74
|
+
import os, signal, subprocess, sys
|
|
75
|
+
|
|
76
|
+
def _exit_without_children(*_args: object) -> None:
|
|
77
|
+
# A shim such as `uv run` can die on SIGTERM without killing the
|
|
78
|
+
# grandchild that actually bound the port.
|
|
79
|
+
os._exit(0)
|
|
80
|
+
|
|
81
|
+
signal.signal(signal.SIGTERM, _exit_without_children)
|
|
82
|
+
signal.signal(signal.SIGINT, _exit_without_children)
|
|
83
|
+
subprocess.Popen([sys.executable, os.environ["SPIKE_GRANDCHILD"]])
|
|
84
|
+
while True:
|
|
85
|
+
signal.pause()
|
|
86
|
+
"""
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _free_port() -> int:
|
|
91
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
92
|
+
sock.bind(("127.0.0.1", 0))
|
|
93
|
+
return int(sock.getsockname()[1])
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _listening(port: int) -> bool:
|
|
97
|
+
"""True when a process accepts TCP connections on ``port``.
|
|
98
|
+
|
|
99
|
+
``SO_REUSEADDR`` plus ``bind()`` is not a listen check on macOS: a second
|
|
100
|
+
bind can succeed while another process is already accepting.
|
|
101
|
+
"""
|
|
102
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
103
|
+
sock.settimeout(0.3)
|
|
104
|
+
try:
|
|
105
|
+
sock.connect(("127.0.0.1", port))
|
|
106
|
+
except OSError:
|
|
107
|
+
return False
|
|
108
|
+
return True
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _pid_alive(pid: int) -> bool:
|
|
112
|
+
try:
|
|
113
|
+
os.kill(pid, 0)
|
|
114
|
+
except ProcessLookupError:
|
|
115
|
+
return False
|
|
116
|
+
except PermissionError:
|
|
117
|
+
return True
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _kill_pid(pid: int) -> None:
|
|
122
|
+
try:
|
|
123
|
+
os.kill(pid, signal.SIGKILL)
|
|
124
|
+
except (ProcessLookupError, PermissionError):
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _write_agent(root: Path, port: int) -> None:
|
|
129
|
+
cfg = root / "monkeybot_config"
|
|
130
|
+
cfg.mkdir(parents=True)
|
|
131
|
+
(cfg / "monkeybot.yaml").write_text(
|
|
132
|
+
f"model:\n provider: gemini\n name: spike\nruntime:\n port: {port}\n",
|
|
133
|
+
encoding="utf-8",
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _run_case(
|
|
138
|
+
*,
|
|
139
|
+
kind: CaseKind,
|
|
140
|
+
child_signal: ChildSignal,
|
|
141
|
+
agent: Path,
|
|
142
|
+
shim: Path,
|
|
143
|
+
grandchild: Path,
|
|
144
|
+
) -> tuple[str, str]:
|
|
145
|
+
port = _free_port()
|
|
146
|
+
pidfile = agent / f"{kind}-{child_signal}.pid"
|
|
147
|
+
if pidfile.exists():
|
|
148
|
+
pidfile.unlink()
|
|
149
|
+
env = os.environ.copy()
|
|
150
|
+
env["MONKEYBOT_CHAT_PLAIN"] = "1"
|
|
151
|
+
env["SPIKE_GRANDCHILD"] = str(grandchild)
|
|
152
|
+
env["SPIKE_PIDFILE"] = str(pidfile)
|
|
153
|
+
env["SPIKE_IGNORE_SIGTERM"] = "1" if child_signal == "ignore" else "0"
|
|
154
|
+
env.pop("SANDBOX_ENABLED", None)
|
|
155
|
+
|
|
156
|
+
args = argparse.Namespace(
|
|
157
|
+
cwd=str(agent),
|
|
158
|
+
config=None,
|
|
159
|
+
attach=False,
|
|
160
|
+
url=None,
|
|
161
|
+
port=port,
|
|
162
|
+
model_provider=None,
|
|
163
|
+
model_name=None,
|
|
164
|
+
show_thinking=False,
|
|
165
|
+
verbose=False,
|
|
166
|
+
usage=False,
|
|
167
|
+
session=None,
|
|
168
|
+
resume_last=False,
|
|
169
|
+
no_animations=True,
|
|
170
|
+
theme="auto",
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
async def _session_returns(*_args: object, **_kwargs: object) -> int:
|
|
174
|
+
# /bye and a normal session return both leave run_chat through finally.
|
|
175
|
+
return 0
|
|
176
|
+
|
|
177
|
+
async def _session_interrupted(*_args: object, **_kwargs: object) -> int:
|
|
178
|
+
# Ctrl-C is caught around asyncio.run(_plain_chat_session).
|
|
179
|
+
raise KeyboardInterrupt
|
|
180
|
+
|
|
181
|
+
def _health_fails_once_listening(
|
|
182
|
+
base: str, proc: subprocess.Popen[str], timeout_s: float = 30.0
|
|
183
|
+
) -> bool:
|
|
184
|
+
del base, timeout_s
|
|
185
|
+
deadline = time.monotonic() + 8
|
|
186
|
+
while time.monotonic() < deadline:
|
|
187
|
+
if proc.poll() is not None:
|
|
188
|
+
return False
|
|
189
|
+
if _listening(port):
|
|
190
|
+
return False
|
|
191
|
+
time.sleep(0.05)
|
|
192
|
+
raise RuntimeError(f"startup case: nothing accepted connections on {port}")
|
|
193
|
+
|
|
194
|
+
patches = [
|
|
195
|
+
patch(
|
|
196
|
+
"monkeybot_cli.commands.chat.prepare_runtime_python",
|
|
197
|
+
lambda *_a, **_k: RuntimePython([sys.executable, str(shim)], "spike"),
|
|
198
|
+
),
|
|
199
|
+
patch("monkeybot_cli.commands.chat.is_sandbox_enabled", return_value=False),
|
|
200
|
+
]
|
|
201
|
+
if kind == "startup":
|
|
202
|
+
patches.append(
|
|
203
|
+
patch(
|
|
204
|
+
"monkeybot_cli.commands.chat._wait_for_health",
|
|
205
|
+
_health_fails_once_listening,
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
else:
|
|
209
|
+
session = _session_interrupted if kind == "interrupt" else _session_returns
|
|
210
|
+
patches.append(patch("monkeybot_cli.commands.chat._plain_chat_session", session))
|
|
211
|
+
|
|
212
|
+
previous = os.environ.copy()
|
|
213
|
+
os.environ.clear()
|
|
214
|
+
os.environ.update(env)
|
|
215
|
+
started = time.monotonic()
|
|
216
|
+
|
|
217
|
+
def _alarm(_signum: int, _frame: object) -> None:
|
|
218
|
+
raise TimeoutError(f"{kind}/{child_signal}: run_chat hung")
|
|
219
|
+
|
|
220
|
+
previous_alarm = signal.signal(signal.SIGALRM, _alarm)
|
|
221
|
+
signal.alarm(20)
|
|
222
|
+
try:
|
|
223
|
+
with patches[0], patches[1]:
|
|
224
|
+
if len(patches) == 3:
|
|
225
|
+
with patches[2]:
|
|
226
|
+
code = run_chat(args)
|
|
227
|
+
else:
|
|
228
|
+
code = run_chat(args)
|
|
229
|
+
except Exception as exc:
|
|
230
|
+
_reap(pidfile)
|
|
231
|
+
return "setup-error", f"{kind}/{child_signal}: run_chat raised {exc!r}"
|
|
232
|
+
finally:
|
|
233
|
+
signal.alarm(0)
|
|
234
|
+
signal.signal(signal.SIGALRM, previous_alarm)
|
|
235
|
+
os.environ.clear()
|
|
236
|
+
os.environ.update(previous)
|
|
237
|
+
elapsed = time.monotonic() - started
|
|
238
|
+
expected = {"bye": 0, "interrupt": 130, "startup": 1}[kind]
|
|
239
|
+
if not pidfile.is_file():
|
|
240
|
+
return "setup-error", (
|
|
241
|
+
f"{kind}/{child_signal}: grandchild never wrote a pid "
|
|
242
|
+
f"(run_chat={code}, expected={expected})"
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
grandchild_pid = int(pidfile.read_text().strip())
|
|
246
|
+
if code != expected:
|
|
247
|
+
_kill_pid(grandchild_pid)
|
|
248
|
+
return "setup-error", (
|
|
249
|
+
f"{kind}/{child_signal}: run_chat returned {code}, expected {expected}"
|
|
250
|
+
)
|
|
251
|
+
# The teardown under test has already returned. A short poll only absorbs
|
|
252
|
+
# SIGKILL reaping; it must not be long enough to hide a live orphan.
|
|
253
|
+
deadline = time.monotonic() + 1.0
|
|
254
|
+
while time.monotonic() < deadline:
|
|
255
|
+
if not _pid_alive(grandchild_pid) and not _listening(port):
|
|
256
|
+
break
|
|
257
|
+
time.sleep(0.05)
|
|
258
|
+
alive = _pid_alive(grandchild_pid)
|
|
259
|
+
listening = _listening(port)
|
|
260
|
+
if alive or listening:
|
|
261
|
+
_kill_pid(grandchild_pid)
|
|
262
|
+
return (
|
|
263
|
+
"leak",
|
|
264
|
+
f"{kind}/{child_signal}: LEAK port={port} pid={grandchild_pid} "
|
|
265
|
+
f"alive={alive} listening={listening} run_chat={code} elapsed={elapsed:.2f}s",
|
|
266
|
+
)
|
|
267
|
+
return (
|
|
268
|
+
"freed",
|
|
269
|
+
f"{kind}/{child_signal}: FREED port={port} pid={grandchild_pid} "
|
|
270
|
+
f"run_chat={code} elapsed={elapsed:.2f}s",
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _reap(pidfile: Path) -> None:
|
|
275
|
+
if not pidfile.is_file():
|
|
276
|
+
return
|
|
277
|
+
try:
|
|
278
|
+
_kill_pid(int(pidfile.read_text().strip()))
|
|
279
|
+
except ValueError:
|
|
280
|
+
return
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _probe_uv_run() -> str:
|
|
284
|
+
"""Report whether `uv run python` forwards one SIGTERM to its child.
|
|
285
|
+
|
|
286
|
+
This does not decide the chat bug. It checks the example in the report.
|
|
287
|
+
"""
|
|
288
|
+
uv = shutil_which("uv")
|
|
289
|
+
if uv is None:
|
|
290
|
+
return "uv-probe: skipped (uv not on PATH)"
|
|
291
|
+
probe = textwrap.dedent(
|
|
292
|
+
"""\
|
|
293
|
+
import os, signal, time
|
|
294
|
+
from pathlib import Path
|
|
295
|
+
Path(os.environ["SPIKE_UV_PIDFILE"]).write_text(str(os.getpid()))
|
|
296
|
+
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
|
297
|
+
time.sleep(30)
|
|
298
|
+
"""
|
|
299
|
+
)
|
|
300
|
+
with tempfile.TemporaryDirectory(prefix="monkeybot-uv-probe-") as raw:
|
|
301
|
+
root = Path(raw)
|
|
302
|
+
pidfile = root / "child.pid"
|
|
303
|
+
proc = subprocess.Popen(
|
|
304
|
+
[uv, "run", "--no-project", "python", "-c", probe],
|
|
305
|
+
env={**os.environ, "SPIKE_UV_PIDFILE": str(pidfile)},
|
|
306
|
+
stdout=subprocess.DEVNULL,
|
|
307
|
+
stderr=subprocess.PIPE,
|
|
308
|
+
)
|
|
309
|
+
try:
|
|
310
|
+
deadline = time.monotonic() + 20
|
|
311
|
+
while time.monotonic() < deadline and not pidfile.is_file():
|
|
312
|
+
if proc.poll() is not None:
|
|
313
|
+
err = proc.stderr.read().decode(errors="replace") if proc.stderr else ""
|
|
314
|
+
return (
|
|
315
|
+
f"uv-probe: uv exited {proc.returncode} before child ready: {err.strip()}"
|
|
316
|
+
)
|
|
317
|
+
time.sleep(0.05)
|
|
318
|
+
if not pidfile.is_file():
|
|
319
|
+
return "uv-probe: child pid file never appeared"
|
|
320
|
+
child = int(pidfile.read_text().strip())
|
|
321
|
+
proc.send_signal(signal.SIGTERM)
|
|
322
|
+
try:
|
|
323
|
+
proc.wait(timeout=3)
|
|
324
|
+
uv_exit = f"uv exited {proc.returncode} within 3s"
|
|
325
|
+
except subprocess.TimeoutExpired:
|
|
326
|
+
proc.kill()
|
|
327
|
+
proc.wait(timeout=2)
|
|
328
|
+
uv_exit = "uv still running after 3s; SIGKILL'd the uv pid"
|
|
329
|
+
time.sleep(0.3)
|
|
330
|
+
if _pid_alive(child):
|
|
331
|
+
_kill_pid(child)
|
|
332
|
+
return (
|
|
333
|
+
f"uv-probe: SIGTERM to uv pid {proc.pid} left child {child} alive "
|
|
334
|
+
f"({uv_exit}; a single-pid signal does not free an ignoring grandchild)"
|
|
335
|
+
)
|
|
336
|
+
return f"uv-probe: SIGTERM to uv pid {proc.pid} also killed child {child} ({uv_exit})"
|
|
337
|
+
finally:
|
|
338
|
+
if proc.poll() is None:
|
|
339
|
+
proc.kill()
|
|
340
|
+
proc.wait(timeout=2)
|
|
341
|
+
if pidfile.is_file():
|
|
342
|
+
try:
|
|
343
|
+
_kill_pid(int(pidfile.read_text().strip()))
|
|
344
|
+
except ValueError:
|
|
345
|
+
pass
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def shutil_which(name: str) -> str | None:
|
|
349
|
+
from shutil import which
|
|
350
|
+
|
|
351
|
+
return which(name)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def run_cases() -> int:
|
|
355
|
+
"""Run the chat teardown cases. 0 means every port was released."""
|
|
356
|
+
cases: list[tuple[CaseKind, ChildSignal]] = [
|
|
357
|
+
("bye", "die"),
|
|
358
|
+
("interrupt", "die"),
|
|
359
|
+
("startup", "die"),
|
|
360
|
+
("bye", "ignore"),
|
|
361
|
+
]
|
|
362
|
+
results: list[tuple[str, str]] = []
|
|
363
|
+
with tempfile.TemporaryDirectory(prefix="monkeybot-chat-port-spike-") as raw:
|
|
364
|
+
root = Path(raw)
|
|
365
|
+
agent = root / "agent"
|
|
366
|
+
_write_agent(agent, port=1)
|
|
367
|
+
grandchild = root / "grandchild.py"
|
|
368
|
+
grandchild.write_text(_GRANDCHILD, encoding="utf-8")
|
|
369
|
+
shim = root / "shim.py"
|
|
370
|
+
shim.write_text(_SHIM, encoding="utf-8")
|
|
371
|
+
for kind, child_signal in cases:
|
|
372
|
+
status, line = _run_case(
|
|
373
|
+
kind=kind,
|
|
374
|
+
child_signal=child_signal,
|
|
375
|
+
agent=agent,
|
|
376
|
+
shim=shim,
|
|
377
|
+
grandchild=grandchild,
|
|
378
|
+
)
|
|
379
|
+
results.append((status, line))
|
|
380
|
+
print(line, flush=True)
|
|
381
|
+
leaks = [line for status, line in results if status == "leak"]
|
|
382
|
+
errors = [line for status, line in results if status == "setup-error"]
|
|
383
|
+
if errors:
|
|
384
|
+
print("SPIKE_RESULT=SETUP_ERROR", flush=True)
|
|
385
|
+
return 2
|
|
386
|
+
if leaks:
|
|
387
|
+
print("SPIKE_RESULT=BUG_REPRODUCED", flush=True)
|
|
388
|
+
return 1
|
|
389
|
+
print("SPIKE_RESULT=PORT_RELEASED", flush=True)
|
|
390
|
+
return 0
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def main() -> int:
|
|
394
|
+
print(_probe_uv_run(), flush=True)
|
|
395
|
+
return run_cases()
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
if __name__ == "__main__":
|
|
399
|
+
raise SystemExit(main())
|
|
@@ -42,6 +42,8 @@ from monkeybot_cli.config_resolve import (
|
|
|
42
42
|
)
|
|
43
43
|
from monkeybot_cli.gateway_health import (
|
|
44
44
|
occupied_gateway_message as _occupied_gateway_message,
|
|
45
|
+
)
|
|
46
|
+
from monkeybot_cli.gateway_health import (
|
|
45
47
|
wait_for_health as _wait_for_health,
|
|
46
48
|
)
|
|
47
49
|
from monkeybot_cli.opensandbox_lifecycle import (
|
|
@@ -49,6 +51,7 @@ from monkeybot_cli.opensandbox_lifecycle import (
|
|
|
49
51
|
is_sandbox_enabled,
|
|
50
52
|
server_url_from_config,
|
|
51
53
|
)
|
|
54
|
+
from monkeybot_cli.process_tree import popen_kwargs_for_platform, stop_process_group
|
|
52
55
|
from monkeybot_cli.runtime_python import DEFAULT_PORT, gateway_argv, prepare_runtime_python
|
|
53
56
|
from monkeybot_cli.terminal_markdown import MarkdownPlainStream
|
|
54
57
|
|
|
@@ -691,6 +694,7 @@ def _spawn_gateway(config_path: Path | None, agent_root: Path, port: int) -> _Sp
|
|
|
691
694
|
cwd=agent_root,
|
|
692
695
|
stdout=subprocess.DEVNULL,
|
|
693
696
|
stderr=log_file,
|
|
697
|
+
**popen_kwargs_for_platform(),
|
|
694
698
|
)
|
|
695
699
|
return _SpawnedGateway(proc=proc, log_path=Path(log_file.name), log_file=log_file)
|
|
696
700
|
|
|
@@ -768,10 +772,7 @@ def run_chat(args: argparse.Namespace) -> int:
|
|
|
768
772
|
print(f"{_DIM}--- end log ---{_RESET}", file=sys.stderr)
|
|
769
773
|
else:
|
|
770
774
|
print("Run `monkeybot run` to see logs.", file=sys.stderr)
|
|
771
|
-
|
|
772
|
-
proc.terminate()
|
|
773
|
-
with contextlib.suppress(subprocess.TimeoutExpired):
|
|
774
|
-
proc.wait(timeout=5)
|
|
775
|
+
stop_process_group(proc)
|
|
775
776
|
_cleanup_gateway_log(spawned)
|
|
776
777
|
return 1
|
|
777
778
|
|
|
@@ -819,17 +820,10 @@ def run_chat(args: argparse.Namespace) -> int:
|
|
|
819
820
|
print(f"{_DIM}Shutting down gateway…{_RESET}", file=sys.stderr)
|
|
820
821
|
return 130
|
|
821
822
|
finally:
|
|
822
|
-
if proc
|
|
823
|
-
#
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
try:
|
|
827
|
-
proc.wait(timeout=5)
|
|
828
|
-
except subprocess.TimeoutExpired:
|
|
829
|
-
with contextlib.suppress(ProcessLookupError):
|
|
830
|
-
proc.kill()
|
|
831
|
-
with contextlib.suppress(subprocess.TimeoutExpired):
|
|
832
|
-
proc.wait(timeout=1)
|
|
823
|
+
if proc is not None:
|
|
824
|
+
# Graceful group signal so the gateway can finish open sessions,
|
|
825
|
+
# then SIGKILL the tree if a shim exited and left the port held.
|
|
826
|
+
stop_process_group(proc)
|
|
833
827
|
if spawned is not None:
|
|
834
828
|
_cleanup_gateway_log(spawned)
|
|
835
829
|
|
|
@@ -24,11 +24,16 @@ from monkeybot_cli.opensandbox_lifecycle import (
|
|
|
24
24
|
is_sandbox_enabled,
|
|
25
25
|
server_url_from_config,
|
|
26
26
|
)
|
|
27
|
-
from monkeybot_cli.process_tree import
|
|
27
|
+
from monkeybot_cli.process_tree import (
|
|
28
|
+
IS_WINDOWS,
|
|
29
|
+
KILL_TIMEOUT_SECS,
|
|
30
|
+
SHUTDOWN_TIMEOUT_SECS,
|
|
31
|
+
kill_process_tree,
|
|
32
|
+
popen_kwargs_for_platform,
|
|
33
|
+
signal_process_group,
|
|
34
|
+
)
|
|
28
35
|
from monkeybot_cli.runtime_python import gateway_argv, prepare_runtime_python
|
|
29
36
|
|
|
30
|
-
_SHUTDOWN_TIMEOUT_SECS = 5.0
|
|
31
|
-
_KILL_TIMEOUT_SECS = 1.0
|
|
32
37
|
_Handler = Callable[[int, FrameType | None], Any] | int | None
|
|
33
38
|
|
|
34
39
|
|
|
@@ -54,22 +59,7 @@ def _cli_exit_status(rc: int | None, received: int | None) -> int:
|
|
|
54
59
|
def _signal_process_group(proc: subprocess.Popen[bytes], signum: int) -> None:
|
|
55
60
|
if proc.poll() is not None:
|
|
56
61
|
return
|
|
57
|
-
|
|
58
|
-
try:
|
|
59
|
-
proc.send_signal(signum)
|
|
60
|
-
except OSError as exc:
|
|
61
|
-
print(f"failed to signal gateway: {exc}", file=sys.stderr)
|
|
62
|
-
return
|
|
63
|
-
try:
|
|
64
|
-
os.killpg(proc.pid, signum)
|
|
65
|
-
return
|
|
66
|
-
except ProcessLookupError:
|
|
67
|
-
return
|
|
68
|
-
except OSError as exc:
|
|
69
|
-
try:
|
|
70
|
-
proc.send_signal(signum)
|
|
71
|
-
except OSError:
|
|
72
|
-
print(f"failed to signal gateway: {exc}", file=sys.stderr)
|
|
62
|
+
signal_process_group(proc, signum)
|
|
73
63
|
|
|
74
64
|
|
|
75
65
|
def run_gateway_process(
|
|
@@ -77,8 +67,8 @@ def run_gateway_process(
|
|
|
77
67
|
*,
|
|
78
68
|
env: dict[str, str],
|
|
79
69
|
cwd: Path,
|
|
80
|
-
shutdown_timeout: float =
|
|
81
|
-
kill_timeout: float =
|
|
70
|
+
shutdown_timeout: float = SHUTDOWN_TIMEOUT_SECS,
|
|
71
|
+
kill_timeout: float = KILL_TIMEOUT_SECS,
|
|
82
72
|
) -> int:
|
|
83
73
|
"""Start the gateway and forward stop signals to its process group.
|
|
84
74
|
|