# muhammadtalha-prog/matlab-coding-agent architecture

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

- Interactive diagram: https://gitdiagram.com/muhammadtalha-prog/matlab-coding-agent
- Repository: https://github.com/muhammadtalha-prog/matlab-coding-agent

## Overview

#### Purpose and Entry Points
This is a local Python CLI agent that converts a natural-language MATLAB task into a planned, generated, optionally executed, and verified `.m` function. Its primary entry point is `main.py`; users run it with `--task` or interactively. Runtime configuration is centralized in `config.py`, populated from `.env` based on `.env.template`.

#### Orchestration Pipeline
`main.py` owns the sequential workflow: validate local LLM availability, obtain a structured implementation plan, generate MATLAB source, execute/verify it when MATLAB is available, and save successful output. The workflow is intentionally serial and retry-bounded for low-memory local operation.
- `main.py`
- `config.py`
- `agents/__init__.py`

#### Local LLM Boundary
The agent uses AG2/AutoGen-style conversational agents backed by an Ollama OpenAI-compatible local endpoint. The LLM client layer performs connectivity/model health checks before generation; model URL, token limit, temperature, and call timeout are configuration inputs.
- `agents/llm_client.py`
- `config.py`
- `.env.template`

Ollama is an external required service (`ollama serve`); the expected models are small local coding models such as `qwen2.5-coder`.

#### Planning Stage
The planner turns the requested task into a constrained structured plan, including the intended MATLAB function/file design. This plan is the handoff boundary between task interpretation and source production.
- `agents/planner.py`
- `main.py`

Planning constraints emphasize base MATLAB compatibility, input handling, and an implementable function-oriented output.

#### Code Generation Stage
The coder consumes the user task plus planner output and produces MATLAB `.m` source. Generated code is first written into the sandbox rather than directly into the final workspace, isolating unverified artifacts from deliverables.
- `agents/coder.py`
- `sandbox/`
- `main.py`

The repository includes `sandbox/moving_average_filter.m` as an example/intermediate MATLAB artifact.

#### MATLAB Execution and Debug Loop
The executor runs generated MATLAB through a subprocess when a MATLAB installation can be located. It applies execution timeouts and returns execution diagnostics. On failure, the debugger receives the code/error context and proposes repaired code; `main.py` repeats execution up to `MAX_DEBUG_RETRIES`. If MATLAB is unavailable, generation can still complete without verification.
- `agents/matlab_executor.py`
- `agents/debugger.py`
- `main.py`

MATLAB itself is an optional external runtime; its executable path and subprocess timeout are configured in `config.py`.

#### Persistence and Approval Boundary
Only code that passes the pipeline’s execution criteria is promoted from `sandbox/` to a task-specific `workspace/<name>/` directory. `AUTO_APPROVE` controls whether this final save requires a human confirmation. Sandbox content is temporary, while workspace output is the durable user-facing artifact.
- `main.py`
- `sandbox/`
- `.env.template`

