Skip to content

The agent that grows with you

fix(lsp): late diagnostics stay with the editing session; fixed errors do not come back

Tekniumf8536de78c12
diff --git a/agent/lsp/deferred.py b/agent/lsp/deferred.py
index 07cdd69409d..548ea4b3edd 100644
--- a/agent/lsp/deferred.py
+++ b/agent/lsp/deferred.py
@@ -76,8 +76,8 @@ def post(owner: str, path: str, text: str, baseline_text: Optional[str]) -> None
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."""
+ diffs against its ``baseline_text``. The newer check carries it and hands it back to
+ :func:`restore` only if it ends with no verdict, so the older errors are not lost."""
if not owner:
return None
with _LOCK:
@@ -85,9 +85,11 @@ def take(owner: Optional[str], path: str) -> Optional[_Note]:
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 restore(owner: Optional[str], path: str, notes) -> None:
+ """Queue ``notes`` (oldest first) withdrawn by :func:`take` again."""
+ if owner:
+ for note in notes:
+ post(owner, path, note.text, note.baseline_text)
def pop_notes(*owners: Optional[str]) -> list[tuple[str, str]]:
diff --git a/agent/lsp/edit_check.py b/agent/lsp/edit_check.py
index 3a4678b8831..a2266219fa6 100644
--- a/agent/lsp/edit_check.py
+++ b/agent/lsp/edit_check.py
@@ -10,9 +10,11 @@ only when that verdict has diagnostics, get the verdict on the pre-edit text the
(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.
+:mod:`agent.lsp.deferred`. Checks of one file run one at a time (two checks syncing one document at
+once would satisfy each other's waits). A newer check by the SAME session takes an unfinished one over
+(its baseline, the undelivered notes it carried, 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; a check by another
+session only queues behind it, so each session hears about its own edits only.
"""
from __future__ import annotations
@@ -48,25 +50,31 @@ _Diags = list[dict[str, Any]]
@dataclass
class CheckResult:
- diagnostics: _Diags # introduced by the edit (baseline removed); [] = clean
+ diagnostics: Optional[_Diags] # introduced by the edit (baseline removed); [] = clean; None = no verdict
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
+ superseded: bool = False # a newer check of the file by the same session took this one over
+ # Undelivered older notes this check took over and did not settle (no verdict): the caller queues
+ # them again. Empty whenever there is a verdict (it was diffed against the oldest baseline).
+ carry: tuple = ()
@dataclass
class _Inflight:
task: asyncio.Task
baseline_text: Optional[str]
+ carry: tuple
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."""
+ """Per-service state, touched only on the loop thread: the last verdict per file (baseline reuse),
+ the newest unfinished check per file (``tail``: serializes every session's checks of it) and per
+ ``(session, file)`` (``inflight``: the one a newer edit by that session takes over)."""
def __init__(self) -> None:
self.verdicts: OrderedDict[str, tuple[str, _Diags]] = OrderedDict()
- self.inflight: dict[str, _Inflight] = {}
+ self.tail: dict[str, _Inflight] = {}
+ self.inflight: dict[tuple[Optional[str], str], _Inflight] = {}
def remember(self, abs_path: str, text: str, diags: _Diags) -> None:
self.verdicts.pop(abs_path, None)
@@ -90,45 +98,54 @@ 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]:
+def start(svc: LSPService, path: str, *, pre_text: Optional[str], owner: Optional[str] = None,
+ carry: tuple = ()) -> 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."""
+ :class:`CheckResult`. ``pre_text`` None = the file is new. ``owner`` = the editing session (None:
+ no later delivery, nothing to take over); ``carry`` = that session's undelivered notes for this
+ file (``agent.lsp.deferred``) this edit supersedes: the oldest one's baseline becomes ours."""
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)
+ return safe_schedule_threadsafe(_serialized(svc, os.path.abspath(path), pre_text, owner, tuple(carry)), 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."""
+async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], owner: Optional[str],
+ carry: tuple) -> CheckResult:
+ """Run after any unfinished check of the same file; take over the same session's unfinished check:
+ its baseline and carried notes become 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)
+ baseline_text = carry[0].baseline_text if carry else pre_text
+ tail = checks.tail.get(abs_path)
+ tail = tail if tail is not None and not tail.task.done() else None
+ prev = checks.inflight.get((owner, abs_path)) if owner else None
if prev is not None and not prev.task.done():
prev.superseded = True
- baseline_text = prev.baseline_text
+ baseline_text, carry = prev.baseline_text, prev.carry + carry
else:
prev = None
current = asyncio.current_task()
assert current is not None
- me = checks.inflight[abs_path] = _Inflight(current, baseline_text)
+ me = _Inflight(current, baseline_text, carry)
+ checks.tail[abs_path] = me
+ if owner:
+ checks.inflight[(owner, abs_path)] = me
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
+ waits = {t.task for t in (prev, tail) if t is not None}
+ if waits:
+ await asyncio.wait(waits)
+ if prev is not None and not prev.task.cancelled() and prev.task.exception() is None:
+ prev_result = prev.task.result()
+ if prev_result.diagnostics is None:
+ prev_result = None
+ if me.superseded:
+ # A newer edit by this session queued behind us: it checks the newest bytes against our
+ # baseline and owns our carried notes, so our own round trip would only delay it.
+ return CheckResult(prev_result.diagnostics if prev_result else None,
+ prev_result.baseline_text if prev_result else baseline_text, True)
budget = svc._wait_budget(abs_path)
try:
diags = await asyncio.wait_for(_check(svc, abs_path, baseline_text, budget),
@@ -137,20 +154,23 @@ async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str],
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)
+ await svc._mark_broken_for_file_async(abs_path, e)
diags = None
if diags is not None:
- result: Optional[CheckResult] = CheckResult(diags, baseline_text)
+ result = 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
+ result = CheckResult(None, baseline_text, carry=carry)
+ result.superseded = me.superseded
+ if me.superseded:
+ result.carry = () # the newer check took them over
return result
finally:
- if checks.inflight.get(abs_path) is me:
- del checks.inflight[abs_path]
+ if checks.tail.get(abs_path) is me:
+ del checks.tail[abs_path]
+ if owner and checks.inflight.get((owner, abs_path)) is me:
+ del checks.inflight[(owner, abs_path)]
async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], budget: float) -> Optional[_Diags]:
@@ -185,9 +205,10 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], b
# Alive but no verdict in budget: "no data", never stale stores (ghost diagnostics).
return _report(server_id, abs_path, None)
synced = client.document_text(abs_path)
- if synced is not None:
- checks.remember(abs_path, synced, diags)
+ verdict = diags
if diags and baseline_text is not None and synced is not None:
+ # Read the cached pre-edit verdict (an earlier check's, or the seed above) BEFORE this edit's
+ # verdict replaces the file's cache entry.
baseline = checks.recall(abs_path, baseline_text)
if baseline is None:
baseline = []
@@ -196,12 +217,13 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], b
base = await _verdict(svc, client, abs_path, base_version, budget, use_sync)
if base is not None:
baseline = base
- 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]
+ if synced is not None:
+ checks.remember(abs_path, synced, verdict) # the next edit's baseline is these bytes
return _report(server_id, abs_path, diags)
@@ -232,25 +254,4 @@ def _report(server_id: str, abs_path: str, diags: Optional[_Diags]) -> Optional[
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.py
index 2ead290f83a..8aa0a42618d 100644
--- a/agent/lsp/manager.py
+++ b/agent/lsp/manager.py
@@ -362,15 +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)):
+ def start_edit_check(self, file_path: str, *, pre_text: Optional[str], owner: Optional[str] = None,
+ carry: tuple = ()):
"""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."""
+ get_diagnostics_sync on the write path, which blocked the write on both waits. ``owner`` = the
+ editing session; ``carry`` = its undelivered notes for the file this check takes over."""
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)
+ return edit_check.start(self, file_path, pre_text=pre_text, owner=owner, carry=carry)
def _set_delta_baseline(self, abs_path: str, diags: _Diags) -> None:
"""Store a baseline, refreshing recency (pop + reinsert) so eviction tracks write order.
@@ -442,29 +443,45 @@ class LSPService:
self._set_delta_baseline(abs_path, fresh)
return diags
- def _mark_broken_for_file(self, file_path: str, exc: BaseException) -> None:
- """Mark the file's ``(server_id, root)`` pair broken after an outer timeout/error.
- The outer ``_loop.run`` timeout cancels the in-flight spawn before ``_get_or_spawn`` could record
- the failure; without this every later write would re-pay the full timeout. Also kills any
- half-initialized client and logs the failure once."""
+ def _detach_broken(self, file_path: str, exc: BaseException) -> Optional[LSPClient]:
+ """Mark the file's ``(server_id, root)`` pair broken after an outer timeout/error, drop its
+ client from the live maps and log the failure once; returns the dropped client for the caller
+ to shut down. The outer timeout cancels the in-flight spawn before ``_get_or_spawn`` could
+ record the failure; without this every later write would re-pay the full timeout. The one
+ owner of this cleanup: ``_mark_broken_for_file`` (tool threads) and
+ ``_mark_broken_for_file_async`` (the loop) only differ in how they await the shutdown."""
srv = self._server_for(file_path)
key = self._broken_key(srv, file_path) if srv is not None else None
if key is None:
- return
+ return None
already_broken = self._is_broken(key)
self._mark_broken(key)
with self._state_lock:
ckey = self._live_key(srv, key[1])
client = self._clients.pop(ckey, None)
self._last_used.pop(ckey, None)
+ if not already_broken:
+ eventlog.log_spawn_failed(key[0], key[1], exc)
+ return client
+
+ def _mark_broken_for_file(self, file_path: str, exc: BaseException) -> None:
+ """Tool-thread bridge of :meth:`_detach_broken`: shuts the client down through the loop."""
+ client = self._detach_broken(file_path, exc)
if client is not None:
try:
# Fire-and-forget shutdown — we're already on a slow path.
self._loop.run(client.shutdown(), timeout=1.0)
except Exception:
pass
- if not already_broken:
- eventlog.log_spawn_failed(key[0], key[1], exc)
+
+ async def _mark_broken_for_file_async(self, file_path: str, exc: BaseException) -> None:
+ """Loop-side :meth:`_detach_broken` (``_mark_broken_for_file`` would block on the loop it runs on)."""
+ client = self._detach_broken(file_path, exc)
+ 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)
def shutdown(self) -> None:
"""Tear down all clients and stop the background loop."""
diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.py
index dcad65c7bf4..6a7966aa6b4 100644
--- a/tools/file_operations_lint.py
+++ b/tools/file_operations_lint.py
@@ -345,13 +345,15 @@ class LintMixin:
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)
+ # This session's undelivered verdict for the file: the new check takes it over (diffs
+ # against its baseline) and hands it back in ``result.carry`` only if it gets no verdict.
+ older = deferred.take(owner, abs_path)
+ carry = (older,) if older is not None else ()
+ future = svc.start_edit_check(path, pre_text=pre_content, owner=owner, carry=carry)
except Exception:
return ""
if future is None:
- deferred.restore(owner, abs_path, superseded)
+ deferred.restore(owner, abs_path, carry)
return ""
import concurrent.futures
try:
@@ -359,16 +361,17 @@ class LintMixin:
except concurrent.futures.TimeoutError:
if owner:
token = deferred.track_pending(owner)
- future.add_done_callback(lambda f: _post_deferred(owner, abs_path, f, superseded, token))
+ future.add_done_callback(lambda f: _post_deferred(owner, abs_path, f, carry, token))
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)
+ deferred.restore(owner, abs_path, carry)
return ""
if result.superseded:
return ""
+ if result.diagnostics is None:
+ deferred.restore(owner, abs_path, result.carry)
+ return ""
return _format_lsp_block(path, result.diagnostics, "LSP diagnostics introduced by this edit:\n")
@@ -384,28 +387,33 @@ def _format_lsp_block(path: str, diagnostics, header: str) -> str:
return ""
-def _post_deferred(owner: str, abs_path: str, future, superseded, token) -> None:
+def _post_deferred(owner: str, abs_path: str, future, carry, 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)
+ _queue_late_verdict(owner, abs_path, future, carry)
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."""
+def _queue_late_verdict(owner: str, abs_path: str, future, carry) -> None:
+ """A superseded check posts nothing: the newer check owns its verdict AND the notes it carried.
+ No verdict hands back only what the check still holds (``result.carry``), never the caller's copy."""
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
+ deferred.restore(owner, abs_path, carry)
+ return
if result is None:
- deferred.restore(owner, abs_path, superseded)
+ deferred.restore(owner, abs_path, carry)
+ return
+ if result.superseded:
+ return
+ if result.diagnostics is None:
+ deferred.restore(owner, abs_path, result.carry)
return
text = _format_lsp_block(abs_path, result.diagnostics, (
f"LSP diagnostics introduced by your earlier edit of {abs_path} "