Files
hermes-agent/gateway/session_client.py
unsupportedpastels d0b49116ec feat(gateway): a client can name itself on session.create
A third-party client can now say who it is: session.create takes an optional
client {name, version?}. It is attribution only. source still picks the
policy, and no toolset, approval, permission, prompt text or telemetry reads
the label.

- Validated beside the editor check (bounded, safe charset); a malformed value
  is invalid_params on field client.
- Kept inside the frozen creation request, so there is no new column, receipt
  key or policy field: an older owner restores the session, and the label is
  part of the exact-retry identity.
- session.info.client echoes it (absent when none was sent); runtime.describe
  lists client in session_create.parameters and capability session-client-v1.
- Plugins read it with hermes_cli.plugin_session.session_client() (published
  in plugin_api.json) and the client / client_version keys of prompt-section
  session info ("" when none). No PluginContext method, no hook kwarg.
- The classic CLI's /new copies the frozen launch request but never the
  creating client's label: the new session belongs to the terminal.
- Contracts regenerated; docs say the label is self-declared and must never
  grant anything.
2026-11-06 20:48:57 -05:00

41 lines
1.9 KiB
Python

"""The client a local session's creator declared itself as (``session.create`` ``client``).
A third-party client names itself (``{"name": "my-client", "version": "1.2.0"}``) so a viewer or a
plugin can tell which client created the session. It is a self-declared label, never an
authentication: no policy, permission, toolset, approval, prompt or telemetry path reads it.
It rides the frozen creation request (``LocalSessionPolicy.request_json``) as sent, so it needs no new
column, receipt key or policy field: an older owner restores the session unchanged, and it is part of
the exact-retry identity (a retry naming another client is a different request).
"""
from __future__ import annotations
import json
import re
_NAME = re.compile(r'[a-z0-9][a-z0-9._-]{0,63}')
_VERSION = re.compile(r'[!-~]{1,64}') # printable ASCII, no whitespace
def client_problem(value) -> str | None:
"""Why *value* is not a client label (``None`` when it is one)."""
if not isinstance(value, dict) or 'name' not in value or not set(value) <= {'name', 'version'}:
return 'must be an object with a name and an optional version'
if not isinstance(value['name'], str) or not _NAME.fullmatch(value['name']):
return 'name must be 1-64 characters of a-z, 0-9, ".", "_" or "-", starting with a letter or digit'
if 'version' in value and (not isinstance(value['version'], str) or not _VERSION.fullmatch(value['version'])):
return 'version must be 1-64 printable ASCII characters without spaces'
return None
def policy_client(policy) -> dict | None:
"""A copy of the client label *policy*'s creation request carries, or ``None`` (none was sent,
no local policy, or a stored value that is not a valid label)."""
if policy is None:
return None
try:
value = json.loads(policy.request_json).get('client')
except (AttributeError, TypeError, ValueError):
return None
return dict(value) if client_problem(value) is None else None