feat(runtime): state persistence + proposals + supervisor hot-restart

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: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 16:19:36 +03:00
co-authored by Claude Opus 4.7
parent ee2b4c26eb
commit 430bf76868
7 changed files with 560 additions and 6 deletions
+3
View File
@@ -14,6 +14,7 @@ export const EVENT_TYPES = Object.freeze({
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.
@@ -24,6 +25,8 @@ export const COMMAND_TYPES = Object.freeze({
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) {