An AI that authors mathematics questions it can prove correct — then learns to teach them.
A decoupled system for Moodle STACK: cloud AI proposes, a real computer-algebra engine verifies, a live Socratic tutor coaches, and a reinforcement-learning agent sequences what to teach next.
New to STACK? It's the open-source engine behind maths quizzes in Moodle, used by universities worldwide — it grades real algebra and calculus (an entered expression like 2*x+1), not multiple-choice. Authoring those questions correctly is hard and error-prone; this project automates it.
The core idea
Language models hallucinate. So the AI is allowed to propose only a source expression — never the answer or the grading. A deterministic template then makes a real CAS compute the teacher's answer, and STACK's own engine proves the question grades correctly across many random variants. Wrong guesses simply fail validation and are retried.
The model supplies a source expression + wording — nothing more.
An expression grammar blocks anything unsafe before it touches the CAS.
The question itself computes ta1 : diff(expr,x).
Full marks across random seeds, or it's rejected.
Importable into any Moodle running STACK — nothing forked.
The arc
Each phase stands alone yet feeds the next — and "intelligence" always lives in an external service Moodle calls, never a fork of Moodle itself.
Generate STACK questions with cloud AI, validate every one against a standalone STACK API (real Maxima on AWS), export Moodle XML. 8 question types; a 14-question validated seed bank.
● Deployed liveA student answers; STACK grades it live; on a wrong answer an AI gives an escalating Socratic hint — never the answer. Shipped both as a web tutor and a real Moodle 4.5 plugin (server-side key, hints logged).
● Deployed liveA reinforcement-learning policy decides which question to set next (skill × difficulty) to reach mastery fastest — discovering a curriculum from reward alone. Served as an external recommend_next() API.
✓ Built & validatedPhase 3 spotlight
Trained against a Bayesian-Knowledge-Tracing student simulator, a tabular Q-learner finds an easy → medium → hard progression and brings every student to all-skills-mastered in roughly half the questions of the best baseline — and the only baseline that finishes at all.
Offline RL from messy logs
Fitted-Q Iteration with pessimism learns a winning policy from a frozen log of sub-optimal play — exactly how real Moodle data will plug in.
Deep Knowledge Tracing
A from-scratch numpy GRU (hand-written, gradient-checked back-prop) recovers ~98% of the BKT oracle's predictive accuracy from raw answer streams.
Learns prerequisites
With a prerequisite graph switched on, the agent front-loads foundational skills before dependents — and beats a prerequisite-blind policy.
Inside Moodle
It all runs as standard Moodle plugins — install, switch on, go. Moodle is never forked; the plugins simply call the external "intelligence" and emit standard questions.
Generates oracle-validated STACK questions straight into a course's question bank: pick a type and difficulty, or let the RL policy build a full curriculum-ordered quiz in one click.
Adds a fact-checked hint button to STACK questions in any quiz: hints are CAS-grounded, so they're accurate and never leak the answer — and can run entirely in the student's browser, with no API key at all.
Under the hood, three specialised "brains" each do one job — which is what keeps it trustworthy and easy to improve.
A real CAS (Maxima) decides what's correct. It never guesses — and the AI is never allowed to.
A language model drafts the questions and phrases the hints — suggestion and wording only.
The RL policy decides what each student should practise next, from their inferred mastery.
What's next
The system works in simulation. The next step is evidence with real students: does AI-sequenced practice actually help people learn more — and does it last?
A within-student design on a live university maths module: each student gets AI-sequenced practice on some topics and standard practice on others, then a short topic-tagged test measures the difference — so every student is their own fair comparison.
Voluntary, informed consent; participation never affects grades; data are pseudonymised and GDPR-compliant. It goes through the University Research Ethics Committee — nothing starts until it is approved.
Real, de-identified practice data refit the student model and retrain the teaching policy — so the recommender improves with use, with a human review gate before any update goes live.
Why it's hard
The interesting problems weren't the models — they were trust, safety, and getting the incentives exactly right.
STACK can score a self-matching test as "passed" even when a question threw a hidden runtime error. A dedicated checker inspects every variant's messages, so silently-broken questions are rejected — not shipped.
A naive design let a "spam easy questions" agent beat real teaching by riding lucky guesses. Mastery was redesigned to rise only from difficulty-matched learning — so adaptive difficulty became the only path to progress.
AI keys never reach the browser; the tutor and policy are external services behind bearer-token auth + locked-down CORS. Moodle is never forked — it only emits and calls.
The DKT network's hand-written BPTT is verified against finite-difference gradients. Offline RL uses pessimism (support constraints + lower-confidence bounds) to survive distribution shift. 96 tests gate it all.
A Baum-Welch EM fitter recovers student parameters from logged answers (validated by parameter recovery), so the simulator can be replaced by real learners the moment enough data exists.
Static site, no build step; the entire RL stack is pure numpy with no framework. Readable end-to-end — every result reproducible from one command.
Built with