feat(runtime): hybrid script reflex + Ink TUI + Pi-on-demand escalation (#4)

* fix(mindcraft-skills): hard timeout on every skill call

mc_avoid_enemies (and 7 other tools) wrapped only in safeCall without a
withTimeout. When mindcraft's underlying pathfinder/pvp goal couldn't be
satisfied, the call never resolved — the Pi tick loop blocked forever.
Observed live: mc_avoid_enemies pending >10 minutes after one mc_observe.

safeCall now takes timeoutMs (default 30s) and wraps withTimeout itself,
so every tool gets a hard ceiling. Per-tool overrides:
  - goToPosition / goToNearestBlock: 120s / 90s (unchanged from before)
  - defendSelf / avoidEnemies: 45s
  - stay: secs*1000 + 10s
  - craft / consume / pickup / place: 30s
  - equip: 15s
collectBlock still uses its bespoke per-iter 75s loop.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(runtime): script-driven reflex daemon + Ink TUI dashboard

Pure-Pi runtime had three failure modes in practice:
  - slow: 20-60s per decision because LLM was in the hot path
  - expensive: every tick (defend, eat, idle) paid for a reasoning pass
  - invisible: required tmux capture-pane to know what the bot was doing

New runtime/ layer is a long-running Node daemon that owns the MC
connection, ticks a priority-ordered reflex chain (defend > eat > sleep
> idle) with NO LLM in the hot path, and exposes status + commands over
a Unix-socket IPC. tui/ is an Ink dashboard that attaches over IPC and
can detach freely — multiple TUI clients can connect at once.

Pi/Codex are still available, but as on-demand escalation: TUI hotkey
'a' spawns `pi -p "<prompt>"` as a subprocess and streams stdout into
the dashboard. The self-improvement loop (proposals → operator approval
→ Pi-driven patch → hot reload) is documented in docs/runtime.md but
not yet wired.

Reflex bodies are stubs today — they log decisions but don't drive
Mineflayer actions yet. The priority chain, IPC contract, and TUI are
fully working; subsequent commits will fill in defend/eat/sleep bodies
and wire automatic escalation.

Run with `npm run bot` + `npm run tui`. Pi-only fallback stays at
`npm run agent`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit was merged in pull request #4.
This commit is contained in:
Yuriy Mayatnikov
2026-05-25 16:04:48 +03:00
committed by GitHub
co-authored by Claude Opus 4.7 mayatnikov
parent 989ddf335e
commit 1e3b36a9a1
16 changed files with 2464 additions and 75 deletions
+66
View File
@@ -0,0 +1,66 @@
// Thin client wrapper around the Unix-socket IPC server. Emits events as a
// regular EventEmitter so the React layer can subscribe.
import net from "node:net";
import { EventEmitter } from "node:events";
import { createLineParser, encodeFrame } from "../runtime/ipc-protocol.js";
import { socketPath } from "../runtime/config.js";
export function createIpcClient() {
const ee = new EventEmitter();
let socket = null;
let reconnectTimer = null;
let shuttingDown = false;
function connect() {
if (socket || shuttingDown) return;
socket = net.createConnection(socketPath);
const parser = createLineParser((obj) => {
if (obj.__parseError) {
ee.emit("error", new Error(`bad frame: ${obj.err}`));
return;
}
ee.emit("frame", obj);
if (obj.type) ee.emit(obj.type, obj.payload, obj.ts);
});
socket.setEncoding("utf8");
socket.on("connect", () => ee.emit("connected"));
socket.on("data", parser);
socket.on("close", () => {
socket = null;
ee.emit("disconnected");
if (!shuttingDown) scheduleReconnect();
});
socket.on("error", (err) => {
ee.emit("ipc-error", err);
});
}
function scheduleReconnect() {
if (reconnectTimer) return;
reconnectTimer = setTimeout(() => {
reconnectTimer = null;
connect();
}, 1500);
}
function send(type, payload) {
if (!socket || socket.destroyed) return false;
try {
socket.write(encodeFrame({ type, payload }));
return true;
} catch {
return false;
}
}
function close() {
shuttingDown = true;
if (reconnectTimer) clearTimeout(reconnectTimer);
socket?.destroy();
}
connect();
return Object.assign(ee, { send, close });
}
+290
View File
@@ -0,0 +1,290 @@
/**
* Ink-based TUI dashboard for the pepa runtime.
*
* Layout:
* ┌─────────── status (HP / food / pos / task / connection / paused) ──────────┐
* │ ┌───────── event log ─────────┐ ┌──────── MC chat ──────────┐ │
* │ │ │ │ │ │
* │ └─────────────────────────────┘ └──────────────────────────┘ │
* └─────────────────── command bar (hotkeys + chat input) ─────────────────────┘
*
* Hotkeys:
* p — pause / resume reflex loop
* s — stop bot (sends cmd:stop)
* r — request fresh snapshot
* c — enter chat mode (type, Enter to send to MC)
* a — enter ask-Pi mode (type, Enter to spawn pi -p)
* q — quit TUI (bot keeps running)
*/
import React, { useEffect, useReducer, useState } from "react";
import { render, Box, Text, useApp, useInput } from "ink";
import TextInput from "ink-text-input";
import { createIpcClient } from "./ipc-client.js";
import { COMMAND_TYPES, EVENT_TYPES } from "../runtime/ipc-protocol.js";
type LogEntry = { ts: string; level: string; source: string; text: string };
type ChatEntry = { ts: string; from: string; text: string; kind: string };
type Snapshot = Record<string, any>;
type State = {
connectedToBot: boolean;
snapshot: Snapshot;
logs: LogEntry[];
chat: ChatEntry[];
paused: boolean;
piStream: string;
piRunning: boolean;
};
type Action =
| { type: "ipc-connected" }
| { type: "ipc-disconnected" }
| { type: "snapshot"; payload: Snapshot }
| { type: "log"; payload: LogEntry }
| { type: "chat"; payload: { from: string; text: string; kind: string }; ts: string }
| { type: "death"; payload: any; ts: string }
| { type: "pi-chunk"; payload: { stream: string; text: string } }
| { type: "pi-done"; payload: { code: number; durationMs: number } }
| { type: "set-paused"; paused: boolean }
| { type: "hello"; payload: { snapshot: Snapshot; recentLogs: LogEntry[] } };
const MAX_LOGS = 200;
const MAX_CHAT = 100;
function reducer(state: State, action: Action): State {
switch (action.type) {
case "ipc-connected":
return { ...state, connectedToBot: true };
case "ipc-disconnected":
return { ...state, connectedToBot: false };
case "snapshot":
return { ...state, snapshot: action.payload || {} };
case "log":
return { ...state, logs: [...state.logs, action.payload].slice(-MAX_LOGS) };
case "chat":
return { ...state, chat: [...state.chat, { ts: action.ts, ...action.payload }].slice(-MAX_CHAT) };
case "death":
return {
...state,
logs: [
...state.logs,
{ ts: action.ts, level: "warn", source: "mc", text: `death at ${JSON.stringify(action.payload?.position ?? null)}` },
].slice(-MAX_LOGS),
};
case "pi-chunk":
return { ...state, piRunning: true, piStream: (state.piStream + action.payload.text).slice(-2000) };
case "pi-done":
return {
...state,
piRunning: false,
piStream: state.piStream + `\n[pi done code=${action.payload.code} after ${action.payload.durationMs}ms]\n`,
};
case "set-paused":
return { ...state, paused: action.paused };
case "hello":
return {
...state,
snapshot: action.payload.snapshot || {},
logs: action.payload.recentLogs || [],
};
default:
return state;
}
}
function StatusBar({ snapshot, paused, connectedToBot }: { snapshot: Snapshot; paused: boolean; connectedToBot: boolean }) {
const tone = snapshot.connected ? "green" : "red";
return (
<Box borderStyle="round" borderColor={tone} flexDirection="column" paddingX={1}>
<Text>
<Text color={tone} bold>
{snapshot.connected ? "● MC online" : "○ MC offline"}
</Text>
{" "}
<Text color={connectedToBot ? "green" : "red"}>{connectedToBot ? "IPC ok" : "IPC down"}</Text>
{" "}
{paused ? <Text color="yellow"> reflex paused</Text> : <Text color="green"> reflex live</Text>}
</Text>
<Text>
user={snapshot.username ?? "?"} hp={snapshot.health ?? "?"} food={snapshot.food ?? "?"}{" "}
pos=
{snapshot.position
? `${snapshot.position.x},${snapshot.position.y},${snapshot.position.z}`
: "?"}{" "}
day={String(snapshot.isDay ?? "?")} hostiles={snapshot.hostileCount ?? 0}
{snapshot.closestHostile ? ` closest=${snapshot.closestHostile.name}@${snapshot.closestHostile.distance}m` : ""}
</Text>
</Box>
);
}
function EventLog({ logs }: { logs: LogEntry[] }) {
const last = logs.slice(-14);
return (
<Box borderStyle="round" flexDirection="column" paddingX={1} width="60%">
<Text bold underline>
events
</Text>
{last.map((l, i) => (
<Text key={i} color={l.level === "warn" ? "yellow" : l.level === "error" ? "red" : "white"}>
{l.ts.slice(11, 19)} [{l.source}] {l.text}
</Text>
))}
</Box>
);
}
function ChatPanel({ chat }: { chat: ChatEntry[] }) {
const last = chat.slice(-14);
return (
<Box borderStyle="round" flexDirection="column" paddingX={1} width="40%">
<Text bold underline>
MC chat
</Text>
{last.map((c, i) => (
<Text key={i} color={c.kind === "system" ? "gray" : "cyan"}>
{c.ts?.slice(11, 19) ?? ""} {c.from}: {c.text}
</Text>
))}
</Box>
);
}
function PiPanel({ piStream, piRunning }: { piStream: string; piRunning: boolean }) {
if (!piStream && !piRunning) return null;
return (
<Box borderStyle="round" flexDirection="column" paddingX={1} borderColor={piRunning ? "magenta" : "gray"}>
<Text bold underline>
pi (escalation) {piRunning ? "● running" : "○ idle"}
</Text>
<Text>{piStream || "(no output yet)"}</Text>
</Box>
);
}
type Mode = "idle" | "chat" | "ask-pi";
function App() {
const { exit } = useApp();
const [state, dispatch] = useReducer(reducer, {
connectedToBot: false,
snapshot: {},
logs: [],
chat: [],
paused: false,
piStream: "",
piRunning: false,
});
const [client] = useState(() => createIpcClient());
const [mode, setMode] = useState<Mode>("idle");
const [inputValue, setInputValue] = useState("");
useEffect(() => {
const onConnected = () => dispatch({ type: "ipc-connected" });
const onDisconnected = () => dispatch({ type: "ipc-disconnected" });
const onFrame = (frame: any) => {
switch (frame.type) {
case EVENT_TYPES.STATUS:
dispatch({ type: "snapshot", payload: frame.payload });
break;
case EVENT_TYPES.LOG:
dispatch({ type: "log", payload: frame.payload });
break;
case EVENT_TYPES.CHAT:
dispatch({ type: "chat", payload: frame.payload, ts: frame.ts });
break;
case EVENT_TYPES.DEATH:
dispatch({ type: "death", payload: frame.payload, ts: frame.ts });
break;
case EVENT_TYPES.HELLO:
dispatch({ type: "hello", payload: frame.payload });
break;
case EVENT_TYPES.ASK_PI_CHUNK:
dispatch({ type: "pi-chunk", payload: frame.payload });
break;
case EVENT_TYPES.ASK_PI_DONE:
dispatch({ type: "pi-done", payload: frame.payload });
break;
}
};
(client as any).on("connected", onConnected);
(client as any).on("disconnected", onDisconnected);
(client as any).on("frame", onFrame);
return () => {
(client as any).off("connected", onConnected);
(client as any).off("disconnected", onDisconnected);
(client as any).off("frame", onFrame);
client.close();
};
}, [client]);
useInput((input, key) => {
if (mode !== "idle") return; // text input has its own handling
if (input === "q") {
client.close();
exit();
return;
}
if (input === "p") {
const next = !state.paused;
client.send(next ? COMMAND_TYPES.PAUSE : COMMAND_TYPES.RESUME, {});
dispatch({ type: "set-paused", paused: next });
}
if (input === "s") {
client.send(COMMAND_TYPES.STOP, {});
}
if (input === "r") {
client.send(COMMAND_TYPES.SNAPSHOT, {});
}
if (input === "c") setMode("chat");
if (input === "a") setMode("ask-pi");
});
function submit(value: string) {
const text = value.trim();
setInputValue("");
const m = mode;
setMode("idle");
if (!text) return;
if (m === "chat") client.send(COMMAND_TYPES.CHAT, { text });
else if (m === "ask-pi") client.send(COMMAND_TYPES.ASK_PI, { prompt: text });
}
const hotkeyHint =
mode === "idle"
? "[p]ause/resume [s]top [r]efresh [c]hat [a]sk-pi [q]uit"
: mode === "chat"
? "chat → MC (Enter to send, Esc to cancel)"
: "ask-pi → spawn pi -p (Enter to send)";
return (
<Box flexDirection="column">
<StatusBar snapshot={state.snapshot} paused={state.paused} connectedToBot={state.connectedToBot} />
<Box flexDirection="row">
<EventLog logs={state.logs} />
<ChatPanel chat={state.chat} />
</Box>
<PiPanel piStream={state.piStream} piRunning={state.piRunning} />
<Box borderStyle="single" paddingX={1}>
{mode === "idle" ? (
<Text dimColor>{hotkeyHint}</Text>
) : (
<>
<Text bold color={mode === "chat" ? "cyan" : "magenta"}>
{mode === "chat" ? "chat> " : "pi> "}
</Text>
<TextInput
value={inputValue}
onChange={setInputValue}
onSubmit={submit}
/>
</>
)}
</Box>
</Box>
);
}
render(<App />);