-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.py
More file actions
174 lines (142 loc) · 5.59 KB
/
workspace.py
File metadata and controls
174 lines (142 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from __future__ import annotations as _annotations
from dataclasses import dataclass
from fnmatch import fnmatchcase
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from vcode.approvals import ApprovalPolicy
from vcode.hooks import HookEventCollector
__all__ = (
"AgentDeps",
"WorkspaceError",
"WorkspacePathError",
"can_write_path",
"is_ignored_path",
"list_workspace_files",
"read_workspace_file",
"resolve_workspace_path",
"write_workspace_file",
)
@dataclass(frozen=True, slots=True, kw_only=True)
class AgentDeps:
workspace_root: Path
mode_id: str
session_id: str
approval_policy: ApprovalPolicy
hook_event_collector: HookEventCollector | None = None
class WorkspaceError(Exception):
"""Base exception for workspace access failures."""
class WorkspacePathError(WorkspaceError):
"""Raised when a requested path escapes the workspace root."""
def __init__(self, path: str) -> None:
self.path = path
super().__init__(f"Path escapes workspace root: {path}")
def resolve_workspace_path(workspace_root: Path, path: str) -> Path:
candidate = (workspace_root / path).resolve()
try:
candidate.relative_to(workspace_root)
except ValueError as exc:
raise WorkspacePathError(path) from exc
return candidate
def can_write_path(mode_id: str, workspace_root: Path, target_path: Path) -> bool:
relative_path = target_path.relative_to(workspace_root)
if mode_id == "agent":
return True
if mode_id == "plan":
return relative_path.parts[:2] == (".vcode", "plans")
return False
def _load_ignore_patterns(workspace_root: Path) -> list[str]:
ignore_file = workspace_root / ".vcode" / ".vcodeignore"
if not ignore_file.exists():
return []
patterns: list[str] = []
for raw_line in ignore_file.read_text(encoding="utf-8", errors="replace").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or line.startswith("!"):
continue
patterns.append(line)
return patterns
def _match_ignore_pattern(relative_path: str, pattern: str) -> bool:
normalized_path = relative_path.strip("/")
normalized_pattern = pattern.strip()
anchored = normalized_pattern.startswith("/")
directory_only = normalized_pattern.endswith("/")
normalized_pattern = normalized_pattern.strip("/")
if not normalized_pattern:
return False
candidates = [normalized_pattern]
if not anchored:
candidates.append(f"**/{normalized_pattern}")
if directory_only:
path_prefixes = []
current = normalized_path
while current:
path_prefixes.append(current)
if "/" not in current:
break
current = current.rsplit("/", 1)[0]
for prefix in path_prefixes:
if fnmatchcase(prefix, normalized_pattern):
return True
if not anchored and fnmatchcase(prefix, f"*/{normalized_pattern}"):
return True
return False
return any(fnmatchcase(normalized_path, candidate) for candidate in candidates)
def is_ignored_path(workspace_root: Path, target_path: Path) -> bool:
relative_path = str(target_path.relative_to(workspace_root)).replace("\\", "/")
if not relative_path or relative_path == ".":
return False
return any(
_match_ignore_pattern(relative_path, pattern)
for pattern in _load_ignore_patterns(workspace_root)
)
def list_workspace_files(workspace_root: Path, path: str = ".", limit: int = 200) -> str:
try:
target = resolve_workspace_path(workspace_root, path)
except WorkspacePathError as exc:
return str(exc)
if is_ignored_path(workspace_root, target):
return f"Path is ignored by .vcode/.vcodeignore: {path}"
if target.is_file():
return str(target.relative_to(workspace_root))
if not target.exists():
return f"Path not found: {path}"
entries: list[str] = []
for candidate in sorted(target.rglob("*")):
if candidate.name == "__pycache__":
continue
if is_ignored_path(workspace_root, candidate):
continue
relative = str(candidate.relative_to(workspace_root))
if candidate.is_dir():
relative = f"{relative}/"
entries.append(relative)
if len(entries) >= limit:
entries.append("...")
break
if not entries:
return f"No files under {path}"
return "\n".join(entries)
def read_workspace_file(workspace_root: Path, path: str, max_chars: int = 20000) -> str:
try:
target = resolve_workspace_path(workspace_root, path)
except WorkspacePathError as exc:
return str(exc)
if is_ignored_path(workspace_root, target):
return f"Read denied by .vcode/.vcodeignore for path: {path}"
if not target.exists() or not target.is_file():
return f"File not found: {path}"
content = target.read_text(encoding="utf-8")
if len(content) <= max_chars:
return content
return f"{content[:max_chars]}\n\n[truncated]"
def write_workspace_file(workspace_root: Path, mode_id: str, path: str, content: str) -> str:
try:
target = resolve_workspace_path(workspace_root, path)
except WorkspacePathError as exc:
return str(exc)
if not can_write_path(mode_id, workspace_root, target):
return f"Write denied in {mode_id} mode for path: {path}"
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content, encoding="utf-8")
return f"Wrote {target.relative_to(workspace_root)}"