Skip to content

The agent that grows with you

fix(lsp): each edit is diagnosed on the bytes it wrote, never on another session's later write

Tekniumff6a08c96fb9
diff --git a/agent/lsp/client.py b/agent/lsp/client.py
index 6a476352fdf..7ea05958bc1 100644
--- a/agent/lsp/client.py
+++ b/agent/lsp/client.py
@@ -135,6 +135,15 @@ class _DocState:
return self.fresh_push(version) or self.fresh_pull(version)
+def as_disk_read(text: str) -> str:
+ """``text`` as ``Path.read_text(encoding="utf-8-sig", errors="replace")`` returns the file holding it."""
+ try:
+ text = text.encode("utf-8", "surrogateescape").decode("utf-8-sig", "replace")
+ except UnicodeEncodeError: # lone surrogate outside the escape range: keep the text, drop the BOM
+ text = text.removeprefix("\ufeff")
+ return text.replace("\r\n", "\n").replace("\r", "\n")
+
+
class LSPClient:
"""One server process + one workspace root. ``start()`` → ``open_file()`` → ``wait_for_diagnostics()`` →
``diagnostics_for()`` → ``shutdown()``."""
@@ -587,7 +596,9 @@ class LSPClient:
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."""
+ syncs the exact pre-/post-edit snapshots of the edit it checks, whatever the disk holds by then.
+ A supplied text is normalized as the disk read would decode it (BOM, newlines, undecodable bytes),
+ so a snapshot and the file it was written to sync identically."""
if not self.is_running:
raise LSPProtocolError("client not running")
abs_path = os.path.abspath(path)
@@ -596,8 +607,8 @@ class LSPClient:
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 read
+ else:
+ text = as_disk_read(text)
uri = file_uri(abs_path)
doc = self._docs.get(abs_path)
if doc is not None and doc.version < 0:
diff --git a/agent/lsp/edit_check.py b/agent/lsp/edit_check.py
index a2266219fa6..a16911ae2eb 100644
--- a/agent/lsp/edit_check.py
+++ b/agent/lsp/edit_check.py
@@ -5,10 +5,12 @@ baseline (open + wait) and the post-write verdict (open + save + wait), each up
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.
+Here the check is one coroutine on the service loop: sync the edit's post-edit snapshot and wait for
+its verdict; only when that verdict has diagnostics, get the verdict on the pre-edit snapshot (or reuse
+the one an earlier check left for exactly that text) and return the diagnostics the edit introduced.
+Both snapshots are the bytes the write itself held, never a later read of the file: by the time a
+queued check runs, another session may have written the file again, and that session's errors are not
+this edit's.
The caller waits for it only within its latency budget (:func:`inline_wait`); a slower verdict is delivered later through
: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
@@ -29,7 +31,7 @@ from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Optional
from agent.lsp import eventlog, sync_diagnostics
-from agent.lsp.client import _diagnostic_key
+from agent.lsp.client import _diagnostic_key, as_disk_read
from agent.lsp.servers import language_id_for
if TYPE_CHECKING:
@@ -75,6 +77,10 @@ class EditChecks:
self.verdicts: OrderedDict[str, tuple[str, _Diags]] = OrderedDict()
self.tail: dict[str, _Inflight] = {}
self.inflight: dict[tuple[Optional[str], str], _Inflight] = {}
+ # abs path -> content of the newest edit scheduled for it (set on the tool thread at schedule
+ # time; a single dict store): what the server should hold once a check has diagnosed its own,
+ # possibly older, snapshot.
+ self.newest: dict[str, str] = {}
def remember(self, abs_path: str, text: str, diags: _Diags) -> None:
self.verdicts.pop(abs_path, None)
@@ -95,24 +101,32 @@ def inline_wait(started: Optional[float]) -> float:
def _digest(text: str) -> str:
- return hashlib.sha256(text.encode("utf-8", "surrogateescape")).hexdigest()
+ return hashlib.sha256(as_disk_read(text).encode("utf-8", "surrogateescape")).hexdigest()
-def start(svc: LSPService, path: str, *, pre_text: Optional[str], owner: Optional[str] = None,
- carry: tuple = ()) -> Optional[concurrent.futures.Future]:
+def start(svc: LSPService, path: str, *, pre_text: Optional[str], post_text: Optional[str] = None,
+ 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`. ``pre_text`` None = the file is new. ``owner`` = the editing session (None:
+ :class:`CheckResult`. ``pre_text`` None = the file is new; ``post_text`` = the content this edit
+ wrote (None: read the file when the check runs). ``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, owner, tuple(carry)), loop)
+ abs_path = os.path.abspath(path)
+ checks: EditChecks = svc._edit_checks
+ if post_text is not None:
+ checks.newest[abs_path] = post_text
+ else:
+ checks.newest.pop(abs_path, None)
+ return safe_schedule_threadsafe(
+ _serialized(svc, abs_path, pre_text, post_text, owner, tuple(carry)), loop)
-async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], owner: Optional[str],
- carry: tuple) -> CheckResult:
+async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], post_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."""
@@ -148,7 +162,7 @@ async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], o
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),
+ diags = await asyncio.wait_for(_check(svc, abs_path, baseline_text, post_text, budget),
timeout=2 * budget + _SPAWN_CEILING)
except asyncio.CancelledError:
raise
@@ -169,13 +183,17 @@ async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], o
finally:
if checks.tail.get(abs_path) is me:
del checks.tail[abs_path]
+ if post_text is not None and checks.newest.get(abs_path) is post_text:
+ del checks.newest[abs_path] # no newer edit scheduled: the server holds these bytes
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]:
- """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.
+async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], post_text: Optional[str],
+ budget: float) -> Optional[_Diags]:
+ """Verdict on ``post_text`` (the bytes this edit wrote) 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. The server is left holding the file's current disk content afterwards.
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)
@@ -191,17 +209,20 @@ async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], b
# 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
+ abs_path, language_id=language_id, text=post_text if baseline_text is None else baseline_text
+ ) == 0 else None
if baseline_text is None:
+ await _resync_disk(svc, client, abs_path, language_id)
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)
+ version = await client.open_file(abs_path, language_id=language_id, text=post_text)
await client.save_file(abs_path)
diags = await _verdict(svc, client, abs_path, version, budget, use_sync)
svc._touch(client)
if diags is None:
+ await _resync_disk(svc, client, abs_path, language_id)
# 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)
@@ -217,16 +238,29 @@ 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
- # 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
+ await _resync_disk(svc, client, abs_path, language_id)
return _report(server_id, abs_path, diags)
+async def _resync_disk(svc: LSPService, client: Any, abs_path: str, language_id: str) -> None:
+ """Leave the server holding the newest content written to the file (a newer edit, or this check's
+ baseline sync, may differ from the snapshot just diagnosed); no-op when it already does. Unknown
+ newest content (a caller without a snapshot) re-reads the file, as every sync used to."""
+ newest = svc._edit_checks.newest.get(abs_path)
+ if newest is None:
+ try:
+ await client.open_file(abs_path, language_id=language_id)
+ except Exception: # the file vanished: the next check syncs whatever is there then
+ logger.debug("LSP resync of %s failed", abs_path, exc_info=True)
+ elif as_disk_read(newest) != client.document_text(abs_path):
+ await client.open_file(abs_path, language_id=language_id, text=newest)
+
+
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
diff --git a/agent/lsp/manager.py b/agent/lsp/manager.py
index 8aa0a42618d..d9d0ead8a33 100644
--- a/agent/lsp/manager.py
+++ b/agent/lsp/manager.py
@@ -362,16 +362,19 @@ 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], owner: Optional[str] = None,
- carry: tuple = ()):
+ def start_edit_check(self, file_path: str, *, pre_text: Optional[str], post_text: Optional[str] = None,
+ 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. ``owner`` = the
- editing session; ``carry`` = its undelivered notes for the file this check takes over."""
+ editing session; ``carry`` = its undelivered notes for the file this check takes over.
+ ``pre_text`` / ``post_text`` = the edit's own before/after content: the check diagnoses these
+ snapshots, never whatever another session has written to the file since."""
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, owner=owner, carry=carry)
+ return edit_check.start(self, file_path, pre_text=pre_text, post_text=post_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.
diff --git a/tests/agent/lsp/test_backend_gate.py b/tests/agent/lsp/test_backend_gate.py
index 4744910dc48..253197a432f 100644
--- a/tests/agent/lsp/test_backend_gate.py
+++ b/tests/agent/lsp/test_backend_gate.py
@@ -51,7 +51,7 @@ def test_maybe_lsp_diagnostics_returns_empty_for_non_local(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."""
+ """The local backend hands the written file and its pre-/post-edit text to the service's edit check."""
from tools.environments.local import LocalEnvironment
from tools.file_operations import ShellFileOperations
@@ -62,10 +62,10 @@ def test_edit_check_started_for_local_env(tmp_path, monkeypatch):
def enabled_for(self, path):
return True
- def start_edit_check(self, path, *, pre_text, owner=None, carry=()):
- started.append((path, pre_text))
+ def start_edit_check(self, path, *, pre_text, post_text=None, owner=None, carry=()):
+ started.append((path, pre_text, post_text))
monkeypatch.setattr("agent.lsp.get_service", lambda: FakeService())
- 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")]
+ assert fops._maybe_lsp_diagnostics(str(tmp_path / "x.py"), pre_content="x = 1\n", post_content="x = 2\n") == ""
+ assert started == [(str(tmp_path / "x.py"), "x = 1\n", "x = 2\n")]
diff --git a/tests/agent/lsp/test_write_never_waits_on_lsp.py b/tests/agent/lsp/test_write_never_waits_on_lsp.py
index 60e6d92c5c1..d51931e7928 100644
--- a/tests/agent/lsp/test_write_never_waits_on_lsp.py
+++ b/tests/agent/lsp/test_write_never_waits_on_lsp.py
@@ -136,3 +136,36 @@ def test_diagnostics_stay_with_the_session_that_made_the_edit(mock_workspace):
notes_a = deferred.pop_notes("S-A")
assert not notes_b, f"session B was told about session A's error: {notes_b}"
assert notes_a and "MOCK001" in notes_a[0][1], f"session A never heard about its own error: {notes_a}"
+
+
+def test_each_session_is_checked_on_the_bytes_it_wrote(mock_workspace):
+ """Session A's clean edit is still unchecked when session B writes an error into the same file:
+ A's check must diagnose the bytes A wrote (clean), not the file as B left it, and B gets its error."""
+ import threading
+
+ repo, gate = mock_workspace("by_content")
+ gate.touch() # the server answers at once; the LSP loop itself is held below
+ target = repo / "e.py"
+ target.write_text("x = 1\n", encoding="utf-8")
+ from agent.lsp import get_service
+ svc = get_service()
+ held, release = threading.Event(), threading.Event()
+
+ def hold():
+ held.set()
+ release.wait(_WAIT)
+
+ svc._loop._loop.call_soon_threadsafe(hold)
+ assert held.wait(5)
+ try:
+ result_a, _ = _patch(target, "x = 1", "x = 2", session="S-A2")
+ result_b, _ = _patch(target, "x = 2", "x = bad", session="S-B2")
+ finally:
+ release.set()
+ assert result_a["success"] is True and result_b["success"] is True, (result_a, result_b)
+ wait_until(lambda: _settled("S-A2", "S-B2"), timeout=_WAIT, what="both edit checks settling")
+ notes_a = deferred.pop_notes("S-A2")
+ notes_b = deferred.pop_notes("S-B2")
+ assert not notes_a and "lsp_diagnostics" not in result_a, f"A was told about B's error: {notes_a or result_a}"
+ told_b = " ".join(text for _path, text in notes_b) + str(result_b.get("lsp_diagnostics") or "")
+ assert "MOCK001" in told_b, f"session B never heard about its own error: {notes_b}"
diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.py
index 6a7966aa6b4..8012415f2d3 100644
--- a/tools/file_operations_lint.py
+++ b/tools/file_operations_lint.py
@@ -331,12 +331,11 @@ class LintMixin:
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
+ (``agent.lsp.edit_check``) diagnoses ``post_content`` (the bytes this write produced) against
+ ``pre_content`` (None = new file) on the LSP loop: those snapshots, not a later read of the
+ file, which another session may already have rewritten. 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_content
+ verdict reaches the model with a later tool result of the same session (``agent.lsp.deferred``)."""
svc = self._lsp_service()
if svc is None or not self._lsp_will_handle(path):
return ""
@@ -349,7 +348,8 @@ class LintMixin:
# 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)
+ future = svc.start_edit_check(path, pre_text=pre_content, post_text=post_content, owner=owner,
+ carry=carry)
except Exception:
return ""
if future is None: