Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

AI-Powered Code Grading & Doubt Resolution Portal

Full-stack LMS module: students submit code that is graded against test cases in a Docker sandbox and receive AI code-quality feedback; students post doubts to a shared board where an AI drafts an answer that a teacher must review before other students can see it.

Architecture

Next.js (apps/web) ──► NestJS API (apps/api) ──► PostgreSQL (Prisma)
                              │                      ▲
                              ├── pg-boss queue ─────┘  (jobs stored in Postgres)
                              │        │
                              │        ▼
                              │   Grading worker ──► Docker sandbox (one container per run)
                              │
                              └── Groq LLM via Vercel AI SDK (Zod-validated structured output)
  • apps/api — NestJS modules: auth, problems, submissions, grading (queue + sandbox + worker), ai, doubts (board + answers state machine + teacher review)
  • apps/web — Next.js App Router: login, problem list, Monaco editor with live grading results, submission history, doubt board, teacher review queue

Running locally

Prerequisites: Node 20+, PostgreSQL, Docker (Docker Desktop or colima).

npm install

# database
createdb grading_portal
cp apps/api/.env.example apps/api/.env   # set DATABASE_URL, JWT_SECRET, GROQ_API_KEY
cd apps/api && npx prisma migrate dev && npm run seed && cd ../..

# sandbox images
docker pull node:22-alpine && docker pull python:3.12-alpine

npm run dev:api   # http://localhost:4000
npm run dev:web   # http://localhost:3000

Seeded logins (password password123): [email protected], [email protected], [email protected].

Tests: npm test (state machine unit tests). scripts/e2e-grading.sh runs sandbox safety checks (timeout kill, network denial, fs probe) against a running API.

Deploying

Single Docker host (any VPS). The API container mounts the host's Docker socket to spawn sandbox containers as siblings — this is why a plain PaaS (Vercel/Render) can't run the grading engine.

cp .env.deploy.example .env.deploy    # set secrets + PUBLIC_API_URL=http://<server-ip>:4000/api
docker compose -f docker-compose.prod.yml --env-file .env.deploy up -d --build
docker pull node:22-alpine && docker pull python:3.12-alpine   # sandbox images

Web on :3000, API on :4000. Migrations + seed run automatically on API boot.

Security design

Sandbox execution

Every test-case run gets a throwaway container:

docker run --rm -i --network none --memory 128m --cpus 0.5 --pids-limit 64 \
  --read-only --cap-drop ALL --security-opt no-new-privileges --user 65534:65534 \
  -v <code-dir>:/sandbox:ro <image> <runtime> /sandbox/main.<ext>

No network namespace, hard memory/CPU/process limits, read-only rootfs, all capabilities dropped, non-root user, code mounted read-only. A 5s wall-clock timer issues docker kill; stdout/stderr are capped at 64KB before touching the database. Grading runs in a pg-boss worker, so slow or hostile code never blocks the API.

Prompt injection resilience (layered)

  1. Structural (the real defense): AI-drafted doubt answers are born DRAFT, promoted to PENDING_REVIEW, and are invisible to students until a teacher approves them. A successful injection still has to convince a human reviewer.
  2. Student code and questions are wrapped in delimiters and the system prompt declares them untrusted data, never instructions.
  3. The model has no tools and no database access — it can only return text.
  4. Output is forced through generateObject with a Zod schema (typed fields, length caps); free-form injected output cannot escape the schema.
  5. Students only ever see APPROVED answers — the filter is applied in the database query, not the UI.

Approval state machine

DRAFT → PENDING_REVIEW → APPROVED | REJECTED (terminal). Enforced twice:

  • a transition map rejects illegal moves before any query runs;
  • the UPDATE is conditional on the expected current status (updateMany where status = from), so a concurrent or stale transition matches zero rows and returns 409 Conflict instead of silently overwriting — the database, not the application's memory, is the source of truth.

Teacher-only endpoints are guarded by a role guard on the JWT claims. Approve accepts an optional edited body, so the teacher can fix a draft in place.

Choices & tradeoffs

  • pg-boss over Redis/BullMQ — one grading queue doesn't justify a second datastore; pg-boss gives persistent jobs and retries on top of the Postgres already present.
  • Vercel AI SDK over LangChain/LangGraph — both AI flows are single, linear LLM calls with schema-validated output; a graph orchestrator adds surface without adding safety. The human-review workflow lives in the database as a state machine, where it belongs.
  • No Mem0 — no cross-session personalization requirement; all state is domain data in Postgres.
  • docker run via child_process over dockerode — the security flags are visible in one place and auditable; no extra dependency.
  • JWT in localStorage + client-side pages — adequate for an assessment demo; production would use httpOnly cookies and server components.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages