chore(hermes-plugin): drop duplicated definitions and add ruff config
The plugin carried five verbatim second copies of definitions — protocol.py's delivery_id() and home_deliver(), connection.py's AcceptedHandler alias and _notify_accepted(), and __init__.py's .home import. All were merge artifacts: Python's last-one-wins semantics made them behavior-neutral, so this deletes the second copy of each (for home_deliver the kept first copy differs only by a literal "≤" vs "<=" in the docstring). Adds a minimal pyproject.toml (tool config only, no packaging) so ruff runs with E/W/F — F811 redefinition is exactly the rule that would have caught these — with E501 off in favor of the plugin's long explanatory prose. Drops test_connection.py's unused contextlib import (F401) so the check passes clean. Also quiets home.py's save_home_thread_id() failure log: outside a Hermes install the failure is just ModuleNotFoundError for hermes_cli, so log a one-line warning with the exception summary instead of a full stack trace on every test run. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
a9b34ea820
commit
b76f8c8bc8
6 files changed
+21
-77
No files matched your search
@@ -11,7 +11,6 @@ from .adapter import (
|
||||
)
|
||||
from .cli import register_cli, t3_command
|
||||
from .home import HOME_CHANNEL_ENV, standalone_send
|
||||
from .home import HOME_CHANNEL_ENV, standalone_send
|
||||
|
||||
|
||||
def _pre_tool_call(
|
||||
|
||||
@@ -22,7 +22,6 @@ except ImportError: # pragma: no cover - Hermes currently installs websockets
|
||||
MessageHandler = Callable[[dict[str, Any]], Awaitable[None]]
|
||||
StateHandler = Callable[[bool, str | None], Awaitable[None] | None]
|
||||
AcceptedHandler = Callable[[dict[str, Any]], Awaitable[None]]
|
||||
AcceptedHandler = Callable[[dict[str, Any]], Awaitable[None]]
|
||||
|
||||
|
||||
class ConnectionRejected(RuntimeError):
|
||||
@@ -346,20 +345,6 @@ class T3GatewayConnection:
|
||||
except Exception: # noqa: BLE001 - reconciliation must not fail a good handshake
|
||||
logger.warning("T3 connection accepted callback failed", exc_info=True)
|
||||
|
||||
async def _notify_accepted(self, accepted: dict[str, Any]) -> None:
|
||||
"""Hand the `connection.accepted` frame to the adapter, best-effort.
|
||||
|
||||
A failure here — a read-only `.env`, an unwritable queue file — must
|
||||
not tear down a connection that authenticated successfully, so it is
|
||||
logged and swallowed exactly like the state callback.
|
||||
"""
|
||||
if self._on_accepted is None:
|
||||
return
|
||||
try:
|
||||
await self._on_accepted(accepted)
|
||||
except Exception: # noqa: BLE001 - reconciliation must not fail a good handshake
|
||||
logger.warning("T3 connection accepted callback failed", exc_info=True)
|
||||
|
||||
async def _notify_state(self, connected: bool, reason: str | None) -> None:
|
||||
if self._on_state is None:
|
||||
return
|
||||
|
||||
@@ -130,12 +130,14 @@ def save_home_thread_id(thread_id: str) -> bool:
|
||||
|
||||
save_env_value(HOME_CHANNEL_ENV, value)
|
||||
saved = True
|
||||
except Exception: # noqa: BLE001 - a read-only .env must not break the handshake
|
||||
except Exception as exc: # noqa: BLE001 - a read-only .env must not break the handshake
|
||||
# No stack trace: outside a Hermes install this is simply
|
||||
# ModuleNotFoundError for hermes_cli, which is expected and noisy.
|
||||
logger.warning(
|
||||
"Could not persist %s; T3 home delivery will use the in-process "
|
||||
"value until the next reconnect",
|
||||
"Could not persist %s (%s); T3 home delivery will use the "
|
||||
"in-process value until the next reconnect",
|
||||
HOME_CHANNEL_ENV,
|
||||
exc_info=True,
|
||||
exc,
|
||||
)
|
||||
# Mirror into this process exactly as enrollment does (`cli.py`): the
|
||||
# running gateway resolves the home channel from the environment and must
|
||||
|
||||
@@ -80,16 +80,6 @@ def delivery_id() -> str:
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
def delivery_id() -> str:
|
||||
"""Mint the idempotency key for one home delivery.
|
||||
|
||||
Stable across retries by construction: the id is minted once, when the
|
||||
delivery is created, and the queued copy carries it verbatim through every
|
||||
flush. T3 dedupes on it, which is what makes double-flushing safe.
|
||||
"""
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
def iso_now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
|
||||
@@ -372,52 +362,6 @@ def home_deliver(
|
||||
text=normalized_text,
|
||||
createdAt=created_at or iso_now(),
|
||||
)
|
||||
|
||||
|
||||
def home_deliver(
|
||||
*,
|
||||
delivery_id_value: str,
|
||||
thread_id: str,
|
||||
kind: str,
|
||||
label: str,
|
||||
text: str,
|
||||
created_at: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Build a `home.deliver` frame with every wire bound already applied.
|
||||
|
||||
Like `protocol_error`, this wraps the generic `frame()` helper rather than
|
||||
assembling the envelope itself — but unlike the plain outbound frames the
|
||||
adapter builds inline, a delivery has server-side validation the plugin
|
||||
must not trip: `label` is trimmed non-empty <=200 chars and `text` is
|
||||
1..120000 chars in the T3 contract. Normalizing here rather than at the
|
||||
call sites means a queued delivery is already wire-valid on disk, so a
|
||||
flush after a plugin upgrade cannot resurrect a payload the server will
|
||||
reject — the plugin would purge it only on an ack that never comes.
|
||||
|
||||
An unknown `kind` degrades to `"other"` and an empty label to `"Hermes"`:
|
||||
a misclassified badge is the documented worst case, a dropped delivery is
|
||||
not.
|
||||
"""
|
||||
normalized_kind = str(kind or "").strip().lower()
|
||||
if normalized_kind not in HOME_DELIVERY_KINDS:
|
||||
normalized_kind = "other"
|
||||
normalized_label = str(label or "").strip()[:MAX_HOME_DELIVERY_LABEL_CHARS].strip()
|
||||
if not normalized_label:
|
||||
normalized_label = "Hermes"
|
||||
normalized_text = str(text or "")[:MAX_HOME_DELIVERY_TEXT_CHARS]
|
||||
if not normalized_text:
|
||||
normalized_text = " "
|
||||
return frame(
|
||||
"home.deliver",
|
||||
deliveryId=delivery_id_value,
|
||||
threadId=str(thread_id),
|
||||
kind=normalized_kind,
|
||||
label=normalized_label,
|
||||
text=normalized_text,
|
||||
createdAt=created_at or iso_now(),
|
||||
)
|
||||
|
||||
|
||||
def protocol_error(
|
||||
code: str,
|
||||
message: str,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# Tool configuration only — the plugin is installed by copying the directory
|
||||
# (see install.sh), not packaged, so there is deliberately no [project] table.
|
||||
|
||||
[tool.ruff]
|
||||
# The plugin's floor: the code uses `X | None` unions and dict/list generics
|
||||
# that require Python 3.10+.
|
||||
target-version = "py310"
|
||||
|
||||
[tool.ruff.lint]
|
||||
# F catches real bugs — F811 (redefinition) is the rule that would have
|
||||
# flagged the duplicated definitions this config was added alongside.
|
||||
select = ["E", "W", "F"]
|
||||
# The plugin favors long explanatory docstrings and comments; do not enforce a
|
||||
# line length rather than reflowing existing prose.
|
||||
ignore = ["E501"]
|
||||
@@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import contextlib
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
Reference in new issue
Block a user