๐จ TerraGuard โ Building an AI-Powered Disaster Simulation System That Learns From You

Most AI projects today stop at one thing:
generate a response โ show it โ done
But real-world decision-making doesn't work like that.
In disasters, decisions evolve. They cascade. They have consequences. And most importantly โ they repeat patterns.
So we asked a simple question:
What if AI didn't just respondโฆ but simulated reality and learned from you over time?
That's how TerraGuard was built.
๐ง The Idea
TerraGuard is an AI-powered disaster simulation system where users make decisions under pressure โ and the environment reacts in real time.
But unlike typical simulations:
it's not scripted
it doesn't reset every session
it doesn't forget your mistakes
It evolves with you.
โ๏ธ How TerraGuard Works
At its core, TerraGuard is a state-driven simulation engine powered by AI.
The loop is simple: AI generates a disaster scenario โ User makes a decision โ System evaluates impact โ Scenario evolves โ Repeat for multiple rounds
But the execution is where things get interesting.
๐ Real-Time AI Simulation (Not Just Chat)
Instead of waiting for full responses, we implemented streaming AI responses.
AI sends output incrementally
Frontend renders it using a typewriter effect
Users experience a live, unfolding situation
This creates:
Lower perceived latency
Higher immersion
Real-time urgency
๐ฎ Three Modes, Three Realities
We didn't want a one-dimensional experience.
TerraGuard introduces three distinct modes:
๐ค Citizen Mode
Personal survival decisions. Small-scale impact. Emotion-driven scenarios.
๐ Coordinator Mode
Resource management โ ambulances, rescue teams. Multiple simultaneous emergencies. Trade-offs and prioritization.
๐ Official Mode
Large-scale decision-making. Public communication. Budget and inter-agency coordination.
Each mode changes the scale, complexity, and consequences of every decision you make.
๐งฑ System Architecture
Frontend
React (Vite + TypeScript)
Tailwind CSS
Context + useReducer for state management
The frontend isn't just a UI โ it's a real-time simulation controller.
Backend
Node.js + Express
Roles: secure API proxy, streaming handler (SSE), memory integration layer
AI Layer
Google Gemini (primary)
Groq (fallback)
Key decision: enforce structured JSON output to maintain consistency across rounds.
โก Streaming with SSE (Server-Sent Events)
One of the hardest parts was implementing streaming correctly.
Instead of: request โ wait โ response
We built: request โ stream โ render progressively
Challenges we solved:
Handling partial data mid-stream
Reconstructing valid JSON from chunks
Syncing UI updates without race conditions
Result: A real-time, responsive simulation experience that feels alive.
๐ง The Biggest Upgrade: Memory with Hindsight
Most AI apps are stateless. You close the tab โ everything is gone.
That's where TerraGuard becomes fundamentally different.
We integrated a memory layer using Hindsight, built around three phases:
1. Retain (During Simulation)
Stores decisions, outcomes, and context as the session progresses.
2. Recall (Before Each Round)
Fetches past behavior and repeated mistakes, then injects them into the AI prompt โ so the scenario adapts to you.
3. Reflect (After Simulation)
Generates behavioral insights and improvement suggestions based on your full history.
The result?
The system can now detect patterns, adapt scenarios, and personalize feedback.
"You tend to delay evacuation decisions โ this has cost lives in previous sessions."
That's not a feature. That's behavior modeling.
๐ After-Action Report
At the end of each simulation, TerraGuard generates a detailed report:
Lives saved vs. lost
Performance score and grade
Round-by-round analysis
Critical mistakes
Best decisions
Real-world insights
With memory enabled:
Behavioral profile across sessions
Pattern detection
Improvement recommendations across time
โ ๏ธ Challenges We Faced
1. JSON Reliability
AI doesn't always produce clean, parseable output under streaming conditions.
Solution: Enforce structured output format + add a parsing fallback layer.
2. Streaming Complexity
Handling partial responses without breaking the UI required careful buffering and state management.
3. State Synchronization
Keeping frontend state aligned with AI output across multiple rounds โ especially when AI behavior was non-deterministic.
4. Making Memory Meaningful
The hard part wasn't storing logs โ it was injecting the right context at the right moment to actually influence AI behavior.
๐ What We Actually Built
Not a chatbot. Not a game. Not a demo project.
A real-time AI simulation engine with memory-driven behavior adaptation.
๐ Tech Stack
| Layer | Tech |
|---|---|
| Frontend | React (Vite + TypeScript), Tailwind CSS |
| Backend | Node.js + Express |
| AI | Google Gemini + Groq (fallback) |
| Memory | Hindsight |
| Streaming | Server-Sent Events (SSE) |
๐ก Final Thought
AI shouldn't just answer questions.
It should simulate environments, challenge decisions, and help you improve over time.
TerraGuard started as a hackathon idea. It turned into something deeper โ a system that doesn't just respond. It reacts, evolves, and remembers.
That shift โ from stateless chatbot to stateful simulation engine โ is where I think the next wave of genuinely useful AI applications lives.
Built at a hackathon. Open to feedback, collaboration, and questions in the comments.

