ZeroForgeLive on 0G Galileo

@sovereignclaw/reflection

Self-critique and learning persistence for SovereignClaw agents. Drop-in reflect hook for @sovereignclaw/core that critiques an agent’s output, optionally revises it, and — when configured — writes a durable learning record back to the agent’s own memory so the next run can learn from it.

Install

pnpm add @sovereignclaw/reflection @sovereignclaw/core

10-line quickstart

import { Agent } from '@sovereignclaw/core';
import { reflectOnOutput } from '@sovereignclaw/reflection';

const agent = new Agent({
  role: 'researcher',
  systemPrompt: 'You are careful.',
  inference: /* sealed0GInference(...) */,
  memory: /* encrypted(OG_Log(...), { kek }) */,
  reflect: reflectOnOutput({
    rounds: 1,           // one critic pass (default)
    critic: 'self',      // reuse the agent's own inference
    rubric: 'accuracy',  // built-in rubric; also 'completeness', 'safety', 'concision'
    persistLearnings: true,
    threshold: 0.7,      // accept above this; revise below
  }),
});

API

ExportKindPurpose
reflectOnOutput(opts)fnBuilds a ReflectionConfig compatible with Agent.reflect.
ReflectOnOutputOptionstyperounds, critic, rubric, persistLearnings, threshold.
parseCritique(raw)fnRobustly extract { score, accept, reason } from a critic’s JSON.
buildBuiltInRubricPromptfnCompose a prompt for one of the four built-in rubrics.
buildCustomRubricPromptfnCompose a prompt for a caller-defined rubric.
persistLearning({ memory, record })fnWrite a LearningRecordV1 under LEARNING_PREFIX on any MemoryProvider.
learningKey(timestamp, suffix)fnCanonical key for a learning record.
CRITIC_SYSTEM_PROMPT / CRITIC_OUTPUT_SHAPEconstThe prompt contract the critic is held to.
BuiltInRubric / CustomRubrictype`'accuracy' \'completeness' \'safety' \'concision'` or a callback.
LearningRecordV1typeDurable shape written back into agent memory.

Errors

All extend ReflectionError:

ErrorWhen
CritiqueParseErrorCritic output didn’t parse to the required JSON shape.
InvalidReflectionConfigErrorCaller passed threshold out of [0, 1] / unknown critic / etc.
LearningPersistErrorMemory provider rejected the learning write.

How it works

  1. Agent runs, produces output.
  2. reflect.run({ output, input, agent, memory }) kicks off:
  • Critic inference with the rubric-specific prompt.
  • parseCritique extracts { score, accept, reason }.
  • If score < threshold, a revision inference is run using the

critic’s suggestion; this loop runs up to rounds times.

  1. If persistLearnings is on, the final record (task, output, score,

reason) is written to memory under LEARNING_PREFIX. The next run of the same agent loads recent learnings via listRecentLearnings from @sovereignclaw/core and includes them in the system prompt.

This is an acyclic integration: @sovereignclaw/core only depends on the ReflectionConfig interface (declared in core itself); the concrete reflectOnOutput implementation lives here and imports core. You can substitute your own reflection by satisfying ReflectionConfig.

Further reading

License

MIT — see the repo root.