Closes the self-improvement loop end-to-end:
reflex fails 3× → proposal file → operator approves in TUI →
`npm run propose:apply <file>` spawns Pi on a feature branch →
Pi commits the patch → supervisor watches runtime/*.js and
restarts the child on change.
runtime/state-store.js — atomic current-task.json writes, daily diary
append, proposals/ + proposals/approved/ helpers.
runtime/bot.js:
- ctx.dispatch writes current-task.json on start and updates it on
completion / failure / throw.
- failure tracker: 3 consecutive same-label failures → writeProposal()
with the snapshot, labels, and a suggested-next-step section.
30-min cooldown prevents proposal spam.
- on startup, surfaces resume info (previous task + pending proposal
count); on death, clears current-task.json + writes diary line.
- new IPC commands: PROPOSAL_LATEST returns the newest pending
proposal body; PROPOSAL_APPROVE moves it to proposals/approved/.
tui/tui.tsx — status bar shows `[proposals N, press y]` badge when
bot.pendingProposals > 0. Hotkey 'y' opens the proposal panel; 'y'
approves, 'n'/Esc closes.
scripts/propose-apply.js — given an approved proposal filename, creates
a `feat/proposal-<slug>` branch and spawns `pi -p` with the proposal
+ repo-conventions prompt. Refuses on dirty tree. No auto-push, no
auto-merge — operator reviews the diff and decides.
runtime/supervisor.js — forks bot.js as a child, watches runtime/*.js,
restarts on file change or on child exit code 42. Rate-limited at 5
restarts/minute. SIGINT/SIGTERM forward cleanly. `npm run bot` now
goes through the supervisor; `npm run bot:bare` skips it.
Smoke-tested: supervisor spawned, bot connected to MC, spawned at
expected coords, diary line written, state cleanup on SIGTERM correct.
Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
54 lines
2.0 KiB
JavaScript
54 lines
2.0 KiB
JavaScript
// Shared IPC contract between runtime/bot.js (server) and tui/tui.tsx (client).
|
|
// Frame format: one JSON object per line over a Unix-domain socket.
|
|
// Socket path: state/<server-key>/bot.sock (created by server, removed on shutdown).
|
|
|
|
export const SOCKET_BASENAME = "bot.sock";
|
|
|
|
// Server → client messages.
|
|
export const EVENT_TYPES = Object.freeze({
|
|
STATUS: "status", // periodic snapshot (HP/food/pos/task/connection)
|
|
LOG: "log", // free-form log line { level, source, text }
|
|
CHAT: "chat", // MC chat { from, text, kind: "player" | "system" }
|
|
DEATH: "death", // death event { reason, position }
|
|
ERROR: "error", // recoverable runtime error { source, text }
|
|
ASK_PI_CHUNK: "ask-pi-chunk", // streamed stdout chunk from Pi subprocess
|
|
ASK_PI_DONE: "ask-pi-done", // Pi subprocess exited { code, durationMs }
|
|
HELLO: "hello", // sent on client connect with current snapshot
|
|
PROPOSAL: "proposal", // pending proposal payload { filename, body }
|
|
});
|
|
|
|
// Client → server commands.
|
|
export const COMMAND_TYPES = Object.freeze({
|
|
PAUSE: "cmd:pause", // reflex loop stops ticking; connection stays
|
|
RESUME: "cmd:resume", // reflex loop resumes
|
|
STOP: "cmd:stop", // graceful disconnect + process exit
|
|
CHAT: "cmd:chat", // { text } sent into MC as bot
|
|
ASK_PI: "cmd:ask-pi", // { prompt } spawn `pi -p` and stream output
|
|
SNAPSHOT: "cmd:snapshot", // request immediate STATUS event
|
|
PROPOSAL_LATEST: "cmd:proposal-latest", // request latest pending proposal
|
|
PROPOSAL_APPROVE: "cmd:proposal-approve", // { filename } move to approved/
|
|
});
|
|
|
|
export function encodeFrame(obj) {
|
|
return JSON.stringify(obj) + "\n";
|
|
}
|
|
|
|
// Stateful line splitter — instance per socket.
|
|
export function createLineParser(onObject) {
|
|
let buf = "";
|
|
return (chunk) => {
|
|
buf += chunk.toString("utf8");
|
|
let idx;
|
|
while ((idx = buf.indexOf("\n")) !== -1) {
|
|
const line = buf.slice(0, idx).trim();
|
|
buf = buf.slice(idx + 1);
|
|
if (!line) continue;
|
|
try {
|
|
onObject(JSON.parse(line));
|
|
} catch (e) {
|
|
onObject({ __parseError: true, raw: line, err: String(e) });
|
|
}
|
|
}
|
|
};
|
|
}
|