Files
hermes-agent/tui_gateway/turn_marker.py
T
teknium1 9b6dcad91d fix(utils): writers that published through mkstemp on main keep NEW files at 0600
0dfb4234 made every mode-less atomic write follow the process umask for NEW
targets, restoring what open("w")-based writers did. Ten of the folded sites
were not open("w") writers: they created the file through mkstemp and never
chmod'd, so on main a fresh file was 0600 regardless of umask (bot mailboxes,
relay inbox, turn markers, sessions.json, cron jobs/output, banner snapshot,
plugin toolset cache, presets, shell hooks, install id). CI caught the loosening
in tests/tools/test_bot_live_owner_delivery.py (st_mode 0o077 bits set).

Pass mode=0o600 explicitly at those ten sites; the umask default stays for the
sites that were open("w") on main. Invariant test exercises two real writers.
2026-09-13 05:07:11 -07:00

107 lines
4.2 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 json
import logging
import threading
import time
from pathlib import Path
from typing import Any
from utils import atomic_json_write
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
atomic_json_write(path, entries, indent=None, mode=0o600)
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