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.tsscript22.1 KB
scripts/local-settings.tsscript1.4 KB
scripts/local-writer.tsscript12.6 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/reflect.ts

View on GitHub
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { AllowanceUsedUp, applyCloudHistoryLinks, applyCloudResponse, buildCloudBatch, removeLocallyWrittenSessions, sendCloudBatch, slimAnsweredRequest } from './cloud.ts';
import type { CloudResponse } from './cloud.ts';
import { isObject, reflectionFiles, SettingsUnavailable, stationCell, readStationSettings } from './config.ts';
import type { Config } from './config.ts';
import type { Backend } from './backend.ts';
import { traceKey } from './evidence.ts';
import { existingTraces } from './harvest.ts';
import { labelHalf, readLabels } from './labeler.ts';
import { writeLocally } from './local-writer.ts';
import { saveLocalSettings } from './local-settings.ts';
import { readLedger } from './ledger.ts';
import { lessonContext, readLessons } from './lessons.ts';
import type { Lesson } from './lessons.ts';
import { eligibleTraces } from './sampling.ts';
import { writeJson } from './store.ts';
import { observe } from './observe.ts';

export interface Reflection { failed: boolean; report: string[] }

export function reflect(store: string, config: Config, runId: string, backend: Backend, log: string[], now: Date): Reflection {
  // Only tonight's sessions (and their earlier versions, which the labeler reads) keep their text.
  const tonight = eligibleTraces(existingTraces(store, () => false), now.getTime());
  const tonightSessions = new Set(Object.values(tonight.halves).flat().map(trace => `${trace.user_id}/${trace.agent_id}/${trace.session_id}`));
  const traces = existingTraces(store, trace => tonightSessions.has(`${trace.user_id}/${trace.agent_id}/${trace.session_id}`));
  const pool = eligibleTraces(traces, now.getTime());
  const labels = readLabels(store);
  const eligible = Object.values(pool.halves).flat();
  // The mode controls sharing; sno rem enforces consent itself. R2 still selects the local labeler.
  let mode: ReturnType<typeof readStationSettings>['mode'] | undefined;
  let reason = '';
  let labelerOn = false;
  try { mode = readStationSettings().mode; labelerOn = stationCell('R2') === 'host'; }
  catch (error) {
    if (!(error instanceof SettingsUnavailable)) throw error;
    reason = error.message;
  }
  const upload = mode === 'agent-native' || mode === 'rem-enhanced';
  const enhanced = mode === 'rem-enhanced';
  if (mode === 'local-first') reason = 'local-first mode';
  log.push(`R2 labeling ${upload && labelerOn ? 'runs' : 'off'}; R3 upload ${upload ? 'sends' : `skipped: ${reason}`}`);
  if (eligible.length && upload) {
    const result = labelHalf(store, runId, null, null, eligible, backend, log, traces, labelerOn);
    for (const [key, label] of result.labels) labels.set(key, label);
    for (const write of result.writes) writeJson(write.path, write.label);
  }
  const retained = eligible.filter(trace => labels.get(traceKey(trace))?.decision !== 'drop');
  const dropped = eligible.length - retained.length;
  for (const trace of pool.expired) log.push(`${trace.trace_id}: expired unknown`);
  for (const trace of pool.superseded) log.push(`${trace.trace_id}: superseded by newer version`);
  const counts = [`Kept: ${retained.length}`, `Dropped: ${dropped}`, `Expired: ${pool.expired.length}`];

  const staging = join(store, reflectionFiles.staging);
  const acknowledged = new Set<string>();
  let historyAcknowledged = false;
  const saved = readdirSync(staging).sort().filter(id => existsSync(join(staging, id, 'cloud-response.json')));
  for (const id of saved) {
    const response: CloudResponse = JSON.parse(readFileSync(join(staging, id, 'cloud-response.json'), 'utf8'));
    if (enhanced) applyCloudResponse(store, config, response);
    else if (mode === 'agent-native') {
      try { applyCloudHistoryLinks(store, response.history_links); }
      catch (error) { log.push(`cloud run ${response.run_id} history links not saved: ${String(error)}; local report unchanged`); }
    }
    historyAcknowledged ||= response.history_acknowledged;
    slimAnsweredRequest(join(staging, id, 'cloud-request.json'));
    const request = JSON.parse(readFileSync(join(staging, id, 'cloud-request.json'), 'utf8'));
    for (const half of request.halves) for (const session of half.sessions) {
      acknowledged.add(`${half.harness}/${session.session_id}/${session.version}`);
    }
  }

  if (!upload) {
    // Local First still does the nightly work: each chosen session's own CLI writes lessons and a reminder proposal here.
    const local = mode === 'local-first' ? writeLocally(store, config, runId, retained, backend, log, now) : undefined;
    const lessons = readLessons(store).filter(lesson => lesson.status === 'candidate' && lesson.listed === true);
    const proposals = readLedger(store).filter(row => row.type === 'proposal' && row.verdict === 'pending');
    observe('rsi.proposal', { level: 'user', proposal_count: local?.proposals ?? 0, skills_touched: local?.proposals ?? 0 },
      join(staging, runId, reflectionFiles.runLog));
    // Every call failing (a logged-out or missing CLI) is a failed night, not "success, 0 lessons".
    return { failed: local !== undefined && local.asked === 0 && local.unavailable > 0, report: [...counts, ...signalLines(store, runId, now), 'Uploaded: 0', `No-upload: ${reason}`,
      ...(local ? [`Local sessions asked: ${local.asked}`, `New lessons: ${local.lessons}`, `New proposals: ${local.proposals}`] : []),
      ...log.filter(line => line.startsWith('local writer:')),
      ...decisionLines(lessons, proposals)] };
  }

  removeLocallyWrittenSessions(store, log);
  const requestPath = join(staging, runId, 'cloud-request.json');
  if (!existsSync(requestPath)) {
    try {
      writeJson(requestPath, buildCloudBatch(store, config, runId,
        retained.filter(trace => !acknowledged.has(`${trace.agent_id}/${trace.session_id}/${trace.version}`)), labels, !historyAcknowledged));
    } catch (error) { log.push(`cloud run ${runId} request not saved: ${String(error)}; local run and report unchanged`); }
  }

  let uploaded = 0;
  let local: ReturnType<typeof writeLocally> | undefined;
  const fallbackLines: string[] = [];
  const responses: CloudResponse[] = [];
  let currentAcknowledged = saved.includes(runId);
  for (;;) {
    const remaining = retained.filter(trace => !acknowledged.has(`${trace.agent_id}/${trace.session_id}/${trace.version}`));
    const pending = readdirSync(staging).some(id =>
      existsSync(join(staging, id, 'cloud-request.json')) && !existsSync(join(staging, id, 'cloud-response.json')));
    if (!pending && currentAcknowledged) break;
    let response: CloudResponse;
    try {
      const batch = buildCloudBatch(store, config, runId, remaining, labels, !historyAcknowledged);
      response = sendCloudBatch(store, batch);
    } catch (error) {
      if (error instanceof AllowanceUsedUp) {
        log.push(`cloud run ${runId}: ${error.message}`);
        local = writeLocally(store, config, runId, remaining, backend, log, now, true);
        removeLocallyWrittenSessions(store, log);
        const reset = new Intl.DateTimeFormat('en-US', { timeZone: config.time_zone,
          month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }).format(new Date(error.reset_at));
        fallbackLines.push(`Self Learning ran on your own agent's model tonight: your weekly allowance is used up, resets ${reset}.`,
          `Local sessions asked: ${local.asked}`, `New lessons: ${local.lessons}`, `New proposals: ${local.proposals}`);
        const identityPath = process.env.SNO_IDENTITY_PATH ?? join(process.env.SNO_PROFILE_DIR ?? join(homedir(), '.sno'), 'identity.json');
        try {
          const identity: unknown = existsSync(identityPath) ? JSON.parse(readFileSync(identityPath, 'utf8')) : {};
          if (!isObject(identity) || !identity.user_account_id) {
            fallbackLines.push('This computer gets one tenth of the free allowance until you link it: sno account claim.');
          }
        } catch (identityError) { log.push(`account identity not read at ${identityPath}: ${String(identityError)}`); }
        fallbackLines.push(...log.filter(line => line.startsWith('local writer:')));
        break;
      }
      const pendingRun = readdirSync(staging).sort().find(id =>
        existsSync(join(staging, id, 'cloud-request.json')) && !existsSync(join(staging, id, 'cloud-response.json')));
      log.push(`cloud run ${pendingRun ?? runId} send failed: ${String(error)}; local run and report unchanged`);
      break;
    }
    currentAcknowledged ||= response.run_id === runId;
    if (enhanced) {
      try { saveLocalSettings(store, response); }
      catch (error) { log.push(`local settings not saved: ${String(error)}`); }
      applyCloudResponse(store, config, response);
      responses.push(response);
    } else {
      try { applyCloudHistoryLinks(store, response.history_links); }
      catch (error) { log.push(`cloud run ${response.run_id} history links not saved: ${String(error)}; local report unchanged`); }
    }
    log.push(`cloud run ${response.run_id} sent`);
    historyAcknowledged ||= response.history_acknowledged;
    const sent = JSON.parse(readFileSync(join(staging, response.run_id, 'cloud-request.json'), 'utf8'));
    for (const half of sent.halves) for (const session of half.sessions) {
      uploaded++;
      acknowledged.add(`${half.harness}/${session.session_id}/${session.version}`);
    }
  }
  const halves = responses.flatMap(response => response.halves as Record<string, unknown>[]);
  const attention = halves.flatMap(half => Array.isArray(half.attention) ? half.attention as Record<string, unknown>[] : []);
  const lines = halves.map(half => String(half.log_entry ?? '')).filter(Boolean);
  const pages = halves.flatMap(half => Array.isArray(half.pages) ? half.pages : []).filter(isObject)
    .map(item => isObject(item.page) ? `${item.page.page_id}: ${item.page.summary}` : '').filter(Boolean);
  const lessons = readLessons(store).filter(lesson => lesson.status === 'candidate' && lesson.listed === true);
  const proposals = readLedger(store).filter(row => row.type === 'proposal' && row.verdict === 'pending');
  const currentProposals = proposals.filter(row => row.run_id === runId);
  observe('rsi.proposal', { level: 'user', proposal_count: currentProposals.length,
    skills_touched: new Set(currentProposals.map(row => row.target)).size }, join(staging, runId, reflectionFiles.runLog));
  const evidence = responses.map(response => join(staging, response.run_id, 'cloud-request.json'));
  const localOutcome = new Map(traces.map(trace => [trace.trace_id, labels.get(traceKey(trace))?.outcome]));
  // The cloud's response names its ranking basis and predicted outcome with its own wire names ('jev', jev_outcome); the report prints "cloud".
  return { failed: local !== undefined && local.asked === 0 && local.unavailable > 0, report: [...counts, ...signalLines(store, runId, now),
    ...fallbackLines,
    'Local session outcomes:', ...retained.map(trace => `${trace.trace_id}: ${labels.get(traceKey(trace))?.outcome ?? 'unknown'}`),
    ...(enhanced && responses.length ? [`Uploaded: ${uploaded}`,
    `Cloud-ranked: ${attention.filter(row => row.basis === 'jev').length}`,
    `Local-order fallback: ${attention.filter(row => row.basis === 'local-fallback').length}`,
    'Attention order:', ...attention.map(row =>
      `${row.trace_id}: predicted ${row.jev_outcome ?? 'unscored'}; fail probability ${row.fail_probability ?? 'unscored'}; local ${localOutcome.get(String(row.trace_id))} (${row.basis === 'jev' ? 'cloud' : row.basis})`),
    `Pages written: ${pages.length}`, ...pages] : []),
    ...decisionLines(lessons, proposals),
    ...(enhanced && responses.length ? [`Evidence: ${evidence.join(', ') || 'none'}`, ...lines] : [])] };
}

// The effect signal chain over the last seven nights: sessions shown lessons at start, full reads, the cloud's
// judgments on them, and what the first-message cloud recall did. A dead link shows here instead of going unnoticed.
function signalLines(store: string, runId: string, now: Date): string[] {
  const since = now.getTime() - 7 * 86_400_000;
  const path = join(store, reflectionFiles.usage);
  const usage: Record<string, unknown>[] = [];
  for (const line of existsSync(path) ? readFileSync(path, 'utf8').split('\n').filter(Boolean) : []) {
    let row: unknown;
    try { row = JSON.parse(line); } catch { continue; /* recall counts malformed lines; the report reads past them */ }
    if (isObject(row) && typeof row.time === 'string' && Date.parse(row.time) >= since) usage.push(row);
  }
  const of = (type: string): Record<string, unknown>[] => usage.filter(row => row.type === type);
  const sinceRun = new Date(since).toISOString().slice(0, 10).replaceAll('-', '');
  const judgments = readLedger(store).filter(row => row.type === 'lesson-judgment' && typeof row.run_id === 'string' && row.run_id >= sinceRun);
  const having = (key: string, value: string): number => judgments.filter(row => row[key] === value).length;
  const tonight = readLessons(store).filter(lesson => lesson.created_run === runId);
  return [
    `Lessons tonight: ${tonight.length}, backed by a test verdict: ${tonight.filter(lesson => lesson.verdict_ref?.kind === 'test').length}`,
    `Sessions shown lessons at start: ${new Set(of('shown').map(row => row.session_id)).size}`,
    `Full lesson reads: ${of('read').length}`,
    `Judgments: ${judgments.length} (helped ${having('effect', 'helped')}, harmful ${having('effect', 'harmful')}; basis shown ${having('basis', 'shown')}, read ${having('basis', 'read')})`,
    `Cloud recall: sent ${of('first-message-recall').filter(row => row.skipped === undefined).length} / allowance refused ${of('first-message-recall-skipped').length} / timed out ${of('first-message-recall-failed').filter(row => String(row.error).includes('ETIMEDOUT')).length}`,
  ];
}

// What waits for the owner: listed lessons with their accept commands, and staged proposals.
function decisionLines(lessons: readonly Lesson[], proposals: readonly Record<string, unknown>[]): string[] {
  return [`Lessons eligible for decision: ${lessons.length}`, ...lessons.flatMap(lesson => [
    `${lesson.lesson_id}: ${lesson.advice}`,
    lessonContext(lesson),
    `sno rem-reflect accept ${lesson.lesson_id}`,
    `sno rem-reflect accept ${lesson.lesson_id} --all-projects`,
  ]),
  `Pending decisions: ${proposals.length + lessons.length}`,
  ...proposals.map(row => `${row.proposal_id}: ${row.target} — ${isObject(row.purpose) ? row.purpose.summary : ''}`)];
}
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 CodeCodexUnverifiedInstall
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