# suraj-gameramp/scribe architecture

> Architecture diagram of the GitHub repository suraj-gameramp/scribe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/suraj-gameramp/scribe
- Repository: https://github.com/suraj-gameramp/scribe

## Overview

#### Purpose and Entry Points
SCRIBE is a reproducible harness for DABStep data-analysis tasks using a three-role, spec-conditioned agent workflow. The runtime entry point is `src/run.ts`; canonical behavior is selected through `configs/scribe_main.yaml`. A small end-to-end reproduction is orchestrated by `scripts/repro.sh`.

#### Run Configuration and Model Resolution
Run YAML config defines model/provider assignments, prompts, task split, and execution settings. `src/config.ts` loads the run configuration, while `src/model_resolver.ts` maps configured model names/providers to callable model clients. Canonical and baseline variants are preserved under `configs/` and `experiments/configs/`, enabling planner/executor/provider comparisons.

#### Frozen Spec Planning
The first frontier-model role reads each task plus downloaded source context and produces a structured computation plan. That plan is injected into the executor context and intended to remain immutable during normal execution. Prompt contracts for baseline, grafted, iterative, and domain-knowledge variants reside in:
- `src/prompts/dabstep_baseline.yaml`
- `src/prompts/dabstep_grafted.yaml`
- `src/prompts/dabstep_grafted_iterative.yaml`

#### ReAct Execution and Python Boundary
A lower-cost executor model runs the main iterative agent/tool loop. It can inspect the frozen specification, reason over the task, and execute data analysis only through the controlled Python tool boundary:
- `src/harness/tools/run_python.ts`
- `src/harness/services/python_repl.ts`
- `src/harness/tools/index.ts`

The Python REPL is the operational boundary for loading task artifacts, calculating results, and producing the final answer; source documents and data are supplied under `data/context/` after acquisition.

#### Backreview Escalation Path
When the executor identifies an interpretation conflict or ambiguity, it invokes the planner-facing escalation tool. This review path re-reads the task/source material and returns either confirmation or revised guidance, separating expensive specification reasoning from routine execution:
- `src/harness/tools/ask_planner.ts`
- `src/harness/hooks/after_tool.ts`
- `src/run.ts`

This is the primary control-flow branch beyond ordinary ReAct: initial spec → executor actions → optional review/escalation → continued execution.

#### Observability and Session Artifacts
The harness records token use, local task/run status, prompt cache behavior, and session traces, making each run inspectable and resumable at the experiment layer:
- `src/harness/services/session_logger.ts`
- `src/harness/services/token_tracker.ts`
- `src/harness/services/local_tracker.ts`

Committed outputs in `results/`, especially JSONL samples and reproduction manifests, are the durable evidence layer rather than application-facing output.

#### Dataset, Evaluation, and External Dependencies
Tasks and split definitions are versioned in `data/target_tasks.jsonl` and `data/splits/`. `scripts/fetch_dabstep_data.py` retrieves benchmark context/data; `scripts/build_dataset.py` and `scripts/extract_specs.py` support preparation; `scripts/grade.py` evaluates results against DABStep conventions. The system is Node/TypeScript plus Python, with model access configured through environment credentials for OpenRouter, Fireworks, and potentially Baseten; `scripts/patch_pi_ai_env.cjs` adapts the underlying AI SDK environment for provider compatibility.

