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/cloud-batch.test.ts
View on GitHubimport { test } from 'node:test';
import { saveLocalSettings } from './local-settings.ts';
import assert from 'node:assert/strict';
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { buildCloudBatch, sendCloudBatch } from './cloud.ts';
import { chunkRendering } from './chunk.ts';
import { appendLedger } from './ledger.ts';
import type { Trace, StoredRecord } from './harvest.ts';
import { renderTrace, loadRenderBudgets } from './render.ts';
import { fixtureConfig, makeStore, tmp, writeInstalledSkill } from './test-helpers.ts';
import { fileURLToPath } from 'node:url';
function trace(session: string): Trace {
const records: StoredRecord[] = [2, 3, 4, 5, 6].map((line, index) => ({
line_number: line, source_line: line,
record: { type: 'response_item', payload: { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: `${session}-${index}-` + 'x'.repeat(15_000) }] } },
}));
return {
user_id: 'local-u', project_id: 'p', agent_id: 'codex', source_harness: 'codex', originator: 'codex',
trace_id: `${session}.v1`, session_id: session, version: 1, source_path: '/source', source_line_range: [1, 6],
source_mtime: Date.parse('2026-09-21T00:00:00Z'), records, skipped_records: 0, skipped_blocks: 0,
redactions: 0, trivial: false, skills_loaded: [],
};
}
test('cloud batch sends every retained chunk, complete installed skill bodies, and only relevant actual reads', () => {
const config = fixtureConfig();
const first = writeInstalledSkill(config.claude_home, 'batch-first', 'FIRST-BODY-' + 'a'.repeat(1200));
const second = writeInstalledSkill(config.codex_home, 'batch-second', 'SECOND-BODY-' + 'b'.repeat(1300));
const store = makeStore(config);
const retained = [trace('kept-a'), trace('kept-b')];
const usage = join(store, 'ledger', 'usage.jsonl');
mkdirSync(join(store, 'ledger'), { recursive: true });
writeFileSync(usage, [
{ type: 'shown', session_id: 'kept-a', lesson_ids: ['L-a', 'L-b'] },
{ type: 'shown', session_id: 'kept-a', agent_id: 'codex', lesson_ids: ['L-b', 'L-c'] },
{ type: 'shown', session_id: 'kept-b', lesson_ids: [] },
{ type: 'shown', session_id: 'not-uploaded', lesson_ids: ['L-x'] },
{ type: 'read', session_id: 'kept-a', lesson_id: 'L-used' },
{ type: 'read', session_id: 'not-uploaded', lesson_id: 'L-irrelevant' },
].map(row => JSON.stringify(row)).join('\n') + '\n');
const labels = new Map(retained.map(row => [`local-u/codex/${row.trace_id}`, {
decision: 'keep' as const, outcome: 'fail' as const, reason: 'the task failed',
evidence: [], key_ranges: [], labeled_by: 'codex', model: 'local-model',
}]));
const batch = buildCloudBatch(store, config, '20260921-0100', retained, labels, false);
assert.equal(batch.halves.length, 1);
assert.deepEqual(batch.halves[0].sessions.map(row => row.trace_id), retained.map(row => row.trace_id));
for (const [index, row] of batch.halves[0].sessions.entries()) {
const expected = chunkRendering(retained[index].trace_id, renderTrace(retained[index], loadRenderBudgets()));
assert.ok(expected.length > 1, 'this fixture really exercises multi-chunk upload');
assert.deepEqual(row.chunks.map(chunk => chunk.text), expected.map(chunk => chunk.text));
assert.equal(row.local_outcome, 'fail');
assert.equal(row.local_reason, 'the task failed');
}
const byPath = new Map(batch.catalogue.map(item => [item.path, item]));
assert.match(byPath.get(join(first, 'SKILL.md'))?.skill_md ?? '', /FIRST-BODY-a{1200}/);
assert.match(byPath.get(join(second, 'SKILL.md'))?.skill_md ?? '', /SECOND-BODY-b{1300}/);
assert.equal(byPath.get(join(first, 'SKILL.md'))?.skill_md, readFileSync(join(first, 'SKILL.md'), 'utf8'));
assert.deepEqual(batch.halves[0].sessions.map(row => row.external_verdicts), [[], []], 'a session without calls uploads no verdicts');
assert.deepEqual(batch.usage_reads, [{ session_id: 'kept-a', lesson_id: 'L-used' }]);
assert.deepEqual(batch.usage_shown, [{ session_id: 'kept-a', lesson_ids: ['L-a', 'L-b', 'L-c'] }]);
assert.equal('history' in batch, false);
appendLedger(store, { type: 'proposal', proposal_id: 'old-proposal', verdict: 'pending' });
appendLedger(store, { type: 'verdict', proposal_id: 'old-proposal', verdict: 'Rejected' });
const firstBatch = buildCloudBatch(store, config, '20260921-0100', retained, labels, true);
assert.deepEqual(firstBatch.history?.proposals.map(row => row.proposal_id), ['old-proposal']);
assert.deepEqual(firstBatch.history?.skill_impact_rows.map(row => row.verdict), ['Rejected']);
});
test('a lost cloud answer leaves the exact batch for the next entry to replay under its original id', t => {
const config = fixtureConfig();
const store = makeStore(config);
const bin = tmp('sno-bin');
const capture = join(bin, 'requests.jsonl');
const executable = join(bin, 'sno');
writeFileSync(executable, [
'#!/usr/bin/env node',
"const fs = require('node:fs');",
"const raw = fs.readFileSync(0, 'utf8');",
"const path = process.env.REM_REQUEST_CAPTURE;",
"const first = !fs.existsSync(path);",
"fs.appendFileSync(path, JSON.stringify(raw) + '\\n');",
"if (first) { console.error('response lost'); process.exit(1); }",
"const batch = JSON.parse(raw);",
"console.log(JSON.stringify({schema_version:1,run_id:batch.run_id,history_acknowledged:true,history_links:[],halves:[],settings_version:'v1',local:{labeler_input_max_chars:120000,labeler_event_window_lines:60,recall_timeout_ms:5000}}));",
].join('\n') + '\n');
chmodSync(executable, 0o755);
const oldPath = process.env.PATH;
const oldCapture = process.env.REM_REQUEST_CAPTURE;
process.env.PATH = `${bin}:${oldPath ?? ''}`;
process.env.REM_REQUEST_CAPTURE = capture;
t.after(() => {
if (oldPath === undefined) delete process.env.PATH; else process.env.PATH = oldPath;
if (oldCapture === undefined) delete process.env.REM_REQUEST_CAPTURE; else process.env.REM_REQUEST_CAPTURE = oldCapture;
});
const firstBatch = { schema_version: 1, run_id: '20260921-0100', halves: [], catalogue: [], usage_reads: [], usage_shown: [], outcome_summary: {} };
assert.throws(() => sendCloudBatch(store, firstBatch), /response lost/);
const replacement = { ...firstBatch, run_id: '20260921-0300', outcome_summary: { changed: true } };
const response = sendCloudBatch(store, replacement);
assert.equal(response.run_id, firstBatch.run_id);
saveLocalSettings(store, response);
assert.deepEqual(JSON.parse(readFileSync(join(store, 'settings.local.json'), 'utf8')),
{ version: 'v1', labeler_input_max_chars: 120000, labeler_event_window_lines: 60, recall_timeout_ms: 5000 });
const requests = readFileSync(capture, 'utf8').trim().split('\n').map(row => JSON.parse(row) as string);
assert.equal(requests.length, 2);
assert.equal(requests[1], requests[0], 'the complete stdin bytes, not a rebuilt batch, are replayed');
});
test('a request holding half of an emoji reaches sno as well-formed JSON text', t => {
const config = fixtureConfig();
const store = makeStore(config);
const bin = tmp('sno-bin');
const capture = join(bin, 'stdin.txt');
const executable = join(bin, 'sno');
writeFileSync(executable, [
'#!/usr/bin/env node',
"const fs = require('node:fs');",
"const raw = fs.readFileSync(0, 'utf8');",
"fs.writeFileSync(process.env.REM_REQUEST_CAPTURE, raw);",
"const batch = JSON.parse(raw);",
"console.log(JSON.stringify({schema_version:1,run_id:batch.run_id,history_acknowledged:true,history_links:[],halves:[]}));",
].join('\n') + '\n');
chmodSync(executable, 0o755);
const oldPath = process.env.PATH;
const oldCapture = process.env.REM_REQUEST_CAPTURE;
process.env.PATH = `${bin}:${oldPath ?? ''}`;
process.env.REM_REQUEST_CAPTURE = capture;
t.after(() => {
if (oldPath === undefined) delete process.env.PATH; else process.env.PATH = oldPath;
if (oldCapture === undefined) delete process.env.REM_REQUEST_CAPTURE; else process.env.REM_REQUEST_CAPTURE = oldCapture;
});
const batch = { schema_version: 1, run_id: '20260923-0000', halves: [{ text: 'React with \ud83d[… 12 characters omitted …]' }, { signature: 'patch\0skills/peer-review/skill' }],
catalogue: [], usage_reads: [], usage_shown: [], outcome_summary: {} };
sendCloudBatch(store, batch);
assert.doesNotMatch(readFileSync(capture, 'utf8'), /\\u[dD][89abAB][0-9a-fA-F]{2}(?!\\u[dD][c-fC-F])/);
// the cloud's JSON column refuses NUL, which older proposal signatures and some session text carry
assert.doesNotMatch(readFileSync(capture, 'utf8'), /\\u0000/);
});
test('an answered batch keeps only what later nights read, so the store does not grow by the session text every night', t => {
// Each night's request kept every uploaded session's text after the cloud answered it (up to ~29 MB a night, never removed).
const config = fixtureConfig();
const store = makeStore(config);
const bin = tmp('sno-bin');
const executable = join(bin, 'sno');
writeFileSync(executable, [
'#!/usr/bin/env node',
"const batch = JSON.parse(require('node:fs').readFileSync(0, 'utf8'));",
"console.log(JSON.stringify({schema_version:1,run_id:batch.run_id,history_acknowledged:true,history_links:[],halves:[],settings_version:'v1',local:{labeler_input_max_chars:120000,labeler_event_window_lines:60,recall_timeout_ms:5000}}));",
].join('\n') + '\n');
chmodSync(executable, 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 catalogue = [{ name: 'heartbeat', path: 'claude/heartbeat/SKILL.md', skill_md: 'name: heartbeat' }];
const batch = { schema_version: 1, run_id: '20260921-0100', catalogue, usage_reads: [], usage_shown: [], outcome_summary: {},
halves: [{ harness: 'codex', sessions: [{ session_id: 's1', version: 2, chunks: [{ text: 'secret session text '.repeat(1000) }] }] }] };
sendCloudBatch(store, batch);
const saved = readFileSync(join(store, 'staging', '20260921-0100', 'cloud-request.json'), 'utf8');
assert.doesNotMatch(saved, /secret session text/);
assert.deepEqual(JSON.parse(saved), { schema_version: 1, run_id: '20260921-0100', catalogue,
halves: [{ harness: 'codex', sessions: [{ session_id: 's1', version: 2 }] }] });
});
test('the upload carries the code-computed verdicts of a real session beside its calls', () => {
const config = fixtureConfig();
const store = makeStore(config);
const fixtures = JSON.parse(readFileSync(fileURLToPath(new URL('./selftest/fixtures/verdict-traces.json', import.meta.url)), 'utf8')) as Record<string, Trace>;
const real = fixtures.claude_vitest;
const labels = new Map([[`${real.user_id}/claude-code/${real.trace_id}`, { decision: 'keep' as const, outcome: 'success' as const, reason: 'tests pass',
evidence: [], key_ranges: [], labeled_by: 'claude', model: 'local-model' }]]);
const session = buildCloudBatch(store, config, '20261022-0100', [real], labels, false).halves[0].sessions[0];
assert.deepEqual(session.external_verdicts.map(item => [item.line, item.kind, item.passed]), [[184, 'test', false], [202, 'test', true]]);
assert.match(session.external_verdicts[1].command, /npx vitest run/);
assert.equal(session.calls.length, 2);
});
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 |