The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(file-tools): patch/write_file return in milliseconds; LSP diagnostics no longer block the write
Tekniumfb55eb6d34d9
diff --git a/agent/lsp/client.py b/agent/lsp/client.pyindex fb7bfe4010f..6a476352fdf 100644--- a/agent/lsp/client.py+++ b/agent/lsp/client.py@@ -182,6 +182,8 @@ class LSPClient:self._docs: dict[str, _DocState] = {} # keyed by absolute path (NOT URI)self._state: str = "stopped"self._sync_kind: int = 1 # 1=Full, 2=Incremental+ # Cleared when the server rejects textDocument/diagnostic as unknown: no further pulls.+ self._pull_supported: bool = Trueself._stopping: bool = False# Waiters snapshot ``_push_counter`` and treat any increase as "recheck the# predicate" — avoids the asyncio.Event sticky-state trap.@@ -582,15 +584,20 @@ class LSPClient:# ---- public file-sync API ----- async def open_file(self, path: str, *, language_id: str = "plaintext") -> int:- """Send didOpen (first time) or didChange (subsequent); return the new document version."""+ async def open_file(self, path: str, *, language_id: str = "plaintext", text: Optional[str] = None) -> int:+ """Send didOpen (first time) or didChange (subsequent); return the new document version.+ ``text`` is the document content to sync (default: the file on disk) — the deferred write check+ syncs the pre-edit content it already holds for its baseline after the new bytes have landed."""if not self.is_running:raise LSPProtocolError("client not running")abs_path = os.path.abspath(path)- try:- text = Path(abs_path).read_text(encoding="utf-8-sig", errors="replace")- except OSError as e:- raise LSPProtocolError(f"cannot read {abs_path}: {e}") from e+ if text is None:+ try:+ text = Path(abs_path).read_text(encoding="utf-8-sig", errors="replace")+ except OSError as e:+ raise LSPProtocolError(f"cannot read {abs_path}: {e}") from e+ elif text.startswith("\ufeff"):+ text = text[1:] # same as the utf-8-sig disk readuri = file_uri(abs_path)doc = self._docs.get(abs_path)if doc is not None and doc.version < 0:@@ -658,6 +665,8 @@ class LSPClient:timeout=DIAGNOSTICS_REQUEST_TIMEOUT,)except (TimeoutError, LSPRequestError, LSPProtocolError) as e:+ if isinstance(e, LSPRequestError) and e.code == ERROR_METHOD_NOT_FOUND:+ self._pull_supported = Falselogger.debug("[%s] document diagnostic pull failed: %s", self.server_id, e)returnif not isinstance(result, dict):@@ -687,24 +696,29 @@ class LSPClient:now = asyncio.get_event_loop().timedeadline = now() + timeoutabs_path = os.path.abspath(path)- while True:- if not self._connection_is_open():- raise LSPProtocolError("server connection closed while waiting for diagnostics")- remaining = deadline - now()- if remaining <= 0:- return False- # Concurrent: document pull + push wait.- tasks = {- asyncio.create_task(self._pull_document_diagnostics(abs_path)),- asyncio.create_task(self._wait_for_fresh_push(abs_path, version, remaining)),- }- _done, pending = await asyncio.wait(tasks, timeout=remaining, return_when=asyncio.FIRST_COMPLETED)+ # Document pull + push wait run concurrently. A pull that settles without a fresh verdict+ # (push-only servers such as typescript-language-server reject it at once) is NOT re-sent:+ # re-sending it spun this loop hundreds of thousands of times per wait. The push wait alone+ # then runs to the deadline.+ pull = asyncio.create_task(self._pull_document_diagnostics(abs_path)) if self._pull_supported else None+ push = asyncio.create_task(self._wait_for_fresh_push(abs_path, version, timeout))+ try:+ while True:+ if not self._connection_is_open():+ raise LSPProtocolError("server connection closed while waiting for diagnostics")+ doc = self._docs.get(abs_path)+ if doc and doc.fresh(version):+ return True+ remaining = deadline - now()+ live = {t for t in (pull, push) if t is not None and not t.done()}+ if remaining <= 0 or not live:+ return False+ await asyncio.wait(live, timeout=remaining, return_when=asyncio.FIRST_COMPLETED)+ finally:+ pending = [t for t in (pull, push) if t is not None and not t.done()]for t in pending:t.cancel()await asyncio.gather(*pending, return_exceptions=True)- doc = self._docs.get(abs_path)- if doc and doc.fresh(version):- return Trueasync def _await_push(self, timeout: float) -> bool:"""Block until the next publishDiagnostics or ``timeout``; True iff a push woke us."""@@ -741,6 +755,31 @@ class LSPClient:continueawait self._await_push(min(remaining, 0.5))+ async def wait_for_seed(self, path: str, timeout: float) -> Optional[list[dict[str, Any]]]:+ """For a server whose first push per document is a seed (never fresh): wait up to ``timeout``+ for that push and return what it carried (the verdict on the text sent with didOpen), or None.+ ``path`` must be absolute (the key ``open_file`` stored it under)."""+ abs_path = path+ now = asyncio.get_running_loop().time+ deadline = now() + timeout+ while True:+ doc = self._docs.get(abs_path)+ if doc is not None and doc.seed_seen:+ return list(doc.push)+ remaining = deadline - now()+ if remaining <= 0 or not self._connection_is_open():+ return None+ await self._await_push(min(remaining, 0.5))++ @property+ def seeds_first_push(self) -> bool:+ return self._seed_first_push++ def document_text(self, path: str) -> Optional[str]:+ """The text last synced to the server for ``path`` (None when never opened)."""+ doc = self._docs.get(os.path.abspath(path))+ return doc.text if doc is not None and doc.version >= 0 else None+def diagnostics_for(self, path: str, *, fresh_only: bool = False) -> list[dict[str, Any]]:"""Merged + deduped push/pull diagnostics for one file. With ``fresh_only=True`` a store onlycontributes once its version tag has caught up to the document's — report paths must use thisdiff --git a/agent/lsp/deferred.py b/agent/lsp/deferred.pynew file mode 100644index 00000000000..50bce4aea50--- /dev/null+++ b/agent/lsp/deferred.py@@ -0,0 +1,106 @@+"""LSP diagnostics for a write, delivered after the write's tool result instead of inside it.++``write_file`` / ``patch`` return as soon as the bytes are on disk and verified; the language server+check runs on the LSP loop (``agent.lsp.edit_check``). Its verdict is queued here under the session+that made the edit (bound by ``tools.file_tools`` around the write) and handed to that session's agent+at its next tool result, or its next user turn when no tool runs again+(``agent.plugin_live_notes``: a new row not yet flushed, so no cached byte moves).++A later edit of the same file takes over an undelivered verdict: the newer check diffs against the+older baseline, so an error the first edit introduced is still reported unless the second fixed it.+"""+from __future__ import annotations++import contextlib+import threading+from collections import OrderedDict+from contextvars import ContextVar+from dataclasses import dataclass+from typing import Any, Iterator, Optional++_OWNER: ContextVar[Optional[str]] = ContextVar("hermes_lsp_deferred_owner", default=None)+_MAX_OWNERS = 64 # sessions that never drain (a one-shot caller) must not grow this forever+_MAX_PER_OWNER = 32+_LOCK = threading.Lock()+++@dataclass(frozen=True)+class _Note:+ text: str+ baseline_text: Optional[str] # pre-edit content the verdict was diffed against+++# owner -> {abs path -> note}, oldest owner first+_QUEUED: OrderedDict[str, OrderedDict[str, _Note]] = OrderedDict()+++def bind_owner(owner: Optional[str]) -> Iterator[None]:+ """Attribute writes in this block to ``owner`` (the session id, else the task id)."""+ token = _OWNER.set(owner or None)+ try:+ yield+ finally:+ _OWNER.reset(token)+++def current_owner() -> Optional[str]:+ return _OWNER.get()+++def post(owner: str, path: str, text: str, baseline_text: Optional[str]) -> None:+ """Queue ``text`` (a formatted diagnostics block) for ``owner``. A note still queued for ``path``+ (an older check finished while a newer one was starting) is kept in front, with its baseline."""+ with _LOCK:+ notes = _QUEUED.pop(owner, None) or OrderedDict()+ older = notes.pop(path, None)+ if older is not None and older.text != text:+ text, baseline_text = f"{older.text}\n\n{text}", older.baseline_text+ notes[path] = _Note(text, baseline_text)+ while len(notes) > _MAX_PER_OWNER:+ notes.popitem(last=False)+ _QUEUED[owner] = notes+ while len(_QUEUED) > _MAX_OWNERS:+ _QUEUED.popitem(last=False)+++def take(owner: Optional[str], path: str) -> Optional[_Note]:+ """Withdraw ``owner``'s undelivered note for ``path``: a newer edit of the file supersedes it and+ diffs against its ``baseline_text``. Hand it back to :func:`restore` if that newer check yields+ no verdict, so the older errors are not lost."""+ if not owner:+ return None+ with _LOCK:+ notes = _QUEUED.get(owner)+ return notes.pop(path, None) if notes else None+++def restore(owner: Optional[str], path: str, note: Optional[_Note]) -> None:+ if owner and note is not None:+ post(owner, path, note.text, note.baseline_text)+++def pop_notes(*owners: Optional[str]) -> list[tuple[str, str]]:+ """Every queued ``(path, text)`` for ``owners``, oldest first, and clear them."""+ out: list[tuple[str, str]] = []+ with _LOCK:+ for owner in dict.fromkeys(o for o in owners if o):+ notes = _QUEUED.pop(owner, None)+ if notes:+ out.extend((path, note.text) for path, note in notes.items())+ return out+++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)+ if not notes:+ return+ from agent.plugin_live_notes import queue_plugin_notice+ for path, text in notes:+ queue_plugin_notice(agent, f"lsp:{path}", text)+++__all__ = ["bind_owner", "current_owner", "forward_to_agent", "pop_notes", "post", "restore", "take"]diff --git a/agent/lsp/edit_check.py b/agent/lsp/edit_check.pynew file mode 100644index 00000000000..e1b421e9487--- /dev/null+++ b/agent/lsp/edit_check.py@@ -0,0 +1,237 @@+"""Post-write LSP check that never holds the write.++``write_file`` / ``patch`` used to block on two language-server round trips per edit: a pre-write+baseline (open + wait) and the post-write verdict (open + save + wait), each up to ``lsp.wait_timeout``.+A push-only server that stays silent on a clean edit (typescript-language-server publishes nothing+when a file's diagnostics stay empty) ran both waits to the deadline: 10s per clean TypeScript edit.++Here the check is one coroutine on the service loop: sync the written file and wait for its verdict;+only when that verdict has diagnostics, get the verdict on the pre-edit text the write already holds+(or reuse the one an earlier check left for exactly that text) and return the diagnostics the edit+introduced.+The caller waits for it only within its latency budget (:func:`inline_wait`); a slower verdict is delivered later through+:mod:`agent.lsp.deferred`. A newer check of the same file runs after an unfinished one and takes it+over (its baseline, and its result when the newer check gets no verdict), so a superseded edit's+errors are still caught unless the newer edit fixed them.+"""+from __future__ import annotations++import asyncio+import concurrent.futures+import hashlib+import logging+import os+import time+from collections import OrderedDict+from dataclasses import dataclass+from typing import TYPE_CHECKING, Any, Optional++from agent.lsp import eventlog+from agent.lsp.client import _diagnostic_key+from agent.lsp.servers import language_id_for++if TYPE_CHECKING:+ from agent.lsp.manager import LSPService++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.+WRITE_BUDGET = 0.08+INLINE_GRACE = 0.05+_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)++_Diags = list[dict[str, Any]]+++@dataclass+class CheckResult:+ diagnostics: _Diags # introduced by the edit (baseline removed); [] = clean+ baseline_text: Optional[str] # the pre-edit text this result was diffed against+ superseded: bool = False # a newer check of the file took this one over and will report it+++@dataclass+class _Inflight:+ task: asyncio.Task+ baseline_text: Optional[str]+ superseded: bool = False+++class EditChecks:+ """Per-service state, touched only on the loop thread: the last verdict per file (baseline reuse)+ and the newest unfinished check per file."""++ def __init__(self) -> None:+ self.verdicts: OrderedDict[str, tuple[str, _Diags]] = OrderedDict()+ self.inflight: dict[str, _Inflight] = {}++ def remember(self, abs_path: str, text: str, diags: _Diags) -> None:+ self.verdicts.pop(abs_path, None)+ self.verdicts[abs_path] = (_digest(text), list(diags))+ while len(self.verdicts) > _VERDICT_CAP:+ self.verdicts.popitem(last=False)++ def recall(self, abs_path: str, text: str) -> Optional[_Diags]:+ hit = self.verdicts.get(abs_path)+ return list(hit[1]) if hit is not None and hit[0] == _digest(text) else None+++def inline_wait(started: Optional[float]) -> float:+ """Seconds the write may still wait for its verdict inline."""+ if started is None:+ return INLINE_GRACE+ return max(0.0, min(INLINE_GRACE, started + WRITE_BUDGET - time.monotonic()))+++def _digest(text: str) -> str:+ return hashlib.sha256(text.encode("utf-8", "surrogateescape")).hexdigest()+++def start(svc: LSPService, path: str, *, pre_text: Optional[str],+ inherited: tuple[bool, Optional[str]] = (False, None)) -> Optional[concurrent.futures.Future]:+ """Schedule the check of ``path`` (already written) on the service loop; the Future resolves to a+ :class:`CheckResult` (or None when no server answered). ``pre_text`` None = the file is new.+ ``inherited`` = ``(True, text)`` when an undelivered verdict for this file is being superseded."""+ from agent.async_utils import safe_schedule_threadsafe+ loop = svc._loop._loop+ if loop is None:+ return None+ return safe_schedule_threadsafe(_serialized(svc, os.path.abspath(path), pre_text, inherited), loop)+++async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str],+ inherited: tuple[bool, Optional[str]]) -> Optional[CheckResult]:+ """Run after any unfinished check of the same file (two checks syncing one document at once would+ satisfy each other's waits), taking it over: its baseline becomes ours, and its result is ours to+ report when we get no verdict. Never cancelled: cancelling mid-spawn would restart a cold server+ on every rapid edit."""+ checks: EditChecks = svc._edit_checks+ baseline_text = inherited[1] if inherited[0] else pre_text+ prev = checks.inflight.get(abs_path)+ if prev is not None and not prev.task.done():+ prev.superseded = True+ baseline_text = prev.baseline_text+ else:+ prev = None+ current = asyncio.current_task()+ assert current is not None+ me = checks.inflight[abs_path] = _Inflight(current, baseline_text)+ try:+ prev_result = None+ if prev is not None:+ await asyncio.wait({prev.task})+ if not prev.task.cancelled() and prev.task.exception() is None:+ prev_result = prev.task.result()+ if me.superseded:+ # A newer edit queued behind us while we waited: it checks the newest bytes against+ # our baseline, so our own round trip would only delay it. Hand it what we hold.+ return CheckResult(prev_result.diagnostics, prev_result.baseline_text, True) if prev_result else None+ budget = svc._wait_budget(abs_path)+ try:+ diags = await asyncio.wait_for(_check(svc, abs_path, baseline_text, budget),+ timeout=2 * budget + _SPAWN_CEILING)+ except asyncio.CancelledError:+ raise+ except Exception as e: # timeout / dead server: same treatment as the old blocking path+ logger.debug("LSP edit check failed for %s", abs_path, exc_info=True)+ await _mark_broken(svc, abs_path, e)+ diags = None+ if diags is not None:+ result: Optional[CheckResult] = CheckResult(diags, baseline_text)+ elif prev_result is not None:+ result = CheckResult(prev_result.diagnostics, prev_result.baseline_text)+ else:+ result = None+ if result is not None and me.superseded:+ result.superseded = True+ return result+ finally:+ if checks.inflight.get(abs_path) is me:+ del checks.inflight[abs_path]+++async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], budget: float) -> Optional[_Diags]:+ """Verdict on the written file first; only when it has diagnostics, the verdict on ``baseline_text``+ (cached from an earlier check when that text was checked before) to drop the pre-existing ones.+ A clean edit costs one round trip, and push-only servers answer the baseline fast in the common+ case (errors -> fewer errors is a change they publish)."""+ client = await svc._get_or_spawn(abs_path)+ if client is None:+ return None+ srv = svc._server_for(abs_path)+ server_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:+ # 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.+ seed = await client.wait_for_seed(abs_path, budget) if await client.open_file(+ abs_path, language_id=language_id, text=baseline_text) == 0 else None+ if baseline_text is None:+ svc._touch(client)+ return _report(server_id, abs_path, seed)+ if seed is not None:+ checks.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)+ svc._touch(client)+ if not fresh:+ # 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)+ if diags and baseline_text is not None and synced is not None:+ baseline = checks.recall(abs_path, baseline_text)+ if baseline is None:+ baseline = []+ 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))+ checks.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)+ from agent.lsp.range_shift import build_line_shift, shift_baseline+ seen = {_diagnostic_key(d) for d in shift_baseline(baseline, build_line_shift(baseline_text, synced))}+ diags = [d for d in diags if _diagnostic_key(d) not in seen]+ return _report(server_id, abs_path, diags)+++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")+ elif diags:+ eventlog.log_diagnostics(server_id, abs_path, len(diags))+ else:+ eventlog.log_clean(server_id, abs_path)+ return diags+++async def _mark_broken(svc: LSPService, abs_path: str, exc: BaseException) -> None:+ """Loop-side twin of ``LSPService._mark_broken_for_file`` (that one blocks on the loop)."""+ srv = svc._server_for(abs_path)+ key = svc._broken_key(srv, abs_path) if srv is not None else None+ if srv is None or key is None:+ return+ already = svc._is_broken(key)+ svc._mark_broken(key)+ with svc._state_lock:+ ckey = svc._live_key(srv, key[1])+ client = svc._clients.pop(ckey, None)+ svc._last_used.pop(ckey, None)+ if client is not None:+ try:+ await asyncio.wait_for(client.shutdown(), timeout=1.0)+ except Exception: # best effort on an already-failed server; the pair is marked broken either way+ logger.debug("shutdown of broken LSP client failed", exc_info=True)+ if not already:+ eventlog.log_spawn_failed(key[0], key[1], exc)+++__all__ = ["INLINE_GRACE", "WRITE_BUDGET", "CheckResult", "EditChecks", "inline_wait", "start"]diff --git a/agent/lsp/manager.py b/agent/lsp/manager.pyindex 75bf0604838..2ead290f83a 100644--- a/agent/lsp/manager.py+++ b/agent/lsp/manager.py@@ -22,6 +22,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuplefrom agent.lsp import eventlogfrom agent.lsp.client import DIAGNOSTICS_DOCUMENT_WAIT, LSPClient, _diagnostic_key as _diag_key+from agent.lsp.edit_check import EditChecksfrom agent.lsp.servers import (SERVERS, UNTRUSTED_SAFE_SERVERS, ServerContext, ServerDef, custom_servers, find_server_for_file, language_id_for,)@@ -184,6 +185,7 @@ class LSPService:self._idle_reaper_task: Optional[asyncio.Task] = None# abs file path → diagnostics snapshot taken immediately before a write.self._delta_baseline: dict[str, _Diags] = {}+ self._edit_checks = EditChecks() # loop-thread state of start_edit_checkif self._enabled and self._idle_timeout > 0:self._loop.run(self._start_idle_reaper(), timeout=2.0)@@ -360,6 +362,16 @@ class LSPService:diags = []self._set_delta_baseline(os.path.abspath(file_path), diags or [])+ def start_edit_check(self, file_path: str, *, pre_text: Optional[str],+ inherited: tuple[bool, Optional[str]] = (False, None)):+ """Non-blocking post-write check (``agent.lsp.edit_check``): a Future of the diagnostics the edit+ introduced, or None when LSP doesn't handle the file. Replaces snapshot_baseline ++ get_diagnostics_sync on the write path, which blocked the write on both waits."""+ if not self.enabled_for(file_path):+ return None+ from agent.lsp import edit_check+ return edit_check.start(self, file_path, pre_text=pre_text, inherited=inherited)+def _set_delta_baseline(self, abs_path: str, diags: _Diags) -> None:"""Store a baseline, refreshing recency (pop + reinsert) so eviction tracks write order.Callers run on arbitrary threads; the multi-step mutation needs the lock (callers don't hold it)."""@@ -573,7 +585,13 @@ class LSPService:return await self._attach_root(srv, client, root)if not owner:try:- client = await spawning+ # Shielded: cancelling this waiter (its caller timed out) must not cancel the spawn+ # every other waiter of this root shares.+ client = await asyncio.shield(spawning)+ except asyncio.CancelledError:+ if spawning.cancelled():+ return None # the owner was cancelled mid-spawn+ raiseexcept Exception:return Nonereturn await self._attach_root(srv, client, root) if client is not None else None@@ -591,6 +609,8 @@ class LSPService:finally:with self._state_lock:self._spawning.pop(key, None)+ if not spawning.done():+ spawning.cancel() # owner cancelled/failed mid-spawn: release the waiters@staticmethodasync def _attach_root(srv: ServerDef, client: LSPClient, root: str) -> LSPClient:diff --git a/agent/tool_executor.py b/agent/tool_executor.pyindex 4bd1bab397e..2100ff22d04 100644--- a/agent/tool_executor.py+++ b/agent/tool_executor.py@@ -35,6 +35,7 @@ from agent.interrupt_control import _REASON_USER_INTERRUPT, interrupt_skip_wordifrom agent.message_sanitization import coalesce_tool_call_idfrom agent.onboarding import observe_tool_result as observe_onboarding_resultfrom agent import plugin_live_notes+from agent.lsp import deferred as lsp_deferredfrom agent.inline_tool_executors import (INLINE_TOOL_EXECUTORS,InlineToolContext,@@ -1146,7 +1147,9 @@ def _commit_tool_result(# string-safe fallback so a rejected image result never poisons history._tool_content = agent._tool_result_content_for_active_model(function_name, persisted_result)tool_message = make_tool_result_message(function_name, _tool_content, tool_call_id, effect_disposition=effect_disposition)- # A plugin that went live during this turn is told on this new row (outside any untrusted-data wrapper).+ # A plugin that went live during this turn, and late LSP verdicts on this session's earlier edits,+ # are told on this new row (outside any untrusted-data wrapper).+ lsp_deferred.forward_to_agent(agent)plugin_live_notes.attach_to_tool_message(agent, tool_message)# Prepare presentation data before the append. The emitting completion callback# stays below the durability fence; raw tool/model content remains unchanged.diff --git a/agent/turn_context.py b/agent/turn_context.pyindex b813a27c14e..c3d16d40bcd 100644--- a/agent/turn_context.py+++ b/agent/turn_context.py@@ -22,6 +22,7 @@ from agent.iteration_budget import IterationBudgetfrom agent.memory_manager import build_memory_context_blockfrom agent.memory_provider import is_trivial_promptfrom agent.message_content import flatten_message_text+from agent.lsp.deferred import forward_to_agent as forward_lsp_verdictsfrom agent.plugin_live_notes import pop_plugin_noticesfrom agent.skills_index_delta import stage_skills_index_notefrom agent.message_metadata import PERSISTENCE_ONLY_MESSAGE_FIELDS, append_message, stamp_message_timestamp@@ -859,6 +860,7 @@ def _merge_gateway_notes(ephemeral system prompt stays byte-stable: the gateway's staged notes, plugin go-live notices notool result carried, then the surface-switch correction. Multimodal (list) content can't take thestring sidecar — append a durable text part instead."""+ forward_lsp_verdicts(agent) # late LSP verdicts on last turn's edits ride the plugin-notice queue_turn_notes = "\n\n".join(part for part in (consume_gateway_turn_context_notes(agent), pop_plugin_notices(agent),consume_surface_switch_note(agent),diff --git a/tests/agent/lsp/_mock_lsp_server.py b/tests/agent/lsp/_mock_lsp_server.pyindex c02c13dcf15..fd22372724a 100644--- a/tests/agent/lsp/_mock_lsp_server.py+++ b/tests/agent/lsp/_mock_lsp_server.py@@ -34,6 +34,8 @@ Behaviour (all behaviours selectable via env var ``MOCK_LSP_SCRIPT``):no ``version`` field in any publishDiagnostics (the client creditseach push with its current document version at receipt). Push-only:the pull endpoint rejects.+- ``"by_content"`` — push-only; after ``MOCK_LSP_PUSH_DELAY`` (default 0.4s) pushes one error iff the+ synced text contains ``bad``, else empty diagnostics.- ``"incremental"`` — apply ranged edits as UTF-16 and expose the server'sdocument mirror through hover, for synchronization contract tests.- ``"clean_eof"`` — closes stdout after ``didOpen`` but keeps the@@ -55,6 +57,32 @@ import sysimport time+def _push_by_content(params, is_change, error_diag):+ """``by_content``: after MOCK_LSP_PUSH_DELAY, one error iff the synced text contains "bad" (so a+ baseline of the pre-edit text differs from the edit's verdict)."""+ td = params.get("textDocument") or {}+ text = (params.get("contentChanges") or [{}])[-1].get("text", "") if is_change else td.get("text")+ # health: allow HX013 -- the delay IS the behaviour under test (a server slower than the write)+ time.sleep(float(os.environ.get("MOCK_LSP_PUSH_DELAY", "0.4")))+ write_message({"jsonrpc": "2.0", "method": "textDocument/publishDiagnostics",+ "params": {"uri": td.get("uri", ""), "version": td.get("version", 0),+ "diagnostics": error_diag if "bad" in (text or "") else []}})+++def _push_slow(params, is_change, error_diag):+ """``slow_push``: one error on didOpen; on didChange sleep MOCK_LSP_PUSH_DELAY, then push empty."""+ td = params.get("textDocument") or {}+ if is_change:+ # health: allow HX013 -- the delay IS the behaviour under test (a slow re-check)+ time.sleep(float(os.environ.get("MOCK_LSP_PUSH_DELAY", "1.0")))+ write_message({"jsonrpc": "2.0", "method": "textDocument/publishDiagnostics",+ "params": {"uri": td.get("uri", ""), "version": td.get("version", 0),+ "diagnostics": [] if is_change else error_diag}})+++_PUSH_HANDLERS = {"by_content": _push_by_content, "slow_push": _push_slow}++def read_message():"""Read one Content-Length framed JSON-RPC message from stdin."""headers = {}@@ -231,18 +259,8 @@ def main():})continue- if script == "slow_push":- diagnostics = error_diag- if is_change:- time.sleep(float(os.environ.get("MOCK_LSP_PUSH_DELAY", "1.0")))- diagnostics = []- write_message(- {- "jsonrpc": "2.0",- "method": "textDocument/publishDiagnostics",- "params": {"uri": uri, "version": version, "diagnostics": diagnostics},- }- )+ if script in _PUSH_HANDLERS:+ _PUSH_HANDLERS[script](params, is_change, error_diag)continuediagnostics = []if script == "errors":@@ -265,7 +283,7 @@ def main():continueif msg.get("method") == "textDocument/diagnostic":- if script in {"stale", "slow_push", "versionless", "silent"}:+ if script in {"stale", "slow_push", "versionless", "silent", "by_content"}:# These scripts model push-only servers so the ghost# can't be papered over by the pull channel.write_message(diff --git a/tests/agent/lsp/test_backend_gate.py b/tests/agent/lsp/test_backend_gate.pyindex 3583efca076..18cea1cc0b5 100644--- a/tests/agent/lsp/test_backend_gate.py+++ b/tests/agent/lsp/test_backend_gate.py@@ -2,8 +2,7 @@The host-side LSP server can't see files inside a Docker/Modal/SSHsandbox. When the agent's terminal env isn't ``LocalEnvironment``,-the file_operations layer must skip both ``snapshot_baseline`` and-``get_diagnostics_sync`` calls — falling back to the in-process+the file_operations layer must not query the service at all — falling back to the in-processsyntax check exactly as if LSP were disabled."""from __future__ import annotations@@ -51,19 +50,22 @@ def test_maybe_lsp_diagnostics_returns_empty_for_non_local(monkeypatch):assert called == [], "service must not be queried for non-local backends"-def test_snapshot_baseline_called_for_local_env(tmp_path, monkeypatch):+def test_edit_check_started_for_local_env(tmp_path, monkeypatch):+ """The local backend hands the written file and its pre-edit text to the service's edit check."""from tools.environments.local import LocalEnvironmentfrom tools.file_operations import ShellFileOperationsfops = ShellFileOperations(LocalEnvironment(cwd=str(tmp_path)))-- snapshot_called = []+ started = []class FakeService:- def snapshot_baseline(self, path):- snapshot_called.append(path)+ def enabled_for(self, path):+ return True++ def start_edit_check(self, path, *, pre_text, inherited):+ started.append((path, pre_text))monkeypatch.setattr("agent.lsp.get_service", lambda: FakeService())- fops._snapshot_lsp_baseline(str(tmp_path / "x.py"))- assert snapshot_called == [str(tmp_path / "x.py")]+ assert fops._maybe_lsp_diagnostics(str(tmp_path / "x.py"), pre_content="x = 1\n") == ""+ assert started == [(str(tmp_path / "x.py"), "x = 1\n")]diff --git a/tests/agent/lsp/test_write_never_waits_on_lsp.py b/tests/agent/lsp/test_write_never_waits_on_lsp.pynew file mode 100644index 00000000000..8bb299400d6--- /dev/null+++ b/tests/agent/lsp/test_write_never_waits_on_lsp.py@@ -0,0 +1,86 @@+"""Invariant: a write never waits on the language server, and its diagnostics still reach the model.++The write path used to block on a pre-write baseline wait plus a post-write wait, each up to+``lsp.wait_timeout``; a server that stays silent (typescript-language-server on a clean edit) ran+both to the deadline on every edit.+"""+from __future__ import annotations++import json+import time++import pytest++from agent.lsp.manager import LSPService+from model_tools import handle_function_call+from tests.agent.lsp.test_service import _install_mock_server+++def mock_workspace(monkeypatch, tmp_path):+ def install(script, **env):+ repo = tmp_path / "repo"+ repo.mkdir()+ (repo / ".git").mkdir()+ (repo / "pyproject.toml").write_text("", encoding="utf-8")+ monkeypatch.chdir(str(repo))+ for key, value in env.items():+ monkeypatch.setenv(key, value)+ gen = _install_mock_server(monkeypatch, script, "pyright")+ next(gen)+ svc = LSPService(enabled=True, wait_mode="document", wait_timeout=3.0, install_strategy="manual",+ trusted_workspaces=[str(repo)])+ monkeypatch.setattr("agent.lsp.get_service", lambda: svc)+ return repo, svc, gen+ created = []++ def _install(script, **env):+ created.append(install(script, **env))+ return created[-1][:2]+ yield _install+ for _repo, svc, gen in created:+ svc.shutdown()+ try:+ next(gen)+ except StopIteration:+ pass+++def _patch(path, old, new, session="S-lsp"):+ handle_function_call("read_file", {"path": str(path)}, task_id="t-lsp", session_id=session)+ started = time.monotonic()+ out = handle_function_call("patch", {"path": str(path), "old_string": old, "new_string": new},+ task_id="t-lsp", session_id=session)+ return json.loads(out), time.monotonic() - started+++def test_silent_server_does_not_hold_the_write(mock_workspace):+ """A server that never answers costs the write nothing: two edits stay far below one wait budget."""+ repo, _svc = mock_workspace("silent")+ target = repo / "a.py"+ target.write_text("x = 1\n", encoding="utf-8")+ for old, new in (("x = 1", "x = 2"), ("x = 2", "x = 3")):+ result, elapsed = _patch(target, old, new)+ assert result["success"] is True, result+ assert elapsed < 1.0, f"write blocked {elapsed:.2f}s on a silent language server"+ assert target.read_text(encoding="utf-8-sig") == "x = 3\n"+++def test_late_diagnostics_reach_the_session(mock_workspace):+ """A verdict slower than the write is delivered to the editing session afterwards (attached to+ its next tool result), and only the error the edit introduced is reported."""+ repo, _svc = mock_workspace("by_content", MOCK_LSP_PUSH_DELAY="0.4")+ target = repo / "b.py"+ target.write_text("x = 1\n", encoding="utf-8")+ result, elapsed = _patch(target, "x = 1", "x = bad")+ assert result["success"] is True and elapsed < 0.4, (result, elapsed)+ assert "lsp_diagnostics" not in result+ from agent.lsp import deferred+ notes = []+ deadline = time.monotonic() + 10+ while not notes and time.monotonic() < deadline:+ notes = deferred.pop_notes("S-lsp")+ time.sleep(0.05)+ assert notes, "the late verdict never reached the session"+ (path, text), = notes+ assert path == str(target) and "MOCK001" in text and "earlier edit" in textdiff --git a/tests/tools/test_write_verification.py b/tests/tools/test_write_verification.pyindex 15e61e7a604..9ad3375bae0 100644--- a/tests/tools/test_write_verification.py+++ b/tests/tools/test_write_verification.py@@ -40,7 +40,8 @@ class TestWriteVerification:assert r.get("verified") is Trueassert b"\r\n" in f.read_bytes()- def test_hash_mismatch_is_hard_error(self, workdir):+ def test_hash_mismatch_is_hard_error(self, workdir, monkeypatch):+ monkeypatch.setenv("HERMES_NATIVE_FILE_READ", "0") # the shell sha256sum transportf = workdir / "bad.txt"import tools.file_operations as foreal_sha = fo.hashlib.sha256@@ -55,8 +56,15 @@ class TestWriteVerification:r = json.loads(write_file_tool(str(f), "actual content\n", task_id="t-wv"))assert "error" in r- def test_verification_failure_never_breaks_write(self, workdir):+ def test_native_hash_mismatch_is_hard_error(self, workdir):+ f = workdir / "bad_native.txt"+ with mock_patch("tools.file_operations_native.sha256_of", return_value="0" * 64):+ r = json.loads(write_file_tool(str(f), "actual content\n", task_id="t-wv3"))+ assert "error" in r and "verification failed" in r["error"]++ def test_verification_failure_never_breaks_write(self, workdir, monkeypatch):# sha256sum unavailable/failing -> verified omitted, write still ok.+ monkeypatch.setenv("HERMES_NATIVE_FILE_READ", "0") # the shell sha256sum transportf = workdir / "ok.txt"import tools.file_operations as fodiff --git a/tools/file_operations.py b/tools/file_operations.pyindex 1a93283f8c9..efc2aa07e60 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -17,6 +17,7 @@ import hashlibimport jsonimport loggingimport secrets+import timeimport unicodedatafrom abc import ABC, abstractmethodfrom typing import Optional, Dict@@ -30,6 +31,7 @@ from tools.file_operations_common import (_strip_terminal_fence_leaks, normalize_read_pagination, normalize_search_pagination)from tools.file_operations_lint import LINTERS_INPROC, LintMixin, _FAIL_CLOSED_INPROC_EXTSfrom tools.file_operations_search import SearchMixin+from tools import file_operations_native as _native_iologger = logging.getLogger(__name__)@@ -576,7 +578,11 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):gives umask-default perms instead of mktemp's 0600 — not ``$(umask)``arithmetic (zsh parses leading-zero constants as decimal), quoted so zshdoesn't =word-expand. ``trap ... EXIT`` removes the temp on every failure.+ The local POSIX backend does the same in-process (``tools.file_operations_native``)."""+ if self._native_read_enabled():+ error = _native_io.atomic_write(self._native_path(path), content.encode("utf-8", "surrogateescape"))+ return ExecuteResult(stdout=error or "", exit_code=1 if error else 0)q_path = self._escape_shell_arg(path)q_parent = self._escape_shell_arg(os.path.dirname(path) or ".")tmpl = self._escape_shell_arg(".hermes-tmp.XXXXXX")@@ -915,6 +921,10 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):# microseconds and self.env is never rebound, so nothing to memoize.return sys.platform != "win32" and self._lsp_local_only()+ def _native_path(self, path: str) -> str:+ """``path`` resolved the way the shell resolves it: against the live backend cwd."""+ return path if os.path.isabs(path) else os.path.join(getattr(self.env, "cwd", None) or self.cwd, path)+def _read_file_native(self, path: str, offset: int, limit: int) -> ReadResult:"""``read_file`` without a shell — same contract as the shell path, byte forbyte. ``os.stat`` is the ``[ -f ]`` guard (a stat, never an open, so FIFOs and@@ -1414,6 +1424,16 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):else:body_mode = None+ native = _native_io.probe(self._native_path(path), body_mode == "cat") if self._native_read_enabled() else None+ if native is not None:+ has_bom, raw = native+ # Decoded as the shell transport decodes (errors="replace"): same pre_content either way.+ body = raw.decode("utf-8", "replace") if raw else ""+ if body_mode == "cat" and body:+ pre_content = body+ sample = pre_content or (body if body_mode == "sample" else "")+ return has_bom, pre_content, _detect_line_ending(sample) if sample else None+sentinel = _new_sentinel(_WRITE_SENTINEL_PREFIX)probe = self._exec(self._write_probe_cmd(path, sentinel, body_mode))output = probe.stdout or ""@@ -1477,9 +1497,11 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):The explicit flag saves the model a confirming re-read; a mismatch is a harderror. ``verified`` is None when the hash could not be taken."""try:- hash_result = self._exec(f"sha256sum {self._escape_shell_arg(path)} 2>/dev/null")- if hash_result.exit_code == 0 and hash_result.stdout.strip():+ disk_sha = _native_io.sha256_of(self._native_path(path)) if self._native_read_enabled() else None+ hash_result = None if disk_sha else self._exec(f"sha256sum {self._escape_shell_arg(path)} 2>/dev/null")+ if hash_result is not None and hash_result.exit_code == 0 and hash_result.stdout.strip():disk_sha = hash_result.stdout.strip().split()[0]+ if disk_sha:if disk_sha != hashlib.sha256(content_bytes).hexdigest():return False, WriteResult(error=(f"Post-write verification failed for {path}: on-disk "@@ -1498,11 +1520,12 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):CANDIDATE content (JSON/YAML/TOML) → one compound on-disk probe(pre-content when wanted, CRLF, BOM; see ``_probe_write_target``) →JSON NaN/Infinity refusal when the write introduces one → CRLF/BOM- preservation → LSP baseline snapshot → atomic write (content rides- stdin: no ARG_MAX limit) → sha256 verification → lint delta → LSP- diagnostics when syntax is clean. ``pre_content``: pre-edit content the+ preservation → atomic write (content rides stdin: no ARG_MAX limit) →+ sha256 verification → lint delta → LSP check when syntax is clean+ (non-blocking; the pre-edit content is its baseline, see ``_maybe_lsp_diagnostics``). ``pre_content``: pre-edit content thecaller already has (skips the read); BOM detection always probes disk."""+ started = time.monotonic()path = self._expand_path(path)denied = get_write_denied_error(path)if denied:@@ -1529,8 +1552,6 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):content = _normalize_line_endings(content, "\r\n")if has_bom and not _has_bom(content):content = _UTF8_BOM + content- # Best-effort snapshot so the LSP tier reports only this edit's diagnostics.- self._snapshot_lsp_baseline(path)# ``dirs_created`` means "parent dirs ensured" (mkdir -p is folded into# _atomic_write; its failure surfaces as the atomic-write error below).dirs_created = bool(os.path.dirname(path))@@ -1550,7 +1571,8 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):# clean (no point asking an LSP about a file that won't parse).lsp_diagnostics: Optional[str] = Noneif lint_result.success or lint_result.skipped:- lsp_diagnostics = self._maybe_lsp_diagnostics(path, pre_content=pre_content, post_content=content) or None+ lsp_diagnostics = self._maybe_lsp_diagnostics(+ path, pre_content=pre_content, post_content=content, started=started) or Nonereturn WriteResult(bytes_written=len(content_bytes), dirs_created=dirs_created, verified=content_verified,_content_sha256=hashlib.sha256(content_bytes).hexdigest(),@@ -1635,10 +1657,10 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):verify_error = self._verify_patch_persisted(path, new_content)if verify_error is not None:return verify_error- lint_result = self._check_lint_delta(path, pre_content=content, post_content=new_content)+ # write_file already ran the lint delta on these bytes (pre = the raw read).return PatchResult(success=True, diff=self._unified_diff(content, new_content, path), files_modified=[path],- lint=lint_result.to_dict() if lint_result else None,+ lint=write_result.lint,note="\n".join(n for n in (*correction_notes, boundary_note([strategy])) if n) or None,# From the internal write_file call, whose baseline was the pre-patch content.lsp_diagnostics=write_result.lsp_diagnostics,diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.pyindex 6af3dd2c7fb..cf4fd5bbdc1 100644--- a/tools/file_operations_lint.py+++ b/tools/file_operations_lint.py@@ -6,12 +6,15 @@ linters are pure functions importable from this module.import astimport json+import loggingimport osimport tomllibfrom typing import Callable, Dict, Optionalfrom tools.file_operations_common import ExecuteResult, LintResult+logger = logging.getLogger(__name__)+# Shell linters by extension (external toolchain). ``.tsx`` is deliberately absent:# it hits the "No linter" skip and LSP covers it when enabled.LINTERS = {@@ -324,35 +327,78 @@ class LintMixin:False so the shell linter still runs."""return bool(self._lsp_call("enabled_for", path, False))- def _snapshot_lsp_baseline(self, path: str) -> None:- """Capture pre-edit LSP diagnostics so the post-write delta is correct. Silent on failure."""- self._lsp_call("snapshot_baseline", path, None)-def _maybe_lsp_diagnostics(self, path: str, *, pre_content: Optional[str] = None,- post_content: Optional[str] = None) -> str:- """Formatted LSP diagnostics introduced by this edit, or "" when LSP is- unavailable/disabled/clean. With both pre and post content a line-shift map- remaps baseline diagnostics into post-edit coordinates; otherwise every- pre-existing diagnostic below an inserted line would look new."""+ post_content: Optional[str] = None, started: Optional[float] = None) -> str:+ """Formatted LSP diagnostics this edit introduced, or "" when LSP is unavailable/disabled/clean+ OR the verdict is not ready yet. Never blocks the write on the language server: the check+ (``agent.lsp.edit_check``) diffs the written file against ``pre_content`` (None = new file) on+ the LSP loop. The write waits for the verdict only until ``inline_deadline(started)`` (what is+ left of the write's latency budget; ``started`` = ``time.monotonic()`` at write entry); a slower+ verdict reaches the model with a later tool result of the same session (``agent.lsp.deferred``).+ ``post_content`` is unused (the check reads the file it just wrote) and kept for callers."""+ del post_contentsvc = self._lsp_service()if svc is None or not self._lsp_will_handle(path):return ""- line_shift = None- if pre_content is not None and post_content is not None and pre_content != post_content:- try:- from agent.lsp.range_shift import build_line_shift- line_shift = build_line_shift(pre_content, post_content)- except Exception:- line_shift = Nonetry:- diagnostics = svc.get_diagnostics_sync(path, delta=True, line_shift=line_shift)+ from agent.lsp import deferred+ from agent.lsp.edit_check import inline_wait+ abs_path = os.path.abspath(path)+ owner = deferred.current_owner()+ superseded = deferred.take(owner, abs_path)+ inherited = (True, superseded.baseline_text) if superseded is not None else (False, None)+ future = svc.start_edit_check(path, pre_text=pre_content, inherited=inherited)except Exception:return ""- if not diagnostics:+ if future is None:+ deferred.restore(owner, abs_path, superseded)return ""+ import concurrent.futurestry:- from agent.lsp.reporter import report_for_file, truncate- block = report_for_file(path, diagnostics)- return truncate("LSP diagnostics introduced by this edit:\n" + block) if block else ""- except Exception:+ 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))+ return ""+ except Exception: # LSP never breaks a write+ logger.debug("LSP edit check failed for %s", path, exc_info=True)+ result = None+ if result is None:+ deferred.restore(owner, abs_path, superseded)+ return ""+ if result.superseded:return ""+ return _format_lsp_block(path, result.diagnostics, "LSP diagnostics introduced by this edit:\n")+++def _format_lsp_block(path: str, diagnostics, header: str) -> str:+ if not diagnostics:+ return ""+ try:+ from agent.lsp.reporter import report_for_file, truncate+ block = report_for_file(path, diagnostics)+ return truncate(header + block) if block else ""+ except Exception: # LSP never breaks a write+ logger.debug("formatting LSP diagnostics for %s failed", path, exc_info=True)+ 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."""+ from agent.lsp import deferred+ try:+ result = None if future.cancelled() else future.result()+ except Exception: # a failed check reports nothing; the older note goes back+ logger.debug("deferred LSP edit check failed for %s", abs_path, exc_info=True)+ result = None+ if result is not None and result.superseded:+ return # the newer check of this file reports it+ if result is None:+ deferred.restore(owner, abs_path, superseded)+ return+ text = _format_lsp_block(abs_path, result.diagnostics, (+ f"LSP diagnostics introduced by your earlier edit of {abs_path} "+ "(the language server answered after that tool result returned):\n"))+ if text:+ deferred.post(owner, abs_path, text, result.baseline_text)diff --git a/tools/file_operations_native.py b/tools/file_operations_native.pynew file mode 100644index 00000000000..dbafc3ae034--- /dev/null+++ b/tools/file_operations_native.py@@ -0,0 +1,103 @@+"""In-process write primitives for the local POSIX backend.++``write_file`` asked the backend shell three questions per write (pre-write probe, atomic write,+sha256 verification): three ``bash`` spawns, ~100 ms on a warm host before any real work. On the+local POSIX backend the file is on this host, so ``ShellFileOperations`` answers them here instead,+under the same gate as the native read path (``_native_read_enabled``; ``HERMES_NATIVE_FILE_READ=0``+turns both off). Semantics mirror the shell scripts they replace; anything they cannot answer+(a FIFO, a device, an unexpected error) returns None and the caller takes the shell path.+"""+from __future__ import annotations++import contextlib+import hashlib+import os+import secrets+import stat+from typing import Optional++_UTF8_BOM = b"\xef\xbb\xbf"+++def _open_regular(path: str) -> Optional[int]:+ """fd of ``path`` when it is a regular file; None when missing; raises ``_NotRegular`` otherwise.+ One lookup with O_NONBLOCK, judged by fstat: a FIFO swapped in never blocks this thread."""+ try:+ fd = os.open(path, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0))+ except FileNotFoundError:+ return None+ if not stat.S_ISREG(os.fstat(fd).st_mode):+ os.close(fd)+ raise _NotRegular(path)+ return fd+++class _NotRegular(OSError):+ pass+++def probe(path: str, want_text: bool) -> Optional[tuple[bool, Optional[bytes]]]:+ """``(has_bom, body)`` for a write target: ``body`` is the whole file when ``want_text`` else its+ first 4 KB (the line-ending sample); ``(False, None)`` when the file is missing; None = ask the shell."""+ try:+ fd = _open_regular(path)+ if fd is None:+ return False, None+ with open(fd, "rb") as fh:+ data = fh.read() if want_text else fh.read(4096)+ except OSError:+ return None+ return data.startswith(_UTF8_BOM), data+++def atomic_write(path: str, data: bytes) -> Optional[str]:+ """Replace ``path`` with ``data`` atomically; None on success, else an error message.++ Same contract as the shell script: a symlink target is written through (the link survives),+ parent directories are created, the temp file lives in the target's directory so the rename is+ atomic, an existing target's mode is kept and a new one gets the umask default (``O_CREAT`` with+ 0o666 applies the process umask itself), and the temp is removed on every failure."""+ target = os.path.realpath(path) if os.path.islink(path) else path+ parent = os.path.dirname(target) or "."+ try:+ os.makedirs(parent, exist_ok=True)+ try:+ mode: Optional[int] = stat.S_IMODE(os.stat(target).st_mode)+ except FileNotFoundError:+ mode = None+ tmp = os.path.join(parent, f".hermes-tmp.{secrets.token_hex(6)}")+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_CLOEXEC", 0), 0o666)+ except OSError as e:+ return f"{e.strerror or e}: {e.filename or target}"+ try:+ with open(fd, "wb") as fh:+ fh.write(data)+ if mode is not None:+ try:+ os.chmod(tmp, mode)+ except OSError:+ pass # best effort, as in the shell script+ os.replace(tmp, target)+ except OSError as e:+ with contextlib.suppress(OSError):+ os.unlink(tmp)+ return f"{e.strerror or e}: {e.filename or target}"+ return None+++def sha256_of(path: str) -> Optional[str]:+ """Hex sha256 of the regular file at ``path``; None when it cannot be read natively."""+ try:+ fd = _open_regular(path)+ if fd is None:+ return None+ digest = hashlib.sha256()+ with open(fd, "rb") as fh:+ for chunk in iter(lambda: fh.read(1 << 20), b""):+ digest.update(chunk)+ except OSError:+ return None+ return digest.hexdigest()+++__all__ = ["atomic_write", "probe", "sha256_of"]diff --git a/tools/file_tools.py b/tools/file_tools.pyindex f9eeda023a1..5dbdce7479f 100644--- a/tools/file_tools.py+++ b/tools/file_tools.py@@ -1335,19 +1335,27 @@ def _handle_write_file(args, **kw):)from hermes_cli.observability.shared_metrics_harness import record_file_edit- return record_file_edit("write_file", "whole_file", lambda: write_file_tool(+ return record_file_edit("write_file", "whole_file", lambda: _owned(kw.get("session_id") or tid, write_file_tool,path=args["path"], content=args["content"], task_id=tid,cross_profile=bool(args.get("cross_profile", False)),session_id=kw.get("session_id"),))+def _owned(owner: str, fn, **call_kwargs):+ """Run an edit tool with its late LSP verdicts attributed to ``owner``, the calling session+ (``agent.lsp.deferred``): they ride that session's next tool result."""+ from agent.lsp.deferred import bind_owner+ with bind_owner(owner):+ return fn(**call_kwargs)++def _handle_patch(args, **kw):from hermes_cli.observability.shared_metrics_harness import record_file_edittid = kw.get("task_id") or "default"mode = args.get("mode", "replace")- return record_file_edit("patch", mode, lambda: patch_tool(+ return record_file_edit("patch", mode, lambda: _owned(kw.get("session_id") or tid, patch_tool,mode=mode, path=args.get("path"),old_string=args.get("old_string"), new_string=args.get("new_string"),replace_all=args.get("replace_all", False), patch=args.get("patch"), task_id=tid,diff --git a/website/docs/user-guide/features/lsp.md b/website/docs/user-guide/features/lsp.mdindex a5e188c437d..5a6f07f760b 100644--- a/website/docs/user-guide/features/lsp.md+++ b/website/docs/user-guide/features/lsp.md@@ -33,10 +33,22 @@ falls back silently to the syntax-only result.Concretely, on every successful `write_file` or `patch`:-1. Hermes captures a baseline of current diagnostics for the file.-2. Performs the write.-3. Re-queries the language server, filters out diagnostics that were- already in the baseline, and surfaces only the new ones.+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.++If the same file is edited again before its check finishes, the newer+check runs against the older edit's baseline. Errors the first edit+introduced are still reported unless the second edit fixed them.The agent sees output like: