Files
hermes-agent/tui_gateway/turn_marker.py
T
FalconOrtiz db96c8ced7 fix: admit Bot Chat deliveries through the live session owner
Adapt FalconOrtiz's owner-mailbox proposal from #101564 onto the current
notification poller and topical modules. The durable mailbox is cross-process
ingress only: the existing owner admits its normal prompt turn after the
current turn and human FIFO clear. Retain immutable receipts, stable admission
identities, capability and lease fencing, compression lineage, and disable
blind recovery replay of imported turns.

The original stale server hunks and expiring receipt protocol were rebuilt
rather than cherry-picked: the current facade decomposition and durable busy
admission contract differ. Credit the earlier owner-mailbox work in #100544
and durable producer work in #100319.

Co-authored-by: fangliquanflq <fangliquan@qq.com>
Co-authored-by: 686f6c61 <github@00b.tech>
2026-09-07 16:48:29 -07:00

118 lines
4.5 KiB
Python

"""Durable interrupted-turn markers for the desktop/TUI auto-continue path. A running turn's progress
lives only in process memory (the agent flushes to SQLite at turn end), so a marker is written at turn
start and cleared on any conclusion — only a process death leaves one behind, and ``session.resume``
reads it (``_maybe_schedule_auto_continue``). Stored per ``HERMES_HOME`` (profile-aware); writes prune
entries older than ``_MAX_AGE_SECS`` and cap the count so a crash streak can't grow the file. Every
function is best-effort — marker bookkeeping must never break a turn — so I/O errors degrade to "no
marker" instead of raising."""
from __future__ import annotations
import contextlib
import json
import logging
import os
import tempfile
import threading
import time
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
_MAX_AGE_SECS = 24 * 3600
_MAX_ENTRIES = 32
# Enough to re-submit any realistic prompt; guards against a multi-megabyte paste being journaled.
_MAX_PROMPT_CHARS = 64_000
_lock = threading.Lock()
def _marker_path(home: Path | str) -> Path:
return Path(home) / "desktop" / "interrupted_turns.json"
def _started_at(entry: dict) -> float:
return float(entry.get("started_at") or 0)
def _load(path: Path) -> dict[str, dict]:
try:
with open(path, encoding="utf-8") as f:
data = json.load(f)
except FileNotFoundError:
return {}
except Exception:
logger.debug("unreadable turn-marker file %s; starting fresh", path, exc_info=True)
return {}
return {k: v for k, v in data.items() if isinstance(v, dict)} if isinstance(data, dict) else {}
def _prune(entries: dict[str, dict], now: float) -> dict[str, dict]:
fresh = {k: e for k, e in entries.items() if now - _started_at(e) <= _MAX_AGE_SECS}
if len(fresh) <= _MAX_ENTRIES:
return fresh
return dict(sorted(fresh.items(), key=lambda item: _started_at(item[1]), reverse=True)[:_MAX_ENTRIES])
def _store(path: Path, entries: dict[str, dict]) -> None:
if not entries:
path.unlink(missing_ok=True)
return
path.parent.mkdir(parents=True, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=".turn-marker-")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(entries, f)
os.replace(tmp, path)
except Exception:
with contextlib.suppress(OSError):
os.unlink(tmp)
raise
def _update(home: Path | str, session_key: str, mutate, what: str) -> None:
"""Load → ``mutate(entries)`` → store under the lock; ``mutate`` returns None to skip the write."""
try:
with _lock:
path = _marker_path(home)
entries = mutate(_load(path))
if entries is not None:
_store(path, entries)
except Exception:
logger.debug("failed to %s turn marker for %s", what, session_key, exc_info=True)
def record_turn_start(home: Path | str, session_key: str, prompt: str, *, attempts: int = 0,
auto_continue: bool = True) -> None:
"""Persist the marker for a turn that is about to run. ``attempts`` = how many auto-continues led to
this run (0 for a user-initiated turn); the crash-loop breaker reads it back on the next resume."""
if not session_key or not prompt:
return
now = time.time()
entry = {"attempts": max(0, int(attempts)), "prompt": prompt[:_MAX_PROMPT_CHARS], "started_at": now,
"auto_continue": bool(auto_continue)}
_update(home, session_key, lambda entries: {**_prune(entries, now), session_key: entry}, "record")
def clear_turn_marker(home: Path | str, session_key: str) -> None:
"""Remove the marker once its turn concluded (any outcome the client saw)."""
if session_key:
_update(home, session_key, lambda e: {k: v for k, v in e.items() if k != session_key} if session_key in e else None, "clear")
def read_turn_marker(home: Path | str, session_key: str) -> dict[str, Any] | None:
"""The marker left by a turn that never concluded, or None."""
if not session_key:
return None
try:
with _lock:
entry = _load(_marker_path(home)).get(session_key)
prompt = str(entry.get("prompt") or "") if isinstance(entry, dict) else ""
if not prompt.strip():
return None
return {"attempts": max(0, int(entry.get("attempts") or 0)), "prompt": prompt, "started_at": _started_at(entry),
"auto_continue": bool(entry.get("auto_continue", True))}
except Exception:
return None