Skip to content

sno-ai/sno-station-skills / skills/R-recursive-self-improvement/rem-reflect/skill

rem-reflect

Verified

Self Learning from this machine's Claude Code, Codex, Hermes, OpenClaw and Cursor sessions. Decide skill changes and lessons with accept/reject/tbd; recall lessons at session start.

FileKindSize
SKILL.mdskill md14.1 KB
references/labeler.mdmarkdown1.2 KB
references/labeler.schema.jsonconfig1.7 KB
references/local-settings.jsonconfig126 B
references/local-writer.schema.jsonconfig2.3 KB
references/proposal.schema.jsonconfig3.2 KB
references/render.jsonconfig245 B
references/signals.jsonconfig71 B
references/text.jsonconfig4.0 KB
scripts/adopt.test.tsscript13.0 KB
scripts/adopt.tsscript14.3 KB
scripts/agent-sessions.tsscript9.1 KB
scripts/allowance-fallback.test.tsscript16.0 KB
scripts/allowance-request-isolation.test.tsscript7.1 KB
scripts/backend.tsscript9.1 KB
scripts/calls.tsscript8.6 KB
scripts/catalogue-cloud.test.tsscript1.5 KB
scripts/catalogue.tsscript3.2 KB
scripts/chunk.tsscript2.1 KB
scripts/cloud-apply.test.tsscript25.2 KB
scripts/cloud-batch.test.tsscript11.2 KB
scripts/cloud-day.test.tsscript6.8 KB
scripts/cloud-verdict.test.tsscript12.1 KB
scripts/cloud-wait.test.tsscript1.3 KB
scripts/cloud.tsscript24.4 KB
scripts/config.tsscript8.1 KB
scripts/cursor.test.tsscript16.1 KB
scripts/cursor.tsscript6.7 KB
scripts/evidence.tsscript1.5 KB
scripts/first-message.test.tsscript10.3 KB
scripts/harvest.test.tsscript16.5 KB
scripts/harvest.tsscript22.6 KB
scripts/hermes-openclaw.test.tsscript25.6 KB
scripts/identity.test.tsscript3.3 KB
scripts/identity.tsscript3.5 KB
scripts/installed-roots.test.tsscript11.0 KB
scripts/journey-adopt-lesson.test.tsscript4.1 KB
scripts/journey-ops.test.tsscript8.0 KB
scripts/labeler.test.tsscript3.6 KB
scripts/labeler.tsscript9.5 KB
scripts/ledger.tsscript2.1 KB
scripts/lessons.tsscript4.7 KB
scripts/local-filter.test.tsscript3.9 KB
scripts/local-first-output.test.tsscript23.5 KB
scripts/local-settings.tsscript1.4 KB
scripts/local-writer.tsscript12.7 KB
scripts/login-in-place.test.tsscript5.8 KB
scripts/model-json.tsscript4.1 KB
scripts/no-network.test.tsscript5.0 KB
scripts/observe.tsscript1.4 KB
scripts/page-journey.test.tsscript6.7 KB
scripts/pages.test.tsscript4.1 KB
scripts/pages.tsscript4.8 KB
scripts/proposer-loop.test.tsscript1.5 KB
scripts/proposer-validate.test.tsscript9.1 KB
scripts/proposer.tsscript8.7 KB
scripts/recall-hook.test.tsscript20.9 KB
scripts/recall.test.tsscript31.4 KB
scripts/recall.tsscript18.2 KB
scripts/reflect.test.tsscript9.3 KB
scripts/reflect.tsscript14.3 KB
scripts/rem-reflectexecutablescript823 B
scripts/rem-reflect.tsscript17.8 KB
scripts/render-chunk.test.tsscript7.2 KB
scripts/render.tsscript9.7 KB
scripts/report.tsscript8.2 KB
scripts/run-status.test.tsscript19.1 KB
scripts/sampling.test.tsscript2.1 KB
scripts/sampling.tsscript915 B
scripts/selftest-guard.test.tsscript4.2 KB
scripts/selftest/bin/claudeexecutablescript94 B
scripts/selftest/bin/codexexecutablescript93 B
scripts/selftest/bin/shim-core.cjsscript4.6 KB
scripts/selftest/bin/snoexecutablescript5.7 KB
scripts/selftest/fixtures/hermes-session-export.jsonlother1.5 KB
scripts/selftest/fixtures/openclaw-events.jsonlother3.7 KB
scripts/selftest/fixtures/verdict-traces.jsonconfig16.6 KB
scripts/selftest/journey-e2e.shexecutablescript1.4 KB
scripts/selftest/journey.mjsscript9.5 KB
scripts/selftest/observe-journey.mjsscript12.5 KB
scripts/selftest/run-selftest.shexecutablescript7.0 KB
scripts/selftest/spawn-python.mjsexecutablescript765 B
scripts/session.test.tsscript5.2 KB
scripts/session.tsscript251 B
scripts/signal-local.test.tsscript7.0 KB
scripts/skill-impact.test.tsscript6.2 KB
scripts/station-settings.test.tsscript13.6 KB
scripts/store-memory.test.tsscript3.4 KB
scripts/store.tsscript7.7 KB
scripts/test-helpers.tsscript15.3 KB
scripts/text.tsscript760 B
scripts/trigger.test.tsscript3.6 KB
scripts/trigger.tsscript2.5 KB
scripts/verdicts.test.tsscript8.1 KB
scripts/verdicts.tsscript8.0 KB

