Skip to content

The agent that grows with you

fix(lsp): inline the deleted-cwd guard and keep one real-cwd invariant test

teknium1311123a8da6d
diff --git a/agent/lsp/workspace.py b/agent/lsp/workspace.py
index 6688193c307..8543c35135a 100644
--- a/agent/lsp/workspace.py
+++ b/agent/lsp/workspace.py
@@ -129,27 +129,20 @@ 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 is
inside it; otherwise the file's own worktree is the fallback anchor (monorepos / unrelated
checkouts). ``(None, False)`` when neither is in a git worktree."""
- 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, True
+ try:
+ cwd_anchor = cwd or os.getcwd()
+ except OSError:
+ # The process cwd was removed underneath us (a scratch workspace cleaned up at
+ # card completion); getcwd keeps raising even after the path is recreated, so
+ # there is simply no cwd anchor — fall through to the file's own worktree.
+ cwd_anchor = None
+ cwd_root = find_git_worktree(cwd_anchor) if cwd_anchor else None
+ if cwd_root is not None and is_inside_workspace(file_path, cwd_root):
+ return cwd_root, True
file_root = find_git_worktree(file_path)
if file_root is not None:
return file_root, True
diff --git a/tests/agent/lsp/test_workspace.py b/tests/agent/lsp/test_workspace.py
index c4657cc1717..ce05320b2bf 100644
--- a/tests/agent/lsp/test_workspace.py
+++ b/tests/agent/lsp/test_workspace.py
@@ -23,8 +23,6 @@ def _clear():
clear_cache()
-
-
def test_find_git_worktree_finds_dotgit(tmp_path: Path):
repo = tmp_path / "repo"
repo.mkdir()
@@ -34,12 +32,6 @@ def test_find_git_worktree_finds_dotgit(tmp_path: Path):
assert find_git_worktree(str(sub)) == str(repo)
-
-
-
-
-
-
def test_nearest_root_finds_first_marker(tmp_path: Path):
root = tmp_path / "p"
deep = root / "src" / "pkg"
@@ -62,10 +54,6 @@ def test_nearest_root_skips_package_dirs(tmp_path: Path):
assert found == str(root)
-
-
-
-
def test_resolve_workspace_for_file_uses_cwd_first(tmp_path: Path, monkeypatch):
repo = tmp_path / "repo"
(repo / ".git").mkdir(parents=True)
@@ -86,34 +74,19 @@ def test_resolve_workspace_for_file_survives_deleted_cwd(tmp_path: Path, monkeyp
(repo / ".git").mkdir(parents=True)
file_path = repo / "x.py"
file_path.write_text("")
+ scratch = tmp_path / "scratch"
+ scratch.mkdir()
+ monkeypatch.chdir(scratch)
+ scratch.rmdir()
+ with pytest.raises(OSError):
+ os.getcwd()
- 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"
-
-
-
-
-
-
def test_normalize_path_expands_tilde(monkeypatch):
monkeypatch.setenv("HOME", "/home/user")
p = normalize_path("~/x.py")