Main entry points: [`main.py`](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/main.py) (CLI orchestrator), [`sandbox`](https://github.com/muhammadtalha-prog/matlab-coding-agent/tree/main/sandbox) (Sandbox), [`config.py`](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/config.py) (Runtime config), [`agents/matlab_executor.py`](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/matlab_executor.py) (MATLAB executor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["CLI Pipeline"]
  node_main["CLI orchestrator<br/>Python entry point<br/>[main.py]"]
  node_config["Runtime config<br/>Python config<br/>[config.py]"]
  node_env_template["Environment template<br/>env template<br/>[.env.template]"]
end

subgraph group_agents["Agent Stages"]
  node_llm_client["LLM client<br/>agent client<br/>[llm_client.py]"]
  node_planner["Planner<br/>agent stage<br/>[planner.py]"]
  node_plan["Structured plan<br/>handoff artifact"]
  node_coder["MATLAB coder<br/>agent stage<br/>[coder.py]"]
  node_executor["MATLAB executor<br/>subprocess runner<br/>[matlab_executor.py]"]
  node_debugger["Debugger<br/>agent stage<br/>[debugger.py]"]
end

subgraph group_artifacts["Artifact Boundaries"]
  node_sandbox[("Sandbox<br/>temporary artifacts")]
  node_example_matlab["Example MATLAB artifact<br/>MATLAB source"]
  node_workspace[("Workspace output<br/>durable artifacts")]
end

node_user["User task<br/>input"]
node_ollama{{"Ollama<br/>external LLM service"}}
node_matlab{{"MATLAB runtime<br/>external runtime"}}
node_approval(("Human approval<br/>approval gate"))

node_user -->|"task input"| node_main
node_env_template -.->|"documents settings"| node_config
node_config -->|"runtime settings"| node_main
node_config -->|"model settings"| node_llm_client
node_config -->|"MATLAB path and timeout"| node_executor
node_main -->|"health check"| node_llm_client
node_llm_client -->|"local API"| node_ollama
node_main -->|"request task plan"| node_planner
node_planner -->|"produces"| node_plan
node_main -->|"task and plan"| node_coder
node_plan -->|"implementation design"| node_coder
node_coder -->|"writes generated .m"| node_sandbox
node_sandbox -->|"unverified source"| node_executor
node_executor -->|"subprocess execution"| node_matlab
node_executor -->|"execution diagnostics"| node_main
node_main -.->|"failure context"| node_debugger
node_debugger -->|"repaired code"| node_sandbox
node_sandbox -->|"verified artifact"| node_approval
node_approval -->|"promote on approval"| node_workspace
node_sandbox -.->|"promote when auto-approved"| node_workspace
node_sandbox -.->|"contains example"| node_example_matlab

click node_main "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/main.py"
click node_config "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/config.py"
click node_env_template "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/.env.template"
click node_llm_client "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/llm_client.py"
click node_planner "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/planner.py"
click node_coder "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/coder.py"
click node_executor "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/matlab_executor.py"
click node_debugger "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/debugger.py"
click node_sandbox "https://github.com/muhammadtalha-prog/matlab-coding-agent/tree/main/sandbox"
click node_example_matlab "https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/sandbox/moving_average_filter.m"

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_config,node_env_template toneBlue
class node_llm_client,node_planner,node_plan,node_coder,node_executor,node_debugger toneAmber
class node_sandbox,node_example_matlab,node_workspace toneMint
class node_user,node_ollama,node_matlab,node_approval toneNeutral
```

## Components

### CLI Pipeline

Serial local workflow owned by the CLI.

- [CLI orchestrator](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/main.py): Python entry point · Coordinates validation, generation, execution, retries, and promotion. · `main.py`
- [Runtime config](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/config.py): Python config · Provides model, timeout, MATLAB, retry, and approval settings. · `config.py`
- [Environment template](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/.env.template): env template · Documents configuration populated through .env. · `.env.template`

### Agent Stages

LLM-backed planning, generation, and repair.

- [LLM client](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/llm_client.py): agent client · Checks local endpoint and model availability. · `agents/llm_client.py`
- [Planner](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/planner.py): agent stage · Produces the structured MATLAB implementation plan. · `agents/planner.py`
- **Structured plan**: handoff artifact · Defines intended function and file design.
- [MATLAB coder](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/coder.py): agent stage · Generates .m source from the task and plan. · `agents/coder.py`
- [MATLAB executor](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/matlab_executor.py): subprocess runner · Runs generated code with discovery and timeout handling. · `agents/matlab_executor.py`
- [Debugger](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/agents/debugger.py): agent stage · Repairs code using execution diagnostics. · `agents/debugger.py`

### Artifact Boundaries

Temporary and durable MATLAB output locations.

- [Sandbox](https://github.com/muhammadtalha-prog/matlab-coding-agent/tree/main/sandbox): temporary artifacts · Holds generated code before verification. · `sandbox`
- [Example MATLAB artifact](https://github.com/muhammadtalha-prog/matlab-coding-agent/blob/main/sandbox/moving_average_filter.m): MATLAB source · Example or intermediate generated function. · `sandbox/moving_average_filter.m`
- **Workspace output**: durable artifacts · Task-specific destination for approved verified code.

### Other components

- **User task**: input · Natural-language task from --task or interactive input.
- **Ollama**: external LLM service · Required local OpenAI-compatible model endpoint.
- **MATLAB runtime**: external runtime · Optional verifier; generation continues when unavailable.
- **Human approval**: approval gate · Required before final save when AUTO_APPROVE is disabled.

## Connections

- User task → CLI orchestrator: task input
- Environment template → Runtime config: documents settings
- Runtime config → CLI orchestrator: runtime settings
- Runtime config → LLM client: model settings
- Runtime config → MATLAB executor: MATLAB path and timeout
- CLI orchestrator → LLM client: health check
- LLM client → Ollama: local API
- CLI orchestrator → Planner: request task plan
- Planner → Structured plan: produces
- CLI orchestrator → MATLAB coder: task and plan
- Structured plan → MATLAB coder: implementation design
- MATLAB coder → Sandbox: writes generated .m
- Sandbox → MATLAB executor: unverified source
- MATLAB executor → MATLAB runtime: subprocess execution
- MATLAB executor → CLI orchestrator: execution diagnostics
- CLI orchestrator → Debugger: failure context
- Debugger → Sandbox: repaired code
- Sandbox → Human approval: verified artifact
- Human approval → Workspace output: promote on approval
- Sandbox → Workspace output: promote when auto-approved
- Sandbox → Example MATLAB artifact: contains example
