refactor: remove redundant comments and docstrings (#6527)
* refactor: remove redundant comments and docstrings * fix(signal): annotate async context manager as a generator
This commit is contained in:
1 parent
cb3ba45317
commit
a4d2430140
922 files changed
+406
-7738
No files matched your search
@@ -1,5 +1,3 @@
|
||||
"""Foreground gateway runtime and lifecycle helpers."""
|
||||
|
||||
import asyncio
|
||||
import signal
|
||||
from collections.abc import Awaitable, Callable, Coroutine, Iterable
|
||||
@@ -79,7 +77,6 @@ async def _deliver_to_channel(
|
||||
session_key: str | None = None,
|
||||
session_generation: str | None = None,
|
||||
) -> None:
|
||||
"""Publish a user-visible message and mirror it into its admitted session."""
|
||||
metadata = dict(msg.metadata or {})
|
||||
record = record or bool(metadata.pop("_record_channel_delivery", False))
|
||||
raw_key = metadata.pop("_session_key", None)
|
||||
@@ -135,7 +132,6 @@ class _MCPReadinessHook(AgentHook):
|
||||
|
||||
|
||||
def _http_endpoint_responding(url: str, *, timeout_s: float = 0.25) -> bool:
|
||||
"""Return whether an HTTP endpoint responds, including with an auth error."""
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
@@ -154,7 +150,6 @@ async def _watch_webui_dev_server(
|
||||
*,
|
||||
poll_interval_s: float = 0.2,
|
||||
) -> None:
|
||||
"""Fail the foreground gateway when its owned Vite sidecar exits."""
|
||||
while not shutdown_event.is_set():
|
||||
await asyncio.sleep(poll_interval_s)
|
||||
if shutdown_event.is_set():
|
||||
@@ -174,7 +169,6 @@ def _install_gateway_shutdown_handlers(
|
||||
tasks: list[asyncio.Task[Any]],
|
||||
print_status: Callable[[str], None],
|
||||
) -> Callable[[], None]:
|
||||
"""Install foreground gateway signal handlers and return a restore callback."""
|
||||
loop_signals: list[int] = []
|
||||
previous_handlers: list[tuple[int, Any]] = []
|
||||
shutdown_requested = False
|
||||
@@ -229,7 +223,6 @@ def _advance_dream_cursor_if_behind(memory: Any) -> None:
|
||||
|
||||
|
||||
def _commit_dream_changes(memory: Any) -> str | None:
|
||||
"""Commit durable Dream edits, without entering the commit path for a no-op run."""
|
||||
if not memory.git.is_initialized():
|
||||
return None
|
||||
diff_body = memory.dream_content_diff()
|
||||
@@ -252,7 +245,6 @@ _HEARTBEAT_PREAMBLE = (
|
||||
|
||||
|
||||
def _heartbeat_has_active_tasks(content: str) -> bool:
|
||||
"""True if HEARTBEAT.md has task lines, ignoring headers, blanks and comments."""
|
||||
in_comment = False
|
||||
in_active_section: bool = False
|
||||
for line in content.splitlines():
|
||||
@@ -311,7 +303,6 @@ _GATEWAY_HEALTH_READ_TIMEOUT_SECONDS = 2.0
|
||||
|
||||
|
||||
def _print_gateway_health_endpoint(host: str, port: int) -> None:
|
||||
"""Print a usable health URL and make non-loopback binds explicit."""
|
||||
console.print(
|
||||
f"[green]✓[/green] Health endpoint: {_gateway_health_url(host, port)}"
|
||||
f"{_gateway_health_bind_note(host)}"
|
||||
@@ -327,7 +318,6 @@ def _print_gateway_health_endpoint(host: str, port: int) -> None:
|
||||
|
||||
|
||||
def _gateway_readiness_payload(channels: Any) -> tuple[bool, dict[str, object]]:
|
||||
"""Describe process liveness separately from required WebSocket readiness."""
|
||||
channel_status: dict[str, Any] = {}
|
||||
get_status = getattr(channels, "get_status", None)
|
||||
if callable(get_status):
|
||||
@@ -433,7 +423,6 @@ def _run_gateway(
|
||||
gateway_instance: GatewayInstance | None = None,
|
||||
update_startup: "GatewayStartup | None" = None,
|
||||
) -> None:
|
||||
"""Claim the installation and profile before initializing any runtime writers."""
|
||||
from nanobot.config.loader import get_config_path
|
||||
from nanobot.update_profiles import record_gateway_profile
|
||||
|
||||
@@ -485,7 +474,6 @@ def _run_claimed_gateway(
|
||||
update_startup: "GatewayStartup | None" = None,
|
||||
update_profile: "ProfileReceipt | None" = None,
|
||||
) -> None:
|
||||
"""Initialize and run only while the caller holds the gateway lifecycle claim."""
|
||||
from nanobot.agent.model_presets import load_model_preset_catalog
|
||||
from nanobot.agent.tools.message import MessageTool
|
||||
from nanobot.agent.turn_delivery import TurnDeliveryFactory
|
||||
@@ -584,7 +572,6 @@ def _run_claimed_gateway(
|
||||
if is_default_workspace(config.workspace_path):
|
||||
_migrate_cron_store(config)
|
||||
|
||||
# Create cron service with workspace-scoped store
|
||||
cron_store_path = config.workspace_path / "cron" / "jobs.json"
|
||||
cron = CronService(cron_store_path, admission=bus.admission)
|
||||
trigger_store = LocalTriggerStore(config.workspace_path)
|
||||
@@ -606,7 +593,6 @@ def _run_claimed_gateway(
|
||||
unified_session=config.agents.defaults.unified_session,
|
||||
)
|
||||
|
||||
# Create agent with cron service
|
||||
agent = AgentLoop.from_config(
|
||||
config, bus,
|
||||
provider=provider_snapshot.provider,
|
||||
@@ -651,9 +637,7 @@ def _run_claimed_gateway(
|
||||
if isinstance(message_tool, MessageTool):
|
||||
message_tool.set_send_callback(deliver_to_channel)
|
||||
|
||||
# Set cron callback (needs agent)
|
||||
async def on_cron_job(job: CronJob) -> str | CronRunResult | None:
|
||||
"""Execute a cron job through the agent."""
|
||||
async def _silent(*_args: Any, **_kwargs: Any) -> None:
|
||||
pass
|
||||
|
||||
@@ -716,7 +700,6 @@ def _run_claimed_gateway(
|
||||
await agent.sessions.state.prune_dream_sessions()
|
||||
return None
|
||||
|
||||
# Heartbeat is a system job that checks HEARTBEAT.md for active tasks.
|
||||
if job.name == "heartbeat":
|
||||
heartbeat_file = config.workspace_path / "HEARTBEAT.md"
|
||||
try:
|
||||
@@ -822,8 +805,6 @@ def _run_claimed_gateway(
|
||||
registry=tools,
|
||||
)
|
||||
|
||||
# Create channel manager (forwards SessionManager so the WebSocket channel
|
||||
# can serve the embedded webui's REST surface).
|
||||
channels = ChannelManager(
|
||||
config,
|
||||
bus,
|
||||
@@ -848,7 +829,6 @@ def _run_claimed_gateway(
|
||||
)
|
||||
|
||||
async def _pick_heartbeat_target() -> tuple[str, str]:
|
||||
"""Pick a routable channel/chat target for heartbeat-triggered messages."""
|
||||
sidebar_state = await asyncio.to_thread(read_webui_sidebar_state)
|
||||
unified_metadata = None
|
||||
if config.agents.defaults.unified_session:
|
||||
@@ -874,7 +854,6 @@ def _run_claimed_gateway(
|
||||
console.print("[yellow]✗[/yellow] Heartbeat: disabled")
|
||||
|
||||
async def _health_server(host: str, health_port: int) -> None:
|
||||
"""Lightweight HTTP health endpoint on the gateway port."""
|
||||
import json as _json
|
||||
|
||||
connection_slots = asyncio.Semaphore(_GATEWAY_HEALTH_MAX_CONNECTIONS)
|
||||
@@ -931,7 +910,6 @@ def _run_claimed_gateway(
|
||||
_print_gateway_health_endpoint(host, health_port)
|
||||
async with server:
|
||||
await server.serve_forever()
|
||||
# Register Dream system job (idempotent on restart)
|
||||
from nanobot.cron.types import CronJob, CronPayload, CronSchedule
|
||||
dream_cfg = config.agents.defaults.dream
|
||||
if dream_cfg.enabled:
|
||||
@@ -948,7 +926,6 @@ def _run_claimed_gateway(
|
||||
_advance_dream_cursor_if_behind(agent.context.memory)
|
||||
cron.remove_system_job("dream")
|
||||
|
||||
# Register Heartbeat system job (idempotent on restart)
|
||||
if hb_cfg.enabled:
|
||||
cron.register_system_job(CronJob(
|
||||
id="heartbeat",
|
||||
@@ -969,7 +946,6 @@ def _run_claimed_gateway(
|
||||
console.print(f"[green]✓[/green] Cron: {cron_job_count} scheduled jobs")
|
||||
|
||||
async def _open_browser_when_ready() -> None:
|
||||
"""Wait for the gateway to bind, then point the user's browser at the webui."""
|
||||
if not open_browser_url:
|
||||
return
|
||||
from urllib.parse import urlparse
|
||||
@@ -978,7 +954,7 @@ def _run_claimed_gateway(
|
||||
# readiness route, wait for an actual HTTP response rather than probing
|
||||
# the WebSocket listener with an incomplete TCP connection.
|
||||
if open_browser_ready_url:
|
||||
for _ in range(40): # ~4s max per listener
|
||||
for _ in range(40):
|
||||
if await asyncio.to_thread(
|
||||
_http_endpoint_responding,
|
||||
open_browser_ready_url,
|
||||
@@ -989,7 +965,7 @@ def _run_claimed_gateway(
|
||||
parsed = urlparse(open_browser_url)
|
||||
target_host = parsed.hostname or config.gateway.host or "127.0.0.1"
|
||||
target_port = parsed.port or port
|
||||
for _ in range(40): # ~4s max
|
||||
for _ in range(40):
|
||||
try:
|
||||
_reader, writer = await asyncio.open_connection(
|
||||
target_host,
|
||||
|
||||
Reference in new issue
Block a user