-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapprovals.py
More file actions
317 lines (286 loc) · 10.1 KB
/
approvals.py
File metadata and controls
317 lines (286 loc) · 10.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from __future__ import annotations as _annotations
import json
from collections.abc import Awaitable, Callable
from dataclasses import asdict, dataclass, replace
from pathlib import Path
from typing import Literal
from vcode.config import load_preferences
from vcode.sessions import SessionStore, utc_now
__all__ = (
"ApprovalDecision",
"ApprovalPolicy",
"ApprovalRequest",
"ApprovalResolution",
"ApprovalRule",
)
ApprovalOutcome = Literal["allow", "deny", "ask"]
ApprovalToolKind = Literal[
"read",
"edit",
"delete",
"move",
"search",
"execute",
"think",
"fetch",
"switch_mode",
"other",
]
ApprovalResolverKind = Literal[
"allow_once",
"allow_always",
"reject_once",
"reject_always",
"cancelled",
]
ApprovalResolver = Callable[["ApprovalRequest"], Awaitable["ApprovalResolution"]]
@dataclass(frozen=True, slots=True, kw_only=True)
class ApprovalRule:
tool_name: str
target: str
outcome: ApprovalOutcome
created_at: str
source: str = "manual"
imported_from_session_id: str | None = None
@dataclass(frozen=True, slots=True, kw_only=True)
class ApprovalRequest:
session_id: str
workspace_root: Path
tool_name: str
target: str
kind: ApprovalToolKind
reason: str
tool_call_id: str
title: str | None = None
raw_input: dict[str, object] | None = None
old_text: str | None = None
new_text: str | None = None
@dataclass(frozen=True, slots=True, kw_only=True)
class ApprovalResolution:
kind: ApprovalResolverKind
@dataclass(frozen=True, slots=True, kw_only=True)
class ApprovalDecision:
outcome: ApprovalOutcome
message: str | None = None
class ApprovalPolicy:
def __init__(
self,
store: SessionStore | None = None,
resolver: ApprovalResolver | None = None,
) -> None:
self.store = store or SessionStore()
self.resolver = resolver
def approvals_file(self, cwd: Path, session_id: str) -> Path:
return self.store.session_dir(cwd.resolve(), session_id) / "approvals.json"
def load_rules(self, cwd: Path, session_id: str) -> list[ApprovalRule]:
path = self.approvals_file(cwd, session_id)
if not path.exists():
return []
payload = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(payload, list):
return []
rules: list[ApprovalRule] = []
for raw_rule in payload:
if not isinstance(raw_rule, dict):
continue
tool_name = str(raw_rule.get("tool_name", "")).strip()
target = str(raw_rule.get("target", "")).strip()
outcome = _parse_approval_outcome(raw_rule.get("outcome"))
if not tool_name or not target or outcome is None:
continue
rules.append(
ApprovalRule(
tool_name=tool_name,
target=target,
outcome=outcome,
created_at=str(raw_rule.get("created_at", utc_now())),
source=str(raw_rule.get("source", "manual")),
imported_from_session_id=(
str(raw_rule["imported_from_session_id"])
if raw_rule.get("imported_from_session_id") is not None
else None
),
)
)
return rules
def save_rules(self, cwd: Path, session_id: str, rules: list[ApprovalRule]) -> None:
path = self.approvals_file(cwd, session_id)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps([asdict(rule) for rule in rules], indent=2, sort_keys=True),
encoding="utf-8",
)
def find_rule(
self, cwd: Path, session_id: str, tool_name: str, target: str
) -> ApprovalRule | None:
for rule in self.load_rules(cwd, session_id):
if rule.tool_name == tool_name and rule.target == target:
return rule
return None
def set_rule(
self,
cwd: Path,
session_id: str,
tool_name: str,
target: str,
outcome: ApprovalOutcome,
*,
source: str = "manual",
imported_from_session_id: str | None = None,
) -> ApprovalRule:
rules = [
rule
for rule in self.load_rules(cwd, session_id)
if not (rule.tool_name == tool_name and rule.target == target)
]
rule = ApprovalRule(
tool_name=tool_name,
target=target,
outcome=outcome,
created_at=utc_now(),
source=source,
imported_from_session_id=imported_from_session_id,
)
rules.append(rule)
self.save_rules(cwd, session_id, rules)
return rule
def import_rules(self, cwd: Path, session_id: str, source_session_id: str) -> int:
destination_record = self.store.load(cwd, session_id)
source_record = self.store.load(cwd, source_session_id)
if destination_record is None or source_record is None:
return 0
existing = {
(rule.tool_name, rule.target): rule for rule in self.load_rules(cwd, session_id)
}
merged = list(existing.values())
imported_count = 0
for rule in self.load_rules(cwd, source_session_id):
key = (rule.tool_name, rule.target)
if key in existing:
continue
merged.append(
ApprovalRule(
tool_name=rule.tool_name,
target=rule.target,
outcome=rule.outcome,
created_at=utc_now(),
source="imported",
imported_from_session_id=source_session_id,
)
)
imported_count += 1
self.save_rules(cwd, session_id, merged)
updated_record = replace(
destination_record,
imported_approval_session_ids=sorted(
set(destination_record.imported_approval_session_ids + [source_session_id])
),
updated_at=utc_now(),
)
self.store.save(updated_record)
return imported_count
def build_write_request(
self,
workspace_root: Path,
session_id: str,
target: Path,
content: str,
*,
tool_call_id: str | None = None,
) -> ApprovalRequest:
relative_target = str(target.relative_to(workspace_root))
old_text: str | None = None
if target.exists() and target.is_file():
old_text = target.read_text(encoding="utf-8", errors="replace")
return ApprovalRequest(
session_id=session_id,
workspace_root=workspace_root,
tool_name="write_file",
target=relative_target,
kind="edit",
reason=f"Write {relative_target}",
tool_call_id=tool_call_id or f"approval-write-{session_id}-{relative_target}",
title=f"Write {relative_target}",
raw_input={"path": relative_target, "content": content},
old_text=old_text,
new_text=content,
)
def evaluate(self, request: ApprovalRequest) -> ApprovalDecision:
if request.tool_name != "write_file":
return ApprovalDecision(
outcome="ask", message=f"Approval required for {request.tool_name}."
)
relative_target = request.target
if relative_target.split("/")[:2] == [".vcode", "plans"]:
return ApprovalDecision(outcome="allow")
preferences = load_preferences(request.workspace_root)
if preferences.yolo_default:
return ApprovalDecision(outcome="allow")
existing_rule = self.find_rule(
request.workspace_root,
request.session_id,
request.tool_name,
relative_target,
)
if existing_rule is not None:
if existing_rule.outcome == "allow":
return ApprovalDecision(outcome="allow")
return ApprovalDecision(
outcome="deny",
message=f"Approval denied for write_file {relative_target}.",
)
return ApprovalDecision(
outcome="ask",
message=(
f"Approval required for write_file {relative_target}. "
f"Run `/approve write {relative_target}` to allow it for this session."
),
)
async def resolve(self, request: ApprovalRequest) -> ApprovalDecision:
decision = self.evaluate(request)
if decision.outcome != "ask":
return decision
if self.resolver is None:
return decision
relative_target = request.target
resolution = await self.resolver(request)
if resolution.kind == "allow_always":
self.set_rule(
request.workspace_root,
request.session_id,
request.tool_name,
relative_target,
"allow",
source="approval_prompt",
)
return ApprovalDecision(outcome="allow")
if resolution.kind == "reject_always":
self.set_rule(
request.workspace_root,
request.session_id,
request.tool_name,
relative_target,
"deny",
source="approval_prompt",
)
return ApprovalDecision(
outcome="deny",
message=f"Approval denied for write_file {relative_target}.",
)
if resolution.kind == "allow_once":
return ApprovalDecision(outcome="allow")
return ApprovalDecision(
outcome="deny",
message=f"Approval denied for write_file {relative_target}.",
)
def _parse_approval_outcome(value: object) -> ApprovalOutcome | None:
if not isinstance(value, str):
return None
normalized = value.strip()
if normalized == "allow":
return "allow"
if normalized == "deny":
return "deny"
if normalized == "ask":
return "ask"
return None