# sugarlabs/sugar-activity-on-demand architecture

> Architecture diagram of the GitHub repository sugarlabs/sugar-activity-on-demand, 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/sugar-activity-on-demand
- Repository: https://github.com/sugarlabs/sugar-activity-on-demand

## Overview

#### Purpose and entry points
Sugar Activity Studio is a Linux GTK3/PyGObject desktop application that turns natural-language learning ideas into installable Sugar activities. It runs standalone without the Sugar shell, or as a Sugar-ring activity.
- Standalone launch: `main.py`, `bin/sugar-aod-studio`
- Sugar activity launch: `activity.py`, `activity/activity.info`
- Application UI is shared across both modes, centered on the studio panel/window.

#### UI and interaction layer
The GTK/Sugar interface provides a Sugar-inspired home ring, project creation, generation progress, preview, code/plan review, refinement, and export actions.
- `ui/window.py`
- `ui/panel.py`
- `ui/ring.py`
- `ui/reference_image.py`
UI submits requests to service and generation layers rather than directly managing model calls or generated files.

#### Project domain and persistence
Core models define activity specifications, project/revision state, and licensing constraints. Persistent user state—including projects, sessions, queued jobs, and credentials—lives under `~/.sugar/default/aod/`.
- `core/spec.py`
- `core/projects.py`
- `core/licenses.py`
Projects form the durable boundary between UI sessions and generated Sugar activity artifacts.

#### LLM access and prompt preparation
The LLM layer abstracts provider selection, local credential storage, prompt clarification/enhancement, and optional reference-image handling. Supported remote providers include OpenRouter, Gemini, OpenAI, Claude, DeepSeek, Qwen, Moonshot, and Ollama; offline template mode avoids a provider.
- `llm/providers.py`
- `llm/credentials.py`
- `llm/enhance.py`
Provider calls are an external-service boundary; keys remain local and are used only for the selected provider.

#### Generation pipeline and safety gates
The generation subsystem orchestrates idea-to-activity production: focus/spec work, local RAG retrieval from installed Sugar activities, planning, code creation, criticism/reflection, validation, and iterative repair.
- `generation/pipeline.py`
- `generation/generator.py`
- `generation/rag.py`
- `generation/validator.py`
- `generation/repair_loop.py`
A complete candidate is generated once; failures trigger transactional, focused search/replace repairs with rollback, then rerun acceptance gates. Validation covers syntax, Sugar API/import safety, allowlisted calls, and alignment with the request.

#### Runtime execution, preview, and refinement
Accepted candidates can be exercised in a constrained subprocess with minimal environment, GTK event pumping, and save/restore checks when runtime infrastructure exists. Preview runs the generated activity inside the studio; user-selected preview targets drive minimal refinement patches and revision history.
- `generation/runtime_harness.py`
- `generation/runtime_check.py`
- `preview/runner.py`
- `generation/refine.py`
Runtime verification may explicitly remain unavailable instead of being falsely reported as passing.

#### Jobs, packaging, and deployment
Service modules decouple long-running generation work from interactive UI through sessions, jobs, and a queue. Outputs can be packaged as Sugar `.xo` bundles, installed to `~/Activities`, or emitted as Flatpak sources. The studio itself supports Sugar bundle and AppImage distribution.
- `service/service.py`
- `service/jobs.py`
- `service/queue.py`
- `exports/flatpak.py`
- `packaging/appimage/build-appimage.sh`
Generated activities launch through Sugar Toolkit tooling such as `sugar-activity3`; GTK3 and Sugar Toolkit GTK3 are material runtime dependencies supplied by the OS or bundled in the AppImage.

Main entry points: [`generation/pipeline.py`](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/pipeline.py) (Generation pipeline), [`ui/panel.py`](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/ui/panel.py) (Studio UI), [`core/projects.py`](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/core/projects.py) (Project domain), [`generation/repair_loop.py`](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/repair_loop.py) (Validation & repair)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Launch &amp; UI"]
  node_standalone["Standalone launcher<br/>entry point<br/>[main.py]"]
  node_sugar_activity["Sugar activity launcher<br/>entry point<br/>[activity.py]"]
  node_studio_ui["Studio UI<br/>GTK/Sugar interface<br/>[panel.py]"]
end

subgraph group_domain["Projects &amp; Jobs"]
  node_project_domain["Project domain<br/>models<br/>[projects.py]"]
  node_user_state[("User state store<br/>local persistence")]
  node_job_service["Job service<br/>orchestration service<br/>[service.py]"]
  node_job_queue["Job queue<br/>[queue.py]"]
end

subgraph group_generation["Generation Pipeline"]
  node_prompt_layer["Prompt preparation<br/>LLM preparation<br/>[enhance.py]"]
  node_provider_gateway{{"Provider gateway<br/>LLM abstraction<br/>[providers.py]"}}
  node_pipeline["Generation pipeline<br/>pipeline orchestrator<br/>[pipeline.py]"]
  node_rag["Installed-activity RAG<br/>local retrieval<br/>[rag.py]"]
  node_validation_repair["Validation &amp; repair<br/>safety gate<br/>[repair_loop.py]"]
end

subgraph group_execution["Preview &amp; Export"]
  node_runtime_checks["Runtime verification<br/>constrained harness<br/>[runtime_harness.py]"]
  node_preview["In-studio preview<br/>generated activity runner<br/>[runner.py]"]
  node_exports["Activity export<br/>packaging<br/>[flatpak.py]"]
end

