## Summary
This PR upgrades the Minecraft bot's cognitive stack so it perceives
danger
accurately, reacts below the LLM brain with fast reflexes, and recovers
from
common brain failure modes. All changes are confined to
`services/minecraft/**`.
The desktop-side integration (spoken in-game chat, the online-gated
relay tool,
master binding in the UI) is submitted separately to keep this PR
focused; the
two are coupled only at runtime via AIRI context lanes, not at compile
time.
## What's included
**Perception — accurate damage attribution**
- `fall-tracker`: classify fall damage from physics-tick vertical
velocity.
- `attacker-tracker`: record the *real* attacker from the `entityHurt`
source,
fixing "a mob shot me but I chased the player standing next to me".
- `low-health`: emit a danger signal at health ≤ 6 when no ready food is
held.
- `damage-taken`: resolve the source via a lava → drown → fire → fall →
attacker chain and surface the attacker's name to the brain.
**Reflex — autonomic behaviors (run on the FSM tick, below the brain)**
- `defend`: auto-engage a hostile mob that attacks the bot/master and
hold the
fight instead of thrashing (suppress damage wakes while attacking).
- `escape-hazard`: climb out of lava, or surface when drowning.
- `auto-eat`: eat ready food at health ≤ 6 (skipped during combat).
- A `reflexEngaged` autonomy flag suppresses auto-follow and auto-eat
while a
defend/escape reflex owns the body.
**Conscious / skills — master recognition and reliability**
- Bind the "主人" role to the owner's in-game username so the bot
recognizes its
master in-world; enforce master-only command authority, never attack the
master, and react to a light hit with a short protest. Resolve players
by
username, never the literal "player"/"主人".
- `js-planner`: strip prose-mixed LLM replies that caused `X is not
defined` and
give-up spirals; add a `botCall` sandbox bridge and the `whereName`
query
chain method.
- Brain-prompt discipline: *saying is not doing*, *`recipePlan` is recon
not
crafting*, *eat only when food < 18* (regen is time-based), *defer equip
until
a queued craft lands*.
- Skills: collect drops that spawn shortly after a kill (no more 0
loot);
`goToBed` skips occupied beds; detach auto-follow while
mining/collecting so
digging is not interrupted.
- Platform: load perception rules on Windows (`fileURLToPath`); widen
the AIRI
client read timeout to stop connection flapping.
The master username is injected via `generateBrainSystemPrompt(..., {
masterUsername })`
from config — it is **not** hardcoded.
## How tested
```bash
pnpm -F @proj-airi/minecraft-bot exec vitest run # 220 tests; +56 new, all green
pnpm -F @proj-airi/minecraft-bot typecheck
pnpm exec moeru-lint services/minecraft # 0 problems on changed files
```
- Each feature ships with unit tests (trackers, reflex behaviors,
planner
salvage, skills, prompt, context service).
- Manual in-game testing: combat vs. pillagers/zombies, lava/water
escape,
low-health eating, crafting, master recognition with multiple players
present.
### Pre-existing baseline (not introduced by this PR)
Measured on `main` (`cb5c0783e`) before any change here:
- `tsc` reports 12 errors, all from a `vec3` 0.1.x/0.2.0
dependency-dedup
mismatch (`angleTo`) and `unknown[]` casts in `map-renderer.ts` /
`mcdata.ts` —
files this PR does not touch. This PR adds **0** new type errors.
- 7 unit tests already fail on `main` (4 in `map-renderer.test.ts`, 2
timing-sensitive `brain.test.ts`, 1 timing-sensitive `rules.test.ts`).
This
PR leaves those exactly as-is and does not touch those modules.
## Known limitations / follow-ups
- The `defend` reflex does not yet reliably engage ranged pillagers at
distance;
the brain's `attack` tool currently covers that case.
- `craftRecipe` does not auto-unpack blocks (e.g. diamond_block →
diamond) or
auto-gather missing intermediates for complex recipes.
- `inferDamageSource` orders environmental causes before
`recentAttacker`, which
can mis-attribute an in-water-while-shot hit; low impact, deferred.
AIRI Minecraft Service
This workspace runs AIRI's dedicated Minecraft bot. It connects a Mineflayer runtime to a Minecraft server, loads the cognitive stack in src/cognitive, and bridges status, context, and command traffic back to AIRI so the Stage settings shell can observe the service.
Deprecation Notice
This service is on a deprecation path. The current Mineflayer-based bot is expected to be replaced by a Fabric mod based runtime, which will become the primary Minecraft integration surface going forward.
Use this service for current local development and maintenance, but avoid building new long-term features around the Mineflayer runtime unless they are also part of the migration plan.
Safety Notice
Do not connect this bot to public servers you do not trust.
The runtime can execute JavaScript-generated action plans to control the bot. Those scripts run in an isolated environment, but they still drive a real local process with access to your Minecraft session, local network reachability, and other machine-side resources. A malicious or hostile server can still cause unwanted actions, or damage to your system.
Treat this service as a local-development and trusted-server tool only.
Setup
-
Install workspace dependencies from the repo root:
pnpm i -
Copy the template:
cp services/minecraft/.env services/minecraft/.env.local -
Edit
services/minecraft/.env.local. -
Start the service:
pnpm -F @proj-airi/minecraft-bot devOr, from
services/minecraft/:pnpm dev -
The bot should automatically connect to both AIRI and the Minecraft server.
Cognitive Architecture
AIRI's Minecraft agent is built on a four-layered cognitive architecture inspired by cognitive science, enabling reactive, conscious, and physically grounded behaviors.
Architecture Overview
graph TB
subgraph "Layer A: Perception"
Events[Raw Events]
EM[Event Manager]
Events --> EM
end
subgraph "Layer B: Reflex (Subconscious)"
RM[Reflex Manager]
FSM[State Machine]
RM --> FSM
end
subgraph "Layer C: Conscious (Reasoning)"
ORC[Orchestrator]
Planner[Planning Agent (LLM)]
Chat[Chat Agent (LLM)]
ORC --> Planner
ORC --> Chat
end
subgraph "Layer D: Action (Execution)"
TE[Task Executor]
AA[Action Agent]
Planner -->|Plan| TE
TE -->|Action Steps| AA
end
EM -->|High Priority| RM
EM -->|All Events| ORC
RM -.->|Inhibition Signal| ORC
ORC -->|Execution Request| TE
style EM fill:#e1f5ff
style RM fill:#fff4e1
style ORC fill:#ffe1f5
style TE fill:#dcedc8
Layer A: Perception
Location: src/cognitive/perception/
The perception layer acts as the sensory input hub, collecting raw Mineflayer signals and translating them into typed events/signals through an event registry + rule engine pipeline.
Pipeline:
- Event definitions in
events/definitions/*bind Mineflayer events to normalized raw events. EventRegistryemitsraw:<modality>:<kind>events to the cognitive event bus.RuleEngineevaluates YAML rules and emits derivedsignal:*events consumed by Reflex/Conscious layers.
Key files:
events/index.tsevents/definitions/*rules/engine.tsrules/*.yamlpipeline.ts
Layer B: Reflex
Location: src/cognitive/reflex/
The reflex layer handles immediate, instinctive reactions. It operates on a finite state machine (FSM) pattern for predictable, fast responses.
Components:
- Reflex Manager (
reflex-manager.ts): Coordinates reflex behaviors - Inhibition: Reflexes can inhibit Conscious layer processing to prevent redundant responses.
Layer C: Conscious
Location: src/cognitive/conscious/
The conscious layer handles complex reasoning, planning, and high-level decision-making. No physical execution happens here anymore.
Components:
- Brain (
brain.ts): Event queue orchestration, LLM turn lifecycle, safety/budget guards, debug REPL integration. - JavaScript Planner (
js-planner.ts): Sandboxed planning/runtime execution against exposed tools/globals. - Query Runtime (
query-dsl.ts): Read-only world/inventory/entity query helpers for planner scripts. - Task State (
task-state.ts): Cancellation token and task lifecycle primitives used by action execution.
Layer D: Action
Location: src/cognitive/action/
The action layer is responsible for the actual execution of tasks in the world. It isolates "Doing" from "Thinking".
Components:
- Task Executor (
task-executor.ts): Runs normalized action instructions and emits action lifecycle events. - Action Registry (
action-registry.ts): Validates params and dispatches tool calls. - Tool Catalog (
llm-actions.ts): Action/tool definitions and schemas bound to mineflayer skills.
Event Flow Example
Scenario: "Build a house"
Player: "build a house"
↓
[Perception] Event detected
↓
[Conscious] Architect plans the structure
↓
[Action] Executor takes the plan and manages the construction loop:
- Step 1: Collect wood (calls ActionRegistry tool)
- Step 2: Craft planks
- Step 3: Build walls
↓
[Conscious] Brain confirms completion: "House is ready!"
Project Structure
src/
├── airi/ # AIRI bridge, module shell, status publishing
├── cognitive/ # 🧠 Perception → Reflex → Conscious → Action
│ ├── perception/ # Event definitions + rule evaluation
│ │ ├── events/
│ │ │ ├── index.ts
│ │ │ └── definitions/*
│ │ ├── rules/
│ │ │ ├── *.yaml
│ │ │ ├── engine.ts
│ │ │ ├── loader.ts
│ │ │ └── matcher.ts
│ │ └── pipeline.ts
│ ├── reflex/ # Fast, rule-based reactions
│ │ ├── reflex-manager.ts
│ │ ├── runtime.ts
│ │ ├── context.ts
│ │ └── behaviors/idle-gaze.ts
│ ├── conscious/ # LLM-powered reasoning
│ │ ├── brain.ts # Core reasoning loop/orchestration
│ │ ├── js-planner.ts # JS planning sandbox
│ │ ├── query-dsl.ts # Read-only query runtime
│ │ ├── llm-log.ts # Turn/log query helpers
│ │ ├── task-state.ts # Task lifecycle enums/helpers
│ │ └── prompts/ # Prompt definitions (e.g., brain-prompt.ts)
│ ├── action/ # Task execution layer
│ │ ├── task-executor.ts # Executes actions and emits lifecycle events
│ │ ├── action-registry.ts # Tool dispatch + schema validation
│ │ ├── llm-actions.ts # Tool catalog
│ │ └── types.ts
│ ├── event-bus.ts # Event bus core
│ ├── container.ts # Dependency injection wiring
│ ├── index.ts # Cognitive system entrypoint
│ └── types.ts # Shared cognitive types
├── composables/
│ ├── config.ts # Environment schema + defaults
│ ├── runtime-config.ts # Persisted local runtime config
│ └── bot.ts
├── debug/ # Debug dashboard, MCP REPL, viewer integration
├── libs/
│ └── mineflayer/ # Mineflayer bot wrapper/adapters
├── skills/ # Atomic bot capabilities
├── plugins/ # Mineflayer/bot plugins
├── utils/ # Helpers
├── minecraft-bot-runtime.ts # Bot lifecycle wrapper for reconnect/reconfigure
└── main.ts # Bot entrypoint
Design Principles
- Separation of Concerns: Each layer has a distinct responsibility
- Event-Driven: Loose coupling via centralized event system
- Inhibition Control: Reflexes prevent unnecessary LLM calls
- Extensibility: Easy to add new reflexes or conscious behaviors
- Cognitive Realism: Mimics human-like perception → reaction → deliberation
Future Enhancements
-
Perception Layer:
- ⏱️ Temporal context window (remember recent events)
- 🎯 Salience detection (filter noise, prioritize important events)
-
Reflex Layer:
- 🏃 Dodge hostile mobs
- 🛡️ Emergency combat responses
-
Conscious Layer:
- 💭 Emotional state management
- 🧠 Long-term memory integration
- 🎭 Personality-driven responses
🛠️ Development
Commands
pnpm dev- Start the bot in development modepnpm lint- Run ESLintpnpm typecheck- Run TypeScript type checkingpnpm test- Run tests
🙏 Acknowledgements
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.