Main entry points: [`src/run.ts`](https://github.com/suraj-gameramp/scribe/blob/main/src/run.ts) (Run entry point), [`data/target_tasks.jsonl`](https://github.com/suraj-gameramp/scribe/blob/main/data/target_tasks.jsonl) (Versioned tasks), [`src/harness/hooks/after_tool.ts`](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/hooks/after_tool.ts) (Post-tool review hook), [`data/context/dabstep_helper.py`](https://github.com/suraj-gameramp/scribe/blob/main/data/context/dabstep_helper.py) (Downloaded context)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Agent Runtime"]
  node_entry["Run entry point<br/>TypeScript entry<br/>[run.ts]"]
  node_run_config["Run configuration<br/>YAML config<br/>[scribe_main.yaml]"]
  node_config_loader["Config loader<br/>TypeScript module<br/>[config.ts]"]
  node_model_resolver["Model resolver<br/>TypeScript module<br/>[model_resolver.ts]"]
  node_planner["Frozen spec planner<br/>frontier-model role"]
  node_prompt_contracts["Planning prompt variants<br/>prompt YAML"]
  node_executor["ReAct executor<br/>lower-cost model role"]
  node_tool_registry["Tool registry<br/>TypeScript module<br/>[index.ts]"]
  node_python_tool["Python tool<br/>controlled tool<br/>[run_python.ts]"]
  node_python_repl{{"Python REPL<br/>execution boundary<br/>[python_repl.ts]"}}
  node_planner_escalation["Planner escalation tool<br/>controlled tool<br/>[ask_planner.ts]"]
  node_review_hook["Post-tool review hook<br/>TypeScript hook<br/>[after_tool.ts]"]
  node_session_observability["Session observability<br/>run services<br/>[session_logger.ts]"]
end

subgraph group_data["Dataset Pipeline"]
  node_tasks[("Versioned tasks<br/>JSONL dataset<br/>[target_tasks.jsonl]")]
  node_source_context[("Downloaded context<br/>task artifacts<br/>[dabstep_helper.py]")]
  node_data_acquisition["Data acquisition<br/>Python script"]
  node_grader["DABStep grader<br/>Python script<br/>[grade.py]"]
end

subgraph group_evidence["Experiment Evidence"]
  node_variant_configs["Experiment variants<br/>configuration set<br/>[kimi_grafted.yaml]"]
  node_results[("Results and manifests<br/>durable artifacts<br/>[run_manifest.json]")]
  node_reproduction["End-to-end reproduction<br/>shell script<br/>[repro.sh]"]
end

node_run_config -->|"loaded by"| node_config_loader
node_config_loader -->|"run settings"| node_entry
node_entry -->|"resolves role clients"| node_model_resolver
node_variant_configs -.->|"comparison variants"| node_run_config
node_tasks -->|"task and split"| node_entry
node_data_acquisition -->|"downloads"| node_source_context
node_source_context -->|"source material"| node_planner
node_tasks -->|"task"| node_planner
node_prompt_contracts -->|"prompt contract"| node_planner
node_planner -->|"immutable computation plan"| node_executor
node_tasks -->|"task"| node_executor
node_executor -->|"tool calls"| node_tool_registry
node_tool_registry -->|"exposes"| node_python_tool
node_python_tool -->|"executes code"| node_python_repl
node_source_context -->|"data and documents"| node_python_repl
node_executor -.->|"ambiguity or conflict"| node_planner_escalation
node_planner_escalation -->|"review guidance"| node_review_hook
node_review_hook -.->|"re-reads specification"| node_planner
node_review_hook -->|"continues execution"| node_executor
node_entry -->|"run telemetry"| node_session_observability
node_python_repl -->|"answers and artifacts"| node_results
node_session_observability -->|"traces and manifests"| node_results
node_results -->|"evaluated by"| node_grader
node_reproduction -->|"orchestrates"| node_entry

click node_entry "https://github.com/suraj-gameramp/scribe/blob/main/src/run.ts"
click node_run_config "https://github.com/suraj-gameramp/scribe/blob/main/configs/scribe_main.yaml"
click node_config_loader "https://github.com/suraj-gameramp/scribe/blob/main/src/config.ts"
click node_model_resolver "https://github.com/suraj-gameramp/scribe/blob/main/src/model_resolver.ts"
click node_prompt_contracts "https://github.com/suraj-gameramp/scribe/blob/main/src/prompts/dabstep_grafted.yaml"
click node_tool_registry "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/index.ts"
click node_python_tool "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/run_python.ts"
click node_python_repl "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/services/python_repl.ts"
click node_planner_escalation "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/ask_planner.ts"
click node_review_hook "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/hooks/after_tool.ts"
click node_session_observability "https://github.com/suraj-gameramp/scribe/blob/main/src/harness/services/session_logger.ts"
click node_tasks "https://github.com/suraj-gameramp/scribe/blob/main/data/target_tasks.jsonl"
click node_source_context "https://github.com/suraj-gameramp/scribe/blob/main/data/context/dabstep_helper.py"
click node_data_acquisition "https://github.com/suraj-gameramp/scribe/blob/main/scripts/fetch_dabstep_data.py"
click node_grader "https://github.com/suraj-gameramp/scribe/blob/main/scripts/grade.py"
click node_variant_configs "https://github.com/suraj-gameramp/scribe/blob/main/experiments/configs/kimi_grafted.yaml"
click node_results "https://github.com/suraj-gameramp/scribe/blob/main/results/repro_pilot/run_manifest.json"
click node_reproduction "https://github.com/suraj-gameramp/scribe/blob/main/scripts/repro.sh"

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_entry,node_run_config,node_config_loader,node_model_resolver,node_planner,node_prompt_contracts,node_executor,node_tool_registry,node_python_tool,node_python_repl,node_planner_escalation,node_review_hook,node_session_observability toneBlue
class node_tasks,node_source_context,node_data_acquisition,node_grader toneAmber
class node_variant_configs,node_results,node_reproduction toneMint
```

## Components

### Agent Runtime

TypeScript orchestration and controlled execution.

- [Run entry point](https://github.com/suraj-gameramp/scribe/blob/main/src/run.ts): TypeScript entry · Selects canonical workflow and coordinates a run. · `src/run.ts`
- [Run configuration](https://github.com/suraj-gameramp/scribe/blob/main/configs/scribe_main.yaml): YAML config · Defines model roles, prompts, splits, and execution settings. · `configs/scribe_main.yaml`
- [Config loader](https://github.com/suraj-gameramp/scribe/blob/main/src/config.ts): TypeScript module · `src/config.ts`
- [Model resolver](https://github.com/suraj-gameramp/scribe/blob/main/src/model_resolver.ts): TypeScript module · Maps configured providers and model names to clients. · `src/model_resolver.ts`
- **Frozen spec planner**: frontier-model role · Creates the structured computation plan from task and source context.
- [Planning prompt variants](https://github.com/suraj-gameramp/scribe/blob/main/src/prompts/dabstep_grafted.yaml): prompt YAML · Baseline, grafted, and iterative planner contracts. · `src/prompts/dabstep_grafted.yaml`
- **ReAct executor**: lower-cost model role · Iterates over the task using the frozen plan and tools.
- [Tool registry](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/index.ts): TypeScript module · `src/harness/tools/index.ts`
- [Python tool](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/run_python.ts): controlled tool · `src/harness/tools/run_python.ts`
- [Python REPL](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/services/python_repl.ts): execution boundary · Loads artifacts, computes results, and produces answers. · `src/harness/services/python_repl.ts`
- [Planner escalation tool](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/tools/ask_planner.ts): controlled tool · Requests interpretation review for ambiguity or conflict. · `src/harness/tools/ask_planner.ts`
- [Post-tool review hook](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/hooks/after_tool.ts): TypeScript hook · Routes escalation and continued execution. · `src/harness/hooks/after_tool.ts`
- [Session observability](https://github.com/suraj-gameramp/scribe/blob/main/src/harness/services/session_logger.ts): run services · Records traces, token use, cache behavior, and local status. · `src/harness/services/session_logger.ts`

### Dataset Pipeline

Task acquisition, preparation, and grading.

- [Versioned tasks](https://github.com/suraj-gameramp/scribe/blob/main/data/target_tasks.jsonl): JSONL dataset · DABStep tasks and split definitions. · `data/target_tasks.jsonl`
- [Downloaded context](https://github.com/suraj-gameramp/scribe/blob/main/data/context/dabstep_helper.py): task artifacts · Source documents and data supplied to planning and Python. · `data/context/dabstep_helper.py`
- [Data acquisition](https://github.com/suraj-gameramp/scribe/blob/main/scripts/fetch_dabstep_data.py): Python script · Retrieves benchmark context and data. · `scripts/fetch_dabstep_data.py`
- [DABStep grader](https://github.com/suraj-gameramp/scribe/blob/main/scripts/grade.py): Python script · Evaluates run results against benchmark conventions. · `scripts/grade.py`

### Experiment Evidence

Inspectable run artifacts and variant configurations.

- [Experiment variants](https://github.com/suraj-gameramp/scribe/blob/main/experiments/configs/kimi_grafted.yaml): configuration set · Canonical and comparison model/provider configurations. · `experiments/configs/kimi_grafted.yaml`
- [Results and manifests](https://github.com/suraj-gameramp/scribe/blob/main/results/repro_pilot/run_manifest.json): durable artifacts · Committed JSONL samples and reproduction evidence. · `results/repro_pilot/run_manifest.json`
- [End-to-end reproduction](https://github.com/suraj-gameramp/scribe/blob/main/scripts/repro.sh): shell script · Orchestrates a small reproduction run. · `scripts/repro.sh`

## Connections

- Run configuration → Config loader: loaded by
- Config loader → Run entry point: run settings
- Run entry point → Model resolver: resolves role clients
- Experiment variants → Run configuration: comparison variants
- Versioned tasks → Run entry point: task and split
- Data acquisition → Downloaded context: downloads
- Downloaded context → Frozen spec planner: source material
- Versioned tasks → Frozen spec planner: task
- Planning prompt variants → Frozen spec planner: prompt contract
- Frozen spec planner → ReAct executor: immutable computation plan
- Versioned tasks → ReAct executor: task
- ReAct executor → Tool registry: tool calls
- Tool registry → Python tool: exposes
- Python tool → Python REPL: executes code
- Downloaded context → Python REPL: data and documents
- ReAct executor → Planner escalation tool: ambiguity or conflict
- Planner escalation tool → Post-tool review hook: review guidance
- Post-tool review hook → Frozen spec planner: re-reads specification
- Post-tool review hook → ReAct executor: continues execution
- Run entry point → Session observability: run telemetry
- Python REPL → Results and manifests: answers and artifacts
- Session observability → Results and manifests: traces and manifests
- Results and manifests → DABStep grader: evaluated by
- End-to-end reproduction → Run entry point: orchestrates
