Skip to content
Open
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
199 changes: 110 additions & 89 deletions gateway/kanban_watchers.py

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,8 @@ def __init__(self, config: Optional[GatewayConfig] = None):
self._async_session_store = AsyncSessionStore(self.session_store)
self.delivery_router = DeliveryRouter(self.config)
self._running = False
self._kanban_db_draining = False
self._kanban_db_operations: set[asyncio.Task] = set()
self._gateway_loop: Optional[asyncio.AbstractEventLoop] = None
self._shutdown_event = asyncio.Event()
self._exit_cleanly = False
Expand Down Expand Up @@ -8448,13 +8450,26 @@ def _phase_elapsed() -> float:

_cron_at_start = self._active_cron_job_count()
_api_at_start = self._active_api_run_count()
_kanban_db_at_start = len(getattr(self, "_kanban_db_operations", ()))
_drain_started_at = time.monotonic()
active_agents, timed_out = await self._drain_active_agents(timeout)
_drain_deadline = asyncio.get_running_loop().time() + timeout
_drain_kanban_db = getattr(self, "_drain_kanban_db_operations", None)
_kanban_db_drained = (
await _drain_kanban_db(timeout) if _drain_kanban_db else True
)
_remaining_drain = max(
0.0, _drain_deadline - asyncio.get_running_loop().time()
)
active_agents, _agents_timed_out = await self._drain_active_agents(
_remaining_drain
)
timed_out = _agents_timed_out or not _kanban_db_drained
logger.info(
"Shutdown phase: drain done at +%.2fs (drain took %.2fs, "
"timed_out=%s, active_at_start=%d, active_now=%d, "
"cron_at_start=%d, cron_now=%d, "
"api_at_start=%d, api_now=%d)",
"api_at_start=%d, api_now=%d, "
"kanban_db_at_start=%d, kanban_db_now=%d)",
_phase_elapsed(),
time.monotonic() - _drain_started_at,
timed_out,
Expand All @@ -8464,6 +8479,8 @@ def _phase_elapsed() -> float:
self._active_cron_job_count(),
_api_at_start,
self._active_api_run_count(),
_kanban_db_at_start,
len(getattr(self, "_kanban_db_operations", ())),
)

if not timed_out:
Expand All @@ -8483,12 +8500,14 @@ def _phase_elapsed() -> float:
if timed_out:
logger.warning(
"Gateway drain timed out after %.1fs with %d active agent(s), "
"%d in-flight cron job(s), and %d api_server run(s); "
"%d in-flight cron job(s), %d api_server run(s), and "
"%d kanban DB operation(s); "
"interrupting remaining work.",
timeout,
self._running_agent_count(),
self._active_cron_job_count(),
self._active_api_run_count(),
len(getattr(self, "_kanban_db_operations", ())),
)
# Mark forcibly-interrupted sessions as resume_pending BEFORE
# interrupting the agents. This preserves each session's
Expand Down
23 changes: 23 additions & 0 deletions hermes_cli/kanban.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,11 @@ def kanban_command(args: argparse.Namespace) -> int:
# schema creation; `create` / `list` / every other command would
# error out on a fresh install.
with board_scope:
# Diagnostics is the one command that must remain available while the
# DB circuit is open; it reads the persisted sidecar without opening
# the quarantined database.
if action in {"diagnostics", "diag"} and kb.get_db_health() is not None:
return int(_cmd_diagnostics(args) or 0)
try:
kb.init_db()
except Exception as exc:
Expand Down Expand Up @@ -1696,6 +1701,24 @@ def _cmd_diagnostics(args: argparse.Namespace) -> int:
from hermes_cli import kanban_diagnostics as kd
from hermes_cli.config import load_config

board_health = kb.get_db_health()
if board_health is not None:
if getattr(args, "json", False):
print(json.dumps({"board_health": board_health}, indent=2, ensure_ascii=False))
else:
print("Kanban database circuit is OPEN (board quarantined).")
print(f" Incident: {board_health['incident_id']}")
print(f" Class: {board_health['classification']}")
sqlite_code = (
board_health.get("sqlite_errorname")
or board_health.get("sqlite_errorcode")
or "unknown"
)
print(f" SQLite: {sqlite_code}")
print(f" Manifest: {board_health['manifest_path']}")
print(f" Action: {board_health['recovery_command']}")
return 2

diag_config = kd.config_from_runtime_config(load_config())

with kb.connect_closing() as conn:
Expand Down
Loading