feat(runtime): fully autonomous self-healing — no operator approval (#10)
Operator feedback: "бот должен быть полностью автономным — сам себя улучшать и чинить, в этом и есть смысл; все что я вижу пока что он стоит на месте и кидает proposals на каждый чих — это кардинально не то что я хочу". Acted on: 1. Trigger filter — proposals only on real bugs. runtime/bot.js classifies failure detail into bug / timeout / feature-gap / other. The 5-in-a-row trigger fires only when the run contains a bug (TypeError / Cannot read / is not defined …) OR is entirely timeouts on the same operation. Feature gaps like "no reachable log within 32 blocks", "no food in inventory", "no bed in range", "no target in reach" are SKIPPED — the reflex layer routes around them (noTreesUntil → wander, etc). The LLM has no business patching code for missing inventory. Threshold raised 3 → 5 in a row. Cooldown unchanged (30 min). 2. Auto-apply, no operator-in-the-loop. New runtime/auto-improve.js polls proposals/ every 2s. When it sees a new .md and 10s have passed since first sighting (debounce), spawns scripts/auto-patch.js detached. New scripts/auto-patch.js: refuses on dirty tree, moves proposal pending → approved/, branches `auto/<slug>` off main, runs `pi -p` with 10-min timeout. If Pi committed AND every changed file is under runtime/ → cherry-picks onto main. Otherwise discards the branch. No push, no PR. Audit trail in state/<host>/proposals/approved/. Rate limit: 15-min cooldown between finished runs + 4/hour hard cap. 3. Auto-rollback on bad patches. runtime/supervisor.js: when MAX_RESTARTS_PER_MINUTE is exceeded AND `git log -1 HEAD` is younger than 15 min AND HEAD touched runtime/, runs `git reset --hard HEAD~1`. Up to MAX_ROLLBACKS=3 lifetime, then exits 1 for manual investigation. Restart counters are reset after a successful rollback so the next attempt isn't immediately killed. 4. current-task.json slim. No longer stores the full perception snapshot (was ~3 KB per write × every action). Position only — sufficient as a resume anchor. Slim snapshot still goes into the proposal markdown for context. docs/runtime.md — rewrote the self-improvement section: full flow diagram, classification rules, all rate-limit knobs, manual escape hatches kept but documented as rarely-needed. Also cleared 5 stale proposals from previous smoke tests so the first production run isn't burning Pi tokens on stale bugs that have since been fixed. 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 #10.
This commit is contained in:
+52
-1
@@ -11,7 +11,7 @@
|
||||
// Run via `npm run bot`. Falls back to plain `node runtime/bot.js` via
|
||||
// `npm run bot:bare` if you want to skip the supervisor.
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -21,11 +21,17 @@ import { stateDir } from "./config.js";
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const RUNTIME_DIR = __dirname;
|
||||
const REPO_ROOT = path.resolve(RUNTIME_DIR, "..");
|
||||
const BOT_ENTRY = path.join(RUNTIME_DIR, "bot.js");
|
||||
|
||||
export const RELOAD_EXIT_CODE = 42;
|
||||
const WATCH_DEBOUNCE_MS = 800;
|
||||
const MAX_RESTARTS_PER_MINUTE = 5;
|
||||
// When a recent auto-patch breaks the bot, we roll it back. "Recent" =
|
||||
// landed on main within the last ROLLBACK_FRESHNESS_MS. The signal is
|
||||
// MAX_RESTARTS_PER_MINUTE exceeded — i.e. the patch reliably crashes.
|
||||
const ROLLBACK_FRESHNESS_MS = 15 * 60_000;
|
||||
const MAX_ROLLBACKS = 3; // hard ceiling per supervisor lifetime
|
||||
|
||||
// Pidfile prevents two supervisors from racing on the same MC nickname. The
|
||||
// Minecraft server refuses the second login ("Игрок с данным никнеймом уже
|
||||
@@ -37,6 +43,37 @@ const PID_FILE = path.join(stateDir, "supervisor.pid");
|
||||
let child = null;
|
||||
let restartingDueToWatch = false;
|
||||
const restartTimestamps = [];
|
||||
let rollbackCount = 0;
|
||||
|
||||
function lastCommitAgeMs() {
|
||||
const res = spawnSync("git", ["log", "-1", "--format=%ct", "HEAD"], { cwd: REPO_ROOT, encoding: "utf8" });
|
||||
if (res.status !== 0) return Number.POSITIVE_INFINITY;
|
||||
const epochSec = Number.parseInt(res.stdout.trim(), 10);
|
||||
if (!Number.isFinite(epochSec)) return Number.POSITIVE_INFINITY;
|
||||
return Date.now() - epochSec * 1000;
|
||||
}
|
||||
|
||||
function lastCommitTouchedRuntime() {
|
||||
const res = spawnSync("git", ["diff", "--name-only", "HEAD~1..HEAD"], { cwd: REPO_ROOT, encoding: "utf8" });
|
||||
if (res.status !== 0) return false;
|
||||
return res.stdout.split("\n").some((f) => f.startsWith("runtime/"));
|
||||
}
|
||||
|
||||
function rollbackLastCommit() {
|
||||
const sha = spawnSync("git", ["rev-parse", "HEAD"], { cwd: REPO_ROOT, encoding: "utf8" }).stdout.trim();
|
||||
console.log(`[supervisor] rolling back HEAD (${sha.slice(0, 8)})`);
|
||||
const reset = spawnSync("git", ["reset", "--hard", "HEAD~1"], {
|
||||
cwd: REPO_ROOT,
|
||||
encoding: "utf8",
|
||||
stdio: "inherit",
|
||||
});
|
||||
if (reset.status !== 0) {
|
||||
console.error(`[supervisor] git reset failed — operator intervention needed`);
|
||||
return false;
|
||||
}
|
||||
rollbackCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
function nowMs() {
|
||||
return Date.now();
|
||||
@@ -97,6 +134,20 @@ function spawnChild() {
|
||||
restartTimestamps.push(now);
|
||||
while (restartTimestamps.length && now - restartTimestamps[0] > 60_000) restartTimestamps.shift();
|
||||
if (restartTimestamps.length > MAX_RESTARTS_PER_MINUTE) {
|
||||
// Crash loop. If the last commit is young AND touched runtime/, it
|
||||
// probably broke us — roll it back and try once more.
|
||||
const ageMs = lastCommitAgeMs();
|
||||
if (
|
||||
ageMs < ROLLBACK_FRESHNESS_MS &&
|
||||
lastCommitTouchedRuntime() &&
|
||||
rollbackCount < MAX_ROLLBACKS &&
|
||||
rollbackLastCommit()
|
||||
) {
|
||||
console.log(`[supervisor] auto-rollback ${rollbackCount}/${MAX_ROLLBACKS} applied; restart counters reset`);
|
||||
restartTimestamps.length = 0;
|
||||
setTimeout(spawnChild, 500);
|
||||
return;
|
||||
}
|
||||
console.error(`[supervisor] too many restarts (${restartTimestamps.length} in 60s) — giving up`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user