v0.3.0-rc.1: live skill registry + fast advisor scaffold

Roots out the v0.2.x failure mode: Pi-extracted lessons routinely named
hallucinated skill ids (relocate.surface, choose.safe.surface,
survive.shelter, gather.visible_log, …). All 47 Pi-lessons in the live DB
had applied_count=0 because normalisePreferSkill couldn't find them.

Fix:
1. runtime/skill-registry.js — single source of truth derived from
   skills/index.js. Exports listSkillIds, isRegistered, and a
   prompt-ready block (skillRegistryPrompt) grouped by namespace.
2. Pi prompts (coach/postmortem, coach/reflect) embed the live registry
   with a "USE ONLY THESE, never invent" instruction. Lessons are
   filtered at write-time too — anything not in the registry and not a
   known mode name gets dropped.
3. coach/advice.js — normalisePreferSkill now returns null for unknown
   ids, hardening consult() against any hallucinations that slip
   through. Warn-logged for visibility.

Also lays the LLM substrate for the rest of v0.3.0:

- runtime/llm/provider.js — OpenAI-compatible chat client. Configured
  via PEPA_FAST_LLM_{BASE_URL,API_KEY,MODEL,TIMEOUT_MS}. Safe no-op
  unless API_KEY is set. Supports JSON-mode.
- runtime/coach/fast-advisor.js — tactical advisor tier (scaffold).
  Exposes advise() that asks the fast LLM what to do RIGHT NOW when
  the reflex is wedged/stuck. Rejects hallucinated skill ids using the
  registry. Rate-limited 6/h, 30s cooldown. Not auto-triggered yet —
  wired into reflex in rc.3 (awareness layer).

Tests: 279 green (+24 vs rc.3): 5 registry, 9 provider, 10 advisor.

See dev/v0.3.0/PLAN.md for the full iteration design (manifesto needs
ladder, event-driven awareness, skill pre-emption) and STATUS.md for
shipped/pending tracking.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 17:39:43 +03:00
co-authored by Claude Opus 4.7
parent 865aae1213
commit fcfa2277ba
13 changed files with 1113 additions and 20 deletions
+152
View File
@@ -0,0 +1,152 @@
// OpenAI-compatible chat client for the "fast advisor" tier.
//
// The original "coach" loop uses Pi via the CLI subprocess (5-15s latency,
// rate-limited to a few calls per hour). That's appropriate for deep
// post-mortem analytics but useless when the bot needs tactical advice
// right now ("I'm wedged in a pit, what should I do?").
//
// This provider opens a parallel path: any OpenAI-compatible HTTP endpoint
// (TimeWeb, OpenAI direct, Groq, OpenRouter, local Ollama with the OpenAI
// shim, …) producing a structured JSON answer in ≤8 seconds.
//
// Configuration is strictly env-driven. The provider is a NO-OP unless
// PEPA_FAST_LLM_API_KEY is set, so it's safe to ship the code disabled.
import { info, warn } from "../log.js";
const ENV = {
BASE_URL: "PEPA_FAST_LLM_BASE_URL",
API_KEY: "PEPA_FAST_LLM_API_KEY",
MODEL: "PEPA_FAST_LLM_MODEL",
TIMEOUT_MS: "PEPA_FAST_LLM_TIMEOUT_MS",
};
const DEFAULT_BASE_URL = "https://api.openai.com/v1";
const DEFAULT_TIMEOUT_MS = 8000;
export function isAvailable() {
return !!process.env[ENV.API_KEY];
}
export function getConfig() {
return {
baseUrl: (process.env[ENV.BASE_URL] || DEFAULT_BASE_URL).replace(/\/+$/, ""),
apiKey: process.env[ENV.API_KEY] || null,
model: process.env[ENV.MODEL] || null,
timeoutMs: Number(process.env[ENV.TIMEOUT_MS]) || DEFAULT_TIMEOUT_MS,
};
}
/**
* complete({ system, user, json, model?, timeoutMs? })
* → { ok: true, text, raw, latencyMs } | { ok: false, code, detail, latencyMs }
*
* `json: true` requests JSON-mode (response_format) and returns the
* parsed object as `text`. If the provider doesn't honour JSON-mode the
* call still works but caller is responsible for parsing.
*/
export async function complete({
system,
user,
json = false,
model,
timeoutMs,
} = {}) {
const cfg = getConfig();
if (!cfg.apiKey) {
return { ok: false, code: "not_configured", detail: `set ${ENV.API_KEY}`, latencyMs: 0 };
}
const useModel = model || cfg.model;
if (!useModel) {
return { ok: false, code: "no_model", detail: `set ${ENV.MODEL} or pass model arg`, latencyMs: 0 };
}
const body = {
model: useModel,
messages: [
system ? { role: "system", content: system } : null,
{ role: "user", content: user ?? "" },
].filter(Boolean),
temperature: 0.3,
};
if (json) {
body.response_format = { type: "json_object" };
}
const url = `${cfg.baseUrl}/chat/completions`;
const startedAt = Date.now();
const controller = new AbortController();
const t = setTimeout(() => controller.abort(), timeoutMs ?? cfg.timeoutMs);
let resp;
try {
resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cfg.apiKey}`,
},
body: JSON.stringify(body),
signal: controller.signal,
});
} catch (e) {
clearTimeout(t);
const latency = Date.now() - startedAt;
const aborted = e?.name === "AbortError";
return {
ok: false,
code: aborted ? "timeout" : "network_error",
detail: e?.message ?? String(e),
latencyMs: latency,
};
}
clearTimeout(t);
const latencyMs = Date.now() - startedAt;
if (!resp.ok) {
let body;
try { body = await resp.text(); } catch { body = "<no body>"; }
warn("llm", `${useModel} ${resp.status}: ${body.slice(0, 200)}`);
return {
ok: false,
code: `http_${resp.status}`,
detail: body.slice(0, 500),
latencyMs,
};
}
let payload;
try {
payload = await resp.json();
} catch (e) {
return { ok: false, code: "bad_json", detail: e?.message ?? "parse error", latencyMs };
}
const text = payload?.choices?.[0]?.message?.content;
if (typeof text !== "string") {
return { ok: false, code: "no_content", detail: "no choices[0].message.content", latencyMs };
}
let parsed = text;
if (json) {
parsed = tryParseJson(text);
if (parsed === null) {
return { ok: false, code: "bad_json", detail: text.slice(0, 200), latencyMs };
}
}
info("llm", `${useModel} ok (${latencyMs}ms, ${text.length}ch)`);
return { ok: true, text: parsed, raw: text, latencyMs };
}
function tryParseJson(text) {
if (!text) return null;
const trimmed = text.trim().replace(/^```(?:json)?/i, "").replace(/```$/, "").trim();
try { return JSON.parse(trimmed); } catch {}
const m = trimmed.match(/\{[\s\S]*\}/);
if (!m) return null;
try { return JSON.parse(m[0]); } catch { return null; }
}
// Test exports
export const __testing = { ENV, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, tryParseJson };