File size: 3,797 Bytes
2db0a88
 
 
 
 
 
 
a8a9661
d360475
2db0a88
d360475
 
 
 
2db0a88
 
d360475
 
2db0a88
 
d360475
 
 
2db0a88
 
 
 
 
a8a9661
d360475
2db0a88
d360475
 
 
 
 
 
2db0a88
 
d360475
 
2db0a88
 
d360475
 
 
2db0a88
 
 
 
 
a8a9661
d360475
2db0a88
d360475
 
 
 
 
 
 
 
 
 
 
2db0a88
 
d360475
2db0a88
 
 
d360475
 
 
 
7b9f847
2db0a88
 
 
 
 
 
 
147b2e1
 
 
2db0a88
 
 
 
 
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
from app.models import TaskDefinition, TaskType


TASKS: dict[str, TaskDefinition] = {
    "easy": TaskDefinition(
        id="easy",
        task_type=TaskType.EASY,
        has_grader=True,
        title="Detect SQL Injection Vulnerability",
        code=(
            "def authenticate_user(username, password, db_cursor):\n"
            "    query = f\"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'\"\n"
            "    db_cursor.execute(query)\n"
            "    return db_cursor.fetchone()\n"
        ),
        instructions=(
            "Identify the critical security vulnerability in this authentication code and propose a secure fix. "
            "Mention the exact vulnerability name and standard mitigation technique."
        ),
        criteria=[
            ["sql injection", "sqli", "injection attack"],
            ["parameterized query", "prepared statement", "bind variables", "pass as parameters"],
            ["f-string", "string formatting", "interpolation"],
        ],
    ),
    "medium": TaskDefinition(
        id="medium",
        task_type=TaskType.MEDIUM,
        has_grader=True,
        title="Identify Async Race Condition",
        code=(
            "import asyncio\n\n"
            "async def update_balance(user_id, amount, db_client):\n"
            "    balance = await db_client.get_balance(user_id)\n"
            "    new_balance = balance + amount\n"
            "    await asyncio.sleep(0.05)  # Simulate latency\n"
            "    await db_client.set_balance(user_id, new_balance)\n"
        ),
        instructions=(
            "Review this asynchronous money transfer logic. Identify the concurrency bug, "
            "explain why it occurs, and suggest a robust solution."
        ),
        criteria=[
            ["race condition", "data race", "concurrency issue"],
            ["lock", "mutex", "transaction", "atomic"],
            [".sleep", "await", "interleaving"],
        ],
    ),
    "hard": TaskDefinition(
        id="hard",
        task_type=TaskType.HARD,
        has_grader=True,
        title="Comprehensive FastAPI Endpoint Review",
        code=(
            "from fastapi import FastAPI\n"
            "import requests\n"
            "import json\n\n"
            "app = FastAPI()\n\n"
            "@app.post('/webhook')\n"
            "def handle_webhook(payload: dict):\n"
            "    if not payload.get('user_id'): return {'error': 'missing id'}\n"
            "    res = requests.post('http://internal-log.local/audit', json=payload)\n"
            "    with open('audit.log', 'a') as f:\n"
            "        f.write(json.dumps(res.json()) + '\\n')\n"
            "    return {'status': 'processed'}\n"
        ),
        instructions=(
            "Perform a comprehensive code review covering performance, architecture, resilience, and correctness. "
            "Include an explicit overall score in the review."
        ),
        criteria=[
            ["blocking", "synchronous call", "requests.post", "async def", "httpx"],
            ["file i/o", "blocking i/o", "open('audit.log", "aiofiles"],
            ["error handling", "exception", "res.json() fails", "status code", "try/except"],
            ["hardcoded", "config", "environment variable", "http://internal-log.local"],
            ["overall score", "score", "rating", "final score", "overall rating"],
        ],
    ),
}


ORDERED_TASK_IDS: list[str] = ["easy", "medium", "hard"]

# Explicit curriculum ordering for multi-task episodes and static validators.
ORDERED_TASK_DEFINITIONS: list[TaskDefinition] = [TASKS[tid] for tid in ORDERED_TASK_IDS]


def get_task(task_id: str) -> TaskDefinition:
    if task_id not in TASKS:
        raise KeyError(f"Unknown task_id: {task_id}")
    return TASKS[task_id]