Two more ways a held turn (hold_stream=True transform_llm_output) left history out of step with what the user got: - When verify-on-stop or pre_verify sent the answer back on the last allowed call, _append_interim_answer had already flushed it as a hidden row. The finalizer reused it as the budget fallback and transformed it, but the row was settled: _store_rewrite_on_tail skipped it and _close_transcript_tail added nothing (the tail already held assistant text), so reopening the session lost the reply. A held turn's sent-back answer now flushes with the turn instead of at once (the user never saw it), and when the turn ends on it _store_rewrite_on_tail publishes that row: visible, content = the rewrite, api_content = the bytes the model wrote and already saw, so the wire and the cached prefix stay unchanged. One row, so role alternation holds. - A codex_responses reply can carry commentary and the final answer in codex_message_items. Publishing the held final unhid the whole row, and project_history_commentary rebuilt display_commentary from those items on reload. publish_held_final_row now stamps display_metadata commentary_withheld on such a row; the projection then shows no commentary for it, while the items stay for model replay. Also: the plugin-reload hold test waited 0.5 s for a negative check; it now signals that the read started and waits 2 s (tests/AGENTS.md). Tests (red on 72fd611fef): a pre_verify fallback ending a held turn (history shows the rewrite, the resumed request replays the original bytes), and a held codex_responses final with a commentary item (FakeResponsesServer Message gains an optional phase). Review: https://github.com/NousResearch/hermes-agent/pull/144266#discussion_r4370905634 Review: https://github.com/NousResearch/hermes-agent/pull/144266#discussion_r4370905635 Review: https://github.com/NousResearch/hermes-agent/pull/144266#discussion_r4370905636
123 lines
5.3 KiB
Python
123 lines
5.3 KiB
Python
"""``register_hook("transform_llm_output", fn, hold_stream=True)``: the declaration and its scope.
|
|
|
|
A plugin that rewrites the reply (redaction) must see it before the user does. Declaring
|
|
``hold_stream=True`` holds the streamed reply of every session in that plugin's profile; a plain
|
|
registration keeps streaming. The hold lives as long as the registration and never crosses into
|
|
another profile's plugin manager.
|
|
"""
|
|
|
|
import hermes_yaml as yaml
|
|
import pytest
|
|
|
|
from hermes_cli.plugins import PluginContext, PluginManager, PluginManifest
|
|
|
|
REDACTOR = (
|
|
"def register(ctx):\n"
|
|
" ctx.register_hook('transform_llm_output', lambda response_text, **_: response_text, hold_stream=True)\n"
|
|
)
|
|
|
|
|
|
def test_the_hold_lasts_as_long_as_the_registration_that_declared_it():
|
|
manager = PluginManager()
|
|
ctx = PluginContext(PluginManifest(name="redactor", source="user"), manager)
|
|
ctx.register_hook("transform_llm_output", lambda **_: None)
|
|
assert manager.holds_stream() is False # a rewrite that did not ask keeps streaming
|
|
|
|
held = ctx.register_hook("transform_llm_output", lambda **_: None, hold_stream=True)
|
|
assert manager.holds_stream() is True
|
|
|
|
held.dispose()
|
|
assert manager.holds_stream() is False
|
|
assert manager.has_hook("transform_llm_output") # the undeclared registration is still live
|
|
|
|
|
|
def test_only_the_declaring_profile_holds(tmp_path, monkeypatch):
|
|
from hermes_cli import plugins
|
|
from hermes_cli.plugins_dispatch import stream_held
|
|
|
|
monkeypatch.setenv("HERMES_ENABLE_PROJECT_PLUGINS", "0")
|
|
monkeypatch.setenv("HERMES_BUNDLED_PLUGINS", str(tmp_path / "no-bundled"))
|
|
homes = {name: tmp_path / name for name in ("alpha", "beta")}
|
|
for name, home in homes.items():
|
|
plugin = home / "plugins" / "redactor"
|
|
plugin.mkdir(parents=True)
|
|
(plugin / "plugin.yaml").write_text(yaml.safe_dump({"name": "redactor", "version": "0.1.0"}))
|
|
(plugin / "__init__.py").write_text(REDACTOR if name == "alpha" else REDACTOR.replace(", hold_stream=True", ""))
|
|
(home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["redactor"]}}))
|
|
|
|
plugins._reset_plugin_managers_for_tests()
|
|
try:
|
|
seen = []
|
|
for name in ("alpha", "beta", "alpha"):
|
|
monkeypatch.setenv("HERMES_HOME", str(homes[name]))
|
|
seen.append(stream_held())
|
|
assert seen == [True, False, True]
|
|
finally:
|
|
plugins._reset_plugin_managers_for_tests()
|
|
|
|
|
|
@pytest.mark.parametrize("hook", ["post_llm_call", "transform_tool_result"])
|
|
def test_a_hold_on_a_hook_that_does_not_rewrite_the_reply_holds_nothing(hook, caplog):
|
|
manager = PluginManager()
|
|
ctx = PluginContext(PluginManifest(name="observer", source="user"), manager)
|
|
ctx.register_hook(hook, lambda **_: None, hold_stream=True)
|
|
assert manager.holds_stream() is False
|
|
assert "hold_stream" in caplog.text
|
|
|
|
|
|
def test_a_turn_starting_during_a_plugin_reload_sees_the_settled_hold(tmp_path, monkeypatch):
|
|
"""``reload-plugins`` unloads every registration, then loads them again. A turn that asks in
|
|
between must wait for the reload instead of reading the empty ledger and streaming raw text."""
|
|
import sys
|
|
import threading
|
|
import types
|
|
|
|
from hermes_cli import plugins
|
|
|
|
gate = types.ModuleType("hold_stream_reload_gate")
|
|
gate.loading, gate.entered, gate.release = threading.Event(), threading.Event(), threading.Event()
|
|
monkeypatch.setitem(sys.modules, gate.__name__, gate)
|
|
monkeypatch.setenv("HERMES_ENABLE_PROJECT_PLUGINS", "0")
|
|
monkeypatch.setenv("HERMES_BUNDLED_PLUGINS", str(tmp_path / "no-bundled"))
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
plugin = tmp_path / "plugins" / "redactor"
|
|
plugin.mkdir(parents=True)
|
|
(plugin / "plugin.yaml").write_text(yaml.safe_dump({"name": "redactor", "version": "0.1.0"}))
|
|
(plugin / "__init__.py").write_text(
|
|
"import hold_stream_reload_gate as gate\n"
|
|
"def register(ctx):\n"
|
|
" if gate.loading.is_set():\n"
|
|
" gate.entered.set()\n"
|
|
" gate.release.wait(10)\n"
|
|
" ctx.register_hook('transform_llm_output', lambda response_text, **_: response_text, hold_stream=True)\n")
|
|
(tmp_path / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["redactor"]}}))
|
|
|
|
plugins._reset_plugin_managers_for_tests()
|
|
try:
|
|
manager = plugins.get_plugin_manager()
|
|
manager.discover_and_load()
|
|
assert manager.holds_stream() is True
|
|
gate.loading.set()
|
|
reload = threading.Thread(target=manager.discover_and_load, kwargs={"force": True})
|
|
reload.start()
|
|
assert gate.entered.wait(10) # the reload unloaded everything and is re-registering
|
|
answers: list[bool] = []
|
|
reading = threading.Event()
|
|
|
|
def read() -> None:
|
|
reading.set()
|
|
answers.append(manager.holds_stream())
|
|
|
|
turn = threading.Thread(target=read)
|
|
turn.start()
|
|
assert reading.wait(10) # the read has started: from here only the reload lock can hold it
|
|
turn.join(2.0)
|
|
assert answers == [] and turn.is_alive() # the turn waits while the plugin is mid-reload
|
|
gate.release.set()
|
|
reload.join(10)
|
|
turn.join(10)
|
|
assert answers == [True]
|
|
finally:
|
|
gate.release.set()
|
|
plugins._reset_plugin_managers_for_tests()
|