File size: 7,936 Bytes
c5292d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Main evaluation script for running all checks."""
import asyncio
import json
import shutil
from datetime import datetime
from pathlib import Path

from instructor.checks.dynamic_checks import DynamicChecker
from instructor.checks.llm_checks import LLMChecker
from instructor.checks.static_checks import StaticChecker
from instructor.database import Database
from shared.config import settings
from shared.logger import setup_logger

logger = setup_logger(__name__)


class Evaluator:
    """Main evaluator for running all checks on submissions."""

    def __init__(self) -> None:
        """Initialize evaluator."""
        self.db = Database()
        self.llm_checker = LLMChecker()
        self.temp_dir = Path("./temp_evaluations")
        self.temp_dir.mkdir(parents=True, exist_ok=True)

    def get_pending_repos(self) -> list[dict]:
        """Get repos that need evaluation.

        Returns:
            List of repo dictionaries
        """
        all_repos = self.db.get_repos()
        pending = []

        for repo in all_repos:
            repo_dict = repo.to_dict()

            # Check if already evaluated
            existing_results = self.db.get_results(
                email=repo_dict["email"], task=repo_dict["task"]
            )

            # Filter to this round
            round_results = [
                r for r in existing_results if r.to_dict()["round"] == repo_dict["round"]
            ]

            if not round_results:
                pending.append(repo_dict)

        logger.info(f"Found {len(pending)} pending repos for evaluation")
        return pending

    async def evaluate_repo(self, repo: dict) -> None:
        """Evaluate a single repository.

        Args:
            repo: Repository submission data
        """
        logger.info(f"Evaluating {repo['email']}/{repo['task']}, round {repo['round']}")

        # Get task details
        session = self.db.get_session()
        try:
            task = (
                session.query(self.db.Task)
                .filter_by(
                    email=repo["email"],
                    task=repo["task"],
                    round=repo["round"],
                )
                .first()
            )

            if not task:
                logger.error(f"No task found for {repo['task']}")
                return

            task_dict = task.to_dict()
            checks_list = task_dict["checks"]

        finally:
            session.close()

        # Clone repo
        clone_dir = self.temp_dir / f"{repo['task']}_{repo['round']}"
        static_checker = StaticChecker(repo["repo_url"], repo["commit_sha"], clone_dir)

        try:
            static_checker.clone_repo()

            # Run static checks
            logger.info("Running static checks...")
            static_results = await self._run_static_checks(
                static_checker, task_dict["timestamp"]
            )

            # Run LLM checks
            logger.info("Running LLM checks...")
            llm_results = self._run_llm_checks(clone_dir)

            # Run dynamic checks
            logger.info("Running dynamic checks...")
            dynamic_results = await self._run_dynamic_checks(repo["pages_url"], checks_list)

            # Save all results
            all_results = static_results + llm_results + dynamic_results
            for result in all_results:
                self.db.add_result(
                    {
                        "timestamp": datetime.utcnow(),
                        "email": repo["email"],
                        "task": repo["task"],
                        "round": repo["round"],
                        "repo_url": repo["repo_url"],
                        "commit_sha": repo["commit_sha"],
                        "pages_url": repo["pages_url"],
                        "check": result["check"],
                        "score": result["score"],
                        "reason": result["reason"],
                        "logs": result.get("logs", ""),
                    }
                )

            logger.info(
                f"Completed evaluation for {repo['email']}/{repo['task']}: "
                f"{len(all_results)} checks"
            )

        except Exception as e:
            logger.error(f"Error evaluating {repo['task']}: {e}", exc_info=True)

            # Save error result
            self.db.add_result(
                {
                    "timestamp": datetime.utcnow(),
                    "email": repo["email"],
                    "task": repo["task"],
                    "round": repo["round"],
                    "repo_url": repo["repo_url"],
                    "commit_sha": repo["commit_sha"],
                    "pages_url": repo["pages_url"],
                    "check": "Evaluation",
                    "score": 0.0,
                    "reason": f"Evaluation failed: {e}",
                    "logs": str(e),
                }
            )

        finally:
            # Cleanup clone
            if clone_dir.exists():
                shutil.rmtree(clone_dir)

    async def _run_static_checks(
        self, checker: StaticChecker, task_timestamp: str
    ) -> list[dict]:
        """Run static checks.

        Args:
            checker: Static checker instance
            task_timestamp: When task was sent

        Returns:
            List of check results
        """
        results = []

        # Parse timestamp
        if isinstance(task_timestamp, str):
            task_time = datetime.fromisoformat(task_timestamp.replace("Z", "+00:00"))
        else:
            task_time = task_timestamp

        # Repo creation time
        result = checker.check_created_after_task(task_time)
        results.append({"check": "Repo created after task", **result})

        # MIT License
        result = checker.check_mit_license()
        results.append({"check": "MIT LICENSE exists", **result})

        # README exists and basic quality
        result = checker.check_readme()
        results.append({"check": "README.md basic quality", **result})

        # No secrets
        result = checker.check_no_secrets()
        results.append({"check": "No secrets in code", **result})

        return results

    def _run_llm_checks(self, code_dir: Path) -> list[dict]:
        """Run LLM-based checks.

        Args:
            code_dir: Directory containing code

        Returns:
            List of check results
        """
        results = []

        # README quality
        readme_path = code_dir / "README.md"
        if readme_path.exists():
            result = self.llm_checker.check_readme_quality(readme_path)
            results.append({"check": "LLM: README.md quality", **result})

        # Code quality
        result = self.llm_checker.check_code_quality(code_dir)
        results.append({"check": "LLM: Code quality", **result})

        return results

    async def _run_dynamic_checks(
        self, pages_url: str, checks: list[str]
    ) -> list[dict]:
        """Run dynamic Playwright checks.

        Args:
            pages_url: GitHub Pages URL
            checks: List of checks to run

        Returns:
            List of check results
        """
        dynamic_checker = DynamicChecker(pages_url, checks)
        results = await dynamic_checker.run_checks()
        return results

    async def run(self) -> None:
        """Run evaluation on all pending repos."""
        logger.info("Starting evaluation process")

        pending_repos = self.get_pending_repos()

        if not pending_repos:
            logger.info("No pending repos to evaluate")
            return

        # Evaluate each repo
        for repo in pending_repos:
            await self.evaluate_repo(repo)

        logger.info("Evaluation process complete")


async def main():
    """Main entry point."""
    evaluator = Evaluator()
    await evaluator.run()


if __name__ == "__main__":
    asyncio.run(main())