-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (92 loc) · 3.24 KB
/
Copy pathmain.py
File metadata and controls
106 lines (92 loc) · 3.24 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
import argparse
import asyncio
import re
import subprocess
from pathlib import Path
from qoder_agent_sdk import (
AssistantMessage,
QoderAgentOptions,
ResultMessage,
TextBlock,
access_token_from_env,
query,
)
MAX_DIFF_BYTES = 100_000
GIT_REF_PATTERN = re.compile(r"^[A-Za-z0-9._/@{}^~:+-]+$")
def validate_git_ref(ref: str) -> str:
if ref.startswith("-") or not GIT_REF_PATTERN.fullmatch(ref):
raise ValueError(f"Invalid Git revision: {ref}")
return ref
def read_diff(workspace: Path, base: str | None, head: str) -> str:
revisions = (
[f"{validate_git_ref(base)}...{validate_git_ref(head)}"]
if base
else [validate_git_ref(head)]
)
result = subprocess.run(
[
"git",
"diff",
"--no-ext-diff",
"--unified=40",
*revisions,
"--",
],
cwd=workspace,
check=True,
capture_output=True,
text=True,
)
diff = result.stdout
if not diff.strip():
if base:
raise RuntimeError(f"No changes found between {base} and {head}.")
raise RuntimeError(f"No working-tree changes found relative to {head}.")
if len(diff.encode()) > MAX_DIFF_BYTES:
raise RuntimeError("The diff is larger than 100 KB; review a smaller change.")
return diff
async def review(workspace: Path, diff: str) -> None:
prompt = (
"Review the following Git diff. Use the repository only to understand "
"surrounding code. Report only concrete correctness, security, reliability, "
"or maintainability problems introduced by the change. For each finding "
"include severity, file, location, explanation, and a specific fix. If there "
"are no findings, say so.\n\n<git_diff>\n"
f"{diff}\n</git_diff>"
)
options = QoderAgentOptions(
auth=access_token_from_env(),
cwd=workspace,
model="auto",
tools=["Read", "Glob", "Grep"],
allowed_tools=["Read", "Glob", "Grep"],
max_turns=6,
)
completed = False
async for message in query(prompt=prompt, options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text, end="", flush=True)
elif isinstance(message, ResultMessage):
if message.subtype != "success":
raise RuntimeError("\n".join(message.errors or [message.subtype]))
completed = True
if not completed:
raise RuntimeError("The review ended without a success result.")
print()
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--workspace", type=Path, default=Path.cwd())
parser.add_argument("--base")
parser.add_argument("--head", default="HEAD")
return parser.parse_args()
def main() -> None:
args = parse_args()
workspace = args.workspace.resolve()
asyncio.run(review(workspace, read_diff(workspace, args.base, args.head)))
if __name__ == "__main__":
try:
main()
except (RuntimeError, ValueError, OSError, subprocess.CalledProcessError) as error:
raise SystemExit(str(error)) from error