edit_check.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """Post-write LSP check that never holds the write.
  2. ``write_file`` / ``patch`` used to block on two language-server round trips per edit: a pre-write
  3. baseline (open + wait) and the post-write verdict (open + save + wait), each up to ``lsp.wait_timeout``.
  4. A push-only server that stays silent on a clean edit (typescript-language-server publishes nothing
  5. when a file's diagnostics stay empty) ran both waits to the deadline: 10s per clean TypeScript edit.
  6. Here the check is one coroutine on the service loop: sync the edit's post-edit snapshot and wait for
  7. its verdict; only when that verdict has diagnostics, get the verdict on the pre-edit snapshot (or reuse
  8. the one an earlier check left for exactly that text) and return the diagnostics the edit introduced.
  9. Both snapshots are the bytes the write itself held, never a later read of the file: by the time a
  10. queued check runs, another session may have written the file again, and that session's errors are not
  11. this edit's.
  12. The caller waits for it only within its latency budget (:func:`inline_wait`); a slower verdict is delivered later through
  13. :mod:`agent.lsp.deferred`. Checks of one file run one at a time (two checks syncing one document at
  14. once would satisfy each other's waits). A newer check by the SAME session takes an unfinished one over
  15. (its baseline, the undelivered notes it carried, and its result when the newer check gets no verdict),
  16. so a superseded edit's errors are still caught unless the newer edit fixed them; a check by another
  17. session only queues behind it, so each session hears about its own edits only.
  18. """
  19. from __future__ import annotations
  20. import asyncio
  21. import concurrent.futures
  22. import hashlib
  23. import logging
  24. import os
  25. import time
  26. from collections import OrderedDict
  27. from dataclasses import dataclass
  28. from typing import TYPE_CHECKING, Any, Optional
  29. from agent.lsp import eventlog, sync_diagnostics
  30. from agent.lsp.client import _diagnostic_key, as_disk_read
  31. from agent.lsp.servers import language_id_for
  32. if TYPE_CHECKING:
  33. from agent.lsp.manager import LSPService
  34. logger = logging.getLogger("agent.lsp.edit_check")
  35. # A write waits for its verdict only while it stays inside WRITE_BUDGET (from write entry). A warm
  36. # tsserver answers its synchronous request in ~10-30ms and a warm pyright its pull in ~10-90ms, so the
  37. # common verdict is reported inline; a slower one is delivered later (agent.lsp.deferred).
  38. WRITE_BUDGET = 0.08
  39. INLINE_GRACE = 0.08
  40. _VERDICT_CAP = 256 # per-file last verdicts kept for baseline reuse
  41. _SPAWN_CEILING = 60.0 # outer bound on one check beyond its two waits (cold spawn + initialize)
  42. _Diags = list[dict[str, Any]]
  43. @dataclass
  44. class CheckResult:
  45. diagnostics: Optional[_Diags] # introduced by the edit (baseline removed); [] = clean; None = no verdict
  46. baseline_text: Optional[str] # the pre-edit text this result was diffed against
  47. superseded: bool = False # a newer check of the file by the same session took this one over
  48. # Undelivered older notes this check took over and did not settle (no verdict): the caller queues
  49. # them again. Empty whenever there is a verdict (it was diffed against the oldest baseline).
  50. carry: tuple = ()
  51. @dataclass
  52. class _Inflight:
  53. task: asyncio.Task
  54. baseline_text: Optional[str]
  55. carry: tuple
  56. superseded: bool = False
  57. class EditChecks:
  58. """Per-service state, touched only on the loop thread: the last verdict per file (baseline reuse),
  59. the newest unfinished check per file (``tail``: serializes every session's checks of it) and per
  60. ``(session, file)`` (``inflight``: the one a newer edit by that session takes over)."""
  61. def __init__(self) -> None:
  62. self.verdicts: OrderedDict[str, tuple[str, _Diags]] = OrderedDict()
  63. self.tail: dict[str, _Inflight] = {}
  64. self.inflight: dict[tuple[Optional[str], str], _Inflight] = {}
  65. # abs path -> content of the newest edit scheduled for it (set on the tool thread at schedule
  66. # time; a single dict store): what the server should hold once a check has diagnosed its own,
  67. # possibly older, snapshot.
  68. self.newest: dict[str, str] = {}
  69. def remember(self, abs_path: str, text: str, diags: _Diags) -> None:
  70. self.verdicts.pop(abs_path, None)
  71. self.verdicts[abs_path] = (_digest(text), list(diags))
  72. while len(self.verdicts) > _VERDICT_CAP:
  73. self.verdicts.popitem(last=False)
  74. def recall(self, abs_path: str, text: str) -> Optional[_Diags]:
  75. hit = self.verdicts.get(abs_path)
  76. return list(hit[1]) if hit is not None and hit[0] == _digest(text) else None
  77. def inline_wait(started: Optional[float]) -> float:
  78. """Seconds the write may still wait for its verdict inline."""
  79. if started is None:
  80. return INLINE_GRACE
  81. return max(0.0, min(INLINE_GRACE, started + WRITE_BUDGET - time.monotonic()))
  82. def _digest(text: str) -> str:
  83. return hashlib.sha256(as_disk_read(text).encode("utf-8", "surrogateescape")).hexdigest()
  84. def start(svc: LSPService, path: str, *, pre_text: Optional[str], post_text: Optional[str] = None,
  85. owner: Optional[str] = None, carry: tuple = ()) -> Optional[concurrent.futures.Future]:
  86. """Schedule the check of ``path`` (already written) on the service loop; the Future resolves to a
  87. :class:`CheckResult`. ``pre_text`` None = the file is new; ``post_text`` = the content this edit
  88. wrote (None: read the file when the check runs). ``owner`` = the editing session (None:
  89. no later delivery, nothing to take over); ``carry`` = that session's undelivered notes for this
  90. file (``agent.lsp.deferred``) this edit supersedes: the oldest one's baseline becomes ours."""
  91. from agent.async_utils import safe_schedule_threadsafe
  92. loop = svc._loop._loop
  93. if loop is None:
  94. return None
  95. abs_path = os.path.abspath(path)
  96. checks: EditChecks = svc._edit_checks
  97. if post_text is not None:
  98. checks.newest[abs_path] = post_text
  99. else:
  100. checks.newest.pop(abs_path, None)
  101. return safe_schedule_threadsafe(
  102. _serialized(svc, abs_path, pre_text, post_text, owner, tuple(carry)), loop)
  103. async def _serialized(svc: LSPService, abs_path: str, pre_text: Optional[str], post_text: Optional[str],
  104. owner: Optional[str], carry: tuple) -> CheckResult:
  105. """Run after any unfinished check of the same file; take over the same session's unfinished check:
  106. its baseline and carried notes become ours, and its result is ours to report when we get no
  107. verdict. Never cancelled: cancelling mid-spawn would restart a cold server on every rapid edit."""
  108. checks: EditChecks = svc._edit_checks
  109. baseline_text = carry[0].baseline_text if carry else pre_text
  110. tail = checks.tail.get(abs_path)
  111. tail = tail if tail is not None and not tail.task.done() else None
  112. prev = checks.inflight.get((owner, abs_path)) if owner else None
  113. if prev is not None and not prev.task.done():
  114. prev.superseded = True
  115. baseline_text, carry = prev.baseline_text, prev.carry + carry
  116. else:
  117. prev = None
  118. current = asyncio.current_task()
  119. assert current is not None
  120. me = _Inflight(current, baseline_text, carry)
  121. checks.tail[abs_path] = me
  122. if owner:
  123. checks.inflight[(owner, abs_path)] = me
  124. try:
  125. prev_result = None
  126. waits = {t.task for t in (prev, tail) if t is not None}
  127. if waits:
  128. await asyncio.wait(waits)
  129. if prev is not None and not prev.task.cancelled() and prev.task.exception() is None:
  130. prev_result = prev.task.result()
  131. if prev_result.diagnostics is None:
  132. prev_result = None
  133. if me.superseded:
  134. # A newer edit by this session queued behind us: it checks the newest bytes against our
  135. # baseline and owns our carried notes, so our own round trip would only delay it.
  136. return CheckResult(prev_result.diagnostics if prev_result else None,
  137. prev_result.baseline_text if prev_result else baseline_text, True)
  138. budget = svc._wait_budget(abs_path)
  139. try:
  140. diags = await asyncio.wait_for(_check(svc, abs_path, baseline_text, post_text, budget),
  141. timeout=2 * budget + _SPAWN_CEILING)
  142. except asyncio.CancelledError:
  143. raise
  144. except Exception as e: # timeout / dead server: same treatment as the old blocking path
  145. logger.debug("LSP edit check failed for %s", abs_path, exc_info=True)
  146. await svc._mark_broken_for_file_async(abs_path, e)
  147. diags = None
  148. if diags is not None:
  149. result = CheckResult(diags, baseline_text)
  150. elif prev_result is not None:
  151. result = CheckResult(prev_result.diagnostics, prev_result.baseline_text)
  152. else:
  153. result = CheckResult(None, baseline_text, carry=carry)
  154. result.superseded = me.superseded
  155. if me.superseded:
  156. result.carry = () # the newer check took them over
  157. return result
  158. finally:
  159. if checks.tail.get(abs_path) is me:
  160. del checks.tail[abs_path]
  161. if post_text is not None and checks.newest.get(abs_path) is post_text:
  162. del checks.newest[abs_path] # no newer edit scheduled: the server holds these bytes
  163. if owner and checks.inflight.get((owner, abs_path)) is me:
  164. del checks.inflight[(owner, abs_path)]
  165. async def _check(svc: LSPService, abs_path: str, baseline_text: Optional[str], post_text: Optional[str],
  166. budget: float) -> Optional[_Diags]:
  167. """Verdict on ``post_text`` (the bytes this edit wrote) first; only when it has diagnostics, the
  168. verdict on ``baseline_text`` (cached from an earlier check when that text was checked before) to drop
  169. the pre-existing ones. The server is left holding the file's current disk content afterwards.
  170. A clean edit costs one round trip, and push-only servers answer the baseline fast in the common
  171. case (errors -> fewer errors is a change they publish)."""
  172. client = await svc._get_or_spawn(abs_path)
  173. if client is None:
  174. return None
  175. srv = svc._server_for(abs_path)
  176. server_id = srv.server_id if srv is not None else "lsp"
  177. language_id = language_id_for(abs_path, srv)
  178. checks: EditChecks = svc._edit_checks
  179. use_sync = sync_diagnostics.supported(client)
  180. if client.seeds_first_push and client.document_text(abs_path) is None and not use_sync:
  181. # First sync of this document on a seeding server: its first push is a verdict on the didOpen
  182. # text that never counts as fresh. Open the pre-edit text so that push becomes the baseline;
  183. # a new file is opened as written, so the seed is the verdict itself.
  184. seed = await client.wait_for_seed(abs_path, budget) if await client.open_file(
  185. abs_path, language_id=language_id, text=post_text if baseline_text is None else baseline_text
  186. ) == 0 else None
  187. if baseline_text is None:
  188. await _resync_disk(svc, client, abs_path, language_id)
  189. svc._touch(client)
  190. return _report(server_id, abs_path, seed)
  191. if seed is not None:
  192. checks.remember(abs_path, baseline_text, seed)
  193. version = await client.open_file(abs_path, language_id=language_id, text=post_text)
  194. await client.save_file(abs_path)
  195. diags = await _verdict(svc, client, abs_path, version, budget, use_sync)
  196. svc._touch(client)
  197. if diags is None:
  198. await _resync_disk(svc, client, abs_path, language_id)
  199. # Alive but no verdict in budget: "no data", never stale stores (ghost diagnostics).
  200. return _report(server_id, abs_path, None)
  201. synced = client.document_text(abs_path)
  202. verdict = diags
  203. if diags and baseline_text is not None and synced is not None:
  204. # Read the cached pre-edit verdict (an earlier check's, or the seed above) BEFORE this edit's
  205. # verdict replaces the file's cache entry.
  206. baseline = checks.recall(abs_path, baseline_text)
  207. if baseline is None:
  208. baseline = []
  209. base_version = await client.open_file(abs_path, language_id=language_id, text=baseline_text)
  210. # No verdict on the pre-edit text -> empty baseline: the filter removes less, never more.
  211. base = await _verdict(svc, client, abs_path, base_version, budget, use_sync)
  212. if base is not None:
  213. baseline = base
  214. from agent.lsp.range_shift import build_line_shift, shift_baseline
  215. seen = {_diagnostic_key(d) for d in shift_baseline(baseline, build_line_shift(baseline_text, synced))}
  216. diags = [d for d in diags if _diagnostic_key(d) not in seen]
  217. if synced is not None:
  218. checks.remember(abs_path, synced, verdict) # the next edit's baseline is these bytes
  219. await _resync_disk(svc, client, abs_path, language_id)
  220. return _report(server_id, abs_path, diags)
  221. async def _resync_disk(svc: LSPService, client: Any, abs_path: str, language_id: str) -> None:
  222. """Leave the server holding the newest content written to the file (a newer edit, or this check's
  223. baseline sync, may differ from the snapshot just diagnosed); no-op when it already does. Unknown
  224. newest content (a caller without a snapshot) re-reads the file, as every sync used to."""
  225. newest = svc._edit_checks.newest.get(abs_path)
  226. if newest is None:
  227. try:
  228. await client.open_file(abs_path, language_id=language_id)
  229. except Exception: # the file vanished: the next check syncs whatever is there then
  230. logger.debug("LSP resync of %s failed", abs_path, exc_info=True)
  231. elif as_disk_read(newest) != client.document_text(abs_path):
  232. await client.open_file(abs_path, language_id=language_id, text=newest)
  233. async def _verdict(svc: LSPService, client: Any, abs_path: str, version: int, budget: float,
  234. use_sync: bool) -> Optional[_Diags]:
  235. """Diagnostics on the text synced at ``version``: a synchronous request where the server has one
  236. (``agent.lsp.sync_diagnostics``, ~10ms, so the write can report it inline), else the push/pull
  237. wait. None = no verdict within ``budget``."""
  238. if use_sync:
  239. try:
  240. diags = await sync_diagnostics.request(client, abs_path, timeout=budget)
  241. except TimeoutError:
  242. return None
  243. if diags is not None:
  244. return diags
  245. if not await client.wait_for_diagnostics(abs_path, version, mode=svc._wait_mode, timeout=budget):
  246. return None
  247. return list(client.diagnostics_for(abs_path, fresh_only=True))
  248. def _report(server_id: str, abs_path: str, diags: Optional[_Diags]) -> Optional[_Diags]:
  249. if diags is None:
  250. eventlog.log_timeout(server_id, abs_path, kind="fresh diagnostics")
  251. elif diags:
  252. eventlog.log_diagnostics(server_id, abs_path, len(diags))
  253. else:
  254. eventlog.log_clean(server_id, abs_path)
  255. return diags
  256. __all__ = ["INLINE_GRACE", "WRITE_BUDGET", "CheckResult", "EditChecks", "inline_wait", "start"]