The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(lsp): treat a deleted process cwd as no anchor so write diagnostics never fail a landed write
Konstantin Khlopkov570b3c1a6c4d
diff --git a/agent/lsp/workspace.py b/agent/lsp/workspace.pyindex 799798cae65..6688193c307 100644--- a/agent/lsp/workspace.py+++ b/agent/lsp/workspace.py@@ -129,13 +129,27 @@ def nearest_root(return None+def _current_dir() -> Optional[str]:+ """Process cwd, or ``None`` when the directory was removed underneath the+ running process (a scratch workspace cleaned up at card completion). The+ kernel keeps the inode alive, so ``getcwd`` keeps raising ``FileNotFoundError``+ even after the same path is recreated — from the resolver's point of view+ there is simply no cwd anchor left."""+ try:+ return os.getcwd()+ except OSError:+ return None++def resolve_workspace_for_file(file_path: str, *, cwd: Optional[str] = None) -> Tuple[Optional[str], bool]:"""Return ``(workspace_root, gated_in)`` for a file. The cwd's worktree wins when the file isinside it; otherwise the file's own worktree is the fallback anchor (monorepos / unrelatedcheckouts). ``(None, False)`` when neither is in a git worktree."""- cwd_root = find_git_worktree(cwd or os.getcwd())- if cwd_root is not None and is_inside_workspace(file_path, cwd_root):- return cwd_root, True+ cwd_anchor = cwd if cwd else _current_dir()+ if cwd_anchor is not None:+ cwd_root = find_git_worktree(cwd_anchor)+ if cwd_root is not None and is_inside_workspace(file_path, cwd_root):+ return cwd_root, Truefile_root = find_git_worktree(file_path)if file_root is not None:return file_root, Truediff --git a/tests/agent/lsp/test_diagnostics_field.py b/tests/agent/lsp/test_diagnostics_field.pyindex 55410f9b022..d9c3f129935 100644--- a/tests/agent/lsp/test_diagnostics_field.py+++ b/tests/agent/lsp/test_diagnostics_field.py@@ -6,7 +6,7 @@ having LSP output prepended to the lint string."""from __future__ import annotations-from unittest.mock import patch+from unittest.mock import MagicMock, patchfrom tools.environments.local import LocalEnvironment@@ -80,6 +80,22 @@ def test_write_file_skips_lsp_when_syntax_failed(tmp_path):assert res.lint["status"] == "error"+def test_maybe_lsp_diagnostics_swallows_enabled_for_failure(tmp_path):+ """``_maybe_lsp_diagnostics`` gates through the guarded ``_lsp_will_handle``+ helper, so a workspace-resolution failure (e.g. the process cwd was removed+ under a running worker) degrades to "no LSP for this write" instead of+ surfacing as an error from a write that already landed on disk."""+ fops = ShellFileOperations(LocalEnvironment(cwd=str(tmp_path)))++ with patch.object(fops, "_lsp_service") as mock_service:+ mock_service.return_value.enabled_for = MagicMock(+ side_effect=FileNotFoundError(2, "No such file or directory")+ )+ result = fops._maybe_lsp_diagnostics(str(tmp_path / "x.py"))++ assert result == ""++# ---------------------------------------------------------------------------# patch_replace propagates the field from the inner write_file# ---------------------------------------------------------------------------diff --git a/tests/agent/lsp/test_workspace.py b/tests/agent/lsp/test_workspace.pyindex 7a0a6abcd74..c4657cc1717 100644--- a/tests/agent/lsp/test_workspace.py+++ b/tests/agent/lsp/test_workspace.py@@ -78,6 +78,38 @@ def test_resolve_workspace_for_file_uses_cwd_first(tmp_path: Path, monkeypatch):assert gated is True+def test_resolve_workspace_for_file_survives_deleted_cwd(tmp_path: Path, monkeypatch):+ """A removed process cwd must read as "no anchor", not raise — the LSP+ workspace resolver runs inside a write tool and must never break a write+ that already landed on disk."""+ repo = tmp_path / "repo"+ (repo / ".git").mkdir(parents=True)+ file_path = repo / "x.py"+ file_path.write_text("")++ def _deleted_cwd():+ raise FileNotFoundError(2, "No such file or directory")++ monkeypatch.setattr("agent.lsp.workspace.os.getcwd", _deleted_cwd)+ # cwd argument absent → the process cwd is consulted and raises+ root, gated = resolve_workspace_for_file(str(file_path))+ # falls through to the file's own worktree+ assert root == str(repo)+ assert gated is True+++def test_current_dir_none_when_cwd_unreadable(monkeypatch):+ from agent.lsp.workspace import _current_dir++ def _deleted_cwd():+ raise FileNotFoundError(2, "No such file or directory")++ monkeypatch.setattr("agent.lsp.workspace.os.getcwd", _deleted_cwd)+ assert _current_dir() is None+ monkeypatch.setattr("agent.lsp.workspace.os.getcwd", lambda: "/somewhere")+ assert _current_dir() == "/somewhere"++diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.pyindex 48bad2b8c19..32dec2f7d2a 100644--- a/tools/file_operations_lint.py+++ b/tools/file_operations_lint.py@@ -275,7 +275,7 @@ class LintMixin:remaps baseline diagnostics into post-edit coordinates; otherwise everypre-existing diagnostic below an inserted line would look new."""svc = self._lsp_service()- if svc is None or not svc.enabled_for(path):+ if svc is None or not self._lsp_will_handle(path):return ""line_shift = Noneif pre_content is not None and post_content is not None and pre_content != post_content: