# amazon-science/diversityofthoughts architecture

> Architecture diagram of the GitHub repository amazon-science/diversityofthoughts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amazon-science/diversityofthoughts
- Repository: https://github.com/amazon-science/diversityofthoughts

## Overview

1. This repository is an LLM-agent research harness, not a single application. Its core loop is: load a benchmark task, ask a model to produce one or more candidate solutions/thoughts, execute those candidates in a task-specific sandbox, and use the results to drive the next iteration. The main architectural idea is iterative search over solution trajectories.

2. The top-level entry scripts define the agent variants and run modes. `main.py` is the likely central dispatcher, while `dot.py`, `dot_bank.py`, `reflexion.py`, and `simple.py` represent the four supported policies: Diversity of Thoughts, Diversity of Thoughts with memory bank, Reflexion-style self-improvement, and a one-shot baseline. `example_run.sh` is the canonical launcher for experiments.

3. The generation subsystem lives under `generators/`. It abstracts how prompts are formed and how model outputs are parsed into structured candidate actions or code. `generator_types.py`, `generator_utils.py`, `factory.py`, and `model.py` indicate a shared interface with pluggable generators, while `py_generate.py` and `rs_generate.py` split by language/task format. This boundary isolates model prompting from the rest of the system.

4. The execution subsystem lives under `executors/`. It is responsible for running generated code or task actions and returning structured feedback. `py_executor.py` and `rs_executor.py` handle local Python and Rust execution, while `leet_executor.py` and the `leetcode_env/` package support LeetCode-style problems in an external environment. `executor_types.py`, `executor_utils.py`, and `factory.py` provide the common contracts and selection logic.

5. The LeetCodeHardGym integration is a distinct runtime boundary. `executors/leetcode_env/environment.py` and its helpers implement a browser/session-backed environment that depends on external LeetCode authentication and dataset preparation. This path is materially different from local code execution because it interacts with a remote service and browser cookies rather than running purely offline.

6. The repository supports multiple benchmark sources and task formats. The `benchmarks/` directory contains HumanEval and MBPP JSONL datasets, and the README points to optional LeetCodeHardGym experiments. `generate_dataset.py` appears to convert or prepare these tasks into the internal format consumed by the agent loop.

7. The feedback loop is what makes the architecture agentic. A generator produces a candidate; an executor runs it against tests or the environment; the result is parsed and fed back into the next generation step. That loop is governed by `max_iters`, which bounds the search depth. The DoT variants differ mainly in how they manage and diversify the candidate set across iterations.

8. `dot_bank.py` adds a task-agnostic memory bank on top of the base DoT workflow. This introduces a persistent retrieval-like store of prior thoughts/solutions that can influence later prompts, creating a second memory boundary separate from the immediate trajectory state used by the standard agents.

9. Validation and scoring are handled by standalone scripts rather than inside the agent loop. `evaluate_leet_results.py`, `validate_py_results.py`, `validate_rs_results.py`, and `test_acc.py` suggest an offline evaluation stage that reads saved trajectories or outputs and computes pass/fail or accuracy metrics. This keeps experiment execution and metric computation decoupled.

10. Utility modules such as `utils.py`, `memory_utils.py`, `gpt_usage.py`, and `generators/parse.py` provide shared support for formatting, prompt/response parsing, memory manipulation, and OpenAI usage tracking. `requirements.txt` and the OpenAI API key setup make it clear that the model backend is an external dependency, not embedded locally.

11. The data flow across the repository is therefore: benchmark input → agent-specific prompt generation → model call → parsed candidate output → execution in Python/Rust/LeetCode environment → result/trace storage → optional reflection or memory update → next iteration → final evaluation. That is the main architecture to capture in a diagram.

Main entry points: [`executors/factory.py`](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/factory.py) (Exec factory), [`generators/factory.py`](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/factory.py) (Gen factory), [`main.py`](https://github.com/amazon-science/diversityofthoughts/blob/main/main.py) (Main), [`benchmarks/humaneval_full.jsonl`](https://github.com/amazon-science/diversityofthoughts/blob/main/benchmarks/humaneval_full.jsonl) (Benchmarks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_entry["Run modes"]
  node_main["Main<br/>dispatcher<br/>[main.py]"]
  node_dot["DoT<br/>agent policy<br/>[dot.py]"]
  node_dot_bank["DoT Bank<br/>agent policy<br/>[dot_bank.py]"]
  node_reflexion["Reflexion<br/>agent policy<br/>[reflexion.py]"]
  node_simple["Baseline<br/>agent policy<br/>[simple.py]"]
  node_example_run["Launcher<br/>script<br/>[example_run.sh]"]
end

subgraph group_grp_gen["Generation"]
  node_gen_factory["Gen factory<br/>generator selector<br/>[factory.py]"]
  node_gen_types["Gen types<br/>interfaces<br/>[generator_types.py]"]
  node_gen_utils["Gen utils<br/>helpers<br/>[generator_utils.py]"]
  node_model["Model<br/>backend adapter<br/>[model.py]"]
  node_parse["Parse<br/>[parse.py]"]
  node_py_generate["Python gen<br/>task generator<br/>[py_generate.py]"]
  node_rs_generate["Rust gen<br/>task generator<br/>[rs_generate.py]"]
end

subgraph group_grp_exec["Execution"]
  node_exec_factory["Exec factory<br/>executor selector<br/>[factory.py]"]
  node_exec_types["Exec types<br/>interfaces<br/>[executor_types.py]"]
  node_py_exec["Python exec<br/>local executor<br/>[py_executor.py]"]
  node_rs_exec["Rust exec<br/>local executor<br/>[rs_executor.py]"]
  node_leet_exec["Leet exec<br/>remote executor<br/>[leet_executor.py]"]
  node_leet_env["Leet env<br/>browser env<br/>[environment.py]"]
end

subgraph group_grp_eval["Data and eval"]
  node_benchmarks["Benchmarks<br/>datasets"]
  node_dataset["Prep data<br/>dataset prep"]
  node_validate_py["Py validate<br/>offline eval"]
  node_validate_rs["Rs validate<br/>offline eval"]
  node_leet_results["Leet eval<br/>offline eval"]
  node_acc["Accuracy<br/>metric<br/>[test_acc.py]"]
end

node_utils["Utils<br/>shared helpers<br/>[utils.py]"]
node_memory_utils["Memory utils<br/>memory helpers<br/>[memory_utils.py]"]
node_gpt_usage["Usage<br/>tracking<br/>[gpt_usage.py]"]

node_example_run -->|"launches"| node_main
node_main -->|"selects"| node_dot
node_main -->|"selects"| node_dot_bank
node_main -->|"selects"| node_reflexion
node_main -->|"selects"| node_simple
node_dot_bank -->|"uses"| node_memory_utils
node_dot -->|"calls"| node_gen_factory
node_reflexion -->|"calls"| node_gen_factory
node_simple -->|"calls"| node_gen_factory
node_gen_factory -->|"implements"| node_gen_types
node_gen_factory -->|"selects"| node_py_generate
node_gen_factory -->|"selects"| node_rs_generate
node_py_generate -->|"calls"| node_model
node_rs_generate -->|"calls"| node_model
node_py_generate -->|"parses"| node_parse
node_rs_generate -->|"parses"| node_parse
node_parse -->|"feeds"| node_exec_factory
node_exec_factory -->|"implements"| node_exec_types
node_exec_factory -->|"selects"| node_py_exec
node_exec_factory -->|"selects"| node_rs_exec
node_exec_factory -->|"selects"| node_leet_exec
node_leet_exec -->|"drives"| node_leet_env
node_py_exec -->|"runs against"| node_benchmarks
node_rs_exec -->|"runs against"| node_benchmarks
node_leet_env -->|"uses tasks"| node_benchmarks
node_dataset -->|"prepares"| node_benchmarks
node_validate_py -->|"evaluates"| node_py_exec
node_validate_rs -->|"evaluates"| node_rs_exec
node_leet_results -->|"evaluates"| node_leet_exec
node_acc -->|"scores"| node_validate_py
node_acc -->|"scores"| node_validate_rs
node_acc -->|"scores"| node_leet_results
node_model -->|"tracks"| node_gpt_usage
node_gen_utils -->|"uses"| node_utils
node_exec_factory -->|"uses"| node_utils

click node_main "https://github.com/amazon-science/diversityofthoughts/blob/main/main.py"
click node_dot "https://github.com/amazon-science/diversityofthoughts/blob/main/dot.py"
click node_dot_bank "https://github.com/amazon-science/diversityofthoughts/blob/main/dot_bank.py"
click node_reflexion "https://github.com/amazon-science/diversityofthoughts/blob/main/reflexion.py"
click node_simple "https://github.com/amazon-science/diversityofthoughts/blob/main/simple.py"
click node_example_run "https://github.com/amazon-science/diversityofthoughts/blob/main/example_run.sh"
click node_gen_factory "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/factory.py"
click node_gen_types "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/generator_types.py"
click node_gen_utils "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/generator_utils.py"
click node_model "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/model.py"
click node_parse "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/parse.py"
click node_py_generate "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/py_generate.py"
click node_rs_generate "https://github.com/amazon-science/diversityofthoughts/blob/main/generators/rs_generate.py"
click node_exec_factory "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/factory.py"
click node_exec_types "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/executor_types.py"
click node_py_exec "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/py_executor.py"
click node_rs_exec "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/rs_executor.py"
click node_leet_exec "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/leet_executor.py"
click node_leet_env "https://github.com/amazon-science/diversityofthoughts/blob/main/executors/leetcode_env/environment.py"
click node_benchmarks "https://github.com/amazon-science/diversityofthoughts/blob/main/benchmarks/humaneval_full.jsonl"
click node_dataset "https://github.com/amazon-science/diversityofthoughts/blob/main/generate_dataset.py"
click node_validate_py "https://github.com/amazon-science/diversityofthoughts/blob/main/validate_py_results.py"
click node_validate_rs "https://github.com/amazon-science/diversityofthoughts/blob/main/validate_rs_results.py"
click node_leet_results "https://github.com/amazon-science/diversityofthoughts/blob/main/evaluate_leet_results.py"
click node_acc "https://github.com/amazon-science/diversityofthoughts/blob/main/test_acc.py"
click node_utils "https://github.com/amazon-science/diversityofthoughts/blob/main/utils.py"
click node_memory_utils "https://github.com/amazon-science/diversityofthoughts/blob/main/memory_utils.py"
click node_gpt_usage "https://github.com/amazon-science/diversityofthoughts/blob/main/gpt_usage.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_main,node_dot,node_dot_bank,node_reflexion,node_simple,node_example_run toneBlue
class node_gen_factory,node_gen_types,node_gen_utils,node_model,node_parse,node_py_generate,node_rs_generate toneAmber
class node_exec_factory,node_exec_types,node_py_exec,node_rs_exec,node_leet_exec,node_leet_env toneMint
class node_benchmarks,node_dataset,node_validate_py,node_validate_rs,node_leet_results,node_acc toneRose
class node_utils,node_memory_utils,node_gpt_usage toneNeutral
```

## Components

### Run modes

Top-level agent variants and experiment launchers.

- [Main](https://github.com/amazon-science/diversityofthoughts/blob/main/main.py): dispatcher · Central experiment dispatcher and shared orchestration entry point. · `main.py`
- [DoT](https://github.com/amazon-science/diversityofthoughts/blob/main/dot.py): agent policy · Diversity of Thoughts search policy. · `dot.py`
- [DoT Bank](https://github.com/amazon-science/diversityofthoughts/blob/main/dot_bank.py): agent policy · DoT variant with persistent memory bank. · `dot_bank.py`
- [Reflexion](https://github.com/amazon-science/diversityofthoughts/blob/main/reflexion.py): agent policy · Self-improving reflection-based policy. · `reflexion.py`
- [Baseline](https://github.com/amazon-science/diversityofthoughts/blob/main/simple.py): agent policy · One-shot baseline run mode. · `simple.py`
- [Launcher](https://github.com/amazon-science/diversityofthoughts/blob/main/example_run.sh): script · Canonical experiment shell launcher. · `example_run.sh`

### Generation

Prompting, model access, and output parsing.

- [Gen factory](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/factory.py): generator selector · Chooses generator implementation by task/model. · `generators/factory.py`
- [Gen types](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/generator_types.py): interfaces · Shared generator contracts and data structures. · `generators/generator_types.py`
- [Gen utils](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/generator_utils.py): helpers · Prompting and generation helper utilities. · `generators/generator_utils.py`
- [Model](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/model.py): backend adapter · Model invocation and backend abstraction. · `generators/model.py`
- [Parse](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/parse.py): response parser · Turns raw model output into structured actions. · `generators/parse.py`
- [Python gen](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/py_generate.py): task generator · Python-oriented prompt/output generation. · `generators/py_generate.py`
- [Rust gen](https://github.com/amazon-science/diversityofthoughts/blob/main/generators/rs_generate.py): task generator · Rust-oriented prompt/output generation. · `generators/rs_generate.py`

### Execution

Code and environment execution backends.

- [Exec factory](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/factory.py): executor selector · Chooses the executor implementation. · `executors/factory.py`
- [Exec types](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/executor_types.py): interfaces · Shared executor contracts and result types. · `executors/executor_types.py`
- [Python exec](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/py_executor.py): local executor · Runs Python candidate code locally. · `executors/py_executor.py`
- [Rust exec](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/rs_executor.py): local executor · Runs Rust candidate code locally. · `executors/rs_executor.py`
- [Leet exec](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/leet_executor.py): remote executor · Executes LeetCode-style tasks through the LeetCode environment. · `executors/leet_executor.py`
- [Leet env](https://github.com/amazon-science/diversityofthoughts/blob/main/executors/leetcode_env/environment.py): browser env · Browser/session-backed LeetCode runtime boundary. · `executors/leetcode_env/environment.py`

### Data and eval

Benchmarks, dataset prep, and offline scoring.

- [Benchmarks](https://github.com/amazon-science/diversityofthoughts/blob/main/benchmarks/humaneval_full.jsonl): datasets · HumanEval and MBPP benchmark sources. · `benchmarks/humaneval_full.jsonl`
- [Prep data](https://github.com/amazon-science/diversityofthoughts/blob/main/generate_dataset.py): dataset prep · Converts or prepares tasks into the internal format. · `generate_dataset.py`
- [Py validate](https://github.com/amazon-science/diversityofthoughts/blob/main/validate_py_results.py): offline eval · Validates saved Python results and trajectories. · `validate_py_results.py`
- [Rs validate](https://github.com/amazon-science/diversityofthoughts/blob/main/validate_rs_results.py): offline eval · Validates saved Rust results and trajectories. · `validate_rs_results.py`
- [Leet eval](https://github.com/amazon-science/diversityofthoughts/blob/main/evaluate_leet_results.py): offline eval · Evaluates LeetCode experiment results offline. · `evaluate_leet_results.py`
- [Accuracy](https://github.com/amazon-science/diversityofthoughts/blob/main/test_acc.py): metric · Computes aggregate accuracy metrics. · `test_acc.py`

### Other components

- [Utils](https://github.com/amazon-science/diversityofthoughts/blob/main/utils.py): shared helpers · General formatting and shared repository utilities. · `utils.py`
- [Memory utils](https://github.com/amazon-science/diversityofthoughts/blob/main/memory_utils.py): memory helpers · Memory manipulation helpers used by banked variants. · `memory_utils.py`
- [Usage](https://github.com/amazon-science/diversityofthoughts/blob/main/gpt_usage.py): tracking · Tracks model usage and cost-related metadata. · `gpt_usage.py`

## Connections

- Launcher → Main: launches · Shell launcher starts the main experiment dispatcher.
- Main → DoT: selects · Dispatcher can run the DoT policy.
- Main → DoT Bank: selects · Dispatcher can run the banked DoT policy.
- Main → Reflexion: selects · Dispatcher can run the Reflexion policy.
- Main → Baseline: selects · Dispatcher can run the baseline policy.
- DoT Bank → Memory utils: uses · Banked policy reads and updates persistent memory.
- DoT → Gen factory: calls · Policies request generator implementations.
- Reflexion → Gen factory: calls · Reflection loop uses the same generator boundary.
- Baseline → Gen factory: calls · Baseline still routes through generation abstraction.
- Gen factory → Gen types: implements · Factory instantiates generator implementations conforming to shared contracts.
- Gen factory → Python gen: selects · Chooses Python-specific generation flow when needed.
- Gen factory → Rust gen: selects · Chooses Rust-specific generation flow when needed.
- Python gen → Model: calls · Python generator prompts and queries the model backend.
- Rust gen → Model: calls · Rust generator prompts and queries the model backend.
- Python gen → Parse: parses · Python outputs are normalized into structured candidates.
- Rust gen → Parse: parses · Rust outputs are normalized into structured candidates.
- Parse → Exec factory: feeds · Structured candidates are sent toward execution selection.
- Exec factory → Exec types: implements · Factory instantiates executor implementations conforming to shared contracts.
- Exec factory → Python exec: selects · Chooses the Python runtime executor.
- Exec factory → Rust exec: selects · Chooses the Rust runtime executor.
- Exec factory → Leet exec: selects · Chooses the LeetCode-style executor.
- Leet exec → Leet env: drives · Remote executor operates through the browser/session environment.
- Python exec → Benchmarks: runs against · Python executor validates candidate code against benchmark tasks.
- Rust exec → Benchmarks: runs against · Rust executor validates candidate code against benchmark tasks.
- Leet env → Benchmarks: uses tasks · LeetCode environment consumes benchmark/task definitions.
- Prep data → Benchmarks: prepares · Dataset generation and conversion starts from benchmark sources.
- Py validate → Python exec: evaluates · Offline validation consumes Python execution outputs.
- Rs validate → Rust exec: evaluates · Offline validation consumes Rust execution outputs.
- Leet eval → Leet exec: evaluates · Offline evaluation consumes LeetCode execution traces.
- Accuracy → Py validate: scores · Accuracy script aggregates evaluation outputs.
- Accuracy → Rs validate: scores · Accuracy script aggregates evaluation outputs.
- Accuracy → Leet eval: scores · Accuracy script aggregates LeetCode evaluation outputs.
- Model → Usage: tracks · Model calls are tracked for usage accounting.
- Gen utils → Utils: uses · Generation helpers rely on shared utilities.
- Exec factory → Utils: uses · Executor selection shares common utility helpers.
