ci: apply all automatic linter fixes available
This commit is contained in:
@@ -73,7 +73,7 @@ def run_cell(request, execution_count):
|
||||
}, out.getvalue()
|
||||
'''
|
||||
|
||||
KERNEL_RUNNER_SOURCE = '''\
|
||||
KERNEL_RUNNER_SOURCE = f'''\
|
||||
"""Auto-generated Hermes session-kernel runner. One exec cell per request."""
|
||||
import contextlib
|
||||
import io
|
||||
@@ -84,9 +84,9 @@ import threading
|
||||
import traceback
|
||||
|
||||
_SENTINEL = os.environ["HERMES_KERNEL_SENTINEL"]
|
||||
_CAPTURE_LIMIT = {capture_limit}
|
||||
_CAPTURE_LIMIT = {_RUNNER_CAPTURE_BYTES}
|
||||
_SPILL_DIR = os.environ.get("HERMES_KERNEL_SPILL_DIR", "")
|
||||
_SPILL_CAP = {spill_cap}
|
||||
_SPILL_CAP = {5_000_000}
|
||||
_PARENT_PROCESS_HANDLE = os.environ.pop("HERMES_KERNEL_PARENT_PROCESS_HANDLE", "")
|
||||
_PARENT_DEATH_FD = os.environ.pop("HERMES_KERNEL_PARENT_DEATH_FD", "")
|
||||
|
||||
@@ -178,7 +178,7 @@ _start_parent_death_pipe_watchdog()
|
||||
|
||||
_real_stdout = sys.stdout
|
||||
|
||||
{cell_source}
|
||||
{RUNNER_CELL_SOURCE}
|
||||
|
||||
def _spill(text, spill_name):
|
||||
"""Best-effort: write the FULL clipped stdout to disk, return its path or ""."""
|
||||
@@ -225,7 +225,7 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
'''.format(cell_source=RUNNER_CELL_SOURCE, capture_limit=_RUNNER_CAPTURE_BYTES, spill_cap=5_000_000)
|
||||
'''
|
||||
|
||||
|
||||
class CellAuthority:
|
||||
@@ -288,7 +288,7 @@ class _BoundedBuffer:
|
||||
"""Byte chunks capped at a total size; ``drain`` returns text and resets."""
|
||||
|
||||
def __init__(self):
|
||||
self.chunks: List[bytes] = []
|
||||
self.chunks: list[bytes] = []
|
||||
self.total = 0
|
||||
|
||||
def append(self, data: bytes, cap: int) -> None:
|
||||
@@ -305,7 +305,7 @@ class _BoundedBuffer:
|
||||
class SessionKernel:
|
||||
"""One live kernel process plus its RPC server and reader threads."""
|
||||
|
||||
def __init__(self, key: Tuple):
|
||||
def __init__(self, key: tuple):
|
||||
self.key, self.owner, self.lock = key, key[0], threading.Lock()
|
||||
self.proc: Optional[subprocess.Popen] = None
|
||||
self.tmpdir = self.rpc_token = self.sentinel = ""
|
||||
@@ -313,9 +313,9 @@ class SessionKernel:
|
||||
self.server_sock: Optional[socket.socket] = None
|
||||
self.stop_event = threading.Event()
|
||||
self.death_pipe_w: Optional[int] = None
|
||||
self.tool_call_log: List = []
|
||||
self.tool_call_log: list = []
|
||||
self.cell_log_start = 0
|
||||
self.tool_call_counter: List[int] = [0]
|
||||
self.tool_call_counter: list[int] = [0]
|
||||
# Cells currently attached (bumped under the registry lock on selection, dropped when the
|
||||
# cell settles). Reaping/cap-eviction skip attached kernels: tearing one down mid-spawn
|
||||
# rmtree'd the staging dir under the spawner and killed live cells.
|
||||
@@ -365,7 +365,7 @@ class KernelRegistry:
|
||||
under the lock and torn down outside it — teardown may block on the child or the transport."""
|
||||
|
||||
def __init__(self, teardown: Callable[[Any], None]):
|
||||
self.kernels: Dict[Tuple, Any] = {}
|
||||
self.kernels: dict[tuple, Any] = {}
|
||||
self.lock, self._teardown = threading.Lock(), teardown
|
||||
|
||||
def shutdown(self, owner: Optional[str] = None, *, owner_matches: Optional[Callable[[str], bool]] = None) -> None:
|
||||
@@ -378,7 +378,7 @@ class KernelRegistry:
|
||||
for kernel in doomed:
|
||||
self._teardown(kernel)
|
||||
|
||||
def discard(self, key: Tuple, kernel: Any) -> None:
|
||||
def discard(self, key: tuple, kernel: Any) -> None:
|
||||
"""Drop *kernel*'s registry entry (only if it is still the one registered under *key* —
|
||||
never a replacement) and tear the kernel down."""
|
||||
with self.lock:
|
||||
@@ -388,7 +388,7 @@ class KernelRegistry:
|
||||
|
||||
|
||||
_REGISTRY = KernelRegistry(lambda kernel: kernel.teardown())
|
||||
_KERNELS: Dict[Tuple, SessionKernel] = _REGISTRY.kernels
|
||||
_KERNELS: dict[tuple, SessionKernel] = _REGISTRY.kernels
|
||||
|
||||
# Bounded lifecycle defaults (config: code_execution.max_session_kernels / kernel_idle_timeout).
|
||||
# A long-lived gateway must never accumulate one live child per finished conversation:
|
||||
@@ -398,7 +398,7 @@ DEFAULT_MAX_SESSION_KERNELS = 4
|
||||
DEFAULT_KERNEL_IDLE_TIMEOUT = 1800
|
||||
|
||||
|
||||
def _lifecycle_limits() -> Tuple[int, int]:
|
||||
def _lifecycle_limits() -> tuple[int, int]:
|
||||
from tools.code_execution_tool import _load_config
|
||||
config = _load_config()
|
||||
def limit(key: str, default: int) -> int:
|
||||
@@ -579,7 +579,7 @@ def _bind_rpc_socket(kernel: SessionKernel) -> str:
|
||||
return rpc_endpoint
|
||||
|
||||
|
||||
def _parent_process_handle(child_env: Dict[str, str]):
|
||||
def _parent_process_handle(child_env: dict[str, str]):
|
||||
"""Windows: open an inheritable SYNCHRONIZE handle to this process for the kernel's parent-death
|
||||
watchdog. Returns (handle, CloseHandle, startupinfo) or (None, None, None); fails open."""
|
||||
handle = close = startupinfo = None
|
||||
@@ -630,7 +630,7 @@ def _spawn(kernel: SessionKernel, *, child_python: str, child_cwd: str,
|
||||
# inherits the read end of a pipe whose only write end we hold (EOF == host gone, any cause).
|
||||
parent_handle, close_handle, startupinfo = _parent_process_handle(child_env) if _IS_WINDOWS else (None, None, None)
|
||||
death_r: Optional[int] = None
|
||||
pass_fds: Tuple[int, ...] = ()
|
||||
pass_fds: tuple[int, ...] = ()
|
||||
if not _IS_WINDOWS:
|
||||
death_r, kernel.death_pipe_w = os.pipe()
|
||||
child_env["HERMES_KERNEL_PARENT_DEATH_FD"] = str(death_r)
|
||||
@@ -657,14 +657,14 @@ def _spawn(kernel: SessionKernel, *, child_python: str, child_cwd: str,
|
||||
_ensure_background_reaper()
|
||||
|
||||
|
||||
def _pop_idle_expired(now: float, idle_timeout: float) -> List[SessionKernel]:
|
||||
def _pop_idle_expired(now: float, idle_timeout: float) -> list[SessionKernel]:
|
||||
"""Pop (caller holds ``_REGISTRY.lock``) every kernel idle past *idle_timeout*. Kernels with
|
||||
attached cells are skipped: the last cell out tears them down."""
|
||||
return [_KERNELS.pop(k) for k in list(_KERNELS)
|
||||
if _KERNELS[k].attached == 0 and now - _KERNELS[k].last_used > idle_timeout]
|
||||
|
||||
|
||||
def _acquire_kernel(key: Tuple, reset: bool, *, pinned: bool = False) -> Tuple[SessionKernel, bool]:
|
||||
def _acquire_kernel(key: tuple, reset: bool, *, pinned: bool = False) -> tuple[SessionKernel, bool]:
|
||||
"""Look up or register the kernel for *key*; returns (kernel, state_reset). Every entry also
|
||||
sweeps idle-expired kernels and enforces the process-wide LRU cap (doomed kernels are popped
|
||||
under the lock, torn down outside it), so a long-lived host stays bounded. ``pinned`` kernels
|
||||
@@ -757,7 +757,7 @@ def _background_reaper() -> None:
|
||||
logger.exception("kernel idle reaper pass failed; retrying next interval")
|
||||
|
||||
|
||||
def _await_cell(kernel: SessionKernel, timeout: int, is_interrupted) -> Tuple[str, Dict[str, Any]]:
|
||||
def _await_cell(kernel: SessionKernel, timeout: int, is_interrupted) -> tuple[str, dict[str, Any]]:
|
||||
"""Wait for the cell's reply; returns (host status, payload)."""
|
||||
deadline = time.monotonic() + timeout if timeout else None
|
||||
while True:
|
||||
@@ -778,9 +778,9 @@ def _with_stderr(stdout_text: str, stderr_text: str) -> str:
|
||||
return stdout_text + "\n--- stderr ---\n" + stderr_text
|
||||
|
||||
|
||||
def _cell_result(kernel: SessionKernel, key: Tuple, status: str, payload: Dict[str, Any], *,
|
||||
def _cell_result(kernel: SessionKernel, key: tuple, status: str, payload: dict[str, Any], *,
|
||||
timeout: int, sandbox_tools: frozenset, reused: bool,
|
||||
state_reset: bool, exec_start: float) -> Dict[str, Any]:
|
||||
state_reset: bool, exec_start: float) -> dict[str, Any]:
|
||||
"""Assemble the tool result for one settled cell (disposing the kernel where the contract says so)."""
|
||||
from tools.code_execution_tool import _sandbox_failure_hint, _truncate_stdout_text
|
||||
from agent.redact import redact_sensitive_text
|
||||
@@ -796,7 +796,7 @@ def _cell_result(kernel: SessionKernel, key: Tuple, status: str, payload: Dict[s
|
||||
stdout_text, stdout_metadata = _truncate_stdout_text(clean(str(payload.get("stdout", "")) + kernel.raw.drain()))
|
||||
cell_stderr = clean(str(payload.get("stderr", "")) + stderr_raw)
|
||||
cell_status = payload.get("status", "")
|
||||
result: Dict[str, Any] = {
|
||||
result: dict[str, Any] = {
|
||||
"status": status, "output": stdout_text, "exit_code": 0,
|
||||
"tool_calls_made": kernel.tool_call_counter[0], "duration_seconds": duration,
|
||||
"kernel": {"mode": "session", "reused": reused,
|
||||
@@ -872,7 +872,7 @@ def execute_in_session_kernel(
|
||||
kernel.teardown()
|
||||
|
||||
|
||||
def _run_cell(kernel: SessionKernel, key: Tuple, code: str, *, task_id: str, child_python: str,
|
||||
def _run_cell(kernel: SessionKernel, key: tuple, code: str, *, task_id: str, child_python: str,
|
||||
child_cwd: str, sandbox_tools: frozenset, timeout: int, max_tool_calls: int,
|
||||
is_interrupted, exec_start: float, state_reset: bool) -> str:
|
||||
reused = kernel.proc is not None
|
||||
|
||||
Reference in New Issue
Block a user