The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(lsp): type errors reach the model inline or before the turn ends; writes stay under ~0.1s
Tekniumeb8a1350639e
diff --git a/agent/lsp/deferred.py b/agent/lsp/deferred.pyindex 50bce4aea50..07cdd69409d 100644--- a/agent/lsp/deferred.py+++ b/agent/lsp/deferred.py@@ -8,6 +8,11 @@ at its next tool result, or its next user turn when no tool runs againA later edit of the same file takes over an undelivered verdict: the newer check diffs against theolder baseline, so an error the first edit introduced is still reported unless the second fixed it.++A verdict must not be lost when the edit was the turn's last tool call (a one-shot ``-q`` run has no+next turn): checks still running are tracked per session (:func:`track_pending`), and before the turn's+final answer is accepted :func:`settle_for_agent` waits for them (bounded) and returns every verdict+still undelivered, which ``agent.turn_stop_gates`` hands to the model as a new message."""from __future__ import annotations@@ -22,6 +27,9 @@ _OWNER: ContextVar[Optional[str]] = ContextVar("hermes_lsp_deferred_owner", defa_MAX_OWNERS = 64 # sessions that never drain (a one-shot caller) must not grow this forever_MAX_PER_OWNER = 32_LOCK = threading.Lock()+_SETTLED = threading.Condition(_LOCK)+# A turn-end wait longer than this hands the verdict to the next user turn instead.+TURN_END_WAIT = 5.0@dataclass(frozen=True)@@ -32,6 +40,8 @@ class _Note:# owner -> {abs path -> note}, oldest owner first_QUEUED: OrderedDict[str, OrderedDict[str, _Note]] = OrderedDict()+# owner -> tokens of its checks still running (their verdict lands in _QUEUED or nowhere)+_PENDING: dict[str, set[object]] = {}@contextlib.contextmanager@@ -91,11 +101,45 @@ def pop_notes(*owners: Optional[str]) -> list[tuple[str, str]]:return out+def track_pending(owner: str) -> object:+ """Record a check of ``owner``'s still running; pass the token to :func:`settled` once its verdict+ has been posted (or dropped)."""+ token = object()+ with _LOCK:+ _PENDING.setdefault(owner, set()).add(token)+ return token+++def settled(owner: str, token: object) -> None:+ with _SETTLED:+ tokens = _PENDING.get(owner)+ if tokens is not None:+ tokens.discard(token)+ if not tokens:+ del _PENDING[owner]+ _SETTLED.notify_all()+++def _owners(agent: Any) -> tuple[Optional[str], ...]:+ owners = (getattr(agent, "session_id", None), getattr(agent, "_current_task_id", None))+ return tuple(o if isinstance(o, str) else None for o in owners)+++def settle_for_agent(agent: Any, timeout: float = TURN_END_WAIT) -> list[tuple[str, str]]:+ """Turn end: wait up to ``timeout`` for ``agent``'s running checks, then pop every undelivered+ ``(path, text)`` verdict. Returns at once when nothing is running."""+ owners = [o for o in dict.fromkeys(_owners(agent)) if o]+ if not owners:+ return []+ with _SETTLED:+ _SETTLED.wait_for(lambda: not any(_PENDING.get(o) for o in owners), timeout=timeout)+ return pop_notes(*owners)++def forward_to_agent(agent: Any) -> None:"""Move ``agent``'s finished verdicts onto its live-notes queue, which rides the next tool result(or the next user turn). Called right before that tool result is built."""- owners = (getattr(agent, "session_id", None), getattr(agent, "_current_task_id", None))- notes = pop_notes(*owners)+ notes = pop_notes(*_owners(agent))if not notes:returnfrom agent.plugin_live_notes import queue_plugin_notice@@ -103,4 +147,5 @@ def forward_to_agent(agent: Any) -> None:queue_plugin_notice(agent, f"lsp:{path}", text)-__all__ = ["bind_owner", "current_owner", "forward_to_agent", "pop_notes", "post", "restore", "take"]+__all__ = ["TURN_END_WAIT", "bind_owner", "current_owner", "forward_to_agent", "pop_notes", "post", "restore",+ "settle_for_agent", "settled", "take", "track_pending"]diff --git a/agent/lsp/edit_check.py b/agent/lsp/edit_check.pyindex e1b421e9487..3a4678b8831 100644--- a/agent/lsp/edit_check.py+++ b/agent/lsp/edit_check.py@@ -26,7 +26,7 @@ from collections import OrderedDictfrom dataclasses import dataclassfrom typing import TYPE_CHECKING, Any, Optional-from agent.lsp import eventlog+from agent.lsp import eventlog, sync_diagnosticsfrom agent.lsp.client import _diagnostic_keyfrom agent.lsp.servers import language_id_for@@ -35,10 +35,11 @@ if TYPE_CHECKING:logger = logging.getLogger("agent.lsp.edit_check")-# A write waits for an in-flight verdict only while it stays inside WRITE_BUDGET (from write entry),-# and never longer than INLINE_GRACE: a warm pyright usually answers a small edit inside it.+# A write waits for its verdict only while it stays inside WRITE_BUDGET (from write entry). A warm+# tsserver answers its synchronous request in ~10-30ms and a warm pyright its pull in ~10-90ms, so the+# common verdict is reported inline; a slower one is delivered later (agent.lsp.deferred).WRITE_BUDGET = 0.08-INLINE_GRACE = 0.05+INLINE_GRACE = 0.08_VERDICT_CAP = 256 # per-file last verdicts kept for baseline reuse_SPAWN_CEILING = 60.0 # outer bound on one check beyond its two waits (cold spawn + initialize)@@ -164,7 +165,8 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], bserver_id = srv.server_id if srv is not None else "lsp"language_id = language_id_for(abs_path, srv)checks: EditChecks = svc._edit_checks- if client.seeds_first_push and client.document_text(abs_path) is None:+ use_sync = sync_diagnostics.supported(client)+ if client.seeds_first_push and client.document_text(abs_path) is None and not use_sync:# First sync of this document on a seeding server: its first push is a verdict on the didOpen# text that never counts as fresh. Open the pre-edit text so that push becomes the baseline;# a new file is opened as written, so the seed is the verdict itself.@@ -177,12 +179,11 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], bchecks.remember(abs_path, baseline_text, seed)version = await client.open_file(abs_path, language_id=language_id)await client.save_file(abs_path)- fresh = await client.wait_for_diagnostics(abs_path, version, mode=svc._wait_mode, timeout=budget)+ diags = await _verdict(svc, client, abs_path, version, budget, use_sync)svc._touch(client)- if not fresh:+ if diags is None:# Alive but no verdict in budget: "no data", never stale stores (ghost diagnostics).return _report(server_id, abs_path, None)- diags = list(client.diagnostics_for(abs_path, fresh_only=True))synced = client.document_text(abs_path)if synced is not None:checks.remember(abs_path, synced, diags)@@ -192,8 +193,9 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], bbaseline = []base_version = await client.open_file(abs_path, language_id=language_id, text=baseline_text)# No verdict on the pre-edit text -> empty baseline: the filter removes less, never more.- if await client.wait_for_diagnostics(abs_path, base_version, mode=svc._wait_mode, timeout=budget):- baseline = list(client.diagnostics_for(abs_path, fresh_only=True))+ base = await _verdict(svc, client, abs_path, base_version, budget, use_sync)+ if base is not None:+ baseline = basechecks.remember(abs_path, baseline_text, baseline)# Leave the server holding the bytes on disk again (its verdict on them is already in hand).await client.open_file(abs_path, language_id=language_id)@@ -203,6 +205,23 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], breturn _report(server_id, abs_path, diags)+async def _verdict(svc: LSPService, client: Any, abs_path: str, version: int, budget: float,+ use_sync: bool) -> Optional[_Diags]:+ """Diagnostics on the text synced at ``version``: a synchronous request where the server has one+ (``agent.lsp.sync_diagnostics``, ~10ms, so the write can report it inline), else the push/pull+ wait. None = no verdict within ``budget``."""+ if use_sync:+ try:+ diags = await sync_diagnostics.request(client, abs_path, timeout=budget)+ except TimeoutError:+ return None+ if diags is not None:+ return diags+ if not await client.wait_for_diagnostics(abs_path, version, mode=svc._wait_mode, timeout=budget):+ return None+ return list(client.diagnostics_for(abs_path, fresh_only=True))++def _report(server_id: str, abs_path: str, diags: Optional[_Diags]) -> Optional[_Diags]:if diags is None:eventlog.log_timeout(server_id, abs_path, kind="fresh diagnostics")diff --git a/agent/lsp/sync_diagnostics.py b/agent/lsp/sync_diagnostics.pynew file mode 100644index 00000000000..f4520d5de87--- /dev/null+++ b/agent/lsp/sync_diagnostics.py@@ -0,0 +1,84 @@+"""Synchronous diagnostics requests, for servers that answer "what is wrong with this file now" directly.++typescript-language-server publishes diagnostics asynchronously and stays silent when a file's+diagnostics are unchanged, so waiting for a push costs the push latency (hundreds of ms after an edit)+or the whole wait budget (a clean edit never publishes). It also forwards raw tsserver requests through+``workspace/executeCommand typescript.tsserverRequest``: ``syntacticDiagnosticsSync`` ++``semanticDiagnosticsSync`` answer for the text last synced, in about 10ms on a warm project. The edit+check (``agent.lsp.edit_check``) asks this module first and falls back to the push/pull wait when the+server has no synchronous request or the request fails.++Returned diagnostics use the shape typescript-language-server pushes (0-based range, numeric severity,+``source`` "typescript"), so a sync verdict and a pushed baseline compare equal by+``agent.lsp.client._diagnostic_key``.+"""+from __future__ import annotations++import logging+from typing import Any, Optional++from agent.lsp.protocol import LSPProtocolError, LSPRequestError++logger = logging.getLogger("agent.lsp.sync_diagnostics")++# tsserver category -> LSP DiagnosticSeverity, as typescript-language-server maps it.+_TS_SEVERITY = {"error": 1, "warning": 2, "message": 3, "suggestion": 4}+_TS_SERVERS = frozenset({"typescript"})+_TS_COMMANDS = ("syntacticDiagnosticsSync", "semanticDiagnosticsSync")+_UNSUPPORTED = "_hermes_sync_diagnostics_unsupported"+++def supported(client: Any) -> bool:+ return client.server_id in _TS_SERVERS and not getattr(client, _UNSUPPORTED, False)+++async def request(client: Any, abs_path: str, *, timeout: float) -> Optional[list[dict[str, Any]]]:+ """Diagnostics for the text last synced to ``client`` for ``abs_path``, or None when the server has+ no synchronous request (it failed): wait for a push then. Raises ``TimeoutError`` when the server+ did not answer within ``timeout`` (a cold project still loading): there is no verdict in budget,+ and a push wait after it would only hold the next check of the file longer."""+ if not supported(client) or timeout <= 0:+ return None+ out: list[dict[str, Any]] = []+ for command in _TS_COMMANDS:+ try:+ reply = await client._send_request_with_retry("workspace/executeCommand", {+ "command": "typescript.tsserverRequest",+ "arguments": [command, {"file": abs_path, "includeLinePosition": True}, {"executionTarget": 0}],+ }, timeout=timeout)+ except TimeoutError:+ raise TimeoutError(f"{command} did not answer in {timeout:.1f}s") from None+ except LSPRequestError as e: # the server rejects the command: use the push wait from now on+ logger.debug("[%s] %s rejected: %s", client.server_id, command, e)+ setattr(client, _UNSUPPORTED, True)+ return None+ except LSPProtocolError as e: # connection trouble: the push wait reports it+ logger.debug("[%s] %s failed: %s", client.server_id, command, e)+ return None+ body = reply.get("body") if isinstance(reply, dict) and reply.get("success", True) else None+ if not isinstance(body, list):+ setattr(client, _UNSUPPORTED, True)+ return None+ out.extend(d for d in (_from_tsserver(item) for item in body) if d is not None)+ return out+++def _from_tsserver(item: Any) -> Optional[dict[str, Any]]:+ if not isinstance(item, dict):+ return None+ start, end = item.get("startLocation"), item.get("endLocation")+ if not isinstance(start, dict) or not isinstance(end, dict):+ return None+ diag: dict[str, Any] = {+ "range": {"start": {"line": start.get("line", 1) - 1, "character": start.get("offset", 1) - 1},+ "end": {"line": end.get("line", 1) - 1, "character": end.get("offset", 1) - 1}},+ "message": str(item.get("message") or ""),+ "severity": _TS_SEVERITY.get(str(item.get("category") or "error"), 1),+ "source": item.get("source") or "typescript",+ }+ if item.get("code") is not None:+ diag["code"] = item["code"]+ return diag+++__all__ = ["request", "supported"]diff --git a/agent/turn_context.py b/agent/turn_context.pyindex c3d16d40bcd..d681ebc6caa 100644--- a/agent/turn_context.py+++ b/agent/turn_context.py@@ -614,7 +614,7 @@ _PER_TURN_RESET_STATE: tuple[tuple[str, Any], ...] = (("_harness_metrics_turn", None),("_iteration_budget_warning_injected", False),("_run_budget_wrapup_injected", False), ("_verification_stop_nudges", 0),- ("_pre_verify_nudges", 0),+ ("_pre_verify_nudges", 0), ("_lsp_turn_end_notes", 0),)diff --git a/agent/turn_stop_gates.py b/agent/turn_stop_gates.pyindex 9580c635d46..c3c24088cf6 100644--- a/agent/turn_stop_gates.py+++ b/agent/turn_stop_gates.py@@ -1,7 +1,8 @@"""Text-response stop gates for the conversation turn loop.-When the model stops with a text answer, three gates may instead append the answer as an-interim row plus a synthetic user-role nudge and continue the turn: verify-on-stop (#65919),+When the model stops with a text answer, four gates may instead append the answer as an+interim row plus a user-role nudge and continue the turn: late language-server verdicts on this+turn's edits (a durable row, see ``_late_lsp_verdicts``), verify-on-stop (#65919),the ``pre_verify`` plugin hook after code edits, and the kanban worker terminal-tool guard.Each keeps the candidate answer as a budget-exhaustion fallback(``pending_verification_response``) and clears ``final_response`` so the finalizer can tell@@ -91,6 +92,31 @@ def _kanban_stop_nudge(agent, messages) -> Optional[str]:return None+_MAX_LSP_TURN_END_NOTES = 3+++def _late_lsp_verdicts(agent) -> Optional[str]:+ """Language-server verdicts on this turn's edits that no tool result carried yet (the edit was the+ last tool call; a one-shot run has no next turn to carry them): waits briefly for checks still+ running (``agent.lsp.deferred.settle_for_agent``) and returns them as a message for the model."""+ issued = getattr(agent, "_lsp_turn_end_notes", 0)+ if not isinstance(issued, int) or issued >= _MAX_LSP_TURN_END_NOTES:+ return None+ try:+ from agent.lsp.deferred import settle_for_agent++ notes = settle_for_agent(agent)+ except Exception:+ logger.debug("late LSP verdict check failed", exc_info=True)+ return None+ if not notes:+ return None+ body = "\n\n".join(text for _path, text in notes)+ return ("[System note: the language server reported problems in files you edited this turn, after "+ "those tool results had returned. Fix them if they are real, then give your final answer.]\n\n"+ + body)++def _append_interim_answer(agent, final_msg, messages, conversation_history, flush_fail_msg: str) -> None:"""Real content: persist and emit as interim so the user sees the attempted answer;only the nudge is flagged synthetic (#65919)."""@@ -129,6 +155,23 @@ def apply_stop_gates(),)+ _lsp_note = _late_lsp_verdicts(agent)+ if _lsp_note:+ agent._lsp_turn_end_notes = getattr(agent, "_lsp_turn_end_notes", 0) + 1 # int: checked above+ final_msg["finish_reason"] = "verification_required"+ _append_interim_answer(+ agent, final_msg, messages, conversation_history, "late-LSP interim flush failed"+ )+ # A durable row (no scaffolding flag): it is never stripped, so no sent byte changes later.+ append_message(messages, {"role": "user", "content": _lsp_note})+ agent._session_messages = messages+ logger.debug("late LSP verdicts delivered before the final answer (%d)", agent._lsp_turn_end_notes)+ return StopGateVerdict(+ continue_turn=True, final_response=None,+ pending_verification_response=final_response,+ pending_verification_response_previewed=agent._interim_content_was_streamed(final_response or ""),+ )+_verify_nudge = _verify_on_stop_nudge(agent)if _verify_nudge:agent._verification_stop_nudges = getattr(agent, "_verification_stop_nudges", 0) + 1diff --git a/tests/agent/lsp/test_write_never_waits_on_lsp.py b/tests/agent/lsp/test_write_never_waits_on_lsp.pyindex 8bb299400d6..125df3bd125 100644--- a/tests/agent/lsp/test_write_never_waits_on_lsp.py+++ b/tests/agent/lsp/test_write_never_waits_on_lsp.py@@ -84,3 +84,29 @@ def test_late_diagnostics_reach_the_session(mock_workspace):assert notes, "the late verdict never reached the session"(path, text), = notesassert path == str(target) and "MOCK001" in text and "earlier edit" in text+++def test_late_verdict_reaches_the_model_before_the_turn_ends(mock_workspace, monkeypatch):+ """The turn's last edit gets a verdict after its tool result returned: the turn must not end on+ it (a one-shot run has no next turn). The final-answer gate waits for it and hands it to the+ model as a new message, continuing the turn."""+ from types import SimpleNamespace++ from agent.turn_stop_gates import apply_stop_gates++ monkeypatch.setenv("HERMES_VERIFY_ON_STOP", "0")+ repo, _svc = mock_workspace("by_content", MOCK_LSP_PUSH_DELAY="0.4")+ target = repo / "c.py"+ target.write_text("x = 1\n", encoding="utf-8")+ result, _elapsed = _patch(target, "x = 1", "x = bad", session="S-end")+ assert result["success"] is True and "lsp_diagnostics" not in result, result+ agent = SimpleNamespace(session_id="S-end", _current_task_id="t-lsp",+ _interim_content_was_streamed=lambda _text: False,+ _emit_interim_assistant_message=lambda _msg: None,+ _flush_messages_to_session_db=lambda *_a: None)+ messages = [{"role": "user", "content": "edit c.py"}]+ verdict = apply_stop_gates(agent, {"role": "assistant", "content": "done"}, final_response="done",+ messages=messages, conversation_history=[],+ pending_verification_response=None, pending_verification_response_previewed=False)+ assert verdict.continue_turn, "the turn ended with the edit's verdict still undelivered"+ assert messages[-1]["role"] == "user" and "MOCK001" in messages[-1]["content"], messages[-1]diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.pyindex cf4fd5bbdc1..dcad65c7bf4 100644--- a/tools/file_operations_lint.py+++ b/tools/file_operations_lint.py@@ -358,7 +358,8 @@ class LintMixin:result = future.result(timeout=inline_wait(started))except concurrent.futures.TimeoutError:if owner:- future.add_done_callback(lambda f: _post_deferred(owner, abs_path, f, superseded))+ token = deferred.track_pending(owner)+ future.add_done_callback(lambda f: _post_deferred(owner, abs_path, f, superseded, token))return ""except Exception: # LSP never breaks a writelogger.debug("LSP edit check failed for %s", path, exc_info=True)@@ -383,9 +384,18 @@ def _format_lsp_block(path: str, diagnostics, header: str) -> str:return ""-def _post_deferred(owner: str, abs_path: str, future, superseded) -> None:- """Done-callback (LSP loop thread): queue a late verdict for the session that made the edit.- No verdict (timeout, dead server) puts back the older note this check had superseded."""+def _post_deferred(owner: str, abs_path: str, future, superseded, token) -> None:+ """Done-callback (LSP loop thread): queue a late verdict for the session that made the edit, then+ release a turn end waiting on it (``agent.lsp.deferred.settle_for_agent``)."""+ from agent.lsp import deferred+ try:+ _queue_late_verdict(owner, abs_path, future, superseded)+ finally:+ deferred.settled(owner, token)+++def _queue_late_verdict(owner: str, abs_path: str, future, superseded) -> None:+ """No verdict (timeout, dead server) puts back the older note this check had superseded."""from agent.lsp import deferredtry:result = None if future.cancelled() else future.result()diff --git a/website/docs/user-guide/features/lsp.md b/website/docs/user-guide/features/lsp.mdindex 5a6f07f760b..d5807419159 100644--- a/website/docs/user-guide/features/lsp.md+++ b/website/docs/user-guide/features/lsp.md@@ -33,18 +33,23 @@ falls back silently to the syntax-only result.Concretely, on every successful `write_file` or `patch`:-1. Hermes performs the write and returns. The tool never waits on the- language server beyond a short inline window (at most 50 ms, and- only while the whole write stays under about 80 ms).-2. In the background, Hermes sends the new content to the language- server. When the server reports diagnostics, it compares them against- the server's verdict on the pre-edit content and keeps only the new- ones.-3. If that verdict arrives inside the inline window, it appears in the- tool result's `lsp_diagnostics`. Otherwise it is attached to the- session's **next** tool result (or the next user turn if no tool- runs), labelled `LSP diagnostics introduced by your earlier edit of- <path>`. Earlier messages are never rewritten.+1. Hermes performs the write, then sends the new content to the language+ server. The write waits for the verdict only while the whole tool call+ stays under about 80 ms.+2. Hermes asks for the verdict directly where the server supports it:+ TypeScript (`typescript-language-server`) answers a synchronous+ diagnostics request, and pyright answers a document pull. Warm servers+ usually reply in 10–90 ms. When the verdict has diagnostics, Hermes+ compares them against the verdict on the pre-edit content and keeps+ only the new ones.+3. A verdict that arrives inside that window appears in the tool result's+ `lsp_diagnostics`. A slower one (cold server, very large file) is+ attached to the session's **next** tool result, labelled `LSP+ diagnostics introduced by your earlier edit of <path>`. If the model+ tries to finish the turn while a check is still running, Hermes waits+ up to 5 s for it. Any problems it finds go to the model as a new+ message before the answer is accepted, so a one-shot `hermes chat -q`+ run still sees them. Earlier messages are never rewritten.If the same file is edited again before its check finishes, the newercheck runs against the older edit's baseline. Errors the first edit