Merge remote-tracking branch 'upstream/main' into fix/dedup-codex-app-server-final

This commit is contained in:
Haider Sultan 2026-08-01 17:45:42 +08:00
commit 0f2b5a1d63
52 changed files with 2965 additions and 220 deletions

View File

@ -247,16 +247,22 @@ def main(argv: list[str] | None = None) -> None:
import acp
from .server import HermesACPAgent
# MCP tool discovery from config.yaml — run before asyncio.run() so
# it's safe to use blocking waits. (ACP also registers per-session
# MCP servers dynamically via asyncio.to_thread inside the event
# loop; that path is unaffected.) Moved from model_tools.py module
# scope to avoid freezing the gateway's loop on lazy import (#16856).
# MCP tool discovery from config.yaml — fire-and-forget in a
# background daemon thread so the ACP server becomes responsive
# immediately while MCP servers connect. Previously this blocked
# asyncio.run() for 2-5 s. (ACP also registers per-session MCP
# servers dynamically via asyncio.to_thread inside the event loop;
# that path is unaffected.) Moved from model_tools.py module scope
# to avoid freezing the gateway's loop on lazy import (#16856).
# Metadata-only hosts can opt out of unrelated global MCP startup.
if os.environ.get("HERMES_ACP_SKIP_CONFIGURED_MCP", "").strip() != "1":
try:
from tools.mcp_tool import discover_mcp_tools
discover_mcp_tools()
from hermes_cli.mcp_startup import start_background_mcp_discovery
start_background_mcp_discovery(
logger=logger,
thread_name="acp-mcp-discovery",
)
except Exception:
logger.debug("MCP tool discovery failed at ACP startup", exc_info=True)

View File

@ -1037,6 +1037,102 @@ class HermesACPAgent(acp.Agent):
exc_info=True,
)
def _schedule_mcp_late_refresh(self, state: SessionState) -> None:
"""Refresh the agent's tool snapshot when background MCP discovery lands late.
ACP entry.py starts MCP tool discovery in a background daemon thread so a
slow/dead configured server can't block ``asyncio.run()``. ``_make_agent``
briefly joins that thread (``wait_for_mcp_discovery``, bounded ~1.5s) so
already-spawning fast servers land in the snapshot — but a server slower
than the bound lands *after* the agent is built, leaving its tools absent
for the whole session.
This schedules an off-critical-path daemon that waits for discovery to
finish (bounded 30s), then rebuilds the snapshot via the shared
``refresh_agent_mcp_tools`` helper — the same rebuild ``/reload-mcp``
performs, but automatic. Mirrors the TUI late-refresh (PR #48403).
Cache safety: the rebuild only runs while the session is still
pre-first-turn (no API call made yet → nothing cached to invalidate).
Once the user has sent a message we leave the snapshot frozen rather
than break the cached prompt prefix mid-conversation; servers that land
later are picked up cache-safely by the between-turns prologue refresh
(``agent/turn_context.py``) at the next turn boundary. The marginal
value of this pre-first-turn daemon is therefore freshness in the
window [session created → first message] — e.g. the "Available tools"
listing a client may request before the first prompt.
No-op when discovery already finished, when the join times out, when the
registry was unchanged, or when the session was closed while waiting.
"""
try:
from hermes_cli.mcp_startup import mcp_discovery_in_flight
except Exception:
return
if not mcp_discovery_in_flight():
return
import threading
agent = state.agent
session_id = state.session_id
def _wait_then_refresh() -> None:
try:
from hermes_cli.mcp_startup import join_mcp_discovery
if not join_mcp_discovery(timeout=30.0):
return
# Session may have been closed while we waited. In-memory-only
# lookup on purpose: ``get_session()`` falls through to a DB
# restore that builds a whole new AIAgent as a side effect just
# to decide "no-op" here (the TUI equivalent also checks its
# in-memory dict only).
with self.session_manager._lock:
current = self.session_manager._sessions.get(session_id)
if current is None or current.agent is not agent:
return
# Cache safety: never rebuild the tool list once the conversation
# has started — that would invalidate the cached prompt prefix.
# Serialized with turn start: ``prompt()`` flips ``is_running``
# under ``runtime_lock`` before dispatching, so holding it here
# (and bailing when a turn is already running) closes the window
# where the guard passes but the first prompt starts before the
# refresh publishes — which would swap ``tools=`` mid-turn and
# break the just-created cache prefix.
with current.runtime_lock:
if current.is_running:
return
if (
int(getattr(agent, "_user_turn_count", 0) or 0) > 0
or int(getattr(agent, "_api_call_count", 0) or 0) > 0
):
return
from tools.mcp_tool import refresh_agent_mcp_tools
added = refresh_agent_mcp_tools(agent, quiet_mode=True)
if added:
logger.info(
"Session %s: late MCP refresh added %d tools: %s",
session_id,
len(added),
", ".join(sorted(added)),
)
except Exception:
logger.debug(
"Session %s: late MCP refresh failed",
session_id,
exc_info=True,
)
threading.Thread(
target=_wait_then_refresh,
name=f"acp-mcp-late-refresh-{session_id}",
daemon=True,
).start()
# ---- ACP lifecycle ------------------------------------------------------
async def initialize(
@ -1343,6 +1439,7 @@ class HermesACPAgent(acp.Agent):
) -> NewSessionResponse:
state = self.session_manager.create_session(cwd=cwd)
await self._register_session_mcp_servers(state, mcp_servers)
self._schedule_mcp_late_refresh(state)
logger.info("New session %s (cwd=%s)", state.session_id, cwd)
self._schedule_available_commands_update(state.session_id)
self._schedule_usage_update(state)
@ -1367,6 +1464,7 @@ class HermesACPAgent(acp.Agent):
logger.warning("load_session: session %s not found", session_id)
return None
await self._register_session_mcp_servers(state, mcp_servers)
self._schedule_mcp_late_refresh(state)
logger.info("Loaded session %s", session_id)
# Per ACP spec, `session/load` must stream the prior conversation back
# to the client via `session/update` notifications BEFORE responding,
@ -1414,6 +1512,7 @@ class HermesACPAgent(acp.Agent):
logger.warning("resume_session: session %s not found, creating new", session_id)
state = self.session_manager.create_session(cwd=cwd)
await self._register_session_mcp_servers(state, mcp_servers)
self._schedule_mcp_late_refresh(state)
logger.info("Resumed session %s", state.session_id)
# See `load_session` above for the spec rationale — replay must
# complete before the response so clients receive the full transcript

View File

@ -648,6 +648,30 @@ class SessionManager:
logger.debug("ACP session falling back to default provider resolution", exc_info=True)
_register_task_cwd(session_id, cwd)
# Bounded wait for background MCP discovery so already-spawning fast
# servers land in the agent's tool snapshot. ACP entry.py fires
# discovery in a background daemon thread (start_background_mcp_discovery);
# the agent snapshots tools once at build (run_agent/agent_init) and
# never re-reads the registry, so without this join a reachable-but-
# slow configured server would be invisible for the whole session.
# ``ensure_mcp_discovery_before_agent_build`` also (re)starts discovery
# when the entry.py spawn never ran or exited with zero connected
# servers (the retry-after-zero-connected allowance), making this
# construction site self-sufficient. Bounded by
# ``mcp_discovery_timeout`` (config.yaml, default ~1.5s) so a dead
# server can't block — servers that miss the bound are picked up by
# the automatic late-refresh (see HermesACPAgent._schedule_mcp_late_refresh).
try:
from hermes_cli.mcp_startup import ensure_mcp_discovery_before_agent_build
ensure_mcp_discovery_before_agent_build(
logger=logger,
thread_name="acp-mcp-discovery",
)
except Exception:
logger.debug("ACP: bounded MCP discovery wait failed", exc_info=True)
agent = AIAgent(**kwargs)
# Codex app-server sessions are spawned lazily on the first turn. Stamp
# the ACP workspace onto the agent so the Codex runtime starts from the

View File

