feat(runtime): stuck-incident detector + skill metrics + edit scope (Phase 6) (#17)

Phase 6 of plans/autonomous-survival-bot-prd.md. Expand the
self-improvement loop so the bot can spot and report no-progress
stagnation, not just exception-class failures.

New:
- runtime/stuck-incident.js: detector fires a structured proposal when
  the same noProgressReason persists past 5 min (cooldown 30 min).
  Body includes runtimeState, milestone, suggested skill, slim
  snapshot, last action result, per-skill success/failure metrics
  and a forbidden-paths list. Pure module — caller (bot.js) writes
  the proposal.
- runtime/skill-metrics.js: in-memory per-skill ok/fail counters
  surfaced on snapshot.skillMetrics for the TUI and the incident
  body.
- runtime/stuck-incident.test.js: 6 tests covering null reason,
  threshold gating, cooldown, reason change resetting the timer,
  body composition and metrics snapshot.

Wiring:
- runtime/state-store.js: writeProposal accepts {editScope: string[]}
  and persists it in the frontmatter; readProposalEditScope() reads
  it back so future auto-patch.js can refuse cherry-picks that touch
  other areas.
- runtime/bot.js: tick() invokes the stuck detector each tick,
  records skill ok/fail via skillMetrics, stamps snapshot.skillMetrics
  and writes the stuck proposal via writeProposal({editScope}).
  dispatchAction now records into skillMetrics for both the
  resolved-result and the exception path.

npm test now 46/46.

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 #17.
This commit is contained in:
Yuriy Mayatnikov
2026-05-25 22:35:01 +03:00
committed by GitHub
co-authored by mayatnikov Claude Opus 4.7
parent fc62160524
commit c7eab06f22
7 changed files with 325 additions and 8 deletions
+31 -7
View File
@@ -134,25 +134,49 @@ function slugify(s) {
.slice(0, 60);
}
export function writeProposal({ kind, summary, body }) {
// editScope (optional): array of repo-relative path prefixes the
// auto-patcher is allowed to modify. Read back from the frontmatter
// so scripts/auto-patch.js can refuse cherry-picks that touch other
// areas. Falls back to the historical default (`runtime/`) when
// absent for backwards compatibility with older proposals.
export function writeProposal({ kind, summary, body, editScope }) {
const stamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
const filename = `${stamp}-${slugify(kind)}.md`;
const filePath = path.join(PROPOSALS_DIR, filename);
const content = [
const frontmatter = [
"---",
`kind: ${kind}`,
`ts: ${new Date().toISOString()}`,
`summary: ${JSON.stringify(summary)}`,
"approved: false",
"---",
"",
body,
"",
].join("\n");
];
if (Array.isArray(editScope) && editScope.length) {
frontmatter.push(`editScope: ${JSON.stringify(editScope)}`);
}
frontmatter.push("---", "");
const content = frontmatter.concat([body, ""]).join("\n");
fs.writeFileSync(filePath, content);
return { filePath, filename };
}
// Best-effort parse of editScope from a proposal markdown body's
// frontmatter. Returns null when not specified.
export function readProposalEditScope(filename, { approved = false } = {}) {
const dir = approved ? PROPOSALS_APPROVED_DIR : PROPOSALS_DIR;
const filePath = path.join(dir, filename);
let raw;
try { raw = fs.readFileSync(filePath, "utf8"); } catch { return null; }
const m = raw.match(/^---\n([\s\S]*?)\n---/);
if (!m) return null;
const scopeLine = m[1].split("\n").find((l) => l.startsWith("editScope:"));
if (!scopeLine) return null;
try {
return JSON.parse(scopeLine.slice("editScope:".length).trim());
} catch {
return null;
}
}
export function listProposals({ approved = false } = {}) {
const dir = approved ? PROPOSALS_APPROVED_DIR : PROPOSALS_DIR;
try {