Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions agent/agent_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def init_agent(
checkpoint_max_total_size_mb: int = 500,
checkpoint_max_file_size_mb: int = 10,
pass_session_id: bool = False,
prefetch_after_turn: bool = True,
):
"""
Initialize the AI Agent.
Expand Down Expand Up @@ -317,6 +318,7 @@ def init_agent(
agent.skip_context_files = skip_context_files
agent.load_soul_identity = load_soul_identity
agent.pass_session_id = pass_session_id
agent.prefetch_after_turn = prefetch_after_turn
agent._credential_pool = credential_pool
agent.log_prefix_chars = log_prefix_chars
agent.log_prefix = f"{log_prefix} " if log_prefix else ""
Expand Down
12 changes: 10 additions & 2 deletions hermes_cli/oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def _run_agent(
# - dangerous-command approval → bypassed via HERMES_YOLO_MODE=1
# - skill secret capture → returns gracefully when no callback set
clarify_callback=_oneshot_clarify_callback,
# Oneshot has no next turn, so don't warm one.
prefetch_after_turn=False,
)

# Belt-and-braces: make sure AIAgent doesn't invoke any streaming
Expand All @@ -369,8 +371,14 @@ def _run_agent(
agent.stream_delta_callback = None
agent.tool_gen_callback = None

result = agent.run_conversation(prompt)
return (result.get("final_response") or "", result)
try:
result = agent.run_conversation(prompt)
return (result.get("final_response") or "", result)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AIAgent does not retain its conversation on messages; _persist_session() stores it as _session_messages (run_agent.py:1693). Passing this fallback empty list reaches MemoryManager.on_session_end() and drops the transcript for providers that perform end-of-session extraction. Please mirror the _session_messages/no-argument fallback used by CLI cleanup.

finally:
try:
agent.shutdown_memory_provider(getattr(agent, "messages", []))
except Exception:
pass


def _oneshot_clarify_callback(question: str, choices=None) -> str:
Expand Down
11 changes: 7 additions & 4 deletions run_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ def __init__(
checkpoint_max_total_size_mb: int = 500,
checkpoint_max_file_size_mb: int = 10,
pass_session_id: bool = False,
prefetch_after_turn: bool = True,
):
"""Forwarder — see ``agent.agent_init.init_agent``."""
from agent.agent_init import init_agent
Expand Down Expand Up @@ -570,6 +571,7 @@ def __init__(
checkpoint_max_total_size_mb=checkpoint_max_total_size_mb,
checkpoint_max_file_size_mb=checkpoint_max_file_size_mb,
pass_session_id=pass_session_id,
prefetch_after_turn=prefetch_after_turn,
)

def _get_session_db_for_recall(self):
Expand Down Expand Up @@ -3375,10 +3377,11 @@ def _sync_external_memory_for_turn(
response_text,
**sync_kwargs,
)
self._memory_manager.queue_prefetch_all(
user_text,
session_id=self.session_id or "",
)
if getattr(self, "prefetch_after_turn", True):
self._memory_manager.queue_prefetch_all(
user_text,
session_id=self.session_id or "",
)
except Exception:
pass

Expand Down
35 changes: 35 additions & 0 deletions tests/hermes_cli/test_tui_resume_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,41 @@ def test_oneshot_exit_code_zero_when_failed_with_error_text(monkeypatch, capsys)
assert "HTTP 404" in capsys.readouterr().out


def test_oneshot_agent_disables_next_turn_prefetch(monkeypatch):
_stub_plugin_discovery(monkeypatch)
import hermes_cli.oneshot as oneshot_mod

captured = {}
result = {"final_response": "ok"}

class FakeAgent:
def __init__(self, **kwargs):
captured.update(kwargs)
self.suppress_status_output = False
self.stream_delta_callback = object()
self.tool_gen_callback = object()
self.messages = [{"role": "user", "content": "hello"}]
self.shutdown_calls = []

def run_conversation(self, _prompt):
return result

def shutdown_memory_provider(self, messages):
self.shutdown_calls.append(messages)
captured["shutdown_calls"] = list(self.shutdown_calls)

import hermes_cli.runtime_provider as runtime_provider_mod
import run_agent as run_agent_mod

monkeypatch.setattr(run_agent_mod, "AIAgent", FakeAgent)
monkeypatch.setattr(runtime_provider_mod, "resolve_runtime_provider", lambda **_kw: {})
monkeypatch.setattr(oneshot_mod, "_create_session_db_for_oneshot", lambda: object())

assert oneshot_mod._run_agent("hello") == ("ok", result)
assert captured["prefetch_after_turn"] is False
assert captured["shutdown_calls"] == [[{"role": "user", "content": "hello"}]]


def test_oneshot_reraises_keyboard_interrupt(monkeypatch):
_stub_plugin_discovery(monkeypatch)
import hermes_cli.oneshot as oneshot_mod
Expand Down
18 changes: 18 additions & 0 deletions tests/run_agent/test_memory_sync_interrupted.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _bare_agent():
# providers that cache per-session state can update it mid-process
# (see #6672).
agent.session_id = "test_session_001"
setattr(agent, "prefetch_after_turn", True)
return agent


Expand Down Expand Up @@ -91,6 +92,23 @@ def test_completed_turn_syncs_and_queues_prefetch(self):
session_id="test_session_001",
)

def test_completed_oneshot_turn_syncs_but_skips_next_turn_prefetch(self):
"""Oneshot should save memory, but not warm a turn that cannot exist."""
agent = _bare_agent()
setattr(agent, "prefetch_after_turn", False)

agent._sync_external_memory_for_turn(
original_user_message="remember this",
final_response="remembered",
interrupted=False,
)

agent._memory_manager.sync_all.assert_called_once_with(
"remember this", "remembered",
session_id="test_session_001",
)
agent._memory_manager.queue_prefetch_all.assert_not_called()

def test_completed_turn_syncs_messages_when_present(self):
agent = _bare_agent()
messages = [
Expand Down