Buckets:
| #!/usr/bin/env python3 | |
| """ | |
| Canvas Homework Grader Task Evaluation | |
| Evaluates whether the agent correctly: | |
| 1. Checked emails for homework submissions | |
| 2. Downloaded and executed Python files | |
| 3. Graded assignments on Canvas based on code execution results | |
| 4. Handled multiple submissions (used latest) | |
| 5. Applied correct grading: 10 points (no errors) or 0 points (has errors) | |
| """ | |
| from argparse import ArgumentParser | |
| import sys | |
| import csv | |
| import json | |
| from typing import Tuple, Dict | |
| from pathlib import Path | |
| import os | |
| # Add utils directory to path to import Canvas API | |
| current_file = Path(__file__).absolute() | |
| # From evaluation/main.py -> canvas-homework-grader-python -> finalpool -> tasks -> toolathlon | |
| toolathlon_root = current_file.parent.parent.parent.parent.parent | |
| utils_dir = toolathlon_root / "utils" | |
| sys.path.insert(0, str(utils_dir)) | |
| from app_specific.canvas import CanvasAPI | |
| from utils.evaluation.retry import grade_with_retry | |
| class HomeworkGraderEvaluator: | |
| """Evaluates Canvas homework grader task completion""" | |
| def __init__(self, canvas_url: str = None, teacher_token: str = None): | |
| """Initialize evaluator with Canvas API using teacher token""" | |
| if canvas_url is None: | |
| canvas_url = "http://localhost:10001" | |
| if teacher_token is None: | |
| # Load teacher token using the same approach as preprocessing | |
| teacher_token = self.load_canvas_token() | |
| self.canvas = CanvasAPI(canvas_url, teacher_token) | |
| self.course_name = "CS5123 Programming Fundamentals" | |
| self.assignment_name = "homework2" # Exact name from Canvas | |
| self.course_id = None | |
| self.assignment_id = None | |
| def load_canvas_token(self) -> str: | |
| """Load Canvas token using the same approach as preprocessing script""" | |
| try: | |
| # Try to load from token_key_session.py like preprocessing does | |
| task_dir = Path(__file__).parent.parent | |
| sys.path.insert(0, str(task_dir)) | |
| from token_key_session import all_token_key_session | |
| return all_token_key_session.admin_canvas_api_token | |
| except ImportError: | |
| # Fallback to loading from teacher CSV | |
| return self.load_teacher_token_from_csv() | |
| def load_teacher_token_from_csv(self) -> str: | |
| """Load teacher Canvas token from teacher_list.csv as fallback""" | |
| teacher_csv = Path(__file__).parent.parent / "preprocess" / "teacher_list.csv" | |
| try: | |
| with open(teacher_csv, 'r', encoding='utf-8') as f: | |
| reader = csv.DictReader(f) | |
| for row in reader: | |
| return row.get('canvas_token', '').strip() | |
| except Exception as e: | |
| print(f"โ Error loading teacher token: {e}") | |
| return "canvas_token_TT1021#WQtww" # fallback | |
| def load_expected_grades(self) -> Dict[str, Dict]: | |
| """Load expected grades from emails.jsonl (only homework2 submissions)""" | |
| emails_file = Path(__file__).parent.parent / "files" / "origin_from_implementor" / "emails.jsonl" | |
| expected_grades = {} | |
| try: | |
| with open(emails_file, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| line = line.strip() | |
| if not line: | |
| continue | |
| email_data = json.loads(line) | |
| # Only process homework2 submissions (skip homework1) | |
| homework_assignment = email_data.get('homework_assignment', 'homework2') | |
| if homework_assignment != 'homework2': | |
| continue | |
| sender_name = email_data['sender_name'] | |
| student_id = email_data['student_id'] | |
| expected_grade = email_data['expected_grade'] | |
| error_type = email_data.get('error_type', 'none') | |
| is_latest = email_data.get('is_latest_submission', False) | |
| # For students with multiple submissions, only use the latest | |
| if sender_name not in expected_grades or is_latest: | |
| expected_grades[sender_name] = { | |
| 'student_id': student_id, | |
| 'expected_grade': expected_grade, | |
| 'error_type': error_type, | |
| 'is_correct': email_data['is_correct'], | |
| 'note': email_data.get('note', ''), | |
| 'homework_assignment': homework_assignment | |
| } | |
| print(f"๐ Loaded expected grades for {len(expected_grades)} students (homework2 only)") | |
| return expected_grades | |
| except Exception as e: | |
| print(f"โ Error loading expected grades: {e}") | |
| return {} | |
| def find_course_and_assignment(self) -> bool: | |
| """Find the CS5123 course and Homework 2 assignment using same logic as preprocessing""" | |
| print(f"๐ Finding CS5123 course (flexible search)...") | |
| # Get all courses including deleted ones (same as preprocessing) | |
| courses = self.canvas.list_courses(include_deleted=True, account_id=1) | |
| if not courses: | |
| print("โ No courses found") | |
| return False | |
| # Find the course using same flexible logic as preprocessing | |
| found_course = None | |
| for course in courses: | |
| course_name = course.get('name', '') | |
| course_code = course.get('course_code', '') | |
| # Check if it's a CS5123 course (same criteria as preprocessing) | |
| is_cs5123 = (course_code == 'CS5123') | |
| # Also check if it's taught by Teresa Torres (like preprocessing does) | |
| is_teresa_course = False | |
| try: | |
| enrollments = self.canvas.get_course_enrollments(course['id']) | |
| for enrollment in enrollments: | |
| if (enrollment.get('type') == 'TeacherEnrollment' and | |
| enrollment.get('user', {}).get('name') == 'Teresa Torres'): | |
| is_teresa_course = True | |
| break | |
| except: | |
| pass # Skip enrollment check if it fails | |
| # Accept the course if it's CS5123 OR taught by Teresa Torres | |
| if is_cs5123 or is_teresa_course: | |
| found_course = course | |
| if is_teresa_course: | |
| print(f" ๐ Found course taught by Teresa Torres: {course_name}") | |
| break | |
| if not found_course: | |
| print(f"โ CS5123 course not found") | |
| print(f"Available courses: {[c.get('name') for c in courses[:5]]}") # Show first 5 courses | |
| return False | |
| self.course_id = found_course['id'] | |
| actual_course_name = found_course.get('name', 'CS5123') | |
| print(f"โ Found course: {actual_course_name} (ID: {self.course_id})") | |
| # Optional: Verify teacher enrollment (like preprocessing checks) | |
| try: | |
| enrollments = self.canvas.get_course_enrollments(self.course_id) | |
| teacher_found = False | |
| for enrollment in enrollments: | |
| if enrollment.get('type') == 'TeacherEnrollment': | |
| teacher_name = enrollment.get('user', {}).get('name', 'Unknown') | |
| print(f" ๐จโ๐ซ Teacher: {teacher_name}") | |
| teacher_found = True | |
| break | |
| if not teacher_found: | |
| print(" โ ๏ธ No teacher enrollment found") | |
| except Exception as e: | |
| print(f" โ ๏ธ Could not check teacher enrollment: {e}") | |
| # Find Homework 2 assignment | |
| print(f"๐ Finding '{self.assignment_name}' assignment...") | |
| assignments = self.canvas.list_assignments(self.course_id) | |
| if not assignments: | |
| print("โ No assignments found") | |
| return False | |
| for assignment in assignments: | |
| assignment_name = assignment.get('name', '') | |
| # Try exact match first, then substring match | |
| if (assignment_name.lower() == self.assignment_name.lower() or | |
| self.assignment_name.lower() in assignment_name.lower()): | |
| self.assignment_id = assignment['id'] | |
| points_possible = assignment.get('points_possible', 0) | |
| print(f"โ Found assignment: {assignment_name} (ID: {self.assignment_id}, Points: {points_possible})") | |
| # Verify it's worth 10 points | |
| if points_possible != 10: | |
| print(f"โ ๏ธ Assignment worth {points_possible} points, expected 10") | |
| return True | |
| print(f"โ Assignment '{self.assignment_name}' not found") | |
| print(f"Available assignments: {[a.get('name') for a in assignments]}") | |
| return False | |
| def get_student_grades(self) -> Dict[str, float]: | |
| """Get all student grades for Homework 2 using course enrollments and direct API calls""" | |
| print(f"๐ Fetching grades for assignment {self.assignment_id}...") | |
| # Get course enrollments to find all students | |
| enrollments = self.canvas.get_course_enrollments(self.course_id) | |
| if not enrollments: | |
| print("โ No enrollments found") | |
| return {} | |
| student_grades = {} | |
| # For each student enrollment, try to get their grade for this assignment | |
| for enrollment in enrollments: | |
| if enrollment.get('type') != 'StudentEnrollment': | |
| continue | |
| user = enrollment.get('user', {}) | |
| user_id = user.get('id') | |
| student_name = user.get('name', 'Unknown') | |
| if not user_id: | |
| continue | |
| # Try to get the student's submission for this assignment using direct API call | |
| try: | |
| endpoint = f"courses/{self.course_id}/assignments/{self.assignment_id}/submissions/{user_id}" | |
| submission = self.canvas._make_request('GET', endpoint) | |
| if submission and submission.get('workflow_state') == 'graded': | |
| score = submission.get('score') | |
| if score is not None: | |
| student_grades[student_name] = float(score) | |
| print(f" {student_name}: {score} points (graded)") | |
| else: | |
| # Score is None but marked as graded - treat as 0 | |
| student_grades[student_name] = 0.0 | |
| print(f" {student_name}: 0 points (graded but no score)") | |
| else: | |
| # Not graded yet - treat as 0 points | |
| student_grades[student_name] = 0.0 | |
| print(f" {student_name}: 0 points (not graded)") | |
| except Exception as e: | |
| # Error getting grade - treat as 0 points | |
| student_grades[student_name] = 0.0 | |
| print(f" {student_name}: 0 points (error: {e})") | |
| print(f"โ Found grades for {len(student_grades)} students") | |
| return student_grades | |
| def evaluate_grading_accuracy(self, expected_grades: Dict[str, Dict], actual_grades: Dict[str, float]) -> Tuple[bool, str]: | |
| """Compare expected vs actual grades""" | |
| print("\n๐ Grading Analysis:") | |
| print("=" * 60) | |
| total_students = len(expected_grades) | |
| correct_grades = 0 | |
| errors = [] | |
| # Only evaluate students who are in the expected grades (from emails.jsonl) | |
| for student_name, expected_data in expected_grades.items(): | |
| expected_grade = expected_data['expected_grade'] | |
| error_type = expected_data['error_type'] | |
| note = expected_data['note'] | |
| if student_name not in actual_grades: | |
| errors.append(f"{student_name}: Not graded (missing)") | |
| print(f"โ {student_name}: Not graded (expected: {expected_grade})") | |
| continue | |
| actual_grade = actual_grades[student_name] | |
| if actual_grade == expected_grade: | |
| correct_grades += 1 | |
| status = "โ " | |
| else: | |
| status = "โ" | |
| errors.append(f"{student_name}: Got {actual_grade}, expected {expected_grade}") | |
| print(f"{status} {student_name}: {actual_grade}/{expected_grade} points ({error_type}) - {note}") | |
| # Show extra students but don't count them as errors (they're not part of the task) | |
| extra_students = set(actual_grades.keys()) - set(expected_grades.keys()) | |
| if extra_students: | |
| print(f"\n๐ Additional students in course (not part of homework task):") | |
| for student in extra_students: | |
| print(f" โน๏ธ {student}: {actual_grades[student]} points (not evaluated)") | |
| print(f"\n๐ Grading Summary:") | |
| print(f" Target students: {total_students} (from emails.jsonl)") | |
| print(f" Correct grades: {correct_grades}/{total_students}") | |
| print(f" Accuracy: {correct_grades/total_students*100:.1f}%") | |
| if errors: | |
| print(f"โ Grading errors found:") | |
| for error in errors: | |
| print(f" - {error}") | |
| return False, f"Grading accuracy: {correct_grades}/{total_students} ({len(errors)} errors)" | |
| else: | |
| return True, f"All {total_students} target students graded correctly" | |
| def verify_grading_logic(self, expected_grades: Dict[str, Dict], actual_grades: Dict[str, float]) -> Tuple[bool, str]: | |
| """Verify the grading logic follows the 10/0 rule""" | |
| print("\n๐งฎ Grading Logic Verification:") | |
| logic_errors = [] | |
| for student_name, actual_grade in actual_grades.items(): | |
| if student_name not in expected_grades: | |
| continue | |
| expected_data = expected_grades[student_name] | |
| is_correct = expected_data['is_correct'] | |
| error_type = expected_data['error_type'] | |
| # Check if grading follows the 10/0 rule | |
| if is_correct and actual_grade != 10: | |
| logic_errors.append(f"{student_name}: Correct code but got {actual_grade} (should be 10)") | |
| elif not is_correct and actual_grade != 0: | |
| logic_errors.append(f"{student_name}: Code with {error_type} but got {actual_grade} (should be 0)") | |
| # Also check that only 10 or 0 are used (no partial credit) | |
| if actual_grade not in [0, 10]: | |
| logic_errors.append(f"{student_name}: Invalid grade {actual_grade} (only 0 or 10 allowed)") | |
| if logic_errors: | |
| print("โ Grading logic errors:") | |
| for error in logic_errors: | |
| print(f" - {error}") | |
| return False, f"Grading logic errors: {len(logic_errors)} violations" | |
| else: | |
| print("โ All grades follow 10/0 rule correctly") | |
| return True, "Grading logic is correct" | |
| def check_latest_submission_handling(self, actual_grades: Dict[str, float]) -> Tuple[bool, str]: | |
| """Verify that for students with multiple submissions, the latest was used""" | |
| print("\n๐ Multiple Submission Handling:") | |
| # Timothy Ruiz has 2 submissions - check if latest was used | |
| timothy_grade = actual_grades.get("Timothy Ruiz") | |
| if timothy_grade is None: | |
| return False, "Timothy Ruiz not graded (should have latest submission graded)" | |
| # Latest submission should be correct (10 points) | |
| if timothy_grade == 10: | |
| print("โ Timothy Ruiz: Latest submission used correctly (10 points)") | |
| return True, "Multiple submissions handled correctly" | |
| else: | |
| print(f"โ Timothy Ruiz: Got {timothy_grade} points, but latest submission should be 10") | |
| return False, f"Timothy Ruiz: Wrong submission used (got {timothy_grade}, latest should be 10)" | |
| def evaluate_task_completion(self) -> Tuple[bool, str]: | |
| """Main evaluation function""" | |
| print("๐ฏ Starting Canvas Homework Grader Task Evaluation") | |
| print("=" * 70) | |
| # Step 1: Find course and assignment | |
| if not self.find_course_and_assignment(): | |
| return False, "Failed to find course or assignment" | |
| # Step 2: Load expected grades from emails.jsonl | |
| expected_grades = self.load_expected_grades() | |
| if not expected_grades: | |
| return False, "Failed to load expected grades" | |
| # Step 3: Get actual grades from Canvas | |
| actual_grades = self.get_student_grades() | |
| if not actual_grades: | |
| return False, "No grades found in Canvas" | |
| # Step 4: Evaluate grading accuracy | |
| accuracy_success, accuracy_message = self.evaluate_grading_accuracy(expected_grades, actual_grades) | |
| # Step 5: Verify grading logic (10/0 rule) | |
| logic_success, logic_message = self.verify_grading_logic(expected_grades, actual_grades) | |
| # Step 6: Check latest submission handling | |
| submission_success, submission_message = self.check_latest_submission_handling(actual_grades) | |
| # Final evaluation | |
| all_checks_passed = accuracy_success and logic_success and submission_success | |
| print("\n๐ Final Evaluation Summary:") | |
| print(f"โ Course found: {self.course_name} (ID: {self.course_id})") | |
| print(f"โ Assignment found: {self.assignment_name} (ID: {self.assignment_id})") | |
| print(f"{'โ ' if accuracy_success else 'โ'} Grading accuracy: {accuracy_message}") | |
| print(f"{'โ ' if logic_success else 'โ'} Grading logic: {logic_message}") | |
| print(f"{'โ ' if submission_success else 'โ'} Submission handling: {submission_message}") | |
| print(f"๐ Task focus: Agent should only grade homework2 (not homework1 submissions)") | |
| if all_checks_passed: | |
| homework2_student_count = len(expected_grades) | |
| return True, f"Task completed successfully - All {homework2_student_count} homework2 students graded correctly" | |
| else: | |
| failed_checks = [] | |
| if not accuracy_success: | |
| failed_checks.append("accuracy") | |
| if not logic_success: | |
| failed_checks.append("logic") | |
| if not submission_success: | |
| failed_checks.append("submissions") | |
| return False, f"Task failed - Issues with: {', '.join(failed_checks)}" | |
| def main(): | |
| """Main evaluation function""" | |
| parser = ArgumentParser(description="Evaluate Canvas Homework Grader Task") | |
| parser.add_argument("--agent_workspace", required=True, | |
| help="Agent workspace directory") | |
| parser.add_argument("--groundtruth_workspace", required=False, | |
| help="Groundtruth workspace (not used in this evaluation)") | |
| parser.add_argument("--res_log_file", required=False, | |
| help="Result log file path") | |
| parser.add_argument("--canvas_url", default=None, | |
| help="Canvas base URL (defaults to localhost:10001)") | |
| parser.add_argument("--teacher_token", default=None, | |
| help="Teacher Canvas API token") | |
| parser.add_argument("--launch_time", nargs='*', required=False, help="Launch time (can contain spaces)") | |
| args = parser.parse_args() | |
| try: | |
| # Initialize evaluator | |
| evaluator = HomeworkGraderEvaluator( | |
| canvas_url=args.canvas_url, | |
| teacher_token=args.teacher_token | |
| ) | |
| # Run evaluation (Layer-2 wrap to absorb Canvas propagation lag) | |
| success, message = grade_with_retry(lambda: evaluator.evaluate_task_completion()) | |
| # Output results | |
| if success: | |
| print(f"\n๐ EVALUATION PASSED: {message}") | |
| result_code = 0 | |
| else: | |
| print(f"\nโ EVALUATION FAILED: {message}") | |
| result_code = 1 | |
| # Write to log file if specified | |
| if args.res_log_file: | |
| result_data = { | |
| "success": success, | |
| "message": message, | |
| "timestamp": __import__('datetime').datetime.now().isoformat() | |
| } | |
| try: | |
| # Write evaluation results to a separate file, not the trajectory file | |
| eval_temp_file = os.path.join(os.path.dirname(args.res_log_file) if args.res_log_file else ".", "eval_temp.json") | |
| with open(eval_temp_file, 'w') as f: | |
| json.dump(result_data, f, indent=2) | |
| print(f"๐ Results logged to: {eval_temp_file}") | |
| except Exception as e: | |
| print(f"โ ๏ธ Failed to write log file: {e}") | |
| sys.exit(result_code) | |
| except Exception as e: | |
| print(f"โ Evaluation error: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
Xet Storage Details
- Size:
- 21.6 kB
- Xet hash:
- fb5a6b0c298094f7fa8461eb2f4c8be98f84862c4a26d4ea617d6cad57387c6e
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.