Files
pepa-pi-bot/runtime/config.js
T
e84148d189 v0.2.0-rc.2: P0 hardening — Pi headless, test state isolation, advice fixes (#21)
P0 (correctness):

1. PEPA_HEADLESS=1 guard in extensions/mineflayer-bridge.ts. When `pi -p`
   spawns a subprocess (banter, coach, planner, reflect, auto-patch), the
   bridge no longer attempts a second MC connect — the hybrid runtime
   already owns the nickname. runtime/pi-bridge.js sets the env var on
   every spawn. Root cause of the "two pepa_bot's racing for the slot"
   bug seen in reply-pi stderr.

2. Test state isolation in runtime/config.js. When running under the node
   test runner (detected via execArgv/argv) — or when PEPA_STATE_DIR is
   set — stateDir redirects to /tmp/pepa-test-state-<pid>/. log.js,
   scenario-memory, world-journal, and knowledge.db all follow.
   `npm test` no longer pollutes live scenarios.jsonl, world-journal.jsonl,
   or daily log files. Verified empirically: post-fix run added 0 test
   rows to the live scenarios file. Cleaned ~550 historical test rows
   from live state in the same change.

3. defendReflex outcome reporting (runtime/reflex.js). Previously a
   creeper-rule override marked the lesson succeeded=false BEFORE the
   flee skill returned. Now dispatchDefendFlee accepts {lessonId} and
   the onComplete fires reportAdviceOutcome with the actual flee result.

4. Mode-name → skill-id translation in runtime/coach/advice.js. Pi-coach
   occasionally returns prefer_skill values that are mode names
   ("night_shelter", "self_preservation", "hunger"). normalisePreferSkill
   maps these to SAFE_OVERRIDES entries before dispatch. Also handles
   "tunnel-out", "survive_flee", "survive flee" shapes.

New behavior:

5. Self-reflection loop (runtime/coach/reflect.js). Every 30 min, the
   bot asks Pi: "Are you making progress, or stuck in a loop? What
   should you do differently?" Pi answers with a verdict
   (progress/loop/recovering/idle/emergency), summary, next-action, and
   0-N new lessons. The reflection is written to
   state/<host>/reflections/<ts>.md and lessons land in the DB with
   source="pi-reflect". Rate-limited to 2 calls/hour. Wired through
   bot.js with the existing askPi + lastSnapshot accessor.

Tests: 246/246 green (+9 new: 6 advice mode-name + 4 reflect).

Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 14:10:28 +03:00

101 lines
4.0 KiB
JavaScript

import { config as loadDotenv } from "dotenv";
import path from "node:path";
import os from "node:os";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const REPO_ROOT = path.resolve(__dirname, "..");
loadDotenv({ path: path.join(REPO_ROOT, ".env") });
// v0.2.0-rc.2: detect that we're running under the node test runner so the
// log / scenario-memory / world-journal / knowledge modules redirect their
// writes to a tmp dir instead of the live state/<host>/ directory. Without
// this guard, every `npm test` poisons live scenarios.jsonl, world-journal,
// and the daily log file — and the learning loop can pick test rows up as
// real experience.
function detectTestContext() {
if (process.env.PEPA_STATE_DIR) return process.env.PEPA_STATE_DIR;
const execArgv = process.execArgv || [];
const argv = process.argv || [];
const isNodeTest = execArgv.includes("--test")
|| argv.includes("--test")
|| argv.some((a) => typeof a === "string" && /\.test\.[mc]?[jt]sx?$/.test(a));
if (isNodeTest) {
return path.join(os.tmpdir(), `pepa-test-state-${process.pid}`);
}
return null;
}
const TEST_STATE_DIR = detectTestContext();
function req(name) {
const v = process.env[name]?.trim();
if (!v) throw new Error(`Missing required env var: ${name}`);
return v;
}
function opt(name, fallback = "") {
return process.env[name]?.trim() || fallback;
}
function optInt(name, fallback) {
const raw = Number.parseInt(opt(name, String(fallback)), 10);
return Number.isFinite(raw) ? raw : fallback;
}
const host = req("MC_HOST");
const port = Number.parseInt(opt("MC_PORT", "25565"), 10);
const username = req("MC_USERNAME");
const learningMode = opt("PEPA_LEARNING_MODE", "normal").toLowerCase();
const fastLearning = learningMode === "fast" || learningMode === "dev";
// MC_VERSION: "auto" (or empty) lets mineflayer auto-detect from the server
// handshake — the right default per the survival-bot PRD (no hard-coded modern
// version unless the server requires pinning). Mineflayer accepts `false` to
// auto-detect, so we translate "auto" into false at the boundary; the original
// string is preserved for logging.
const rawVersion = opt("MC_VERSION", "auto");
const mineflayerVersion = rawVersion.toLowerCase() === "auto" ? false : rawVersion;
export const config = Object.freeze({
host,
port,
username,
version: rawVersion,
mineflayerVersion,
authMode: opt("MC_AUTH_MODE", "offline"),
authmePassword: opt("MC_AUTHME_PASSWORD", ""),
operators: opt("OPERATOR_USERNAMES", "")
.split(",")
.map((s) => s.trim().toLowerCase())
.filter(Boolean),
tickIntervalMs: Math.max(1, Number.parseInt(opt("TICK_INTERVAL_SECONDS", "3"), 10)) * 1000,
chatRateLimitPerMin: Number.parseInt(opt("CHAT_RATE_LIMIT_PER_MIN", "15"), 10),
learningMode,
stuckThresholdMs: Math.max(15, optInt("PEPA_STUCK_THRESHOLD_SECONDS", fastLearning ? 60 : 300)) * 1000,
stuckCooldownMs: Math.max(60, optInt("PEPA_STUCK_COOLDOWN_SECONDS", fastLearning ? 600 : 1800)) * 1000,
autoImproveCooldownMs: Math.max(60, optInt("PEPA_AUTO_IMPROVE_COOLDOWN_SECONDS", fastLearning ? 300 : 900)) * 1000,
autoImproveMaxPerHour: Math.max(1, optInt("PEPA_AUTO_IMPROVE_MAX_PER_HOUR", fastLearning ? 8 : 4)),
// Optional prismarine-viewer port for local visual debugging. 0/empty = off.
viewerPort: (() => {
const v = Number.parseInt(opt("VIEWER_PORT", "0"), 10);
return Number.isFinite(v) && v > 0 ? v : 0;
})(),
});
export const serverKey = `${host}_${port}`;
// Tests get an isolated tmp dir so they don't pollute live scenarios/journal/log.
// Override via PEPA_STATE_DIR if you need a custom location.
export const stateDir = TEST_STATE_DIR
? path.resolve(TEST_STATE_DIR)
: path.join(REPO_ROOT, "state", serverKey);
export const isTestStateDir = !!TEST_STATE_DIR;
export const socketPath = path.join(stateDir, "bot.sock");
// Redacted env view for logs — never include the AuthMe password.
export function redactedConfig() {
const { authmePassword, ...rest } = config;
return { ...rest, authmePassword: authmePassword ? "***" : "(unset)" };
}