How sugarlabs/reflection-engine works
Purpose and Integration Boundary
reflection-engine is a stateless Python library that guides short, child-led reflection conversations about completed work. A host application owns UI, child-facing wording, session persistence, companion identity, and network access. The engine receives work metadata plus prior turns and returns exactly one typed next turn or a session end. Its public package surface is under src/reflection_engine/__init__.py; behavior is governed by SPEC.md.
Primary Entry Point
next_turn is the host-facing orchestration API, imported as from reflection_engine import ChildTurn, Work, next_turn. It accepts a Work, conversation history, and an injected model provider. Core domain inputs and outputs—including work, child turns, engine turns, and end states—are defined in src/reflection_engine/types.py. The engine retains no session state: the host must append replies and resubmit complete relevant history each turn.
Turn Orchestration and Policy
The central control path is implemented across src/reflection_engine/guard.py and src/reflection_engine/policy.py. Guard logic handles deterministic close/end conditions that can return without a model call. Policy logic determines the permitted reflection progression and constrains the engine to questions rather than praise, answers, grading, or unrelated conversation. This is the principal safety and product-contract boundary.
Prompt Construction and Model Boundary
For a turn requiring generation, src/reflection_engine/prompts.py transforms the allowed work and conversation content into a system prompt, user message, and response schema. The engine’s prompt boundary is intentionally narrow: only the work and turns are sent to the model; host UI strings and other application data remain outside the package. The provider contract in src/reflection_engine/provider.py abstracts model transport and decoding, so the library itself has no network implementation.
Provider Injection and External Services
Hosts supply an object implementing the Provider.complete interface from src/reflection_engine/provider.py. Providers receive prompt material and a JSON schema, then return schema-decoded model output. Production-facing examples for Gemini and OpenRouter-compatible APIs reside in evals/providers.py, rather than in the engine runtime. The stated first host path is Sugar Journal through a separate sugar-ai service.
Traceability and Contract Validation
src/reflection_engine/trace.py represents or validates session-trace behavior used to make turn sequences inspectable. examples/session_trace.jsonl provides a canonical two-session wire example. SPEC.md is the behavioral contract, including close behavior and versioned changes; the package is validated against that contract through the repository test suite and real-model smoke harness.
Evaluation Runtime
The runtime library is Python, packaged via pyproject.toml. Offline/unit validation targets engine contracts; real-model evaluation is separated into evals/smoke.py and evals/providers.py. The smoke harness drives scripted conversations through injected OpenRouter/Gemini-capable providers, measuring whether live model behavior remains compatible with the engine’s policy and schema constraints.
Main entry points: provider.py Provider Contract · __init__.py Public API · smoke.py Smoke Harness · SPEC.md Behavior Spec
Components 16
Reflection Engine
Stateless runtime library.
- Public API · Host imports Work, ChildTurn, and next_turn here.
src/reflection_engine/__init__.py - Domain Types · Work, turns, and session-end outputs.
src/reflection_engine/types.py - Turn Orchestration · Consumes complete history and returns one next turn or end.
- Close Guard · Returns deterministic close/end results without model calls.
src/reflection_engine/guard.py - Reflection Policy · Constrains progression to child-led reflection questions.
src/reflection_engine/policy.py - Prompt Builder · Builds system prompt, user message, and response schema.
src/reflection_engine/prompts.py - Provider Contract · Injected transport and schema-decoding boundary.
src/reflection_engine/provider.py - Trace Validation · Represents or validates inspectable turn sequences.
src/reflection_engine/trace.py
Evaluation
Offline and live-model validation.
- Smoke Harness · Drives scripted conversations against real providers.
evals/smoke.py - Eval Provider Adapters · Production-facing provider examples outside runtime.
evals/providers.py
Other components
- Behavior Spec · Defines close behavior and versioned product rules.
SPEC.md - Host Application · Owns UI, persistence, identity, and network configuration.
- Complete History · Host appends replies and resubmits relevant turns.
- Injected Model Provider · Implements Provider.complete for a model service.
- Canonical Session Trace · Two-session wire-format example.
examples/session_trace.jsonl - Python Package · Packages the Python runtime library.
pyproject.toml
Connections 22
- Behavior Spec → Turn Orchestration: governs
- Behavior Spec → Trace Validation: defines trace behavior
- Public API → Turn Orchestration: exports
- Public API → Domain Types: exports types
- Host Application → Turn Orchestration: calls with work and history
- Host Application → Complete History: persists
- Complete History → Turn Orchestration: resubmits
- Turn Orchestration → Close Guard: checks close conditions
- Close Guard → Reflection Policy: if generation continues
- Reflection Policy → Prompt Builder: constrains prompt
- Prompt Builder → Provider Contract: prompt and JSON schema
- Provider Contract → Injected Model Provider: implemented by
- Injected Model Provider → Provider Contract: decoded output
- Provider Contract → Turn Orchestration: generated turn
- Turn Orchestration → Domain Types: returns typed result
- Turn Orchestration → Trace Validation: traceable sequence
- Canonical Session Trace → Trace Validation: wire example
- Python Package → Public API: packages
- Smoke Harness → Public API: exercises
- Smoke Harness → Eval Provider Adapters: uses
- Eval Provider Adapters → Provider Contract: implements
- Smoke Harness → Behavior Spec: validates compatibility