Skip to content

The agent that grows with you

fix(kanban): dashboard rejects stale archived/deleted board slugs

Austin Pickett1a85226e226d
diff --git a/plugins/kanban/dashboard/plugin_api.py b/plugins/kanban/dashboard/plugin_api.py
index 6a5af13ed84..0a9dc7a1a42 100644
--- a/plugins/kanban/dashboard/plugin_api.py
+++ b/plugins/kanban/dashboard/plugin_api.py
@@ -69,8 +69,14 @@ def _resolve_board(board: Optional[str]) -> Optional[str]:
if board is None or board == "":
return None
normed = _normalize_slug_or_400(board)
- if normed and normed != kanban_db.DEFAULT_BOARD and not kanban_db.board_exists(normed):
- raise HTTPException(status_code=404, detail=f"board {normed!r} does not exist")
+ if normed and normed != kanban_db.DEFAULT_BOARD:
+ if not kanban_db.board_exists(normed):
+ raise HTTPException(status_code=404, detail=f"board {normed!r} does not exist")
+ # An archived board has a tombstone board.json; it must not be openable
+ # (connect() would refuse anyway) — a stale dashboard tab gets a clean
+ # 404 instead of a resurrection or a 500 (#43243).
+ if kanban_db.read_board_metadata(normed).get("archived"):
+ raise HTTPException(status_code=404, detail=f"board {normed!r} is archived")
return normed
@@ -1718,6 +1724,24 @@ def _ws_board(raw: Optional[str]) -> Optional[str]:
return None
+def _ws_board_live(normed: Optional[str]) -> Optional[str]:
+ """Require an already-normalised slug to name a *live* board.
+
+ A stale dashboard tab can keep its old board slug around after the board
+ was archived or deleted; the event stream must reject it instead of handing
+ it to ``connect(board=slug)``, which would resurrect an empty board (#43243).
+ Returns ``None`` when the board is unknown/archived.
+ """
+ if not normed or normed == kanban_db.DEFAULT_BOARD:
+ return normed
+ try:
+ if kanban_db.read_board_metadata(normed).get("archived"):
+ return None
+ except Exception:
+ return None
+ return normed if kanban_db.board_exists(normed) else None
+
+
class _EventTail:
"""Per-socket ``task_events`` tailer. One SQLite connection, used/closed only on a
dedicated single-thread executor (connections are thread-affine); reusing it avoids
@@ -1788,7 +1812,14 @@ async def stream_events(ws: WebSocket):
await ws.accept()
# Board is pinned at the handshake; the UI opens a new WS on board change
# rather than reconciling two cursors mid-stream.
- tail = _EventTail(_ws_board(ws.query_params.get("board")))
+ raw_board = _ws_board(ws.query_params.get("board"))
+ board = _ws_board_live(raw_board)
+ if raw_board is not None and board is None:
+ # Stale tab: the slug names an archived or deleted board. Close the
+ # stream instead of connecting, which would resurrect the board (#43243).
+ await ws.close(code=http_status.WS_1008_POLICY_VIOLATION)
+ return
+ tail = _EventTail(board)
since = _since_param(ws)
try:
# Capture the tail at accept, before the first wait, so an event that
diff --git a/tests/plugins/test_kanban_dashboard_plugin.py b/tests/plugins/test_kanban_dashboard_plugin.py
index 43fb650b74b..9c6f9b9de14 100644
--- a/tests/plugins/test_kanban_dashboard_plugin.py
+++ b/tests/plugins/test_kanban_dashboard_plugin.py
@@ -7,6 +7,7 @@ REST surface without spinning up the whole dashboard.
from __future__ import annotations
+import contextlib
import importlib.util
import json
import os
@@ -1174,3 +1175,86 @@ def test_board_card_exposes_current_run_start(client):
# The detail endpoint carries the same contract.
detail = client.get(f"/api/plugins/kanban/tasks/{t}").json()["task"]
assert detail["current_run_started_at"] == retry_start
+
+
+def test_ws_events_for_archived_board_does_not_recreate_it(tmp_path, monkeypatch):
+ """A stale dashboard tab reopens /events?board=<slug> after the operator
+ archived or deleted that board. The stream must close instead of handing
+ the slug to connect(), which used to resurrect an empty DB-only board
+ (#43243)."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ kb.init_db()
+
+ import hermes_cli
+ import types
+
+ def _fake_ws_auth_ok(ws):
+ return ws.query_params.get("token", "") == "secret-xyz"
+
+ stub = types.SimpleNamespace(_SESSION_TOKEN="secret-xyz", _ws_auth_ok=_fake_ws_auth_ok)
+ monkeypatch.setitem(sys.modules, "hermes_cli.web_server_chat", stub)
+ monkeypatch.setattr(hermes_cli, "web_server_chat", stub, raising=False)
+
+ app = FastAPI()
+ app.include_router(_load_plugin_router(), prefix="/api/plugins/kanban")
+ c = TestClient(app)
+
+ from starlette.websockets import WebSocketDisconnect
+
+ for action, board in (("archive", "gone"), ("delete", "nuked")):
+ kb.create_board(board)
+ kb.remove_board(board, archive=(action == "archive"))
+
+ # Stale tab: open the event stream for the dead board. Bounded
+ # receive: with the fix the handshake rejects the slug and closes
+ # immediately; on an unfixed build nothing is ever sent, so the
+ # receive runs on a daemon thread and must not block the suite.
+ import threading
+
+ recv: dict = {}
+ ws_cm = c.websocket_connect(
+ f"/api/plugins/kanban/events?token=secret-xyz&board={board}&since=0"
+ )
+ ws = ws_cm.__enter__()
+
+ def _recv() -> None:
+ try:
+ ws.receive_json()
+ except WebSocketDisconnect:
+ recv["closed"] = True
+ except Exception as exc: # noqa: BLE001 - reported via the dict
+ recv["error"] = exc
+
+ thread = threading.Thread(target=_recv, daemon=True)
+ thread.start()
+ thread.join(timeout=2.0) # unfixed build: stream stays open, times out
+ received_close = bool(recv.get("closed"))
+ try:
+ ws.close()
+ except Exception:
+ pass
+ try:
+ ws_cm.__exit__(None, None, None)
+ except Exception:
+ pass
+
+ # With the fix the stream rejects the dead slug at the handshake.
+ assert received_close, (
+ f"event stream stayed open for the {action}d board {board!r}"
+ )
+ # No DB-only stub may reappear at the original slug.
+ assert not (kb.board_dir(board) / "kanban.db").exists()
+ if action == "archive":
+ assert kb.read_board_metadata(board)["archived"] is True
+ else:
+ assert board not in [b["slug"] for b in kb.list_boards(include_archived=True)]
+
+ # A live board still streams.
+ kb.create_board("alive")
+ with c.websocket_connect(
+ "/api/plugins/kanban/events?token=secret-xyz&board=alive"
+ ) as ws:
+ assert ws is not None