How muhammadtalha-prog/matlab-coding-agent works
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 CLI orchestrator · sandbox Sandbox · config.py Runtime config · matlab_executor.py MATLAB executor
Components 16
CLI Pipeline
Serial local workflow owned by the CLI.
- CLI orchestrator · Coordinates validation, generation, execution, retries, and promotion.
main.py - Runtime config · Provides model, timeout, MATLAB, retry, and approval settings.
config.py - Environment template · Documents configuration populated through .env.
.env.template
Agent Stages
LLM-backed planning, generation, and repair.
- LLM client · Checks local endpoint and model availability.
agents/llm_client.py - Planner · Produces the structured MATLAB implementation plan.
agents/planner.py - Structured plan · Defines intended function and file design.
- MATLAB coder · Generates .m source from the task and plan.
agents/coder.py - MATLAB executor · Runs generated code with discovery and timeout handling.
agents/matlab_executor.py - Debugger · Repairs code using execution diagnostics.
agents/debugger.py
Artifact Boundaries
Temporary and durable MATLAB output locations.
- Sandbox · Holds generated code before verification.
sandbox - Example MATLAB artifact · Example or intermediate generated function.
sandbox/moving_average_filter.m - Workspace output · Task-specific destination for approved verified code.
Other components
- User task · Natural-language task from --task or interactive input.
- Ollama · Required local OpenAI-compatible model endpoint.
- MATLAB runtime · Optional verifier; generation continues when unavailable.
- Human approval · Required before final save when AUTO_APPROVE is disabled.
Connections 21
- 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