scripts/local-writer.ts

View on GitHub
import { randomBytes } from 'node:crypto';
import { spawnSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { BackendError } from './backend.ts';
import type { Backend, Cli } from './backend.ts';
import { limits, reflectionFiles, skillRoots, stationCell, SettingsUnavailable } from './config.ts';
import type { Config } from './config.ts';
import { traceKey, validateEvidence } from './evidence.ts';
import type { Evidence } from './evidence.ts';
import type { Trace } from './harvest.ts';
import { eventLines } from './labeler.ts';
import { readCalls } from './calls.ts';
import type { CallRecord } from './calls.ts';
import { externalVerdicts, verdictRef } from './verdicts.ts';
import { lessonTrigger } from './trigger.ts';
import type { Verdict, VerdictRef } from './verdicts.ts';
import { appendLedger } from './ledger.ts';
import { loadLocalSettings } from './local-settings.ts';
import { appendRows } from './lessons.ts';
import { firstObject, reference, validateSchema } from './model-json.ts';
import { readPages, writePage } from './pages.ts';
import type { Page } from './pages.ts';
import { proposalSignature, validateProposal } from './proposer.ts';
import { loadRenderBudgets, renderHeader, renderTrace } from './render.ts';
import { CeilingReached } from './session.ts';
import { atomicWrite, privateDirectory, writeJson } from './store.ts';

interface WrittenLesson {
  summary: string; class: 'SKILL_DEFECT' | 'EXECUTION_LAPSE';
  situation: { task_type: string; trigger: string; tools: string[] };
  advice: string; because: string; polarity: 'from_failure' | 'from_success';
  expected_result: string; success_criteria: string; memory_fact_ids: string[];
  evidence: { line_start: number; line_end: number; quote: string }[];
  skill_target: string | null; reminder_line: string | null; verdict_line: number;
}

export function locallyWrittenSessions(store: string): Set<string> {
  const path = join(store, reflectionFiles.localWriter);
  if (!existsSync(path)) return new Set();
  return new Set(readFileSync(path, 'utf8').split('\n').filter(Boolean)
    .map(line => String((JSON.parse(line) as { trace_key: unknown }).trace_key)));
}

// Lessons cannot cite a git commit or push, so exclude those verdicts before choosing sessions; among the
// sessions left, the most failed calls and typed owner lines go first.
function pick(eligible: readonly Trace[], harness: Trace['agent_id'], done: ReadonlySet<string>):
  { trace: Trace; events: Set<number>; calls: CallRecord[]; verdicts: Verdict[] }[] {
  return eligible.filter(trace => trace.agent_id === harness && !done.has(traceKey(trace)))
    .map(trace => {
      const calls = readCalls(trace);
      const verdicts = externalVerdicts(calls).filter(verdict => verdict.kind !== 'landed');
      return { trace, events: eventLines(trace), calls, verdicts };
    })
    .filter(item => item.events.size > 0 && item.verdicts.length > 0)
    .sort((a, b) => b.events.size - a.events.size)
    .slice(0, limits.localSessionsPerHalf);
}

function promptFor(instructions: string, trace: Trace, events: ReadonlySet<number>, verdicts: readonly Verdict[], maxChars: number, windowLines: number): string {
  const listed = verdicts.map(item => `${item.line} ${item.kind} ${item.passed ? 'passed' : 'failed'} ${item.command.split('\n')[0]}`).join('\n');
  const prefix = `${instructions}\n${reference('local-writer.schema.json')}\n${renderHeader(trace)}\nExternal verdicts (line kind result command):\n${listed}\n`;
  const rendered = renderTrace(trace, loadRenderBudgets());
  const full = rendered.body.length + prefix.length <= maxChars;
  let body = '';
  for (const row of rendered.records) {
    if (!full && ![...events].some(line => Math.abs(line - row.line_number) <= windowLines)) continue;
    if (prefix.length + body.length + row.text.length > maxChars) break;
    body += row.text;
  }
  return prefix + body;
}

function pageFor(id: string, trace: Trace, lesson: WrittenLesson, evidence: Evidence[], skillTarget: string | null, now: Date): Page {
  return {
    page_id: id, body: `${lesson.because}\n\n${lesson.advice}\n`, count: 1, task_ids: [trace.trace_id],
    last_seen: now.toISOString(), skills_observed: trace.skills_loaded, backend: 'local',
    summary: lesson.summary, class: lesson.class,
    root_cause: { subject: lesson.situation.trigger, relation: 'caused', fact: lesson.because, valid_at: now.toISOString().slice(0, 10),
      evidence_pages: [], evidence_traces: [trace.trace_id] },
    fix: lesson.advice, scenario: lesson.situation, skills_used: trace.skills_loaded, skill_target: skillTarget, knowledge_used: [],
    outcome: lesson.polarity === 'from_failure' ? 'fail' : 'success', agent_id: trace.agent_id, project_id: trace.project_id,
    user_id: trace.user_id, evidence: evidence.map(item => `Line ${item.line_start}: ${item.quote.slice(0, 120)}`),
    citations: evidence, counter_examples: 'not searched', superseded: false,
  };
}

// A reminder line for the skill the session read, staged exactly as a cloud proposal is, for the owner to accept or reject.
function stageReminder(store: string, config: Config, runId: string, harness: string, page: Page, line: string, log: string[]): boolean {
  const raw = { kind: 'patch', target: page.skill_target, region: 'reminders', class: page.class,
    purpose: { summary: page.summary, page_ids: [page.page_id] }, scenario: page.scenario, skills_used: page.skills_used,
    skill_target: page.skill_target, knowledge_used: [], outcome: page.outcome, evidence: page.evidence, line };
  let validated: ReturnType<typeof validateProposal>;
  try {
    validated = validateProposal(raw, { store, roots: skillRoots(config), pages: readPages(store) }, new Set([String(page.skill_target)]));
  } catch (error) {
    log.push(`proposal refused: run ${runId} half ${harness} target ${String(page.skill_target)}: ${String(error)}`);
    return false;
  }
  const directory = join(store, reflectionFiles.staging, runId, harness);
  const hashes = validated.targetHashes ?? {};
  writeJson(join(directory, reflectionFiles.proposal), { ...raw, target_hashes: hashes });
  writeJson(join(directory, reflectionFiles.patch), { region: validated.region, ops: validated.ops ?? null,
    line: validated.reminderLine ?? null, replace_line: validated.replaceLine ?? null });
  atomicWrite(join(directory, reflectionFiles.purpose), `${page.summary}\nPages: ${page.page_id}\n`);
  appendLedger(store, { type: 'proposal', proposal_id: `${runId}/${harness}`, run_id: runId, half: harness, backend: 'local',
    kind: validated.kind, target: validated.target, region: validated.region, diff: validated.diff,
    expertise: { scenario: page.scenario, skills_used: page.skills_used, skill_target: page.skill_target, knowledge_used: [],
      outcome: page.outcome, evidence: page.evidence },
    purpose: raw.purpose, verdict: 'pending', evidence: page.evidence, signature: proposalSignature(validated.proposal),
    target_hashes: hashes });
  return true;
}

// Local First's nightly work: ask each chosen session's own CLI for one lesson, keep only answers whose quotes
// are verbatim in the session, and store them as pages, listed lessons and at most one reminder proposal per
// harness. Nothing is sent to Sno: the session text goes only to the user's own agent CLI and its model account. A lesson
// is not recalled until the owner accepts it. Sno CLI serves the prompt from its compiled binary; the public skill carries no prompt file.
export function writeLocally(store: string, config: Config, runId: string, eligible: readonly Trace[], backend: Backend,
  log: string[], now: Date, allowanceUsedUp: boolean = false): { asked: number; lessons: number; proposals: number; unavailable: number } {
  const done = locallyWrittenSessions(store);
  const settings = loadLocalSettings(store, log);
  const result = { asked: 0, lessons: 0, proposals: 0, unavailable: 0 };
  let instructions: string | undefined;
  const mark = (trace: Trace, outcome: string): void => appendRows(join(store, reflectionFiles.localWriter),
    [{ trace_key: traceKey(trace), run_id: runId, call_id: 'R5', result: outcome }]);
  for (const harness of ['claude-code', 'codex', 'hermes', 'openclaw', 'cursor'] as const) {
    let staged = false;
    for (const { trace, events, calls, verdicts } of pick(eligible, harness, done)) {
      try {
        if (!allowanceUsedUp && stationCell('R5') !== 'host') { log.push('R5 local lesson generation: off'); continue; }
      } catch (error) {
        if (!(error instanceof SettingsUnavailable)) throw error;
        log.push(`local writer: R5 unavailable: ${error.message}; no model call made`);
        continue;
      }
      if (instructions === undefined) {
        const fetched = spawnSync('sno', ['skills', 'rem-reflect-local-writer'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
        if (fetched.error || fetched.status !== 0 || !fetched.stdout.trim()) {
          log.push(`local writer: sno skills rem-reflect-local-writer failed: ${fetched.error?.message ?? (fetched.stderr.trim() || `exit ${fetched.status}`)}; no lessons or reminders written`);
          result.unavailable++;
          return result;
        }
        instructions = fetched.stdout;
      }
      log.push(`R5 local lesson generation: host (${harness})`);
      const cwd = join(store, reflectionFiles.staging, runId, harness);
      privateDirectory(cwd);
      const cli: Cli = harness === 'claude-code' ? 'claude' : harness;
      const input = promptFor(instructions, trace, events, verdicts, settings.labeler_input_max_chars, settings.labeler_event_window_lines);
      let stdout: string;
      try {
        stdout = backend.spawn({ cli, cwd, input, kind: 'model' }).stdout;
      } catch (error) {
        if (!(error instanceof BackendError) && !(error instanceof CeilingReached)) throw error;
        log.push(`${trace.trace_id}: local writer unavailable (${cli}: ${error.message})`);
        result.unavailable++;
        continue;
      }
      result.asked++;
      let lesson: WrittenLesson | null;
      let evidence: Evidence[] = [];
      let ref: VerdictRef | null = null;
      try {
        const value = firstObject(stdout);
        validateSchema(value, JSON.parse(reference('local-writer.schema.json')));
        lesson = (value as { lesson: WrittenLesson | null }).lesson;
        if (lesson) {
          evidence = lesson.evidence.map(item => ({ ...item, trace_id: trace.trace_id }));
          validateEvidence(evidence, [trace]);
          const cited = verdictRef(lesson, verdicts, calls);
          if ('reason' in cited) throw new Error(cited.reason);
          ref = cited.ref;
          const suppliedFacts = new Set([...input.matchAll(/["']?fact_?[Ii]d["']?\s*[:=]\s*["']([^"']+)["']/g)].map(match => match[1]));
          const missingFacts = lesson.memory_fact_ids.filter(id => !suppliedFacts.has(id));
          if (missingFacts.length) log.push(`${trace.trace_id}: local writer omitted memory fact references ${missingFacts.join(', ')}: absent from supplied evidence; lesson kept`);
          lesson.memory_fact_ids = lesson.memory_fact_ids.filter(id => suppliedFacts.has(id));
        }
      } catch (error) {
        log.push(`${trace.trace_id}: local writer answer refused: ${String(error)}`);
        mark(trace, 'refused');
        continue;
      }
      mark(trace, lesson ? 'lesson' : 'none');
      if (!lesson) continue;
      const id = `${runId}-${randomBytes(6).toString('hex')}`;
      const skillTarget = lesson.skill_target && trace.skills_loaded.includes(lesson.skill_target) ? lesson.skill_target : null;
      const page = pageFor(id, trace, lesson, evidence, skillTarget, now);
      writePage(store, page);
      appendRows(join(store, reflectionFiles.lessons), [{ lesson_id: `L-${id}`, page_id: id, status: 'candidate', listed: true,
        count: 1, helped: 0, harmful: 0, measured: false, created_run: runId, project_id: trace.project_id, agent_id: trace.agent_id,
        user_id: trace.user_id, skill_target: skillTarget, scenario: lesson.situation, situation: lesson.situation,
        advice: lesson.advice, because: lesson.because, applies_to: `project:${trace.project_id}`, polarity: lesson.polarity,
        expected_result: lesson.expected_result, success_criteria: lesson.success_criteria, memory_fact_ids: lesson.memory_fact_ids,
        evidence, counter_examples: 'not searched', verdict_ref: ref,
        trigger_match: lessonTrigger(trace, evidence, line => log.push(`${trace.trace_id}: ${line}`)) }]);
      result.lessons++;
      if (!staged && lesson.class === 'EXECUTION_LAPSE' && skillTarget && lesson.reminder_line?.trim()) {
        staged = stageReminder(store, config, runId, harness, page, lesson.reminder_line.trim(), log);
        if (staged) result.proposals++;
      }
    }
  }
  log.push(`local writer: ${result.asked} sessions asked; ${result.lessons} lessons; ${result.proposals} proposals`);
  return result;
}
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall