sno-ai/sno-station-skills / skills/R-recursive-self-improvement/rem-reflect/skill
rem-reflect
VerifiedSelf 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.
scripts/reflect.test.ts
View on GitHubimport { test } from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { run } from './rem-reflect.ts';
import { reflect } from './reflect.ts';
import { readLabels } from './labeler.ts';
import { appendRows } from './lessons.ts';
import { reflectionFiles } from './config.ts';
import { readState } from './store.ts';
import { tmp, fixtureConfig, makeStore, FixtureBackend,
writeCodexSession, codexMeta, codexUser, codexAssistant } from './test-helpers.ts';
test('a long trace gets one persisted keep decision on its own CLI', () => {
const config = fixtureConfig({ claude_root: tmp('c'), codex_root: tmp('x') });
const store = makeStore(config);
writeCodexSession(config.codex_root, 'big', [
codexMeta('big', '/n', { originator: 'codex_exec' }), codexUser('start the task'),
codexAssistant('Z'.repeat(60_000)), codexUser('more'), codexAssistant('done'),
], Date.parse('2026-09-07T00:00:00Z'));
const backend = new FixtureBackend();
const result = run(store, new Date('2026-09-08T00:00:00Z'), 'u', backend);
assert.equal(result.code, 0);
const labels = readLabels(store);
assert.equal(labels.size, 1);
const [label] = [...labels.values()];
assert.equal(label.labeled_by, 'codex');
assert.equal(label.decision, 'keep');
assert.deepEqual(backend.calls.filter(call => call.kind === 'labeler').map(call => call.cli), ['codex']);
});
test('a failed upload preserves a successful day and the next day retries the saved batch without relabeling', t => {
const bin = tmp('retry-bin');
const script = join(bin, 'sno');
const marker = join(bin, 'failed-once');
const first = join(bin, 'first.json');
const second = join(bin, 'second.json');
writeFileSync(script, `#!/usr/bin/env node
const fs = require('node:fs');
const args = process.argv.slice(2).join(' ');
if (args === 'station consent --json') { console.log(JSON.stringify({ consent: 'full' })); process.exit(0); }
if (args !== 'rem judge --json') process.exit(2);
const input = fs.readFileSync(0, 'utf8');
if (!fs.existsSync(${JSON.stringify(marker)})) {
fs.writeFileSync(${JSON.stringify(first)}, input);
fs.writeFileSync(${JSON.stringify(marker)}, '1');
console.error('temporary cloud outage'); process.exit(2);
}
const batch = JSON.parse(input);
if (batch.run_id === '20260908-0000') fs.writeFileSync(${JSON.stringify(second)}, input);
console.log(JSON.stringify({schema_version:1,run_id:batch.run_id,history_acknowledged:true,history_links:[],
halves:batch.halves.map(half=>({harness:half.harness,attention:[],pages:[],lessons:[],proposals:[],read_judgments:[],log_entry:'no supported change'}))}));
`);
chmodSync(script, 0o755);
const oldPath = process.env.PATH;
process.env.PATH = `${bin}:${oldPath ?? ''}`;
t.after(() => { if (oldPath === undefined) delete process.env.PATH; else process.env.PATH = oldPath; });
const config = fixtureConfig({ claude_root: tmp('c'), codex_root: tmp('x') });
const store = makeStore(config);
writeCodexSession(config.codex_root, 'cx', [codexMeta('cx', '/n'), codexUser('do it'), codexAssistant('ok')], Date.parse('2026-09-07T00:00:00Z'));
const today = new Date('2026-09-08T00:00:00Z');
const firstBackend = new FixtureBackend();
const failed = run(store, today, 'u', firstBackend);
assert.equal(failed.code, 0);
assert.equal(readState(store).last_terminal?.status, 'success');
assert.match(failed.lines.join('\n'), /cloud run 20260908-0000 send failed: .*temporary cloud outage/);
const report = readFileSync(join(store, 'staging', '20260908-0000', 'REPORT.md'), 'utf8');
assert.equal(firstBackend.calls.filter(call => call.kind === 'labeler').length, 1);
assert.equal(existsSync(first), true);
const secondBackend = new FixtureBackend();
const sameDay = run(store, today, 'u', secondBackend);
assert.match(sameDay.lines.join('\n'), /already succeeded/);
assert.equal(existsSync(second), false);
const resumed = run(store, new Date('2026-09-09T00:00:00Z'), 'u', secondBackend);
assert.equal(resumed.code, 0, resumed.lines.join('\n'));
assert.equal(secondBackend.calls.filter(call => call.kind === 'labeler').length, 0);
assert.equal(readFileSync(second, 'utf8'), readFileSync(first, 'utf8'), 'the same persisted batch reached the cloud');
assert.equal(JSON.parse(readFileSync(second, 'utf8')).halves[0].sessions.length, 1);
assert.equal(readFileSync(join(store, 'staging', '20260908-0000', 'REPORT.md'), 'utf8'), report);
});
test('eligible lessons show their advice and both accept commands, while no eligible lesson shows neither command', t => {
const bin = tmp('report-bin');
const sno = join(bin, 'sno');
writeFileSync(sno, '#!/bin/sh\n[ "$*" = "station consent --json" ] && { echo \'{"consent":"full"}\'; exit 0; }\nexit 2\n');
chmodSync(sno, 0o755);
const oldPath = process.env.PATH;
process.env.PATH = `${bin}:${oldPath ?? ''}`;
t.after(() => { if (oldPath === undefined) delete process.env.PATH; else process.env.PATH = oldPath; });
const config = fixtureConfig({ claude_root: tmp('c'), codex_root: tmp('x') });
const store = makeStore(config);
const situation = { task_type: 'debug', trigger: 'a repeated failure', tools: [] };
appendRows(join(store, reflectionFiles.lessons), [{
lesson_id: 'L-report', page_id: 'pg-report', status: 'candidate', count: 1, helped: 0, harmful: 0,
measured: false, created_run: '20260923-0000', project_id: 'p', agent_id: 'codex', user_id: 'u', skill_target: null,
scenario: situation, situation, advice: 'check the exact failure before retrying', because: 'blind retries repeat it',
applies_to: 'project:p', polarity: 'from_failure', evidence: [], counter_examples: 'not searched', listed: true,
}]);
const report = reflect(store, config, '20260923-0000', new FixtureBackend(), [], new Date('2026-09-23T00:00:00Z')).report.join('\n');
assert.match(report, /L-report: check the exact failure before retrying/);
assert.match(report, /sno rem-reflect accept L-report(?:\n|$)/);
assert.match(report, /sno rem-reflect accept L-report --all-projects/);
const empty = makeStore(config);
const emptyReport = reflect(empty, config, '20260923-0100', new FixtureBackend(), [], new Date('2026-09-23T01:00:00Z')).report.join('\n');
assert.doesNotMatch(emptyReport, /sno rem-reflect accept/);
});
test('the report counts the last seven nights of the effect signal chain from the usage and judgment rows', t => {
const bin = tmp('signal-bin');
const sno = join(bin, 'sno');
writeFileSync(sno, '#!/bin/sh\n[ "$*" = "station consent --json" ] && { echo \'{"consent":"full"}\'; exit 0; }\nexit 2\n');
chmodSync(sno, 0o755);
const oldPath = process.env.PATH;
process.env.PATH = `${bin}:${oldPath ?? ''}`;
t.after(() => { if (oldPath === undefined) delete process.env.PATH; else process.env.PATH = oldPath; });
const config = fixtureConfig({ claude_root: tmp('c'), codex_root: tmp('x') });
const store = makeStore(config);
const now = new Date('2026-10-22T06:00:00Z');
const recent = '2026-10-20T10:00:00Z';
const old = '2026-10-01T10:00:00Z';
appendRows(join(store, reflectionFiles.usage), [
{ type: 'shown', session_id: 'a', lesson_ids: ['L-1'], time: recent },
{ type: 'shown', session_id: 'a', lesson_ids: ['L-2'], time: recent },
{ type: 'shown', session_id: 'b', lesson_ids: ['L-1'], time: recent },
{ type: 'shown', session_id: 'z', lesson_ids: ['L-1'], time: old },
{ type: 'read', session_id: 'a', lesson_id: 'L-1', time: recent },
{ type: 'read', session_id: 'z', lesson_id: 'L-1', time: old },
{ type: 'first-message-recall', session_id: 'a', call: 'R4', time: recent },
{ type: 'first-message-recall', session_id: 'b', call: 'R4', time: recent },
{ type: 'first-message-recall', session_id: 'c', call: 'R4', skipped: 'consent off', time: recent },
{ type: 'first-message-recall-skipped', session_id: 'b', until: '2026-10-21T00:00:00Z', time: recent },
{ type: 'first-message-recall-skipped', session_id: 'd', until: '2026-10-21T00:00:00Z', time: recent },
{ type: 'first-message-recall-failed', session_id: 'e', error: 'Error: spawnSync sno ETIMEDOUT', time: recent },
{ type: 'first-message-recall-failed', session_id: 'f', error: 'exit 7', time: recent },
]);
appendRows(join(store, reflectionFiles.ledger), [
{ type: 'lesson-judgment', run_id: '20261020-1906', trace_id: 'a.v1', lesson_id: 'L-1', followed: 'yes', effect: 'helped', basis: 'shown' },
{ type: 'lesson-judgment', run_id: '20261021-1906', trace_id: 'b.v1', lesson_id: 'L-1', followed: 'yes', effect: 'harmful', basis: 'read' },
{ type: 'lesson-judgment', run_id: '20261021-1906', trace_id: 'b.v1', lesson_id: 'L-2', followed: 'no', effect: 'neutral', basis: 'shown' },
{ type: 'lesson-judgment', run_id: '20261001-1906', trace_id: 'z.v1', lesson_id: 'L-1', followed: 'yes', effect: 'helped', basis: 'read' },
]);
const report = reflect(store, config, '20261022-0600', new FixtureBackend(), [], now).report;
assert.ok(report.includes('Sessions shown lessons at start: 2'), report.join('\n'));
assert.ok(report.includes('Full lesson reads: 1'), report.join('\n'));
assert.ok(report.includes('Judgments: 3 (helped 1, harmful 1; basis shown 2, read 1)'), report.join('\n'));
assert.ok(report.includes('Cloud recall: sent 2 / allowance refused 2 / timed out 1'), report.join('\n'));
});
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| 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: 472k | Updated | Claude CodeCodex | Unverified | Install |
| 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: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| 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 available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |