diff --git a/AGENTS.md b/AGENTS.md index b8a22cf7eb..93705624e3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1342,6 +1342,47 @@ while the agent is blocked (e.g. approval prompts) MUST bypass BOTH guards and be dispatched inline, not via `_process_message_background()` (which races session lifecycle). +### Streaming delivery contract (stream-is-the-message adapters) — duplicate-final class +Adapters with `draft_stream_is_message = True` (relay Slack native streaming) +keep ONE cumulative native stream per turn; the stream IS the final message. +Four invariants, each learned from a live duplicate-final incident (NS-658 +canary ledger, hermes#85796 / gateway-gateway#210). Violating any of them +re-creates a duplicate or a frozen stream: + +1. **Draft frames must be prefix-stable.** The connector computes append-only + deltas: frame N must be a string prefix of frame N+1. NEVER mutate draft + frames per-tick — no fence-closing (`ensure_closed_code_fences`), no cursor + suffix, no segment-state resets at tool boundaries, no mrkdwn conversion. + Any non-prefix frame triggers a whole-snapshot re-append on the platform + ("stacked copies"). The finalize path may still transform the real final. +2. **The consumer declares the final; the adapter never guesses.** + `finish(final_text)` carries the completed `final_response` (verifier + footer, completion explainer included) as the authoritative finalize + payload. New post-stream response augmentation MUST ride this payload — + if it mutates `final_response` after the stream sealed, it re-opens the + #11 bug (`delivered_final_matches` mismatch → corrective duplicate send). +3. **Interim sends must carry `_interim_send` metadata.** Any consumer-side + `adapter.send()` that is NOT the turn-final (commentary, segment-tail + flushes) must set `metadata["_interim_send"] = True`, or the relay + adapter's seal-interception will seal the live stream with interim text. + Seal-interception exists at BOTH egress doors (`send()` AND + `send_for_platform()`); a new egress door needs the same two checks. +4. **Reconcile by edit, never by plain send.** Any lane that delivers a final + beside an already-sealed stream (queued follow-ups, media-accompanied + finals, future lanes) must first try `edit_message` on the consumer's + `message_id`; plain `send()` is the fallback only when no editable message + exists. A sealed native stream is a regular message — `chat.update` on it + works (live-verified). + +Contract tests: `tests/gateway/test_stream_final_contract.py` (all four +invariants, mutation-checked). Slack streaming API ground truth (live-probed, +also encoded in connector comments/tests): `chat.*Stream` speaks STANDARD +markdown, not mrkdwn; `stopStream.markdown_text` APPENDS (never replaces); +`startStream`/`stopStream` are rate-limit Tier 2 (~20/min). + +Guard style note: check `draft_stream_is_message` with `is True` — MagicMock +adapters in older tests auto-create truthy attributes. + ### Squash merges from stale branches silently revert recent fixes Before squash-merging a PR, ensure the branch is up to date with `main` (`git fetch origin main && git reset --hard origin/main` in the worktree, diff --git a/CONTRIBUTING.es.md b/CONTRIBUTING.es.md index 78c80113c6..0ef0023da0 100644 --- a/CONTRIBUTING.es.md +++ b/CONTRIBUTING.es.md @@ -582,7 +582,7 @@ test(tools): añadir tests unitarios para file_operations ## Reportar Issues - Usa [GitHub Issues](https://github.com/NousResearch/hermes-agent/issues) -- Incluye: SO, versión de Python, versión de Hermes (`hermes version`), traza de error completa +- Incluye: SO, versión de Python, versión de Hermes (`hermes --version`), traza de error completa - Incluye pasos para reproducir - Verifica los issues existentes antes de crear duplicados - Para vulnerabilidades de seguridad, por favor reporta de forma privada diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bb157cc8f8..61bacafbd8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -973,7 +973,7 @@ test(tools): add unit tests for file_operations ## Reporting Issues - Use [GitHub Issues](https://github.com/NousResearch/hermes-agent/issues) -- Include: OS, Python version, Hermes version (`hermes version`), full error traceback +- Include: OS, Python version, Hermes version (`hermes --version`), full error traceback - Include steps to reproduce - Check existing issues before creating duplicates - For security vulnerabilities, please report privately diff --git a/SECURITY.es.md b/SECURITY.es.md index 086656d4f7..1328c11609 100644 --- a/SECURITY.es.md +++ b/SECURITY.es.md @@ -16,7 +16,7 @@ Un informe útil incluye: - Una descripción concisa y evaluación de severidad. - El componente afectado, identificado por ruta de archivo y rango de líneas (ej. `path/to/file.py:120-145`). -- Detalles del entorno (`hermes version`, SHA del commit, SO, versión de Python). +- Detalles del entorno (`hermes --version`, SHA del commit, SO, versión de Python). - Una reproducción contra `main` o el último release. - Una declaración de qué límite de confianza del §2 se cruza. diff --git a/SECURITY.md b/SECURITY.md index 7f5fd42db7..cea2a9a2e8 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -16,7 +16,7 @@ A useful report includes: - A concise description and severity assessment. - The affected component, identified by file path and line range (e.g. `path/to/file.py:120-145`). -- Environment details (`hermes version`, commit SHA, OS, Python +- Environment details (`hermes --version`, commit SHA, OS, Python version). - A reproduction against `main` or the latest release. - A statement of which trust boundary in §2 is crossed. diff --git a/agent/agent_init.py b/agent/agent_init.py index 20e44607bb..d8a5bd75e9 100644 --- a/agent/agent_init.py +++ b/agent/agent_init.py @@ -490,6 +490,25 @@ def _merge_custom_provider_extra_body(agent, custom_providers: List[Dict[str, An agent.request_overrides = overrides +def _normalize_run_budget_seconds(value) -> Optional[float]: + """Normalize a wall-clock run budget value to a positive float or None. + + None / absent / non-numeric / non-positive all resolve to ``None`` + (feature off) so a malformed config value can never activate the + deadline machinery, only leave it dormant. ``bool`` is rejected because + YAML ``true`` would otherwise become a 1-second budget. + """ + if value is None or isinstance(value, bool): + return None + try: + seconds = float(value) + except (TypeError, ValueError): + return None + if seconds != seconds or seconds <= 0: # NaN or non-positive + return None + return seconds + + def init_agent( agent, base_url: str = None, @@ -501,7 +520,7 @@ def init_agent( command: str = None, args: list[str] | None = None, model: str = "", - max_iterations: int = 90, # Default tool-calling iterations (shared with subagents) + max_iterations: int = sys.maxsize, # Default: unlimited tool-calling iterations (shared with subagents) enabled_toolsets: List[str] = None, disabled_toolsets: List[str] = None, save_trajectories: bool = False, @@ -527,6 +546,7 @@ def init_agent( clarify_callback: callable = None, read_terminal_callback: callable = None, read_preview_callback: callable = None, + drive_preview_callback: callable = None, read_window_below_callback: callable = None, setup_mcp_callback: callable = None, tour_callback: callable = None, @@ -560,6 +580,7 @@ def init_agent( session_db=None, parent_session_id: str = None, iteration_budget: "IterationBudget" = None, + run_budget_seconds: Optional[float] = None, fallback_model: Dict[str, Any] = None, credential_pool=None, checkpoints_enabled: bool = False, @@ -823,6 +844,7 @@ def init_agent( agent.clarify_callback = clarify_callback agent.read_terminal_callback = read_terminal_callback agent.read_preview_callback = read_preview_callback + agent.drive_preview_callback = drive_preview_callback agent.read_window_below_callback = read_window_below_callback agent.setup_mcp_callback = setup_mcp_callback agent.tour_callback = tour_callback @@ -972,6 +994,17 @@ def init_agent( agent._budget_exhausted_injected = False agent._budget_grace_call = False + # Optional wall-clock run budget (seconds per run_conversation turn). + # Explicit constructor arg wins; else resolved from config.yaml + # (agent.run_budget_seconds) further below. None = feature fully off: + # no clock reads, no injection, no stale-timeout capping. + agent.run_budget_seconds = _normalize_run_budget_seconds(run_budget_seconds) + # Wall-clock start of the CURRENT run_conversation turn. Set by + # turn_context.prepare_turn when a run budget is active; None otherwise. + agent._run_budget_started_at = None + # One-shot latch for the 80% wrap-up notice (reset each turn). + agent._run_budget_wrapup_injected = False + # Activity tracking — updated on each API call, tool execution, and # stream chunk. Used by the gateway timeout handler to report what the # agent was doing when it was killed, and by the "still working" @@ -1896,6 +1929,20 @@ def init_agent( _agent_section = {} agent._tool_use_enforcement = _agent_section.get("tool_use_enforcement", "auto") + # Execution-discipline guidance gate: "auto" (default — matches + # EXECUTION_GUIDANCE_MODELS), true (always), false (never), or list of + # model-name substrings. Independent of tool_use_enforcement — see + # agent/system_prompt.py for the injection gate. + agent._execution_guidance = _agent_section.get("execution_guidance", "auto") + + # Wall-clock run budget from config (agent.run_budget_seconds) — only + # consulted when the constructor arg was not given. Absent/None/invalid + # keeps the feature fully off (zero behavior change in the default path). + if agent.run_budget_seconds is None: + agent.run_budget_seconds = _normalize_run_budget_seconds( + _agent_section.get("run_budget_seconds") + ) + # Empty-response retry guard config (NS-503): additive # ``agent.empty_response_guard`` subsection. Resolution is tolerant — # a malformed section falls back to the schema defaults (guard on, @@ -1912,6 +1959,11 @@ def init_agent( # conversation loop's intent-ack block. agent._intent_ack_continuation = _agent_section.get("intent_ack_continuation", "auto") + # Runtime anti-stall guards (identical-call loop-breaker notice on tool + # results + continue-intent extension of the empty-response recovery). + # Single boolean gate, default True. Notice-only — never blocks a call. + agent._stall_guards = bool(_agent_section.get("stall_guards", True)) + # Universal task-completion guidance toggle. Default True. Surfaced # as a separate flag from tool_use_enforcement because the guidance # applies to ALL models, not just the model families enforcement diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py index dbbb0d11e9..2744c1b72d 100644 --- a/agent/agent_runtime_helpers.py +++ b/agent/agent_runtime_helpers.py @@ -99,7 +99,7 @@ def _ra(): AGENT_RUNTIME_POST_HOOK_TOOL_NAMES = frozenset( - {"todo", "session_search", "memory", "clarify", "read_terminal", "read_preview", "read_window_below", "setup_mcp", "tour", "delegate_task"} + {"todo", "session_search", "memory", "clarify", "read_terminal", "read_preview", "drive_preview", "annotate_preview", "read_window_below", "setup_mcp", "tour", "delegate_task"} ) @@ -3234,6 +3234,37 @@ def invoke_tool(agent, function_name: str, function_args: dict, effective_task_i ), next_args, ) + elif function_name == "drive_preview": + def _execute(next_args: dict) -> Any: + from tools.drive_preview_tool import drive_preview_tool as _drive_preview_tool + return _finish_agent_tool( + _drive_preview_tool( + action=next_args.get("action", ""), + ref=next_args.get("ref"), + selector=next_args.get("selector"), + text=next_args.get("text"), + key=next_args.get("key"), + submit=next_args.get("submit"), + amount=next_args.get("amount"), + to=next_args.get("to"), + limit=next_args.get("max"), + callback=getattr(agent, "drive_preview_callback", None), + ), + next_args, + ) + elif function_name == "annotate_preview": + def _execute(next_args: dict) -> Any: + from tools.annotate_preview_tool import annotate_preview_tool as _annotate_preview_tool + return _finish_agent_tool( + _annotate_preview_tool( + action=next_args.get("action", "add"), + ref=next_args.get("ref"), + selector=next_args.get("selector"), + label=next_args.get("label"), + callback=getattr(agent, "drive_preview_callback", None), + ), + next_args, + ) elif function_name == "read_window_below": def _execute(next_args: dict) -> Any: from tools.read_window_tool import read_window_below_tool as _read_window_below_tool @@ -3863,6 +3894,37 @@ def looks_like_codex_intermediate_ack( return user_targets_workspace or assistant_targets_workspace +# Conservative "trailing continue-intent" detector for the said-continue-but- +# stopped stall guard (agent.stall_guards). Matches only when the message TAIL +# announces an immediate next action ("Let me now…", "I will now…", +# "Next, I…"), which is the observed stall shape: the model narrates the next +# step and then ends the turn with no tool call. Kept deliberately narrow so +# ordinary answers that merely contain "I will" mid-sentence never trip it. +_TRAILING_CONTINUE_INTENT_RE = re.compile( + r"(?:\blet me now\b|\bi(?:['\u2019])?ll now\b|\bi will now\b" + r"|\bnow i(?:['\u2019]ll| will)\b|\bnext[,:] i\b)" + r"[^.!?\n]{0,100}[.:\u2026]?\s*$", + re.IGNORECASE, +) + +# Content longer than this is a substantive reply, not a dangling ack. +_TRAILING_CONTINUE_INTENT_MAX_CHARS = 400 + + +def trailing_continue_intent(text: str) -> bool: + """Whether ``text`` is a short reply ENDING on an announced next action. + + Used by the stall-guard extension of the intent-ack continuation path in + ``agent.conversation_loop``: when a turn is about to end with this shape + (no tool calls, short content, trailing intent), the loop re-prompts via + the existing bounded continuation mechanism instead of stopping. + """ + t = (text or "").strip() + if not t or len(t) > _TRAILING_CONTINUE_INTENT_MAX_CHARS: + return False + return bool(_TRAILING_CONTINUE_INTENT_RE.search(t[-160:])) + + def intent_ack_continuation_mode(agent) -> str: """Classify the resolved intent-ack continuation mode for this turn. diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py index ca6bea2d26..b4796b2da6 100644 --- a/agent/auxiliary_client.py +++ b/agent/auxiliary_client.py @@ -1551,10 +1551,16 @@ class _CodexCompletionsAdapter: # Codex backend, which rejects e.g. {"effort": null} # with a 400. effort = reasoning_cfg.get("effort") or "medium" - # Codex backend rejects "minimal"; clamp to "low" to - # match the main-agent Codex transport behavior. - if effort == "minimal": - effort = "low" + # Same declared vocabulary + shared clamp as the main + # Codex transport (agent.reasoning_effort): per-model — + # "max" is gpt-5.6-only, "minimal"/"ultra" always + # rejected (live-verified, #68365). + from agent.reasoning_effort import ( + clamp_effort, + codex_supported_efforts, + ) + + effort = clamp_effort(effort, codex_supported_efforts(model)) resp_kwargs["reasoning"] = { "effort": effort, "summary": "auto", diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py index f01d042b53..6c8a811dd4 100644 --- a/agent/codex_runtime.py +++ b/agent/codex_runtime.py @@ -1305,6 +1305,62 @@ def _consume_codex_event_stream( return final +def _sanitize_consumer_codex_request( + agent: Any, + request: dict[str, Any], +) -> dict[str, Any]: + """Drop fields the ChatGPT OAuth Codex endpoint does not accept. + + This guard intentionally lives at the final wire boundary, after Relay or + other request middleware has had a chance to transform the request. The + normal transport builder already omits ``prompt_cache_retention`` for this + endpoint, but a late mutation must not be allowed to turn a valid tool + follow-up into a non-retryable HTTP 400. + + Explicit ``request_overrides`` are subject to the same endpoint contract: + unsupported retention is dropped with a warning instead of being sent and + rejected by the provider. The check covers both the top-level kwarg and a + nested ``extra_body`` entry — the OpenAI SDK merges ``extra_body`` into + the outgoing JSON body, so either shape reaches the endpoint. + """ + sanitized = dict(request) + # Resolved defensively on purpose: run_codex_stream is also driven with + # lightweight stand-in agents that carry only the attributes a given path + # needs (see tests/agent/test_codex_request_transport_diagnostics.py), so a + # bare agent._is_codex_backend() here would raise AttributeError on them. + backend_predicate = getattr(agent, "_is_codex_backend", None) + is_consumer_codex = ( + bool(backend_predicate()) if callable(backend_predicate) else False + ) + if not is_consumer_codex: + return sanitized + dropped_from: list[str] = [] + if "prompt_cache_retention" in sanitized: + sanitized.pop("prompt_cache_retention") + dropped_from.append("top-level") + # The OpenAI SDK merges ``extra_body`` into the outgoing JSON body, so a + # nested ``extra_body.prompt_cache_retention`` reaches the endpoint just + # like the top-level field would. Copy before editing — the caller's + # mapping must not be mutated — and drop the mapping when it empties. + extra_body = sanitized.get("extra_body") + if isinstance(extra_body, dict) and "prompt_cache_retention" in extra_body: + extra_body = dict(extra_body) + extra_body.pop("prompt_cache_retention") + if extra_body: + sanitized["extra_body"] = extra_body + else: + sanitized.pop("extra_body") + dropped_from.append("extra_body") + if dropped_from: + logger.warning( + "Dropped unsupported prompt_cache_retention at consumer Codex " + "wire boundary (model=%s, via %s).", + sanitized.get("model", getattr(agent, "model", "unknown")), + ", ".join(dropped_from), + ) + return sanitized + + def run_codex_stream(agent, api_kwargs: dict, client: Any = None, on_first_delta=None): """Execute one streaming Responses API request and return the final response. @@ -1347,7 +1403,10 @@ def run_codex_stream(agent, api_kwargs: dict, client: Any = None, on_first_delta writer_token = {"value": None} def _open_codex_stream(next_api_kwargs: dict[str, Any]): - stream_kwargs = dict(next_api_kwargs) + stream_kwargs = _sanitize_consumer_codex_request( + agent, + next_api_kwargs, + ) stream_kwargs["stream"] = True return active_client.responses.create(**stream_kwargs) diff --git a/agent/context_compressor.py b/agent/context_compressor.py index d66bee1cad..4c48794fb2 100644 --- a/agent/context_compressor.py +++ b/agent/context_compressor.py @@ -350,6 +350,147 @@ _SUMMARY_END_MARKER = ( _MERGED_PRIOR_CONTEXT_HEADER = "[PRIOR CONTEXT — for reference only; not a new message]" _MERGED_SUMMARY_DELIMITER = "[END OF PRIOR CONTEXT — COMPACTION SUMMARY BELOW]" +_SALVAGE_SUMMARY_MAX_CHARS = 8_000 +_SALVAGE_KEEP_RECENT_TOOLS = 2 + + +def _looks_like_compaction_summary(msg: Dict[str, Any], content: str) -> bool: + # Only cap a standalone handoff. Merged carriers preserve a real tail ask + # in the same content string; truncating those could delete live user text. + if not content.rstrip().endswith(_SUMMARY_END_MARKER): + return False + if content.startswith(_MERGED_PRIOR_CONTEXT_HEADER): + return False + # Content heuristics alone must never authorize mutating a live turn. + # Compressor-generated summaries carry this private marker; ordinary + # user input — and live assistant replies or kept tool bodies that + # merely quote a summary header/marker — do not. Tool messages are + # handled exclusively by the stub/keep-recent pass, never the cap. + if msg.get("role") == "tool": + return False + if ( + msg.get("role") in ("user", "assistant") + and not msg.get(COMPRESSED_SUMMARY_METADATA_KEY) + ): + return False + head = content[:280] + return ( + bool(msg.get(COMPRESSED_SUMMARY_METADATA_KEY)) + or "CONTEXT COMPACTION" in head + or "[CONTEXT COMPACTION]" in head + or "Conversation Summary" in head + ) + + +def _salvage_reduce_todo_snapshot(out: List[Dict[str, Any]]) -> None: + """Last-resort shrink: reduce or drop the synthetic todo snapshot. + + The snapshot is the only in-transcript todo re-injection at a compaction + boundary, and since 7a16840add the pruned-skill reload notice is coupled + into the same string — so it is only touched when the cheaper shrink ops + could not get under budget. When the snapshot carries a reload notice, + keep just the notice (the coupling must survive salvage); otherwise drop + the row entirely. + """ + from agent.conversation_compression import _PRUNED_SKILL_RELOAD_NOTICE_HEADER + + for i in range(len(out) - 1, -1, -1): + msg = out[i] + if not isinstance(msg, dict): + continue + if msg.get("_todo_snapshot_synthetic") and msg.get("role") == "user": + content = msg.get("content") + notice_idx = ( + content.find(_PRUNED_SKILL_RELOAD_NOTICE_HEADER) + if isinstance(content, str) + else -1 + ) + if isinstance(content, str) and notice_idx >= 0: + msg["content"] = content[notice_idx:] + else: + del out[i] + return + + +def salvage_grown_transcript( + original: List[Dict[str, Any]], + candidate: List[Dict[str, Any]], + budget: Optional[int] = None, +) -> Optional[List[Dict[str, Any]]]: + """Mechanically shrink a compression candidate, or return ``None``. + + Already-compacted middles can be summarized slightly larger while retained + tool bodies, stale reasoning, or a synthetic todo snapshot tip the final + candidate over the input size. Work on copies and admit the salvage only + when the same rough estimator proves it is strictly smaller than the input. + + Shrink order is cheapest-information-loss first: stale reasoning keys and + codex replay sidecars, then old tool bodies, then an oversized summary cap. + The synthetic todo snapshot (which carries the pruned-skill reload notice, + see ``_salvage_reduce_todo_snapshot``) is only reduced as a LAST resort + when everything else still leaves the candidate at or over budget. + """ + if not candidate or not original: + return None + if budget is None: + budget = estimate_messages_tokens_rough(original) + if budget <= 0: + return None + + out: List[Dict[str, Any]] = [] + tool_indices: List[int] = [] + last_assistant_idx = -1 + for msg in candidate: + if not isinstance(msg, dict): + out.append(msg) + continue + copied = dict(msg) + out.append(copied) + role = copied.get("role") + if role == "tool": + tool_indices.append(len(out) - 1) + elif role == "assistant": + last_assistant_idx = len(out) - 1 + + salvage_reasoning_keys = _NEWEST_TURN_ONLY_BUDGET_KEYS + ("reasoning_details",) + keep_tools = set(tool_indices[-_SALVAGE_KEEP_RECENT_TOOLS:]) + for index, msg in enumerate(out): + if not isinstance(msg, dict): + continue + if msg.get("role") == "assistant" and index != last_assistant_idx: + for key in salvage_reasoning_keys: + msg.pop(key, None) + if msg.get("role") == "tool" and index not in keep_tools: + content = msg.get("content") + if isinstance(content, str) and len(content) > _PRUNE_MIN_CHARS: + msg["content"] = _PRUNED_TOOL_PLACEHOLDER + content = msg.get("content") + if ( + isinstance(content, str) + and len(content) > _SALVAGE_SUMMARY_MAX_CHARS + and _looks_like_compaction_summary(msg, content) + ): + msg["content"] = ( + content[:_SALVAGE_SUMMARY_MAX_CHARS].rstrip() + + "\n…[summary truncated so compaction can shrink]\n\n" + + _SUMMARY_END_MARKER + ) + # Heavier codex replay sidecars (encrypted reasoning blobs) — reuse the + # proven prune with its last-user-turn safety boundary (#71058). + _prune_stale_reasoning_replay(out) + + if estimate_messages_tokens_rough(out) >= budget: + _salvage_reduce_todo_snapshot(out) + + if not any( + isinstance(message, dict) and message.get("role") == "user" + for message in out + ): + return None + if estimate_messages_tokens_rough(out) < budget: + return out + return None + # Handoff prefixes that shipped in earlier releases. A summary persisted under # one of these can be inherited into a resumed lineage (#35344); when it is # re-normalized on re-compaction we must strip the OLD prefix too, otherwise the @@ -1885,6 +2026,7 @@ class ContextCompressor(ContextEngine): self._cooldown_persist_failed = False self._last_summary_error = None self._last_compress_aborted = False + self._last_compress_refused_would_grow = False self.last_real_prompt_tokens = 0 self.last_compression_rough_tokens = 0 self.last_rough_tokens_when_real_prompt_fit = 0 @@ -2167,6 +2309,7 @@ class ContextCompressor(ContextEngine): self._summary_failure_cooldown_until = 0.0 self._cooldown_persist_failed = False self._last_compress_aborted = False + self._last_compress_refused_would_grow = False self._context_probed = False self._context_probe_persistable = False self.last_real_prompt_tokens = 0 @@ -2371,6 +2514,31 @@ class ContextCompressor(ContextEngine): self._ineffective_compression_count = count self._persist_ineffective_compression_count() + def record_rejected_compaction(self) -> None: + """Record one compaction whose result was REJECTED before committing. + + The anti-growth guard in the commit layer (conversation_compression) + discards a candidate that would grow the transcript and keeps the + original. Without recording the attempt, the anti-thrash breaker + never sees a strike, so automatic compression retries the SAME + unchanged transcript on every turn — same summary request, same + refusal, same user-facing warning (#88568). This counts one + ineffective strike (persisted, so the normal >= 2 latch and its + recovery window apply) WITHOUT arming post-compaction real-usage + verification — nothing was committed, so there is no new compaction + to verify — and without touching the fallback-summary streak (no + summary was accepted). + """ + self._record_ineffective_compression_verdict( + self._ineffective_compression_count + 1 + ) + if not self.quiet_mode: + logger.warning( + "Compaction rejected before commit (would grow the " + "transcript); ineffective_compression_count=%d", + self._ineffective_compression_count, + ) + def record_completed_compaction( self, *, used_fallback: bool = False, feasibility_skip: bool = False, ) -> None: @@ -6927,6 +7095,7 @@ This compaction should PRIORITISE preserving all information related to the focu self._last_aux_model_failure_error = None self._last_aux_model_failure_model = None self._last_compress_aborted = False + self._last_compress_refused_would_grow = False self._last_compression_made_progress = False # NOTE: do NOT reset _last_summary_auth_failure or # _last_summary_network_failure here. These flags are set by diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py index 97a9e3dbf2..a2b2643bf3 100644 --- a/agent/conversation_compression.py +++ b/agent/conversation_compression.py @@ -3386,6 +3386,27 @@ def compress_context( # transcript stays untouched and durable. _rough_in = estimate_messages_tokens_rough(messages) _rough_out = estimate_messages_tokens_rough(compressed) + if _rough_out > _rough_in: + # Todo refresh and user-turn anchoring happen after the + # compressor's own size check, so they can tip a break-even + # candidate over. Give it one mechanical salvage pass. + from agent.context_compressor import salvage_grown_transcript + + _salvaged = salvage_grown_transcript( + messages, compressed, budget=_rough_in + ) + if _salvaged is not None: + _salv_est = estimate_messages_tokens_rough(_salvaged) + if _salv_est < _rough_in: + logger.info( + "Compression salvage recovered a shrinking " + "transcript (session=%s, ~%s -> ~%s tokens)", + agent.session_id or "none", + f"{_rough_in:,}", + f"{_salv_est:,}", + ) + compressed = _salvaged + _rough_out = _salv_est if _rough_out > _rough_in: logger.warning( "Compression refused: compressed transcript would be " @@ -3395,6 +3416,17 @@ def compress_context( f"{_rough_in:,}", f"{_rough_out:,}", ) + # Flag the refusal on the compressor state so manual + # /compress feedback can report it honestly. Without this, + # the CLI compared the returned list against its pre-call + # snapshot, saw a difference (durable-snapshot adoption can + # legitimately change the count), and printed + # "✅ Compressed: 8 → 14 messages" directly under the + # refusal warning (Aug 2026 full-surface CLI QA sweep). + try: + agent.context_compressor._last_compress_refused_would_grow = True + except Exception: + pass try: agent._emit_warning( "⚠️ Compression refused: the generated summary " @@ -3414,6 +3446,20 @@ def compress_context( split_status="aborted", failure_class="would_grow", ) + # Record the rejected attempt as an ineffective + # compaction strike so the anti-thrash breaker latches + # after the normal threshold. Without this, the unchanged + # transcript stays over the compression threshold and + # automatic compression retries the identical summary + # request on every turn (#88568). Manual /compress keeps + # bypassing the latch (force=True skips the guards). + try: + agent.context_compressor.record_rejected_compaction() + except Exception: + logger.debug( + "could not record rejected-compaction strike", + exc_info=True, + ) _release_lock() return messages, _existing_sp diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py index ab8e60b6d0..951b874701 100644 --- a/agent/conversation_loop.py +++ b/agent/conversation_loop.py @@ -107,6 +107,64 @@ logger = logging.getLogger(__name__) _INTERRUPT_SCAFFOLD_MARKER = "[This response was interrupted by a user correction.]" +# One-time wrap-up notice appended when a wall-clock run budget crosses its +# 80% threshold (agent.run_budget_seconds / --run-budget). Mirrors the Codex +# CLI budget wrap-up template: stop new work, deliver from current state. +RUN_BUDGET_WRAPUP_NOTICE = ( + "[SYSTEM NOTICE — run time budget nearly exhausted] " + "Run time budget nearly exhausted. Stop new discovery/verification work " + "now. Produce the required final deliverable (answer/JSON/summary) from " + "the state you already have, completing only mandatory writes." +) + + +def _maybe_inject_run_budget_wrapup(agent: Any, messages: List[Dict[str, Any]]) -> bool: + """Inject the one-time wall-clock wrap-up notice when past 80% of budget. + + Cache-safe delivery: the notice is appended to the NEWEST ``role:"tool"`` + message (the same channel /steer uses) — no synthetic user message is + inserted mid-loop and no past context is rewritten, so role alternation + and the prompt-cache prefix survive. Latches ``_run_budget_wrapup_injected`` + only on a successful append, so a first iteration without tool results + retries on the next iteration. Returns True when the notice was injected. + + Dormant unless ``agent.run_budget_seconds`` is set AND the turn stamped + ``_run_budget_started_at`` (see ``turn_context.prepare_conversation_turn``). + """ + budget = getattr(agent, "run_budget_seconds", None) + if not budget: + return False + if getattr(agent, "_run_budget_wrapup_injected", False): + return False + started = getattr(agent, "_run_budget_started_at", None) + if not started: + return False + if (time.time() - started) < 0.8 * float(budget): + return False + for i in range(len(messages) - 1, -1, -1): + msg = messages[i] + if isinstance(msg, dict) and msg.get("role") == "tool": + existing = msg.get("content", "") + if isinstance(existing, str): + msg["content"] = existing + f"\n\n{RUN_BUDGET_WRAPUP_NOTICE}" + else: + # Multimodal content blocks — append a text block. + try: + blocks = list(existing) if existing else [] + blocks.append({"type": "text", "text": RUN_BUDGET_WRAPUP_NOTICE}) + msg["content"] = blocks + except Exception: + return False + agent._run_budget_wrapup_injected = True + logger.info( + "Run budget wrap-up notice injected (budget=%.0fs, elapsed=%.0fs)", + float(budget), + time.time() - started, + ) + return True + return False + + def _restore_user_after_reference_handoff( messages: List[Dict[str, Any]], user_message: Any ) -> bool: @@ -2041,6 +2099,15 @@ def run_conversation( existing = getattr(agent, "_pending_steer", None) agent._pending_steer = (existing + "\n" + _pre_api_steer) if existing else _pre_api_steer + # ── Wall-clock run-budget wrap-up notice ─────────────────────── + # One-shot: when a run budget (agent.run_budget_seconds / + # --run-budget) is active and 80% of it has elapsed, ask the model + # to wrap up and deliver from the state it already has. Same + # cache-safe channel as /steer (appended to the newest tool + # result); dormant when no budget is set. + if getattr(agent, "run_budget_seconds", None): + _maybe_inject_run_budget_wrapup(agent, messages) + # Prepare messages for API call # If we have an ephemeral system prompt, prepend it to the messages # Note: Reasoning is embedded in content via tags for trajectory storage. @@ -2698,7 +2765,34 @@ def run_conversation( request_pressure_tokens, int(getattr(_compressor, "threshold_tokens", 0) or 0), ) - + elif not agent.compression_enabled and len(messages) > 1: + # Uncompressed session guard (#89297): compression is disabled, so + # nothing shrinks a growing session. Reuse the unconditionally + # computed request estimate (zero marginal cost — this site runs + # before every provider request, covering turn-start AND mid-turn + # tool-result growth) and surface a deduped, actionable warning + # when the request exceeds the model context window. The dedup is + # re-armed by the turn-context preflight once the session is back + # under the window (manual /compress works with compression + # disabled), so the guard warns again on a later re-overflow. + # context_compressor always exists (agent_init constructs it even + # when compression is disabled) and its context_length property + # hard-floors at a positive default — no metadata re-resolution + # needed here. + _ctx_len = getattr( + getattr(agent, "context_compressor", None), "context_length", None + ) + if ( + isinstance(_ctx_len, int) + and _ctx_len > 0 + and request_pressure_tokens > _ctx_len + ): + _warn_fn = getattr( + agent, "_warn_uncompressed_context_overflow", None + ) + if callable(_warn_fn): + _warn_fn(request_pressure_tokens, _ctx_len) + # Thinking spinner for quiet mode (animated during API call) thinking_spinner = None @@ -5237,6 +5331,21 @@ def run_conversation( FailoverReason.billing, FailoverReason.upstream_rate_limit, } + # Relay-wrapped output-cap errors: some gateways wrap an + # upstream "[400]: max_tokens (...) exceeds model's maximum + # output tokens (...)" as HTTP 429, which classifies as + # rate_limit. The failure is a deterministic request-shape + # problem — falling back to another provider (or burning + # generic retries) can't fix it, but the output-cap clamp + # below can, in one retry (#72281). Parse once here; the + # result gates both the eager-fallback exemption and the + # widened is_context_length_error entry, and is reused as + # available_out inside the handler. + _wrapped_output_cap_budget = ( + parse_available_output_tokens_from_error(error_msg) + if classified.reason == FailoverReason.rate_limit + else None + ) _is_transport_failure = classified.reason in { FailoverReason.timeout, FailoverReason.overloaded, @@ -5254,7 +5363,7 @@ def run_conversation( if _is_zai_coding_overload: max_retries = max(max_retries, zai_coding_overload_retry_ceiling()) _should_fallback = ( - is_rate_limited + (is_rate_limited and _wrapped_output_cap_budget is None) or (_is_transport_failure and retry_count >= 2) ) if _should_fallback and agent._fallback_index < len(agent._fallback_chain): @@ -5547,6 +5656,11 @@ def run_conversation( # server disconnect + large session pattern (#2153). is_context_length_error = ( classified.reason == FailoverReason.context_overflow + # Relay-wrapped output-cap 429s (parsed once above, where + # the eager-fallback exemption is gated) route into the + # output-cap clamp below instead of provider failover or + # generic retries (#72281). + or _wrapped_output_cap_budget is not None ) if is_context_length_error: @@ -7864,10 +7978,28 @@ def run_conversation( from agent.agent_runtime_helpers import ( intent_ack_continuation_mode, + trailing_continue_intent, ) _ack_mode = intent_ack_continuation_mode(agent) - if ( + # Said-continue-but-stopped guard (agent.stall_guards): the + # model ended the turn with no tool calls but its short reply + # TAILS with an announced next action ("Let me now…", + # "I will now…"). Unlike the intent-ack detector below, this + # fires mid-task too (after tool results), which is exactly + # where eval traces show the stall. It reuses the SAME bounded + # continuation path and counter (max 2 per turn), so the + # alternation-safe interim-assistant + user-nudge mechanism — + # not a new parallel one — carries the recovery. + _stall_continue_intent = ( + bool(getattr(agent, "_stall_guards", True)) + and agent.valid_tool_names + and codex_ack_continuations < 2 + and trailing_continue_intent( + agent._strip_think_blocks(final_response or "") + ) + ) + if _stall_continue_intent or ( _ack_mode != "off" and agent.valid_tool_names and codex_ack_continuations < 2 @@ -7878,6 +8010,12 @@ def run_conversation( require_workspace=(_ack_mode == "codex_only"), ) ): + if _stall_continue_intent: + logger.info( + "Stall guard: turn ending on trailing continue-" + "intent with no tool calls — re-prompting to act " + "(%d/2)", codex_ack_continuations + 1, + ) codex_ack_continuations += 1 interim_msg = agent._build_assistant_message(assistant_message, "incomplete") append_message(messages, interim_msg) diff --git a/agent/image_gen_registry.py b/agent/image_gen_registry.py index 82393e88db..6239bbe891 100644 --- a/agent/image_gen_registry.py +++ b/agent/image_gen_registry.py @@ -139,6 +139,18 @@ def get_active_provider() -> Optional[ImageGenProvider]: except Exception as exc: logger.debug("Could not read image_gen.provider from config: %s", exc) + # The managed "Nous Subscription" selection is serviced by the FAL + # plugin through the managed fal-queue gateway (the legacy FAL pipeline + # routes managed when the stored selection is "nous"). + if configured: + try: + from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER + + if configured.lower() == NOUS_MANAGED_PROVIDER: + configured = "fal" + except Exception: # pragma: no cover — helpers are in-repo + pass + with _lock: snapshot = dict(_providers) snapshot.update(_scoped_providers.get(hermes_home_key(), {})) diff --git a/agent/manual_compression_feedback.py b/agent/manual_compression_feedback.py index b2e12d6834..b37361e6e2 100644 --- a/agent/manual_compression_feedback.py +++ b/agent/manual_compression_feedback.py @@ -53,6 +53,11 @@ def summarize_manual_compression( compression_state is not None and getattr(compression_state, "_last_compress_aborted", False) is True ) + refused_would_grow = ( + compression_state is not None + and getattr(compression_state, "_last_compress_refused_would_grow", False) + is True + ) fallback_used = ( compression_state is not None and getattr(compression_state, "_last_summary_fallback_used", False) is True @@ -65,7 +70,12 @@ def summarize_manual_compression( if not isinstance(failure_reason, str) or not failure_reason.strip(): failure_reason = None - if aborted: + if refused_would_grow: + headline = ( + f"Compression refused (summary would grow the conversation): " + f"{before_count} messages preserved" + ) + elif aborted: headline = f"Compression aborted: {before_count} messages preserved" elif fallback_used: headline = ( @@ -78,6 +88,8 @@ def summarize_manual_compression( if noop and after_tokens == before_tokens: token_line = f"Approx request size: ~{before_tokens:,} tokens (unchanged)" + elif refused_would_grow: + token_line = f"Approx request size: ~{before_tokens:,} tokens (unchanged)" else: token_line = ( f"Approx request size: ~{before_tokens:,} → " @@ -85,7 +97,12 @@ def summarize_manual_compression( ) note = None - if aborted: + if refused_would_grow: + note = ( + "The generated summary was larger than what it would replace; " + "no messages were removed." + ) + elif aborted: note = "Summary generation failed; no messages were removed." elif fallback_used: dropped_count = getattr( @@ -113,6 +130,7 @@ def summarize_manual_compression( return { "noop": noop, "aborted": aborted, + "refused_would_grow": refused_would_grow, "fallback_used": fallback_used, "headline": headline, "token_line": token_line, diff --git a/agent/model_metadata.py b/agent/model_metadata.py index 57168fc960..0bbbaea061 100644 --- a/agent/model_metadata.py +++ b/agent/model_metadata.py @@ -1659,10 +1659,28 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]: # The input itself fits — this is purely an output-cap error, so reduce # max_tokens and retry; do NOT compress. "range of max_tokens should be" in error_lower + ) or ( + # OpenAI-compatible relays may reject a request whose output cap exceeds + # the model's separate completion-token limit, e.g. + # "max_tokens (98304) exceeds model's maximum output tokens (65536)" + # This is independent of the input context window. + "exceeds model" in error_lower + and "maximum output tokens" in error_lower ) if not is_output_cap_error: return None + # Generic model-output-cap form: + # "max_tokens (98304) exceeds model's maximum output tokens (65536)" + _m_max_output = re.search( + r'exceeds model(?:\'s)? maximum output tokens\s*\(?\s*(\d+)\s*\)?', + error_lower, + ) + if _m_max_output: + _cap = int(_m_max_output.group(1)) + if _cap >= 1: + return _cap + # DashScope / Alibaba range form: "Range of max_tokens should be [1, 65536]". # The upper bound is the available output cap. _m_range = re.search( @@ -1726,11 +1744,28 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]: # Available output = window - input. When the input alone is at or over # the window this stays None, so the caller correctly falls through to # compression instead of futilely shrinking the output cap. + # + # Caveat: when max_tokens is the BINDING constraint, vLLM does not report + # the real prompt size at all. It back-computes a lower bound from the + # constraint itself -- "at least N input tokens" where + # N == window + 1 - requested_output -- so window - N is always exactly + # requested_output - 1. Subtracting the caller's safety margin then walks + # the cap down ~65 tokens per retry while the reported input walks up by + # the same amount, burning every compression attempt without ever fitting. + # Detect that degenerate case and halve the requested cap instead: it + # carries the same guarantee (strictly below what was rejected) and + # converges in one or two retries. _m_vllm_input = re.search( r'prompt contains (?:at least )?(\d+)\s*input tokens', error_lower ) if _m_ctx_tok and _m_vllm_input: _available = int(_m_ctx_tok.group(1)) - int(_m_vllm_input.group(1)) + _m_requested_out = re.search(r'requested (\d+)\s*output tokens', error_lower) + if 'at least' in error_lower and _m_requested_out: + _requested_out = int(_m_requested_out.group(1)) + if _available >= _requested_out - 1: + # The budget is derived from the constraint, not measured. + return max(1, _requested_out // 2) if _available >= 1: return _available @@ -1782,6 +1817,8 @@ def is_output_cap_error(error_msg: str) -> bool: or "should be" in error_lower # generic "max_tokens should be <= N" or "less than or equal" in error_lower or "must be" in error_lower + or ("exceeds model" in error_lower + and "maximum output tokens" in error_lower) ) if not output_cap_signal: return False diff --git a/agent/prompt_builder.py b/agent/prompt_builder.py index 998e5606a0..4f26ffcf2a 100644 --- a/agent/prompt_builder.py +++ b/agent/prompt_builder.py @@ -191,6 +191,21 @@ MEMORY_GUIDANCE = ( "workflows belong in skills, not memory." ) +USER_PROFILE_GUIDANCE = ( + "You have a persistent user profile across sessions. Save durable facts about " + "the user with the memory tool (target='user'): name, role, preferences, " + "corrections, and communication style. The profile is injected into every turn, " + "so keep it compact and focused on facts that will still matter later.\n" + "The built-in memory notes store is disabled — write only to the user profile " + "(target='user'), never target='memory'.\n" + "Prioritize what reduces future user steering — the most valuable entry is one " + "that prevents the user from having to correct or remind you again.\n" + "Write entries as declarative facts, not instructions to yourself. " + "'User prefers concise responses' ✓ — 'Always respond concisely' ✗. " + "Imperative phrasing gets re-read as a directive in later sessions and can " + "cause repeated work or override the user's current request." +) + SESSION_SEARCH_GUIDANCE = ( "When the user references something from a past conversation or you suspect " "relevant cross-session context exists, use session_search to recall it before " @@ -358,6 +373,25 @@ TOOL_USE_ENFORCEMENT_GUIDANCE = ( # Add new patterns here when a model family needs explicit steering. TOOL_USE_ENFORCEMENT_MODELS = ("gpt", "codex", "gemini", "gemma", "grok", "glm", "qwen", "deepseek") +# Model name substrings whose sessions receive OPENAI_MODEL_EXECUTION_GUIDANCE +# (execution discipline: tool persistence, mandatory tool use for arithmetic, +# external-write read-back, count reconciliation, literal preservation, +# verification-gated completion) when agent.execution_guidance is "auto". +# +# gpt/codex/grok are the historical set; deepseek/kimi/qwen/glm/minimax/ +# mimo/mistral were added after Composio agentic-eval traces showed the same +# failure modes on those families (financial math in prose, no read-back after +# external writes, identifier "repair", completeness claims despite count +# mismatches). GLM's tool-calls-as-plain-text stall (#53847) and MiMo (#41874) +# are covered here too. Gemini/Gemma are excluded — they get the more specific +# GOOGLE_MODEL_OPERATIONAL_GUIDANCE block instead. Claude is excluded because +# it does not exhibit these failure modes; users can opt any model in via +# config.yaml `agent.execution_guidance: true` or a substring list. +EXECUTION_GUIDANCE_MODELS = ( + "gpt", "codex", "grok", + "deepseek", "kimi", "qwen", "glm", "minimax", "mimo", "mistral", +) + # Universal "finish the job" guidance — applied to ALL models, not gated # by model family. Addresses two cross-model failure modes: # 1. Stopping after a stub: writing a tiny file or running one command @@ -438,13 +472,22 @@ PARALLEL_TOOL_CALL_GUIDANCE = ( # without tool calls, suggests workarounds instead of using existing tools, # replies with plans/suggestions instead of executing). The body is # family-agnostic; the OPENAI_ prefix reflects origin, not exclusivity. +# +# As of the Composio agentic-eval follow-up, the block is no longer fenced to +# gpt/codex/grok: eval traces showed DeepSeek/Kimi doing financial math in +# prose, skipping read-back verification after external writes, "repairing" +# malformed identifiers, and claiming completeness despite count mismatches — +# exactly the failure modes this block targets. The injection gate lives in +# agent/system_prompt.py and is controlled by config.yaml +# ``agent.execution_guidance`` (auto/true/false/list); "auto" matches the +# EXECUTION_GUIDANCE_MODELS substring tuple below. OPENAI_MODEL_EXECUTION_GUIDANCE = ( "# Execution discipline\n" "\n" "- Use tools whenever they improve correctness, completeness, or grounding.\n" "- Do not stop early when another tool call would materially improve the result.\n" - "- If a tool returns empty or partial results, retry with a different query or " - "strategy before giving up.\n" + "- If a tool returns empty, partial, or suspiciously narrow results, retry " + "with a broader or different query or strategy before concluding.\n" "- Keep calling tools until: (1) the task is complete, AND (2) you have verified " "the result.\n" "\n" @@ -487,8 +530,30 @@ OPENAI_MODEL_EXECUTION_GUIDANCE = ( "- Formatting: does the output match the requested format or schema?\n" "- Safety: if the next step has side effects (file writes, commands, API calls), " "confirm scope before executing.\n" + "- Completion: 'done' means every named acceptance criterion is verified — " + "never a plausible subset. Completing your plan is not itself the answer; " + "the requested output must appear in your response.\n" "\n" "\n" + "\n" + "- After any state-changing write to an external system (API call, message " + "post, record update), verify the effect by reading back the exact target " + "before claiming success — a successful tool call is not a successful task. " + "Do NOT re-verify internal file edits a tool already confirmed.\n" + "- Declared totals in responses (total, reply_count, has_more, '...N more') " + "are hard assertions. If your enumerated count disagrees, re-fetch or parse " + "programmatically — never finalize on 'go with what I have'.\n" + "- When building write payloads, set fields explicitly rather than relying " + "on provider defaults that could contradict intent.\n" + "\n" + "\n" + "\n" + "- Preserve identifiers, commands, and values exactly as given — never " + "'repair' or normalize a token that fails a stated format. A successful " + "lookup does not validate a malformed source token; validate format first, " + "then look up.\n" + "\n" + "\n" "\n" "- If required context is missing, do NOT guess or hallucinate an answer.\n" "- Use the appropriate lookup tool when missing information is retrievable " diff --git a/agent/reasoning_effort.py b/agent/reasoning_effort.py new file mode 100644 index 0000000000..48b44a25ee --- /dev/null +++ b/agent/reasoning_effort.py @@ -0,0 +1,213 @@ +"""Canonical reasoning-effort vocabulary and wire clamping. + +Hermes' internal effort ladder (``hermes_constants.VALID_REASONING_EFFORTS`` +plus the ``none`` disable level) is wider than what any single provider wire +accepts. Historically every transport and provider profile hand-rolled its own +translation map, and the class of bugs that produced was constant: a new +internal level (``ultra``) leaking to a wire that rejects it with HTTP 400 +(#89503, #70058), or an unknown level being dropped to a weak default so the +strongest ask resolved *weaker* than an explicit ``high`` — a ladder +inversion (#74295, #87279). + +This module is the single source of truth both kinds of code use instead: + +- :data:`EFFORT_LADDER` — canonical low→high ordering. +- :func:`clamp_effort` — the one clamping policy: keep a supported level + verbatim, otherwise take the **nearest weaker** supported level (never + silently escalate cost above what was asked), and only when nothing weaker + exists take the weakest supported level (a provider whose minimum thinking + level is ``high`` serves ``high`` for a ``low`` ask — GLM-5.2's shape). +- Named wire-vocabulary constants for the common OpenAI-compatible surfaces, + so call sites declare *data* ("this route accepts these levels") rather + than logic. + +Rules for call sites: + +1. **Wire shape stays local.** Whether a route wants ``extra_body.reasoning``, + a top-level ``reasoning_effort`` string, or a ``thinking`` toggle is the + caller's business. Only the *vocabulary math* lives here. +2. **Unset stays unset.** ``clamp_effort`` translates an explicit request; it + does not invent one. When the user expressed no effort, prefer omitting + the field so the server default applies. +3. **Never patch a predicate.** When a provider rejects a level, fix its + declared supported set (data), never add another vendor-name special case + at the call site. +""" + +from __future__ import annotations + +import re +from typing import Optional, Sequence + +#: K3 slug detector — matches ``k3`` as a delimited token (``k3``, +#: ``k3-256k``, ``kimi-k3``, ``kimi-k3-cot``) without matching K2-era names +#: (``kimi-k2.6``). From #76427 by @ruizanthony. +_KIMI_K3_SLUG_RE = re.compile(r"(?:^|[^a-z0-9])k3(?:[^a-z0-9]|$)") + +# Canonical low→high ordering used for nearest-level clamping. Superset of +# hermes_constants.VALID_REASONING_EFFORTS ("none" included so an explicit +# disable can be clamped too when a provider publishes it as a level). +EFFORT_LADDER: tuple[str, ...] = ( + "none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra", +) + +# ``ultra`` is Hermes-internal ladder vocabulary (the Codex product tier); no +# provider wire accepts it verbatim anywhere. Every declared wire set below +# therefore stops at ``max`` — ``ultra`` always clamps down. + +#: The widest OpenAI-compatible wire vocabulary (OpenRouter, Nous Portal): +#: exactly max|xhigh|high|medium|low|minimal|none. +OPENAI_COMPAT_WIRE_EFFORTS: tuple[str, ...] = ( + "none", "minimal", "low", "medium", "high", "xhigh", "max", +) + +#: OpenAI/Codex Responses backend — per-model vocabulary, live-verified +#: (Aug 2026): ``minimal`` is rejected by both generations (clamps to low); +#: ``max`` is gpt-5.6-only — gpt-5.5 rejects it with "Supported values are: +#: 'none', 'low', 'medium', 'high', 'xhigh'" (#68365's premise, confirmed). +CODEX_GPT56_EFFORTS: tuple[str, ...] = ( + "none", "low", "medium", "high", "xhigh", "max", +) +CODEX_LEGACY_EFFORTS: tuple[str, ...] = ( + "none", "low", "medium", "high", "xhigh", +) + + +def codex_supported_efforts(model: Optional[str]) -> tuple[str, ...]: + """Supported effort set for an OpenAI/Codex Responses model.""" + if "gpt-5.6" in (model or "").lower(): + return CODEX_GPT56_EFFORTS + return CODEX_LEGACY_EFFORTS + + +#: Backward-compat alias (pre-#68365-verification name). +CODEX_RESPONSES_EFFORTS: tuple[str, ...] = CODEX_GPT56_EFFORTS + +#: xAI Responses — Grok 4.6+ accepts xhigh; older Grok tops out at high. +XAI_GROK46_EFFORTS: tuple[str, ...] = ("low", "medium", "high", "xhigh") +XAI_LEGACY_EFFORTS: tuple[str, ...] = ("low", "medium", "high") + +#: Actual Computer relays (SGLang/vLLM): none/low/medium/high/max. +ACTUAL_RELAY_EFFORTS: tuple[str, ...] = ("none", "low", "medium", "high", "max") + +#: Moonshot/Kimi K3: low/high/max (server default high). +KIMI_K3_EFFORTS: tuple[str, ...] = ("low", "high", "max") +#: Moonshot/Kimi K2-era models: low/medium/high. +KIMI_K2_EFFORTS: tuple[str, ...] = ("low", "medium", "high") + +#: Tencent TokenHub: low/medium/high. +TOKENHUB_EFFORTS: tuple[str, ...] = ("low", "medium", "high") + +#: Kimi K3's vendor-documented translation quirks (platform.kimi.ai +#: thinking-model guide): ``high`` is K3's positional middle AND server +#: default, so ``medium`` rounds to it rather than down to ``low``; ``xhigh`` +#: rounds up to ``max`` (K3's top tier), matching the kimi-coding plugin. +KIMI_K3_OVERRIDES: dict[str, str] = {"medium": "high", "xhigh": "max"} + +#: GLM-5.2 native reasoning_effort knob: exactly two enabled levels, +#: ``high`` (its minimum thinking level) and ``max`` (per Z.AI/BigModel +#: docs). ``xhigh`` requests the top tier, not the floor. +GLM52_EFFORTS: tuple[str, ...] = ("high", "max") +GLM52_OVERRIDES: dict[str, str] = {"xhigh": "max"} + +#: DeepSeek V4 OpenAI-compat endpoint: low/medium/high/max; ``xhigh`` +#: requests the top tier (matches the shipped profile mapping). +DEEPSEEK_V4_EFFORTS: tuple[str, ...] = ("low", "medium", "high", "max") +DEEPSEEK_V4_OVERRIDES: dict[str, str] = {"xhigh": "max"} + +#: Ollama Cloud /v1/chat/completions: accepts {none, low, medium, high, max}; +#: rejects ``minimal`` with HTTP 400. ``xhigh`` requests the top tier. +OLLAMA_CLOUD_EFFORTS: tuple[str, ...] = ("none", "low", "medium", "high", "max") +OLLAMA_CLOUD_OVERRIDES: dict[str, str] = {"xhigh": "max"} + +#: Meta Model API (Muse): minimal..xhigh; rejects ``none``. +META_AI_EFFORTS: tuple[str, ...] = ("minimal", "low", "medium", "high", "xhigh") + +#: Upstage Solar Pro/Open: low/medium/high. +SOLAR_EFFORTS: tuple[str, ...] = ("low", "medium", "high") + + +def kimi_supported_efforts(model: Optional[str]) -> tuple[str, ...]: + """Supported effort set for a Moonshot/Kimi model slug. + + K3 is served as the bare slug ``k3``, plan variants like ``k3-256k``, + and the ``kimi-k3*`` aliases; its documented set is low/high/max. + Everything earlier speaks low/medium/high. Boundary-matched so K2-era + names (``kimi-k2.6``) never match (detection regex from #76427 by + @ruizanthony). + """ + m = (model or "").strip().lower().split("/")[-1] + if _KIMI_K3_SLUG_RE.search(m): + return KIMI_K3_EFFORTS + return KIMI_K2_EFFORTS + + +def clamp_effort( + effort: Optional[str], + supported: Optional[Sequence[str]], + overrides: Optional[dict[str, str]] = None, +) -> Optional[str]: + """Clamp a requested reasoning effort onto a wire's supported levels. + + ``overrides`` is an optional declared mapping consulted first, for routes + whose vendor documents a translation that differs from nearest-weaker + (Kimi K3 documents ``medium → high``: high is its positional middle and + server default). Overrides are data, not logic — a call site never adds + vendor ``if``\\ s around this function. + + Otherwise: returns the requested effort unchanged when it is supported, + when the supported set is unknown (``None``/empty), or when the effort + isn't a recognized ladder level (custom providers may use bespoke names — + pass through rather than guess). Otherwise returns the **nearest weaker** + supported level, so a clamp never silently escalates cost; when nothing + weaker exists, the weakest supported level is returned (the caller asked + for *some* thinking and the provider's floor is the closest honest match). + + The policy is monotonic: a stronger request never resolves to a weaker + wire level than a weaker request would. + """ + requested = str(effort or "").strip().lower() + if not requested or not supported: + return effort + supported_norm = [ + str(level).strip().lower() + for level in supported + if str(level).strip().lower() in EFFORT_LADDER + ] + if not supported_norm or requested in supported_norm: + return effort + if overrides: + mapped = overrides.get(requested) + if mapped in supported_norm: + return mapped + if requested not in EFFORT_LADDER: + return effort + # "none" disables reasoning — it is never a *degradation target* for an + # enabled ask (clamping "minimal" to "none" would silently switch + # thinking off). It still passes through verbatim when requested. + candidates = [level for level in supported_norm if level != "none"] + if not candidates: + return effort + requested_idx = EFFORT_LADDER.index(requested) + below = [ + level for level in candidates + if EFFORT_LADDER.index(level) < requested_idx + ] + if below: + return max(below, key=EFFORT_LADDER.index) + return min(candidates, key=EFFORT_LADDER.index) + + +def requested_effort(reasoning_config: Optional[dict]) -> Optional[str]: + """Extract the user's explicit effort from a reasoning config, or None. + + Returns ``None`` when the config is absent, malformed, carries no effort, + or reasoning is explicitly disabled — callers should then omit the wire + field entirely so the server default applies (rule 2 above). + """ + if not isinstance(reasoning_config, dict): + return None + if reasoning_config.get("enabled") is False: + return None + effort = str(reasoning_config.get("effort") or "").strip().lower() + return effort or None diff --git a/agent/relay_llm.py b/agent/relay_llm.py index ea969da43f..2d8c37a18f 100644 --- a/agent/relay_llm.py +++ b/agent/relay_llm.py @@ -434,6 +434,7 @@ class ManagedLlmStream(Iterator[Any]): self._stream: Any = None self._raw_stream_resource: Any = None self._closed = False + self._runtime_lease: relay_runtime.RelayOperationLease | None = None self._close_error: BaseException | None = None self._callback_error: BaseException | None = None self._logical: tuple[relay_runtime.RelayTurnContext, Any, str] | None = None @@ -573,7 +574,12 @@ class ManagedLlmStream(Iterator[Any]): self._callback_error = exc raise - loop = asyncio.new_event_loop() + self._runtime_lease = runtime.acquire_operation_lease() + try: + loop = asyncio.new_event_loop() + except BaseException: + self._release_runtime_lease() + raise self._loop = loop self._relay_observes_chunks = True try: @@ -613,10 +619,14 @@ class ManagedLlmStream(Iterator[Any]): model_name=self._logical_model_name, provider_name=self._logical_provider_name, response_model_name=self._logical_response_model_name, + operation_lease=self._runtime_lease, ) self._logical = None - loop.close() - self._loop = None + try: + loop.close() + finally: + self._loop = None + self._release_runtime_lease() raise def __iter__(self) -> "ManagedLlmStream": @@ -662,6 +672,7 @@ class ManagedLlmStream(Iterator[Any]): model_name=self._logical_model_name, provider_name=self._logical_provider_name, response_model_name=self._logical_response_model_name, + operation_lease=self._runtime_lease, ) self._logical = None self._close(logical_outcome="cancelled") @@ -719,8 +730,75 @@ class ManagedLlmStream(Iterator[Any]): self._stream = iter(pending) self._raw_stream_resource = None self._accept_chunk = None - if loop is not None: - close = getattr(relay_stream, "aclose", None) + try: + if loop is not None: + close = getattr(relay_stream, "aclose", None) + if callable(close): + + async def close_stream() -> None: + await close() + + try: + loop.run_until_complete(close_stream()) + except Exception: + logger.debug( + "Relay stream cleanup failed during provider fallback", + exc_info=True, + ) + loop.close() + if not self._defer_logical_completion: + _complete_logical( + self._logical, + outcome="success", + model_name=self._logical_model_name, + provider_name=self._logical_provider_name, + response_model_name=self._logical_response_model_name, + operation_lease=self._runtime_lease, + ) + self._logical = None + finally: + self._release_runtime_lease() + + def _close(self, *, logical_outcome: str) -> None: + if self._closed: + return + self._closed = True + self._prefetched_chunks.clear() + try: + loop = self._loop + self._loop = None + if loop is None: + resources = (self._stream, self._raw_stream_resource) + self._stream = None + self._raw_stream_resource = None + closed_ids: set[int] = set() + for resource in resources: + if resource is None or id(resource) in closed_ids: + continue + closed_ids.add(id(resource)) + close = getattr(resource, "close", None) + if callable(close): + try: + close() + except Exception as exc: + if self._close_error is None: + self._close_error = exc + logger.debug( + "Provider stream cleanup failed", + exc_info=True, + ) + if not self._defer_logical_completion: + _complete_logical( + self._logical, + outcome=logical_outcome, + model_name=self._logical_model_name, + provider_name=self._logical_provider_name, + response_model_name=self._logical_response_model_name, + operation_lease=self._runtime_lease, + ) + self._logical = None + return + close = getattr(self._stream, "aclose", None) if callable(close): async def close_stream() -> None: @@ -728,49 +806,9 @@ class ManagedLlmStream(Iterator[Any]): try: loop.run_until_complete(close_stream()) - except Exception: - logger.debug( - "Relay stream cleanup failed during provider fallback", - exc_info=True, - ) - loop.close() - if not self._defer_logical_completion: - _complete_logical( - self._logical, - outcome="success", - model_name=self._logical_model_name, - provider_name=self._logical_provider_name, - response_model_name=self._logical_response_model_name, - ) - self._logical = None - - def _close(self, *, logical_outcome: str) -> None: - if self._closed: - return - self._closed = True - self._prefetched_chunks.clear() - loop = self._loop - self._loop = None - if loop is None: - resources = (self._stream, self._raw_stream_resource) - self._stream = None - self._raw_stream_resource = None - closed_ids: set[int] = set() - for resource in resources: - if resource is None or id(resource) in closed_ids: - continue - closed_ids.add(id(resource)) - close = getattr(resource, "close", None) - if callable(close): - try: - close() - except Exception as exc: - if self._close_error is None: - self._close_error = exc - logger.debug( - "Provider stream cleanup failed", - exc_info=True, - ) + except Exception as exc: + if self._close_error is None: + self._close_error = exc if not self._defer_logical_completion: _complete_logical( self._logical, @@ -778,30 +816,18 @@ class ManagedLlmStream(Iterator[Any]): model_name=self._logical_model_name, provider_name=self._logical_provider_name, response_model_name=self._logical_response_model_name, + operation_lease=self._runtime_lease, ) self._logical = None - return - close = getattr(self._stream, "aclose", None) - if callable(close): + loop.close() + finally: + self._release_runtime_lease() - async def close_stream() -> None: - await close() - - try: - loop.run_until_complete(close_stream()) - except Exception as exc: - if self._close_error is None: - self._close_error = exc - if not self._defer_logical_completion: - _complete_logical( - self._logical, - outcome=logical_outcome, - model_name=self._logical_model_name, - provider_name=self._logical_provider_name, - response_model_name=self._logical_response_model_name, - ) - self._logical = None - loop.close() + def _release_runtime_lease(self) -> None: + lease = self._runtime_lease + self._runtime_lease = None + if lease is not None: + lease.release() def __del__(self) -> None: self._close(logical_outcome="cancelled") @@ -941,6 +967,7 @@ def _complete_logical( model_name: str | None = None, provider_name: str | None = None, response_model_name: str | None = None, + operation_lease: relay_runtime.RelayOperationLease | None = None, ) -> None: if logical is None: return @@ -960,7 +987,10 @@ def _complete_logical( output.update({"model": model_name, "provider": provider_name}) if response_model_name is not None: output["response_model"] = response_model_name - lease.host.run_in_session( + callback = lease.host.run_in_session + if operation_lease is not None: + callback = operation_lease.run_in_session + callback( lease.session, relay_runtime.pop_relay_scope, lease.host.relay, diff --git a/agent/relay_runtime.py b/agent/relay_runtime.py index 003213d87d..3dbbd397a0 100644 --- a/agent/relay_runtime.py +++ b/agent/relay_runtime.py @@ -8,13 +8,21 @@ import contextvars import importlib import inspect import logging +import os import threading +import tomllib import uuid from concurrent.futures import TimeoutError as FuturesTimeoutError from dataclasses import dataclass, field +from enum import Enum, auto +from pathlib import Path from typing import Any, Callable from hermes_constants import get_hermes_home +from hermes_cli.relay_plugin_cutover import ( + RELAY_PLUGINS_CONFIG_ENV, + configured_legacy_relay_env_vars, +) logger = logging.getLogger(__name__) @@ -24,6 +32,7 @@ LOGICAL_LLM_SCOPE = "hermes.logical_llm_call" RUNTIME_SCHEMA_KEY = "hermes.relay.schema_version" RUNTIME_SCHEMA_VERSION = "hermes.relay.runtime.v1" RUNTIME_INSTANCE_KEY = "hermes.relay.runtime_instance" +RELAY_PLUGINS_EXECUTION_CONSUMER = "hermes.nemo_relay.plugins" _PROFILE_KEY_CACHE: dict[str, str] = {} # Bound for native scope lifecycle operations (push/pop/flush) that gate @@ -128,6 +137,20 @@ def pop_relay_scope( return pop(handle, **kwargs) +class _RelayPluginConfigurationState(Enum): + """Process-wide result shared by every currently hosted profile.""" + + UNINITIALIZED = auto() + DISABLED = auto() + ACTIVE = auto() + FOREIGN = auto() + FAILED = auto() + + +class _RelayPluginConfigurationLoadError(RuntimeError): + """An explicitly selected Relay plugin configuration could not be loaded.""" + + @dataclass class RelaySession: """One isolated Relay scope stack owned by a Hermes session.""" @@ -199,8 +222,232 @@ def _reset_segments_config_for_tests() -> None: _SEGMENTS_CONFIG = None +class RelayOperationLease: + """Keep process-wide Relay plugins alive across a deferred operation.""" + + def __init__(self, runtime: "RelayRuntime") -> None: + self._lock = threading.Lock() + self._runtime: RelayRuntime | None = runtime + + def run_in_session( + self, + session: RelaySession, + callback: Callable[..., Any], + *args: Any, + **kwargs: Any, + ) -> Any: + """Run cleanup while this lease still owns the runtime lifetime.""" + with self._lock: + runtime = self._runtime + if runtime is None: + raise RuntimeError("Hermes Relay operation lease is released") + return runtime._run_in_session_untracked( + session, + callback, + *args, + **kwargs, + ) + + def release(self) -> None: + """Release this lease exactly once.""" + with self._lock: + runtime = self._runtime + self._runtime = None + if runtime is not None: + runtime._end_operation() + + +class _ProcessRelayPluginConfiguration: + """Own one Relay plugin configuration across profile-scoped hosts.""" + + def __init__(self) -> None: + self._lock = threading.RLock() + self._owners: set[int] = set() + self._state = _RelayPluginConfigurationState.UNINITIALIZED + self._active = False + self._relay: Any = None + self._activation: Any = None + + def acquire( + self, + owner: Any, + relay: Any, + ) -> _RelayPluginConfigurationState: + """Join the process configuration, initializing it for the first host.""" + owner_id = id(owner) + with self._lock: + if owner_id in self._owners: + return self._state + if self._owners: + self._owners.add(owner_id) + return self._state + if self._active and not self._clear_active(): + logger.warning( + "Hermes Relay plugin cleanup is still pending; refusing to " + "replace the process-global configuration" + ) + return self._remember( + owner_id, + _RelayPluginConfigurationState.FAILED, + ) + + try: + existing_report = relay.plugin.report() + except Exception: + logger.warning( + "Hermes could not determine whether a process-global Relay " + "plugin configuration is already active; refusing to replace it", + exc_info=True, + ) + return self._remember( + owner_id, + _RelayPluginConfigurationState.FAILED, + ) + if existing_report is not None: + logger.warning( + "A process-global Relay plugin configuration is already active " + "outside Hermes native ownership; leaving it unchanged and " + "disabling Hermes-managed Relay middleware for this process" + ) + return self._remember( + owner_id, + _RelayPluginConfigurationState.FOREIGN, + ) + + try: + configured_inputs = _configured_plugin_inputs(relay) + if configured_inputs is None: + return self._remember( + owner_id, + _RelayPluginConfigurationState.DISABLED, + ) + plugin_config, dynamic_plugins = configured_inputs + if dynamic_plugins: + try: + activation = _resolve_plugin_awaitable( + relay.plugin.initialize_with_dynamic_plugins( + plugin_config, + dynamic_plugins, + ) + ) + if activation is None: + raise RuntimeError( + "NeMo Relay dynamic plugin initialization " + "returned no activation handle" + ) + self._activation = activation + except Exception as exc: + raise RuntimeError( + "Hermes Relay dynamic plugin activation failed" + ) from exc + + if self._activation is None: + # Hermes only enters Relay's initialization path after an + # explicit opt-in. Relay currently owns any subsequent ambient + # layering; a future discovery=False API can make this exact. + _resolve_plugin_awaitable(relay.plugin.initialize(plugin_config)) + except Exception as exc: + self._activation = None + logger.warning( + "Hermes Relay plugin initialization failed: %s", + exc, + exc_info=True, + ) + return self._remember( + owner_id, + _RelayPluginConfigurationState.FAILED, + ) + + self._active = True + self._relay = relay + state = self._remember( + owner_id, + _RelayPluginConfigurationState.ACTIVE, + ) + logger.info( + "Relay plugins are active process-wide and apply to all profiles " + "hosted by this Hermes process." + ) + return state + + def _remember( + self, + owner_id: int, + state: _RelayPluginConfigurationState, + ) -> _RelayPluginConfigurationState: + """Retain one process decision for all concurrently hosted profiles.""" + self._owners.add(owner_id) + self._state = state + return state + + def release(self, owner: Any) -> None: + """Release one host and clear Relay after the final host exits.""" + owner_id = id(owner) + with self._lock: + if owner_id not in self._owners: + return + self._owners.remove(owner_id) + if self._owners: + return + if self._clear_active(): + self._state = _RelayPluginConfigurationState.UNINITIALIZED + + def reset_for_tests(self) -> None: + """Clear process-global state left by directly constructed test hosts.""" + with self._lock: + self._owners.clear() + if self._clear_active(): + self._state = _RelayPluginConfigurationState.UNINITIALIZED + + def retry_pending_cleanup(self) -> None: + """Retry a failed final cleanup without disrupting live owners.""" + with self._lock: + if not self._owners: + if self._clear_active(): + self._state = _RelayPluginConfigurationState.UNINITIALIZED + + def _clear_active(self) -> bool: + relay = self._relay + activation = self._activation + active = self._active + if not active or relay is None: + return True + try: + _flush_relay_subscribers(relay) + except Exception: + logger.warning( + "Hermes Relay plugin subscriber flush failed", + exc_info=True, + ) + return False + try: + if activation is not None: + close = getattr(activation, "close", None) + if not callable(close): + raise RuntimeError( + "NeMo Relay dynamic plugin activation has no close method" + ) + _resolve_plugin_awaitable(close()) + else: + _clear_relay_plugins(relay) + except Exception: + logger.warning( + "Hermes Relay plugin configuration cleanup failed", + exc_info=True, + ) + return False + self._active = False + self._relay = None + self._activation = None + return True + + +_PLUGIN_CONFIGURATION = _ProcessRelayPluginConfiguration() +atexit.register(_PLUGIN_CONFIGURATION.retry_pending_cleanup) + + class RelayRuntime: - """Own Relay session scopes independently of any exporter or plugin.""" + """Own Relay session scopes and optional process plugin configuration.""" def __init__(self, relay: Any = None, *, profile_key: str | None = None) -> None: self.relay = relay or _load_nemo_relay() @@ -210,8 +457,24 @@ class RelayRuntime: self._sessions: dict[str, RelaySession] = {} self._subagent_parents: dict[str, str] = {} self._subagent_parent_handles: dict[str, Any] = {} + self._closing = False + self._shutdown_started = False + self._shutdown_complete = threading.Event() + self._operations_idle = threading.Event() + self._operations_idle.set() + self._active_operations = 0 self._execution_consumers_lock = threading.RLock() self._execution_consumers: set[str] = set() + self._plugin_configuration_state = _PLUGIN_CONFIGURATION.acquire( + self, + self.relay, + ) + self._plugin_configuration_registered = True + if ( + self._plugin_configuration_state + is _RelayPluginConfigurationState.ACTIVE + ): + self.retain_managed_execution(RELAY_PLUGINS_EXECUTION_CONSUMER) self._shutdown_registered = True atexit.register(self.shutdown) @@ -244,6 +507,8 @@ class RelayRuntime: if not session_id: return None with self._sessions_lock: + if self._closing: + return None session = self._sessions.get(session_id) if session is None: parent_session_id = self._subagent_parents.get(session_id, "") @@ -404,6 +669,8 @@ class RelayRuntime: ): parent_handle = turn.handle with self._sessions_lock: + if self._closing: + return None self._subagent_parents[child_session_id] = parent_session_id if parent_handle is not None: self._subagent_parent_handles[child_session_id] = parent_handle @@ -425,6 +692,8 @@ class RelayRuntime: def get_session(self, session_id: str) -> RelaySession | None: """Return an active Hermes Relay session without creating one.""" with self._sessions_lock: + if self._closing: + return None session = self._sessions.get(str(session_id or "")) if session is None: return None @@ -458,6 +727,29 @@ class RelayRuntime: span, never the agent. The abandoned daemon worker cannot block process exit (tools.daemon_pool contract). """ + self._begin_operation() + try: + return self._run_in_session_untracked( + session, + callback, + *args, + allow_closing=allow_closing, + timeout=timeout, + **kwargs, + ) + finally: + self._end_operation() + + def _run_in_session_untracked( + self, + session: RelaySession, + callback: Callable[..., Any], + *args: Any, + allow_closing: bool = False, + timeout: float | None = None, + **kwargs: Any, + ) -> Any: + """Run inside a session whose host-level lifetime is already held.""" with session.lock: if session.closing and not allow_closing: raise RuntimeError("Hermes Relay session is closing") @@ -506,26 +798,49 @@ class RelayRuntime: **kwargs: Any, ) -> Any: """Create and await an operation inside the session's saved context.""" - with session.lock: - if session.closing and not allow_closing: - raise RuntimeError("Hermes Relay session is closing") - if session.context is None or session.handle is None: - raise RuntimeError("Hermes Relay session context is unavailable") - relay_context = session.context.copy() + self._begin_operation() + try: + with session.lock: + if session.closing and not allow_closing: + raise RuntimeError("Hermes Relay session is closing") + if session.context is None or session.handle is None: + raise RuntimeError("Hermes Relay session context is unavailable") + relay_context = session.context.copy() - context = contextvars.copy_context() - for variable, value in relay_context.items(): - context.run(variable.set, value) + context = contextvars.copy_context() + for variable, value in relay_context.items(): + context.run(variable.set, value) - async def invoke() -> Any: - self.relay.get_scope_stack() - result = callback(*args, **kwargs) - if inspect.isawaitable(result): - return await result - return result + async def invoke() -> Any: + self.relay.get_scope_stack() + result = callback(*args, **kwargs) + if inspect.isawaitable(result): + return await result + return result - task = context.run(asyncio.create_task, invoke()) - return await task + task = context.run(asyncio.create_task, invoke()) + return await task + finally: + self._end_operation() + + def _begin_operation(self) -> None: + """Admit one Relay call while keeping process plugins alive.""" + with self._sessions_lock: + if self._closing: + raise RuntimeError("Hermes Relay runtime is shutting down") + self._active_operations += 1 + self._operations_idle.clear() + + def _end_operation(self) -> None: + with self._sessions_lock: + self._active_operations -= 1 + if self._active_operations == 0: + self._operations_idle.set() + + def acquire_operation_lease(self) -> RelayOperationLease: + """Retain plugin lifetime for work that outlives one Relay await.""" + self._begin_operation() + return RelayOperationLease(self) def emit_mark( self, @@ -586,6 +901,7 @@ class RelayRuntime: allow_closing: bool = False, failure_label: str = "scope close failed", drain_limit: int = 32, + operation_already_held: bool = False, ) -> str | None: """Pop ``handle``, draining orphaned children in the same session context. @@ -702,7 +1018,12 @@ class RelayRuntime: error_holder["retry"] = retry_exc try: - self.run_in_session( + run_in_session = ( + self._run_in_session_untracked + if operation_already_held + else self.run_in_session + ) + run_in_session( session, close_with_drain, allow_closing=allow_closing, @@ -721,6 +1042,17 @@ class RelayRuntime: def close_session(self, event: dict[str, Any]) -> None: """Close one session scope and remove it from the core registry.""" + try: + self._begin_operation() + except RuntimeError: + return + try: + self._close_session(event) + finally: + self._end_operation() + + def _close_session(self, event: dict[str, Any]) -> None: + """Close one session already admitted by the host lifecycle gate.""" session_id = _session_id(event) with self._sessions_lock: session = self._sessions.get(session_id) @@ -741,23 +1073,13 @@ class RelayRuntime: output={}, allow_closing=True, failure_label="session scope close failed", + operation_already_held=True, ) if failure: failures.append(failure) - try: - try: - _scope_op_executor().submit( - self.relay.subscribers.flush - ).result(timeout=_SCOPE_OP_TIMEOUT) - except RuntimeError: - # Interpreter shutdown: executor refuses new futures; flush - # on a bounded exit thread so a wedged pipeline cannot - # block process exit. - _run_bounded_on_exit_thread( - self.relay.subscribers.flush, _SCOPE_OP_TIMEOUT - ) - except Exception as exc: - failures.append(f"subscriber flush failed: {exc}") + # Subscriber flushing is process-wide and may wait for publications + # owned by other sessions. Final plugin teardown flushes once after all + # tracked operations drain; doing it here can deadlock an asyncio loop. with self._sessions_lock: if self._sessions.get(session_id) is session: self._sessions.pop(session_id, None) @@ -771,17 +1093,64 @@ class RelayRuntime: ) def shutdown(self) -> None: - """Close all core-owned Relay session scopes.""" + """Close core scopes and release process plugin configuration.""" with self._sessions_lock: - session_ids = list(self._sessions) - for session_id in session_ids: - self._safe(self.close_session, {"session_id": session_id}) - if self._shutdown_registered: + if self._shutdown_started: + return + self._shutdown_started = True + self._closing = True + has_active_operations = self._active_operations > 0 + if has_active_operations: + thread = threading.Thread( + target=self._finish_shutdown_after_operations, + name=f"hermes-nemo-relay-shutdown-{self.runtime_id[:8]}", + daemon=True, + ) try: - atexit.unregister(self.shutdown) + thread.start() except Exception: - pass - self._shutdown_registered = False + with self._sessions_lock: + self._shutdown_started = False + logger.warning( + "Hermes Relay deferred shutdown could not start", + exc_info=True, + ) + return + self._finish_shutdown() + + def _finish_shutdown_after_operations(self) -> None: + self._operations_idle.wait() + self._finish_shutdown() + + def _finish_shutdown(self) -> None: + try: + with self._sessions_lock: + session_ids = list(self._sessions) + for session_id in session_ids: + self._safe(self._close_session, {"session_id": session_id}) + if self._plugin_configuration_registered: + if ( + self._plugin_configuration_state + is _RelayPluginConfigurationState.ACTIVE + ): + self.release_managed_execution( + RELAY_PLUGINS_EXECUTION_CONSUMER + ) + _PLUGIN_CONFIGURATION.release(self) + self._plugin_configuration_registered = False + if self._shutdown_registered: + try: + atexit.unregister(self.shutdown) + except Exception: + pass + self._shutdown_registered = False + except Exception: + with self._sessions_lock: + self._shutdown_started = False + logger.warning("Hermes Relay shutdown failed", exc_info=True) + return + with self._sessions_lock: + self._shutdown_complete.set() @staticmethod def _safe(callback: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: @@ -1611,6 +1980,87 @@ def _load_nemo_relay() -> Any: return importlib.import_module("nemo_relay") +def _configured_plugin_inputs( + relay: Any, +) -> tuple[dict[str, Any], list[Any]] | None: + """Load selected plugin inputs, or return ``None`` when none were selected.""" + configured = os.environ.get(RELAY_PLUGINS_CONFIG_ENV, "").strip() + if not configured: + legacy_vars = configured_legacy_relay_env_vars(os.environ) + if legacy_vars: + logger.warning( + "Legacy NeMo Relay exporter variables are set but no %s was " + "provided. %s no longer activate Relay exporters; migrate the " + "exporter configuration to a Relay plugins.toml file.", + RELAY_PLUGINS_CONFIG_ENV, + ", ".join(legacy_vars), + ) + return None + + config_path = Path(configured).expanduser() + try: + with config_path.open("rb") as config_file: + config = tomllib.load(config_file) + if "dynamic_plugins" in config: + raise ValueError( + "Hermes [[dynamic_plugins]] records are unsupported; use Relay " + "[[plugins.dynamic]] records" + ) + dynamic_plugins: list[Any] = [] + if "plugins" in config: + dynamic_plugins = relay.plugin.load_dynamic_plugin_activation_specs( + config_path + ) + plugin_config = dict(config) + plugin_config.pop("plugins", None) + return plugin_config, dynamic_plugins + except Exception as exc: + raise _RelayPluginConfigurationLoadError( + "Hermes Relay plugin configuration could not be loaded from " + f"{config_path}; continuing without Relay plugins" + ) from exc + + +def _flush_relay_subscribers(relay: Any) -> None: + """Flush Relay without blocking an asyncio event-loop thread.""" + _resolve_plugin_awaitable(relay.subscribers.flush_async()) + + +def _clear_relay_plugins(relay: Any) -> None: + """Clear Relay plugins without blocking an asyncio event-loop thread.""" + _resolve_plugin_awaitable(relay.plugin.clear_async()) + + +def _resolve_plugin_awaitable(value: Any) -> Any: + """Resolve Relay's async plugin API from synchronous host construction.""" + if not inspect.isawaitable(value): + return value + try: + asyncio.get_running_loop() + except RuntimeError: + return asyncio.run(value) + + result: dict[str, Any] = {} + error: dict[str, BaseException] = {} + + def _runner() -> None: + try: + result["value"] = asyncio.run(value) + except BaseException as exc: # pragma: no cover - re-raised below + error["exc"] = exc + + thread = threading.Thread( + target=_runner, + name="hermes-nemo-relay-plugin-lifecycle", + daemon=True, + ) + thread.start() + thread.join() + if "exc" in error: + raise error["exc"] + return result.get("value") + + def _session_id(event: dict[str, Any]) -> str: return str(event.get("session_id") or "") @@ -1619,4 +2069,5 @@ def _reset_for_tests() -> None: """Reset all profile-scoped Relay hosts for isolated tests.""" SESSION_COORDINATOR._reset_active_turns_for_tests() HOST_REGISTRY.shutdown_all() + _PLUGIN_CONFIGURATION.reset_for_tests() _PROFILE_KEY_CACHE.clear() diff --git a/agent/skill_commands.py b/agent/skill_commands.py index 4169e54fe4..f1e42e6902 100644 --- a/agent/skill_commands.py +++ b/agent/skill_commands.py @@ -8,6 +8,7 @@ import json import logging import os import re +import threading from pathlib import Path from typing import Any, Dict, Optional @@ -24,6 +25,10 @@ logger = logging.getLogger(__name__) _skill_commands: Dict[str, Dict[str, Any]] = {} _skill_commands_platform: Optional[str] = None _skill_commands_home: Optional[str] = None +# Guards the (map, platform-tag, home-tag) triple so publication and the +# freshness lookup always see a consistent snapshot. Scanning itself stays +# outside this lock. +_publish_lock = threading.Lock() # Patterns for sanitizing skill names into clean hyphen-separated slugs. _SKILL_INVALID_CHARS = re.compile(r"[^a-z0-9-]") _SKILL_MULTI_HYPHEN = re.compile(r"-{2,}") @@ -423,9 +428,15 @@ def scan_skill_commands() -> Dict[str, Dict[str, Any]]: Dict mapping "/skill-name" to {name, description, skill_md_path, skill_dir}. """ global _skill_commands, _skill_commands_platform, _skill_commands_home - _skill_commands_platform = _resolve_skill_commands_platform() - _skill_commands_home = _resolve_skill_commands_home() - _skill_commands = {} + platform = _resolve_skill_commands_platform() + home = _resolve_skill_commands_home() + # Build into a local map and publish once, at the end. Writing straight + # into the global made a scan's partial results visible to everything + # else in the process: a second, overlapping scan deduped against its own + # (empty) ``seen_names`` but collided against the first scan's already- + # published slugs, logging one bogus "already claimed" warning per skill — + # each naming the same skill as its own incumbent (#74574). + commands: Dict[str, Dict[str, Any]] = {} try: from tools.skills_tool import SKILLS_DIR, _parse_frontmatter, skill_matches_platform, skill_matches_environment, _get_disabled_skill_names from agent.skill_utils import ( @@ -505,14 +516,14 @@ def scan_skill_commands() -> Dict[str, Dict[str, Any]]: # slug (e.g. "git_helper" vs "git-helper"). First-wins # preserves local-before-external precedence. cmd_key = f"/{cmd_name}" - if cmd_key in _skill_commands: + if cmd_key in commands: logger.warning( "Skill %r maps to slash command %s already claimed " "by %r; keeping the first and skipping this one.", - name, cmd_key, _skill_commands[cmd_key]["name"], + name, cmd_key, commands[cmd_key]["name"], ) continue - _skill_commands[cmd_key] = { + commands[cmd_key] = { "name": name, "description": description or f"Invoke the {name} skill", "skill_md_path": str(skill_md), @@ -522,7 +533,18 @@ def scan_skill_commands() -> Dict[str, Dict[str, Any]]: continue except Exception: pass - return _skill_commands + # Publish the finished map and the platform/home it was scanned for as + # ONE step. Bare assignments are not atomic together: a reader landing + # between them sees the NEW map still carrying the OLD platform tag, and + # if that stale tag happens to match its own platform it accepts the map + # without rescanning — serving another platform's disabled-skill view, + # exactly the leak #14536 closed. Only the publish/lookup pair is locked; + # the scan above (file I/O, deferred imports) stays outside it. + with _publish_lock: + _skill_commands = commands + _skill_commands_platform = platform + _skill_commands_home = home + return commands def get_skill_commands() -> Dict[str, Dict[str, Any]]: @@ -534,13 +556,22 @@ def get_skill_commands() -> Dict[str, Dict[str, Any]]: active profile's Hermes home changes (e.g. Desktop switching profiles mid-session) so each profile sees its own ``skills.external_dirs`` (#88023). """ - if ( - not _skill_commands - or _skill_commands_platform != _resolve_skill_commands_platform() - or _skill_commands_home != _resolve_skill_commands_home() - ): - scan_skill_commands() - return _skill_commands + current_platform = _resolve_skill_commands_platform() + current_home = _resolve_skill_commands_home() + # Read the map and its tags under the same lock that publishes them, so + # the freshness decision is made against a consistent snapshot. + with _publish_lock: + commands = _skill_commands + is_fresh = ( + bool(commands) + and _skill_commands_platform == current_platform + and _skill_commands_home == current_home + ) + if is_fresh: + return commands + # Scan outside the lock — it does file I/O and deferred imports, and + # concurrent scans are already safe (each builds its own map). + return scan_skill_commands() def reload_skills() -> Dict[str, Any]: diff --git a/agent/system_prompt.py b/agent/system_prompt.py index 2e2579a79c..b176a2554f 100644 --- a/agent/system_prompt.py +++ b/agent/system_prompt.py @@ -33,10 +33,12 @@ from typing import Any, Dict, List, Optional from agent.prompt_builder import ( DEFAULT_AGENT_IDENTITY, + EXECUTION_GUIDANCE_MODELS, GOOGLE_MODEL_OPERATIONAL_GUIDANCE, HERMES_AGENT_HELP_GUIDANCE, KANBAN_GUIDANCE, MEMORY_GUIDANCE, + USER_PROFILE_GUIDANCE, OPENAI_MODEL_EXECUTION_GUIDANCE, PARALLEL_TOOL_CALL_GUIDANCE, PLATFORM_HINTS, @@ -414,8 +416,21 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None) # Tool-aware behavioral guidance: only inject when the tools are loaded tool_guidance = [] + # MEMORY_GUIDANCE instructs the model to save facts to the built-in + # MEMORY.md/USER.md stores. With both disabled in config no store is built, + # so the guidance would steer the model at a tool whose every call returns + # "Memory is not available". Defaults to True for the rare code paths that + # build an agent view without going through agent_init. + # When only the user profile store is enabled, the narrower + # USER_PROFILE_GUIDANCE is injected instead — the full block instructs the + # model to write notes to a MEMORY.md store that does not exist. + _mem_enabled = getattr(agent, "_memory_enabled", True) + _profile_enabled = getattr(agent, "_user_profile_enabled", True) if "memory" in agent.valid_tool_names: - tool_guidance.append(MEMORY_GUIDANCE) + if _mem_enabled: + tool_guidance.append(MEMORY_GUIDANCE) + elif _profile_enabled: + tool_guidance.append(USER_PROFILE_GUIDANCE) if "session_search" in agent.valid_tool_names: tool_guidance.append(SESSION_SEARCH_GUIDANCE) if "skill_manage" in agent.valid_tool_names: @@ -477,13 +492,36 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None) # paths, parallel tool calls, verify-before-edit, etc.) if "gemini" in _model_lower or "gemma" in _model_lower: stable_parts.append(GOOGLE_MODEL_OPERATIONAL_GUIDANCE) - # OpenAI GPT/Codex execution discipline (tool persistence, - # prerequisite checks, verification, anti-hallucination). - # Also applied to xAI Grok — same failure modes (claims completion - # without tool calls, suggests workarounds instead of using - # existing tools, replies with plans instead of executing). - if "gpt" in _model_lower or "codex" in _model_lower or "grok" in _model_lower: - stable_parts.append(OPENAI_MODEL_EXECUTION_GUIDANCE) + + # Execution-discipline guidance (tool persistence, mandatory tool use + # for arithmetic, external-write read-back, count reconciliation, + # literal preservation, verification-gated completion). Historically + # nested inside the tool-use-enforcement branch and fenced to + # gpt/codex/grok; now an independent gate so DeepSeek/Kimi/Qwen-class + # models receive it even when tool_use_enforcement is off. Controlled + # by config.yaml agent.execution_guidance: + # "auto" (default) — matches EXECUTION_GUIDANCE_MODELS + # true — always inject (all models) + # false — never inject + # list — custom model-name substrings to match + # Resolved once at session start keyed on the (fixed) model name, so + # the system prompt stays byte-stable for the life of the conversation. + if agent.valid_tool_names: + _exec_guidance = getattr(agent, "_execution_guidance", "auto") + _exec_inject = False + if _exec_guidance is True or (isinstance(_exec_guidance, str) and _exec_guidance.lower() in {"true", "always", "yes", "on"}): + _exec_inject = True + elif _exec_guidance is False or (isinstance(_exec_guidance, str) and _exec_guidance.lower() in {"false", "never", "no", "off"}): + _exec_inject = False + elif isinstance(_exec_guidance, list): + model_lower = (agent.model or "").lower() + _exec_inject = any(p.lower() in model_lower for p in _exec_guidance if isinstance(p, str)) + else: + # "auto" or any unrecognised value — use hardcoded defaults + model_lower = (agent.model or "").lower() + _exec_inject = any(p in model_lower for p in EXECUTION_GUIDANCE_MODELS) + if _exec_inject: + stable_parts.append(OPENAI_MODEL_EXECUTION_GUIDANCE) has_skills_tools = any(name in agent.valid_tool_names for name in ['skills_list', 'skill_view', 'skill_manage']) if has_skills_tools: diff --git a/agent/tool_dispatch_helpers.py b/agent/tool_dispatch_helpers.py index af0970accb..0c4259b656 100644 --- a/agent/tool_dispatch_helpers.py +++ b/agent/tool_dispatch_helpers.py @@ -557,7 +557,11 @@ def make_tool_result_message( The outer list itself is rebuilt rather than returned by identity, so callers should compare by value, not by ``is``. """ - wrapped = _maybe_wrap_untrusted(name, content) + # Order matters: detect provider-side elision on the RAW content and + # append the notice first, THEN wrap — so the notice lives inside the + # untrusted block next to the data it describes, appended exactly once + # at construction time (cache-safe). + wrapped = _maybe_wrap_untrusted(name, _maybe_append_elision_notice(name, content)) message = stamp_message_timestamp({ "role": "tool", "name": name, @@ -608,6 +612,70 @@ def _is_untrusted_tool(name: Optional[str]) -> bool: return any(name.startswith(p) for p in _UNTRUSTED_TOOL_PREFIXES) +# --- Upstream-elision detection -------------------------------------------- +# +# Some MCP servers elide data SERVER-SIDE and mark the elision inside the +# payload itself (e.g. Composio: '...13 more items' inside a JSON array, +# '"has_more": true', 'Complete response was large (N tokens). Full data +# saved to sandbox in /mnt/files/...', 'data_preview' envelopes). Because the +# result looks structurally complete, models treat the visible slice as the +# whole dataset and falsely claim completeness. When one of these markers is +# present, we append ONE compact notice at result-construction time — before +# the message enters history, never mutated later, so prompt caching is safe. + +# Conservative patterns only: each one is an explicit provider-side "there is +# more data than what you can see" signal, not a generic truncation heuristic. +_UPSTREAM_ELISION_PATTERNS = ( + re.compile(r"\.\.\.\s*\d+\s+more\s+items?", re.IGNORECASE), + re.compile(r'"has_more"\s*:\s*true', re.IGNORECASE), + re.compile(r"saved to sandbox", re.IGNORECASE), + re.compile(r"data_preview", re.IGNORECASE), +) + +# Results smaller than this can't meaningfully hide an elided enumeration — +# skip the scan entirely so tiny results pay nothing. +_ELISION_SCAN_MIN_CHARS = 1_000 + +# Bound the regex scan: markers appear near the elided structure, which for +# the payload sizes that matter (20-50K) is always inside the first 64KB. +_ELISION_SCAN_MAX_CHARS = 65_536 + +_UPSTREAM_ELISION_NOTICE = ( + '\n[hermes note: this result contains provider-side elision markers ' + '(e.g. "...N more items" / has_more:true). The data shown is INCOMPLETE ' + '— page/fetch the remainder before treating any enumeration as complete.]' +) + + +def _detect_upstream_elision(content: Any) -> bool: + """True when a string tool result carries provider-side elision markers. + + Cheap and safe by construction: non-string content is never scanned, + results under ``_ELISION_SCAN_MIN_CHARS`` short-circuit, and the regex + scan is capped at the first ``_ELISION_SCAN_MAX_CHARS`` chars. + """ + if not isinstance(content, str): + return False + if len(content) < _ELISION_SCAN_MIN_CHARS: + return False + window = content[:_ELISION_SCAN_MAX_CHARS] + return any(p.search(window) for p in _UPSTREAM_ELISION_PATTERNS) + + +def _maybe_append_elision_notice(name: str, content: Any) -> Any: + """Append the incompleteness notice to untrusted string results that + embed upstream elision markers. Returns ``content`` unchanged otherwise. + + Runs on the RAW result before untrusted-wrapping so the notice sits with + the data it describes, and only at result-construction time (cache-safe). + """ + if not _is_untrusted_tool(name): + return content + if _detect_upstream_elision(content): + return content + _UPSTREAM_ELISION_NOTICE + return content + + def _tool_output_risk_metadata(name: str, content: Any) -> Optional[Dict[str, Any]]: """Classify textual attacker-controlled output without retaining a copy. @@ -729,5 +797,7 @@ __all__ = [ "_extract_landed_file_mutation_paths", "_extract_error_preview", "_trajectory_normalize_msg", + "_detect_upstream_elision", + "_maybe_append_elision_notice", "make_tool_result_message", ] diff --git a/agent/tool_executor.py b/agent/tool_executor.py index 90226b02e1..e7bb9126db 100644 --- a/agent/tool_executor.py +++ b/agent/tool_executor.py @@ -48,12 +48,31 @@ from tools.thread_context import propagate_context_to_thread from tools.tool_result_storage import ( maybe_persist_tool_result, enforce_turn_budget, + extract_persisted_path, ) from tools.budget_config import BudgetConfig, DEFAULT_BUDGET, budget_for_context_window logger = logging.getLogger(__name__) +def _record_persisted_path_for_stub(agent, tool_call_id: str, function_result) -> None: + """Tell the stall guards where a persisted result's full content lives. + + When a large result is spilled to disk ( preview), a + later result-reference stub pointing at that first occurrence must carry + the spillover file path so the reference can't dangle. Best-effort: never + lets bookkeeping break tool execution. + """ + try: + if not isinstance(function_result, str): + return + path = extract_persisted_path(function_result) + if path: + agent._tool_guardrails.record_persisted_result(tool_call_id, path) + except Exception as exc: + logger.debug("persisted-path record for result stub failed: %s", exc) + + def _ensure_file_checkpoint( agent, function_name: str, @@ -87,7 +106,10 @@ def _budget_for_agent(agent) -> BudgetConfig: """ try: ctx = getattr(getattr(agent, "context_compressor", None), "context_length", None) - return budget_for_context_window(int(ctx)) if ctx else DEFAULT_BUDGET + # budget_for_context_window(None) (rather than DEFAULT_BUDGET) so the + # config-driven MCP threshold override still applies when the context + # length isn't resolvable. + return budget_for_context_window(int(ctx) if ctx else None) except Exception: return DEFAULT_BUDGET @@ -1730,6 +1752,7 @@ def execute_tool_calls_concurrent(agent, assistant_message, messages: list, effe function_args, function_result, failed=is_error, + tool_call_id=getattr(tc, "id", "") or "", ) if is_error: @@ -1764,6 +1787,7 @@ def execute_tool_calls_concurrent(agent, assistant_message, messages: list, effe env=get_active_env(effective_task_id), config=_tool_budget, ) if not _is_multimodal_tool_result(function_result) else function_result + _record_persisted_path_for_stub(agent, tc.id, function_result) subdir_hints = agent._subdirectory_hints.check_tool_call(name, args) if subdir_hints: @@ -2178,6 +2202,57 @@ def execute_tool_calls_sequential(agent, assistant_message, messages: list, effe tool_duration = time.time() - tool_start_time if agent._should_emit_quiet_tool_messages(): agent._vprint(f" {_get_cute_tool_message_impl('read_preview', function_args, tool_duration, result=function_result)}") + elif function_name == "drive_preview": + def _execute(next_args: dict) -> Any: + from tools.drive_preview_tool import drive_preview_tool as _drive_preview_tool + return _drive_preview_tool( + action=next_args.get("action", ""), + ref=next_args.get("ref"), + selector=next_args.get("selector"), + text=next_args.get("text"), + key=next_args.get("key"), + submit=next_args.get("submit"), + amount=next_args.get("amount"), + to=next_args.get("to"), + limit=next_args.get("max"), + callback=getattr(agent, "drive_preview_callback", None), + ) + function_result, function_args, middleware_trace, _execution_blocked, _execution_dispatched = _managed_values(_run_agent_tool_execution_middleware( + agent, + function_name=function_name, + function_args=function_args, + effective_task_id=effective_task_id, + tool_call_id=getattr(tool_call, "id", "") or "", + execute=_execute, + scope_block=_ts_scope_block, + display_index=i, + )) + tool_duration = time.time() - tool_start_time + if agent._should_emit_quiet_tool_messages(): + agent._vprint(f" {_get_cute_tool_message_impl('drive_preview', function_args, tool_duration, result=function_result)}") + elif function_name == "annotate_preview": + def _execute(next_args: dict) -> Any: + from tools.annotate_preview_tool import annotate_preview_tool as _annotate_preview_tool + return _annotate_preview_tool( + action=next_args.get("action", "add"), + ref=next_args.get("ref"), + selector=next_args.get("selector"), + label=next_args.get("label"), + callback=getattr(agent, "drive_preview_callback", None), + ) + function_result, function_args, middleware_trace, _execution_blocked, _execution_dispatched = _managed_values(_run_agent_tool_execution_middleware( + agent, + function_name=function_name, + function_args=function_args, + effective_task_id=effective_task_id, + tool_call_id=getattr(tool_call, "id", "") or "", + execute=_execute, + scope_block=_ts_scope_block, + display_index=i, + )) + tool_duration = time.time() - tool_start_time + if agent._should_emit_quiet_tool_messages(): + agent._vprint(f" {_get_cute_tool_message_impl('annotate_preview', function_args, tool_duration, result=function_result)}") elif function_name == "read_window_below": def _execute(next_args: dict) -> Any: from tools.read_window_tool import read_window_below_tool as _read_window_below_tool @@ -2569,6 +2644,7 @@ def execute_tool_calls_sequential(agent, assistant_message, messages: list, effe function_args, function_result, failed=_is_error_result, + tool_call_id=getattr(tool_call, "id", "") or "", ) result_preview = function_result if agent.verbose_logging else ( function_result[:200] if len(function_result) > 200 else function_result @@ -2607,6 +2683,7 @@ def execute_tool_calls_sequential(agent, assistant_message, messages: list, effe env=get_active_env(effective_task_id), config=_tool_budget, ) if not _is_multimodal_tool_result(function_result) else function_result + _record_persisted_path_for_stub(agent, tool_call.id, function_result) # Discover subdirectory context files from tool arguments subdir_hints = agent._subdirectory_hints.check_tool_call(function_name, function_args) diff --git a/agent/tool_guardrails.py b/agent/tool_guardrails.py index 6e6a9cd8f5..e4afbbba82 100644 --- a/agent/tool_guardrails.py +++ b/agent/tool_guardrails.py @@ -59,6 +59,52 @@ MUTATING_TOOL_NAMES = frozenset( } ) +# Tools that are legitimately re-invoked with identical arguments and may +# legitimately return an unchanged result while waiting on external progress — +# background-process management and job pollers. The identical-call loop +# notice (agent.stall_guards) never fires for these, so polling patterns like +# ``process(action="poll")`` or repeatedly checking a generation job stay +# unannotated. +STALL_GUARD_REPEATABLE_TOOLS = frozenset( + { + "process", + "bfl_flux3_get_result", + } +) + +# Poller naming conventions (e.g. ``_get_result``) used by generated / +# MCP tool surfaces. Matched as suffixes so vendor-prefixed pollers are exempt +# without enumerating every vendor. +_STALL_GUARD_REPEATABLE_SUFFIXES = ( + "_get_result", + "_poll", +) + +# The notice fires on the Nth consecutive identical call (same tool, same +# canonical args, same result). 3 tolerates one legitimate double-check while +# catching the observed re-issue loops (3x/4x identical calls in eval traces). +STALL_GUARD_IDENTICAL_CALL_THRESHOLD = 3 + +# Result-reference stubbing (agent.stall_guards): from the 2nd consecutive +# identical call whose FRESH result is byte-identical to the previous one, +# the duplicate payload is replaced in context by a short reference stub. +# Results under this size aren't worth stubbing (the stub itself plus the +# lost locality outweigh the savings), and error results are never stubbed +# (the model must see every fresh error verbatim). +IDENTICAL_RESULT_STUB_MIN_CHARS = 512 + +# How much of the canonical args JSON the stub carries so the model still +# knows WHAT the referenced call was even if context compression later +# evicts the referenced result (cheap dangling-reference mitigation). +_RESULT_STUB_ARGS_PREVIEW_CHARS = 120 + + +def is_stall_guard_repeatable(tool_name: str) -> bool: + """Whether a tool is exempt from the identical-call loop notice.""" + if tool_name in STALL_GUARD_REPEATABLE_TOOLS: + return True + return tool_name.endswith(_STALL_GUARD_REPEATABLE_SUFFIXES) + @dataclass(frozen=True) class ToolCallGuardrailConfig: @@ -173,6 +219,21 @@ class LoopCapConfig: ) +@dataclass(frozen=True) +class IdenticalCallObservation: + """Outcome of observing one completed tool call for the stall guards. + + ``notice`` is the identical-call loop-breaker notice (appended after the + result). ``stub`` is the result-reference replacement for a byte-identical + duplicate result (replaces the result content). Both may be set on the + same call (3rd+ identical call): the stub replaces the payload and the + notice is appended after it. + """ + + notice: str | None = None + stub: str | None = None + + @dataclass(frozen=True) class ToolCallSignature: """Stable, non-reversible identity for a tool name plus canonical args.""" @@ -282,6 +343,26 @@ class ToolCallGuardrailController: self._same_tool_failure_counts: dict[str, int] = {} self._no_progress: dict[ToolCallSignature, tuple[str, int]] = {} self._halt_decision: ToolGuardrailDecision | None = None + # Identical-call loop-breaker state (agent.stall_guards): tracks the + # CONSECUTIVE streak of identical (tool, canonical args) calls whose + # results were also identical. Any different call — or a different + # result — resets the streak, so legitimate re-reads after edits and + # varied polling are never flagged. Per-turn, like everything else here. + # NOTE: open PR #85352 (patrykkopycinski) tracks no-progress loops + # ACROSS turns via a detection window — a different mechanism from + # this per-turn consecutive streak. Coordinate future work there. + self._identical_streak_sig: ToolCallSignature | None = None + self._identical_streak_result_hash: str = "" + self._identical_streak_count: int = 0 + # tool_call_id of the FIRST call in the current streak, so a + # result-reference stub can point at the message that carries the + # full payload. + self._identical_streak_first_call_id: str = "" + # tool_call_id -> spillover file path for results that were persisted + # out of context (persisted-output preview). Lets a reference stub + # carry the file path so the reference can't dangle when the first + # occurrence entered context as a preview. + self._persisted_result_paths: dict[str, str] = {} # Per-turn runaway-loop cap counters. Reset every turn (this method # runs at the start of each run_conversation), so the caps bound a # single agent loop rather than accumulating across the session. @@ -444,6 +525,138 @@ class ToolCallGuardrailController: return False return tool_name in self.config.idempotent_tools + def observe_identical_call( + self, + tool_name: str, + args: Mapping[str, Any] | None, + result: str | None, + ) -> str | None: + """Track consecutive identical calls; return a loop-breaker notice or None. + + Back-compat wrapper around :meth:`observe_call` for callers that only + care about the loop-breaker notice. + """ + return self.observe_call(tool_name, args, result).notice + + def observe_call( + self, + tool_name: str, + args: Mapping[str, Any] | None, + result: str | None, + *, + tool_call_id: str = "", + failed: bool = False, + ) -> "IdenticalCallObservation": + """Track consecutive identical calls; return notice + dedupe stub info. + + Two independent outputs from the same consecutive-streak tracker: + + - ``notice``: the compact loop-breaker notice, fired when the SAME + tool is called with identical canonical arguments AND returns an + identical result for the ``STALL_GUARD_IDENTICAL_CALL_THRESHOLD``-th + (and every subsequent) consecutive time within the turn. Purely + observational — never blocks the call. Allowlisted pollers + (``is_stall_guard_repeatable``) are exempt from the NOTICE. + - ``stub``: a short reference replacement for the CURRENT result, + produced from the 2nd consecutive identical call whose fresh result + is byte-identical to the previous one. The tool still executed — + only the context representation is deduplicated, so polling + semantics are preserved (a changed result flows through whole and + resets the streak). Pollers are NOT exempt from stubbing: for a + poller, an identical result means nothing changed, which is exactly + when the stub saves the most context and loses nothing. Results + under ``IDENTICAL_RESULT_STUB_MIN_CHARS`` and failed/error results + are never stubbed, and only plain-string results are considered. + + Any intervening different call or changed result resets the streak. + Callers substitute/append at tool RESULT construction time, which is + cache-safe: tool results are append-only and never mutate + already-sent context. + """ + is_plain_str = isinstance(result, str) + signature = ToolCallSignature.from_call(tool_name, _coerce_args(args)) + result_hash = _result_hash(result) if is_plain_str else "" + + if ( + is_plain_str + and self._identical_streak_sig == signature + and self._identical_streak_result_hash == result_hash + ): + self._identical_streak_count += 1 + else: + # New streak (or non-string result, which never forms a streak — + # multimodal content lists pass through untouched). + self._identical_streak_sig = signature if is_plain_str else None + self._identical_streak_result_hash = result_hash + self._identical_streak_count = 1 if is_plain_str else 0 + self._identical_streak_first_call_id = tool_call_id or "" + + count = self._identical_streak_count + + notice = None + if ( + not is_stall_guard_repeatable(tool_name) + and count >= STALL_GUARD_IDENTICAL_CALL_THRESHOLD + ): + ordinal = f"{count}{'th' if 11 <= count % 100 <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(count % 10, 'th')}" + notice = ( + f"[hermes note: this is the {ordinal} consecutive identical call to " + f"{tool_name} with identical arguments returning the same result. " + "Do not repeat it — change arguments, use a different tool, or " + "proceed with what you have.]" + ) + + stub = None + if ( + is_plain_str + and count >= 2 + and not failed + and len(result) >= IDENTICAL_RESULT_STUB_MIN_CHARS + ): + stub = self._build_result_reference_stub(tool_name, args) + + return IdenticalCallObservation(notice=notice, stub=stub) + + def record_persisted_result(self, tool_call_id: str, file_path: str) -> None: + """Remember the spillover path a persisted result was saved to. + + When the first occurrence of a result entered context as a + persisted-output preview, a later reference stub must carry the + spillover file path so the reference can't dangle. + """ + if tool_call_id and file_path: + self._persisted_result_paths[tool_call_id] = file_path + + def _build_result_reference_stub( + self, tool_name: str, args: Mapping[str, Any] | None + ) -> str: + """Build the reference stub replacing a byte-identical duplicate result. + + Carries the tool name + a canonical-args preview so that even if + context compression later evicts the referenced result, the model + still knows WHAT the call was (cheap dangling-reference mitigation). + """ + try: + args_preview = canonical_tool_args(_coerce_args(args)) + except TypeError: + args_preview = "{}" + if len(args_preview) > _RESULT_STUB_ARGS_PREVIEW_CHARS: + args_preview = args_preview[:_RESULT_STUB_ARGS_PREVIEW_CHARS] + "…" + first_id = self._identical_streak_first_call_id + ref = f" (tool_call_id {first_id})" if first_id else "" + stub = ( + f"[hermes note: this result is byte-identical to the {tool_name} " + f"result earlier this turn{ref}. Refer to that result; it has not " + f"changed. Args: {args_preview}]" + ) + spill_path = self._persisted_result_paths.get(first_id) if first_id else None + if spill_path: + stub += ( + f"\n[The referenced result was persisted to: {spill_path} — " + "page through it with read_file if you need the full content.]" + ) + return stub + def _check_loop_cap( self, tool_name: str, diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.py index 939ef6f446..bec0f9a82b 100644 --- a/agent/transports/chat_completions.py +++ b/agent/transports/chat_completions.py @@ -13,6 +13,15 @@ import json from typing import Any, Dict from agent.lmstudio_reasoning import resolve_lmstudio_effort +from agent.reasoning_effort import ( + KIMI_K3_EFFORTS, + KIMI_K3_OVERRIDES, + OPENAI_COMPAT_WIRE_EFFORTS, + TOKENHUB_EFFORTS, + clamp_effort, + kimi_supported_efforts, + requested_effort, +) from agent.moonshot_schema import is_moonshot_model, sanitize_moonshot_tools from agent.prompt_builder import DEVELOPER_ROLE_MODELS from agent.transports.base import ProviderTransport @@ -84,15 +93,25 @@ def _add_prompt_cache_key( def _reasoning_config_for_model(model: str, reasoning_config: dict | None) -> dict | None: - """Return the model's wire-compatible reasoning config.""" + """Return the model's wire-compatible reasoning config. + + Hermes' internal effort set extends the wire vocabulary with ``ultra`` + (the /reasoning command documents none..xhigh|max|ultra). OpenAI- + compatible wires — OpenRouter chief among them — accept exactly + max|xhigh|high|medium|low|minimal|none and reject the extension with + HTTP 400 (#89503). Clamp against the declared wire vocabulary via the + shared policy in ``agent.reasoning_effort``; provider profiles with + narrower sets clamp again downstream. + """ if not isinstance(reasoning_config, dict): return reasoning_config - if ( - "gpt-5.6" in (model or "").lower() - and str(reasoning_config.get("effort") or "").strip().lower() == "ultra" - ): + effort = str(reasoning_config.get("effort") or "").strip().lower() + if not effort: + return reasoning_config + clamped = clamp_effort(effort, OPENAI_COMPAT_WIRE_EFFORTS) + if clamped != effort: normalized = dict(reasoning_config) - normalized["effort"] = "max" + normalized["effort"] = clamped return normalized return reasoning_config @@ -559,11 +578,22 @@ class ChatCompletionsTransport(ProviderTransport): and reasoning_config.get("enabled") is False ) if not _kimi_thinking_off: - _kimi_effort = "medium" - if reasoning_config and isinstance(reasoning_config, dict): - _e = (reasoning_config.get("effort") or "").strip().lower() - if _e in {"low", "medium", "high"}: - _kimi_effort = _e + # Kimi vocabularies are declared in agent.reasoning_effort: + # K3 = low/high/max (with the vendor-documented medium→high, + # xhigh→max rounding), K2-era = low/medium/high. Default when + # no effort was requested: K3's server default is high, + # K2-era's is medium. + _supported = kimi_supported_efforts(model) + _overrides = ( + KIMI_K3_OVERRIDES if _supported is KIMI_K3_EFFORTS else None + ) + _e = requested_effort(reasoning_config) + if _e is None: + _kimi_effort = ( + "high" if _supported is KIMI_K3_EFFORTS else "medium" + ) + else: + _kimi_effort = clamp_effort(_e, _supported, _overrides) api_kwargs["reasoning_effort"] = _kimi_effort # Tencent TokenHub: top-level reasoning_effort (unless thinking disabled) @@ -574,11 +604,13 @@ class ChatCompletionsTransport(ProviderTransport): and reasoning_config.get("enabled") is False ) if not _tokenhub_thinking_off: - _tokenhub_effort = "high" - if reasoning_config and isinstance(reasoning_config, dict): - _e = (reasoning_config.get("effort") or "").strip().lower() - if _e in {"low", "medium", "high"}: - _tokenhub_effort = _e + # TokenHub accepts low/medium/high (declared in + # agent.reasoning_effort); default high when no effort was + # requested. + _e = requested_effort(reasoning_config) + _tokenhub_effort = ( + "high" if _e is None else clamp_effort(_e, TOKENHUB_EFFORTS) + ) api_kwargs["reasoning_effort"] = _tokenhub_effort # LM Studio: top-level reasoning_effort. Only emit when the model diff --git a/agent/transports/codex.py b/agent/transports/codex.py index 9a866225b0..02e7390c63 100644 --- a/agent/transports/codex.py +++ b/agent/transports/codex.py @@ -27,6 +27,13 @@ def _cache_scope_from_session_id(session_id: Optional[str]) -> str: match = _CRON_SESSION_ID_RE.match(sid) return match.group(1) if match else sid +from agent.reasoning_effort import ( + ACTUAL_RELAY_EFFORTS, + XAI_GROK46_EFFORTS, + XAI_LEGACY_EFFORTS, + clamp_effort, + codex_supported_efforts, +) from agent.transports.base import ProviderTransport from agent.transports.types import NormalizedResponse, ToolCall @@ -432,30 +439,31 @@ class ResponsesApiTransport(ProviderTransport): elif reasoning_config.get("effort"): reasoning_effort = reasoning_config["effort"] - _effort_clamp = {"minimal": "low"} - if "gpt-5.6" in (model or "").lower(): - # Ultra is the Codex product tier; the Responses API wire value is max. - _effort_clamp["ultra"] = "max" + # Wire vocabularies are declared in agent.reasoning_effort; the shared + # clamp policy (nearest weaker supported level, never escalate, + # never invert the ladder) replaces the per-backend hand maps that + # repeatedly leaked internal levels like "ultra" to the wire + # (#89503 class) or clamped one rung below a model's real ceiling + # (#87279). if params.get("is_xai_responses", False): from agent.model_metadata import is_grok_46_family - # Grok 4.6 accepts xhigh as a wire value; older Grok models top - # out at high. max/ultra are Hermes ladder aliases for "this - # model's ceiling", so they clamp to the strongest level the - # model actually accepts — xhigh on grok-4.6, high elsewhere — - # never one rung below it (#87279). - if is_grok_46_family(model): - _effort_clamp.update({"max": "xhigh", "ultra": "xhigh"}) - else: - _effort_clamp["xhigh"] = "high" - _effort_clamp.update({"max": "high", "ultra": "high"}) - if (params.get("provider") or "").strip().lower() == "actual": - # Actual Computer relays to SGLang/vLLM backends that accept only - # none/low/medium/high/max for reasoning effort — a forwarded - # xhigh/ultra fails with a wrapped HTTP 400 ("Expecting value: - # line 1 column 1"). Clamp Hermes' wider set to the supported one. - _effort_clamp.update({"xhigh": "high", "ultra": "max"}) - reasoning_effort = _effort_clamp.get(reasoning_effort, reasoning_effort) + # Grok 4.6 accepts xhigh as a wire value; older Grok tops out + # at high. + _supported = ( + XAI_GROK46_EFFORTS if is_grok_46_family(model) + else XAI_LEGACY_EFFORTS + ) + elif (params.get("provider") or "").strip().lower() == "actual": + # Actual Computer relays to SGLang/vLLM backends: + # none/low/medium/high/max. + _supported = ACTUAL_RELAY_EFFORTS + else: + # OpenAI/Codex Responses backend — per-model vocabulary + # (live-verified: "max" is gpt-5.6-only, "minimal" always + # rejected). #68365 premise confirmed. + _supported = codex_supported_efforts(model) + reasoning_effort = clamp_effort(reasoning_effort, _supported) response_tools = _responses_tools(tools) diff --git a/agent/turn_context.py b/agent/turn_context.py index a90bee9a1a..df8969a666 100644 --- a/agent/turn_context.py +++ b/agent/turn_context.py @@ -605,6 +605,15 @@ def build_turn_context( # NOTE: _turns_since_memory and _iters_since_skill are NOT reset here. agent.iteration_budget = IterationBudget(agent.max_iterations) + # Wall-clock run budget: per-run_conversation clock. Only stamped when a + # budget is configured so the default path stays clock-free; the wrap-up + # latch resets each turn (one notice per run, not per session). + if getattr(agent, "run_budget_seconds", None): + agent._run_budget_started_at = time.time() + else: + agent._run_budget_started_at = None + agent._run_budget_wrapup_injected = False + # Log conversation turn start for debugging/observability. _preview_text = summarize_user_message_for_log(user_message) _msg_preview = (_preview_text[:80] + "...") if len(_preview_text) > 80 else _preview_text @@ -1154,6 +1163,57 @@ def build_turn_context( agent._last_content_with_tools = None agent._last_content_tools_all_housekeeping = False agent._mute_post_response = False + elif not agent.compression_enabled: + # Uncompressed session guard (#89297): when compression is explicitly + # disabled, sessions can grow past the model's context window across + # hundreds of messages with nothing to shrink them. The warning itself + # fires from the conversation loop's pre-API site, which reuses the + # unconditionally computed request estimate at zero marginal cost and + # covers both turn-start and mid-turn growth (every provider request + # passes through it). Here we only RE-ARM the dedup once the session + # is back under the window, so the guard can warn again after the + # user compacts (/compress with force=True works with compression + # disabled) and the context later regrows past the limit. + _ctx_len = getattr( + getattr(agent, "context_compressor", None), "context_length", None + ) + if isinstance(_ctx_len, int) and _ctx_len > 0: + _raw_chars = 0 + for _m in messages: + if not isinstance(_m, dict): + continue + _c = _m.get("content") + if isinstance(_c, str): + _raw_chars += len(_c) + elif _c: + # Non-string, non-empty content (multimodal part lists, + # dict payloads) defeats a char count — force the real + # estimate by treating it as over-gate. None/"" (routine + # assistant tool-call rows) contribute nothing. + _raw_chars = _ctx_len + 1 + break + # Cheap gate: a session whose raw text is under ~1/4 of the + # window (4 chars/token upper bound) cannot be over it — skip + # the estimator. Non-string (multimodal) content defeats a char + # count, so any such message forces the real estimate. + if _raw_chars <= _ctx_len: + _clear_warn = getattr( + agent, "_clear_context_overflow_warn", None + ) + if callable(_clear_warn): + _clear_warn() + else: + _uncompressed_tokens = estimate_request_tokens_rough( + messages, + system_prompt=active_system_prompt or "", + tools=agent.tools or None, + ) + if _uncompressed_tokens <= _ctx_len: + _clear_warn = getattr( + agent, "_clear_context_overflow_warn", None + ) + if callable(_clear_warn): + _clear_warn() if _preflight_compressed: # Compression rebuilt the list (tail messages are fresh compaction diff --git a/agent/video_gen_registry.py b/agent/video_gen_registry.py index ff46b087c0..5b4c9cea54 100644 --- a/agent/video_gen_registry.py +++ b/agent/video_gen_registry.py @@ -132,6 +132,18 @@ def get_active_provider() -> Optional[VideoGenProvider]: except Exception as exc: logger.debug("Could not read video_gen.provider from config: %s", exc) + # The managed "Nous Subscription" selection is serviced by the FAL + # plugin through the managed fal-queue gateway (the plugin's resolver + # routes managed when the stored selection is "nous"). + if configured: + try: + from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER + + if configured.lower() == NOUS_MANAGED_PROVIDER: + configured = "fal" + except Exception: # pragma: no cover — helpers are in-repo + pass + with _lock: snapshot = dict(_providers) snapshot.update(_scoped_providers.get(hermes_home_key(), {})) diff --git a/agent/web_search_provider.py b/agent/web_search_provider.py index e0f7ea1f1d..0f7f706c86 100644 --- a/agent/web_search_provider.py +++ b/agent/web_search_provider.py @@ -126,6 +126,22 @@ class WebSearchProvider(abc.ABC): """Return True if this provider implements :meth:`search`.""" return True + def is_keyless_available(self) -> bool: + """Return True when this provider can serve calls WITHOUT credentials. + + A separate, weaker tier than :meth:`is_available`: providers with a + public anonymous free tier (Exa / Parallel MCP endpoints) return + True here so the registry can fall back to them when NO provider is + configured or keyed — and only then. Keyless availability must never + make :meth:`is_available` return True, or the legacy preference walk + would route users with real credentials for a lower-priority backend + onto the free tier of a higher-priority one. + + Like :meth:`is_available`, this must be cheap and must NOT make + network calls. Default: False. + """ + return False + def supports_extract(self) -> bool: """Return True if this provider implements :meth:`extract`. diff --git a/agent/web_search_registry.py b/agent/web_search_registry.py index 2e0c116ec0..112fadc6df 100644 --- a/agent/web_search_registry.py +++ b/agent/web_search_registry.py @@ -166,6 +166,44 @@ _LEGACY_PREFERENCE = ( "ddgs", ) +# Keyless free-tier walk — strictly LAST-resort, tried only after the +# availability-filtered legacy walk finds nothing (i.e. the user has zero +# web credentials and no importable ddgs). All five vendors expose public +# anonymous free tiers (see plugins/web/keyless_mcp.py). Unpinned keyless +# traffic round-robins across the ring per request (the ring cursor lives +# in keyless_mcp; an explicit `hermes tools` pick bypasses this walk +# entirely, and rate-limited requests fail over to the next ring vendor). +# Disable the tier with ``web.keyless_fallback: false``. +_KEYLESS_PREFERENCE = ( + "exa", + "parallel", + "tavily", + "firecrawl", + "keenable", +) + + +def _keyless_preference() -> tuple: + """Return the keyless walk order for resolution. + + Delegates the entry-vendor choice to the ring cursor in + :mod:`plugins.web.keyless_mcp` (round-robin per request, seeded by the + per-process random session id) so resolution and dispatch agree on + which vendor a fresh install starts at. The remaining vendors follow + in ring order as fallbacks for registration gaps. + """ + try: + from plugins.web.keyless_mcp import _KEYLESS_RING, _ring_cursor + + start = _ring_cursor % len(_KEYLESS_RING) + return tuple( + _KEYLESS_RING[(start + i) % len(_KEYLESS_RING)] + for i in range(len(_KEYLESS_RING)) + ) + except Exception as exc: # noqa: BLE001 — ring optional in stripped envs + logger.debug("keyless ring order unavailable: %s", exc) + return _KEYLESS_PREFERENCE + def _resolve(configured: Optional[str], *, capability: str) -> Optional[WebSearchProvider]: """Resolve the active provider for a capability ("search" | "extract"). @@ -254,9 +292,39 @@ def _resolve(configured: Optional[str], *, capability: str) -> Optional[WebSearc ): return provider + # 4. Keyless free-tier walk — the user has NO credentialed/importable + # backend at all. Fall back to providers that can serve anonymously + # (public MCP free tiers), unless disabled via + # ``web.keyless_fallback: false``. This tier never pre-empts a keyed + # setup: it is only reachable when the legacy walk found nothing. + if _keyless_tier_enabled(): + for name in _keyless_preference(): + provider = snapshot.get(name) + if provider is None or not _capable(provider): + continue + try: + if provider.is_keyless_available(): + return provider + except Exception as exc: # noqa: BLE001 — buggy provider skipped + logger.debug( + "provider %s.is_keyless_available() raised %s", name, exc + ) + return None +def _keyless_tier_enabled() -> bool: + """Read ``web.keyless_fallback`` from config.yaml (default: enabled).""" + try: + from hermes_cli.config import load_config + + web_cfg = load_config().get("web") or {} + return bool(web_cfg.get("keyless_fallback", True)) + except Exception as exc: # noqa: BLE001 — config layer optional + logger.debug("keyless_fallback config read failed: %s", exc) + return True + + def _disabled_web_plugin_for(configured: Optional[str] = None, *, capability: Optional[str] = None) -> Optional[str]: """Return the plugin key of a *disabled* bundled web plugin that would have provided the configured backend, or None. diff --git a/apps/desktop/DESIGN.md b/apps/desktop/DESIGN.md index e2f3614022..8d4512e65f 100644 --- a/apps/desktop/DESIGN.md +++ b/apps/desktop/DESIGN.md @@ -151,6 +151,12 @@ Notes: - SVGs inherit `size-3.5` (`size-3` at `xs`). Don't re-set icon size. - Polymorph with `asChild` when the button must render as a link/Slot. +## Badges — one component + +`src/components/ui/badge.tsx`. Variants: `default` (tinted primary), `muted`, +`warn`, `destructive`, `outline`, `solid` (primary fill — icon-corner counts). +Sizes: `default`, `xs`, `overlay` (titlebar glyph counts). + ## Form controls - **`controlVariants`** (`src/components/ui/control.ts`) is the shared shape for @@ -190,6 +196,15 @@ Notes: - **Empty:** `EmptyState` for plain page bodies; `PanelEmpty` for overlay master/detail empties with an icon and action. Don't hand-roll a third centered empty. +- **Confirmation:** `ConfirmDialog` is the only way we ask "are you sure". It + opens focused on Confirm, so `Enter` confirms and `Esc` cancels, and it owns + the pending → done → close beat and the inline error — a call site passes an + async `onConfirm` and nothing else. A third way out (e.g. "Remove from + sidebar" beside "Delete worktree") goes in the one `secondaryAction` slot. + Never `window.confirm`: it's an unstyled blocking Chromium modal. A handler + that wants the answer inline instead of a mounted dialog calls `confirm()` + from `src/store/confirm.ts`, which renders this same primitive through the + single `ConfirmHost` at the shell — the way `notify()` backs notifications. ## Chat, tools & boot surfaces @@ -315,7 +330,8 @@ The detailed state contract lives in the scoped ## Before you add something — checklist - [ ] Reuse a primitive (`Button`, `SearchField`, `SegmentedControl`, - `ListRow`, `Loader`, `ErrorState`, `LogView`) instead of forking one? + `ListRow`, `Loader`, `ErrorState`, `LogView`, `ConfirmDialog`) instead of + forking one? - [ ] Tokens (`--ui-*`, `shadow-nous`, `--stroke-nous`) — zero raw colors / one-off shadows? - [ ] No `className` overriding a primitive's padding / size / radius / chrome? diff --git a/apps/desktop/electron/backend-ownership.test.ts b/apps/desktop/electron/backend-ownership.test.ts index 98b03a3b5f..7910478a67 100644 --- a/apps/desktop/electron/backend-ownership.test.ts +++ b/apps/desktop/electron/backend-ownership.test.ts @@ -284,3 +284,72 @@ test('shutdown coordinator returns one promise and awaits teardown exactly once' assert.equal(finished, true) assert.equal(coordinator.run(), first) }) + +// #89298: a corrupt ownership file must never be silently rewritten as [] — +// that permanently erases the only record of still-running backends. The reap +// sweep quarantines the file and skips; a later healthy write recreates it. +test('reapOrphans on a corrupt file quarantines and does not rewrite', async () => { + let contents = '{ this is not json' + let quarantined = 0 + const writes: string[] = [] + const stopped: number[] = [] + + const store = { + read: () => contents, + value: () => contents, + write: (next: string) => { + writes.push(next) + contents = next + }, + quarantine: () => { + quarantined += 1 + } + } + + const ownership = createOwnership(store, { + stop: identityArg => { + stopped.push(identityArg.pid) + } + }) + + assert.deepEqual(await ownership.reapOrphans(), []) + assert.equal(quarantined, 1) + assert.deepEqual(writes, []) + assert.deepEqual(stopped, []) +}) + +test('reapOrphans on a corrupt file without a quarantine hook still skips the rewrite', async () => { + const writes: string[] = [] + + const store = { + read: () => 'garbage{{{', + value: () => 'garbage{{{', + write: (next: string) => { + writes.push(next) + } + } + + const ownership = createOwnership(store) + + assert.deepEqual(await ownership.reapOrphans(), []) + assert.deepEqual(writes, []) +}) + +test('an empty or missing ownership file is NOT corrupt — reap sweeps normally', async () => { + const writes: string[] = [] + + const store = { + read: () => '', + value: () => '', + write: (next: string) => { + writes.push(next) + }, + quarantine: () => assert.fail('empty file must not be quarantined') + } + + const ownership = createOwnership(store) + + assert.deepEqual(await ownership.reapOrphans(), []) + // Empty roster: rewriting [] is harmless and keeps the legacy behavior. + assert.equal(writes.length, 1) +}) diff --git a/apps/desktop/electron/backend-ownership.ts b/apps/desktop/electron/backend-ownership.ts index 32af3b2074..682dbe3f5b 100644 --- a/apps/desktop/electron/backend-ownership.ts +++ b/apps/desktop/electron/backend-ownership.ts @@ -16,6 +16,10 @@ export interface BackendOwnershipEntry extends BackendIdentity { export interface BackendOwnershipStore { read: () => string | null write: (contents: string) => void + /** Move an unreadable ownership file aside (e.g. rename to `.corrupt`) so + * its contents survive for inspection instead of being rewritten away. + * Optional: stores that can't quarantine simply skip the sweep. */ + quarantine?: () => void } export interface BackendOwnershipDeps { @@ -62,12 +66,30 @@ function identitiesMatch(left: BackendIdentity, right: BackendIdentity): boolean } export function parseBackendOwnership(contents: unknown): BackendOwnershipEntry[] { + return parseBackendOwnershipDetailed(contents).entries +} + +/** Parse result that distinguishes "empty/valid" from "unreadable". A corrupt + * ownership file must NOT read as an empty roster: `reapOrphans` rewrites the + * file with its survivors, so treating garbage as `[]` permanently erased the + * records of still-running backends — the exact shape of the #89298 report + * (ownership file gone, 28 leaked serve processes nothing will ever reap). */ +export function parseBackendOwnershipDetailed(contents: unknown): { + corrupt: boolean + entries: BackendOwnershipEntry[] +} { + const text = String(contents ?? '') + + if (!text.trim()) { + return { corrupt: false, entries: [] } + } + let parsed: unknown try { - parsed = JSON.parse(String(contents ?? '')) + parsed = JSON.parse(text) } catch { - return [] + return { corrupt: true, entries: [] } } const values = Array.isArray(parsed) @@ -109,7 +131,7 @@ export function parseBackendOwnership(contents: unknown): BackendOwnershipEntry[ } } - return entries + return { corrupt: false, entries } } export function serializeBackendOwnership(entries: BackendOwnershipEntry[]): string { @@ -123,7 +145,8 @@ export function serializeBackendOwnership(entries: BackendOwnershipEntry[]): str * cleanup before reporting failure to the caller. */ export function createBackendOwnership(deps: BackendOwnershipDeps) { - const read = () => parseBackendOwnership(deps.store.read()) + const readDetailed = () => parseBackendOwnershipDetailed(deps.store.read()) + const read = () => readDetailed().entries const write = (entries: BackendOwnershipEntry[]) => deps.store.write(serializeBackendOwnership(entries)) return { @@ -181,7 +204,22 @@ export function createBackendOwnership(deps: BackendOwnershipDeps) { }, async reapOrphans(): Promise { - const entries = read() + const { corrupt, entries } = readDetailed() + + // An unreadable ownership file yields zero parsed entries — rewriting + // survivors ([]) here would DESTROY the only record of any backends the + // corrupt file described, guaranteeing they leak forever (#89298). + // Preserve the evidence for inspection and skip the sweep. + if (corrupt) { + try { + deps.store.quarantine?.() + } catch { + // Quarantine is best-effort; the important part is not rewriting. + } + + return [] + } + const survivors: BackendOwnershipEntry[] = [] const reaped: number[] = [] diff --git a/apps/desktop/electron/backend-start-failure.test.ts b/apps/desktop/electron/backend-start-failure.test.ts index f535266f95..f612171f46 100644 --- a/apps/desktop/electron/backend-start-failure.test.ts +++ b/apps/desktop/electron/backend-start-failure.test.ts @@ -3,8 +3,10 @@ import assert from 'node:assert/strict' import { test } from 'vitest' import { + isHostKeyChangedBootFailure, isRetryableRemoteBootFailure, shouldLatchBackendStartFailure, + shouldLatchHostKeyChangedFailure, shouldLatchRemoteReauthFailure } from './backend-start-failure' @@ -81,3 +83,53 @@ test('retryable and reauth-latch are mutually exclusive for remote failures', () assert.equal(retry !== latch, true, `remote failure with reauth=${isReauth} must pick exactly one path`) } }) + +test('FIX host-key change: classified from the kind tag and from stringified ssh banners', () => { + // classifySshError tags the Error it built; errors that crossed an IPC or + // string boundary only keep the message. Both shapes must classify. + const tagged = Object.assign(new Error('SSH refused to connect.'), { kind: 'host-key-changed' }) + assert.equal(isHostKeyChangedBootFailure(tagged), true) + assert.equal( + isHostKeyChangedBootFailure(new Error('@@@@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @@@@')), + true + ) + assert.equal(isHostKeyChangedBootFailure(new Error('Host key verification failed.')), true) + assert.equal( + isHostKeyChangedBootFailure(new Error('The host key for root@203.0.113.7 has CHANGED since you last connected.')), + true + ) + assert.equal(isHostKeyChangedBootFailure(new Error('Connection refused')), false) + assert.equal(isHostKeyChangedBootFailure(null), false) +}) + +test('FIX host-key change: latches and is never auto-retried (157-failure loop, Aug 2026 bundle)', () => { + // SSH fails closed on a changed host key: every retry re-drives the same + // doomed boot until the user clears known_hosts. Terminal, like reauth. + const context = { attemptedRemote: true, isReauth: false, isHostKeyChanged: true } + assert.equal(shouldLatchHostKeyChangedFailure(context), true) + assert.equal(isRetryableRemoteBootFailure(context), false) +}) + +test('host-key latch never fires for local failures or ordinary remote faults', () => { + assert.equal( + shouldLatchHostKeyChangedFailure({ attemptedRemote: false, isReauth: false, isHostKeyChanged: true }), + false + ) + assert.equal( + shouldLatchHostKeyChangedFailure({ attemptedRemote: true, isReauth: false, isHostKeyChanged: false }), + false + ) + assert.equal(shouldLatchHostKeyChangedFailure({ attemptedRemote: true, isReauth: false }), false) +}) + +test('every remote failure picks exactly one path: retry, reauth latch, or host-key latch', () => { + for (const isReauth of [true, false]) { + for (const isHostKeyChanged of [true, false]) { + const retry = isRetryableRemoteBootFailure({ attemptedRemote: true, isReauth, isHostKeyChanged }) + const reauth = shouldLatchRemoteReauthFailure({ attemptedRemote: true, isReauth }) + const hostKey = shouldLatchHostKeyChangedFailure({ attemptedRemote: true, isReauth, isHostKeyChanged }) + const picked = [retry, reauth, hostKey].filter(Boolean).length + assert.ok(picked >= 1, `remote failure reauth=${isReauth} hostKey=${isHostKeyChanged} fell through every path`) + } + } +}) diff --git a/apps/desktop/electron/backend-start-failure.ts b/apps/desktop/electron/backend-start-failure.ts index efc4361637..9c89822397 100644 --- a/apps/desktop/electron/backend-start-failure.ts +++ b/apps/desktop/electron/backend-start-failure.ts @@ -79,6 +79,44 @@ export interface RemoteBootRetryContext { * never self-heal without the user signing in again. */ isReauth: boolean + /** + * True when SSH refused to connect because the host's key CHANGED + * (StrictHostKeyChecking fails closed). Retrying cannot succeed until the + * user verifies the change and removes the stale known_hosts entry, so this + * is terminal like a reauth rejection — not connectivity. + */ + isHostKeyChanged?: boolean +} + +/** + * A host-key-change refusal is identifiable both by the `kind` tag + * classifySshError puts on the error and — for errors that crossed a + * stringifying boundary — by the stable phrases ssh/our own message carry. + * One user hit 157 consecutive boot-retry failures over 2.5h against a + * reinstalled VPS (Aug 2026 bundle) because this was classified as transient. + */ +export function isHostKeyChangedBootFailure(error: unknown): boolean { + if ((error as { kind?: string } | null | undefined)?.kind === 'host-key-changed') { + return true + } + + const message = error instanceof Error ? error.message : String(error ?? '') + + return /REMOTE HOST IDENTIFICATION HAS CHANGED|Host key verification failed|host key for .+ has CHANGED/i.test( + message + ) +} + +/** + * Whether a failed remote boot should latch (into `backendStartFailure`) + * because the host key changed. Same rationale as the reauth latch: the + * failure cannot self-heal, and an unlatched terminal failure makes every + * recovery surface re-drive the identical doomed boot. The latch is released + * by the existing reset/repair/apply-config paths once the user has run + * `ssh-keygen -R `. + */ +export function shouldLatchHostKeyChangedFailure(context: RemoteBootRetryContext): boolean { + return context.attemptedRemote && context.isHostKeyChanged === true } /** @@ -93,9 +131,10 @@ export interface RemoteBootRetryContext { * only arms after a completed boot, so the app sat on "Desktop boot failed" * until the user manually re-entered the same connection details (which just * forced a fresh bootstrap). A missing capability differs from a transient - * failure: confirmed reauth rejections and local failures stay out of the - * retry path; everything else remote is connectivity and should retry. + * failure: confirmed reauth rejections, host-key changes, and local failures + * stay out of the retry path; everything else remote is connectivity and + * should retry. */ export function isRetryableRemoteBootFailure(context: RemoteBootRetryContext): boolean { - return context.attemptedRemote && !context.isReauth + return context.attemptedRemote && !context.isReauth && context.isHostKeyChanged !== true } diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.ts index c5ff57c9df..d357597a01 100644 --- a/apps/desktop/electron/connection-registry.test.ts +++ b/apps/desktop/electron/connection-registry.test.ts @@ -271,6 +271,28 @@ test('rememberSshEnumeration: live list wins, cache then seed default', () => { }) }) +test('rememberSshEnumeration: a bounced remote source keeps its last-known roster (4-bots-show-as-2)', () => { + // A VPS restart makes the remote source unreachable for a few polls. The + // last successful enumeration must keep painting so the roster does not + // silently drop that source's bots mid-outage. + assert.deepEqual( + rememberSshEnumeration({ profiles: null, error: 'unreachable' }, ['default', 'ceo', 'accounter'], 'remote'), + { profiles: ['default', 'ceo', 'accounter'], error: 'unreachable' } + ) + // Never-seen remote source: no seed — an unreachable URL is not evidence a + // backend exists there. + assert.deepEqual(rememberSshEnumeration({ profiles: null, error: 'unreachable' }, null, 'remote'), { + profiles: null, + error: 'unreachable' + }) + // Local enumeration failures never reuse a cache (the local runtime answers + // authoritatively or not at all). + assert.deepEqual(rememberSshEnumeration({ profiles: null, error: 'boom' }, ['default'], 'local'), { + profiles: null, + error: 'boom' + }) +}) + test('shouldRetrySshInventory: first try, cooldown, then retry; cache never retries', () => { assert.equal(shouldRetrySshInventory(false, null, 1_000), true) assert.equal(shouldRetrySshInventory(false, 1_000, 30_000, 60_000), false) diff --git a/apps/desktop/electron/connection-registry.ts b/apps/desktop/electron/connection-registry.ts index 5c75f583f9..5d7f2456de 100644 --- a/apps/desktop/electron/connection-registry.ts +++ b/apps/desktop/electron/connection-registry.ts @@ -341,10 +341,15 @@ export interface RosterAgent { } /** - * SSH roster enumeration skips undialed sources (connect-on-demand). Reuse the - * last successful profile list so Bot Mode does not go empty the moment the - * window switches back to local. Never-seen SSH sources still get a `default` - * seed so the device is clickable. + * Roster enumeration skips undialed sources (connect-on-demand) and reports + * unreachable ones with `profiles: null`. Reuse the last successful profile + * list so Bot Mode does not go empty (or drop to a partial roster) the moment + * a source is briefly unreachable — SSH tunnels drop on sleep/wake, and a + * remote gateway bounce (VPS restart) otherwise erased its bots from the + * roster until the next successful enumeration ("my 4 bots show as 2", Aug + * 2026 bundle). Never-seen SSH sources still get a `default` seed so the + * device is clickable; never-seen remote sources stay empty (no seed) since + * an unreachable URL is not evidence a backend exists there. */ export function rememberSshEnumeration( enumeration: Pick, @@ -355,7 +360,7 @@ export function rememberSshEnumeration( return enumeration } - if (kind !== 'ssh') { + if (kind === 'local') { return enumeration } @@ -363,7 +368,7 @@ export function rememberSshEnumeration( return { profiles: cached, error: enumeration.error } } - if (enumeration.error === 'connect-on-demand') { + if (kind === 'ssh' && enumeration.error === 'connect-on-demand') { return { profiles: ['default'], error: 'connect-on-demand' } } diff --git a/apps/desktop/electron/hud-ipc.ts b/apps/desktop/electron/hud-ipc.ts index 8a8b7cf000..3eff9c1488 100644 --- a/apps/desktop/electron/hud-ipc.ts +++ b/apps/desktop/electron/hud-ipc.ts @@ -4,15 +4,84 @@ // latch when handing the session back to the app window. import { type BrowserWindow, ipcMain } from 'electron' +import { hudFrostFor, type TranslucencyState } from './translucency' + export interface HudIpcDeps { isMac: boolean + isWindows: boolean + glassSupported: boolean + /** Main's authoritative translucency state (Settings → Appearance). */ + getTranslucencyState: () => TranslucencyState getHudWindow: () => BrowserWindow | null openHudWindow: (sessionId: null | string, profile: null | string) => void closeHudWindow: () => void setHudSessionId: (sessionId: null | string) => void } -export function registerHudIpc({ isMac, getHudWindow, openHudWindow, closeHudWindow, setHudSessionId }: HudIpcDeps) { +export function registerHudIpc({ + isMac, + isWindows, + glassSupported, + getTranslucencyState, + getHudWindow, + openHudWindow, + closeHudWindow, + setHudSessionId +}: HudIpcDeps) { + // Whether the band currently covers the window below the bar. The renderer + // is the only party that can know this (it measures the transcript), and it + // is half of the frost decision — the other half is the user's setting, + // which main owns. Latched so a Settings change can re-decide without + // waiting for the HUD to report again. + let bandShowing = false + let applied: null | string = null + let appliedTo: BrowserWindow | null = null + + // Real frosted glass behind the band — the thing CSS backdrop-filter cannot do, + // because Chromium composites a transparent window's page against nothing and + // the desktop is not in its backdrop root. The material IS the window's content + // view, so it frosts the whole rectangle; the HUD's layout leaves no dead + // margins for that reason, and it only turns on while the band is showing + // (idle HUD mode must be the bar and nothing else). + // + // Diffed before issuing: `setVibrancy` carries a 150ms animation that restarts + // if re-issued, so a repeated call would keep the material from ever settling + // (the same churn the chat windows' native-diff contract exists to prevent). + // + // The diff is keyed to the WINDOW as well as the value. A HUD respawn (the + // profile switch in openHudWindow destroys and rebuilds it) hands back a + // fresh window carrying no material, and a latch that only remembered the + // value would recognise its own last answer and skip — leaving the new HUD + // unfrosted until something else happened to change the signature. + const applyHudFrost = () => { + const hudWindow = getHudWindow() + + if (!hudWindow || hudWindow.isDestroyed()) { + applied = null + appliedTo = null + + return + } + + const frost = hudFrostFor(getTranslucencyState(), bandShowing) + const signature = `${frost.vibrancy ?? 'off'}:${frost.backgroundMaterial}` + + if (applied === signature && appliedTo === hudWindow) { + return + } + + applied = signature + appliedTo = hudWindow + + if (isMac && typeof hudWindow.setVibrancy === 'function') { + hudWindow.setVibrancy(frost.vibrancy) + } + + if (isWindows && glassSupported && typeof hudWindow.setBackgroundMaterial === 'function') { + hudWindow.setBackgroundMaterial(frost.backgroundMaterial) + } + } + ipcMain.handle('hermes:hud:open', async (_event, request) => { openHudWindow( typeof request?.sessionId === 'string' ? request.sessionId : null, @@ -22,18 +91,9 @@ export function registerHudIpc({ isMac, getHudWindow, openHudWindow, closeHudWin return { ok: true } }) - // Real frosted glass behind the band — the thing CSS backdrop-filter cannot do, - // because Chromium composites a transparent window's page against nothing and - // the desktop is not in its backdrop root. Vibrancy IS the window's content - // view, so it frosts the whole rectangle; the HUD's layout leaves no dead - // margins for that reason, and the renderer only turns it on while the band is - // showing (idle HUD mode must be the bar and nothing else). - ipcMain.handle('hermes:hud:vibrancy', (_event, on) => { - const hudWindow = getHudWindow() - - if (hudWindow && !hudWindow.isDestroyed() && isMac) { - hudWindow.setVibrancy(on ? 'hud' : null) - } + ipcMain.handle('hermes:hud:frost', (_event, showing) => { + bandShowing = Boolean(showing) + applyHudFrost() return { ok: true } }) @@ -125,4 +185,8 @@ export function registerHudIpc({ isMac, getHudWindow, openHudWindow, closeHudWin return { ok: true } }) + + // Main re-applies the frost when the translucency SETTING changes, since the + // band's own report only fires when the band itself moves. + return { applyHudFrost } } diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 0cada4507e..c03190e050 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -46,8 +46,10 @@ import { } from './backend-probes' import { waitForDashboardPortAnnouncement } from './backend-ready' import { + isHostKeyChangedBootFailure, isRetryableRemoteBootFailure, shouldLatchBackendStartFailure, + shouldLatchHostKeyChangedFailure, shouldLatchRemoteReauthFailure } from './backend-start-failure' import { @@ -188,6 +190,7 @@ import { createMediaProtocolHandler, MEDIA_PROTOCOL } from './media-protocol' import { oauthGuardMayHardFail, oauthSessionIsLive, + resolveGatedDownloadAuth, resolveJsonBody, resolveOauthRestAuth, resolveReadinessProbeAuth @@ -256,6 +259,7 @@ import { import { missingRendererAssets } from './renderer-bundle' import { attachRendererConsoleCapture, formatRendererBoundaryReport } from './renderer-log' import { + buildInstanceWindowUrl, buildSessionWindowUrl, chatWindowWebPreferences, createSessionWindowRegistry, @@ -272,6 +276,7 @@ import { registerTerminalIpc } from './terminal-ipc' import { nativeOverlayWidth as computeNativeOverlayWidth, macTitleBarOverlayHeight } from './titlebar-overlay-width' import { backgroundMaterialFor, + defaultTranslucencyState, glassActive, glassSupportedOn, normalizeState as normalizeTranslucency, @@ -877,7 +882,12 @@ function readPersistedTranslucency() { try { return normalizeTranslucency(JSON.parse(fs.readFileSync(TRANSLUCENCY_CONFIG_PATH, 'utf8')), GLASS_SUPPORTED) } catch { - return normalizeTranslucency(null, GLASS_SUPPORTED) + // Nothing persisted yet — a first launch. Glass ships on, so the FIRST + // window has to be created with the glass backing already: a window born + // opaque cannot reliably be swapped to glass afterwards (see + // windowBackingOptions). nativeTheme is the only appearance signal main + // has this early; the renderer's first resolved send corrects it. + return defaultTranslucencyState(nativeTheme.shouldUseDarkColors ? 'dark' : 'light', GLASS_SUPPORTED, IS_WINDOWS) } } @@ -3260,7 +3270,20 @@ const backendOwnership = createBackendOwnership({ return null } }, - write: writeBackendOwnership + write: writeBackendOwnership, + // A corrupt ownership file is moved aside instead of being rewritten + // away by the reap sweep — its records are the only pointer to any + // still-running backends it described (#89298). + quarantine: () => { + const parked = `${DESKTOP_BACKEND_OWNERSHIP_PATH}.corrupt` + + try { + fs.renameSync(DESKTOP_BACKEND_OWNERSHIP_PATH, parked) + rememberLog(`Backend ownership file was unreadable; moved to ${parked}`) + } catch { + // Nothing to move (or no permission) — the sweep already skipped. + } + } } }) @@ -4859,7 +4882,8 @@ function fetchJson(url, token, options: any = {}) { // Token-auth download that streams the response body straight to a // user-selected destination (via finalizeGatewayDownload) instead of buffering // the whole file in memory. The connect timeout is cleared once headers arrive -// so a slow save dialog or a large stream doesn't trip it. +// so a slow save dialog or a large stream doesn't trip it. `options.bearer` +// switches the header to Authorization (RFC 8252 native flow), matching fetchJson. function downloadViaTokenToFile(url, token, ctx, options: any = {}) { return new Promise((resolve, reject) => { let parsed @@ -4885,9 +4909,7 @@ function downloadViaTokenToFile(url, token, ctx, options: any = {}) { parsed, { method: 'GET', - headers: { - 'X-Hermes-Session-Token': token - } + headers: options.bearer ? { Authorization: `Bearer ${options.bearer}` } : { 'X-Hermes-Session-Token': token } }, res => { // Headers arrived — the connection phase is done. Drop the idle timeout @@ -7251,6 +7273,13 @@ function readGatewayErrorText(res): Promise { }) } +async function gatedFileAuth(connection) { + const nativeAt = + connection.authMode === 'oauth' ? await ensureNativeAccessToken(connection.baseUrl).catch(() => null) : null + + return resolveGatedDownloadAuth(connection.authMode, nativeAt, connection.token) +} + async function saveGatewayFile(payload: any = {}) { const filePath = gatewayFilePath(payload.path) @@ -7273,9 +7302,17 @@ async function saveGatewayFile(payload: any = {}) { const url = `${connection.baseUrl}${requestPath}` try { - return await (connection.authMode === 'oauth' - ? downloadViaOauthSessionToFile(url, ctx) - : downloadViaTokenToFile(url, connection.token, ctx)) + const auth = await gatedFileAuth(connection) + + if (auth.kind === 'bearer') { + return await downloadViaTokenToFile(url, auth.token, ctx, { bearer: auth.token }) + } + + if (auth.kind === 'cookie') { + return await downloadViaOauthSessionToFile(url, ctx) + } + + return await downloadViaTokenToFile(url, auth.token, ctx) } catch (error) { // Desktop and the remote gateway update independently. A gateway predating // /api/fs/download 404s here; fall back (ONLY on 404) to the older capped @@ -7300,10 +7337,16 @@ async function saveGatewayFileViaDataUrl(connection, profile, filePath, ctx: any ) const url = `${connection.baseUrl}${requestPath}` + const auth = await gatedFileAuth(connection) + let json: any - const json = ( - connection.authMode === 'oauth' ? await fetchJsonViaOauthSession(url) : await fetchJson(url, connection.token) - ) as any + if (auth.kind === 'bearer') { + json = await fetchJson(url, null, { bearer: auth.token }) + } else if (auth.kind === 'cookie') { + json = await fetchJsonViaOauthSession(url) + } else { + json = await fetchJson(url, auth.token) + } const dataUrl = json?.dataUrl @@ -9716,6 +9759,13 @@ async function ensureBackend(profile) { } entry.connectionPromise = spawnPoolBackend(key, entry).catch(async error => { + // Land the failure in desktop.log: without this a spawn that dies before + // its child exists (guard rejection, runtime resolution) leaves no trace + // beyond renderer-side rejections users never see in a bundle. + rememberLog( + `Hermes backend for profile "${key}" failed to start: ${error instanceof Error ? error.message : String(error)}` + ) + if (backendPool.get(key) === entry) { backendPool.delete(key) } @@ -9798,6 +9848,12 @@ async function ensureRegistryBackend(connectionId, profile) { forceLocal: true, poolKey: localRoute.poolKey }).catch(async error => { + // Same trace rule as the v1 pool path: a forced-local child whose spawn + // rejects before the child exists must still land in desktop.log. + rememberLog( + `Hermes backend for profile "${profileKey}" (forced-local) failed to start: ${error instanceof Error ? error.message : String(error)}` + ) + if (backendPool.get(localRoute.poolKey) === localEntry) { backendPool.delete(localRoute.poolKey) } @@ -10079,12 +10135,18 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po const webDist = resolveWebDist() const readyFile = backend.readyFile ? makeDashboardReadyFile() : null - rememberLog(`Starting Hermes backend for profile "${profile}" via ${backend.label}`) - - const parentStartMarker = await desktopParentStartMarker() + // Guard BEFORE the "Starting" line: a profile that only exists on a remote + // backend (remote-primary desktop asked for a forced-local child) rejects + // here, and logging "Starting" first left an orphaned line with no READY + // and no exit — the exact undiagnosable burst signature in remote-gateway + // user bundles (Aug 2026, Dash's report). assertLocalProfileCanStart(profile, profileDeletionGate, key => directoryExists(path.join(HERMES_HOME, 'profiles', key)) ) + + rememberLog(`Starting Hermes backend for profile "${profile}" via ${backend.label}`) + + const parentStartMarker = await desktopParentStartMarker() const backendNonce = crypto.randomBytes(16).toString('hex') const parentIdentityEnv = parentWatchdogEnv(process.pid, parentStartMarker, backendNonce) @@ -10630,6 +10692,7 @@ async function startHermes() { } const message = error instanceof Error ? error.message : String(error) + const hostKeyChanged = isHostKeyChangedBootFailure(error) // Only latch LOCAL boot failures. A remote failure (lapsed session / mint // timeout / host briefly unreachable across sleep) is transient and has no @@ -10640,6 +10703,16 @@ async function startHermes() { backendStartFailure = error instanceof Error ? error : new Error(message) } + // A host-key CHANGE is the terminal exception among remote failures: SSH + // fails closed until the user verifies the change and clears the stale + // known_hosts entry, so retrying re-drives the identical doomed boot (one + // bundle showed 157 consecutive failures over 2.5h). Latch it like a local + // failure — reset/repair/apply-config clear the latch after the user fixes + // known_hosts. + if (shouldLatchHostKeyChangedFailure({ attemptedRemote, isReauth: false, isHostKeyChanged: hostKeyChanged })) { + backendStartFailure = error instanceof Error ? error : new Error(message) + } + // A confirmed reauth rejection latches separately: it can't self-heal, and // leaving it unlatched hides the overlay's "Sign in" button on every retry. if (shouldLatchRemoteReauthFailure({ attemptedRemote, isReauth: isReauthRequiredError(error) })) { @@ -10654,9 +10727,14 @@ async function startHermes() { // Renderer contract for the self-heal loop (#82679): a transient // REMOTE failure (dropped SSH/HTTP registered connection, mint // timeout) is retryable — the renderer re-attempts the boot with - // bounded backoff. Local failures and confirmed reauth rejections - // are not: those end in the recovery overlay / sign-in affordance. - retryable: isRetryableRemoteBootFailure({ attemptedRemote, isReauth: isReauthRequiredError(error) }), + // bounded backoff. Local failures, confirmed reauth rejections, and + // host-key changes are not: those end in the recovery overlay / + // sign-in affordance. + retryable: isRetryableRemoteBootFailure({ + attemptedRemote, + isReauth: isReauthRequiredError(error), + isHostKeyChanged: hostKeyChanged + }), running: false }, { allowDecrease: true } @@ -10850,8 +10928,10 @@ function createSessionWindow(sessionId, { watch = false } = {}) { // Additional full "instance" windows — peers of the primary that render the // COMPLETE app (sidebar, routing, its own draft) against the shared backend, so // a user can run multiple GUI windows at once (⌘⇧N / the "New Window" palette -// command). Unlike the compact session windows they carry no `?win` flag. The -// primary mainWindow stays the notification / deep-link / pet-overlay anchor and +// command). Unlike the compact session windows they carry no `?win` flag; a +// separate `peer=1` marker prevents them from replaying app-launch source +// restoration after joining that shared backend. The primary mainWindow stays +// the notification / deep-link / pet-overlay anchor and // is NOT tracked here. The set holds a strong reference so an open peer isn't // garbage-collected, and drops it on close. const instanceWindows = new Set() @@ -10931,7 +11011,14 @@ function createInstanceWindow() { }) attachRendererConsoleCapture(win, 'instance', rememberLog) - loadWindowUrl(win, DEV_SERVER || pathToFileURL(resolveRendererIndex()).toString(), 'Instance window') + loadWindowUrl( + win, + buildInstanceWindowUrl({ + devServer: DEV_SERVER, + rendererIndexPath: DEV_SERVER ? undefined : resolveRendererIndex() + }), + 'Instance window' + ) return win } @@ -12137,8 +12224,11 @@ registerPetOverlayIpc({ }) // --- HUD mode (chrome-free floating chat) — see hud-ipc.ts. --------------- -registerHudIpc({ +const hudIpc = registerHudIpc({ isMac: IS_MAC, + isWindows: IS_WINDOWS, + glassSupported: GLASS_SUPPORTED, + getTranslucencyState: () => translucencyState, getHudWindow: () => hudWindow, openHudWindow, closeHudWindow, @@ -12146,6 +12236,7 @@ registerHudIpc({ hudSessionId = value } }) + ipcMain.handle('hermes:bootstrap:reset', async () => { // Renderer's "Reload and retry" path. Clear the latched failure and // reset connection state so the next startHermes() call restarts the @@ -12584,6 +12675,31 @@ async function probeSshProfileInventory(connection) { } async function enumerateRegistryAgentSources(registry = readDesktopConnectionsRegistry()) { + // One dead source must not wedge the whole roster: ensureRegistryBackend on + // an unreachable remote can block up to the 45s readiness timeout, and the + // Bot Mode poll runs every 5s — each poll queued behind the dead dial, so + // the renderer painted stale rows for the entire outage (and the roster IPC + // hung >30s in live repro). Bound each source's enumeration; a timeout is + // reported like any other unreachable source and retried on the next poll. + const perSourceTimeoutMs = 10_000 + + const withEnumerationDeadline = async (work: Promise): Promise => { + let timer: ReturnType | null = null + + try { + return await Promise.race([ + work, + new Promise((_resolve, reject) => { + timer = setTimeout(() => reject(new Error('roster enumeration timed out')), perSourceTimeoutMs) + }) + ]) + } finally { + if (timer !== null) { + clearTimeout(timer) + } + } + } + return Promise.all( registry.connections.map(async connection => { let raw: { connection: typeof connection; error?: string; installId?: string; profiles: null | string[] } @@ -12617,7 +12733,10 @@ async function enumerateRegistryAgentSources(registry = readDesktopConnectionsRe } } - const descriptor: any = await ensureRegistryBackend(connection.id, null) + const descriptor: any = await withEnumerationDeadline( + Promise.resolve(ensureRegistryBackend(connection.id, null)) + ) + const body: any = await getJsonForBackend(descriptor, '/api/profiles', { timeoutMs: 8_000 }) // Cached with a TTL, so the 5s roster poll usually pays zero extra @@ -12703,42 +12822,59 @@ ipcMain.handle('hermes:gateway:ws-url-for', async (_event, payload) => { // app's own update pipeline; remote/ssh POST the backend's own // /api/hermes/update endpoint (the dashboard updater), which runs // `hermes update` on THAT machine. -ipcMain.handle('hermes:connections:update-all', async () => { +ipcMain.handle('hermes:connections:update-all', async (_event, payload) => { const registry = readDesktopConnectionsRegistry() + // Optional renderer-side exclusions: the everything-update flow dispatches + // the ACTIVE backend through its own detailed-progress path and chains the + // local client apply LAST (it relaunches the app), so it excludes those ids + // here to avoid double-dispatch. No payload keeps the Settings button's + // original all-rows behavior byte-identical. + const excludeIds = new Set( + Array.isArray((payload as any)?.excludeIds) ? (payload as any).excludeIds.map((id: unknown) => String(id)) : [] + ) + const results = await Promise.all( - registry.connections.map(async connection => { - const base = { connectionId: connection.id, label: connection.label, kind: connection.kind } - const eligibility = updateEligibility(connection) + registry.connections + .filter(connection => !excludeIds.has(connection.id)) + .map(async connection => { + const base = { connectionId: connection.id, label: connection.label, kind: connection.kind } + const eligibility = updateEligibility(connection) - if (!eligibility.eligible) { - return { ...base, ok: false, skipped: true, reason: eligibility.reason } - } - - try { - if (connection.kind === 'local') { - // The app-managed runtime updates through the same pipeline as the - // Settings → Updates button (marker + venv gate + relaunch flow). - const result: any = await applyUpdates({}) - - return { ...base, ok: result?.ok !== false, detail: result?.message || 'update started' } + if (!eligibility.eligible) { + return { ...base, ok: false, skipped: true, reason: eligibility.reason } } - const descriptor: any = await ensureRegistryBackend(connection.id, null) + try { + if (connection.kind === 'local') { + // The app-managed runtime updates through the same pipeline as the + // Settings → Updates button (marker + venv gate + relaunch flow). + const result: any = await applyUpdates({}) - const body: any = await postJsonForBackend(descriptor, '/api/hermes/update', {}, { timeoutMs: 15_000 }) + return { ...base, ok: result?.ok !== false, detail: result?.message || 'update started' } + } - if (body?.ok === false) { - // The backend refused (docker/nix/externally-managed installs) — - // surface ITS message, per-row, instead of failing the batch. - return { ...base, ok: false, skipped: true, reason: body?.error || 'backend-refused', detail: body?.message } + const descriptor: any = await ensureRegistryBackend(connection.id, null) + + const body: any = await postJsonForBackend(descriptor, '/api/hermes/update', {}, { timeoutMs: 15_000 }) + + if (body?.ok === false) { + // The backend refused (docker/nix/externally-managed installs) — + // surface ITS message, per-row, instead of failing the batch. + return { + ...base, + ok: false, + skipped: true, + reason: body?.error || 'backend-refused', + detail: body?.message + } + } + + return { ...base, ok: true, detail: body?.message || 'update started' } + } catch (error: any) { + return { ...base, ok: false, error: String(error?.message || error) } } - - return { ...base, ok: true, detail: body?.message || 'update started' } - } catch (error: any) { - return { ...base, ok: false, error: String(error?.message || error) } - } - }) + }) ) return { ok: true, results } @@ -13283,12 +13419,17 @@ async function pooledRegistrySessionSources(): Promise return sources } -async function dispatchRegistryApiRequest(request, registryConnectionId, routeProfile = request?.profile) { +async function dispatchRegistryApiRequest( + request, + registryConnectionId, + routeProfile = request?.profile, + requestProfile = request?.profile +) { const connection: any = await ensureRegistryBackend(registryConnectionId, routeProfile) const requestPath = connection.sharedRemote - ? pathWithProfileScope(request.path, request?.profile) - : translateSelfProfileQuery(request.path, request?.profile, connection.remoteProfile) + ? pathWithProfileScope(request.path, requestProfile) + : translateSelfProfileQuery(request.path, requestProfile, connection.remoteProfile) return fetchJsonForBackend(connection, requestPath, { method: request?.method, @@ -13309,8 +13450,8 @@ function registryConnectionKind(connectionId) { return source.kind } -async function teardownConnectionScopedProfileBackend(connectionId, profile, request) { - const key = backendScopeKey(connectionId, request?.profile || profile) +async function teardownConnectionScopedProfileBackend(connectionId, profile) { + const key = backendScopeKey(connectionId, profile) await Promise.all([ poolStopper.stop(key), sshBootstrapCoordinator.cancelAndWait(key).then(() => teardownSshConnection(key)) @@ -13446,12 +13587,13 @@ ipcMain.handle('hermes:api', async (_event, request) => { return dispatchConnectionScopedProfileDelete(request, { acquire: profile => profileDeletionGate.acquire(profile), connectionKind: connectionId => registryConnectionKind(connectionId), - dispatch: routeProfile => dispatchRegistryApiRequest(request, registryConnectionId, routeProfile), + dispatch: routeProfile => + dispatchRegistryApiRequest(request, registryConnectionId, routeProfile, deletingProfile), isDefaultProfile: profile => profile === 'default', isValidProfileName: profile => PROFILE_NAME_RE.test(profile), prepareLocal: localRequest => prepareProfileDeleteRequest(localRequest).then(() => undefined), teardownConnection: (connectionId, profile) => - teardownConnectionScopedProfileBackend(connectionId, profile, request) + teardownConnectionScopedProfileBackend(connectionId, profile) }) } @@ -13931,6 +14073,12 @@ ipcMain.on('hermes:translucency', (_event, payload) => { scheduleTranslucencyWrite() + // The HUD's frost reads the same setting but answers on its own terms (see + // hudFrostFor) — and it is a transparent window, so it is deliberately not + // in the chat fan-out below. It self-diffs, so an unrelated change costs + // nothing native. + hudIpc.applyHudFrost() + if (changed.backing || changed.material || changed.opacity) { for (const win of BrowserWindow.getAllWindows()) { applyWindowTranslucency(win, changed) diff --git a/apps/desktop/electron/native-auth-decisions.test.ts b/apps/desktop/electron/native-auth-decisions.test.ts index d4cfc068cd..6eb7a10dac 100644 --- a/apps/desktop/electron/native-auth-decisions.test.ts +++ b/apps/desktop/electron/native-auth-decisions.test.ts @@ -13,6 +13,7 @@ import { test } from 'vitest' import { oauthGuardMayHardFail, oauthSessionIsLive, + resolveGatedDownloadAuth, resolveJsonBody, resolveOauthRestAuth, resolveReadinessProbeAuth @@ -130,3 +131,20 @@ test('oauthGuardMayHardFail keeps the strict guard when the list is unusable', ( assert.equal(oauthGuardMayHardFail('nonsense' as any), true) assert.equal(oauthGuardMayHardFail([{ supportsPassword: true }]), true) }) + +// --- 6. gated download auth (guards the Files-panel 401 on cookieless native) --- + +test('resolveGatedDownloadAuth matches oauth REST: bearer first, then cookie', () => { + assert.deepEqual(resolveGatedDownloadAuth('oauth', 'native-at'), { kind: 'bearer', token: 'native-at' }) + assert.deepEqual(resolveGatedDownloadAuth('oauth', null), { kind: 'cookie' }) + assert.deepEqual(resolveGatedDownloadAuth('oauth', ''), { kind: 'cookie' }) +}) + +test('resolveGatedDownloadAuth uses the session token for token and local modes', () => { + assert.deepEqual(resolveGatedDownloadAuth('token', 'native-at', 'session-token'), { + kind: 'token', + token: 'session-token' + }) + assert.deepEqual(resolveGatedDownloadAuth('local', null, 'sess'), { kind: 'token', token: 'sess' }) + assert.deepEqual(resolveGatedDownloadAuth(undefined, null, null), { kind: 'token', token: null }) +}) diff --git a/apps/desktop/electron/native-auth-decisions.ts b/apps/desktop/electron/native-auth-decisions.ts index c0978c3ecd..9e620bd906 100644 --- a/apps/desktop/electron/native-auth-decisions.ts +++ b/apps/desktop/electron/native-auth-decisions.ts @@ -2,7 +2,7 @@ * native-auth-decisions.ts * * Pure decision helpers extracted from main.ts for the RFC 8252 native-app - * auth flow. These encode three choices that were each the site of a real + * auth flow. These encode six choices that were each the site of a real * runtime bug — invisible to the mocked flow tests because the tests never * exercised the real main.ts internals. Keeping them pure + unit-tested here * prevents silent regressions: @@ -31,7 +31,12 @@ * can satisfy neither the native-bearer nor the OAuth-partition-cookie * check by design, so the pre-flight guard must not hard-fail it. * - * All five are trivial once named; the value is the test that pins the + * 6. resolveGatedDownloadAuth — file save/read must present the SAME + * credentials as oauth REST. `saveGatewayFile` used to always ride the + * OAuth cookie partition, so a cookieless native (or native-password) + * session could list files via `hermes:api` and still 401 on Download. + * + * All six are trivial once named; the value is the test that pins the * contract so the god-file call sites can't drift back to the buggy shape. */ @@ -106,6 +111,28 @@ export function resolveReadinessProbeAuth( return { kind: 'public' } } +export type GatedDownloadAuth = OauthRestAuth | { kind: 'token'; token: string | null } + +/** + * Decide how a gated file download authenticates. + * + * Must match oauth REST (`resolveOauthRestAuth`): native bearer when present, + * else the OAuth cookie partition. Token/local connections keep the static + * session-token header. A cookie-only download against a cookieless native + * session is the #88987 401 — Files panel listing works, Download does not. + */ +export function resolveGatedDownloadAuth( + authMode: string | null | undefined, + nativeAccessToken?: string | null, + connectionToken?: string | null +): GatedDownloadAuth { + if (authMode === 'oauth') { + return resolveOauthRestAuth(nativeAccessToken) + } + + return { kind: 'token', token: connectionToken ?? null } +} + export interface AdvertisedAuthProvider { name?: string supportsPassword?: boolean diff --git a/apps/desktop/electron/parent-process-identity.test.ts b/apps/desktop/electron/parent-process-identity.test.ts index fd46025c7d..a1b071c245 100644 --- a/apps/desktop/electron/parent-process-identity.test.ts +++ b/apps/desktop/electron/parent-process-identity.test.ts @@ -5,7 +5,8 @@ import { test, vi } from 'vitest' import { createParentStartMarkerResolver, electronProcessStartMarker, - parentWatchdogEnv + parentWatchdogEnv, + spawnTag } from './parent-process-identity' test('electronProcessStartMarker uses Electron creation time only for its own PID', () => { @@ -60,14 +61,26 @@ test('parentWatchdogEnv emits an exact identity when the marker is available', ( assert.deepEqual(parentWatchdogEnv(42, 'winms:1723456789123', 'nonce-1'), { HERMES_PARENT_NONCE: 'nonce-1', HERMES_PARENT_PID: '42', - HERMES_PARENT_START_MARKER: 'winms:1723456789123' + HERMES_PARENT_START_MARKER: 'winms:1723456789123', + // Spawn tag mirrored by hermes_cli/process_identity.py — spawner create + // time in seconds derived from the same winms marker. + HERMES_SPAWN: 'v1:-:serve:42:1723456789.123' }) }) test('parentWatchdogEnv atomically falls back to PID-only identity', () => { assert.deepEqual(parentWatchdogEnv(42, null, 'unused-nonce'), { - HERMES_PARENT_PID: '42' + HERMES_PARENT_PID: '42', + // Marker probe failed → spawn tag still present, create time unknown. + HERMES_SPAWN: 'v1:-:serve:42:-' }) assert.throws(() => parentWatchdogEnv(42, '', 'nonce-1'), /marker and nonce must be non-empty/) assert.throws(() => parentWatchdogEnv(42, 'winms:1723456789123', ''), /marker and nonce must be non-empty/) }) + +test('spawnTag derives seconds from a winms marker and dashes without one', () => { + assert.equal(spawnTag(42, 'winms:1723456789123'), 'v1:-:serve:42:1723456789.123') + assert.equal(spawnTag(42, null), 'v1:-:serve:42:-') + assert.equal(spawnTag(42, 'win:638908765432100000'), 'v1:-:serve:42:-') + assert.equal(spawnTag(42, 'winms:garbage'), 'v1:-:serve:42:-') +}) diff --git a/apps/desktop/electron/parent-process-identity.ts b/apps/desktop/electron/parent-process-identity.ts index 0aa8956aca..c67010d329 100644 --- a/apps/desktop/electron/parent-process-identity.ts +++ b/apps/desktop/electron/parent-process-identity.ts @@ -2,6 +2,10 @@ export type ParentWatchdogEnv = { HERMES_PARENT_PID: string HERMES_PARENT_START_MARKER?: string HERMES_PARENT_NONCE?: string + /** Spawn tag consumed by hermes_cli.process_identity (`v1::::`). + * Install is `-` (unknown) from the Desktop — the Python side scopes by + * venv membership, so the tag only needs lineage, not install identity. */ + HERMES_SPAWN?: string } export interface ParentStartMarkerResolverOptions { @@ -69,6 +73,7 @@ export function parentWatchdogEnv(pid: number, startMarker: string | null, nonce } const env: ParentWatchdogEnv = { HERMES_PARENT_PID: String(pid) } + env.HERMES_SPAWN = spawnTag(pid, startMarker) if (startMarker === null) { return env @@ -83,3 +88,21 @@ export function parentWatchdogEnv(pid: number, startMarker: string | null, nonce return env } + +/** Build the `HERMES_SPAWN` tag mirrored by hermes_cli/process_identity.py. + * The Desktop only ever spawns backend servers, so purpose is `serve`; the + * spawner create-time is derived from the same `winms:` marker the parent + * watchdog uses (seconds, 3 decimals), `-` when the marker probe failed. */ +export function spawnTag(pid: number, startMarker: string | null): string { + let createPart = '-' + + if (startMarker?.startsWith('winms:')) { + const ms = Number(startMarker.slice('winms:'.length)) + + if (Number.isFinite(ms) && ms > 0) { + createPart = (ms / 1000).toFixed(3) + } + } + + return `v1:-:serve:${pid}:${createPart}` +} diff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.ts index 2d1ba597ce..e95c5e389a 100644 --- a/apps/desktop/electron/preload.ts +++ b/apps/desktop/electron/preload.ts @@ -75,7 +75,10 @@ contextBridge.exposeInMainWorld('hermesDesktop', { setIgnoreMouse: ignore => ipcRenderer.send('hermes:hud:ignore-mouse', ignore), moveBy: delta => ipcRenderer.send('hermes:hud:move-by', delta), setBounds: bounds => ipcRenderer.send('hermes:hud:set-bounds', bounds), - setVibrancy: on => ipcRenderer.invoke('hermes:hud:vibrancy', on), + // Whether the band covers the window below the bar. Main pairs it with the + // user's translucency setting to decide the native frost (macOS vibrancy / + // Windows 11 DWM backdrop) — see hudFrostFor. + setFrost: showing => ipcRenderer.invoke('hermes:hud:frost', showing), // The HUD tells main which session it is on; main hands that back to the // app window when the HUD closes, so the app can re-home onto it. setSession: sessionId => ipcRenderer.send('hermes:hud:session', sessionId), @@ -152,7 +155,8 @@ contextBridge.exposeInMainWorld('hermesDesktop', { setLastUsed: id => ipcRenderer.invoke('hermes:connections:set-last-used', id), test: id => ipcRenderer.invoke('hermes:connections:test', id), // Fan out `hermes update` to every eligible registered connection. - updateAll: () => ipcRenderer.invoke('hermes:connections:update-all'), + // Optional excludeIds skips rows the caller updates through another path. + updateAll: options => ipcRenderer.invoke('hermes:connections:update-all', options), // Registry lifecycle push (main → renderer): a connection was removed or // materially edited, so secondaries scoped to it must be disposed (and, // for edits, re-dialed at the new target). diff --git a/apps/desktop/electron/profile-delete-routing.test.ts b/apps/desktop/electron/profile-delete-routing.test.ts index 7538f63c17..b7f3b97e43 100644 --- a/apps/desktop/electron/profile-delete-routing.test.ts +++ b/apps/desktop/electron/profile-delete-routing.test.ts @@ -222,6 +222,41 @@ test('explicit registered SSH DELETE tears down its source backend before dispat ]) }) +test('non-identity connection DELETE tears down the logical pool and deletes the backend target', async () => { + const events: string[] = [] + const request = { + connectionId: 'source-a', + method: 'DELETE', + path: '/api/profiles/backend-worker', + profile: 'worker' + } + + await dispatchConnectionScopedProfileDelete(request, { + ...deps, + acquire: profile => { + events.push(`gate:${profile}`) + return () => events.push(`release:${profile}`) + }, + connectionKind: () => 'ssh', + dispatch: async routeProfile => { + events.push(`delete:${request.path}:${routeProfile ?? 'primary'}`) + return 'deleted' + }, + prepareLocal: async () => assert.fail('remote deletion must not use local profile teardown'), + teardownConnection: async (connectionId, profile) => { + events.push(`teardown:${connectionId}:${profile}`) + assert.notEqual(profile, 'backend-worker', 'must not stop the backend-target pool') + } + }) + + assert.deepEqual(events, [ + 'gate:backend-worker', + 'teardown:source-a:worker', + 'delete:/api/profiles/backend-worker:primary', + 'release:backend-worker' + ]) +}) + test('connection-scoped default DELETE rejects before gate, preparation, teardown, or dispatch', async () => { const events: string[] = [] diff --git a/apps/desktop/electron/profile-delete-routing.ts b/apps/desktop/electron/profile-delete-routing.ts index bf6d25e2d7..39bada976a 100644 --- a/apps/desktop/electron/profile-delete-routing.ts +++ b/apps/desktop/electron/profile-delete-routing.ts @@ -88,28 +88,29 @@ export async function dispatchConnectionScopedProfileDelete( request: ConnectionScopedProfileDeleteRequest, deps: ConnectionScopedProfileDeleteDeps ): Promise { - const profile = profileNameFromDeleteRequest(request) + const targetProfile = profileNameFromDeleteRequest(request) + const logicalProfile = String(request.profile ?? '').trim() || targetProfile || '' const connectionId = String(request.connectionId ?? '').trim() - if (!profile || !connectionId) { + if (!targetProfile || !connectionId) { throw new Error('Connection-scoped profile deletion requires a connection and profile.') } - if (deps.isDefaultProfile(profile)) { + if (deps.isDefaultProfile(targetProfile)) { throw new Error('The default profile cannot be deleted.') } - if (!deps.isValidProfileName(profile)) { - throw new Error(`Invalid profile name: ${profile}`) + if (!deps.isValidProfileName(targetProfile)) { + throw new Error(`Invalid profile name: ${targetProfile}`) } - const release = deps.acquire(profile) + const release = deps.acquire(targetProfile) try { if (deps.connectionKind(connectionId) === 'local') { await deps.prepareLocal(request) } else { - await deps.teardownConnection(connectionId, profile) + await deps.teardownConnection(connectionId, logicalProfile) } return await deps.dispatch(null) diff --git a/apps/desktop/electron/session-windows.test.ts b/apps/desktop/electron/session-windows.test.ts index 1959cc4458..c0a1178429 100644 --- a/apps/desktop/electron/session-windows.test.ts +++ b/apps/desktop/electron/session-windows.test.ts @@ -3,6 +3,7 @@ import assert from 'node:assert/strict' import { test } from 'vitest' import { + buildInstanceWindowUrl, buildSessionWindowUrl, chatWindowWebPreferences, createSessionWindowRegistry, @@ -88,6 +89,19 @@ test('buildSessionWindowUrl adds the watch flag for spectator windows, before th assert.equal(url, 'http://localhost:5173/?win=secondary&watch=1#/abc') }) +test('buildInstanceWindowUrl marks a full peer without selecting a specialized renderer', () => { + const url = buildInstanceWindowUrl({ devServer: 'http://localhost:5173/' }) + + assert.equal(url, 'http://localhost:5173/?peer=1') + assert.ok(!url.includes('win=')) +}) + +test('buildInstanceWindowUrl marks a packaged full peer', () => { + const url = buildInstanceWindowUrl({ rendererIndexPath: '/opt/app/index.html' }) + + assert.match(url, /^file:\/\/.*index\.html\?peer=1$/) +}) + test('instanceWindowBounds cascades a new window off its source bounds', () => { const bounds = instanceWindowBounds({ x: 100, y: 120, width: 1400, height: 900 }, { width: 1, height: 1 }) diff --git a/apps/desktop/electron/session-windows.ts b/apps/desktop/electron/session-windows.ts index 790f1c03a7..19be87ff1c 100644 --- a/apps/desktop/electron/session-windows.ts +++ b/apps/desktop/electron/session-windows.ts @@ -77,6 +77,23 @@ function buildSessionWindowUrl(sessionId: string, { devServer, rendererIndexPath return `${pathToFileURL(rendererIndexPath).toString()}${query}${route}` } +// Full peer windows render the ordinary app shell, so they deliberately do +// not use the `win` query parameter that selects a specialized renderer. The +// separate marker lets the renderer distinguish a peer from the one primary +// app window: app-launch source restoration belongs to the primary only, while +// a peer keeps the already-running backend it joined during boot. +function buildInstanceWindowUrl({ devServer, rendererIndexPath }: any = {}) { + const query = '?peer=1' + + if (devServer) { + const base = devServer.endsWith('/') ? devServer.slice(0, -1) : devServer + + return `${base}/${query}` + } + + return `${pathToFileURL(rendererIndexPath).toString()}${query}` +} + // Full "instance" windows (⌘⇧N / the "New Window" command) open a complete app // peer, not a compact chat. Cascade each one off its source window's bounds so a // new window doesn't land exactly on top of the one it was spawned from. Pure so @@ -160,6 +177,7 @@ function createSessionWindowRegistry() { } export { + buildInstanceWindowUrl, buildSessionWindowUrl, chatWindowWebPreferences, createSessionWindowRegistry, diff --git a/apps/desktop/electron/translucency.test.ts b/apps/desktop/electron/translucency.test.ts index 6795fdd5d7..59cc15828e 100644 --- a/apps/desktop/electron/translucency.test.ts +++ b/apps/desktop/electron/translucency.test.ts @@ -15,6 +15,8 @@ import { clampIntensity, DEFAULT_GLASS_MATERIAL, DEFAULT_GLASS_SCOPE, + defaultTranslucencyState, + defaultTranslucencyValues, GLASS_MATERIALS, GLASS_SCOPES, glassActive, @@ -23,10 +25,14 @@ import { glassMaterialsFor, glassSupportedOn, glassSurfaceKeep, + hudFrostFor, + normalizeBook, normalizeMaterial, normalizeMode, normalizeScope, normalizeState, + resolveTranslucency, + setTranslucencyValues, TRANSLUCENCY_CURVE, TRANSLUCENCY_MAX, TRANSLUCENCY_MIN, @@ -183,7 +189,7 @@ describe('windowOpacityFor', () => { it('fades a glass window only through its own lever, on the ramp clear uses', () => { expect(windowOpacityFor(glass(60, DEFAULT_GLASS_MATERIAL, 0))).toBe(1) expect(windowOpacityFor(glass(60, DEFAULT_GLASS_MATERIAL, 40))).toBe(windowOpacityFor(clear(40))) - expect(windowOpacityFor(glass(0, DEFAULT_GLASS_MATERIAL, 100))).toBe(windowOpacityFor(clear(100))) + expect(windowOpacityFor(glass(100, DEFAULT_GLASS_MATERIAL, 100))).toBe(windowOpacityFor(clear(100))) }) it('leaves fade inert under clear, where the intensity lever already is the opacity', () => { @@ -245,6 +251,53 @@ describe('vibrancyFor', () => { }) }) +// The HUD is a transparent window, so its frost has no opaque page to hide +// behind: every state that isn't "frost wanted" has to resolve to no material +// at all, or the band leaves a grey slab hanging over another app. +describe('hudFrostFor', () => { + it('wears the chosen frost on both platforms while the band is showing', () => { + expect(hudFrostFor(glass(60, 'header'), true)).toEqual({ vibrancy: 'header', backgroundMaterial: 'mica' }) + expect(hudFrostFor(glass(60, 'under-window'), true)).toEqual({ + vibrancy: 'under-window', + backgroundMaterial: 'acrylic' + }) + }) + + // The material is the whole window rectangle and nothing on the page can + // clip it, so a hidden band must mean no frost — this is the veto that keeps + // idle HUD mode the bar and nothing else. + it('is off whenever the band is not covering the window', () => { + expect(hudFrostFor(glass(60, 'header'), false)).toEqual({ vibrancy: null, backgroundMaterial: 'none' }) + }) + + // ...and the setting is the other veto: Glass off, or the tint at zero, + // means the HUD never frosts however engaged the band is. + it('is off whenever glass itself is off', () => { + expect(hudFrostFor(clear(60), true)).toEqual({ vibrancy: null, backgroundMaterial: 'none' }) + expect(hudFrostFor(glass(0, 'header'), true)).toEqual({ vibrancy: null, backgroundMaterial: 'none' }) + }) + + // Unlike a chat window, which keeps 'sidebar' under its titlebar band in + // every non-glass state. Pinning this is what stops someone "fixing" the + // null into a resting material and painting the slab back. + it('resolves off to no material at all, not to a resting one', () => { + expect(hudFrostFor(clear(60), true).vibrancy).toBeNull() + expect(vibrancyFor(clear(60))).toBe('sidebar') + }) + + // The tint is painted by the renderer, exactly as it is for a chat window — + // dragging it must not re-issue setVibrancy, whose 150ms animation restarts + // on every call and never lets the material settle. + it('does not move any native property as the tint slider is dragged', () => { + for (let intensity = 1; intensity <= 100; intensity += 1) { + expect(hudFrostFor(glass(intensity, 'popover'), true)).toEqual({ + vibrancy: 'popover', + backgroundMaterial: 'tabbed' + }) + } + }) +}) + describe('glassSupportedOn', () => { it('is on for macOS regardless of kernel version', () => { expect(glassSupportedOn('darwin')).toBe(true) @@ -499,4 +552,133 @@ describe('what an update actually changes natively', () => { it('is everything when switching between the two modes', () => { expect(nativeDiff(clear(60), glass(60))).toEqual({ backing: true, material: true, opacity: true }) }) + + it('leaves a window alone when glass is selected but off', () => { + // The light default carries one point of fade. Someone who dragged the + // tint to zero asked for an opaque window, and that point must not follow + // them there — off has to mean exactly 1, not 0.9999. + expect(windowOpacityFor({ ...glass(0), fade: 1 })).toBe(1) + expect(windowOpacityFor({ ...glass(0), fade: 40 })).toBe(1) + }) + + it('still fades a window whose glass is actually on', () => { + expect(windowOpacityFor({ ...glass(66), fade: 40 })).toBeLessThan(1) + }) +}) + +/** + * The shipped defaults, per platform. These are the numbers a fresh profile + * gets before anyone opens Settings, so they are the ones most people will + * ever see — and they differ by platform because the lever means different + * things behind macOS vibrancy and Windows acrylic. + */ +describe('the defaults a fresh profile lands on', () => { + const mac = (appearance: 'dark' | 'light') => defaultTranslucencyValues(appearance, false) + const win = (appearance: 'dark' | 'light') => defaultTranslucencyValues(appearance, true) + + it('ships glass on, not a lever resting at zero', () => { + for (const values of [mac('light'), mac('dark'), win('light'), win('dark')]) { + expect(values.intensity).toBeGreaterThan(0) + expect(glassActive({ ...values, mode: 'glass' })).toBe(true) + } + + for (const appearance of ['light', 'dark'] as const) { + expect(defaultTranslucencyState(appearance, true, false).mode).toBe('glass') + expect(defaultTranslucencyState(appearance, true, true).mode).toBe('glass') + } + }) + + it('falls back to clear where no native material exists', () => { + expect(defaultTranslucencyState('dark', false, false).mode).toBe('clear') + }) + + it('tints light more heavily than dark, on both platforms', () => { + // A dark field already separates from what is behind it; a bright one + // needs real thinning before the desktop reads as a layer underneath. + expect(mac('light').intensity).toBeGreaterThan(mac('dark').intensity) + expect(win('light').intensity).toBeGreaterThan(win('dark').intensity) + }) + + it('asks far less of Windows, which composites its own tint in DWM', () => { + expect(win('light').intensity).toBeLessThan(mac('light').intensity) + expect(win('dark').intensity).toBeLessThan(mac('dark').intensity) + }) + + it('never fades a Windows window — setOpacity dims the composited backdrop', () => { + expect(win('light').fade).toBe(0) + expect(win('dark').fade).toBe(0) + }) + + it('defaults each platform onto a frost that platform can actually render', () => { + for (const appearance of ['light', 'dark'] as const) { + expect(glassMaterialsFor(true)).toContain(win(appearance).material) + expect(glassMaterialsFor(false)).toContain(mac(appearance).material) + } + }) + + it('opens the whole window, not just the sidebar rail', () => { + for (const values of [mac('light'), mac('dark'), win('light'), win('dark')]) { + expect(values.scope).toBe('window') + } + }) +}) + +/** + * The per-appearance ladder: appearance slot → base → platform default, per + * key. This is what makes tuning light mode stay in light mode while an + * untouched dark keeps inheriting. + */ +describe('resolving the book for the painted appearance', () => { + const empty = normalizeBook(null, true) + + it('falls all the way through to the platform default', () => { + expect(resolveTranslucency(empty, 'dark', false).intensity).toBe(defaultTranslucencyValues('dark', false).intensity) + expect(resolveTranslucency(empty, 'dark', true).intensity).toBe(defaultTranslucencyValues('dark', true).intensity) + }) + + it('scopes an edit to the appearance it was made in', () => { + const book = setTranslucencyValues(empty, 'light', { intensity: 90 }) + + expect(resolveTranslucency(book, 'light', false).intensity).toBe(90) + expect(resolveTranslucency(book, 'dark', false).intensity).toBe(defaultTranslucencyValues('dark', false).intensity) + }) + + it('carries a v1 state into BOTH appearances via base', () => { + // Someone who tuned a window before appearances were split keeps exactly + // what was on screen, in either appearance, until they edit one of them. + const migrated = normalizeBook({ intensity: 40, mode: 'glass' }, true) + + expect(migrated.base.intensity).toBe(40) + expect(resolveTranslucency(migrated, 'light', false).intensity).toBe(40) + expect(resolveTranslucency(migrated, 'dark', false).intensity).toBe(40) + }) + + it('lets an appearance override base without disturbing the other', () => { + const tuned = setTranslucencyValues(normalizeBook({ intensity: 40, mode: 'glass' }, true), 'dark', { + intensity: 10 + }) + + expect(resolveTranslucency(tuned, 'dark', false).intensity).toBe(10) + expect(resolveTranslucency(tuned, 'light', false).intensity).toBe(40) + }) + + it('inherits per KEY, not per appearance', () => { + // Editing only the tint in dark must leave dark's material still tracking + // base — a partial edit is not a full snapshot of the appearance. + const book = setTranslucencyValues(normalizeBook({ material: 'popover', mode: 'glass' }, true), 'dark', { + intensity: 33 + }) + + const resolved = resolveTranslucency(book, 'dark', false) + + expect(resolved.intensity).toBe(33) + expect(resolved.material).toBe('popover') + }) + + it('keeps mode global — clear vs glass is about the window, not the palette', () => { + const book = setTranslucencyValues({ ...empty, mode: 'clear' }, 'light', { intensity: 50 }) + + expect(resolveTranslucency(book, 'light', false).mode).toBe('clear') + expect(resolveTranslucency(book, 'dark', false).mode).toBe('clear') + }) }) diff --git a/apps/desktop/electron/translucency.ts b/apps/desktop/electron/translucency.ts index 6e524a8d8f..eecaf57f1b 100644 --- a/apps/desktop/electron/translucency.ts +++ b/apps/desktop/electron/translucency.ts @@ -18,6 +18,8 @@ export { clampIntensity, DEFAULT_GLASS_MATERIAL, DEFAULT_GLASS_SCOPE, + defaultTranslucencyState, + defaultTranslucencyValues, GLASS_MATERIALS, GLASS_SCOPES, glassActive, @@ -26,10 +28,14 @@ export { glassMaterialsFor, glassSupportedOn, glassSurfaceKeep, + hudFrostFor, + normalizeBook, normalizeMaterial, normalizeMode, normalizeScope, normalizeState, + resolveTranslucency, + setTranslucencyValues, TRANSLUCENCY_CURVE, TRANSLUCENCY_MAX, TRANSLUCENCY_MIN, diff --git a/apps/desktop/src/app/chat/composer/composer-utils.ts b/apps/desktop/src/app/chat/composer/composer-utils.ts index 617e650511..3272835892 100644 --- a/apps/desktop/src/app/chat/composer/composer-utils.ts +++ b/apps/desktop/src/app/chat/composer/composer-utils.ts @@ -22,6 +22,24 @@ export const COMPOSER_STACK_BREAKPOINT_PX = 320 // chevron frees is spent keeping the row single for another stretch. export const COMPOSER_COMPACT_PILL_PX = 560 +// The ladder keeps going below the stack breakpoint — a pane can be far +// narrower than even the stacked controls row. Both rungs are budgeted +// against that row's real cost: menu ~24 + surface padding 16 + the cluster +// (~190; ~218 mid-turn with the queue button). +// +// At 260 the three voice toggles fold into the one menu HUD mode already +// uses, clearing the mid-turn worst case with margin. Each stage sits clear +// of the floor below it rather than arriving the instant the previous one +// gives out — the mistake COMPOSER_COMPACT_PILL_PX documents. +export const COMPOSER_FOLD_VOICE_PX = 260 + +// Type and send, nothing else. A pane can be dragged to MIN_PANE_PX (80), and +// even with voice folded the row still costs ~150, so the last rung drops the +// pill AND the voice menu. Both stay reachable — the model by hotkey and the +// full picker, dictation from any wider pane — and Send fits with room to +// spare at any width the layout tree allows (~74 all-in). +export const COMPOSER_MINIMAL_PX = 180 + // A single editor line is ~28px (--composer-input-min-height 1.625rem + 0.5rem // vertical padding). Anything taller means the text wrapped to a second line, // which is when the composer should expand to the stacked layout. diff --git a/apps/desktop/src/app/chat/composer/control-classes.ts b/apps/desktop/src/app/chat/composer/control-classes.ts new file mode 100644 index 0000000000..82285928c7 --- /dev/null +++ b/apps/desktop/src/app/chat/composer/control-classes.ts @@ -0,0 +1,25 @@ +import { cn } from '@/lib/utils' + +// Shared class names for the composer's control row, in a module of their own +// so both the row (`controls.tsx`) and the menus it renders can wear them +// without importing each other in a cycle. + +export const ICON_BTN = 'size-(--composer-control-size) shrink-0 rounded-md' + +export const GHOST_ICON_BTN = cn( + ICON_BTN, + 'text-(--ui-text-tertiary) hover:bg-(--chrome-action-hover) hover:text-foreground' +) + +// Send/voice-conversation primary: solid foreground-on-background circle +// (reads as black-on-white in light mode, white-on-black in dark mode) to +// match the reference composer's high-contrast CTA. Keeps the pill itself +// neutral and lets the action visually dominate the row. +export const PRIMARY_ICON_BTN = cn( + 'size-(--composer-control-primary-size,var(--composer-control-size)) shrink-0 rounded-full p-0', + 'bg-foreground text-background hover:bg-foreground/90', + 'disabled:bg-foreground/30 disabled:text-background disabled:opacity-100' +) + +/** A toggle that is currently ON — dictation, spoken replies, the wake word. */ +export const ACTIVE_ICON_BTN = 'bg-primary/10 text-primary hover:bg-primary/15 hover:text-primary' diff --git a/apps/desktop/src/app/chat/composer/controls.test.tsx b/apps/desktop/src/app/chat/composer/controls.test.tsx index cceb970b9f..bbee021649 100644 --- a/apps/desktop/src/app/chat/composer/controls.test.tsx +++ b/apps/desktop/src/app/chat/composer/controls.test.tsx @@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import type { ChatBarState } from '@/app/chat/composer/types' import { I18nProvider } from '@/i18n' +import { $hudMode } from '@/store/hud' import { applyWakeStartResult, applyWakeStatus, resetWakeWordState } from '@/store/wake-word' import { ComposerControls } from './controls' @@ -57,6 +58,75 @@ async function expectShortcutTooltip(label: string, shortcut: string) { afterEach(() => { cleanup() + $hudMode.set(false) +}) + +// The HUD is a Spotlight bar a few hundred pixels wide: the four voice +// controls fold into one menu there, and the way out of HUD mode joins the +// row instead of floating above the bar in a reserved strip. The docked +// composer keeps every control inline and shows no exit. +describe('HUD mode', () => { + it('keeps the voice controls inline and offers no exit in the docked composer', () => { + renderControls() + + expect(screen.getByLabelText('Voice dictation')).toBeTruthy() + expect(screen.getByLabelText('Read replies aloud')).toBeTruthy() + expect(screen.queryByLabelText('Exit HUD mode')).toBeNull() + expect(screen.queryByLabelText('Voice')).toBeNull() + }) + + it('folds them into one menu and offers the way out in the HUD', () => { + $hudMode.set(true) + renderControls() + + expect(screen.getByLabelText('Voice')).toBeTruthy() + expect(screen.getByLabelText('Exit HUD mode')).toBeTruthy() + + // Folded away, not duplicated — the whole point is the row's width back. + expect(screen.queryByLabelText('Voice dictation')).toBeNull() + expect(screen.queryByLabelText('Read replies aloud')).toBeNull() + }) + + // A collapsed menu that looked idle while the mic was open would be a worse + // trade than the space it saves, so the trigger reports the live state. + it('reports a live voice state on the collapsed trigger', () => { + $hudMode.set(true) + renderControls({ voiceStatus: 'recording' }) + + expect(screen.getByLabelText('Stop dictation')).toBeTruthy() + expect(screen.queryByLabelText('Voice')).toBeNull() + }) +}) + +// A tile can be narrower than the controls cost, and the row is inside an +// overflow-hidden surface — so anything that doesn't fold gets clipped off the +// right edge, send button first. The ladder keeps going past `stacked`: voice +// folds into the same menu the HUD uses, then the model pill drops. Send is +// the last thing standing. +describe('narrow tiles', () => { + it('folds the voice controls into one menu without entering HUD mode', () => { + renderControls({ foldVoice: true }) + + expect(screen.getByLabelText('Voice')).toBeTruthy() + expect(screen.queryByLabelText('Voice dictation')).toBeNull() + expect(screen.queryByLabelText('Read replies aloud')).toBeNull() + + // Folding is a width decision, not the HUD: no exit affordance appears. + expect(screen.queryByLabelText('Exit HUD mode')).toBeNull() + }) + + it('keeps Send at the tightest width, with everything else dropped', () => { + renderControls({ foldVoice: true, minimal: true }) + + expect(screen.getByLabelText('Send')).toBeTruthy() + expect(screen.queryByLabelText('Voice')).toBeNull() + }) + + it('keeps Stop reachable mid-turn at the tightest width', () => { + renderControls({ busy: true, busyAction: 'stop', foldVoice: true, hasComposerPayload: false, minimal: true }) + + expect(screen.getByLabelText('Stop')).toBeTruthy() + }) }) describe('ComposerControls shortcut tooltips', () => { diff --git a/apps/desktop/src/app/chat/composer/controls.tsx b/apps/desktop/src/app/chat/composer/controls.tsx index 286493d006..c48e3eb21b 100644 --- a/apps/desktop/src/app/chat/composer/controls.tsx +++ b/apps/desktop/src/app/chat/composer/controls.tsx @@ -7,26 +7,18 @@ import { useI18n } from '@/i18n' import { triggerHaptic } from '@/lib/haptics' import { AudioLines, Ear, EarOff, iconSize, Layers3, Loader2, Square, Volume2, VolumeX } from '@/lib/icons' import { cn } from '@/lib/utils' +import { $hudMode, closeHud } from '@/store/hud' import { $wakeWord, toggleWakeWord } from '@/store/wake-word' +import { ACTIVE_ICON_BTN, GHOST_ICON_BTN, PRIMARY_ICON_BTN } from './control-classes' import type { ConversationStatus } from './hooks/use-voice-conversation' import { ModelPill } from './model-pill' import type { ChatBarState, VoiceStatus } from './types' +import { VoiceMenu } from './voice-menu' -export const ICON_BTN = 'size-(--composer-control-size) shrink-0 rounded-md' -export const GHOST_ICON_BTN = cn( - ICON_BTN, - 'text-(--ui-text-tertiary) hover:bg-(--chrome-action-hover) hover:text-foreground' -) -// Send/voice-conversation primary: solid foreground-on-background circle -// (reads as black-on-white in light mode, white-on-black in dark mode) to -// match the reference composer's high-contrast CTA. Keeps the pill itself -// neutral and lets the action visually dominate the row. -export const PRIMARY_ICON_BTN = cn( - 'size-(--composer-control-primary-size,var(--composer-control-size)) shrink-0 rounded-full p-0', - 'bg-foreground text-background hover:bg-foreground/90', - 'disabled:bg-foreground/30 disabled:text-background disabled:opacity-100' -) +// Re-exported: `context-menu.tsx` and other row neighbours have always reached +// for these here, and the row is where they read as belonging. +export { ACTIVE_ICON_BTN, GHOST_ICON_BTN, ICON_BTN, PRIMARY_ICON_BTN } from './control-classes' interface ConversationProps { active: boolean @@ -47,7 +39,9 @@ export function ComposerControls({ compactModelPill = false, conversation, disabled, + foldVoice = false, hasComposerPayload, + minimal = false, state, voiceStatus, onDictate, @@ -61,7 +55,9 @@ export function ComposerControls({ compactModelPill?: boolean conversation: ConversationProps disabled: boolean + foldVoice?: boolean hasComposerPayload: boolean + minimal?: boolean state: ChatBarState voiceStatus: VoiceStatus onDictate: () => void @@ -70,6 +66,7 @@ export function ComposerControls({ }) { const { t } = useI18n() const c = t.composer + const hudMode = useStore($hudMode) if (conversation.active) { return @@ -80,13 +77,40 @@ export function ComposerControls({ // only when the composer is empty and a turn is running. const showStop = busy && !hasComposerPayload const showQueueButton = busyAction !== 'stop' && hasComposerPayload + // The HUD is a Spotlight bar a few hundred pixels wide, so the four separate + // voice toggles fold into one menu there and leave the row to the input. A + // narrow tile hits the same wall from the other direction and folds for the + // same reason — same controls, same state, different budget. Below that + // even the menu goes: at `minimal` the row is the send button and nothing + // else, which is the one thing that must survive every width. + const foldedVoice = hudMode || foldVoice - return ( -
- + const voiceControls = foldedVoice ? ( + + ) : ( + <> + + ) + + return ( +
+ {minimal ? null : ( + <> + + {voiceControls} + + )} {showQueueButton ? ( }>
) } +function ExitHudButton() { + const { t } = useI18n() + + return ( + + + + ) +} + function ConversationPill({ disabled, level, @@ -269,11 +320,7 @@ function AutoSpeakButton({ active, disabled, onToggle }: { active: boolean; disa + + + + { + triggerHaptic('open') + onStartConversation() + }} + > + + {c.startVoice} + + + {/* Checkbox items, because all three are toggles the user is reading + the CURRENT state of — the reason they were pressed-state buttons + before. A plain row would fold that state away with the menu. */} + { + // Keep the menu open: dictation is a mode you watch, and closing + // on select hides the recording state the trigger just entered. + event.preventDefault() + triggerHaptic(dictating ? 'close' : 'open') + onDictate() + }} + > + {dictationLabel} + + { + event.preventDefault() + triggerHaptic(autoSpeak ? 'close' : 'open') + onToggleAutoSpeak() + }} + > + {autoSpeak ? : } + {autoSpeak ? c.stopSpeakingReplies : c.speakReplies} + + { + event.preventDefault() + triggerHaptic(wakeListening ? 'close' : 'open') + void toggleWakeWord() + }} + > + {wakeListening ? : } + {wakeLabel} + + + + ) +} diff --git a/apps/desktop/src/app/chat/index.tsx b/apps/desktop/src/app/chat/index.tsx index bf5e4d9cc9..1a7043c3cd 100644 --- a/apps/desktop/src/app/chat/index.tsx +++ b/apps/desktop/src/app/chat/index.tsx @@ -23,6 +23,7 @@ import type { ChatMessage } from '@/lib/chat-messages' import { NEW_SESSION_TITLE, quickModelOptions, sessionTitle } from '@/lib/chat-runtime' import { useIncrementalExternalStoreRuntime } from '@/lib/incremental-external-store-runtime' import { modelOptionsQueryKey, requestModelOptions } from '@/lib/model-options' +import { useStoreSelector } from '@/lib/use-session-slice' import { cn } from '@/lib/utils' import { migrateSessionDraft } from '@/store/composer' import { migrateQueuedPrompts, parkQueuedPrompts } from '@/store/composer-queue' @@ -46,7 +47,7 @@ import { sessionPinId, shouldMigrateComposerScope } from '@/store/session' -import { sessionTileDelegate } from '@/store/session-states' +import { $focusedStoredSessionId, sessionTileDelegate } from '@/store/session-states' import { $transcriptTailBySessionId, transcriptTailState } from '@/store/transcript-tail' import { isAuxiliaryWindow, isWatchWindow } from '@/store/windows' import type { ModelOptionsResponse } from '@/types/hermes' @@ -389,6 +390,13 @@ const ChatViewContent = memo(function ChatViewContent({ const isPrimary = view.kind === 'primary' const activeSessionId = useStore(view.$runtimeId) const storedId = useStore(view.$storedId) + // Multi-pane dimming: only the focused surface paints at full strength, so + // two sessions side by side read as "this one, and that one over there". + // A selector, not a plain useStore — the focused id changes on click, and a + // boolean bails every other surface out of the re-render. Sole surface ⇒ + // always focused (the atom falls back to the primary's selection), so a + // single-pane workspace never dims. + const surfaceFocused = useStoreSelector($focusedStoredSessionId, focused => focused === storedId) // Dock anchor for a session drop onto this surface: the workspace pane for the // primary, this tile's pane id for a tile. Read by the session-drop bridge. const sessionAnchor = isPrimary ? 'workspace' : `session-tile:${storedId ?? ''}` @@ -593,6 +601,7 @@ const ChatViewContent = memo(function ChatViewContent({ className )} data-chat-surface="" + data-chat-unfocused={surfaceFocused ? undefined : ''} data-composer-surface-id={composerSurfaceId} data-composer-target={composerScope.target} data-session-anchor={sessionAnchor} diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.test.ts b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts new file mode 100644 index 0000000000..2da0d2cecd --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts @@ -0,0 +1,316 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { $rightRailActiveTabId } from '@/store/layout' +import { closeRightRail, openPreview, type PreviewTarget } from '@/store/preview' + +import { actOnActivePreview } from './preview-act' +import { registerPreviewInput } from './preview-input' +import { registerPreviewNav } from './preview-nav' +import { registerPreviewScriptRunner } from './preview-script-runner' + +function urlTarget(url: string): PreviewTarget { + return { kind: 'url', label: 'Browser', source: url, url } +} + +describe('actOnActivePreview (drive_preview tool)', () => { + // URL targets share the singleton Browser tab id, so anything a test + // registers would answer the next one. + let cleanups: Array<() => void> = [] + + const openBrowserTab = () => { + openPreview(urlTarget('https://example.com'), 'tool-result') + + return $rightRailActiveTabId.get()! + } + + const withRunner = (runner: (code: string) => Promise) => + cleanups.push(registerPreviewScriptRunner(openBrowserTab(), runner)) + + beforeEach(() => { + vi.useRealTimers() + + for (const cleanup of cleanups) { + cleanup() + } + + cleanups = [] + closeRightRail() + window.localStorage.clear() + }) + + it('tells the agent to open a page when no live pane is behind the tab', async () => { + const result = await actOnActivePreview({ kind: 'elements' }) + + expect(result.success).toBe(false) + expect(result.error).toContain('open_preview') + }) + + it('injects the engine and returns the page’s answer', async () => { + let injected = '' + + withRunner(async code => { + injected = code + + return JSON.stringify({ acted: 'clicked button "Save"', success: true }) + }) + + const result = await actOnActivePreview({ kind: 'click', ref: '@e1' }) + + expect(result).toMatchObject({ acted: 'clicked button "Save"', success: true }) + // Self-contained payload: the engine source and the action travel together, + // and the holder keeps refs alive across calls on the same page. + expect(injected).toContain('__hermesActHolder') + expect(injected).toContain('"ref":"@e1"') + }) + + it('re-inventories after a mutating action so the next ref is current', async () => { + const actions: string[] = [] + + withRunner(code => { + // Stand in for the guest page: run the script's own settle/rescan shape + // by answering each act() call in order. + actions.push(...(code.match(/"kind":"(\w+)"/g) ?? [])) + + return Promise.resolve( + JSON.stringify({ + acted: 'clicked', + elements: [{ label: 'Log out', ref: '@e1', role: 'button', selector: '#out' }], + success: true, + url: 'https://example.com/app' + }) + ) + }) + + const result = await actOnActivePreview({ kind: 'click', ref: '@e1' }) + + expect(result.elements?.[0].label).toBe('Log out') + expect(result.url).toBe('https://example.com/app') + }) + + it('does not pay the settle delay for a plain inventory', async () => { + let injected = '' + withRunner(async code => { + injected = code + + return JSON.stringify({ elements: [], success: true }) + }) + + await actOnActivePreview({ kind: 'elements' }) + + expect(injected).toContain('0 <= 0') + }) + + it('reports a page that answers with nothing', async () => { + withRunner(async () => '') + + expect((await actOnActivePreview({ kind: 'click', ref: '@e1' })).error).toContain('did not answer') + }) + + /** A pane that answers the locate trip with a fixed on-screen point, and the + * read-back trip with an empty inventory. Returns the input spy. */ + const withDrivenPane = () => { + const tabId = openBrowserTab() + const send = vi.fn() + + cleanups.push( + registerPreviewScriptRunner(tabId, async code => + code.includes('"kind":"locate"') + ? JSON.stringify({ acted: 'looking at button "Save"', point: { x: 120, y: 80 }, success: true }) + : // `hit` is the page's witness that the real pointerdown arrived. + JSON.stringify({ elements: [], hit: { tag: 'BUTTON', trusted: true }, success: true }) + ) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send })) + + return send + } + + const sentTypes = (send: ReturnType) => send.mock.calls.map(([event]) => event.type) + + it('clicks with real input, walking the pointer to where the page said', async () => { + const send = withDrivenPane() + + const result = await actOnActivePreview({ kind: 'click', ref: '@e1' }) + const types = sentTypes(send) + + // Stepped rather than teleported: a page learns it is hovered from a stream + // of moves, and one jump to the target skips everything in between. + expect(types.filter(type => type === 'mouseMove').length).toBeGreaterThan(1) + expect(types).toContain('mouseDown') + expect(types).toContain('mouseUp') + expect(send.mock.calls.map(([event]) => event).find(event => event.type === 'mouseDown')).toMatchObject({ + x: 120, + y: 80 + }) + expect(result.acted).toBe('clicked button "Save"') + }) + + it('types by pressing keys, after selecting whatever the field held', async () => { + const send = withDrivenPane() + + await actOnActivePreview({ kind: 'type', ref: '@e1', submit: true, text: 'hi' }) + + const events = send.mock.calls.map(([event]) => event) + const chars = events.filter(event => event.type === 'char').map(event => event.keyCode) + + // One click to focus, then select-all by keyboard — NOT the triple-click + // this used to do. A triple-click is a pointer gesture, so it grabs the + // paragraph under the cursor whenever the target turns out not to be a + // field, and the agent was leaving pages with their body text highlighted. + expect(events.filter(event => event.type === 'mouseDown').map(event => event.clickCount)).toEqual([1]) + expect(events.filter(event => event.type === 'keyDown' && event.keyCode === 'a')[0]).toMatchObject({ + modifiers: ['control', 'meta'] + }) + // The chord must not send a `char` phase, or select-all types a literal 'a'. + expect(chars).toEqual(['h', 'i', 'Enter']) + }) + + it('hovers by walking the pointer over and leaving it there', async () => { + const send = withDrivenPane() + + const result = await actOnActivePreview({ kind: 'hover', ref: '@e1' }) + const types = sentTypes(send) + + expect(types).toContain('mouseMove') + // The whole request is "be on it" — a click here would open the dropdown the + // agent was trying to reveal, or worse, activate it. + expect(types).not.toContain('mouseDown') + expect(result.acted).toBe('hovered over button "Save"') + }) + + // The witness only speaks for verbs that put the button down. Demanding one + // from a key press reported every press as a failure. + it('does not expect a click witness from a verb that never clicks', async () => { + const tabId = openBrowserTab() + + cleanups.push( + registerPreviewScriptRunner(tabId, async code => + code.includes('"kind":"locate"') + ? JSON.stringify({ acted: 'looking at textbox "Search"', point: { x: 40, y: 20 }, success: true }) + : JSON.stringify({ elements: [], hit: null, success: true }) + ) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send: vi.fn() })) + + for (const action of [ + { key: 'Escape', kind: 'press', ref: '@e1' }, + { kind: 'hover', ref: '@e1' } + ]) { + expect(await actOnActivePreview(action)).toMatchObject({ success: true }) + } + }) + + it('scrolls by wheeling for real, not by scripting the page', async () => { + const tabId = openBrowserTab() + const send = vi.fn() + let scripted = false + + cleanups.push( + registerPreviewScriptRunner(tabId, async code => { + scripted ||= code.includes('"kind":"scroll"') + + return code.includes('scrollHeight') + ? JSON.stringify({ page: 700, point: { x: 500, y: 400 }, span: 4_000, success: true }) + : JSON.stringify({ elements: [], success: true }) + }) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send })) + + await actOnActivePreview({ kind: 'scroll' }) + + const wheels = send.mock.calls.map(([event]) => event).filter(event => event.type === 'mouseWheel') + + // A stream of notches, not one delta: scroll-linked headers and lazy loaders + // only react to the events, so a scripted scrollBy leaves them asleep. + expect(wheels.length).toBeGreaterThan(1) + // Electron's wheel delta is wheelDelta-signed, so scrolling DOWN is negative. + expect(wheels.every(event => event.deltaY < 0)).toBe(true) + expect(scripted).toBe(false) + }) + + it('says so plainly when the page has nothing to scroll', async () => { + const tabId = openBrowserTab() + + cleanups.push( + registerPreviewScriptRunner(tabId, async () => + JSON.stringify({ page: 700, point: { x: 500, y: 400 }, span: 0, success: true }) + ) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send: vi.fn() })) + + expect(await actOnActivePreview({ kind: 'scroll' })).toMatchObject({ + note: expect.stringContaining('nothing to scroll') + }) + }) + + it('fails loudly when the pointer input never reaches the page', async () => { + const tabId = openBrowserTab() + + cleanups.push( + registerPreviewScriptRunner(tabId, async code => + code.includes('"kind":"locate"') + ? JSON.stringify({ acted: 'looking at button "Save"', point: { x: 12, y: 8 }, success: true }) + : JSON.stringify({ elements: [], hit: null, success: true }) + ) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send: vi.fn() })) + + const result = await actOnActivePreview({ kind: 'click', ref: '@e1' }) + + // Everything else about this action travels on the script channel and would + // report success whether or not a single event landed. + expect(result.success).toBe(false) + expect(result.error).toContain('never reached the page') + }) + + it('says so when the overlay itself swallowed the click', async () => { + const tabId = openBrowserTab() + + cleanups.push( + registerPreviewScriptRunner(tabId, async code => + code.includes('"kind":"locate"') + ? JSON.stringify({ acted: 'looking at button "Save"', point: { x: 12, y: 8 }, success: true }) + : JSON.stringify({ elements: [], hit: { tag: 'HERMES-WATCH', trusted: true }, success: true }) + ) + ) + cleanups.push(registerPreviewInput(tabId, { focus: vi.fn(), send: vi.fn() })) + + expect((await actOnActivePreview({ kind: 'click', ref: '@e1' })).note).toContain('overlay intercepted') + }) + + it('falls back to scripted events when the pane exposes no input channel', async () => { + let injected = '' + + withRunner(async code => { + injected = code + + return JSON.stringify({ acted: 'clicked', success: true }) + }) + + await actOnActivePreview({ kind: 'click', ref: '@e1' }) + + // The one-trip shape: the engine both acts and re-reads, no locate handshake. + expect(injected).toContain('"kind":"click"') + expect(injected).not.toContain('"kind":"locate"') + }) + + it('routes history verbs to the pane instead of the guest page', async () => { + const back = vi.fn() + const runner = vi.fn() + + const tabId = openBrowserTab() + cleanups.push(registerPreviewNav(tabId, { back, forward: vi.fn(), reload: vi.fn() })) + cleanups.push(registerPreviewScriptRunner(tabId, runner)) + + const result = await actOnActivePreview({ kind: 'back' }) + + expect(back).toHaveBeenCalledOnce() + expect(runner).not.toHaveBeenCalled() + expect(result.success).toBe(true) + expect(result.note).toContain('elements') + }) + + it('reports history verbs with no pane to drive', async () => { + expect((await actOnActivePreview({ kind: 'reload' })).error).toContain('open_preview') + }) +}) diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.ts b/apps/desktop/src/app/chat/right-rail/preview-act.ts new file mode 100644 index 0000000000..959e06927b --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-act.ts @@ -0,0 +1,567 @@ +/** + * PREVIEW ACT — performs the agent's interactions inside the preview pane's + * guest page, so `drive_preview` drives whatever web app is open in the in-app + * browser. + * + * The guest page is out-of-process; nothing here can touch its DOM directly. + * Two separate channels reach it, and the split matters: + * + * - `executeJavaScript` injects the engine SOURCE (see + * lib/preview-act/act-in-page.ts's self-containment contract) to RESOLVE + * and READ — turn 'btn-sign-in' into a node, measure it, inventory the + * page. It is parked on a window global alongside the book of handles so + * they survive between calls, and it vanishes with the page, so a + * navigation retires them and the engine reports that instead of acting on + * the wrong node. + * - `sendInputEvent` (preview-drive.ts) does the ACTING, as real Chromium + * input. Script can only dispatch synthetic events, which the page can tell + * apart and which never move the browser's own hover or focus target. + * + * Panes with no input channel — a remote HTML preview — fall back to the + * engine's synthetic events, which is worse but still works. + * + * A mutating action settles briefly and returns a fresh inventory, so the + * click → re-read → click loop costs one round trip instead of two. + * + * Dynamic-imported by the gateway event handler so the engine payload stays + * out of the boot path. + */ + +import { actEngineSource, type PreviewActAction, type PreviewActResult } from '@/lib/preview-act/act-in-page' +import { watchInPage } from '@/lib/preview-act/watch-in-page' + +import { clickAt, glideTo, pointerPlaced, pressKey, selectAll, typeText, wheelBy } from './preview-drive' +import { activePreviewInput, type PreviewInputHandle } from './preview-input' +import { activePreviewNav, type PreviewNavHandle } from './preview-nav' +import { activePreviewScriptRunner, type PreviewScriptRunner } from './preview-script-runner' + +/** Verbs the pane owns; a guest page cannot drive its own history. */ +const NAV_ACTIONS: readonly (keyof PreviewNavHandle)[] = ['back', 'forward', 'reload'] + +/** How long a click/type is given to land before the page is re-inventoried. + * Long enough for a framework re-render, short enough not to stall the turn. + * A render that misses this window is not lost — the NEXT action re-reads the + * page anyway, so the cost of being early is a slightly stale inventory, not a + * wrong one. That trade is worth several hundred ms on every single step. */ +const SETTLE_MS = 220 + +/** Cap on one round trip into the guest page. A click that starts a navigation + * tears the document down mid-settle, so the injected promise dies with it and + * `executeJavaScript` never settles — without this the tool eats its whole + * 45s bridge deadline for what was actually a successful click. */ +const ACT_TIMEOUT_MS = 8_000 + +/** Verbs that get the look-then-act treatment: locate the target, walk the + * pointer there, then send real input. Actions with no single target (scroll, + * elements) are absent and skip it. */ +const DRIVEN: readonly string[] = ['click', 'hover', 'press', 'type'] + +/** Verbs that put the mouse button down, and so are the only ones the pointerdown + * witness can speak to. `press` and `hover` never click, so demanding a witness + * from them would report every one of them as a failure. */ +const CLICKS: readonly string[] = ['click', 'type'] + +const NOTHING_OPEN = 'No live page is open in the in-app browser — open one with open_preview first.' + +const NAVIGATED = + 'The page stopped answering right after — it is probably navigating. Call elements to see where you landed.' + +/** A fingerprint of the overlay's source, so the guest page can tell that the + * code it is running has changed underneath it. + * + * It needs one because the overlay memoizes its own chrome: the cursor, the + * lock frame and the scan line are built ONCE per page and then reused, so an + * edit to any of their styles lands in the injected source, gets injected, and + * changes nothing — the layers it would have styled were built by the previous + * version and are still on the page. In dev that reads as "HMR didn't pick up + * my CSS"; in production it is a long-lived tab pinned to whichever build first + * touched it, across an app upgrade. + * + * Content-addressed rather than a length or a version literal: the change that + * exposed this was one hex colour for another, which is byte-for-byte the same + * length, and a hand-maintained version is a step someone forgets. */ +const WATCH_TAG = (() => { + const source = watchInPage.toString() + let hash = 5381 + + for (let i = 0; i < source.length; i++) { + hash = ((hash << 5) + hash + source.charCodeAt(i)) | 0 + } + + return hash +})() + +/** Injected once per page: the engine, the overlay, and the two helpers every + * script below shares. Idempotent — re-running it on a live page is a no-op. */ +const preamble = () => ` var w = window; + // Reassigned on every trip rather than cached behind a guard: the source is in + // this payload either way, so the guard saved nothing and pinned a long-lived + // tab to whichever build first touched it. The holder is the exception — it + // carries the aimed element from the locate trip to the act trip. + w.__hermesActHolder = w.__hermesActHolder || {}; + w.__hermesAct = ${actEngineSource()}; + w.__hermesWatch_fn = (${watchInPage.toString()}); + w.__hermesWatchTag = ${WATCH_TAG}; + var holder = w.__hermesActHolder; + var act = function (a) { return w.__hermesAct(document, holder, a); }; + var watch = function (stage, label) { + try { w.__hermesWatch_fn(document, holder, stage, label); } catch (err) {} + }; + var wait = function (ms) { return new Promise(function (resolve) { setTimeout(resolve, ms); }); }; + // Sit out a smooth scroll so the pointer is aimed at a target that has stopped + // moving. A scroll that never started fires no scrollend, so only wait on one + // we can actually see happening; capture catches nested scrollers, whose + // scrollend does not bubble. + var restAfterScroll = function () { + return new Promise(function (resolve) { + var sc = document.scrollingElement || document.documentElement; + var x0 = sc ? sc.scrollLeft : 0; + var y0 = sc ? sc.scrollTop : 0; + requestAnimationFrame(function () { + if (!sc || (sc.scrollLeft === x0 && sc.scrollTop === y0)) { return resolve(); } + var done = false; + var finish = function () { + if (done) { return; } + done = true; + document.removeEventListener('scrollend', finish, true); + clearTimeout(timer); + resolve(); + }; + var timer = setTimeout(finish, 350); + document.addEventListener('scrollend', finish, true); + }); + }); + };` + +/** Bring the target on screen, mark it, and report where it came to rest. */ +function buildLocateScript(action: PreviewActAction, focus: boolean): string { + const locate = { focus, kind: 'locate', ref: action.ref, selector: action.selector } + + return `(function () { +${preamble()} + var locate = ${JSON.stringify(locate)}; + var found = act(locate); + if (!found.success) { return Promise.resolve(JSON.stringify(found)); } + watch('aim'); + // Arm a witness for the real input that is about to arrive. Without it a + // click that never reached the page is indistinguishable from one the page + // ignored, and the agent would report success either way. + w.__hermesHit = null; + document.addEventListener('pointerdown', function (e) { + w.__hermesHit = { tag: e.target ? e.target.tagName : '?', trusted: e.isTrusted === true }; + }, { capture: true, once: true }); + // Measure again once the scroll has stopped: real input is aimed at a fixed + // viewport coordinate, so it has to be where the target ENDS UP. + return restAfterScroll().then(function () { + var settled = act(locate); + var best = settled.success ? settled : found; + var at = best.point; + // Last line of defence before the pointer is sent somewhere real. An element + // that is still outside the viewport after we scrolled to it is hidden, not + // placed, and aiming at it would drive the cursor off into a corner and + // click whatever happens to be under that coordinate. + if (at && (at.x < 0 || at.y < 0 || at.x > window.innerWidth || at.y > window.innerHeight)) { + return JSON.stringify({ + error: 'That element is still off-screen after scrolling to it, so it is hidden rather than clickable. Call elements again for what is really on the page.', + success: false + }); + } + return JSON.stringify(best); + }); +})()` +} + +/** Put up a mark that outlives the action that made it. Every other cue on the + * overlay retires on a timer, which is right for narrating a click and no use + * at all for holding a finding on screen while the agent keeps working. */ +function buildPinScript(action: PreviewActAction, label: string): string { + const locate = { kind: 'locate', ref: action.ref, selector: action.selector } + + return `(function () { +${preamble()} + var found = act(${JSON.stringify(locate)}); + if (!found.success) { return JSON.stringify(found); } + watch('pin', ${JSON.stringify(label)}); + return JSON.stringify({ acted: 'pinned ' + String(found.acted || 'it').replace(/^looking at /, ''), success: true }); +})()` +} + +/** Freeze the whole visible field as marks. Needs a fresh inventory first, so + * the overlay has both the field to draw and the refs to number it by. */ +function buildHoldScript(): string { + return `(function () { +${preamble()} + var found = act({ kind: 'elements' }); + if (!found.success) { return JSON.stringify(found); } + watch('hold'); + found.acted = 'held the field'; + return JSON.stringify(found); +})()` +} + +/** Rattle through the field one box at a time. Needs a fresh inventory for the + * overlay to pick from, and nothing else — the page is never touched. */ +function buildStrobeScript(): string { + return `(function () { +${preamble()} + var found = act({ kind: 'elements' }); + if (!found.success) { return JSON.stringify(found); } + watch('strobe'); + found.acted = 'strobed the field'; + return JSON.stringify(found); +})()` +} + +/** Take one mark down, or — with nothing to aim at — all of them. */ +function buildUnpinScript(action: PreviewActAction): string { + const locate = { kind: 'locate', ref: action.ref, selector: action.selector } + const one = !!(action.ref || action.selector) + + return `(function () { +${preamble()} +${ + one + ? ` var found = act(${JSON.stringify(locate)}); + if (!found.success) { return JSON.stringify(found); } + var gone = 'unpinned ' + String(found.acted || 'it').replace(/^looking at /, '');` + : ` holder.aimed = null; + var gone = 'cleared every pin';` +} + watch('unpin'); + return JSON.stringify({ acted: gone, success: true }); +})()` +} + +/** Mark the hit, let the page react, and hand back a fresh inventory. */ +function buildFinishScript(settleMs: number): string { + return `(function () { +${preamble()} + watch('strike'); + return wait(${settleMs}).then(function () { + // A rescan that throws must still answer — an unresolved promise here costs + // the whole bridge deadline. + try { + var out = act({ kind: 'elements' }); + watch('sweep'); + out.hit = w.__hermesHit || null; + return JSON.stringify(out); + } catch (err) { + return JSON.stringify({ note: 'The page changed before it could be re-read: ' + err, success: true }); + } + }); +})()` +} + +/** The no-real-input fallback: the engine both acts and reads, in one trip. + * Both reads mark the field — the explicit `elements` call and the re-read + * every other verb does on its way out — so the page is marked after every + * single action rather than only when the agent asks for an inventory outright. + * They mark it differently, though, and the difference is what each one MEANS: + * an outright `elements` is the agent reading the whole page, which is the + * moment worth showing, so it strobes. The re-read after a click is + * housekeeping, and strobing there would put five seconds of noise between + * every step of a task, so it sweeps. The two never collide: an `elements` call + * settles in 0ms and returns before the re-read. */ +function buildScriptedScript(action: PreviewActAction, settleMs: number): string { + return `(function () { +${preamble()} + var result = act(${JSON.stringify(action)}); + ${action.kind === 'elements' ? "if (result.success) { watch('strobe'); }" : ''} + if (!result.success || ${settleMs} <= 0) { return Promise.resolve(JSON.stringify(result)); } + return wait(${settleMs}).then(function () { + try { + var after = act({ kind: 'elements' }); + watch('sweep'); + // One or the other, never both: a re-read answers with the whole + // inventory only when it is the first look at this page. + result.elements = after.elements; + result.delta = after.delta; + result.url = after.url; + result.title = after.title; + } catch (err) { + result.note = 'The page changed before it could be re-read: ' + err; + } + return JSON.stringify(result); + }); +})()` +} + +/** The outcome of one round trip into the page. `silent` is its own case on + * purpose: a page that stops answering mid-action is navigating, whereas one + * that answers with nothing is broken, and the agent needs to hear the + * difference. */ +type Trip = { error: string; kind: 'failed' } | { kind: 'answered'; result: PreviewActResult } | { kind: 'silent' } + +async function runJson(run: PreviewScriptRunner, code: string): Promise { + const raw = await Promise.race([ + run(code).catch((error: unknown) => new Error(String(error))), + new Promise(resolve => setTimeout(resolve, ACT_TIMEOUT_MS)) + ]) + + if (raw === undefined) { + return { kind: 'silent' } + } + + if (raw instanceof Error) { + return { error: 'The page rejected the action: ' + raw.message, kind: 'failed' } + } + + if (typeof raw !== 'string' || !raw) { + return { error: 'The page did not answer the action.', kind: 'failed' } + } + + return { kind: 'answered', result: JSON.parse(raw) as PreviewActResult } +} + +/** Past tense of the verb the agent asked for, against what it actually hit. */ +function describeDone(action: PreviewActAction, target: string): string { + if (action.kind === 'type') { + return 'typed into ' + target + (action.submit ? ' and submitted' : '') + } + + if (action.kind === 'press') { + return 'pressed ' + (action.key || '') + ' on ' + target + } + + if (action.kind === 'hover') { + return 'hovered over ' + target + } + + return 'clicked ' + target +} + +/** Look at the target, walk the pointer over, and act on it for real. */ +async function driveAction( + run: PreviewScriptRunner, + input: PreviewInputHandle, + action: PreviewActAction +): Promise { + // A key press must not be preceded by a click — that would activate the + // control rather than type into it — so the page hands it focus instead. + const trip = await runJson(run, buildLocateScript(action, action.kind === 'press')) + + if (trip.kind === 'failed') { + return { error: trip.error, success: false } + } + + if (trip.kind === 'silent') { + return { acted: action.kind, note: NAVIGATED, success: true } + } + + const found = trip.result + + if (!found.success) { + return found + } + + if (!found.point) { + return { error: 'Could not work out where that element is on screen.', success: false } + } + + await glideTo(input, found.point) + + if (action.kind === 'click') { + await clickAt(input) + } else if (action.kind === 'type') { + if (found.typable === false) { + return { + error: `${String(found.acted || 'That').replace(/^looking at /, '')} is not a text field, so typing into it would only select the text under the pointer. Click it if it opens one, then type into that.`, + success: false + } + } + + input.focus() + await clickAt(input) + // Select-all inside the now-focused field, so typing replaces what is there + // the way it would for a person. NOT a triple-click: that is a pointer + // gesture and selects the paragraph under the cursor whenever the target + // turns out not to be a field. + await selectAll(input) + await typeText(input, action.text ?? '') + + if (action.submit) { + await pressKey(input, 'Enter') + } + } else if (action.kind === 'press') { + input.focus() + await pressKey(input, action.key || 'Enter') + } + // hover is the glide and nothing else — the pointer is already sitting on the + // target, which is the whole request. + + const target = String(found.acted || '').replace(/^looking at /, '') + const after = await runJson(run, buildFinishScript(SETTLE_MS)) + const acted = describeDone(action, target) + + // The action itself already happened as real input, so a page that will not + // answer the read-back is a page that navigated — never a failed click. + if (after.kind !== 'answered') { + return { acted, note: NAVIGATED, success: true } + } + + const { hit, ...result } = after.result as PreviewActResult & { hit?: { tag: string; trusted: boolean } | null } + + // The witness the locate trip armed. No record means the input never reached + // the document, which the agent must hear about — every other signal here + // travels on the script channel and would report success regardless. + if (!hit && CLICKS.indexOf(action.kind) !== -1) { + return { + ...result, + error: 'The pointer input never reached the page, so nothing was ' + acted.split(' ')[0] + '.', + success: false + } + } + + return { ...result, acted, note: hitNote(hit), success: true } +} + +/** Flag a click the overlay intercepted, which would otherwise look like a page + * that simply ignored it. */ +function hitNote(hit?: { tag: string; trusted: boolean } | null): string | undefined { + return hit && hit.tag === 'HERMES-WATCH' ? 'The action overlay intercepted the click instead of the page.' : undefined +} + +/** How far a screenful is, whether there is anywhere to go, and a spot to wheel + * over — the renderer cannot see the guest viewport to work any of it out. */ +function buildScrollAnchorScript(): string { + return `(function () { +${preamble()} + var sc = document.scrollingElement || document.documentElement; + var track = sc.clientHeight || window.innerHeight; + return Promise.resolve(JSON.stringify({ + page: Math.round(window.innerHeight * 0.9), + point: { x: Math.round(window.innerWidth / 2), y: Math.round(track / 2) }, + span: sc.scrollHeight - track, + success: true + })); +})()` +} + +/** Scroll the page the way a hand does — wheel it. The scripted path calls + * `scrollBy`, which moves the page without the page ever seeing an input + * event, so scroll-linked headers, reveal animations and infinite-scroll + * loaders all stay asleep through a scroll that looked like it happened. */ +async function driveScroll( + run: PreviewScriptRunner, + input: PreviewInputHandle, + action: PreviewActAction +): Promise { + const far = action.amount ?? 0 + + const trip = await runJson(run, buildScrollAnchorScript()) + + if (trip.kind === 'failed') { + return { error: trip.error, success: false } + } + + if (trip.kind === 'silent') { + return { acted: 'scrolled', note: NAVIGATED, success: true } + } + + const anchor = trip.result as PreviewActResult & { page?: number; span?: number } + + if (!anchor.span) { + return { ...anchor, acted: 'scrolled the page', note: 'The page has nothing to scroll — it all fits already.' } + } + + // A person does not move the mouse to scroll; the wheel turns wherever their + // hand already is. Only send it somewhere if it has never been anywhere. + if (!pointerPlaced() && anchor.point) { + await glideTo(input, anchor.point) + } + + await wheelBy(input, action.amount ?? anchor.page ?? 600) + + const after = await runJson(run, buildFinishScript(SETTLE_MS)) + + if (after.kind !== 'answered') { + return { acted: 'scrolled the page', note: NAVIGATED, success: true } + } + + return { ...after.result, acted: 'scrolled the page', success: true } +} + +/** Run one action against the ACTIVE preview tab's page. `kind` is a bare + * string: the verb arrives off the wire, and the history ones never reach + * the in-page engine. */ +export async function actOnActivePreview( + action: Omit & { kind: string } +): Promise { + const nav = NAV_ACTIONS.find(verb => verb === action.kind) + + if (nav) { + const handle = activePreviewNav() + + if (!handle) { + return { error: NOTHING_OPEN, success: false } + } + + handle[nav]() + + // Navigation is fire-and-forget through the webview; the new document has + // its own refs, so the agent has to re-inventory either way. + return { acted: nav, note: 'Page is loading — call elements to see what is on it.', success: true } + } + + const run = activePreviewScriptRunner() + + if (!run) { + return { error: NOTHING_OPEN, success: false } + } + + const typed = action as PreviewActAction + + // Annotation, not interaction: nothing is clicked, nothing settles, and the + // page is not re-read, so these skip the whole act-then-inventory path. + if (typed.kind === 'pin' || typed.kind === 'unpin' || typed.kind === 'hold' || typed.kind === 'strobe') { + const mark = + typed.kind === 'hold' + ? buildHoldScript() + : typed.kind === 'strobe' + ? buildStrobeScript() + : typed.kind === 'pin' + ? buildPinScript(typed, typed.text || '') + : buildUnpinScript(typed) + + const trip = await runJson(run, mark) + + if (trip.kind === 'failed') { + return { error: trip.error, success: false } + } + + return trip.kind === 'answered' ? trip.result : { acted: typed.kind, note: NAVIGATED, success: true } + } + + const input = activePreviewInput() + + if (input && DRIVEN.indexOf(typed.kind) !== -1) { + return driveAction(run, input, typed) + } + + // A plain page scroll is a wheel gesture. Jumping to an end is not — no hand + // wheels to the bottom of a long article — so `to` stays a scripted glide. + const plain = typed.kind === 'scroll' && !typed.to && !typed.ref && !typed.selector + + if (plain && input) { + return driveScroll(run, input, typed) + } + + const settle = typed.kind === 'elements' ? 0 : SETTLE_MS + const scripted = await runJson(run, buildScriptedScript(typed, settle)) + + if (scripted.kind === 'failed') { + return { error: scripted.error, success: false } + } + + // The action almost certainly landed — a page that stops answering right + // after a click is one that navigated. Say so instead of failing it. + return scripted.kind === 'silent' ? { acted: typed.kind, note: NAVIGATED, success: true } : scripted.result +} + +// Self-accept so an edit here, or to the in-page sources this module +// stringifies, doesn't reload the whole renderer out from under a live session. +// The bridge re-requests this module per action in dev, so it picks the change +// up without one (see loadPreviewEngine in gateway-event/desktop-bridge.ts). +if (import.meta.hot) { + import.meta.hot.accept() +} diff --git a/apps/desktop/src/app/chat/right-rail/preview-drive.ts b/apps/desktop/src/app/chat/right-rail/preview-drive.ts new file mode 100644 index 0000000000..18b9dbf24e --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-drive.ts @@ -0,0 +1,138 @@ +/** + * PREVIEW DRIVE — the agent's hand on the mouse and keyboard. + * + * Everything here goes out through `sendInputEvent`, so the guest page receives + * ordinary trusted input: the pointer really moves across it, `:hover` really + * matches, focus really lands, and a menu that only exists while hovered is + * actually open by the time the click arrives. That last part is the whole + * point — a synthetic `el.click()` on a dropdown item fires at a node the page + * never rendered, which is why script-driven clicking quietly misses. + * + * The pointer is walked to its target in steps rather than teleported, for the + * same reason: a page learns it is hovered from a stream of moves, and one + * move from nowhere to the target skips every element in between. Fourteen + * steps over ~280ms is what Playwright's action cursor settles on too. + */ + +import type { PreviewInputHandle } from './preview-input' + +export interface DrivePoint { + x: number + y: number +} + +/** The pointer JUMPS. These few steps over a couple of frames exist only so the + * page receives a move stream at all — `:hover`, `pointerenter` and the menus + * that hang off them need more than a single teleport to resolve — not so the + * travel can be watched. A machine does not sweep a mouse across a screen. */ +const GLIDE_STEPS = 3 +const GLIDE_MS = 36 + +/** Gap between keystrokes. Fast enough not to pad the turn, slow enough that a + * page debouncing its input handler still sees them as separate. */ +const KEY_MS = 7 + +/** Notches in a wheel gesture, and how long the gesture takes. Sent as a stream + * rather than one big delta so scroll-linked effects — sticky headers, reveal + * animations, infinite-scroll loaders — get the same series of events a hand + * would produce, and so it looks like momentum rather than a jump cut. */ +const WHEEL_STEPS = 12 +const WHEEL_MS = 240 + +/** Where the pointer was left, so the next glide starts from there instead of + * jumping. Module-level because the pointer is a property of the pane, not of + * any one action. The origin is a placeholder, not a position — see below. */ +let pointer: DrivePoint = { x: 0, y: 0 } +let placed = false + +/** Whether the pointer has ever been sent anywhere. Until it has, its notional + * spot is the top-left corner, which is a real coordinate a caller can wheel + * or click at by accident — so callers that only need it to be SOMEWHERE + * sensible have to know the difference. */ +export function pointerPlaced(): boolean { + return placed +} + +const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) + +/** Decelerating, like a hand arriving at a target rather than a linear sweep. */ +const easeOut = (t: number) => 1 - Math.pow(1 - t, 3) + +/** Walk the pointer to `to`, letting the page hover everything on the way. */ +export async function glideTo(input: PreviewInputHandle, to: DrivePoint): Promise { + const from = pointer + + for (let step = 1; step <= GLIDE_STEPS; step++) { + const progress = easeOut(step / GLIDE_STEPS) + + input.send({ + type: 'mouseMove', + x: Math.round(from.x + (to.x - from.x) * progress), + y: Math.round(from.y + (to.y - from.y) * progress) + }) + await wait(GLIDE_MS / GLIDE_STEPS) + } + + pointer = to + placed = true +} + +/** Press and release at the pointer's current spot. `clicks` of 3 selects the + * text under it, which is how a field gets cleared without a modifier key. */ +export async function clickAt(input: PreviewInputHandle, clicks = 1): Promise { + for (let click = 1; click <= clicks; click++) { + input.send({ button: 'left', clickCount: click, type: 'mouseDown', x: pointer.x, y: pointer.y }) + input.send({ button: 'left', clickCount: click, type: 'mouseUp', x: pointer.x, y: pointer.y }) + await wait(KEY_MS) + } +} + +/** Wheel `down` pixels' worth of notches at the pointer's current spot; negative + * scrolls up. Electron's wheel delta is the legacy `wheelDelta` sign — positive + * moves the content down, i.e. scrolls UP — so it is the inverse of the number + * a caller asks for, and of DOM `WheelEvent.deltaY`. */ +export async function wheelBy(input: PreviewInputHandle, down: number): Promise { + let sent = 0 + + for (let step = 1; step <= WHEEL_STEPS; step++) { + const so_far = Math.round(down * easeOut(step / WHEEL_STEPS)) + const notch = so_far - sent + + sent = so_far + + if (notch) { + input.send({ deltaX: 0, deltaY: -notch, type: 'mouseWheel', x: pointer.x, y: pointer.y }) + } + + await wait(WHEEL_MS / WHEEL_STEPS) + } +} + +/** Send one key the long way round, so a page watching any of the three sees it. */ +export async function pressKey(input: PreviewInputHandle, key: string): Promise { + input.send({ keyCode: key, type: 'keyDown' }) + input.send({ keyCode: key, type: 'char' }) + input.send({ keyCode: key, type: 'keyUp' }) + await wait(KEY_MS) +} + +/** Select-all inside whatever has focus, which for a focused field is that + * field's own text and nothing else. This replaced a triple-click: a triple + * click is a POINTER gesture, so it selects whatever paragraph sits under the + * cursor whenever the target turns out not to be a field, and the agent was + * leaving pages with their body text highlighted. There is no `char` phase — + * a chord is not text entry, and sending one types a literal 'a'. */ +export async function selectAll(input: PreviewInputHandle): Promise { + const chord = ['control', 'meta'] + + input.send({ keyCode: 'a', modifiers: chord, type: 'keyDown' }) + input.send({ keyCode: 'a', modifiers: chord, type: 'keyUp' }) + await wait(KEY_MS) +} + +/** Type `text` a character at a time into whatever currently has focus. */ +export async function typeText(input: PreviewInputHandle, text: string): Promise { + for (const character of text) { + await pressKey(input, character) + } +} diff --git a/apps/desktop/src/app/chat/right-rail/preview-input.ts b/apps/desktop/src/app/chat/right-rail/preview-input.ts new file mode 100644 index 0000000000..c0f1dbb5a0 --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-input.ts @@ -0,0 +1,56 @@ +/** + * PREVIEW INPUT REGISTRY — real input into the preview pane's guest page, the + * difference between the agent DRIVING the browser and merely poking its DOM. + * + * `executeJavaScript` can only ever dispatch synthetic events: `isTrusted` is + * false, the browser's own hover target never moves, `:hover` rules never + * match, and hover-gated menus never open — so a click lands on a dropdown item + * that was never rendered. `sendInputEvent` goes in through Chromium's input + * pipeline instead, producing the same events a hand on the mouse would. + * + * It has to be called on the `` ELEMENT. Sending to the embedder's + * webContents does not reach a guest (electron/electron#20333), which is why + * this is a per-pane registry rather than something main could do. + * + * Coordinates are relative to the webview, and the webview IS the guest + * viewport — so a rect the act engine measured inside the page needs no + * conversion on the way back out. + */ + +import { $rightRailActiveTabId } from '@/store/layout' +import { $previewTabs } from '@/store/preview' + +/** The subset of Electron's input events the agent needs to drive a page. */ +export type PreviewInputEvent = + | { button: 'left'; clickCount: number; type: 'mouseDown' | 'mouseUp'; x: number; y: number } + | { deltaX: number; deltaY: number; type: 'mouseWheel'; x: number; y: number } + | { keyCode: string; modifiers?: string[]; type: 'char' | 'keyDown' | 'keyUp' } + | { type: 'mouseMove'; x: number; y: number } + +export interface PreviewInputHandle { + /** Give the guest keyboard focus, so key events reach its active element. */ + focus: () => void + send: (event: PreviewInputEvent) => void +} + +const handles = new Map() + +/** Register a live pane's input channel; returns an idempotent unregister. */ +export function registerPreviewInput(tabId: string, handle: PreviewInputHandle): () => void { + handles.set(tabId, handle) + + return () => { + if (handles.get(tabId) === handle) { + handles.delete(tabId) + } + } +} + +/** The ACTIVE preview tab's input channel. Null = nothing real to drive, and + * the caller falls back to synthesizing events inside the page. */ +export function activePreviewInput(): PreviewInputHandle | null { + const tabs = $previewTabs.get() + const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0] + + return (tab && handles.get(tab.id)) || null +} diff --git a/apps/desktop/src/app/chat/right-rail/preview-mind.ts b/apps/desktop/src/app/chat/right-rail/preview-mind.ts new file mode 100644 index 0000000000..3f7a1856c4 --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-mind.ts @@ -0,0 +1,29 @@ +/** + * IDLE PULSE — keeps the preview overlay alive while the model reasons. + * + * Everything else the overlay draws is narration of an action, so the pane goes + * dark for the gap between actions — which is most of the time the agent spends + * on a page, and the part where it is deciding what to do with what it just + * read. A page that stops responding the moment the agent is thinking hardest + * reads as the agent having wandered off. + * + * This is deliberately NOT part of the act pipeline: a turn boundary only has + * to poke the overlay the last action left behind. See preview-nudge.ts. + */ + +import { $busy } from '@/store/session' + +import { nudgeOverlay } from './preview-nudge' + +// Module-level, matching how review.ts and coding-status.ts watch this edge. It +// is inert without a live pane, so there is nothing to mount or tear down. +let running = $busy.get() + +$busy.subscribe(busy => { + if (busy === running) { + return + } + + running = busy + nudgeOverlay(busy ? 'think' : 'rest') +}) diff --git a/apps/desktop/src/app/chat/right-rail/preview-nav.ts b/apps/desktop/src/app/chat/right-rail/preview-nav.ts index 954c1a5e9b..5e03db3005 100644 --- a/apps/desktop/src/app/chat/right-rail/preview-nav.ts +++ b/apps/desktop/src/app/chat/right-rail/preview-nav.ts @@ -9,6 +9,9 @@ * sitting in Hermes' own DOM, where `activeElement` is authoritative. */ +import { $rightRailActiveTabId } from '@/store/layout' +import { $previewTabs } from '@/store/preview' + /** Marks a live browser pane so a gesture can find the one holding focus. */ export const PREVIEW_BROWSER_ATTR = 'data-preview-browser' @@ -31,6 +34,15 @@ export function registerPreviewNav(tabId: string, handle: PreviewNavHandle): () } } +/** The ACTIVE preview tab's commands, for callers with no focus to key off — + * the agent's drive_preview, which runs while focus is in the composer. */ +export function activePreviewNav(): PreviewNavHandle | null { + const tabs = $previewTabs.get() + const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0] + + return (tab && handles.get(tab.id)) || null +} + /** Run `command` on the browser pane holding DOM focus. False = focus is * elsewhere in the app, so the caller falls back to the app-level meaning. */ export function commandFocusedPreview(command: keyof PreviewNavHandle): boolean { diff --git a/apps/desktop/src/app/chat/right-rail/preview-nudge.ts b/apps/desktop/src/app/chat/right-rail/preview-nudge.ts new file mode 100644 index 0000000000..8a8f419987 --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-nudge.ts @@ -0,0 +1,38 @@ +/** + * OVERLAY NUDGE — the one way to say a stage to an overlay that is ALREADY on + * the page. + * + * The act pipeline (preview-act.ts) injects the whole engine because it has to: + * it needs to resolve a ref, measure a node and read the page back. The stages + * that only narrate — the idle pulse, the read wipe — need none of that. They + * are one function call against something the last action already left on the + * page, so re-shipping the engine to make it would put the payload on the wire + * on every turn boundary. + * + * A page the agent has never acted on has no overlay, and the nudge is a no-op + * there. That is the right answer rather than a gap: chrome on a page the agent + * never touched would be a lie about what it did. + */ + +import type { WatchStage } from '@/lib/preview-act/watch-in-page' + +import { activePreviewScriptRunner } from './preview-script-runner' + +/** Run one stage against the active pane's overlay, if it has one. */ +export function nudgeOverlay(stage: WatchStage): void { + const run = activePreviewScriptRunner() + + if (!run) { + return + } + + void run(`(function () { + var w = window; + var fn = w.__hermesWatch_fn; + if (!fn) { return 'cold'; } + try { fn(document, w.__hermesActHolder || {}, ${JSON.stringify(stage)}); } catch (err) {} + return 'ok'; +})()`).catch(() => { + // The page navigated out from under us. The next action re-injects. + }) +} diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx index 7d6150b2f3..817cec638a 100644 --- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx +++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx @@ -1,3 +1,7 @@ +// Side-effect import: watches the turn edge so the overlay keeps a pulse while +// the model reasons. Lives here because the pane is what makes it reachable. +import './preview-mind' + import { useStore } from '@nanostores/react' import type { PointerEvent as ReactPointerEvent } from 'react' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' @@ -28,9 +32,10 @@ import { import { type ConsoleEntry } from './preview-console-state' import { previewConsoleState } from './preview-console-store' import { LocalFilePreview, PreviewEmptyState } from './preview-file' +import { type PreviewInputEvent, registerPreviewInput } from './preview-input' import { PREVIEW_BROWSER_ATTR, registerPreviewNav } from './preview-nav' import { registerPreviewPageReader } from './preview-reader' -import { registerPreviewTourRunner } from './preview-tour-runner' +import { registerPreviewScriptRunner } from './preview-script-runner' type PreviewWebview = HTMLElement & { canGoBack?: () => boolean @@ -53,6 +58,7 @@ type PreviewWebview = HTMLElement & { reloadIgnoringCache?: () => void replaceMisspelling?: (word: string) => void selectAll?: () => void + sendInputEvent?: (event: PreviewInputEvent) => void } /** The raw Chromium params riding the webview tag's `context-menu` event. */ @@ -455,15 +461,15 @@ export function PreviewPane({ embedded = false, onRestartServer, reloadRequest = }) }, [isWebPreview, tabId]) - // Publish the TOUR runner for this tab (the tour tool, surface='preview'): - // runs injected driver.js actions inside the guest page so the agent can - // give guided walkthroughs of whatever web app is open here. + // Publish the SCRIPT runner for this tab: the one channel into the guest + // page, shared by the tour tool (injected driver.js walkthroughs) and the + // drive_preview tool (clicking, typing, scrolling the page the user sees). useEffect(() => { if (!isWebPreview || !tabId) { return } - return registerPreviewTourRunner(tabId, async code => { + return registerPreviewScriptRunner(tabId, async code => { const webview = webviewRef.current if (!webview?.executeJavaScript) { @@ -474,6 +480,32 @@ export function PreviewPane({ embedded = false, onRestartServer, reloadRequest = }) }, [isWebPreview, tabId]) + // Publish the INPUT channel for this tab. Same idea as the script runner, but + // it carries real Chromium input rather than script — the agent's clicks and + // keystrokes arrive as trusted events, so the page hovers, focuses and reacts + // exactly as it would under a human hand. + useEffect(() => { + if (!isWebPreview || isRemoteHtml || !tabId) { + return + } + + return registerPreviewInput(tabId, { + focus: () => webviewRef.current?.focus?.(), + send: event => { + const webview = webviewRef.current + + // Never optional-chain this call away: a missing method would make every + // agent click a silent no-op that still reports success, because the + // overlay and the read-back both run on the separate script channel. + if (typeof webview?.sendInputEvent !== 'function') { + throw new Error('preview webview cannot take input events') + } + + webview.sendInputEvent(event) + } + }) + }, [isRemoteHtml, isWebPreview, tabId]) + // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment) useEffect(() => { if (!consoleOpen) { diff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.ts index 7f48c8cbb1..97a372db18 100644 --- a/apps/desktop/src/app/chat/right-rail/preview-reader.ts +++ b/apps/desktop/src/app/chat/right-rail/preview-reader.ts @@ -15,6 +15,8 @@ import { $rightRailActiveTabId } from '@/store/layout' import { $previewTabs } from '@/store/preview' +import { nudgeOverlay } from './preview-nudge' + export interface PreviewReadOptions { /** Characters to return from `start` (capped at PREVIEW_READ_MAX_CHARS). */ count?: number @@ -89,6 +91,13 @@ export async function readActivePreview(opts: PreviewReadOptions = {}): Promise< try { const page = await reader() + // Say it on the page. Reading is by far the cheapest thing the agent + // does — a few hundredths of a second against a model round trip either + // side of it — so a run of reads used to leave the pane dark for the + // twenty seconds it took to page through a document, immediately after + // the one moment that showed anything. + nudgeOverlay('read') + return windowText( { kind: target.kind, path: target.path, title: page.title || target.label, url: page.url || target.url }, page.text, diff --git a/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts b/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts new file mode 100644 index 0000000000..69a0efa2a4 --- /dev/null +++ b/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts @@ -0,0 +1,38 @@ +/** + * PREVIEW SCRIPT RUNNER REGISTRY — the one way anything in the app reaches into + * the preview pane's guest page, the script analog of preview-nav's handle + * registry. + * + * A live browser pane registers its webview's `executeJavaScript` here, keyed + * by tab id; `activePreviewScriptRunner` resolves the ACTIVE tab from the + * store. Both guest-page features ride it — the tour tool (preview-tour.ts) + * and the interaction tool (preview-act.ts) — so their heavy payloads stay out + * of the pane component's static import graph and only load when used. + */ + +import { $rightRailActiveTabId } from '@/store/layout' +import { $previewTabs } from '@/store/preview' + +/** Runs JS source in the pane's guest page, resolving its completion value. */ +export type PreviewScriptRunner = (code: string) => Promise + +const runners = new Map() + +/** Register a live preview's script runner; returns an idempotent unregister. */ +export function registerPreviewScriptRunner(tabId: string, runner: PreviewScriptRunner): () => void { + runners.set(tabId, runner) + + return () => { + if (runners.get(tabId) === runner) { + runners.delete(tabId) + } + } +} + +/** The ACTIVE preview tab's script runner. Null = no live page behind it. */ +export function activePreviewScriptRunner(): PreviewScriptRunner | null { + const tabs = $previewTabs.get() + const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0] + + return (tab && runners.get(tab.id)) || null +} diff --git a/apps/desktop/src/app/chat/right-rail/preview-tour-runner.ts b/apps/desktop/src/app/chat/right-rail/preview-tour-runner.ts deleted file mode 100644 index 84a15c8869..0000000000 --- a/apps/desktop/src/app/chat/right-rail/preview-tour-runner.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * PREVIEW TOUR RUNNER REGISTRY — the tour tool's script bridge into the - * preview pane, the tour analog of preview-nav's handle registry. - * - * A live browser pane registers a script runner (its webview's - * `executeJavaScript`) here, keyed by tab id; `activeTourRunner` resolves the - * ACTIVE tab from the store. Kept separate from preview-tour.ts so the pane - * component's static import stays tiny — the engine source + driver.js - * payload only load when a tour actually runs (gateway-event dynamic-imports - * preview-tour.ts). - */ - -import { $rightRailActiveTabId } from '@/store/layout' -import { $previewTabs } from '@/store/preview' - -/** Runs JS source in the pane's guest page, resolving its completion value. */ -export type TourScriptRunner = (code: string) => Promise - -const runners = new Map() - -/** Register a live preview's script runner; returns an idempotent unregister. */ -export function registerPreviewTourRunner(tabId: string, runner: TourScriptRunner): () => void { - runners.set(tabId, runner) - - return () => { - if (runners.get(tabId) === runner) { - runners.delete(tabId) - } - } -} - -/** The ACTIVE preview tab's script runner. Null = no live page to tour. */ -export function activeTourRunner(): TourScriptRunner | null { - const tabs = $previewTabs.get() - const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0] - - return (tab && runners.get(tab.id)) || null -} diff --git a/apps/desktop/src/app/chat/right-rail/preview-tour.ts b/apps/desktop/src/app/chat/right-rail/preview-tour.ts index 12ad67dc71..a033a7c1f8 100644 --- a/apps/desktop/src/app/chat/right-rail/preview-tour.ts +++ b/apps/desktop/src/app/chat/right-rail/preview-tour.ts @@ -21,7 +21,7 @@ import driverIife from 'driver.js/dist/driver.js.iife.js?raw' import { collectTourTargets } from '@/lib/tour/collect-targets' import { runTourEngine, type TourAction, type TourResult } from '@/lib/tour/engine' -import { activeTourRunner } from './preview-tour-runner' +import { activePreviewScriptRunner } from './preview-script-runner' /** Build the idempotent inject-and-run script for one tour action. */ function buildTourScript(action: TourAction): string { @@ -51,7 +51,7 @@ function buildTourScript(action: TourAction): string { /** Run one tour action in the ACTIVE preview tab's page. */ export async function runPreviewTour(action: TourAction): Promise { - const run = activeTourRunner() + const run = activePreviewScriptRunner() if (!run) { return { error: 'No live page is open in the preview pane — open one first.', success: false } diff --git a/apps/desktop/src/app/chat/session-status-dot.tsx b/apps/desktop/src/app/chat/session-status-dot.tsx index e5e97e743f..3e278556b1 100644 --- a/apps/desktop/src/app/chat/session-status-dot.tsx +++ b/apps/desktop/src/app/chat/session-status-dot.tsx @@ -59,10 +59,13 @@ const DOT_VARIANTS: Record = { role: 'status', title: r => r.backgroundRunning }, - // Emerald — the turn finished while the user was looking elsewhere. + // Emerald — the turn finished while the user was looking elsewhere. The + // color is theme-derived (`--ui-success`, a success green rotated toward the + // accent) so eight finished dots can't sit in the sidebar fighting a palette + // they don't belong to. Under a green accent it stays emerald. unread: { ariaLabel: r => r.finishedUnread, - className: `${DOT_BASE} bg-emerald-500`, + className: `${DOT_BASE} bg-(--ui-success)`, role: 'status', title: r => r.finishedUnread }, diff --git a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx index fe9ef7afd5..fa0af2324f 100644 --- a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx +++ b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx @@ -40,6 +40,11 @@ vi.mock('@/store/boot', () => ({ }) })) +vi.mock('@/store/windows', () => ({ + isAuxiliaryWindow: vi.fn(() => false), + isPeerInstanceWindow: vi.fn(() => false) +})) + vi.mock('@/i18n', () => ({ useI18n: () => ({ t: { @@ -65,6 +70,7 @@ vi.mock('@/i18n', () => ({ const connectionStore = await import('@/store/connections') const bootStore = await import('@/store/boot') +const windowStore = await import('@/store/windows') const $activeConnectionId = connectionStore.$activeConnectionId as ReturnType> const $connectionsRegistry = connectionStore.$connectionsRegistry const $desktopBoot = bootStore.$desktopBoot @@ -72,6 +78,8 @@ const $pendingConnectionId = connectionStore.$pendingConnectionId const initializeConnectionsRegistry = vi.mocked(connectionStore.initializeConnectionsRegistry) const refreshConnectionsRegistry = vi.mocked(connectionStore.refreshConnectionsRegistry) const selectConnection = vi.mocked(connectionStore.selectConnection) +const isAuxiliaryWindow = vi.mocked(windowStore.isAuxiliaryWindow) +const isPeerInstanceWindow = vi.mocked(windowStore.isPeerInstanceWindow) const onConnect = vi.fn() const connection = (id: string, label: string, kind: 'local' | 'remote' = 'remote') => ({ @@ -106,6 +114,8 @@ afterEach(() => { }) $pendingConnectionId.set(null) $findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0 }) + isAuxiliaryWindow.mockReturnValue(false) + isPeerInstanceWindow.mockReturnValue(false) }) describe('ConnectionSwitcher', () => { @@ -127,6 +137,38 @@ describe('ConnectionSwitcher', () => { await waitFor(() => expect(initializeConnectionsRegistry).toHaveBeenCalledTimes(1)) }) + it('keeps a full peer on the shared backend instead of replaying app-launch source restoration', async () => { + isPeerInstanceWindow.mockReturnValue(true) + $desktopBoot.set({ + ...$desktopBoot.get(), + phase: 'renderer.ready', + progress: 100, + running: false, + visible: false + }) + + render() + + await waitFor(() => expect(refreshConnectionsRegistry).toHaveBeenCalledTimes(1)) + expect(initializeConnectionsRegistry).not.toHaveBeenCalled() + }) + + it('keeps a secondary session window from replaying app-launch source restoration', async () => { + isAuxiliaryWindow.mockReturnValue(true) + $desktopBoot.set({ + ...$desktopBoot.get(), + phase: 'renderer.ready', + progress: 100, + running: false, + visible: false + }) + + render() + + await waitFor(() => expect(refreshConnectionsRegistry).toHaveBeenCalledTimes(1)) + expect(initializeConnectionsRegistry).not.toHaveBeenCalled() + }) + it('adds no source chrome for a local-only setup', () => { $connectionsRegistry.set(registry([connection('local', 'This device', 'local')])) render() diff --git a/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx b/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx index f0396abfd2..ce0e56b801 100644 --- a/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx +++ b/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx @@ -36,6 +36,7 @@ import { } from '@/store/connections' import { closeFindBar } from '@/store/find-in-page' import { notifyError } from '@/store/notifications' +import { isAuxiliaryWindow, isPeerInstanceWindow } from '@/store/windows' export function ConnectionSwitcher({ compact = false, onConnect }: { compact?: boolean; onConnect: () => void }) { const { t } = useI18n() @@ -64,8 +65,10 @@ export function ConnectionSwitcher({ compact = false, onConnect }: { compact?: b // The primary boot owns its initial config/session fetches. Restoring a // different source before those settle lets a late primary response repaint // the sidebar under the new source label. Switch only after boot completes, - // then the normal source reset/refetch remains the final writer. - if (!boot.running) { + // then the normal source reset/refetch remains the final writer. Peer and + // auxiliary windows already boot into their intended runtime; replaying the + // primary window's app-launch preference would move them away from it. + if (!boot.running && !isAuxiliaryWindow() && !isPeerInstanceWindow()) { void initializeConnectionsRegistry().catch(() => undefined) } }, [boot.running]) diff --git a/apps/desktop/src/app/chat/sidebar/cron-jobs-section.tsx b/apps/desktop/src/app/chat/sidebar/cron-jobs-section.tsx index 8be72aba34..9d4d54b9af 100644 --- a/apps/desktop/src/app/chat/sidebar/cron-jobs-section.tsx +++ b/apps/desktop/src/app/chat/sidebar/cron-jobs-section.tsx @@ -13,6 +13,7 @@ import { deleteCronJob, getCronJobRuns, pauseCronJob, resumeCronJob, type Sessio import { useI18n } from '@/i18n' import { fmtDayTime, relativeTime } from '@/lib/time' import { cn } from '@/lib/utils' +import { confirm } from '@/store/confirm' import { updateCronJobs } from '@/store/cron' import { $changeEventsAvailable, $cronChangeTick } from '@/store/live-sync' import { notify, notifyError } from '@/store/notifications' @@ -259,7 +260,14 @@ function CronJobSidebarRow({ } const remove = async () => { - if (!window.confirm(`${c.deleteDescPrefix}${label}${c.deleteDescSuffix}`)) { + const ok = await confirm({ + confirmLabel: t.common.delete, + description: `${c.deleteDescPrefix}${label}${c.deleteDescSuffix}`, + destructive: true, + title: c.deleteTitle + }) + + if (!ok) { return } diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsx index d36903a5ee..868b116708 100644 --- a/apps/desktop/src/app/chat/sidebar/index.tsx +++ b/apps/desktop/src/app/chat/sidebar/index.tsx @@ -257,6 +257,16 @@ const HEADER_NAV_BTN = // FTS results cover sessions that aren't in the loaded page; synthesize a // minimal SessionInfo so they render in the same row component (resume works // by id; the snippet stands in for the preview). + +// The backend's FTS layer wraps matched terms in literal '>>>' / '<<<' +// highlight markers (sqlite snippet() delimiters — see hermes_state_search.py). +// The sidebar renders the snippet as plain text, so the markers must be +// stripped or a search for "foo" paints rows titled ">>>foo<<<". +// Exported for tests. +export function stripFtsMarkers(snippet: string): string { + return snippet.replaceAll('>>>', '').replaceAll('<<<', '') +} + function searchResultToSession(result: SessionSearchResult): SessionInfo { const ts = result.session_started ?? Date.now() / 1000 @@ -272,7 +282,7 @@ function searchResultToSession(result: SessionSearchResult): SessionInfo { message_count: 0, model: result.model ?? null, output_tokens: 0, - preview: result.snippet?.trim() || null, + preview: stripFtsMarkers(result.snippet ?? '').trim() || null, source: result.source ?? null, started_at: ts, title: null, diff --git a/apps/desktop/src/app/chat/sidebar/projects/entered-content.tsx b/apps/desktop/src/app/chat/sidebar/projects/entered-content.tsx index 6b09788157..7d2fc7117c 100644 --- a/apps/desktop/src/app/chat/sidebar/projects/entered-content.tsx +++ b/apps/desktop/src/app/chat/sidebar/projects/entered-content.tsx @@ -2,16 +2,8 @@ import { useStore } from '@nanostores/react' import type * as React from 'react' import { useMemo, useState } from 'react' -import { Button } from '@/components/ui/button' import { Codicon } from '@/components/ui/codicon' -import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle -} from '@/components/ui/dialog' +import { ConfirmDialog } from '@/components/ui/confirm-dialog' import type { HermesGitWorktree } from '@/global' import type { SessionInfo } from '@/hermes' import { useI18n } from '@/i18n' @@ -190,43 +182,26 @@ function RepoFlatSection({ destructiveLabel: string, onDestructive: (group: SidebarSessionGroup) => void ) => ( - !isOpen && setTarget(null)} open={Boolean(target)}> - - - {`${s.projects.removeWorktree} "${target?.label ?? ''}"?`} - {description} - - - - - - - - + setTarget(null)} + onConfirm={() => { + if (target) { + onDestructive(target) + } + }} + open={Boolean(target)} + secondaryAction={{ + label: s.projects.removeFromSidebar, + onClick: () => target && dismissWorktree(target.id) + }} + title={`${s.projects.removeWorktree} "${target?.label ?? ''}"?`} + /> ) const removeDialog = ( diff --git a/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.tsx b/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.tsx index 60de5f5ca5..21e276bb04 100644 --- a/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.tsx +++ b/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.tsx @@ -247,12 +247,19 @@ describe('SessionActionsMenu', () => { expect(await screen.queryByRole('dialog')).toBeNull() expect(onDelete).not.toHaveBeenCalled() - // Re-open and confirm with Enter: the delete call fires. + // Re-open and confirm with Enter at wherever focus actually is. Firing on + // the dialog node would pass even when the menu leaves focus on the row + // trigger — where Enter re-activates the row instead of confirming. fireEvent.pointerDown(trigger, { button: 0, pointerType: 'mouse' }) fireEvent.pointerUp(trigger, { button: 0, pointerType: 'mouse' }) fireEvent.click(trigger) fireEvent.click(await screen.findByRole('menuitem', { name: /delete/i })) - fireEvent.keyDown(await screen.findByRole('dialog'), { key: 'Enter' }) + + const reopened = await screen.findByRole('dialog') + // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document + await waitFor(() => expect(reopened.contains(document.activeElement)).toBe(true)) + // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document + fireEvent.keyDown(document.activeElement!, { key: 'Enter' }) expect(await screen.findByText('Session deleted')).toBeTruthy() expect(onDelete).toHaveBeenCalledTimes(1) diff --git a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx index 2550800e72..70962073db 100644 --- a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx +++ b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx @@ -22,14 +22,7 @@ import { Codicon } from '@/components/ui/codicon' import { ColorSwatches } from '@/components/ui/color-swatches' import { ConfirmDialog } from '@/components/ui/confirm-dialog' import { CopyButton } from '@/components/ui/copy-button' -import { - Dialog, - DialogContent, - DialogFooter, - DialogHeader, - DialogTitle, - preventCloseButtonAutoFocus -} from '@/components/ui/dialog' +import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { renameSession } from '@/hermes' import { useI18n } from '@/i18n' @@ -580,7 +573,6 @@ function DeleteSessionDialog({ open, onOpenChange, onConfirm, sessionTitle }: De doneLabel={r.deleted} onClose={() => onOpenChange(false)} onConfirm={onConfirm} - onOpenAutoFocus={preventCloseButtonAutoFocus} open={open} title={r.deleteTitle} /> diff --git a/apps/desktop/src/app/chat/sidebar/session-row.tsx b/apps/desktop/src/app/chat/sidebar/session-row.tsx index 71e3cee766..553061f882 100644 --- a/apps/desktop/src/app/chat/sidebar/session-row.tsx +++ b/apps/desktop/src/app/chat/sidebar/session-row.tsx @@ -30,6 +30,7 @@ import { $projects } from '@/store/projects' import { $pullRequestsByBranch, sessionPrKey } from '@/store/pull-requests' import { $sessionDotStateById, hasLiveTurn, showsRunningArc } from '@/store/session-dot-state' import { $sessionListDensity } from '@/store/session-list-density' +import { $openStoredSessionIds } from '@/store/session-states' import { sessionCostUsd } from '@/store/sidebar-archive' import { $todoProgressBySession } from '@/store/todos' @@ -173,6 +174,10 @@ function SidebarSessionRowImpl({ // those branches should repaint. const prKey = sessionPrKey(session) const pr = useStoreSelector($pullRequestsByBranch, prs => (rowMeta.includes('pr') && prKey ? prs[prKey] : undefined)) + // Open in a pane, but not the focused one. A selector rather than a prop: + // it reaches all four row render paths at once, the set only changes when a + // tile opens or closes, and the boolean bails every unaffected row out. + const openUnfocused = useStoreSelector($openStoredSessionIds, open => !isSelected && open.has(session.id)) const totalTokens = session.input_tokens + session.output_tokens const cost = sessionCostUsd(session) @@ -352,6 +357,10 @@ function SidebarSessionRowImpl({ !card && density !== 'compact' && 'min-h-[2.75rem]', !card && density === 'detailed' && 'min-h-[3.875rem]', isSelected && 'bg-(--ui-row-active-background)', + // Open in another pane: the SAME band, just weaker. Its own mixed + // token rather than row opacity — dimming the whole row would take + // the title and the status dot down with it. + openUnfocused && 'bg-(--ui-row-open-background)', liveTurn && 'text-foreground', // Opaque surface while lifted so the dragged row erases what's under // it (translucency let the rows below bleed through). data-glass-opaque diff --git a/apps/desktop/src/app/chat/sidebar/strip-fts-markers.test.ts b/apps/desktop/src/app/chat/sidebar/strip-fts-markers.test.ts new file mode 100644 index 0000000000..1b33c7bc45 --- /dev/null +++ b/apps/desktop/src/app/chat/sidebar/strip-fts-markers.test.ts @@ -0,0 +1,27 @@ +// Regression: the backend's session-search FTS layer wraps matched terms in +// literal '>>>' / '<<<' snippet() delimiters (hermes_state_search.py). The +// sidebar paints the snippet as plain text, so an unstripped marker renders +// rows titled ">>>foo<<<" (Aug 2026 desktop audit). +import { describe, expect, it } from 'vitest' + +import { stripFtsMarkers } from './index' + +describe('stripFtsMarkers', () => { + it('strips highlight markers around the matched term', () => { + expect(stripFtsMarkers('...replied with >>>MARCO<<< and nothing else...')).toBe( + '...replied with MARCO and nothing else...' + ) + }) + + it('strips multiple marked terms', () => { + expect(stripFtsMarkers('>>>alpha<<< then >>>beta<<<')).toBe('alpha then beta') + }) + + it('leaves marker-free snippets untouched', () => { + expect(stripFtsMarkers('plain snippet text')).toBe('plain snippet text') + }) + + it('handles empty string', () => { + expect(stripFtsMarkers('')).toBe('') + }) +}) diff --git a/apps/desktop/src/app/command-center/maintenance.tsx b/apps/desktop/src/app/command-center/maintenance.tsx index e8ee2f0e4e..d603bfc7fd 100644 --- a/apps/desktop/src/app/command-center/maintenance.tsx +++ b/apps/desktop/src/app/command-center/maintenance.tsx @@ -23,6 +23,7 @@ import { useI18n } from '@/i18n' import { AlertCircle } from '@/lib/icons' import { cn } from '@/lib/utils' import { upsertDesktopActionTask } from '@/store/activity' +import { confirm } from '@/store/confirm' import { notify, notifyError } from '@/store/notifications' import type { ActionStatusResponse } from '@/types/hermes' @@ -169,7 +170,7 @@ export function MaintenancePanel() { const doResetMemory = useCallback( async (target: 'all' | 'memory' | 'user', label: string) => { - if (!window.confirm(mm.resetConfirm(label))) { + if (!(await confirm({ destructive: true, title: mm.resetConfirm(label) }))) { return } diff --git a/apps/desktop/src/app/command-palette/index.tsx b/apps/desktop/src/app/command-palette/index.tsx index 6a457aa6c1..da4d4f3742 100644 --- a/apps/desktop/src/app/command-palette/index.tsx +++ b/apps/desktop/src/app/command-palette/index.tsx @@ -18,6 +18,7 @@ import { Command, CommandGroup, CommandInput, CommandItem, CommandList } from '@ import { HighlightMatches } from '@/components/ui/highlight-matches' import { KbdCombo } from '@/components/ui/kbd' import { getHermesConfigRecord, listAllProfileSessions } from '@/hermes' +import { useMediaQuery } from '@/hooks/use-media-query' import { useI18n } from '@/i18n' import { sessionTitle } from '@/lib/chat-runtime' import { @@ -248,8 +249,8 @@ const rankGroups = (groups: PaletteGroup[], search: string): PaletteGroup[] => { .map(entry => entry.group) } -// cmdk selection values must be unique; labels alone can repeat (the same -// theme lists under both Light and Dark). The id suffix disambiguates. +// cmdk selection values must be unique; labels alone can repeat (a settings +// field and a session can share a title). The id suffix disambiguates. const paletteValue = (item: PaletteItem): string => `${item.label}\u0001${item.id}` const EMPTY_GROUPS: PaletteGroup[] = [] @@ -559,6 +560,16 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { const { availableThemes, clearThemePreview, mode, previewTheme, resolvedMode, setMode, setTheme, themeName } = useTheme() + // Mode rows preview like theme rows do: paint the committed skin at the + // highlighted brightness. `system` has to be resolved here — previewTheme + // paints a concrete light/dark. + const systemDark = useMediaQuery('(prefers-color-scheme: dark)') + + const resolveThemeMode = useCallback( + (target: ThemeMode): 'light' | 'dark' => (target === 'system' ? (systemDark ? 'dark' : 'light') : target), + [systemDark] + ) + const [search, setSearch] = useState('') const [page, setPage] = useState(null) const inputRef = useRef(null) @@ -1148,6 +1159,7 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { keepOpen: true, keywords: ['appearance', 'color mode', 'brightness', entry.mode, t.settings.modeOptions[entry.mode].label], label: t.settings.modeOptions[entry.mode].label, + onHighlight: () => previewTheme(themeName, resolveThemeMode(entry.mode)), run: () => setMode(entry.mode) })) }) @@ -1232,6 +1244,7 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { mode, previewTheme, resolvedMode, + resolveThemeMode, search, sessions, setMode, @@ -1325,27 +1338,51 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { } ] }, - // Built-ins and imported families list under the mode(s) they support; - // picking sets skin + mode at once. A multi-variant import (GitHub, - // Solarized) appears in both groups and switches variants with the mode. - ...(['light', 'dark'] as const).map(groupMode => ({ - heading: groupMode === 'light' ? t.settings.modeOptions.light.label : t.settings.modeOptions.dark.label, - items: availableThemes - .filter(theme => themeSupportsMode(theme.name, groupMode)) - .map(theme => ({ - active: themeName === theme.name && resolvedMode === groupMode, - icon: groupMode === 'light' ? Sun : Moon, - id: `theme-${theme.name}-${groupMode}`, + // Brightness lives with the palettes: one mode toggle for the whole + // list instead of splitting every theme across a Light and a Dark group. + { + heading: t.settings.appearance.colorMode, + items: THEME_MODES.map(entry => ({ + active: mode === entry.mode, + icon: entry.icon, + id: `theme-mode-${entry.mode}`, + keepOpen: true, + keywords: ['appearance', 'brightness', 'color mode', t.settings.modeOptions[entry.mode].label], + label: t.settings.modeOptions[entry.mode].label, + onHighlight: () => previewTheme(themeName, resolveThemeMode(entry.mode)), + run: () => setMode(entry.mode) + })) + }, + // Every palette once, applied on top of the selected mode. An import + // that only ships one variant (Dracula) flips the mode to the side it + // can actually render. + { + heading: t.settings.appearance.themeTitle, + items: availableThemes.map(theme => { + const previewMode = themeSupportsMode(theme.name, resolvedMode) + ? resolvedMode + : resolvedMode === 'dark' + ? 'light' + : 'dark' + + return { + active: themeName === theme.name, + icon: Palette, + id: `theme-${theme.name}`, keepOpen: true, - keywords: ['theme', 'appearance', 'palette', groupMode, theme.label, theme.description ?? ''], + keywords: ['theme', 'appearance', 'palette', theme.label, theme.description ?? ''], label: theme.label, - onHighlight: () => previewTheme(theme.name, groupMode), + onHighlight: () => previewTheme(theme.name, previewMode), run: () => { setTheme(theme.name) - setMode(groupMode) + + if (previewMode !== resolvedMode) { + setMode(previewMode) + } } - })) - })) + } + }) + } ] }, 'color-mode': { @@ -1361,6 +1398,7 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { keepOpen: true, keywords: ['appearance', 'brightness', t.settings.modeOptions[entry.mode].label], label: t.settings.modeOptions[entry.mode].label, + onHighlight: () => previewTheme(themeName, resolveThemeMode(entry.mode)), run: () => setMode(entry.mode) })) } @@ -1387,7 +1425,18 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) { groups: settingsPageGroups } }), - [availableThemes, mode, previewTheme, resolvedMode, setMode, setTheme, settingsPageGroups, t, themeName] + [ + availableThemes, + mode, + previewTheme, + resolvedMode, + resolveThemeMode, + setMode, + setTheme, + settingsPageGroups, + t, + themeName + ] ) const activePage = page ? subPages[page] : null diff --git a/apps/desktop/src/app/context-menu/app-context-menu.test.tsx b/apps/desktop/src/app/context-menu/app-context-menu.test.tsx index ea406caa10..f7e93a211c 100644 --- a/apps/desktop/src/app/context-menu/app-context-menu.test.tsx +++ b/apps/desktop/src/app/context-menu/app-context-menu.test.tsx @@ -3,6 +3,7 @@ import { MemoryRouter } from 'react-router' import { afterEach, describe, expect, it, vi } from 'vitest' import { registerTerminalContextMenu } from '@/app/right-sidebar/terminal/terminal-context-menu' +import { ContextMenu, ContextMenuTrigger, HERMES_CONTEXT_MENU_TRIGGER_ATTR } from '@/components/ui/context-menu' import { formatCombo } from '@/lib/keybinds/combo' import { $previewTabs, closeRightRail } from '@/store/preview' import { $connection } from '@/store/session' @@ -567,3 +568,20 @@ describe('AppContextMenu guest (in-app browser)', () => { expect(screen.queryByText('Add to dictionary')).toBeNull() }) }) + +describe('ContextMenuTrigger asChild', () => { + it('keeps the coordinator marker when the child overwrites data-slot', () => { + render( + + +
bar
+
+
+ ) + + const footer = screen.getByText('bar') + + expect(footer.getAttribute('data-slot')).toBe('statusbar') + expect(footer.hasAttribute(HERMES_CONTEXT_MENU_TRIGGER_ATTR)).toBe(true) + }) +}) diff --git a/apps/desktop/src/app/context-menu/app-context-menu.tsx b/apps/desktop/src/app/context-menu/app-context-menu.tsx index 96e478bd06..17ca30fa38 100644 --- a/apps/desktop/src/app/context-menu/app-context-menu.tsx +++ b/apps/desktop/src/app/context-menu/app-context-menu.tsx @@ -5,6 +5,7 @@ import { useNavigate } from 'react-router' import { terminalMenuHandleFor } from '@/app/right-sidebar/terminal/terminal-context-menu' import { Codicon } from '@/components/ui/codicon' +import { HERMES_CONTEXT_MENU_TRIGGER_ATTR } from '@/components/ui/context-menu' import { writeClipboardText } from '@/components/ui/copy-button' import { DropdownMenu, @@ -609,7 +610,11 @@ export function AppContextMenu() { const element = event.target instanceof Element ? event.target : null // Surfaces with their own Radix context menu keep the whole gesture. - if (element?.closest('[data-slot="context-menu-trigger"]')) { + // Guard the dedicated marker first: Radix `asChild` Slot merges + // `mergeProps(slotProps, childProps)` so the child's `data-slot` wins + // (status bar footer is `data-slot="statusbar"`). The marker is stamped + // after `{...props}` on ContextMenuTrigger and is not overwritten. + if (element?.closest(`[${HERMES_CONTEXT_MENU_TRIGGER_ATTR}], [data-slot="context-menu-trigger"]`)) { return } diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx index 3aa1d34f45..1b418400ea 100644 --- a/apps/desktop/src/app/contrib/wiring.tsx +++ b/apps/desktop/src/app/contrib/wiring.tsx @@ -16,6 +16,7 @@ import { useLocation, useNavigate } from 'react-router' import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill' import { formatRefValue } from '@/components/assistant-ui/directive-text' import { BootFailureOverlay } from '@/components/boot-failure-overlay' +import { ConfirmHost } from '@/components/confirm-host' import { DesktopInstallOverlay } from '@/components/desktop-install-overlay' import { FindBar } from '@/components/find-bar' import { GatewayConnectingOverlay } from '@/components/gateway-connecting-overlay' @@ -62,12 +63,14 @@ import { $selectedStoredSessionId, $sessionResumeRequest, $sessions, + rememberedSessionProfile, sessionMatchesStoredId, sessionPinId, setAwaitingResponse, setBusy, setMessages } from '@/store/session' +import { requestForSessionProfile } from '@/store/session-request-router' import { clearSessionTodos, setSessionTodos, todosForHydration } from '@/store/todos' import { armWakeWord, stopClientCapture } from '@/store/wake-word' import { isAuxiliaryWindow, isHudWindow } from '@/store/windows' @@ -279,7 +282,22 @@ export function ContribWiring({ children }: { children: ReactNode }) { setMessages }) - const { connectionRef, gateway, gatewayRef, requestGateway } = useGatewayRequest() + const { connectionRef, gateway, gatewayRef, requestGateway: ambientRequestGateway } = useGatewayRequest() + + // When chrome stays on the launch backend (Bot Mode / all-profiles + // navigation), session-owned RPCs still have to hit the session's backend. + const requestGateway = useCallback( + (method: string, params?: Record, timeoutMs?: number, signal?: AbortSignal) => { + const owner = rememberedSessionProfile( + $sessions.get(), + selectedStoredSessionIdRef.current, + $activeGatewayProfile.get() + ) + + return requestForSessionProfile(owner, ambientRequestGateway, method, params ?? {}, timeoutMs, signal) + }, + [ambientRequestGateway] + ) const { loadMoreMessagingForPlatform, loadMoreSessions, refreshCronJobs, refreshMessagingSessions, refreshSessions } = useSessionListActions({ profileScope }) @@ -1160,6 +1178,9 @@ export function ContribWiring({ children }: { children: ReactNode }) { {/* Toasts above everything. */} + {/* Backs confirm() from @/store/confirm — renders only while one is open. */} + + {/* Petdex floating mascot — renders nothing unless installed + enabled. Never in the HUD: that window is the chat bar and nothing else. */} {!isHudWindow() && } diff --git a/apps/desktop/src/app/cron/index.tsx b/apps/desktop/src/app/cron/index.tsx index 9bf6923b5c..ee7f0af5aa 100644 --- a/apps/desktop/src/app/cron/index.tsx +++ b/apps/desktop/src/app/cron/index.tsx @@ -8,6 +8,7 @@ import { PageLoader } from '@/components/page-loader' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/checkbox' import { Codicon } from '@/components/ui/codicon' +import { ConfirmDialog } from '@/components/ui/confirm-dialog' import { Dialog, DialogContent, @@ -343,7 +344,6 @@ export function CronView({ onClose, onOpenSession, setStatusbarItemGroup: _setSt const [editor, setEditor] = useState({ mode: 'closed' }) const [pendingDelete, setPendingDelete] = useState(null) - const [deleting, setDeleting] = useState(false) // Jobs live per-profile on disk and the list endpoint aggregates 'all' by // default — scope the fetch to the sidebar's profile scope so this overlay @@ -533,31 +533,23 @@ export function CronView({ onClose, onOpenSession, setStatusbarItemGroup: _setSt } } + // Throws on failure — ConfirmDialog reports it inline and stays open. async function handleConfirmDelete() { if (!pendingDelete) { return } - setDeleting(true) + const { refreshError, stale } = await mutateAndRefreshCronJobs(profile, () => deleteCronJob(pendingDelete.id)) - try { - const { refreshError, stale } = await mutateAndRefreshCronJobs(profile, () => deleteCronJob(pendingDelete.id)) - - if (stale) { - return - } - - if (refreshError) { - notifyError(refreshError, c.failedLoad) - } - - notify({ kind: 'success', title: c.deleted, message: truncate(jobTitle(pendingDelete), 60) }) - setPendingDelete(null) - } catch (err) { - notifyError(err, c.failedDelete) - } finally { - setDeleting(false) + if (stale) { + return } + + if (refreshError) { + notifyError(refreshError, c.failedLoad) + } + + notify({ kind: 'success', title: c.deleted, message: truncate(jobTitle(pendingDelete), 60) }) } async function handleEditorSave(values: EditorValues) { @@ -682,7 +674,9 @@ export function CronView({ onClose, onOpenSession, setStatusbarItemGroup: _setSt /> ))} {visibleJobs.length === 0 && ( -

{c.emptyTitleSearch}

+

+ {query.trim() ? c.emptyTitleSearch : c.emptyTitleNew} +

)} setEditor({ mode: 'create' })} /> {visibleBlueprints.length > 0 && ( @@ -711,8 +705,17 @@ export function CronView({ onClose, onOpenSession, setStatusbarItemGroup: _setSt onPauseResume={() => void handlePauseResume(selectedJob)} onTrigger={() => void handleTrigger(selectedJob)} /> - ) : ( + ) : query.trim() ? ( + // A search with no selected job: search-flavored copy is right. + ) : ( + // No selection and no search — "Try a broader search query" here + // just confused people staring at an empty panel with zero jobs. + )} )} @@ -724,30 +727,24 @@ export function CronView({ onClose, onOpenSession, setStatusbarItemGroup: _setSt onSave={handleEditorSave} /> - !open && !deleting && setPendingDelete(null)} open={pendingDelete !== null}> - - - {c.deleteTitle} - - {pendingDelete ? ( - <> - {c.deleteDescPrefix} - {truncate(jobTitle(pendingDelete), 60)} - {c.deleteDescSuffix} - - ) : null} - - - - - - - - + + {c.deleteDescPrefix} + {truncate(jobTitle(pendingDelete), 60)} + {c.deleteDescSuffix} + + ) : null + } + destructive + onClose={() => setPendingDelete(null)} + onConfirm={handleConfirmDelete} + open={pendingDelete !== null} + title={c.deleteTitle} + /> ) } diff --git a/apps/desktop/src/app/hud/glass.ts b/apps/desktop/src/app/hud/glass.ts index 572f68b58f..2a917e4433 100644 --- a/apps/desktop/src/app/hud/glass.ts +++ b/apps/desktop/src/app/hud/glass.ts @@ -3,14 +3,18 @@ import { type RefObject, useEffect } from 'react' /** The caret is in the composer — see the `:has()` rules in styles.css. */ const TYPING_SELECTOR = '[data-slot="composer-rich-input"]:focus' +/** An open completion list owns the surface; the band falls back behind it. */ +const DRAWER_SELECTOR = '[data-slot="composer-completion-drawer"]' + /** * Native frost behind the band. * - * macOS vibrancy, not CSS — `backdrop-filter` reaches nothing here, because a - * transparent window's backdrop root is the document and the desktop was never - * in it. Vibrancy is composited by WindowServer BELOW the web contents, which - * is what lets it see the desktop and also what makes it untouchable from the - * page: no mask, clip or stacking order can shape it. + * A platform material, not CSS — `backdrop-filter` reaches nothing here, + * because a transparent window's backdrop root is the document and the desktop + * was never in it. The material is composited BELOW the web contents (macOS + * vibrancy via WindowServer, Windows 11 via the DWM backdrop), which is what + * lets it see the desktop and also what makes it untouchable from the page: no + * mask, clip or stacking order can shape it. * * That is survivable because the band is a flat panel. It was NOT survivable * while the band carried a vertical gradient — the frost stayed a slab under a @@ -31,38 +35,79 @@ const TYPING_SELECTOR = '[data-slot="composer-rich-input"]:focus' * document.activeElement, which stays put when the window is blurred and would * latch the frost on forever once the user had ever typed here. * - * `backing` is the veto over both of those. Because the frost is the window and - * not the sheet, it is only ever right when the sheet covers the window; short - * of that the excess is frost over empty space. Gating the caller's `engaged` - * alone would not do it — focus turns the frost on by itself, which is how a - * brand new thread still frosted its whole empty window. + * Two vetoes sit over that: + * + * - `backing` — because the frost is the window and not the sheet, it is only + * ever right when the sheet covers the window; short of that the excess is + * frost over empty space. Gating the caller's `engaged` alone would not do + * it — focus turns the frost on by itself, which is how a brand new thread + * still frosted its whole empty window. + * - An open completion drawer, which drops the band to 25% and blurs it + * (see the `composer-completion-drawer` rule in styles.css). Full-strength + * frost behind a band that has deliberately stepped back is the same bare + * slab in a different disguise. Observed rather than passed in: the drawer + * mounts inside the composer subtree from three different call sites, so a + * prop would need every one of them to remember. + * + * Whether the frost is wanted AT ALL is the user's translucency setting, and + * that answer lives in main (`hudFrostFor`) next to the state it reads. This + * hook reports what the band is doing; it does not decide the material. */ export function useHudGlass(rootRef: RefObject, engaged: boolean, backing: boolean): void { useEffect(() => { const root = rootRef.current - const setVibrancy = window.hermesDesktop?.hud?.setVibrancy + const setFrost = window.hermesDesktop?.hud?.setFrost - if (!root || !setVibrancy) { + if (!root || !setFrost) { return } let on: boolean | null = null const apply = () => { - const next = backing && (engaged || root.querySelector(TYPING_SELECTOR) !== null) + const next = + backing && + root.querySelector(DRAWER_SELECTOR) === null && + (engaged || root.querySelector(TYPING_SELECTOR) !== null) if (on !== next) { on = next - void setVibrancy(next) + void setFrost(next) } } + // The drawer mounts and unmounts without any focus change, so neither + // focusin/focusout nor a re-render is guaranteed to follow it. Coalesced + // to a frame: this observes the whole shell, and a streaming reply mutates + // the transcript tens of times a second — the drawer's state cannot change + // more than once per paint, so re-deciding per mutation is pure churn. + let frame: null | number = null + + const schedule = () => { + if (frame === null) { + frame = requestAnimationFrame(() => { + frame = null + apply() + }) + } + } + + const observer = new MutationObserver(schedule) + + observer.observe(root, { childList: true, subtree: true }) + apply() root.addEventListener('focusin', apply) root.addEventListener('focusout', apply) return () => { - void setVibrancy(false) + void setFrost(false) + observer.disconnect() + + if (frame !== null) { + cancelAnimationFrame(frame) + } + root.removeEventListener('focusin', apply) root.removeEventListener('focusout', apply) } diff --git a/apps/desktop/src/app/hud/hud-shell.tsx b/apps/desktop/src/app/hud/hud-shell.tsx index 9b5d175503..0053d2cb9b 100644 --- a/apps/desktop/src/app/hud/hud-shell.tsx +++ b/apps/desktop/src/app/hud/hud-shell.tsx @@ -2,18 +2,12 @@ import { useStore } from '@nanostores/react' import { type CSSProperties, useCallback, useEffect, useRef, useState } from 'react' import { useNavigate } from 'react-router' -import { TitlebarIcon } from '@/app/shell/titlebar-icon' -import { Button } from '@/components/ui/button' -import { Tip } from '@/components/ui/tooltip' -import { useI18n } from '@/i18n' import { chatMessageText } from '@/lib/chat-messages' -import { closeHud } from '@/store/hud' import { $activeSessionAwaitingInput } from '@/store/prompts' import { $busy, $messages } from '@/store/session' import { RICH_INPUT_SLOT } from '../chat/composer/rich-editor' import { WiredPane } from '../contrib/wiring' -import { titlebarButtonClass } from '../shell/titlebar' import { useHudClickThrough } from './click-through' import { useHudGlass } from './glass' @@ -170,7 +164,6 @@ function useHudHeld(): boolean { * `useRecentActivity`). */ export function HudShell() { - const { t } = useI18n() const [recent, holdBand] = useRecentActivity() const held = useHudHeld() @@ -388,22 +381,6 @@ export function HudShell() { - {/* The way back — without it the only exits are ⌘⇧H and ⌘W, both - invisible. Placed and revealed entirely from styles.css. */} - - - - {/* The resize handle: bottom-right corner, the one sanctioned way to change the HUD's size. Invisible chrome — a hot corner, not a button — so it never reads as part of the surface. `data-hud-grabbing` diff --git a/apps/desktop/src/app/right-sidebar/review/index.tsx b/apps/desktop/src/app/right-sidebar/review/index.tsx index 5b719b01cf..08c82fb466 100644 --- a/apps/desktop/src/app/right-sidebar/review/index.tsx +++ b/apps/desktop/src/app/right-sidebar/review/index.tsx @@ -4,14 +4,7 @@ import { FileDiffPanel } from '@/components/chat/diff-lines' import { DiffSkeleton, TreeSkeleton } from '@/components/chat/skeletons' import { Button } from '@/components/ui/button' import { Codicon } from '@/components/ui/codicon' -import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle -} from '@/components/ui/dialog' +import { ConfirmDialog } from '@/components/ui/confirm-dialog' import { DiffCount } from '@/components/ui/diff-count' import { Tip } from '@/components/ui/tooltip' import { useDelayedTrue } from '@/hooks/use-delayed-true' @@ -209,32 +202,30 @@ export function ReviewPane() { - !open && cancelRevert()} open={revertTarget !== undefined}> - - - {revertingAll ? c.revertAll : c.revert} - - {revertingAll ? c.revertAllConfirm : c.revertConfirm} - {!revertingAll && revertTarget?.path && ( - - {displayPath(revertTarget.path)} - - )} - - - - - - - - + + {revertingAll ? c.revertAllConfirm : c.revertConfirm} + {!revertingAll && revertTarget?.path && ( + + {displayPath(revertTarget.path)} + + )} + + } + destructive + // confirmRevert closes the dialog itself, then reverts in the + // background — so the failure lands in a toast, not inline. + dismissOnConfirm + onClose={cancelRevert} + onConfirm={() => confirmRevert().catch(err => void notifyError(err, c.revert))} + open={revertTarget !== undefined} + title={revertingAll ? c.revertAll : c.revert} + /> ) } diff --git a/apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx b/apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx index 5cf40b6ee6..ab4155823b 100644 --- a/apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx +++ b/apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx @@ -365,4 +365,41 @@ describe('PersistentTerminal rect tracking', () => { expect(overlay.style.pointerEvents).toBe('auto') expect(mount.container!.querySelector('[data-testid="terminal-workspace"]')).toBe(workspace) }) + + it('hides the overlay on a tab switch that happens while the window is unfocused', () => { + // The trap: the terminal is a tab in the main zone and the user clicks + // another tab without the window being focused (or right as it blurs). + // The rect chase is paused then — but visibility is correctness, not perf, + // so the overlay must still stand down instead of covering the chat with + // an opaque, pointer-interactive surface until something refocuses. + installRaf() + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(rect(10, 20, 200, 100)) + $terminalTakeover.set(true) + + mount.render(
diff --git a/apps/desktop/src/app/shell/model-catalog-menu.tsx b/apps/desktop/src/app/shell/model-catalog-menu.tsx index 2513725fd9..1dfd70f15b 100644 --- a/apps/desktop/src/app/shell/model-catalog-menu.tsx +++ b/apps/desktop/src/app/shell/model-catalog-menu.tsx @@ -458,6 +458,7 @@ export function ModelCatalogMenu({ ) : null}