# sugarlabs/reflection-engine architecture

> Architecture diagram of the GitHub repository sugarlabs/reflection-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sugarlabs/reflection-engine
- Repository: https://github.com/sugarlabs/reflection-engine

## Overview

#### 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: [`src/reflection_engine/provider.py`](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/provider.py) (Provider Contract), [`src/reflection_engine/__init__.py`](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/__init__.py) (Public API), [`evals/smoke.py`](https://github.com/sugarlabs/reflection-engine/blob/main/evals/smoke.py) (Smoke Harness), [`SPEC.md`](https://github.com/sugarlabs/reflection-engine/blob/main/SPEC.md) (Behavior Spec)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_engine["Reflection Engine"]
  node_public_api["Public API<br/>Python package<br/>[__init__.py]"]
  node_types["Domain Types<br/>typed models<br/>[types.py]"]
  node_next_turn{{"Turn Orchestration<br/>next_turn API"}}
  node_guard["Close Guard<br/>deterministic policy<br/>[guard.py]"]
  node_policy["Reflection Policy<br/>progression policy<br/>[policy.py]"]
  node_prompts["Prompt Builder<br/>prompt construction<br/>[prompts.py]"]
  node_provider_contract{{"Provider Contract<br/>Provider.complete<br/>[provider.py]"}}
  node_trace["Trace Validation<br/>session trace<br/>[trace.py]"]
end

subgraph group_evaluation["Evaluation"]
  node_smoke["Smoke Harness<br/>live-model evaluation<br/>[smoke.py]"]
  node_eval_providers["Eval Provider Adapters<br/>Gemini/OpenRouter adapters<br/>[providers.py]"]
end

node_spec["Behavior Spec<br/>contract<br/>[SPEC.md]"]
node_host["Host Application<br/>external host"]
node_history[("Complete History<br/>host-managed state")]
node_model_provider{{"Injected Model Provider<br/>external adapter"}}
node_trace_example["Canonical Session Trace<br/>JSONL example"]
node_package["Python Package<br/>build metadata<br/>[pyproject.toml]"]

node_spec -->|"governs"| node_next_turn
node_spec -->|"defines trace behavior"| node_trace
node_public_api -->|"exports"| node_next_turn
node_public_api -->|"exports types"| node_types
node_host -->|"calls with work and history"| node_next_turn
node_host -->|"persists"| node_history
node_history -->|"resubmits"| node_next_turn
node_next_turn -->|"checks close conditions"| node_guard
node_guard -->|"if generation continues"| node_policy
node_policy -->|"constrains prompt"| node_prompts
node_prompts -->|"prompt and JSON schema"| node_provider_contract
node_provider_contract -->|"implemented by"| node_model_provider
node_model_provider -->|"decoded output"| node_provider_contract
node_provider_contract -->|"generated turn"| node_next_turn
node_next_turn -->|"returns typed result"| node_types
node_next_turn -.->|"traceable sequence"| node_trace
node_trace_example -.->|"wire example"| node_trace
node_package -->|"packages"| node_public_api
node_smoke -->|"exercises"| node_public_api
node_smoke -->|"uses"| node_eval_providers
node_eval_providers -->|"implements"| node_provider_contract
node_smoke -.->|"validates compatibility"| node_spec

click node_spec "https://github.com/sugarlabs/reflection-engine/blob/main/SPEC.md"
click node_public_api "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/__init__.py"
click node_types "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/types.py"
click node_guard "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/guard.py"
click node_policy "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/policy.py"
click node_prompts "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/prompts.py"
click node_provider_contract "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/provider.py"
click node_trace "https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/trace.py"
click node_trace_example "https://github.com/sugarlabs/reflection-engine/blob/main/examples/session_trace.jsonl"
click node_package "https://github.com/sugarlabs/reflection-engine/blob/main/pyproject.toml"
click node_smoke "https://github.com/sugarlabs/reflection-engine/blob/main/evals/smoke.py"
click node_eval_providers "https://github.com/sugarlabs/reflection-engine/blob/main/evals/providers.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_types,node_next_turn,node_guard,node_policy,node_prompts,node_provider_contract,node_trace toneBlue
class node_smoke,node_eval_providers toneAmber
class node_spec,node_host,node_history,node_model_provider,node_trace_example,node_package toneNeutral
```

## Components

### Reflection Engine

Stateless runtime library.

- [Public API](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/__init__.py): Python package · Host imports Work, ChildTurn, and next_turn here. · `src/reflection_engine/__init__.py`
- [Domain Types](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/types.py): typed models · Work, turns, and session-end outputs. · `src/reflection_engine/types.py`
- **Turn Orchestration**: next_turn API · Consumes complete history and returns one next turn or end.
- [Close Guard](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/guard.py): deterministic policy · Returns deterministic close/end results without model calls. · `src/reflection_engine/guard.py`
- [Reflection Policy](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/policy.py): progression policy · Constrains progression to child-led reflection questions. · `src/reflection_engine/policy.py`
- [Prompt Builder](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/prompts.py): prompt construction · Builds system prompt, user message, and response schema. · `src/reflection_engine/prompts.py`
- [Provider Contract](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/provider.py): Provider.complete · Injected transport and schema-decoding boundary. · `src/reflection_engine/provider.py`
- [Trace Validation](https://github.com/sugarlabs/reflection-engine/blob/main/src/reflection_engine/trace.py): session trace · Represents or validates inspectable turn sequences. · `src/reflection_engine/trace.py`

### Evaluation

Offline and live-model validation.

- [Smoke Harness](https://github.com/sugarlabs/reflection-engine/blob/main/evals/smoke.py): live-model evaluation · Drives scripted conversations against real providers. · `evals/smoke.py`
- [Eval Provider Adapters](https://github.com/sugarlabs/reflection-engine/blob/main/evals/providers.py): Gemini/OpenRouter adapters · Production-facing provider examples outside runtime. · `evals/providers.py`

### Other components

- [Behavior Spec](https://github.com/sugarlabs/reflection-engine/blob/main/SPEC.md): contract · Defines close behavior and versioned product rules. · `SPEC.md`
- **Host Application**: external host · Owns UI, persistence, identity, and network configuration.
- **Complete History**: host-managed state · Host appends replies and resubmits relevant turns.
- **Injected Model Provider**: external adapter · Implements Provider.complete for a model service.
- [Canonical Session Trace](https://github.com/sugarlabs/reflection-engine/blob/main/examples/session_trace.jsonl): JSONL example · Two-session wire-format example. · `examples/session_trace.jsonl`
- [Python Package](https://github.com/sugarlabs/reflection-engine/blob/main/pyproject.toml): build metadata · Packages the Python runtime library. · `pyproject.toml`

## Connections

- 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
