-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview-implementation-mark-edit.py
More file actions
executable file
·85 lines (67 loc) · 2.23 KB
/
Copy pathreview-implementation-mark-edit.py
File metadata and controls
executable file
·85 lines (67 loc) · 2.23 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
#!/usr/bin/env python3
import json
import sys
from pathlib import Path
from review_implementation_helpers import (
extract_session_id,
extract_turn_id,
mark_edited,
noop,
)
def _is_path_inside_workspace(file_path: str, workspace_roots: list[str]) -> bool:
try:
file = Path(file_path).resolve(strict=False)
except (OSError, RuntimeError):
return False
for root in workspace_roots:
try:
root_path = Path(root).resolve(strict=False)
except (OSError, RuntimeError):
continue
if file.is_relative_to(root_path):
return True
return False
def _is_effective_edit_payload(payload: dict[str, object]) -> bool:
edits = payload.get("edits")
if isinstance(edits, list) and edits:
all_empty = True
for edit in edits:
if not isinstance(edit, dict):
all_empty = False
break
old = edit.get("old_string")
new = edit.get("new_string")
if old not in ("", None) or new not in ("", None):
all_empty = False
break
if all_empty:
return False
file_path = payload.get("file_path")
workspace_roots_raw = payload.get("workspace_roots")
if isinstance(file_path, str) and file_path and isinstance(workspace_roots_raw, list):
workspace_roots = [root for root in workspace_roots_raw if isinstance(root, str) and root]
if workspace_roots and not _is_path_inside_workspace(file_path=file_path, workspace_roots=workspace_roots):
return False
return True
def main() -> int:
raw = sys.stdin.read().strip()
if not raw:
return noop()
try:
payload = json.loads(raw)
except json.JSONDecodeError:
return noop()
if not isinstance(payload, dict):
return noop()
session_id = extract_session_id(payload=payload)
if not session_id:
return noop()
turn_id = extract_turn_id(payload=payload)
if not turn_id:
return noop()
if not _is_effective_edit_payload(payload):
return noop()
mark_edited(session_id=session_id, turn_id=turn_id)
return noop()
if __name__ == "__main__":
raise SystemExit(main())