-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
219 lines (173 loc) · 6.2 KB
/
cli.py
File metadata and controls
219 lines (173 loc) · 6.2 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
"""
Command-line interface for LLM-Bisect.
"""
import argparse
import logging
import sys
import json
from typing import Optional
from .config import Config, set_config, get_config
from .bisect import bisect_vulnerability, Bisector
from .analysis import evaluate_bisect_accuracy
from .utils.file_utils import save_json, load_json
def setup_logging(verbose: bool = False) -> None:
"""Configure logging based on verbosity level."""
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def cmd_bisect(args: argparse.Namespace) -> int:
"""Execute bisect command."""
logger = logging.getLogger(__name__)
if args.commit:
# Single commit mode
logger.info(f"Bisecting vulnerability fix: {args.commit}")
result = bisect_vulnerability(args.commit)
if result.success:
logger.info(f"Bug-inducing commit: {result.inducing_commit}")
print(f"Inducing commit: {result.inducing_commit}")
else:
logger.error(f"Bisect failed: {result.error}")
return 1
if args.output:
save_json({args.commit: result.inducing_commit}, args.output)
elif args.input:
# Batch mode from file
commits = load_json(args.input, [])
if isinstance(commits, dict):
commits = list(commits.keys())
logger.info(f"Processing {len(commits)} commits")
results = {}
for commit in commits:
logger.info(f"Processing {commit}")
result = bisect_vulnerability(commit)
results[commit] = result.inducing_commit
if result.success:
logger.info(f" -> {result.inducing_commit}")
else:
logger.warning(f" -> Failed: {result.error}")
if args.output:
save_json(results, args.output)
else:
print(json.dumps(results, indent=2))
else:
logger.error("Either --commit or --input must be specified")
return 1
return 0
def cmd_evaluate(args: argparse.Namespace) -> int:
"""Execute evaluation command."""
logger = logging.getLogger(__name__)
logger.info(f"Evaluating results from {args.results}")
result = evaluate_bisect_accuracy(args.results, args.ground_truth)
print(f"\nEvaluation Results:")
print(f" Total: {result.total}")
print(f" Correct: {result.correct}")
print(f" Incorrect: {result.incorrect}")
print(f" Accuracy: {result.accuracy:.2%}")
if result.true_positives > 0 or result.false_positives > 0:
print(f"\nVersion-based Metrics:")
print(f" Precision: {result.precision:.2%}")
print(f" Recall: {result.recall:.2%}")
print(f" F1 Score: {result.f1_score:.2%}")
if args.show_incorrect and result.incorrect_commits:
print(f"\nIncorrect commits:")
for commit in result.incorrect_commits:
print(f" - {commit}")
return 0
def cmd_analyze(args: argparse.Namespace) -> int:
"""Execute analysis command."""
logger = logging.getLogger(__name__)
from .analysis import extract_critical_lines
logger.info(f"Analyzing patch: {args.commit}")
lines = extract_critical_lines(args.commit, use_llm=not args.no_llm)
print(f"\nCritical lines in {args.commit}:")
for line in lines:
print(f" {line.filename}:{line.absolute_line} - {line.content[:60]}")
return 0
def create_parser() -> argparse.ArgumentParser:
"""Create the argument parser."""
parser = argparse.ArgumentParser(
prog="llm-bisect",
description="LLM-powered Bug-Inducing Commit Identification",
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Enable verbose output",
)
parser.add_argument(
"--kernel-path",
help="Path to Linux kernel repository",
)
parser.add_argument(
"--model",
default="o1",
help="LLM model to use (default: o1)",
)
subparsers = parser.add_subparsers(dest="command", help="Commands")
# Bisect command
bisect_parser = subparsers.add_parser("bisect", help="Find bug-inducing commit")
bisect_parser.add_argument(
"-c", "--commit",
help="Single patch commit to bisect",
)
bisect_parser.add_argument(
"-i", "--input",
help="Input JSON file with commits to process",
)
bisect_parser.add_argument(
"-o", "--output",
help="Output JSON file for results",
)
bisect_parser.set_defaults(func=cmd_bisect)
# Evaluate command
eval_parser = subparsers.add_parser("evaluate", help="Evaluate bisect results")
eval_parser.add_argument(
"results",
help="JSON file with bisect results",
)
eval_parser.add_argument(
"-g", "--ground-truth",
required=True,
help="JSON file with ground truth",
)
eval_parser.add_argument(
"--show-incorrect",
action="store_true",
help="Show list of incorrect commits",
)
eval_parser.set_defaults(func=cmd_evaluate)
# Analyze command
analyze_parser = subparsers.add_parser("analyze", help="Analyze a patch")
analyze_parser.add_argument(
"commit",
help="Patch commit to analyze",
)
analyze_parser.add_argument(
"--no-llm",
action="store_true",
help="Disable LLM-based analysis",
)
analyze_parser.set_defaults(func=cmd_analyze)
return parser
def main(argv: Optional[list] = None) -> int:
"""Main entry point."""
parser = create_parser()
args = parser.parse_args(argv)
setup_logging(args.verbose)
# Configure
config = get_config()
if args.kernel_path:
config.paths.kernel_repo = args.kernel_path
if args.model:
config.llm.model = args.model
config.debug = args.verbose
set_config(config)
if not args.command:
parser.print_help()
return 1
return args.func(args)
if __name__ == "__main__":
sys.exit(main())