@ -834,10 +834,19 @@ def init_agent(
agent._use_prompt_caching, agent._use_native_cache_layout = (
agent._anthropic_prompt_cache_policy()
)
agent._cache_disabled = False
# Anthropic supports "5m" (default) and "1h" cache TTL tiers. Read from
# config.yaml under prompt_caching.cache_ttl; unknown values keep "5m".
# 1h tier costs 2x on write vs 1.25x for 5m, but amortizes across long
# sessions with >5-minute pauses between turns (#14971).
#
# Setting cache_ttl to a falsy value (false / null / "off" / "disabled" /
# "no" / "none") disables prompt caching entirely. This is useful for
# OAuth subscription users where cache writes bill against "extra usage"
# or for third-party proxies that inject their own cache_control markers
# (#13477). The disable propagates through anthropic_prompt_cache_policy()
# and restore_primary_runtime() so it survives /model switches and
# fallback re-derivation (#33555).
agent._cache_ttl = "5m"
try:
from hermes_cli.config import load_config_readonly as _load_pc_cfg
@ -846,6 +855,15 @@ def init_agent(
_ttl = _pc_cfg.get("cache_ttl", "5m")
if _ttl in {"5m", "1h"}:
agent._cache_ttl = _ttl
elif (
_ttl is False
or _ttl is None
or str(_ttl).lower() in ("off", "false", "disabled", "no", "none")
):
agent._use_prompt_caching = False
agent._use_native_cache_layout = False
agent._cache_ttl = None
agent._cache_disabled = True
except Exception:
pass

View File

@ -1475,6 +1475,12 @@ def restore_primary_runtime(agent) -> bool:
"use_native_cache_layout",
agent.api_mode == "anthropic_messages" and agent.provider == "anthropic",
)
# If the operator has disabled caching via config (cache_ttl is
# falsy → _cache_disabled flag is set), the disable must survive
# runtime snapshot restoration (#33555).
if getattr(agent, "_cache_disabled", False):
agent._use_prompt_caching = False
agent._use_native_cache_layout = False
# ── Rebuild client for the primary provider ──
if agent.provider == "moa":
@ -1834,6 +1840,23 @@ def dump_api_request_debug(
def _direct_native_anthropic_tool_cache_capability(
agent,
*,
provider: Optional[str] = None,
base_url: Optional[str] = None,
api_mode: Optional[str] = None,
model: Optional[str] = None,
) -> bool:
"""Return whether this resolved destination accepts native tool markers."""
eff_base_url = base_url if base_url is not None else (agent.base_url or "")
eff_api_mode = api_mode if api_mode is not None else (agent.api_mode or "")
return (
eff_api_mode == "anthropic_messages"
and base_url_hostname(eff_base_url) == "api.anthropic.com"
)
def anthropic_prompt_cache_policy(
agent,
*,
@ -1866,7 +1889,19 @@ def anthropic_prompt_cache_policy(
documented this for opencode-go Qwen; #24617 reports the same gateway
contract for DeepSeek. Without markers these providers serve zero cache
hits, re-billing the full prompt on every turn.
If the operator has set ``prompt_caching.cache_ttl`` to a falsy value
(``false``, ``null``, ``"off"``, etc.) in config.yaml, prompt caching
is fully disabled — this early return ensures the disable survives
``/model`` switches, fallback re-derivation, and runtime snapshot
restoration (#33555). We check ``"_cache_disabled"`` (set by
init_agent when the disable is detected) rather than ``_cache_ttl``
directly, because ``_cache_ttl`` is not yet set when the policy runs
during the initial ``init_agent`` call.
"""
if getattr(agent, "_cache_disabled", False):
return (False, False)
eff_provider = (provider if provider is not None else agent.provider) or ""
eff_base_url = base_url if base_url is not None else (agent.base_url or "")
eff_api_mode = api_mode if api_mode is not None else (agent.api_mode or "")

View File

@ -42,6 +42,7 @@ Payment / credit exhaustion fallback:
import contextlib
import contextvars
import copy
import functools
import hashlib
import inspect
@ -54,7 +55,7 @@ import time
import uuid
from pathlib import Path # noqa: F401 — used by test mocks
from types import SimpleNamespace
from typing import Any, Callable, Dict, List, Optional, Tuple, TYPE_CHECKING
from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, TYPE_CHECKING
from urllib.parse import urlparse, parse_qs, urlunparse
# NOTE: `from openai import OpenAI` is deliberately NOT at module top — the
@ -4214,6 +4215,133 @@ def _fallback_entry_timeout(task: Optional[str], fb_label: str) -> Optional[floa
return None
def _fallback_provider_from_label(label: str) -> str:
"""Recover the provider identifier from a fallback display label."""
match = re.match(r"(?:fallback_chain\[\d+\]|main-agent)\(([^)]+)\)$", label or "")
return match.group(1).strip() if match else str(label or "").strip()
class _FallbackDestination(NamedTuple):
provider: str
base_url: str
api_mode: Optional[str]
model: Optional[str]
def _complete_fallback_destination(
provider: str,
base_url: str,
api_mode: Optional[str],
model: Optional[str],
) -> _FallbackDestination:
if not api_mode:
if _endpoint_speaks_anthropic_messages(base_url):
api_mode = "anthropic_messages"
else:
try:
from hermes_cli.runtime_provider import resolve_runtime_provider
runtime = resolve_runtime_provider(
requested=provider,
explicit_base_url=base_url or None,
target_model=model or "",
)
api_mode = str(runtime.get("api_mode") or "").strip() or None
except Exception:
pass
return _FallbackDestination(provider, base_url, api_mode, model)
def _fallback_destination_from_entry(
entry: Dict[str, Any],
fb_client: Any,
fb_model: Optional[str],
) -> _FallbackDestination:
provider = str(entry.get("provider") or "").strip()
base_url = str(
entry.get("base_url") or getattr(fb_client, "base_url", "") or ""
).strip()
api_mode = str(
entry.get("api_mode") or entry.get("transport") or ""
).strip() or None
model = fb_model or str(entry.get("model") or "").strip() or None
return _complete_fallback_destination(provider, base_url, api_mode, model)
def _fallback_destination(
task: Optional[str],
fb_client: Any,
fb_model: Optional[str],
fb_label: str,
) -> _FallbackDestination:
"""Return the resolved route identity used by a fallback request."""
attached = getattr(fb_client, "_hermes_fallback_destination", None)
if isinstance(attached, _FallbackDestination):
return attached
provider = _fallback_provider_from_label(fb_label)
base_url = str(getattr(fb_client, "base_url", "") or "")
api_mode = None
model = fb_model
match = re.match(r"fallback_chain\[(\d+)\]", fb_label or "")
if match and task:
try:
chain = _get_auxiliary_task_config(task).get("fallback_chain")
entry = chain[int(match.group(1))] if isinstance(chain, list) else None
except Exception:
entry = None
if isinstance(entry, dict):
return _fallback_destination_from_entry(entry, fb_client, fb_model)
return _complete_fallback_destination(provider, base_url, api_mode, model)
def _replan_synchronous_cache_sections(
messages: list,
tools: Optional[list],
*,
destination: _FallbackDestination,
) -> tuple[list, list]:
"""Strip source decoration and plan one synchronous destination locally."""
from agent.agent_runtime_helpers import (
_direct_native_anthropic_tool_cache_capability,
anthropic_prompt_cache_policy,
)
from agent.prompt_caching import (
build_prompt_cache_plan,
strip_anthropic_cache_control,
strip_anthropic_tool_cache_control,
)
canonical_messages = copy.deepcopy(messages or [])
strip_anthropic_cache_control(canonical_messages)
canonical_tools = strip_anthropic_tool_cache_control(tools)
stub = SimpleNamespace(provider="", base_url="", api_mode="", model="")
should_cache, native_layout = anthropic_prompt_cache_policy(
stub,
provider=destination.provider,
base_url=destination.base_url,
api_mode=destination.api_mode or "",
model=destination.model or "",
)
if not should_cache:
return canonical_messages, canonical_tools
plan = build_prompt_cache_plan(
canonical_messages,
canonical_tools,
native_anthropic=native_layout,
direct_native_tool_cache=_direct_native_anthropic_tool_cache_capability(
stub,
provider=destination.provider,
base_url=destination.base_url,
api_mode=destination.api_mode or "",
model=destination.model or "",
),
)
return plan.messages, plan.tools
def _call_fallback_candidate_sync(
fb_client: Any,
fb_model: Optional[str],
@ -4256,36 +4384,70 @@ def _call_fallback_candidate_sync(
task or "call", fb_label, fb_timeout, effective_timeout,
)
effective_timeout = fb_timeout
fb_base = str(getattr(fb_client, "base_url", "") or "")
destination = _fallback_destination(task, fb_client, fb_model, fb_label)
fallback_messages, fallback_tools = _replan_synchronous_cache_sections(
messages,
tools,
destination=destination,
)
fb_kwargs = _build_call_kwargs(
fb_label, fb_model, messages,
destination.provider, destination.model, fallback_messages,
temperature=temperature, max_tokens=max_tokens,
tools=tools, timeout=effective_timeout,
tools=fallback_tools, timeout=effective_timeout,
extra_body=effective_extra_body, reasoning_config=reasoning_config,
base_url=fb_base, task=task)
base_url=destination.base_url, task=task)
try:
return _validate_llm_response(
_relay_sync_completion(fb_client, fb_kwargs, provider=fb_label), task)
_relay_sync_completion(
fb_client,
fb_kwargs,
provider=destination.provider,
api_mode=destination.api_mode,
),
task,
)
except Exception as fb_err:
if not _is_auth_error(fb_err):
raise
fb_provider = _auth_refresh_provider_for_route(fb_label, fb_base)
fb_provider = _auth_refresh_provider_for_route(
destination.provider, destination.base_url
)
if fb_provider not in {"auto", "", None} and _refresh_provider_credentials(fb_provider):
retry_client, retry_model = _get_cached_client(fb_provider, fb_model)
retry_client, retry_model = _get_cached_client(
fb_provider,
destination.model,
base_url=destination.base_url or None,
api_mode=destination.api_mode,
)
if retry_client is not None:
retry_destination = _FallbackDestination(
fb_provider,
destination.base_url
or str(getattr(retry_client, "base_url", "") or ""),
destination.api_mode,
retry_model or destination.model,
)
retry_messages, retry_tools = _replan_synchronous_cache_sections(
messages,
tools,
destination=retry_destination,
)
retry_kwargs = _build_call_kwargs(
fb_provider, retry_model or fb_model, messages,
retry_destination.provider,
retry_destination.model,
retry_messages,
temperature=temperature, max_tokens=max_tokens,
tools=tools, timeout=effective_timeout,
tools=retry_tools, timeout=effective_timeout,
extra_body=effective_extra_body,
reasoning_config=reasoning_config,
base_url=str(getattr(retry_client, "base_url", "") or fb_base), task=task)
base_url=retry_destination.base_url, task=task)
try:
return _validate_llm_response(
_relay_sync_completion(
retry_client,
retry_kwargs,
provider=fb_provider,
provider=retry_destination.provider,
api_mode=retry_destination.api_mode,
),
task,
)
@ -4761,13 +4923,21 @@ def _resolve_fallback_entry(entry: Dict[str, Any]) -> Tuple[Optional[Any], Optio
base_url = str(entry.get("base_url") or "").strip() or None
api_key = _fallback_entry_api_key(entry)
api_mode = str(entry.get("api_mode") or entry.get("transport") or "").strip() or None
return resolve_provider_client(
client, resolved_model = resolve_provider_client(
provider,
model=model,
explicit_base_url=base_url,
explicit_api_key=api_key,
api_mode=api_mode,
)
if client is not None:
try:
client._hermes_fallback_destination = _fallback_destination_from_entry(
entry, client, resolved_model
)
except Exception:
pass
return client, resolved_model
def _try_main_fallback_chain(

View File

@ -1120,9 +1120,10 @@ def interruptible_api_call(agent, api_kwargs: dict):
def build_api_kwargs(agent, api_messages: list) -> dict:
def build_api_kwargs(agent, api_messages: list, tools_for_api: list | None = None) -> dict:
"""Build the keyword arguments dict for the active API mode."""
tools_for_api = agent.tools
if tools_for_api is None:
tools_for_api = agent.tools
if agent.api_mode == "anthropic_messages":
_transport = agent._get_transport()

View File

@ -70,8 +70,9 @@ from agent.model_metadata import (
)
from agent.process_bootstrap import _install_safe_stdio
from agent.prompt_caching import (
apply_anthropic_cache_control,
build_prompt_cache_plan,
strip_anthropic_cache_control,
strip_anthropic_tool_cache_control,
)
from agent.retry_utils import (
adaptive_rate_limit_backoff,
@ -941,7 +942,8 @@ def _redecorate_prompt_cache_for_provider(
*,
system_message=None,
moa_prepared: Optional[Dict[str, Any]] = None,
) -> tuple[List[Dict[str, Any]], Optional[Dict[str, Any]]]:
tools_for_api: Optional[List[Dict[str, Any]]] = None,
) -> tuple[List[Dict[str, Any]], Optional[Dict[str, Any]]] | tuple[List[Dict[str, Any]], Optional[Dict[str, Any]], List[Dict[str, Any]]]:
"""Strip and re-apply cache_control for the *current* provider policy.
Decoration runs once per call block before the retry loop for the primary
@ -951,10 +953,9 @@ def _redecorate_prompt_cache_for_provider(
by reshaping at the top of each retry attempt.
The source list is the mutated in-flight request (image shrink / ASCII /
reasoning_details recoveries already applied) — never a pristine
pre-decoration snapshot. MoA guidance is peeled, the base is redecorated,
then ``rebase_prepared_request`` re-attaches guidance outside the cached
span.
reasoning_details recoveries already applied), never a pristine
pre-decoration snapshot. MoA guidance is peeled and rebased without
decoration; the acting aggregator plans its resolved destination later.
"""
messages: List[Dict[str, Any]] = [
dict(m) if isinstance(m, dict) else m for m in (api_messages or [])
@ -965,6 +966,21 @@ def _redecorate_prompt_cache_for_provider(
messages = _peel_moa_guidance(messages, guidance)
strip_anthropic_cache_control(messages)
planned_tools = strip_anthropic_tool_cache_control(
tools_for_api if tools_for_api is not None else getattr(agent, "tools", [])
)
if prepared is not None and getattr(agent, "provider", None) == "moa":
# Prepared MoA state is canonical: the synchronous acting-aggregator
# sender owns its destination-local cache plan after it resolves the slot.
completions = getattr(getattr(agent.client, "chat", None), "completions", None)
rebase = getattr(completions, "rebase_prepared_request", None)
if callable(rebase):
prepared = rebase(prepared, messages)
messages = prepared["messages"]
if tools_for_api is None:
return messages, prepared
return messages, prepared, planned_tools
# Direct attribute access matches the call-block decoration site — the
# flags are unconditionally initialized on AIAgent, and a getattr
@ -972,31 +988,25 @@ def _redecorate_prompt_cache_for_provider(
if agent._use_prompt_caching:
_ensure_cached_system_prompt_static(agent, system_message=system_message)
static = getattr(agent, "_cached_system_prompt_static", None)
messages = apply_anthropic_cache_control(
direct_tool_cache = getattr(
agent,
"_direct_native_anthropic_tool_cache_capability",
lambda: False,
)()
plan = build_prompt_cache_plan(
messages,
planned_tools,
cache_ttl=agent._cache_ttl,
native_anthropic=agent._use_native_cache_layout,
static_system_prefix=static if isinstance(static, str) else None,
direct_native_tool_cache=direct_tool_cache,
)
messages = plan.messages
planned_tools = plan.tools
if (
prepared is not None
and getattr(agent, "provider", None) == "moa"
):
# No `and guidance` here: guidance=None is a real prepared shape
# (all-references-failed / silent degraded policy builds the
# prepared request without attaching guidance), and the MoA facade
# sends prepared["messages"] — not api_kwargs["messages"] — so the
# rebase must refresh the prepared object even when there is no
# guidance to re-attach. rebase_prepared_request handles falsy
# guidance by copying the messages and skipping the attach.
completions = getattr(getattr(agent.client, "chat", None), "completions", None)
rebase = getattr(completions, "rebase_prepared_request", None)
if callable(rebase):
prepared = rebase(prepared, messages)
messages = prepared["messages"]
return messages, prepared
if tools_for_api is None:
return messages, prepared
return messages, prepared, planned_tools
def _apply_context_engine_selection(
@ -1748,12 +1758,8 @@ def run_conversation(
# regardless of ordering (a single-space pad here previously had to
# be sequenced after normalization to survive, forking the concept).
# Apply Anthropic prompt caching for Claude models on native
# Anthropic, OpenRouter, and third-party Anthropic-compatible
# gateways. Auto-detected: if ``_use_prompt_caching`` is set, inject
# cache_control breakpoints for the static system prefix, full system
# prompt, and last two messages (or the legacy system-and-3 layout
# when no static prefix is available).
# Build the request-local cache sections only after every transcript
# mutation. The canonical tool registry stays undecorated.
#
# Runs LAST, after every message mutation above. Marking earlier
# defeats the prefix stability the mutations exist to create:
@ -1768,10 +1774,12 @@ def run_conversation(
# exactly the point the breakpoints were meant to protect. Marking
# last also keeps breakpoints off messages that the orphan sweep or
# the thinking-only drop is about to remove or merge away.
if agent._use_prompt_caching:
tools_for_api = agent.tools
if agent._use_prompt_caching and agent.provider != "moa":
_static_system_prefix = getattr(agent, "_cached_system_prompt_static", None)
api_messages = apply_anthropic_cache_control(
_initial_cache_plan = build_prompt_cache_plan(
api_messages,
tools_for_api,
cache_ttl=agent._cache_ttl,
native_anthropic=agent._use_native_cache_layout,
static_system_prefix=(
@ -1779,7 +1787,10 @@ def run_conversation(
if isinstance(_static_system_prefix, str)
else None
),
direct_native_tool_cache=agent._direct_native_anthropic_tool_cache_capability(),
)
api_messages = _initial_cache_plan.messages
tools_for_api = _initial_cache_plan.tools
# Build a persistent-MoA request before measuring compression pressure.
# MoA reference output is injected into the aggregator prompt, but it
@ -2115,15 +2126,22 @@ def run_conversation(
# fallback refreshes the policy flags, but the decorated list
# still carries the primary's breakpoints (or none). Strip and
# re-render for the current provider before building kwargs.
api_messages, _moa_prepared_request = (
api_messages, _moa_prepared_request, tools_for_api = (
_redecorate_prompt_cache_for_provider(
agent,
api_messages,
system_message=system_message,
moa_prepared=_moa_prepared_request,
tools_for_api=tools_for_api,
)
)
api_kwargs = agent._build_api_kwargs(api_messages)
if tools_for_api == agent.tools:
api_kwargs = agent._build_api_kwargs(api_messages)
else:
api_kwargs = agent._build_api_kwargs(
api_messages,
tools_for_api=tools_for_api,
)
if agent._force_ascii_payload:
_sanitize_structure_non_ascii(api_kwargs)
if agent.api_mode == "codex_responses":

View File

@ -8,6 +8,7 @@ iteration.
from __future__ import annotations
import copy
import hashlib
import logging
import re
@ -1657,6 +1658,59 @@ class MoAChatCompletions:
max_tokens: Any = agg_kwargs.get("max_tokens")
tools: Any = agg_kwargs.get("tools")
extra_body: Any = agg_kwargs.get("extra_body")
agg_runtime = _slot_runtime(aggregator)
try:
from types import SimpleNamespace
from agent.agent_runtime_helpers import (
_direct_native_anthropic_tool_cache_capability,
anthropic_prompt_cache_policy,
)
from agent.prompt_caching import (
build_prompt_cache_plan,
strip_anthropic_cache_control,
strip_anthropic_tool_cache_control,
)
guidance = prepared.get("guidance")
canonical_messages = copy.deepcopy(agg_messages)
if guidance:
canonical_messages = peel_reference_guidance(
canonical_messages,
str(guidance),
)
strip_anthropic_cache_control(canonical_messages)
canonical_tools = strip_anthropic_tool_cache_control(tools)
cache_stub = SimpleNamespace(provider="", base_url="", api_mode="", model="")
should_cache, native_layout = anthropic_prompt_cache_policy(
cache_stub,
provider=agg_runtime.get("provider") or "",
base_url=agg_runtime.get("base_url") or "",
api_mode=agg_runtime.get("api_mode") or "",
model=agg_runtime.get("model") or "",
)
if should_cache:
plan = build_prompt_cache_plan(
canonical_messages,
canonical_tools,
native_anthropic=native_layout,
direct_native_tool_cache=_direct_native_anthropic_tool_cache_capability(
cache_stub,
provider=agg_runtime.get("provider") or "",
base_url=agg_runtime.get("base_url") or "",
api_mode=agg_runtime.get("api_mode") or "",
model=agg_runtime.get("model") or "",
),
)
agg_messages = plan.messages
tools = plan.tools
else:
agg_messages = canonical_messages
tools = canonical_tools
if guidance:
_attach_reference_guidance(agg_messages, str(guidance))
except Exception as exc: # pragma: no cover - cache planning must not block MoA
logger.debug("MoA aggregator cache plan skipped: %s", exc)
# Record the exact aggregator INPUT (incl. the injected reference
# context) into the pending trace so a trace captures what the
# aggregator actually saw, not a reconstruction. Traces are a
@ -1697,7 +1751,6 @@ class MoAChatCompletions:
# actually governs the aggregator stream, not just call_llm's default.
if api_kwargs.get("timeout") is not None:
stream_kwargs["timeout"] = api_kwargs["timeout"]
agg_runtime = _slot_runtime(aggregator)
# _slot_runtime may carry the provider's request_overrides.extra_body;
# pop it and merge with the caller's extra_body (caller wins) so the
# explicit kwarg below never collides with **agg_runtime.

View File

@ -273,6 +273,27 @@ CONTEXT_PROBE_TIERS = [
# Default context length when no detection method succeeds.
DEFAULT_FALLBACK_CONTEXT = CONTEXT_PROBE_TIERS[0]
# (model, base_url) pairs that already emitted the fallback warning.
# The fallback result itself is deliberately never cached, so without this
# the warning would repeat on every resolution for the same unknown model.
_FALLBACK_WARNED: set = set()
def _warn_context_length_fallback(model: str, base_url: str) -> None:
"""Warn (once per model+endpoint) that context detection failed and the
hard default is being used, so small-context models (8K, 32K) don't
silently get 256K and cause hard-to-debug API failures."""
key = (model, base_url or "")
if key in _FALLBACK_WARNED:
return
_FALLBACK_WARNED.add(key)
logger.warning(
"Could not determine context length for model %r (base_url=%s) "
"— falling back to %s tokens. Set model.context_length in "
"config.yaml to override.",
model, base_url or "default", f"{DEFAULT_FALLBACK_CONTEXT:,}",
)
# Minimum context length required to run Hermes Agent. Models with fewer
# tokens cannot maintain enough working memory for tool-calling workflows.
# Sessions, model switches, and cron jobs should reject models below this.
@ -2599,6 +2620,9 @@ def get_model_context_length(
f"{length:,}", model, default_model,
)
return length
# Same silent-256K bug class as the step-9 fallback below —
# warn here too so custom/local endpoints aren't left invisible.
_warn_context_length_fallback(model, base_url)
return DEFAULT_FALLBACK_CONTEXT
# 4. Anthropic /v1/models API (only for regular API keys, not OAuth)
@ -2771,7 +2795,10 @@ def get_model_context_length(
if default_model in model_lower:
return length
# 9. Default fallback — 256K
# 9. Default fallback — warn (deduped per model+endpoint) so
# small-context models don't silently get 256K. See
# _warn_context_length_fallback for rationale.
_warn_context_length_fallback(model, base_url)
return DEFAULT_FALLBACK_CONTEXT

View File

@ -11,9 +11,19 @@ Pure functions -- no class state, no AIAgent dependency.
"""
import copy
from dataclasses import dataclass
from typing import Any, Dict, List
@dataclass(frozen=True)
class PromptCachePlan:
"""Request-local message and tool sections with their cache markers."""
messages: List[Dict[str, Any]]
tools: List[Dict[str, Any]]
marker_count: int
def _apply_cache_marker(msg: dict, cache_marker: dict, native_anthropic: bool = False) -> None:
"""Add cache_control to a single message, handling all format variations."""
role = msg.get("role", "")
@ -172,6 +182,152 @@ def strip_anthropic_cache_control(
return api_messages
def strip_anthropic_tool_cache_control(tools: List[Dict[str, Any]] | None) -> List[Dict[str, Any]]:
"""Return copied tools without request-local Anthropic cache markers."""
cleaned = copy.deepcopy(tools or [])
for tool in cleaned:
if isinstance(tool, dict):
tool.pop("cache_control", None)
return cleaned
def _count_cache_markers(messages: List[Dict[str, Any]], tools: List[Dict[str, Any]]) -> int:
"""Count the wire-visible cache markers in a request-local plan."""
count = sum(
1
for message in messages
if isinstance(message, dict) and "cache_control" in message
)
count += sum(
1
for message in messages
if isinstance(message, dict) and isinstance(message.get("content"), list)
for part in message["content"]
if isinstance(part, dict) and "cache_control" in part
)
return count + sum(
1 for tool in tools if isinstance(tool, dict) and "cache_control" in tool
)
def _completed_transaction_endpoint_indexes(
messages: List[Dict[str, Any]], *, native_anthropic: bool,
) -> List[int]:
"""Select legal ends of completed tool runs and ordinary turns."""
endpoints: List[int] = []
index = 0
while index < len(messages):
message = messages[index]
if not isinstance(message, dict) or message.get("role") == "system":
index += 1
continue
if message.get("role") == "assistant" and message.get("tool_calls"):
result_start = index + 1
result_end = result_start
while result_end < len(messages):
result = messages[result_end]
if not isinstance(result, dict) or result.get("role") != "tool":
break
result_end += 1
if result_end > result_start:
endpoint = result_end - 1
if _can_carry_marker(messages[endpoint], native_anthropic):
endpoints.append(endpoint)
index = result_end
continue
if message.get("role") == "tool":
while index < len(messages):
result = messages[index]
if not isinstance(result, dict) or result.get("role") != "tool":
break
index += 1
continue
if message.get("role") == "user" and index + 1 < len(messages):
index += 1
continue
if (
message.get("role") == "assistant"
and message.get("content") in (None, "")
):
index += 1
continue
if _can_carry_marker(message, native_anthropic):
endpoints.append(index)
index += 1
return endpoints
def _apply_static_prefix_marker(
messages: List[Dict[str, Any]],
cache_marker: Dict[str, str],
static_system_prefix: str | None,
) -> None:
"""Mark only the reusable static system prefix for a tool-cache plan."""
if not messages or not isinstance(static_system_prefix, str) or not static_system_prefix:
return
system = messages[0]
if not isinstance(system, dict):
return
content = system.get("content")
if system.get("role") != "system" or not isinstance(content, str):
return
if not content.startswith(static_system_prefix):
return
suffix = content[len(static_system_prefix):]
system["content"] = [
{"type": "text", "text": static_system_prefix, "cache_control": cache_marker},
{"type": "text", "text": suffix},
]
def build_prompt_cache_plan(
api_messages: List[Dict[str, Any]],
tools: List[Dict[str, Any]] | None,
*,
cache_ttl: str = "5m",
native_anthropic: bool = False,
static_system_prefix: str | None = None,
direct_native_tool_cache: bool = False,
) -> PromptCachePlan:
"""Build isolated cache sections for one resolved request destination."""
messages = copy.deepcopy(api_messages or [])
strip_anthropic_cache_control(messages)
planned_tools = strip_anthropic_tool_cache_control(tools)
if not direct_native_tool_cache or not planned_tools:
planned_messages = apply_anthropic_cache_control(
messages,
cache_ttl=cache_ttl,
native_anthropic=native_anthropic,
static_system_prefix=static_system_prefix,
)
return PromptCachePlan(
messages=planned_messages,
tools=planned_tools,
marker_count=_count_cache_markers(planned_messages, planned_tools),
)
marker = _build_marker(cache_ttl)
_apply_static_prefix_marker(messages, marker, static_system_prefix)
planned_tools[-1]["cache_control"] = dict(marker)
for endpoint in _completed_transaction_endpoint_indexes(
messages,
native_anthropic=True,
)[-2:]:
_apply_cache_marker(messages[endpoint], marker, native_anthropic=True)
return PromptCachePlan(
messages=messages,
tools=planned_tools,
marker_count=_count_cache_markers(messages, planned_tools),
)
def apply_anthropic_cache_control(
api_messages: List[Dict[str, Any]],
cache_ttl: str = "5m",

View File

@ -15,6 +15,7 @@ import { Codicon } from '@/components/ui/codicon'
import { DiffCount } from '@/components/ui/diff-count'
import type { HermesGitBranch } from '@/global'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { registerRepoStatusCwd, repoStatusForCwd, repoWorktreesForCwd } from '@/store/coding-status'
import { notifyError } from '@/store/notifications'
import { $newWorktreeRequest } from '@/store/projects'
@ -237,6 +238,18 @@ export const CodingStatusRow = memo(function CodingStatusRow({
</span>
</button>
{/* Worktree path — plain muted text, not a chip. Always in the flex
so hover doesn't reflow the row; opacity alone reveals it.
`displayPath` collapses home → ~ (shell / VS Code convention). */}
{resolvedRepoPath && (
<span
className="min-w-0 flex-1 truncate font-mono text-[0.62rem] leading-4 text-muted-foreground/50 opacity-0 transition-opacity group-hover/status-row:opacity-100 group-focus-within/status-row:opacity-100"
data-slot="coding-status-cwd"
>
{displayPath(resolvedRepoPath)}
</span>
)}
{/* Branch actions kebab — same pattern as the session/worktree rows.
ALWAYS laid out; only its opacity flips on hover/focus/open, so
revealing it never reflows the row (no layout shift). pointer-events

View File

@ -15,6 +15,7 @@ import {
import type { HermesGitWorktree } from '@/global'
import type { SessionInfo } from '@/hermes'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { $dismissedWorktreeIds, dismissWorktree, setWorkspaceNodeOpen } from '@/store/layout'
import { notifyError } from '@/store/notifications'
import { removeWorktreePath } from '@/store/projects'
@ -277,7 +278,7 @@ function RepoFlatSection({
label={repo.label}
onToggle={toggleOpen}
open={open}
title={repo.path ?? undefined}
title={repo.path ? displayPath(repo.path) : undefined}
/>
{open && <SidebarRowStack className="pl-2.5">{body}</SidebarRowStack>}
{removeDialog}

View File

@ -4,6 +4,7 @@ import { useState } from 'react'
import { Codicon } from '@/components/ui/codicon'
import type { SessionInfo } from '@/hermes'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { setWorkspaceNodeOpen } from '@/store/layout'
import { notifyError } from '@/store/notifications'
import { newSessionInProfile } from '@/store/profile'
@ -132,7 +133,7 @@ export function SidebarWorkspaceGroup({ group, renderRows, onNewSession, onRemov
label={group.label}
onToggle={toggleOpen}
open={open}
title={group.path ?? undefined}
title={group.path ? displayPath(group.path) : undefined}
/>
</WorkspaceContextMenu>
{open && (

View File

@ -8,6 +8,7 @@ import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import { getProfileSoul, type ProfileInfo, updateProfileSoul } from '@/hermes'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { AlertTriangle, Save } from '@/lib/icons'
import { profileColorSoft, resolveProfileColor } from '@/lib/profile-color'
import { normalize } from '@/lib/text'
@ -258,8 +259,8 @@ function ProfileDetail({ profile }: { profile: ProfileInfo }) {
{profile.is_default && <PanelPill tone="good">{p.defaultBadge}</PanelPill>}
{profile.has_env && <PanelPill tone="muted">.env</PanelPill>}
</div>
<p className="mt-1 truncate font-mono text-[0.66rem] text-muted-foreground/55" title={profile.path}>
{profile.path}
<p className="mt-1 truncate font-mono text-[0.66rem] text-muted-foreground/55" title={displayPath(profile.path)}>
{displayPath(profile.path)}
</p>
</div>

View File

@ -5,6 +5,7 @@ import { Codicon } from '@/components/ui/codicon'
import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/components/ui/dialog'
import { useI18n } from '@/i18n'
import { readDesktopDir, setDesktopFsRemotePicker } from '@/lib/desktop-fs'
import { displayPath, pathLeaf } from '@/lib/display-path'
import { cn } from '@/lib/utils'
function clean(path: string) {
@ -24,7 +25,7 @@ function parentDir(path: string) {
}
function pathName(path: string) {
return path.split('/').filter(Boolean).pop() || path
return pathLeaf(path) || path
}
interface PendingSelection {
@ -167,7 +168,7 @@ export function RemoteFolderPicker() {
</div>
<div className="shrink-0 flex items-center justify-between gap-2 border-t border-border/70 px-4 py-3">
<div className="min-w-0 truncate text-xs text-muted-foreground">{currentPath}</div>
<div className="min-w-0 truncate text-xs text-muted-foreground">{displayPath(currentPath)}</div>
<div className="flex shrink-0 items-center gap-2">
<Button onClick={() => close()} size="sm" variant="ghost">
{t.common.cancel}

View File

@ -16,6 +16,7 @@ import { Tip } from '@/components/ui/tooltip'
import type { HermesReviewFile } from '@/global'
import { useI18n } from '@/i18n'
import { isDesktopFsRemoteMode } from '@/lib/desktop-fs'
import { displayPath } from '@/lib/display-path'
import { normalizeOrLocalPreviewTarget } from '@/lib/local-preview'
import { cn } from '@/lib/utils'
import { $renamingPath, copyFilePath, revealFile, toRelativePath } from '@/store/file-actions'
@ -323,7 +324,7 @@ function ReviewFileRow({ node, depth }: { node: ReviewTreeNode; depth: number })
event.dataTransfer.setData('text/plain', dragPath)
}}
style={rowStyle(depth)}
title={dragPath}
title={displayPath(dragPath)}
>
<Codicon className={cn('shrink-0', glyph.tone)} name={glyph.icon} size="0.8rem" />
{/* Dir collapses first (huge shrink); the name only ellipsizes once the

View File

@ -16,6 +16,7 @@ import { DiffCount } from '@/components/ui/diff-count'
import { Tip } from '@/components/ui/tooltip'
import { useDelayedTrue } from '@/hooks/use-delayed-true'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { cn } from '@/lib/utils'
import { $panesFlipped } from '@/store/layout'
import { notifyError } from '@/store/notifications'
@ -162,9 +163,9 @@ export function ReviewPane() {
<div className="flex items-center gap-1 px-2.5 py-1.5" data-suppress-pane-reveal-side="">
<span
className="min-w-0 flex-1 truncate font-mono text-[0.66rem] text-(--ui-text-secondary)"
title={selectedFile.path}
title={displayPath(selectedFile.path)}
>
{selectedFile.path}
{displayPath(selectedFile.path)}
</span>
<DiffCount added={selectedFile.added} className="text-[0.64rem] leading-4" removed={selectedFile.removed} />
<Tip label={selectedFile.staged ? c.unstage : c.stage}>
@ -217,9 +218,9 @@ export function ReviewPane() {
{!revertingAll && revertTarget?.path && (
<span
className="mt-2 block truncate font-mono text-[0.7rem] text-(--ui-text-secondary)"
title={revertTarget.path}
title={displayPath(revertTarget.path)}
>
{revertTarget.path}
{displayPath(revertTarget.path)}
</span>
)}
</DialogDescription>

View File

@ -482,7 +482,7 @@ export function useSessionActions({
try {
// Fresh tile → the caller's workspace when one was named (the sidebar
// "+" on a project/worktree lane), else the resolved new-session cwd
// (project/default) — never the primary composer's live cwd.
// (focused session's project → project scope → default).
const params = await desktopSessionCreateParams((options?.cwd || resolveNewSessionCwd()).trim())
const created = await requestGateway<SessionCreateResponse>('session.create', params)
const stored = created.stored_session_id

View File

@ -12,6 +12,7 @@ import {
} from '@/hermes'
import { useI18n } from '@/i18n'
import { sessionTitle } from '@/lib/chat-runtime'
import { pathLeaf } from '@/lib/display-path'
import { triggerHaptic } from '@/lib/haptics'
import { Archive, ArchiveOff, FolderOpen, Loader2, Trash2 } from '@/lib/icons'
import { notify, notifyError } from '@/store/notifications'
@ -26,22 +27,6 @@ const DEFAULT_AUTO_ARCHIVE_DAYS = 3
const ARCHIVED_FETCH_LIMIT = 200
function workspaceLabel(cwd: null | string | undefined): string {
const path = cwd?.trim()
if (!path) {
return ''
}
return (
path
.replace(/[/\\]+$/, '')
.split(/[/\\]/)
.filter(Boolean)
.pop() ?? path
)
}
export function SessionsSettings() {
const { t } = useI18n()
const s = t.settings.sessions
@ -139,7 +124,7 @@ export function SessionsSettings() {
) : (
<div className="grid gap-1">
{sessions.map(session => {
const label = workspaceLabel(session.cwd)
const label = pathLeaf(session.cwd)
const busy = busyId === session.id
return (

View File

@ -9,6 +9,7 @@ import { $paneVisible, togglePaneVisible } from '@/components/pane-shell/tree/st
import { Codicon } from '@/components/ui/codicon'
import { GlyphSpinner } from '@/components/ui/glyph-spinner'
import { useI18n } from '@/i18n'
import { displayPath, pathLeaf } from '@/lib/display-path'
import { Activity, AlertCircle, Clock, Command, FolderOpen, Globe, Hash, Loader2, Terminal } from '@/lib/icons'
import type { RuntimeReadinessResult } from '@/lib/runtime-readiness'
import { contextBarLabel, LiveDuration, usageContextLabel } from '@/lib/statusbar'
@ -29,6 +30,7 @@ import {
$sessions,
$sessionStartedAt,
$turnStartedAt,
idsShareLineage,
sessionMatchesStoredId,
setCurrentUsage
} from '@/store/session'
@ -50,13 +52,6 @@ import type { StatusbarItem } from '../statusbar-controls'
const EMPTY_USAGE = { calls: 0, input: 0, output: 0, total: 0 } as const
function workspaceLabel(cwd: string): string {
const normalized = cwd.replace(/[\\/]+$/, '')
const leaf = normalized.split(/[\\/]/).filter(Boolean).pop()
return leaf || cwd
}
interface StatusbarItemsOptions {
agentsOpen: boolean
chatOpen: boolean
@ -97,13 +92,10 @@ export function useStatusbarItems({
// minimized zone, which lit the button for a pane the user couldn't see.
const terminalShowing = useStore($paneVisible('terminal'))
const primaryBusy = useStore($busy)
const currentCwd = useStore($currentCwd)
// Derive the workspace's project name from the already-cached project tree
// (backend truth via projects.*), so the status item labels by project without
// a second per-session copy of the same fact. Re-derives whenever the cwd or
// the tree changes; null (no named project) falls back to the cwd leaf below.
const projectTree = useStore($projectTree)
const projectName = useMemo(() => projectNameForCwd(currentCwd), [currentCwd, projectTree])
// Draft / primary composer atom — used only while the focused surface is the
// primary (or a draft with no runtime slice yet). A focused TILE keeps its
// own cwd in `$sessionStates` and must not paint the primary's workspace.
const primaryCwd = useStore($currentCwd)
const primaryUsage = useStore($currentUsage)
const gatewayRestarting = useStore($gatewayRestarting)
const primarySessionStartedAt = useStore($sessionStartedAt)
@ -131,15 +123,14 @@ export function useStatusbarItems({
// The FOCUSED session (interacted tile, else the primary — the same
// derivation the titlebar title follows): every session-scoped readout
// below (context count, timers, busy pulse) tracks it, so clicking into a
// tile makes the statusbar describe THAT session.
// below (workspace cwd, context count, timers, busy pulse) tracks it, so
// clicking into a tile makes the statusbar describe THAT session.
const focusedStoredSessionId = useStore($focusedStoredSessionId)
const focusedRuntimeId = useStore($focusedRuntimeId)
// `$focusedSessionState` is a projection of `$sessionStates`, which is
// republished on EVERY message delta — tens of times a second during a turn.
// Only three fields are read off it here, so subscribing to the whole object
// re-ran this hook (and re-created all ~9 statusbar items) per token. Select
// each field individually so an unchanged readout bails out instead.
// Only the fields read here are selected, so an unchanged readout bails out
// instead of rebuilding all ~9 statusbar items per token.
const focusedBusy = useStoreSelector($focusedSessionState, state => Boolean(state?.busy))
const focusedTurnStartedAt = useStoreSelector($focusedSessionState, state => state?.turnStartedAt ?? null)
// `usage` is an object, so it can't be compared as a scalar. It IS however
@ -147,6 +138,15 @@ export function useStatusbarItems({
// reports new usage — far rarer than a delta — so its reference is a valid
// bail-out key on its own.
const focusedUsage = useStoreSelector($focusedSessionState, state => state?.usage ?? null)
const focusedStateCwd = useStoreSelector($focusedSessionState, state => state?.cwd?.trim() || '')
// Runtime slices carry the stored id they were bound for. During a primary
// tab switch the runtime id can lag a frame behind the new selection — the
// slice still describes the PREVIOUS chat. Gate live cwd on ownership so we
// never paint session A's workspace while the tab already shows session B.
const focusedStateStoredId = useStoreSelector(
$focusedSessionState,
state => state?.storedSessionId?.trim() || null
)
const selectedStoredSessionId = useStore($selectedStoredSessionId)
const primaryFocused = !focusedStoredSessionId || focusedStoredSessionId === selectedStoredSessionId
@ -159,15 +159,61 @@ export function useStatusbarItems({
const turnStartedAt = primaryFocused ? primaryTurnStartedAt : focusedTurnStartedAt
// A tile's session-start comes from its stored row (the cache only knows
// runtime state); seconds → ms. Only this ONE scalar is read off
// `$sessions`, so select it — a whole-list `useStore` re-ran the hook on
// every session-list write (title updates, poll refreshes, archives).
// A tile's session-start + cold cwd come from its stored row (the cache only
// knows runtime state). Only these scalars are read off `$sessions`, so
// select them — a whole-list `useStore` re-ran the hook on every session-list
// write (title updates, poll refreshes, archives).
const focusedRowStartedAt = useStoreSelector($sessions, sessions =>
focusedStoredSessionId
? (sessions.find(s => sessionMatchesStoredId(s, focusedStoredSessionId))?.started_at ?? null)
: null
)
const focusedRowCwd = useStoreSelector($sessions, sessions => {
if (!focusedStoredSessionId) {
return ''
}
const row = sessions.find(s => sessionMatchesStoredId(s, focusedStoredSessionId))
return row?.cwd?.trim() || ''
})
// Live runtime cwd is authoritative once it belongs to the focused chat
// (agent can relocate mid-turn). Until then — cold tabs, mid-switch lag —
// the stored session row is the selection's project. Primary drafts fall
// through to `$currentCwd`. A focused TILE must never inherit the primary's
// workspace — an empty tile cwd stays empty rather than lying about another
// project's path.
//
// Lineage match is a pure derivation of ($sessions + the two ids). Select it
// so a session-list write only re-renders when the answer actually flips —
// not on every title/archive refresh of an unrelated row.
const liveCwdSharesFocusLineage = useStoreSelector($sessions, sessions => {
if (!focusedStoredSessionId || !focusedStateStoredId) {
return false
}
return idsShareLineage(focusedStoredSessionId, focusedStateStoredId, sessions)
})
const liveCwdBelongsToFocus =
Boolean(focusedStateCwd) &&
(!focusedStoredSessionId ||
!focusedStateStoredId ||
focusedStateStoredId === focusedStoredSessionId ||
liveCwdSharesFocusLineage)
const currentCwd = (
(liveCwdBelongsToFocus ? focusedStateCwd : '') ||
focusedRowCwd ||
(primaryFocused ? primaryCwd : '') ||
''
).trim()
// Derive the workspace's project name from the already-cached project tree
// (backend truth via projects.*), so the status item labels by project without
// a second per-session copy of the same fact. Re-derives whenever the cwd or
// the tree changes; null (no named project) falls back to the cwd leaf below.
const projectTree = useStore($projectTree)
const projectName = useMemo(() => projectNameForCwd(currentCwd), [currentCwd, projectTree])
const sessionStartedAt = primaryFocused
? primarySessionStartedAt
@ -369,33 +415,33 @@ export function useStatusbarItems({
hidden: !currentCwd,
icon: <FolderOpen className="size-3" />,
id: 'workspace-cwd',
// Prefer the named project; fall back to the cwd leaf. The full cwd is
// always in the tooltip (`title` below), so hovering reveals where the
// session actually sits — the worktree/subfolder, not just the project.
label: projectName || (currentCwd ? workspaceLabel(currentCwd) : undefined),
// Prefer the named project; fall back to the cwd leaf. Hover tip uses
// the shared display formatter (home → ~) so statusbar and branch bar
// agree on how a path looks.
label: projectName || (currentCwd ? pathLeaf(currentCwd) : undefined),
menuItems: currentCwd
? [
{
id: 'copy-workspace-path',
label: fileMenu.copyPath,
onSelect: () => void copyFilePath(currentCwd),
title: currentCwd
title: displayPath(currentCwd)
},
{
id: 'reveal-workspace-finder',
label: fileMenu.revealFileManager,
onSelect: () => void revealFile(currentCwd),
title: currentCwd
title: displayPath(currentCwd)
},
{
id: 'reveal-workspace-sidebar',
label: fileMenu.revealInSidebar,
onSelect: () => revealFileInTree(currentCwd),
title: currentCwd
title: displayPath(currentCwd)
}
]
: undefined,
title: currentCwd || undefined,
title: currentCwd ? displayPath(currentCwd) : undefined,
toggleLabel: copy.toggleWorkspace,
variant: 'menu'
},

View File

@ -8,6 +8,7 @@ import { DiffCount } from '@/components/ui/diff-count'
import { FadeScroll } from '@/components/ui/fade-scroll'
import { FileTypeIcon } from '@/components/ui/file-type-icon'
import { useI18n } from '@/i18n'
import { displayPath } from '@/lib/display-path'
import { cn } from '@/lib/utils'
import { openReviewForPath, revealReview } from '@/store/review'
@ -58,7 +59,7 @@ export const ChangedFilesCard: FC<{ parts: readonly unknown[] }> = ({ parts }) =
className="row-hover flex shrink-0 items-center gap-2 rounded-md px-1.5 py-1 text-left"
key={file.path}
onClick={() => void openReviewForPath(file.path, scopeCwd)}
title={file.path}
title={displayPath(file.path)}
type="button"
>
<FileTypeIcon className="shrink-0 text-(--ui-text-tertiary)" path={file.path} size="0.875rem" />

View File

@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest'
import { allFixedAbsorberIndex } from './track-model'
describe('allFixedAbsorberIndex', () => {
it('picks the last uncapped track', () => {
// terminal (uncapped height) + logs (uncapped) → logs absorbs
const growable = [0, 1]
const max = (i: number) => (i === 0 ? '80vh' : undefined)
expect(allFixedAbsorberIndex(growable, max)).toBe(1)
})
it('skips a capped last track and picks an earlier uncapped one', () => {
// uncapped tool panel + capped files rail at the end of a column
const growable = [0, 1]
const max = (i: number) => (i === 1 ? '20rem' : undefined)
expect(allFixedAbsorberIndex(growable, max)).toBe(0)
})
it('refuses to absorb when every fixed track is capped (review/files rail)', () => {
// Default right rail: review | preview | files — all declare maxWidth.
// Promoting any of them to grow-1 dropped the clamp and ballooned ⌘G/⌘J.
const growable = [0, 1, 2]
const max = () => '20rem'
expect(allFixedAbsorberIndex(growable, max)).toBe(-1)
})
it('returns -1 for an empty run', () => {
expect(allFixedAbsorberIndex([], () => undefined)).toBe(-1)
})
it('absorbs a lone uncapped track', () => {
expect(allFixedAbsorberIndex([3], () => undefined)).toBe(3)
})
it('refuses a lone capped sidebar', () => {
// ⌘G alone: only review is visible in the rail — must stay at 237px / max 20rem.
expect(allFixedAbsorberIndex([0], () => '20rem')).toBe(-1)
})
})

View File

@ -158,6 +158,36 @@ export const cssMax = (values: (string | null | undefined)[]): string | undefine
* are both 28px thick. */
export const MINIMIZED_TRACK = '1.75rem'
/**
* In an all-fixed split, the last uncapped track may absorb leftover space
* (terminal/logs stacked at 38vh with nothing else to fill the column). A
* CAPPED track must never be the absorber: review/files declare maxWidth
* 20rem, and promoting them to grow-1 + dropping the clamp made ⌘G / ⌘J open
* a half-window rail and ignore sash-remembered sizes (flex-basis alone
* can't hold against grow).
*
* Returns the child index to absorb, or -1 when every fixed track is capped
* (leave dead space — better than a ballooned sidebar).
*/
export function allFixedAbsorberIndex(
growable: readonly number[],
maxAlongAxis: (index: number) => string | undefined
): number {
if (growable.length === 0) {
return -1
}
for (let i = growable.length - 1; i >= 0; i--) {
const index = growable[i]
if (!maxAlongAxis(index)) {
return index
}
}
return -1
}
export function fixedTrackSize(node: LayoutNode, axis: 'row' | 'column', ctx: TrackContext): string | null {
if (node.type === 'group') {
// Ancestor splits must size a minimized zone as its strip, not as its

View File

@ -2,8 +2,8 @@
* Split node renderer — a flex row/column whose 1px seams double as resize
* sashes (the seam IS the boundary — junction-owned, never doubled). Sizing
* is the TRACK MODEL (track-model.ts): fixed tracks keep their declared size,
* flex tracks share the leftover by weight, and an all-fixed run lets its
* last track absorb the slack (VS Code style).
* flex tracks share the leftover by weight, and an all-fixed run lets an
* UNCAPPED last track absorb the slack (capped sidebars stay put).
*/
import { useStore } from '@nanostores/react'
@ -30,6 +30,7 @@ import {
} from '../store'
import {
allFixedAbsorberIndex,
COLLAPSED_ZONE_PX,
computedPx,
cssMax,
@ -469,8 +470,8 @@ export function TreeSplit({ node, root, rootRow }: { node: SplitNode; root?: boo
// A run of ONLY fixed tracks can't fill the container (grow-0 all around
// leaves dead space — e.g. terminal + logs split into two 38vh zones with
// the rail above them collapsed). The LAST visible track absorbs the
// leftover, VS Code style.
// the rail above them collapsed). An UNCAPPED last track absorbs the
// leftover; capped sidebars (review/files) keep their max and stay put.
const isMinimized = (child: LayoutNode) => child.type === 'group' && Boolean(child.minimized)
// SEMANTIC side collapse (titlebar toggles / ⌘B / ⌘J): at the ROOT row,
@ -508,7 +509,13 @@ export function TreeSplit({ node, root, rootRow }: { node: SplitNode; root?: boo
const growable = tracks.map((_, i) => i).filter(i => !tracks[i].collapsed && !tracks[i].minimized)
const allFixed = growable.length > 0 && growable.every(i => tracks[i].track !== null)
const absorberIndex = allFixed ? growable[growable.length - 1] : -1
// Only an uncapped fixed track may absorb leftover. A maxWidth/maxHeight
// sidebar (review, files, sessions) must keep that clamp — otherwise ⌘G
// balloons the rail and sash-remembered sizes become a flex-basis that
// grow still expands past.
const absorberIndex = allFixed
? allFixedAbsorberIndex(growable, i => (horizontal ? tracks[i].sizing?.maxWidth : tracks[i].sizing?.maxHeight))
: -1
// Weights are RATIOS, but CSS flex-grow is absolute: a run whose grows sum
// below 1 fills only that fraction of the leftover (normalize's flatten
@ -550,6 +557,7 @@ export function TreeSplit({ node, root, rootRow }: { node: SplitNode; root?: boo
>
{tracks.map(({ child, collapsed, minimized, narrowCollapsed, sizing, track }, i) => {
const partner = collapsed ? -1 : seamPartner(i)
const absorbs = i === absorberIndex
return (
<div
@ -565,16 +573,17 @@ export function TreeSplit({ node, root, rootRow }: { node: SplitNode; root?: boo
// grow-0 shrink-1 from its preferred basis (it yields
// gracefully on tight windows, floored by min-width);
// everything else splits the leftover by weight. In an
// all-fixed run the last track grows into the leftover.
flex: track ? `${i === absorberIndex ? 1 : 0} 1 ${track}` : `${grow(i)} ${grow(i)} 0px`,
// all-fixed run an UNCAPPED last track grows into the
// leftover; capped sidebars stay at their declared size.
flex: track ? `${absorbs ? 1 : 0} 1 ${track}` : `${grow(i)} ${grow(i)} 0px`,
// Pane-declared clamps apply along THIS split's axis only
// (a rail's width clamp shouldn't constrain its height).
// The absorber drops its max clamp — it exists to fill
// the leftover, and clamping would recreate the gap.
// The absorber is uncapped by selection, so dropping its
// max is a no-op; capped tracks always keep theirs.
minWidth: (horizontal && sizing?.minWidth) || 0,
maxWidth: horizontal && i !== absorberIndex ? sizing?.maxWidth : undefined,
maxWidth: horizontal && !absorbs ? sizing?.maxWidth : undefined,
minHeight: (!horizontal && sizing?.minHeight) || 0,
maxHeight: horizontal || i === absorberIndex ? undefined : sizing?.maxHeight
maxHeight: horizontal || absorbs ? undefined : sizing?.maxHeight
}
}
>

View File

@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest'
import { displayPath, normalizeDisplayPath, pathLeaf } from './display-path'
describe('displayPath', () => {
it('collapses a macOS home prefix to ~', () => {
expect(displayPath('/Users/brooklyn/www/hermes-agent')).toBe('~/www/hermes-agent')
expect(displayPath('/Users/brooklyn')).toBe('~')
})
it('collapses a Linux home prefix to ~', () => {
expect(displayPath('/home/alice/src/app')).toBe('~/src/app')
})
it('collapses a Windows user profile to ~', () => {
expect(displayPath('C:\\Users\\brooklyn\\src')).toBe('~/src')
expect(displayPath('C:/Users/brooklyn')).toBe('~')
})
it('honours an explicit home override', () => {
expect(displayPath('/opt/work/repo', { home: '/opt/work' })).toBe('~/repo')
expect(displayPath('/elsewhere/repo', { home: '/opt/work' })).toBe('/elsewhere/repo')
})
it('leaves non-home absolute paths alone', () => {
expect(displayPath('/var/log/system.log')).toBe('/var/log/system.log')
expect(displayPath('/Users')).toBe('/Users')
})
it('normalizes separators and trailing slashes', () => {
expect(normalizeDisplayPath('C:\\Users\\me\\src\\')).toBe('C:/Users/me/src')
expect(displayPath('/Users/me/src/')).toBe('~/src')
})
it('keeps an already-tildified path', () => {
expect(displayPath('~/www/app')).toBe('~/www/app')
expect(displayPath('~')).toBe('~')
})
})
describe('pathLeaf', () => {
it('returns the last segment', () => {
expect(pathLeaf('/Users/me/www/hermes-agent')).toBe('hermes-agent')
expect(pathLeaf('~/www/hermes-agent')).toBe('hermes-agent')
expect(pathLeaf('/')).toBe('/')
})
})

View File

@ -0,0 +1,157 @@
/**
* One place to format filesystem paths for DISPLAY.
*
* Industry standard (shells, VS Code `tildify`, Finder path bar): collapse the
* user's home directory to `~`, keep forward slashes, leave everything else
* alone. Copy/reveal/IPC still use the real absolute path — this is paint only.
*
* When `home` is unknown (renderer has no `os.homedir()` and remote cwd may
* not match the local machine), a conservative heuristic still collapses the
* common `/Users/<name>`, `/home/<name>`, and `C:/Users/<name>` prefixes so a
* long absolute path never paints raw in chrome.
*/
export interface DisplayPathOptions {
/** Explicit home directory to collapse (local machine home, remote $HOME). */
home?: null | string
}
/** Normalize separators and drop a trailing slash (except root / drive root). */
export function normalizeDisplayPath(raw: string): string {
let path = (raw || '').trim().replace(/\\/g, '/')
if (!path) {
return ''
}
// Collapse repeated slashes, but keep a leading UNC `//server/...` pair.
if (path.startsWith('//')) {
path = `//${path.slice(2).replace(/\/{2,}/g, '/')}`
} else {
path = path.replace(/\/{2,}/g, '/')
}
// Drop trailing slash except bare `/` or `C:/`.
if (path.length > 1 && path.endsWith('/') && !/^[A-Za-z]:\/$/.test(path)) {
path = path.replace(/\/+$/, '')
}
return path
}
function normalizeHome(home: string): string {
const normalized = normalizeDisplayPath(home)
if (!normalized) {
return ''
}
// Home itself should not keep a trailing slash for prefix checks.
return normalized.replace(/\/+$/, '')
}
function startsWithHome(path: string, home: string, caseInsensitive: boolean): boolean {
if (!home) {
return false
}
if (path === home) {
return true
}
const prefix = `${home}/`
return caseInsensitive
? path.toLowerCase().startsWith(prefix.toLowerCase()) || path.toLowerCase() === home.toLowerCase()
: path.startsWith(prefix) || path === home
}
/**
* Best-effort home prefix when callers don't pass one. Matches the usual
* single-user layouts; never collapses `/Users` or `/home` alone.
*/
function inferredHomePrefix(path: string): string {
// macOS: /Users/name[/...]
let match = path.match(/^(\/Users\/[^/]+)(?:\/|$)/)
if (match) {
return match[1]
}
// Linux (and most UNIX): /home/name[/...]
match = path.match(/^(\/home\/[^/]+)(?:\/|$)/)
if (match) {
return match[1]
}
// Windows user profile: C:/Users/name[/...] (also works after \ → /)
match = path.match(/^([A-Za-z]:\/Users\/[^/]+)(?:\/|$)/)
if (match) {
return match[1]
}
return ''
}
/**
* Format a filesystem path for UI chrome.
*
* /Users/brooklyn/www/hermes-agent → ~/www/hermes-agent
* /Users/brooklyn → ~
* C:\Users\brooklyn\src → ~/src
* /var/log → /var/log
* already/relative → already/relative
*/
export function displayPath(raw: null | string | undefined, options: DisplayPathOptions = {}): string {
const path = normalizeDisplayPath(raw || '')
if (!path) {
return ''
}
// Already tildified — normalize only.
if (path === '~' || path.startsWith('~/')) {
return path
}
const explicitHome = options.home ? normalizeHome(options.home) : ''
// Windows paths are case-insensitive; POSIX paths with an explicit home keep
// case-sensitive matching (Linux). Heuristic homes on mac/win ignore case.
const home = explicitHome || inferredHomePrefix(path)
if (!home) {
return path
}
const caseInsensitive = !explicitHome || /^[A-Za-z]:\//.test(home) || home.startsWith('/Users/')
if (!startsWithHome(path, home, caseInsensitive)) {
return path
}
if (path.length === home.length) {
return '~'
}
return `~${path.slice(home.length)}`
}
/** Last path segment for compact labels (statusbar leaf, settings rows). */
export function pathLeaf(raw: null | string | undefined): string {
const path = normalizeDisplayPath(raw || '')
if (!path || path === '/' || path === '~') {
return path
}
// `C:/` drive root
if (/^[A-Za-z]:\/$/.test(path) || /^[A-Za-z]:$/.test(path)) {
return path.endsWith('/') ? path : `${path}/`
}
const leaf = path.split('/').filter(Boolean).pop()
return leaf || path
}

View File

@ -4,7 +4,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { NO_PROJECT_ID, type SidebarProjectTree } from '@/app/chat/sidebar/projects/workspace-groups'
import { $sidebarAgentsGrouped } from '@/store/layout'
import { $activeGatewayProfile } from '@/store/profile'
import { applyConfiguredDefaultProjectDir } from '@/store/session'
import {
$currentCwd,
$selectedStoredSessionId,
$sessions,
applyConfiguredDefaultProjectDir
} from '@/store/session'
import {
$activeProjectId,
@ -116,11 +121,20 @@ describe('resolveNewSessionCwd', () => {
beforeEach(() => {
$projectScope.set(ALL_PROJECTS)
applyConfiguredDefaultProjectDir('/home/user/configured')
$currentCwd.set('')
$selectedStoredSessionId.set(null)
$sessions.set([])
// Reset focused-session projections by clearing the inputs they read.
// $focusedStoredSessionId falls back to $selectedStoredSessionId.
// $focusedSessionState needs a runtime — leave it empty via no session states.
})
afterEach(() => {
applyConfiguredDefaultProjectDir(null)
$projectScope.set(ALL_PROJECTS)
$currentCwd.set('')
$selectedStoredSessionId.set(null)
$sessions.set([])
})
it('starts a chat detached inside Home, ignoring the configured default dir', () => {
@ -134,6 +148,56 @@ describe('resolveNewSessionCwd', () => {
it('still falls back to the configured default outside Home', () => {
expect(resolveNewSessionCwd()).toBe('/home/user/configured')
})
it('inherits the focused session workspace when not drilled into a project', () => {
// Simulate a primary session whose stored row carries a project cwd —
// the common case: you're in a chat that has a pwd, hit ⌘N/⌘T, and
// expect the new draft to stay in that project without sidebar drill-in.
$selectedStoredSessionId.set('sess-a')
$sessions.set([
{
archived: false,
cwd: '/Users/me/www/hermes-agent',
ended_at: null,
id: 'sess-a',
input_tokens: 0,
is_active: true,
last_active: 0,
message_count: 1,
model: null,
output_tokens: 0,
started_at: 0,
title: 'work'
} as never
])
expect(resolveNewSessionCwd()).toBe('/Users/me/www/hermes-agent')
})
it('does not re-attach a remembered cwd when the focused session is detached', () => {
$currentCwd.set('/Users/me/stale-remembered')
$selectedStoredSessionId.set('sess-detached')
$sessions.set([
{
archived: false,
cwd: null,
ended_at: null,
id: 'sess-detached',
input_tokens: 0,
is_active: true,
last_active: 0,
message_count: 1,
model: null,
output_tokens: 0,
started_at: 0,
title: 'loose'
} as never
])
// Focused session has no workspace → fall through to configured default,
// not the stale $currentCwd from an earlier chat.
expect(resolveNewSessionCwd()).toBe('/home/user/configured')
})
})
describe('projectNameForCwd', () => {

View File

@ -16,7 +16,15 @@ import { $gateway, activeGateway, ensureActiveGatewayOpen } from '@/store/gatewa
import { setSidebarAgentsGrouped } from '@/store/layout'
import { notify } from '@/store/notifications'
import { $activeGatewayProfile, requestFreshSession } from '@/store/profile'
import { $selectedStoredSessionId, $sessions, sessionMatchesStoredId, workspaceCwdForNewSession } from '@/store/session'
import {
$currentCwd,
$selectedStoredSessionId,
$sessions,
idsShareLineage,
sessionMatchesStoredId,
workspaceCwdForNewSession
} from '@/store/session'
import { $focusedSessionState, $focusedStoredSessionId } from '@/store/session-states'
import type { ProjectInfo, ProjectsPayload } from '@/types/hermes'
// First-class, per-profile Projects (named, multi-folder workspaces). State is
@ -191,12 +199,19 @@ export function goToProject(id: string, options?: { newSession?: boolean }): voi
}
}
// The cwd a NEW chat should start in. The "active project" is just an atom
// ($projectScope) — so when you're inside a project, a new session (cmd-n, the
// trunk "+") starts at that project's root (its primary repo = the default-branch
// checkout) instead of inheriting whatever unrelated worktree the live cwd
// drifted into. Outside a project it falls back to the plain default (detached),
// so a bare new chat shows no branch.
// The cwd a NEW chat should start in.
//
// Priority (first hit wins):
// 1. Explicit sidebar project scope (drilled into a project / Home bucket)
// 2. The FOCUSED session's workspace — so ⌘N / ⌘T from a chat that's already
// in a project/worktree stay there without requiring a sidebar drill-in
// 3. Configured default project dir / remote remembered cwd (detached otherwise)
//
// The "active project" is just an atom ($projectScope) — so when you're inside
// a project, a new session (cmd-n, the trunk "+") starts at that project's root
// (its primary repo = the default-branch checkout). Outside a project it used
// to fall straight to the plain default (detached), which dropped the workspace
// of the chat you were looking at — that's the case (2) covers.
export function resolveNewSessionCwd(): string {
const scope = $projectScope.get()
@ -214,9 +229,58 @@ export function resolveNewSessionCwd(): string {
}
}
// Inherit the focused chat's workspace. ⌘N/⌘T from a session that already
// has a project/pwd should stay there — drilling into the sidebar project
// is the uncommon path, not the requirement.
const focusedCwd = focusedSessionWorkspaceCwd()
if (focusedCwd) {
return focusedCwd
}
return workspaceCwdForNewSession()
}
/** Live workspace of the session the user is looking at (tile or primary). */
function focusedSessionWorkspaceCwd(): string {
const focusedStoredId = $focusedStoredSessionId.get()
const state = $focusedSessionState.get()
// Prefer the live runtime slice when it belongs to the focused chat
// (agent can relocate mid-turn). Cold tabs / mid-switch lag fall through
// to the stored session row.
const stateCwd = state?.cwd?.trim() || ''
const stateStoredId = state?.storedSessionId?.trim() || ''
if (
stateCwd &&
(!focusedStoredId ||
!stateStoredId ||
stateStoredId === focusedStoredId ||
idsShareLineage(focusedStoredId, stateStoredId, $sessions.get()))
) {
return stateCwd
}
if (focusedStoredId) {
const row = $sessions.get().find(s => sessionMatchesStoredId(s, focusedStoredId))
const rowCwd = row?.cwd?.trim() || ''
if (rowCwd) {
return rowCwd
}
// Focused a real session with no workspace → stay detached. Do NOT fall
// through to `$currentCwd` (it may still hold a remembered path from an
// earlier chat and would re-attach a project the user left).
return ''
}
// No focused stored session: primary draft. The composer atom is the draft's
// workspace target (set by startFreshSession / startSessionInWorkspace).
return $currentCwd.get().trim()
}
const underPath = (parent: string, child: string): boolean =>
child === parent || child.startsWith(parent.endsWith('/') ? parent : `${parent}/`)

View File

@ -485,6 +485,8 @@ def _process_batch_worker(args: Tuple) -> Dict[str, Any]:
# Append to batch output file
with open(batch_output_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(trajectory_entry, ensure_ascii=False) + "\n")
f.flush()
os.fsync(f.fileno())
# Aggregate tool statistics
for tool_name, stats in result.get("tool_stats", {}).items():
@ -978,8 +980,15 @@ class BatchRunner:
except Exception as ckpt_err:
# Don't fail the run if checkpoint write fails
print(f"⚠️ Warning: Failed to save incremental checkpoint: {ckpt_err}")
except KeyboardInterrupt:
print("\n⚠️ Interrupted — terminating batch workers...")
pool.terminate()
pool.join()
raise
except Exception as e:
logger.error("Batch worker failed: %s", e, exc_info=True)
pool.terminate()
pool.join()
raise
finally:
root_logger.setLevel(original_level)

View File

@ -0,0 +1,2 @@
Prontsevich
# PR #32811 salvage (ACP background MCP discovery)

View File

@ -0,0 +1,2 @@
rodboev
# PR #37611 salvage (prompt-caching: tool schema markers)

View File

@ -0,0 +1,2 @@
spro-work
# PR #32811 salvage (ACP background MCP discovery) — same person as Prontsevich

View File

@ -3233,14 +3233,17 @@ def apply_terminal_config_to_env(
gives those child-process launch paths the same config bridge as classic
CLI without importing ``cli.py`` and paying for its startup side effects.
When the user config contains a ``terminal`` section, config.yaml is
authoritative and overrides existing env values. Otherwise defaults only
backfill missing env vars so exported/.env values keep working.
Explicit keys in the user config's ``terminal`` section are authoritative
and override their matching env values. Merged defaults only backfill
missing env vars; they never replace unrelated exported/.env values.
"""
target = os.environ if env is None else env
raw_config = read_raw_config()
file_has_terminal_config = isinstance(raw_config.get("terminal"), dict)
raw_terminal_cfg = raw_config.get("terminal")
file_has_terminal_config = isinstance(raw_terminal_cfg, dict)
if not file_has_terminal_config:
raw_terminal_cfg = {}
should_override = file_has_terminal_config if override is None else override
cfg = config if config is not None else load_config_readonly()
@ -3248,6 +3251,12 @@ def apply_terminal_config_to_env(
if not isinstance(terminal_cfg, dict):
return target
# A caller-supplied config is its own source of explicit keys. For the
# normal merged-config path, only keys present in raw config.yaml may
# override existing env values; keys inherited from DEFAULT_CONFIG are
# backfill-only.
explicit_keys = terminal_cfg.keys() if config is not None else raw_terminal_cfg.keys()
for cfg_key, env_var in TERMINAL_CONFIG_ENV_MAP.items():
if cfg_key not in terminal_cfg:
continue
@ -3258,7 +3267,7 @@ def apply_terminal_config_to_env(
continue
if isinstance(value, str):
value = os.path.expanduser(value)
if should_override or env_var not in target:
if (should_override and cfg_key in explicit_keys) or env_var not in target:
target[env_var] = _terminal_env_value(value)
return target

View File

@ -712,7 +712,9 @@ DEFAULT_CONFIG = {
},
# Anthropic prompt caching (Claude via OpenRouter or native Anthropic API).
# cache_ttl must be "5m" or "1h" (Anthropic-supported tiers); other values are ignored.
# cache_ttl: "5m" or "1h" (Anthropic-supported tiers). Other non-falsy
# values are silently ignored. Falsy values (false, null, "off",
# "disabled", "no", "none") disable prompt caching entirely.
"prompt_caching": {
"cache_ttl": "5m",
},

View File

@ -62,9 +62,11 @@ except ImportError:
def _get_sessions_dir() -> Path:
"""Return the sessions directory using HERMES_HOME."""
from hermes_constants import get_hermes_home
return get_hermes_home() / "sessions"
try:
from hermes_constants import get_hermes_home
return get_hermes_home() / "sessions"
except ImportError:
return Path(os.environ.get("HERMES_HOME", Path.home() / ".hermes")) / "sessions"
def _get_session_db():
@ -192,9 +194,13 @@ def _load_sessions_index_from_json() -> dict:
def _load_channel_directory() -> dict:
"""Load the cached channel directory for available targets."""
from hermes_constants import get_hermes_home
directory_file = get_hermes_home() / "channel_directory.json"
try:
from hermes_constants import get_hermes_home
directory_file = get_hermes_home() / "channel_directory.json"
except ImportError:
directory_file = Path(
os.environ.get("HERMES_HOME", Path.home() / ".hermes")
) / "channel_directory.json"
if not directory_file.exists():
return {}
@ -292,6 +298,21 @@ class QueueEvent:
data: dict = field(default_factory=dict)
def _ts_float(ts) -> float:
"""Normalize a message timestamp (epoch int/float or ISO string) to float."""
if isinstance(ts, (int, float)):
return float(ts)
if isinstance(ts, str) and ts:
try:
return float(ts)
except ValueError:
try:
return datetime.fromisoformat(ts).timestamp()
except Exception:
return 0.0
return 0.0
class EventBridge:
"""Background poller that watches SessionDB for new messages and
maintains an in-memory event queue with waiter support.
@ -318,6 +339,13 @@ class EventBridge:
"""Start the background polling thread."""
if self._running:
return
# Snapshot existing history BEFORE the poll loop starts so pre-existing
# messages are not replayed as new events on startup (#13414). Sessions
# that first appear afterwards are absent from the baseline and default
# to last_seen=0.0 in _poll_once, so new-conversation delivery is
# preserved. Unit tests that drive _poll_once directly bypass start()
# and still observe first-poll delivery.
self._establish_baseline()
self._running = True
self._thread = threading.Thread(target=self._poll_loop, daemon=True)
self._thread.start()
@ -419,6 +447,46 @@ class EventBridge:
self._queue.pop(0)
self._new_event.set()
def _establish_baseline(self) -> None:
"""Record the latest per-session message timestamp and the current
state.db mtime WITHOUT emitting events, so startup does not replay
history (#13414).
Only sessions that already exist at startup are baselined; a session
that first appears afterwards is absent here and defaults to
last_seen=0.0 in _poll_once, so a brand-new conversation's first
message is still delivered on its state.db-change tick.
"""
db = _get_session_db()
if not db:
return
try:
from hermes_constants import get_hermes_home
db_file = get_hermes_home() / "state.db"
except ImportError:
db_file = Path(os.environ.get("HERMES_HOME", Path.home() / ".hermes")) / "state.db"
try:
self._state_db_mtime = db_file.stat().st_mtime if db_file.exists() else 0.0
except OSError:
self._state_db_mtime = 0.0
try:
self._cached_sessions_index = _load_sessions_index()
except Exception:
self._cached_sessions_index = {}
for session_key, entry in self._cached_sessions_index.items():
session_id = entry.get("session_id", "")
if not session_id:
continue
try:
messages = db.get_messages(session_id)
except Exception:
continue
all_ts = [_ts_float(m.get("timestamp", 0)) for m in (messages or ())]
if all_ts:
latest = max(all_ts)
if latest > 0.0:
self._last_poll_timestamps[session_key] = latest
def _poll_loop(self):
"""Background loop: poll SessionDB for new messages."""
db = _get_session_db()
@ -444,9 +512,11 @@ class EventBridge:
eliminating the old dual-file (sessions.json + state.db) race that
could drop brand-new conversations (#8925).
"""
from hermes_constants import get_hermes_home
db_file = get_hermes_home() / "state.db"
try:
from hermes_constants import get_hermes_home
db_file = get_hermes_home() / "state.db"
except ImportError:
db_file = Path(os.environ.get("HERMES_HOME", Path.home() / ".hermes")) / "state.db"
try:
db_mtime = db_file.stat().st_mtime if db_file.exists() else 0.0
@ -478,23 +548,8 @@ class EventBridge:
if not messages:
continue
# Normalize timestamps to float for comparison
def _ts_float(ts) -> float:
if isinstance(ts, (int, float)):
return float(ts)
if isinstance(ts, str) and ts:
try:
return float(ts)
except ValueError:
# ISO string — parse to epoch
try:
from datetime import datetime
return datetime.fromisoformat(ts).timestamp()
except Exception:
return 0.0
return 0.0
# Find messages newer than our last seen timestamp
# Find messages newer than our last seen timestamp (see the
# module-level _ts_float helper for timestamp normalization).
new_messages = []
for msg in messages:
ts = _ts_float(msg.get("timestamp", 0))

View File

@ -1521,6 +1521,24 @@ class AIAgent:
from agent.agent_runtime_helpers import anthropic_prompt_cache_policy
return anthropic_prompt_cache_policy(self, provider=provider, base_url=base_url, api_mode=api_mode, model=model)
def _direct_native_anthropic_tool_cache_capability(
self,
*,
provider: Optional[str] = None,
base_url: Optional[str] = None,
api_mode: Optional[str] = None,
model: Optional[str] = None,
) -> bool:
"""Forwarder for the request-local native Anthropic tool capability."""
from agent.agent_runtime_helpers import _direct_native_anthropic_tool_cache_capability
return _direct_native_anthropic_tool_cache_capability(
self,
provider=provider,
base_url=base_url,
api_mode=api_mode,
model=model,
)
@staticmethod
def _model_requires_responses_api(model: str) -> bool:
"""Return True for models that require the Responses API path.
@ -6555,10 +6573,10 @@ class AIAgent:
content[-1]["cache_control"] = {"type": "ephemeral"}
break
def _build_api_kwargs(self, api_messages: list) -> dict:
def _build_api_kwargs(self, api_messages: list, tools_for_api: Optional[list] = None) -> dict:
"""Forwarder — see ``agent.chat_completion_helpers.build_api_kwargs``."""
from agent.chat_completion_helpers import build_api_kwargs
return build_api_kwargs(self, api_messages)
return build_api_kwargs(self, api_messages, tools_for_api=tools_for_api)
def _supports_reasoning_extra_body(self) -> bool:
"""Return True when reasoning extra_body is safe to send for this route/model.

View File

@ -75,6 +75,7 @@ LEGACY_AUTHOR_MAP = {
"embwl0x@users.noreply.github.com": "embwl0x", # PR #65105 salvage (gateway: preserve external supervisor ownership)
"41409874+2751738943@users.noreply.github.com": "2751738943", # PR #54785 salvage (tui: post-turn completion ownership routing)
"Burgunthy@users.noreply.github.com": "Burgunthy", # PR #20096 salvage (gateway: profile-based routing for inbound messages)
"BB-light@users.noreply.github.com": "BB-light", # PR #76015 salvage (caching: honor cache_ttl disable; #33555)
"75556242+webtecnica@users.noreply.github.com": "webtecnica", # PR #63360 salvage (nous: restore inference-api base_url)
"contato@webtecnica.com.br": "webtecnica", # PR #70888 salvage
"skosarevivan@yandex.ru": "Epoxidex", # PR #29820 salvage (ollama: top-level reasoning_effort=none; #25758)

View File

@ -0,0 +1,327 @@
"""Behavioral regression tests for ACP background MCP discovery + late-refresh.
These replace the previous AST-based test that only inspected source text.
They verify the *behavior*: (1) a blocked discovery doesn't block startup, and
(2) a delayed-but-reachable MCP server's tools land in the agent's snapshot
via the automatic late-refresh, cache-safely (pre-first-turn only).
"""
from __future__ import annotations
import sys
import threading
import time
import types
from contextlib import nullcontext
from types import ModuleType, SimpleNamespace
import pytest
from acp_adapter.server import HermesACPAgent
from acp_adapter.session import SessionManager, SessionState
from hermes_cli import mcp_startup
# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------
class FakeAgent:
"""Minimal stand-in for AIAgent with the attributes late-refresh touches."""
def __init__(self):
self.model = "fake-model"
self.provider = "fake-provider"
self.enabled_toolsets = ["hermes-acp"]
self.disabled_toolsets = []
self.tools = []
self.valid_tool_names = set()
self._user_turn_count = 0
self._api_call_count = 0
class NoopDb:
def get_session(self, *_a, **_k):
return None
def create_session(self, *_a, **_k):
return None
def update_session(self, *_a, **_k):
return None
def _mod(name: str, **attrs) -> ModuleType:
module = ModuleType(name)
for key, value in attrs.items():
setattr(module, key, value)
return module
@pytest.fixture(autouse=True)
def _reset_mcp_startup_state():
"""Ensure each test starts with a clean discovery thread state."""
saved_started = mcp_startup._mcp_discovery_started
saved_thread = mcp_startup._mcp_discovery_thread
mcp_startup._mcp_discovery_started = False
mcp_startup._mcp_discovery_thread = None
yield
thread = mcp_startup._mcp_discovery_thread
if thread is not None and thread.is_alive():
thread.join(timeout=2.0)
mcp_startup._mcp_discovery_started = saved_started
mcp_startup._mcp_discovery_thread = saved_thread
# ---------------------------------------------------------------------------
# Test 1 — blocked discovery does not block startup
# ---------------------------------------------------------------------------
def test_acp_background_discovery_does_not_block_startup(monkeypatch):
"""start_background_mcp_discovery must return immediately even if discovery hangs."""
block = threading.Event()
def _blocking_discover():
block.wait(timeout=5.0)
monkeypatch.setitem(
sys.modules,
"hermes_cli.config",
_mod(
"hermes_cli.config",
read_raw_config=lambda: {"mcp_servers": {"slow": {"url": "https://mcp.example.test"}}},
),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_oauth",
_mod("tools.mcp_oauth", suppress_interactive_oauth=lambda: nullcontext()),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", discover_mcp_tools=_blocking_discover),
)
start = time.monotonic()
mcp_startup.start_background_mcp_discovery(
logger=SimpleNamespace(debug=lambda *_a, **_k: None),
thread_name="test-acp-discovery",
)
elapsed = time.monotonic() - start
assert elapsed < 0.2, "start_background_mcp_discovery blocked for {:.3f}s".format(elapsed)
assert mcp_startup._mcp_discovery_thread is not None
assert mcp_startup._mcp_discovery_thread.is_alive()
block.set()
mcp_startup._mcp_discovery_thread.join(timeout=2.0)
# ---------------------------------------------------------------------------
# Test 2 — delayed discovery lands tools via late-refresh (pre-first-turn)
# ---------------------------------------------------------------------------
def test_acp_late_refresh_adds_tools_when_discovery_lands_after_build(monkeypatch):
"""A slow MCP server that finishes after agent build must still appear in tools."""
discovery_block = threading.Event()
discovery_done = threading.Event()
def _slow_discover():
discovery_block.wait(timeout=5.0)
discovery_done.set()
monkeypatch.setitem(
sys.modules,
"hermes_cli.config",
_mod(
"hermes_cli.config",
read_raw_config=lambda: {"mcp_servers": {"slow": {"url": "https://mcp.example.test"}}},
),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_oauth",
_mod("tools.mcp_oauth", suppress_interactive_oauth=lambda: nullcontext()),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", discover_mcp_tools=_slow_discover),
)
mcp_startup.start_background_mcp_discovery(
logger=SimpleNamespace(debug=lambda *_a, **_k: None),
thread_name="test-acp-late",
)
# Build the session immediately — discovery is still in flight.
fake = FakeAgent()
manager = SessionManager(agent_factory=lambda **_k: fake, db=NoopDb())
acp_agent = HermesACPAgent(session_manager=manager)
state = manager.create_session(cwd=".")
# Discovery is blocked, so it must still be in flight.
assert not discovery_done.is_set(), "discovery finished too early for this test"
# Track refresh_agent_mcp_tools calls.
refreshed = []
def _fake_refresh(agent, **_kw):
agent.tools = [{"function": {"name": "mcp_slow_tool"}}]
agent.valid_tool_names = {"mcp_slow_tool"}
refreshed.append(agent)
return {"mcp_slow_tool"}
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", refresh_agent_mcp_tools=_fake_refresh),
)
# Trigger late-refresh.
acp_agent._schedule_mcp_late_refresh(state)
# Release discovery so the late-refresh daemon can proceed.
discovery_block.set()
# Wait for the late-refresh daemon to finish.
deadline = time.monotonic() + 5.0
while not refreshed and time.monotonic() < deadline:
time.sleep(0.01)
assert refreshed, "late-refresh daemon did not call refresh_agent_mcp_tools"
assert refreshed[0] is fake
assert "mcp_slow_tool" in fake.valid_tool_names
# ---------------------------------------------------------------------------
# Test 3 — late-refresh is cache-safe: skips after first turn
# ---------------------------------------------------------------------------
def test_acp_late_refresh_skips_after_first_turn(monkeypatch):
"""Once the user has sent a message, late-refresh must NOT rebuild tools."""
discovery_block = threading.Event()
def _slow_discover():
discovery_block.wait(timeout=5.0)
monkeypatch.setitem(
sys.modules,
"hermes_cli.config",
_mod(
"hermes_cli.config",
read_raw_config=lambda: {"mcp_servers": {"slow": {"url": "https://mcp.example.test"}}},
),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_oauth",
_mod("tools.mcp_oauth", suppress_interactive_oauth=lambda: nullcontext()),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", discover_mcp_tools=_slow_discover),
)
mcp_startup.start_background_mcp_discovery(
logger=SimpleNamespace(debug=lambda *_a, **_k: None),
thread_name="test-acp-cache",
)
fake = FakeAgent()
fake._api_call_count = 1 # simulate: user already sent a message
manager = SessionManager(agent_factory=lambda **_k: fake, db=NoopDb())
acp_agent = HermesACPAgent(session_manager=manager)
state = manager.create_session(cwd=".")
refreshed = []
def _fake_refresh(agent, **_kw):
refreshed.append(agent)
return set()
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", refresh_agent_mcp_tools=_fake_refresh),
)
acp_agent._schedule_mcp_late_refresh(state)
# Release discovery so the daemon can proceed (if it were going to).
discovery_block.set()
# Give the daemon time to run (if it were going to).
time.sleep(0.5)
assert not refreshed, "late-refresh rebuilt tools after the first turn — cache broken!"
# ---------------------------------------------------------------------------
# Test 4 — late-refresh is serialized with turn start: skips while running
# ---------------------------------------------------------------------------
def test_acp_late_refresh_skips_while_turn_running(monkeypatch):
"""A turn in flight (state.is_running) must block the rebuild even when
the agent's counters still read zero — closes the guard/turn-start race."""
discovery_block = threading.Event()
def _slow_discover():
discovery_block.wait(timeout=5.0)
monkeypatch.setitem(
sys.modules,
"hermes_cli.config",
_mod(
"hermes_cli.config",
read_raw_config=lambda: {"mcp_servers": {"slow": {"url": "https://mcp.example.test"}}},
),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_oauth",
_mod("tools.mcp_oauth", suppress_interactive_oauth=lambda: nullcontext()),
)
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", discover_mcp_tools=_slow_discover),
)
mcp_startup.start_background_mcp_discovery(
logger=SimpleNamespace(debug=lambda *_a, **_k: None),
thread_name="test-acp-running",
)
fake = FakeAgent() # counters are 0 — only is_running blocks the refresh
manager = SessionManager(agent_factory=lambda **_k: fake, db=NoopDb())
acp_agent = HermesACPAgent(session_manager=manager)
state = manager.create_session(cwd=".")
state.is_running = True # simulate: first prompt dispatched concurrently
refreshed = []
def _fake_refresh(agent, **_kw):
refreshed.append(agent)
return set()
monkeypatch.setitem(
sys.modules,
"tools.mcp_tool",
_mod("tools.mcp_tool", refresh_agent_mcp_tools=_fake_refresh),
)
acp_agent._schedule_mcp_late_refresh(state)
discovery_block.set()
time.sleep(0.5)
assert not refreshed, "late-refresh rebuilt tools while a turn was running!"

View File

@ -4120,3 +4120,96 @@ class TestMoaAggregatorStreamingBypass:
assert result is completed
direct_create.assert_called_once()
relay_stream.assert_not_called()
class TestSynchronousFallbackCachePlans:
@staticmethod
def _run_configured_fallback(monkeypatch, entry):
from agent.auxiliary_client import (
_call_fallback_candidate_sync,
_try_configured_fallback_chain,
)
client = MagicMock()
client.base_url = entry["base_url"]
client.chat.completions.create.return_value = _DummyResponse()
resolved_calls = []
def resolve(provider, model=None, **kwargs):
resolved_calls.append((provider, model, kwargs))
return client, model
monkeypatch.setattr("agent.auxiliary_client.resolve_provider_client", resolve)
monkeypatch.setattr(
"agent.auxiliary_client._get_auxiliary_task_config",
lambda task: {"fallback_chain": [entry]},
)
fallback_client, fallback_model, label = _try_configured_fallback_chain(
task="moa_aggregator",
failed_provider="primary",
)
tools = [{
"type": "function",
"function": {
"name": "lookup",
"parameters": {"type": "object", "properties": {}},
},
}]
_call_fallback_candidate_sync(
fallback_client,
fallback_model,
label,
task="moa_aggregator",
messages=[
{"role": "system", "content": "stable prefix"},
{"role": "user", "content": "lookup"},
],
temperature=None,
max_tokens=None,
tools=tools,
effective_timeout=30.0,
effective_extra_body={},
reasoning_config=None,
)
return client, resolved_calls, tools
def test_direct_anthropic_fallback_uses_entry_destination_for_tool_marker(self, monkeypatch):
client, resolved_calls, tools = self._run_configured_fallback(monkeypatch, {
"provider": "anthropic",
"model": "claude-sonnet-4-6",
"base_url": "https://api.anthropic.com",
"api_mode": "anthropic_messages",
})
assert resolved_calls == [(
"anthropic",
"claude-sonnet-4-6",
{
"explicit_base_url": "https://api.anthropic.com",
"explicit_api_key": None,
"api_mode": "anthropic_messages",
},
)]
wire_tools = client.chat.completions.create.call_args.kwargs["tools"]
assert "cache_control" in wire_tools[-1]
assert "cache_control" not in tools[-1]
def test_third_party_anthropic_fallback_keeps_message_markers_without_tool_marker(self, monkeypatch):
client, resolved_calls, tools = self._run_configured_fallback(monkeypatch, {
"provider": "custom",
"model": "claude-sonnet-4-6",
"base_url": "https://api.minimax.io/anthropic",
"api_mode": "anthropic_messages",
})
assert resolved_calls[0][2]["explicit_base_url"] == "https://api.minimax.io/anthropic"
assert resolved_calls[0][2]["api_mode"] == "anthropic_messages"
wire_request = client.chat.completions.create.call_args.kwargs
assert "cache_control" not in wire_request["tools"][-1]
assert "cache_control" not in tools[-1]
assert any(
isinstance(part, dict) and "cache_control" in part
for message in wire_request["messages"]
for part in (message.get("content") if isinstance(message.get("content"), list) else [])
)

View File

@ -58,6 +58,8 @@ def _cache_agent(
static=None,
cache_ttl="5m",
provider="openai",
tools=None,
direct_tool_cache=False,
):
return SimpleNamespace(
_cached_system_prompt=prompt,
@ -67,6 +69,8 @@ def _cache_agent(
_use_native_cache_layout=native,
_cache_ttl=cache_ttl,
provider=provider,
tools=tools or [],
_direct_native_anthropic_tool_cache_capability=lambda: direct_tool_cache,
client=None,
)
@ -212,6 +216,46 @@ class TestRedecoratePromptCacheOnPolicyChange:
assert isinstance(decorated[0]["content"], list)
assert decorated[0]["content"][0]["text"] == self._STATIC
def test_replans_tools_for_the_active_destination(self):
from agent.prompt_caching import build_prompt_cache_plan
tools = [{"type": "function", "function": {"name": "lookup", "parameters": {"type": "object", "properties": {}}}}]
messages = [
{"role": "system", "content": self._STATIC + "volatile"},
{"role": "user", "content": "lookup"},
]
source = build_prompt_cache_plan(
messages,
tools,
native_anthropic=True,
static_system_prefix=self._STATIC,
direct_native_tool_cache=True,
)
agent = _cache_agent(
use_caching=True,
native=False,
prompt=messages[0]["content"],
static=self._STATIC,
provider="openrouter",
tools=tools,
direct_tool_cache=False,
)
fallback_messages, _, fallback_tools = _redecorate_prompt_cache_for_provider(
agent,
source.messages,
tools_for_api=source.tools,
)
assert "cache_control" in source.tools[-1]
assert "cache_control" not in fallback_tools[-1]
assert "cache_control" not in tools[-1]
assert all(
part.get("cache_control")
for part in fallback_messages[0]["content"]
if isinstance(part, dict)
)

View File

@ -15,6 +15,7 @@ even when the resolved aggregator slot is a cache-honoring route.
from __future__ import annotations
import copy
from types import SimpleNamespace
import pytest
@ -112,3 +113,41 @@ def test_aggregator_synthesis_untouched_on_non_caching_route(
agg_kwargs = _aggregator_kwargs(captured_calls)
synth_message = agg_kwargs["messages"][0]
assert isinstance(synth_message["content"], str), "must stay undecorated (plain string content)"
def test_prepared_aggregator_plans_tools_without_decorating_prepared_state(monkeypatch):
from agent import moa_loop
calls = []
monkeypatch.setattr(moa_loop, "call_llm", lambda **kwargs: calls.append(kwargs) or _response())
monkeypatch.setattr(
moa_loop,
"_slot_runtime",
lambda slot: {
"provider": "anthropic",
"model": "claude-sonnet-4-6",
"base_url": "https://api.anthropic.com",
"api_mode": "anthropic_messages",
},
)
completions = moa_loop.MoAChatCompletions.__new__(moa_loop.MoAChatCompletions)
completions._pending_trace = None
prepared = {
"messages": [
{"role": "system", "content": "system"},
{"role": "user", "content": "lookup"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "lookup", "function": {"name": "lookup", "arguments": "{}"}}]},
{"role": "tool", "tool_call_id": "lookup", "content": "result"},
],
"guidance": None,
"aggregator": {"provider": "anthropic", "model": "claude-sonnet-4-6"},
"aggregator_temperature": None,
}
canonical_prepared = copy.deepcopy(prepared)
tools = [{"type": "function", "function": {"name": "lookup", "parameters": {"type": "object", "properties": {}}}}]
completions._call_prepared_aggregator(prepared, {"tools": tools})
assert "cache_control" in calls[0]["tools"][-1]
assert "cache_control" not in tools[-1]
assert prepared == canonical_prepared

View File

@ -1177,3 +1177,111 @@ class TestMoAContextLength:
assert compressor.context_length == configured_context
assert compressor.threshold_tokens == configured_context // 2
endpoint_probe.assert_not_called()
# =========================================================================
# Fallback diagnostic logging
# =========================================================================
class TestFallbackWarning:
"""When all 9 detection methods fail, the 10th fallback should log a
warning so users with small-context models (8K, 32K) don't silently get
256K and hit hard-to-debug API context-length errors.
The warning is deduped per (model, base_url) — the fallback result is
deliberately never cached, so without dedup it would repeat on every
resolution (e.g. once per gateway message via session hygiene).
"""
@pytest.fixture(autouse=True)
def _reset_warned_set(self):
from agent import model_metadata as mm
mm._FALLBACK_WARNED.clear()
yield
mm._FALLBACK_WARNED.clear()
@staticmethod
def _patch_all_lookups():
from contextlib import ExitStack
stack = ExitStack()
for target, value in [
("agent.model_metadata.get_cached_context_length", None),
("agent.model_metadata.fetch_model_metadata", {}),
("agent.model_metadata.fetch_endpoint_model_metadata", {}),
("agent.model_metadata._query_ollama_api_show", None),
("agent.model_metadata._query_anthropic_context_length", None),
("agent.model_metadata._endpoint_scoped_context_length", None),
("agent.model_metadata._resolve_endpoint_context_length", None),
("agent.models_dev.lookup_models_dev_context", None),
]:
stack.enter_context(patch(target, return_value=value))
return stack
def test_warning_emitted_on_fallback(self, caplog):
import logging
with self._patch_all_lookups():
with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
result = get_model_context_length(
"totally-unknown-model-xyz",
)
assert result == DEFAULT_FALLBACK_CONTEXT
# The warning must mention the model name and the config override hint.
warning_msgs = [r for r in caplog.records if r.levelno == logging.WARNING]
assert any("totally-unknown-model-xyz" in r.getMessage() for r in warning_msgs)
assert any("model.context_length" in r.getMessage() for r in warning_msgs)
def test_warning_fires_once_per_model(self, caplog):
"""Repeated resolutions of the same unknown model warn only once."""
import logging
with self._patch_all_lookups():
with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
for _ in range(3):
get_model_context_length("totally-unknown-model-xyz")
fallback_warnings = [
r for r in caplog.records
if r.levelno == logging.WARNING and "falling back" in r.getMessage()
]
assert len(fallback_warnings) == 1
def test_warning_emitted_on_custom_endpoint_fallback(self, caplog):
"""The sibling step-3b fallback (custom/local endpoint, probes down,
no catalog match) is the same silent-256K bug class and must warn too."""
import logging
with self._patch_all_lookups(), \
patch("agent.model_metadata._query_local_context_length", return_value=None):
with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
result = get_model_context_length(
"totally-unknown-model-xyz",
base_url="http://192.168.1.50:8080/v1",
)
assert result == DEFAULT_FALLBACK_CONTEXT
warning_msgs = [r for r in caplog.records if r.levelno == logging.WARNING]
assert any("totally-unknown-model-xyz" in r.getMessage() for r in warning_msgs)
assert any("model.context_length" in r.getMessage() for r in warning_msgs)
def test_no_warning_when_cached(self, caplog):
"""No fallback warning when the context length is found in the cache."""
import logging
with patch(
"agent.model_metadata.get_cached_context_length",
return_value=32_000,
):
with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
result = get_model_context_length(
"some-model",
base_url="http://127.0.0.1:1/v1",
)
assert result == 32_000
fallback_warnings = [
r for r in caplog.records
if r.levelno == logging.WARNING and "falling back" in r.getMessage()
]
assert len(fallback_warnings) == 0

View File

@ -6,13 +6,139 @@ from agent.prompt_caching import (
_build_marker,
_can_carry_marker,
apply_anthropic_cache_control,
build_prompt_cache_plan,
strip_anthropic_cache_control,
strip_anthropic_tool_cache_control,
)
MARKER = {"type": "ephemeral"}
def _native_marker_indexes(messages):
return {
index
for index, message in enumerate(messages)
if isinstance(message, dict)
and (
"cache_control" in message
or any(
isinstance(part, dict) and "cache_control" in part
for part in (message.get("content") if isinstance(message.get("content"), list) else [])
)
)
}
def _tool_heavy_native_history():
return [
{"role": "system", "content": "stable prefix\nvolatile suffix"},
{"role": "user", "content": "first request"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "first", "function": {"name": "tool_00", "arguments": "{}"}}]},
{"role": "tool", "tool_call_id": "first", "content": "first result"},
{"role": "user", "content": "second request"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "second", "function": {"name": "tool_01", "arguments": "{}"}}]},
{"role": "tool", "tool_call_id": "second", "content": "second result"},
]
def _tool_heavy_native_tools():
return [
{
"type": "function",
"function": {
"name": f"tool_{index:02d}",
"description": f"Deterministic tool {index}",
"parameters": {"type": "object", "properties": {}},
},
}
for index in range(28)
]
def test_t20880_tool_heavy_native_loop_reproduction():
"""A 28-tool native loop needs a tool marker and a retained transaction endpoint."""
tools = _tool_heavy_native_tools()
before_exchange = build_prompt_cache_plan(
_tool_heavy_native_history(),
tools,
native_anthropic=True,
static_system_prefix="stable prefix",
direct_native_tool_cache=True,
)
after_exchange = build_prompt_cache_plan(
_tool_heavy_native_history() + [
{"role": "user", "content": "third request"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "third", "function": {"name": "tool_02", "arguments": "{}"}}]},
{"role": "tool", "tool_call_id": "third", "content": "third result"},
],
tools,
native_anthropic=True,
static_system_prefix="stable prefix",
direct_native_tool_cache=True,
)
before_markers = _native_marker_indexes(before_exchange.messages)
after_markers = _native_marker_indexes(after_exchange.messages)
final_tool_marked = "cache_control" in after_exchange.tools[-1]
shared_transaction_endpoint = bool((before_markers - {0}) & (after_markers - {0}))
assert final_tool_marked
assert shared_transaction_endpoint
assert after_exchange.marker_count <= 4
class TestPromptCachePlan:
def test_copies_sections_and_keeps_canonical_tools_plain(self):
import copy
messages = _tool_heavy_native_history()
tools = _tool_heavy_native_tools()
original_messages = copy.deepcopy(messages)
original_tools = copy.deepcopy(tools)
plan = build_prompt_cache_plan(
messages,
tools,
native_anthropic=True,
static_system_prefix="stable prefix",
direct_native_tool_cache=True,
)
assert messages == original_messages
assert tools == original_tools
assert plan.messages is not messages
assert plan.tools is not tools
assert "cache_control" not in tools[-1]
assert plan.tools[-1]["cache_control"] == MARKER
assert plan.marker_count == 4
def test_unmarkable_endpoint_does_not_consume_a_slot(self):
messages = [
{"role": "system", "content": "stable prefix\nvolatile"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "pending", "function": {"name": "tool_00", "arguments": "{}"}}]},
]
plan = build_prompt_cache_plan(
messages,
_tool_heavy_native_tools(),
native_anthropic=True,
static_system_prefix="stable prefix",
direct_native_tool_cache=True,
)
assert plan.marker_count == 2
assert "cache_control" not in plan.messages[-1]
def test_tool_strip_is_request_local(self):
tools = _tool_heavy_native_tools()
tools[-1]["cache_control"] = MARKER
stripped = strip_anthropic_tool_cache_control(tools)
assert "cache_control" in tools[-1]
assert "cache_control" not in stripped[-1]
class TestApplyCacheMarker:
def test_tool_message_gets_top_level_marker_on_native_anthropic(self):
"""Native Anthropic path: cache_control injected top-level (adapter moves it inside tool_result)."""
@ -248,10 +374,9 @@ class TestNormalizationOrdering:
from agent import conversation_loop
src = inspect.getsource(conversation_loop)
# Anchor on the call-block decoration (before the retry loop), not the
# mid-failover redecoration helper which also calls apply_*.
anchor = src.index("Runs LAST, after every message mutation above")
mark = src.index("apply_anthropic_cache_control(\n", anchor)
# Anchor on the call-block request plan, not the retry helper.
anchor = src.index("Build the request-local cache sections")
mark = src.index("build_prompt_cache_plan(\n", anchor)
for earlier in (
'am["content"].strip()', # whitespace normalization
"_sanitize_api_messages(api_messages)", # orphan sweep

View File

@ -43,6 +43,38 @@ class TestNativeAnthropic:
)
assert agent._anthropic_prompt_cache_policy() == (True, True)
def test_anthropic_provider_on_third_party_host_stays_message_only(self):
agent = _make_agent(
provider="anthropic",
base_url="https://api.minimax.io/anthropic",
api_mode="anthropic_messages",
model="claude-sonnet-4-6",
)
assert agent._anthropic_prompt_cache_policy() == (True, True)
assert agent._direct_native_anthropic_tool_cache_capability() is False
def test_only_direct_native_anthropic_enables_tool_markers(self):
agent = _make_agent(
provider="anthropic",
base_url="https://api.anthropic.com",
api_mode="anthropic_messages",
model="claude-sonnet-4-6",
)
assert agent._direct_native_anthropic_tool_cache_capability() is True
assert agent._direct_native_anthropic_tool_cache_capability(
provider="custom",
base_url="https://api.minimax.io/anthropic",
api_mode="anthropic_messages",
model="claude-sonnet-4-6",
) is False
assert agent._direct_native_anthropic_tool_cache_capability(
provider="openrouter",
base_url="https://openrouter.ai/api/v1",
api_mode="chat_completions",
model="anthropic/claude-sonnet-4.6",
) is False
class TestOpenRouter:

View File

@ -716,6 +716,66 @@ class TestInit:
)
assert a._cache_ttl == "5m"
@pytest.mark.parametrize(
"falsy_value", [False, None, "off", "false", "disabled", "no", "none"],
)
def test_prompt_caching_disabled_by_falsy_cache_ttl(self, falsy_value):
"""Falsy cache_ttl values should fully disable prompt caching."""
with (
patch("run_agent.get_tool_definitions", return_value=[]),
patch("run_agent.check_toolset_requirements", return_value={}),
patch("run_agent.OpenAI"),
patch(
"hermes_cli.config.load_config",
return_value={"prompt_caching": {"cache_ttl": falsy_value}},
),
patch(
"hermes_cli.config.load_config_readonly",
return_value={"prompt_caching": {"cache_ttl": falsy_value}},
),
):
a = AIAgent(
api_key="test-k...7890",
model="anthropic/claude-sonnet-4-20250514",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert a._use_prompt_caching is False
assert a._use_native_cache_layout is False
assert a._cache_ttl is None
def test_prompt_caching_disable_survives_policy_rederivation(self):
"""The disable must survive anthropic_prompt_cache_policy() re-derivation
(called during /model switch and fallback activation)."""
with (
patch("run_agent.get_tool_definitions", return_value=[]),
patch("run_agent.check_toolset_requirements", return_value={}),
patch("run_agent.OpenAI"),
patch(
"hermes_cli.config.load_config",
return_value={"prompt_caching": {"cache_ttl": False}},
),
patch(
"hermes_cli.config.load_config_readonly",
return_value={"prompt_caching": {"cache_ttl": False}},
),
):
a = AIAgent(
api_key="test-k...7890",
model="anthropic/claude-sonnet-4-20250514",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert a._cache_ttl is None
# Re-run the policy (simulates /model switch or fallback)
should_cache, use_native = a._anthropic_prompt_cache_policy()
assert should_cache is False
assert use_native is False
assert a._use_prompt_caching is False
def test_constructor_max_tokens_wins_over_config(self):
@ -1149,6 +1209,38 @@ class TestBuildApiKwargs:
assert kwargs["messages"] is messages
assert kwargs["timeout"] == 1800.0
def test_explicit_request_local_tools_reach_native_transport(self, agent, monkeypatch):
from agent.prompt_caching import build_prompt_cache_plan
canonical_tools = [
{
"type": "function",
"function": {
"name": "lookup",
"parameters": {"type": "object", "properties": {}},
},
}
]
plan = build_prompt_cache_plan(
[{"role": "system", "content": "stable\nvolatile"}, {"role": "user", "content": "lookup"}],
canonical_tools,
native_anthropic=True,
static_system_prefix="stable",
direct_native_tool_cache=True,
)
transport = MagicMock()
transport.build_kwargs.side_effect = lambda **kwargs: kwargs
agent.api_mode = "anthropic_messages"
agent.provider = "anthropic"
agent.base_url = "https://api.anthropic.com"
monkeypatch.setattr(agent, "_get_transport", lambda: transport)
monkeypatch.setattr(agent, "_prepare_anthropic_messages_for_api", lambda messages: messages)
kwargs = agent._build_api_kwargs(plan.messages, tools_for_api=plan.tools)
assert "cache_control" not in canonical_tools[-1]
assert kwargs["tools"][-1]["cache_control"] == {"type": "ephemeral"}
def test_public_moonshot_kimi_k2_5_omits_temperature(self, agent):
"""Kimi models should NOT have client-side temperature overrides.

View File

@ -0,0 +1,136 @@
"""Tests for batch_runner trajectory durability and pool cleanup.
Verifies:
1. Trajectory entries are fsync'd to disk before the checkpoint marks
them as completed (crash-between-write-and-sync safety).
2. BatchRunner.run() calls pool.terminate() + pool.join() on
KeyboardInterrupt and Exception during batch execution (responsive
worker shutdown). CPython's Pool.join() takes no timeout parameter —
join(timeout=10) raises TypeError — so the tests also assert join()
is invoked with no arguments.
"""
import json
import os
import sys
from pathlib import Path
from unittest.mock import MagicMock, call, patch
import pytest
# batch_runner is a root-level module (not part of an installed package),
# so make the repo root importable when tests run from elsewhere.
sys.path.insert(0, str(Path(__file__).parent.parent))
import batch_runner
from batch_runner import BatchRunner, _process_batch_worker
# =========================================================================
# Trajectory write durability (fsync)
# =========================================================================
class TestTrajectoryWriteDurability:
"""Verify that trajectory entries are flushed and fsync'd to disk.
Without fsync, a crash between the write and the disk sync could leave
the checkpoint claiming completion with no trajectory data on disk.
"""
def test_trajectory_entry_is_synced_to_disk(self, tmp_path, monkeypatch):
"""_process_batch_worker should flush+fsync the trajectory file."""
prompt_result = {
"success": True,
"trajectory": [{"role": "assistant", "content": "x"}],
"reasoning_stats": {"has_any_reasoning": True},
"tool_stats": {},
"metadata": {},
"completed": True,
"api_calls": 1,
"toolsets_used": [],
}
monkeypatch.setattr(
"batch_runner._process_single_prompt", lambda *a, **kw: prompt_result
)
# Intercept os.fsync to record calls
fsync_calls = []
monkeypatch.setattr("os.fsync", lambda fd: fsync_calls.append(fd))
_process_batch_worker(
(
1,
[(0, {"prompt": "hi"})],
tmp_path,
set(),
{"verbose": False},
)
)
# Verify fsync was called at least once during trajectory write
assert len(fsync_calls) >= 1, (
"os.fsync was not called — trajectory writes are not durable"
)
# Verify the trajectory file exists and is valid
output_files = list(tmp_path.glob("*.jsonl"))
assert len(output_files) >= 1
for f in output_files:
lines = f.read_text().strip().split("\n")
for line in lines:
if line:
entry = json.loads(line)
assert "conversations" in entry
assert "completed" in entry
# =========================================================================
# Pool cleanup on interruption / exception — drives the REAL run()
# =========================================================================
def _make_runner(tmp_path, monkeypatch):
"""Build a minimal real BatchRunner against a 1-line tmp dataset."""
dataset = tmp_path / "dataset.jsonl"
dataset.write_text(json.dumps({"prompt": "hi"}) + "\n", encoding="utf-8")
# BatchRunner writes to Path("data")/run_name relative to cwd.
monkeypatch.chdir(tmp_path)
return BatchRunner(
dataset_file=str(dataset),
batch_size=1,
run_name="pool-cleanup-test",
num_workers=1,
)
def _make_failing_pool(exc):
"""Context-manager mock whose pool raises `exc` from imap_unordered."""
pool = MagicMock()
pool.imap_unordered.side_effect = exc
pool_cm = MagicMock()
pool_cm.__enter__ = MagicMock(return_value=pool)
pool_cm.__exit__ = MagicMock(return_value=False)
return pool, pool_cm
class TestPoolCleanupOnInterruption:
"""Drive the real BatchRunner.run() with a patched Pool and verify the
cleanup contract: terminate() + join() (join with NO timeout argument —
CPython's Pool.join signature is (self), so join(timeout=10) would
raise TypeError).
"""
@pytest.mark.parametrize("exc_type", [KeyboardInterrupt, RuntimeError])
def test_run_terminates_and_joins_pool(self, tmp_path, monkeypatch, exc_type):
runner = _make_runner(tmp_path, monkeypatch)
pool, pool_cm = _make_failing_pool(exc_type("boom"))
with patch.object(batch_runner, "Pool", return_value=pool_cm):
with pytest.raises(exc_type):
runner.run()
pool.terminate.assert_called_once()
# join() must be called with no positional/keyword arguments.
assert pool.join.call_args_list == [call()], (
f"pool.join() called with unexpected args: {pool.join.call_args_list}"
)

View File

@ -259,9 +259,23 @@ def fake_mcp_server(populated_sessions_dir, mock_session_db, monkeypatch):
# 1. UNIT TESTS — helpers, extraction, attachments
# ---------------------------------------------------------------------------
class TestImports:
def test_import_module(self):
import mcp_serve
assert hasattr(mcp_serve, "create_mcp_server")
assert hasattr(mcp_serve, "run_mcp_server")
assert hasattr(mcp_serve, "EventBridge")
def test_mcp_available_flag(self):
import mcp_serve
assert isinstance(mcp_serve._MCP_SERVER_AVAILABLE, bool)
class TestHelpers:
def test_get_sessions_dir(self, tmp_path):
from mcp_serve import _get_sessions_dir
result = _get_sessions_dir()
assert result == tmp_path / "sessions"
def test_coerce_int_handles_invalid_and_out_of_range_values(self):
from mcp_serve import _coerce_int
@ -272,7 +286,16 @@ class TestHelpers:
assert _coerce_int(999, default=50, minimum=1, maximum=200) == 200
assert _coerce_int(-5, default=50, minimum=1, maximum=200) == 1
def test_load_sessions_index_empty(self, sessions_dir, monkeypatch):
import mcp_serve
monkeypatch.setattr(mcp_serve, "_get_sessions_dir", lambda: sessions_dir)
assert mcp_serve._load_sessions_index() == {}
def test_load_sessions_index_with_data(self, populated_sessions_dir, monkeypatch):
import mcp_serve
monkeypatch.setattr(mcp_serve, "_get_sessions_dir", lambda: populated_sessions_dir)
result = mcp_serve._load_sessions_index()
assert len(result) == 3
def test_load_sessions_index_corrupt(self, sessions_dir, monkeypatch):
(sessions_dir / "sessions.json").write_text("not json!")
@ -295,6 +318,11 @@ class TestContentExtraction:
]}
assert _extract_message_content(msg) == "A\nB"
def test_empty(self):
from mcp_serve import _extract_message_content
assert _extract_message_content({"content": ""}) == ""
assert _extract_message_content({}) == ""
assert _extract_message_content({"content": None}) == ""
class TestAttachmentExtraction:
@ -307,8 +335,21 @@ class TestAttachmentExtraction:
assert len(att) == 1
assert att[0] == {"type": "image", "url": "http://x.com/pic.jpg"}
def test_media_tag_in_text(self):
from mcp_serve import _extract_attachments
msg = {"content": "Here MEDIA: /tmp/out.png done"}
att = _extract_attachments(msg)
assert len(att) == 1
assert att[0] == {"type": "media", "path": "/tmp/out.png"}
def test_multiple_media_tags(self):
from mcp_serve import _extract_attachments
msg = {"content": "MEDIA: /a.png and MEDIA: /b.mp3"}
assert len(_extract_attachments(msg)) == 2
def test_no_attachments(self):
from mcp_serve import _extract_attachments
assert _extract_attachments({"content": "plain text"}) == []
def test_image_content_block(self):
from mcp_serve import _extract_attachments
@ -322,6 +363,11 @@ class TestAttachmentExtraction:
# ---------------------------------------------------------------------------
class TestEventBridge:
def test_create(self):
from mcp_serve import EventBridge
b = EventBridge()
assert b._cursor == 0
assert b._queue == []
def test_enqueue_and_poll(self):
from mcp_serve import EventBridge, QueueEvent
@ -333,8 +379,29 @@ class TestEventBridge:
assert r["events"][0]["type"] == "message"
assert r["next_cursor"] == 1
def test_cursor_filter(self):
from mcp_serve import EventBridge, QueueEvent
b = EventBridge()
for i in range(5):
b._enqueue(QueueEvent(cursor=0, type="message", session_key=f"s{i}"))
r = b.poll_events(after_cursor=3)
assert len(r["events"]) == 2
assert r["events"][0]["session_key"] == "s3"
def test_session_filter(self):
from mcp_serve import EventBridge, QueueEvent
b = EventBridge()
b._enqueue(QueueEvent(cursor=0, type="message", session_key="a"))
b._enqueue(QueueEvent(cursor=0, type="message", session_key="b"))
b._enqueue(QueueEvent(cursor=0, type="message", session_key="a"))
r = b.poll_events(after_cursor=0, session_key="a")
assert len(r["events"]) == 2
def test_poll_empty(self):
from mcp_serve import EventBridge
r = EventBridge().poll_events(after_cursor=0)
assert r["events"] == []
assert r["next_cursor"] == 0
def test_poll_limit(self):
from mcp_serve import EventBridge, QueueEvent
@ -344,8 +411,37 @@ class TestEventBridge:
r = b.poll_events(after_cursor=0, limit=3)
assert len(r["events"]) == 3
def test_wait_immediate(self):
from mcp_serve import EventBridge, QueueEvent
b = EventBridge()
b._enqueue(QueueEvent(cursor=0, type="message", session_key="t",
data={"content": "hi"}))
event = b.wait_for_event(after_cursor=0, timeout_ms=100)
assert event is not None
assert event["type"] == "message"
def test_wait_timeout(self):
from mcp_serve import EventBridge
start = time.monotonic()
event = EventBridge().wait_for_event(after_cursor=0, timeout_ms=150)
assert event is None
assert time.monotonic() - start >= 0.1
def test_wait_wakes_on_enqueue(self):
from mcp_serve import EventBridge, QueueEvent
b = EventBridge()
result = [None]
def waiter():
result[0] = b.wait_for_event(after_cursor=0, timeout_ms=5000)
t = threading.Thread(target=waiter)
t.start()
time.sleep(0.05)
b._enqueue(QueueEvent(cursor=0, type="message", session_key="wake"))
t.join(timeout=2)
assert result[0] is not None
assert result[0]["session_key"] == "wake"
def test_queue_limit(self):
from mcp_serve import EventBridge, QueueEvent, QUEUE_LIMIT
@ -389,6 +485,10 @@ class TestEventBridge:
assert result["resolved"] is True
assert len(b.list_pending_approvals()) == 0
def test_respond_nonexistent(self):
from mcp_serve import EventBridge
r = EventBridge().respond_to_approval("nope", "deny")
assert "error" in r
# ---------------------------------------------------------------------------
@ -434,6 +534,14 @@ class TestE2EConversationsList:
platforms = {c["platform"] for c in result["conversations"]}
assert platforms == {"telegram", "discord", "slack"}
def test_list_sorted_by_updated(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "conversations_list")
keys = [c["session_key"] for c in result["conversations"]]
# Telegram (14:30) > Discord (13:00) > Slack (11:00)
assert keys[0] == "agent:main:telegram:dm:123456"
assert keys[1] == "agent:main:discord:group:789:456"
assert keys[2] == "agent:main:slack:group:C1234:U5678"
def test_filter_by_platform(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
@ -441,8 +549,21 @@ class TestE2EConversationsList:
assert result["count"] == 1
assert result["conversations"][0]["platform"] == "discord"
def test_filter_by_platform_case_insensitive(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "conversations_list", {"platform": "TELEGRAM"})
assert result["count"] == 1
def test_search_by_name(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "conversations_list", {"search": "Alice"})
assert result["count"] == 1
assert result["conversations"][0]["display_name"] == "Alice"
def test_search_no_match(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "conversations_list", {"search": "nobody"})
assert result["count"] == 0
def test_limit(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
@ -479,7 +600,21 @@ class TestE2EMessagesRead:
assert "user" in roles
assert "assistant" in roles
def test_read_messages_content(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "messages_read",
{"session_key": "agent:main:telegram:dm:123456"})
contents = [m["content"] for m in result["messages"]]
assert "Hello Alice!" in contents
assert "Hi! How can I help?" in contents
def test_read_messages_have_ids(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "messages_read",
{"session_key": "agent:main:telegram:dm:123456"})
for msg in result["messages"]:
assert "id" in msg
assert msg["id"] # non-empty
def test_read_with_limit(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
@ -517,10 +652,29 @@ class TestE2EAttachmentsFetch:
assert result["attachments"][0]["type"] == "media"
assert result["attachments"][0]["path"] == "/tmp/screenshot.png"
def test_fetch_from_nonexistent_message(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "attachments_fetch", {
"session_key": "agent:main:telegram:dm:123456",
"message_id": "99999",
})
assert "error" in result
def test_fetch_from_nonexistent_session(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "attachments_fetch", {
"session_key": "nonexistent:key",
"message_id": "1",
})
assert "error" in result
class TestE2EEventsPoll:
def test_poll_empty(self, mcp_server_e2e, _event_loop):
server, bridge = mcp_server_e2e
result = _run_tool(server, "events_poll")
assert result["events"] == []
assert result["next_cursor"] == 0
def test_poll_with_events(self, mcp_server_e2e, _event_loop):
from mcp_serve import QueueEvent
@ -573,7 +727,24 @@ class TestE2EEventsWait:
assert result["event"] is None
assert result["reason"] == "timeout"
def test_wait_with_existing_event(self, mcp_server_e2e, _event_loop):
from mcp_serve import QueueEvent
server, bridge = mcp_server_e2e
bridge._enqueue(QueueEvent(cursor=0, type="message",
session_key="test",
data={"content": "waiting for this"}))
result = _run_tool(server, "events_wait", {"timeout_ms": 100})
assert result["event"] is not None
assert result["event"]["content"] == "waiting for this"
def test_wait_caps_timeout(self, mcp_server_e2e, _event_loop):
"""Timeout should be capped at 300000ms (5 min)."""
from mcp_serve import QueueEvent
server, bridge = mcp_server_e2e
bridge._enqueue(QueueEvent(cursor=0, type="message", session_key="t"))
# Even with huge timeout, should return immediately since event exists
result = _run_tool(server, "events_wait", {"timeout_ms": 999999})
assert result["event"] is not None
class TestMCPToolParameterCoercion:
def test_conversations_list_coerces_string_limit(self, fake_mcp_server, _event_loop):
@ -581,6 +752,14 @@ class TestMCPToolParameterCoercion:
result = _run_tool(server, "conversations_list", {"limit": "2"})
assert result["count"] == 2
def test_messages_read_coerces_string_limit(self, fake_mcp_server, _event_loop):
server, _ = fake_mcp_server
result = _run_tool(
server,
"messages_read",
{"session_key": "agent:main:telegram:dm:123456", "limit": "2"},
)
assert result["count"] == 2
def test_events_poll_coerces_string_cursor_and_limit(self, fake_mcp_server, _event_loop):
from mcp_serve import QueueEvent
@ -647,10 +826,54 @@ class TestE2EChannelsList:
assert result["count"] == 1
assert result["channels"][0]["target"] == "slack:C1234"
def test_channels_with_directory(self, mcp_server_e2e, _event_loop, monkeypatch):
"""Populated channel_directory.json should be unwrapped via the 'platforms' key.
Regression test for issue #21474: the writer wraps platforms under
{"updated_at": ..., "platforms": {...}} but the reader was iterating
directory.items() directly, so channels_list always returned 0.
"""
import mcp_serve
monkeypatch.setattr(mcp_serve, "_load_channel_directory", lambda: {
"updated_at": "2026-05-07T12:00:00",
"platforms": {
"telegram": [
{"id": "123456", "name": "Alice", "type": "dm"},
{"id": "-100999", "name": "Dev Group", "type": "group"},
],
"discord": [
{"id": "789", "name": "general", "type": "text"},
],
},
})
server, _ = mcp_server_e2e
result = _run_tool(server, "channels_list")
assert result["count"] == 3
targets = {c["target"] for c in result["channels"]}
assert targets == {"telegram:123456", "telegram:-100999", "discord:789"}
def test_channels_with_directory_platform_filter(self, mcp_server_e2e, _event_loop, monkeypatch):
"""Platform filter should work against the wrapped 'platforms' payload."""
import mcp_serve
monkeypatch.setattr(mcp_serve, "_load_channel_directory", lambda: {
"updated_at": "2026-05-07T12:00:00",
"platforms": {
"telegram": [{"id": "123456", "name": "Alice", "type": "dm"}],
"discord": [{"id": "789", "name": "general", "type": "text"}],
},
})
server, _ = mcp_server_e2e
result = _run_tool(server, "channels_list", {"platform": "discord"})
assert result["count"] == 1
assert result["channels"][0]["target"] == "discord:789"
class TestE2EPermissions:
def test_list_empty(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "permissions_list_open")
assert result["count"] == 0
assert result["approvals"] == []
def test_list_with_approvals(self, mcp_server_e2e, _event_loop):
server, bridge = mcp_server_e2e
@ -675,6 +898,12 @@ class TestE2EPermissions:
check = _run_tool(server, "permissions_list_open")
assert check["count"] == 0
def test_respond_deny(self, mcp_server_e2e, _event_loop):
server, bridge = mcp_server_e2e
bridge._pending_approvals["a2"] = {"id": "a2", "kind": "plugin"}
result = _run_tool(server, "permissions_respond",
{"id": "a2", "decision": "deny"})
assert result["resolved"] is True
def test_respond_invalid_decision(self, mcp_server_e2e, _event_loop):
server, bridge = mcp_server_e2e
@ -683,6 +912,11 @@ class TestE2EPermissions:
{"id": "a3", "decision": "maybe"})
assert "error" in result
def test_respond_nonexistent(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
result = _run_tool(server, "permissions_respond",
{"id": "nope", "decision": "deny"})
assert "error" in result
# ---------------------------------------------------------------------------
@ -703,6 +937,10 @@ class TestToolRegistration:
}
assert expected == tool_names, f"Missing: {expected - tool_names}, Extra: {tool_names - expected}"
def test_tools_have_descriptions(self, mcp_server_e2e, _event_loop):
server, _ = mcp_server_e2e
for tool in server._tool_manager.list_tools():
assert tool.description, f"Tool {tool.name} has no description"
# ---------------------------------------------------------------------------
@ -710,6 +948,11 @@ class TestToolRegistration:
# ---------------------------------------------------------------------------
class TestServerCreation:
def test_create_server(self, populated_sessions_dir, monkeypatch):
pytest.importorskip("mcp", reason="MCP SDK not installed")
import mcp_serve
monkeypatch.setattr(mcp_serve, "_get_sessions_dir", lambda: populated_sessions_dir)
assert mcp_serve.create_mcp_server() is not None
def test_create_with_bridge(self, populated_sessions_dir, monkeypatch):
pytest.importorskip("mcp", reason="MCP SDK not installed")
@ -777,6 +1020,11 @@ class TestCliIntegration:
# ---------------------------------------------------------------------------
class TestEdgeCases:
def test_empty_sessions_json(self, sessions_dir, monkeypatch):
(sessions_dir / "sessions.json").write_text("{}")
import mcp_serve
monkeypatch.setattr(mcp_serve, "_get_sessions_dir", lambda: sessions_dir)
assert mcp_serve._load_sessions_index() == {}
def test_sessions_without_origin(self, sessions_dir, monkeypatch):
data = {"agent:main:telegram:dm:111": {
@ -920,6 +1168,69 @@ class TestEventBridgePollE2E:
assert db.call_count == first_calls, \
"Second poll should skip DB queries when files unchanged"
def test_poll_detects_new_message_after_db_write(self, tmp_path, monkeypatch):
"""Write a new message to the DB after first poll, verify it's detected."""
import mcp_serve
sessions_dir = tmp_path / "sessions"
sessions_dir.mkdir()
monkeypatch.setattr(mcp_serve, "_get_sessions_dir", lambda: sessions_dir)
session_id = "20260329_150000_new_msg"
db_path = tmp_path / "state.db"
sessions_data = {
"agent:main:telegram:dm:new": {
"session_key": "agent:main:telegram:dm:new",
"session_id": session_id,
"platform": "telegram",
"updated_at": "2026-03-29T15:00:05",
"origin": {"platform": "telegram", "chat_id": "new"},
}
}
(sessions_dir / "sessions.json").write_text(json.dumps(sessions_data))
_create_test_db(db_path, session_id, [
{"role": "user", "content": "First", "timestamp": "2026-03-29T15:00:01"},
])
class TestDB:
def get_messages(self, sid):
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT * FROM messages WHERE session_id = ? ORDER BY id",
(sid,),
).fetchall()
conn.close()
return [dict(r) for r in rows]
db = TestDB()
bridge = mcp_serve.EventBridge()
# First poll
bridge._poll_once(db)
r1 = bridge.poll_events(after_cursor=0)
assert len(r1["events"]) == 1
# Add a new message to the DB
conn = sqlite3.connect(str(db_path))
conn.execute(
"INSERT INTO messages (session_id, role, content, timestamp) VALUES (?, ?, ?, ?)",
(session_id, "assistant", "New reply!", "2026-03-29T15:00:10"),
)
conn.commit()
conn.close()
# Touch the DB file to update mtime (WAL mode may not update mtime on small writes)
os.utime(db_path, None)
# Update sessions.json updated_at to trigger re-check
sessions_data["agent:main:telegram:dm:new"]["updated_at"] = "2026-03-29T15:00:10"
(sessions_dir / "sessions.json").write_text(json.dumps(sessions_data))
# Second poll — should detect the new message
bridge._poll_once(db)
r2 = bridge.poll_events(after_cursor=r1["next_cursor"])
assert len(r2["events"]) == 1
assert r2["events"][0]["content"] == "New reply!"
def test_poll_picks_up_new_conversation_on_db_change(
self, tmp_path, monkeypatch
@ -984,3 +1295,93 @@ class TestEventBridgePollE2E:
assert result["events"][0]["session_key"] == "agent:main:telegram:dm:late"
assert result["events"][0]["content"].startswith("Hello from a freshly")
def test_startup_baseline_suppresses_historical_replay(self, tmp_path, monkeypatch):
"""start()'s baseline records existing history without emitting it, so a
fresh EventBridge does not replay stored messages on startup; only
messages written after the baseline are delivered."""
import mcp_serve
db_path = tmp_path / "state.db"
db_path.write_text("placeholder")
session_id = "20260329_150000_history"
monkeypatch.setattr(
mcp_serve, "_load_sessions_index",
lambda: {
"agent:main:telegram:dm:hist": {
"session_id": session_id,
"platform": "telegram",
"origin": {"platform": "telegram", "chat_id": "hist"},
}
},
)
store = [{
"id": 1, "role": "user", "content": "pre-existing history",
"timestamp": "2026-03-29T15:00:00",
}]
class DB:
def get_messages(self, sid):
return list(store)
monkeypatch.setattr(mcp_serve, "_get_session_db", lambda: DB())
bridge = mcp_serve.EventBridge()
bridge._establish_baseline()
# Messages that existed before start() are not replayed.
assert bridge.poll_events(after_cursor=0)["events"] == []
# A message written after the baseline IS delivered on the next tick.
store.append({
"id": 2, "role": "assistant", "content": "arrived after start",
"timestamp": "2026-03-29T15:05:00",
})
os.utime(db_path, None) # bump mtime so the poll gate opens
bridge._poll_once(DB())
events = bridge.poll_events(after_cursor=0)["events"]
assert len(events) == 1
assert events[0]["content"] == "arrived after start"
def test_new_conversation_after_baseline_is_delivered(self, tmp_path, monkeypatch):
"""A conversation that first appears AFTER the startup baseline is still
delivered on its state.db-change tick — sessions absent from the
baseline default to last_seen=0.0."""
import mcp_serve
db_path = tmp_path / "state.db"
db_path.write_text("placeholder")
index: dict = {}
messages: dict = {}
monkeypatch.setattr(mcp_serve, "_load_sessions_index", lambda: dict(index))
class DB:
def get_messages(self, sid):
return list(messages.get(sid, []))
monkeypatch.setattr(mcp_serve, "_get_session_db", lambda: DB())
bridge = mcp_serve.EventBridge()
bridge._establish_baseline() # no conversations exist yet
# The gateway registers a brand-new conversation + its first message.
sid = "20260329_150000_fresh"
index["agent:main:telegram:dm:fresh"] = {
"session_id": sid,
"platform": "telegram",
"origin": {"platform": "telegram", "chat_id": "fresh"},
}
messages[sid] = [{
"id": 1, "role": "user", "content": "hello after baseline",
"timestamp": "2026-03-29T15:10:00",
}]
os.utime(db_path, None)
bridge._poll_once(DB())
events = bridge.poll_events(after_cursor=0)["events"]
assert len(events) == 1
assert events[0]["session_key"] == "agent:main:telegram:dm:fresh"
assert events[0]["content"] == "hello after baseline"
def test_poll_interval_is_200ms(self):
"""Verify the poll interval constant."""
from mcp_serve import POLL_INTERVAL
assert POLL_INTERVAL == 0.2

View File

@ -1,16 +1,9 @@
"""Regression tests for the terminal config → env fallback bridge.
"""Behavioral regressions for the terminal config → env bridge.
``terminal_tool._get_env_config()`` reads all settings from TERMINAL_* env
vars, which the CLI / gateway / TUI-PTY launchers bridge from config.yaml at
startup. Processes that skip every launcher bridge (``hermes serve`` and the
Desktop app's in-process agents, the desktop cron ticker, ACP) used to fall
back silently to the local backend even when config.yaml selected
``terminal.backend: docker`` — commands the user intended to sandbox ran on
the host (#63141 / #54449 / #61115 / #65696).
``_ensure_terminal_env_bridged()`` closes that hole at the chokepoint: when
TERMINAL_ENV is unset, backfill TERMINAL_* from config.yaml before the
local default applies. An explicitly-set TERMINAL_ENV always wins.
``terminal_tool._get_env_config()`` reads TERMINAL_* variables. The bridge
must let explicitly configured terminal keys override stale launcher/.env
values while preserving environment values for terminal keys omitted from
config.yaml.
"""
import os
@ -23,13 +16,15 @@ from hermes_constants import get_hermes_home
@pytest.fixture(autouse=True)
def _reset_bridge_state(monkeypatch):
"""Each test starts with an un-attempted bridge and no TERMINAL_ENV."""
"""Each test starts with an un-attempted bridge and clean mapped env."""
monkeypatch.setattr(terminal_tool, "_terminal_config_bridge_attempted", False)
monkeypatch.delenv("TERMINAL_ENV", raising=False)
monkeypatch.delenv("TERMINAL_CWD", raising=False)
monkeypatch.delenv("TERMINAL_DOCKER_IMAGE", raising=False)
# The config layer caches by (path, mtime, size); leave it alone — each
# test writes its own config.yaml which changes the signature.
for name in (
"TERMINAL_ENV",
"TERMINAL_CWD",
"TERMINAL_DOCKER_IMAGE",
"TERMINAL_SSH_HOST",
):
monkeypatch.delenv(name, raising=False)
yield
@ -40,8 +35,6 @@ def _write_config(text: str) -> None:
def test_unset_terminal_env_backfills_backend_from_config():
"""The core #63141 fix: config's docker backend reaches _get_env_config
even when no launcher bridged TERMINAL_ENV into this process."""
_write_config(
"terminal:\n"
" backend: docker\n"
@ -52,32 +45,76 @@ def test_unset_terminal_env_backfills_backend_from_config():
assert config["env_type"] == "docker"
assert config["docker_image"] == "custom/image:1"
assert os.environ.get("TERMINAL_ENV") == "docker"
assert os.environ["TERMINAL_ENV"] == "docker"
def test_explicit_terminal_env_wins_over_config(monkeypatch):
"""An explicit env choice (launcher bridge or .env) is never overridden —
honor explicit choice vs accidental fallback."""
def test_explicit_config_backend_overrides_stale_env(monkeypatch):
_write_config("terminal:\n backend: docker\n")
monkeypatch.setenv("TERMINAL_ENV", "local")
config = terminal_tool._get_env_config()
assert config["env_type"] == "docker"
assert os.environ["TERMINAL_ENV"] == "docker"
def test_partial_terminal_config_preserves_unrelated_env_values(monkeypatch):
_write_config("terminal:\n backend: docker\n")
monkeypatch.setenv("TERMINAL_ENV", "ssh")
monkeypatch.setenv("TERMINAL_DOCKER_IMAGE", "env/image:2")
config = terminal_tool._get_env_config()
assert config["env_type"] == "docker"
assert config["docker_image"] == "env/image:2"
assert os.environ["TERMINAL_DOCKER_IMAGE"] == "env/image:2"
def test_explicit_config_key_overrides_matching_env_value(monkeypatch):
_write_config(
"terminal:\n"
" backend: docker\n"
" docker_image: config/image:1\n"
)
monkeypatch.setenv("TERMINAL_ENV", "ssh")
monkeypatch.setenv("TERMINAL_DOCKER_IMAGE", "env/image:2")
config = terminal_tool._get_env_config()
assert config["env_type"] == "docker"
assert config["docker_image"] == "config/image:1"
def test_env_is_preserved_when_config_has_no_terminal_section(monkeypatch):
_write_config("agent:\n max_turns: 100\n")
monkeypatch.setenv("TERMINAL_ENV", "ssh")
monkeypatch.setenv("TERMINAL_SSH_HOST", "example.test")
config = terminal_tool._get_env_config()
assert config["env_type"] == "ssh"
assert config["ssh_host"] == "example.test"
def test_defaults_backfill_when_neither_config_nor_env_selects_backend():
_write_config("{}\n")
config = terminal_tool._get_env_config()
assert config["env_type"] == "local"
assert os.environ["TERMINAL_ENV"] == "local"
def test_bridge_only_attempted_once(monkeypatch):
"""The config load runs at most once per process when TERMINAL_ENV stays
unset (e.g. empty config) — later calls skip the bridge entirely."""
calls = []
import hermes_cli.config as config_mod
real = config_mod.apply_terminal_config_to_env
def _counting(*a, **k):
def _counting(*args, **kwargs):
calls.append(1)
return real(*a, **k)
return real(*args, **kwargs)
monkeypatch.setattr(config_mod, "apply_terminal_config_to_env", _counting)
_write_config("{}\n")
@ -86,3 +123,20 @@ def test_bridge_only_attempted_once(monkeypatch):
terminal_tool._get_env_config()
assert len(calls) == 1
def test_bridge_config_failure_does_not_crash(monkeypatch):
import hermes_cli.config as config_mod
monkeypatch.setattr(
config_mod,
"read_raw_config",
lambda: (_ for _ in ()).throw(RuntimeError("config read failed")),
)
monkeypatch.setenv("TERMINAL_ENV", "ssh")
monkeypatch.setenv("TERMINAL_SSH_HOST", "example.test")
config = terminal_tool._get_env_config()
assert config["env_type"] == "ssh"
assert config["ssh_host"] == "example.test"

View File

@ -36,3 +36,84 @@ class TestAppendModelSwitchMarkerRole:
_append_model_switch_marker(None, model="gpt-4o", provider="openai")
class TestModelSwitchMarkerDedup:
"""#65891: only the newest marker is meaningful; older ones must not
accumulate in the live history and burn context tokens every turn."""
@staticmethod
def _markers(session: dict) -> list:
from tui_gateway.server import _is_model_switch_marker
return [h for h in session["history"] if _is_model_switch_marker(h)]
def test_second_switch_replaces_first_marker(self) -> None:
session: dict = {"session_key": "s", "history": []}
_append_model_switch_marker(session, model="model-a", provider="p")
_append_model_switch_marker(session, model="model-b", provider="p")
markers = self._markers(session)
assert len(markers) == 1, "a second switch must replace, not stack, the marker"
assert "model-b" in markers[0]["content"]
assert "model-a" not in markers[0]["content"]
# The surviving marker is the last history entry.
assert session["history"][-1] is markers[0]
def test_five_switches_leave_one_marker(self) -> None:
# Mirrors the issue's screenshot: 5 consecutive MoA preset switches.
session: dict = {"session_key": "s", "history": []}
for name in ("质量-非高峰", "省钱-非高峰", "代码编程-非高峰", "日常对话-非高峰", "智能-高峰"):
_append_model_switch_marker(session, model=name, provider="moa")
markers = self._markers(session)
assert len(markers) == 1
assert "智能-高峰" in markers[0]["content"]
def test_dedup_preserves_real_conversation_turns(self) -> None:
session: dict = {
"session_key": "s",
"history": [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi"},
],
}
_append_model_switch_marker(session, model="model-a", provider="p")
_append_model_switch_marker(session, model="model-b", provider="p")
# Real turns untouched; exactly one marker, appended at the end.
assert session["history"][0] == {"role": "user", "content": "hello"}
assert session["history"][1] == {"role": "assistant", "content": "hi"}
assert len(self._markers(session)) == 1
assert len(session["history"]) == 3
def test_prior_marker_between_turns_is_removed(self) -> None:
# A stale marker not at the tail (a later turn followed it) is still
# stripped on the next switch.
session: dict = {
"session_key": "s",
"history": [
{"role": "user", "content": "q1"},
_make_marker_entry("model-a"),
{"role": "assistant", "content": "a1"},
],
}
_append_model_switch_marker(session, model="model-b", provider="p")
markers = self._markers(session)
assert len(markers) == 1
assert "model-b" in markers[0]["content"]
# The real turns are preserved in order.
assert [h["content"] for h in session["history"] if not _is_marker(h)] == ["q1", "a1"]
def test_history_version_increments_once_on_replace(self) -> None:
session: dict = {"session_key": "s", "history": [], "history_version": 0}
_append_model_switch_marker(session, model="model-a", provider="p")
_append_model_switch_marker(session, model="model-b", provider="p")
assert session["history_version"] == 2 # one increment per switch
def _make_marker_entry(model: str) -> dict:
from tui_gateway.server import _MODEL_SWITCH_MARKER_PREFIX
return {"role": "user", "content": f"{_MODEL_SWITCH_MARKER_PREFIX}{model}.]"}
def _is_marker(entry: dict) -> bool:
from tui_gateway.server import _is_model_switch_marker
return _is_model_switch_marker(entry)

View File

@ -1391,21 +1391,34 @@ def _ensure_terminal_env_bridged() -> None:
config.yaml selects ``terminal.backend: docker``, running commands on the
host the user intended to sandbox (#63141, #54449, #61115, #65696).
Explicit env always wins: when TERMINAL_ENV is already set (a launcher's
bridge or the user's .env made a deliberate choice) this is a no-op. The
config bridge only fills the unset case, so it changes an accidental
default — never an explicit selection.
Explicit terminal config keys win: when config.yaml has a ``terminal``
section, each key present there overrides its matching env value (which may
be stale from ``hermes setup``). Environment values for omitted terminal
keys are preserved. When no terminal section exists, exported/.env values
keep working unchanged.
"""
global _terminal_config_bridge_attempted
if "TERMINAL_ENV" in os.environ or _terminal_config_bridge_attempted:
if _terminal_config_bridge_attempted:
return
_terminal_config_bridge_attempted = True
try:
from hermes_cli.config import apply_terminal_config_to_env
from hermes_cli.config import apply_terminal_config_to_env, read_raw_config
# env=None targets os.environ inside the helper; override=False keeps
# any already-set TERMINAL_* values (e.g. from .env) authoritative.
apply_terminal_config_to_env(env=None, override=False)
# If config.yaml has an explicit terminal section, bridge with
# override enabled. The helper only overrides env vars for keys present
# in that raw section; merged defaults remain backfill-only. Without a
# terminal section, preserve an existing TERMINAL_ENV selection or
# backfill defaults when no selection exists.
raw_config = read_raw_config()
has_terminal_section = isinstance(raw_config.get("terminal"), dict)
if has_terminal_section:
# Explicit terminal keys in config.yaml win over matching env values.
apply_terminal_config_to_env(env=None, override=True)
elif "TERMINAL_ENV" not in os.environ:
# No terminal section in config.yaml, TERMINAL_ENV not set —
# backfill from config defaults
apply_terminal_config_to_env(env=None, override=False)
except Exception:
# Never let a config problem take the terminal tool down — the
# historical local default still applies.

View File

@ -3595,8 +3595,31 @@ def _persist_live_session_system_prompt(session: dict | None) -> None:
logger.debug("failed to persist live session system prompt", exc_info=True)
# Stable leading text of the model-switch marker, shared by the builder and the
# dedup below. Only the newest marker is meaningful (it names the *currently*
# active model); older ones are stale and would otherwise be re-sent to the
# provider on every turn (#65891).
_MODEL_SWITCH_MARKER_PREFIX = "[System: The active model for this chat has changed to "
def _is_model_switch_marker(entry: Any) -> bool:
"""Whether a history entry is a (self-replacing) model-switch marker."""
if not isinstance(entry, dict):
return False
content = entry.get("content")
return isinstance(content, str) and content.startswith(_MODEL_SWITCH_MARKER_PREFIX)
def _append_model_switch_marker(session: dict | None, *, model: str, provider: str) -> None:
"""Record a real system-history pivot after a live model switch."""
"""Record a real system-history pivot after a live model switch.
Only the most recent marker is kept: each new switch first strips any
prior model-switch markers from the live history, so N switches leave one
marker (naming the active model), not N stale ones accumulating tokens on
every subsequent API call (#65891). The in-memory history is the payload
re-sent each turn; the dedup is self-healing across resumes because the
next switch collapses whatever markers a reload brought back.
"""
if not session:
return
session_key = str(session.get("session_key") or "").strip()
@ -3605,7 +3628,7 @@ def _append_model_switch_marker(session: dict | None, *, model: str, provider: s
provider_part = f" via provider {provider}" if provider else ""
marker = (
"[System: The active model for this chat has changed to "
f"{_MODEL_SWITCH_MARKER_PREFIX}"
f"{model}{provider_part}. From this point forward, use this runtime "
"metadata when answering questions about what model/provider is active.]"
)
@ -3615,14 +3638,19 @@ def _append_model_switch_marker(session: dict | None, *, model: str, provider: s
# beginning of the API message list (#48338).
entry = {"role": "user", "content": marker, "display_kind": "model_switch"}
def _replace_markers() -> None:
history = session.setdefault("history", [])
# Drop any earlier markers in place before appending the new one.
history[:] = [h for h in history if not _is_model_switch_marker(h)]
history.append(entry)
session["history_version"] = int(session.get("history_version", 0)) + 1
lock = session.get("history_lock")
if lock is not None:
with lock:
session.setdefault("history", []).append(entry)
session["history_version"] = int(session.get("history_version", 0)) + 1
_replace_markers()
else:
session.setdefault("history", []).append(entry)
session["history_version"] = int(session.get("history_version", 0)) + 1
_replace_markers()
try:
agent = session.get("agent")