A session still bound to profile b survives `hermes profile delete b` and `hermes profile rename b c`. Its next plugin write recreated profiles/b through a raw mkdir, and nothing raised. Seven plugin state writers now create their directories through mkdir_under_hermes_home, which refuses a missing or tombstoned named home with its existing FileNotFoundError: plugin_data_dir (and the hermes-achievements fallback that duplicates it), the CLI plugins dir, the update-check markers, disk-cleanup state, the Discord recovery ledger, the Weixin account dir and the A2A audit log. Callers that already catch keep skipping the write (Discord recovery, the update-check writers, the disk-cleanup session-end hook and audit log, the A2A audit). The disk-cleanup slash commands reply that the profile no longer exists instead of raising. plugin_data_dir documents the refusal.
48 lines
2.2 KiB
Python
48 lines
2.2 KiB
Python
"""Per-plugin persistent storage: ``<hermes home>/plugin-data/<name>/``.
|
|
|
|
Plugins must NOT park state in ``<hermes home>/plugins/<name>/`` (the install dir, deleted by
|
|
``remove`` and git-pulled by ``update``). Secrets are deliberately NOT part of this convention —
|
|
credential reads go through ``agent.secret_scope`` / ``.env``.
|
|
Usage: ``plugin_data_dir("my-plugin") / "state.json"``; ``plugin_db("my-plugin")`` → ``data.db``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
__all__ = ["plugin_data_dir", "plugin_db"]
|
|
|
|
# Mirrors the plugin-name shape `hermes plugins install` accepts (no separators/traversal).
|
|
_NAME_RE = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$")
|
|
|
|
|
|
def _validate_name(name: str) -> str:
|
|
if not _NAME_RE.fullmatch(name) or ".." in name:
|
|
raise ValueError(f"invalid plugin name for storage: {name!r}")
|
|
return name
|
|
|
|
|
|
def plugin_data_dir(name: str) -> Path:
|
|
"""Return (and create) ``<hermes home>/plugin-data/<name>/``; resolves ``get_hermes_home()`` on
|
|
every call so it follows the active profile — don't cache across profile switches.
|
|
|
|
Raises ``FileNotFoundError`` when the active named profile was deleted or renamed (a session
|
|
still bound to it) instead of recreating that profile's home."""
|
|
from hermes_constants import get_hermes_home, mkdir_under_hermes_home
|
|
return mkdir_under_hermes_home(get_hermes_home() / "plugin-data" / _validate_name(name))
|
|
|
|
|
|
def plugin_db(name: str, filename: str = "data.db") -> sqlite3.Connection:
|
|
"""Open ``<data dir>/<filename>``. WAL so a dashboard reader and a tool writer coexist;
|
|
``check_same_thread=False`` for the threaded FastAPI/tool env — caller owns transactions."""
|
|
if Path(filename).name != filename or not filename:
|
|
raise ValueError(f"invalid plugin db filename: {filename!r}")
|
|
from hermes_cli.sqlite_util import open_db
|
|
|
|
# WAL via the shared fallback helper: network filesystems degrade to DELETE and WAL-reset-bug
|
|
# builds never enable it, instead of every plugin DB bypassing those rules with a raw PRAGMA.
|
|
return open_db(plugin_data_dir(name) / filename, db_label=f"plugin-data/{name}/{filename}",
|
|
foreign_keys=True, row_factory=None, check_same_thread=False)
|