With one always-on gateway per host, `hermes sessions prune` always found
the owner holding state.db and refused (live smoke LS-F7). The only ways
past it were `gateway stop`, which the next client undoes by restarting
the gateway, or `--force`, which makes the CLI a second writer under the
owner. `hermes sessions delete` already worked against the same gateway.
Now, when a ready gateway serves the home (discovery only, it never
starts one), prune opens the store read-only for its preview (the pinned
note, the open-session note, `--dry-run`, the confirm prompt; all
unchanged). It then sends the filters it built to the owner as
`session.prune`. The owner deletes through `prune_sessions` /
`retire_prunable`, the path housekeeping uses, so busy sessions and the
logical owner of a retained reset child are skipped. It then drops the
deleted ids from its own memory (routes, launch secrets, live sessions,
cached agents) through the helper canonical delete now shares
(`retire_live_sessions`). `--never-active` goes the same way. With no
gateway running, prune is the local command it was, holder scan and
`--force` included.
`session.prune` deletes across every principal's rows, so it is open
only to the native local operator ticket from this host's private
control socket. A viewer or a remote/browser grant gets
`permission_denied`. The contract is declared and the generated TS and
OpenRPC files are regenerated. The CLI help and the two user-guide
passages that promised a refusal are updated.
Red on 30b63bda20 (all touched sources swapped back), green after:
tests/hermes_cli/test_sessions_prune_via_owner.py, 3 tests. A real
foreign process holds the store; prune applies through the owner with
the right rows, keeps the reset owner and its child's history, the CLI
opened only read-only, and non-operator grants are refused.
Live, a real `gateway run` in a fake HOME. Before: `prune --yes` printed
"Refusing ... PID <gateway>", rc=1. After: --source cron pruned 1; a bare
--older-than 0 pruned 2 and kept the reset owner and its open child; the
next turn on that session completed; strace shows the CLI opened
state.db O_RDONLY only. With no gateway the local prune still works and
does not start one.
The noqa on canonical.py's trailing import is removed. Neither ruff
config flags that import without it, and strict ruff's RUF100 rejected
the directive, which blocked the lefthook.
Review: https://github.com/NousResearch/hermes-agent/pull/106742
(cherry picked from commit 1e2dd5a49f04439b53a248aab24df9b10f4c7f9e)
39 lines
1.8 KiB
Python
39 lines
1.8 KiB
Python
"""Bulk prune while the gateway is up: the owner applies it, this process never writes.
|
|
|
|
The one always-on gateway owns ``state.db``; a CLI rewriting it underneath is the second writer
|
|
the held-store refusal exists to stop. So when a ready owner serves this home, ``hermes sessions
|
|
prune`` reads its preview from a read-only view and sends the selected filters to the owner
|
|
(``session.prune``), which deletes through the same ``retire_prunable`` path housekeeping uses.
|
|
With no gateway, prune stays the local command it always was (holder scan, ``--force``).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
#: A cron-heavy store prunes tens of thousands of rows in one transaction; the owner shields the
|
|
#: write, so a short client budget would only misreport a prune that still commits.
|
|
PRUNE_RPC_TIMEOUT = 600
|
|
|
|
|
|
def live_owner():
|
|
"""The ready gateway endpoint serving this home, else None. Discovery only: never starts one."""
|
|
from hermes_cli.gateway_runtime import discover_gateway_endpoint
|
|
from hermes_constants import get_hermes_home
|
|
found = discover_gateway_endpoint(get_hermes_home().resolve())
|
|
return found.endpoint if found.state == "ready" else None
|
|
|
|
|
|
def owner_prune(endpoint, **params) -> dict:
|
|
"""Run one ``session.prune`` on *endpoint*; raises ``GatewayClientError`` on refusal/loss."""
|
|
import asyncio
|
|
from hermes_cli.gateway_client import connect_gateway
|
|
|
|
async def _run():
|
|
async with connect_gateway(endpoint) as client:
|
|
return await client.rpc("session.prune", _timeout=PRUNE_RPC_TIMEOUT, **params)
|
|
return asyncio.run(_run())
|
|
|
|
|
|
def report_owner_failure(exc) -> int:
|
|
print(f"Error: the gateway did not complete the prune ({exc}). Run `hermes sessions prune --dry-run` "
|
|
"with the same filters to see what is left.")
|
|
return 1
|