subgraph group_external["External Boundaries"]
  node_llm_providers{{"LLM providers<br/>external service"}}
  node_sugar_runtime{{"Sugar Toolkit runtime<br/>OS runtime"}}
end

node_standalone -->|"launches"| node_studio_ui
node_sugar_activity -->|"launches"| node_studio_ui
node_studio_ui -->|"submits generation"| node_job_service
node_studio_ui -->|"edits projects"| node_project_domain
node_project_domain -->|"persists"| node_user_state
node_job_service -->|"queues work"| node_job_queue
node_job_queue -->|"dispatches"| node_pipeline
node_pipeline -->|"prepares request"| node_prompt_layer
node_prompt_layer -->|"uses model access"| node_provider_gateway
node_provider_gateway -->|"selected provider call"| node_llm_providers
node_pipeline -->|"retrieves context"| node_rag
node_pipeline -->|"candidate acceptance"| node_validation_repair
node_validation_repair -.->|"repair and retry"| node_pipeline
node_validation_repair -->|"accepted candidate"| node_runtime_checks
node_runtime_checks -->|"verified candidate"| node_preview
node_preview -->|"stores refinements"| node_project_domain
node_preview -->|"runs activity"| node_sugar_runtime
node_project_domain -->|"exports revision"| node_exports
node_exports -->|"Sugar bundle target"| node_sugar_runtime

click node_standalone "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/main.py"
click node_sugar_activity "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/activity.py"
click node_studio_ui "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/ui/panel.py"
click node_project_domain "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/core/projects.py"
click node_job_service "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/service/service.py"
click node_job_queue "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/service/queue.py"
click node_prompt_layer "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/llm/enhance.py"
click node_provider_gateway "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/llm/providers.py"
click node_pipeline "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/pipeline.py"
click node_rag "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/rag.py"
click node_validation_repair "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/repair_loop.py"
click node_runtime_checks "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/runtime_harness.py"
click node_preview "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/preview/runner.py"
click node_exports "https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/exports/flatpak.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_standalone,node_sugar_activity,node_studio_ui toneBlue
class node_project_domain,node_user_state,node_job_service,node_job_queue toneAmber
class node_prompt_layer,node_provider_gateway,node_pipeline,node_rag,node_validation_repair toneMint
class node_runtime_checks,node_preview,node_exports toneRose
class node_llm_providers,node_sugar_runtime toneIndigo
```

## Components

### Launch & UI

- [Standalone launcher](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/main.py): entry point · `main.py`
- [Sugar activity launcher](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/activity.py): entry point · `activity.py`
- [Studio UI](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/ui/panel.py): GTK/Sugar interface · Shared panel, window, ring, and reference-image interaction layer. · `ui/panel.py`

### Projects & Jobs

- [Project domain](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/core/projects.py): models · Specifications, revisions, project state, and license constraints. · `core/projects.py`
- **User state store**: local persistence · Stores projects, sessions, queued jobs, and credentials.
- [Job service](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/service/service.py): orchestration service · Decouples interactive requests from long-running generation work. · `service/service.py`
- [Job queue](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/service/queue.py): queue · `service/queue.py`

### Generation Pipeline

- [Prompt preparation](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/llm/enhance.py): LLM preparation · Clarifies and enhances ideas, including optional reference images. · `llm/enhance.py`
- [Provider gateway](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/llm/providers.py): LLM abstraction · Selects a remote provider or offline template mode. · `llm/providers.py`
- [Generation pipeline](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/pipeline.py): pipeline orchestrator · Coordinates spec work, retrieval, planning, creation, criticism, and acceptance. · `generation/pipeline.py`
- [Installed-activity RAG](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/rag.py): local retrieval · Retrieves context from installed Sugar activities. · `generation/rag.py`
- [Validation & repair](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/repair_loop.py): safety gate · Runs acceptance checks and transactional focused repairs with rollback. · `generation/repair_loop.py`

### Preview & Export

- [Runtime verification](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/generation/runtime_harness.py): constrained harness · Can report unavailable rather than falsely passing. · `generation/runtime_harness.py`
- [In-studio preview](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/preview/runner.py): generated activity runner · Drives minimal refinement patches and revision history. · `preview/runner.py`
- [Activity export](https://github.com/sugarlabs/sugar-activity-on-demand/blob/main/exports/flatpak.py): packaging · Produces Sugar bundles, installs activities, or emits Flatpak sources. · `exports/flatpak.py`

### External Boundaries

- **LLM providers**: external service · Selected remote provider receives requests; offline mode bypasses it.
- **Sugar Toolkit runtime**: OS runtime · Runs generated activities through sugar-activity3 with GTK3.

## Connections

- Standalone launcher → Studio UI: launches
- Sugar activity launcher → Studio UI: launches
- Studio UI → Job service: submits generation
- Studio UI → Project domain: edits projects
- Project domain → User state store: persists
- Job service → Job queue: queues work
- Job queue → Generation pipeline: dispatches
- Generation pipeline → Prompt preparation: prepares request
- Prompt preparation → Provider gateway: uses model access
- Provider gateway → LLM providers: selected provider call
- Generation pipeline → Installed-activity RAG: retrieves context
- Generation pipeline → Validation & repair: candidate acceptance
- Validation & repair → Generation pipeline: repair and retry
- Validation & repair → Runtime verification: accepted candidate
- Runtime verification → In-studio preview: verified candidate
- In-studio preview → Project domain: stores refinements
- In-studio preview → Sugar Toolkit runtime: runs activity
- Project domain → Activity export: exports revision
- Activity export → Sugar Toolkit runtime: Sugar bundle target
